Debian Codegraph @

src/lib/libdwarf/dwarf_ranges.c

raw
1 /*
2 Copyright (C) 2008-2020 David Anderson. All Rights Reserved.
3 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
4
5 This program is free software; you can redistribute it
6 and/or modify it under the terms of version 2.1 of the
7 GNU Lesser General Public License as published by the Free
8 Software Foundation.
9
10 This program is distributed in the hope that it would be
11 useful, but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.
14
15 Further, this software is distributed without any warranty
16 that it is free of the rightful claim of any third person
17 regarding infringement or the like. Any license provided
18 herein, whether implied or otherwise, applies only to this
19 software file. Patent licenses, if any, provided herein
20 do not apply to combinations of this program with other
21 software, or any other product whatsoever.
22
23 You should have received a copy of the GNU Lesser General
24 Public License along with this program; if not, write the
25 Free Software Foundation, Inc., 51 Franklin Street - Fifth
26 Floor, Boston MA 02110-1301, USA.
27
28 */
29
30 #include <config.h>
31
32 #include <stdlib.h> /* calloc() free() */
33 #include <stdio.h> /* printf debugging */
34
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_error.h"
46 #include "dwarf_util.h"
47 #include "dwarf_string.h"
48
49 #define DEBUG_RANGES 1
50 #undef DEBUG_RANGES
51
52 struct ranges_entry {
53 struct ranges_entry *next;
54 Dwarf_Ranges cur;
55 };
56
57 static void
58 free_allocated_ranges( struct ranges_entry *base)
59 {
60 struct ranges_entry *cur = 0;
61 struct ranges_entry *next = 0;
62 for ( cur = base ; cur ; cur = next ) {
63 next = cur->next;
64 free(cur);
65 }
66 }
67
68 /* We encapsulate the macro use so we can
69 free local malloc resources that would otherwise
70 leak. See the call points below. */
71 static int
72 read_unaligned_addr_check(Dwarf_Debug dbg,
73 Dwarf_Addr *addr_out,
74 Dwarf_Small *rangeptr,
75 unsigned address_size,
76 Dwarf_Error *error,
77 Dwarf_Small *section_end)
78 {
79 Dwarf_Addr a = 0;
80
81 READ_UNALIGNED_CK(dbg,a,
82 Dwarf_Addr, rangeptr,
83 address_size,
84 error,section_end);
85 *addr_out = a;
86 return DW_DLV_OK;
87 }
88 /* As of DWARF5 the ranges section each range list set has
89 a range-list-table header. See "7.28 Range List Table"
90 in the DWARF5 standard.
91 For DWARF5 the offset should be the offset of
92 the range-list-table-header for that range list.
93 For DWARF3 and DWARF4 the offset has to be that
94 of a range list.
95 */
96 /* Ranges and pc values can be in a split dwarf object.
97 In that case the appropriate values need to be
98 incremented by data from the executable in
99 the compilation unit with the same dwo_id.
100
101 We return an error which is on the incoming dbg, not
102 the possibly-tied-dbg localdbg.
103 If incoming die is NULL there is no context, so do not look
104 for a tied file, and address_size is the size
105 of the overall object, not the address_size of the context. */
106 #define MAX_ADDR ((address_size == 8)? \
107 0xffffffffffffffffULL:0xffffffff)
108 /* New 10 September 2020 to accommodate the
109 GNU extension of DWARF4 split-dwarf.
110 The actual_offset field is set by the function
111 to the actual final offset of the ranges
112 in the separate tied (a.out) file. */
113 int dwarf_get_ranges_b(Dwarf_Debug dbg,
114 Dwarf_Off rangesoffset,
115 Dwarf_Die die,
116 Dwarf_Off *actual_offset /* in .debug_ranges */,
117 Dwarf_Ranges ** rangesbuf,
118 Dwarf_Signed * listlen,
119 Dwarf_Unsigned * bytecount,
120 Dwarf_Error * error)
121 {
122 Dwarf_Small *rangeptr = 0;
123 Dwarf_Small *beginrangeptr = 0;
124 Dwarf_Small *section_end = 0;
125 unsigned entry_count = 0;
126 struct ranges_entry *base = 0;
127 struct ranges_entry *last = 0;
128 struct ranges_entry *curre = 0;
129 Dwarf_Ranges * ranges_data_out = 0;
130 unsigned copyindex = 0;
131 Dwarf_Half address_size = 0;
132 int res = DW_DLV_ERROR;
133 Dwarf_Unsigned ranges_base = 0;
134 Dwarf_Debug localdbg = dbg;
135
136 /* default for dwarf_get_ranges() */
137 Dwarf_Half die_version = 3;
138
139 Dwarf_Half offset_size= 4;
140 Dwarf_CU_Context cucontext = 0;
141
142 CHECK_DBG(dbg,error,"dwarf_get_ranges_b()");
143 address_size = localdbg->de_pointer_size; /* default */
144 if (die) {
145 /* printing DW_AT_ranges attribute. and the local DIE
146 it belongs to.
147 If we wind up using the tied file the die_version
148 had better match! It cannot be other than a match.
149 Can return DW_DLV_ERROR, not DW_DLV_NO_ENTRY.
150 Add err code if error. Version comes from the
151 cu context, not the DIE itself. */
152 res = dwarf_get_version_of_die(die,&die_version,
153 &offset_size);
154 if (res == DW_DLV_ERROR) {
155 _dwarf_error(dbg, error, DW_DLE_DIE_NO_CU_CONTEXT);
156 return DW_DLV_ERROR;
157 }
158 if (!die->di_cu_context) {
159 _dwarf_error(dbg, error, DW_DLE_DIE_NO_CU_CONTEXT);
160 return DW_DLV_ERROR;
161 }
162 cucontext = die->di_cu_context;
163 /* The DW4 ranges base was never used in GNU
164 but did get emitted, the note says, but
165 the note is probably obsolete (so, now wrong).
166 http://llvm.1065342.n5.nabble.com/DebugInfo\
167 -DW-AT-GNU-ranges-base-in-non-fission-\
168 td64194.html
169 HOWEVER: in dw4 GNU fission extension
170 it is used and matters.
171 */
172 /* ranges_base was merged from tied context.
173 Otherwise it is zero. But not if
174 the current die is the skeleton */
175 if (cucontext->cc_unit_type != DW_UT_skeleton) {
176 ranges_base = cucontext->cc_ranges_base;
177 }
178 rangesoffset += ranges_base;
179 address_size = cucontext->cc_address_size;
180 } else {
181 /* Printing by raw offset
182 The caller will use the bytecount to
183 increment to the next part of .debug_ranges
184 and will call again with the next offset */
185 }
186
187 localdbg = dbg;
188 res = _dwarf_load_section(localdbg, &localdbg->de_debug_ranges,
189 error);
190 if (res == DW_DLV_ERROR) {
191 return res;
192 }
193 /* FIX. HAS_TIED or ? */
194 if (res == DW_DLV_NO_ENTRY) {
195 /* data is in a.out, not dwp */
196 if (!DBG_HAS_SECONDARY(dbg)) {
197 return DW_DLV_NO_ENTRY;
198 }
199 localdbg = dbg->de_secondary_dbg;
200 res = _dwarf_load_section(localdbg,
201 &localdbg->de_debug_ranges, error);
202 if (res == DW_DLV_ERROR) {
203 /* Error will automatically be put on dbg (main
204 dbg), not localdbg (tieddbg) as of late 2024. */
205 return res;
206 }
207 if (res == DW_DLV_NO_ENTRY) {
208 return res;
209 }
210 }
211
212 /* Be safe in case adding rangesoffset and rangebase
213 overflows. */
214 if (rangesoffset >= localdbg->de_debug_ranges.dss_size) {
215 /* Documented behavior in libdwarf2.1.mm */
216 return DW_DLV_NO_ENTRY;
217 }
218 if (ranges_base >= localdbg->de_debug_ranges.dss_size) {
219 dwarfstring m;
220
221 dwarfstring_constructor(&m);
222 dwarfstring_append_printf_u(&m,
223 "DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
224 " ranges base is 0x%lx ",ranges_base);
225 dwarfstring_append_printf_u(&m,
226 " and section size is 0x%lx.",
227 localdbg->de_debug_ranges.dss_size);
228 _dwarf_error_string(dbg, error,
229 DW_DLE_DEBUG_RANGES_OFFSET_BAD,
230 dwarfstring_string(&m));
231 dwarfstring_destructor(&m);
232 return DW_DLV_ERROR;
233 }
234 if (rangesoffset >= localdbg->de_debug_ranges.dss_size) {
235 dwarfstring m;
236
237 dwarfstring_constructor(&m);
238 dwarfstring_append_printf_u(&m,
239 "DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
240 " ranges base+offset is 0x%lx ",
241 ranges_base+rangesoffset);
242 dwarfstring_append_printf_u(&m,
243 " and section size is 0x%lx.",
244 localdbg->de_debug_ranges.dss_size);
245 _dwarf_error_string(dbg, error,
246 DW_DLE_DEBUG_RANGES_OFFSET_BAD,
247 dwarfstring_string(&m));
248 dwarfstring_destructor(&m);
249 return DW_DLV_ERROR;
250 }
251
252 /* tied address_size must match the dwo address_size */
253 section_end = localdbg->de_debug_ranges.dss_data +
254 localdbg->de_debug_ranges.dss_size;
255 rangeptr = localdbg->de_debug_ranges.dss_data;
256 rangeptr += rangesoffset;
257 beginrangeptr = rangeptr;
258
259 for (;;) {
260 struct ranges_entry * re = 0;
261
262 if (rangeptr == section_end) {
263 break;
264 }
265 if (rangeptr > section_end) {
266 dwarfstring m;
267
268 free_allocated_ranges(base);
269 dwarfstring_constructor(&m);
270 dwarfstring_append(&m,
271 "DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
272 " ranges pointer ran off the end "
273 "of the section");
274 _dwarf_error_string(dbg, error,
275 DW_DLE_DEBUG_RANGES_OFFSET_BAD,
276 dwarfstring_string(&m));
277 dwarfstring_destructor(&m);
278 return DW_DLV_ERROR;
279 }
280 re = calloc(1, sizeof(struct ranges_entry));
281 if (!re) {
282 free_allocated_ranges(base);
283 _dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OUT_OF_MEM);
284 return DW_DLV_ERROR;
285 }
286 if ((rangeptr + (2*address_size)) > section_end) {
287 free(re);
288 free_allocated_ranges(base);
289 _dwarf_error_string(dbg, error,
290 DW_DLE_DEBUG_RANGES_OFFSET_BAD,
291 "DW_DLE_DEBUG_RANGES_OFFSET_BAD: "
292 " Not at the end of the ranges section "
293 " but there is not enough room in the section "
294 " for the next ranges entry");
295 return DW_DLV_ERROR;
296 }
297 entry_count++;
298 res = read_unaligned_addr_check(localdbg,&re->cur.dwr_addr1,
299 rangeptr, address_size,error,section_end);
300 if (res != DW_DLV_OK) {
301 free(re);
302 free_allocated_ranges(base);
303 return res;
304 }
305 rangeptr += address_size;
306 res = read_unaligned_addr_check(localdbg,&re->cur.dwr_addr2,
307 rangeptr, address_size,error,section_end);
308 if (res != DW_DLV_OK) {
309 free(re);
310 free_allocated_ranges(base);
311 return res;
312 }
313 rangeptr += address_size;
314 if (!base) {
315 base = re;
316 last = re;
317 } else {
318 last->next = re;
319 last = re;
320 }
321 if (re->cur.dwr_addr1 == 0 && re->cur.dwr_addr2 == 0) {
322 re->cur.dwr_type = DW_RANGES_END;
323 break;
324 } else if (re->cur.dwr_addr1 == MAX_ADDR) {
325 re->cur.dwr_type = DW_RANGES_ADDRESS_SELECTION;
326 } else {
327 re->cur.dwr_type = DW_RANGES_ENTRY;
328 }
329 }
330
331 /* We return ranges on dbg, so use that to allocate. */
332 ranges_data_out = (Dwarf_Ranges *)
333 _dwarf_get_alloc(dbg,DW_DLA_RANGES,entry_count);
334 if (!ranges_data_out) {
335 /* Error, apply to original, not local dbg. */
336 free_allocated_ranges(base);
337 _dwarf_error(dbg, error, DW_DLE_DEBUG_RANGES_OUT_OF_MEM);
338 return DW_DLV_ERROR;
339 }
340 curre = base;
341 *rangesbuf = ranges_data_out;
342 *listlen = entry_count;
343 for (copyindex = 0; curre && (copyindex < entry_count);
344 ++copyindex,++ranges_data_out,curre=curre->next) {
345 *ranges_data_out = curre->cur;
346 }
347 /* ASSERT: curre == NULL */
348 free_allocated_ranges(base);
349 base = 0;
350 /* Callers will often not care about the bytes used. */
351 if (actual_offset) {
352 *actual_offset = rangesoffset;
353 }
354 if (bytecount) {
355 *bytecount = rangeptr - beginrangeptr;
356 }
357 return DW_DLV_OK;
358 }
359
360 void
361 dwarf_dealloc_ranges(Dwarf_Debug dbg, Dwarf_Ranges * rangesbuf,
362 Dwarf_Signed rangecount)
363 {
364 (void)rangecount;
365 dwarf_dealloc(dbg,rangesbuf, DW_DLA_RANGES);
366 }
367
368 /* Also used to determine DIE base_address,
369 but that was wrong.
370 Only a CU_die DW_AT_low_pc can provide
371 a CU-wide base address and that is done when a CU is first
372 read, and available as cucontext->cc_base_address
373 and cc_base_address_present. */
374 static int
375 _dwarf_determine_die_range_offset(Dwarf_Debug dw_dbg,
376 Dwarf_Die dw_die,
377 Dwarf_Bool *have_die_ranges_offset,
378 Dwarf_Unsigned *die_ranges_offset,
379 Dwarf_Bool *have_die_base_addr,
380 Dwarf_Unsigned *die_base_addr,
381 Dwarf_Error *dw_error)
382 {
383 Dwarf_Bool hasatranges = FALSE;
384 Dwarf_Attribute attr = 0;
385 Dwarf_Unsigned rangeoffset_local = 0;
386 int res = 0;
387 Dwarf_CU_Context cucon = 0;
388
389 res = dwarf_hasattr(dw_die,DW_AT_ranges, &hasatranges,dw_error);
390 if (res != DW_DLV_OK) {
391 return res;
392 }
393 if (!hasatranges) {
394 /* Give up, this die not directly relevant. */
395 return res;
396 }
397 res = dwarf_attr(dw_die,DW_AT_ranges,&attr,dw_error);
398 if (res != DW_DLV_OK) {
399 if (res == DW_DLV_ERROR) {
400 dwarf_dealloc_error(dw_dbg,*dw_error);
401 *dw_error = 0;
402 }
403 return res;
404 }
405 res = dwarf_global_formref(attr,
406 &rangeoffset_local, dw_error);
407 if (res != DW_DLV_OK) {
408 if (res == DW_DLV_ERROR) {
409 dwarf_dealloc_attribute(attr);
410 dwarf_dealloc_error(dw_dbg,*dw_error);
411 *dw_error = 0;
412 return res;
413 }
414 }
415 cucon = dw_die->di_cu_context;
416 if (cucon->cc_base_address_present) {
417 *die_base_addr = cucon->cc_base_address;
418 *have_die_base_addr = TRUE;
419 }
420 /* rangeoffset_local was set . */
421 dwarf_dealloc_attribute(attr);
422 attr = 0;
423 *have_die_ranges_offset = TRUE;
424 *die_ranges_offset = rangeoffset_local;
425 return DW_DLV_OK;
426 }
427
428 /* Must not ever return DW_DLV_NO_ENTRY.
429 Uses cu_context sometimes, so the base address
430 is from the CU_DIE of the CU that
431 dw_die is a child of.
432 We attempt to cover what compilers actually do
433 in the GNU dwarf4 extensions, but rules are not
434 fully documented and it is difficult to be
435 sure what is fully correct. */
436 int
437 dwarf_get_ranges_baseaddress(Dwarf_Debug dw_dbg,
438 Dwarf_Die dw_die,
439 Dwarf_Bool *dw_known_base,
440 Dwarf_Unsigned *dw_baseaddress,
441 Dwarf_Bool *dw_at_ranges_offset_present,
442 Dwarf_Unsigned *dw_at_ranges_offset,
443 Dwarf_Error *dw_error)
444 {
445 Dwarf_CU_Context context = 0;
446 Dwarf_Unsigned local_ranges_offset = 0;
447 Dwarf_Bool local_ranges_offset_present = FALSE;
448 Dwarf_Bool have_die_ranges_offset = FALSE;
449 Dwarf_Unsigned die_ranges_offset = 0;
450 Dwarf_Bool have_die_base_addr = FALSE;
451 Dwarf_Unsigned die_base_addr = 0;
452 int res = 0;
453
454 CHECK_DBG(dw_dbg,dw_error,"dwarf_get_ranges_baseaddress()");
455 if (!dw_die) {
456 if (dw_known_base) {
457 *dw_known_base = FALSE;
458 *dw_at_ranges_offset_present = FALSE;
459 }
460 if (dw_baseaddress) {
461 *dw_baseaddress = 0;
462 *dw_at_ranges_offset = 0;
463 }
464 return DW_DLV_OK;
465 }
466 /* If the DIE passed in has a DW_AT_ranges attribute
467 we will use that DIE ranges offset.
468 Otherwise we use the DW_AT_ranges from the
469 CU DIE (if any)
470 If the DIE passed in has a DW_AT_low_pc
471 attribute we will use that as the ranges
472 base address, otherwise we use the
473 cu context base adddress (if present) ...
474 which may be incorrect... ? */
475 res = _dwarf_determine_die_range_offset(dw_dbg,
476 dw_die,&have_die_ranges_offset,&die_ranges_offset,
477 &have_die_base_addr,&die_base_addr,dw_error);
478 if (res != DW_DLV_OK ) {
479 if (res == DW_DLV_ERROR) {
480 /* Suppressing knowledge of any error */
481 dwarf_dealloc_error(dw_dbg,*dw_error);
482 *dw_error = 0;
483 }
484 }
485
486 context = dw_die->di_cu_context;
487 if (!context) {
488 _dwarf_error_string(dw_dbg, dw_error,
489 DW_DLE_DIE_NO_CU_CONTEXT,
490 "DW_DLE_DIE_NO_CU_CONTEXT: in a call to "
491 "dwarf_get_ranges_baseaddress");
492 return DW_DLV_ERROR;
493 }
494 if (have_die_ranges_offset) {
495 local_ranges_offset_present = have_die_ranges_offset;
496 local_ranges_offset = die_ranges_offset;
497 } else {
498 local_ranges_offset_present =
499 context->cc_at_ranges_offset_present;
500 local_ranges_offset =
501 context->cc_at_ranges_offset;
502 }
503 if (dw_at_ranges_offset) {
504 *dw_at_ranges_offset = local_ranges_offset;
505 }
506 if (dw_at_ranges_offset_present) {
507 *dw_at_ranges_offset_present = local_ranges_offset_present;
508 }
509 if (context->cc_base_address_present) {
510 *dw_baseaddress = context->cc_base_address;
511 *dw_known_base = context->cc_base_address_present;
512 }
513 return DW_DLV_OK;
514 }