[binutils-gdb] PR 32324, Stripping BOLT'ed binaries leads to unwanted behaviour

Alan Modra amodra@sourceware.org
Tue Dec 24 04:29:34 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=bbc969306f8d5fb6c7b636e25f6f8e278946ef23

commit bbc969306f8d5fb6c7b636e25f6f8e278946ef23
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Dec 23 10:26:02 2024 +1030

    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.

Diff:
---
 bfd/elf.c                    | 10 +++++-----
 ld/testsuite/ld-elf/size-2.d |  4 ++--
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 78394319bf0..9f5ac6384f3 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 9f1a9cf48fa..e3e28cf7a93 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


More information about the Binutils-cvs mailing list