aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Modra <amodra@gmail.com>2024-12-23 10:26:02 +1030
committerAlan Modra <amodra@gmail.com>2024-12-24 14:54:19 +1030
commitbbc969306f8d5fb6c7b636e25f6f8e278946ef23 (patch)
tree95d9b608fdb0ba2da904411628681ea5e3e87f88
parentc8fcfa9aad211dfd8296ae6e978a356797936c61 (diff)
downloadbinutils-bbc969306f8d5fb6c7b636e25f6f8e278946ef23.zip
binutils-bbc969306f8d5fb6c7b636e25f6f8e278946ef23.tar.gz
binutils-bbc969306f8d5fb6c7b636e25f6f8e278946ef23.tar.bz2
PR 32324, Stripping BOLT'ed binaries leads to unwanted behaviour
This patch corrects layout for a PT_LOAD header that doesn't include the ELF file header but does contain PHDRs and sections requiring alignment. The required alignment (which was missing) is placed before the PHDRs.
-rw-r--r--bfd/elf.c10
-rw-r--r--ld/testsuite/ld-elf/size-2.d4
2 files changed, 7 insertions, 7 deletions
diff --git a/bfd/elf.c b/bfd/elf.c
index 7839431..9f5ac63 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -5978,11 +5978,7 @@ assign_file_positions_for_load_sections (bfd *abfd,
p->p_align = 1 << bed->s->log_file_align;
if (m == phdr_load_seg)
- {
- if (!m->includes_filehdr)
- p->p_offset = off;
- off += actual * bed->s->sizeof_phdr;
- }
+ off += actual * bed->s->sizeof_phdr;
no_contents = false;
off_adjust = 0;
@@ -6131,6 +6127,7 @@ assign_file_positions_for_load_sections (bfd *abfd,
{
if (p->p_type == PT_LOAD)
{
+ p->p_offset = off - actual * bed->s->sizeof_phdr;
elf_elfheader (abfd)->e_phoff = p->p_offset;
if (m->count > 0)
{
@@ -6141,6 +6138,9 @@ assign_file_positions_for_load_sections (bfd *abfd,
}
else if (phdr_load_seg != NULL)
{
+ /* Also set PT_PHDR to match phdr_load_seg. We've
+ sorted segments so that phdr_load_seg will
+ already be set by the code immediately above. */
Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx;
bfd_vma phdr_off = 0; /* Octets. */
if (phdr_load_seg->includes_filehdr)
diff --git a/ld/testsuite/ld-elf/size-2.d b/ld/testsuite/ld-elf/size-2.d
index 9f1a9cf..e3e28cf 100644
--- a/ld/testsuite/ld-elf/size-2.d
+++ b/ld/testsuite/ld-elf/size-2.d
@@ -13,8 +13,8 @@
.* \.tbss +NOBITS +0+130 [0-9a-f]+ 0+30 00 WAT .*
.* \.map +PROGBITS +0+130 [0-9a-f]+ 0+c 00 +A .*
#...
- +PHDR +(0x0+40 0x0+40 0x0+40 0x0+a8 0x0+a8|0x0+34 0x0+34 0x0+34 0x0+60 0x0+60|0x0+34 0x0+a0 0x0+a0 0x0+60 0x0+60) R .*
- +LOAD +(0x0+40 0x0+40 0x0+40 0x0+fc 0x0+fc|0x0+34 0x0+34 0x0+34 0x0+1(08|10) 0x0+1(08|10)|0x0+34 0x0+a0 0x0+a0 0x0+9c 0x0+9c) R E .*
+ +PHDR +(0x0+58 0x0+58 0x0+58 0x0+a8 0x0+a8|0x0+(a0|34) 0x0+a0 0x0+a0 0x0+60 0x0+60) R .*
+ +LOAD +(0x0+58 0x0+58 0x0+58 0x0+e4 0x0+e4|0x0+(a0|34) 0x0+a0 0x0+a0 0x0+(9c|a0) 0x0+(9c|a0)) R E .*
+TLS +0x0+(110|a4) 0x0+110 0x0+110 0x0+20 0x0+50 R .*
#...
.* \.text \.tdata \.map