blob: 342f6b9e256f658ce84e32e39549d6fc418375e8 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Look up the DIE in a reference-form attribute.
Roland McGrathb75f4442010-06-15 22:24:19 -07002 Copyright (C) 2005-2010 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00007
Mark Wielaardde2ed972012-06-05 17:15:16 +02008 * 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 Drepper361df7d2006-04-04 21:38:57 +000021 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 Wielaardde2ed972012-06-05 17:15:16 +020025 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 Drepperb08d5a82005-07-26 05:00:05 +000028
29#ifdef HAVE_CONFIG_H
30# include <config.h>
31#endif
32
Roland McGrath3e0f7d12010-06-15 23:10:35 -070033#include <string.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000034#include "libdwP.h"
Roland McGrathe4c22ea2007-10-23 13:07:39 +000035#include <dwarf.h>
36
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000037
38Dwarf_Die *
Roland McGrath3e0f7d12010-06-15 23:10:35 -070039dwarf_formref_die (attr, result)
Roland McGrathe4c22ea2007-10-23 13:07:39 +000040 Dwarf_Attribute *attr;
Roland McGrath3e0f7d12010-06-15 23:10:35 -070041 Dwarf_Die *result;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000042{
Roland McGrathe4c22ea2007-10-23 13:07:39 +000043 if (attr == NULL)
44 return NULL;
45
Roland McGrath3e0f7d12010-06-15 23:10:35 -070046 struct Dwarf_CU *cu = attr->cu;
47
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000048 Dwarf_Off offset;
Roland McGrathe4c22ea2007-10-23 13:07:39 +000049 if (attr->form == DW_FORM_ref_addr)
50 {
51 /* This has an absolute offset. */
52
Roland McGrath3e0f7d12010-06-15 23:10:35 -070053 uint8_t ref_size = (cu->version == 2
54 ? cu->address_size
55 : cu->offset_size);
Roland McGrathe4c22ea2007-10-23 13:07:39 +000056
Roland McGrath3e0f7d12010-06-15 23:10:35 -070057 if (__libdw_read_offset (cu->dbg, IDX_debug_info, attr->valp,
Ulrich Drepper99d23722009-06-14 20:19:45 -070058 ref_size, &offset, IDX_debug_info, 0))
59 return NULL;
Roland McGrath3e0f7d12010-06-15 23:10:35 -070060
61 return INTUSE(dwarf_offdie) (cu->dbg, offset, result);
Roland McGrathe4c22ea2007-10-23 13:07:39 +000062 }
Roland McGrath3e0f7d12010-06-15 23:10:35 -070063
64 Elf_Data *data;
65 if (attr->form == DW_FORM_ref_sig8)
Roland McGrathb75f4442010-06-15 22:24:19 -070066 {
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 McGrath3e0f7d12010-06-15 23:10:35 -070070 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 McGrath00aca7f2010-07-27 04:40:23 -070083 Dwarf_Sig8_Hash_insert (&cu->dbg->sig8_hash, cu->type_sig8, cu);
Roland McGrath3e0f7d12010-06-15 23:10:35 -070084 }
85 while (cu->type_sig8 != sig);
Roland McGrathb75f4442010-06-15 22:24:19 -070086
Roland McGrath3e0f7d12010-06-15 23:10:35 -070087 data = cu->dbg->sectiondata[IDX_debug_types];
88 offset = cu->type_offset;
Roland McGrathb75f4442010-06-15 22:24:19 -070089 }
Roland McGrathe4c22ea2007-10-23 13:07:39 +000090 else
91 {
92 /* Other forms produce an offset from the CU. */
93 if (unlikely (__libdw_formref (attr, &offset) != 0))
94 return NULL;
Roland McGrath3e0f7d12010-06-15 23:10:35 -070095
Roland McGrath5cc030d2010-06-20 17:25:35 -070096 data = cu_data (cu);
Roland McGrathe4c22ea2007-10-23 13:07:39 +000097 }
98
Roland McGrath3e0f7d12010-06-15 23:10:35 -070099 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 Drepperb08d5a82005-07-26 05:00:05 +0000109}
110INTDEF (dwarf_formref_die)