Line data Source code
1 : /* Look up the DIE in a reference-form attribute.
2 : Copyright (C) 2005-2010 Red Hat, Inc.
3 : This file is part of elfutils.
4 :
5 : This file is free software; you can redistribute it and/or modify
6 : it under the terms of either
7 :
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
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 :
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/>. */
28 :
29 : #ifdef HAVE_CONFIG_H
30 : # include <config.h>
31 : #endif
32 :
33 : #include <string.h>
34 : #include "libdwP.h"
35 : #include <dwarf.h>
36 :
37 :
38 : Dwarf_Die *
39 670574 : dwarf_formref_die (Dwarf_Attribute *attr, Dwarf_Die *result)
40 : {
41 670574 : if (attr == NULL)
42 : return NULL;
43 :
44 670574 : struct Dwarf_CU *cu = attr->cu;
45 :
46 : Dwarf_Off offset;
47 670574 : if (attr->form == DW_FORM_ref_addr || attr->form == DW_FORM_GNU_ref_alt)
48 : {
49 : /* This has an absolute offset. */
50 :
51 12092 : uint8_t ref_size = (cu->version == 2 && attr->form == DW_FORM_ref_addr
52 : ? cu->address_size
53 : : cu->offset_size);
54 :
55 12092 : Dwarf *dbg_ret = (attr->form == DW_FORM_GNU_ref_alt
56 12092 : ? cu->dbg->alt_dwarf : cu->dbg);
57 :
58 12092 : if (dbg_ret == NULL)
59 : {
60 0 : __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK);
61 0 : return NULL;
62 : }
63 :
64 12092 : if (__libdw_read_offset (cu->dbg, dbg_ret, IDX_debug_info, attr->valp,
65 : ref_size, &offset, IDX_debug_info, 0))
66 : return NULL;
67 :
68 12092 : return INTUSE(dwarf_offdie) (dbg_ret, offset, result);
69 : }
70 :
71 : const unsigned char *datap;
72 : size_t size;
73 658482 : if (attr->form == DW_FORM_ref_sig8)
74 : {
75 : /* This doesn't have an offset, but instead a value we
76 : have to match in the .debug_types type unit headers. */
77 :
78 4 : uint64_t sig = read_8ubyte_unaligned (cu->dbg, attr->valp);
79 4 : cu = Dwarf_Sig8_Hash_find (&cu->dbg->sig8_hash, sig, NULL);
80 4 : if (cu == NULL)
81 : /* Not seen before. We have to scan through the type units. */
82 : do
83 : {
84 1 : cu = __libdw_intern_next_unit (attr->cu->dbg, true);
85 1 : if (cu == NULL)
86 : {
87 0 : __libdw_seterrno (INTUSE(dwarf_errno) ()
88 : ?: DWARF_E_INVALID_REFERENCE);
89 0 : return NULL;
90 : }
91 : }
92 1 : while (cu->type_sig8 != sig);
93 :
94 4 : datap = cu->dbg->sectiondata[IDX_debug_types]->d_buf;
95 4 : size = cu->dbg->sectiondata[IDX_debug_types]->d_size;
96 4 : offset = cu->start + cu->type_offset;
97 : }
98 : else
99 : {
100 : /* Other forms produce an offset from the CU. */
101 658478 : if (unlikely (__libdw_formref (attr, &offset) != 0))
102 : return NULL;
103 :
104 658478 : datap = cu->startp;
105 658478 : size = cu->endp - cu->startp;
106 : }
107 :
108 658482 : if (unlikely (offset >= size))
109 : {
110 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
111 0 : return NULL;
112 : }
113 :
114 658482 : memset (result, '\0', sizeof (Dwarf_Die));
115 658482 : result->addr = (char *) datap + offset;
116 658482 : result->cu = cu;
117 658482 : return result;
118 : }
119 : INTDEF (dwarf_formref_die)
|