Debian Codegraph @

src/lib/libdwarf/dwarf_debugaddr.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
33 #include <config.h>
34
35 #include <stddef.h> /* NULL size_t */
36 #include <stdio.h> /* debug printf */
37 #include <string.h> /* memset() strlen() */
38
39 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
40 #include "stdafx.h"
41 #endif /* HAVE_STDAFX_H */
42
43 #include "dwarf.h"
44 #include "libdwarf.h"
45 #include "libdwarf_private.h"
46 #include "dwarf_base_types.h"
47 #include "dwarf_opaque.h"
48 #include "dwarf_alloc.h"
49 #include "dwarf_error.h"
50 #include "dwarf_util.h"
51 #include "dwarf_debugaddr.h"
52 #include "dwarf_string.h"
53
54 #if 0 /* dump_bytes */
55 static void
56 dump_bytes(const char *msg,Dwarf_Small * start, long len)
57 {
58 Dwarf_Small *end = start + len;
59 Dwarf_Small *cur = start;
60 int linelim=16;
61 int lineb = 0;
62
63 printf("dump_bytes: %s %ld starting %p \n",msg,len,
64 (void *)start);
65 fflush(stdout);
66 for (; cur < end; cur++, ++lineb) {
67 if (lineb == linelim) {
68 printf("\n");
69 lineb = 0;
70 }
71 printf("%02x",*cur);
72 }
73 printf("\n");
74 fflush(stdout);
75 }
76 #endif /* 0 */
77
78 int
79 dwarf_debug_addr_table(Dwarf_Debug dbg,
80 Dwarf_Unsigned dw_section_offset,
81 Dwarf_Debug_Addr_Table *dw_table_header,
82 Dwarf_Unsigned *dw_length,
83 Dwarf_Half *dw_version,
84 Dwarf_Small *dw_address_size,
85 Dwarf_Unsigned *dw_at_addr_base,
86 Dwarf_Unsigned *dw_entry_count,
87 Dwarf_Unsigned *dw_next_table_offset,
88 Dwarf_Error *error)
89 {
90 int res = 0;
91 struct Dwarf_Debug_Addr_Table_s tab;
92 Dwarf_Unsigned section_size = 0;
93 Dwarf_Small *end_data = 0;
94 Dwarf_Small *data = 0;
95 Dwarf_Small *section_start = 0;
96 Dwarf_Unsigned arealen = 0;
97 Dwarf_Unsigned tablelen = 0;
98 int offset_size = 0;
99 int exten_size = 0;
100 Dwarf_Small address_size = 0;
101 Dwarf_Small segment_selector_size = 0;
102 Dwarf_Half version = 0;
103 Dwarf_Unsigned curlocaloffset = 0;
104 Dwarf_Unsigned offset_one_past_end = 0;
105 /* we will instantiate this below */
106 Dwarf_Debug_Addr_Table newad = 0;
107
108 CHECK_DBG(dbg,error,"dwarf_debug_addr_table()");
109 res = _dwarf_load_section(dbg, &dbg->de_debug_addr,error);
110 if (res == DW_DLV_NO_ENTRY) {
111 return res;
112 }
113 if (res == DW_DLV_ERROR) {
114 if (error) {
115 Dwarf_Error e = *error;
116
117 /* Lets append info to the error string! */
118 if (e->er_static_alloc != DE_STATIC) {
119 dwarfstring *em = (dwarfstring*)(e->er_msg);
120 dwarfstring_append(em, "Unable to open "
121 ".debug_addr section, serious error");
122 }
123 }
124 return DW_DLV_ERROR;
125 }
126 memset(&tab,0,sizeof(tab));
127 tab.da_magic = DW_ADDR_TABLE_MAGIC;
128 section_size = dbg->de_debug_addr.dss_size;
129 section_start = dbg->de_debug_addr.dss_data;
130 end_data = section_start + section_size;
131 tab.da_section_size = section_size;
132 if (dw_section_offset >= section_size) {
133 return DW_DLV_NO_ENTRY;
134 }
135 if (dbg->de_debug_addr_version == DW_CU_VERSION4) {
136 /* Create a table from what we know. */
137 address_size = (Dwarf_Small)dbg->de_debug_addr_address_size;
138 offset_size = dbg->de_debug_addr_offset_size;
139 tab.da_address_size = address_size;
140 tab.da_length_size = (Dwarf_Small)offset_size;
141 tab.da_length = section_size;
142 tab.da_version = dbg->de_debug_addr_version;
143 end_data = section_start + section_size;
144 tab.da_end_table = end_data;
145 /* Must be zero. */
146 if (dw_section_offset) {
147 _dwarf_error_string(dbg,error,DW_DLE_DEBUG_ADDR_ERROR,
148 "DW_DLE_DEBUG_ADDR_ERROR: DWARF4 extension "
149 ".debug_addr has non-zero offset. Impossible");
150 return DW_DLV_ERROR;
151 }
152 tab.da_table_section_offset = dw_section_offset;
153 tab.da_data_entries = section_start;
154 tab.da_entry_count= section_size/tab.da_address_size;
155 /* One past end of this Debug Addr Table */
156 tab.da_end_table = end_data;
157 if (tab.da_address_size != 4 && tab.da_address_size != 8 &&
158 tab.da_address_size != 2) {
159 dwarfstring m;
160 dwarfstring_constructor(&m);
161 dwarfstring_append_printf_u(&m,
162 " DW_DLE_ADDRESS_SIZE_ERROR: The "
163 " .debug_addr DWARF4 address size "
164 "of %u is not supported.",address_size);
165 _dwarf_error_string(dbg,error,DW_DLE_ADDRESS_SIZE_ERROR,
166 dwarfstring_string(&m));
167 dwarfstring_destructor(&m);
168 return DW_DLV_ERROR;
169 }
170 offset_one_past_end = section_size;
171 tab.da_dbg = dbg;
172 } else {
173 /* all other cases, assume DWARF5 table. */
174 curlocaloffset = 0;
175 data = section_start + dw_section_offset;
176 READ_AREA_LENGTH_CK(dbg,arealen,Dwarf_Unsigned,
177 data,offset_size,exten_size,
178 error,
179 section_size,end_data);
180 if (arealen > section_size ||
181 (arealen + offset_size +exten_size) > section_size) {
182 dwarfstring m;
183 dwarfstring_constructor(&m);
184 dwarfstring_append_printf_u(&m,
185 "DW_DLE_SECTION_SIZE_ERROR: A .debug_addr "
186 "area size of 0x%x ",arealen);
187 dwarfstring_append_printf_u(&m,
188 "at offset 0x%x ",dw_section_offset);
189 dwarfstring_append_printf_u(&m,
190 "is larger than the entire section size of "
191 "0x%x. Corrupt DWARF.",section_size);
192 _dwarf_error_string(dbg,error,
193 DW_DLE_SECTION_SIZE_ERROR,
194 dwarfstring_string(&m));
195 dwarfstring_destructor(&m);
196 return DW_DLV_ERROR;
197 }
198 tab.da_dbg = dbg;
199 tablelen = 0;
200 if (arealen <= 4) {
201 _dwarf_error_string(dbg,error,
202 DW_DLE_SECTION_SIZE_ERROR,
203 "DW_DLE_SECTION_SIZE_ERROR: "
204 "The end of a .debug_addr header record is missing, "
205 "corrupt DWARF");
206 return DW_DLV_ERROR;
207 }
208 tablelen = arealen - 4; /* 4: the rest of the header */
209 tab.da_length = tablelen;
210 curlocaloffset = offset_size + exten_size;
211 offset_one_past_end = dw_section_offset
212 + curlocaloffset + 4 /*rest of header */
213 + tablelen;
214 end_data = section_start + offset_one_past_end;
215 tab.da_end_table = end_data;
216 READ_UNALIGNED_CK(dbg,version,Dwarf_Half,data,
217 SIZEOFT16,error,end_data);
218 if (version != DW_CU_VERSION5) {
219 dwarfstring m;
220 dwarfstring_constructor(&m);
221 dwarfstring_append_printf_u(&m,
222 "DW_DLE_VERSION_STAMP_ERROR: "
223 "The .debug_addr version should be 5 "
224 "but we find %u instead.",version);
225 _dwarf_error_string(dbg,error,
226 DW_DLE_VERSION_STAMP_ERROR,
227 dwarfstring_string(&m));
228 dwarfstring_destructor(&m);
229 return DW_DLV_ERROR;
230 }
231 tab.da_version = version;
232 data += SIZEOFT16;
233 curlocaloffset += SIZEOFT16;
234 READ_UNALIGNED_CK(dbg,address_size,Dwarf_Small,data,
235 1,error,end_data);
236 if (address_size != 4 && address_size != 8 &&
237 address_size != 2) {
238 dwarfstring m;
239 dwarfstring_constructor(&m);
240 dwarfstring_append_printf_u(&m,
241 " DW_DLE_ADDRESS_SIZE_ERROR: The "
242 " .debug_addr address size "
243 "of %u is not supported.",address_size);
244 _dwarf_error_string(dbg,error,DW_DLE_ADDRESS_SIZE_ERROR,
245 dwarfstring_string(&m));
246 dwarfstring_destructor(&m);
247 return DW_DLV_ERROR;
248 }
249 tab.da_address_size = address_size;
250 data++;
251 curlocaloffset++;
252
253 READ_UNALIGNED_CK(dbg,segment_selector_size,Dwarf_Small,data,
254 1,error,end_data);
255 if (segment_selector_size != 0) {
256 dwarfstring m;
257 dwarfstring_constructor(&m);
258 dwarfstring_append(&m,
259 " DW_DLE_DEBUG_ADDR_ERROR: The "
260 " .debug_addr segment selector size "
261 "of non-zero is not supported.");
262 _dwarf_error_string(dbg,error,DW_DLE_DEBUG_ADDR_ERROR,
263 dwarfstring_string(&m));
264 dwarfstring_destructor(&m);
265 return DW_DLV_ERROR;
266 }
267 /* We do not record segment selector size,
268 as it is not supported. */
269 curlocaloffset++;
270 data++;
271 tab.da_data_entries = data;
272 }
273 /* Now we are at the beginning of the actual table */
274 {
275 Dwarf_Unsigned entry_count = 0;
276 /* Two byte version and two byte flags preceed the
277 actual table */
278 Dwarf_Unsigned table_len_bytes = tab.da_length;
279
280 if (table_len_bytes%(Dwarf_Unsigned)address_size) {
281 dwarfstring m;
282 dwarfstring_constructor(&m);
283 dwarfstring_append_printf_u(&m,
284 " DW_DLE_DEBUG_ADDR_ERROR: The "
285 " .debug_addr address array "
286 "length of %u not a multiple of "
287 "address_size.",table_len_bytes);
288 _dwarf_error_string(dbg,error,
289 DW_DLE_DEBUG_ADDR_ERROR,
290 dwarfstring_string(&m));
291 dwarfstring_destructor(&m);
292 return DW_DLV_ERROR;
293 }
294 entry_count = table_len_bytes/address_size;
295 tab.da_entry_count = entry_count;
296 }
297 tab.da_table_section_offset = dw_section_offset;
298 tab.da_addr_base = dw_section_offset + curlocaloffset;
299 /* Do alloc as late as possible to avoid
300 any concern about missing a dealloc in
301 case of error. */
302 newad = (Dwarf_Debug_Addr_Table)
303 _dwarf_get_alloc(dbg,DW_DLA_DEBUG_ADDR,1);
304 if (!newad) {
305 _dwarf_error_string(dbg, error,
306 DW_DLE_ALLOC_FAIL,
307 "DW_DLE_ALLOC_FAIL: "
308 "allocating a Dwarf_Debug_Addr_Table "
309 "record.");
310 return DW_DLV_ERROR;
311 }
312 /* Copy structure itself */
313 *newad = tab;
314 *dw_table_header = newad;
315 if (dw_length) {
316 *dw_length = newad->da_length;
317 }
318 if (dw_version) {
319 *dw_version = newad->da_version;
320 }
321 if (dw_address_size) {
322 *dw_address_size = newad->da_address_size;
323 }
324 if (dw_at_addr_base) {
325 *dw_at_addr_base = newad->da_addr_base;
326 }
327 if (dw_entry_count) {
328 *dw_entry_count = newad->da_entry_count;
329 }
330 if (dw_next_table_offset) {
331 *dw_next_table_offset = offset_one_past_end;
332 }
333 return DW_DLV_OK;
334 }
335
336 int
337 dwarf_debug_addr_by_index(Dwarf_Debug_Addr_Table dw_dat,
338 Dwarf_Unsigned dw_entry_index,
339 Dwarf_Unsigned *dw_address,
340 Dwarf_Error *dw_error)
341 {
342 Dwarf_Small *data = 0;
343 Dwarf_Unsigned addr = 0;
344
345 if (!dw_dat) {
346 _dwarf_error_string(NULL,dw_error,
347 DW_DLE_DEBUG_ADDR_ERROR,
348 "DW_DLE_DEBUG_ADDR_ERROR: "
349 "NULL dw_dat passed in.");
350 return DW_DLV_ERROR;
351 }
352 if (dw_dat->da_magic != DW_ADDR_TABLE_MAGIC) {
353 _dwarf_error_string(NULL,dw_error,
354 DW_DLE_DEBUG_ADDR_ERROR,
355 "DW_DLE_DEBUG_ADDR_ERROR: "
356 "Bad debug addr table magic number. ");
357 return DW_DLV_ERROR;
358 }
359 if (dw_entry_index >= dw_dat->da_entry_count) {
360 return DW_DLV_NO_ENTRY;
361 }
362 data = dw_dat->da_data_entries+
363 dw_dat->da_address_size * dw_entry_index;
364 if ((data+ dw_dat->da_address_size) > dw_dat->da_end_table) {
365 _dwarf_error_string(NULL,dw_error,
366 DW_DLE_DEBUG_ADDR_ERROR,
367 "DW_DLE_DEBUG_ADDR_ERROR: "
368 "Bad debug addr table: miscount, too short. ");
369 return DW_DLV_ERROR;
370 }
371 READ_UNALIGNED_CK(dw_dat->da_dbg,
372 addr,Dwarf_Unsigned, data,
373 dw_dat->da_address_size,dw_error,
374 dw_dat->da_end_table);
375 *dw_address = addr;
376 return DW_DLV_OK;
377 }
378
379 void
380 dwarf_dealloc_debug_addr_table(Dwarf_Debug_Addr_Table dw_dat)
381 {
382 Dwarf_Debug dbg = 0;
383 if (!dw_dat) {
384 return;
385 }
386 if (dw_dat->da_magic != DW_ADDR_TABLE_MAGIC) {
387 return;
388 }
389 dbg = dw_dat->da_dbg;
390 dw_dat->da_magic = 0;
391 dwarf_dealloc(dbg,dw_dat,DW_DLA_DEBUG_ADDR);
392 }