Debian Codegraph @

src/lib/libdwarf/dwarf_arange.c

raw
1 /*
2 Copyright (C) 2000-2004 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2020 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. All rights reserved.
5
6 This program is free software; you can redistribute it
7 and/or modify it under the terms of version 2.1 of the
8 GNU Lesser General Public License as published by the Free
9 Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU Lesser General
25 Public License along with this program; if not, write the
26 Free Software Foundation, Inc., 51 Franklin Street - Fifth
27 Floor, Boston MA 02110-1301, USA.
28
29 */
30
31 #include <config.h>
32
33 #include <stddef.h> /* NULL size_t */
34 #include <stdio.h> /* debug printf */
35
36 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
37 #include "stdafx.h"
38 #endif /* HAVE_STDAFX_H */
39
40 #include "dwarf.h"
41 #include "libdwarf.h"
42 #include "libdwarf_private.h"
43 #include "dwarf_base_types.h"
44 #include "dwarf_opaque.h"
45 #include "dwarf_alloc.h"
46 #include "dwarf_error.h"
47 #include "dwarf_util.h"
48 #include "dwarf_arange.h"
49 #include "dwarf_global.h" /* for _dwarf_fixup_* */
50 #include "dwarf_string.h"
51
52 static void
53 free_aranges_chain(Dwarf_Debug dbg, Dwarf_Chain head)
54 {
55 Dwarf_Chain cur = head;
56 Dwarf_Chain next = 0;
57
58 if (!head) {
59 return;
60 }
61 next = head->ch_next;
62 for ( ;cur; cur = next) {
63 void *item = cur->ch_item;
64 int type = cur->ch_itemtype;
65
66 next = cur->ch_next;
67 if (item && type) {
68 dwarf_dealloc(dbg,item,type);
69 cur->ch_item = 0;
70 dwarf_dealloc(dbg,cur,DW_DLA_CHAIN);
71 }
72 }
73 }
74
75 /* Common code for two user-visible routines to share.
76 Errors here result in memory leaks, but errors here
77 are serious (making aranges unusable) so we assume
78 callers will not repeat the error often or mind the leaks.
79 */
80 static int
81 _dwarf_get_aranges_list(Dwarf_Debug dbg,
82 Dwarf_Chain * chain_out,
83 Dwarf_Signed * chain_count_out,
84 Dwarf_Error * error)
85 {
86 /* Sweeps through the arange. */
87 Dwarf_Small *arange_ptr = 0;
88 Dwarf_Small *arange_ptr_start = 0;
89
90 /* Start of arange header.
91 Used for rounding offset of arange_ptr
92 to twice the tuple size. Libdwarf requirement. */
93 Dwarf_Small *header_ptr = 0;
94
95 /* Version of .debug_aranges header. */
96 Dwarf_Unsigned version = 0;
97
98 /* Offset of current set of aranges into .debug_info. */
99 Dwarf_Off info_offset = 0;
100 /* Size in bytes of addresses in target. */
101 Dwarf_Small address_size = 0;
102 /* Size in bytes of segment offsets in target. */
103 Dwarf_Small segment_sel_size = 0;
104 /* Count of total number of aranges. */
105 Dwarf_Signed arange_count = 0;
106 Dwarf_Arange arange = 0;
107 Dwarf_Unsigned section_size = 0;
108 Dwarf_Byte_Ptr arange_end_section = 0;
109 /* Used to chain Dwarf_Aranges structs. */
110 Dwarf_Chain curr_chain = NULL;
111 Dwarf_Chain head_chain = NULL;
112 Dwarf_Chain *plast = &head_chain;
113
114 if (!dbg->de_debug_aranges.dss_size) {
115 return DW_DLV_NO_ENTRY;
116 }
117 arange_ptr = dbg->de_debug_aranges.dss_data;
118 arange_ptr_start = arange_ptr;
119 section_size = dbg->de_debug_aranges.dss_size;
120 arange_end_section = arange_ptr + section_size;
121
122 do {
123 /* Length of current set of aranges.
124 This is local length, which begins just
125 after the length field itself. */
126 Dwarf_Unsigned area_length = 0;
127 Dwarf_Unsigned remainder = 0;
128 Dwarf_Unsigned range_entry_size = 0;
129 int local_length_size;
130 int local_extension_size = 0;
131 Dwarf_Small *end_this_arange = 0;
132 int res = 0;
133
134 header_ptr = arange_ptr;
135 if (header_ptr >= arange_end_section) {
136 free_aranges_chain(dbg,head_chain);
137 _dwarf_error(dbg, error,DW_DLE_ARANGES_HEADER_ERROR);
138 return DW_DLV_ERROR;
139 }
140 res = _dwarf_read_area_length_ck_wrapper(dbg,&area_length,
141 &arange_ptr,&local_length_size,&local_extension_size,
142 section_size,arange_end_section,error);
143 if (res != DW_DLV_OK) {
144 free_aranges_chain(dbg,head_chain);
145 return res;
146 }
147 /* arange_ptr has been incremented appropriately past
148 the length field by READ_AREA_LENGTH. */
149
150 if (area_length > dbg->de_debug_aranges.dss_size ||
151 (area_length +local_length_size+local_extension_size)
152 > dbg->de_debug_aranges.dss_size ) {
153 free_aranges_chain(dbg,head_chain);
154 _dwarf_error(dbg, error,DW_DLE_ARANGES_HEADER_ERROR);
155 return DW_DLV_ERROR;
156 }
157 if ((area_length + local_length_size + local_extension_size) >
158 dbg->de_debug_aranges.dss_size) {
159 free_aranges_chain(dbg,head_chain);
160 _dwarf_error(dbg, error, DW_DLE_ARANGES_HEADER_ERROR);
161 return DW_DLV_ERROR;
162 }
163
164 end_this_arange = arange_ptr + area_length;
165 if (end_this_arange > arange_end_section) {
166 free_aranges_chain(dbg,head_chain);
167 _dwarf_error(dbg, error,DW_DLE_ARANGES_HEADER_ERROR);
168 return DW_DLV_ERROR;
169 }
170 if (!area_length) {
171 /* We read 4 bytes of zero, so area-length zero.
172 Keep scanning. First seen Nov 27, 2018
173 in GNU-cc in windows dll. */
174 continue;
175 }
176
177 res = _dwarf_read_unaligned_ck_wrapper(dbg,&version,
178 arange_ptr,DWARF_HALF_SIZE,end_this_arange,error);
179 if (res != DW_DLV_OK) {
180 free_aranges_chain(dbg,head_chain);
181 return res;
182 }
183 arange_ptr += DWARF_HALF_SIZE;
184 if (arange_ptr >= end_this_arange) {
185 free_aranges_chain(dbg,head_chain);
186 _dwarf_error(dbg, error, DW_DLE_ARANGES_HEADER_ERROR);
187 return DW_DLV_ERROR;
188 }
189 if (version != DW_ARANGES_VERSION2) {
190 free_aranges_chain(dbg,head_chain);
191 _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
192 return DW_DLV_ERROR;
193 }
194 res = _dwarf_read_unaligned_ck_wrapper(dbg,&info_offset,
195 arange_ptr,local_length_size,end_this_arange,error);
196 if (res != DW_DLV_OK) {
197 free_aranges_chain(dbg,head_chain);
198 return res;
199 }
200
201 arange_ptr += local_length_size;
202 if (arange_ptr >= end_this_arange) {
203 free_aranges_chain(dbg,head_chain);
204 _dwarf_error(dbg, error, DW_DLE_ARANGES_HEADER_ERROR);
205 return DW_DLV_ERROR;
206 }
207 /* This applies to debug_info only, not to debug_types. */
208 if (info_offset >= dbg->de_debug_info.dss_size) {
209 FIX_UP_OFFSET_IRIX_BUG(dbg, info_offset,
210 "arange info offset.a");
211 if (info_offset >= dbg->de_debug_info.dss_size) {
212 free_aranges_chain(dbg,head_chain);
213 _dwarf_error(dbg, error, DW_DLE_ARANGE_OFFSET_BAD);
214 return DW_DLV_ERROR;
215 }
216 }
217
218 address_size = *(Dwarf_Small *) arange_ptr;
219 if (address_size > sizeof(Dwarf_Addr)) {
220 free_aranges_chain(dbg,head_chain);
221 _dwarf_error(dbg, error, DW_DLE_ADDRESS_SIZE_ERROR);
222 return DW_DLV_ERROR;
223 }
224 if (address_size == 0) {
225 free_aranges_chain(dbg,head_chain);
226 _dwarf_error(dbg, error, DW_DLE_ADDRESS_SIZE_ZERO);
227 return DW_DLV_ERROR;
228 }
229 /* It is not an error if the sizes differ.
230 Unusual, but not an error. */
231 arange_ptr = arange_ptr + sizeof(Dwarf_Small);
232
233 /* The following deref means we better
234 check the pointer for off-end. */
235 if (arange_ptr >= end_this_arange) {
236 free_aranges_chain(dbg,head_chain);
237 _dwarf_error(dbg, error, DW_DLE_ARANGE_OFFSET_BAD);
238 return DW_DLV_ERROR;
239 }
240
241 /* Even DWARF2 had a segment_sel_size field here,
242 meaning
243 size in bytes of a segment selector/descriptor
244 on the target system.
245 In reality it is unlikely any non-zero
246 value will work sensibly for the user. */
247 segment_sel_size = *(Dwarf_Small *) arange_ptr;
248 if (segment_sel_size > 0) {
249 free_aranges_chain(dbg,head_chain);
250 _dwarf_error_string(dbg, error,
251 DW_DLE_SEGMENT_SIZE_BAD,
252 "DW_DLE_SEGMENT_SIZE_BAD: "
253 "segment selector size > 0 is not supported");
254 return DW_DLV_ERROR;
255 }
256 arange_ptr = arange_ptr + sizeof(Dwarf_Small);
257 /* Code below will check for == end_this_arange
258 as appropriate. */
259 if (arange_ptr > end_this_arange) {
260 free_aranges_chain(dbg,head_chain);
261 _dwarf_error(dbg, error, DW_DLE_ARANGE_OFFSET_BAD);
262 return DW_DLV_ERROR;
263 }
264 range_entry_size = 2*address_size + segment_sel_size;
265 /* Round arange_ptr offset to next multiple of
266 address_size. */
267 remainder = (Dwarf_Unsigned) ((arange_ptr - header_ptr) %
268 (range_entry_size));
269 if (remainder != 0) {
270 arange_ptr = arange_ptr + (2 * address_size) - remainder;
271 }
272
273 do {
274 Dwarf_Addr range_address = 0;
275 Dwarf_Unsigned segment_selector = 0;
276 Dwarf_Unsigned range_length = 0;
277 /* For segmented address spaces, the first field to
278 read is a segment selector (new in DWARF4).
279 The version number DID NOT CHANGE from 2, which
280 is quite surprising.
281 Also surprising since the segment_sel_size
282 was always there
283 in the table header! */
284 /* We want to test cu_version here
285 and segment_sel_size, but
286 currently with no way segment_sel_size
287 can be other than zero.
288 We just hope no one using
289 segment_selectors, really. FIXME */
290 res = _dwarf_read_unaligned_ck_wrapper(dbg,&range_address,
291 arange_ptr,address_size,end_this_arange,error);
292 if (res != DW_DLV_OK) {
293 free_aranges_chain(dbg,head_chain);
294 return res;
295 }
296 arange_ptr += address_size;
297
298 res = _dwarf_read_unaligned_ck_wrapper(dbg,&range_length,
299 arange_ptr,address_size,end_this_arange,error);
300 if (res != DW_DLV_OK) {
301 free_aranges_chain(dbg,head_chain);
302 return res;
303 }
304
305 arange_ptr += address_size;
306
307 {
308 /* We used to suppress all-zero entries, but
309 now we return all aranges entries so we show
310 the entire content. March 31, 2010. */
311
312 arange = (Dwarf_Arange)
313 _dwarf_get_alloc(dbg, DW_DLA_ARANGE, 1);
314 if (arange == NULL) {
315 free_aranges_chain(dbg,head_chain);
316 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
317 return DW_DLV_ERROR;
318 }
319
320 arange->ar_segment_selector = segment_selector;
321 arange->ar_segment_selector_size =
322 segment_sel_size;
323 arange->ar_address = range_address;
324 arange->ar_length = range_length;
325 arange->ar_info_offset = info_offset;
326 arange->ar_dbg = dbg;
327 arange_count++;
328
329 curr_chain = (Dwarf_Chain)
330 _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
331 if (curr_chain == NULL) {
332 dwarf_dealloc(dbg,arange,DW_DLA_ARANGE);
333 free_aranges_chain(dbg,head_chain);
334 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
335 return DW_DLV_ERROR;
336 }
337
338 curr_chain->ch_item = arange;
339 curr_chain->ch_itemtype = DW_DLA_ARANGE;
340 (*plast) = curr_chain;
341 plast = &(curr_chain->ch_next);
342 }
343 /* The current set of ranges is terminated by
344 range_address 0 and range_length 0, but that
345 does not necessarily terminate the ranges for this CU!
346 There can be multiple sets in that DWARF
347 does not explicitly forbid multiple sets.
348 DWARF2,3,4 section 7.20
349 We stop short to avoid overrun of the
350 end of the CU. */
351 } while (end_this_arange >= (arange_ptr + range_entry_size));
352
353 /* A compiler could emit some padding bytes here. dwarf2/3
354 (dwarf4 sec 7.20) does not clearly make extra padding
355 bytes illegal. */
356 if (end_this_arange < arange_ptr) {
357 Dwarf_Unsigned pad_count = arange_ptr - end_this_arange;
358 Dwarf_Unsigned offset = arange_ptr - arange_ptr_start;
359 dwarfstring aramsg;
360
361 dwarfstring_constructor(&aramsg);
362 /* Safe. Length strictly limited. */
363 dwarfstring_append_printf_u(&aramsg,
364 "DW_DLE_ARANGE_LENGTH_BAD."
365 " 0x%" DW_PR_XZEROS DW_PR_DUx,
366 pad_count);
367 dwarfstring_append_printf_u(&aramsg,
368 " pad bytes at offset 0x%" DW_PR_XZEROS DW_PR_DUx
369 " in .debug_aranges",
370 offset);
371 dwarf_insert_harmless_error(dbg,
372 dwarfstring_string(&aramsg));
373 dwarfstring_destructor(&aramsg);
374 }
375 /* For most compilers, arange_ptr == end_this_arange at
376 this point. But not if there were padding bytes */
377 arange_ptr = end_this_arange;
378 } while (arange_ptr < arange_end_section);
379
380 if (arange_ptr != arange_end_section) {
381 free_aranges_chain(dbg,head_chain);
382 _dwarf_error(dbg, error, DW_DLE_ARANGE_DECODE_ERROR);
383 return DW_DLV_ERROR;
384 }
385 *chain_out = head_chain;
386 *chain_count_out = arange_count;
387 return DW_DLV_OK;
388 }
389
390 /*
391 This function returns the count of the number of
392 aranges in the .debug_aranges section. It sets
393 aranges to point to a block of Dwarf_Arange's
394 describing the arange's. It returns DW_DLV_ERROR
395 on error.
396
397 Must be identical in most aspects to
398 dwarf_get_aranges_addr_offsets!
399
400 */
401 int
402 dwarf_get_aranges(Dwarf_Debug dbg,
403 Dwarf_Arange ** aranges,
404 Dwarf_Signed * returned_count, Dwarf_Error * error)
405 {
406 /* Count of total number of aranges. */
407 Dwarf_Signed arange_count = 0;
408
409 Dwarf_Arange *arange_block = 0;
410
411 /* Used to chain Dwarf_Aranges structs. */
412 Dwarf_Chain curr_chain = NULL;
413 Dwarf_Chain head_chain = NULL;
414 Dwarf_Signed i = 0;
415 int res = DW_DLV_ERROR;
416
417 /* ***** BEGIN CODE ***** */
418
419 CHECK_DBG(dbg,error,"dwarf_get_aranges()");
420 res = _dwarf_load_section(dbg, &dbg->de_debug_aranges, error);
421 if (res != DW_DLV_OK) {
422 return res;
423 }
424 /* aranges points in to info, so if info needs expanding
425 we have to load it. */
426 res = _dwarf_load_debug_info(dbg, error);
427 if (res != DW_DLV_OK) {
428 return res;
429 }
430
431 res = _dwarf_get_aranges_list(dbg,&head_chain,
432 &arange_count,error);
433 if (res != DW_DLV_OK) {
434 free_aranges_chain(dbg,head_chain);
435 return res;
436 }
437
438 arange_block = (Dwarf_Arange *)
439 _dwarf_get_alloc(dbg, DW_DLA_LIST, arange_count);
440 if (arange_block == NULL) {
441 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
442 free_aranges_chain(dbg,head_chain);
443 return DW_DLV_ERROR;
444 }
445
446 /* See also free_aranges_chain() above */
447 curr_chain = head_chain;
448 for (i = 0; i < arange_count; i++) {
449 Dwarf_Chain prev = 0;
450
451 /* Copies pointers. No dealloc of ch_item, */
452 *(arange_block + i) = curr_chain->ch_item;
453 curr_chain->ch_item = 0;
454 prev = curr_chain;
455 curr_chain = curr_chain->ch_next;
456 dwarf_dealloc(dbg, prev, DW_DLA_CHAIN);
457 }
458 *aranges = arange_block;
459 *returned_count = (arange_count);
460 return DW_DLV_OK;
461 }
462
463 #if 0 /* _dwarf_get_aranges_addr_offsets Unused */
464 /*
465 This function returns DW_DLV_OK if it succeeds
466 and DW_DLV_ERR or DW_DLV_OK otherwise.
467 count is set to the number of addresses in the
468 .debug_aranges section.
469 For each address, the corresponding element in
470 an array is set to the address itself(aranges) and
471 the section offset (offsets).
472 Must be identical in most aspects to
473 dwarf_get_aranges!
474 */
475 int
476 _dwarf_get_aranges_addr_offsets(Dwarf_Debug dbg,
477 Dwarf_Addr ** addrs,
478 Dwarf_Off ** offsets,
479 Dwarf_Signed * count,
480 Dwarf_Error * error)
481 {
482 Dwarf_Signed i = 0;
483
484 /* Used to chain Dwarf_Aranges structs. */
485 Dwarf_Chain curr_chain = NULL;
486 Dwarf_Chain head_chain = NULL;
487
488 Dwarf_Signed arange_count = 0;
489 Dwarf_Addr *arange_addrs = 0;
490 Dwarf_Off *arange_offsets = 0;
491
492 int res = DW_DLV_ERROR;
493
494 /* ***** BEGIN CODE ***** */
495
496 if (error != NULL)
497 *error = NULL;
498
499 if (IS_INVALID_DBG(dbg)) {
500 _dwarf_error(NULL, error, DW_DLE_DBG_NULL);
501 return DW_DLV_ERROR;
502 }
503
504 res = _dwarf_load_section(dbg, &dbg->de_debug_aranges,error);
505 if (res != DW_DLV_OK) {
506 return res;
507 }
508 /* aranges points in to info, so if info needs expanding
509 we have to load it. */
510 res = _dwarf_load_debug_info(dbg, error);
511 if (res != DW_DLV_OK) {
512 return res;
513 }
514 res = _dwarf_get_aranges_list(dbg,&head_chain,
515 &arange_count,error);
516 if (res != DW_DLV_OK) {
517 return res;
518 }
519 arange_addrs = (Dwarf_Addr *)
520 _dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
521 if (arange_addrs == NULL) {
522 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
523 return DW_DLV_ERROR;
524 }
525 arange_offsets = (Dwarf_Off *)
526 _dwarf_get_alloc(dbg, DW_DLA_ADDR, arange_count);
527 if (arange_offsets == NULL) {
528 free_aranges_chain(dbg,head_chain);
529 dwarf_dealloc(dbg,arange_addrs,DW_DLA_ADDR);
530 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
531 return DW_DLV_ERROR;
532 }
533
534 curr_chain = head_chain;
535 for (i = 0; i < arange_count; i++) {
536 Dwarf_Arange ar = curr_chain->ch_item;
537 int itemtype = curr_chain->ch_itemtype;
538 Dwarf_Chain prev = 0;
539
540 if (!ar) {
541 arange_addrs[i] = 0;
542 arange_offsets[i] = 0;
543 continue;
544 }
545 curr_chain->ch_item = 0;
546 arange_addrs[i] = ar->ar_address;
547 arange_offsets[i] = ar->ar_info_offset;
548 prev = curr_chain;
549 curr_chain = curr_chain->ch_next;
550 if (itemtype) {
551 dwarf_dealloc(dbg, ar, itemtype);
552 }
553 dwarf_dealloc(dbg, prev, DW_DLA_CHAIN);
554 }
555 *count = arange_count;
556 *offsets = arange_offsets;
557 *addrs = arange_addrs;
558 return DW_DLV_OK;
559 }
560 #endif /* 0 */
561
562 /*
563 This function takes a pointer to a block
564 of Dwarf_Arange's, and a count of the
565 length of the block. It checks if the
566 given address is within the range of an
567 address range in the block. If yes, it
568 returns the appropriate Dwarf_Arange.
569 If no, it returns DW_DLV_NO_ENTRY;
570 On error it returns DW_DLV_ERROR.
571 */
572 int
573 dwarf_get_arange(Dwarf_Arange * aranges,
574 Dwarf_Unsigned arange_count,
575 Dwarf_Addr address,
576 Dwarf_Arange * returned_arange, Dwarf_Error * error)
577 {
578 Dwarf_Arange curr_arange = 0;
579 Dwarf_Unsigned i = 0;
580
581 if (!aranges) {
582 _dwarf_error(NULL, error, DW_DLE_ARANGES_NULL);
583 return DW_DLV_ERROR;
584 }
585 for (i = 0; i < arange_count; i++) {
586 curr_arange = *(aranges + i);
587 if (address >= curr_arange->ar_address &&
588 address <
589 curr_arange->ar_address + curr_arange->ar_length) {
590 *returned_arange = curr_arange;
591 return DW_DLV_OK;
592 }
593 }
594 return DW_DLV_NO_ENTRY;
595 }
596
597 /*
598 This function takes an Dwarf_Arange,
599 and returns the offset of the first
600 die in the compilation-unit that the
601 arange belongs to. Returns DW_DLV_ERROR
602 on error.
603
604 For an arange, the cu_die can only be from debug_info,
605 not debug_types, it seems.
606 */
607 int
608 dwarf_get_cu_die_offset(Dwarf_Arange arange,
609 Dwarf_Off * returned_offset,
610 Dwarf_Error * error)
611 {
612 Dwarf_Debug dbg = 0;
613 Dwarf_Off offset = 0;
614 Dwarf_Unsigned headerlen = 0;
615 int cres = 0;
616
617 if (arange == NULL) {
618 _dwarf_error(NULL, error, DW_DLE_ARANGE_NULL);
619 return DW_DLV_ERROR;
620 }
621 dbg = arange->ar_dbg;
622 offset = arange->ar_info_offset;
623 /* This applies to debug_info only, not to debug_types. */
624 if (!dbg->de_debug_info.dss_data) {
625 int res = _dwarf_load_debug_info(dbg, error);
626
627 if (res != DW_DLV_OK) {
628 return res;
629 }
630 }
631 cres = _dwarf_length_of_cu_header(dbg, offset,
632 TRUE, &headerlen,error);
633 if (cres != DW_DLV_OK) {
634 return cres;
635 }
636 *returned_offset = headerlen + offset;
637 return DW_DLV_OK;
638 }
639
640 /* This function takes an Dwarf_Arange,
641 and returns the offset of the CU header
642 in the compilation-unit that the
643 arange belongs to. Returns DW_DLV_ERROR
644 on error.
645 Ensures .debug_info loaded so
646 the cu_offset is meaningful. */
647 int
648 dwarf_get_arange_cu_header_offset(Dwarf_Arange arange,
649 Dwarf_Off * cu_header_offset_returned,
650 Dwarf_Error * error)
651 {
652 Dwarf_Debug dbg = 0;
653 if (arange == NULL) {
654 _dwarf_error(NULL, error, DW_DLE_ARANGE_NULL);
655 return DW_DLV_ERROR;
656 }
657 dbg = arange->ar_dbg;
658 /* This applies to debug_info only, not to debug_types. */
659 /* Like dwarf_get_arange_info_b() this ensures debug_info loaded:
660 the cu_header is in debug_info and will be used else
661 we would not call dwarf_get_arange_cu_header_offset. */
662 if (!dbg->de_debug_info.dss_data) {
663 int res = _dwarf_load_debug_info(dbg, error);
664 if (res != DW_DLV_OK) {
665 return res;
666 }
667 }
668 *cu_header_offset_returned = arange->ar_info_offset;
669 return DW_DLV_OK;
670 }
671
672 /*
673 This function takes a Dwarf_Arange, and returns
674 TRUE if it is not NULL. It also stores the start
675 address of the range in *start, the length of the
676 range in *length, and the offset of the first die
677 in the compilation-unit in *cu_die_offset. It
678 returns FALSE on error.
679 If cu_die_offset returned ensures .debug_info loaded so
680 the cu_die_offset is meaningful.
681
682 New for DWARF4, entries may have segment information.
683 *segment is only meaningful
684 if *segment_entry_size is non-zero.
685 But segment_selectors are not fully defined so
686 a non-zero segment_entry_size is not actually
687 usable. */
688 int
689 dwarf_get_arange_info_b(Dwarf_Arange arange,
690 Dwarf_Unsigned* segment,
691 Dwarf_Unsigned* segment_entry_size,
692 Dwarf_Addr * start,
693 Dwarf_Unsigned* length,
694 Dwarf_Off * cu_die_offset,
695 Dwarf_Error * error)
696 {
697 if (arange == NULL) {
698 _dwarf_error(NULL, error, DW_DLE_ARANGE_NULL);
699 return DW_DLV_ERROR;
700 }
701
702 if (segment != NULL) {
703 *segment = arange->ar_segment_selector;
704 }
705 if (segment_entry_size != NULL) {
706 *segment_entry_size = arange->ar_segment_selector_size;
707 }
708 if (start != NULL)
709 *start = arange->ar_address;
710 if (length != NULL)
711 *length = arange->ar_length;
712 if (cu_die_offset != NULL) {
713 Dwarf_Debug dbg = arange->ar_dbg;
714 Dwarf_Off offset = arange->ar_info_offset;
715 Dwarf_Unsigned headerlen = 0;
716 int cres = 0;
717
718 /* This applies to debug_info only, not to debug_types. */
719 if (!dbg->de_debug_info.dss_data) {
720 int res = _dwarf_load_debug_info(dbg, error);
721 if (res != DW_DLV_OK) {
722 return res;
723 }
724 }
725 cres = _dwarf_length_of_cu_header(dbg, offset,
726 TRUE, &headerlen,error);
727 if (cres != DW_DLV_OK) {
728 return cres;
729 }
730 *cu_die_offset = offset + headerlen;
731
732 }
733 return DW_DLV_OK;
734 }