Skip to content

Commit e93ce21

Browse files
committed
use some linker magic to avoid issues with symbol versioning
1 parent 24fb258 commit e93ce21

File tree

3 files changed

+71
-31
lines changed

3 files changed

+71
-31
lines changed

Makefile

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,70 @@
1-
.PHONY: help clean all test
1+
.PHONY: help clean distclean all test
22

33
VERSIONS := 2.23 2.24 2.27 2.31 2.32 2.33 2.34 2.35 2.36 2.37 2.38 2.39
44
TECH_BINS := $(patsubst %.c,%,$(wildcard glibc_*/*.c))
55
BASE_BINS := $(patsubst %.c,%,$(wildcard *.c))
6+
DOWNLOADED := glibc-all-in-one/libs glibc-all-in-one/debs
67
BINS := $(TECH_BINS) $(BASE_BINS)
8+
ARCH := amd64
9+
10+
ifeq ($(H2H_USE_SYSTEM_LIBC),)
11+
H2H_USE_SYSTEM_LIBC := Y
12+
endif
713

814
help:
915
@echo 'make help - show this message'
1016
@echo 'make base - build all base binaries, namely `malloc_playground`, `first_fit`, `calc_tcache_idx`'
11-
@echo 'make <version> - build all the techniques for the specific version. e.g. `make v2.39`'
17+
@echo 'make <version> - build all the techniques for a specific version. e.g. `make v2.39`'
1218
@echo 'make clean - remove all built binaries'
19+
@echo 'make distclean - remove all built binaries and downloaded libcs'
1320
@echo 'make all - build all binaries'
14-
@echo 'make test version=<version> - test run all techniques for the specific version. e.g. `make test version=2.39`'
21+
@echo 'make test version=<version> - test run all techniques for a specific version. e.g. `make test version=2.39`'
1522

1623
CFLAGS += -std=c99 -g -Wno-unused-result -Wno-free-nonheap-object
1724
LDLIBS += -ldl
1825

1926
base: $(BASE_BINS)
2027

28+
# populate the download_glibc_<version> rules
29+
$(addprefix download_glibc_, $(VERSIONS)):
30+
@echo $@
31+
32+
version=$(patsubst download_glibc_%,%,$@); \
33+
libc=$$(cat glibc-all-in-one/list | grep "$$version" | grep "$(ARCH)" | head -n 1); \
34+
old_libc=$$(cat glibc-all-in-one/old_list | grep "$(version)" | grep "$(ARCH)" | head -n 1); \
35+
if [ -z $$libc ]; then libc=$$old_libc; script="download_old"; else libc=$$libc; script="download"; fi; \
36+
cd glibc-all-in-one; \
37+
rm -rf libs/$$libc; \
38+
./$$script $$libc
39+
40+
# populate the make <version> rules
41+
ifeq ($(H2H_USE_SYSTEM_LIBC),Y)
2142
$(foreach version,$(VERSIONS),$(eval v$(version): $(patsubst %.c,%,$(wildcard glibc_$(version)/*.c))))
43+
else
44+
$(foreach version,$(VERSIONS),$(eval v$(version): download_glibc_$(version) $(patsubst %.c,%,$(wildcard glibc_$(version)/*.c)) ))
45+
endif
46+
47+
# the compilation rules
48+
%: %.c
49+
version=$(word 1, $(subst /, ,$(patsubst glibc_%,%,$@))); \
50+
if [ "$(H2H_USE_SYSTEM_LIBC)" = "Y" ]; \
51+
then \
52+
$(CC) $(CFLAGS) $(DIR_CFLAGS_$(@D)) $^ -o $@ $(LDLIBS); \
53+
else \
54+
$(CC) $(CFLAGS) $(DIR_CFLAGS_$(@D)) $^ -o $@ $(LDLIBS) -Xlinker -rpath=$$(realpath glibc-all-in-one/libs/$$version*) -Xlinker -I$$(realpath glibc-all-in-one/libs/$$version*/ld-linux-x86-64.so.2) -Xlinker $$(realpath glibc-all-in-one/libs/$$version*/libc.so.6) -Xlinker $$(realpath glibc-all-in-one/libs/$$version*/libdl.so.2) -include ./utils/wrapper.h -Wl,--wrap=__libc_start_main -Wl,--wrap=dlsym; \
55+
fi
2256

2357
all: $(BINS)
2458

2559
clean:
2660
@rm -f $(BINS)
2761
@echo "all the built binaries are removed."
2862

63+
distclean:
64+
@rm -f $(BINS)
65+
@rm -rf $(DOWNLOADED)
66+
@echo "all the built binaries and all downloaded libcs are removed."
67+
2968
define test_poc =
3069
echo $(poc)
3170
for i in $$(seq 0 20);\

Makefile_ld

Lines changed: 0 additions & 28 deletions
This file was deleted.

utils/wrapper.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// hook __libc_start_main
2+
__asm__(".symver __libc_start_main_old,__libc_start_main@GLIBC_2.2.5");
3+
int __libc_start_main_old(int (*main) (int, char **, char **),
4+
int argc,
5+
char **argv,
6+
__typeof (main) init,
7+
void (*fini) (void),
8+
void (*rtld_fini) (void),
9+
void *stack_end);
10+
11+
int __wrap___libc_start_main(int (*main) (int, char **, char **),
12+
int argc,
13+
char **argv,
14+
__typeof (main) init,
15+
void (*fini) (void),
16+
void (*rtld_fini) (void),
17+
void *stack_end)
18+
{
19+
return __libc_start_main_old(main, argc, argv, init, fini, rtld_fini, stack_end);
20+
}
21+
22+
// hook dlsym
23+
__asm__(".symver dlsym_old,dlsym@GLIBC_2.2.5");
24+
//__asm__(".symver dlsym_old,__libc_dlsym@GLIBC_PRIVATE");
25+
void *dlsym_old(void *handle, const char *symbol);
26+
void *__wrap_dlsym(void *handle, const char *symbol)
27+
{
28+
return dlsym_old(handle, symbol);
29+
}

0 commit comments

Comments
 (0)