static VALUE symbol_new(bfd * abfd, asymbol *s, char is_dynamic) { symbol_info info; VALUE class, instance; VALUE argv[1] = { Qnil }; class = rb_class_new(clsSymbol); instance = Data_Wrap_Struct(class, NULL, NULL, s); rb_obj_call_init(instance, 0, argv); bfd_symbol_info(s, &info); /* set instance variables */ rb_iv_set(instance, IVAR(SYM_ATTR_NAME), rb_str_new_cstr(info.name) ); rb_iv_set(instance, IVAR(SYM_ATTR_TYPE), INT2NUM((int) info.type) ); rb_iv_set(instance, IVAR(SYM_ATTR_VALUE), SIZET2NUM(info.value) ); rb_iv_set(instance, IVAR(SYM_ATTR_FLAGS), INT2NUM(s->flags) ); rb_iv_set(instance, IVAR(SYM_ATTR_BIND), rb_str_new_cstr( (is_dynamic ? SYM_BIND_DYNAMIC : SYM_BIND_STATIC) ) ); if ( s->section ) { rb_iv_set(instance, IVAR(SYM_ATTR_SECTION), rb_str_new_cstr(s->section->name)); } return instance; }
int main(int argc, char *argv[]) { bfd *abfd; bfd_init(); abfd = bfd_openr(argv[1], NULL); if (abfd == NULL) { bfd_perror("bfd_openr"); exit(1); } if (! bfd_check_format(abfd, bfd_object)) { bfd_perror("bfd_check_format"); } printf("SYMBOL TABLE:\n"); { long storage_needed; asymbol **symbol_table; long number_of_symbols; long i; storage_needed = bfd_get_symtab_upper_bound (abfd); printf("storage_need=%d\n", storage_needed); if (storage_needed < 0) { bfd_perror("bfd_get_symtab_upper_bound"); exit(1); } if (storage_needed == 0) { printf("no symbols\n"); exit(0); } symbol_table = (asymbol **)malloc (storage_needed); number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); if (number_of_symbols < 0) { bfd_perror("bfd_canonicalize_symtab"); exit(1); } for (i = 0; i < number_of_symbols; i++) { asymbol *asym = symbol_table[i]; int symclass = bfd_decode_symclass(asym); symbol_info syminfo; bfd_symbol_info(asym, &syminfo); bfd_print_symbol_vandf(abfd, stdout, asym); printf(" 0x%x %s ", symclass, bfd_is_undefined_symclass(symclass) ? "?" : " "); printf(" %s ", bfd_asymbol_name(asym)); printf("%p ", bfd_asymbol_value(asym)); // printf(" %d ", syminfo.value); /* asymbol_value */ // printf(" %d ", syminfo.type); /* symclass */ // printf(" %s ", syminfo.name); /* asymbol_name */ printf(" %d ", syminfo.stab_type); printf(" %d ", syminfo.stab_other); printf(" %d ", syminfo.stab_desc); // printf(" %s ", syminfo.stab_name); printf("\n"); } } printf("DYNAMIC SYMBOL TABLE:\n"); { long storage_needed; asymbol **symbol_table; long number_of_symbols; long i; storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd); printf("storage_need=%d\n", storage_needed); if (storage_needed < 0) { bfd_perror("bfd_get_symtab_upper_bound"); exit(1); } if (storage_needed == 0) { printf("no symbols\n"); exit(0); } symbol_table = (asymbol **)malloc (storage_needed); number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table); if (number_of_symbols < 0) { bfd_perror("bfd_canonicalize_symtab"); exit(1); } for (i = 0; i < number_of_symbols; i++) { asymbol *asym = symbol_table[i]; int symclass = bfd_decode_symclass(asym); symbol_info syminfo; bfd_symbol_info(asym, &syminfo); bfd_print_symbol_vandf(abfd, stdout, asym); printf(" 0x%x %s ", symclass, bfd_is_undefined_symclass(symclass) ? "?" : " "); printf(" %s ", bfd_asymbol_name(asym)); printf("%p ", bfd_asymbol_value(asym)); // printf(" %d ", syminfo.value); /* asymbol_value */ // printf(" %d ", syminfo.type); /* symclass */ // printf(" %s ", syminfo.name); /* asymbol_name */ printf(" %d ", syminfo.stab_type); printf(" %d ", syminfo.stab_other); printf(" %d ", syminfo.stab_desc); // printf(" %s ", syminfo.stab_name); printf("\n"); } } exit(0); }
static int printStackTrace(JNIEnv *env, jobject sobj, StackTrace tr) { bfd *abfd; long storage_needed; asymbol **symbol_table; symtab_entry *symtab; long number_of_symbols; long number_of_text_symbols; long i, j; StackTrace curr; bfd_init(); abfd = bfd_openr(name_of_binary, "default"); bfd_set_format(abfd, bfd_object); if (!bfd_check_format(abfd, bfd_object)) { printf("Error(1) in printStackTrace\n"); return(1); } storage_needed = bfd_get_symtab_upper_bound(abfd); if (storage_needed < 0) { printf("Error(2) in printStackTrace\n"); return(2); } if (storage_needed == 0) { printf("Error(3) no symbols\n"); return(3); } symbol_table = (asymbol **) xmalloc (storage_needed); number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); if (number_of_symbols < 0) { printf("Error(4) in printStackTrace\n"); return(4); } /* count number of symbols in text segment */ for (i = number_of_text_symbols = 0; i < number_of_symbols; i++) if (bfd_decode_symclass(symbol_table[i]) == 'T') number_of_text_symbols++; if (number_of_text_symbols < 0) { printf("Error(5) in printStackTrace\n"); return(5); } if (number_of_text_symbols == 0) { printf("Error(6) No symbols in text segment.\n"); return(6); } symtab = (symtab_entry *) malloc(number_of_text_symbols * sizeof(symtab_entry)); for (i = j = 0; i < number_of_symbols; i++) { symbol_info si; bfd_symbol_info(symbol_table[i], &si); if (bfd_decode_symclass(symbol_table[i]) == 'T') { if (j >= number_of_text_symbols) { printf("Error(7) in printStackTrace\n"); return(7); } (symtab+j)->value = (unsigned long)si.value; (symtab+j)->name = si.name; j++; } } qsort(symtab, number_of_text_symbols, sizeof(symtab_entry), compare_symtab_entry); curr = tr; while(curr != NULL) { symtab_entry *found = bsearch_symtab((symvalue)curr->retaddr, symtab, number_of_text_symbols); printItem(env, sobj, found, curr); curr = curr->next; } /* clean up */ free(symtab); bfd_close(abfd); return(0); }