Skip to content

Instantly share code, notes, and snippets.

@gagern
Last active December 31, 2015 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gagern/7989748 to your computer and use it in GitHub Desktop.
Save gagern/7989748 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char** argv) {
void *libb, *libc, (*f)(void);
if (argc == 1) {
printf("Loading b.so\n");
if (!(libb = dlopen("./b.so", RTLD_NOW))) {
fprintf(stderr, "dlopen(b): %s\n", dlerror());
return 1;
}
}
else {
printf("Not loading b.so\n");
}
printf("Loading c.so\n");
if (!(libc = dlopen("./c.so", RTLD_NOW))) {
fprintf(stderr, "dlopen(c): %s\n", dlerror());
return 2;
}
printf("Looking up f\n");
if (!(f = (void(*)(void)) dlsym(libc, "f"))) {
fprintf(stderr, "dlsym(f): %s\n", dlerror());
return 3;
}
printf("Calling f\n");
f();
printf("Returned successfully\n");
return 0;
}
extern "C" { void d() { } }
#include <thread>
void t() { }
extern "C" {
void f() {
std::thread(t).join();
}
}
failrun: fixrun
@echo ""
./a
FINDLIBPTHREAD=ldd c.so | awk '/libpthread.so/{print $$3}' | head -n1
fixrun: suceedrun
@echo ""
LD_PRELOAD='$(shell $(FINDLIBPTHREAD))' ./a
suceedrun: a b.so c.so
@echo ""
./a skip-b
.PHONY: failrun fixrun suceedrun
a: a.c Makefile
gcc -Wall -ggdb -O0 -ldl -o a a.c
b.so: b.cc Makefile
g++ -std=c++11 -Wall -ggdb -O0 -fPIC -shared -o b.so b.cc
c.so: c.cc Makefile
g++ -std=c++11 -Wall -ggdb -O0 -fPIC -shared -pthread -o c.so c.cc
./a skip-b
Not loading b.so
Loading c.so
Looking up f
Calling f
Returned successfully
LD_PRELOAD='/lib64/libpthread.so.0' ./a
Loading b.so
Loading c.so
Looking up f
Calling f
Returned successfully
./a
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Loading b.so
Loading c.so
Looking up f
Calling f
Makefile:2: recipe for target 'failrun' failed
make: *** [failrun] Aborted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment