public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: GNU C Library <libc-alpha@sourceware.org>
Subject: Re: [PATCH 2/3] elf: Set l_contiguous to 1 for the main map in more cases
Date: Sat, 15 Jan 2022 16:10:17 -0800	[thread overview]
Message-ID: <CAMe9rOrhEA9zKuG9YoQe7Qxbk18ybZ++vfZAb-XhSneMFTFanQ@mail.gmail.com> (raw)
In-Reply-To: <1c4f55eb03365f9ea7238e7ab4a997802081bf6e.1641228666.git.fweimer@redhat.com>

On Mon, Jan 3, 2022 at 9:12 AM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> l_contiguous was not initialized at all for the main map and
> always 0.  This commit adds code to check if the LOAD segments
> are adjacent to each other, and sets l_contiguous accordingly.
> This helps _dl_find_object because it is more efficient if the
> main mapping is contiguous.
>
> Note that not all (PIE or non-PIE) binaries are contiguous in this
> way because BFD ld creates executables with LOAD holes:
>
> ELF LOAD segments creating holes in the process image on GNU/Linux
> https://sourceware.org/pipermail/binutils/2022-January/119082.html
> https://sourceware.org/bugzilla/show_bug.cgi?id=28743
> ---
>  elf/rtld.c | 25 +++++++++++++++++++++++++
>  1 file changed, 25 insertions(+)
>
> diff --git a/elf/rtld.c b/elf/rtld.c
> index ba6e31377d..53293f5b13 100644
> --- a/elf/rtld.c
> +++ b/elf/rtld.c
> @@ -1144,6 +1144,22 @@ rtld_setup_main_map (struct link_map *main_map)
>    main_map->l_map_start = ~0;
>    /* And it was opened directly.  */
>    ++main_map->l_direct_opencount;
> +  main_map->l_contiguous = 1;
> +
> +  /* A PT_LOAD segment at an unexpected address will clear the
> +     l_contiguous flag.  The ELF specification says that PT_LOAD
> +     segments need to be sorted in in increasing order, but perhaps
> +     not all executables follow this requirement.  Having l_contiguous
> +     equal to 1 is just an optimization, so the code below does not
> +     try to sort the segments in case they are unordered.
> +
> +     There is one corner case in which l_contiguous is not set to 1,
> +     but where it could be set: If a PIE (ET_DYN) binary is loaded by
> +     glibc itself (not the kernel), it is always contiguous due to the
> +     way the glibc loader works.  However, the kernel loader may still
> +     create holes in this case, and the code here still uses 0
> +     conservatively for the glibc-loaded case, too.  */
> +  ElfW(Addr) expected_load_address = 0;
>
>    /* Scan the program header table for the dynamic section.  */
>    for (const ElfW(Phdr) *ph = phdr; ph < &phdr[phnum]; ++ph)
> @@ -1207,12 +1223,21 @@ rtld_setup_main_map (struct link_map *main_map)
>           if (main_map->l_map_start > mapstart)
>             main_map->l_map_start = mapstart;
>
> +         if (main_map->l_contiguous && expected_load_address != 0
> +             && expected_load_address != mapstart)
> +           main_map->l_contiguous = 0;
> +
>           /* Also where it ends.  */
>           allocend = main_map->l_addr + ph->p_vaddr + ph->p_memsz;
>           if (main_map->l_map_end < allocend)
>             main_map->l_map_end = allocend;
>           if ((ph->p_flags & PF_X) && allocend > main_map->l_text_end)
>             main_map->l_text_end = allocend;
> +
> +         /* The next expected address is the page following this load
> +            segment.  */
> +         expected_load_address = ((allocend + GLRO(dl_pagesize) - 1)
> +                                  & ~(GLRO(dl_pagesize) - 1));
>         }
>         break;
>
> --
> 2.33.1
>

LGTM.

Reviewed-by: H.J. Lu <hjl.tools@gmail.com>

Thanks.

-- 
H.J.

  reply	other threads:[~2022-01-16  0:10 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-03 17:11 [PATCH 0/3] Fix elf/tst-dl_find_objects with --enable-hardcoded-path-in-tests Florian Weimer
2022-01-03 17:11 ` [PATCH 1/3] elf: Introduce rtld_setup_main_map Florian Weimer
2022-01-16  0:11   ` H.J. Lu
2022-01-03 17:11 ` [PATCH 2/3] elf: Set l_contiguous to 1 for the main map in more cases Florian Weimer
2022-01-16  0:10   ` H.J. Lu [this message]
2022-01-03 17:11 ` [PATCH 3/3] elf/tst-dl_find_object: Disable subtests for non-contiguous maps (bug 28732) Florian Weimer
2022-01-14 15:06   ` H.J. Lu
2022-01-14 15:09     ` H.J. Lu
2022-01-14 15:10     ` Florian Weimer
2022-01-14 15:19       ` H.J. Lu
2022-01-14 15:39         ` Florian Weimer
2022-01-14 15:47           ` H.J. Lu
2022-01-14 15:51             ` Florian Weimer
2022-01-14 15:54               ` H.J. Lu
2022-01-14 15:56                 ` Florian Weimer
2022-01-14 16:06                   ` H.J. Lu
2022-01-14 16:12                     ` Florian Weimer
2022-01-16 14:05   ` 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=CAMe9rOrhEA9zKuG9YoQe7Qxbk18ybZ++vfZAb-XhSneMFTFanQ@mail.gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=fweimer@redhat.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).