1 /* Return reference offset represented by attribute.
2    Copyright (C) 2003-2010, 2014 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
__libdw_formref(attr,return_offset)38 __libdw_formref (attr, return_offset)
39      Dwarf_Attribute *attr;
40      Dwarf_Off *return_offset;
41 {
42   const unsigned char *datap = attr->valp;
43   const unsigned char *endp = attr->cu->endp;
44 
45   if (attr->valp == NULL)
46     {
47       __libdw_seterrno (DWARF_E_INVALID_REFERENCE);
48       return -1;
49     }
50 
51   switch (attr->form)
52     {
53     case DW_FORM_ref1:
54       if (datap + 1 > endp)
55 	{
56 	invalid:
57 	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
58 	  return -1;
59 	}
60       *return_offset = *attr->valp;
61       break;
62 
63     case DW_FORM_ref2:
64       if (datap + 2 > endp)
65 	goto invalid;
66       *return_offset = read_2ubyte_unaligned (attr->cu->dbg, attr->valp);
67       break;
68 
69     case DW_FORM_ref4:
70       if (datap + 4 > endp)
71 	goto invalid;
72       *return_offset = read_4ubyte_unaligned (attr->cu->dbg, attr->valp);
73       break;
74 
75     case DW_FORM_ref8:
76       if (datap + 8 > endp)
77 	goto invalid;
78       *return_offset = read_8ubyte_unaligned (attr->cu->dbg, attr->valp);
79       break;
80 
81     case DW_FORM_ref_udata:
82       if (datap + 1 > endp)
83 	goto invalid;
84       get_uleb128 (*return_offset, datap, endp);
85       break;
86 
87     case DW_FORM_ref_addr:
88     case DW_FORM_ref_sig8:
89     case DW_FORM_GNU_ref_alt:
90       /* These aren't handled by dwarf_formref, only by dwarf_formref_die.  */
91       __libdw_seterrno (DWARF_E_INVALID_REFERENCE);
92       return -1;
93 
94     default:
95       __libdw_seterrno (DWARF_E_NO_REFERENCE);
96       return -1;
97     }
98 
99   return 0;
100 }
101 
102 /* This is the old public entry point.
103    It is now deprecated in favor of dwarf_formref_die.  */
104 int
dwarf_formref(attr,return_offset)105 dwarf_formref (attr, return_offset)
106      Dwarf_Attribute *attr;
107      Dwarf_Off *return_offset;
108 {
109   if (attr == NULL)
110     return -1;
111 
112   return __libdw_formref (attr, return_offset);
113 }
114