diff mbox series

[uclibc-ng-devel,v2] static pie: fix building static PDE

Message ID 20220902104149.286116-1-jcmvbkbc@gmail.com
State Superseded
Headers show
Series [uclibc-ng-devel,v2] static pie: fix building static PDE | expand

Commit Message

Max Filippov Sept. 2, 2022, 10:41 a.m. UTC
When uclibc is built with static PIE support the _dl_load_base variable
shared between the libc-tls.c and reloc_static_pie.c creates the
dependency that requires linking reloc_static_pie.o into static
position-dependent executables resulting in the following build errors:
  gcc -static test.c -o test
  ...ld:
  ...usr/lib/libc.a(reloc_static_pie.os):(.text+0x0):
	undefined reference to `_DYNAMIC'

Move _dl_load_base definition to libc-tls.c to resolve this dependency
and fix static PDE build.

Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
---
Changes v1->v2:
- make _dl_load_base definition conditional

 libc/misc/internals/reloc_static_pie.c     | 2 +-
 libpthread/nptl/sysdeps/generic/libc-tls.c | 7 +++++--
 2 files changed, 6 insertions(+), 3 deletions(-)

Comments

linted Sept. 2, 2022, 4:20 p.m. UTC | #1
Looks good to me. Tested on i386, x86_64, arm and aarch64. It also works
with mips on its pending patch after fixing the merge conflicts.

On Fri, Sep 2, 2022 at 6:42 AM Max Filippov <jcmvbkbc@gmail.com> wrote:

> When uclibc is built with static PIE support the _dl_load_base variable
> shared between the libc-tls.c and reloc_static_pie.c creates the
> dependency that requires linking reloc_static_pie.o into static
> position-dependent executables resulting in the following build errors:
>   gcc -static test.c -o test
>   ...ld:
>   ...usr/lib/libc.a(reloc_static_pie.os):(.text+0x0):
>         undefined reference to `_DYNAMIC'
>
> Move _dl_load_base definition to libc-tls.c to resolve this dependency
> and fix static PDE build.
>
> Signed-off-by: Max Filippov <jcmvbkbc@gmail.com>
> ---
> Changes v1->v2:
> - make _dl_load_base definition conditional
>
>  libc/misc/internals/reloc_static_pie.c     | 2 +-
>  libpthread/nptl/sysdeps/generic/libc-tls.c | 7 +++++--
>  2 files changed, 6 insertions(+), 3 deletions(-)
>
> diff --git a/libc/misc/internals/reloc_static_pie.c
> b/libc/misc/internals/reloc_static_pie.c
> index 578202d235a1..f66f7a4236c9 100644
> --- a/libc/misc/internals/reloc_static_pie.c
> +++ b/libc/misc/internals/reloc_static_pie.c
> @@ -20,7 +20,7 @@
>  #include <elf.h>
>  #include <dl-elf.h>
>
> -ElfW(Addr) _dl_load_base = NULL;
> +extern ElfW(Addr) _dl_load_base;
>
>  void
>  reloc_static_pie (ElfW(Addr) load_addr);
> diff --git a/libpthread/nptl/sysdeps/generic/libc-tls.c
> b/libpthread/nptl/sysdeps/generic/libc-tls.c
> index 0c8c558d05d3..d9c09fdd6667 100644
> --- a/libpthread/nptl/sysdeps/generic/libc-tls.c
> +++ b/libpthread/nptl/sysdeps/generic/libc-tls.c
> @@ -117,6 +117,10 @@ init_static_tls (size_t memsz, size_t align)
>    GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
>  }
>
> +#if !defined(__FDPIC__) && !defined(SHARED) && defined(STATIC_PIE)
> +ElfW(Addr) _dl_load_base;
> +#endif
> +
>  void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
>  void
>  __libc_setup_tls (size_t tcbsize, size_t tcbalign)
> @@ -143,8 +147,7 @@ __libc_setup_tls (size_t tcbsize, size_t tcbalign)
>  #else
>           initimage = (void *) phdr->p_vaddr;
>  #if !defined(SHARED) && defined(STATIC_PIE)
> -    extern ElfW(Addr) _dl_load_base;
> -    initimage += _dl_load_base;
> +         initimage += _dl_load_base;
>  #endif
>  #endif
>           align = phdr->p_align;
> --
> 2.30.2
>
>
diff mbox series

Patch

diff --git a/libc/misc/internals/reloc_static_pie.c b/libc/misc/internals/reloc_static_pie.c
index 578202d235a1..f66f7a4236c9 100644
--- a/libc/misc/internals/reloc_static_pie.c
+++ b/libc/misc/internals/reloc_static_pie.c
@@ -20,7 +20,7 @@ 
 #include <elf.h>
 #include <dl-elf.h>
 
-ElfW(Addr) _dl_load_base = NULL;
+extern ElfW(Addr) _dl_load_base;
 
 void
 reloc_static_pie (ElfW(Addr) load_addr);
diff --git a/libpthread/nptl/sysdeps/generic/libc-tls.c b/libpthread/nptl/sysdeps/generic/libc-tls.c
index 0c8c558d05d3..d9c09fdd6667 100644
--- a/libpthread/nptl/sysdeps/generic/libc-tls.c
+++ b/libpthread/nptl/sysdeps/generic/libc-tls.c
@@ -117,6 +117,10 @@  init_static_tls (size_t memsz, size_t align)
   GL(dl_tls_static_nelem) = GL(dl_tls_max_dtv_idx);
 }
 
+#if !defined(__FDPIC__) && !defined(SHARED) && defined(STATIC_PIE)
+ElfW(Addr) _dl_load_base;
+#endif
+
 void __libc_setup_tls (size_t tcbsize, size_t tcbalign);
 void
 __libc_setup_tls (size_t tcbsize, size_t tcbalign)
@@ -143,8 +147,7 @@  __libc_setup_tls (size_t tcbsize, size_t tcbalign)
 #else
 	  initimage = (void *) phdr->p_vaddr;
 #if !defined(SHARED) && defined(STATIC_PIE)
-    extern ElfW(Addr) _dl_load_base;
-    initimage += _dl_load_base;
+	  initimage += _dl_load_base;
 #endif
 #endif
 	  align = phdr->p_align;