Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Look up the DIE in a reference-form attribute. |
Mark Wielaard | 240a068 | 2018-01-19 23:59:21 +0100 | [diff] [blame] | 2 | Copyright (C) 2005-2010, 2018 Red Hat, Inc. |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 3 | This file is part of elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of either |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 7 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 8 | * the GNU Lesser General Public License as published by the Free |
| 9 | Software Foundation; either version 3 of the License, or (at |
| 10 | your option) any later version |
| 11 | |
| 12 | or |
| 13 | |
| 14 | * the GNU General Public License as published by the Free |
| 15 | Software Foundation; either version 2 of the License, or (at |
| 16 | your option) any later version |
| 17 | |
| 18 | or both in parallel, as here. |
| 19 | |
| 20 | elfutils is distributed in the hope that it will be useful, but |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
Mark Wielaard | de2ed97 | 2012-06-05 17:15:16 +0200 | [diff] [blame] | 25 | You should have received copies of the GNU General Public License and |
| 26 | the GNU Lesser General Public License along with this program. If |
| 27 | not, see <http://www.gnu.org/licenses/>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 28 | |
| 29 | #ifdef HAVE_CONFIG_H |
| 30 | # include <config.h> |
| 31 | #endif |
| 32 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 33 | #include <string.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 34 | #include "libdwP.h" |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 35 | #include <dwarf.h> |
| 36 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 37 | |
| 38 | Dwarf_Die * |
Mark Wielaard | 1ccdfb6 | 2015-09-22 22:27:01 +0200 | [diff] [blame] | 39 | dwarf_formref_die (Dwarf_Attribute *attr, Dwarf_Die *result) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 40 | { |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 41 | if (attr == NULL) |
| 42 | return NULL; |
| 43 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 44 | struct Dwarf_CU *cu = attr->cu; |
| 45 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 46 | Dwarf_Off offset; |
Mark Wielaard | af14380 | 2018-05-08 15:34:03 +0200 | [diff] [blame] | 47 | if (attr->form == DW_FORM_ref_addr || attr->form == DW_FORM_GNU_ref_alt |
| 48 | || attr->form == DW_FORM_ref_sup4 || attr->form == DW_FORM_ref_sup8) |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 49 | { |
| 50 | /* This has an absolute offset. */ |
| 51 | |
Mark Wielaard | af14380 | 2018-05-08 15:34:03 +0200 | [diff] [blame] | 52 | uint8_t ref_size; |
| 53 | if (cu->version == 2 && attr->form == DW_FORM_ref_addr) |
| 54 | ref_size = cu->address_size; |
| 55 | else if (attr->form == DW_FORM_ref_sup4) |
| 56 | ref_size = 4; |
| 57 | else if (attr->form == DW_FORM_ref_sup8) |
| 58 | ref_size = 8; |
| 59 | else |
| 60 | ref_size = cu->offset_size; |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 61 | |
Mark Wielaard | 775375e | 2012-06-22 12:02:45 +0200 | [diff] [blame] | 62 | Dwarf *dbg_ret = (attr->form == DW_FORM_GNU_ref_alt |
Mark Wielaard | 240a068 | 2018-01-19 23:59:21 +0100 | [diff] [blame] | 63 | ? INTUSE(dwarf_getalt) (cu->dbg) : cu->dbg); |
Mark Wielaard | 775375e | 2012-06-22 12:02:45 +0200 | [diff] [blame] | 64 | |
| 65 | if (dbg_ret == NULL) |
| 66 | { |
| 67 | __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK); |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | if (__libdw_read_offset (cu->dbg, dbg_ret, IDX_debug_info, attr->valp, |
Ulrich Drepper | 99d2372 | 2009-06-14 20:19:45 -0700 | [diff] [blame] | 72 | ref_size, &offset, IDX_debug_info, 0)) |
| 73 | return NULL; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 74 | |
Mark Wielaard | 775375e | 2012-06-22 12:02:45 +0200 | [diff] [blame] | 75 | return INTUSE(dwarf_offdie) (dbg_ret, offset, result); |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 76 | } |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 77 | |
Mark Wielaard | 9202665 | 2014-12-12 16:43:04 +0100 | [diff] [blame] | 78 | const unsigned char *datap; |
| 79 | size_t size; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 80 | if (attr->form == DW_FORM_ref_sig8) |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 81 | { |
| 82 | /* This doesn't have an offset, but instead a value we |
Mark Wielaard | 532ab1e | 2018-01-29 15:59:05 +0100 | [diff] [blame] | 83 | have to match in the type unit headers. */ |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 84 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 85 | uint64_t sig = read_8ubyte_unaligned (cu->dbg, attr->valp); |
| 86 | cu = Dwarf_Sig8_Hash_find (&cu->dbg->sig8_hash, sig, NULL); |
| 87 | if (cu == NULL) |
Mark Wielaard | 532ab1e | 2018-01-29 15:59:05 +0100 | [diff] [blame] | 88 | { |
| 89 | /* Not seen before. We have to scan through the type units. |
| 90 | Since DWARFv5 these can (also) be found in .debug_info, |
| 91 | so scan that first. */ |
| 92 | bool scan_debug_types = false; |
| 93 | do |
| 94 | { |
| 95 | cu = __libdw_intern_next_unit (attr->cu->dbg, scan_debug_types); |
| 96 | if (cu == NULL) |
| 97 | { |
| 98 | if (scan_debug_types == false) |
| 99 | scan_debug_types = true; |
| 100 | else |
| 101 | { |
| 102 | __libdw_seterrno (INTUSE(dwarf_errno) () |
| 103 | ?: DWARF_E_INVALID_REFERENCE); |
| 104 | return NULL; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | while (cu == NULL || cu->unit_id8 != sig); |
| 109 | } |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 110 | |
Mark Wielaard | 532ab1e | 2018-01-29 15:59:05 +0100 | [diff] [blame] | 111 | int secid = cu_sec_idx (cu); |
| 112 | datap = cu->dbg->sectiondata[secid]->d_buf; |
| 113 | size = cu->dbg->sectiondata[secid]->d_size; |
| 114 | offset = cu->start + cu->subdie_offset; |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 115 | } |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 116 | else |
| 117 | { |
| 118 | /* Other forms produce an offset from the CU. */ |
| 119 | if (unlikely (__libdw_formref (attr, &offset) != 0)) |
| 120 | return NULL; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 121 | |
Mark Wielaard | 9202665 | 2014-12-12 16:43:04 +0100 | [diff] [blame] | 122 | datap = cu->startp; |
| 123 | size = cu->endp - cu->startp; |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Mark Wielaard | 9202665 | 2014-12-12 16:43:04 +0100 | [diff] [blame] | 126 | if (unlikely (offset >= size)) |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 127 | { |
| 128 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
| 129 | return NULL; |
| 130 | } |
| 131 | |
| 132 | memset (result, '\0', sizeof (Dwarf_Die)); |
Mark Wielaard | 9202665 | 2014-12-12 16:43:04 +0100 | [diff] [blame] | 133 | result->addr = (char *) datap + offset; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 134 | result->cu = cu; |
| 135 | return result; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 136 | } |
| 137 | INTDEF (dwarf_formref_die) |