[v1] elf: Replace `strcpy` call with `memcpy` [BZ #29454]

Message ID 20220808032622.3804534-2-goldstein.w.n@gmail.com (mailing list archive)
State Committed
Commit 483cfe1a6a33d6335b1901581b41040d2d412511
Headers
Series [v1] elf: Replace `strcpy` call with `memcpy` [BZ #29454] |

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

Noah Goldstein Aug. 8, 2022, 3:26 a.m. UTC
  GCC normally does this optimization for us in
strlen_pass::handle_builtin_strcpy but only for optimized
build. To avoid needing to include strcpy.S in the rtld build to
support the debug build, just do the optimization by hand.
---
 elf/dl-cache.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Yann Droneaud Aug. 8, 2022, 10:19 a.m. UTC | #1
Hi,


Le 08/08/2022 à 05:26, Noah Goldstein via Libc-alpha a écrit :
> GCC normally does this optimization for us in
> strlen_pass::handle_builtin_strcpy but only for optimized
> build. To avoid needing to include strcpy.S in the rtld build to
> support the debug build, just do the optimization by hand.

It does fixes this build issue I had when using -Og (using gcc 11.2, on amd64):

     gcc   -nostdlib -nostartfiles -shared -o .../build/elf/ld.so.new        \
           -Wl,-z,relro -Wl,-z,defs     \
           -Wl,-z,pack-relative-relocs \
           .../build/elf/librtld.os -Wl,--version-script=.../build/ld.map        \
           -Wl,-soname=ld-linux-x86-64.so.2
     /usr/bin/ld: .../build/elf/librtld.os: in function `_dl_load_cache_lookup':
     .../elf/dl-cache.c:513: undefined reference to `strcpy'
     collect2: error: ld returned 1 exit status
     make[2]: *** [Makefile:1347: .../build/elf/ld.so] Error 1
     make[2]: Leaving directory '.../elf'
     make[1]: *** [Makefile:484: elf/subdir_lib] Error 2
     make[1]: Leaving directory '...'
     make: *** [Makefile:9: all] Error 2

Thanks.

> ---
>   elf/dl-cache.c | 5 +++--
>   1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/elf/dl-cache.c b/elf/dl-cache.c
> index 8bbf110d02..b97c17b3a9 100644
> --- a/elf/dl-cache.c
> +++ b/elf/dl-cache.c
> @@ -509,8 +509,9 @@ _dl_load_cache_lookup (const char *name)
>        we are accessing. Therefore we must make the copy of the
>        mapping data without using malloc.  */
>     char *temp;
> -  temp = alloca (strlen (best) + 1);
> -  strcpy (temp, best);
> +  size_t best_len = strlen (best) + 1;
> +  temp = alloca (best_len);
> +  memcpy (temp, best, best_len);
>     return __strdup (temp);
>   }
>   


Regards.
  
H.J. Lu Aug. 8, 2022, 2:23 p.m. UTC | #2
On Sun, Aug 7, 2022 at 8:26 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
>
> GCC normally does this optimization for us in
> strlen_pass::handle_builtin_strcpy but only for optimized
> build. To avoid needing to include strcpy.S in the rtld build to
> support the debug build, just do the optimization by hand.
> ---
>  elf/dl-cache.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/elf/dl-cache.c b/elf/dl-cache.c
> index 8bbf110d02..b97c17b3a9 100644
> --- a/elf/dl-cache.c
> +++ b/elf/dl-cache.c
> @@ -509,8 +509,9 @@ _dl_load_cache_lookup (const char *name)
>       we are accessing. Therefore we must make the copy of the
>       mapping data without using malloc.  */
>    char *temp;
> -  temp = alloca (strlen (best) + 1);
> -  strcpy (temp, best);
> +  size_t best_len = strlen (best) + 1;
> +  temp = alloca (best_len);
> +  memcpy (temp, best, best_len);
>    return __strdup (temp);
>  }
>
> --
> 2.34.1
>

LGTM.

Thanks.
  
Noah Goldstein Aug. 10, 2022, 3:29 a.m. UTC | #3
On Mon, Aug 8, 2022 at 10:24 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On Sun, Aug 7, 2022 at 8:26 PM Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> >
> > GCC normally does this optimization for us in
> > strlen_pass::handle_builtin_strcpy but only for optimized
> > build. To avoid needing to include strcpy.S in the rtld build to
> > support the debug build, just do the optimization by hand.
> > ---
> >  elf/dl-cache.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> >
> > diff --git a/elf/dl-cache.c b/elf/dl-cache.c
> > index 8bbf110d02..b97c17b3a9 100644
> > --- a/elf/dl-cache.c
> > +++ b/elf/dl-cache.c
> > @@ -509,8 +509,9 @@ _dl_load_cache_lookup (const char *name)
> >       we are accessing. Therefore we must make the copy of the
> >       mapping data without using malloc.  */
> >    char *temp;
> > -  temp = alloca (strlen (best) + 1);
> > -  strcpy (temp, best);
> > +  size_t best_len = strlen (best) + 1;
> > +  temp = alloca (best_len);
> > +  memcpy (temp, best, best_len);
> >    return __strdup (temp);
> >  }
> >
> > --
> > 2.34.1
> >
>
> LGTM.
>
> Thanks.
>
> --
> H.J.

Any objections to backporting this to 2.36?
  

Patch

diff --git a/elf/dl-cache.c b/elf/dl-cache.c
index 8bbf110d02..b97c17b3a9 100644
--- a/elf/dl-cache.c
+++ b/elf/dl-cache.c
@@ -509,8 +509,9 @@  _dl_load_cache_lookup (const char *name)
      we are accessing. Therefore we must make the copy of the
      mapping data without using malloc.  */
   char *temp;
-  temp = alloca (strlen (best) + 1);
-  strcpy (temp, best);
+  size_t best_len = strlen (best) + 1;
+  temp = alloca (best_len);
+  memcpy (temp, best, best_len);
   return __strdup (temp);
 }