/* If running this long set of tests is slow enough to matter one could set up a local tsearch tree with all this content and search it instead of this set of sequential tests. */ static int enter_section_in_de_debug_sections_array(Dwarf_Debug dbg, const char *scn_name, int *err) { int sectionerr = 0; /* Setup the table that contains the basic information about the sections that are DWARF related. The entries are very unlikely to change very often. */ if(!strcmp(scn_name,".debug_info")) { sectionerr = add_debug_section_info(dbg,".debug_info", &dbg->de_debug_info, DW_DLE_DEBUG_INFO_DUPLICATE,DW_DLE_DEBUG_INFO_NULL, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_info.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_info.dwo", &dbg->de_debug_info, DW_DLE_DEBUG_INFO_DUPLICATE,DW_DLE_DEBUG_INFO_NULL, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_types")) { sectionerr = add_debug_section_info(dbg,".debug_types", &dbg->de_debug_types, DW_DLE_DEBUG_TYPES_DUPLICATE,DW_DLE_DEBUG_TYPES_NULL, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_types.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_types.dwo", &dbg->de_debug_types, DW_DLE_DEBUG_TYPES_DUPLICATE,DW_DLE_DEBUG_TYPES_NULL, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_abbrev")) { sectionerr = add_debug_section_info(dbg,".debug_abbrev", &dbg->de_debug_abbrev, /*03*/ DW_DLE_DEBUG_ABBREV_DUPLICATE,DW_DLE_DEBUG_ABBREV_NULL, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_abbrev.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_abbrev.dwo", &dbg->de_debug_abbrev, /*03*/ DW_DLE_DEBUG_ABBREV_DUPLICATE,DW_DLE_DEBUG_ABBREV_NULL, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_aranges")) { sectionerr = add_debug_section_info(dbg,".debug_aranges", &dbg->de_debug_aranges, DW_DLE_DEBUG_ARANGES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_aranges.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_aranges.dwo", &dbg->de_debug_aranges, DW_DLE_DEBUG_ARANGES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_line")) { sectionerr = add_debug_section_info(dbg,".debug_line", &dbg->de_debug_line, DW_DLE_DEBUG_LINE_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_line.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_line.dwo", &dbg->de_debug_line, DW_DLE_DEBUG_LINE_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_frame")) { sectionerr = add_debug_section_info(dbg,".debug_frame", &dbg->de_debug_frame, DW_DLE_DEBUG_FRAME_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".eh_frame")) { /* gnu egcs-1.1.2 data */ sectionerr = add_debug_section_info(dbg,".eh_frame", &dbg->de_debug_frame_eh_gnu, DW_DLE_DEBUG_FRAME_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_loc")) { sectionerr = add_debug_section_info(dbg,".debug_loc", &dbg->de_debug_loc, DW_DLE_DEBUG_LOC_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_loc.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_loc.dwo", &dbg->de_debug_loc, DW_DLE_DEBUG_LOC_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_pubnames")) { sectionerr = add_debug_section_info(dbg,".debug_pubnames", &dbg->de_debug_pubnames, DW_DLE_DEBUG_PUBNAMES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_str")) { sectionerr = add_debug_section_info(dbg,".debug_str", &dbg->de_debug_str, DW_DLE_DEBUG_STR_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_str.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_str.dwo", &dbg->de_debug_str, DW_DLE_DEBUG_STR_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_funcnames")) { /* SGI IRIX-only. */ sectionerr = add_debug_section_info(dbg,".debug_funcnames", &dbg->de_debug_funcnames, /*11*/ DW_DLE_DEBUG_FUNCNAMES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_typenames")) { /* SGI IRIX-only, created years before DWARF3. Content essentially identical to .debug_pubtypes. */ sectionerr = add_debug_section_info(dbg,".debug_typenames", &dbg->de_debug_typenames, /*12*/ DW_DLE_DEBUG_TYPENAMES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_pubtypes")) { /* Section new in DWARF3. */ sectionerr = add_debug_section_info(dbg,".debug_pubtypes", &dbg->de_debug_pubtypes, /*13*/ DW_DLE_DEBUG_PUBTYPES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_varnames")) { /* SGI IRIX-only. */ sectionerr = add_debug_section_info(dbg,".debug_varnames", &dbg->de_debug_varnames, DW_DLE_DEBUG_VARNAMES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_weaknames")) { /* SGI IRIX-only. */ sectionerr = add_debug_section_info(dbg,".debug_weaknames", &dbg->de_debug_weaknames, DW_DLE_DEBUG_WEAKNAMES_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_macinfo")) { sectionerr = add_debug_section_info(dbg,".debug_macinfo", &dbg->de_debug_macinfo, DW_DLE_DEBUG_MACINFO_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_macinfo.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_macinfo.dwo", &dbg->de_debug_macinfo, DW_DLE_DEBUG_MACINFO_DUPLICATE,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_ranges")) { sectionerr = add_debug_section_info(dbg,".debug_ranges", &dbg->de_debug_ranges, DW_DLE_DEBUG_RANGES_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_ranges.dwo")) { sectionerr = add_debug_section_info(dbg,".debug_ranges.dwo", &dbg->de_debug_ranges, DW_DLE_DEBUG_RANGES_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_str_offsets")) { /* New DWARF5 */ sectionerr = add_debug_section_info(dbg,".debug_str_offsets", &dbg->de_debug_str_offsets, DW_DLE_DEBUG_STR_OFFSETS_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_str_offsets.dwo")) { /* New DWARF5 */ sectionerr = add_debug_section_info(dbg,".debug_str_offsets.dwo", &dbg->de_debug_str_offsets, DW_DLE_DEBUG_STR_OFFSETS_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".symtab")) { sectionerr = add_debug_section_info(dbg,".symtab", &dbg->de_elf_symtab, DW_DLE_DEBUG_SYMTAB_ERR,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".strtab")) { sectionerr = add_debug_section_info(dbg,".strtab", &dbg->de_elf_strtab, DW_DLE_DEBUG_STRTAB_ERR,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_addr")) { /* New DWARF5 */ sectionerr = add_debug_section_info(dbg,".debug_addr", &dbg->de_debug_addr, DW_DLE_DEBUG_ADDR_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_addr.dwo")) { /* New DWARF5 */ sectionerr = add_debug_section_info(dbg,".debug_addr.dwo", &dbg->de_debug_addr, DW_DLE_DEBUG_ADDR_DUPLICATE,0, TRUE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".gdb_index")) { /* gdb added this. */ sectionerr = add_debug_section_info(dbg,".gdb_index", &dbg->de_debug_gdbindex, DW_DLE_DUPLICATE_GDB_INDEX,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_cu_index")) { /* gdb added this. */ sectionerr = add_debug_section_info(dbg,".debug_cu_index", &dbg->de_debug_cu_index, DW_DLE_DUPLICATE_CU_INDEX,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } if(!strcmp(scn_name,".debug_tu_index")) { /* gdb added this. */ sectionerr = add_debug_section_info(dbg,".debug_tu_index", &dbg->de_debug_tu_index, DW_DLE_DUPLICATE_TU_INDEX,0, FALSE,err); if (sectionerr != DW_DLV_OK) { return sectionerr; } return DW_DLV_OK; } return DW_DLV_NO_ENTRY; }
static int _dwarf_setup(Dwarf_Debug dbg, Dwarf_Error * error) { const char *scn_name = 0; int foundDwarf = 0; struct Dwarf_Obj_Access_Interface_s * obj = 0; Dwarf_Endianness endianness; /* Table with pointers to debug sections */ struct Dwarf_Section_s **sections = 0; Dwarf_Unsigned section_count = 0; Dwarf_Half section_index = 0; foundDwarf = FALSE; dbg->de_assume_string_in_bounds = _dwarf_assume_string_in_bounds; dbg->de_same_endian = 1; dbg->de_copy_word = memcpy; obj = dbg->de_obj_file; endianness = obj->methods->get_byte_order(obj->object); #ifdef WORDS_BIGENDIAN dbg->de_big_endian_object = 1; if (endianness == DW_OBJECT_LSB ) { dbg->de_same_endian = 0; dbg->de_big_endian_object = 0; dbg->de_copy_word = _dwarf_memcpy_swap_bytes; } #else /* little endian */ dbg->de_big_endian_object = 0; if (endianness == DW_OBJECT_MSB ) { dbg->de_same_endian = 0; dbg->de_big_endian_object = 1; dbg->de_copy_word = _dwarf_memcpy_swap_bytes; } #endif /* !WORDS_BIGENDIAN */ /* The following de_length_size is Not Too Significant. Only used one calculation, and an approximate one at that. */ dbg->de_length_size = obj->methods->get_length_size(obj->object); dbg->de_pointer_size = obj->methods->get_pointer_size(obj->object); /* For windows always is 4 ? */ #ifdef WIN32 dbg->de_pointer_size = 4; #endif /* WIN32 */ section_count = obj->methods->get_section_count(obj->object); /* Allocate space to record references to debug sections, that can be referenced by RELA sections in the 'sh_info' field. */ sections = (struct Dwarf_Section_s **)calloc(section_count + 1, sizeof(struct Dwarf_Section_s *)); if (!sections) { /* Impossible case, we hope. Give up. */ return DW_DLV_ERROR; } /* Setup the table that contains the basic information about the sections that are DWARF related. The entries are very unlikely to change very often. */ add_debug_section_info(dbg,".debug_info",&dbg->de_debug_info, /*01*/ DW_DLE_DEBUG_INFO_DUPLICATE,DW_DLE_DEBUG_INFO_NULL, TRUE); add_debug_section_info(dbg,".debug_types",&dbg->de_debug_types, /*02*/ DW_DLE_DEBUG_TYPES_DUPLICATE,DW_DLE_DEBUG_TYPES_NULL, TRUE); add_debug_section_info(dbg,".debug_abbrev",&dbg->de_debug_abbrev, /*03*/ DW_DLE_DEBUG_ABBREV_DUPLICATE,DW_DLE_DEBUG_ABBREV_NULL, FALSE); add_debug_section_info(dbg,".debug_aranges",&dbg->de_debug_aranges, /*04*/ DW_DLE_DEBUG_ARANGES_DUPLICATE,0, FALSE); add_debug_section_info(dbg,".debug_line",&dbg->de_debug_line, /*05*/ DW_DLE_DEBUG_LINE_DUPLICATE,0, FALSE); add_debug_section_info(dbg,".debug_frame",&dbg->de_debug_frame, /*06*/ DW_DLE_DEBUG_FRAME_DUPLICATE,0, TRUE); /* gnu egcs-1.1.2 data */ add_debug_section_info(dbg,".eh_frame",&dbg->de_debug_frame_eh_gnu, /*07*/ DW_DLE_DEBUG_FRAME_DUPLICATE,0, TRUE); add_debug_section_info(dbg,".debug_loc",&dbg->de_debug_loc, /*08*/ DW_DLE_DEBUG_LOC_DUPLICATE,0, FALSE); add_debug_section_info(dbg,".debug_pubnames",&dbg->de_debug_pubnames,/*09*/ DW_DLE_DEBUG_PUBNAMES_DUPLICATE,0, FALSE); add_debug_section_info(dbg,".debug_str",&dbg->de_debug_str, /*10*/ DW_DLE_DEBUG_STR_DUPLICATE,0, FALSE); /* SGI IRIX-only. */ add_debug_section_info(dbg,".debug_funcnames",&dbg->de_debug_funcnames, /*11*/ DW_DLE_DEBUG_FUNCNAMES_DUPLICATE,0, FALSE); /* SGI IRIX-only, created years before DWARF3. Content essentially identical to .debug_pubtypes. */ add_debug_section_info(dbg,".debug_typenames",&dbg->de_debug_typenames, /*12*/ DW_DLE_DEBUG_TYPENAMES_DUPLICATE,0, FALSE); /* Section new in DWARF3. */ add_debug_section_info(dbg,".debug_pubtypes",&dbg->de_debug_pubtypes, /*13*/ DW_DLE_DEBUG_PUBTYPES_DUPLICATE,0, FALSE); /* SGI IRIX-only. */ add_debug_section_info(dbg,".debug_varnames",&dbg->de_debug_varnames, /*14*/ DW_DLE_DEBUG_VARNAMES_DUPLICATE,0, FALSE); /* SGI IRIX-only. */ add_debug_section_info(dbg,".debug_weaknames",&dbg->de_debug_weaknames, /*15*/ DW_DLE_DEBUG_WEAKNAMES_DUPLICATE,0, FALSE); add_debug_section_info(dbg,".debug_macinfo",&dbg->de_debug_macinfo, /*16*/ DW_DLE_DEBUG_MACINFO_DUPLICATE,0, FALSE); add_debug_section_info(dbg,".debug_ranges",&dbg->de_debug_ranges, /*17*/ DW_DLE_DEBUG_RANGES_DUPLICATE,0, TRUE); add_debug_section_info(dbg,".symtab",&dbg->de_elf_symtab, /*18*/ DW_DLE_DEBUG_SYMTAB_ERR,0, FALSE); add_debug_section_info(dbg,".strtab",&dbg->de_elf_strtab, /*19*/ DW_DLE_DEBUG_STRTAB_ERR,0, FALSE); /* Newtype macros. */ add_debug_section_info(dbg,".debug_macro",&dbg->de_debug_macro, /*20*/ DW_DLE_DEBUG_MACRO_DUPLICATE,0, FALSE); /* We can skip index 0 when considering ELF files, but not other object types. Indeed regardless of the object type we should skip section 0 here. This is a convention. We depend on it. Non-elf object access code should (in itself) understand we will index beginning at 1 and adjust itself to deal with this Elf convention. Without this convention various parts of the code in this file won't work correctly. A dss_index of 0 must not be used, even though we start at 0 here. So the get_section_info() must adapt to the situation (the elf version does automatically as a result of Elf having a section zero with zero length and an empty name). */ for (section_index = 0; section_index < section_count; ++section_index) { struct Dwarf_Obj_Access_Section_s doas; int res = DW_DLV_ERROR; int err = 0; memset(&doas,0,sizeof(doas)); res = obj->methods->get_section_info(obj->object, section_index, &doas, &err); if (res == DW_DLV_ERROR){ DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR); } scn_name = doas.name; if (strncmp(scn_name, ".debug_", 7) && strcmp(scn_name, ".eh_frame") && strcmp(scn_name, ".symtab") && strcmp(scn_name, ".strtab") && strncmp(scn_name, ".rela.",6) /* For an object file with incorrect rela section name, readelf prints correct debug information, as the tool takes the section type instead of the section name. Include the incorrect section name, until this test uses the section type. */ && doas.type != SHT_RELA) { continue; } else { /* Search the debug sections table for a match */ struct Dwarf_dbg_sect_s *section; unsigned i = 0; int found_match = FALSE; for (i = 0; i < dbg->de_debug_sections_total_entries; ++i) { section = &dbg->de_debug_sections[i]; if (strcmp(scn_name, section->ds_name) == 0) { res = get_basic_section_data(dbg, section->ds_secdata, &doas, section_index, error, section->ds_duperr, section->ds_emptyerr); if (res != DW_DLV_OK) { return res; } /* Mark the entry as debug section related */ sections[section_index] = section->ds_secdata; foundDwarf += section->ds_have_dwarf; found_match = TRUE; break; } } if (!found_match) { /* For an object file with incorrect rela section name, the 'readelf' tool, prints correct debug information, as the tool takes the section type instead of the section name. If the current section is a RELA one and the 'sh_info' refers to a debug section, add the relocation data. */ if (doas.type == SHT_RELA && sections[doas.info]) { add_rela_data(sections[doas.info],&doas,section_index); } } /* Fetch next section */ } } /* Free table with section information. */ if (sections){ free(sections); } if (foundDwarf) { return DW_DLV_OK; } return DW_DLV_NO_ENTRY; }