]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
PR 32324, Stripping BOLT'ed binaries leads to unwanted behaviour
authorAlan Modra <amodra@gmail.com>
Sun, 22 Dec 2024 23:56:02 +0000 (10:26 +1030)
committerAlan Modra <amodra@gmail.com>
Tue, 24 Dec 2024 04:24:19 +0000 (14:54 +1030)
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.

bfd/elf.c
ld/testsuite/ld-elf/size-2.d

index 78394319bf0075f1dcf659830f749420048d2383..9f5ac6384f3b921f368443c2344b7fbcb76a9c44 100644 (file)
--- 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)
index 9f1a9cf48fa192bda776306154f7275662672372..e3e28cf7a936e7010ef7ead9219534ddf4518636 100644 (file)
@@ -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