/* * Lookup an address * - modname is set to NULL if it's in the kernel. * - We guarantee that the returned name is valid until we reschedule even if. * It resides in a module. * - We also guarantee that modname will be valid until rescheduled. */ const char *kallsyms_lookup(unsigned long addr, unsigned long *symbolsize, unsigned long *offset, char **modname, char *namebuf) { namebuf[KSYM_NAME_LEN - 1] = 0; namebuf[0] = 0; if (is_kernel_text(addr)) { unsigned long pos; pos = get_symbol_pos(addr, symbolsize, offset); /* Grab name */ kallsyms_expand_symbol(get_symbol_offset(pos), namebuf); if (modname) *modname = NULL; return namebuf; } /* moduled not yet supported in kallsyms */ return NULL; }
/* archive file (*.a) loading test */ int main() { int ret; int symnum; bfd* abfd; asymbol** syms; int symbol_pos, symbol_size; int index; #ifndef OBJ_TEST const char* file = "foo.a"; #else const char* file = "hello.o"; #endif unsigned char* file_o; file_o = load_file(file); abfd = bfd_openr(file, NULL); assert(abfd); ret = bfd_check_format(abfd, bfd_archive); //ret = bfd_check_format(abfd, bfd_object); assert(ret); #ifndef OBJ_TEST bfd* b = NULL; #else bfd* b = abfd;//NULL; #endif link_list_t* bfds = NULL; STEP_LOG("create function map\n"); #ifndef OBJ_TEST while(NULL != (b = bfd_openr_next_archived_file(abfd, b))) #endif { ret = bfd_check_format(b, bfd_object); assert(ret); // STEP_LOG("next\n"); if (!(bfd_get_file_flags(b) & HAS_SYMS)) { assert(bfd_get_error() == bfd_error_no_error); /* no symbol */ bfd_close(abfd); return 1; } if (NULL == bfds) { LOG("Add first:0x%08X\n", (int)b); bfds = add_item(bfds, &b, sizeof(b)); } else { LOG("Add bfd:0x%08X\n", (int)b); add_item(bfds, &b, sizeof(b)); } get_symbols(&syms, &symnum, b); create_symbol_function_pos(file_o, b, syms, symnum); } STEP_LOG("relocate function addresses\n"); link_list_t* list = bfds; while(NULL != list) { b = *(bfd**)(list->item); get_symbols(&syms, &symnum, b); reloc_file(file_o, b, syms); list = list->next; } STEP_LOG("try to get function addressses\n"); int (*func)(); void (*func1)(const char*); func = NULL; func1 = NULL; list = bfds; while(NULL != list) { b = *(bfd**)(list->item); LOG("bfd:0x%08X\n", (int)b); LOG("call goodby...."); get_symbols(&syms, &symnum, b); symbol_pos = get_symbol_pos("goodby", b, syms, symnum, file_o); if (0 != symbol_pos) { func = (int (*) ())(get_function(symbol_pos)); } LOG("call hello_someone...."); symbol_pos = get_symbol_pos("hello_someone", b, syms, symnum, file_o); if (0 != symbol_pos) { func1 = (void (*) (const char*))(get_function(symbol_pos)); } if (NULL != func && NULL != func1) break; list = list->next; } STEP_LOG("try to call functions\n"); if (NULL != func) { func(); } else { LOG("failed to call func\n"); } if (NULL != func1) { func1("WORLD!"); } else { LOG("failed to call func\n"); } delete_all_items(bfds); free(syms); bfd_close(abfd); free(file_o); return 0; }