cb290456be55cf237ad2c2867bb75b8809add237
[platform/upstream/elfutils.git] / libdw / dwarf_getlocation_attr.c
1 /* Return DWARF attribute associated with a location expression op.
2    Copyright (C) 2013 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
36
37 int
38 dwarf_getlocation_attr (attr, op, result)
39      Dwarf_Attribute *attr;
40      const Dwarf_Op *op;
41      Dwarf_Attribute *result;
42 {
43   if (attr == NULL)
44     return -1;
45
46   result->cu = attr->cu;
47
48   switch (op->atom)
49     {
50       case DW_OP_implicit_value:
51         result->code = DW_AT_const_value;
52         result->form = DW_FORM_block;
53         result->valp = (unsigned char *) (uintptr_t) op->number2;
54         break;
55
56       case DW_OP_GNU_entry_value:
57         result->code = DW_AT_location;
58         result->form = DW_FORM_exprloc;
59         result->valp = (unsigned char *) (uintptr_t) op->number2;
60         break;
61
62       case DW_OP_GNU_const_type:
63         result->code = DW_AT_const_value;
64         result->form = DW_FORM_block1;
65         result->valp = (unsigned char *) (uintptr_t) op->number2;
66         break;
67
68       case DW_OP_call2:
69       case DW_OP_call4:
70       case DW_OP_call_ref:
71         {
72           Dwarf_Die die;
73           if (INTUSE(dwarf_getlocation_die) (attr, op, &die) != 0)
74             return -1;
75           if (INTUSE(dwarf_attr) (&die, DW_AT_location, result) == NULL)
76             {
77               __libdw_empty_loc_attr (result, attr->cu);
78               return 0;
79             }
80         }
81         break;
82
83       case DW_OP_GNU_implicit_pointer:
84         {
85           Dwarf_Die die;
86           if (INTUSE(dwarf_getlocation_die) (attr, op, &die) != 0)
87             return -1;
88           if (INTUSE(dwarf_attr) (&die, DW_AT_location, result) == NULL
89               && INTUSE(dwarf_attr) (&die, DW_AT_const_value, result) == NULL)
90             {
91               __libdw_empty_loc_attr (result, attr->cu);
92               return 0;
93             }
94         }
95         break;
96
97       default:
98         __libdw_seterrno (DWARF_E_INVALID_ACCESS);
99         return -1;
100     }
101
102   return 0;
103 }