blob: 63f669716e88f8d0d8a71a68e504d9046654eca2 [file] [log] [blame]
Ben Cheng25b3c042013-11-20 14:45:36 -08001/* Look up the DIE in a reference-form attribute.
2 Copyright (C) 2005-2010 Red Hat, Inc.
Elliott Hughes03333822015-02-18 22:19:45 -08003 This file is part of elfutils.
Ben Cheng25b3c042013-11-20 14:45:36 -08004
Elliott Hughes03333822015-02-18 22:19:45 -08005 This file is free software; you can redistribute it and/or modify
6 it under the terms of either
Ben Cheng25b3c042013-11-20 14:45:36 -08007
Elliott Hughes03333822015-02-18 22:19:45 -08008 * 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
Ben Cheng25b3c042013-11-20 14:45:36 -080021 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
Elliott Hughes03333822015-02-18 22:19:45 -080025 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/>. */
Ben Cheng25b3c042013-11-20 14:45:36 -080028
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
38Dwarf_Die *
39dwarf_formref_die (attr, result)
40 Dwarf_Attribute *attr;
41 Dwarf_Die *result;
42{
43 if (attr == NULL)
44 return NULL;
45
46 struct Dwarf_CU *cu = attr->cu;
47
48 Dwarf_Off offset;
Elliott Hughes03333822015-02-18 22:19:45 -080049 if (attr->form == DW_FORM_ref_addr || attr->form == DW_FORM_GNU_ref_alt)
Ben Cheng25b3c042013-11-20 14:45:36 -080050 {
51 /* This has an absolute offset. */
52
Elliott Hughes03333822015-02-18 22:19:45 -080053 uint8_t ref_size = (cu->version == 2 && attr->form == DW_FORM_ref_addr
Ben Cheng25b3c042013-11-20 14:45:36 -080054 ? cu->address_size
55 : cu->offset_size);
56
Elliott Hughes03333822015-02-18 22:19:45 -080057 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,
Ben Cheng25b3c042013-11-20 14:45:36 -080067 ref_size, &offset, IDX_debug_info, 0))
68 return NULL;
69
Elliott Hughes03333822015-02-18 22:19:45 -080070 return INTUSE(dwarf_offdie) (dbg_ret, offset, result);
Ben Cheng25b3c042013-11-20 14:45:36 -080071 }
72
Elliott Hughes03333822015-02-18 22:19:45 -080073 const unsigned char *datap;
74 size_t size;
Ben Cheng25b3c042013-11-20 14:45:36 -080075 if (attr->form == DW_FORM_ref_sig8)
76 {
77 /* This doesn't have an offset, but instead a value we
78 have to match in the .debug_types type unit headers. */
79
80 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 }
Ben Cheng25b3c042013-11-20 14:45:36 -080093 }
94 while (cu->type_sig8 != sig);
95
Elliott Hughes03333822015-02-18 22:19:45 -080096 datap = cu->dbg->sectiondata[IDX_debug_types]->d_buf;
97 size = cu->dbg->sectiondata[IDX_debug_types]->d_size;
Ben Cheng25b3c042013-11-20 14:45:36 -080098 offset = cu->type_offset;
99 }
100 else
101 {
102 /* Other forms produce an offset from the CU. */
103 if (unlikely (__libdw_formref (attr, &offset) != 0))
104 return NULL;
105
Elliott Hughes03333822015-02-18 22:19:45 -0800106 datap = cu->startp;
107 size = cu->endp - cu->startp;
Ben Cheng25b3c042013-11-20 14:45:36 -0800108 }
109
Elliott Hughes03333822015-02-18 22:19:45 -0800110 if (unlikely (offset >= size))
Ben Cheng25b3c042013-11-20 14:45:36 -0800111 {
112 __libdw_seterrno (DWARF_E_INVALID_DWARF);
113 return NULL;
114 }
115
116 memset (result, '\0', sizeof (Dwarf_Die));
Elliott Hughes03333822015-02-18 22:19:45 -0800117 result->addr = (char *) datap + offset;
Ben Cheng25b3c042013-11-20 14:45:36 -0800118 result->cu = cu;
119 return result;
120}
121INTDEF (dwarf_formref_die)