Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Look up the DIE in a reference-form attribute. |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 2 | Copyright (C) 2005-2010 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 * |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 39 | dwarf_formref_die (attr, result) |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 40 | Dwarf_Attribute *attr; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 41 | Dwarf_Die *result; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 42 | { |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 43 | if (attr == NULL) |
| 44 | return NULL; |
| 45 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 46 | struct Dwarf_CU *cu = attr->cu; |
| 47 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 48 | Dwarf_Off offset; |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 49 | if (attr->form == DW_FORM_ref_addr) |
| 50 | { |
| 51 | /* This has an absolute offset. */ |
| 52 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 53 | uint8_t ref_size = (cu->version == 2 |
| 54 | ? cu->address_size |
| 55 | : cu->offset_size); |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 56 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 57 | if (__libdw_read_offset (cu->dbg, IDX_debug_info, attr->valp, |
Ulrich Drepper | 99d2372 | 2009-06-14 20:19:45 -0700 | [diff] [blame] | 58 | ref_size, &offset, IDX_debug_info, 0)) |
| 59 | return NULL; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 60 | |
| 61 | return INTUSE(dwarf_offdie) (cu->dbg, offset, result); |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 62 | } |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 63 | |
| 64 | Elf_Data *data; |
| 65 | if (attr->form == DW_FORM_ref_sig8) |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 66 | { |
| 67 | /* This doesn't have an offset, but instead a value we |
| 68 | have to match in the .debug_types type unit headers. */ |
| 69 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 70 | uint64_t sig = read_8ubyte_unaligned (cu->dbg, attr->valp); |
| 71 | cu = Dwarf_Sig8_Hash_find (&cu->dbg->sig8_hash, sig, NULL); |
| 72 | if (cu == NULL) |
| 73 | /* Not seen before. We have to scan through the type units. */ |
| 74 | do |
| 75 | { |
| 76 | cu = __libdw_intern_next_unit (attr->cu->dbg, true); |
| 77 | if (cu == NULL) |
| 78 | { |
| 79 | __libdw_seterrno (INTUSE(dwarf_errno) () |
| 80 | ?: DWARF_E_INVALID_REFERENCE); |
| 81 | return NULL; |
| 82 | } |
Roland McGrath | 00aca7f | 2010-07-27 04:40:23 -0700 | [diff] [blame] | 83 | Dwarf_Sig8_Hash_insert (&cu->dbg->sig8_hash, cu->type_sig8, cu); |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 84 | } |
| 85 | while (cu->type_sig8 != sig); |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 86 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 87 | data = cu->dbg->sectiondata[IDX_debug_types]; |
| 88 | offset = cu->type_offset; |
Roland McGrath | b75f444 | 2010-06-15 22:24:19 -0700 | [diff] [blame] | 89 | } |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 90 | else |
| 91 | { |
| 92 | /* Other forms produce an offset from the CU. */ |
| 93 | if (unlikely (__libdw_formref (attr, &offset) != 0)) |
| 94 | return NULL; |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 95 | |
Roland McGrath | 5cc030d | 2010-06-20 17:25:35 -0700 | [diff] [blame] | 96 | data = cu_data (cu); |
Roland McGrath | e4c22ea | 2007-10-23 13:07:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Roland McGrath | 3e0f7d1 | 2010-06-15 23:10:35 -0700 | [diff] [blame] | 99 | if (unlikely (data->d_size - cu->start <= offset)) |
| 100 | { |
| 101 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | memset (result, '\0', sizeof (Dwarf_Die)); |
| 106 | result->addr = (char *) data->d_buf + cu->start + offset; |
| 107 | result->cu = cu; |
| 108 | return result; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 109 | } |
| 110 | INTDEF (dwarf_formref_die) |