From c6abe2a51f2ebb532670d2c891709ffec507b06a Mon Sep 17 00:00:00 2001 From: Wilfred Mallawa Date: Mon, 11 Dec 2023 10:14:36 +1000 Subject: [PATCH] build_scripts/layout.ld: add a relro section This accomadates situations for example: where global variables are initialized to a value that requires relocation. Things like globally stored function pointers, or storing the address of another global variable. Such items require runtime initialization in the form of dynamic relocation, and cannot be placed in a RO segment. However, as it is declared to be a constant (not modified by the program), the dynamic linker can mark it as RO after the dynamic relocation as been applied [1]. When GCC sees a variable which is constant but requires dynamic relocation, it puts it into a section named `.data.rel.ro`, further, a variable that requires dynamic relocation against a local symbol is put into a `.data.rel.ro.local` section, this helps group such variables together so that the dynamic linker may apply the relocations, which will always be RELATIVE locations [1]. [1] https://www.airs.com/blog/archives/189 Signed-off-by: Wilfred Mallawa --- build_scripts/libtock_layout.ld | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/build_scripts/libtock_layout.ld b/build_scripts/libtock_layout.ld index e9c68c68..14029fd8 100644 --- a/build_scripts/libtock_layout.ld +++ b/build_scripts/libtock_layout.ld @@ -114,6 +114,11 @@ SECTIONS { /* Read-only data section. Contains strings and other global constants. */ .rodata ALIGN(4) : { *(.rodata.*) + + /* Generated by GCC in libraries for variables that still have relocations but are constant + * at runtime. See https://www.airs.com/blog/archives/189 for background. + */ + *(.data.rel.ro.*) } > FLASH /* Sections located in RAM at runtime.