Skip to content

Instantly share code, notes, and snippets.

@el2ro
Created February 10, 2014 11:54
Show Gist options
  • Save el2ro/8914557 to your computer and use it in GitHub Desktop.
Save el2ro/8914557 to your computer and use it in GitHub Desktop.
Get symbols and offset using BFD from unlinked / linked kernel module
if ( 1 ) {
bfd *abfd;
char * target;
char **matching;
asection *asection_p;
fprintf(stderr, "section not found bfd time (%s) (0x%lx)\n",path_.c_str(),load_address);
bfd_init();
abfd = bfd_openr(path_.c_str(),target);
if (bfd_check_format_matches (abfd, bfd_object, &matching)) {
fprintf(stderr, "found object section %d\n",bfd_count_sections(abfd));
fprintf(stderr, "obj architecture = %d\n",bfd_get_arch_size(abfd));
unsigned int opb = bfd_octets_per_byte (abfd);
asection_p = bfd_get_section_by_name(abfd, ".text");
if (asection_p != NULL) {
fprintf(stderr, ".TEXT FOUND!\n");
fprintf(stderr, "section (%s) size(%lx)\n",bfd_get_section_name (abfd, asection_p),(unsigned long) bfd_section_size (abfd, asection_p) / opb);
fprintf(stderr, "Entry point is at address 0x%lx\n",bfd_ge§t_start_address(abfd));
if (bfd_section_lma (abfd,asection_p) != bfd_section_vma (abfd,asection_p)) {
fprintf(stderr,"loadable section %s: lma = 0x%08x (vma = 0x%08x) size = 0x%08x.\n", bfd_section_name (abfd,asection_p), (unsigned int) bfd_section_lma (abfd,asection_p), (unsigned int) bfd_section_vma (abfd,asection_p), (unsigned int) bfd_section_size (abfd,asection_p));
} else {
fprintf(stderr,"loadable section %s: addr = 0x%08x size = 0x%08x.\n", bfd_section_name (abfd,asection_p), (unsigned int) bfd_section_lma (abfd,asection_p), (unsigned int) bfd_section_size (abfd,asection_p));
}
/*
* LOOK FOR SYMBOLS FROM UNLINKED / LINKED MODULE !
*/
unsigned int storage_needed;
asymbol **symbol_table;
unsigned int number_of_symbols;
unsigned int i;
symbol_info syminfo;
storage_needed = bfd_get_symtab_upper_bound(abfd);
if (storage_needed == 0) {
return found;
}
symbol_table = (asymbol **) malloc(storage_needed);
number_of_symbols =
bfd_canonicalize_symtab(abfd, symbol_table);
for (i = 0; i < number_of_symbols; i++) {
if ( !(symbol_table[i]->flags & ( BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT )) &&
symbol_table[i]->flags & ( BSF_LOCAL | /*BSF_COMMON | BSF_UNDEFINED |*/ BSF_GLOBAL) /*BSF_LOCAL*/)
{
bfd_symbol_info(symbol_table[i], &syminfo);
fprintf(stderr, "symbol name (%s) (0x%lx) flags(0x%x) \n",symbol_table[i]->name, (syminfo.value), symbol_table[i]->flags);
}
}
}
}
bfd_close (abfd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment