[PATCH 2/7] lds: implement is_barebox_rodata

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Nov 25 07:29:22 PST 2024


Linux defines is_kernel_rodata(), which is mainly used to implement
kstrdup_const/kfree_const, which are useful to avoid duplication of
strings that already have static storage duration.

Implement the same for barebox on all architectures.

Note that this is of limited use on sandbox, because we don't define all
of the linker script ourselves, but this only means that we will have
some unneeded allocations there, which is a non-issue.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 arch/arm/lib/pbl.lds.S                   |  4 +++-
 arch/arm/lib32/barebox.lds.S             |  4 +++-
 arch/arm/lib64/barebox.lds.S             |  4 +++-
 arch/kvx/cpu/barebox.lds.S               |  5 ++++-
 arch/mips/lib/barebox.lds.S              |  4 +++-
 arch/mips/lib/pbl.lds.S                  |  4 +++-
 arch/mips/pbl/zbarebox.lds.S             |  4 +++-
 arch/openrisc/cpu/barebox.lds.S          |  4 +++-
 arch/powerpc/boards/pcm030/barebox.lds.S |  2 ++
 arch/powerpc/mach-mpc85xx/barebox.lds.S  |  2 ++
 arch/riscv/lib/barebox.lds.S             |  4 +++-
 arch/riscv/lib/pbl.lds.S                 |  4 +++-
 arch/sandbox/board/barebox.lds.S         |  2 ++
 arch/x86/mach-efi/elf_ia32_efi.lds.S     |  2 ++
 arch/x86/mach-efi/elf_x86_64_efi.lds.S   |  2 ++
 include/asm-generic/sections.h           | 18 ++++++++++++++++++
 16 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/arch/arm/lib/pbl.lds.S b/arch/arm/lib/pbl.lds.S
index f097ac1fdd46..dad37c9e9bca 100644
--- a/arch/arm/lib/pbl.lds.S
+++ b/arch/arm/lib/pbl.lds.S
@@ -62,6 +62,7 @@ SECTIONS
 	BAREBOX_PBL_SIZE
 
 	. = ALIGN(ASM_SZPTR);
+	__start_rodata = .;
 	__pbl_board_stack_top = .;
 	.rodata.pbl_board_stack_top : {
 		*(.pbl_board_stack_top_*)
@@ -76,7 +77,8 @@ SECTIONS
 	.barebox_imd : { BAREBOX_IMD }
 
 	. = ALIGN(PBL_SEGMENT_ALIGN);
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 	_sdata = .;
 
 	. = ALIGN(4);
diff --git a/arch/arm/lib32/barebox.lds.S b/arch/arm/lib32/barebox.lds.S
index 97f41f72fef7..a52556a35696 100644
--- a/arch/arm/lib32/barebox.lds.S
+++ b/arch/arm/lib32/barebox.lds.S
@@ -31,6 +31,7 @@ SECTIONS
 	BAREBOX_BARE_INIT_SIZE
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : {
 		*(.rodata*)
 		RO_DATA_SECTION
@@ -52,7 +53,8 @@ SECTIONS
 		__stop_unwind_tab = .;
 	}
 #endif
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 	_sdata = .;
 
 	. = ALIGN(4);
diff --git a/arch/arm/lib64/barebox.lds.S b/arch/arm/lib64/barebox.lds.S
index 1d0cc6dc543e..50e4b6f42cb8 100644
--- a/arch/arm/lib64/barebox.lds.S
+++ b/arch/arm/lib64/barebox.lds.S
@@ -29,12 +29,14 @@ SECTIONS
 	BAREBOX_BARE_INIT_SIZE
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : {
 		*(.rodata*)
 		RO_DATA_SECTION
 	}
 
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 	_sdata = .;
 
 	. = ALIGN(4);
diff --git a/arch/kvx/cpu/barebox.lds.S b/arch/kvx/cpu/barebox.lds.S
index 1edaeae18824..b05c1f36e595 100644
--- a/arch/kvx/cpu/barebox.lds.S
+++ b/arch/kvx/cpu/barebox.lds.S
@@ -45,13 +45,16 @@ SECTIONS
 		KEEP(*(.exception.syscall));
 	}
 
+	__start_rodata = .;
+
 	.rodata ALIGN(8) : {
 		*(.rodata*)
 		. = ALIGN(8);
 		RO_DATA_SECTION
 	}
 
