]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
ld/ELF: leave note sections alone for relocatable linking
authorJan Beulich <jbeulich@suse.com>
Fri, 12 Dec 2025 07:02:12 +0000 (08:02 +0100)
committerJan Beulich <jbeulich@suse.com>
Fri, 12 Dec 2025 07:02:12 +0000 (08:02 +0100)
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.

ld/scripttempl/elf.sc

index 6d34912e45d3c0c58db0d195965973978dac529f..7ecf536f06ab89ab14f5b12ccb28180861679e82 100644 (file)
@@ -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
 }