Blame libdwarf/dwarf_abbrev.c

Packit cdaae3
/*
Packit cdaae3
Packit cdaae3
  Copyright (C) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
Packit cdaae3
  Portions Copyright (C) 2009-2011 David Anderson. All Rights Reserved.
Packit cdaae3
Packit cdaae3
  This program is free software; you can redistribute it and/or modify it
Packit cdaae3
  under the terms of version 2.1 of the GNU Lesser General Public License
Packit cdaae3
  as published by the Free Software Foundation.
Packit cdaae3
Packit cdaae3
  This program is distributed in the hope that it would be useful, but
Packit cdaae3
  WITHOUT ANY WARRANTY; without even the implied warranty of
Packit cdaae3
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Packit cdaae3
Packit cdaae3
  Further, this software is distributed without any warranty that it is
Packit cdaae3
  free of the rightful claim of any third person regarding infringement
Packit cdaae3
  or the like.  Any license provided herein, whether implied or
Packit cdaae3
  otherwise, applies only to this software file.  Patent licenses, if
Packit cdaae3
  any, provided herein do not apply to combinations of this program with
Packit cdaae3
  other software, or any other product whatsoever.
Packit cdaae3
Packit cdaae3
  You should have received a copy of the GNU Lesser General Public
Packit cdaae3
  License along with this program; if not, write the Free Software
Packit cdaae3
  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
Packit cdaae3
  USA.
Packit cdaae3
Packit cdaae3
*/
Packit cdaae3
Packit cdaae3
#include "config.h"
Packit cdaae3
#include "dwarf_incl.h"
Packit cdaae3
#include <stdio.h>
Packit cdaae3
#include "dwarf_abbrev.h"
Packit cdaae3
Packit cdaae3
/*  This is used to print a .debug_abbrev section without
Packit cdaae3
    knowing about the DIEs that use the abbrevs.
Packit cdaae3
Packit cdaae3
    dwarf_get_abbrev() and,
Packit cdaae3
    in dwarf_util.c,  _dwarf_get_abbrev_for_code()
Packit cdaae3
Packit cdaae3
Packit cdaae3
    When we have a simple .o
Packit cdaae3
    there is at least a hope of iterating through
Packit cdaae3
    the abbrevs meaningfully without knowing
Packit cdaae3
    a CU context.
Packit cdaae3
Packit cdaae3
    This often fails or gets incorrect info
Packit cdaae3
    because there is no guarantee the .debug_abbrev
Packit cdaae3
    section is free of garbage bytes.
Packit cdaae3
Packit cdaae3
    In an object with multiple CU/TUs the
Packit cdaae3
    output is difficult/impossible to usefully interpret.
Packit cdaae3
Packit cdaae3
    In a dwp (Package File)  it is really impossible
Packit cdaae3
    to associate abbrevs with a CU.
Packit cdaae3
Packit cdaae3
*/
Packit cdaae3
Packit cdaae3
int
Packit cdaae3
dwarf_get_abbrev(Dwarf_Debug dbg,
Packit cdaae3
    Dwarf_Unsigned offset,
Packit cdaae3
    Dwarf_Abbrev * returned_abbrev,
Packit cdaae3
    Dwarf_Unsigned * length,
Packit cdaae3
    Dwarf_Unsigned * abbr_count, Dwarf_Error * error)
