Debian Codegraph @

src/lib/libdwarf/dwarf_gnu_index.c

raw
1 /*
2 Copyright (C) 2020 David Anderson. All Rights Reserved.
3 Redistribution and use in source and binary forms, with
4 or without modification, are permitted provided that the
5 following conditions are met:
6
7 Redistributions of source code must retain the above
8 copyright notice, this list of conditions and the following
9 disclaimer.
10
11 Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials
14 provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
17 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 /* This is for accessing .debug_gnu_pubnames
32 and .debug_gnu_pubtypes.
33
34 These sections are designed to be compiler-generated
35 to be used by the linker to create the .gdb_index
36 section for DWARF3 and DWARF4 as part of
37 Split Dwarf (aka Debug Fission).
38 They are not expected to be in an executable. */
39
40 #include <config.h>
41
42 #include <stdlib.h> /* calloc() free() */
43 #include <stdio.h> /* debugging */
44
45 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
46 #include "stdafx.h"
47 #endif /* HAVE_STDAFX_H */
48
49 #include "dwarf.h"
50 #include "libdwarf.h"
51 #include "libdwarf_private.h"
52 #include "dwarf_base_types.h"
53 #include "dwarf_opaque.h"
54 #include "dwarf_alloc.h"
55 #include "dwarf_error.h"
56 #include "dwarf_util.h"
57 #include "dwarf_gnu_index.h"
58 #include "dwarf_string.h"
59
60 #if 0 /* dump_head */
61 static void
62 dump_head(const char *msg,int lno,
63 struct Dwarf_Gnu_Index_Head_s *h)
64 {
65 printf("HEAD block line %d\n",__LINE__);
66 printf("data : %p\n",
67 (void *)h->gi_section_data);
68 printf("sec len : %llu 0x%llx\n",
69 h->gi_section_length,
70 h->gi_section_length);
71 printf("data end : %p\n",
72 (void *)(h->gi_section_end));
73 printf("blockarray : %p\n",
74 (void *)h->gi_blockarray);
75 printf("block count : %llu 0x%llx\n",
76 h->gi_blockcount,
77 h->gi_blockcount);
78 printf("gnu_pubnames: %u\n",h->gi_is_pubnames);
79 }
80
81 static void
82 dump_block(const char *msg,int bn, int lno,
83 struct Dwarf_Gnu_IBlock_s *b)
84 {
85 printf("BLOCK dump block %d %s line %d\n",
86 bn,msg,lno);
87 printf("head : 0x%lx\n",
88 (unsigned long)b->ib_head);
89 printf("index : %lu\n",
90 (unsigned long)b->ib_index);
91 printf("blk len offset : %lu 0x%lx\n",
92 (unsigned long)b->ib_block_length_offset,
93 (unsigned long)b->ib_block_length_offset);
94 printf("block length : %lu 0x%lx\n",
95 (unsigned long)b->ib_block_length,
96 (unsigned long)b->ib_block_length);
97 printf("offset size : %u\n",
98 b->ib_offset_size);
99 printf("extension size : %u\n",
100 b->ib_extension_size);
101 printf("version : %u\n",
102 b->ib_version);
103 printf("built entries? : %s\n",
104 b->ib_counted_entries?"yes":"no");
105 printf("debug_info offset: %lu 0x%lx\n",
106 (unsigned long)b->ib_offset_in_debug_info,
107 (unsigned long)b->ib_offset_in_debug_info);
108 printf("debug_info size : %lu 0x%lx\n",
109 (unsigned long)b->ib_size_in_debug_info,
110 (unsigned long)b->ib_size_in_debug_info);
111 printf("data offset : %lu 0x%lx\n",
112 (unsigned long)b->ib_b_data_offset,
113 (unsigned long)b->ib_b_data_offset);
114 printf("entries offset : %lu 0x%lx\n",
115 (unsigned long)b->ib_b_offset,
116 (unsigned long)b->ib_b_offset);
117 printf("entries ptr : 0x%lx\n",
118 (unsigned long)b->ib_b_data);
119 printf("entries length : %lu 0x%lx\n",
120 (unsigned long)b->ib_b_entrylength,
121 (unsigned long)b->ib_b_entrylength);
122 printf("entry count : %lu 0x%lx\n",
123 (unsigned long)b->ib_entry_count,
124 (unsigned long)b->ib_entry_count);
125 printf("entries array : 0x%lx\n",
126 (unsigned long)b->ib_entryarray);
127 fflush(stdout);
128 }
129 #endif /*0*/
130 /* We could use dwarf_get_real_section_name()
131 to determine the real name (perhaps ending in .dwo)
132 but for now we just use the standard name here. */
133 static void
134 get_pubxx_fields(Dwarf_Debug dbg,
135 Dwarf_Bool for_gnu_pubnames,
136 struct Dwarf_Section_s **sec_out,
137 const char **sec_name_out,
138 int *errnum_out,
139 const char **errstr_out)
140 {
141 if (IS_INVALID_DBG(dbg)) {
142 return;
143 }
144 if (for_gnu_pubnames) {
145 if (sec_name_out) {
146 *sec_name_out = ".debug_gnu_pubnames";
147 }
148 if (sec_out) {
149 *sec_out = &dbg->de_debug_gnu_pubnames;
150 }
151 if (errnum_out) {
152 *errnum_out = DW_DLE_GNU_PUBNAMES_ERROR;
153 }
154
155 if (errstr_out) {
156 *errstr_out = "DW_DLE_GNU_PUBNAMES_ERROR";
157 }
158 } else {
159 if (sec_name_out) {
160 *sec_name_out = ".debug_gnu_pubtypes";
161 }
162 if (sec_out) {
163 *sec_out = &dbg->de_debug_gnu_pubtypes;
164 }
165 if (errnum_out) {
166 *errnum_out = DW_DLE_GNU_PUBTYPES_ERROR;
167 }
168 if (errstr_out) {
169 *errstr_out = "DW_DLE_GNU_PUBTYPES_ERROR";
170 }
171 }
172 }
173 static void
174 build_errm_no_v(Dwarf_Debug dbg,
175 Dwarf_Bool is_for_pubnames,
176 const char * str1,
177 Dwarf_Error *error)
178 {
179 const char *errstr = 0;
180 int errnum = 0;
181 const char *secname = 0;
182 dwarfstring m;
183
184 get_pubxx_fields(dbg,is_for_pubnames,
185 0,&secname,&errnum, &errstr);
186 dwarfstring_constructor(&m);
187 dwarfstring_append(&m,(char *)errstr);
188 dwarfstring_append(&m," ");
189 dwarfstring_append(&m,(char *)str1);
190 dwarfstring_append_printf_s(&m," for section %s",
191 (char*)secname);
192 _dwarf_error_string(dbg,error,errnum,
193 dwarfstring_string(&m));
194 dwarfstring_destructor(&m);
195 return;
196 }
197
198 static void
199 build_errm_one_num(Dwarf_Debug dbg,
200 Dwarf_Bool is_for_pubnames,
201 const char * str1,
202 Dwarf_Unsigned val1,
203 Dwarf_Error *error)
204 {
205 const char *errstr = 0;
206 int errnum = 0;
207 const char *secname = 0;
208 dwarfstring m;
209
210 /* Something is very wrong */
211 get_pubxx_fields(dbg,is_for_pubnames,
212 0,&secname,&errnum, &errstr);
213 dwarfstring_constructor(&m);
214 dwarfstring_append(&m,(char *)errstr);
215 dwarfstring_append(&m," ");
216 dwarfstring_append_printf_u(&m,(char *)str1,val1);
217 dwarfstring_append_printf_s(&m," for section %s",
218 (char*)secname);
219 _dwarf_error_string(dbg,error,errnum,
220 dwarfstring_string(&m));
221 dwarfstring_destructor(&m);
222 return;
223 }
224
225 static int
226 load_pub_section(Dwarf_Debug dbg,
227 Dwarf_Bool for_gnu_pubnames,
228 Dwarf_Error *error)
229 {
230 struct Dwarf_Section_s * sec = 0;
231 int res;
232
233 get_pubxx_fields(dbg,for_gnu_pubnames,&sec,0,0,0);
234 if (sec) {
235 res = _dwarf_load_section(dbg,sec,error);
236 return res;
237 }
238 return DW_DLV_NO_ENTRY;
239 }
240
241 static int
242 scan_block_entries(Dwarf_Debug dbg,
243 Dwarf_Bool for_gnu_pubnames,
244 Dwarf_Unsigned *count_out,
245 Dwarf_Error *error)
246 {
247 struct Dwarf_Section_s *sec = 0;
248 Dwarf_Small *startptr = 0;
249 Dwarf_Small *curptr = 0;
250 Dwarf_Small *endptr = 0;
251 Dwarf_Unsigned seclen = 0;
252 Dwarf_Unsigned count = 0;
253 Dwarf_Unsigned filesize = 0;
254 int errnum = 0;
255 Dwarf_Unsigned totalframessize = 0;
256 const char * errstr = 0;
257 const char * secname = 0;
258
259 get_pubxx_fields(dbg,for_gnu_pubnames,&sec,&secname,
260 &errnum,&errstr);
261 filesize = dbg->de_filesize;
262 startptr = sec->dss_data;
263 curptr = startptr;
264 seclen = sec->dss_size;
265 endptr = startptr + seclen;
266 if (filesize) {
267 if (seclen >= filesize) {
268 build_errm_one_num(dbg,for_gnu_pubnames,
269 ": section length %u"
270 " is larger than the file size in",
271 seclen,error);
272 return DW_DLV_ERROR;
273 }
274 }
275 for (;;) {
276 Dwarf_Unsigned length = 0;
277 unsigned int offsetsize = 0;
278 unsigned int extensize = 0;
279 unsigned int sumsize = 0;
280
281 if (curptr == endptr) {
282 break;
283 }
284 /* Not sure how the coders think about
285 the initial value. But the last
286 4 bytes are zero, ignore those.
287 Unclear 64bit is not allowed. */
288 READ_AREA_LENGTH_CK(dbg,length,Dwarf_Unsigned,
289 curptr,offsetsize,extensize,error,seclen,endptr);
290 totalframessize += length +offsetsize+extensize;
291 if (length > seclen || totalframessize > seclen) {
292 build_errm_one_num(dbg,for_gnu_pubnames,
293 "Sum of frame fde/cies sizes 0x%" DW_PR_DUx
294 " exceeds section size",totalframessize,error);
295 return DW_DLV_ERROR;
296 }
297
298 ++count;
299 sumsize = offsetsize +extensize;
300 if (length < sumsize) {
301 build_errm_one_num(dbg,for_gnu_pubnames,
302 "Length of fde/cies header sizes 0x%" DW_PR_DUx
303 " is impossibly small",length,error);
304 return DW_DLV_ERROR;
305 }
306 curptr += length - sumsize;
307 curptr += 4;
308 }
309 *count_out = count;
310 return DW_DLV_OK;
311 }
312
313 static int
314 _dwarf_count_entries_in_block(struct Dwarf_Gnu_IBlock_s * gib,
315 struct DGI_Entry_s* entries,
316 Dwarf_Bool for_gnu_pubnames,
317 Dwarf_Error* error)
318 {
319 Dwarf_Small *curptr = gib->ib_b_data;
320 Dwarf_Small *endptr = curptr + gib->ib_b_entrylength;
321 Dwarf_Unsigned entrycount = 0;
322 Dwarf_Half offsetsize = gib->ib_offset_size;
323 struct DGI_Entry_s *curentry = 0;
324 Dwarf_Debug dbg = 0;
325 Dwarf_Gnu_Index_Head head = 0;
326 Dwarf_Bool for_pubnames = 0;
327 char *strptr = 0;
328
329 head = gib->ib_head;
330 for_pubnames = head->gi_is_pubnames;
331 dbg = head->gi_dbg;
332 for ( ; curptr < endptr; ++entrycount) {
333 Dwarf_Unsigned infooffset = 0;
334 Dwarf_Unsigned offset = 0;
335 char flagbyte = 0;
336
337 if ((curptr+offsetsize) == endptr) {
338 break;
339 }
340 if ((curptr+offsetsize) > endptr) {
341 build_errm_one_num(dbg,for_gnu_pubnames,
342 " Reading an address runs off the end of "
343 "this entry at entry %" DW_PR_DUu,
344 entrycount,error);
345 return DW_DLV_ERROR;
346 }
347 READ_UNALIGNED_CK(dbg,offset,
348 Dwarf_Unsigned,curptr,
349 offsetsize,error,endptr);
350 infooffset = offset;
351 curptr += offsetsize;
352 if (entries) {
353 curentry = entries +entrycount;
354 curentry->ge_debug_info_offset = infooffset;
355 }
356 /* Ensure flag and start-of-string possible. */
357 if ((curptr+2) >= endptr) {
358 build_errm_one_num(dbg,for_gnu_pubnames,
359 "Past end of current block reading strings"
360 " Reading an address runs off the end of "
361 "this entry at entry %" DW_PR_DUu,
362 entrycount,error);
363 return DW_DLV_ERROR;
364 }
365 flagbyte = *curptr;
366 curptr += 1;
367 strptr = (char *)curptr;
368 if (curentry) {
369 curentry->ge_flag_byte = flagbyte;
370 curentry->ge_string = (char *)strptr;
371 }
372 for ( ; curptr && curptr < endptr ;++curptr,++offset ) {
373 if (! *curptr ) {
374 /* end of string */
375 break;
376 }
377 if (curptr > endptr) {
378 build_errm_no_v(dbg,for_gnu_pubnames,
379 "Past end of current block reading strings",
380 error);
381 return DW_DLV_ERROR;
382 }
383 }
384 /* string-terminating null byte */
385 curptr += 1;
386 }
387 if (!entries) {
388 gib->ib_entry_count = entrycount;
389 } else if (gib->ib_entry_count != entrycount) {
390 int err = 0;
391 const char *errstr = 0;
392 const char *secname = 0;
393 char buf[120];
394 dwarfstring m;
395
396 buf[0] = 0;
397 get_pubxx_fields(dbg,for_pubnames,0,&secname,
398 &err,&errstr);
399 dwarfstring_constructor_static(&m,buf,sizeof(buf));
400 dwarfstring_append(&m,(char *)errstr);
401 dwarfstring_append_printf_s(&m,":mismatch counts "
402 "creating %s"
403 "block_entries.",
404 (char *)secname);
405 dwarfstring_append_printf_u(&m," Origcount %u",
406 gib->ib_entry_count);
407 dwarfstring_append_printf_u(&m," new count %u",
408 entrycount);
409 _dwarf_error_string(dbg,error,err,
410 dwarfstring_string(&m));
411 dwarfstring_destructor(&m);
412 return DW_DLV_ERROR;
413 }
414 return DW_DLV_OK;
415 }
416
417 static int
418 fill_in_blocks(Dwarf_Gnu_Index_Head head,
419 Dwarf_Error *error)
420 {
421 Dwarf_Unsigned i = 0;
422 Dwarf_Unsigned dataoffset = 0;
423 Dwarf_Small * endptr = 0;
424 Dwarf_Small * curptr = 0;
425 Dwarf_Small * baseptr = 0;
426 Dwarf_Bool is_for_pubnames = head->gi_is_pubnames;
427 Dwarf_Debug dbg = head->gi_dbg;
428 Dwarf_Unsigned seclen = head->gi_section_length;
429
430 baseptr = head->gi_section_data;
431 endptr = baseptr + head->gi_section_length;
432 for ( ;i < head->gi_blockcount; ++i) {
433 Dwarf_Unsigned length = 0;
434 unsigned int offsetsize = 0;
435 unsigned int extensize = 0;
436 Dwarf_Half version = 0;
437 Dwarf_Unsigned offset_into_debug_info = 0;
438 Dwarf_Unsigned length_of_CU_in_debug_info = 0;
439 struct Dwarf_Gnu_IBlock_s *gib = 0;
440 int res = 0;
441
442 gib = head->gi_blockarray+i;
443 /* gib is a blank slate ready to be filled */
444 curptr = baseptr+ dataoffset;
445 READ_AREA_LENGTH_CK(dbg,length,Dwarf_Unsigned,
446 curptr,offsetsize,extensize,error,seclen,endptr);
447 if (!length) {
448 /* Must be end of the section */
449 if (curptr != endptr) {
450 build_errm_no_v(dbg,is_for_pubnames,
451 ": encountered zero area length "
452 "before the section end.",
453 error);
454 return DW_DLV_ERROR;
455 }
456 return DW_DLV_OK;
457 }
458 if (length > head->gi_section_length ||
459 (length+offsetsize+extensize) >
460 head->gi_section_length) {
461 build_errm_one_num(dbg,is_for_pubnames,
462 ": block"
463 " length %" DW_PR_DUu " is too big",
464 length,error);
465 return DW_DLV_ERROR;
466 }
467 /* offsetsize will be set to a constant
468 of either 4 or 8 by READ_AREA_LENGTH_CK.
469 So any check for a usable offsetsize
470 here is dead code.
471 see dwarf_util.h */
472 gib->ib_index = i;
473 gib->ib_head = head;
474 gib->ib_offset_size = (Dwarf_Half)offsetsize;
475 gib->ib_block_length = length;
476 gib->ib_block_length_offset = dataoffset;
477 dataoffset += offsetsize + extensize;
478 /* Now past length field. */
479 gib->ib_b_data_offset = dataoffset;
480 if ((dataoffset + length) > head->gi_section_length){
481 build_errm_one_num(dbg,is_for_pubnames,
482 " header length is %u which runs past the "
483 "end of section suggesting corrupt data",
484 length,error);
485 return DW_DLV_ERROR;
486 }
487 READ_UNALIGNED_CK(dbg,version,Dwarf_Half,curptr,
488 DWARF_HALF_SIZE,error,endptr);
489 if (version != 2) {
490 build_errm_one_num(dbg,is_for_pubnames,
491 " version is %u, not 2,"
492 " which suggests corrupt data",version,error);
493 return DW_DLV_ERROR;
494 }
495 curptr += DWARF_HALF_SIZE;
496 dataoffset += DWARF_HALF_SIZE;
497 gib->ib_version = version;
498 READ_UNALIGNED_CK(dbg,offset_into_debug_info,
499 Dwarf_Unsigned,curptr,
500 offsetsize,error,endptr);
501 curptr += offsetsize;
502 dataoffset += offsetsize;
503 gib->ib_offset_in_debug_info = offset_into_debug_info;
504 READ_UNALIGNED_CK(dbg,length_of_CU_in_debug_info,
505 Dwarf_Unsigned,curptr,
506 offsetsize,error,endptr);
507 gib->ib_size_in_debug_info = length_of_CU_in_debug_info;
508 dataoffset += offsetsize;
509 curptr += offsetsize;
510 gib->ib_b_data = curptr;
511 gib->ib_b_offset = dataoffset;
512 gib->ib_b_entrylength = length - (2 +
513 (2*offsetsize)) -4;
514 if ((gib->ib_b_data +gib->ib_b_entrylength)
515 > endptr) {
516 build_errm_one_num(dbg,is_for_pubnames,
517 " length is %u which runs past the "
518 "end of section suggesting corrupt data",
519 length,error);
520 return DW_DLV_ERROR;
521 }
522 res = _dwarf_count_entries_in_block(gib,0,is_for_pubnames,
523 error);
524 if (res != DW_DLV_OK) {
525 return res;
526 }
527 /* Set for next block., add in 4 for ending zeros */
528 dataoffset = gib->ib_block_length_offset + length + 4;
529 }
530 return DW_DLV_OK;
531 }
532
533 static int
534 fill_in_entries(Dwarf_Gnu_Index_Head head,
535 struct Dwarf_Gnu_IBlock_s *gib,
536 Dwarf_Error *error)
537 {
538 Dwarf_Unsigned count = gib->ib_entry_count;
539 struct DGI_Entry_s * entryarray = 0;
540 Dwarf_Bool for_gnu_pubnames = head->gi_is_pubnames;
541 int res = 0;
542 Dwarf_Debug dbg = 0;
543
544 dbg = head->gi_dbg;
545 entryarray = (struct DGI_Entry_s*)calloc(count,
546 sizeof(struct DGI_Entry_s));
547 if (!entryarray) {
548 build_errm_no_v(dbg,for_gnu_pubnames,
549 ": Unable to allocate "
550 "block_entries. Out of memory creating record.",
551 error);
552 return DW_DLV_ERROR;
553 }
554 res = _dwarf_count_entries_in_block(gib,
555 entryarray,for_gnu_pubnames,error);
556 if (res != DW_DLV_OK) {
557 free(entryarray);
558 return res;
559 }
560 gib->ib_entryarray = entryarray;
561 entryarray = 0;
562 return DW_DLV_OK;
563 }
564
565 int
566 dwarf_get_gnu_index_head(Dwarf_Debug dbg,
567 /* The following arg false to select gnu_pubtypes */
568 Dwarf_Bool for_gnu_pubnames ,
569 Dwarf_Gnu_Index_Head * index_head_out,
570 Dwarf_Unsigned * index_block_count_out,
571 Dwarf_Error * error)
572 {
573 Dwarf_Unsigned count = 0;
574 Dwarf_Gnu_Index_Head head = 0;
575 struct Dwarf_Gnu_IBlock_s *iblock_array = 0;
576 int res = 0;
577
578 CHECK_DBG(dbg,error,"dwarf_get_gnu_index_head()");
579 res = load_pub_section(dbg,for_gnu_pubnames,error);
580 if (res != DW_DLV_OK) {
581 return res;
582 }
583 /* We want this loaded for error checking by callers
584 in case they had no reason to load already. */
585 res = _dwarf_load_debug_info(dbg,error);
586 if (res == DW_DLV_ERROR) {
587 return res;
588 }
589 /* if count zero, returns DW_DLV_NO_ENTRY */
590 res = scan_block_entries(dbg,for_gnu_pubnames,&count,error);
591 if (res != DW_DLV_OK) {
592 return res;
593 }
594 head = (Dwarf_Gnu_Index_Head)
595 _dwarf_get_alloc(dbg,DW_DLA_GNU_INDEX_HEAD,1);
596 if (!head) {
597 build_errm_no_v(dbg,for_gnu_pubnames,
598 " Unable to allocate "
599 "a header record. Out of memory creating record.",
600 error);
601 return DW_DLV_ERROR;
602 }
603 head->gi_dbg = dbg;
604 head->gi_section_data = for_gnu_pubnames?
605 dbg->de_debug_gnu_pubnames.dss_data:
606 dbg->de_debug_gnu_pubtypes.dss_data;
607 head->gi_section_length = for_gnu_pubnames?
608 dbg->de_debug_gnu_pubnames.dss_size:
609 dbg->de_debug_gnu_pubtypes.dss_size;
610 head->gi_section_end = head->gi_section_data +
611 head->gi_section_length;
612 head->gi_is_pubnames = for_gnu_pubnames;
613 iblock_array = calloc(count,
614 sizeof(struct Dwarf_Gnu_IBlock_s));
615 if (!iblock_array) {
616 build_errm_one_num(dbg,for_gnu_pubnames,
617 "Unable to allocate "
618 " %u block records. Out of memory.",
619 count,error);
620 dwarf_gnu_index_dealloc(head);
621 head = 0;
622 return DW_DLV_ERROR;
623 }
624 head->gi_blockarray = iblock_array;
625 head->gi_blockcount = count;
626 res = fill_in_blocks(head,error);
627 if (res != DW_DLV_OK) {
628 dwarf_gnu_index_dealloc(head);
629 head = 0;
630 return res;
631 }
632 *index_head_out = head;
633 *index_block_count_out = count;
634 return DW_DLV_OK;
635 }
636
637 /* Frees all resources used for the indexes. */
638 void
639 _dwarf_free_gnu_index_head_content(Dwarf_Gnu_Index_Head head)
640 {
641 if (!head) {
642 return;
643 }
644 if (head->gi_blockarray) {
645 Dwarf_Unsigned i = 0;
646 struct Dwarf_Gnu_IBlock_s *block =
647 head->gi_blockarray;
648
649 for ( ; i < head->gi_blockcount; ++i,block++) {
650 if (block->ib_entryarray) {
651 free(block->ib_entryarray);
652 block->ib_entryarray = 0;
653 }
654 block->ib_entry_count = 0;
655 }
656 free(head->gi_blockarray);
657 head->gi_blockarray = 0;
658 head->gi_blockcount = 0;
659 }
660 }
661
662 void
663 dwarf_gnu_index_dealloc(Dwarf_Gnu_Index_Head head)
664 {
665 Dwarf_Debug dbg = 0;
666 if (!head) {
667 return;
668 }
669 dbg = head->gi_dbg;
670 if (IS_INVALID_DBG(dbg)) {
671 return;
672 }
673 _dwarf_free_gnu_index_head_content(head);
674 dwarf_dealloc(dbg,head,DW_DLA_GNU_INDEX_HEAD);
675 }
676
677 void
678 _dwarf_gnu_index_head_destructor(void *incoming)
679 {
680 Dwarf_Gnu_Index_Head head = 0;
681
682 head = (Dwarf_Gnu_Index_Head)incoming;
683 if (!head) {
684 return;
685 }
686 _dwarf_free_gnu_index_head_content(head);
687 return;
688 }
689
690 int
691 dwarf_get_gnu_index_block(Dwarf_Gnu_Index_Head head,
692 Dwarf_Unsigned number,
693 Dwarf_Unsigned * block_length,
694 Dwarf_Half * version,
695 Dwarf_Unsigned * offset_into_debug_info,
696 Dwarf_Unsigned * size_of_debug_info_area,
697 Dwarf_Unsigned * count_of_entries,
698 Dwarf_Error * error)
699 {
700 struct Dwarf_Gnu_IBlock_s *gib = 0;
701
702 if (!head) {
703 _dwarf_error_string(0,error,DW_DLE_DBG_NULL,
704 "DW_DLE_DBG_NULL: in "
705 "dwarf_get_gnu_index_block");
706 return DW_DLV_ERROR;
707 }
708 if (number >= head->gi_blockcount) {
709 return DW_DLV_NO_ENTRY;
710 }
711 gib = head->gi_blockarray + number;
712 if (block_length) {
713 *block_length = gib->ib_block_length;
714 }
715 if (version) {
716 *version = gib->ib_version;
717 }
718 if (offset_into_debug_info) {
719 *offset_into_debug_info = gib->ib_offset_in_debug_info;
720 }
721 if (size_of_debug_info_area) {
722 *size_of_debug_info_area = gib->ib_size_in_debug_info;
723 }
724 if (count_of_entries) {
725 *count_of_entries = gib->ib_entry_count;
726 }
727 return DW_DLV_OK;
728 }
729
730 int
731 dwarf_get_gnu_index_block_entry(Dwarf_Gnu_Index_Head head,
732 Dwarf_Unsigned blocknumber,
733 Dwarf_Unsigned entrynumber,
734 Dwarf_Unsigned * offset_in_debug_info,
735 const char ** name_string,
736 unsigned char * flagbyte,
737 unsigned char * staticorglobal,
738 unsigned char * typeofentry,
739 Dwarf_Error * error)
740 {
741 struct Dwarf_Gnu_IBlock_s *gib = 0;
742 struct DGI_Entry_s *be = 0;
743
744 if (!head) {
745 _dwarf_error_string(0,error,DW_DLE_DBG_NULL,
746 "DW_DLE_DBG_NULL: in "
747 "dwarf_get_gnu_index_block_entry");
748 return DW_DLV_ERROR;
749 }
750 if (blocknumber >= head->gi_blockcount) {
751 return DW_DLV_NO_ENTRY;
752 }
753 gib = head->gi_blockarray + blocknumber;
754 if (!gib->ib_counted_entries) {
755 int res = 0;
756
757 gib->ib_counted_entries = TRUE;
758 res = fill_in_entries(head,gib,error);
759 if (res != DW_DLV_OK) {
760 return res;
761 }
762 }
763 if (entrynumber >= gib->ib_entry_count) {
764 return DW_DLV_NO_ENTRY;
765 }
766 be = gib->ib_entryarray + entrynumber;
767
768 if (offset_in_debug_info) {
769 *offset_in_debug_info = be->ge_debug_info_offset;
770 }
771 if (name_string) {
772 *name_string = be->ge_string;
773 }
774 if (flagbyte) {
775 *flagbyte = be->ge_flag_byte;
776 }
777 if (staticorglobal) {
778 if (be->ge_flag_byte &0x80) {
779 /* value 1, of course... */
780 *staticorglobal = DW_GNUIVIS_global;
781 } else {
782 *staticorglobal = DW_GNUIVIS_static;
783 }
784 }
785 if (typeofentry) {
786 /* DW_GNUIKIND_ */
787 unsigned v = be->ge_flag_byte & 0x70;
788 v = v>>4;
789 *typeofentry = (unsigned char)v;
790 }
791 return DW_DLV_OK;
792 }