Skip to content

Commit

Permalink
Re-enable 32-bit time-compatibility functions to support AC_CHECK_FUN…
Browse files Browse the repository at this point in the history
…CS([clock_gettime ...])
  • Loading branch information
nicklauslittle-gov committed Jun 27, 2024
1 parent cf45980 commit 75dc10e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ RUN make -j4 -C musl-cross-make \
${GCC_CONFIG} --disable-libssp --disable-libsanitizer --with-linker-hash-style=gnu --with-pic --enable-libstdcxx-backtrace=yes" \
BINUTILS_CONFIG='--enable-gold' \
MUSL_CONFIG='CFLAGS="-DNO_GLIBC_ABI_COMPATIBLE"' \
MUSL_VARS='COMPAT_SRC_DIRS= AR="../obj_binutils/binutils/ar --plugin liblto_plugin.so" RANLIB="../obj_binutils/binutils/ranlib --plugin liblto_plugin.so"' \
MUSL_VARS='AR="../obj_binutils/binutils/ar --plugin liblto_plugin.so" RANLIB="../obj_binutils/binutils/ranlib --plugin liblto_plugin.so"' \
TARGET=${TARGET} \
GCC_VER=${GCC_VER} \
MUSL_VER=${MUSL_VER} \
Expand Down Expand Up @@ -102,7 +102,7 @@ RUN /musl-cross-make/output/bin/patchar /musl-cross-make/output/${TARGET}/lib/li
-defined 'strnlen' -exclude '-strlcat,-strlcpy' \
-exclude '-__convert_scm_timestamps,-_*p?poll.*,-_*recvm?msg.*,-_*sendm?msg.*,-_*p?select.*' \
-defined 'asctime(_r)?,localtime(_r)?,memcpy,strcmp' -exclude '-__vdsosym,-__.*_to_secs,-__secs_to_.*,-__utc,-_*clock_nanosleep.*,-_*clock_getres.*,-_*clock_gettime.*,-_*futimesat.*,-_*gmtime(_r)?,-_*nanosleep.*,-_*timegm.*,-_*timerfd_.*,-timespec_get,-.*time64.*(includes 39/62)*' -info | tee patchar.log && \
diff <(sed -nre '/^[^ ]+$/p' -e 's/^Including symbol ([^ ]+).*/\1/p' /musl-cross-results/libgabi.${TARGET}.sym | sort) <(sed -nre 's/^Including symbol ([^ ]+).*/\1/p' patchar.log | sort) && \
diff <(sed -nre '/^[^ ]+$/p' -e '/objcopy /p' -e 's/^Including symbol ([^ ]+).*/\1/p' /musl-cross-results/libgabi.${TARGET}.sym | sort) <(sed -nre '/objcopy /p' -e 's/^Including symbol ([^ ]+).*/\1/p' patchar.log | sort) && \
/musl-cross-make/output/bin/${TARGET}-gcc -DNO_GLIBC_ABI_COMPATIBLE -O3 -flto -fPIC -fvisibility=hidden -Wall -c -o compat_libc.o /musl-cross-src/compat_libc.c && \
/musl-cross-make/output/bin/${TARGET}-gcc-ar r /musl-cross-make/output/${TARGET}/lib/libgabi.a compat_libc.o && \
rm -rf compat_libc.o
Expand Down
20 changes: 20 additions & 0 deletions results/libgabi.arm-linux-musleabihf.sym
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ basename
ceil
ceilf
ceill
clock_getres
clock_gettime
clock_nanosleep
creat
div
explicit_bzero
Expand All @@ -124,13 +127,18 @@ fmodl
frexp
frexpf
frexpl
fstat
fstatat
fstatfs
fstatvfs
ftruncate
futimesat
getdents
getentropy
getrandom
getrlimit
gmtime
gmtime_r
ilogb
ilogbf
ilogbl
Expand Down Expand Up @@ -159,6 +167,7 @@ lround
lroundf
lroundl
lseek
lstat
mknod
mknodat
mmap64
Expand All @@ -169,6 +178,7 @@ msgctl
nan
nanf
nanl
nanosleep
open
openat
poll
Expand All @@ -177,16 +187,19 @@ posix_fallocate
pow
powf
powl
ppoll
pread
preadv
prlimit
pselect
pthread_equal
pwrite
pwritev
readdir
readdir_r
readdir64
readdir64_r
recvmmsg
recvmsg
remquo
remquof
Expand All @@ -204,6 +217,7 @@ scalbn
scalbnf
scalbnl
secure_getenv
select
semctl
sendfile
sendmmsg
Expand All @@ -213,14 +227,20 @@ shmctl
sqrt
sqrtf
sqrtl
stat
statfs
statvfs
statx
strlcat
strlcpy
thrd_equal
thrd_sleep
thrd_yield
timegm
timerfd_create
timerfd_gettime
timerfd_settime
timespec_get
trunc
truncate
truncf
Expand Down
20 changes: 20 additions & 0 deletions results/libgabi.i686-linux-musl.sym
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ basename
ceil
ceilf
ceill
clock_getres
clock_gettime
clock_nanosleep
creat
div
explicit_bzero
Expand All @@ -116,13 +119,18 @@ fmodl
frexp
frexpf
frexpl
fstat
fstatat
fstatfs
fstatvfs
ftruncate
futimesat
getdents
getentropy
getrandom
getrlimit
gmtime
gmtime_r
ilogb
ilogbf
ilogbl
Expand Down Expand Up @@ -156,6 +164,7 @@ lround
lroundf
lroundl
lseek
lstat
mknod
mknodat
mmap64
Expand All @@ -166,6 +175,7 @@ msgctl
nan
nanf
nanl
nanosleep
open
openat
poll
Expand All @@ -174,16 +184,19 @@ posix_fallocate
pow
powf
powl
ppoll
pread
preadv
prlimit
pselect
pthread_equal
pwrite
pwritev
readdir
readdir_r
readdir64
readdir64_r
recvmmsg
recvmsg
remquo
remquof
Expand All @@ -201,6 +214,7 @@ scalbn
scalbnf
scalbnl
secure_getenv
select
semctl
sendfile
sendmmsg
Expand All @@ -210,14 +224,20 @@ shmctl
sqrt
sqrtf
sqrtl
stat
statfs
statvfs
statx
strlcat
strlcpy
thrd_equal
thrd_sleep
thrd_yield
timegm
timerfd_create
timerfd_gettime
timerfd_settime
timespec_get
trunc
truncate
truncf
Expand Down
20 changes: 20 additions & 0 deletions results/libgabi.powerpc-linux-musl.sym
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ basename
ceil
ceilf
ceill
clock_getres
clock_gettime
clock_nanosleep
creat
div
explicit_bzero
Expand All @@ -112,13 +115,18 @@ fmodl
frexp
frexpf
frexpl
fstat
fstatat
fstatfs
fstatvfs
ftruncate
futimesat
getdents
getentropy
getrandom
getrlimit
gmtime
gmtime_r
ilogb
ilogbf
ilogbl
Expand Down Expand Up @@ -147,6 +155,7 @@ lround
lroundf
lroundl
lseek
lstat
mknod
mknodat
mmap64
Expand All @@ -157,6 +166,7 @@ msgctl
nan
nanf
nanl
nanosleep
open
openat
poll
Expand All @@ -165,16 +175,19 @@ posix_fallocate
pow
powf
powl
ppoll
pread
preadv
prlimit
pselect
pthread_equal
pwrite
pwritev
readdir
readdir_r
readdir64
readdir64_r
recvmmsg
recvmsg
remquo
remquof
Expand All @@ -192,6 +205,7 @@ scalbn
scalbnf
scalbnl
secure_getenv
select
semctl
sendfile
sendmmsg
Expand All @@ -201,14 +215,20 @@ shmctl
sqrt
sqrtf
sqrtl
stat
statfs
statvfs
statx
strlcat
strlcpy
thrd_equal
thrd_sleep
thrd_yield
timegm
timerfd_create
timerfd_gettime
timerfd_settime
timespec_get
trunc
truncate
truncf
Expand Down
58 changes: 31 additions & 27 deletions src/compat_libc.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,52 +67,56 @@ void *dlsym(void *restrict handle, const char *restrict name);
#endif