Packit cdaae3
{
Packit cdaae3
    Dwarf_Small *abbrev_ptr = 0;
Packit cdaae3
    Dwarf_Small *abbrev_section_end = 0;
Packit cdaae3
    Dwarf_Half attr         = 0;
Packit cdaae3
    Dwarf_Half attr_form    = 0;
Packit cdaae3
    Dwarf_Abbrev ret_abbrev = 0;
Packit cdaae3
    Dwarf_Unsigned labbr_count = 0;
Packit cdaae3
    Dwarf_Unsigned utmp     = 0;
Packit cdaae3
Packit cdaae3
    if (!dbg) {
Packit cdaae3
        _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
Packit cdaae3
        return DW_DLV_ERROR;
Packit cdaae3
    }
Packit cdaae3
    if (dbg->de_debug_abbrev.dss_data == 0) {
Packit cdaae3
        /*  Loads abbrev section (and .debug_info as we do those
Packit cdaae3
            together). */
Packit cdaae3
        int res = _dwarf_load_debug_info(dbg, error);
Packit cdaae3
Packit cdaae3
        if (res != DW_DLV_OK) {
Packit cdaae3
            return res;
Packit cdaae3
        }
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    if (offset >= dbg->de_debug_abbrev.dss_size) {
Packit cdaae3
        return DW_DLV_NO_ENTRY;
Packit cdaae3
    }
Packit cdaae3
    ret_abbrev = (Dwarf_Abbrev) _dwarf_get_alloc(dbg, DW_DLA_ABBREV, 1);
Packit cdaae3
    if (ret_abbrev == NULL) {
Packit cdaae3
        _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
Packit cdaae3
        return DW_DLV_ERROR;
Packit cdaae3
    }
Packit cdaae3
    ret_abbrev->dab_dbg = dbg;
Packit cdaae3
    if (returned_abbrev == 0 || abbr_count == 0) {
Packit cdaae3
        dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV);
Packit cdaae3
        _dwarf_error(dbg, error, DW_DLE_DWARF_ABBREV_NULL);
Packit cdaae3
        return DW_DLV_ERROR;
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
Packit cdaae3
    *abbr_count = 0;
Packit cdaae3
    if (length) {
Packit cdaae3
        *length = 1;
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
Packit cdaae3
    abbrev_ptr = dbg->de_debug_abbrev.dss_data + offset;
Packit cdaae3
    abbrev_section_end =
Packit cdaae3
        dbg->de_debug_abbrev.dss_data + dbg->de_debug_abbrev.dss_size;
Packit cdaae3
Packit cdaae3
    DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp,
Packit cdaae3
        dbg,error,abbrev_section_end);
Packit cdaae3
    ret_abbrev->dab_code = (Dwarf_Word) utmp;
Packit cdaae3
    if (ret_abbrev->dab_code == 0) {
Packit cdaae3
        *returned_abbrev = ret_abbrev;
Packit cdaae3
        *abbr_count = 0;
Packit cdaae3
        if (length) {
Packit cdaae3
            *length = 1;
Packit cdaae3
        }
Packit cdaae3
        return DW_DLV_OK;
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp,
Packit cdaae3
        dbg,error,abbrev_section_end);
Packit cdaae3
    ret_abbrev->dab_tag = utmp;
Packit cdaae3
    if (abbrev_ptr >= abbrev_section_end) {
Packit cdaae3
        _dwarf_error(dbg, error, DW_DLE_ABBREV_DECODE_ERROR);
Packit cdaae3
        return DW_DLV_ERROR;
Packit cdaae3
    }
Packit cdaae3
    ret_abbrev->dab_has_child = *(abbrev_ptr++);
Packit cdaae3
    ret_abbrev->dab_abbrev_ptr = abbrev_ptr;
Packit cdaae3
Packit cdaae3
    do {
Packit cdaae3
        Dwarf_Unsigned utmp2 = 0;
Packit cdaae3
Packit cdaae3
        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp2,
Packit cdaae3
            dbg,error,abbrev_section_end);
Packit cdaae3
        attr = (Dwarf_Half) utmp2;
Packit cdaae3
        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp2,
Packit cdaae3
            dbg,error,abbrev_section_end);
Packit cdaae3
        attr_form = (Dwarf_Half) utmp2;
Packit cdaae3
        if (!_dwarf_valid_form_we_know(dbg,attr_form,attr)) {
Packit cdaae3
            _dwarf_error(dbg, error, DW_DLE_UNKNOWN_FORM);
Packit cdaae3
            return (DW_DLV_ERROR);
Packit cdaae3
        }
Packit cdaae3
        if (attr != 0) {
Packit cdaae3
            labbr_count++;
Packit cdaae3
        }
Packit cdaae3
    } while (abbrev_ptr < abbrev_section_end &&
Packit cdaae3
        (attr != 0 || attr_form != 0));
Packit cdaae3
    /* Global section offset. */
Packit cdaae3
    ret_abbrev->dab_goffset = offset;
Packit cdaae3
    ret_abbrev->dab_count = labbr_count;
Packit cdaae3
    if (abbrev_ptr > abbrev_section_end) {
Packit cdaae3
        dwarf_dealloc(dbg, ret_abbrev, DW_DLA_ABBREV);
Packit cdaae3
        _dwarf_error(dbg, error, DW_DLE_ABBREV_DECODE_ERROR);
Packit cdaae3
        return DW_DLV_ERROR;
Packit cdaae3
    }
Packit cdaae3
    if (length) {
Packit cdaae3
        *length = abbrev_ptr - dbg->de_debug_abbrev.dss_data - offset;
Packit cdaae3
    }
Packit cdaae3
    *returned_abbrev = ret_abbrev;
Packit cdaae3
    *abbr_count = labbr_count;
Packit cdaae3
    return DW_DLV_OK;
Packit cdaae3
}
Packit cdaae3
Packit cdaae3
int
Packit cdaae3
dwarf_get_abbrev_code(Dwarf_Abbrev abbrev,
Packit cdaae3
    Dwarf_Unsigned * returned_code,
Packit cdaae3
    Dwarf_Error * error)
Packit cdaae3
{
Packit cdaae3
    if (abbrev == NULL) {
Packit cdaae3
        _dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL);
Packit cdaae3
        return (DW_DLV_ERROR);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    *returned_code = abbrev->dab_code;
Packit cdaae3
    return (DW_DLV_OK);
Packit cdaae3
}
Packit cdaae3
Packit cdaae3
/*  DWARF defines DW_TAG_hi_user as 0xffff so no tag should be
Packit cdaae3
    over 16 bits.  */
Packit cdaae3
int
Packit cdaae3
dwarf_get_abbrev_tag(Dwarf_Abbrev abbrev,
Packit cdaae3
    Dwarf_Half * returned_tag, Dwarf_Error * error)
Packit cdaae3
{
Packit cdaae3
    if (abbrev == NULL) {
Packit cdaae3
        _dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL);
Packit cdaae3
        return (DW_DLV_ERROR);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    *returned_tag = abbrev->dab_tag;
Packit cdaae3
    return (DW_DLV_OK);
Packit cdaae3
}
Packit cdaae3
Packit cdaae3
Packit cdaae3
int
Packit cdaae3
dwarf_get_abbrev_children_flag(Dwarf_Abbrev abbrev,
Packit cdaae3
    Dwarf_Signed * returned_flag,
Packit cdaae3
    Dwarf_Error * error)
Packit cdaae3
{
Packit cdaae3
    if (abbrev == NULL) {
Packit cdaae3
        _dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL);
Packit cdaae3
        return (DW_DLV_ERROR);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    *returned_flag = abbrev->dab_has_child;
Packit cdaae3
    return (DW_DLV_OK);
Packit cdaae3
}
Packit cdaae3
Packit cdaae3
Packit cdaae3
int
Packit cdaae3
dwarf_get_abbrev_entry(Dwarf_Abbrev abbrev,
Packit cdaae3
    Dwarf_Signed indx,
Packit cdaae3
    Dwarf_Half * returned_attr_num,
Packit cdaae3
    Dwarf_Signed * form,
Packit cdaae3
    Dwarf_Off * offset, Dwarf_Error * error)
