[binutils-gdb] ld/ELF: leave note sections alone for relocatable linking
Jan Beulich
jbeulich@sourceware.org
Fri Dec 12 07:04:57 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8709351936f7308b460b3d417dd1ff2dba4858cd
commit 8709351936f7308b460b3d417dd1ff2dba4858cd
Author: Jan Beulich <jbeulich@suse.com>
Date: Fri Dec 12 08:02:12 2025 +0100
ld/ELF: leave note sections alone for relocatable linking
Commit 023e60ced0c8 ("ld: Move note sections after .rodata section") had a
rather undesirable effect for relocatable links (which typically wouldn't
use a custom linker script): .note.GNU-stack, which isn't even a proper
notes sections (it's SHT_PROGBITS instead, which likely will want
correcting independently), would be moved immediately past .text and
.rodata (and alike), ahead of any custom notes sections. A later final
link, possibly simply combining all .note and .note.* sections, would then
find .note.GNU-stack first, resulting in the output section to also become
SHT_PROGBITS. This way consumers looking for SHT_NOTE wouldn't find the
data they're after.
The goal of mentioning some known .note.* in the linker scripts is to
avoid orphan section diagnostics. That's not typically of interest for
relocatable links, though (people caring about this will want to have
custom scripts anyway, much like they may need to if they had any custom
.note.* sections). Therefore suppress that part of the linker script for
relocatable links.
Diff:
---
ld/scripttempl/elf.sc | 26 ++++++++++++++------------
1 file changed, 14 insertions(+), 12 deletions(-)
diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index 6d34912e45d..7ecf536f06a 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -727,23 +727,25 @@ cat <<EOF
.exception_ranges ${RELOCATING-0} : ONLY_IF_RO { *(.exception_ranges${RELOCATING+*}) }
${TEXT_PLT+${PLT_NEXT_DATA+${PLT} ${OTHER_PLT_SECTIONS}}}
+EOF
+test -n "${RELOCATING}" && cat <<EOF
/* Various note sections. Placed here so that they are always included
in the read-only segment and not treated as orphan sections. The
current orphan handling algorithm does place note sections after R/O
data, but this is not guaranteed to always be the case. */
- .note.build-id : { *(.note.build-id) } ${RELOCATING+${REGION}}
- .note.GNU-stack : { *(.note.GNU-stack) } ${RELOCATING+${REGION}}
- .note.gnu.property : { *(.note.gnu.property) } ${RELOCATING+${REGION}}
- .note.ABI-tag : { *(.note.ABI-tag) } ${RELOCATING+${REGION}}
- .note.package : { *(.note.package) } ${RELOCATING+${REGION}}
- .note.dlopen : { *(.note.dlopen) } ${RELOCATING+${REGION}}
- .note.netbsd.ident : { *(.note.netbsd.ident) } ${RELOCATING+${REGION}}
- .note.openbsd.ident : { *(.note.openbsd.ident) } ${RELOCATING+${REGION}}
-
- ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (__${ETEXT_NAME} = .);}}
- ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (_${ETEXT_NAME} = .);}}
- ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (${ETEXT_NAME} = .);}}
+ .note.build-id : { *(.note.build-id) } ${REGION}
+ .note.GNU-stack : { *(.note.GNU-stack) } ${REGION}
+ .note.gnu.property : { *(.note.gnu.property) } ${REGION}
+ .note.ABI-tag : { *(.note.ABI-tag) } ${REGION}
+ .note.package : { *(.note.package) } ${REGION}
+ .note.dlopen : { *(.note.dlopen) } ${REGION}
+ .note.netbsd.ident : { *(.note.netbsd.ident) } ${REGION}
+ .note.openbsd.ident : { *(.note.openbsd.ident) } ${REGION}
+
+ ${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (__${ETEXT_NAME} = .);}
+ ${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (_${ETEXT_NAME} = .);}
+ ${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (${ETEXT_NAME} = .);}
EOF
}
More information about the Binutils-cvs
mailing list