public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: "H.J. Lu" <hjl.tools@gmail.com>, libc-alpha@sourceware.org
Cc: Florian Weimer <fweimer@redhat.com>
Subject: Re: [PATCH v8 1/4] elf: Properly align all PT_LOAD segments [BZ #28676]
Date: Fri, 21 Jan 2022 15:26:36 -0300	[thread overview]
Message-ID: <b5fcd4ed-f85e-9d47-2137-a307fb69bd1e@linaro.org> (raw)
In-Reply-To: <20220118215731.1320930-2-hjl.tools@gmail.com>



On 18/01/2022 18:57, H.J. Lu wrote:
> Linker may set p_align of a PT_LOAD segment larger than p_align of the
> first PT_LOAD segment to satisfy a section alignment:
> 
> Elf file type is DYN (Shared object file)
> Entry point 0x0
> There are 10 program headers, starting at offset 64
> 
> Program Headers:
>   Type           Offset             VirtAddr           PhysAddr
>                  FileSiz            MemSiz              Flags  Align
>   LOAD           0x0000000000000000 0x0000000000000000 0x0000000000000000
>                  0x0000000000000834 0x0000000000000834  R E    0x1000
>   LOAD           0x0000000000000e00 0x0000000000001e00 0x0000000000001e00
>                  0x0000000000000230 0x0000000000000230  RW     0x1000
>   LOAD           0x0000000000400000 0x0000000000400000 0x0000000000400000
>                  0x0000000000000004 0x0000000000000008  RW     0x400000
> ...
> 
>  Section to Segment mapping:
>   Segment Sections...
>    00     .note.gnu.property .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame
>    01     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt
>    02     .data .bss
> 
> We should align the first PT_LOAD segment to the maximum p_align of all
> PT_LOAD segments, similar to the kernel commit:
> 
> commit ce81bb256a224259ab686742a6284930cbe4f1fa
> Author: Chris Kennelly <ckennelly@google.com>
> Date:   Thu Oct 15 20:12:32 2020 -0700
> 
>     fs/binfmt_elf: use PT_LOAD p_align values for suitable start address

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  elf/dl-load.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index 12744caf91..5b0ff41ee1 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -1101,6 +1101,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>      size_t nloadcmds = 0;
>      bool has_holes = false;
>      bool empty_dynamic = false;
> +    ElfW(Addr) p_align_max = 0;
>  
>      /* The struct is initialized to zero so this is not necessary:
>      l->l_ld = 0;
> @@ -1146,7 +1147,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>  	  c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize));
>  	  c->dataend = ph->p_vaddr + ph->p_filesz;
>  	  c->allocend = ph->p_vaddr + ph->p_memsz;
> -	  c->mapalign = ph->p_align;
> +	  /* Remember the maximum p_align.  */
> +	  if (powerof2 (ph->p_align) && ph->p_align > p_align_max)
> +	    p_align_max = ph->p_align;
>  	  c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize));
>  
>  	  /* Determine whether there is a gap between the last segment
> @@ -1221,6 +1224,10 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>  	goto lose;
>        }
>  
> +    /* Align all PT_LOAD segments to the maximum p_align.  */
> +    for (size_t i = 0; i < nloadcmds; i++)
> +      loadcmds[i].mapalign = p_align_max;
> +
>      /* dlopen of an executable is not valid because it is not possible
>         to perform proper relocations, handle static TLS, or run the
>         ELF constructors.  For PIE, the check needs the dynamic

  reply	other threads:[~2022-01-21 18:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-18 21:57 [PATCH v8 0/4] Properly align all PT_LOAD segments with tests H.J. Lu
2022-01-18 21:57 ` [PATCH v8 1/4] elf: Properly align all PT_LOAD segments [BZ #28676] H.J. Lu
2022-01-21 18:26   ` Adhemerval Zanella [this message]
2022-01-18 21:57 ` [PATCH v8 2/4] elf: Add a test for PT_LOAD segments with mixed p_align " H.J. Lu
2022-01-21 20:00   ` Adhemerval Zanella
2022-01-21 21:13     ` H.J. Lu
2022-01-18 21:57 ` [PATCH v8 3/4] elf: Add a test for PT_LOAD segments with p_align == 1 [BZ #28688] H.J. Lu
2022-01-18 21:57 ` [PATCH v8 4/4] elf: Add a test for PT_LOAD segments with invalid p_align " H.J. Lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=b5fcd4ed-f85e-9d47-2137-a307fb69bd1e@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fweimer@redhat.com \
    --cc=hjl.tools@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).