elf: Copy l_addr/l_ld when adding ld.so to a new namespace

Message ID 20210818022847.2041537-1-hjl.tools@gmail.com
State Committed
Delegated to: Florian Weimer
Headers
Series elf: Copy l_addr/l_ld when adding ld.so to a new namespace |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent
dj/TryBot-32bit success Build for i686

Commit Message

H.J. Lu Aug. 18, 2021, 2:28 a.m. UTC
  When add ld.so to a new namespace, we don't actually load ld.so.  We
create a new link map and refers the real one for almost everything.
Copy l_addr and l_ld from the real ld.so link map to avoid GDB warning:

warning: .dynamic section for ".../elf/ld-linux-x86-64.so.2" is not at the expected address (wrong library or version mismatch?)

when handling shared library loaded by dlmopen.
---
 elf/dl-load.c | 4 ++++
 1 file changed, 4 insertions(+)
  

Comments

H.J. Lu Sept. 20, 2021, 4:52 p.m. UTC | #1
On Tue, Aug 17, 2021 at 7:28 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> When add ld.so to a new namespace, we don't actually load ld.so.  We
> create a new link map and refers the real one for almost everything.
> Copy l_addr and l_ld from the real ld.so link map to avoid GDB warning:
>
> warning: .dynamic section for ".../elf/ld-linux-x86-64.so.2" is not at the expected address (wrong library or version mismatch?)
>
> when handling shared library loaded by dlmopen.
> ---
>  elf/dl-load.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index cb0618b7fc..aab290bea6 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -1026,6 +1026,10 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>        /* Refer to the real descriptor.  */
>        l->l_real = &GL(dl_rtld_map);
>
> +      /* Also copy l_addr and l_ld.  */
> +      l->l_addr = l->l_real->l_addr;
> +      l->l_ld = l->l_real->l_ld;
> +
>        /* No need to bump the refcount of the real object, ld.so will
>          never be unloaded.  */
>        __close_nocancel (fd);
> --
> 2.31.1
>

PING.
  
H.J. Lu Sept. 29, 2021, 4:59 p.m. UTC | #2
On Mon, Sep 20, 2021 at 9:52 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Tue, Aug 17, 2021 at 7:28 PM H.J. Lu <hjl.tools@gmail.com> wrote:
> >
> > When add ld.so to a new namespace, we don't actually load ld.so.  We
> > create a new link map and refers the real one for almost everything.
> > Copy l_addr and l_ld from the real ld.so link map to avoid GDB warning:
> >
> > warning: .dynamic section for ".../elf/ld-linux-x86-64.so.2" is not at the expected address (wrong library or version mismatch?)
> >
> > when handling shared library loaded by dlmopen.
> > ---
> >  elf/dl-load.c | 4 ++++
> >  1 file changed, 4 insertions(+)
> >
> > diff --git a/elf/dl-load.c b/elf/dl-load.c
> > index cb0618b7fc..aab290bea6 100644
> > --- a/elf/dl-load.c
> > +++ b/elf/dl-load.c
> > @@ -1026,6 +1026,10 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
> >        /* Refer to the real descriptor.  */
> >        l->l_real = &GL(dl_rtld_map);
> >
> > +      /* Also copy l_addr and l_ld.  */
> > +      l->l_addr = l->l_real->l_addr;
> > +      l->l_ld = l->l_real->l_ld;
> > +
> >        /* No need to bump the refcount of the real object, ld.so will
> >          never be unloaded.  */
> >        __close_nocancel (fd);
> > --
> > 2.31.1
> >
>
> PING.
>

Any comments on this patch?
  
Adhemerval Zanella Sept. 29, 2021, 6:57 p.m. UTC | #3
On 17/08/2021 23:28, H.J. Lu via Libc-alpha wrote:
> When add ld.so to a new namespace, we don't actually load ld.so.  We
> create a new link map and refers the real one for almost everything.
> Copy l_addr and l_ld from the real ld.so link map to avoid GDB warning:
> 
> warning: .dynamic section for ".../elf/ld-linux-x86-64.so.2" is not at the expected address (wrong library or version mismatch?)
> 
> when handling shared library loaded by dlmopen.

LGTM, thanks.

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

> ---
>  elf/dl-load.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/elf/dl-load.c b/elf/dl-load.c
> index cb0618b7fc..aab290bea6 100644
> --- a/elf/dl-load.c
> +++ b/elf/dl-load.c
> @@ -1026,6 +1026,10 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
>        /* Refer to the real descriptor.  */
>        l->l_real = &GL(dl_rtld_map);
>  
> +      /* Also copy l_addr and l_ld.  */

This comments seems obvious, maybe add it is required to avoid a
GDB warning with dlmopen();

> +      l->l_addr = l->l_real->l_addr;
> +      l->l_ld = l->l_real->l_ld;
> +
>        /* No need to bump the refcount of the real object, ld.so will
>  	 never be unloaded.  */
>        __close_nocancel (fd);
>
  
H.J. Lu Sept. 29, 2021, 7:38 p.m. UTC | #4
On Wed, Sep 29, 2021 at 11:57 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> On 17/08/2021 23:28, H.J. Lu via Libc-alpha wrote:
> > When add ld.so to a new namespace, we don't actually load ld.so.  We
> > create a new link map and refers the real one for almost everything.
> > Copy l_addr and l_ld from the real ld.so link map to avoid GDB warning:
> >
> > warning: .dynamic section for ".../elf/ld-linux-x86-64.so.2" is not at the expected address (wrong library or version mismatch?)
> >
> > when handling shared library loaded by dlmopen.
>
> LGTM, thanks.
>
> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
>
> > ---
> >  elf/dl-load.c | 4 ++++
> >  1 file changed, 4 insertions(+)
> >
> > diff --git a/elf/dl-load.c b/elf/dl-load.c
> > index cb0618b7fc..aab290bea6 100644
> > --- a/elf/dl-load.c
> > +++ b/elf/dl-load.c
> > @@ -1026,6 +1026,10 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
> >        /* Refer to the real descriptor.  */
> >        l->l_real = &GL(dl_rtld_map);
> >
> > +      /* Also copy l_addr and l_ld.  */
>
> This comments seems obvious, maybe add it is required to avoid a
> GDB warning with dlmopen();

Changed to

/* Copy l_addr and l_ld to avoid a GDB warning with dlmopen().  */

> > +      l->l_addr = l->l_real->l_addr;
> > +      l->l_ld = l->l_real->l_ld;
> > +
> >        /* No need to bump the refcount of the real object, ld.so will
> >        never be unloaded.  */
> >        __close_nocancel (fd);
> >

This is the patch I am checking in.

Thanks.
  

Patch

diff --git a/elf/dl-load.c b/elf/dl-load.c
index cb0618b7fc..aab290bea6 100644
--- a/elf/dl-load.c
+++ b/elf/dl-load.c
@@ -1026,6 +1026,10 @@  _dl_map_object_from_fd (const char *name, const char *origname, int fd,
       /* Refer to the real descriptor.  */
       l->l_real = &GL(dl_rtld_map);
 
+      /* Also copy l_addr and l_ld.  */
+      l->l_addr = l->l_real->l_addr;
+      l->l_ld = l->l_real->l_ld;
+
       /* No need to bump the refcount of the real object, ld.so will
 	 never be unloaded.  */
       __close_nocancel (fd);