Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame^] | 1 | /* Get abbreviation at given offset. |
| 2 | Copyright (C) 2003, 2004, 2005, 2006, 2014, 2017 Red Hat, Inc. |
| 3 | This file is part of elfutils. |
| 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2003. |
| 5 | |
| 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of either |
| 8 | |
| 9 | * the GNU Lesser General Public License as published by the Free |
| 10 | Software Foundation; either version 3 of the License, or (at |
| 11 | your option) any later version |
| 12 | |
| 13 | or |
| 14 | |
| 15 | * the GNU General Public License as published by the Free |
| 16 | Software Foundation; either version 2 of the License, or (at |
| 17 | your option) any later version |
| 18 | |
| 19 | or both in parallel, as here. |
| 20 | |
| 21 | elfutils is distributed in the hope that it will be useful, but |
| 22 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | General Public License for more details. |
| 25 | |
| 26 | You should have received copies of the GNU General Public License and |
| 27 | the GNU Lesser General Public License along with this program. If |
| 28 | not, see <http://www.gnu.org/licenses/>. */ |
| 29 | |
| 30 | #ifdef HAVE_CONFIG_H |
| 31 | # include <config.h> |
| 32 | #endif |
| 33 | |
| 34 | #include <dwarf.h> |
| 35 | #include "libdwP.h" |
| 36 | |
| 37 | |
| 38 | Dwarf_Abbrev * |
| 39 | internal_function |
| 40 | __libdw_getabbrev (Dwarf *dbg, struct Dwarf_CU *cu, Dwarf_Off offset, |
| 41 | size_t *lengthp, Dwarf_Abbrev *result) |
| 42 | { |
| 43 | /* Don't fail if there is not .debug_abbrev section. */ |
| 44 | if (dbg->sectiondata[IDX_debug_abbrev] == NULL) |
| 45 | return NULL; |
| 46 | |
| 47 | if (offset >= dbg->sectiondata[IDX_debug_abbrev]->d_size) |
| 48 | { |
| 49 | __libdw_seterrno (DWARF_E_INVALID_OFFSET); |
| 50 | return NULL; |
| 51 | } |
| 52 | |
| 53 | const unsigned char *abbrevp |
| 54 | = (unsigned char *) dbg->sectiondata[IDX_debug_abbrev]->d_buf + offset; |
| 55 | |
| 56 | if (*abbrevp == '\0') |
| 57 | /* We are past the last entry. */ |
| 58 | return DWARF_END_ABBREV; |
| 59 | |
| 60 | /* 7.5.3 Abbreviations Tables |
| 61 | |
| 62 | [...] Each declaration begins with an unsigned LEB128 number |
| 63 | representing the abbreviation code itself. [...] The |
| 64 | abbreviation code is followed by another unsigned LEB128 |
| 65 | number that encodes the entry's tag. [...] |
| 66 | |
| 67 | [...] Following the tag encoding is a 1-byte value that |
| 68 | determines whether a debugging information entry using this |
| 69 | abbreviation has child entries or not. [...] |
| 70 | |
| 71 | [...] Finally, the child encoding is followed by a series of |
| 72 | attribute specifications. Each attribute specification |
| 73 | consists of two parts. The first part is an unsigned LEB128 |
| 74 | number representing the attribute's name. The second part is |
| 75 | an unsigned LEB128 number representing the attribute's form. */ |
| 76 | const unsigned char *end = (dbg->sectiondata[IDX_debug_abbrev]->d_buf |
| 77 | + dbg->sectiondata[IDX_debug_abbrev]->d_size); |
| 78 | const unsigned char *start_abbrevp = abbrevp; |
| 79 | unsigned int code; |
| 80 | get_uleb128 (code, abbrevp, end); |
| 81 | |
| 82 | /* Check whether this code is already in the hash table. */ |
| 83 | bool foundit = false; |
| 84 | Dwarf_Abbrev *abb = NULL; |
| 85 | if (cu == NULL |
| 86 | || (abb = Dwarf_Abbrev_Hash_find (&cu->abbrev_hash, code, NULL)) == NULL) |
| 87 | { |
| 88 | if (result == NULL) |
| 89 | abb = libdw_typed_alloc (dbg, Dwarf_Abbrev); |
| 90 | else |
| 91 | abb = result; |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | foundit = true; |
| 96 | |
| 97 | if (unlikely (abb->offset != offset)) |
| 98 | { |
| 99 | /* A duplicate abbrev code at a different offset, |
| 100 | that should never happen. */ |
| 101 | invalid: |
| 102 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | /* If the caller doesn't need the length we are done. */ |
| 107 | if (lengthp == NULL) |
| 108 | goto out; |
| 109 | } |
| 110 | |
| 111 | /* If there is already a value in the hash table we are going to |
| 112 | overwrite its content. This must not be a problem, since the |
| 113 | content better be the same. */ |
| 114 | abb->code = code; |
| 115 | if (abbrevp >= end) |
| 116 | goto invalid; |
| 117 | get_uleb128 (abb->tag, abbrevp, end); |
| 118 | if (abbrevp + 1 >= end) |
| 119 | goto invalid; |
| 120 | abb->has_children = *abbrevp++ == DW_CHILDREN_yes; |
| 121 | abb->attrp = (unsigned char *) abbrevp; |
| 122 | abb->offset = offset; |
| 123 | |
| 124 | /* Skip over all the attributes and check rest of the abbrev is valid. */ |
| 125 | unsigned int attrname; |
| 126 | unsigned int attrform; |
| 127 | do |
| 128 | { |
| 129 | if (abbrevp >= end) |
| 130 | goto invalid; |
| 131 | get_uleb128 (attrname, abbrevp, end); |
| 132 | if (abbrevp >= end) |
| 133 | goto invalid; |
| 134 | get_uleb128 (attrform, abbrevp, end); |
| 135 | } |
| 136 | while (attrname != 0 && attrform != 0); |
| 137 | |
| 138 | /* Return the length to the caller if she asked for it. */ |
| 139 | if (lengthp != NULL) |
| 140 | *lengthp = abbrevp - start_abbrevp; |
| 141 | |
| 142 | /* Add the entry to the hash table. */ |
| 143 | if (cu != NULL && ! foundit) |
| 144 | (void) Dwarf_Abbrev_Hash_insert (&cu->abbrev_hash, abb->code, abb); |
| 145 | |
| 146 | out: |
| 147 | return abb; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | Dwarf_Abbrev * |
| 152 | dwarf_getabbrev (Dwarf_Die *die, Dwarf_Off offset, size_t *lengthp) |
| 153 | { |
| 154 | return __libdw_getabbrev (die->cu->dbg, die->cu, |
| 155 | die->cu->orig_abbrev_offset + offset, lengthp, |
| 156 | NULL); |
| 157 | } |