Branch data Line data Source code
1 : : /* Look up the DIE in a reference-form attribute.
2 : : Copyright (C) 2005-2010, 2018 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 : 2454626 : dwarf_formref_die (Dwarf_Attribute *attr, Dwarf_Die *result)
40 : : {
41 [ + + ]: 2454626 : if (attr == NULL)
42 : : return NULL;
43 : :
44 : 2043472 : struct Dwarf_CU *cu = attr->cu;
45 : :
46 : 2043472 : Dwarf_Off offset;
47 [ + + ]: 2043472 : if (attr->form == DW_FORM_ref_addr || attr->form == DW_FORM_GNU_ref_alt
48 [ + + - + ]: 2023062 : || attr->form == DW_FORM_ref_sup4 || attr->form == DW_FORM_ref_sup8)
49 : : {
50 : : /* This has an absolute offset. */
51 : :
52 : 20426 : uint8_t ref_size;
53 [ - + - - ]: 20426 : if (cu->version == 2 && attr->form == DW_FORM_ref_addr)
54 : 0 : ref_size = cu->address_size;
55 [ + + ]: 20426 : else if (attr->form == DW_FORM_ref_sup4)
56 : : ref_size = 4;
57 [ + - ]: 20410 : else if (attr->form == DW_FORM_ref_sup8)
58 : : ref_size = 8;
59 : : else
60 : 20410 : ref_size = cu->offset_size;
61 : :
62 : 20426 : Dwarf *dbg_ret = (attr->form == DW_FORM_GNU_ref_alt
63 : 20426 : || attr->form == DW_FORM_ref_sup4
64 [ - + ]: 20336 : || attr->form == DW_FORM_ref_sup8
65 [ + + ]: 20426 : ? INTUSE(dwarf_getalt) (cu->dbg) : cu->dbg);
66 : :
67 [ - + ]: 20426 : if (dbg_ret == NULL)
68 : : {
69 : 0 : __libdw_seterrno (DWARF_E_NO_ALT_DEBUGLINK);
70 : 0 : return NULL;
71 : : }
72 : :
73 [ + - ]: 20426 : if (__libdw_read_offset (cu->dbg, dbg_ret, IDX_debug_info, attr->valp,
74 : : ref_size, &offset, IDX_debug_info, 0))
75 : : return NULL;
76 : :
77 : 20426 : return INTUSE(dwarf_offdie) (dbg_ret, offset, result);
78 : : }
79 : :
80 : 2023046 : const unsigned char *datap;
81 : 2023046 : size_t size;
82 [ + + ]: 2023046 : if (attr->form == DW_FORM_ref_sig8)
83 : : {
84 : : /* This doesn't have an offset, but instead a value we
85 : : have to match in the type unit headers. */
86 : :
87 [ - + ]: 12 : uint64_t sig = read_8ubyte_unaligned (cu->dbg, attr->valp);
88 : 12 : cu = Dwarf_Sig8_Hash_find (&cu->dbg->sig8_hash, sig);
89 [ + + ]: 12 : if (cu == NULL)
90 : : {
91 : : /* Not seen before. We have to scan through the type units.
92 : : Since DWARFv5 these can (also) be found in .debug_info,
93 : : so scan that first. */
94 : : bool scan_debug_types = false;
95 : 4 : do
96 : : {
97 : 4 : mutex_lock (attr->cu->dbg->dwarf_lock);
98 : 4 : cu = __libdw_intern_next_unit (attr->cu->dbg, scan_debug_types);
99 : 4 : mutex_unlock (attr->cu->dbg->dwarf_lock);
100 : :
101 [ + + ]: 4 : if (cu == NULL)
102 : : {
103 [ + - ]: 2 : if (scan_debug_types == false)
104 : : scan_debug_types = true;
105 : : else
106 : : {
107 [ # # ]: 0 : __libdw_seterrno (INTUSE(dwarf_errno) ()
108 : : ?: DWARF_E_INVALID_REFERENCE);
109 : 0 : return NULL;
110 : : }
111 : : }
112 : : }
113 [ - + ]: 2 : while (cu == NULL || cu->unit_id8 != sig);
114 : : }
115 : :
116 : 12 : int secid = cu_sec_idx (cu);
117 : 12 : datap = cu->dbg->sectiondata[secid]->d_buf;
118 : 12 : size = cu->dbg->sectiondata[secid]->d_size;
119 : 12 : offset = cu->start + cu->subdie_offset;
120 : : }
121 : : else
122 : : {
123 : : /* Other forms produce an offset from the CU. */
124 [ + - ]: 2023034 : if (unlikely (__libdw_formref (attr, &offset) != 0))
125 : : return NULL;
126 : :
127 : 2023034 : datap = cu->startp;
128 : 2023034 : size = cu->endp - cu->startp;
129 : : }
130 : :
131 [ - + ]: 2023046 : if (unlikely (offset >= size))
132 : : {
133 : 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
134 : 0 : return NULL;
135 : : }
136 : :
137 : 2023046 : memset (result, '\0', sizeof (Dwarf_Die));
138 : 2023046 : result->addr = (char *) datap + offset;
139 : 2023046 : result->cu = cu;
140 : 2023046 : return result;
141 : : }
142 : : INTDEF (dwarf_formref_die)
|