Line data Source code
1 : /* Helper functions for form handling.
2 : Copyright (C) 2003-2009, 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 <string.h>
36 :
37 : #include "libdwP.h"
38 :
39 :
40 : size_t
41 : internal_function
42 4192030 : __libdw_form_val_compute_len (struct Dwarf_CU *cu, unsigned int form,
43 : const unsigned char *valp)
44 : {
45 4192030 : const unsigned char *startp = valp;
46 4192030 : const unsigned char *endp = cu->endp;
47 : Dwarf_Word u128;
48 : size_t result;
49 :
50 : /* NB: This doesn't cover constant form lengths, which are
51 : already handled by the inlined __libdw_form_val_len. */
52 4192030 : switch (form)
53 : {
54 422889 : case DW_FORM_addr:
55 422889 : result = cu->address_size;
56 422889 : break;
57 :
58 63796 : case DW_FORM_ref_addr:
59 63796 : result = cu->version == 2 ? cu->address_size : cu->offset_size;
60 : break;
61 :
62 3128654 : case DW_FORM_strp:
63 : case DW_FORM_sec_offset:
64 : case DW_FORM_GNU_ref_alt:
65 : case DW_FORM_GNU_strp_alt:
66 3128654 : result = cu->offset_size;
67 3128654 : break;
68 :
69 2600 : case DW_FORM_block1:
70 2600 : if (unlikely ((size_t) (endp - startp) < 1))
71 : goto invalid;
72 2600 : result = *valp + 1;
73 2600 : break;
74 :
75 3 : case DW_FORM_block2:
76 3 : if (unlikely ((size_t) (endp - startp) < 2))
77 : goto invalid;
78 3 : result = read_2ubyte_unaligned (cu->dbg, valp) + 2;
79 : break;
80 :
81 0 : case DW_FORM_block4:
82 0 : if (unlikely ((size_t) (endp - startp) < 4))
83 : goto invalid;
84 0 : result = read_4ubyte_unaligned (cu->dbg, valp) + 4;
85 : break;
86 :
87 365995 : case DW_FORM_block:
88 : case DW_FORM_exprloc:
89 365995 : get_uleb128 (u128, valp, endp);
90 365995 : result = u128 + (valp - startp);
91 365995 : break;
92 :
93 127606 : case DW_FORM_string:
94 : {
95 127606 : const unsigned char *endstrp = memchr (valp, '\0',
96 127606 : (size_t) (endp - startp));
97 127606 : if (unlikely (endstrp == NULL))
98 : goto invalid;
99 127606 : result = (size_t) (endstrp - startp) + 1;
100 127606 : break;
101 : }
102 :
103 80487 : case DW_FORM_sdata:
104 : case DW_FORM_udata:
105 : case DW_FORM_ref_udata:
106 80487 : get_uleb128 (u128, valp, endp);
107 80487 : result = valp - startp;
108 80487 : break;
109 :
110 0 : case DW_FORM_indirect:
111 0 : get_uleb128 (u128, valp, endp);
112 : // XXX Is this really correct?
113 0 : result = __libdw_form_val_len (cu, u128, valp);
114 0 : if (result != (size_t) -1)
115 0 : result += valp - startp;
116 : else
117 : return (size_t) -1;
118 0 : break;
119 :
120 : default:
121 : goto invalid;
122 : }
123 :
124 4192030 : if (unlikely (result > (size_t) (endp - startp)))
125 : {
126 0 : invalid:
127 0 : __libdw_seterrno (DWARF_E_INVALID_DWARF);
128 0 : result = (size_t) -1;
129 : }
130 :
131 : return result;
132 : }
|