Skip to content

Commit

Permalink
use some linker magic to avoid issues with symbol versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Kyle committed May 1, 2024
1 parent 24fb258 commit e93ce21
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
45 changes: 42 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,31 +1,70 @@
.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 <version> - build all the techniques for the specific version. e.g. `make v2.39`'
@echo 'make <version> - 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=<version> - test run all techniques for the specific version. e.g. `make test version=2.39`'
@echo 'make test version=<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_<version> 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 <version> 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)

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);\
Expand Down
28 changes: 0 additions & 28 deletions Makefile_ld

This file was deleted.

29 changes: 29 additions & 0 deletions utils/wrapper.h
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit e93ce21

Please # to comment.