blob: 8b92e22d11fafe09be84a5799d28884ca717d7c7 [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;
Mark Wielaard775375e2012-06-22 12:02:45 +020049 if (attr->form == DW_FORM_ref_addr || attr->form == DW_FORM_GNU_ref_alt)
Roland McGrathe4c22ea2007-10-23 13:07:39 +000050 {
51 /* This has an absolute offset. */
52
Mark Wielaardd4744a72013-04-10 15:52:57 +020053 uint8_t ref_size = (cu->version == 2 && attr->form == DW_FORM_ref_addr
Roland McGrath3e0f7d12010-06-15 23:10:35 -070054 ? cu->address_size
55 : cu->offset_size);
Roland McGrathe4c22ea2007-10-23 13:07:39 +000056
Mark Wielaard775375e2012-06-22 12:02:45 +020057 Dwarf *dbg_ret = (attr->form == DW_FORM_GNU_ref_alt
58 ? cu->dbg->alt_dwarf : cu->dbg);
59
60 if (dbg_ret == NULL)
61 {
62 __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK);
63 return NULL;
64 }
65
66 if (__libdw_read_offset (cu->dbg, dbg_ret, IDX_debug_info, attr->valp,
Ulrich Drepper99d23722009-06-14 20:19:45 -070067 ref_size, &offset, IDX_debug_info, 0))
68 return NULL;
Roland McGrath3e0f7d12010-06-15 23:10:35 -070069
Mark Wielaard775375e2012-06-22 12:02:45 +020070 return INTUSE(dwarf_offdie) (dbg_ret, offset, result);
Roland McGrathe4c22ea2007-10-23 13:07:39 +000071 }
Roland McGrath3e0f7d12010-06-15 23:10:35 -070072
Mark Wielaard92026652014-12-12 16:43:04 +010073 const unsigned char *datap;
74 size_t size;
Roland McGrath3e0f7d12010-06-15 23:10:35 -070075 if (attr->form == DW_FORM_ref_sig8)
Roland McGrathb75f4442010-06-15 22:24:19 -070076 {
77 /* This doesn't have an offset, but instead a value we
78 have to match in the .debug_types type unit headers. */
79
Roland McGrath3e0f7d12010-06-15 23:10:35 -070080 uint64_t sig = read_8ubyte_unaligned (cu->dbg, attr->valp);
81 cu = Dwarf_Sig8_Hash_find (&cu->dbg->sig8_hash, sig, NULL);
82 if (cu == NULL)
83 /* Not seen before. We have to scan through the type units. */
84 do
85 {
86 cu = __libdw_intern_next_unit (attr->cu->dbg, true);
87 if (cu == NULL)
88 {
89 __libdw_seterrno (INTUSE(dwarf_errno) ()
90 ?: DWARF_E_INVALID_REFERENCE);
91 return NULL;
92 }
Roland McGrath3e0f7d12010-06-15 23:10:35 -070093 }
94 while (cu->type_sig8 != sig);
Roland McGrathb75f4442010-06-15 22:24:19 -070095
Mark Wielaard92026652014-12-12 16:43:04 +010096 datap = cu->dbg->sectiondata[IDX_debug_types]->d_buf;
97 size = cu->dbg->sectiondata[IDX_debug_types]->d_size;
Jason P. Leasure7c713822015-01-14 09:26:55 -050098 offset = cu->start + cu->type_offset;
Roland McGrathb75f4442010-06-15 22:24:19 -070099 }
Roland McGrathe4c22ea2007-10-23 13:07:39 +0000100 else
101 {
102 /* Other forms produce an offset from the CU. */
103 if (unlikely (__libdw_formref (attr, &offset) != 0))
104 return NULL;
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700105
Mark Wielaard92026652014-12-12 16:43:04 +0100106 datap = cu->startp;
107 size = cu->endp - cu->startp;
Roland McGrathe4c22ea2007-10-23 13:07:39 +0000108 }
109
Mark Wielaard92026652014-12-12 16:43:04 +0100110 if (unlikely (offset >= size))
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700111 {
112 __libdw_seterrno (DWARF_E_INVALID_DWARF);
113 return NULL;
114 }
115
116 memset (result, '\0', sizeof (Dwarf_Die));
Mark Wielaard92026652014-12-12 16:43:04 +0100117 result->addr = (char *) datap + offset;
Roland McGrath3e0f7d12010-06-15 23:10:35 -0700118 result->cu = cu;
119 return result;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000120}
121INTDEF (dwarf_formref_die)