From ca6ce3b9470147881d18225ea9d39a04ea9dada3 Mon Sep 17 00:00:00 2001 From: kaizoku Date: Sat, 13 May 2017 02:26:43 -0700 Subject: [PATCH 1/4] add include folder into CFLAGS and remove path from relative include --- src/Makefile.in | 2 +- src/libelfmaster.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Makefile.in b/src/Makefile.in index 86441e9..e469295 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -5,7 +5,7 @@ include @BUILD_DIR@/build/libelfmaster.build TARGET_DIR=$(BUILD_DIR)/src SDIR=$(SRC_DIR)/src INCLUDE_DIR=$(SRC_DIR)/include -CFLAGS+=-ggdb +CFLAGS+=-ggdb -I$(INCLUDE_DIR) HEADERS=$(INCLUDE_DIR)/*.h OBJECTS=libelfmaster.o diff --git a/src/libelfmaster.c b/src/libelfmaster.c index e49e815..df26bf0 100644 --- a/src/libelfmaster.c +++ b/src/libelfmaster.c @@ -11,7 +11,7 @@ #include #include -#include "../include/libelfmaster.h" +#include "libelfmaster.h" #include "misc.h" #include "internal.c" From 37d186056046e2e0586f8724e20530f7ce0f59fb Mon Sep 17 00:00:00 2001 From: kaizoku Date: Sat, 13 May 2017 02:27:39 -0700 Subject: [PATCH 2/4] add Makefile target for .so --- src/Makefile.in | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Makefile.in b/src/Makefile.in index e469295..2671f39 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -10,12 +10,15 @@ HEADERS=$(INCLUDE_DIR)/*.h OBJECTS=libelfmaster.o -all: libelfmaster.a +all: libelfmaster.a libelfmaster.so libelfmaster.a: $(OBJECTS) rm -f $(TARGET_DIR)/libelfmaster.a ar rcs $(TARGET_DIR)/libelfmaster.a $(OBJECTS) +libelfmaster.so: $(OBJECTS) + $(CC) $(CFLAGS) $(OBJECTS) -shared -o $(TARGET_DIR)/libelfmaster.so + libelfmaster.o: $(SDIR)/libelfmaster.c $(HEADERS) $(CC) $(CFLAGS) -c -o $(TARGET_DIR)/libelfmaster.o $(SDIR)/libelfmaster.c From 3754e356834c4483ae47082d3f448a5702ebcf47 Mon Sep 17 00:00:00 2001 From: kaizoku Date: Sat, 13 May 2017 02:29:45 -0700 Subject: [PATCH 3/4] add .gitignore --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4400e19 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +Makefile +src/libelfmaster.o +src/libelfmaster.a +src/libelfmaster.so From 881dbdb0530f8813ed1e86dfb1ba2590b715a5e0 Mon Sep 17 00:00:00 2001 From: elfmaster Date: Mon, 15 May 2017 18:06:18 -0700 Subject: [PATCH 4/4] updated Makefile.in so it compiles internal.c, removed macros into internal.h and removed them from internal.c, removed static keyword from functions in internal.c that wont be called by libelfmaster.c --- .gitignore | 1 + include/internal.h | 39 +++++-------- src/Makefile.in | 6 +- src/internal.c | 134 ++++++++++----------------------------------- tools/Makefile | 4 +- 5 files changed, 48 insertions(+), 136 deletions(-) diff --git a/.gitignore b/.gitignore index 4400e19..57cf263 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ Makefile src/libelfmaster.o src/libelfmaster.a src/libelfmaster.so +build/libelfmaster.build diff --git a/include/internal.h b/include/internal.h index 163698e..201942d 100644 --- a/include/internal.h +++ b/include/internal.h @@ -96,50 +96,37 @@ typedef struct elf_plt_node { } elf_plt_node_t; -static bool -elf_error_set(elf_error_t *, const char *, ...); +bool elf_error_set(elf_error_t *, const char *, ...); -static int -section_name_cmp(const void *, const void *); +int section_name_cmp(const void *, const void *); -static bool -build_plt_data(struct elfobj *); +bool build_plt_data(struct elfobj *); -static bool -build_dynsym_data(struct elfobj *); +bool build_dynsym_data(struct elfobj *); -static bool -build_symtab_data(struct elfobj *); +bool build_symtab_data(struct elfobj *); -static int -ldso_cache_cmp(const char *, const char *); +static int ldso_cache_cmp(const char *, const char *); static inline bool ldso_cache_check_flags(struct elf_shared_object_iterator *, uint32_t); -static const char * -ldso_cache_bsearch(struct elf_shared_object_iterator *, +static const char * ldso_cache_bsearch(struct elf_shared_object_iterator *, const char *); -static bool -ldso_insert_yield_entry(struct elf_shared_object_iterator *, +static bool ldso_insert_yield_entry(struct elf_shared_object_iterator *, const char *); -static bool -ldso_recursive_cache_resolve(struct elf_shared_object_iterator *, +bool ldso_recursive_cache_resolve(struct elf_shared_object_iterator *, const char *); -static bool -load_dynamic_segment_data(struct elfobj *); +bool load_dynamic_segment_data(struct elfobj *); -static void -free_lists(elfobj_t *); +void free_lists(elfobj_t *); -static void -free_caches(elfobj_t *); +void free_caches(elfobj_t *); -static void -free_arrays(elfobj_t *); +void free_arrays(elfobj_t *); #endif // _LIBELFMASTER_INTERNAL_H_ diff --git a/src/Makefile.in b/src/Makefile.in index 2671f39..f33d66a 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -5,10 +5,10 @@ include @BUILD_DIR@/build/libelfmaster.build TARGET_DIR=$(BUILD_DIR)/src SDIR=$(SRC_DIR)/src INCLUDE_DIR=$(SRC_DIR)/include -CFLAGS+=-ggdb -I$(INCLUDE_DIR) +CFLAGS+=-ggdb -O2 -I$(INCLUDE_DIR) HEADERS=$(INCLUDE_DIR)/*.h -OBJECTS=libelfmaster.o +OBJECTS=libelfmaster.o, internal.o all: libelfmaster.a libelfmaster.so @@ -22,6 +22,8 @@ libelfmaster.so: $(OBJECTS) libelfmaster.o: $(SDIR)/libelfmaster.c $(HEADERS) $(CC) $(CFLAGS) -c -o $(TARGET_DIR)/libelfmaster.o $(SDIR)/libelfmaster.c +internal.o: $(SDIR)/internal.c $(HEADERS) + $(CC) $(CFLAGS) -c -o $(TARGET_DIR)/internal.o $(SDIR)/internal.c clean: rm -rf $(TARGET_DIR)/*.dSYM $(TARGET_DIR)/*~ $(TARGET_DIR)/*.o \ $(OBJECTS) $(TARGET_DIR)/libelfmaster.a $(TARGET_DIR)/libelfmaster.so diff --git a/src/internal.c b/src/internal.c index ff6e93c..14e1747 100644 --- a/src/internal.c +++ b/src/internal.c @@ -1,99 +1,21 @@ -#ifdef DEBUG -#define DEBUG_LOG(...) do { fprintf(stderr, __VA_ARGS__); } while(0) -#else -#define DEBUG_LOG(...) do {} while(0) -#endif - -#ifdef __GNUC__ -#define likely(x) __builtin_expect(!!(x), 1) -#define unlikely(x) __builtin_expect(!!(x), 0) -#else -#define likely(x) (x) -#define unlikely(x) (x) -#endif - - -#define CACHEMAGIC "ld.so-1.7.0" -struct file_entry { - int flags; - uint32_t key; - uint32_t value; -}; - -struct cache_file { - char magic[sizeof CACHEMAGIC - 1]; - uint32_t nlibs; - struct file_entry libs[0]; -}; - -#define CACHEMAGIC_NEW "glibc-ld.so.cache" -#define CACHE_VERSION "1.1" - -struct file_entry_new { - int32_t flags; - uint32_t key; - uint32_t value; - uint32_t osversion; - uint64_t hwcap; -}; - -struct cache_file_new { - char magic[sizeof CACHEMAGIC_NEW - 1]; - char version[sizeof CACHE_VERSION - 1]; - uint32_t nlibs; /* number of entries */ - uint32_t len_strings; /* size of string table */ - uint32_t unused[5]; /* space for future extension */ - struct file_entry_new libs[0]; /* Entries describing libraries */ - /* After this the string table of size len_strings is found */ -}; - -/* - * This struct is used internally only. - */ -struct elf_rel_helper_node { - union { - Elf32_Rel *rel32; - Elf64_Rel *rel64; - }; - union { - Elf32_Rela *rela32; - Elf64_Rela *rela64; - }; - size_t size; - bool addend; - char *section_name; - LIST_ENTRY(elf_rel_helper_node) _linkage; -}; - -/* - * This should only be used internally. - */ -struct elf_symbol_node { - const char *name; - uint64_t value; - uint64_t size; - uint16_t shndx; - uint8_t bind; - uint8_t type; - uint8_t visibility; - LIST_ENTRY(elf_symbol_node) _linkage; -}; - -typedef struct elf_shared_object_node { - const char *basename; - char *path; - unsigned int index; // used by elf_shared_object iterator - LIST_ENTRY(elf_shared_object_node) _linkage; -} elf_shared_object_node_t; - -typedef struct elf_plt_node { - char *symname; - uint64_t addr; - LIST_ENTRY(elf_plt_node) _linkage; -} elf_plt_node_t; - - -static bool +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "libelfmaster.h" +#include "internal.h" +#include "misc.h" + +bool elf_error_set(elf_error_t *error, const char *fmt, ...) { va_list va; @@ -110,7 +32,7 @@ elf_error_set(elf_error_t *error, const char *fmt, ...) * of pointers to section structs. One which is sorted by address, and * one sorted by name. */ -static int +int section_name_cmp(const void *p0, const void *p1) { const struct elf_section *s0 = *(void **)p0; @@ -124,7 +46,7 @@ section_name_cmp(const void *p0, const void *p1) */ #define ELF_RELOC_JUMP_SLOT 7 -static bool +bool build_plt_data(struct elfobj *obj) { ENTRY e, *ep; @@ -186,7 +108,7 @@ build_plt_data(struct elfobj *obj) return true; } -static bool +bool build_dynsym_data(struct elfobj *obj) { ENTRY e, *ep; @@ -238,7 +160,7 @@ build_dynsym_data(struct elfobj *obj) return true; } -static bool +bool build_symtab_data(struct elfobj *obj) { ENTRY e, *ep; @@ -294,7 +216,7 @@ build_symtab_data(struct elfobj *obj) * Compares libraries by version numbers, and returns 0 * on equal. */ -static int +int ldso_cache_cmp(const char *p1, const char *p2) { while (*p1) { @@ -442,7 +364,7 @@ ldso_insert_yield_entry(struct elf_shared_object_iterator *iter, return true; } -static bool +bool ldso_recursive_cache_resolve(struct elf_shared_object_iterator *iter, const char *bname) { @@ -494,7 +416,7 @@ ldso_recursive_cache_resolve(struct elf_shared_object_iterator *iter, /* * Used internally to build information from the dynamic segment. */ -static bool +bool load_dynamic_segment_data(struct elfobj *obj) { struct elf_dynamic_entry entry; @@ -569,7 +491,7 @@ load_dynamic_segment_data(struct elfobj *obj) return true; } -static void +void free_lists(elfobj_t *obj) { if (LIST_EMPTY(&obj->list.symtab) == 0) { @@ -607,7 +529,7 @@ free_lists(elfobj_t *obj) return; } -static void +void free_caches(elfobj_t *obj) { @@ -617,7 +539,7 @@ free_caches(elfobj_t *obj) return; } -static void +void free_arrays(elfobj_t *obj) { size_t i; diff --git a/tools/Makefile b/tools/Makefile index 68263cc..efe4a3b 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,5 +1,5 @@ all: - gcc -lasan -fsanitize=address -g -fPIC -pie elfparse.c ../src/libelfmaster.a -o elfparse - gcc -lasan -fsanitize=address -g -fPIC -pie ldd.c ../src/libelfmaster.a -o ldd + gcc -O2 -g -O2 -fPIC -pie elfparse.c ../src/libelfmaster.a -o elfparse + gcc -O2 -g -fPIC -pie ldd.c ../src/libelfmaster.a -o ldd clean: rm elfparse