-	_etext = .; /* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 
 	.data ALIGN(4): {
 		sdata = .;
diff --git a/arch/mips/lib/barebox.lds.S b/arch/mips/lib/barebox.lds.S
index 0720f9295dff..bbb9b114993d 100644
--- a/arch/mips/lib/barebox.lds.S
+++ b/arch/mips/lib/barebox.lds.S
@@ -29,12 +29,14 @@ SECTIONS
 	PRE_IMAGE
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : {
 		*(.rodata*)
 		RO_DATA_SECTION
 	}
 
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 	_sdata = .;
 
 	. = ALIGN(4);
diff --git a/arch/mips/lib/pbl.lds.S b/arch/mips/lib/pbl.lds.S
index 4cf0398f33d1..cca980c35fdf 100644
--- a/arch/mips/lib/pbl.lds.S
+++ b/arch/mips/lib/pbl.lds.S
@@ -32,11 +32,13 @@ SECTIONS
 	BAREBOX_BARE_INIT_SIZE
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : { *(.rodata*) }
 
 	.barebox_imd : { BAREBOX_IMD }
 
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 
 	. = ALIGN(4);
 	.data : { *(.data*) }
diff --git a/arch/mips/pbl/zbarebox.lds.S b/arch/mips/pbl/zbarebox.lds.S
index e3114dfe2230..97d77229b6fb 100644
--- a/arch/mips/pbl/zbarebox.lds.S
+++ b/arch/mips/pbl/zbarebox.lds.S
@@ -29,11 +29,13 @@ SECTIONS
 	BAREBOX_BARE_INIT_SIZE
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : { *(.rodata*) }
 
 	.barebox_imd : { BAREBOX_IMD }
 
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 
 	. = ALIGN(4);
 	.data : { *(.data*) }
diff --git a/arch/openrisc/cpu/barebox.lds.S b/arch/openrisc/cpu/barebox.lds.S
index 3fae1af37594..26bb622edc27 100644
--- a/arch/openrisc/cpu/barebox.lds.S
+++ b/arch/openrisc/cpu/barebox.lds.S
@@ -46,6 +46,7 @@ SECTIONS
 	}  > ram
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : {
 		*(.rodata);
 		*(.rodata.*)
@@ -53,7 +54,8 @@ SECTIONS
 		RO_DATA_SECTION
 	} > ram
 
-	__etext = .; /* End of text and rodata section */
+	__end_rodata = .;
+	__etext = .;
 
 	. = ALIGN(4);
 	.data : {
diff --git a/arch/powerpc/boards/pcm030/barebox.lds.S b/arch/powerpc/boards/pcm030/barebox.lds.S
index 146b63fe8889..07c2a671a8a9 100644
--- a/arch/powerpc/boards/pcm030/barebox.lds.S
+++ b/arch/powerpc/boards/pcm030/barebox.lds.S
@@ -32,6 +32,7 @@ SECTIONS
     *(.text*)
     *(.got1*)
     . = ALIGN(16);
+    __start_rodata = .;
     *(.rodata*)
     *(.rodata1*)
     *(.rodata.str1.4)
@@ -70,6 +71,7 @@ SECTIONS
 
   /* Read-write section, merged into data segment: */
   . = (. + 0x0FFF) & 0xFFFFF000;
+  __end_rodata = .;
   _etext = .;
   PROVIDE (erotext = .);
   _sdata = .;
diff --git a/arch/powerpc/mach-mpc85xx/barebox.lds.S b/arch/powerpc/mach-mpc85xx/barebox.lds.S
index 6e348d4db363..5407ecc7e295 100644
--- a/arch/powerpc/mach-mpc85xx/barebox.lds.S
+++ b/arch/powerpc/mach-mpc85xx/barebox.lds.S
@@ -68,6 +68,7 @@ SECTIONS
     PROVIDE (etext = .);
     _sdata = .;
 
+    __start_rodata = .;
    .barebox_imd : { BAREBOX_IMD }
 
    .rodata    :
@@ -79,6 +80,7 @@ SECTIONS
   /* Read-write section, merged into data segment: */
   . = (. + 0x00FF) & 0xFFFFFF00;
 
+  __end_rodata = .;
   _erotext = .;
   PROVIDE (erotext = .);
   .reloc   :
diff --git a/arch/riscv/lib/barebox.lds.S b/arch/riscv/lib/barebox.lds.S
index 101615ab052a..2d220e48142b 100644
--- a/arch/riscv/lib/barebox.lds.S
+++ b/arch/riscv/lib/barebox.lds.S
@@ -39,12 +39,14 @@ SECTIONS
 	BAREBOX_BARE_INIT_SIZE
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : {
 		*(.rodata*)
 		RO_DATA_SECTION
 	}
 
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 	_sdata = .;
 
 	. = ALIGN(4);
diff --git a/arch/riscv/lib/pbl.lds.S b/arch/riscv/lib/pbl.lds.S
index 0fe7dfda8eb4..3a37d01475b2 100644
--- a/arch/riscv/lib/pbl.lds.S
+++ b/arch/riscv/lib/pbl.lds.S
@@ -29,11 +29,13 @@ SECTIONS
 	BAREBOX_PBL_SIZE
 
 	. = ALIGN(4);
+	__start_rodata = .;
 	.rodata : { *(.rodata*) }
 
 	.barebox_imd : { BAREBOX_IMD }
 
-	_etext = .;			/* End of text and rodata section */
+	__end_rodata = .;
+	_etext = .;
 
 	.data : { *(.data*) }
 
diff --git a/arch/sandbox/board/barebox.lds.S b/arch/sandbox/board/barebox.lds.S
index 0102b9333174..60bd9a533ce6 100644
--- a/arch/sandbox/board/barebox.lds.S
+++ b/arch/sandbox/board/barebox.lds.S
@@ -5,9 +5,11 @@
 SECTIONS
 {
 	. = ALIGN(64);
+	__start_rodata = .;
 	.barebox_rodata () : {
 		RO_DATA_SECTION
 	}
+	__end_rodata = .;
 }
 
 INSERT BEFORE .data;
diff --git a/arch/x86/mach-efi/elf_ia32_efi.lds.S b/arch/x86/mach-efi/elf_ia32_efi.lds.S
index 4b1300724300..0a3b6ef947b9 100644
--- a/arch/x86/mach-efi/elf_ia32_efi.lds.S
+++ b/arch/x86/mach-efi/elf_ia32_efi.lds.S
@@ -36,8 +36,10 @@ SECTIONS
 	_sdata = .;
 
 	.data : {
+		__start_rodata = .;
 		*(.rodata*)
 		RO_DATA_SECTION
+		__end_rodata = .;
 		*(.data)
 		*(.data1)
 		*(.data.*)
diff --git a/arch/x86/mach-efi/elf_x86_64_efi.lds.S b/arch/x86/mach-efi/elf_x86_64_efi.lds.S
index cb6a31101eab..ed2ddd52482a 100644
--- a/arch/x86/mach-efi/elf_x86_64_efi.lds.S
+++ b/arch/x86/mach-efi/elf_x86_64_efi.lds.S
@@ -39,8 +39,10 @@ SECTIONS
 	_sdata = .;
 
 	.data : {
+		__start_rodata = .;
 		*(.rodata*)
 		RO_DATA_SECTION
+		__end_rodata = .;
 		*(.got.plt)
 		*(.got)
 		*(.data*)
diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 0b2ed5615bd6..35b0dd8cac1a 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -3,7 +3,10 @@
 #ifndef _ASM_GENERIC_SECTIONS_H_
 #define _ASM_GENERIC_SECTIONS_H_
 
+#include <linux/types.h>
+
 extern char _text[], _stext[], _etext[];
+extern char __start_rodata[], __end_rodata[];
 extern char __bss_start[], __bss_stop[];
 extern char _sdata[], _edata[];
 extern char __bare_init_start[], __bare_init_end[];
@@ -21,4 +24,19 @@ extern char __ctors_start[], __ctors_end[];
 #define barebox_image_size	(__image_end - __image_start)
 #define barebox_bare_init_size	(unsigned int)&_barebox_bare_init_size
 #define barebox_pbl_size	(__piggydata_start - __image_start)
+
+/**
+ * is_barebox_rodata - checks if the pointer address is located in the
+ *                    .rodata section
+ *
+ * @addr: address to check
+ *
+ * Returns: true if the address is located in .rodata, false otherwise.
+ */
+static inline bool is_barebox_rodata(unsigned long addr)
+{
+	return addr >= (unsigned long)__start_rodata &&
+	       addr < (unsigned long)__end_rodata;
+}
+
 #endif /* _ASM_GENERIC_SECTIONS_H_ */
-- 
2.39.5




More information about the barebox mailing list