Debian Codegraph @

src/lib/libdwarf/dwarf_fill_in_attr_form.c

raw
1 /*
2 Copyright (C) 2022 David Anderson. All Rights Reserved.
3
4 Redistribution and use in source and binary forms, with
5 or without modification, are permitted provided that the
6 following conditions are met:
7
8 Redistributions of source code must retain the above
9 copyright notice, this list of conditions and the following
10 disclaimer.
11
12 Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials
15 provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
18 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
19 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "config.h"
33 #include <stddef.h> /* NULL size_t */
34 #include <stdlib.h> /* calloc() free() malloc() */
35 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
36 #include "stdafx.h"
37 #endif /* HAVE_STDAFX_H */
38
39 #include "dwarf.h"
40 #include "libdwarf.h"
41 #include "libdwarf_private.h"
42 #include "dwarf_base_types.h"
43 #include "dwarf_opaque.h"
44 #include "dwarf_alloc.h"
45 #include "dwarf_die_deliv.h"
46 #include "dwarf_abbrev.h"
47 #include "dwarf_error.h"
48 #include "dwarf_util.h"
49 #include "dwarf_string.h"
50
51 static void
52 build_alloc_ab_error(Dwarf_Debug dbg,
53 Dwarf_Unsigned count,
54 const char *fieldname,
55 Dwarf_Error *error)
56 {
57 dwarfstring m;
58
59 dwarfstring_constructor(&m);
60 dwarfstring_append_printf_s(&m,
61 "DW_DLE_ALLOC_FAIL :"
62 " Attempt to malloc space for %s ",
63 (char *)fieldname);
64 dwarfstring_append_printf_u(&m,
65 " with %u entries failed.",
66 count);
67 _dwarf_error_string(dbg,error,DW_DLE_ALLOC_FAIL,
68 dwarfstring_string(&m));
69 dwarfstring_destructor(&m);
70 }
71
72 /*
73 This is a pre-scan of the abbrev/form list.
74 We will not handle DW_FORM_indirect here as that
75 accesses data outside of the abbrev section.
76 */
77 int
78 _dwarf_fill_in_attr_form_abtable(Dwarf_CU_Context context,
79 Dwarf_Byte_Ptr abbrev_ptr,
80 Dwarf_Byte_Ptr abbrev_end,
81 Dwarf_Abbrev_List abbrev_list,
82 Dwarf_Error *error)
83 {
84 Dwarf_Debug dbg = 0;
85 Dwarf_Unsigned i = 0;
86
87 dbg = context->cc_dbg;
88 abbrev_list->abl_attr = (Dwarf_Half*)
89 calloc(abbrev_list->abl_abbrev_count,
90 SIZEOFT16);
91 if (!abbrev_list->abl_attr) {
92 build_alloc_ab_error(dbg,abbrev_list->abl_abbrev_count,
93 "abbrev_list->abl_attr",error);
94 return DW_DLV_ERROR;
95 }
96 abbrev_list->abl_form = (Dwarf_Half *)
97 calloc(abbrev_list->abl_abbrev_count,
98 SIZEOFT16);
99 if (!abbrev_list->abl_form) {
100 build_alloc_ab_error(dbg,abbrev_list->abl_abbrev_count,
101 "abbrev_list->abl_form",error);
102 return DW_DLV_ERROR;
103 }
104 if (abbrev_list->abl_implicit_const_count > 0) {
105 abbrev_list->abl_implicit_const = (Dwarf_Signed *)
106 calloc(abbrev_list->abl_abbrev_count,
107 sizeof(Dwarf_Signed));
108 if (!abbrev_list->abl_implicit_const) {
109 build_alloc_ab_error(dbg,abbrev_list->abl_abbrev_count,
110 "abbrev_list->abl_implicit_const",error);
111 return DW_DLV_ERROR;
112 }
113 }
114
115 for (i = 0; i < abbrev_list->abl_abbrev_count; ++i) {
116 Dwarf_Unsigned attr = 0;
117 Dwarf_Unsigned attr_form = 0;
118 Dwarf_Signed implicit_const = 0;
119 int res = 0;
120
121 res = _dwarf_leb128_uword_wrapper(dbg,
122 &abbrev_ptr,abbrev_end,&attr,error);
123 if (res == DW_DLV_ERROR) {
124 return res;
125 }
126 if (attr > 0xffff) {
127 dwarfstring m;
128 dwarfstring_constructor(&m);
129 dwarfstring_append(&m,
130 "DW_DLE_ATTR_FORM_SIZE_BAD :"
131 " reading Attribute number ");
132 dwarfstring_append(&m," for abbrev list entry"
133 " the ULEB number is too large. Corrupt Dwarf.");
134 _dwarf_error_string(dbg,error,DW_DLE_ATTR_FORM_SIZE_BAD,
135 dwarfstring_string(&m));
136 dwarfstring_destructor(&m);
137 return DW_DLV_ERROR;
138 }
139 abbrev_list->abl_attr[i] = (Dwarf_Half)attr;
140 if (attr > DW_AT_hi_user) {
141 _dwarf_error(dbg, error,DW_DLE_ATTR_CORRUPT);
142 return DW_DLV_ERROR;
143 }
144 res = _dwarf_leb128_uword_wrapper(dbg,
145 &abbrev_ptr,abbrev_end,&attr_form,error);
146 if (res == DW_DLV_ERROR) {
147 return res;
148 }
149 if (attr_form > 0xffff) {
150 _dwarf_error_string(dbg, error,
151 DW_DLE_ATTR_FORM_SIZE_BAD,
152 "DW_DLE_ATTR_FORM_SIZE_BAD :"
153 " reading attr_form of"
154 " an abbrev list entry: "
155 "the ULEB form number is too large "
156 "to be valid. Corrupt Dwarf.");
157 return DW_DLV_ERROR;
158 }
159 if (!_dwarf_valid_form_we_know(attr_form,attr)) {
160 dwarfstring m;
161
162 dwarfstring_constructor(&m);
163 dwarfstring_append(&m,"Reading an abbreviation list "
164 " we find the attribute form pair to be "
165 " impossible or unknown.");
166 dwarfstring_append_printf_u(&m," attr 0x%x ",attr);
167 dwarfstring_append_printf_u(&m," attrform 0x%x ",
168 attr_form);
169 _dwarf_error_string(dbg, error, DW_DLE_UNKNOWN_FORM,
170 dwarfstring_string(&m));
171 dwarfstring_destructor(&m);
172 return DW_DLV_ERROR;
173 }
174 abbrev_list->abl_attr[i] = (Dwarf_Half)attr;
175 abbrev_list->abl_form[i] = (Dwarf_Half)attr_form;
176 if (attr_form == DW_FORM_implicit_const) {
177 res = _dwarf_leb128_sword_wrapper(dbg,
178 &abbrev_ptr,abbrev_end,&implicit_const,error);
179 if (res == DW_DLV_ERROR) {
180 return res;
181 }
182 abbrev_list->abl_implicit_const_count++;
183 abbrev_list->abl_implicit_const[i] = implicit_const;
184 }
185 #if 0 /* Do nothing special for DW_FORM_indirect here. Ignore. */
186 if (attr_form == DW_FORM_indirect) {
187 /* Do nothing special here. Do not read
188 from the DIE till reading for
189 a specific DIE, which we are not
190 intending here, we do not know
191 where the DIE is. */
192 }
193 #endif
194 }
195 return DW_DLV_OK;
196 }