From e93ce21cd827defa80ac6691078f2e3e4d59aec3 Mon Sep 17 00:00:00 2001 From: Kyle Zeng Date: Tue, 30 Apr 2024 17:36:46 -0700 Subject: [PATCH] use some linker magic to avoid issues with symbol versioning --- Makefile | 45 ++++++++++++++++++++++++++++++++++++++++++--- Makefile_ld | 28 ---------------------------- utils/wrapper.h | 29 +++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 31 deletions(-) delete mode 100644 Makefile_ld create mode 100644 utils/wrapper.h diff --git a/Makefile b/Makefile index df0d393..9e8e0e9 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,58 @@ -.PHONY: help clean all test +.PHONY: help clean distclean all test 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 TECH_BINS := $(patsubst %.c,%,$(wildcard glibc_*/*.c)) BASE_BINS := $(patsubst %.c,%,$(wildcard *.c)) +DOWNLOADED := glibc-all-in-one/libs glibc-all-in-one/debs BINS := $(TECH_BINS) $(BASE_BINS) +ARCH := amd64 + +ifeq ($(H2H_USE_SYSTEM_LIBC),) +H2H_USE_SYSTEM_LIBC := Y +endif help: @echo 'make help - show this message' @echo 'make base - build all base binaries, namely `malloc_playground`, `first_fit`, `calc_tcache_idx`' - @echo 'make - build all the techniques for the specific version. e.g. `make v2.39`' + @echo 'make - build all the techniques for a specific version. e.g. `make v2.39`' @echo 'make clean - remove all built binaries' + @echo 'make distclean - remove all built binaries and downloaded libcs' @echo 'make all - build all binaries' - @echo 'make test version= - test run all techniques for the specific version. e.g. `make test version=2.39`' + @echo 'make test version= - test run all techniques for a specific version. e.g. `make test version=2.39`' CFLAGS += -std=c99 -g -Wno-unused-result -Wno-free-nonheap-object LDLIBS += -ldl base: $(BASE_BINS) +# populate the download_glibc_ rules +$(addprefix download_glibc_, $(VERSIONS)): + @echo $@ + + version=$(patsubst download_glibc_%,%,$@); \ + libc=$$(cat glibc-all-in-one/list | grep "$$version" | grep "$(ARCH)" | head -n 1); \ + old_libc=$$(cat glibc-all-in-one/old_list | grep "$(version)" | grep "$(ARCH)" | head -n 1); \ + if [ -z $$libc ]; then libc=$$old_libc; script="download_old"; else libc=$$libc; script="download"; fi; \ + cd glibc-all-in-one; \ + rm -rf libs/$$libc; \ + ./$$script $$libc + +# populate the make rules +ifeq ($(H2H_USE_SYSTEM_LIBC),Y) $(foreach version,$(VERSIONS),$(eval v$(version): $(patsubst %.c,%,$(wildcard glibc_$(version)/*.c)))) +else +$(foreach version,$(VERSIONS),$(eval v$(version): download_glibc_$(version) $(patsubst %.c,%,$(wildcard glibc_$(version)/*.c)) )) +endif + +# the compilation rules +%: %.c + version=$(word 1, $(subst /, ,$(patsubst glibc_%,%,$@))); \ + if [ "$(H2H_USE_SYSTEM_LIBC)" = "Y" ]; \ + then \ + $(CC) $(CFLAGS) $(DIR_CFLAGS_$(@D)) $^ -o $@ $(LDLIBS); \ + else \ + $(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; \ + fi all: $(BINS) @@ -26,6 +60,11 @@ clean: @rm -f $(BINS) @echo "all the built binaries are removed." +distclean: + @rm -f $(BINS) + @rm -rf $(DOWNLOADED) + @echo "all the built binaries and all downloaded libcs are removed." + define test_poc = echo $(poc) for i in $$(seq 0 20);\ diff --git a/Makefile_ld b/Makefile_ld deleted file mode 100644 index 460a13b..0000000 --- a/Makefile_ld +++ /dev/null @@ -1,28 +0,0 @@ -CC := gcc -SRC_DIRS := glibc_2.23 glibc_2.24 glibc_2.27 glibc_2.31 glibc_2.32 glibc_2.33 glibc_2.34 glibc_2.35 glibc_2.36 glibc_2.37 glibc_2.38 glibc_2.39 -DIR_CFLAGS_glibc_2.23 := -Xlinker -rpath=glibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.23-0ubuntu11.3_amd64/ld-2.23.so -DIR_CFLAGS_glibc_2.24 := -Xlinker -rpath=glibc-all-in-one/libs/2.24-3ubuntu1_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.24-3ubuntu1_amd64/ld-2.24.so -DIR_CFLAGS_glibc_2.27 := -Xlinker -rpath=glibc-all-in-one/libs/2.27-3ubuntu1.5_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.27-3ubuntu1.5_amd64/ld-2.27.so -DIR_CFLAGS_glibc_2.31 := -Xlinker -rpath=glibc-all-in-one/libs/2.31-0ubuntu9.15_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.31-0ubuntu9.15_amd64/ld-2.31.so -DIR_CFLAGS_glibc_2.32 := -Xlinker -rpath=glibc-all-in-one/libs/2.32-0ubuntu3.2_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.32-0ubuntu3.2_amd64/ld-2.32.so -DIR_CFLAGS_glibc_2.33 := -Xlinker -rpath=glibc-all-in-one/libs/2.33-0ubuntu5_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.33-0ubuntu5_amd64/ld-2.33.so -DIR_CFLAGS_glibc_2.34 := -Xlinker -rpath=glibc-all-in-one/libs/2.34-0ubuntu3.2_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.34-0ubuntu3.2_amd64/ld-linux-x86-64.so.2 -DIR_CFLAGS_glibc_2.35 := -Xlinker -rpath=glibc-all-in-one/libs/2.35-0ubuntu3.7_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.35-0ubuntu3.7_amd64/ld-linux-x86-64.so.2 -DIR_CFLAGS_glibc_2.36 := -Xlinker -rpath=glibc-all-in-one/libs/2.36-0ubuntu4_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.36-0ubuntu4_amd64/ld-linux-x86-64.so.2 -DIR_CFLAGS_glibc_2.37 := -Xlinker -rpath=glibc-all-in-one/libs/2.37-0ubuntu2.2_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.37-0ubuntu2.2_amd64/ld-linux-x86-64.so.2 -DIR_CFLAGS_glibc_2.38 := -Xlinker -rpath=glibc-all-in-one/libs/2.38-1ubuntu6.2_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.38-1ubuntu6.2_amd64/ld-linux-x86-64.so.2 -DIR_CFLAGS_glibc_2.39 := -Xlinker -rpath=glibc-all-in-one/libs/2.39-0ubuntu8_amd64/ -Xlinker -Iglibc-all-in-one/libs/2.39-0ubuntu8_amd64/ld-linux-x86-64.so.2 -CFLAGS += -std=c99 -g -Wno-unused-result -Wno-free-nonheap-object -LDLIBS += -ldl - -SRCS := $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.c)) -BINS := $(patsubst %.c,%,$(SRCS)) - -$(BINS): % : %.c - $(CC) $(CFLAGS) $(DIR_CFLAGS_$(@D)) $^ -o $@ $(LDLIBS) - -clean: - rm -rf $(BINS) - -all: $(BINS) -.PHONY: all diff --git a/utils/wrapper.h b/utils/wrapper.h new file mode 100644 index 0000000..1cd1d17 --- /dev/null +++ b/utils/wrapper.h @@ -0,0 +1,29 @@ +// hook __libc_start_main +__asm__(".symver __libc_start_main_old,__libc_start_main@GLIBC_2.2.5"); +int __libc_start_main_old(int (*main) (int, char **, char **), + int argc, + char **argv, + __typeof (main) init, + void (*fini) (void), + void (*rtld_fini) (void), + void *stack_end); + +int __wrap___libc_start_main(int (*main) (int, char **, char **), + int argc, + char **argv, + __typeof (main) init, + void (*fini) (void), + void (*rtld_fini) (void), + void *stack_end) +{ + return __libc_start_main_old(main, argc, argv, init, fini, rtld_fini, stack_end); +} + +// hook dlsym +__asm__(".symver dlsym_old,dlsym@GLIBC_2.2.5"); +//__asm__(".symver dlsym_old,__libc_dlsym@GLIBC_PRIVATE"); +void *dlsym_old(void *handle, const char *symbol); +void *__wrap_dlsym(void *handle, const char *symbol) +{ + return dlsym_old(handle, symbol); +}