Imported Upstream version 0.155
[platform/upstream/elfutils.git] / libdw / dwarf_formref.c
1 /* Return reference offset represented by attribute.
2    Copyright (C) 2003-2010 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2003.
5
6    This file is free software; you can redistribute it and/or modify
7    it under the terms of either
8
9      * the GNU Lesser General Public License as published by the Free
10        Software Foundation; either version 3 of the License, or (at
11        your option) any later version
12
13    or
14
15      * the GNU General Public License as published by the Free
16        Software Foundation; either version 2 of the License, or (at
17        your option) any later version
18
19    or both in parallel, as here.
20
21    elfutils is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24    General Public License for more details.
25
26    You should have received copies of the GNU General Public License and
27    the GNU Lesser General Public License along with this program.  If
28    not, see <http://www.gnu.org/licenses/>.  */
29
30 #ifdef HAVE_CONFIG_H
31 # include <config.h>
32 #endif
33
34 #include <dwarf.h>
35 #include "libdwP.h"
36
37 int
38 __libdw_formref (attr, return_offset)
39      Dwarf_Attribute *attr;
40      Dwarf_Off *return_offset;
41 {
42   const unsigned char *datap;
43
44   if (attr->valp == NULL)
45     {
46       __libdw_seterrno (DWARF_E_INVALID_REFERENCE);
47       return -1;
48     }
49
50   switch (attr->form)
51     {
52     case DW_FORM_ref1:
53       *return_offset = *attr->valp;
54       break;
55
56     case DW_FORM_ref2:
57       *return_offset = read_2ubyte_unaligned (attr->cu->dbg, attr->valp);
58       break;
59
60     case DW_FORM_ref4:
61       *return_offset = read_4ubyte_unaligned (attr->cu->dbg, attr->valp);
62       break;
63
64     case DW_FORM_ref8:
65       *return_offset = read_8ubyte_unaligned (attr->cu->dbg, attr->valp);
66       break;
67
68     case DW_FORM_ref_udata:
69       datap = attr->valp;
70       get_uleb128 (*return_offset, datap);
71       break;
72
73     case DW_FORM_ref_addr:
74     case DW_FORM_ref_sig8:
75     case DW_FORM_GNU_ref_alt:
76       /* These aren't handled by dwarf_formref, only by dwarf_formref_die.  */
77       __libdw_seterrno (DWARF_E_INVALID_REFERENCE);
78       return -1;
79
80     default:
81       __libdw_seterrno (DWARF_E_NO_REFERENCE);
82       return -1;
83     }
84
85   return 0;
86 }
87
88 /* This is the old public entry point.
89    It is now deprecated in favor of dwarf_formref_die.  */
90 int
91 dwarf_formref (attr, return_offset)
92      Dwarf_Attribute *attr;
93      Dwarf_Off *return_offset;
94 {
95   if (attr == NULL)
96     return -1;
97
98   return __libdw_formref (attr, return_offset);
99 }