meaningless p_offset for zero p_filesz PT_LOAD

Message ID Z813o_k_loiKCw-K@squeak.grove.modra.org
State New
Headers
Series meaningless p_offset for zero p_filesz PT_LOAD |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

Alan Modra March 9, 2025, 11:12 a.m. UTC
  Maybe I should have stayed with WONTFIX for pr25237..

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.
  

Comments

H.J. Lu March 9, 2025, 11:22 p.m. UTC | #1
On Sun, Mar 9, 2025 at 4:14 AM Alan Modra <amodra@gmail.com> wrote:
>
> Maybe I should have stayed with WONTFIX for pr25237..
>
> 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 --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;

I think your original patch makes sense since it is odd
for p_offset > file size even if it isn't used.

>                 }
>             }
>           else
>
> --
> Alan Modra
  

Patch

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