Debian Codegraph @

src/lib/libdwarf/dwarf_global.c

raw
1 /*
2
3 Copyright (C) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 Portions Copyright (C) 2007-2011 David Anderson. All Rights Reserved.
5
6 This program is free software; you can redistribute it
7 and/or modify it under the terms of version 2.1 of the
8 GNU Lesser General Public License as published by the Free
9 Software Foundation.
10
11 This program is distributed in the hope that it would be
12 useful, but WITHOUT ANY WARRANTY; without even the implied
13 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE.
15
16 Further, this software is distributed without any warranty
17 that it is free of the rightful claim of any third person
18 regarding infringement or the like. Any license provided
19 herein, whether implied or otherwise, applies only to this
20 software file. Patent licenses, if any, provided herein
21 do not apply to combinations of this program with other
22 software, or any other product whatsoever.
23
24 You should have received a copy of the GNU Lesser General
25 Public License along with this program; if not, write the
26 Free Software Foundation, Inc., 51 Franklin Street - Fifth
27 Floor, Boston MA 02110-1301, USA.
28
29 */
30
31 #include <config.h>
32 #include <stdio.h>
33
34 #include <string.h> /* strlen() */
35 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
36 #include "stdafx.h"
37 #endif /* HAVE_STDAFX_H */
38
39 #include "dwarf.h"
40 #include "libdwarf.h"
41 #include "libdwarf_private.h"
42 #include "dwarf_base_types.h"
43 #include "dwarf_opaque.h"
44 #include "dwarf_alloc.h"
45 #include "dwarf_error.h"
46 #include "dwarf_util.h"
47 #include "dwarf_string.h"
48 #include "dwarf_global.h"
49
50 #ifdef __sgi /* __sgi should only be defined for IRIX/MIPS. */
51 /* The 'fixup' here intended for IRIX targets only.
52 With a 2+GB Elf64 IRIX executable (under 4GB in size),
53 some DIE offsets wrongly
54 got the 32bit upper bit sign extended. For the cu-header
55 offset in the .debug_pubnames section and in the
56 .debug_aranges section.
57 the 'varp' here is a pointer to an offset into .debug_info.
58 We fix up the offset here if it seems advisable..
59
60 As of June 2005 we have identified a series of mistakes
61 in ldx64 that can cause this (64 bit values getting passed
62 thru 32-bit signed knothole).
63 */
64 void
65 _dwarf_fix_up_offset_irix(Dwarf_Debug dbg,
66 Dwarf_Unsigned * varp, char *caller_site_name)
67 {
68
69 Dwarf_Unsigned var = *varp;
70
71 #define UPPER33 0xffffffff80000000LL
72 #define LOWER32 0xffffffffLL
73 /* Restrict the hack to the known case. Upper 32 bits erroneously
74 sign extended from lower 32 upper bit. */
75 if ((var & UPPER33) == UPPER33) {
76 var &= LOWER32;
77 /* Apply the fix. Dreadful hack. */
78 *varp = var;
79 }
80 #undef UPPER33
81 #undef LOWER32
82 return;
83 }
84 #endif /* __sgi */
85
86 #if 0 /* debug_print_range (debugging) */
87 /* Debugging only. Requires start. can calulate one of len, end */
88 static void
89 debug_print_range(const char *msg,
90 int lineno,
91 void *start, signed long len,
92 void *end)
93 {
94
95 char *st = (char *)start;
96 char *en = (char *)end;
97 signed long le = len;
98
99 if (len) {
100 if (en) {
101 le = (long)(en-st);
102 } else {
103 en= start+len;
104 }
105 } else if (en) {
106 le = (long)(en-st);
107 }
108 printf("RANGEdebug %s st=0x%lx le=%ld en=0x%lx line %d\n",
109 msg,(unsigned long)st,le,(unsigned long)en,lineno);
110 }
111 #endif /* 0 */
112
113 static void
114 dealloc_globals_chain(Dwarf_Debug dbg,
115 Dwarf_Chain head_chain)
116 {
117 Dwarf_Chain curr_chain = 0;
118 int chaintype = DW_DLA_CHAIN;
119 Dwarf_Global_Context lastcontext = 0;
120 Dwarf_Global_Context curcontext = 0;
121
122 curr_chain = head_chain;
123 for (; curr_chain; ) {
124 Dwarf_Global item = 0;
125 int itemtype = 0;
126 Dwarf_Chain prev = 0;
127
128 item = (Dwarf_Global)curr_chain->ch_item;
129 itemtype = curr_chain->ch_itemtype;
130 curcontext = item->gl_context;
131 if (curcontext && curcontext != lastcontext) {
132 /* First time we see a context, dealloc it. */
133 lastcontext = curcontext;
134 dwarf_dealloc(dbg,curcontext,curcontext->pu_alloc_type);
135 }
136 prev = curr_chain;
137 dwarf_dealloc(dbg, item,itemtype);
138 prev->ch_item = 0;
139 curr_chain = curr_chain->ch_next;
140 dwarf_dealloc(dbg, prev, chaintype);
141 }
142 }
143
144 /* INVARIANTS:
145 1) on error does not leak Dwarf_Global
146 2) glname is not malloc space. Never free.
147 */
148 static int
149 _dwarf_make_global_add_to_chain(Dwarf_Debug dbg,
150 Dwarf_Global_Context pubnames_context,
151 Dwarf_Off die_offset_in_cu,
152 unsigned char *glname,
153 Dwarf_Signed *global_count,
154 Dwarf_Bool *pubnames_context_on_list,
155 Dwarf_Unsigned global_DLA_code,
156 Dwarf_Chain **plast_chain,
157 Dwarf_Half tag,
158 Dwarf_Error *error)
159 {
160 Dwarf_Chain curr_chain = 0;
161 Dwarf_Global global = 0;
162
163 global = (Dwarf_Global)
164 _dwarf_get_alloc(dbg, (Dwarf_Small)global_DLA_code, 1);
165 if (!global) {
166 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
167 "DW_DLE_ALLOC_FAIL: Allocating Dwarf_Global");
168 return DW_DLV_ERROR;
169 }
170 (*global_count)++;
171 /* Recording the same context in another Dwarf_Global */
172 global->gl_context = pubnames_context;
173 global->gl_alloc_type = (Dwarf_Small)global_DLA_code;
174 global->gl_named_die_offset_within_cu = die_offset_in_cu;
175 global->gl_name = glname;
176 global->gl_tag = tag;
177 /* Finish off current entry chain */
178 curr_chain = (Dwarf_Chain) _dwarf_get_alloc(dbg,
179 (Dwarf_Small)DW_DLA_CHAIN, 1);
180 if (!curr_chain) {
181 dwarf_dealloc(dbg,global,pubnames_context->pu_alloc_type);
182 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
183 "DW_DLE_ALLOC_FAIL: allocating a Dwarf_Chain"
184 " internal structure.");
185 return DW_DLV_ERROR;
186 }
187 /* Put current global on singly_linked list. */
188 curr_chain->ch_item = (Dwarf_Global) global;
189 curr_chain->ch_itemtype = (int)global_DLA_code;
190 **plast_chain = curr_chain;
191 *plast_chain = &(curr_chain->ch_next);
192 *pubnames_context_on_list = TRUE;
193 return DW_DLV_OK;
194 }
195
196 static int
197 _dwarf_chain_to_array(Dwarf_Debug dbg,
198 Dwarf_Chain head_chain,
199 Dwarf_Signed global_count,
200 Dwarf_Global **globals,
201 Dwarf_Error *error)
202 {
203 Dwarf_Global *ret_globals = 0;
204
205 if (!head_chain ) {
206 /* ASSERT: global_count == 0 */
207 return DW_DLV_NO_ENTRY;
208 }
209 /* Now turn list into a block */
210 /* Points to contiguous block of Dwarf_Global. */
211 ret_globals = (Dwarf_Global *)
212 _dwarf_get_alloc(dbg, (Dwarf_Small)DW_DLA_LIST,
213 (Dwarf_Unsigned)global_count);
214 if (!ret_globals) {
215 dealloc_globals_chain(dbg,head_chain);
216 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
217 "DW_DLE_ALLOC_FAIL: Allocating a Dwarf_Global");
218 return DW_DLV_ERROR;
219 }
220
221 /* Store pointers to Dwarf_Global_s structs in contiguous block,
222 and deallocate the chain. This ignores the various
223 headers, since they are not involved. */
224 {
225 Dwarf_Signed i = 0;
226 Dwarf_Chain curr_chain = 0;
227
228 curr_chain = head_chain;
229 for ( ; i < global_count; i++) {
230 Dwarf_Chain prev = 0;
231
232 *(ret_globals + i) = curr_chain->ch_item;
233 prev = curr_chain;
234 curr_chain = curr_chain->ch_next;
235 prev->ch_item = 0; /* Not actually necessary. */
236 dwarf_dealloc(dbg, prev, DW_DLA_CHAIN);
237 }
238 }
239 head_chain = 0; /* Unneccesary, but showing intent. */
240 *globals = ret_globals;
241 return DW_DLV_OK;
242 }
243
244 static void
245 pubnames_error_length(Dwarf_Debug dbg,
246 Dwarf_Error *error,
247 Dwarf_Unsigned spaceneeded,
248 const char *secname,
249 const char *specificloc)
250 {
251 dwarfstring m;
252
253 dwarfstring_constructor(&m);
254 dwarfstring_append(&m,"DW_DLE_PUBNAMES_LENGTH_BAD: "
255 " In section ");
256 dwarfstring_append(&m,(char *)secname);
257 dwarfstring_append_printf_u(&m,
258 " %u bytes of space needed "
259 "but the section is out of space ",
260 spaceneeded);
261 dwarfstring_append(&m, "reading ");
262 dwarfstring_append(&m, (char *)specificloc);
263 dwarfstring_append(&m, ".");
264 _dwarf_error_string(dbg,error,DW_DLE_PUBNAMES_LENGTH_BAD,
265 dwarfstring_string(&m));
266 dwarfstring_destructor(&m);
267 }
268
269 /* There are only 6 DW_IDX values defined in DWARF5
270 so 7 would suffice, but lets allow for future DW_IDX too.
271
272 On returning DW_DLV_ERROR the caller will free the
273 chain, we do not need to here. */
274 #define IDX_ARRAY_SIZE 12
275 static int
276 _dwarf_internal_get_debug_names_globals(Dwarf_Debug dbg,
277 Dwarf_Chain **pplast_chain,
278 Dwarf_Signed *total_count,
279 Dwarf_Error *error,
280 int context_DLA_code,
281 int global_DLA_code)
282 {
283 int res = 0;
284 Dwarf_Off cur_offset = 0;
285 Dwarf_Off next_table_offset = 0;
286 Dwarf_Dnames_Head dn_head = 0;
287 Dwarf_Bool pubnames_context_on_list = FALSE;
288 Dwarf_Global_Context pubnames_context = 0;
289
290 res = _dwarf_load_section(dbg, &dbg->de_debug_names,error);
291 if (res != DW_DLV_OK) {
292 return res;
293 }
294 if (!dbg->de_debug_names.dss_size) {
295 return DW_DLV_NO_ENTRY;
296 }
297
298 for ( ; ; cur_offset = next_table_offset) {
299 Dwarf_Unsigned comp_unit_count = 0;
300 Dwarf_Unsigned name_count = 0;
301 Dwarf_Unsigned section_size = 0;
302 Dwarf_Half table_version = 0;
303 Dwarf_Half offset_size = 0;
304 Dwarf_Unsigned n = 0;
305
306 res = dwarf_dnames_header(dbg,cur_offset,&dn_head,
307 &next_table_offset,error);
308 if (res == DW_DLV_NO_ENTRY) {
309 /* Detected the end point */
310 break;
311 }
312 if (res == DW_DLV_ERROR) {
313 return res;
314 }
315 /* DW_DLV_NO_ENTRY impossible here */
316 res = dwarf_dnames_sizes(dn_head,&comp_unit_count,
317 0,0,0,&name_count,0,0,0,0,&section_size,&table_version,
318 &offset_size,error);
319 if (res != DW_DLV_OK) {
320 dwarf_dealloc_dnames(dn_head);
321 return res;
322 }
323
324 for (n = 1 ; n <= name_count; ++n) {
325 Dwarf_Unsigned aindex = 0;
326 Dwarf_Unsigned bucket_number = 0;
327 Dwarf_Unsigned hash_value = 0;
328 Dwarf_Unsigned offset_to_debug_str = 0;
329 char *ptrtostr = 0;
330 Dwarf_Unsigned abbrev_code = 0;
331 Dwarf_Unsigned offset_in_entrypool = 0;
332 Dwarf_Unsigned offset_of_next_entrypool = 0;
333 Dwarf_Half abbrev_tag = 0;
334 Dwarf_Half idxattr_array[IDX_ARRAY_SIZE];
335 Dwarf_Half form_array[IDX_ARRAY_SIZE];
336 Dwarf_Unsigned attr_count = 0;
337
338 Dwarf_Unsigned epool_abbrev_code = 0;
339 Dwarf_Half epool_abbrev_tag = 0;
340 Dwarf_Unsigned epool_value_count = 0;
341 Dwarf_Unsigned epool_index_of_abbrev = 0;
342 Dwarf_Unsigned epool_offset_of_initial_value = 0;
343
344 Dwarf_Unsigned offset_array[IDX_ARRAY_SIZE];
345 Dwarf_Sig8 signature_array[IDX_ARRAY_SIZE];
346 Dwarf_Bool single_cu = FALSE;
347 Dwarf_Unsigned single_cu_hdr_offset = 0;
348 Dwarf_Unsigned die_local_offset = 0;
349 Dwarf_Unsigned cu_header_global_offset = 0;
350 Dwarf_Unsigned cu_header_index = 0;
351 Dwarf_Bool have_die_local_offset = FALSE;
352 Dwarf_Bool have_cu_header_index = FALSE;
353 Dwarf_Bool have_cu_header_global_offset = 0;
354 Dwarf_Bool have_cu_header_offset = FALSE;
355
356 memset(idxattr_array,0,sizeof(Dwarf_Half)*IDX_ARRAY_SIZE);
357 memset(form_array,0,sizeof(Dwarf_Half)*IDX_ARRAY_SIZE);
358 memset(offset_array,0,sizeof(Dwarf_Unsigned)*
359 IDX_ARRAY_SIZE);
360 memset(signature_array,0,sizeof(Dwarf_Sig8)*
361 IDX_ARRAY_SIZE);
362 res = dwarf_dnames_name(dn_head,n,&bucket_number,
363 &hash_value,&offset_to_debug_str,&ptrtostr,
364 &offset_in_entrypool,
365 &abbrev_code,&abbrev_tag,IDX_ARRAY_SIZE,
366 idxattr_array,form_array,&attr_count,error);
367 if (res == DW_DLV_ERROR) {
368 dwarf_dealloc_dnames(dn_head);
369 return res;
370 }
371 if (res == DW_DLV_NO_ENTRY) {
372 /* internal error or corruption or simply past
373 the end of section. Normal.*/
374 dwarf_dealloc_dnames(dn_head);
375 return res;
376 }
377 switch (abbrev_tag) {
378 case DW_TAG_subprogram:
379 case DW_TAG_variable:
380 case DW_TAG_label:
381 case DW_TAG_member:
382 case DW_TAG_common_block:
383 case DW_TAG_enumerator:
384 case DW_TAG_namelist:
385 case DW_TAG_module:
386 break;
387 default:
388 continue;
389 }
390 if (attr_count >= IDX_ARRAY_SIZE) {
391 dwarf_dealloc_dnames(dn_head);
392 _dwarf_error_string(dbg,error,
393 DW_DLE_DEBUG_NAMES_ERROR,
394 "DW_DLE_DEBUG_NAMES_ERROR: "
395 "a .debug_names index attribute count "
396 "is unreasonable. "
397 "Corrupt data.");
398 return DW_DLV_ERROR;
399 }
400
401 res = dwarf_dnames_entrypool(dn_head,
402 offset_in_entrypool,&epool_abbrev_code,
403 &epool_abbrev_tag,&epool_value_count,
404 &epool_index_of_abbrev,
405 &epool_offset_of_initial_value,error);
406 if (res == DW_DLV_ERROR) {
407 dwarf_dealloc_dnames(dn_head);
408 return DW_DLV_ERROR;
409 }
410 if (res == DW_DLV_NO_ENTRY) {
411 dwarf_dealloc_dnames(dn_head);
412 _dwarf_error_string(dbg,error,
413 DW_DLE_DEBUG_NAMES_ERROR,
414 "DW_DLE_DEBUG_NAMES_ERROR: "
415 "a .debug_names entry has no entrypool. "
416 "Unreasonable. "
417 "Corrupt data.");
418 return DW_DLV_ERROR;
419 }
420 res = dwarf_dnames_entrypool_values(dn_head,
421 epool_index_of_abbrev,
422 epool_offset_of_initial_value,IDX_ARRAY_SIZE,
423 idxattr_array,form_array,
424 offset_array, signature_array,
425 &single_cu,&single_cu_hdr_offset,
426 &offset_of_next_entrypool,
427 error);
428
429 if (res == DW_DLV_ERROR) {
430 dwarf_dealloc_dnames(dn_head);
431 return res;
432 }
433 if (res == DW_DLV_NO_ENTRY) {
434 dwarf_dealloc_dnames(dn_head);
435 _dwarf_error_string(dbg,error,
436 DW_DLE_DEBUG_NAMES_ERROR,
437 "DW_DLE_DEBUG_NAMES_ERROR: "
438 "a .debug_names entry entrypool value "
439 " improperly empty. "
440 "Unreasonable. "
441 "Corrupt data.");
442 return DW_DLV_ERROR;
443 }
444 for (aindex = 0; aindex < epool_value_count; aindex++) {
445 Dwarf_Half idx = idxattr_array[aindex];
446
447 switch(idx) {
448 case DW_IDX_type_unit:
449 case DW_IDX_parent:
450 case DW_IDX_type_hash:
451 break;
452 case DW_IDX_compile_unit:
453 cu_header_index = offset_array[aindex];
454 have_cu_header_index = TRUE;
455 break;
456 case DW_IDX_die_offset:
457 die_local_offset = offset_array[aindex];
458 have_die_local_offset = TRUE;
459 break;
460 default:
461 /* Non-standard DW_IDX. */
462 break;
463 }
464 }
465 if (!have_cu_header_index) {
466 if (single_cu) {
467 have_cu_header_global_offset = TRUE;
468 cu_header_global_offset = single_cu_hdr_offset;
469 } else {
470 /* Ignore this entry, not global? */
471 continue;
472 }
473 }
474
475 if (!have_die_local_offset) {
476 /* Ignore this entry */
477 continue;
478 }
479 if (!have_cu_header_offset) {
480 int ores = 0;
481 Dwarf_Unsigned offset = 0;
482 Dwarf_Sig8 signature;
483 Dwarf_Error cuterr = 0;
484
485 ores = dwarf_dnames_cu_table(dn_head,
486 "cu", cu_header_index,
487 &offset,&signature,&cuterr);
488 if (ores != DW_DLV_OK) {
489 } else {
490 cu_header_global_offset = offset;
491 have_cu_header_global_offset = TRUE;
492 }
493 }
494 if (!have_cu_header_global_offset) {
495 /* Ignore this entry */
496 continue;
497 }
498
499 if (!pubnames_context ||
500 (pubnames_context->pu_offset_of_cu_header !=
501 cu_header_global_offset)) {
502 pubnames_context_on_list = FALSE;
503 pubnames_context = (Dwarf_Global_Context)
504 _dwarf_get_alloc(dbg,
505 (Dwarf_Small)context_DLA_code, 1);
506 if (!pubnames_context) {
507 dwarf_dealloc_dnames(dn_head);
508 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
509 "DW_DLE_ALLOC_FAIL: allocating "
510 "Dwarf_Global_Context");
511 return DW_DLV_ERROR;
512 }
513 /* Dwarf_Global_Context initialization. */
514 pubnames_context->pu_dbg = dbg;
515 pubnames_context->pu_alloc_type = context_DLA_code;
516 pubnames_context->pu_is_debug_names = TRUE;
517 pubnames_context->pu_offset_of_cu_header =
518 cu_header_global_offset;
519 /* For .debug_names we don't need to
520 set the rest of the fields.
521 All the translations from disk
522 form to libdwarf types and the sanity
523 chacking are already done. */
524 }
525 /* we have an entry to set up Dwarf_Global */
526 res = _dwarf_make_global_add_to_chain(dbg,
527 pubnames_context,
528 die_local_offset,
529 (unsigned char *)ptrtostr,
530 total_count,
531 &pubnames_context_on_list,
532 global_DLA_code,
533 pplast_chain,
534 abbrev_tag,
535 error);
536 if (res == DW_DLV_ERROR) {
537 if (!pubnames_context_on_list) {
538 dwarf_dealloc(dbg,pubnames_context,
539 context_DLA_code);
540 }
541 dwarf_dealloc_dnames(dn_head);
542 return res;
543 }
544 }
545 dwarf_dealloc_dnames(dn_head);
546 dn_head = 0;
547 }
548 return DW_DLV_OK;
549 }
550 #undef IDX_ARRAY_SIZE
551 static void
552 _dwarf_get_DLE_name(int errnum,dwarfstring *out)
553 {
554 char * basemsg = 0;
555 char * curp = 0;
556 unsigned long count = 0;
557
558 basemsg = dwarf_errmsg_by_number(errnum);
559 curp = basemsg;
560 while (*curp) {
561 if (*curp == ' ') {
562 break;
563 }
564 if (*curp == '(') {
565 break;
566 }
567 ++count;
568 ++curp;
569 }
570 dwarfstring_append_length(out,basemsg,count);
571 }
572
573 static void
574 _dwarf_global_cu_len_error_msg(Dwarf_Debug dbg,
575 int errornumber,
576 const char * section_name,
577 Dwarf_Unsigned section_length,
578 Dwarf_Unsigned cu_number,
579 Dwarf_Unsigned length_section_offset,
580 Dwarf_Unsigned length_field,
581 Dwarf_Error *error)
582 {
583 dwarfstring m;
584 Dwarf_Unsigned remaining = 0;
585
586 remaining = section_length - length_section_offset;
587 dwarfstring_constructor(&m);
588 _dwarf_get_DLE_name(errornumber,&m);
589 dwarfstring_append_printf_u(&m,
590 ": For cu context %u ",
591 cu_number);
592 dwarfstring_append_printf_s(&m,"of section %s ",
593 (char *)section_name);
594 dwarfstring_append_printf_u(&m,"the length field at "
595 "offset %u ",length_section_offset);
596 dwarfstring_append_printf_u(&m,"has value %u ",
597 length_field);
598 dwarfstring_append_printf_u(&m,"though just %u bytes "
599 "remain in the section. Corrupt DWARF",remaining);
600 _dwarf_error_string(dbg, error,errornumber,
601 dwarfstring_string(&m));
602 dwarfstring_destructor(&m);
603 }
604
605 /* Sweeps the complete section.
606 On error it frees the head_chain,
607 and the caller never sees the head_chain data.
608 On success, if the out*chain data exists
609 it updates the caller head_chain through
610 the pointers.
611 */
612 static int
613 _dwarf_internal_get_pubnames_like(Dwarf_Debug dbg,
614 int category, /* DW_GL_GLOBAL or ... */
615 const char *secname,
616 Dwarf_Small * section_data_ptr,
617 Dwarf_Unsigned section_length,
618 Dwarf_Chain * out_phead_chain,
619 Dwarf_Chain ** out_pplast_chain,
620 Dwarf_Signed * return_count,
621 Dwarf_Error * error,
622 int length_err_num,
623 int version_err_num)
624 {
625 Dwarf_Small *pubnames_like_ptr = 0;
626 /* Section offset to the above pointer. */
627 Dwarf_Unsigned pubnames_like_offset = 0;
628
629 Dwarf_Small *section_end_ptr = section_data_ptr +section_length;
630
631 /* Points to the context for the current set of global names,
632 and contains information to identify the compilation-unit
633 that the set refers to. */
634 Dwarf_Global_Context pubnames_context = 0;
635 Dwarf_Bool pubnames_context_on_list = FALSE;
636 Dwarf_Unsigned context_DLA_code = DW_DLA_GLOBAL_CONTEXT;
637 Dwarf_Unsigned global_DLA_code = DW_DLA_GLOBAL;
638
639 Dwarf_Unsigned version = 0;
640
641 /* Offset from the start of compilation-unit for the current
642 global. */
643 Dwarf_Off die_offset_in_cu = 0;
644 Dwarf_Signed global_count = 0;
645
646 /* The count is just to improve the error message
647 a few lines above. */
648 Dwarf_Unsigned context_count = 0;
649
650 if (IS_INVALID_DBG(dbg)) {
651 _dwarf_error_string(NULL, error, DW_DLE_DBG_NULL,
652 "DW_DLE_DBG_NULL: "
653 "calling for pubnames-like data Dwarf_Debug "
654 "either null or it contains"
655 "a stale Dwarf_Debug pointer");
656 return DW_DLV_ERROR;
657 }
658 /* We will eventually need the .debug_info data. Load it now. */
659 if (!dbg->de_debug_info.dss_data) {
660 int res = _dwarf_load_debug_info(dbg, error);
661
662 if (res != DW_DLV_OK) {
663 return res;
664 }
665 }
666 if (section_data_ptr == NULL) {
667 return DW_DLV_NO_ENTRY;
668 }
669 pubnames_like_ptr = section_data_ptr;
670 pubnames_like_offset = 0;
671 do {
672 int mres = 0;
673 Dwarf_Unsigned length = 0;
674 int local_extension_size = 0;
675 int local_length_size = 0;
676 Dwarf_Off pubnames_section_cu_offset =
677 pubnames_like_offset;
678
679 /* Some compilers emit padding at the end of each cu's area.
680 pubnames_ptr_past_end_cu records the true area end for the
681 pubnames(like) content of a cu.
682 Essentially the length in the header and the 0
683 terminator of the data are redundant information. The
684 dwarf2/3 spec does not mention what to do if the length is
685 past the 0 terminator. So we take any bytes left
686 after the 0 as padding and ignore them. */
687 Dwarf_Small *pubnames_ptr_past_end_cu = 0;
688
689 pubnames_context_on_list = FALSE;
690 pubnames_context = (Dwarf_Global_Context)
691 _dwarf_get_alloc(dbg,
692 (Dwarf_Small)context_DLA_code, 1);
693 if (!pubnames_context) {
694 dealloc_globals_chain(dbg,*out_phead_chain);
695 *out_phead_chain = 0;
696 _dwarf_error_string(dbg, error, DW_DLE_ALLOC_FAIL,
697 "DW_DLE_ALLOC_FAIL: Allocating a"
698 " Dwarf_Global_Context for a pubnames entry.");
699 return DW_DLV_ERROR;
700 }
701 /* ========pubnames_context not recorded anywhere yet. */
702 /* READ_AREA_LENGTH updates pubnames_like_ptr for consumed
703 bytes. */
704 if ((pubnames_like_ptr + DWARF_32BIT_SIZE +
705 DWARF_HALF_SIZE + DWARF_32BIT_SIZE) >
706 /* A minimum size needed */
707 section_end_ptr) {
708 pubnames_error_length(dbg,error,
709 DWARF_32BIT_SIZE + DWARF_HALF_SIZE + DWARF_32BIT_SIZE,
710 secname,
711 "header-record");
712 dealloc_globals_chain(dbg,*out_phead_chain);
713 *out_phead_chain = 0;
714 if (!pubnames_context_on_list) {
715 dwarf_dealloc(dbg,pubnames_context,
716 context_DLA_code);
717 pubnames_context = 0;
718 }
719 return DW_DLV_ERROR;
720 }
721 mres = _dwarf_read_area_length_ck_wrapper(dbg,
722 &length,&pubnames_like_ptr,&local_length_size,
723 &local_extension_size,section_length,section_end_ptr,
724 error);
725 if (mres != DW_DLV_OK) {
726 dealloc_globals_chain(dbg,*out_phead_chain);
727 *out_phead_chain = 0;
728 if (!pubnames_context_on_list) {
729 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
730 pubnames_context = 0;
731 }
732 return mres;
733 }
734 {
735 Dwarf_Small * localend =pubnames_like_ptr + length;
736 if ((length > section_length) ||
737 (localend > section_end_ptr)){
738 _dwarf_global_cu_len_error_msg(dbg,
739 length_err_num,
740 secname, section_length,
741 context_count,
742 (Dwarf_Unsigned)pubnames_like_offset,
743 (Dwarf_Unsigned)length,
744 error);
745 dealloc_globals_chain(dbg,*out_phead_chain);
746 *out_phead_chain = 0;
747 if (!pubnames_context_on_list) {
748 dwarf_dealloc(dbg,pubnames_context,
749 context_DLA_code);
750 pubnames_context = 0;
751 }
752 return DW_DLV_ERROR;
753 }
754 }
755 pubnames_like_offset += local_length_size +
756 local_extension_size;
757 /* The count is just to improve the error message
758 a few lines above. */
759 ++context_count;
760 /* Dwarf_Global_Context initialization. */
761 pubnames_context->pu_global_category = category;
762 pubnames_context->pu_alloc_type =
763 (unsigned)context_DLA_code;
764 pubnames_context->pu_length_size =
765 (unsigned char)local_length_size;
766 pubnames_context->pu_length = (unsigned char)length;
767 pubnames_context->pu_extension_size =
768 (unsigned char)local_extension_size;
769 pubnames_context->pu_dbg = dbg;
770 pubnames_context->pu_pub_offset = pubnames_section_cu_offset;
771 pubnames_ptr_past_end_cu = pubnames_like_ptr + length;
772 pubnames_context->pu_pub_entries_end_ptr =
773 pubnames_ptr_past_end_cu;
774 if ((pubnames_like_ptr + (DWARF_HALF_SIZE) ) >=
775 /* A minimum size needed */
776 section_end_ptr) {
777 pubnames_error_length(dbg,error,
778 DWARF_HALF_SIZE,
779 secname,"version-number");
780 dealloc_globals_chain(dbg,*out_phead_chain);
781 *out_phead_chain = 0;
782 if (!pubnames_context_on_list) {
783 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
784 pubnames_context = 0;
785 }
786 return DW_DLV_ERROR;
787 }
788 mres = _dwarf_read_unaligned_ck_wrapper(dbg,
789 &version,pubnames_like_ptr,DWARF_HALF_SIZE,
790 section_end_ptr,error);
791 if (mres != DW_DLV_OK) {
792 dealloc_globals_chain(dbg,*out_phead_chain);
793 *out_phead_chain = 0;
794 if (!pubnames_context_on_list) {
795 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
796 pubnames_context = 0;
797 }
798 return mres;
799 }
800 pubnames_context->pu_version = (Dwarf_Half)version;
801 pubnames_like_ptr += DWARF_HALF_SIZE;
802 pubnames_like_offset += DWARF_HALF_SIZE;
803 /* ASSERT: DW_PUBNAMES_VERSION2 == DW_PUBTYPES_VERSION2 */
804 if (version != DW_PUBNAMES_VERSION2) {
805 dealloc_globals_chain(dbg,*out_phead_chain);
806 *out_phead_chain = 0;
807 if (!pubnames_context_on_list) {
808 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
809 pubnames_context = 0;
810 }
811 _dwarf_error(dbg, error, version_err_num);
812 return DW_DLV_ERROR;
813 }
814
815 /* Offset of CU header in debug section. */
816 if ((pubnames_like_ptr + 3*pubnames_context->pu_length_size)>
817 section_end_ptr) {
818 pubnames_error_length(dbg,error,
819 3*pubnames_context->pu_length_size,
820 secname,
821 "header/DIE offsets");
822 dealloc_globals_chain(dbg,*out_phead_chain);
823 *out_phead_chain = 0;
824 if (!pubnames_context_on_list) {
825 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
826 pubnames_context = 0;
827 }
828 return DW_DLV_ERROR;
829 }
830 mres = _dwarf_read_unaligned_ck_wrapper(dbg,
831 &pubnames_context->pu_offset_of_cu_header,
832 pubnames_like_ptr,
833 pubnames_context->pu_length_size,
834 section_end_ptr,error);
835 if (mres != DW_DLV_OK) {
836 dealloc_globals_chain(dbg,*out_phead_chain);
837 *out_phead_chain = 0;
838 if (!pubnames_context_on_list) {
839 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
840 pubnames_context = 0;
841 }
842 return mres;
843 }
844
845 pubnames_like_ptr += pubnames_context->pu_length_size;
846 pubnames_like_offset += pubnames_context->pu_length_size;
847
848 FIX_UP_OFFSET_IRIX_BUG(dbg,
849 pubnames_context->pu_offset_of_cu_header,
850 "pubnames cu header offset");
851 mres = _dwarf_read_unaligned_ck_wrapper(dbg,
852 &pubnames_context->pu_info_length,
853 pubnames_like_ptr,
854 pubnames_context->pu_length_size,
855 section_end_ptr,error);
856 if (mres != DW_DLV_OK) {
857 dealloc_globals_chain(dbg,*out_phead_chain);
858 *out_phead_chain = 0;
859 if (!pubnames_context_on_list) {
860 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
861 pubnames_context = 0;
862 }
863 return mres;
864 }
865 pubnames_like_ptr += pubnames_context->pu_length_size;
866 pubnames_like_offset += pubnames_context->pu_length_size;
867
868 if (pubnames_like_ptr > (section_data_ptr + section_length)) {
869 dealloc_globals_chain(dbg,*out_phead_chain);
870 *out_phead_chain = 0;
871 if (!pubnames_context_on_list) {
872 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
873 pubnames_context = 0;
874 }
875 _dwarf_error(dbg, error, length_err_num);
876 return DW_DLV_ERROR;
877 }
878
879 /* Read initial offset (of DIE within CU) of a pubname, final
880 entry is not a pair, just a zero offset. */
881 mres = _dwarf_read_unaligned_ck_wrapper(dbg,
882 &die_offset_in_cu,
883 pubnames_like_ptr,
884 pubnames_context->pu_length_size,
885 pubnames_context->pu_pub_entries_end_ptr,error);
886 if (mres != DW_DLV_OK) {
887 dealloc_globals_chain(dbg,*out_phead_chain);
888 *out_phead_chain = 0;
889 if (!pubnames_context_on_list) {
890 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
891 pubnames_context = 0;
892 }
893 return mres;
894 }
895 pubnames_like_ptr += pubnames_context->pu_length_size;
896 pubnames_like_offset += pubnames_context->pu_length_size;
897 FIX_UP_OFFSET_IRIX_BUG(dbg,
898 die_offset_in_cu, "offset of die in cu");
899 if (pubnames_like_ptr > (section_data_ptr + section_length)) {
900 dealloc_globals_chain(dbg,*out_phead_chain);
901 *out_phead_chain = 0;
902 if (!pubnames_context_on_list) {
903 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
904 pubnames_context = 0;
905 }
906 _dwarf_error(dbg, error, length_err_num);
907 return DW_DLV_ERROR;
908 }
909
910 /* Check if empty section */
911 if (!die_offset_in_cu) {
912 if (dbg->de_return_empty_pubnames) {
913 int res = 0;
914
915 /* Here we have a pubnames CU with no actual
916 entries so we fake up an entry to hold the
917 header data. There are no 'pairs' here,
918 just the end of list zero value. We do this
919 only if de_return_empty_pubnames is set
920 so that we by default return exactly the same
921 data this always returned, yet dwarfdump can
922 request the empty-cu records get created
923 to test that feature.
924 see dwarf_get_globals_header() */
925 res = _dwarf_make_global_add_to_chain(dbg,
926 pubnames_context,
927 die_offset_in_cu,
928 /* It is a fake global, so empty name */
929 (unsigned char *)"",
930 &global_count,
931 &pubnames_context_on_list,
932 global_DLA_code,
933 out_pplast_chain,
934 0,
935 error);
936 if (res != DW_DLV_OK) {
937 dealloc_globals_chain(dbg,*out_phead_chain);
938 *out_phead_chain = 0;
939 if (!pubnames_context_on_list) {
940 dwarf_dealloc(dbg,pubnames_context,
941 context_DLA_code);
942 pubnames_context = 0;
943 }
944 return res;
945 }
946 /* ========pubnames_context recorded in chain. */
947 } else {
948 /* The section is empty.
949 Nowhere to record pubnames_context); */
950 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
951 /* pubnames_context = 0; reset at top of loop */
952 continue;
953 }
954 }
955 /* Loop thru pairs. DIE off with CU followed by string. */
956 /* Now read pairs of entries */
957 while (die_offset_in_cu) {
958 int res = 0;
959 unsigned char *glname = 0;
960 Dwarf_Unsigned nstrlen = 0;
961
962 /* non-zero die_offset_in_cu already read, so
963 pubnames_like_ptr points to a string. */
964 res = _dwarf_check_string_valid(dbg,section_data_ptr,
965 pubnames_like_ptr,
966 pubnames_context->pu_pub_entries_end_ptr,
967 DW_DLE_STRING_OFF_END_PUBNAMES_LIKE,error);
968 if (res != DW_DLV_OK) {
969 dealloc_globals_chain(dbg,*out_phead_chain);
970 *out_phead_chain = 0;
971 if (!pubnames_context_on_list) {
972 dwarf_dealloc(dbg,pubnames_context,
973 context_DLA_code);
974 pubnames_context = 0;
975 }
976 return res;
977 }
978 glname = (unsigned char *)pubnames_like_ptr;
979 nstrlen = strlen((char *)pubnames_like_ptr);
980 pubnames_like_ptr += nstrlen + 1;
981 pubnames_like_offset += nstrlen + 1;
982 /* Already read offset and verified string, glname
983 now points to the string. */
984 res = _dwarf_make_global_add_to_chain(dbg,
985 pubnames_context,
986 die_offset_in_cu,
987 glname,
988 &global_count,
989 &pubnames_context_on_list,
990 global_DLA_code,
991 out_pplast_chain,
992 0,
993 error);
994 if (res != DW_DLV_OK) {
995 dealloc_globals_chain(dbg,*out_phead_chain);
996 *out_phead_chain = 0;
997 if (!pubnames_context_on_list) {
998 dwarf_dealloc(dbg,pubnames_context,
999 context_DLA_code);
1000 pubnames_context = 0;
1001 }
1002 return res;
1003 }
1004 /* ========pubnames_context recorded in chain. */
1005 /* Ensure room for a next entry to exist. */
1006 if ((pubnames_like_ptr +
1007 pubnames_context->pu_length_size ) >
1008 section_end_ptr) {
1009 pubnames_error_length(dbg,error,
1010 2*pubnames_context->pu_length_size,
1011 secname,
1012 "global record offset");
1013 dealloc_globals_chain(dbg,*out_phead_chain);
1014 *out_phead_chain = 0;
1015 if (!pubnames_context_on_list) {
1016 dwarf_dealloc(dbg,pubnames_context,
1017 context_DLA_code);
1018 pubnames_context = 0;
1019 }
1020 return DW_DLV_ERROR;
1021 }
1022 /* Read die offset for the *next* entry */
1023 mres = _dwarf_read_unaligned_ck_wrapper(dbg,
1024 &die_offset_in_cu,
1025 pubnames_like_ptr,
1026 pubnames_context->pu_length_size,
1027 pubnames_context->pu_pub_entries_end_ptr,
1028 error);
1029 if (mres != DW_DLV_OK) {
1030 if (!pubnames_context_on_list) {
1031 dwarf_dealloc(dbg,pubnames_context,
1032 context_DLA_code);
1033 pubnames_context = 0;
1034 }
1035 dealloc_globals_chain(dbg,*out_phead_chain);
1036 *out_phead_chain = 0;
1037 return mres;
1038 }
1039 /* die_offset_in_cu may now be zero, meaing
1040 end of the pairs list */
1041 pubnames_like_ptr += pubnames_context->pu_length_size;
1042 pubnames_like_offset += pubnames_context->pu_length_size;
1043 FIX_UP_OFFSET_IRIX_BUG(dbg,
1044 die_offset_in_cu, "offset of next die in cu");
1045 if (pubnames_like_ptr >
1046 (section_data_ptr + section_length)) {
1047 if (!pubnames_context_on_list) {
1048 dwarf_dealloc(dbg,pubnames_context,
1049 context_DLA_code);
1050 }
1051 dealloc_globals_chain(dbg,*out_phead_chain);
1052 *out_phead_chain = 0;
1053 _dwarf_error(dbg, error, length_err_num);
1054 return DW_DLV_ERROR;
1055 }
1056 }
1057 /* ASSERT: die_offset_in_cu == 0 */
1058 if (pubnames_like_ptr > pubnames_ptr_past_end_cu) {
1059 /* This is some kind of error. This simply cannot happen.
1060 The encoding is wrong or the length in the header for
1061 this cu's contribution is wrong. */
1062 _dwarf_error(dbg, error, length_err_num);
1063 if (!pubnames_context_on_list) {
1064 dwarf_dealloc(dbg,pubnames_context,context_DLA_code);
1065 pubnames_context = 0;
1066 }
1067 dealloc_globals_chain(dbg,*out_phead_chain);
1068 *out_phead_chain = 0;
1069 return DW_DLV_ERROR;
1070 }
1071 #if 1
1072 /* If there is some kind of padding at the end of
1073 the section, following a pairs terminator,
1074 as emitted by some compilers, skip over that padding and
1075 simply ignore the bytes thus passed-over. */
1076 {
1077 Dwarf_Unsigned finaloffset =
1078 pubnames_section_cu_offset+
1079 pubnames_context->pu_length_size +
1080 pubnames_context->pu_length +
1081 pubnames_context->pu_extension_size;
1082 if (finaloffset > pubnames_like_offset) {
1083 pubnames_like_offset = finaloffset;
1084 }
1085 }
1086 #endif
1087 pubnames_like_ptr = pubnames_ptr_past_end_cu;
1088 } while (pubnames_like_ptr < section_end_ptr);
1089 *return_count = global_count;
1090 return DW_DLV_OK;
1091 }
1092
1093 static const int err3[]=
1094 {
1095 DW_DLE_PUBNAMES_LENGTH_BAD,
1096 DW_DLE_DEBUG_PUBTYPES_LENGTH_BAD,
1097 DW_DLE_DEBUG_FUNCNAMES_LENGTH_BAD,
1098 DW_DLE_DEBUG_TYPENAMES_LENGTH_BAD,
1099 DW_DLE_DEBUG_VARNAMES_LENGTH_BAD,
1100 DW_DLE_DEBUG_WEAKNAMES_LENGTH_BAD
1101 };
1102 static const int err4[]=
1103 {
1104 DW_DLE_PUBNAMES_VERSION_ERROR,
1105 DW_DLE_DEBUG_PUBTYPES_VERSION_ERROR,
1106 DW_DLE_DEBUG_FUNCNAMES_VERSION_ERROR,
1107 DW_DLE_DEBUG_TYPENAMES_VERSION_ERROR,
1108 DW_DLE_DEBUG_VARNAMES_VERSION_ERROR,
1109 DW_DLE_DEBUG_WEAKNAMES_VERSION_ERROR
1110 };
1111 static const char * secna[] =
1112 {
1113 ".debug_pubnames",
1114 ".debug_pubtypes",
1115 ".debug_funcnames",
1116 ".debug_typenames",
1117 ".debug_varnames",
1118 ".debug_weaknames",
1119 };
1120
1121 /* New in 0.6.0, unifies all the access routines
1122 for the sections like .debug_pubtypes.
1123 */
1124 int
1125 dwarf_globals_by_type(Dwarf_Debug dbg,
1126 int requested_section,
1127 Dwarf_Global **contents,
1128 Dwarf_Signed *ret_count,
1129 Dwarf_Error *error)
1130 {
1131 struct Dwarf_Section_s *section = 0;
1132 Dwarf_Chain head_chain = 0;
1133 Dwarf_Chain *plast_chain = &head_chain;
1134 Dwarf_Bool have_base_sec = FALSE;
1135 Dwarf_Bool have_second_sec = FALSE;
1136 int res = 0;
1137
1138 /* Zero caller's fields in case caller
1139 failed to do so. Bad input here causes
1140 segfault! */
1141 *contents = 0;
1142 *ret_count = 0;
1143 CHECK_DBG(dbg,error,"dwarf_globals_by_type()");
1144 switch(requested_section){
1145 case DW_GL_GLOBALS:
1146 section = &dbg->de_debug_pubnames;
1147 break;
1148 case DW_GL_PUBTYPES:
1149 section = &dbg->de_debug_pubtypes;
1150 break;
1151 /* The Following are IRIX only. */
1152 case DW_GL_FUNCS:
1153 section = &dbg->de_debug_funcnames;
1154 break;
1155 case DW_GL_TYPES:
1156 section = &dbg->de_debug_typenames;
1157 break;
1158 case DW_GL_VARS:
1159 section = &dbg->de_debug_varnames;
1160 break;
1161 case DW_GL_WEAKS:
1162 section = &dbg->de_debug_weaknames;
1163 break;
1164 default: {
1165 dwarfstring m;
1166 char buf[100];
1167
1168 dwarfstring_constructor_static(&m,buf,sizeof(buf));
1169 dwarfstring_append_printf_u(&m,
1170 "ERROR DW_DLE_GLOBAL_NULL: Passed in Dwarf_Global "
1171 "requested section "
1172 "%u which is unknown to dwarf_globals_by_type().",
1173 requested_section);
1174 _dwarf_error_string(dbg, error, DW_DLE_GLOBAL_NULL,
1175 dwarfstring_string(&m));
1176 dwarfstring_destructor(&m);
1177 return DW_DLV_ERROR;
1178 }
1179 }
1180 res = _dwarf_load_section(dbg, section, error);
1181 if (res == DW_DLV_ERROR) {
1182 return res;
1183 }
1184 if (section->dss_size) {
1185 have_base_sec = TRUE;
1186 }
1187
1188 if (have_base_sec) {
1189 res = _dwarf_internal_get_pubnames_like(dbg,
1190 requested_section,
1191 secna[requested_section],
1192 section->dss_data,
1193 section->dss_size,
1194 &head_chain,
1195 &plast_chain,
1196 ret_count,
1197 error,
1198 err3[requested_section],
1199 err4[requested_section]);
1200 if (res == DW_DLV_ERROR) {
1201 dealloc_globals_chain(dbg,head_chain);
1202 return res;
1203 }
1204 }
1205 if (0 == requested_section) {
1206 res = _dwarf_load_section(dbg, &dbg->de_debug_names,error);
1207 if (res == DW_DLV_ERROR) {
1208 return res;
1209 } else if (dbg->de_debug_names.dss_size) {
1210 have_second_sec = TRUE;
1211 }
1212 }
1213 if (have_second_sec) {
1214 res = _dwarf_internal_get_debug_names_globals(dbg,
1215 &plast_chain,
1216 ret_count,
1217 error,
1218 DW_DLA_GLOBAL_CONTEXT,
1219 DW_DLA_GLOBAL);
1220 if (res == DW_DLV_ERROR) {
1221 dealloc_globals_chain(dbg,head_chain);
1222 head_chain = 0;
1223 return res;
1224 }
1225 }
1226 res = _dwarf_chain_to_array(dbg,head_chain,
1227 *ret_count, contents, error);
1228 if (res == DW_DLV_ERROR) {
1229 /* head chain maybe zero. Is ok. */
1230 dealloc_globals_chain(dbg,head_chain);
1231 return res;
1232 }
1233 /* Must not return DW_DLV_NO_ENTRY. Count
1234 is set zero in caller so no need for NO_ENTRY. */
1235 return DW_DLV_OK;
1236 }
1237
1238 int
1239 dwarf_get_globals(Dwarf_Debug dbg,
1240 Dwarf_Global **ret_globals,
1241 Dwarf_Signed *return_count,
1242 Dwarf_Error *error)
1243 {
1244 int res = 0;
1245
1246 CHECK_DBG(dbg,error,"dwarf_get_globals()");
1247 res = dwarf_globals_by_type(dbg,
1248 DW_GL_GLOBALS,ret_globals,return_count,error);
1249 return res;
1250 }
1251 /* This now returns Dwarf_Global for types so
1252 all the dwarf_global data retrieval calls work.
1253 This is just a shorthand.
1254
1255 Before 0.6.0 this would return Dwarf_Type.
1256 */
1257 int
1258 dwarf_get_pubtypes(Dwarf_Debug dbg,
1259 Dwarf_Global **types,
1260 Dwarf_Signed *return_count,
1261 Dwarf_Error *error)
1262 {
1263 int res = 0;
1264 CHECK_DBG(dbg,error,"dwarf_get_pubtypes()");
1265 res = dwarf_globals_by_type(dbg,
1266 DW_GL_PUBTYPES,types,return_count,error);
1267 return res;
1268 }
1269
1270 /* Deallocating fully requires deallocating the list
1271 and all entries. But some internal data is
1272 not exposed, so we need a function with internal knowledge.
1273 */
1274 void
1275 dwarf_globals_dealloc(Dwarf_Debug dbg, Dwarf_Global * dwgl,
1276 Dwarf_Signed count)
1277 {
1278 _dwarf_internal_globals_dealloc(dbg, dwgl, count);
1279 return;
1280 }
1281
1282 void
1283 _dwarf_internal_globals_dealloc(Dwarf_Debug dbg,
1284 Dwarf_Global * dwgl,
1285 Dwarf_Signed count)
1286 {
1287 Dwarf_Signed i = 0;
1288 struct Dwarf_Global_Context_s *glcp = 0;
1289 struct Dwarf_Global_Context_s *lastglcp = 0;
1290
1291 if (IS_INVALID_DBG(dbg)) {
1292 return;
1293 }
1294 if (!dwgl) {
1295 return;
1296 }
1297 for (i = 0; i < count; i++) {
1298 Dwarf_Global dgd = dwgl[i];
1299
1300 if (!dgd) {
1301 continue;
1302 }
1303 /* Avoids duplicate frees of repeated
1304 use of contexts (while assuming that
1305 all uses of a particular gl_context
1306 will appear next to each other. */
1307 glcp = dgd->gl_context;
1308 if (glcp && lastglcp != glcp) {
1309 lastglcp = glcp;
1310 dwarf_dealloc(dbg, glcp, glcp->pu_alloc_type);
1311 }
1312 dwarf_dealloc(dbg, dgd, dgd->gl_alloc_type);
1313 }
1314 dwarf_dealloc(dbg, dwgl, DW_DLA_LIST);
1315 return;
1316 }
1317
1318 /* Given a pubnames entry (or other like section entry)
1319 return thru the ret_name pointer
1320 a pointer to the string which is the entry name. */
1321 int
1322 dwarf_globname(Dwarf_Global glob,
1323 char **ret_name,
1324 Dwarf_Error * error)
1325 {
1326 if (glob == NULL) {
1327 _dwarf_error(NULL, error, DW_DLE_GLOBAL_NULL);
1328 return DW_DLV_ERROR;
1329 }
1330
1331 *ret_name = (char *) (glob->gl_name);
1332 return DW_DLV_OK;
1333 }
1334
1335 /* Given a pubnames entry (or other like section entry)
1336 return thru the ret_off pointer the
1337 global offset of the DIE for this entry.
1338 The global offset is the offset within the .debug_info
1339 section as a whole. */
1340 int
1341 dwarf_global_die_offset(Dwarf_Global global,
1342 Dwarf_Off * ret_off, Dwarf_Error * error)
1343 {
1344 if (global == NULL) {
1345 _dwarf_error(NULL, error, DW_DLE_GLOBAL_NULL);
1346 return DW_DLV_ERROR;
1347 }
1348
1349 if (global->gl_context == NULL) {
1350 _dwarf_error(NULL, error, DW_DLE_GLOBAL_CONTEXT_NULL);
1351 return DW_DLV_ERROR;
1352 }
1353
1354 *ret_off = (global->gl_named_die_offset_within_cu +
1355 global->gl_context->pu_offset_of_cu_header);
1356 return DW_DLV_OK;
1357 }
1358
1359 /* Given a pubnames entry (or other like section entry)
1360 return thru the ret_off pointer the
1361 offset of the compilation unit header of the
1362 compilation unit the global is part of.
1363 */
1364 int
1365 dwarf_global_cu_offset(Dwarf_Global global,
1366 Dwarf_Off * cu_header_offset,
1367 Dwarf_Error * error)
1368 {
1369 Dwarf_Global_Context con = 0;
1370
1371 if (global == NULL) {
1372 _dwarf_error(NULL, error, DW_DLE_GLOBAL_NULL);
1373 return DW_DLV_ERROR;
1374 }
1375 con = global->gl_context;
1376 if (con == NULL) {
1377 _dwarf_error_string(NULL, error, DW_DLE_GLOBAL_CONTEXT_NULL,
1378 "DW_DLE_GLOBAL_CONTEXT_NULL in call of "
1379 "dwarf_global_cu_offset()");
1380 return DW_DLV_ERROR;
1381 }
1382 *cu_header_offset = con->pu_offset_of_cu_header;
1383 return DW_DLV_OK;
1384 }
1385
1386 static void
1387 build_off_end_msg(Dwarf_Unsigned offval,
1388 Dwarf_Unsigned withincr,
1389 Dwarf_Unsigned secsize,
1390 dwarfstring *m)
1391 {
1392 const char *msg = "past";
1393 if (offval < secsize){
1394 msg = "too near";
1395 }
1396 dwarfstring_append_printf_u(m,"DW_DLE_OFFSET_BAD: "
1397 "The CU header offset of %u in a pubnames-like entry ",
1398 withincr);
1399 dwarfstring_append_printf_s(m,
1400 "would put us %s the end of .debug_info. "
1401 "No room for a DIE there... "
1402 "Corrupt Dwarf.",(char *)msg);
1403 return;
1404 }
1405
1406 /*
1407 Give back the pubnames entry (or any other like section)
1408 name, symbol DIE offset, and the cu-DIE offset.
1409
1410 This provides all the information that
1411 dwarf_globname(), dwarf_global_die_offset()
1412 and dwarf_global_cu_offset() do, but do it
1413 in one call.
1414
1415 The string pointer returned thru ret_name is not
1416 dwarf_get_alloc()ed, so no dwarf_dealloc()
1417 DW_DLA_STRING should be applied to it.
1418
1419 */
1420 int
1421 dwarf_global_name_offsets(Dwarf_Global global,
1422 char **ret_name,
1423 Dwarf_Off * die_offset,
1424 Dwarf_Off * cu_die_offset,
1425 Dwarf_Error * error)
1426 {
1427 Dwarf_Global_Context con = 0;
1428 Dwarf_Debug dbg = 0;
1429 Dwarf_Off cuhdr_off = 0;
1430
1431 if (global == NULL) {
1432 _dwarf_error(NULL, error, DW_DLE_GLOBAL_NULL);
1433 return DW_DLV_ERROR;
1434 }
1435
1436 con = global->gl_context;
1437 if (con == NULL) {
1438 _dwarf_error_string(NULL, error, DW_DLE_GLOBAL_CONTEXT_NULL,
1439 "DW_DLE_GLOBAL_CONTEXT_NULL in call of "
1440 "dwarf_global_name_offsets()");
1441 return DW_DLV_ERROR;
1442 }
1443
1444 cuhdr_off = con->pu_offset_of_cu_header;
1445 /* The offset had better not be too close to the end. If it is,
1446 _dwarf_length_of_cu_header() will step off the end
1447 and therefore
1448 must not be used. 10 is a meaningless heuristic, but no CU
1449 header is that small so it is safe. An erroneous offset is due
1450 to a bug in the tool chain. A bug like this has been seen on
1451 IRIX with MIPSpro 7.3.1.3 and an executable > 2GB in size and
1452 with 2 million pubnames entries. */
1453 #define MIN_CU_HDR_SIZE 10
1454 dbg = con->pu_dbg;
1455 CHECK_DBG(dbg,error,"dwarf_global_name_offsets()");
1456 /* Cannot refer to debug_types, see p141 of
1457 DWARF4 Standard */
1458 if (dbg->de_debug_info.dss_size &&
1459 ((cuhdr_off + MIN_CU_HDR_SIZE) >=
1460 dbg->de_debug_info.dss_size)) {
1461 dwarfstring m;
1462
1463 dwarfstring_constructor(&m);
1464 build_off_end_msg(cuhdr_off,cuhdr_off,
1465 dbg->de_debug_info.dss_size,&m);
1466 _dwarf_error_string(dbg, error, DW_DLE_OFFSET_BAD,
1467 dwarfstring_string(&m));
1468 dwarfstring_destructor(&m);
1469 return DW_DLV_ERROR;
1470 }
1471 /* If global->gl_named_die_offset_within_cu
1472 is zero then this is a fake global for
1473 a pubnames CU with no pubnames. The offset is from the
1474 start of the CU header, so no die can have a zero
1475 offset, all valid offsets are positive numbers */
1476 if (die_offset) {
1477 if (global->gl_named_die_offset_within_cu) {
1478 *die_offset = global->gl_named_die_offset_within_cu +
1479 cuhdr_off;
1480 } else {
1481 *die_offset = 0;
1482 }
1483 }
1484 #undef MIN_CU_HDR_SIZE
1485 *ret_name = (char *) global->gl_name;
1486 if (cu_die_offset) {
1487 /* Global cannot refer to debug_types */
1488 int cres = 0;
1489 Dwarf_Unsigned headerlen = 0;
1490 int res = _dwarf_load_debug_info(dbg, error);
1491
1492 if (res != DW_DLV_OK) {
1493 return res;
1494 }
1495 /* We already checked to make sure enough room
1496 with MIN_CU_HDR_SIZE */
1497 #if 0 /* Heuristic check for about to pass end, not usable. */
1498 /* The offset had better not be too close to the end.
1499 If it is,
1500 _dwarf_length_of_cu_header() will step off the end and
1501 therefore must not be used. 10 is a meaningless heuristic,
1502 but no CU header is that small so it is safe. */
1503 /* Global cannot refer to debug_types */
1504 if ((cuhdr_off + MIN_CU_HDR_SIZE)
1505 >= dbg->de_debug_info.dss_size) {
1506 dwarfstring m;
1507
1508 dwarfstring_constructor(&m);
1509 build_off_end_msg(cuhdr_off,cuhdr_off,
1510 dbg->de_debug_info.dss_size,&m);
1511 _dwarf_error_string(dbg, error, DW_DLE_OFFSET_BAD,
1512 dwarfstring_string(&m));
1513 dwarfstring_destructor(&m);
1514 return DW_DLV_ERROR;
1515 }
1516 #endif /* 0 */
1517 cres = _dwarf_length_of_cu_header(dbg, cuhdr_off,TRUE,
1518 &headerlen,error);
1519 if (cres != DW_DLV_OK) {
1520 return cres;
1521 }
1522 *cu_die_offset = cuhdr_off + headerlen;
1523 }
1524 return DW_DLV_OK;
1525 }
1526
1527 /* New February 2019 from better dwarfdump printing
1528 of debug_pubnames and pubtypes.
1529 For all the Dwarf_Global records in one pubnames
1530 CU group exactly the same data will be returned.
1531
1532 */
1533 int
1534 dwarf_get_globals_header(Dwarf_Global global,
1535 int *category,/* DW_GL_GLOBAL for example*/
1536 Dwarf_Off *pub_section_hdr_offset,
1537 Dwarf_Unsigned *pub_offset_size,
1538 Dwarf_Unsigned *pub_cu_length,
1539 Dwarf_Unsigned *version,
1540 Dwarf_Off *info_header_offset,
1541 Dwarf_Unsigned *info_length,
1542 Dwarf_Error* error)
1543 {
1544 Dwarf_Global_Context con = 0;
1545 Dwarf_Debug dbg = 0;
1546
1547 if (global == NULL) {
1548 _dwarf_error(NULL, error, DW_DLE_GLOBAL_NULL);
1549 return DW_DLV_ERROR;
1550 }
1551 con = global->gl_context;
1552 if (con == NULL) {
1553 _dwarf_error(NULL, error, DW_DLE_GLOBAL_CONTEXT_NULL);
1554 return DW_DLV_ERROR;
1555 }
1556 dbg = con->pu_dbg;
1557 CHECK_DBG(dbg,error,"dwarf_get_globals_header()");
1558 if (category) {
1559 *category = con->pu_global_category;
1560 }
1561 if (pub_section_hdr_offset) {
1562 *pub_section_hdr_offset = con->pu_pub_offset;
1563 }
1564 if (pub_offset_size) {
1565 *pub_offset_size = con->pu_length_size;
1566 }
1567 if (pub_cu_length) {
1568 *pub_cu_length = con->pu_length;
1569 }
1570 if (version) {
1571 *version = con->pu_version;
1572 }
1573 if (info_header_offset) {
1574 *info_header_offset = con->pu_offset_of_cu_header;
1575 }
1576 if (info_length) {
1577 *info_length = con->pu_info_length;
1578 }
1579 return DW_DLV_OK;
1580 }
1581
1582 /* We have the offset to a CU header.
1583 Return thru outFileOffset the offset of the CU DIE.
1584
1585 New June, 2001.
1586 Used by SGI IRIX debuggers.
1587 No error used to be possible.
1588 As of May 2016 an error is possible if the DWARF is
1589 corrupted! (IRIX debuggers are no longer built ...)
1590
1591 See also dwarf_CU_dieoffset_given_die().
1592
1593 This is assumed to never apply to data in .debug_types, it
1594 only refers to .debug_info.
1595
1596 */
1597
1598 /* ARGSUSED */
1599 /* The following version new in October 2011, does allow finding
1600 the offset if one knows whether debug_info or debug_types
1601 or any .debug_info type including the DWARF5 flavors.
1602
1603 It indirectly calls _dwarf_length_of_cu_header()
1604 which knows all the varieties of header. */
1605 int
1606 dwarf_get_cu_die_offset_given_cu_header_offset_b(Dwarf_Debug dbg,
1607 Dwarf_Off in_cu_header_offset,
1608 Dwarf_Bool is_info,
1609 Dwarf_Off * out_cu_die_offset,
1610 Dwarf_Error * error)
1611 {
1612 Dwarf_Off headerlen = 0;
1613 int cres = 0;
1614
1615 if (IS_INVALID_DBG(dbg)) {
1616 _dwarf_error_string(NULL, error, DW_DLE_DBG_NULL,
1617 "DW_DLE_DBG_NULL: "
1618 "calling dwarf_get_cu_die_offset_given"
1619 "cu_header_offset_b Dwarf_Debug is"
1620 "either null or it is"
1621 "a stale Dwarf_Debug pointer");
1622 return DW_DLV_ERROR;
1623 }
1624 cres = _dwarf_length_of_cu_header(dbg,
1625 in_cu_header_offset,is_info, &headerlen,error);
1626 if (cres != DW_DLV_OK) {
1627 return cres;
1628 }
1629 *out_cu_die_offset = in_cu_header_offset + headerlen;
1630 return DW_DLV_OK;
1631 }
1632 /* dwarf_CU_dieoffset_given_die returns
1633 the global debug_info section offset of the CU die
1634 that is the CU containing the given (passed-in) die.
1635 This information makes it possible for a consumer to
1636 find and print context information for any die.
1637
1638 Use dwarf_offdie_b() passing in the offset this returns
1639 to get a die pointer to the CU die. */
1640 int
1641 dwarf_CU_dieoffset_given_die(Dwarf_Die die,
1642 Dwarf_Off* return_offset,
1643 Dwarf_Error* error)
1644 {
1645 Dwarf_Off dieoff = 0;
1646 Dwarf_CU_Context cucontext = 0;
1647
1648 CHECK_DIE(die, DW_DLV_ERROR);
1649 cucontext = die->di_cu_context;
1650 dieoff = cucontext->cc_debug_offset;
1651 /* The following call cannot fail, so no error check. */
1652 dwarf_get_cu_die_offset_given_cu_header_offset_b(
1653 cucontext->cc_dbg, dieoff,
1654 die->di_is_info, return_offset,error);
1655 return DW_DLV_OK;
1656 }
1657
1658 /* Just sets a flag in dbg record if it can. */
1659 int
1660 dwarf_return_empty_pubnames(Dwarf_Debug dbg, int flag)
1661 {
1662 if (IS_INVALID_DBG(dbg)) {
1663 return DW_DLV_OK;
1664 }
1665 if (flag && flag != 1) {
1666 return DW_DLV_OK;
1667 }
1668 dbg->de_return_empty_pubnames = (unsigned char)flag;
1669 return DW_DLV_OK;
1670 }
1671
1672 Dwarf_Half
1673 dwarf_global_tag_number(Dwarf_Global dw_global)
1674 {
1675 if (!dw_global) {
1676 return 0;
1677 }
1678 return dw_global->gl_tag;
1679 }