|
Message-Id: <20220524092723.6459a5b7073e887af279820b@zhasha.com> Date: Tue, 24 May 2022 09:27:23 +0200 From: Joakim Sindholt <opensource@...sha.com> To: musl@...ts.openwall.com Subject: Re: Linking musl with -shared -static - is this supported? On Tue, 24 May 2022 07:54:43 +0200, Kuba Kazimierczak <kuba@...imierczak.name> wrote: > Hi, I'm on Alpine Linux, musl version 1.2.2. I'm attempting to create a > statically-linked shared object using "-shared -static" and use it in a > driver program as follows: Musl does support building a statically linked shared object but it does not (and cannot) support loading said object into another process with its own libc, regardless of which libc it is. > /tmp # cat lib.c > #include <stdlib.h> > > unsigned int add(unsigned int a, unsigned int b) > { > free(malloc(1)); > return (a+b); > } > /tmp # cat main.c > #include <stdio.h> > > extern unsigned int add(unsigned int a, unsigned int b); > > int main(void) > { > printf("%d\n", add(1,2)); > return 0; > } > /tmp # clang-14 -g -c -fPIC lib.c > /tmp # clang-14 -shared -static -o liblib.so lib.o > /tmp # clang-14 -g -L. main.c -o main -llib > /tmp # LD_LIBRARY_PATH=$PWD ./main > Segmentation fault > > The segfault happens in musl's malloc implementation internals: > > #0 0x00007f9c882adc7a in get_random_secret () at > src/malloc/mallocng/glue.h:45 > #1 __malloc_alloc_meta () at src/malloc/mallocng/malloc.c:50 > #2 0x00007f9c882ae11b in alloc_group (req=1, sc=0) at > src/malloc/mallocng/malloc.c:179 > #3 alloc_slot (sc=sc@...ry=0, req=req@...ry=1) at > src/malloc/mallocng/malloc.c:291 > #4 0x00007f9c882ae646 in __libc_malloc_impl (n=1) at > src/malloc/mallocng/malloc.c:369 > #5 0x00007f9c882ad208 in add (a=1, b=2) at lib.c:5 > #6 0x000055909082f22e in main () at main.c:7 > > I see get_random_secret is trying to use struct __libc.argv, which is > probably not initialized in this case. > > Is this use case supported at all? You are entirely correct. The DSO you've built does not get to run its instance of the dynamic linker which leaves all the libc-global data uninitialized. Unfortunately just running the init won't give you a functional environment. Someone else asked roughly the same question half a year ago: https://www.openwall.com/lists/musl/2022/01/01/1 Joakim
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.