diff options
author | Stefan Bucur <stefanb@zytor.com> | 2008-06-11 15:20:25 +0300 |
---|---|---|
committer | Stefan Bucur <stefan@stefan-ubumac.(none)> | 2009-03-15 10:02:13 +0200 |
commit | df69084e5b888e3283c2558380ee4f1fd9644ac7 (patch) | |
tree | a149543a3566c50658aa9c5ca12a967962ac0abd | |
parent | faad4e48cbd6e8f552a4c0edf1bdd177a8d375ce (diff) | |
download | syslinux-elf-df69084e5b888e3283c2558380ee4f1fd9644ac7.tar.gz syslinux-elf-df69084e5b888e3283c2558380ee4f1fd9644ac7.tar.xz syslinux-elf-df69084e5b888e3283c2558380ee4f1fd9644ac7.zip |
Added support for searching symbols
-rw-r--r-- | elf/Makefile | 2 | ||||
-rw-r--r-- | elf/elf_module.c | 22 | ||||
-rw-r--r-- | elf/elf_module.h | 2 | ||||
-rw-r--r-- | elf/elf_utils.c | 16 | ||||
-rw-r--r-- | elf/elf_utils.h | 2 | ||||
-rw-r--r-- | elf/elftest.c | 19 |
6 files changed, 60 insertions, 3 deletions
diff --git a/elf/Makefile b/elf/Makefile index 0cab8eab..30f5059e 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -33,7 +33,7 @@ all: test-prog test-module # The testing user-space application test-prog: $(TESTPROG) -$(TESTPROG): elftest.o elf_module.o +$(TESTPROG): elftest.o elf_module.o elf_utils.o $(CC) -o $@ $^ diff --git a/elf/elf_module.c b/elf/elf_module.c index 22900b7a..0a78b7c0 100644 --- a/elf/elf_module.c +++ b/elf/elf_module.c @@ -418,3 +418,25 @@ int module_unload(struct elf_module *module) { return 0; } + + +Elf32_Sym *module_find_symbol(const char *name, struct elf_module *module) { + unsigned long h = elf_hash((const unsigned char*)name); + + Elf32_Word *bkt = module->hash_table + 2; + Elf32_Word *chn = module->hash_table + 2 + module->hash_table[0]; + + Elf32_Word crt_index = bkt[h % module->hash_table[0]]; + Elf32_Sym *crt_sym; + + while (crt_index != STN_UNDEF) { + crt_sym = (Elf32_Sym*)(module->sym_table + crt_index*module->syment_size); + + if (strcmp(name, module->str_table + crt_sym->st_name) == 0) + return crt_sym; + + crt_index = chn[crt_index]; + } + + return NULL; +} diff --git a/elf/elf_module.h b/elf/elf_module.h index 46bbfd3b..8a8d4830 100644 --- a/elf/elf_module.h +++ b/elf/elf_module.h @@ -73,4 +73,6 @@ extern int module_load(struct elf_module *module); // Unloads the module from the system and releases all the associated memory extern int module_unload(struct elf_module *module); +extern Elf32_Sym *module_find_symbol(const char *name, struct elf_module *module); + #endif /*ELF_MODULE_H_*/ diff --git a/elf/elf_utils.c b/elf/elf_utils.c new file mode 100644 index 00000000..b96d1ec6 --- /dev/null +++ b/elf/elf_utils.c @@ -0,0 +1,16 @@ +#include "elf_utils.h" + +unsigned long elf_hash(const unsigned char *name) { + unsigned long h = 0; + unsigned long g; + + while (*name) { + h = (h << 4) + *name++; + if ((g = h & 0xF0000000)) + h ^= g >> 24; + + h &= ~g; + } + + return h; +} diff --git a/elf/elf_utils.h b/elf/elf_utils.h index 5e7387f3..604daa58 100644 --- a/elf/elf_utils.h +++ b/elf/elf_utils.h @@ -23,4 +23,6 @@ static inline Elf32_Phdr *elf_get_ph(void *elf_image, int index) { return (Elf32_Phdr*)((Elf32_Off)elf_pht + index * elf_hdr->e_phentsize); } +extern unsigned long elf_hash(const unsigned char *name); + #endif /*ELF_UTILS_H_*/ diff --git a/elf/elftest.c b/elf/elftest.c index 6462ad6a..3495ad11 100644 --- a/elf/elftest.c +++ b/elf/elftest.c @@ -11,7 +11,7 @@ void print_usage() { fprintf(stderr, "Usage:\n"); - fprintf(stderr, "\telftest objfile\n"); + fprintf(stderr, "\telftest objfile [symbol ...]\n"); } int main(int argc, char **argv) { @@ -23,7 +23,7 @@ int main(int argc, char **argv) { argc--; argv++; - if (argc != 1) { + if (argc < 1) { print_usage(); return 1; } @@ -51,6 +51,21 @@ int main(int argc, char **argv) { goto error; } + argc--; + argv++; + + while (argc > 0) { + if (module_find_symbol(argv[0], module) != NULL) { + printf("Symbol %s found\n", argv[0]); + } else { + printf("Symbol %s not found\n", argv[0]); + } + + argc--; + argv++; + } + + module_unload(module); modules_term(); |