Debian Codegraph @

src/lib/libdwarf/dwarf_loclists.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 debugging */
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_loc.h"
53 #include "dwarf_loclists.h"
54
55 #define SIZEOFT8 1
56 #define SIZEOFT16 2
57 #define SIZEOFT32 4
58 #define SIZEOFT64 8
59
60 #if 0 /* dump_bytes */
61 static void
62 dump_bytes(const char *msg,Dwarf_Small * start, long len)
63 {
64 Dwarf_Small *end = start + len;
65 Dwarf_Unsigned max=16;
66 Dwarf_Unsigned ct=0;
67 Dwarf_Unsigned full=0;
68 Dwarf_Small *cur = start;
69
70 printf("%s (0x%lx)\n",msg,(unsigned long)start);
71 for (; cur < end; cur++, ++full) {
72 if (!ct) {
73 printf("[0x%04lx] ",(unsigned long)full);
74 }
75 printf("%02x", *cur);
76 ++ct;
77 if ( ct % 4 == 0) {
78 printf(" ");
79 }
80 if ( ct % max == 0) {
81 printf("\n");
82 ct = 0;
83 }
84 }
85 printf("\n");
86 }
87 #endif /*0*/
88
89 static void
90 free_loclists_context(Dwarf_Loclists_Context cx)
91 {
92 if (cx) {
93 free(cx->lc_offset_value_array);
94 cx->lc_offset_value_array = 0;
95 cx->lc_magic = 0;
96 free(cx);
97 }
98 }
99
100 /* Used in case of error reading the
101 loclists headers (not referring to Dwarf_Loc_Head_c
102 here), to clean up. The chain is of
103 Dwarf_Loclists_Context items */
104 static void
105 free_loclists_chain(Dwarf_Debug dbg, Dwarf_Chain head)
106 {
107 Dwarf_Chain cur = head;
108 Dwarf_Chain next = 0;
109
110 if (!head || IS_INVALID_DBG(dbg)) {
111 return;
112 }
113 for ( ;cur; cur = next) {
114 next = cur->ch_next;
115 if (cur->ch_item) {
116 Dwarf_Loclists_Context cx =
117 (Dwarf_Loclists_Context)cur->ch_item;
118 free_loclists_context(cx);
119 cur->ch_item = 0;
120 dwarf_dealloc(dbg,cur,DW_DLA_CHAIN);
121 }
122 }
123 }
124
125 static int
126 counted_loc_descr(Dwarf_Debug dbg,
127 Dwarf_Small *data,
128 Dwarf_Small *enddata,
129 Dwarf_Unsigned offset,
130 Dwarf_Unsigned *loc_ops_overall_size,
131 Dwarf_Unsigned *loc_ops_count_len,
132 Dwarf_Unsigned *loc_ops_len,
133 Dwarf_Small **opsdata,
134 Dwarf_Unsigned *opsoffset,
135 Dwarf_Error * error)
136 {
137 Dwarf_Unsigned ops_len = 0;
138 Dwarf_Unsigned leblen = 0;
139 DECODE_LEB128_UWORD_LEN_CK(data,ops_len,leblen,
140 dbg,error,enddata);
141 *loc_ops_count_len = leblen;
142 *loc_ops_overall_size = ops_len+leblen;
143 *loc_ops_len = ops_len;
144 *opsdata = data;
145 *opsoffset = offset +leblen;
146 return DW_DLV_OK;
147 }
148
149 /* See also read_single_rle_entry() for similar code */
150 static int
151 read_single_lle_entry(Dwarf_Debug dbg,
152 Dwarf_Small *data,
153 Dwarf_Unsigned dataoffset,
154 Dwarf_Small *enddata,
155 Dwarf_Half address_size,
156 unsigned *bytes_count_out,
157 unsigned *entry_kind,
158 Dwarf_Unsigned *entry_operand1,
159 Dwarf_Unsigned *entry_operand2,
160 Dwarf_Unsigned *opsblocksize, /* Just the expr data */
161 Dwarf_Unsigned *opsoffset, /* Just the expr ops data */
162 Dwarf_Small **ops, /* pointer to expr ops ops */
163 Dwarf_Error* error)
164 {
165 Dwarf_Unsigned count = 0;
166 Dwarf_Unsigned leblen = 0;
167 unsigned int code = 0;
168 Dwarf_Unsigned val1 = 0;
169 Dwarf_Unsigned val2 = 0;
170 Dwarf_Unsigned loc_ops_overall_size = 0;
171 Dwarf_Unsigned loc_ops_count_len = 0;
172 Dwarf_Unsigned loc_ops_len = 0;
173 Dwarf_Small *lopsdata = 0;
174 Dwarf_Unsigned lopsoffset = 0;
175 Dwarf_Small *startdata = 0;
176
177 /* Some of these have a Counted Location Description
178 in them. */
179 if (data >= enddata) {
180 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
181 "DW_DLE_LOCLISTS_ERROR: "
182 "An lle entry begins past the end of "
183 "its allowed space. Corrupt DWARF.");
184 return DW_DLV_ERROR;
185 }
186 startdata = data;
187 code = *data;
188 ++data;
189 ++count;
190 switch(code) {
191 case DW_LLE_end_of_list: break;
192 case DW_LLE_base_addressx:{
193 DECODE_LEB128_UWORD_LEN_CK(data,val1,leblen,
194 dbg,error,enddata);
195 count += leblen;
196 }
197 break;
198 case DW_LLE_startx_endx:
199 case DW_LLE_startx_length:
200 case DW_LLE_offset_pair: {
201 int res = 0;
202
203 DECODE_LEB128_UWORD_LEN_CK(data,val1,leblen,
204 dbg,error,enddata);
205 count += leblen;
206 DECODE_LEB128_UWORD_LEN_CK(data,val2,leblen,
207 dbg,error,enddata);
208 count += leblen;
209 res = counted_loc_descr(dbg,data,enddata,
210 dataoffset,
211 &loc_ops_overall_size,
212 &loc_ops_count_len,
213 &loc_ops_len,
214 &lopsdata,
215 &lopsoffset,
216 error);
217 if (res != DW_DLV_OK) {
218 return res;
219 }
220 count += loc_ops_overall_size;
221 data += loc_ops_overall_size;
222
223 }
224 break;
225 case DW_LLE_default_location: {
226 int res = 0;
227
228 res = counted_loc_descr(dbg,data,enddata,
229 dataoffset,
230 &loc_ops_overall_size,
231 &loc_ops_count_len,
232 &loc_ops_len,
233 &lopsdata,
234 &lopsoffset,
235 error);
236 if (res != DW_DLV_OK) {
237 return res;
238 }
239 data += loc_ops_overall_size;
240 count += loc_ops_overall_size;
241 }
242 break;
243 case DW_LLE_base_address: {
244 READ_UNALIGNED_CK(dbg,val1, Dwarf_Unsigned,
245 data,address_size,error,enddata);
246 data += address_size;
247 count += address_size;
248 }
249 break;
250 case DW_LLE_start_end: {
251 int res = 0;
252
253 READ_UNALIGNED_CK(dbg,val1, Dwarf_Unsigned,
254 data,address_size,error,enddata);
255 data += address_size;
256 count += address_size;
257 READ_UNALIGNED_CK(dbg,val2, Dwarf_Unsigned,
258 data,address_size,error,enddata);
259 data += address_size;
260 count += address_size;
261 res = counted_loc_descr(dbg,data,enddata,
262 dataoffset,
263 &loc_ops_overall_size,
264 &loc_ops_count_len,
265 &loc_ops_len,
266 &lopsdata,
267 &lopsoffset,
268 error);
269 if (res != DW_DLV_OK) {
270 return res;
271 }
272 count += loc_ops_overall_size;
273 data += loc_ops_overall_size;
274 }
275 break;
276 case DW_LLE_start_length: {
277 int res = 0;
278
279 READ_UNALIGNED_CK(dbg,val1, Dwarf_Unsigned,
280 data,address_size,error,enddata);
281 data += address_size;
282 count += address_size;
283 DECODE_LEB128_UWORD_LEN_CK(data,val2,leblen,
284 dbg,error,enddata);
285 count += (unsigned)leblen;
286 res = counted_loc_descr(dbg,data,enddata,
287 dataoffset,
288 &loc_ops_overall_size,
289 &loc_ops_count_len,
290 &loc_ops_len,
291 &lopsdata,
292 &lopsoffset,
293 error);
294 if (res != DW_DLV_OK) {
295 return res;
296 }
297 count += loc_ops_overall_size;
298 data += loc_ops_overall_size;
299 }
300 break;
301 default: {
302 if (error) {
303 dwarfstring m;
304
305 dwarfstring_constructor(&m);
306 dwarfstring_append_printf_u(&m,
307 "DW_DLE_LOCLISTS_ERROR: "
308 "The loclists entry at .debug_loclists"
309 " offset 0x%x" ,dataoffset);
310 dwarfstring_append_printf_u(&m,
311 " has code 0x%x which is unknown",code);
312 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
313 dwarfstring_string(&m));
314 dwarfstring_destructor(&m);
315 }
316 return DW_DLV_ERROR;
317 }
318 break;
319 }
320 {
321 /* We want to avoid overflow in additions, and
322 the overall section size is a reasonable check
323 on count. The sequence of tests is to
324 preserve a testing baseline:
325 baselines/hongg2024-02-18-m.base
326 otherwise we would test against sectionsize first.*/
327 Dwarf_Unsigned section_size = dbg->de_debug_loclists.dss_size;
328
329 if (data > enddata || data < startdata ) {
330 /* Corrupt data being read. */
331 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
332 "DW_DLE_LOCLISTS_ERROR: "
333 "The end of an lle entry is past the end "
334 "of its allowed space");
335 return DW_DLV_ERROR;
336 }
337 if (count > section_size) {
338 /* Corrupt data being read. */
339 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
340 "DW_DLE_LOCLISTS_ERROR: "
341 "The number of bytes in a single "
342 "loclist entry is "
343 "too large to be reasonable");
344 return DW_DLV_ERROR;
345 }
346 }
347 *bytes_count_out = (unsigned)count;
348 *entry_kind = (unsigned)code;
349 *entry_operand1 = val1;
350 *entry_operand2 = val2;
351 *opsblocksize = loc_ops_len;
352 *opsoffset = lopsoffset;
353 *ops = lopsdata;
354 return DW_DLV_OK;
355 }
356
357 /* Reads the header. Determines the
358 various offsets, including offset
359 of the next header. Does no memory
360 allocations here. */
361 int
362 _dwarf_internal_read_loclists_header(Dwarf_Debug dbg,
363 Dwarf_Bool build_offset_array,
364 Dwarf_Unsigned contextnum,
365 Dwarf_Unsigned sectionlength,
366 Dwarf_Small *data,
367 Dwarf_Small *end_data,
368 Dwarf_Unsigned offset,
369 Dwarf_Loclists_Context buildhere,
370 Dwarf_Unsigned *next_offset,
371 Dwarf_Error *error)
372 {
373 Dwarf_Small *startdata = data;
374 Dwarf_Unsigned arealen = 0;
375 int offset_size = 0;
376 int exten_size = 0;
377 Dwarf_Unsigned version = 0;
378 unsigned address_size = 0;
379 unsigned segment_selector_size= 0;
380 Dwarf_Unsigned offset_entry_count = 0;
381 Dwarf_Unsigned localoff = 0;
382 Dwarf_Unsigned lists_len = 0;
383
384 READ_AREA_LENGTH_CK(dbg,arealen,Dwarf_Unsigned,
385 data,offset_size,exten_size,
386 error,
387 sectionlength,end_data);
388 if (arealen > sectionlength ||
389 (arealen+offset_size+exten_size) > sectionlength) {
390 dwarfstring m;
391 dwarfstring_constructor(&m);
392 dwarfstring_append_printf_u(&m,
393 "DW_DLE_SECTION_SIZE_ERROR: A .debug_loclists "
394 "area size of 0x%x ",arealen);
395 dwarfstring_append_printf_u(&m,
396 "at offset 0x%x ",offset);
397 dwarfstring_append_printf_u(&m,
398 "is larger than the entire section size of "
399 "0x%x. Corrupt DWARF.",sectionlength);
400 _dwarf_error_string(dbg,error,DW_DLE_SECTION_SIZE_ERROR,
401 dwarfstring_string(&m));
402 dwarfstring_destructor(&m);
403 return DW_DLV_ERROR;
404 }
405
406 buildhere->lc_length = arealen +offset_size+exten_size;
407 buildhere->lc_dbg = dbg;
408 buildhere->lc_index = contextnum;
409 buildhere->lc_header_offset = offset;
410 buildhere->lc_offset_size = offset_size;
411 buildhere->lc_extension_size = exten_size;
412 READ_UNALIGNED_CK(dbg,version,Dwarf_Unsigned,data,
413 SIZEOFT16,error,end_data);
414 if (version != DW_CU_VERSION5) {
415 dwarfstring m;
416 dwarfstring_constructor(&m);
417 dwarfstring_append_printf_u(&m,
418 "DW_DLE_VERSION_STAMP_ERROR: The version should be 5 "
419 "but we find %u instead.",version);
420 _dwarf_error_string(dbg,error,DW_DLE_VERSION_STAMP_ERROR,
421 dwarfstring_string(&m));
422 dwarfstring_destructor(&m);
423 return DW_DLV_ERROR;
424 }
425 buildhere->lc_version = (Dwarf_Half)version;
426 data += SIZEOFT16;
427
428 READ_UNALIGNED_CK(dbg,address_size,unsigned,data,
429 SIZEOFT8,error,end_data);
430 if (address_size != 4 && address_size != 8 &&
431 address_size != 2) {
432 dwarfstring m;
433 dwarfstring_constructor(&m);
434 dwarfstring_append_printf_u(&m,
435 " DW_DLE_ADDRESS_SIZE_ERROR: The address size "
436 "of %u is not supported.",address_size);
437 _dwarf_error_string(dbg,error,DW_DLE_ADDRESS_SIZE_ERROR,
438 dwarfstring_string(&m));
439 dwarfstring_destructor(&m);
440 return DW_DLV_ERROR;
441 }
442 buildhere->lc_address_size = address_size;
443 data++;
444
445 READ_UNALIGNED_CK(dbg,segment_selector_size,unsigned,data,
446 SIZEOFT8,error,end_data);
447 buildhere->lc_segment_selector_size = segment_selector_size;
448 if (buildhere->lc_segment_selector_size) {
449 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
450 "DW_DLE_LOCLISTS_ERROR: "
451 " The debug_loclists segment selector size is non-zero. "
452 "Not supported.");
453 return DW_DLV_ERROR;
454 }
455 data++;
456
457 READ_UNALIGNED_CK(dbg,offset_entry_count,Dwarf_Unsigned,data,
458 SIZEOFT32,error,end_data);
459 buildhere->lc_offset_entry_count = offset_entry_count;
460 data += SIZEOFT32;
461 if (offset_entry_count >= (sectionlength/offset_size)) {
462 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
463 "dbg,DW_DLE_LOCLISTS_ERROR: "
464 " The debug_loclists offset table entry "
465 "count exceeds the "
466 "section length, Corrupt dwarf");
467 return DW_DLV_ERROR;
468 }
469 localoff = data - startdata;
470 if (offset_entry_count) {
471 buildhere->lc_offsets_array = data;
472 lists_len += offset_size*offset_entry_count;
473 if (build_offset_array) {
474 Dwarf_Unsigned tabentrynum = 0;
475
476 buildhere->lc_offset_value_array = (Dwarf_Unsigned *)
477 calloc(offset_entry_count, sizeof(Dwarf_Unsigned));
478 if (!buildhere->lc_offset_value_array) {
479 _dwarf_error_string(dbg,error, DW_DLE_ALLOC_FAIL,
480 "dbg,DW_DLE_ALLOC_CAIL: "
481 " The debug_loclists offset table "
482 "cannot be allocated.");
483 return DW_DLV_ERROR;
484 }
485 for (tabentrynum = 0 ; tabentrynum < offset_entry_count;
486 data += offset_size,++tabentrynum ) {
487 Dwarf_Unsigned entry = 0;
488 int res = 0;
489
490 res = _dwarf_read_unaligned_ck_wrapper(dbg,
491 &entry,data,offset_size,end_data,error);
492 if (res != DW_DLV_OK) {
493 free(buildhere->lc_offset_value_array);
494 buildhere->lc_offset_value_array = 0;
495 return res;
496 }
497 buildhere->lc_offset_value_array[tabentrynum] =
498 entry;
499 }
500 }
501 } /* else no offset table */
502
503 buildhere->lc_offsets_off_in_sect = offset+localoff;
504 buildhere->lc_first_loclist_offset =
505 buildhere->lc_offsets_off_in_sect + lists_len;
506 buildhere->lc_loclists_header = startdata;
507 buildhere->lc_endaddr = startdata + buildhere->lc_length;
508 buildhere->lc_past_last_loclist_offset =
509 buildhere->lc_header_offset + buildhere->lc_length;
510 *next_offset = buildhere->lc_past_last_loclist_offset;
511 return DW_DLV_OK;
512 }
513
514 /* We return a pointer to an array of contexts
515 (not context pointers through *cxt if
516 we succeed and are returning DW_DLV_OK.
517 We never return DW_DLV_NO_ENTRY here. */
518 static int
519 internal_load_loclists_contexts(Dwarf_Debug dbg,
520 Dwarf_Loclists_Context **cxt,
521 Dwarf_Unsigned *count,
522 Dwarf_Error *error)
523 {
524 Dwarf_Unsigned offset = 0;
525 Dwarf_Unsigned nextoffset = 0;
526 Dwarf_Small * data = dbg->de_debug_loclists.dss_data;
527 Dwarf_Unsigned section_size = dbg->de_debug_loclists.dss_size;
528 Dwarf_Small * startdata = data;
529 Dwarf_Small * end_data = data +section_size;
530 Dwarf_Chain curr_chain = 0;
531 Dwarf_Chain head_chain = 0;
532 Dwarf_Chain *plast = &head_chain;
533 int res = 0;
534 Dwarf_Unsigned chainlength = 0;
535 Dwarf_Loclists_Context *fullarray = 0;
536 Dwarf_Unsigned i = 0;
537
538 for ( ; data < end_data ; data = startdata+nextoffset) {
539 Dwarf_Loclists_Context newcontext = 0;
540
541 /* sizeof the context struct, not sizeof a pointer */
542 newcontext = malloc(sizeof(*newcontext));
543 if (!newcontext) {
544 free_loclists_chain(dbg,head_chain);
545 _dwarf_error_string(dbg,error,
546 DW_DLE_ALLOC_FAIL,
547 "DW_DLE_ALLOC_FAIL: Allocation of "
548 "Loclists_Context failed");
549 return DW_DLV_ERROR;
550 }
551 memset(newcontext,0,sizeof(*newcontext));
552 res = _dwarf_internal_read_loclists_header(dbg, TRUE,
553 chainlength,
554 section_size,
555 data,end_data,offset,
556 newcontext,&nextoffset,error);
557 if (res == DW_DLV_ERROR) {
558 free_loclists_chain(dbg,head_chain);
559 free_loclists_context(newcontext);
560 newcontext = 0;
561 return DW_DLV_ERROR;
562 }
563 newcontext->lc_magic = LOCLISTS_MAGIC;
564 curr_chain = (Dwarf_Chain)
565 _dwarf_get_alloc(dbg, DW_DLA_CHAIN, 1);
566 if (curr_chain == NULL) {
567 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
568 "DW_DLE_ALLOC_FAIL: allocating Loclists_Context"
569 " chain entry");
570 free_loclists_chain(dbg,head_chain);
571 free_loclists_context(newcontext);
572 newcontext = 0;
573 return DW_DLV_ERROR;
574 }
575 curr_chain->ch_item = newcontext;
576 ++chainlength;
577 (*plast) = curr_chain;
578 plast = &(curr_chain->ch_next);
579 offset = nextoffset;
580 }
581 fullarray= (Dwarf_Loclists_Context *)malloc(
582 chainlength *sizeof(Dwarf_Loclists_Context /*pointer*/));
583 if (!fullarray) {
584 free_loclists_chain(dbg,head_chain);
585 _dwarf_error_string(dbg,error,
586 DW_DLE_ALLOC_FAIL,"Allocation of "
587 "Loclists_Context pointer array failed");
588 return DW_DLV_ERROR;
589 }
590 curr_chain = head_chain;
591 for (i = 0; i < chainlength; ++i) {
592 Dwarf_Chain prev = 0;
593 fullarray[i] = (Dwarf_Loclists_Context)curr_chain->ch_item;
594 curr_chain->ch_item = 0;
595 prev = curr_chain;
596 curr_chain = curr_chain->ch_next;
597 dwarf_dealloc(dbg, prev, DW_DLA_CHAIN);
598 }
599 /* ASSERT: the chain is entirely alloc'd
600 and the array of pointers points to
601 individually malloc'd Dwarf_Loclists_Context_s */
602 *cxt = fullarray;
603 *count = chainlength;
604 return DW_DLV_OK;
605 }
606
607 /* Used by dwarfdump to print raw loclists data.
608 Loads all the .debug_loclists[.dwo] headers and
609 returns DW_DLV_NO_ENTRY if the section
610 is missing or empty.
611 Intended to be done quite early and
612 done exactly once.
613 Harmless to do more than once.
614 With DW_DLV_OK it returns the number of
615 loclists headers in the section through
616 loclists_count. */
617 int
618 dwarf_load_loclists(Dwarf_Debug dbg,
619 Dwarf_Unsigned *loclists_count,
620 Dwarf_Error *error)
621 {
622 int res = DW_DLV_ERROR;
623 Dwarf_Loclists_Context *cxt = 0;
624 Dwarf_Unsigned count = 0;
625
626 CHECK_DBG(dbg,error,"dwarf_load_loclists()");
627 if (dbg->de_loclists_context) {
628 if (loclists_count) {
629 *loclists_count = dbg->de_loclists_context_count;
630 }
631 return DW_DLV_OK;
632 }
633 if (!dbg->de_debug_loclists.dss_size) {
634 /* nothing there. */
635 return DW_DLV_NO_ENTRY;
636 }
637 if (!dbg->de_debug_loclists.dss_data) {
638 res = _dwarf_load_section(dbg, &dbg->de_debug_loclists,
639 error);
640 if (res != DW_DLV_OK) {
641 return res;
642 }
643 }
644 /* cxt is set to a pointer to a context, which is
645 actually an array of pointers to such contexts, 'count'
646 of them. Each context has the loclists offset table
647 (if it has at least on entry in the table)
648 pointed in each cxt. */
649 res = internal_load_loclists_contexts(dbg,&cxt,&count,error);
650 if (res == DW_DLV_ERROR) {
651 return res;
652 }
653 dbg->de_loclists_context = cxt;
654 dbg->de_loclists_context_count = count;
655 if (loclists_count) {
656 *loclists_count = count;
657 }
658 return DW_DLV_OK;
659 }
660
661 /* Frees the memory in use in all loclists contexts.
662 Done by dwarf_finish() */
663 void
664 _dwarf_dealloc_loclists_context(Dwarf_Debug dbg)
665 {
666 Dwarf_Unsigned i = 0;
667 Dwarf_Loclists_Context * loccon = 0;
668
669 if (!dbg->de_loclists_context) {
670 return;
671 }
672 loccon = dbg->de_loclists_context;
673 for ( ; i < dbg->de_loclists_context_count; ++i) {
674 Dwarf_Loclists_Context con = loccon[i];
675 free_loclists_context(con);
676 loccon[i] = 0;
677 }
678 free(dbg->de_loclists_context);
679 dbg->de_loclists_context = 0;
680 dbg->de_loclists_context_count = 0;
681 }
682
683 /* Used by dwarfdump to print raw loclists data. */
684 int
685 dwarf_get_loclist_offset_index_value(Dwarf_Debug dbg,
686 Dwarf_Unsigned context_index,
687 Dwarf_Unsigned offsetentry_index,
688 Dwarf_Unsigned * offset_value_out,
689 Dwarf_Unsigned * global_offset_value_out,
690 Dwarf_Error *error)
691 {
692 Dwarf_Loclists_Context con = 0;
693 unsigned offset_len = 0;
694 Dwarf_Small *offsetptr = 0;
695 Dwarf_Unsigned targetoffset = 0;
696
697 CHECK_DBG(dbg,error,"dwarf_get_loclist_offset_index_value()");
698 if (!dbg->de_loclists_context_count) {
699 }
700 if (context_index >= dbg->de_loclists_context_count) {
701 return DW_DLV_NO_ENTRY;
702 }
703 con = dbg->de_loclists_context[context_index];
704 if (!con || con->lc_magic != LOCLISTS_MAGIC) {
705 _dwarf_error_string(dbg, error,DW_DLE_DBG_NULL,
706 "DW_DLE_DBG_NULL"
707 "context NULL or not LOCLISTS_MAGIC "
708 "found in "
709 "dwarf_get_loclist_offset_index_value()");
710 return DW_DLV_ERROR;
711 }
712
713 if (offsetentry_index >= con->lc_offset_entry_count) {
714 return DW_DLV_NO_ENTRY;
715 }
716 offset_len = con->lc_offset_size;
717 offsetptr = con->lc_offsets_array +
718 (offsetentry_index*offset_len);
719 READ_UNALIGNED_CK(dbg,targetoffset,Dwarf_Unsigned,
720 offsetptr,
721 offset_len,error,con->lc_endaddr);
722 if (targetoffset >= con->lc_length) {
723 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
724 "DW_DLE_LOCLISTS_ERROR: "
725 "An lle target offset value is "
726 "Too large to be real");
727 return DW_DLV_ERROR;
728 }
729
730 if (offset_value_out) {
731 *offset_value_out = targetoffset;
732 }
733 if (global_offset_value_out) {
734 *global_offset_value_out = targetoffset +
735 con->lc_offsets_off_in_sect;
736 }
737 return DW_DLV_OK;
738 }
739
740 /* Used by dwarfdump to print basic data from the
741 data generated to look at a specific loclist
742 as returned by dwarf_loclists_index_get_rle_head()
743 or dwarf_loclists_offset_get_rle_head. */
744 int dwarf_get_loclist_head_basics(Dwarf_Loc_Head_c head,
745 Dwarf_Small * lkind,
746 Dwarf_Unsigned * lle_count,
747 Dwarf_Unsigned * lle_version,
748 Dwarf_Unsigned * loclists_index_returned,
749 Dwarf_Unsigned * bytes_total_in_lle,
750 Dwarf_Half * offset_size,
751 Dwarf_Half * address_size,
752 Dwarf_Half * segment_selector_size,
753 Dwarf_Unsigned * overall_offset_of_this_context,
754 Dwarf_Unsigned * total_length_of_this_context,
755 Dwarf_Unsigned * offset_table_offset,
756 Dwarf_Unsigned * offset_table_entrycount,
757 Dwarf_Bool * loclists_base_present,
758 Dwarf_Unsigned * loclists_base,
759 Dwarf_Bool * loclists_base_address_present,
760 Dwarf_Unsigned * loclists_base_address,
761 Dwarf_Bool * loclists_debug_addr_base_present,
762 Dwarf_Unsigned * loclists_debug_addr_base,
763 Dwarf_Unsigned * loclists_offset_lle_set,
764 Dwarf_Error *error)
765 {
766 Dwarf_Loclists_Context loccontext = 0;
767
768 if (!head) {
769 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
770 "DW_DLE_DBG_NULL"
771 "NULL Dwarf_Loc_Head_c "
772 "argument passed to "
773 "dwarf_get_loclist_head_basics()");
774 return DW_DLV_ERROR;
775 }
776 if (head->ll_magic != LOCLISTS_MAGIC) {
777 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
778 "DW_DLE_DBG_NULL"
779 "NULL Dwarf_Loc_Head_c "
780 " not set with LOCLISTS_MAGIC in "
781 "argument passed to "
782 "dwarf_get_loclist_head_basics()");
783 return DW_DLV_ERROR;
784 }
785 *lkind = (Dwarf_Small)head->ll_lkind;
786 *lle_count = head->ll_locdesc_count;
787 *lle_version = head->ll_cuversion;
788 *loclists_index_returned = head->ll_index;
789 *bytes_total_in_lle = head->ll_bytes_total;
790 *offset_size = head->ll_offset_size;
791 *address_size = head->ll_address_size;
792 *segment_selector_size = head->ll_segment_selector_size;
793 /* If a dwarf_expression, no ll_loccontext */
794 loccontext = head->ll_localcontext;
795 if (loccontext) {
796 *overall_offset_of_this_context =
797 loccontext->lc_header_offset;
798 *total_length_of_this_context = loccontext->lc_length;
799 *offset_table_offset = loccontext->lc_offsets_off_in_sect;
800 *offset_table_entrycount = loccontext->lc_offset_entry_count;
801 }
802 *loclists_base_present = head->ll_at_loclists_base_present;
803 *loclists_base= head->ll_at_loclists_base;
804
805 *loclists_base_address_present = head->ll_cu_base_address_present;
806 *loclists_base_address= head->ll_cu_base_address;
807
808 *loclists_debug_addr_base_present =
809 head->ll_cu_addr_base_offset_present;
810 *loclists_debug_addr_base = head->ll_cu_addr_base_offset;
811 *loclists_offset_lle_set = head->ll_llearea_offset;
812 return DW_DLV_OK;
813 }
814
815 /* Used by dwarfdump to print raw loclists data.
816 Enables printing of details about the Range List Table
817 Headers, one header per call. Index starting at 0.
818 Returns DW_DLV_NO_ENTRY if index is too high for the table.
819 A .debug_loclists section may contain any number
820 of Range List Table Headers with their details. */
821 int
822 dwarf_get_loclist_context_basics(Dwarf_Debug dbg,
823 Dwarf_Unsigned context_index,
824 Dwarf_Unsigned * header_offset,
825 Dwarf_Small * offset_size,
826 Dwarf_Small * extension_size,
827 unsigned * version, /* 5 */
828 Dwarf_Small * address_size,
829 Dwarf_Small * segment_selector_size,
830 Dwarf_Unsigned * offset_entry_count,
831 Dwarf_Unsigned * offset_of_offset_array,
832 Dwarf_Unsigned * offset_of_first_loclistentry,
833 Dwarf_Unsigned * offset_past_last_loclistentry,
834 Dwarf_Error *error)
835 {
836 Dwarf_Loclists_Context con = 0;
837
838 CHECK_DBG(dbg,error,"dwarf_get_loclist_context_basics()");
839 if (!dbg->de_loclists_context_count) {
840 return DW_DLV_NO_ENTRY;
841 }
842 if (context_index >= dbg->de_loclists_context_count) {
843 return DW_DLV_NO_ENTRY;
844 }
845 con = dbg->de_loclists_context[context_index];
846 if (!con || con->lc_magic != LOCLISTS_MAGIC) {
847 _dwarf_error_string(dbg, error,DW_DLE_DBG_NULL,
848 "DW_DLE_DBG_NULL"
849 "context NULL or not LOCLISTS_MAGIC "
850 "found in "
851 "dwarf_get_loclist_context_basics()");
852 return DW_DLV_ERROR;
853 }
854 if (header_offset) {
855 *header_offset = con->lc_header_offset;
856 }
857 if (offset_size) {
858 *offset_size = con->lc_offset_size;
859 }
860 if (extension_size) {
861 *extension_size = con->lc_extension_size;
862 }
863 if (version) {
864 *version = con->lc_version;
865 }
866 if (address_size) {
867 *address_size = con->lc_address_size;
868 }
869 if (segment_selector_size) {
870 *segment_selector_size = con->lc_segment_selector_size;
871 }
872 if (offset_entry_count) {
873 *offset_entry_count = con->lc_offset_entry_count;
874 }
875 if (offset_of_offset_array) {
876 *offset_of_offset_array = con->lc_offsets_off_in_sect;
877 }
878 if (offset_of_first_loclistentry) {
879 *offset_of_first_loclistentry = con->lc_first_loclist_offset;
880 }
881 if (offset_past_last_loclistentry) {
882 *offset_past_last_loclistentry =
883 con->lc_past_last_loclist_offset;
884 }
885 return DW_DLV_OK;
886 }
887
888 /* Used by dwarfdump to print raw loclists data.
889 entry offset is offset_of_first_loclistentry.
890 Stop when the returned *next_entry_offset
891 is == offset_past_last_loclistentry (from
892 dwarf_get_loclist_context_plus).
893 This only makes sense within those loclists
894 This retrieves raw detail from the section,
895 no base values or anything are added.
896 So this returns raw individual entries
897 for a single loclist header, meaning a
898 a single Dwarf_Loclists_Context. */
899 int dwarf_get_loclist_lle(Dwarf_Debug dbg,
900 Dwarf_Unsigned contextnumber,
901 Dwarf_Unsigned entry_offset,
902 Dwarf_Unsigned endoffset,
903 unsigned *entrylen,
904 unsigned *entry_kind,
905 Dwarf_Unsigned *entry_operand1,
906 Dwarf_Unsigned *entry_operand2,
907 Dwarf_Unsigned *expr_ops_blocksize,
908 Dwarf_Unsigned *expr_ops_offset,
909 Dwarf_Small **expr_opsdata,
910 Dwarf_Error *error)
911 {
912 Dwarf_Loclists_Context con = 0;
913 Dwarf_Small *data = 0;
914 Dwarf_Small *enddata = 0;
915 int res = 0;
916 Dwarf_Half address_size = 0;
917
918 CHECK_DBG(dbg,error,"dwarf_get_loclist_lle()");
919 if (!dbg->de_loclists_context_count) {
920 return DW_DLV_NO_ENTRY;
921 }
922 data = dbg->de_debug_loclists.dss_data +
923 entry_offset;
924 enddata = dbg->de_debug_loclists.dss_data +
925 endoffset;
926 if (contextnumber >= dbg->de_loclists_context_count) {
927 return DW_DLV_NO_ENTRY;
928 }
929 con = dbg->de_loclists_context[contextnumber];
930 if (!con || con->lc_magic != LOCLISTS_MAGIC) {
931 _dwarf_error_string(dbg, error,DW_DLE_DBG_NULL,
932 "DW_DLE_DBG_NULL"
933 "context NULL or not LOCLISTS_MAGIC "
934 "found in "
935 "dwarf_get_loclist_lle()");
936 return DW_DLV_ERROR;
937 }
938 address_size = con->lc_address_size;
939 res = read_single_lle_entry(dbg,
940 data,entry_offset,enddata,
941 address_size, entrylen,
942 entry_kind, entry_operand1, entry_operand2,
943 expr_ops_blocksize,
944 expr_ops_offset,
945 expr_opsdata,
946 error);
947 return res;
948 }
949
950 /* We have more data than we need here if the compiler
951 provided a loclists_base_address or provided
952 a DW_SECT_LOCLISTS Per_CU value set for loclists.
953 Or we plucked an offset from .debug_addr. */
954 static int
955 _dwarf_which_loclists_context(Dwarf_Debug dbg,
956 Dwarf_CU_Context ctx,
957 Dwarf_Unsigned loclist_offset /* Not always set */,
958 Dwarf_Unsigned *index,
959 Dwarf_Error *error)
960 {
961 Dwarf_Unsigned count = 0;
962 Dwarf_Loclists_Context *array = 0;
963 Dwarf_Unsigned i = 0;
964 Dwarf_Loclists_Context rcx = 0;
965 Dwarf_Unsigned rcxoff = 0;
966 Dwarf_Unsigned rcxend = 0;
967 Dwarf_Unsigned loclists_base = 0;
968 Dwarf_Bool loclists_base_present = FALSE;
969 int res = 0;
970 Dwarf_Bool found_base = FALSE;
971 Dwarf_Unsigned chosen_offset = 0;
972
973 array = dbg->de_loclists_context;
974 count = dbg->de_loclists_context_count;
975 if (!array) {
976 return DW_DLV_NO_ENTRY;
977 }
978 if (count == 1) {
979 *index = 0;
980 return DW_DLV_OK;
981 }
982
983 if (ctx->cc_loclists_base_present) {
984 loclists_base_present = ctx->cc_loclists_base_present;
985 loclists_base = ctx->cc_loclists_base;
986 found_base = TRUE;
987 chosen_offset = loclists_base;
988 }
989 if (!found_base) {
990 res = _dwarf_has_SECT_fission(ctx,
991 DW_SECT_LOCLISTS,
992 &loclists_base_present,&loclists_base);
993 if (res == DW_DLV_OK) {
994 found_base = TRUE;
995 chosen_offset = loclists_base;
996 }
997 }
998 if (!found_base) {
999 loclists_base = loclist_offset;
1000 chosen_offset = loclist_offset;
1001 }
1002
1003 rcx = array[i];
1004 rcxoff = rcx->lc_header_offset;
1005 rcxend = rcxoff + rcx->lc_length;
1006 /* We look at the location of each loclist context
1007 to find one with the offset we want */
1008 for ( i = 0 ; i < count; ++i) {
1009 rcx = array[i];
1010 rcxoff = rcx->lc_header_offset;
1011 rcxend = rcxoff +
1012 rcx->lc_length;
1013 rcxend = rcxoff +
1014 rcx->lc_length;
1015 if (chosen_offset < rcxoff){
1016 continue;
1017 }
1018 if (chosen_offset < rcxend ){
1019 *index = i;
1020 return DW_DLV_OK;
1021 }
1022 }
1023 {
1024 dwarfstring m;
1025
1026 dwarfstring_constructor(&m);
1027 dwarfstring_append_printf_u(&m,
1028 "DW_DLE_LOCLISTS_ERROR: loclist ran off end "
1029 " finding target offset of"
1030 " 0x%" DW_PR_XZEROS DW_PR_DUx ,chosen_offset);
1031 dwarfstring_append(&m,
1032 " Not found anywhere in .debug_loclists[.dwo] "
1033 "data. Corrupted data?");
1034 _dwarf_error_string(dbg,error,
1035 DW_DLE_LOCLISTS_ERROR,
1036 dwarfstring_string(&m));
1037 dwarfstring_destructor(&m);
1038 }
1039 return DW_DLV_ERROR;
1040 }
1041
1042 /* Caller will eventually free as appropriate. */
1043 static int
1044 alloc_rle_and_append_to_list(Dwarf_Debug dbg,
1045 Dwarf_Loc_Head_c rctx,
1046 Dwarf_Locdesc_c *e_out,
1047 Dwarf_Error *error)
1048 {
1049 Dwarf_Locdesc_c e = 0;
1050
1051 e = malloc(sizeof(struct Dwarf_Locdesc_c_s));
1052 if (!e) {
1053 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
1054 "DW_DLE_ALLOC_FAIL: Out of memory in "
1055 "building list of loclists entries on a DIE.");
1056 return DW_DLV_ERROR;
1057 }
1058 memset(e,0,sizeof(struct Dwarf_Locdesc_c_s));
1059 _dwarf_locdesc_c_constructor(dbg,e);
1060 e->ld_magic = LOCLISTS_MAGIC;
1061 if (rctx->ll_first) {
1062 rctx->ll_last->ld_next = e;
1063 rctx->ll_last = e;
1064 } else {
1065 rctx->ll_first = e;
1066 rctx->ll_last = e;
1067 }
1068 rctx->ll_locdesc_count++;
1069 *e_out = e;
1070 return DW_DLV_OK;
1071 }
1072
1073 /* Read the group of loclists entries, and
1074 finally build an array of Dwarf_Locdesc_c
1075 records. Attach to rctx here.
1076 Since on error the caller will destruct the rctx
1077 and we ensure to attach allocations there
1078 the caller will destruct the allocations here
1079 in case we return DW_DLV_ERROR*/
1080 static int
1081 build_array_of_lle(Dwarf_Debug dbg,
1082 Dwarf_Loc_Head_c rctx,
1083 Dwarf_Error *error)
1084 {
1085 int res = 0;
1086 Dwarf_Small *data = rctx->ll_llepointer;
1087 Dwarf_Unsigned dataoffset = rctx->ll_llearea_offset;
1088 Dwarf_Small *enddata = rctx->ll_end_data_area;
1089 unsigned int offset_size = rctx->ll_offset_size;
1090 unsigned int address_size = rctx->ll_address_size;
1091 Dwarf_Unsigned bytescounttotal= 0;
1092 int done = FALSE;
1093 Dwarf_Unsigned i = 0;
1094
1095 for ( ; !done ; ) {
1096 unsigned int entrylen = 0;
1097 unsigned int code = 0;
1098 Dwarf_Unsigned val1 = 0;
1099 Dwarf_Unsigned val2 = 0;
1100 Dwarf_Locdesc_c e = 0;
1101 Dwarf_Unsigned opsblocksize = 0;
1102 Dwarf_Unsigned opsoffset = 0;
1103 Dwarf_Small *ops = 0;
1104 Dwarf_Block_c eops;
1105
1106 memset(&eops,0,sizeof(eops));
1107 res = read_single_lle_entry(dbg,
1108 data,dataoffset, enddata,
1109 address_size,&entrylen,
1110 &code,&val1, &val2,
1111 &opsblocksize,&opsoffset,&ops,
1112 error);
1113 if (res != DW_DLV_OK) {
1114 return res;
1115 }
1116 res = alloc_rle_and_append_to_list(dbg,rctx,&e,error);
1117 if (res != DW_DLV_OK) {
1118 return res;
1119 }
1120 eops.bl_len =opsblocksize;
1121 eops.bl_data = ops;
1122 eops.bl_kind = rctx->ll_lkind;
1123 eops.bl_section_offset = opsoffset;
1124 eops.bl_locdesc_offset = dataoffset;
1125 e->ld_lkind = rctx->ll_lkind;
1126 e->ld_magic = LOCLISTS_MAGIC;
1127 e->ld_lle_value = code,
1128 e->ld_entrylen = entrylen;
1129 e->ld_rawlow = val1;
1130 e->ld_rawhigh = val2;
1131 e->ld_opsblock = eops;
1132 e->ld_lle_bytecount = entrylen;
1133 data += entrylen;
1134 bytescounttotal += entrylen;
1135 if (code == DW_LLE_end_of_list) {
1136 /* This terminates the entries for a loclist portion.*/
1137 done = TRUE;
1138 break;
1139 }
1140 }
1141 if (rctx->ll_locdesc_count > 0) {
1142 Dwarf_Locdesc_c array = 0;
1143 Dwarf_Locdesc_c cur = 0;
1144 Dwarf_Locdesc_c prev = 0;
1145
1146 /* array of structs. Here we copy the previous
1147 malloc set of Dwarf_Locdesc_c into
1148 a dwarf_get_alloc set and free the malloc set */
1149
1150 array = (Dwarf_Locdesc_c)_dwarf_get_alloc(dbg,
1151 DW_DLA_LOCDESC_C, rctx->ll_locdesc_count);
1152 if (!array) {
1153 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
1154 "DW_DLE_ALLOC_FAIL: Out of memory in "
1155 "copying list of locdescs into array ");
1156 return DW_DLV_ERROR;
1157 }
1158 rctx->ll_locdesc = array;
1159 cur = rctx->ll_first;
1160 for (i = 0 ; i < rctx->ll_locdesc_count; ++i) {
1161 prev = cur;
1162 array[i] = *cur;
1163 cur = cur->ld_next;
1164 free(prev);
1165 }
1166 rctx->ll_first = 0;
1167 rctx->ll_last = 0;
1168 }
1169 for (i = 0; i < rctx->ll_locdesc_count; ++i) {
1170 Dwarf_Locdesc_c ldc = rctx->ll_locdesc + i;
1171
1172 res = _dwarf_fill_in_locdesc_op_c(dbg,
1173 i,
1174 rctx,
1175 &ldc->ld_opsblock,
1176 address_size, offset_size,
1177 rctx->ll_cuversion,
1178 ldc->ld_rawlow, ldc->ld_rawhigh,
1179 ldc->ld_lle_value,
1180 error);
1181 if (res != DW_DLV_OK) {
1182 return res;
1183 }
1184 }
1185 rctx->ll_bytes_total = bytescounttotal;
1186 return DW_DLV_OK;
1187 }
1188
1189 /* Build a head with all the relevent Entries
1190 attached, all the locdescs and for each such,
1191 all its expression operators.
1192 */
1193 int
1194 _dwarf_loclists_fill_in_lle_head(Dwarf_Debug dbg,
1195 Dwarf_Attribute attr,
1196 Dwarf_Half theform,
1197 Dwarf_Unsigned attr_val,
1198 Dwarf_Loc_Head_c llhead,
1199 Dwarf_Error *error)
1200 {
1201 int res = 0;
1202 Dwarf_Unsigned loclists_contextnum = 0;
1203 Dwarf_Small *table_base = 0;
1204 Dwarf_Small *table_entry = 0;
1205 Dwarf_Small *enddata = 0;
1206 Dwarf_Loclists_Context *array = 0;
1207 Dwarf_Loclists_Context rctx = 0;
1208 Dwarf_Unsigned entrycount = 0;
1209 unsigned offsetsize = 0;
1210 Dwarf_Unsigned lle_global_offset = 0;
1211 Dwarf_CU_Context ctx = 0;
1212 Dwarf_Unsigned offset_in_loclists = 0;
1213 Dwarf_Bool is_loclistx = FALSE;
1214 Dwarf_Unsigned secsize = 0;
1215
1216 if (theform == DW_FORM_loclistx) {
1217 is_loclistx = TRUE;
1218 } else {
1219 offset_in_loclists = attr_val;
1220 }
1221 if (!attr) {
1222 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1223 "DW_DLE_DBG_NULL "
1224 "NULL attribute "
1225 "argument passed to "
1226 "_dwarf_loclists_fill_in_lle_head()");
1227 return DW_DLV_ERROR;
1228 }
1229 secsize = dbg->de_debug_loclists.dss_size;
1230 ctx = attr->ar_cu_context;
1231 array = dbg->de_loclists_context;
1232 if (dbg != ctx->cc_dbg) {
1233 /* is_loclistx TRUE */
1234 /* Now find the correct secondary CU context via
1235 signature. */
1236 Dwarf_CU_Context lctx = 0;
1237
1238 if (ctx->cc_signature_present) {
1239 /* Looking in current dbg for the correct cu context */
1240 res = _dwarf_search_for_signature(dbg,
1241 ctx->cc_signature,
1242 &lctx,error);
1243 if (res != DW_DLV_OK) {
1244 if (res == DW_DLV_ERROR) {
1245 _dwarf_error_string(dbg,error, DW_DLE_LLE_ERROR,
1246 "DW_DLE_RLE_ERROR: a .debug_loclists "
1247 "cu context cannot be found with the "
1248 "correct signature");
1249 }
1250 return res;
1251 } else {
1252 ctx = lctx;
1253 }
1254 } else {
1255 /* No signature. Hopeless, I think. */
1256 _dwarf_error_string(dbg,error, DW_DLE_LLE_ERROR,
1257 "DW_DLE_LLE_ERROR: a .debug_loclists "
1258 "cu context cannot be found as there is "
1259 "no signature to use");
1260 return DW_DLV_ERROR;
1261 }
1262 }
1263 /* A */
1264 if (ctx->cc_loclists_base_present) {
1265 offset_in_loclists = ctx->cc_loclists_base;
1266 } else {
1267 offset_in_loclists = attr_val;
1268 }
1269
1270 if (offset_in_loclists >= secsize) {
1271 _dwarf_error_string(dbg,error, DW_DLE_LLE_ERROR,
1272 "DW_DLE_LLE_ERROR: a .debug_loclists offset "
1273 "is greater than the loclists section size");
1274 return DW_DLV_ERROR;
1275 }
1276 /* B */
1277 {
1278 res = _dwarf_which_loclists_context(dbg,ctx,
1279 offset_in_loclists,
1280 &loclists_contextnum,error);
1281 if (res == DW_DLV_OK) {
1282 /* FALL THROUGH */
1283 } else if (res == DW_DLV_NO_ENTRY) {
1284 loclists_contextnum = 0;
1285 /* FALL THROUGH */
1286 } else {
1287 return res;
1288 }
1289 }
1290
1291 /* C */
1292 rctx = array[loclists_contextnum];
1293 table_base = rctx->lc_offsets_array;
1294 entrycount = rctx->lc_offset_entry_count;
1295 offsetsize = rctx->lc_offset_size;
1296 enddata = rctx->lc_endaddr;
1297
1298 if (is_loclistx && attr_val >= entrycount) {
1299 dwarfstring m;
1300
1301 dwarfstring_constructor(&m);
1302 dwarfstring_append_printf_u(&m,
1303 "DW_DLE_LOCLISTS_ERROR: loclists table index of"
1304 " %u" ,attr_val);
1305 dwarfstring_append_printf_u(&m,
1306 " too large for table of %u "
1307 "entries.",entrycount);
1308 _dwarf_error_string(dbg,error,
1309 DW_DLE_LOCLISTS_ERROR,
1310 dwarfstring_string(&m));
1311 dwarfstring_destructor(&m);
1312 return DW_DLV_ERROR;
1313 }
1314 llhead->ll_localcontext = rctx;
1315 llhead->ll_index = loclists_contextnum;
1316 llhead->ll_cuversion = rctx->lc_version;
1317 llhead->ll_offset_size = offsetsize;
1318 llhead->ll_address_size = rctx->lc_address_size;
1319 llhead->ll_segment_selector_size =
1320 rctx->lc_segment_selector_size;
1321 /* D */
1322 if (is_loclistx) {
1323 Dwarf_Unsigned table_entryval = 0;
1324 Dwarf_Unsigned globaloff = 0;
1325
1326 table_entry = attr_val*offsetsize + table_base;
1327 /* No malloc here yet so no leak if the macro returns
1328 DW_DLV_ERROR */
1329 READ_UNALIGNED_CK(dbg,table_entryval, Dwarf_Unsigned,
1330 table_entry,offsetsize,error,enddata);
1331 if (table_entryval >= rctx->lc_length) {
1332 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
1333 "DW_DLE_LOCLISTS_ERROR: "
1334 "A DW_FORM_loclistx value is too large for "
1335 "the space in .debug_loclists[.dwo]");
1336 return DW_DLV_ERROR;
1337 }
1338 globaloff = rctx->lc_offsets_off_in_sect + table_entryval;
1339 lle_global_offset = globaloff;
1340 if (lle_global_offset >= secsize) {
1341 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
1342 "DW_DLE_LOCLISTS_ERROR: "
1343 "A DW_FORM_loclistx global offset is too large to "
1344 "the space in .debug_loclists[.dwo]");
1345 return DW_DLV_ERROR;
1346 }
1347 } else {
1348 lle_global_offset = attr_val;
1349 if (lle_global_offset >= secsize) {
1350 _dwarf_error_string(dbg,error,DW_DLE_LOCLISTS_ERROR,
1351 "DW_DLE_LOCLISTS_ERROR: "
1352 "A loclist global offset is too large for "
1353 "the space in .debug_loclists[.dwo]");
1354 return DW_DLV_ERROR;
1355 }
1356 }
1357
1358 /* E */
1359 llhead->ll_end_data_area = enddata;
1360 llhead->ll_cu_base_address_present =
1361 ctx->cc_base_address_present;
1362 llhead->ll_cu_base_address = ctx->cc_base_address;
1363 llhead->ll_dbg = dbg;
1364
1365 llhead->ll_llearea_offset = lle_global_offset;
1366 llhead->ll_llepointer = lle_global_offset +
1367 dbg->de_debug_loclists.dss_data;
1368
1369 res = build_array_of_lle(dbg,llhead,error);
1370 if (res != DW_DLV_OK) {
1371 return res;
1372 }
1373 return DW_DLV_OK;
1374 }
1375
1376 #if 0 /* candidate for public api?. No, not usable. */
1377 int
1378 dwarf_get_loclists_entry_fields(
1379 Dwarf_Loc_Head_c head,
1380 Dwarf_Unsigned entrynum,
1381 unsigned *entrylen,
1382 unsigned *code,
1383 Dwarf_Unsigned *raw1,
1384 Dwarf_Unsigned *raw2,
1385 Dwarf_Unsigned *cooked1,
1386 Dwarf_Unsigned *cooked2,
1387 Dwarf_Unsigned *lle_bytesize;
1388 /* FIXME not right for loclists or their loc exprs */
1389 Dwarf_Error *error)
1390 {
1391 Dwarf_Locdesc_c e = 0;
1392 if (!head || head->ll_magic != LOCLISTS_MAGIC) {
1393 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1394 "DW_DLE_DBG_NULL"
1395 "NULL Dwarf_Loc_Head_c "
1396 " not set with LOCLISTS_MAGIC in "
1397 "argument passed to "
1398 "dwarf_get_loclist_entry_fields()");
1399 return DW_DLV_ERROR;
1400 }
1401
1402 if (entrynum >= head->ll_locdesc_count) {
1403 return DW_DLV_NO_ENTRY;
1404 }
1405 e = head->ll_locdesc + entrynum;
1406 *entrylen = e->ld_entrylen;
1407 *code = e->ld_lle_value;
1408 *lle_bytesize = e->ld_lle_value /* bogus */;
1409 *raw1 = e->ld_rawlow;
1410 *raw2 = e->ld_rawhigh;
1411 *cooked1 = e->ld_lopc;
1412 *cooked2 = e->ld_highpc;
1413 return DW_DLV_OK;
1414 }
1415 #endif /*0*/
1416
1417 static void
1418 _dwarf_free_loclists_limited_head_content(Dwarf_Loc_Head_c head)
1419 {
1420 if (head->ll_first) {
1421 /* partially built head. */
1422 /* ASSERT: ll_loclists is NULL */
1423 Dwarf_Locdesc_c cur = head->ll_first;
1424 Dwarf_Locdesc_c next = 0;
1425
1426 for ( ; cur ; cur = next) {
1427 next = cur->ld_next;
1428 cur->ld_next = 0;
1429 cur->ld_magic = 0;
1430 free(cur);
1431 }
1432 head->ll_first = 0;
1433 head->ll_last = 0;
1434 head->ll_locdesc_count = 0;
1435 }
1436 }
1437 /* Deals with both fully and partially build head,
1438 called by user calls to clean up or by
1439 libdwarf to clean up on error. */
1440 void
1441 dwarf_dealloc_loc_head_c(Dwarf_Loc_Head_c head)
1442 {
1443 Dwarf_Debug dbg = 0;
1444
1445 if (!head) {
1446 return;
1447 }
1448 if (head->ll_magic != LOCLISTS_MAGIC) {
1449 return;
1450 }
1451 dbg = head->ll_dbg;
1452 if (IS_INVALID_DBG(dbg)) {
1453 return;
1454 }
1455 if (head->ll_first) {
1456 _dwarf_free_loclists_limited_head_content(head);
1457 }
1458 if (head->ll_locdesc) {
1459 Dwarf_Locdesc_c desc = head->ll_locdesc;
1460 /* ASSERT: ll_first and ll_last are NULL */
1461 /* fully built head. */
1462 Dwarf_Unsigned listlen = head->ll_locdesc_count;
1463 Dwarf_Unsigned i = 0;
1464
1465 /* It's an array of desc. */
1466 for ( ; i < listlen; ++i) {
1467 Dwarf_Loc_Expr_Op loc = desc[i].ld_s;
1468 if (loc) {
1469 dwarf_dealloc(dbg,loc,DW_DLA_LOC_BLOCK_C);
1470 desc[i].ld_s = 0;
1471 }
1472 }
1473 /* It is an array of structs,
1474 and the block in each is gone.
1475 It will be a simple single free. */
1476 dwarf_dealloc(dbg,head->ll_locdesc,DW_DLA_LOCDESC_C);
1477 head->ll_locdesc_count = 0;
1478 head->ll_locdesc = 0;
1479 }
1480 dwarf_dealloc(dbg,head,DW_DLA_LOC_HEAD_C);
1481 }
1482
1483 /* dwarf_alloc calls this on dealloc. head is freed there
1484 after this returns. */
1485 void
1486 _dwarf_loclists_head_destructor(void *head)
1487 {
1488 Dwarf_Loc_Head_c h = head;
1489
1490 _dwarf_free_loclists_limited_head_content(h);
1491 }