Debian Codegraph @

src/lib/libdwarf/dwarf_debugnames.c

raw
1 /*
2 Portions Copyright (C) 2017-2021 David Anderson. All Rights Reserved.
3
4 This program is free software; you can redistribute it
5 and/or modify it under the terms of version 2.1 of the
6 GNU Lesser General Public License as published by the Free
7 Software Foundation.
8
9 This program is distributed in the hope that it would be
10 useful, but WITHOUT ANY WARRANTY; without even the implied
11 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE.
13
14 Further, this software is distributed without any warranty
15 that it is free of the rightful claim of any third person
16 regarding infringement or the like. Any license provided
17 herein, whether implied or otherwise, applies only to this
18 software file. Patent licenses, if any, provided herein
19 do not apply to combinations of this program with other
20 software, or any other product whatsoever.
21
22 You should have received a copy of the GNU Lesser General
23 Public License along with this program; if not, write the
24 Free Software Foundation, Inc., 51 Franklin Street - Fifth
25 Floor, Boston MA 02110-1301, USA.
26 */
27
28 /* This provides access to the DWARF5 .debug_names section. */
29
30 #include <config.h>
31
32 #include <stdlib.h> /* calloc() free() */
33 #include <stdio.h>
34 #include <string.h> /* memcpy */
35 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
36 #include "stdafx.h"
37 #endif /* HAVE_STDAFX_H */
38 #ifdef HAVE_STDINT_H
39 #include <stdint.h> /* uintptr_t */
40 #endif /* HAVE_STDINT_H */
41
42 #include "dwarf.h"
43 #include "libdwarf.h"
44 #include "libdwarf_private.h"
45 #include "dwarf_base_types.h"
46 #include "dwarf_safe_strcpy.h"
47 #include "dwarf_opaque.h"
48 #include "dwarf_alloc.h"
49 #include "dwarf_error.h"
50 #include "dwarf_util.h"
51 #include "dwarf_global.h"
52 #include "dwarf_debugnames.h"
53 #include "dwarf_string.h"
54
55 #if 0 /* dump_bytes */
56 static void
57 dump_bytes(char * msg,Dwarf_Small * start, long len)
58 {
59 Dwarf_Small *end = start + len;
60 Dwarf_Small *cur = start;
61
62 printf("%s(%ld bytes):",msg,len);
63 for (; cur < end; cur++) {
64 printf("%02x ", *cur);
65 }
66 printf("\n");
67 }
68 #endif /*0*/
69 #if 0 /* dump_abbrev_record() */
70 static void
71 dump_abbrev_record(const char *msg,
72 Dwarf_Unsigned abcode,
73 struct Dwarf_D_Abbrev_s *abb,
74 int line)
75 {
76 unsigned long i = 0;
77
78 printf("%s: code %lu abpairs %lu line %d \n",
79 msg,(unsigned long)abcode,
80 (unsigned long)abb->da_pairs_count,line);
81 for (i=0; i < abb->da_pairs_count ; ++i) {
82 printf(" [%lu] 0x%02lx 0x%02lx\n",
83 i,
84 (unsigned long)abb->da_idxattr[i],
85 (unsigned long)abb->da_form[i]);
86 }
87 }
88 #endif
89
90 /* Encapsulates DECODE_LEB128_UWORD_LEN_CK
91 so the caller can free resources
92 in case of problems.
93 This updates *lp to point to next byte
94 */
95 static int
96 _dwarf_read_uleb_ck(Dwarf_Small **lp,
97 Dwarf_Unsigned *out_p,
98 Dwarf_Debug dbg,
99 Dwarf_Error *err,
100 Dwarf_Small *lpend)
101 {
102 Dwarf_Small *inptr = *lp;
103 Dwarf_Unsigned abcode = 0;
104
105 /* The macro updates inptr */
106 DECODE_LEB128_UWORD_CK(inptr,
107 abcode,dbg,err,lpend);
108 *lp = inptr;
109 *out_p = abcode;
110 return DW_DLV_OK;
111 }
112
113 /* Encapsulates DECODE_LEB128_UWORD_CK
114 so the caller can free resources
115 in case of problems. */
116 static int
117 read_uword_ab(Dwarf_Small **lp,
118 Dwarf_Unsigned *out_p,
119 Dwarf_Debug dbg,
120 Dwarf_Error *err,
121 Dwarf_Small *lpend)
122
123 {
124 Dwarf_Small *inptr = *lp;
125 Dwarf_Unsigned out = 0;
126
127 /* The macro updates inptr */
128 DECODE_LEB128_UWORD_CK(inptr,
129 out, dbg,err,lpend);
130 *lp = inptr;
131 *out_p = out;
132 return DW_DLV_OK;
133 }
134
135 static void
136 free_temp_abbrevs( struct Dwarf_D_Abbrev_s * firstab)
137 {
138 struct Dwarf_D_Abbrev_s *curab = firstab;
139
140 while (curab) {
141 struct Dwarf_D_Abbrev_s *nxtab = 0;
142
143 nxtab = curab->da_next;
144 curab->da_next = 0;
145 free(curab);
146 curab = nxtab;
147 }
148 }
149
150 static int
151 fill_in_abbrevs_table(Dwarf_Dnames_Head dn,
152 Dwarf_Error * error)
153 {
154 Dwarf_Unsigned ablen = dn->dn_abbrev_table_size;
155 Dwarf_Small *abdata = dn->dn_abbrevs;
156 Dwarf_Small *tabend = abdata + ablen;
157 Dwarf_Small *abcur = 0;
158 Dwarf_Unsigned code = 0;
159 Dwarf_Unsigned tag = 0;
160 int foundabend = FALSE;
161 Dwarf_Unsigned abcount = 0;
162 struct Dwarf_D_Abbrev_s *curdab = 0;
163 struct Dwarf_D_Abbrev_s *firstdab = 0;
164 struct Dwarf_D_Abbrev_s **lastabp = &firstdab;
165 Dwarf_Debug dbg = dn->dn_dbg;
166
167 for (abcur = abdata; abcur < tabend; ) {
168 Dwarf_Unsigned attr = 0;
169 Dwarf_Unsigned form = 0;
170 Dwarf_Small *inner = 0;
171 Dwarf_Unsigned inroffset = 0;
172 int res = 0;
173
174 inroffset = abcur - abdata;
175 res = read_uword_ab(&abcur,&code,dbg,error,tabend);
176 if (res != DW_DLV_OK) {
177 free_temp_abbrevs(firstdab);
178 return res;
179 }
180 if (code == 0) {
181 foundabend = TRUE;
182 break;
183 }
184
185 res = read_uword_ab(&abcur,&tag,dbg,error,tabend);
186 if (res != DW_DLV_OK) {
187 free_temp_abbrevs(firstdab);
188 return res;
189 }
190 inner = abcur;
191 curdab = (struct Dwarf_D_Abbrev_s *)calloc(1,
192 sizeof(struct Dwarf_D_Abbrev_s));
193 if (!curdab) {
194 free_temp_abbrevs(firstdab);
195 firstdab = 0;
196 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
197 return DW_DLV_ERROR;
198 }
199 curdab->da_abbrev_offset = inroffset;
200 curdab->da_abbrev_code = code;
201 curdab->da_tag = tag;
202 abcount++;
203 for (;;) {
204 res = read_uword_ab(&inner,&attr,dbg,error,tabend);
205 if (res != DW_DLV_OK) {
206 free_temp_abbrevs(curdab);
207 free_temp_abbrevs(firstdab);
208 firstdab = 0;
209 return res;
210 }
211 res = read_uword_ab(&inner,&form,dbg,error,tabend);
212 if (res != DW_DLV_OK) {
213 free_temp_abbrevs(curdab);
214 free_temp_abbrevs(firstdab);
215 firstdab = 0;
216 return res;
217 }
218 if ( curdab->da_pairs_count ==
219 ABB_PAIRS_MAX) {
220 free_temp_abbrevs(curdab);
221 free_temp_abbrevs(firstdab);
222 _dwarf_error_string(dbg,error,
223 DW_DLE_DEBUG_NAMES_ABBREV_CORRUPTION,
224 "DW_DLE_DEBUG_NAMES_ABBREV_CORRUPTION: "
225 "Impossible: too many idxattr/form pairs"
226 "corrupt abbrevs");
227 return DW_DLV_ERROR;
228 }
229 curdab->da_idxattr[curdab->da_pairs_count] =
230 (Dwarf_Half)attr;
231 curdab->da_form[curdab->da_pairs_count] =
232 (Dwarf_Half)form;
233 curdab->da_pairs_count++;
234 if (!attr && !form) {
235 /* We put the terminator into the pairs list.
236 done for this pair set */
237 break;
238 }
239 }
240
241 /* Building singly linked list. without if stmt. */
242 abcur = inner;
243 *lastabp = curdab;
244 lastabp = &curdab->da_next;
245 }
246 if (!foundabend) {
247 free_temp_abbrevs(firstdab);
248 _dwarf_error_string(dbg, error,
249 DW_DLE_DEBUG_NAMES_ABBREV_CORRUPTION,
250 "DW_DLE_DEBUG_NAMES_ABBREV_CORRUPTION: Never found"
251 "abbrev final NUL byte");
252 return DW_DLV_ERROR;
253 }
254 if ( abcur < tabend) {
255 unsigned padcount = 0;
256
257 for (; abcur < tabend; ++abcur) {
258 ++padcount;
259 if (*abcur) {
260 dwarfstring m;
261
262 dwarfstring_constructor(&m);
263 dwarfstring_append_printf_u(&m,
264 "WARNING NON NULL debug_names "
265 "abbreviation pad. "
266 "padcount %u at ",padcount);
267 dwarfstring_append_printf_u(&m,
268 ".debug_names sec_offset 0x%lx",
269 (uintptr_t)(abcur - dn->dn_section_data));
270 dwarf_insert_harmless_error(dbg,
271 dwarfstring_string(&m));
272 dwarfstring_destructor(&m);
273 }
274 }
275 }
276 {
277 Dwarf_Unsigned ct = 0;
278 struct Dwarf_D_Abbrev_s *tmpa = 0;
279
280 dn->dn_abbrev_instances =
281 (struct Dwarf_D_Abbrev_s *)calloc(
282 abcount,sizeof(struct Dwarf_D_Abbrev_s));
283 if (!dn->dn_abbrev_instances) {
284 free_temp_abbrevs(firstdab);
285 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
286 return DW_DLV_ERROR;
287 }
288 dn->dn_abbrev_instance_count = abcount;
289 tmpa = firstdab;
290 for (ct = 0; ct < abcount; ++ct) {
291 struct Dwarf_D_Abbrev_s *tmpb =tmpa->da_next;
292 /* da_next no longer means anything */
293 dn->dn_abbrev_instances[ct] = *tmpa;
294 dn->dn_abbrev_instances[ct].da_next = 0;
295 free(tmpa);
296 tmpa = tmpb;
297 }
298 tmpa = 0;
299 firstdab = 0;
300 /* Now the list has turned into an
301 array. We can ignore
302 the list aspect. */
303 }
304 return DW_DLV_OK;
305 }
306 static int
307 read_uword_val(Dwarf_Debug dbg,
308 Dwarf_Small **ptr_in,
309 Dwarf_Small *endptr,
310 int errcode,
311 Dwarf_Unsigned *val_out,
312 Dwarf_Unsigned area_length,
313 Dwarf_Error *error)
314 {
315 Dwarf_Unsigned val = 0;
316 Dwarf_Small *ptr = *ptr_in;
317
318 READ_UNALIGNED_CK(dbg, val, Dwarf_Unsigned,
319 ptr, DWARF_32BIT_SIZE,
320 error,endptr);
321 ptr += DWARF_32BIT_SIZE;
322 if (ptr >= endptr) {
323 _dwarf_error(dbg, error,errcode);
324 return DW_DLV_ERROR;
325 }
326 /* Some of the fields are not length fields, but
327 if non-zero the size will be longer than
328 the value, so we do the following
329 overall sanity check to avoid overflows. */
330 if (val > area_length) {
331 _dwarf_error(dbg, error,errcode);
332 return DW_DLV_ERROR;
333 }
334 *val_out = val;
335 *ptr_in = ptr;
336 return DW_DLV_OK;
337 }
338
339 /* For now, check using global offset, not pointer */
340 #define VALIDATEOFFSET(dnm,used,m) \
341 do { \
342 if ((used) >= (dnm)->dn_next_set_offset) { \
343 _dwarf_error_string(dbg, error, \
344 DW_DLE_DEBUG_NAMES_HEADER_ERROR, \
345 "DW_DLE_DEBUG_NAMES_HEADER_ERROR: " \
346 m); \
347 return DW_DLV_ERROR; \
348 } \
349 } while(0)
350
351 /* Reading a .debug_names Name Index header */
352 static int
353 read_a_name_table_header(Dwarf_Dnames_Head dn,
354 Dwarf_Unsigned starting_offset,
355 Dwarf_Unsigned remaining_space,
356 Dwarf_Small *curptr_in,
357 Dwarf_Unsigned *usedspace_out,
358 Dwarf_Unsigned *next_offset,
359 Dwarf_Small *end_section,
360 Dwarf_Error *error)
361 {
362 Dwarf_Unsigned area_length = 0;
363 Dwarf_Unsigned area_max_offset = 0;
364 unsigned initial_length = 0; /*offset_size+local_ext */
365 int offset_size = 0;
366 int local_extension_size = 0;
367 Dwarf_Small *end_dnames = 0; /* 1 past local table */
368 Dwarf_Half version = 0;
369 Dwarf_Half padding = 0;
370 Dwarf_Unsigned comp_unit_count = 0;
371 Dwarf_Unsigned local_type_unit_count = 0;
372 Dwarf_Unsigned foreign_type_unit_count = 0;
373 Dwarf_Unsigned bucket_count = 0;
374 Dwarf_Unsigned name_count = 0;
375 Dwarf_Unsigned abbrev_table_size = 0; /* bytes */
376 Dwarf_Unsigned entry_pool_size = 0; /* bytes */
377 Dwarf_Unsigned augmentation_string_size = 0; /* bytes */
378 Dwarf_Unsigned usedspace = 0;
379 Dwarf_Unsigned totaloffset = 0;
380 int res = 0;
381 const char *str_utf8 = 0; /* Augmentation string */
382 Dwarf_Small *curptr = 0;
383 Dwarf_Debug dbg = dn->dn_dbg;
384
385 curptr = curptr_in;
386 usedspace = 0;
387 totaloffset = starting_offset;
388 /* 1 */
389 READ_AREA_LENGTH_CK(dbg, area_length, Dwarf_Unsigned,
390 curptr, offset_size,
391 local_extension_size,error,
392 remaining_space,end_section);
393 initial_length = offset_size+local_extension_size;
394
395 /* curptr now points past the length field */
396 area_max_offset = area_length + initial_length;
397 usedspace = initial_length;
398 totaloffset += initial_length;
399 dn->dn_offset_size = (Dwarf_Half)offset_size;
400 /* Two stage length test so overflow is caught. */
401 if (area_length > remaining_space ||
402 (area_length +offset_size +local_extension_size) >
403 remaining_space) {
404 _dwarf_error_string(dbg, error,
405 DW_DLE_DEBUG_NAMES_HEADER_ERROR,
406 "DW_DLE_DEBUG_NAMES_HEADER_ERROR: "
407 "The index table runs off the end of the .debug_names "
408 "section. Corrupt data.");
409 return DW_DLV_ERROR;
410 }
411 if (area_max_offset > remaining_space) {
412 _dwarf_error_string(dbg, error,
413 DW_DLE_DEBUG_NAMES_HEADER_ERROR,
414 "DW_DLE_DEBUG_NAMES_HEADER_ERROR: "
415 "The index table runs off the end of the .debug_names "
416 "section... Corrupt data.");
417 return DW_DLV_ERROR;
418 }
419 end_dnames = curptr + area_length;
420 dn->dn_unit_length = area_length + local_extension_size;
421 dn->dn_indextable_data_end = end_dnames;
422 dn->dn_next_set_offset = area_length
423 + initial_length +
424 + starting_offset;
425
426 /* 2 */
427 READ_UNALIGNED_CK(dbg, version, Dwarf_Half,
428 curptr, DWARF_HALF_SIZE,
429 error,end_dnames);
430 curptr += DWARF_HALF_SIZE;
431 usedspace += DWARF_HALF_SIZE;
432 totaloffset += DWARF_HALF_SIZE;
433 if (curptr >= end_dnames) {
434 _dwarf_error(dbg, error,DW_DLE_DEBUG_NAMES_HEADER_ERROR);
435 return DW_DLV_ERROR;
436 }
437 if (version != DWARF_DNAMES_VERSION5) {
438 _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
439 return DW_DLV_ERROR;
440 }
441 /* 3 */
442 READ_UNALIGNED_CK(dbg, padding, Dwarf_Half,
443 curptr, DWARF_HALF_SIZE,
444 error,end_dnames);
445 curptr += DWARF_HALF_SIZE;
446 usedspace += DWARF_HALF_SIZE;
447 totaloffset += DWARF_HALF_SIZE;
448 if (curptr >= end_dnames) {
449 _dwarf_error(dbg, error,DW_DLE_DEBUG_NAMES_HEADER_ERROR);
450 return DW_DLV_ERROR;
451 }
452 if (padding) {
453 _dwarf_error(dbg, error,DW_DLE_DEBUG_NAMES_HEADER_ERROR);
454 return DW_DLV_ERROR;
455 }
456 /* 4 */
457 res = read_uword_val(dbg, &curptr,
458 end_dnames, DW_DLE_DEBUG_NAMES_HEADER_ERROR,
459 &comp_unit_count,area_max_offset,error);
460 if (res != DW_DLV_OK) {
461 return res;
462 }
463 if (comp_unit_count > dn->dn_section_size) {
464 _dwarf_error_string(dbg,error,
465 DW_DLE_DEBUG_NAMES_ERROR,
466 "DW_DLE_DEBUG_NAMES_ERROR comp_unit_count too large");
467 return DW_DLV_ERROR;
468 }
469 dn->dn_comp_unit_count = comp_unit_count;
470 usedspace += SIZEOFT32;
471 totaloffset += SIZEOFT32;
472 /* 5 */
473 res = read_uword_val(dbg, &curptr,
474 end_dnames, DW_DLE_DEBUG_NAMES_HEADER_ERROR,
475 &local_type_unit_count,area_max_offset,error);
476 if (res != DW_DLV_OK) {
477 return res;
478 }
479 if (local_type_unit_count > dn->dn_section_size) {
480 _dwarf_error_string(dbg,error,
481 DW_DLE_DEBUG_NAMES_ERROR,
482 "DW_DLE_DEBUG_NAMES_ERROR local_type_unit_count large");
483 return DW_DLV_ERROR;
484 }
485 dn->dn_local_type_unit_count = local_type_unit_count;
486 usedspace += SIZEOFT32;
487 totaloffset += SIZEOFT32;
488
489 /* 6 */
490 res = read_uword_val(dbg, &curptr,
491 end_dnames, DW_DLE_DEBUG_NAMES_HEADER_ERROR,
492 &foreign_type_unit_count,area_max_offset,error);
493 if (res != DW_DLV_OK) {
494 return res;
495 }
496 if (foreign_type_unit_count > dn->dn_section_size) {
497 _dwarf_error_string(dbg,error,
498 DW_DLE_DEBUG_NAMES_ERROR,
499 "DW_DLE_DEBUG_NAMES_ERROR: "
500 "foreign_type_unit_count large");
501 return DW_DLV_ERROR;
502 }
503 dn->dn_foreign_type_unit_count = foreign_type_unit_count;
504 usedspace += SIZEOFT32;
505 totaloffset += SIZEOFT32;
506 /* 7 */
507 res = read_uword_val(dbg, &curptr,
508 end_dnames, DW_DLE_DEBUG_NAMES_HEADER_ERROR,
509 &bucket_count,area_max_offset,error);
510 if (res != DW_DLV_OK) {
511 return res;
512 }
513 if (bucket_count > dn->dn_section_size) {
514 _dwarf_error_string(dbg,error,
515 DW_DLE_DEBUG_NAMES_ERROR,
516 "DW_DLE_DEBUG_NAMES_ERROR bucketcount too large");
517 return DW_DLV_ERROR;
518 }
519 dn->dn_bucket_count = bucket_count;
520 usedspace += SIZEOFT32;
521 totaloffset += SIZEOFT32;
522 /* name_count gives the size of
523 the string-offsets and entry-offsets arrays,
524 and if hashes present, the size of the hashes
525 array. */
526 /* 8 */
527 res = read_uword_val(dbg, &curptr,
528 end_dnames, DW_DLE_DEBUG_NAMES_HEADER_ERROR,
529 &name_count,area_max_offset,error);
530 if (res != DW_DLV_OK) {
531 return res;
532 }
533 dn->dn_name_count = name_count;
534 if (name_count > dn->dn_section_size) {
535 _dwarf_error_string(dbg,error,
536 DW_DLE_DEBUG_NAMES_ERROR,
537 "DW_DLE_DEBUG_NAMES_ERROR name_count too large");
538 return DW_DLV_ERROR;
539 }
540 usedspace += SIZEOFT32;
541 totaloffset += SIZEOFT32;
542
543 /* abbrev_table */
544 /* 9 */
545 res = read_uword_val(dbg, &curptr,
546 end_dnames, DW_DLE_DEBUG_NAMES_HEADER_ERROR,
547 &abbrev_table_size,area_max_offset,error);
548 if (res != DW_DLV_OK) {
549 return res;
550 }
551 if (abbrev_table_size > dn->dn_section_size) {
552 _dwarf_error_string(dbg,error,
553 DW_DLE_DEBUG_NAMES_ERROR,
554 "DW_DLE_DEBUG_NAMES_ERROR abbrev_table_size too large");
555 return DW_DLV_ERROR;
556 }
557 dn->dn_abbrev_table_size = abbrev_table_size;
558 usedspace += SIZEOFT32;
559 totaloffset += SIZEOFT32;
560
561 /* 10 */
562 res = read_uword_val(dbg, &curptr,
563 end_dnames, DW_DLE_DEBUG_NAMES_HEADER_ERROR,
564 &augmentation_string_size,area_max_offset,error);
565 if (res != DW_DLV_OK) {
566 return res;
567 }
568 if (augmentation_string_size > dn->dn_section_size) {
569 _dwarf_error_string(dbg,error,
570 DW_DLE_DEBUG_NAMES_ERROR,
571 "DW_DLE_DEBUG_NAMES_ERROR augmentation string too long");
572 return DW_DLV_ERROR;
573 }
574 usedspace += SIZEOFT32;
575 totaloffset += SIZEOFT32;
576
577 str_utf8 = (const char *) curptr;
578 totaloffset += augmentation_string_size;
579 usedspace += augmentation_string_size;
580 curptr += augmentation_string_size;
581 dn->dn_augmentation_string_size = augmentation_string_size;
582 if (curptr >= end_dnames) {
583 _dwarf_error(dbg, error,DW_DLE_DEBUG_NAMES_HEADER_ERROR);
584 return DW_DLV_ERROR;
585 }
586
587 dn->dn_version = version;
588 dn->dn_abbrev_table_size = abbrev_table_size;
589 if (augmentation_string_size) {
590 /* 11. len: The string size includes zero or
591 more NUL bytes to be a multiple of
592 four bytes long. */
593 Dwarf_Unsigned len = augmentation_string_size;
594 const char *cp = 0;
595 const char *cpend = 0;
596 Dwarf_Unsigned finallen = 0;
597
598 dn->dn_augmentation_string = calloc(1,
599 augmentation_string_size + 1);
600 /* Ensures a final NUL byte */
601 _dwarf_safe_strcpy(dn->dn_augmentation_string,
602 len +1,str_utf8, len);
603 /* This validates a zero length string too. */
604 /* in LLVM0700 there is no NUL terminator there:
605 See DWARF5 page 144 and also ISSUE
606 200505.4 */
607 cp = dn->dn_augmentation_string;
608 cpend = cp + len;
609 /* It's null terminated now, so find the true length */
610 for ( ; cp<cpend; ++cp) {
611 if (!*cp) {
612 break;
613 }
614 ++finallen;
615 }
616 if (len%4) {
617 dwarfstring m;
618
619 dwarfstring_constructor(&m);
620 dwarfstring_append_printf_u(&m,
621 "DW_DLE_DEBUG_NAMES_PAD_NON_ZERO: "
622 "The augmentation_string_size "
623 " is %u, not a multiple of four",len);
624 _dwarf_error_string(dbg, error,
625 DW_DLE_DEBUG_NAMES_PAD_NON_ZERO,
626 dwarfstring_string(&m));
627 dwarfstring_destructor(&m);
628 return DW_DLV_ERROR;
629 }
630 if (finallen != len) {
631 cp = str_utf8 + finallen;
632 cpend = str_utf8+len;
633 /* Ensure that there is no corruption in
634 the padding. */
635 for ( ; cp < cpend; ++cp) {
636 if (*cp) {
637 _dwarf_error_string(dbg, error,
638 DW_DLE_DEBUG_NAMES_PAD_NON_ZERO,
639 "DW_DLE_DEBUG_NAMES_PAD_NON_ZERO: "
640 "padding in augmentation string not zeros");
641 return DW_DLV_ERROR;
642 }
643 }
644 }
645 }
646
647 /* Now we deal with the arrays following the header. */
648 dn->dn_cu_list = curptr;
649 dn->dn_cu_list_offset = usedspace;
650 curptr += dn->dn_offset_size * comp_unit_count;
651 usedspace += dn->dn_offset_size * comp_unit_count ;
652 totaloffset += dn->dn_offset_size * comp_unit_count ;
653 VALIDATEOFFSET(dn,totaloffset,"comp_unit array error");
654
655 dn->dn_local_tu_list = curptr;
656 dn->dn_local_tu_list_offset = usedspace;
657 curptr += dn->dn_offset_size *local_type_unit_count;
658 usedspace += dn->dn_offset_size* local_type_unit_count;
659 totaloffset += dn->dn_offset_size* local_type_unit_count;
660 VALIDATEOFFSET(dn,totaloffset,"local_type__unit array error");
661
662 dn->dn_foreign_tu_list = curptr;
663 dn->dn_foreign_tu_list_offset = usedspace;
664 curptr += sizeof(Dwarf_Sig8) * foreign_type_unit_count;
665 usedspace += sizeof(Dwarf_Sig8) * foreign_type_unit_count;
666 totaloffset += sizeof(Dwarf_Sig8)* foreign_type_unit_count;
667 VALIDATEOFFSET(dn,totaloffset,"foreign_type__unit array error");
668
669 dn->dn_buckets_offset = usedspace;
670 dn->dn_buckets = curptr;
671 curptr += SIZEOFT32 * bucket_count ;
672 usedspace += SIZEOFT32 * bucket_count;
673 totaloffset += SIZEOFT32 * bucket_count;
674 VALIDATEOFFSET(dn,totaloffset," bucket array error");
675
676 if (comp_unit_count == 1) {
677 Dwarf_Small *ptrx = dn->dn_cu_list;
678 Dwarf_Small *endptr = dn->dn_foreign_tu_list;
679 Dwarf_Unsigned unit_entry_size = dn->dn_offset_size;
680 Dwarf_Unsigned offsetval = 0;
681
682 READ_UNALIGNED_CK(dbg, offsetval, Dwarf_Unsigned,
683 ptrx,(unsigned long) unit_entry_size,
684 error,endptr);
685 dn->dn_single_cu = TRUE;
686 dn->dn_single_cu_offset = offsetval;
687 }
688 dn->dn_hash_table = curptr;
689 dn->dn_hash_table_offset = usedspace;
690 if (bucket_count) {
691 curptr += SIZEOFT32 * name_count;
692 usedspace += SIZEOFT32 * name_count;
693 totaloffset += SIZEOFT32 * name_count;
694 }
695 VALIDATEOFFSET(dn,totaloffset,"hashes array error");
696
697 dn->dn_string_offsets = curptr;
698 dn->dn_string_offsets_offset = usedspace;
699 curptr += dn->dn_offset_size * name_count;
700 usedspace += dn->dn_offset_size * name_count;
701 totaloffset += dn->dn_offset_size * name_count;
702 VALIDATEOFFSET(dn,totaloffset,"string offsets array error");
703
704 dn->dn_entry_offsets = curptr;
705 dn->dn_entry_offsets_offset = usedspace;
706 curptr += dn->dn_offset_size * name_count;
707 usedspace += dn->dn_offset_size * name_count;
708 totaloffset += dn->dn_offset_size * name_count;
709 VALIDATEOFFSET(dn,totaloffset,"entry offsets array error");
710
711 dn->dn_abbrevs = curptr;
712 dn->dn_abbrevs_offset = totaloffset;
713 VALIDATEOFFSET(dn,totaloffset,"abbrev table error");
714 curptr += dn->dn_abbrev_table_size;
715 usedspace += dn->dn_abbrev_table_size;
716 totaloffset += dn->dn_abbrev_table_size;
717 VALIDATEOFFSET(dn,totaloffset,"abbrev table error");
718
719 dn->dn_entry_pool = curptr;
720 dn->dn_entry_pool_offset = totaloffset;
721 if (dn->dn_next_set_offset < totaloffset) {
722 _dwarf_error_string(dbg, error,
723 DW_DLE_DEBUG_NAMES_HEADER_ERROR,
724 "DW_DLE_DEBUG_NAMES_HEADER_ERROR: "
725 "Abbrev total wrong, exceeds "
726 "the room available.");
727 return DW_DLV_ERROR;
728 }
729
730 entry_pool_size = dn->dn_next_set_offset - totaloffset;
731 dn->dn_entry_pool_size = entry_pool_size;
732 curptr += entry_pool_size;
733 usedspace += entry_pool_size;
734 totaloffset += entry_pool_size;
735
736 if (totaloffset != dn->dn_next_set_offset) {
737 _dwarf_error_string(dbg, error,
738 DW_DLE_DEBUG_NAMES_HEADER_ERROR,
739 "DW_DLE_DEBUG_NAMES_HEADER_ERROR: "
740 "Final total offset does not match base "
741 "calculation. Logic error.");
742 return DW_DLV_ERROR;
743 }
744
745 *usedspace_out = usedspace;
746 *next_offset = dn->dn_next_set_offset;
747 res = fill_in_abbrevs_table(dn,error);
748 if (res != DW_DLV_OK) {
749 free(dn->dn_augmentation_string);
750 dn->dn_augmentation_string = 0;
751 return res;
752 }
753 return DW_DLV_OK;
754 }
755
756 #define FAKE_LAST_USED 0xffffffff
757
758 /* There may be one debug index for an entire object file,
759 for multiple CUs or there can be individual indexes
760 for some CUs.
761 see DWARF5 6.1.1.3 Per_CU versus Per-Module Indexes.
762 The initial of these tables starts at offset 0.
763 If the starting-offset is too high for the section
764 return DW_DLV_NO_ENTRY */
765 int
766 dwarf_dnames_header(Dwarf_Debug dbg,
767 Dwarf_Off starting_offset,
768 Dwarf_Dnames_Head * dn_out,
769 Dwarf_Off * offset_of_next_table,
770 Dwarf_Error * error)
771 {
772 Dwarf_Unsigned remaining = 0;
773 Dwarf_Dnames_Head dn = 0;
774 Dwarf_Unsigned section_size = 0;
775 Dwarf_Unsigned usedspace = 0;
776 Dwarf_Unsigned next_offset = 0;
777 Dwarf_Small *start_section= 0;
778 Dwarf_Small *end_section = 0;
779 Dwarf_Small *curptr = 0;
780 int res = 0;
781
782 CHECK_DBG(dbg,error,"dwarf_dnames_header()");
783 res = _dwarf_load_section(dbg, &dbg->de_debug_names, error);
784 if (res != DW_DLV_OK) {
785 return res;
786 }
787 section_size = dbg->de_debug_names.dss_size;
788 if (!section_size){
789 return DW_DLV_NO_ENTRY;
790 }
791 res = _dwarf_load_section(dbg, &dbg->de_debug_str,error);
792 if (res == DW_DLV_ERROR) {
793 return res;
794 }
795 if (starting_offset >= section_size) {
796 return DW_DLV_NO_ENTRY;
797 }
798 start_section = dbg->de_debug_names.dss_data;
799 curptr = start_section += starting_offset;
800 end_section = start_section + section_size;
801 dn = (Dwarf_Dnames_Head)_dwarf_get_alloc(dbg,
802 DW_DLA_DNAMES_HEAD, 1);
803 if (!dn) {
804 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
805 "DW_DLE_ALLOC_FAIL: dwarf_get_alloc of "
806 "a Dwarf_Dnames_Head record failed.");
807 return DW_DLV_ERROR;
808 }
809 dn->dn_magic = DWARF_DNAMES_MAGIC;
810 dn->dn_section_data = start_section;
811 dn->dn_section_size = section_size;
812 dn->dn_section_end = start_section + section_size;
813 dn->dn_dbg = dbg;
814 dn->dn_section_offset = starting_offset;
815 dn->dn_indextable_data = starting_offset + start_section;;
816 remaining = dn->dn_section_size - starting_offset;
817 res = read_a_name_table_header(dn,
818 starting_offset,
819 remaining,
820 curptr,
821 &usedspace,
822 &next_offset,
823 dn->dn_section_end,
824 error);
825 if (res == DW_DLV_ERROR) {
826 dwarf_dealloc_dnames(dn);
827 return res;
828 }
829 if (res == DW_DLV_NO_ENTRY) {
830 /* Impossible. A bug. Or possibly
831 a bunch of zero pad? */
832 dwarf_dealloc_dnames(dn);
833 return res;
834 }
835 if (usedspace > section_size) {
836 dwarf_dealloc_dnames(dn);
837 _dwarf_error_string(dbg, error,DW_DLE_DEBUG_NAMES_OFF_END,
838 "DW_DLE_DEBUG_NAMES_OFF_END: "
839 " used space > section size");
840 return DW_DLV_ERROR;
841 }
842 remaining -= usedspace;
843 if (remaining && remaining < 15) {
844 /* No more content in here, just padding. Check for zero
845 in padding. */
846 curptr += usedspace;
847 for ( ; curptr < end_section; ++curptr) {
848 if (*curptr) {
849 /* One could argue this is a harmless error,
850 but for now assume it is real corruption. */
851 dwarf_dealloc(dbg,dn,DW_DLA_DNAMES_HEAD);
852 _dwarf_error_string(dbg, error,
853 DW_DLE_DEBUG_NAMES_PAD_NON_ZERO,
854 "DW_DLE_DEBUG_NAMES_PAD_NON_ZERO: "
855 "space at end of valid tables not zeros");
856 return DW_DLV_ERROR;
857 }
858 }
859 }
860 *dn_out = dn;
861 *offset_of_next_table = next_offset;
862 return DW_DLV_OK;
863 }
864
865 /* Frees all the space in dn. It's up to you
866 to to "dn = 0;" after the call. */
867 static void
868 _dwarf_internal_dwarf_dealloc_dnames(Dwarf_Dnames_Head dn)
869 {
870 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
871 return;
872 }
873 free(dn->dn_augmentation_string);
874 dn->dn_augmentation_string = 0;
875 free(dn->dn_bucket_array);
876 dn->dn_bucket_array = 0;
877 free(dn->dn_abbrev_instances);
878 dn->dn_abbrev_instances = 0;
879 dn->dn_abbrev_instance_count = 0;
880 dn->dn_magic = 0;
881 }
882
883 void
884 dwarf_dealloc_dnames(Dwarf_Dnames_Head dn)
885 {
886 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
887 return;
888 }
889 _dwarf_internal_dwarf_dealloc_dnames(dn);
890 /* Now dn_magic 0 so the dwarf_dealloc
891 will just do the base record free. */
892 dwarf_dealloc(dn->dn_dbg,dn,DW_DLA_DNAMES_HEAD);
893 }
894 /* Frees any Dwarf_Dnames_Head_s data content that is directly
895 mallocd, unless such is already done. */
896 void
897 _dwarf_dnames_destructor(void *m)
898 {
899 Dwarf_Dnames_Head dn = (Dwarf_Dnames_Head)m;
900 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
901 return;
902 }
903 _dwarf_internal_dwarf_dealloc_dnames(dn);
904 }
905
906 /* These are the sizes/counts applicable a particular
907 names table (most likely the only one) in the
908 .debug_names section, numbers from the section
909 Dwarf_Dnames header.
910 DWARF5 section 6.1.1.2 Structure of the Name Header. */
911 int dwarf_dnames_sizes(Dwarf_Dnames_Head dn,
912 /* The counts are entry counts, not byte sizes. */
913 Dwarf_Unsigned * comp_unit_count,
914 Dwarf_Unsigned * local_type_unit_count,
915 Dwarf_Unsigned * foreign_type_unit_count,
916 Dwarf_Unsigned * bucket_count,
917 Dwarf_Unsigned * name_count,
918
919 /* The following are counted in bytes */
920 Dwarf_Unsigned * abbrev_table_size,
921 Dwarf_Unsigned * entry_pool_size,
922 Dwarf_Unsigned * augmentation_string_size,
923 char ** augmentation_string,
924 Dwarf_Unsigned * section_size,
925 Dwarf_Half * table_version,
926 Dwarf_Half * offset_size,
927 Dwarf_Error * error)
928 {
929 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
930 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
931 "DW_DLE_DBG_NULL: A call to dwarf_dnames_sizes() "
932 "has a NULL Dwarf_Dnames_Head or an improper one.");
933 return DW_DLV_ERROR;
934 }
935 if (comp_unit_count) {
936 *comp_unit_count = dn->dn_comp_unit_count;
937 }
938 if (local_type_unit_count) {
939 *local_type_unit_count = dn->dn_local_type_unit_count;
940 }
941 if (foreign_type_unit_count) {
942 *foreign_type_unit_count = dn->dn_foreign_type_unit_count;
943 }
944 if (bucket_count) {
945 *bucket_count = dn->dn_bucket_count;
946 }
947 if (name_count) {
948 *name_count = dn->dn_name_count;
949 }
950 if (abbrev_table_size) {
951 *abbrev_table_size = dn->dn_abbrev_table_size;
952 }
953 if (entry_pool_size) {
954 *entry_pool_size = dn->dn_entry_pool_size;
955 }
956 if (augmentation_string_size) {
957 *augmentation_string_size = dn->dn_augmentation_string_size;
958 }
959 if (augmentation_string) {
960 *augmentation_string = dn->dn_augmentation_string;
961 }
962 if (section_size) {
963 *section_size = dn->dn_section_size;
964 }
965 if (table_version) {
966 *table_version = dn->dn_version;
967 }
968 if (offset_size) {
969 *offset_size = dn->dn_offset_size;
970 }
971 return DW_DLV_OK;
972 }
973
974 /* Useful for investigating errors in libdwarf or the
975 .debug_names section. */
976 int
977 dwarf_dnames_offsets(Dwarf_Dnames_Head dn,
978 Dwarf_Unsigned * header_offset,
979 Dwarf_Unsigned * cu_table_offset,
980 Dwarf_Unsigned * tu_local_offset,
981 Dwarf_Unsigned * foreign_tu_offset,
982 Dwarf_Unsigned * bucket_offset,
983 Dwarf_Unsigned * hashes_offset,
984 Dwarf_Unsigned * stringoffsets_offset,
985 Dwarf_Unsigned * entryoffsets_offset,
986 Dwarf_Unsigned * abbrev_table_offset,
987 Dwarf_Unsigned * entry_pool_offset,
988 Dwarf_Error * error)
989 {
990 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
991 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
992 "DW_DLE_DBG_NULL: A call to dwarf_dnames_offsets() "
993 "has a NULL Dwarf_Dnames_Head or an improper one.");
994 return DW_DLV_ERROR;
995 }
996 if (header_offset) {
997 *header_offset = dn->dn_section_offset;
998 }
999 if (cu_table_offset) {
1000 *cu_table_offset = dn->dn_cu_list_offset;
1001 }
1002 if (tu_local_offset) {
1003 *tu_local_offset = dn->dn_local_tu_list_offset;
1004 }
1005 if (foreign_tu_offset) {
1006 *foreign_tu_offset = dn->dn_foreign_tu_list_offset;
1007 }
1008 if (bucket_offset) {
1009 *bucket_offset = dn->dn_buckets_offset;
1010 }
1011 if (hashes_offset) {
1012 *hashes_offset = dn->dn_hash_table_offset;
1013 }
1014 if (stringoffsets_offset) {
1015 *stringoffsets_offset = dn->dn_string_offsets_offset;
1016 }
1017 if (entryoffsets_offset) {
1018 *entryoffsets_offset = dn->dn_entry_offsets_offset;
1019 }
1020 if (abbrev_table_offset) {
1021 *abbrev_table_offset = dn->dn_abbrevs_offset;
1022 }
1023 if (entry_pool_offset) {
1024 *entry_pool_offset = dn->dn_entry_pool_offset;
1025 }
1026 return DW_DLV_OK;
1027 }
1028
1029 /* The "tu" case covers both local type units
1030 and foreign type units.
1031 This table is indexed starting at 0.
1032 */
1033 int
1034 dwarf_dnames_cu_table(Dwarf_Dnames_Head dn,
1035 const char * type /* "cu", "tu" */,
1036 Dwarf_Unsigned index_number,
1037 Dwarf_Unsigned * offset,
1038 Dwarf_Sig8 * sig,
1039 Dwarf_Error * error)
1040 {
1041 Dwarf_Debug dbg = 0;
1042 Dwarf_Unsigned unit_count = 0;
1043 Dwarf_Unsigned total_count = 0;
1044 Dwarf_Unsigned unit_entry_size = 0;
1045 Dwarf_Small * unit_ptr = 0;
1046 Dwarf_Unsigned foreign_count = 0;
1047 Dwarf_Bool offset_case = TRUE;
1048
1049 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
1050 _dwarf_error_string(NULL, error,DW_DLE_DEBUG_NAMES_ERROR,
1051 "DW_DLE_DEBUG_NAMES_ERROR: "
1052 " Dwarf_Dnames_Head is NULL or invalid pointer"
1053 "calling dwarf_dnames_cu_table()");
1054 return DW_DLV_ERROR;
1055 }
1056
1057 dbg = dn->dn_dbg;
1058 if (type[0] == 'c') {
1059 unit_ptr = dn->dn_cu_list;
1060 unit_entry_size = dn->dn_offset_size;
1061 unit_count = dn->dn_comp_unit_count;
1062 total_count = unit_count;
1063 offset_case = TRUE;
1064 } else if (type[0] == 't') {
1065 unit_count = dn->dn_local_type_unit_count;
1066 foreign_count = dn->dn_foreign_type_unit_count;
1067 total_count = unit_count + foreign_count;
1068 if (index_number < dn->dn_local_type_unit_count) {
1069 unit_ptr = dn->dn_local_tu_list;
1070 unit_entry_size = dn->dn_offset_size;
1071 offset_case = TRUE;
1072 } else {
1073 unit_ptr = dn->dn_foreign_tu_list;
1074 unit_entry_size = sizeof(Dwarf_Sig8);
1075 offset_case = FALSE;
1076 }
1077 } else {
1078 _dwarf_error_string(dbg,error,DW_DLE_DEBUG_NAMES_ERROR,
1079 "DW_DLE_DEBUG_NAMES_ERROR: "
1080 "type string is not start with cu or tu"
1081 "so invalid call to dwarf_dnames_cu_table()");
1082 return DW_DLV_ERROR;
1083 }
1084 if (index_number >= total_count) {
1085 return DW_DLV_NO_ENTRY;
1086 }
1087 if (offset_case) {
1088 /* CU or TU ref */
1089 Dwarf_Unsigned offsetval = 0;
1090 Dwarf_Small *ptr = unit_ptr +
1091 (index_number) *unit_entry_size;
1092 Dwarf_Small *endptr = dn->dn_indextable_data_end;
1093
1094 READ_UNALIGNED_CK(dbg, offsetval, Dwarf_Unsigned,
1095 ptr, (unsigned long)unit_entry_size,
1096 error,endptr);
1097 if (offset) {
1098 *offset = offsetval;
1099 }
1100 return DW_DLV_OK;
1101 }
1102 {
1103 Dwarf_Small *ptr = unit_ptr +
1104 (index_number -unit_count) *unit_entry_size;
1105 if (sig) {
1106 memcpy(sig,ptr,sizeof(*sig));
1107 }
1108 }
1109 return DW_DLV_OK;
1110 }
1111
1112 /* Each bucket gives the index of the first of
1113 the name entries for the bucket
1114 index_of_entry returns the name table entry.
1115 indexcount returns the number of name table entries
1116 (representing name collisions) for this bucket. */
1117 static int
1118 _dwarf_initialize_bucket_details(Dwarf_Dnames_Head dn,
1119 Dwarf_Error * error)
1120 {
1121 Dwarf_Debug dbg = 0;
1122 Dwarf_Unsigned i = 0;
1123 struct Dwarf_DN_Bucket_s *curbucket = 0;
1124
1125 dbg = dn->dn_dbg;
1126 if (dn->dn_bucket_array) {
1127 return DW_DLV_OK;
1128 }
1129 if (!dn->dn_bucket_count) {
1130 return DW_DLV_NO_ENTRY;
1131 }
1132 dn->dn_bucket_array = (struct Dwarf_DN_Bucket_s*)
1133 calloc(dn->dn_bucket_count,sizeof(struct Dwarf_DN_Bucket_s));
1134 if (!dn->dn_bucket_array) {
1135 _dwarf_error_string(dbg,error,DW_DLE_ALLOC_FAIL,
1136 "DW_DLE_ALLOC_FAIL: "
1137 ".debug_names bucket array could not be allocated");
1138 return DW_DLV_ERROR;
1139 }
1140
1141 curbucket = dn->dn_bucket_array ;
1142 for (i = 0 ; i < dn->dn_bucket_count; ++i) {
1143 Dwarf_Unsigned offsetval = 0;
1144 Dwarf_Small *ptr = dn->dn_buckets +
1145 i * DWARF_32BIT_SIZE;
1146 Dwarf_Small *endptr = dn->dn_buckets+
1147 dn->dn_bucket_count*DWARF_32BIT_SIZE;
1148
1149 READ_UNALIGNED_CK(dbg, offsetval, Dwarf_Unsigned,
1150 ptr, DWARF_32BIT_SIZE,
1151 error,endptr);
1152 curbucket = dn->dn_bucket_array +i;
1153 curbucket->db_nameindex = offsetval;
1154 }
1155 for (i = 0; i < dn->dn_bucket_count; ) {
1156 Dwarf_Unsigned j = 0;
1157
1158 curbucket = dn->dn_bucket_array+i;
1159 if (!curbucket->db_nameindex) {
1160 ++i;
1161 continue;
1162 }
1163 for (j = i+1; j < dn->dn_bucket_count; ++j) {
1164 struct Dwarf_DN_Bucket_s *partial =
1165 dn->dn_bucket_array+j;
1166
1167 if (partial->db_nameindex) {
1168 curbucket->db_collisioncount =
1169 partial->db_nameindex - curbucket->db_nameindex;
1170 i = j;
1171 break;
1172 }
1173 }
1174 if (j >= dn->dn_bucket_count) {
1175 /* Ran off end */
1176 curbucket->db_collisioncount =
1177 dn->dn_name_count - curbucket->db_nameindex;
1178 if (!curbucket->db_collisioncount) {
1179 curbucket->db_collisioncount = 1;
1180 }
1181 break;
1182 }
1183 }
1184 return DW_DLV_OK;
1185 }
1186
1187 int
1188 dwarf_dnames_bucket(Dwarf_Dnames_Head dn,
1189 Dwarf_Unsigned bucket_number,
1190 Dwarf_Unsigned * name_index,
1191 Dwarf_Unsigned * collision_count,
1192 Dwarf_Error * error)
1193 {
1194 struct Dwarf_DN_Bucket_s *cur = 0;
1195 int res = 0;
1196
1197 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
1198 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1199 "DW_DLE_DBG_NULL: bad Head argument to "
1200 "dwarf_dnames_bucket");
1201 return DW_DLV_ERROR;
1202 }
1203 if (bucket_number >= dn->dn_bucket_count) {
1204 return DW_DLV_NO_ENTRY;
1205 }
1206 res = _dwarf_initialize_bucket_details(dn,error);
1207 if (res == DW_DLV_ERROR) {
1208 return res;
1209 }
1210 if (!dn->dn_bucket_array) {
1211 return DW_DLV_NO_ENTRY;
1212 }
1213 cur = dn->dn_bucket_array + bucket_number;
1214 *name_index = cur->db_nameindex;
1215 *collision_count = cur->db_collisioncount;
1216 return DW_DLV_OK;
1217 }
1218 static int
1219 get_hash_value_number(Dwarf_Dnames_Head dn,
1220 Dwarf_Unsigned name_index,
1221 Dwarf_Unsigned *hash_value,
1222 Dwarf_Error *error)
1223 {
1224 Dwarf_Unsigned hash_val = 0;
1225 int res = 0;
1226 Dwarf_Small * hashesentry = 0;
1227 Dwarf_Small *end = 0;
1228
1229 if (!dn->dn_bucket_count) {
1230 return DW_DLV_NO_ENTRY;
1231 }
1232 hashesentry = dn->dn_hash_table + (name_index-1)*DWARF_32BIT_SIZE;
1233 end = dn->dn_hash_table + DWARF_32BIT_SIZE*(dn->dn_name_count+1);
1234 res = read_uword_val(dn->dn_dbg,
1235 &hashesentry,
1236 end,
1237 DW_DLE_DEBUG_NAMES_OFF_END,
1238 &hash_val,
1239 0xffffffff, /* hashes fit in 32 bits */
1240 error);
1241 if (res != DW_DLV_OK) {
1242 return res;
1243 }
1244 *hash_value = hash_val;
1245 return DW_DLV_OK;
1246 }
1247
1248 static int
1249 get_bucket_number(Dwarf_Dnames_Head dn,
1250 Dwarf_Unsigned name_index,
1251 Dwarf_Unsigned *bucket_num)
1252 {
1253 Dwarf_Unsigned i = 0;
1254
1255 if (!dn->dn_bucket_count) {
1256 return DW_DLV_NO_ENTRY;
1257 }
1258 if (!dn->dn_bucket_array) {
1259 return DW_DLV_NO_ENTRY;
1260 }
1261 /* Binary search would be better FIXME */
1262 for (i = 0; i < dn->dn_bucket_count; ++i) {
1263 Dwarf_Unsigned bindx = 0;
1264 Dwarf_Unsigned ccount = 0;
1265 Dwarf_Unsigned lastbindx = 0;
1266 struct Dwarf_DN_Bucket_s *cur = dn->dn_bucket_array +i;
1267
1268 bindx = cur->db_nameindex;
1269 ccount = cur->db_collisioncount;
1270 lastbindx = ccount + bindx -1;
1271 if (name_index > lastbindx) {
1272 continue;
1273 }
1274 if (!bindx ) {
1275 /* empty bucket */
1276 continue;
1277 }
1278 if (bindx == name_index) {
1279 *bucket_num = i;
1280 return DW_DLV_OK;
1281 }
1282 if (name_index <= lastbindx) {
1283 *bucket_num = i;
1284 return DW_DLV_OK;
1285 }
1286 }
1287 /* ? is it an error? */
1288 return DW_DLV_NO_ENTRY;
1289 }
1290
1291 /* We try to protect against bogus arguments,
1292 but all we can do is check for null. */
1293 int
1294 dwarf_dnames_abbrevtable(Dwarf_Dnames_Head dn,
1295 Dwarf_Unsigned index,
1296 Dwarf_Unsigned *abbrev_offset,
1297 Dwarf_Unsigned *abbrev_code,
1298 Dwarf_Unsigned *abbrev_tag,
1299 Dwarf_Unsigned array_size,
1300 Dwarf_Half * idxattr_array,
1301 Dwarf_Half * form_array,
1302 Dwarf_Unsigned * attr_count)
1303 {
1304 struct Dwarf_D_Abbrev_s *ab = 0;
1305 Dwarf_Unsigned abnumber = 0;
1306 Dwarf_Unsigned abmax = 0;
1307
1308 if (!dn) {
1309 return DW_DLV_NO_ENTRY;
1310 }
1311 if (!idxattr_array || !form_array) {
1312 return DW_DLV_NO_ENTRY;
1313 }
1314 if (index >= dn->dn_abbrev_instance_count) {
1315 return DW_DLV_NO_ENTRY;
1316 }
1317 ab = dn->dn_abbrev_instances + index;
1318 if (abbrev_offset) {
1319 *abbrev_offset = ab->da_abbrev_offset;
1320 }
1321 if (abbrev_code) {
1322 *abbrev_code = ab->da_abbrev_code;
1323 }
1324 if (abbrev_tag) {
1325 *abbrev_tag = ab->da_tag;
1326 }
1327 abmax = ab->da_pairs_count;
1328 if (attr_count) {
1329 *attr_count = abmax;
1330 }
1331 if (array_size < abmax) {
1332 abmax = array_size;
1333 }
1334 for ( ; abnumber < abmax; ++abnumber) {
1335 idxattr_array[abnumber] = ab->da_idxattr[abnumber];
1336 form_array[abnumber] = ab->da_form[abnumber];
1337 }
1338 return DW_DLV_OK;
1339 }
1340
1341 static int
1342 _dwarf_read_abbrev_code_from_pool(Dwarf_Dnames_Head dn,
1343 Dwarf_Unsigned entrypooloffset,
1344 Dwarf_Unsigned *code,
1345 Dwarf_Error *error)
1346 {
1347 Dwarf_Small *epool = 0;
1348 Dwarf_Small *end = 0;
1349 Dwarf_Debug dbg = 0;
1350 int res = 0;
1351
1352 epool = dn->dn_entry_pool;
1353 end = epool + dn->dn_entry_pool_size;
1354 if (entrypooloffset >= dn->dn_entry_pool_size) {
1355 _dwarf_error_string(dbg,error,DW_DLE_DEBUG_NAMES_ERROR,
1356 "DW_DLE_DEBUG_NAMES_ERROR: "
1357 "The entry pool offset from the names table "
1358 " is out of bounds.");
1359 return DW_DLV_ERROR;
1360 }
1361 epool += entrypooloffset;
1362 dbg = dn->dn_dbg;
1363 res = _dwarf_read_uleb_ck(&epool,
1364 code,dbg,error,end);
1365 if (res == DW_DLV_ERROR) {
1366 return res;
1367 }
1368 return DW_DLV_OK;
1369 }
1370
1371 /* Starting out with a simple linear search.
1372 Better to use a dwarf_tsearch function. */
1373 static int
1374 _dwarf_find_abbrev_for_code(Dwarf_Dnames_Head dn,
1375 Dwarf_Unsigned code,
1376 struct Dwarf_D_Abbrev_s **abbrevdata,
1377 Dwarf_Error *error)
1378 {
1379 Dwarf_Unsigned i = 0;
1380 struct Dwarf_D_Abbrev_s *ap =0;
1381
1382 ap = dn->dn_abbrev_instances;
1383 for (i = 0; i < dn->dn_abbrev_instance_count; ++i,++ap) {
1384 if (ap->da_abbrev_code == code) {
1385 *abbrevdata = ap;
1386 return DW_DLV_OK;
1387 }
1388 }
1389 {
1390 Dwarf_Debug dbg = 0;
1391 dwarfstring m;
1392
1393 dbg = dn->dn_dbg;
1394 dwarfstring_constructor(&m);
1395 dwarfstring_append_printf_u(&m,
1396 "DW_DLE_DEBUG_NAMES_ERROR: "
1397 "The abbrev code %u",code);
1398 dwarfstring_append_printf_u(&m,
1399 "(0x%x) ",code);
1400 dwarfstring_append(&m,
1401 "from the entry pool "
1402 "is absent from the abbrev table.");
1403 _dwarf_error_string(dbg,error,DW_DLE_DEBUG_NAMES_ERROR,
1404 dwarfstring_string(&m));
1405 dwarfstring_destructor(&m);
1406 }
1407 return DW_DLV_ERROR;
1408 }
1409
1410 static int
1411 _dwarf_fill_in_attr_form(Dwarf_Dnames_Head dn,
1412 struct Dwarf_D_Abbrev_s *abbrevdata,
1413 Dwarf_Half *idxattr_array,
1414 Dwarf_Half *form_array,
1415 Dwarf_Unsigned array_size,
1416 Dwarf_Error *error)
1417 {
1418 Dwarf_Unsigned limit = abbrevdata->da_pairs_count;
1419 Dwarf_Unsigned i = 0;
1420 Dwarf_Debug dbg = 0;
1421
1422 if (limit > array_size) {
1423 limit = array_size;
1424 }
1425 for ( ; i < limit ; ++i) {
1426 idxattr_array[i] = abbrevdata->da_idxattr[i];
1427 form_array[i] = abbrevdata->da_form[i];
1428 }
1429 if (i < limit) {
1430 dwarfstring m;
1431
1432 dbg= dn->dn_dbg;
1433 dwarfstring_constructor(&m);
1434 dwarfstring_append_printf_u(&m,
1435 "DW_DLE_DEBUG_NAMES_ERROR: "
1436 "At attr-form pair ",i);
1437 dwarfstring_append_printf_u(&m,
1438 ", with attr-form max of %u",
1439 abbrevdata->da_pairs_count);
1440 dwarfstring_append_printf_u(&m,
1441 ", and limit of of %u",
1442 limit);
1443 dwarfstring_append(&m,
1444 " something is very wrong. "
1445 "a pairs pointer is null");
1446 _dwarf_error_string(dbg,error,DW_DLE_DEBUG_NAMES_ERROR,
1447 dwarfstring_string(&m));
1448 dwarfstring_destructor(&m);
1449 }
1450 return DW_DLV_OK;
1451
1452 }
1453
1454 /* Each Name Table entry, one at a time.
1455 It is not an error if array_size is zero or
1456 small. Check the returned attr_count to
1457 know now much of array filled in and
1458 if the array you provided is
1459 large enough. Possibly 40 is
1460 sufficient.
1461 name indexes start at 1. */
1462 int
1463 dwarf_dnames_name(Dwarf_Dnames_Head dn,
1464 Dwarf_Unsigned name_index,
1465 Dwarf_Unsigned * bucket_number,
1466 Dwarf_Unsigned * hash_value,
1467 Dwarf_Unsigned * offset_to_debug_str,
1468 char * * ptrtostr ,
1469 Dwarf_Unsigned * offset_in_entrypool,
1470 Dwarf_Unsigned * abbrev_code,
1471 Dwarf_Half * abbrev_tag,
1472 Dwarf_Unsigned array_size,
1473 Dwarf_Half * idxattr_array,
1474 Dwarf_Half * form_array,
1475 Dwarf_Unsigned * attr_count,
1476 Dwarf_Error * error)
1477 {
1478 Dwarf_Debug dbg = 0;
1479 int res = 0;
1480 Dwarf_Unsigned entrypooloffset = 0;
1481 Dwarf_Unsigned debugstroffset = 0;
1482 Dwarf_Small * strpointer = 0;
1483 struct Dwarf_D_Abbrev_s *abbrevdata = 0;
1484 Dwarf_Unsigned code = 0;
1485
1486 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
1487 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1488 "DW_DLE_DBG_NULL: "
1489 "a call to dwarf_dnames_name() "
1490 "Passes in a NULL or uninitialized pointer");
1491 return DW_DLV_ERROR;
1492 }
1493 dbg = dn->dn_dbg;
1494 if (IS_INVALID_DBG(dbg)) {
1495 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1496 "DW_DLE_DBG_NULL: "
1497 "a call to dwarf_dnames_name() "
1498 "finds a NULL Dwarf_Debug in a Dwarf_Dnames_Head");
1499 return DW_DLV_ERROR;
1500 }
1501 if (!name_index || name_index > dn->dn_name_count) {
1502 return DW_DLV_NO_ENTRY;
1503 }
1504 res = get_bucket_number(dn,name_index,bucket_number);
1505 if (res == DW_DLV_OK) {
1506 res = get_hash_value_number(dn,name_index,hash_value,error);
1507 if (res == DW_DLV_ERROR) {
1508 return res;
1509 }
1510 }
1511 {
1512 Dwarf_Small *ptr = dn->dn_string_offsets +
1513 (name_index-1) * dn->dn_offset_size;
1514 Dwarf_Small *endptr = dn->dn_abbrevs;
1515
1516 READ_UNALIGNED_CK(dbg, debugstroffset, Dwarf_Unsigned,
1517 ptr, dn->dn_offset_size,
1518 error,endptr);
1519 if (offset_to_debug_str) {
1520 *offset_to_debug_str = debugstroffset;
1521 }
1522 }
1523 /* Get str ptr from .debug_str */
1524 {
1525 Dwarf_Small *secdataptr = 0;
1526 Dwarf_Unsigned secdatalen = 0;
1527 Dwarf_Small *secend = 0;
1528 int res_s = 0;
1529
1530 secdataptr = (Dwarf_Small *)dbg->de_debug_str.dss_data;
1531 secdatalen = dbg->de_debug_str.dss_size;
1532 secend = secdataptr+secdatalen;
1533 strpointer = secdataptr +debugstroffset;
1534 res_s = _dwarf_check_string_valid(dbg,
1535 secdataptr,strpointer,secend,
1536 DW_DLE_FORM_STRING_BAD_STRING,error);
1537 if (res_s != DW_DLV_OK) {
1538 return res_s;
1539 }
1540 if (ptrtostr) {
1541 *ptrtostr = (char *)strpointer;
1542 }
1543 }
1544
1545 {
1546 Dwarf_Small *ptr = dn->dn_entry_offsets +
1547 (name_index-1) * dn->dn_offset_size;
1548 Dwarf_Small *endptr = dn->dn_abbrevs;
1549 /* offsets relative to the start of the
1550 entry_pool */
1551
1552 READ_UNALIGNED_CK(dbg, entrypooloffset, Dwarf_Unsigned,
1553 ptr, dn->dn_offset_size,
1554 error,endptr);
1555 if (entrypooloffset >= dn->dn_entry_pool_size) {
1556 _dwarf_error_string(dbg, error,DW_DLE_DEBUG_NAMES_ERROR,
1557 "DW_DLE_DEBUG_NAMES_ERROR: "
1558 "The entrypool offset read is larger than"
1559 "the entrypool size");
1560 return DW_DLV_ERROR;
1561 }
1562 if (offset_in_entrypool) {
1563 *offset_in_entrypool = entrypooloffset;
1564 }
1565 }
1566 /* Find abbrev code at the given entry offset */
1567 res = _dwarf_read_abbrev_code_from_pool(dn,
1568 entrypooloffset, &code,error);
1569 if (res != DW_DLV_OK) {
1570 return res;
1571 }
1572 res = _dwarf_find_abbrev_for_code(dn,code,&abbrevdata,error);
1573 if (res == DW_DLV_ERROR) {
1574 return res;
1575 }
1576 if (res == DW_DLV_NO_ENTRY) {
1577 return res;
1578 }
1579
1580 if (abbrev_code) {
1581 *abbrev_code = code;
1582 }
1583 if (code && abbrev_tag) {
1584 *abbrev_tag = (Dwarf_Half)abbrevdata->da_tag;
1585 }
1586 if (code) {
1587 if (attr_count) {
1588 *attr_count = abbrevdata->da_pairs_count;
1589 }
1590 res = _dwarf_fill_in_attr_form(dn,abbrevdata,idxattr_array,
1591 form_array, array_size,error);
1592 if (res == DW_DLV_ERROR) {
1593 return res;
1594 }
1595 }
1596 return DW_DLV_OK;
1597 }
1598
1599 /* If abbrev_code returned is zero there is no tag returned
1600 and we are at the end of the entry pool set for this name
1601 entry.
1602 abbrev code, tag
1603 attr,form
1604 ...
1605 0,0
1606 ... repeat like the above
1607 0
1608 */
1609
1610 static int
1611 _dwarf_internal_abbrev_by_code(Dwarf_Dnames_Head dn,
1612 Dwarf_Unsigned abbrev_code,
1613 Dwarf_Half *tag,
1614 Dwarf_Unsigned *index_of_abbrev,
1615 Dwarf_Unsigned *number_of_attr_form_entries)
1616 {
1617 Dwarf_Unsigned n = 0;
1618 struct Dwarf_D_Abbrev_s * abbrev = 0;
1619
1620 abbrev = dn->dn_abbrev_instances;
1621 for (n = 0; n < dn->dn_abbrev_instance_count; ++n,++abbrev) {
1622 if (abbrev_code == abbrev->da_abbrev_code) {
1623 if (tag) {
1624 *tag = (Dwarf_Half)abbrev->da_tag;
1625 }
1626 if (index_of_abbrev) {
1627 *index_of_abbrev = n;
1628 }
1629 if (number_of_attr_form_entries) {
1630 *number_of_attr_form_entries = abbrev->da_pairs_count;
1631 }
1632 return DW_DLV_OK;
1633 }
1634 }
1635 /* Something is wrong, not found! */
1636 return DW_DLV_NO_ENTRY;
1637 }
1638
1639 /* This, combined with dwarf_dnames_entrypool_values(),
1640 lets one examine as much or as little of an entrypool
1641 as one wants to by alternately calling these two
1642 functions.
1643 */
1644
1645 int dwarf_dnames_entrypool(Dwarf_Dnames_Head dn,
1646 Dwarf_Unsigned offset_in_entrypool,
1647 Dwarf_Unsigned *abbrev_code,
1648 Dwarf_Half *tag,
1649 Dwarf_Unsigned *value_count,
1650 Dwarf_Unsigned *index_of_abbrev,
1651 Dwarf_Unsigned *offset_of_initial_value,
1652 Dwarf_Error *error)
1653 {
1654 Dwarf_Debug dbg = 0;
1655 int res = 0;
1656 Dwarf_Small *entrypool = 0;
1657 Dwarf_Small *endentrypool = 0;
1658 Dwarf_Unsigned abcode = 0;
1659 Dwarf_Unsigned leblen = 0;
1660 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
1661 _dwarf_error(NULL, error,DW_DLE_DBG_NULL);
1662 return DW_DLV_ERROR;
1663 }
1664
1665 dbg = dn->dn_dbg;
1666 if (offset_in_entrypool >= dn->dn_entry_pool_size) {
1667 _dwarf_error(NULL, error,DW_DLE_DEBUG_NAMES_ENTRYPOOL_OFFSET);
1668 return DW_DLV_ERROR;
1669 }
1670 endentrypool = dn->dn_entry_pool +dn->dn_entry_pool_size;
1671 entrypool = dn->dn_entry_pool + offset_in_entrypool;
1672
1673 DECODE_LEB128_UWORD_LEN_CK(entrypool,abcode,leblen,
1674 dbg,error,endentrypool);
1675
1676 res = _dwarf_internal_abbrev_by_code(dn,
1677 abcode,
1678 tag, index_of_abbrev,
1679 value_count);
1680 if (res != DW_DLV_OK) {
1681 /* Never DW_DLV_ERROR (so far) */
1682 return res;
1683 }
1684 *offset_of_initial_value = offset_in_entrypool + leblen;
1685 *abbrev_code = abcode;
1686 return DW_DLV_OK;
1687 }
1688
1689 static int
1690 isformrefval(Dwarf_Debug dbg,Dwarf_Half form,
1691 Dwarf_Small *poolptr,
1692 Dwarf_Small *endpool,
1693 Dwarf_Unsigned *val,
1694 Dwarf_Unsigned *bytesread,
1695 Dwarf_Error *error)
1696 {
1697 Dwarf_Unsigned localval = 0;
1698
1699 if (poolptr >= endpool) {
1700 dwarfstring m;
1701 const char * formname = 0;
1702
1703 dwarfstring_constructor(&m);
1704 dwarfstring_append_printf_u(&m,
1705 "DW_DLE_DEBUG_NAMES_OFF_END: "
1706 "Reading data of form 0x%02x ",form);
1707 dwarf_get_FORM_name((unsigned int)form,&formname);
1708 dwarfstring_append_printf_s(&m,
1709 "%s from entrypool would read "
1710 "off the end of the pool",
1711 (char *)formname);
1712 _dwarf_error_string(dbg,error,
1713 DW_DLE_DEBUG_NAMES_OFF_END,
1714 dwarfstring_string(&m));
1715 dwarfstring_destructor(&m);
1716 return DW_DLV_ERROR;
1717 }
1718 switch(form) {
1719 case DW_FORM_ref1:
1720 *val = *poolptr;
1721 *bytesread = 1;
1722 break;
1723 case DW_FORM_ref2:
1724 READ_UNALIGNED_CK(dbg, localval, Dwarf_Unsigned,
1725 poolptr, DWARF_HALF_SIZE,
1726 error,endpool);
1727 *bytesread = DWARF_HALF_SIZE;
1728 *val = localval;
1729 break;
1730 case DW_FORM_ref4:
1731 READ_UNALIGNED_CK(dbg, localval, Dwarf_Unsigned,
1732 poolptr, DWARF_32BIT_SIZE,
1733 error,endpool);
1734 *bytesread = DWARF_32BIT_SIZE;
1735 *val = localval;
1736 break;
1737 case DW_FORM_ref8:
1738 default:
1739 return DW_DLV_NO_ENTRY;
1740 }
1741 return DW_DLV_OK;
1742 }
1743 /* Caller, knowing array size needed, passes in arrays
1744 it allocates of for idx, form, offset-size-values,
1745 and signature values. Caller must examine idx-number
1746 and form to decide, for each array element, whether
1747 the offset or the signature contains the value.
1748 So this returns all the values for the abbrev code.
1749 And points via offset_of_next to the next abbrev code.
1750
1751 While an array of structs would be easier for the caller
1752 to allocate than parallel arrays, public structs have
1753 turned out to be difficult to work with as interfaces
1754 (as formats change over time).
1755 */
1756 int dwarf_dnames_entrypool_values(Dwarf_Dnames_Head dn,
1757 Dwarf_Unsigned index_of_abbrev,
1758 Dwarf_Unsigned offset_in_entrypool_of_values,
1759 Dwarf_Unsigned array_sizes,
1760 Dwarf_Half * array_dw_idx_number,
1761 Dwarf_Half * array_form,
1762 Dwarf_Unsigned * array_of_offsets,
1763 Dwarf_Sig8 * array_of_signatures,
1764 Dwarf_Bool * single_cu,
1765 Dwarf_Unsigned * single_cu_offset,
1766 /* offset of the next entrypool entry. */
1767 Dwarf_Unsigned * offset_of_next_entrypool,
1768 Dwarf_Error * error)
1769 {
1770 struct Dwarf_D_Abbrev_s * abbrev = 0;
1771 Dwarf_Debug dbg = 0;
1772 unsigned n = 0;
1773 int res = 0;
1774 Dwarf_Unsigned abcount = 0;
1775 Dwarf_Unsigned pooloffset =
1776 offset_in_entrypool_of_values;
1777 Dwarf_Small * endpool = 0;
1778 Dwarf_Small * poolptr = 0;
1779 Dwarf_Unsigned bytesread = 0;
1780
1781 if (!dn || dn->dn_magic != DWARF_DNAMES_MAGIC) {
1782 _dwarf_error_string(NULL, error,DW_DLE_DBG_NULL,
1783 "DW_DLE_DBG_NULL: bad Head argument to "
1784 "dwarf_dnames_entrypool_values");
1785 return DW_DLV_ERROR;
1786 }
1787 dbg = dn->dn_dbg;
1788 endpool = dn->dn_entry_pool + dn->dn_entry_pool_size;
1789 if (pooloffset >= dn->dn_entry_pool_size) {
1790 /* make error or harmless error? */
1791 return DW_DLV_NO_ENTRY;
1792 }
1793
1794 if (index_of_abbrev >= dn->dn_abbrev_instance_count) {
1795 /* make error or harmless error? */
1796 return DW_DLV_NO_ENTRY;
1797 }
1798
1799 poolptr = dn->dn_entry_pool + pooloffset;
1800 abbrev = dn->dn_abbrev_instances + index_of_abbrev;
1801 abcount = abbrev->da_pairs_count;
1802 if (!abcount) {
1803 return DW_DLV_NO_ENTRY;
1804 }
1805 if (abcount > array_sizes) {
1806 dwarfstring m;
1807
1808 dwarfstring_constructor(&m);
1809 dwarfstring_append_printf_u(&m,
1810 "DW_DLE_DEBUG_NAMES_OFF_END: "
1811 "The size of the pair of arrays "
1812 "passed to dwarf_dnames_entrypool_values "
1813 "is %u",
1814 array_sizes);
1815 dwarfstring_append_printf_u(&m,
1816 " but the entry requires %u entries.",
1817 abcount);
1818 _dwarf_error_string(dbg,error,
1819 DW_DLE_DEBUG_NAMES_OFF_END,
1820 dwarfstring_string(&m));
1821 dwarfstring_destructor(&m);
1822 return DW_DLV_ERROR;
1823 }
1824 if (abcount > dn->dn_entry_pool_size) {
1825 /* Just looking for a horrible giant value */
1826 dwarfstring m;
1827
1828 dwarfstring_constructor(&m);
1829 dwarfstring_append_printf_u(&m,
1830 "DW_DLE_DEBUG_NAMES_ABBREV_CORRUPTION: "
1831 "The abbrev count for this entry pool entry"
1832 " is %u, impossibly large. Corrupt data",
1833 abcount);
1834 _dwarf_error_string(dbg,error,
1835 DW_DLE_DEBUG_NAMES_ABBREV_CORRUPTION,
1836 dwarfstring_string(&m));
1837 dwarfstring_destructor(&m);
1838 return DW_DLV_ERROR;
1839 }
1840 for (n = 0; n < abcount ; ++n) {
1841 Dwarf_Half idxtype = 0;
1842 Dwarf_Half form = 0;
1843
1844 idxtype = abbrev->da_idxattr[n];
1845 form = abbrev->da_form[n];
1846 array_dw_idx_number[n] = idxtype;
1847 array_form[n] = form;
1848
1849 if (!idxtype && !form) {
1850 break;
1851 }
1852
1853 if (form == DW_FORM_data8 && idxtype == DW_IDX_type_hash) {
1854 if ((poolptr + sizeof(Dwarf_Sig8)) > endpool){
1855 _dwarf_error(dbg,error,
1856 DW_DLE_DEBUG_NAMES_ENTRYPOOL_OFFSET);
1857 return DW_DLV_ERROR;
1858 }
1859 bytesread = sizeof(Dwarf_Sig8);
1860 memcpy(array_of_signatures+n,
1861 poolptr,bytesread);
1862 poolptr += bytesread;
1863 pooloffset += bytesread;
1864 continue;
1865 } else if (_dwarf_allow_formudata(form)) {
1866 Dwarf_Unsigned val = 0;
1867 res = _dwarf_formudata_internal(dbg,0,form,poolptr,
1868 endpool,&val,&bytesread,error);
1869 if (res != DW_DLV_OK) {
1870 return res;
1871 }
1872 poolptr += bytesread;
1873 pooloffset += bytesread;
1874
1875 array_of_offsets[n] = val;
1876 continue;
1877 } else if (form == DW_FORM_flag_present) {
1878 array_of_offsets[n] = 1;
1879 /* No change to poolptr or pooloffset */
1880 continue;
1881 } else {
1882 Dwarf_Unsigned val = 0;
1883 res =isformrefval(dbg,form,poolptr,
1884 endpool,&val,&bytesread,error);
1885 if (res == DW_DLV_ERROR) {
1886 return res;
1887 }
1888 if (res == DW_DLV_OK) {
1889 poolptr += bytesread;
1890 if (poolptr >= endpool) {
1891 _dwarf_error_string(dbg,error,
1892 DW_DLE_DEBUG_NAMES_ENTRYPOOL_OFFSET,
1893 "DW_DLE_DEBUG_NAMES_ENTRYPOOL_OFFSET:"
1894 " a DW_FORM_ref* would read past end"
1895 " of the entrypool");
1896 return DW_DLV_ERROR;
1897 }
1898 pooloffset += bytesread;
1899 array_of_offsets[n] = val;
1900 continue;
1901 }
1902 }
1903 /* There is some mistake/omission in our code here or in
1904 the data. */
1905 {
1906 dwarfstring m;
1907 const char *name = "<unexpected form>";
1908
1909 dwarfstring_constructor(&m);
1910 dwarfstring_append_printf_u(&m,
1911 "DW_DLE_DEBUG_NAMES_UNHANDLED_FORM: Form 0x%x",
1912 form);
1913 dwarf_get_FORM_name(form,&name);
1914 dwarfstring_append_printf_s(&m,
1915 " %s is not currently supported for .debug_names ",
1916 (char *)name);
1917 _dwarf_error_string(dbg,error,
1918 DW_DLE_DEBUG_NAMES_UNHANDLED_FORM,
1919 dwarfstring_string(&m));
1920 dwarfstring_destructor(&m);
1921 }
1922 return DW_DLV_ERROR;
1923 }
1924 if ( dn->dn_single_cu) {
1925 if (single_cu && single_cu_offset) {
1926 *single_cu = dn->dn_single_cu;
1927 *single_cu_offset = dn->dn_single_cu_offset;
1928 }
1929 }
1930 *offset_of_next_entrypool = pooloffset;
1931 return DW_DLV_OK;
1932 }