// Bypass Functions:
// These functions are the public glibc functions.
// The public functions that map to these are actually hidden in the static C library, so they cannot be linked and must be looked up by name.
// The standard public functions that map to these functions are marked hidden by the static glibc library (libc_nonshared.a), resulting in link-time failures when linking any DSO compiled against musl-libc.
// Therefore, the standard public functions must be bypassed and looked up by name (or using the glibc exposed replacement functions).

POSSIBLY_UNDEFINED int __cxa_at_quick_exit(void (*func)(void *), void *dso_handle)
{
static void *cxa_at_quick_exit_fn = 0;
static void *at_quick_exit_fn = 0;

if (cxa_at_quick_exit_fn)
return ((int (*)(void (*)(void *), void *))cxa_at_quick_exit_fn)(func, dso_handle);
for (;;) {
// Try to use the glibc replacement function (__cxa_at_quick_exit) first, then the standard public function (at_quick_exit)
if (cxa_at_quick_exit_fn)
return ((int (*)(void (*)(void *), void *))cxa_at_quick_exit_fn)(func, dso_handle);
else if (at_quick_exit_fn)
return ((int (*)(void (*)(void *)))at_quick_exit_fn)(func);

if (at_quick_exit_fn)
return ((int (*)(void (*)(void *)))at_quick_exit_fn)(func);
// Attempt to find the glibc replacement function (__cxa_at_quick_exit) or the standard public function (at_quick_exit)
cxa_at_quick_exit_fn = dlsym(RTLD_NEXT, "__cxa_at_quick_exit");
at_quick_exit_fn = dlsym(RTLD_DEFAULT, "at_quick_exit");

cxa_at_quick_exit_fn = dlsym(RTLD_NEXT, "__cxa_at_quick_exit");
at_quick_exit_fn = dlsym(RTLD_DEFAULT, "at_quick_exit");

if (!cxa_at_quick_exit_fn && !at_quick_exit_fn)
return errno = ENOSYS;

return __cxa_at_quick_exit(func, dso_handle);
// If neither function was found then return ENOSYS
if (!cxa_at_quick_exit_fn && !at_quick_exit_fn)
return errno = ENOSYS;
}
}

