[PATCH] x86: Drop an empty .relr.dyn from segments given by PHDRS
mintsuki
mintsuki@protonmail.com
Sat Nov 14 06:49:45 GMT 2026
With -z pack-relative-relocs, ld reports this when the output needs no
relative relocations and the linker script has PHDRS:
ld: a.out: section `.relr.dyn' can't be allocated in segment 0
LOAD: .text .interp .note.gnu.property .dynsym .dynstr .hash .gnu.hash .relr.dyn .dynamic
_bfd_elf_x86_size_relative_relocs removes the empty .relr.dyn output
section from the section list, but unlike
strip_excluded_output_sections, maybe_strip_output in elf64-ppc.c and
_bfd_elf_final_link, which also remove output sections, it does not
set SEC_EXCLUDE on it. Without PHDRS that does no harm, as the segment
map is built from the section list after the removal. A segment map
from PHDRS is set up by lang_record_phdrs before .relr.dyn is sized,
though, and elf_modify_segment_map only drops excluded sections from
it. The removed section stays in its segment, where
assign_file_positions_for_load_sections reports it, and when it is the
last section there, the segment is extended to its aligned address.
Set SEC_EXCLUDE on it before removing it, as those do.
bfd/
* elfxx-x86.c (_bfd_elf_x86_size_relative_relocs): Set SEC_EXCLUDE
on the empty .relr.dyn output section before removing it.
ld/
* testsuite/ld-i386/dt-relr-3.d: New file.
* testsuite/ld-i386/i386.exp: Run dt-relr-3.
* testsuite/ld-x86-64/dt-relr-3-x32.d: New file.
* testsuite/ld-x86-64/dt-relr-3.d: Likewise.
* testsuite/ld-x86-64/dt-relr-3.t: Likewise.
* testsuite/ld-x86-64/x86-64.exp: Run dt-relr-3 and dt-relr-3-x32.
Generated-By: Claude Opus 5.5 (claude-opus-5-5)
Signed-off-by: Mintsuki <mintsuki@protonmail.com>
---
bfd/elfxx-x86.c | 3 +++
ld/testsuite/ld-i386/dt-relr-3.d | 11 +++++++++++
ld/testsuite/ld-i386/i386.exp | 1 +
ld/testsuite/ld-x86-64/dt-relr-3-x32.d | 11 +++++++++++
ld/testsuite/ld-x86-64/dt-relr-3.d | 11 +++++++++++
ld/testsuite/ld-x86-64/dt-relr-3.t | 10 ++++++++++
ld/testsuite/ld-x86-64/x86-64.exp | 2 ++
7 files changed, 49 insertions(+)
create mode 100644 ld/testsuite/ld-i386/dt-relr-3.d
create mode 100644 ld/testsuite/ld-x86-64/dt-relr-3-x32.d
create mode 100644 ld/testsuite/ld-x86-64/dt-relr-3.d
create mode 100644 ld/testsuite/ld-x86-64/dt-relr-3.t
diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index c42de64cdf..1e9df63345 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -2049,6 +2049,9 @@ _bfd_elf_x86_size_relative_relocs (struct bfd_link_info *info,
/* Remove the empty .relr.dyn sections now. */
if (!bfd_is_abs_section (htab->elf.srelrdyn->output_section))
{
+ /* Exclude it too, so that elf_modify_segment_map drops it
+ from a segment map set up by PHDRS in a linker script. */
+ htab->elf.srelrdyn->output_section->flags |= SEC_EXCLUDE;
bfd_section_list_remove
(info->output_bfd, htab->elf.srelrdyn->output_section);
info->output_bfd->section_count--;
diff --git a/ld/testsuite/ld-i386/dt-relr-3.d b/ld/testsuite/ld-i386/dt-relr-3.d
new file mode 100644
index 0000000000..a33916abee
--- /dev/null
+++ b/ld/testsuite/ld-i386/dt-relr-3.d
@@ -0,0 +1,11 @@
+#source: ../ld-x86-64/simple.s
+#as: --32
+#ld: -pie -melf_i386 $DT_RELR_LDFLAGS -T ../ld-x86-64/dt-relr-3.t
+#readelf: -l --wide
+#target: x86_64-*-linux* i?86-*-linux-gnu i?86-*-gnu*
+
+#...
+ Section to Segment mapping:
+ Segment Sections\.\.\.
+ 00 +\.text .*
+#pass
diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp
index 807bb8e9fc..1c51b36519 100644
--- a/ld/testsuite/ld-i386/i386.exp
+++ b/ld/testsuite/ld-i386/i386.exp
@@ -521,6 +521,7 @@ run_dump_test "dt-relr-1b"
run_dump_test "dt-relr-1c"
run_dump_test "dt-relr-1d"
run_dump_test "dt-relr-2"
+run_dump_test "dt-relr-3"
run_dump_test "pr28870"
run_dump_test "pr28894"
run_dump_test "pr30787"
diff --git a/ld/testsuite/ld-x86-64/dt-relr-3-x32.d b/ld/testsuite/ld-x86-64/dt-relr-3-x32.d
new file mode 100644
index 0000000000..9011a229e5
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/dt-relr-3-x32.d
@@ -0,0 +1,11 @@
+#source: simple.s
+#as: --x32
+#ld: -pie -melf32_x86_64 $DT_RELR_LDFLAGS -T dt-relr-3.t
+#readelf: -l --wide
+#target: x86_64-*-linux*
+
+#...
+ Section to Segment mapping:
+ Segment Sections\.\.\.
+ 00 +\.text .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/dt-relr-3.d b/ld/testsuite/ld-x86-64/dt-relr-3.d
new file mode 100644
index 0000000000..c31efd191f
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/dt-relr-3.d
@@ -0,0 +1,11 @@
+#source: simple.s
+#as: --64
+#ld: -pie -melf_x86_64 $DT_RELR_LDFLAGS -T dt-relr-3.t
+#readelf: -l --wide
+#target: x86_64-*-linux*
+
+#...
+ Section to Segment mapping:
+ Segment Sections\.\.\.
+ 00 +\.text .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/dt-relr-3.t b/ld/testsuite/ld-x86-64/dt-relr-3.t
new file mode 100644
index 0000000000..d9c3cb9382
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/dt-relr-3.t
@@ -0,0 +1,10 @@
+PHDRS
+{
+ text PT_LOAD FILEHDR PHDRS FLAGS (5);
+}
+
+SECTIONS
+{
+ . = SIZEOF_HEADERS;
+ .text : { *(.text) } :text
+}
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index 985d96dcfb..428c16d50e 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -540,6 +540,8 @@ run_dump_test "dt-relr-1d"
run_dump_test "dt-relr-1d-x32"
run_dump_test "dt-relr-2"
run_dump_test "dt-relr-2-x32"
+run_dump_test "dt-relr-3"
+run_dump_test "dt-relr-3-x32"
run_dump_test "pr30787"
run_dump_test "pr31047"
run_dump_test "pr31047-x32"
--
2.56.1
More information about the Binutils
mailing list