Packit cdaae3
{
Packit cdaae3
    Dwarf_Byte_Ptr abbrev_ptr = 0;
Packit cdaae3
    Dwarf_Byte_Ptr abbrev_end = 0;
Packit cdaae3
    Dwarf_Byte_Ptr mark_abbrev_ptr = 0;
Packit cdaae3
    Dwarf_Half attr = 0;
Packit cdaae3
    Dwarf_Half attr_form = 0;
Packit cdaae3
Packit cdaae3
    if (indx < 0)
Packit cdaae3
        return (DW_DLV_NO_ENTRY);
Packit cdaae3
Packit cdaae3
    if (abbrev == NULL) {
Packit cdaae3
        _dwarf_error(NULL, error, DW_DLE_DWARF_ABBREV_NULL);
Packit cdaae3
        return (DW_DLV_ERROR);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    if (abbrev->dab_code == 0) {
Packit cdaae3
        return (DW_DLV_NO_ENTRY);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    if (abbrev->dab_dbg == NULL) {
Packit cdaae3
        _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
Packit cdaae3
        return (DW_DLV_ERROR);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    abbrev_ptr = abbrev->dab_abbrev_ptr;
Packit cdaae3
    abbrev_end =
Packit cdaae3
        abbrev->dab_dbg->de_debug_abbrev.dss_data +
Packit cdaae3
        abbrev->dab_dbg->de_debug_abbrev.dss_size;
Packit cdaae3
Packit cdaae3
    for (attr = 1, attr_form = 1;
Packit cdaae3
        indx >= 0 && abbrev_ptr < abbrev_end && (attr != 0 ||
Packit cdaae3
            attr_form != 0);
Packit cdaae3
        indx--) {
Packit cdaae3
        Dwarf_Unsigned utmp4;
Packit cdaae3
Packit cdaae3
        mark_abbrev_ptr = abbrev_ptr;
Packit cdaae3
        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp4,abbrev->dab_dbg,
Packit cdaae3
            error,abbrev_end);
Packit cdaae3
        attr = (Dwarf_Half) utmp4;
Packit cdaae3
        DECODE_LEB128_UWORD_CK(abbrev_ptr, utmp4,abbrev->dab_dbg,
Packit cdaae3
            error,abbrev_end);
Packit cdaae3
        attr_form = (Dwarf_Half) utmp4;
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    if (abbrev_ptr >= abbrev_end) {
Packit cdaae3
        _dwarf_error(abbrev->dab_dbg, error, DW_DLE_ABBREV_DECODE_ERROR);
Packit cdaae3
        return (DW_DLV_ERROR);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    if (indx >= 0) {
Packit cdaae3
        return (DW_DLV_NO_ENTRY);
Packit cdaae3
    }
Packit cdaae3
Packit cdaae3
    if (form != NULL) {
Packit cdaae3
        *form = attr_form;
Packit cdaae3
    }
Packit cdaae3
    if (offset != NULL) {
Packit cdaae3
        *offset = mark_abbrev_ptr - abbrev->dab_dbg->de_debug_abbrev.dss_data;
Packit cdaae3
    }
Packit cdaae3
    *returned_attr_num = (attr);
Packit cdaae3
    return DW_DLV_OK;
Packit cdaae3
}
Packit cdaae3
Packit cdaae3
/*  This function is not entirely safe to call.
Packit cdaae3
    The problem is that the DWARF[234] specification does not insist
Packit cdaae3
    that bytes in .debug_abbrev that are not referenced by .debug_info
Packit cdaae3
    or .debug_types need to be initialized to anything specific.
Packit cdaae3
    Any garbage bytes may cause trouble.  Not all compilers/linkers
Packit cdaae3
    leave unreferenced garbage bytes in .debug_abbrev, so this may
Packit cdaae3
    work for most objects. */
Packit cdaae3
int
Packit cdaae3
dwarf_get_abbrev_count(Dwarf_Debug dbg)
Packit cdaae3
{
Packit cdaae3
    Dwarf_Abbrev ab;
Packit cdaae3
    Dwarf_Unsigned offset = 0;
Packit cdaae3
    Dwarf_Unsigned length = 0;
Packit cdaae3
    Dwarf_Unsigned attr_count = 0;
Packit cdaae3
    Dwarf_Unsigned abbrev_count = 0;
Packit cdaae3
    int abres = DW_DLV_OK;
Packit cdaae3
    Dwarf_Error err;
Packit cdaae3
Packit cdaae3
    while ((abres = dwarf_get_abbrev(dbg, offset, &ab,
Packit cdaae3
        &length, &attr_count,
Packit cdaae3
        &err)) == DW_DLV_OK) {
Packit cdaae3
Packit cdaae3
        ++abbrev_count;
Packit cdaae3
        offset += length;
Packit cdaae3
        dwarf_dealloc(dbg, ab, DW_DLA_ABBREV);
Packit cdaae3
    }
Packit cdaae3
    return abbrev_count;
Packit cdaae3
}
Packit cdaae3