예제 #1
0
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 obj_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;
    }

    /*  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 (obj_section_index = 0; obj_section_index < section_count;
        ++obj_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,
            obj_section_index,
            &doas, &err);
        if (res == DW_DLV_ERROR){
            DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
        }

        scn_name = doas.name;

        if (!this_section_dwarf_relevant(scn_name,doas.type) ) {
            continue;
        } else {
            /*  Build up the sections table and the
                de_debug* etc pointers in Dwarf_Debug. */
            struct Dwarf_dbg_sect_s *section;

            int found_match = FALSE;
            unsigned initial_start_number = 0;
            unsigned dbg_section_number = 0;
            res = is_section_known_already(dbg,scn_name,
                &dbg_section_number,
                initial_start_number,
                &err);
            if (res == DW_DLV_OK) {
                /* DUPLICATE */
                DWARF_DBG_ERROR(dbg, DW_DLE_SECTION_DUPLICATION,
                    DW_DLV_ERROR);
            } else if (res == DW_DLV_ERROR) {
                DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
            }
            /* No entry: new-to-us section, the normal case. */
            res = enter_section_in_de_debug_sections_array(dbg,scn_name,&err);
            if (res == DW_DLV_OK) {
                /*  We just added a new entry in the dbg
                    de_debug_sections array.  So we know its number. */
                unsigned real_start_number =
                    dbg->de_debug_sections_total_entries-1;
                res = is_section_known_already(dbg,scn_name,
                    &dbg_section_number,
                    real_start_number,
                    &err);
                if (res == DW_DLV_OK) {
                    section = &dbg->de_debug_sections[dbg_section_number];
                    res = get_basic_section_data(dbg,
                        section->ds_secdata, &doas,
                        obj_section_index, error,
                        section->ds_duperr,
                        section->ds_emptyerr);
                    if (res != DW_DLV_OK) {
                        return res;
                    }
                    sections[obj_section_index] = section->ds_secdata;
                    foundDwarf += section->ds_have_dwarf;
                    found_match = TRUE;
                    /*  Normal section set up.
                        Fall through. */
                }else if (res == DW_DLV_NO_ENTRY) {
                    /*  Some sort of bug in the code here.
                        Should be impossible to get here. */
                    DWARF_DBG_ERROR(dbg, DW_DLE_SECTION_ERROR, DW_DLV_ERROR);
                } else {
                    DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
                }
            } else if (res == DW_DLV_NO_ENTRY) {
                /*  We get here for relocation sections.
                    Fall through. */
            } else {
                DWARF_DBG_ERROR(dbg, err, DW_DLV_ERROR);
            }

            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,
                        obj_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;
}
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;
}