[binutils-gdb] meaningless p_offset for zero p_filesz PT_LOAD

Alan Modra amodra@sourceware.org
Mon Mar 10 12:56:01 GMT 2025


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

commit ebed0bfecc7f482a2c284947527427625ee42c1a
Author: Alan Modra <amodra@gmail.com>
Date:   Mon Mar 10 23:02:19 2025 +1030

    meaningless p_offset for zero p_filesz PT_LOAD
    
    This patch avoids generating PT_LOAD segments that trip a bug in
    glibc's loader.
    
            PR 25237
            PR 32763
            * elf.c (assign_file_positions_for_load_sections): Don't put
            p_offset zero for empty PT_LOAD.

Diff:
---
 bfd/elf.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/bfd/elf.c b/bfd/elf.c
index 20f96e689ce..ee89dd909da 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -6170,7 +6170,10 @@ assign_file_positions_for_load_sections (bfd *abfd,
 		    align = p->p_align;
 		  if (align < 1)
 		    align = 1;
-		  p->p_offset = off % align;
+		  /* Avoid p_offset of zero, which might be wrongly
+		     interpreted as the segment being the first one,
+		     containing the file header.  PR32763.  */
+		  p->p_offset = (off + align - 1) % align + 1;
 		}
 	    }
 	  else


More information about the Binutils-cvs mailing list