Debian Codegraph @

src/lib/libdwarf/dwarf_rnglists.c

raw
1 /*
2 Copyright (c) 2020, David Anderson
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with
6 or without modification, are permitted provided that the
7 following conditions are met:
8
9 Redistributions of source code must retain the above
10 copyright notice, this list of conditions and the following
11 disclaimer.
12
13 Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials
16 provided with the distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <config.h>
34
35 #include <stdlib.h> /* free() malloc() */
36 #include <stdio.h> /* printf */
37 #include <string.h> /* memset() */
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_string.h"
52 #include "dwarf_rnglists.h"
53
54 #undef DEBUG_RNGLIST
55
56 #define SIZEOFT8 1
57 #define SIZEOFT16 2
58 #define SIZEOFT32 4
59 #define SIZEOFT64 8
60
61 #ifdef DEBUG_RNGLIST /* dump_bytes */
62 static void
63 dump_bytes(const char *msg,Dwarf_Small * start, long len)
64 {
65 Dwarf_Small *end = start + len;
66 Dwarf_Small *cur = start;
67 printf("%s (0x%lx) ",msg,(unsigned long)start);
68 for (; cur < end; cur++) {
69 printf("%02x", *cur);
70 }
71 printf("\n");
72 fflush(stdout);
73 }
74 #endif /*0*/
75 #if 0 /* dump_rh */
76 static void
77 dump_rh(const char *msg,
78 int line,
79 struct Dwarf_Rnglists_Head_s *head)
80 {
81 printf("Rnglists_Head: %s line %d\n",
82 msg,line);
83 printf(" count of entries: %"
84 DW_PR_DUu
85 " (0x%" DW_PR_DUx ")\n",
86 head->rh_count,head->rh_count);
87 printf(" rh__offset_size : %p\n",
88 (void *) head->rh_offset_size);
89 printf(" rh_rnglists : %p\n",(void *) head->rh_rnglists);
90 printf(" rh_index : %lu\n",
91 (unsigned long)head->rh_index);
92 printf(" rh_first : %lu\n",
93 (unsigned long)head->rh_first);
94 printf(" rh_last : %lu\n",
95 (unsigned long)head->rh_last);
96 printf(" rh_bytes total : %lu (0x%lx)\n",
97 (unsigned long)head->rh_bytes_total,
98 (unsigned long)head->rh_bytes_total);
99 printf(" CU Context : %p",(void *)head->rh_context);
100 printf(" Rnglists Context: %p",(void *)head->rh_localcontext);
101 }
102 #endif
103 #if 0 /* debug print struct */
104 static void
105 dump_rc(const char *msg,
106 int line,
107 Dwarf_Rnglists_Context rc)
108 {
109 printf("Rnglists_Context %s line %d index %lu\n", msg,
110 line,
111 (unsigned long)rc->rc_index);
112 printf(" rc_header_offset 0x%lx\n",
113 (unsigned long)rc->rc_header_offset);
114 printf(" rc_length 0x%lx\n",
115 (unsigned long)rc->rc_length);
116 printf(" rc_offset_size 0x%lu\n",
117 (unsigned long)rc->rc_offset_size);
118 printf(" rc_version 0x%lu\n",
119 (unsigned long)rc->rc_version);
120 printf(" rc_address_size 0x%lu\n",
121 (unsigned long)rc->rc_address_size);
122 printf(" rc_address_size 0x%lu\n",
123 (unsigned long)rc->rc_address_size);
124 printf(" rc_offset_entry_count 0x%lu\n",
125 (unsigned long)rc->rc_offset_entry_count);
126 printf(" rc_offsets_off_in_sect 0x%lu\n",
127 (unsigned long)rc->rc_offsets_off_in_sect);
128 printf(" rc_first_rnglist_offset 0x%lx\n",
129 (unsigned long)rc->rc_first_rnglist_offset);
130 printf(" rc_past_last_rnglist_offset 0x%lx\n",
131 (unsigned long)rc->rc_past_last_rnglist_offset);
132 }
133 #endif
134
135 static void
136 free_rnglists_context(Dwarf_Rnglists_Context cx)
137 {
138 if (cx) {
139 free(cx->rc_offset_value_array);
140 cx->rc_offset_value_array = 0;
141 cx->rc_magic = 0;
142 free(cx);
143 }
144 }
145
146 /* Used in case of error reading the
147 rnglists headers (not referring to Dwarf_Rnglists_Head
148 here), to clean up. */
149 static void
150 free_rnglists_chain(Dwarf_Debug dbg, Dwarf_Chain head)
151 {
152 Dwarf_Chain cur = head;
153 Dwarf_Chain next = 0;
154
155 if (!head || IS_INVALID_DBG(dbg)) {
156 return;
157 }
158 for ( ;cur; cur = next) {
159 next = cur->ch_next;
160 if (cur->ch_item) {
161 /* ch_item is Dwarf_Rnglists_Context */
162 Dwarf_Rnglists_Context cx =
163 (Dwarf_Rnglists_Context)cur->ch_item;
164 free_rnglists_context(cx);
165 cur->ch_item = 0;
166 dwarf_dealloc(dbg,cur,DW_DLA_CHAIN);
167 }
168 }
169 }
170 /* See also read_single_lle_entry() for similar code */
171 static int
172 read_single_rle_entry(Dwarf_Debug dbg,
173 Dwarf_Small *data,
174 Dwarf_Unsigned dataoffset,
175 Dwarf_Small *enddata,
176 unsigned address_size,
177 unsigned *bytes_count_out,
178 unsigned *entry_kind,
179 Dwarf_Unsigned *entry_operand1,
180 Dwarf_Unsigned *entry_operand2,
181 Dwarf_Error* error)
182 {
183 Dwarf_Unsigned count = 0;
184 Dwarf_Unsigned leblen = 0;
185 unsigned code = 0;
186 Dwarf_Unsigned val1 = 0;
187 Dwarf_Unsigned val2 = 0;
188 Dwarf_Small * startdata = 0;
189
190 if (data >= enddata) {
191 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
192 "DW_DLE_RNGLISTS_ERROR: "
193 "An rle entry begins past the end of "
194 "its allowed space. Corrupt DWARF.");
195 return DW_DLV_ERROR;
196 }
197 startdata = data;
198 code = *data;
199 ++data;
200 ++count;
201 switch(code) {
202 case DW_RLE_end_of_list: break;
203 case DW_RLE_base_addressx:{
204 DECODE_LEB128_UWORD_LEN_CK(data,val1,leblen,
205 dbg,error,enddata);
206 count += leblen;
207 }
208 break;
209 case DW_RLE_startx_endx:
210 case DW_RLE_startx_length:
211 case DW_RLE_offset_pair: {
212 DECODE_LEB128_UWORD_LEN_CK(data,val1,leblen,
213 dbg,error,enddata);
214 count += leblen;
215 DECODE_LEB128_UWORD_LEN_CK(data,val2,leblen,
216 dbg,error,enddata);
217 count += leblen;
218 }
219 break;
220 case DW_RLE_base_address: {
221 READ_UNALIGNED_CK(dbg,val1, Dwarf_Unsigned,
222 data,address_size,error,enddata);
223 data += address_size;
224 count += address_size;
225 }
226 break;
227 case DW_RLE_start_end: {
228 READ_UNALIGNED_CK(dbg,val1, Dwarf_Unsigned,
229 data,address_size,error,enddata);
230 data += address_size;
231 count += address_size;
232 READ_UNALIGNED_CK(dbg,val2, Dwarf_Unsigned,
233 data,address_size,error,enddata);
234 data += address_size;
235 count += address_size;
236 }
237 break;
238 case DW_RLE_start_length: {
239 READ_UNALIGNED_CK(dbg,val1, Dwarf_Unsigned,
240 data,address_size,error,enddata);
241 data += address_size;
242 count += address_size;
243 DECODE_LEB128_UWORD_LEN_CK(data,val2,leblen,
244 dbg,error,enddata);
245 count += leblen;
246 }
247 break;
248 default: {
249 dwarfstring m;
250
251 dwarfstring_constructor(&m);
252 dwarfstring_append_printf_u(&m,
253 "DW_DLE_RNGLISTS_ERROR: "
254 "The rangelists entry at .debug_rnglists[.dwo]"
255 " offset 0x%x" ,dataoffset);
256 dwarfstring_append_printf_u(&m,
257 " has code 0x%x which is unknown",code);
258 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
259 dwarfstring_string(&m));
260 dwarfstring_destructor(&m);
261 return DW_DLV_ERROR;
262 }
263 break;
264 }
265 {
266 /* We want to avoid overflow in additions, and
267 the overall section size is a reasonable check
268 on count. The sequence of tests is to
269 preserve a testing baseline:
270 baselines/hongg2024-02-18-m.base
271 otherwise we would test against sectionsize first.*/
272 Dwarf_Unsigned sectionsize = 0;
273
274 if (data > enddata || data < startdata ) {
275 /* Corrupt data being read. */
276 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
277 "DW_DLE_RNGLISTS_ERROR: "
278 "The end of an rle entry is past the end "
279 "of its allowed space");
280 return DW_DLV_ERROR;
281 }
282 sectionsize = dbg->de_debug_rnglists.dss_size;
283 if (count > sectionsize) {
284 /* Corrupt data being read. */
285 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
286 "DW_DLE_RNGLISTS_ERROR: "
287 "The number of bytes in a single "
288 "rnglist entry is "
289 "too large to be reasonable");
290 return DW_DLV_ERROR;
291 }
292 }
293
294 *bytes_count_out = (unsigned int)count;
295 *entry_kind = code;
296 *entry_operand1 = val1;
297 *entry_operand2 = val2;
298 return DW_DLV_OK;
299 }
300
301 /* Reads the header. Determines the
302 various offsets, including offset
303 of the next header. ALlocates
304 an array pointed to by rc_offset_value_array
305 here. */
306 int
307 _dwarf_internal_read_rnglists_header(Dwarf_Debug dbg,
308 Dwarf_Bool build_offset_array,
309 Dwarf_Unsigned contextnum,
310 Dwarf_Unsigned sectionlength,
311 Dwarf_Small *data,
312 Dwarf_Small *end_data,
313 Dwarf_Unsigned starting_offset,
314 Dwarf_Rnglists_Context buildhere,
315 Dwarf_Unsigned *next_offset,
316 Dwarf_Error *error)
317 {
318 Dwarf_Small *startdata = data;
319 Dwarf_Unsigned arealen = 0;
320 int offset_size = 0;
321 int exten_size = 0;
322 Dwarf_Unsigned version = 0;
323 unsigned address_size = 0;
324 unsigned segment_selector_size= 0;
325 Dwarf_Unsigned offset_entry_count = 0;
326 Dwarf_Unsigned localoff = 0;
327 Dwarf_Unsigned lists_byte_len = 0;
328 Dwarf_Unsigned secsize_dbg = 0;
329 Dwarf_Unsigned sum_size = 0;
330
331 secsize_dbg = dbg->de_debug_rnglists.dss_size;
332 /* Sanity checks */
333 if (sectionlength > secsize_dbg) {
334 dwarfstring m;
335 dwarfstring_constructor(&m);
336 dwarfstring_append_printf_u(&m,
337 "DW_DLE_RNGLISTS_ERROR: "
338 " section_length argument (%lu) mismatch vs.",
339 sectionlength);
340 dwarfstring_append_printf_u(&m,
341 ".debug_rnglists[.dwo]"
342 " section length",secsize_dbg);
343 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
344 dwarfstring_string(&m));
345 dwarfstring_destructor(&m);
346 return DW_DLV_ERROR;
347 }
348 buildhere->rc_startaddr = data;
349 READ_AREA_LENGTH_CK(dbg,arealen,Dwarf_Unsigned,
350 data,offset_size,exten_size,
351 error,
352 sectionlength,end_data);
353 localoff = offset_size+exten_size;
354 /* local off is bytes including length field */
355 sum_size = arealen+localoff;
356 if (arealen > sectionlength ||
357 sum_size < arealen ||
358 sum_size > sectionlength) {
359 dwarfstring m;
360 dwarfstring_constructor(&m);
361 dwarfstring_append_printf_u(&m,
362 "DW_DLE_RNGLISTS_ERROR: A .debug_rnglists[.dwo] "
363 "area size of 0x%x ",arealen);
364 dwarfstring_append_printf_u(&m,
365 "at offset 0x%x ",starting_offset);
366 dwarfstring_append_printf_u(&m,
367 "is larger than the entire section size of "
368 "0x%x. Corrupt DWARF.",sectionlength);
369 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
370 dwarfstring_string(&m));
371 dwarfstring_destructor(&m);
372 return DW_DLV_ERROR;
373 }
374 buildhere->rc_length = sum_size;
375 buildhere->rc_dbg = dbg;
376 buildhere->rc_index = contextnum;
377 buildhere->rc_header_offset = starting_offset;
378 buildhere->rc_offset_size = offset_size;
379 buildhere->rc_extension_size = exten_size;
380 buildhere->rc_magic = RNGLISTS_MAGIC;
381 READ_UNALIGNED_CK(dbg,version,Dwarf_Unsigned,data,
382 SIZEOFT16,error,end_data);
383 if (version != DW_CU_VERSION5) {
384 dwarfstring m;
385 dwarfstring_constructor(&m);
386 dwarfstring_append_printf_u(&m,
387 "DW_DLE_RNGLISTS_ERROR: The version should be 5 "
388 "but we find %u instead.",version);
389 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
390 dwarfstring_string(&m));
391 dwarfstring_destructor(&m);
392 return DW_DLV_ERROR;
393 }
394 buildhere->rc_version = (Dwarf_Half)version;
395 data += SIZEOFT16;
396 localoff += SIZEOFT16;
397 READ_UNALIGNED_CK(dbg,address_size,unsigned,data,
398 SIZEOFT8,error,end_data);
399 if (address_size != 4 && address_size != 8 &&
400 address_size != 2) {
401 dwarfstring m;
402 dwarfstring_constructor(&m);
403 dwarfstring_append_printf_u(&m,
404 " DW_DLE_RNGLISTS_ERROR: .debug_rnglists[.dwo] "
405 "The address size "
406 "of %u is not supported.",address_size);
407 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
408 dwarfstring_string(&m));
409 dwarfstring_destructor(&m);
410 return DW_DLV_ERROR;
411 }
412 buildhere->rc_address_size = address_size;
413 localoff++;
414 data++;
415
416 READ_UNALIGNED_CK(dbg,segment_selector_size,unsigned,data,
417 SIZEOFT8,error,end_data);
418 buildhere->rc_segment_selector_size = segment_selector_size;
419 data++;
420 localoff++;
421 if (segment_selector_size) {
422 dwarfstring m;
423 dwarfstring_constructor(&m);
424 dwarfstring_append_printf_u(&m,
425 " DW_DLE_RNGLISTS_ERROR: .debug_rnglists[.dwo]"
426 " The segment selector size "
427 "of %u is not supported.",address_size);
428 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
429 dwarfstring_string(&m));
430 dwarfstring_destructor(&m);
431 return DW_DLV_ERROR;
432 }
433 if ((starting_offset+localoff+SIZEOFT32) > secsize_dbg) {
434 dwarfstring m;
435 dwarfstring_constructor(&m);
436 dwarfstring_append_printf_u(&m,
437 " DW_DLE_RNGLISTS_ERROR: .debug_rnglists[.dwo]"
438 " Header runs off the end of the section "
439 " with offset %u",starting_offset+localoff+SIZEOFT32);
440 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
441 dwarfstring_string(&m));
442 dwarfstring_destructor(&m);
443 return DW_DLV_ERROR;
444 }
445
446 READ_UNALIGNED_CK(dbg,offset_entry_count,Dwarf_Unsigned,data,
447 SIZEOFT32,error,end_data);
448 buildhere->rc_offset_entry_count = offset_entry_count;
449 data += SIZEOFT32;
450 localoff+= SIZEOFT32;
451 if (offset_entry_count > (arealen/offset_size)) {
452 dwarfstring m;
453 dwarfstring_constructor(&m);
454 dwarfstring_append_printf_u(&m,
455 " DW_DLE_RNGLISTS_ERROR: .debug_rnglists[.dwo]"
456 " offset entry size impossibly large "
457 " with size 0x%x",
458 offset_entry_count*offset_size);
459 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
460 dwarfstring_string(&m));
461 dwarfstring_destructor(&m);
462 return DW_DLV_ERROR;
463 }
464 if (offset_entry_count){
465 buildhere->rc_offsets_array = data;
466 lists_byte_len += offset_size*offset_entry_count;
467 if (build_offset_array) {
468 Dwarf_Unsigned tabentrynum = 0;
469
470 buildhere->rc_offset_value_array = (Dwarf_Unsigned *)
471 calloc(offset_entry_count, sizeof(Dwarf_Unsigned));
472 if (!buildhere->rc_offset_value_array) {
473 _dwarf_error_string(dbg,error, DW_DLE_ALLOC_FAIL,
474 "dbg,DW_DLE_ALLOC_CAIL: "
475 " The debug_rnglists offset table "
476 "cannot be allocated.");
477 return DW_DLV_ERROR;
478 }
479 for (tabentrynum = 0 ; tabentrynum < offset_entry_count;
480 data += offset_size,++tabentrynum ) {
481 Dwarf_Unsigned entry = 0;
482 int res = 0;
483
484 res = _dwarf_read_unaligned_ck_wrapper(dbg,
485 &entry,data,offset_size,end_data,error);
486 if (res != DW_DLV_OK) {
487 free(buildhere->rc_offset_value_array);
488 buildhere->rc_offset_value_array = 0;
489 return res;
490 }
491 buildhere->rc_offset_value_array[tabentrynum] =
492 entry;
493 }
494 }
495 } /* else no offset table */
496 if (offset_entry_count >= secsize_dbg ||
497 lists_byte_len >= secsize_dbg) {
498 dwarfstring m;
499
500 free(buildhere->rc_offset_value_array);
501 buildhere->rc_offset_value_array = 0;
502 dwarfstring_constructor(&m);
503 dwarfstring_append_printf_u(&m,
504 " DW_DLE_RNGLISTS_ERROR: .debug_rnglists[.dwo]"
505 " offset entry count"
506 " of %u is clearly impossible. Corrupt data",
507 offset_entry_count);
508 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
509 dwarfstring_string(&m));
510 dwarfstring_destructor(&m);
511 return DW_DLV_ERROR;
512 }
513 data += lists_byte_len;
514 buildhere->rc_offsets_off_in_sect = starting_offset+localoff;
515 localoff += lists_byte_len;
516 if (localoff > buildhere->rc_length) {
517 dwarfstring m;
518
519 free(buildhere->rc_offset_value_array);
520 buildhere->rc_offset_value_array = 0;
521
522 dwarfstring_constructor(&m);
523 dwarfstring_append_printf_u(&m,
524 " DW_DLE_RNGLISTS_ERROR: .debug_rnglists[.dwo]"
525 " length of rnglists header too large at"
526 " of %u is clearly impossible. Corrupt data",
527 localoff);
528 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
529 dwarfstring_string(&m));
530 dwarfstring_destructor(&m);
531 return DW_DLV_ERROR;
532 }
533 buildhere->rc_first_rnglist_offset = starting_offset+localoff;
534 buildhere->rc_rnglists_header = startdata;
535 buildhere->rc_endaddr = startdata +buildhere->rc_length;
536 if (buildhere->rc_endaddr > end_data) {
537 dwarfstring m;
538
539 free(buildhere->rc_offset_value_array);
540 buildhere->rc_offset_value_array = 0;
541 dwarfstring_constructor(&m);
542 dwarfstring_append_printf_u(&m,
543 " DW_DLE_RNGLISTS_ERROR: .debug_rnglists[.dwo]"
544 " length of rnglists header (%u) "
545 "runs off end of section. Corrupt data",
546 buildhere->rc_length);
547 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
548 dwarfstring_string(&m));
549 dwarfstring_destructor(&m);
550 return DW_DLV_ERROR;
551 }
552 buildhere->rc_past_last_rnglist_offset =
553 buildhere->rc_header_offset +buildhere->rc_length;
554 if (buildhere->rc_past_last_rnglist_offset > secsize_dbg) {
555 free(buildhere->rc_offset_value_array);
556 buildhere->rc_offset_value_array = 0;
557 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
558 "Impossible end of rnglist");
559 return DW_DLV_ERROR;
560 }
561 *next_offset = buildhere->rc_past_last_rnglist_offset;
562 return DW_DLV_OK;
563 }
564
565 /* We return a pointer to an array of contexts
566 (not context pointers) through *cxt if
567 we succeed and are returning DW_DLV_OK.
568 We never return DW_DLV_NO_ENTRY here. */
569 static int
570 internal_load_rnglists_contexts(Dwarf_Debug dbg,
571 Dwarf_Rnglists_Context **cxt,
572 Dwarf_Unsigned *count,
573 Dwarf_Error *error)
574 {
575 Dwarf_Unsigned offset = 0;
576 Dwarf_Unsigned nextoffset = 0;
577 Dwarf_Small * data = dbg->de_debug_rnglists.dss_data;
578 Dwarf_Unsigned section_size = dbg->de_debug_rnglists.dss_size;
579 Dwarf_Small * startdata = data;
580 Dwarf_Small * end_data = data +section_size;
581 Dwarf_Chain curr_chain = 0;
582 Dwarf_Chain head_chain = 0;
583 Dwarf_Chain * plast = &head_chain;
584 int res = 0;
585 Dwarf_Unsigned i = 0;
586 Dwarf_Unsigned chainlength = 0;
587 Dwarf_Rnglists_Context *fullarray = 0;
588
589 for (i = 0 ; data < end_data ; ++i,data = startdata+nextoffset) {
590 Dwarf_Rnglists_Context newcontext = 0;
591
592 /* sizeof the context struct, not sizeof a pointer */
593 newcontext = malloc(sizeof(*newcontext));
594 if (!newcontext) {
595 free_rnglists_chain(dbg,head_chain);
596 _dwarf_error_string(dbg,error,
597 DW_DLE_ALLOC_FAIL,
598 "DW_DLE_ALLOC_FAIL: Allocation of "
599 "Rnglists_Context failed");
600 return DW_DLV_ERROR;
601 }
602 memset(newcontext,0,sizeof(*newcontext));
603 newcontext->rc_magic = RNGLISTS_MAGIC;
604 res = _dwarf_internal_read_rnglists_header(dbg, TRUE,
605 chainlength,
606 section_size,
607 data,end_data,offset,
608 newcontext,&nextoffset,error);
609 if (res == DW_DLV_ERROR) {
610 free_rnglists_context(newcontext);
611 newcontext = 0;
612 free_rnglists_chain(dbg,head_chain);
613 return res;
614 }
615 newcontext->rc_magic = RNGLISTS_MAGIC;
616 curr_chain = (Dwarf_Chain)
617 _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
618 if (curr_chain == NULL) {
619 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
620 "DW_DLE_ALLOC_FAIL: allocating Rnglists_Context"
621 " chain entry");
622 free_rnglists_context(newcontext);
623 newcontext = 0;
624 free_rnglists_chain(dbg,head_chain);
625 return DW_DLV_ERROR;
626 }
627 curr_chain->ch_item = newcontext;
628 ++chainlength;
629 (*plast) = curr_chain;
630 plast = &(curr_chain->ch_next);
631 offset = nextoffset;
632 newcontext = 0;
633 }
634 fullarray= (Dwarf_Rnglists_Context *)malloc(
635 chainlength *sizeof(Dwarf_Rnglists_Context /*pointer*/));
636 if (!fullarray) {
637 free_rnglists_chain(dbg,head_chain);
638 _dwarf_error_string(dbg,error,
639 DW_DLE_ALLOC_FAIL,"Allocation of "
640 "Rnglists_Context pointer array failed");
641 return DW_DLV_ERROR;
642 }
643 curr_chain = head_chain;
644 for (i = 0; i < chainlength; ++i) {
645 Dwarf_Chain prev = 0;
646 Dwarf_Rnglists_Context c =
647 (Dwarf_Rnglists_Context)curr_chain->ch_item;
648 fullarray[i] = c;
649 curr_chain->ch_item = 0;
650 prev = curr_chain;
651 curr_chain = curr_chain->ch_next;
652 dwarf_dealloc(dbg, prev, DW_DLA_CHAIN);
653 }
654 /* ASSERT: the chain is entirely alloc'd
655 and the array of pointers points to
656 individually malloc'd Dwarf_Rnglists_Context_s */
657 *cxt = fullarray;
658 *count = chainlength;
659 return DW_DLV_OK;
660 }
661
662 /* Used by dwarfdump to print raw rnglists data.
663 Loads all the .debug_rnglists[.dwo] headers and
664 returns DW_DLV_NO_ENTRY if the section
665 is missing or empty.
666 Intended to be done quite early and
667 done exactly once.
668 Harmless to do more than once.
669 With DW_DLV_OK it returns the number of
670 rnglists headers in the section through
671 rnglists_count. */
672 int dwarf_load_rnglists(
673 Dwarf_Debug dbg,
674 Dwarf_Unsigned *rnglists_count,
675 Dwarf_Error *error)
676 {
677 int res = DW_DLV_ERROR;
678 Dwarf_Rnglists_Context *cxt = 0;
679 Dwarf_Unsigned count = 0;
680
681 CHECK_DBG(dbg,error,"dwarf_load_rnglists");
682 if (dbg->de_rnglists_context) {
683 if (rnglists_count) {
684 *rnglists_count = dbg->de_rnglists_context_count;
685 }
686 if (rnglists_count) {
687 }
688 return DW_DLV_OK;
689 }
690 if (!dbg->de_debug_rnglists.dss_size) {
691 /* nothing there. */
692 return DW_DLV_NO_ENTRY;
693 }
694 if (!dbg->de_debug_rnglists.dss_data) {
695 res = _dwarf_load_section(dbg, &dbg->de_debug_rnglists,
696 error);
697 if (res != DW_DLV_OK) {
698 return res;
699 }
700 }
701 res = internal_load_rnglists_contexts(dbg,&cxt,&count,error);
702 if (res == DW_DLV_ERROR) {
703 return res;
704 }
705 dbg->de_rnglists_context = cxt;
706 dbg->de_rnglists_context_count = count;
707 if (rnglists_count) {
708 *rnglists_count = count;
709 }
710 return DW_DLV_OK;
711 }
712
713 /* Frees the memory in use in all rnglists contexts.
714 Done by dwarf_finish() */
715 void
716 _dwarf_dealloc_rnglists_context(Dwarf_Debug dbg)
717 {
718 Dwarf_Unsigned i = 0;
719 Dwarf_Rnglists_Context * rngcon = 0;
720
721 if (IS_INVALID_DBG(dbg)) {
722 return;
723 }
724 if (!dbg->de_rnglists_context) {
725 return;
726 }
727 rngcon = dbg->de_rnglists_context;
728 for ( ; i < dbg->de_rnglists_context_count; ++i) {
729 Dwarf_Rnglists_Context con = rngcon[i];
730 free_rnglists_context(con);
731 rngcon[i] = 0;
732 }
733 free(dbg->de_rnglists_context);
734 dbg->de_rnglists_context = 0;
735 dbg->de_rnglists_context_count = 0;
736 }
737
738 /* Used by dwarfdump to print raw rnglists data. */
739 int
740 dwarf_get_rnglist_offset_index_value(
741 Dwarf_Debug dbg,
742 Dwarf_Unsigned context_index,
743 Dwarf_Unsigned offsetentry_index,
744 Dwarf_Unsigned * offset_value_out,
745 Dwarf_Unsigned * global_offset_value_out,
746 Dwarf_Error *error)
747 {
748 Dwarf_Rnglists_Context con = 0;
749 unsigned offset_len = 0;
750 Dwarf_Small *offsetptr = 0;
751 Dwarf_Unsigned targetoffset = 0;
752 Dwarf_Unsigned localoffset = 0;
753 Dwarf_Unsigned globaloffset = 0;
754 Dwarf_Unsigned section_size = 0;
755
756 CHECK_DBG(dbg,error,"dwarf_get_rnglist_offset_index_value()");
757 if (!dbg->de_rnglists_context) {
758 return DW_DLV_NO_ENTRY;
759 }
760
761 if (!dbg->de_rnglists_context_count) {
762 return DW_DLV_NO_ENTRY;
763 }
764 if (context_index >= dbg->de_rnglists_context_count) {
765 return DW_DLV_NO_ENTRY;
766 }
767 con = dbg->de_rnglists_context[context_index];
768 if (con->rc_magic != RNGLISTS_MAGIC) {
769 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
770 "DW_DLE_DBG_NULL "
771 "rnglists context magic wrong "
772 "not RNGLISTS_MAGIC");
773 return DW_DLV_ERROR;
774 }
775 if (offsetentry_index >= con->rc_offset_entry_count) {
776 return DW_DLV_NO_ENTRY;
777 }
778 offset_len = con->rc_offset_size;
779 localoffset = offsetentry_index*offset_len;
780 if (localoffset >= con->rc_length) {
781 _dwarf_error_string(dbg,error, DW_DLE_RLE_ERROR,
782 "DW_DLE_RLE_ERROR: a .debug_rnglists[.dwo] "
783 "section offset "
784 "is greater than this rnglists table length");
785 return DW_DLV_ERROR;
786 }
787 if ((con->rc_offsets_off_in_sect +localoffset +
788 offset_len) >
789 con->rc_past_last_rnglist_offset) {
790 dwarfstring m;
791
792 dwarfstring_constructor(&m);
793 dwarfstring_append_printf_u(&m,
794 "DW_DLE_RNGLISTS_ERROR "
795 "dwarf_get_rnglist_offset_index_value() "
796 " Offset for index %u is too large. ",
797 offsetentry_index);
798 _dwarf_error_string(dbg, error,DW_DLE_RNGLISTS_ERROR,
799 dwarfstring_string(&m));
800 dwarfstring_destructor(&m);
801 return DW_DLV_ERROR;
802
803 }
804 offsetptr = con->rc_offsets_array + localoffset;
805 READ_UNALIGNED_CK(dbg,targetoffset,Dwarf_Unsigned,
806 offsetptr,
807 offset_len,error,con->rc_endaddr);
808 globaloffset = con->rc_offsets_off_in_sect;
809 section_size = dbg->de_debug_rnglists.dss_size;
810 if (globaloffset >= section_size) {
811 _dwarf_error_string(dbg,error,DW_DLE_RNGLISTS_ERROR,
812 "DW_DLE_RNGLISTS_ERROR: "
813 "The offset of a rnglists entry is past "
814 "its allowed space");
815 return DW_DLV_ERROR;
816 }
817 if (offset_value_out) {
818 *offset_value_out = targetoffset;
819 }
820 if (global_offset_value_out) {
821 *global_offset_value_out = globaloffset;
822 }
823 return DW_DLV_OK;
824 }
825
826 /* Used by dwarfdump to print basic data from the
827 data generated to look at a specific rangelist
828 as returned by dwarf_rnglists_index_get_rle_head()
829 or dwarf_rnglists_offset_get_rle_head. */
830 int dwarf_get_rnglist_head_basics(
831 Dwarf_Rnglists_Head head,
832 Dwarf_Unsigned * rle_count,
833 Dwarf_Unsigned * rle_version,
834 Dwarf_Unsigned * rnglists_index_returned,
835 Dwarf_Unsigned * bytes_total_in_rle,
836 Dwarf_Half * offset_size,
837 Dwarf_Half * address_size,
838 Dwarf_Half * segment_selector_size,
839 Dwarf_Unsigned * overall_offset_of_this_context,
840 Dwarf_Unsigned * total_length_of_this_context,
841 Dwarf_Unsigned * offset_table_offset,
842 Dwarf_Unsigned * offset_table_entrycount,
843 Dwarf_Bool * rnglists_base_present,
844 Dwarf_Unsigned * rnglists_base,
845 Dwarf_Bool * rnglists_base_address_present,
846 Dwarf_Unsigned * rnglists_base_address,
847 Dwarf_Bool * rnglists_debug_addr_base_present,
848 Dwarf_Unsigned * rnglists_debug_addr_base,
849 Dwarf_Error *error)
850 {
851 Dwarf_Rnglists_Context rngcontext = 0;
852
853 if (!head || (!head->rh_dbg) ||
854 head->rh_magic != RNGLISTS_MAGIC) {
855 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
856 "DW_DLE_DBG_NULL "
857 "NULL or invalid Dwarf_Rnglists_Head "
858 "argument passed to "
859 "dwarf_get_rnglist_head_basics()");
860 return DW_DLV_ERROR;
861 }
862 *rle_count = head->rh_count;
863 *rle_version = head->rh_version;
864 *rnglists_index_returned = head->rh_index;
865 *bytes_total_in_rle = head->rh_bytes_total;
866 *offset_size = (Dwarf_Half)head->rh_offset_size;
867 *address_size = (Dwarf_Half)head->rh_address_size;
868 *segment_selector_size =
869 (Dwarf_Half)head->rh_segment_selector_size;
870 rngcontext = head->rh_localcontext;
871 if (rngcontext) {
872 *overall_offset_of_this_context =
873 rngcontext->rc_header_offset;
874 *total_length_of_this_context = rngcontext->rc_length;
875 *offset_table_offset = rngcontext->rc_offsets_off_in_sect;
876 *offset_table_entrycount = rngcontext->rc_offset_entry_count;
877 }
878 *rnglists_base_present = head->rh_at_rnglists_base_present;
879 *rnglists_base= head->rh_at_rnglists_base;
880
881 *rnglists_base_address_present = head->rh_cu_base_address_present;
882 *rnglists_base_address= head->rh_cu_base_address;
883
884 *rnglists_debug_addr_base_present =
885 head->rh_cu_addr_base_offset_present;
886 *rnglists_debug_addr_base = head->rh_cu_addr_base_offset;
887 return DW_DLV_OK;
888 }
889
890 /* Used by dwarfdump to print raw rnglists data.
891 Enables printing of details about the Range List Table
892 Headers, one header per call. Index starting at 0.
893 Returns DW_DLV_NO_ENTRY if index is too high for the table.
894 A .debug_rnglists section may contain any number
895 of Range List Table Headers with their details. */
896 int dwarf_get_rnglist_context_basics(
897 Dwarf_Debug dbg,
898 Dwarf_Unsigned context_index,
899 Dwarf_Unsigned * header_offset,
900 Dwarf_Small * offset_size,
901 Dwarf_Small * extension_size,
902 unsigned * version, /* 5 */
903 Dwarf_Small * address_size,
904 Dwarf_Small * segment_selector_size,
905 Dwarf_Unsigned * offset_entry_count,
906 Dwarf_Unsigned * offset_of_offset_array,
907 Dwarf_Unsigned * offset_of_first_rangeentry,
908 Dwarf_Unsigned * offset_past_last_rangeentry,
909 Dwarf_Error *error)
910 {
911 Dwarf_Rnglists_Context con = 0;
912
913 CHECK_DBG(dbg,error,"dwarf_get_rnglist_context_basics()");
914 if (!dbg->de_rnglists_context_count) {
915 return DW_DLV_NO_ENTRY;
916 }
917 if (context_index >= dbg->de_rnglists_context_count) {
918 return DW_DLV_NO_ENTRY;
919 }
920 con = dbg->de_rnglists_context[context_index];
921 if (con->rc_magic != RNGLISTS_MAGIC) {
922 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
923 "DW_DLE_DBG_NULL "
924 "rnglists context "
925 "not RNGLISTS_MAGIC "
926 "in dwarf_get_rnglist_context_basics()");
927 return DW_DLV_ERROR;
928 }
929 if (header_offset) {
930 *header_offset = con->rc_header_offset;
931 }
932 if (offset_size) {
933 *offset_size = con->rc_offset_size;
934 }
935 if (extension_size) {
936 *extension_size = con->rc_extension_size;
937 }
938 if (version) {
939 *version = con->rc_version;
940 }
941 if (address_size) {
942 *address_size = con->rc_address_size;
943 }
944 if (segment_selector_size) {
945 *segment_selector_size = con->rc_segment_selector_size;
946 }
947 if (offset_entry_count) {
948 *offset_entry_count = con->rc_offset_entry_count;
949 }
950 if (offset_of_offset_array) {
951 *offset_of_offset_array = con->rc_offsets_off_in_sect;
952 }
953 if (offset_of_first_rangeentry) {
954 *offset_of_first_rangeentry = con->rc_first_rnglist_offset;
955 }
956 if (offset_past_last_rangeentry) {
957 *offset_past_last_rangeentry =
958 con->rc_past_last_rnglist_offset;
959 }
960 return DW_DLV_OK;
961 }
962
963 /* Used by dwarfdump to print raw rnglists data.
964 entry offset is offset_of_first_rangeentry.
965 Stop when the returned *next_entry_offset
966 is == offset_past_last_rangentry (from
967 dwarf_get_rnglist_context_plus).
968 This only makes sense within those ranges.
969 This retrieves raw detail from the section,
970 no base values or anything are added.
971 So this returns raw individual entries
972 for a single rnglist header, meaning a
973 a single Dwarf_Rnglists_Context. */
974 int dwarf_get_rnglist_rle(
975 Dwarf_Debug dbg,
976 Dwarf_Unsigned contextnumber,
977 Dwarf_Unsigned entry_offset,
978 Dwarf_Unsigned endoffset,
979 unsigned *entrylen,
980 unsigned *entry_kind,
981 Dwarf_Unsigned *entry_operand1,
982 Dwarf_Unsigned *entry_operand2,
983 Dwarf_Error *error)
984 {
985 Dwarf_Rnglists_Context con = 0;
986 Dwarf_Small *data = 0;
987 Dwarf_Small *enddata = 0;
988 int res = 0;
989 unsigned address_size = 0;
990 Dwarf_Unsigned secsize = 0;
991
992 CHECK_DBG(dbg,error,"dwarf_get_rnglist_rle()");
993 secsize = dbg->de_debug_rnglists.dss_size;
994 if (!dbg->de_rnglists_context_count) {
995 return DW_DLV_NO_ENTRY;
996 }
997 if (!dbg->de_rnglists_context_count) {
998 return DW_DLV_NO_ENTRY;
999 }
1000 if (contextnumber >= dbg->de_rnglists_context_count) {
1001 return DW_DLV_NO_ENTRY;
1002 }
1003 if (entry_offset >= secsize) {
1004 return DW_DLV_NO_ENTRY;
1005 }
1006 if (endoffset > secsize) {
1007 _dwarf_error_string(dbg, error, DW_DLE_RNGLISTS_ERROR,
1008 " DW_DLE_RNGLISTS_ERROR "
1009 "The end offset to "
1010 "dwarf_get_rnglist_rle() is "
1011 "too large for the section");
1012 return DW_DLV_ERROR;
1013 }
1014 if (endoffset <= entry_offset) {
1015 _dwarf_error_string(dbg, error, DW_DLE_RNGLISTS_ERROR,
1016 " DW_DLE_RNGLISTS_ERROR "
1017 "The end offset to "
1018 "dwarf_get_rnglist_rle() is smaller than "
1019 "the entry offset! Corrupt data");
1020 return DW_DLV_ERROR;
1021 }
1022 if ((entry_offset +1) > endoffset) {
1023 /* The read_single_rle_entry call will need
1024 at least 1 byte as it reads at least one
1025 ULEB */
1026 dwarfstring m;
1027 dwarfstring_constructor(&m);
1028 dwarfstring_append_printf_u(&m,
1029 " DW_DLE_RNGLISTS_ERROR "
1030 "The entry offset+1 (%lu) "
1031 "dwarf_get_rnglist_rle() is too close to the"
1032 " end",entry_offset+1);
1033 dwarfstring_append_printf_u(&m,
1034 " of the offset of the end of the entry (%lu)"
1035 " Apparently corrupt Dwarf",endoffset);
1036 _dwarf_error_string(dbg, error, DW_DLE_RNGLISTS_ERROR,
1037 dwarfstring_string(&m));
1038 dwarfstring_destructor(&m);
1039 return DW_DLV_ERROR;
1040 }
1041 data = dbg->de_debug_rnglists.dss_data +
1042 entry_offset;
1043 enddata = dbg->de_debug_rnglists.dss_data +
1044 endoffset;
1045
1046 con = dbg->de_rnglists_context[contextnumber];
1047 address_size = con->rc_address_size;
1048 res = read_single_rle_entry(dbg,
1049 data,entry_offset,enddata,
1050 address_size,entrylen,
1051 entry_kind, entry_operand1, entry_operand2,
1052 error);
1053 return res;
1054 }
1055
1056 static int
1057 _dwarf_which_rnglists_context(Dwarf_Debug dbg,
1058 Dwarf_CU_Context ctx,
1059 Dwarf_Unsigned rnglist_offset/* not always set */,
1060 Dwarf_Unsigned *index,
1061 Dwarf_Error *error)
1062 {
1063 Dwarf_Unsigned count = 0;
1064 Dwarf_Rnglists_Context *array;
1065 Dwarf_Rnglists_Context rcx = 0;
1066 Dwarf_Unsigned rcxoff = 0;
1067 Dwarf_Unsigned rcxend = 0;
1068
1069 Dwarf_Unsigned i = 0;
1070 Dwarf_Unsigned rnglists_base = 0;
1071 Dwarf_Bool rnglists_base_present = FALSE;
1072 int res = 0;
1073 Dwarf_Bool found_base = FALSE;
1074 Dwarf_Unsigned chosen_offset = 0;
1075
1076 array = dbg->de_rnglists_context;
1077 count = dbg->de_rnglists_context_count;
1078
1079 if (!array) {
1080 return DW_DLV_NO_ENTRY;
1081 }
1082 if (count == 1) {
1083 *index = 0;
1084 return DW_DLV_OK;
1085 }
1086 if (ctx->cc_rnglists_base_present) {
1087 rnglists_base_present = ctx->cc_rnglists_base_present;
1088 rnglists_base = ctx->cc_rnglists_base;
1089 found_base = TRUE;
1090 chosen_offset = rnglists_base;
1091 }
1092 if (!found_base) {
1093 res = _dwarf_has_SECT_fission(ctx,
1094 DW_SECT_RNGLISTS,
1095 &rnglists_base_present,&rnglists_base);
1096 if (res == DW_DLV_OK) {
1097 found_base = TRUE;
1098 chosen_offset = rnglists_base;
1099 }
1100 }
1101 if (!found_base) {
1102 rnglists_base = rnglist_offset;
1103 chosen_offset = rnglist_offset;
1104 }
1105
1106 rcx = array[i];
1107 rcxoff = rcx->rc_header_offset;
1108 rcxend = rcxoff + rcx->rc_length;
1109
1110 /* We look at the location of each rnglist context
1111 to find one with the offset we want */
1112 for ( i = 0 ; i < count; ++i) {
1113 rcx = array[i];
1114 rcxoff = rcx->rc_header_offset;
1115 rcxend = rcxoff +
1116 rcx->rc_length;
1117 if (chosen_offset < rcxoff){
1118 continue;
1119 }
1120 if (chosen_offset < rcxend ){
1121 *index = i;
1122 return DW_DLV_OK;
1123 }
1124 }
1125 {
1126 dwarfstring m;
1127
1128 dwarfstring_constructor(&m);
1129 dwarfstring_append_printf_u(&m,
1130 "DW_DLE_RNGLISTS_ERROR: rnglist ran off end "
1131 " finding target offset of"
1132 " 0x%" DW_PR_XZEROS DW_PR_DUx ,chosen_offset);
1133 dwarfstring_append(&m,
1134 " Not found anywhere in .debug_rnglists[.dwo] "
1135 "data. Corrupted data?");
1136 _dwarf_error_string(dbg,error,
1137 DW_DLE_RNGLISTS_ERROR,
1138 dwarfstring_string(&m));
1139 dwarfstring_destructor(&m);
1140 }
1141 return DW_DLV_ERROR;
1142 }
1143
1144 void
1145 dwarf_dealloc_rnglists_head(Dwarf_Rnglists_Head h)
1146 {
1147 Dwarf_Debug dbg = 0;
1148
1149 if (!h || !h->rh_dbg || h->rh_magic != RNGLISTS_MAGIC) {
1150 return;
1151 }
1152 dbg = h->rh_dbg;
1153 h->rh_magic = 0;
1154 dwarf_dealloc(dbg,h,DW_DLA_RNGLISTS_HEAD);
1155 return;
1156 }
1157
1158 /* Appends to singly-linked list.
1159 Caller will eventually free as appropriate. */
1160 static int
1161 alloc_rle_and_append_to_list(Dwarf_Debug dbg,
1162 Dwarf_Rnglists_Head rctx,
1163 Dwarf_Rnglists_Entry *e_out,
1164 Dwarf_Error *error)
1165 {
1166 Dwarf_Rnglists_Entry e = 0;
1167
1168 e = malloc(sizeof(struct Dwarf_Rnglists_Entry_s));
1169 if (!e) {
1170 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
1171 "DW_DLE_ALLOC_FAIL: Out of memory in "
1172 "building list of rnglists entries on a DIE.");
1173 return DW_DLV_ERROR;
1174 }
1175 memset(e,0,sizeof(struct Dwarf_Rnglists_Entry_s));
1176 if (rctx->rh_first) {
1177 rctx->rh_last->rle_next = e;
1178 rctx->rh_last = e;
1179 } else {
1180 rctx->rh_first = e;
1181 rctx->rh_last = e;
1182 }
1183 rctx->rh_count++;
1184 *e_out = e;
1185 return DW_DLV_OK;
1186 }
1187
1188 /* Read the group of rangelists entries, and
1189 finally build an array of Dwarf_Rnglists_Entry
1190 records. Attach to rctx here.
1191 Since on error the caller will destruct the rctx
1192 and we ensure to attach allocations there
1193 the caller will destruct the allocations here
1194 in case we return DW_DLV_ERROR*/
1195 static int
1196 build_array_of_rle(Dwarf_Debug dbg,
1197 Dwarf_Rnglists_Head rhd,
1198 Dwarf_Error *error)
1199 {
1200 int res = 0;
1201 Dwarf_Small * data = rhd->rh_rlepointer;
1202 Dwarf_Unsigned dataoffset = rhd->rh_rlearea_offset;
1203 Dwarf_Small *enddata = rhd->rh_end_data_area;
1204 unsigned address_size = (unsigned int)rhd->rh_address_size;
1205 Dwarf_Unsigned bytescounttotal= 0;
1206 Dwarf_Unsigned latestbaseaddr = 0;
1207 Dwarf_Bool foundbaseaddr = FALSE;
1208 int done = FALSE;
1209 Dwarf_Bool no_debug_addr_available = FALSE;
1210
1211 if (rhd->rh_cu_base_address_present) {
1212 /* The CU DIE had DW_AT_low_pc
1213 and it is a base address. */
1214 latestbaseaddr = rhd->rh_cu_base_address;
1215 foundbaseaddr = TRUE;
1216 }
1217 for ( ; !done ; ) {
1218 unsigned entrylen = 0;
1219 unsigned code = 0;
1220 Dwarf_Unsigned val1 = 0;
1221 Dwarf_Unsigned val2 = 0;
1222 Dwarf_Addr addr1= 0;
1223 Dwarf_Addr addr2 = 0;
1224 Dwarf_Rnglists_Entry e = 0;
1225
1226 res = read_single_rle_entry(dbg,
1227 data,dataoffset, enddata,
1228 address_size,&entrylen,
1229 &code,&val1, &val2,error);
1230 if (res != DW_DLV_OK) {
1231 return res;
1232 }
1233 res = alloc_rle_and_append_to_list(dbg,rhd,&e,error);
1234 if (res != DW_DLV_OK) {
1235 return res;
1236 }
1237 e->rle_code = code,
1238 e->rle_entrylen = entrylen;
1239 e->rle_raw1 = val1;
1240 e->rle_raw2 = val2;
1241 bytescounttotal += entrylen;
1242 data += entrylen;
1243 if (code == DW_RLE_end_of_list) {
1244 done = TRUE;
1245 break;
1246 }
1247 switch(code) {
1248 case DW_RLE_base_addressx:
1249 if (no_debug_addr_available) {
1250 res = DW_DLV_NO_ENTRY;
1251 } else {
1252 res = _dwarf_look_in_local_and_tied_by_index(
1253 dbg,rhd->rh_context,val1,&addr1,
1254 error);
1255 }
1256 if (res != DW_DLV_OK) {
1257 no_debug_addr_available = TRUE;
1258 e->rle_index_failed = TRUE;
1259 e->rle_cooked1 = 0;
1260 foundbaseaddr = FALSE;
1261 if (res == DW_DLV_ERROR) {
1262 dwarf_dealloc_error(dbg,*error);
1263 *error = 0;
1264 }
1265 } else {
1266 foundbaseaddr = TRUE;
1267 e->rle_cooked1 = addr1;
1268 latestbaseaddr = addr1;
1269 }
1270 break;
1271 case DW_RLE_startx_endx:
1272 if (no_debug_addr_available) {
1273 res = DW_DLV_NO_ENTRY;
1274 } else {
1275 res = _dwarf_look_in_local_and_tied_by_index(
1276 dbg,rhd->rh_context,val1,&addr1,
1277 error);
1278 }
1279 if (res != DW_DLV_OK) {
1280 no_debug_addr_available = TRUE;
1281 e->rle_index_failed = TRUE;
1282 e->rle_cooked1 = 0;
1283 if (res == DW_DLV_ERROR) {
1284 dwarf_dealloc_error(dbg,*error);
1285 *error = 0;
1286 }
1287 } else {
1288 e->rle_cooked1 = addr1;
1289 }
1290 if (no_debug_addr_available) {
1291 res = DW_DLV_NO_ENTRY;
1292 } else {
1293 res = _dwarf_look_in_local_and_tied_by_index(
1294 dbg,rhd->rh_context,val2,&addr2,
1295 error);
1296 }
1297 if (res != DW_DLV_OK) {
1298 no_debug_addr_available = TRUE;
1299 e->rle_index_failed = TRUE;
1300 e->rle_cooked2 = 0;
1301 if (res == DW_DLV_ERROR) {
1302 dwarf_dealloc_error(dbg,*error);
1303 *error = 0;
1304 }
1305 } else {
1306 e->rle_cooked2 = addr2;
1307 }
1308 break;
1309 case DW_RLE_startx_length:
1310 if (no_debug_addr_available) {
1311 res = DW_DLV_NO_ENTRY;
1312 } else {
1313 res = _dwarf_look_in_local_and_tied_by_index(
1314 dbg,rhd->rh_context,val1,&addr1,
1315 error);
1316 }
1317 if (res != DW_DLV_OK) {
1318 no_debug_addr_available = TRUE;
1319 e->rle_index_failed = TRUE;
1320 e->rle_cooked2 = 0;
1321 e->rle_cooked1 = 0;
1322 if (res == DW_DLV_ERROR) {
1323 dwarf_dealloc_error(dbg,*error);
1324 *error = 0;
1325 }
1326 } else {
1327 e->rle_cooked1 = addr1;
1328 e->rle_cooked2 = val2+addr1;
1329 }
1330 break;
1331 case DW_RLE_offset_pair:
1332 if (foundbaseaddr) {
1333 e->rle_cooked1 = val1+latestbaseaddr;
1334 e->rle_cooked2 = val2+latestbaseaddr;
1335 } else {
1336 /* Well, something failed, could be
1337 missing debug_addr. */
1338 e->rle_index_failed = TRUE;
1339 e->rle_cooked2 = 0;
1340 e->rle_cooked1 = 0;
1341 }
1342 break;
1343 case DW_RLE_base_address:
1344 foundbaseaddr = TRUE;
1345 latestbaseaddr = val1;
1346 e->rle_cooked1 = val1;
1347 break;
1348 case DW_RLE_start_end:
1349 e->rle_cooked1 = val1;
1350 e->rle_cooked2 = val2;
1351 break;
1352 case DW_RLE_start_length:
1353 e->rle_cooked1 = val1;
1354 e->rle_cooked2 = val2+val1;
1355 break;
1356 default: {
1357 dwarfstring m;
1358
1359 dwarfstring_constructor(&m);
1360 dwarfstring_append_printf_u(&m,
1361 " DW_DLE_RNGLISTS_ERROR: "
1362 " The .debug_rnglists[.dwo] "
1363 " rangelist code 0x%x is unknown, "
1364 " DWARF5 is corrupted.",code);
1365 _dwarf_error_string(dbg, error,
1366 DW_DLE_RNGLISTS_ERROR,
1367 dwarfstring_string(&m));
1368 dwarfstring_destructor(&m);
1369 return DW_DLV_ERROR;
1370 }
1371 }
1372 }
1373 if (rhd->rh_count > 0) {
1374 Dwarf_Rnglists_Entry* array = 0;
1375 Dwarf_Rnglists_Entry cur = 0;
1376 Dwarf_Unsigned i = 0;
1377
1378 /* Creating an array of pointers. */
1379 array = (Dwarf_Rnglists_Entry*)malloc(
1380 rhd->rh_count *sizeof(Dwarf_Rnglists_Entry));
1381 if (!array) {
1382 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
1383 "DW_DLE_ALLOC_FAIL: Out of memory in "
1384 "turning list of rnglists entries on a DIE"
1385 "into a pointer array");
1386 return DW_DLV_ERROR;
1387 }
1388 cur = rhd->rh_first;
1389 for ( ; i < rhd->rh_count; ++i) {
1390 array[i] = cur;
1391 cur = cur->rle_next;
1392 }
1393 rhd->rh_rnglists = array;
1394 rhd->rh_first = 0;
1395 rhd->rh_last = 0;
1396 }
1397 rhd->rh_bytes_total = bytescounttotal;
1398 return DW_DLV_OK;
1399 }
1400
1401 static int
1402 _dwarf_fill_in_rle_head(Dwarf_Debug dbg,
1403 Dwarf_Half theform,
1404 /* attr_val is either an offset
1405 (theform == DW_FORM_sec_offset)
1406 to a specific rangelist entry set or
1407 or a rnglistx index DW_FORM_rnglistx
1408 at that index is the local offset of
1409 a specific rangelist entry set. */
1410 Dwarf_Unsigned attr_val,
1411 /* ctx is the context from the PRIMARY
1412 dbg, not necessarily for *this* dbg */
1413 Dwarf_CU_Context ctx,
1414 Dwarf_Rnglists_Head *head_out,
1415 Dwarf_Unsigned *entries_count_out,
1416 Dwarf_Unsigned *global_offset_of_rle_set,
1417 Dwarf_Error *error)
1418 {
1419 Dwarf_Bool is_rnglistx = FALSE;
1420 Dwarf_Unsigned entrycount = 0;
1421 Dwarf_Unsigned offset_in_rnglists = 0;
1422 Dwarf_Rnglists_Context *array = 0;
1423 Dwarf_Rnglists_Context rctx = 0;
1424 struct Dwarf_Rnglists_Head_s shead;
1425 Dwarf_Rnglists_Head lhead = 0;
1426 Dwarf_Unsigned rnglists_contextnum = 0;
1427 int res = 0;
1428 Dwarf_Small *table_base = 0;
1429 Dwarf_Small *table_entry = 0;
1430 Dwarf_Small *enddata = 0;
1431 Dwarf_Unsigned rle_global_offset = 0;
1432 unsigned offsetsize = 0;
1433 Dwarf_Unsigned secsize = 0;
1434 Dwarf_Debug localdbg = 0;
1435
1436 if (theform == DW_FORM_rnglistx) {
1437 is_rnglistx = TRUE;
1438 } else {
1439 offset_in_rnglists = attr_val;
1440 }
1441 /* ASSERT: the 3 pointers just set are non-null */
1442 /* the context cc_rnglists_base gives the offset
1443 of the array. of offsets (if cc_rnglists_base_present) */
1444 secsize = dbg->de_debug_rnglists.dss_size;
1445 if (dbg != ctx->cc_dbg) {
1446 /* is_rnglistx TRUE */
1447 /* Now find the correct secondary context via
1448 signature. */
1449 Dwarf_CU_Context lctx = 0;
1450 if (ctx->cc_signature_present) {
1451 /* Looking in current dbg for the correct cu context */
1452 res = _dwarf_search_for_signature(dbg,
1453 ctx->cc_signature,
1454 &lctx,error);
1455 if (res != DW_DLV_OK) {
1456 if (res == DW_DLV_NO_ENTRY) {
1457 } else {
1458 _dwarf_error_string(dbg,error, DW_DLE_RLE_ERROR,
1459 "DW_DLE_RLE_ERROR: a .debug_rnglists[.dwo] "
1460 "cu context cannot be found with the "
1461 "correct signature");
1462 }
1463 return res;
1464 } else {
1465 ctx = lctx;
1466 }
1467 } else {
1468 /* No signature. Hopeless, I think. */
1469 _dwarf_error_string(dbg,error, DW_DLE_RLE_ERROR,
1470 "DW_DLE_RLE_ERROR: a .debug_rnglists[.dwo] "
1471 "cu context cannot be found as there is "
1472 "no signature to use");
1473 return DW_DLV_ERROR;
1474 }
1475 }
1476
1477 /* A */
1478 if (ctx->cc_rnglists_base_present) {
1479 offset_in_rnglists = ctx->cc_rnglists_base;
1480 } else {
1481 offset_in_rnglists = attr_val;
1482 }
1483 if (offset_in_rnglists >= secsize) {
1484 _dwarf_error_string(dbg,error, DW_DLE_RLE_ERROR,
1485 "DW_DLE_RLE_ERROR: a .debug_rnglists[.dwo] offset "
1486 "is greater than the rnglists section size");
1487 return DW_DLV_ERROR;
1488 }
1489 /* B */
1490 localdbg = dbg;
1491 {
1492 /* This call works for updated DWARF5 not inheriting
1493 DW_AT_rnglists_base or generating such.
1494 It returns rangelists_context 0 if no
1495 base, which works for gcc/clang. */
1496 res = _dwarf_which_rnglists_context(localdbg,ctx,
1497 offset_in_rnglists,
1498 &rnglists_contextnum,error);
1499 if (res == DW_DLV_OK) {
1500 /* FALL THROUGH */
1501 } else if (res == DW_DLV_NO_ENTRY) {
1502 rnglists_contextnum = 0;
1503 /* FALL THROUGH */
1504 } else {
1505 return res;
1506 }
1507 }
1508 /* C */
1509 array = localdbg->de_rnglists_context;
1510 rctx = array[rnglists_contextnum];
1511 table_base = rctx->rc_offsets_array;
1512 entrycount = rctx->rc_offset_entry_count;
1513 offsetsize = rctx->rc_offset_size;
1514 enddata = rctx->rc_endaddr;
1515 if (is_rnglistx && attr_val >= entrycount) {
1516 dwarfstring m;
1517
1518 dwarfstring_constructor(&m);
1519 dwarfstring_append_printf_u(&m,
1520 "DW_DLE_RNGLISTS_ERROR: rnglists table index of"
1521 " %u" ,attr_val);
1522 dwarfstring_append_printf_u(&m,
1523 " too large for table of %u "
1524 "entries.",entrycount);
1525 _dwarf_error_string(dbg,error,
1526 DW_DLE_RNGLISTS_ERROR,
1527 dwarfstring_string(&m));
1528 dwarfstring_destructor(&m);
1529 return DW_DLV_ERROR;
1530 }
1531 memset(&shead,0,sizeof(shead));
1532 shead.rh_context = ctx;
1533 shead.rh_magic = RNGLISTS_MAGIC;
1534 shead.rh_localcontext = rctx;
1535 shead.rh_index = rnglists_contextnum;
1536 shead.rh_version = rctx->rc_version;
1537 shead.rh_offset_size = offsetsize;
1538 shead.rh_address_size = rctx->rc_address_size;
1539 shead.rh_segment_selector_size =
1540 rctx->rc_segment_selector_size;
1541
1542 /* DW_AT_rnglists_base from CU */
1543 shead.rh_at_rnglists_base_present =
1544 ctx->cc_rnglists_base_present;
1545 shead.rh_at_rnglists_base = ctx->cc_rnglists_base;
1546
1547 /* DW_AT_low_pc originally if present. From CU,
1548 possibly inherited into dwo from skeleton. */
1549 shead.rh_cu_base_address_present = ctx->cc_base_address_present;
1550 shead.rh_cu_base_address = ctx->cc_base_address;;
1551
1552 /* base address DW_AT_addr_base of our part of
1553 .debug_addr, from CU */
1554 shead.rh_cu_addr_base_offset = ctx->cc_addr_base_offset;
1555 shead.rh_cu_addr_base_offset_present =
1556 ctx->cc_addr_base_offset_present;
1557
1558 /* D */
1559 if (is_rnglistx) {
1560 Dwarf_Unsigned table_entryval = 0;
1561
1562 table_entry = attr_val*offsetsize + table_base;
1563 /* No malloc here yet so no leak if the macro returns
1564 DW_DLV_ERROR */
1565 READ_UNALIGNED_CK(localdbg,table_entryval, Dwarf_Unsigned,
1566 table_entry,offsetsize,error,enddata);
1567 if (table_entryval >= secsize) {
1568 _dwarf_error_string(dbg,error, DW_DLE_RLE_ERROR,
1569 "DW_DLE_RLE_ERROR: a .debug_rnglists[.dwo] "
1570 "table entry "
1571 "value is impossibly large");
1572 return DW_DLV_ERROR;
1573 }
1574 if ((rctx->rc_offsets_off_in_sect +
1575 table_entryval) >= secsize) {
1576 _dwarf_error_string(dbg,error, DW_DLE_RLE_ERROR,
1577 "DW_DLE_RLE_ERROR: a .debug_rnglist[.dwo]s "
1578 "table entry "
1579 "value + section offset is impossibly large");
1580 return DW_DLV_ERROR;
1581 }
1582 rle_global_offset = rctx->rc_offsets_off_in_sect +
1583 table_entryval;
1584 } else {
1585 rle_global_offset = attr_val;
1586 }
1587 /* E */
1588 shead.rh_end_data_area = enddata;
1589 shead.rh_rlearea_offset = rle_global_offset;
1590 shead.rh_rlepointer = rle_global_offset +
1591 localdbg->de_debug_rnglists.dss_data;
1592 lhead = (Dwarf_Rnglists_Head)
1593 _dwarf_get_alloc(dbg,DW_DLA_RNGLISTS_HEAD,1);
1594 if (!lhead) {
1595 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
1596 "Allocating a Dwarf_Rnglists_Head struct fails"
1597 " in libdwarf function "
1598 "dwarf_rnglists_index_get_rle_head()");
1599 return DW_DLV_ERROR;
1600 }
1601 /* To read correctly from DWARF data we must
1602 use the data in the tied (SECONDARY) when
1603 applicable. This is a bit delicate. */
1604 shead.rh_dbg = localdbg;
1605 *lhead = shead;
1606 res = build_array_of_rle(localdbg,lhead,error);
1607 if (res != DW_DLV_OK) {
1608 dwarf_dealloc(localdbg,lhead,DW_DLA_RNGLISTS_HEAD);
1609 return res;
1610 }
1611 if (global_offset_of_rle_set) {
1612 *global_offset_of_rle_set = rle_global_offset;
1613 }
1614 /* Caller needs the head pointer else there will be leaks. */
1615 *head_out = lhead;
1616 if (entries_count_out) {
1617 *entries_count_out = lhead->rh_count;
1618 }
1619 return DW_DLV_OK;
1620 }
1621
1622 /* Build a head with all the relevent Entries
1623 attached.
1624 If the context is not found in the main object,
1625 and there is a tied-file
1626 use the context signature (should be present)
1627 and look in the tied-file for a match and
1628 get the content there.
1629 */
1630 int
1631 dwarf_rnglists_get_rle_head(
1632 Dwarf_Attribute attr,
1633 Dwarf_Half theform,
1634 /* attr_val is either an offset
1635 (theform == DW_FORM_sec_offset)
1636 to a specific rangelist entry set or
1637 or a rnglistx index DW_FORM_rnglistx
1638 at that index is the local offset of
1639 a specific rangelist entry set. */
1640 Dwarf_Unsigned attr_val,
1641 Dwarf_Rnglists_Head *head_out,
1642 Dwarf_Unsigned *entries_count_out,
1643 Dwarf_Unsigned *global_offset_of_rle_set,
1644 Dwarf_Error *error)
1645 {
1646 int res = 0;
1647 Dwarf_CU_Context ctx = 0;
1648 Dwarf_Debug dbg = 0;
1649 Dwarf_Bool is_rnglistx = FALSE;
1650
1651 if (!attr) {
1652 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1653 "DW_DLE_DBG_NULL "
1654 "NULL attribute "
1655 "argument passed to "
1656 "dwarf_rnglists_get_rle_head()");
1657 return DW_DLV_ERROR;
1658 }
1659 ctx = attr->ar_cu_context;
1660 dbg = ctx->cc_dbg;
1661
1662 if (theform == DW_FORM_rnglistx) {
1663 is_rnglistx = TRUE;
1664 }
1665 CHECK_DBG(dbg,error,
1666 "dwarf_rnglists_get_rle_head() via attribute");
1667
1668 if (is_rnglistx) {
1669 if (ctx->cc_rnglists_base_present ||
1670 dbg->de_rnglists_context_count == 1) {
1671 /* leave on primary.
1672 WARNING: It is not clear whether
1673 looking for a context count of 1
1674 is actually correct, but it
1675 seems to work. */
1676 } else if (DBG_HAS_SECONDARY(dbg)){
1677 dbg = dbg->de_secondary_dbg;
1678 CHECK_DBG(dbg,error,
1679 "dwarf_rnglists_get_rle_head() via attribute(sec)");
1680 }
1681 } else {
1682 /* attr_val is .debug_rnglists section global offset
1683 of a range list*/
1684 if (!dbg->de_debug_rnglists.dss_size ||
1685 attr_val >= dbg->de_debug_rnglists.dss_size) {
1686 if (DBG_HAS_SECONDARY(dbg)) {
1687 dbg = dbg->de_secondary_dbg;
1688 CHECK_DBG(dbg,error,
1689 "dwarf_rnglists_get_rle_head() "
1690 "via attribute(secb)");
1691 } else {
1692 /* There is an error to be
1693 generated later */
1694 }
1695 }
1696 }
1697
1698 res = _dwarf_load_section(dbg,
1699 &dbg->de_debug_rnglists,
1700 error);
1701 if (res == DW_DLV_ERROR) {
1702 return res;
1703 }
1704 if (res == DW_DLV_OK && dbg->de_debug_rnglists.dss_size) {
1705 res = _dwarf_fill_in_rle_head(dbg, theform,
1706 /* attr_val is either an offset
1707 (theform == DW_FORM_sec_offset)
1708 to a specific rangelist entry set or
1709 or a rnglistx index DW_FORM_rnglistx
1710 at that index is the local offset of
1711 a specific rangelist entry set offset table. */
1712 attr_val,
1713 ctx,
1714 head_out,
1715 entries_count_out,
1716 global_offset_of_rle_set,
1717 error);
1718 if (res == DW_DLV_ERROR) {
1719 return res;
1720 }
1721 }
1722 return res;
1723 }
1724
1725 /* As of 18 Aug 2020
1726 this ignores null pointer inputs for the various
1727 pointers-returning-values (rle_value_out etc)
1728 for the convenience of callers. */
1729 int
1730 dwarf_get_rnglists_entry_fields_a(
1731 Dwarf_Rnglists_Head head,
1732 Dwarf_Unsigned entrynum,
1733 unsigned *entrylen,
1734 unsigned *rle_value_out,
1735 Dwarf_Unsigned *raw1,
1736 Dwarf_Unsigned *raw2,
1737 Dwarf_Bool *debug_addr_unavailable,
1738 Dwarf_Unsigned *cooked1,
1739 Dwarf_Unsigned *cooked2,
1740 Dwarf_Error *error)
1741 {
1742 Dwarf_Rnglists_Entry e = 0;
1743
1744 if (!head || !head->rh_dbg || head->rh_magic != RNGLISTS_MAGIC) {
1745 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1746 "DW_DLE_DBG_NULL "
1747 "NULL or invalid Dwarf_Rnglists_Head "
1748 "argument passed to "
1749 "dwarf_get_rnglists_entry_fields_a()");
1750 return DW_DLV_ERROR;
1751 }
1752 if (entrynum >= head->rh_count) {
1753 return DW_DLV_NO_ENTRY;
1754 }
1755 e = head->rh_rnglists[entrynum];
1756 if (entrylen) {
1757 *entrylen = e->rle_entrylen;
1758 }
1759 if (rle_value_out) {
1760 *rle_value_out = e->rle_code;
1761 }
1762 if (raw1) {
1763 *raw1 = e->rle_raw1;
1764 }
1765 if (raw2) {
1766 *raw2 = e->rle_raw2;
1767 }
1768 if (debug_addr_unavailable) {
1769 *debug_addr_unavailable = e->rle_index_failed;
1770 }
1771 if (cooked1) {
1772 *cooked1 = e->rle_cooked1;
1773 }
1774 if (cooked2) {
1775 *cooked2 = e->rle_cooked2;
1776 }
1777 return DW_DLV_OK;
1778 }
1779
1780 /* Deals with both fully and partially build head */
1781 static void
1782 _dwarf_free_rnglists_head(Dwarf_Rnglists_Head head)
1783 {
1784 if (head->rh_first) {
1785 /* partially built head. */
1786 /* ASSERT: rh_rnglists is NULL */
1787 Dwarf_Rnglists_Entry cur = head->rh_first;
1788 Dwarf_Rnglists_Entry next = 0;
1789
1790 for ( ; cur ; cur = next) {
1791 next = cur->rle_next;
1792 free(cur);
1793 }
1794 head->rh_first = 0;
1795 head->rh_last = 0;
1796 head->rh_count = 0;
1797 } else {
1798 /* ASSERT: rh_first and rh_last are NULL */
1799 /* fully built head. */
1800 Dwarf_Unsigned i = 0;
1801
1802 /* Deal with the array form. */
1803 for ( ; i < head->rh_count; ++i) {
1804 free(head->rh_rnglists[i]);
1805 }
1806 free(head->rh_rnglists);
1807 head->rh_rnglists = 0;
1808 }
1809 }
1810
1811 void
1812 _dwarf_rnglists_head_destructor(void *head)
1813 {
1814 Dwarf_Rnglists_Head h = head;
1815
1816 _dwarf_free_rnglists_head(h);
1817 }