/src/elfutils/libdw/libdw_form.c
Line | Count | Source (jump to first uncovered line) |
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 | | __libdw_form_val_compute_len (struct Dwarf_CU *cu, unsigned int form, |
43 | | const unsigned char *valp) |
44 | 0 | { |
45 | 0 | const unsigned char *startp = valp; |
46 | 0 | const unsigned char *endp = cu->endp; |
47 | 0 | Dwarf_Word u128; |
48 | 0 | 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 | 0 | switch (form) |
53 | 0 | { |
54 | 0 | case DW_FORM_addr: |
55 | 0 | result = cu->address_size; |
56 | 0 | break; |
57 | | |
58 | 0 | case DW_FORM_ref_addr: |
59 | 0 | result = cu->version == 2 ? cu->address_size : cu->offset_size; |
60 | 0 | break; |
61 | | |
62 | 0 | case DW_FORM_strp: |
63 | 0 | case DW_FORM_strp_sup: |
64 | 0 | case DW_FORM_line_strp: |
65 | 0 | case DW_FORM_sec_offset: |
66 | 0 | case DW_FORM_GNU_ref_alt: |
67 | 0 | case DW_FORM_GNU_strp_alt: |
68 | 0 | result = cu->offset_size; |
69 | 0 | break; |
70 | | |
71 | 0 | case DW_FORM_block1: |
72 | 0 | if (unlikely ((size_t) (endp - startp) < 1)) |
73 | 0 | goto invalid; |
74 | 0 | result = *valp + 1; |
75 | 0 | break; |
76 | | |
77 | 0 | case DW_FORM_block2: |
78 | 0 | if (unlikely ((size_t) (endp - startp) < 2)) |
79 | 0 | goto invalid; |
80 | 0 | result = read_2ubyte_unaligned (cu->dbg, valp) + 2; |
81 | 0 | break; |
82 | | |
83 | 0 | case DW_FORM_block4: |
84 | 0 | if (unlikely ((size_t) (endp - startp) < 4)) |
85 | 0 | goto invalid; |
86 | 0 | result = read_4ubyte_unaligned (cu->dbg, valp) + 4; |
87 | 0 | break; |
88 | | |
89 | 0 | case DW_FORM_block: |
90 | 0 | case DW_FORM_exprloc: |
91 | 0 | if (valp >= endp) |
92 | 0 | goto invalid; |
93 | 0 | get_uleb128 (u128, valp, endp); |
94 | 0 | result = u128 + (valp - startp); |
95 | 0 | break; |
96 | | |
97 | 0 | case DW_FORM_string: |
98 | 0 | { |
99 | 0 | const unsigned char *endstrp = memchr (valp, '\0', |
100 | 0 | (size_t) (endp - startp)); |
101 | 0 | if (unlikely (endstrp == NULL)) |
102 | 0 | goto invalid; |
103 | 0 | result = (size_t) (endstrp - startp) + 1; |
104 | 0 | break; |
105 | 0 | } |
106 | | |
107 | 0 | case DW_FORM_sdata: |
108 | 0 | case DW_FORM_udata: |
109 | 0 | case DW_FORM_ref_udata: |
110 | 0 | case DW_FORM_addrx: |
111 | 0 | case DW_FORM_loclistx: |
112 | 0 | case DW_FORM_rnglistx: |
113 | 0 | case DW_FORM_strx: |
114 | 0 | case DW_FORM_GNU_addr_index: |
115 | 0 | case DW_FORM_GNU_str_index: |
116 | 0 | if (valp >= endp) |
117 | 0 | goto invalid; |
118 | 0 | get_uleb128 (u128, valp, endp); |
119 | 0 | result = valp - startp; |
120 | 0 | break; |
121 | | |
122 | 0 | case DW_FORM_indirect: |
123 | | /* The amount of data to skip in the DIE is the size of the actual |
124 | | FORM data (which is __libdw_form_val_len) plus the size of the |
125 | | uleb128 encoding that FORM (which is valp - startp). */ |
126 | 0 | if (valp >= endp) |
127 | 0 | goto invalid; |
128 | 0 | get_uleb128 (u128, valp, endp); |
129 | 0 | if (*valp == DW_FORM_indirect || *valp == DW_FORM_implicit_const) |
130 | 0 | return (size_t) -1; |
131 | 0 | result = __libdw_form_val_len (cu, u128, valp); |
132 | 0 | if (result != (size_t) -1) |
133 | 0 | result += valp - startp; |
134 | 0 | else |
135 | 0 | return (size_t) -1; |
136 | 0 | break; |
137 | | |
138 | 0 | default: |
139 | 0 | goto invalid; |
140 | 0 | } |
141 | | |
142 | 0 | if (unlikely (result > (size_t) (endp - startp))) |
143 | 0 | { |
144 | 0 | invalid: |
145 | 0 | __libdw_seterrno (DWARF_E_INVALID_DWARF); |
146 | 0 | result = (size_t) -1; |
147 | 0 | } |
148 | |
|
149 | 0 | return result; |
150 | 0 | } |