blob: 3229baf624dc844dab920988d75059d93654c01e [file] [log] [blame]
Elliott Hughes03333822015-02-18 22:19:45 -08001/* Return DWARF attribute associated with a location expression op.
2 Copyright (C) 2013, 2014 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 <dwarf.h>
34#include <libdwP.h>
35
36static Dwarf_CU *
37attr_form_cu (Dwarf_Attribute *attr)
38{
39 /* If the attribute has block/expr form the data comes from the
40 .debug_info from the same cu as the attr. Otherwise it comes from
41 the .debug_loc data section. */
42 switch (attr->form)
43 {
44 case DW_FORM_block1:
45 case DW_FORM_block2:
46 case DW_FORM_block4:
47 case DW_FORM_block:
48 case DW_FORM_exprloc:
49 return attr->cu;
50 default:
51 return attr->cu->dbg->fake_loc_cu;
52 }
53}
54
55int
56dwarf_getlocation_attr (attr, op, result)
57 Dwarf_Attribute *attr;
58 const Dwarf_Op *op;
59 Dwarf_Attribute *result;
60{
61 if (attr == NULL)
62 return -1;
63
64 switch (op->atom)
65 {
66 case DW_OP_implicit_value:
67 result->code = DW_AT_const_value;
68 result->form = DW_FORM_block;
69 result->valp = (unsigned char *) (uintptr_t) op->number2;
70 result->cu = attr_form_cu (attr);
71 break;
72
73 case DW_OP_GNU_entry_value:
74 result->code = DW_AT_location;
75 result->form = DW_FORM_exprloc;
76 result->valp = (unsigned char *) (uintptr_t) op->number2;
77 result->cu = attr_form_cu (attr);
78 break;
79
80 case DW_OP_GNU_const_type:
81 result->code = DW_AT_const_value;
82 result->form = DW_FORM_block1;
83 result->valp = (unsigned char *) (uintptr_t) op->number2;
84 result->cu = attr_form_cu (attr);
85 break;
86
87 case DW_OP_call2:
88 case DW_OP_call4:
89 case DW_OP_call_ref:
90 {
91 Dwarf_Die die;
92 if (INTUSE(dwarf_getlocation_die) (attr, op, &die) != 0)
93 return -1;
94 if (INTUSE(dwarf_attr) (&die, DW_AT_location, result) == NULL)
95 {
96 __libdw_empty_loc_attr (result);
97 return 0;
98 }
99 }
100 break;
101
102 case DW_OP_GNU_implicit_pointer:
103 {
104 Dwarf_Die die;
105 if (INTUSE(dwarf_getlocation_die) (attr, op, &die) != 0)
106 return -1;
107 if (INTUSE(dwarf_attr) (&die, DW_AT_location, result) == NULL
108 && INTUSE(dwarf_attr) (&die, DW_AT_const_value, result) == NULL)
109 {
110 __libdw_empty_loc_attr (result);
111 return 0;
112 }
113 }
114 break;
115
116 default:
117 __libdw_seterrno (DWARF_E_INVALID_ACCESS);
118 return -1;
119 }
120
121 return 0;
122}