Debian Codegraph @

src/bin/dwarfdump/print_gdbindex.c

raw
1 /*
2 Copyright 2014-2020 David Anderson. All rights reserved.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of version 2 of the GNU General
6 Public License as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it would be
9 useful, but WITHOUT ANY WARRANTY; without even the implied
10 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE.
12
13 Further, this software is distributed without any warranty
14 that it is free of the rightful claim of any third person
15 regarding infringement or the like. Any license provided
16 herein, whether implied or otherwise, applies only to this
17 software file. Patent licenses, if any, provided herein
18 do not apply to combinations of this program with other
19 software, or any other product whatsoever.
20
21 You should have received a copy of the GNU General Public
22 License along with this program; if not, write the Free
23 Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24 Boston MA 02110-1301, USA.
25
26 */
27
28 #include <config.h>
29 #include <stdio.h> /* FILE decl for dd_esb.h, printf etc */
30
31 #include "dwarf.h"
32 #include "libdwarf.h"
33 #include "libdwarf_private.h"
34 #include "dd_defined_types.h"
35 #include "dd_checkutil.h"
36 #include "dd_glflags.h"
37 #include "dd_globals.h"
38 #include "dd_naming.h"
39 #include "dd_esb.h"
40 #include "dd_esb_using_functions.h"
41 #include "dd_sanitized.h"
42
43 #include "print_sections.h"
44
45 static const char *
46 dw_dlv_string(int res)
47 {
48 if (res == DW_DLV_ERROR) {
49 return "DW_DLV_ERROR";
50 }
51 if (res == DW_DLV_NO_ENTRY) {
52 return "DW_DLV_NO_ENTRY";
53 }
54 if (res == DW_DLV_OK) {
55 return "DW_DLV_OK";
56 }
57 return "ERROR: Impossible libdwarf DW_DLV code";
58 }
59
60 static int
61 print_culist_array(Dwarf_Gdbindex gdbindex,
62 Dwarf_Unsigned *cu_list_len,
63 Dwarf_Error * err)
64 {
65 Dwarf_Unsigned list_len = 0;
66 Dwarf_Unsigned i;
67 int res = dwarf_gdbindex_culist_array(gdbindex,
68 &list_len,err);
69 if (res == DW_DLV_NO_ENTRY) {
70 return res;
71 }
72 if (res == DW_DLV_ERROR) {
73 simple_err_return_msg_either_action(res,
74 "ERROR: dwarf_gdbindex_culist_array failed.");
75 return res;
76 }
77 printf(" CU list. array length: %" DW_PR_DUu
78 " format: [entry#] cuoffset culength\n",
79 list_len);
80
81 for (i = 0; i < list_len; i++) {
82 Dwarf_Unsigned cuoffset = 0;
83 Dwarf_Unsigned culength = 0;
84
85 res = dwarf_gdbindex_culist_entry(gdbindex,i,
86 &cuoffset,&culength,err);
87 if (res != DW_DLV_OK) {
88 struct esb_s msg;
89 const char * et= dw_dlv_string(res);
90
91 esb_constructor(&msg);
92 esb_append_printf_s(&msg,
93 "ERROR: dwarf_get_gdbindex_culist_entry "
94 "got %s ",et);
95 esb_append_printf_u(&msg,
96 " on entry %u ",i);
97 esb_append_printf_u(&msg," of %u entries.",
98 list_len);
99 simple_err_return_action(res,
100 esb_get_string(&msg));
101 esb_destructor(&msg);
102 return res;
103 }
104 printf(" [%4" DW_PR_DUu "] 0x%"
105 DW_PR_XZEROS DW_PR_DUx
106 " 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
107 i,
108 cuoffset,
109 culength);
110 }
111 printf("\n");
112 *cu_list_len = list_len;
113 return DW_DLV_OK;
114 }
115
116 static int
117 print_types_culist_array(Dwarf_Gdbindex gdbindex,
118 Dwarf_Error * cular_err)
119 {
120 Dwarf_Unsigned list_len = 0;
121 Dwarf_Unsigned i;
122 int res = 0;
123
124 res = dwarf_gdbindex_types_culist_array(gdbindex,
125 &list_len,cular_err);
126 if (res != DW_DLV_OK) {
127 print_error_and_continue(
128 "dwarf_gdbindex_types_culist_array call failed",
129 res,*cular_err);
130 return res;
131 }
132 printf(" TU list. array length: %" DW_PR_DUu
133 " format: [entry#] cuoffset culength signature\n",
134 list_len);
135
136 for (i = 0; i < list_len; i++) {
137 Dwarf_Unsigned cuoffset = 0;
138 Dwarf_Unsigned culength = 0;
139 Dwarf_Unsigned signature = 0;
140
141 res = dwarf_gdbindex_types_culist_entry(gdbindex,i,
142 &cuoffset,&culength,
143 &signature,
144 cular_err);
145 if (res != DW_DLV_OK) {
146 struct esb_s msg;
147 const char * et= dw_dlv_string(res);
148
149 esb_constructor(&msg);
150 esb_append_printf_s(&msg,
151 "ERROR: dwarf_get_gdbindex_culist_entry call "
152 "got %s ",et);
153 esb_append_printf_u(&msg,
154 " on entry %u ",i);
155 esb_append_printf_u(&msg," of %u entries.",
156 list_len);
157 simple_err_return_action(res,
158 esb_get_string(&msg));
159 esb_destructor(&msg);
160 return res;
161 }
162 printf(" [%4" DW_PR_DUu "] 0x%"
163 DW_PR_XZEROS DW_PR_DUx
164 " 0x%" DW_PR_XZEROS DW_PR_DUx
165 " 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
166 i,
167 cuoffset,
168 culength,
169 signature);
170 }
171 printf("\n");
172 return DW_DLV_OK;
173 }
174
175 static int
176 print_addressarea(Dwarf_Gdbindex gdbindex,
177 Dwarf_Error * addra_err)
178 {
179 Dwarf_Unsigned list_len = 0;
180 Dwarf_Unsigned i;
181 int res = dwarf_gdbindex_addressarea(gdbindex,
182 &list_len,addra_err);
183 if (res != DW_DLV_OK) {
184 print_error_and_continue(
185 "dwarf_gdbindex_addressarea failed",res,*addra_err);
186 return res;
187 }
188 printf(" Address table array length: %" DW_PR_DUu
189 " format: [entry#] lowpc highpc cu-index\n",
190 list_len);
191
192 for (i = 0; i < list_len; i++) {
193 Dwarf_Unsigned lowpc = 0;
194 Dwarf_Unsigned highpc = 0;
195 Dwarf_Unsigned cu_index = 0;
196
197 res = dwarf_gdbindex_addressarea_entry(gdbindex,i,
198 &lowpc,&highpc,
199 &cu_index,
200 addra_err);
201 if (res != DW_DLV_OK) {
202 struct esb_s msg;
203 const char * et= dw_dlv_string(res);
204
205 esb_constructor(&msg);
206 esb_append_printf_s(&msg,
207 "ERROR: dwarf_get_gdbindex_addressarea_entry call "
208 "got %s ",et);
209 esb_append_printf_u(&msg,
210 " on entry %u ",i);
211 esb_append_printf_u(&msg," of %u entries.",
212 list_len);
213 simple_err_return_action(res,
214 esb_get_string(&msg));
215 esb_destructor(&msg);
216 return res;
217 }
218 printf(" [%4" DW_PR_DUu "] 0x%"
219 DW_PR_XZEROS DW_PR_DUx
220 " 0x%" DW_PR_XZEROS DW_PR_DUx
221 " %4" DW_PR_DUu "\n",
222 i,
223 lowpc,
224 highpc,
225 cu_index);
226 }
227 printf("\n");
228 return DW_DLV_OK;
229 }
230
231 const char *kind_list[] = {
232 "unknown(0) ",
233 "type(1) ",
234 "var-enum(2) ",
235 "function(3) ",
236 "other-sym(4)",
237 "reserved(5) ",
238 "function(6) ",
239 "reserved(7) ",
240 };
241
242 static void
243 get_kind_string(struct esb_s *out,Dwarf_Unsigned k)
244 {
245 if (k <= 7) {
246 esb_append(out,sanitized(kind_list[k]));
247 return;
248 }
249 esb_append(out, "kind-erroneous");
250 }
251
252 /* NOTE: Returns string through esb pointer */
253 static void
254 get_cu_index_string(struct esb_s *out,
255 Dwarf_Unsigned index,
256 Dwarf_Unsigned culist_len)
257 {
258 Dwarf_Unsigned type_index = 0;
259 if (index < culist_len) {
260 esb_append_printf_u(out,"%4" DW_PR_DUu,index);
261 return;
262 }
263 type_index = index-culist_len;
264 esb_append_printf_u(out, "%4" DW_PR_DUu ,index);
265 esb_append_printf_u(out, "(T%4" DW_PR_DUu ")",type_index);
266 return;
267 }
268
269 static int
270 print_symtab_entry(Dwarf_Gdbindex gdbindex,
271 Dwarf_Unsigned index,
272 Dwarf_Unsigned symnameoffset,
273 Dwarf_Unsigned cuvecoffset,
274 Dwarf_Unsigned culist_len,
275 Dwarf_Error *sym_err)
276 {
277 int res = 0;
278 const char *name = 0;
279 Dwarf_Unsigned cuvec_len = 0;
280 Dwarf_Unsigned ii = 0;
281
282 if (symnameoffset == 0 && cuvecoffset == 0) {
283 if (glflags.verbose > 1) {
284 printf(" [%4" DW_PR_DUu "] "
285 "\"empty-hash-entry\"\n",
286 index);
287 }
288 return DW_DLV_OK;
289 }
290 res = dwarf_gdbindex_string_by_offset(gdbindex,
291 symnameoffset,&name,sym_err);
292 if (res != DW_DLV_OK) {
293 struct esb_s msg;
294 const char * et= dw_dlv_string(res);
295
296 esb_constructor(&msg);
297 esb_append_printf_s(&msg,
298 "ERROR: dwarf_get_gdbindex_string_by_offset call "
299 "failed with %s ",et);
300 esb_append_printf_u(&msg,
301 " on offset 0x%lx",symnameoffset);
302 esb_append_printf_u(&msg,
303 " (%u).",symnameoffset);
304 simple_err_return_action(res,
305 esb_get_string(&msg));
306 esb_destructor(&msg);
307 return res;
308 }
309 res = dwarf_gdbindex_cuvector_length(gdbindex,
310 cuvecoffset,&cuvec_len,sym_err);
311 if (res != DW_DLV_OK) {
312 struct esb_s msg;
313 const char * et= dw_dlv_string(res);
314
315 esb_constructor(&msg);
316 esb_append_printf_s(&msg,
317 "ERROR: dwarf_get_gdbindex_cuvector_length() call "
318 "failed with %s ",et);
319 esb_append_printf_u(&msg,
320 " on cu vector offset 0x%lx",cuvecoffset);
321 esb_append_printf_u(&msg,
322 " (%u).",cuvecoffset);
323 simple_err_return_action(res,
324 esb_get_string(&msg));
325 esb_destructor(&msg);
326 return res;
327 }
328 if (glflags.verbose > 1) {
329 printf(" [%4" DW_PR_DUu "]"
330 "stroff 0x%" DW_PR_XZEROS DW_PR_DUx
331 " cuvecoff 0x%" DW_PR_XZEROS DW_PR_DUx
332 " cuveclen 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
333 index,symnameoffset,cuvecoffset,cuvec_len);
334 }
335 for (ii = 0; ii < cuvec_len; ++ii ) {
336 Dwarf_Unsigned attributes = 0;
337 Dwarf_Unsigned cu_index = 0;
338 Dwarf_Unsigned symbol_kind = 0;
339 Dwarf_Unsigned is_static = 0;
340 struct esb_s tmp_cuindx;
341 struct esb_s tmp_kind;
342
343 res = dwarf_gdbindex_cuvector_inner_attributes(
344 gdbindex,cuvecoffset,ii,
345 &attributes,sym_err);
346 if (res != DW_DLV_OK) {
347 print_error_and_continue(
348 "dwarf_gdbindex_cuvector_inner_attributes failed",
349 res,*sym_err);
350 return res;
351 }
352 res = dwarf_gdbindex_cuvector_instance_expand_value(gdbindex,
353 attributes, &cu_index,&symbol_kind,
354 &is_static, sym_err);
355 if (res != DW_DLV_OK) {
356 struct esb_s msg;
357 const char * et= dw_dlv_string(res);
358
359 esb_constructor(&msg);
360 esb_append_printf_s(&msg,
361 "ERROR: dwarf_gdbindex_cuvector_instance_expand"
362 "_value() call "
363 "failed with %s ",et);
364 esb_append_printf_u(&msg,
365 " on cu vector index %d ",ii);
366 esb_append_printf_u(&msg,
367 " on of %d entries.",cuvec_len);
368 simple_err_return_action(res,
369 esb_get_string(&msg));
370 esb_destructor(&msg);
371 return res;
372 }
373 /* if cu_index is > the cu-count, then it refers
374 to a tu_index of 'cu_index - cu-count' */
375 esb_constructor(&tmp_cuindx);
376 esb_constructor(&tmp_kind);
377 get_kind_string(&tmp_kind,symbol_kind),
378 get_cu_index_string(&tmp_cuindx,cu_index,culist_len);
379 if (cuvec_len == 1) {
380 printf(" [%4" DW_PR_DUu "]"
381 "%s"
382 " [%s %s] \"%s\"\n",
383 index,
384 esb_get_string(&tmp_cuindx),
385 is_static?
386 "static ":
387 "global ",
388 esb_get_string(&tmp_kind),
389 sanitized(name));
390 } else if (ii == 0) {
391 printf(" [%4" DW_PR_DUu "] \"%s\"\n" ,
392 index,
393 sanitized(name));
394 printf(" %s [%s %s]\n",
395 esb_get_string(&tmp_cuindx),
396 is_static?
397 "static ":
398 "global ",
399 esb_get_string(&tmp_kind));
400 }else{
401 printf(" %s [%s %s]\n",
402 esb_get_string(&tmp_cuindx),
403 is_static?
404 "static ":
405 "global ",
406 esb_get_string(&tmp_kind));
407 }
408 esb_destructor(&tmp_cuindx);
409 esb_destructor(&tmp_kind);
410 if (glflags.verbose > 1) {
411 printf(" [%4" DW_PR_DUu "]"
412 "attr 0x%" DW_PR_XZEROS DW_PR_DUx
413 " cuindx 0x%" DW_PR_XZEROS DW_PR_DUx
414 " kind 0x%" DW_PR_XZEROS DW_PR_DUx
415 " static 0x%" DW_PR_XZEROS DW_PR_DUx "\n",
416 ii,attributes,cu_index,symbol_kind,is_static);
417 }
418 }
419 return DW_DLV_OK;
420 }
421
422 static int
423 print_symboltable(Dwarf_Gdbindex gdbindex,
424 Dwarf_Unsigned culist_len,
425 Dwarf_Error * symt_err)
426 {
427 Dwarf_Unsigned list_len = 0;
428 Dwarf_Unsigned i;
429 int res = dwarf_gdbindex_symboltable_array(gdbindex,
430 &list_len,symt_err);
431 if (res != DW_DLV_OK) {
432 struct esb_s msg;
433 const char * et= dw_dlv_string(res);
434
435 esb_constructor(&msg);
436 esb_append_printf_s(&msg,
437 "ERROR: dwarf_get_gdbindex_symboltable_array call "
438 "failed with %s ",et);
439 simple_err_return_action(res,
440 esb_get_string(&msg));
441 esb_destructor(&msg);
442 return res;
443 }
444 printf("\n Symbol table: length %" DW_PR_DUu
445 " format: [entry#] symindex cuindex [type] \"name\" or \n",
446 list_len);
447 printf(" "
448 " format: [entry#] \"name\" , list of cuindex [type]\n");
449
450 for (i = 0; i < list_len; i++) {
451 Dwarf_Unsigned symnameoffset = 0;
452 Dwarf_Unsigned cuvecoffset = 0;
453 res = dwarf_gdbindex_symboltable_entry(gdbindex,i,
454 &symnameoffset,&cuvecoffset,
455 symt_err);
456 if (res != DW_DLV_OK) {
457 struct esb_s msg;
458 const char * et= dw_dlv_string(res);
459
460 esb_constructor(&msg);
461 esb_append_printf_s(&msg,
462 "ERROR: dwarf_gdbindex_symboltable_entry() call "
463 "failed with %s ",et);
464 esb_append_printf_u(&msg,
465 " on symtab index %d ",i);
466 esb_append_printf_u(&msg,
467 " on of %d entries.",list_len);
468 simple_err_return_action(res,
469 esb_get_string(&msg));
470 esb_destructor(&msg);
471 return res;
472 }
473 res = print_symtab_entry(gdbindex,i,
474 symnameoffset,cuvecoffset,
475 culist_len,symt_err);
476 if (res != DW_DLV_OK) {
477 return res;
478 }
479 }
480 printf("\n");
481 return DW_DLV_OK;
482 }
483
484 int
485 print_gdb_index(Dwarf_Debug dbg,Dwarf_Error *err)
486 {
487 Dwarf_Gdbindex gdbindex = 0;
488 Dwarf_Unsigned version = 0;
489 Dwarf_Unsigned cu_list_offset = 0;
490 Dwarf_Unsigned types_cu_list_offset = 0;
491 Dwarf_Unsigned address_area_offset = 0;
492 Dwarf_Unsigned symbol_table_offset = 0;
493 Dwarf_Unsigned constant_pool_offset = 0;
494 Dwarf_Unsigned section_size = 0;
495 const char *section_name = 0; /* unused */
496 Dwarf_Unsigned culist_len = 0;
497 int res = 0;
498
499 glflags.current_section_id = DEBUG_GDB_INDEX;
500 if (!glflags.gf_do_print_dwarf) {
501 return DW_DLV_OK;
502 }
503 res = dwarf_gdbindex_header(dbg, &gdbindex,
504 &version,
505 &cu_list_offset,
506 &types_cu_list_offset,
507 &address_area_offset,
508 &symbol_table_offset,
509 &constant_pool_offset,
510 &section_size,
511 &section_name,
512 err);
513 if (res == DW_DLV_NO_ENTRY) {
514 /* Silently! The section is rare so lets
515 say nothing. */
516 return res;
517 }
518 if (res == DW_DLV_ERROR) {
519 printf(" ERROR: .gdb_index not readable %s\n",
520 err?dwarf_errmsg(*err):"No details available");
521 glflags.gf_count_major_errors++;
522 return res;
523 }
524 {
525 struct esb_s truename;
526 char buf[DWARF_SECNAME_BUFFER_SIZE];
527
528 esb_constructor_fixed(&truename,buf,sizeof(buf));
529 get_true_section_name(dbg,".gdb_index",
530 &truename,TRUE);
531 printf("\n%s\n",sanitized(esb_get_string(&truename)));
532 esb_destructor(&truename);
533 }
534
535 printf(" Version : "
536 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
537 version);
538 printf(" CU list offset : "
539 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
540 cu_list_offset);
541 printf(" Address area offset : "
542 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
543 types_cu_list_offset);
544 printf(" Symbol table offset : "
545 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
546 address_area_offset);
547 printf(" Constant pool offset: "
548 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
549 constant_pool_offset);
550 printf(" section size : "
551 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
552 section_size);
553
554 res = print_culist_array(gdbindex,&culist_len,err);
555 if (res != DW_DLV_OK) {
556 dwarf_dealloc_gdbindex(gdbindex);
557 return res;
558 }
559 res = print_types_culist_array(gdbindex,err);
560 if (res != DW_DLV_OK) {
561 dwarf_dealloc_gdbindex(gdbindex);
562 return res;
563 }
564 res = print_addressarea(gdbindex,err);
565 if (res != DW_DLV_OK) {
566 dwarf_dealloc_gdbindex(gdbindex);
567 return res;
568 }
569 res = print_symboltable(gdbindex,culist_len,err);
570 dwarf_dealloc_gdbindex(gdbindex);
571 return res;
572 }