POSSIBLY_UNDEFINED int __register_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void), void *dso_handle)
{
static void *register_atfork_fn = 0;
static void *pthread_atfork_fn = 0;

if (register_atfork_fn)
return ((int (*)(void (*)(void), void (*)(void), void (*)(void), void *))register_atfork_fn)(prepare, parent, child, dso_handle);
for (;;) {
// Try to use the glibc replacement function (__register_atfork) first, then the standard public function (pthread_atfork)
if (register_atfork_fn)
return ((int (*)(void (*)(void), void (*)(void), void (*)(void), void *))register_atfork_fn)(prepare, parent, child, dso_handle);
else if (pthread_atfork_fn)
return ((int (*)(void (*)(void), void (*)(void), void (*)(void)))pthread_atfork_fn)(prepare, parent, child);

if (pthread_atfork_fn)
return ((int (*)(void (*)(void), void (*)(void), void (*)(void)))pthread_atfork_fn)(prepare, parent, child);
// Attempt to find the glibc replacement function (__register_atfork) or the standard public function (pthread_atfork)
register_atfork_fn = dlsym(RTLD_NEXT, "__register_atfork");
pthread_atfork_fn = dlsym(RTLD_DEFAULT, "pthread_atfork");

register_atfork_fn = dlsym(RTLD_NEXT, "__register_atfork");
pthread_atfork_fn = dlsym(RTLD_DEFAULT, "pthread_atfork");

if (!register_atfork_fn && !pthread_atfork_fn)
return errno = ENOSYS;

return __register_atfork(prepare, parent, child, dso_handle);
// If neither function was found then return ENOSYS
if (!register_atfork_fn && !pthread_atfork_fn)
return errno = ENOSYS;
}
}

// Looking up the function (using dlsym()) will find the latest version of the symbol.
// Whereas linking against the symbol (without a specific version) will always use the oldest version of the symbol.
// Use LOOKUP_LIBC_FUNC() to use the latest version of a symbol with some minor overhead used to lookup the symbol the first time it is called.
// Use LOOKUP_LIBC_FUNC*() to use the latest version of a symbol with some minor overhead used to lookup the symbol the first time it is called.
// Note: Looking up the function (using dlsym()) will find the latest version of the symbol.
// Whereas linking against the symbol (without a specific version) will always use the oldest version of the symbol.

#define LOOKUP_LIBC_FUNC_NO_RET(NAME, PARAM_LIST, ARG_LIST) \
void NAME PARAM_LIST \
Expand Down

0 comments on commit 75dc10e

Please sign in to comment.