From 7ecf0250f7f0868f3b828ffadd47d0e5f2dbd875 Mon Sep 17 00:00:00 2001 From: Nick Clifton Date: Mon, 30 Sep 2024 14:41:11 +0100 Subject: [PATCH] Improve the placement of orphan note sections. PR 32219 --- ld/ldelf.c | 15 +++++++++++++-- ld/ldlang.c | 2 +- ld/scripttempl/arclinux.sc | 1 + ld/scripttempl/elf32cr16.sc | 2 +- ld/scripttempl/elf32crx.sc | 2 +- ld/scripttempl/elfd10v.sc | 6 +++++- ld/scripttempl/elfxtensa.sc | 5 +++++ ld/scripttempl/mep.sc | 5 +++++ ld/scripttempl/misc-sections.sc | 28 ++++++++++++++++++++++++++-- ld/scripttempl/nds32elf.sc | 3 ++- ld/scripttempl/v850.sc | 4 ++++ ld/scripttempl/v850_rh850.sc | 4 ++++ ld/testsuite/ld-elf/pr23658-1a.d | 4 +--- ld/testsuite/ld-elf/pr23658-1e.d | 3 ++- 14 files changed, 71 insertions(+), 13 deletions(-) diff --git a/ld/ldelf.c b/ld/ldelf.c index 09691609fc0..2371af38b79 100644 --- a/ld/ldelf.c +++ b/ld/ldelf.c @@ -2118,7 +2118,7 @@ ldelf_place_orphan (asection *s, const char *secname, int constraint) { ".bss", SEC_ALLOC, 0, 0, 0, 0 }, - { 0, + { NULL, SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA, 0, 0, 0, 0 }, { ".interp", @@ -2328,7 +2328,18 @@ ldelf_place_orphan (asection *s, const char *secname, int constraint) && (elfinput ? sh_type == SHT_NOTE : startswith (secname, ".note"))) - place = &hold[orphan_interp]; + { + /* PR 32219: Check that the .interp section + exists before attaching orphans to it. */ + if (lang_output_section_find (hold[orphan_interp].name)) + place = &hold[orphan_interp]; + /* Next best place: after .rodata. */ + else if (lang_output_section_find (hold[orphan_rodata].name)) + place = &hold[orphan_rodata]; + /* Last attempt: the .text section. */ + else + place = &hold[orphan_text]; + } else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0) place = &hold[orphan_bss]; else if ((flags & SEC_SMALL_DATA) != 0) diff --git a/ld/ldlang.c b/ld/ldlang.c index 95d64fd05e2..7f9e3d2b119 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -9012,7 +9012,7 @@ lang_record_phdrs (void) continue; /* Don't add orphans to PT_INTERP header. */ - if (l->type == 3) + if (l->type == PT_INTERP) continue; if (last == NULL) diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc index 36ba5a664d3..245e0208f0b 100644 --- a/ld/scripttempl/arclinux.sc +++ b/ld/scripttempl/arclinux.sc @@ -661,6 +661,7 @@ source_sh $srcdir/scripttempl/DWARF.sc cat <INSN} + .text ${RELOCATING+${TEXT_START_ADDR}} : { ${RELOCATING+${TEXT_START_SYMBOLS} @@ -175,7 +179,7 @@ SECTIONS EOF -source_sh $srcdir/scripttempl/misc-sections.sc +source_sh $srcdir/scripttempl/misc-sections.sc DATA source_sh $srcdir/scripttempl/DWARF.sc cat <