Resolving symbol 'default implementations' at link time?
Posted: Sun Apr 17, 2016 10:56 am
Hi.
I wanted to add locking into my libc heap initialization but only do it when pthread library is linked, yet I couldn't come up with a nice way to do this.
Basically at compile time heap_init has to call some locking function because we don't know what user will link against.
So my plan was to add a default no-op mutex to libnopthread.a library and add it after libc (and the optional libpthread).
However, the order of default libraries is awkward: gcc -o foo foo.o -lpthread => foo.o + libpthread.a + libc.a + libnopthread.a,
thus the locking symbols might not get pulled in unless there is some dependency tricks in libpthread
(i.e. if foo.o refers to pthread_create, that in turn must refer to all symbols required by libc just to ensure they are pulled in).
Anyone have a better idea?
I wanted to add locking into my libc heap initialization but only do it when pthread library is linked, yet I couldn't come up with a nice way to do this.
Basically at compile time heap_init has to call some locking function because we don't know what user will link against.
So my plan was to add a default no-op mutex to libnopthread.a library and add it after libc (and the optional libpthread).
However, the order of default libraries is awkward: gcc -o foo foo.o -lpthread => foo.o + libpthread.a + libc.a + libnopthread.a,
thus the locking symbols might not get pulled in unless there is some dependency tricks in libpthread
(i.e. if foo.o refers to pthread_create, that in turn must refer to all symbols required by libc just to ensure they are pulled in).
Anyone have a better idea?