Skip to content

Commit

Permalink
libc-wrappers: Added wrappers for __libc_start_main, pthread_attr_get…
Browse files Browse the repository at this point in the history
…stacksize, pthread_create and pthread_detach. Fixes containers#821
  • Loading branch information
olivergs committed Jul 5, 2021
1 parent a733357 commit 27605eb
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/go-build-wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# limitations under the License.
#

readonly WRAPPED_FUNCTIONS="--wrap,__libc_start_main,--wrap,pthread_attr_getstacksize,--wrap,pthread_create,--wrap,pthread_detach,--wrap,pthread_sigmask"


if [ "$#" -ne 4 ]; then
echo "go-build-wrapper: wrong arguments" >&2
Expand All @@ -27,5 +29,5 @@ if ! cd "$1"; then
exit 1
fi

go build -trimpath -ldflags "-extldflags '-Wl,--wrap,pthread_sigmask $4' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" -o "$2/toolbox"
go build -trimpath -ldflags "-extldflags '-Wl,$WRAPPED_FUNCTIONS $4' -linkmode external -X github.com/containers/toolbox/pkg/version.currentVersion=$3" -o "$2/toolbox"
exit "$?"
137 changes: 137 additions & 0 deletions src/libc-wrappers/libc-wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,145 @@


#include <signal.h>
#include <pthread.h>
#include <link.h>


/*
__libc_start_main wrapping
*/

struct startup_info
{
void *sda_base;
int (*main) (int, char **, char **, void *);
int (*init) (int, char **, char **, void *);
void (*fini) (void);
};

extern int __libc_start_main (int argc, char **argv,
char **ev,
ElfW (auxv_t) * auxvec,
void (*rtld_fini) (void),
struct startup_info * stinfo,
char **stack_on_entry);


#if defined __aarch64__
__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.17");
#elif defined __arm__
__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.4");
#elif defined __i386__
__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.0");
#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.17");
#elif defined __s390x__
__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.2");
#elif defined __x86_64__
__asm__(".symver __libc_start_main,__libc_start_main@GLIBC_2.2.5");
#else
#error "Please specify symbol version for __libc_start_main"
#endif


int
__wrap___libc_start_main (int argc, char **argv,
char **ev,
ElfW (auxv_t) * auxvec,
void (*rtld_fini) (void),
struct startup_info * stinfo,
char **stack_on_entry)
{
return __libc_start_main (argc, argv, ev, auxvec, rtld_fini, stinfo, stack_on_entry);
}


/*
pthread_attr_getstacksize wrapping
*/
#if defined __aarch64__
__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.17");
#elif defined __arm__
__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.4");
#elif defined __i386__
__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.1");
#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.17");
#elif defined __s390x__
__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.2");
#elif defined __x86_64__
__asm__(".symver pthread_attr_getstacksize,pthread_attr_getstacksize@GLIBC_2.2.5");
#else
#error "Please specify symbol version for pthread_attr_getstacksize"
#endif


int
__wrap_pthread_attr_getstacksize (const pthread_attr_t *attr, size_t *stacksize)
{
return pthread_attr_getstacksize (attr, stacksize);
}


/*
pthread_create wrapping
*/
#if defined __aarch64__
__asm__(".symver pthread_create,pthread_create@GLIBC_2.17");
#elif defined __arm__
__asm__(".symver pthread_create,pthread_create@GLIBC_2.4");
#elif defined __i386__
__asm__(".symver pthread_create,pthread_create@GLIBC_2.0");
#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
__asm__(".symver pthread_create,pthread_create@GLIBC_2.17");
#elif defined __s390x__
__asm__(".symver pthread_create,pthread_create@GLIBC_2.2");
#elif defined __x86_64__
__asm__(".symver pthread_create,pthread_create@GLIBC_2.2.5");
#else
#error "Please specify symbol version for pthread_create"
#endif


int
__wrap_pthread_create (pthread_t *newthread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
{
return pthread_create(newthread, attr, start_routine, arg);
}


/*
pthread_detach wrapping
*/
#if defined __aarch64__
__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.17");
#elif defined __arm__
__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.4");
#elif defined __i386__
__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.0");
#elif defined __powerpc64__ && _CALL_ELF == 2 /* ppc64le */
__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.17");
#elif defined __s390x__
__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.2");
#elif defined __x86_64__
__asm__(".symver pthread_detach,pthread_detach@GLIBC_2.2.5");
#else
#error "Please specify symbol version for pthread_detach"
#endif


int
__wrap_pthread_detach (pthread_t th)
{
return pthread_detach (th);
}


/*
pthread_sigmask wrapping
*/

#if defined __aarch64__
__asm__(".symver pthread_sigmask,pthread_sigmask@GLIBC_2.17");
#elif defined __arm__
Expand Down

0 comments on commit 27605eb

Please sign in to comment.