• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Return signed constant represented by attribute.
2    Copyright (C) 2003, 2005, 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 
38 int
dwarf_formsdata(attr,return_sval)39 dwarf_formsdata (attr, return_sval)
40      Dwarf_Attribute *attr;
41      Dwarf_Sword *return_sval;
42 {
43   if (attr == NULL)
44     return -1;
45 
46   const unsigned char *datap = attr->valp;
47   const unsigned char *endp = attr->cu->endp;
48 
49   switch (attr->form)
50     {
51     case DW_FORM_data1:
52       if (datap + 1 > endp)
53 	{
54 	invalid:
55 	  __libdw_seterrno (DWARF_E_INVALID_DWARF);
56 	  return -1;
57 	}
58       *return_sval = *attr->valp;
59       break;
60 
61     case DW_FORM_data2:
62       if (datap + 2 > endp)
63 	goto invalid;
64       *return_sval = read_2ubyte_unaligned (attr->cu->dbg, attr->valp);
65       break;
66 
67     case DW_FORM_data4:
68       if (datap + 4 > endp)
69 	goto invalid;
70       *return_sval = read_4ubyte_unaligned (attr->cu->dbg, attr->valp);
71       break;
72 
73     case DW_FORM_data8:
74       if (datap + 8 > endp)
75 	goto invalid;
76       *return_sval = read_8ubyte_unaligned (attr->cu->dbg, attr->valp);
77       break;
78 
79     case DW_FORM_sdata:
80       if (datap + 1 > endp)
81 	goto invalid;
82       get_sleb128 (*return_sval, datap, endp);
83       break;
84 
85     case DW_FORM_udata:
86       if (datap + 1 > endp)
87 	goto invalid;
88       get_uleb128 (*return_sval, datap, endp);
89       break;
90 
91     default:
92       __libdw_seterrno (DWARF_E_NO_CONSTANT);
93       return -1;
94     }
95 
96   return 0;
97 }
98 INTDEF(dwarf_formsdata)
99