aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Bucur <stefanb@zytor.com>2008-06-11 15:20:25 +0300
committerStefan Bucur <stefanb@zytor.com>2008-06-11 15:20:25 +0300
commit19f9ce0fe3b9f2a1e6ed2ef0c46d99f87066a4c4 (patch)
tree54e9009fb770c4d8b15b55afd42b8985a272e39e
parent3254fd09e82313547090e449fd1870f17b88c0c1 (diff)
downloadsyslinux-elf-19f9ce0fe3b9f2a1e6ed2ef0c46d99f87066a4c4.tar.gz
syslinux-elf-19f9ce0fe3b9f2a1e6ed2ef0c46d99f87066a4c4.tar.xz
syslinux-elf-19f9ce0fe3b9f2a1e6ed2ef0c46d99f87066a4c4.zip
Added support for searching symbols
-rw-r--r--elf/Makefile2
-rw-r--r--elf/elf_module.c22
-rw-r--r--elf/elf_module.h2
-rw-r--r--elf/elf_utils.c16
-rw-r--r--elf/elf_utils.h2
-rw-r--r--elf/elftest.c19
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();