Debian Codegraph @

src/lib/libdwarf/dwarf_print_lines.c

raw
1 /*
2 Copyright (C) 2000,2002,2004,2005,2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2020 David Anderson. All Rights Reserved.
4 Portions Copyright 2012 SN Systems Ltd. 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 #include <config.h>
31
32 #include <stdlib.h> /* free() malloc() realloc() */
33 #include <string.h> /* memset() strlen() */
34 #include <time.h> /* ctime() */
35
36 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
37 #include "stdafx.h"
38 #endif /* HAVE_STDAFX_H */
39
40 #include "dwarf.h"
41 #include "libdwarf.h"
42 #include "libdwarf_private.h"
43 #include "dwarf_base_types.h"
44 #include "dwarf_opaque.h"
45 #include "dwarf_alloc.h"
46 #include "dwarf_error.h"
47 #include "dwarf_util.h"
48 #include "dwarf_line.h"
49 #include "dwarf_string.h"
50
51 #define PRINTING_DETAILS 1
52 /* for dwarfstring_constructor_static, saving lots of malloc
53 and free but beware: make sure each routine using
54 this DOES NOT call another routine using it.
55 And do not use it in Dwarf_Error related calls.
56 would be safer to have a buffer per function, but
57 currently not necessary. */
58 static char locallinebuf[200];
59
60 static void
61 print_line_header(Dwarf_Debug dbg,
62 Dwarf_Bool is_single_tab,
63 Dwarf_Bool is_actuals_tab)
64 {
65 if (!is_single_tab) {
66 /* Ugly indenting follows, it makes lines shorter
67 to see them better.
68 Best to use a wider text window to really
69 see how it looks.*/
70 if (is_actuals_tab) {
71 _dwarf_printf(dbg,"\nActuals Table\n");
72 _dwarf_printf(dbg,
73 " be\n"
74 " ls\n"
75 " ce\n"
76 " section op kq\n"
77 " offset code address/index row isa ??\n");
78 return;
79 } else {
80 _dwarf_printf(dbg,"\nLogicals Table\n");
81 _dwarf_printf(dbg,
82 " "
83 " s pe\n"
84 " "
85 " tirp\n"
86 " "
87 " msoi\n"
88 " section op "
89 " tall\n"
90 " offset row code address/indx fil l"
91 "ne col disc cntx subp ????\n");
92 return;
93 }
94 }
95
96 /* Single level table */
97 _dwarf_printf(dbg,
98 " "
99 "s b e p e i d\n"
100 " "
101 "t l s r p s i\n"
102 " "
103 "m c e o i a s\n"
104 " section op col "
105 "t k q l l c\n"
106 " offset code address file line umn ? ? ? ? ?\n"
107 );
108 } /* End of function with ugly indenting. */
109
110 static void
111 print_line_detail(
112 Dwarf_Debug dbg,
113 const char *prefix,
114 int opcode,
115 unsigned curr_line,
116 struct Dwarf_Line_Registers_s * regs,
117 Dwarf_Bool is_single_table, Dwarf_Bool is_actuals_table)
118 {
119 dwarfstring m1;
120
121 dwarfstring_constructor_static(&m1,locallinebuf,
122 sizeof(locallinebuf));
123 if (!is_single_table && is_actuals_table) {
124 dwarfstring_append_printf_s(&m1,"%-15s ",(char *)prefix);
125 dwarfstring_append_printf_i(&m1,"%3d ",opcode);
126 dwarfstring_append_printf_u(&m1,"0x%" DW_PR_XZEROS DW_PR_DUx,
127 regs->lr_address);
128 dwarfstring_append_printf_u(&m1,"/%01u",regs->lr_op_index);
129 dwarfstring_append_printf_u(&m1," %5lu", regs->lr_line);
130 dwarfstring_append_printf_u(&m1," %3u",regs->lr_isa);
131 dwarfstring_append_printf_i(&m1," %1d",
132 regs->lr_basic_block);
133 dwarfstring_append_printf_i(&m1,"%1d\n",
134 regs->lr_end_sequence);
135 _dwarf_printf(dbg,dwarfstring_string(&m1));
136 dwarfstring_destructor(&m1);
137 return;
138 }
139 if (!is_single_table && !is_actuals_table) {
140 dwarfstring_append_printf_i(&m1,
141 "[%3d] " /* row number */, curr_line);
142 dwarfstring_append_printf_s(&m1,
143 "%-15s ",(char *)prefix);
144 dwarfstring_append_printf_i(&m1,
145 "%3d ",opcode);
146 dwarfstring_append_printf_u(&m1,
147 "x%" DW_PR_XZEROS DW_PR_DUx, regs->lr_address);
148 dwarfstring_append_printf_u(&m1,
149 "/%01u", regs->lr_op_index);
150 dwarfstring_append_printf_u(&m1," %2lu ",regs->lr_file);
151 dwarfstring_append_printf_u(&m1,"%4lu ",regs->lr_line);
152 dwarfstring_append_printf_u(&m1,"%1lu",regs->lr_column);
153 if (regs->lr_discriminator ||
154 regs->lr_prologue_end ||
155 regs->lr_epilogue_begin ||
156 regs->lr_isa ||
157 regs->lr_is_stmt ||
158 regs->lr_call_context ||
159 regs->lr_subprogram) {
160 dwarfstring_append_printf_u(&m1,
161 " x%02" DW_PR_DUx ,
162 regs->lr_discriminator); /* DWARF4 */
163 dwarfstring_append_printf_u(&m1,
164 " x%02" DW_PR_DUx,
165 regs->lr_call_context); /* EXPERIMENTAL */
166 dwarfstring_append_printf_u(&m1,
167 " x%02" DW_PR_DUx ,
168 regs->lr_subprogram); /* EXPERIMENTAL */
169 dwarfstring_append_printf_i(&m1,
170 " %1d", regs->lr_is_stmt);
171 dwarfstring_append_printf_i(&m1,
172 "%1d", (int) regs->lr_isa);
173 dwarfstring_append_printf_i(&m1,
174 "%1d", regs->lr_prologue_end); /* DWARF3 */
175 dwarfstring_append_printf_i(&m1,
176 "%1d", regs->lr_epilogue_begin); /* DWARF3 */
177 }
178 dwarfstring_append(&m1,"\n");
179 _dwarf_printf(dbg,dwarfstring_string(&m1));
180 dwarfstring_destructor(&m1);
181 return;
182 }
183 /* In the first quoted line below:
184 3d looks better than 2d, but best to do that as separate
185 change and test from two-level-line-tables. */
186 dwarfstring_append_printf_s(&m1,
187 "%-15s ",(char *)prefix);
188 dwarfstring_append_printf_i(&m1,
189 "%2d ",opcode);
190 dwarfstring_append_printf_u(&m1,
191 "0x%" DW_PR_XZEROS DW_PR_DUx " ",
192 regs->lr_address);
193 dwarfstring_append_printf_u(&m1,
194 "%2lu ", regs->lr_file);
195 dwarfstring_append_printf_u(&m1,
196 "%4lu ", regs->lr_line);
197 dwarfstring_append_printf_u(&m1,
198 "%2lu ", regs->lr_column);
199 dwarfstring_append_printf_i(&m1,
200 "%1d ",regs->lr_is_stmt);
201 dwarfstring_append_printf_i(&m1,
202 "%1d ", regs->lr_basic_block);
203 dwarfstring_append_printf_i(&m1,
204 "%1d",regs->lr_end_sequence);
205 if (regs->lr_discriminator ||
206 regs->lr_prologue_end ||
207 regs->lr_epilogue_begin ||
208 regs->lr_isa) {
209 dwarfstring_append_printf_i(&m1,
210 " %1d", regs->lr_prologue_end); /* DWARF3 */
211 dwarfstring_append_printf_i(&m1,
212 " %1d", regs->lr_epilogue_begin); /* DWARF3 */
213 dwarfstring_append_printf_i(&m1,
214 " %1d", regs->lr_isa); /* DWARF3 */
215 dwarfstring_append_printf_u(&m1,
216 " 0x%" DW_PR_DUx , regs->lr_discriminator); /* DWARF4 */
217 }
218 dwarfstring_append(&m1, "\n");
219 _dwarf_printf(dbg,dwarfstring_string(&m1));
220 dwarfstring_destructor(&m1);
221 }
222
223 #include "dwarf_line_table_reader_common.h"
224
225 static void
226 print_include_directory_details(Dwarf_Debug dbg,
227 unsigned int line_version,
228 Dwarf_Line_Context line_context)
229 {
230 Dwarf_Unsigned u = 0;
231 dwarfstring m4;
232 Dwarf_Unsigned indexbase = 0;
233 Dwarf_Unsigned indexlimit = 0;
234
235 dwarfstring_constructor_static(&m4,locallinebuf,
236 sizeof(locallinebuf));
237 if (line_version == DW_LINE_VERSION5) {
238 Dwarf_Unsigned i = 0;
239 Dwarf_Unsigned dfcount =
240 line_context->lc_directory_entry_format_count;
241
242 dwarfstring_constructor(&m4);
243 dwarfstring_append_printf_u(&m4,
244 " directory entry format count %u\n",dfcount);
245 _dwarf_printf(dbg,dwarfstring_string(&m4));
246 dwarfstring_reset(&m4);
247 for ( ; i < dfcount;++i) {
248 struct Dwarf_Unsigned_Pair_s *valpair = 0;
249 const char *tname = 0;
250 const char *fname = 0;
251 int res;
252
253 valpair = line_context->lc_directory_format_values +i;
254 dwarfstring_append_printf_u(&m4,
255 " format [%2u] ",i);
256 res = dwarf_get_LNCT_name((unsigned int)
257 valpair->up_first, &tname);
258 if ( res != DW_DLV_OK) {
259 tname = "<unknown type>";
260 }
261 dwarfstring_append_printf_u (&m4,
262 " type 0x%" DW_PR_XZEROS DW_PR_DUx,
263 valpair->up_first);
264 dwarfstring_append_printf_s (&m4,
265 " %-20s\n",(char *)tname);
266 res = dwarf_get_FORM_name((unsigned int)
267 valpair->up_second,&fname);
268 if ( res != DW_DLV_OK) {
269 fname = "<unknown form>";
270 }
271 dwarfstring_append_printf_u(&m4,
272 " code 0x%" DW_PR_XZEROS DW_PR_DUx ,
273 valpair->up_second);
274 dwarfstring_append_printf_s(&m4,
275 " %-20s\n", (char *)fname);
276 _dwarf_printf(dbg,dwarfstring_string(&m4));
277 dwarfstring_reset(&m4);
278
279 }
280 }
281 /* Common print of the directories.
282 For DWARF 2,3,4 it has always started
283 the indexing at 0 even though the directory index
284 in line entries starts at 1 (zero meaning
285 current directory at compile time).
286 That is odd, given the non-dash-v printed
287 starting at 1. So lets adjust for consistency. */
288 if (line_version == DW_LINE_VERSION5) {
289 dwarfstring_append_printf_i(&m4,
290 " include directories count %d\n",
291 (int) line_context->lc_include_directories_count);
292 } else {
293 if (!line_context->lc_include_directories_count) {
294 dwarfstring_append_printf_i(&m4,
295 " include directories count %d\n",
296 (int) line_context->lc_include_directories_count);
297 } else {
298 dwarfstring_append_printf_i(&m4,
299 " include directories count %d"
300 " (index starts at 1)\n",
301 (int) line_context->lc_include_directories_count);
302 }
303 }
304 _dwarf_printf(dbg,dwarfstring_string(&m4));
305 dwarfstring_reset(&m4);
306 if (line_version == DW_LINE_VERSION5) {
307 indexbase = 0;
308 indexlimit = line_context->lc_include_directories_count;
309 } else {
310 indexbase = 1;
311 indexlimit = 1 + line_context->lc_include_directories_count;
312 }
313 for (u = indexbase; u < indexlimit; ++u) {
314 dwarfstring_append_printf_u(&m4,
315 " include dir[%u] ",u);
316 dwarfstring_append_printf_s(&m4,
317 "%s\n",(char *)
318 line_context->lc_include_directories[u-indexbase]);
319 _dwarf_printf(dbg,dwarfstring_string(&m4));
320 dwarfstring_reset(&m4);
321 }
322 dwarfstring_destructor(&m4);
323 }
324
325 static void
326 print_just_file_entry_details(Dwarf_Debug dbg,
327 Dwarf_Line_Context line_context)
328 {
329 unsigned fiu = 0;
330 Dwarf_File_Entry fe = line_context->lc_file_entries;
331 Dwarf_File_Entry fe2 = fe;
332 dwarfstring m3;
333 unsigned increment = 1;
334
335 if (line_context->lc_version_number == DW_LINE_VERSION5 ) {
336 increment = 0;
337 }
338 dwarfstring_constructor_static(&m3,locallinebuf,
339 sizeof(locallinebuf));
340 dwarfstring_append_printf_i(&m3,
341 " file names count %d\n",
342 line_context->lc_file_entry_count);
343 _dwarf_printf(dbg,dwarfstring_string(&m3));
344 dwarfstring_reset(&m3);
345 for (fiu = 0 ; fe2 ; fe2 = fe->fi_next,++fiu ) {
346 Dwarf_Unsigned tlm2 = 0;
347 unsigned filenum = 0;
348
349 fe = fe2;
350 tlm2 = fe->fi_time_last_mod;
351 filenum = fiu+increment;
352
353 /* The space character at the end of line is silly,
354 but lets leave it there for the moment to avoid
355 changing output. */
356 if (line_context->lc_file_entry_count > 9) {
357 dwarfstring_append_printf_u(&m3,
358 " file[%2u] ",fiu);
359 } else {
360 dwarfstring_append_printf_u(&m3,
361 " file[%u] ", fiu);
362 }
363 /* DWARF5 can have a null fi_file_name
364 if the format code in the
365 line table header is unknown, such
366 as in a corrupt object file. */
367 dwarfstring_append_printf_s(&m3,
368 "%-20s ",
369 fe->fi_file_name?
370 (char *) fe->fi_file_name:
371 "<no file name>");
372 dwarfstring_append_printf_u(&m3,
373 "(file-number: %u)\n",
374 filenum);
375 _dwarf_printf(dbg,dwarfstring_string(&m3));
376 dwarfstring_reset(&m3);
377 if (fe->fi_dir_index_present) {
378 Dwarf_Unsigned di = 0;
379 di = fe->fi_dir_index;
380 dwarfstring_append_printf_i(&m3,
381 " dir index %d\n", di);
382 }
383 if (fe->fi_time_last_mod_present) {
384 time_t tt = (time_t) tlm2;
385
386 /* ctime supplies newline */
387 dwarfstring_append_printf_u(&m3,
388 " last time 0x%x ",tlm2);
389 dwarfstring_append(&m3,(char *)ctime(&tt));
390 }
391 if (fe->fi_file_length_present) {
392 Dwarf_Unsigned fl = 0;
393
394 fl = fe->fi_file_length;
395 dwarfstring_append_printf_i(&m3,
396 " file length %ld ",fl);
397 dwarfstring_append_printf_u(&m3,
398 "0x%lx\n",fl);
399 }
400 if (fe->fi_md5_present) {
401 char *c = (char *)&fe->fi_md5_value;
402 char *end = c+sizeof(fe->fi_md5_value);
403 dwarfstring_append(&m3, " file md5 value 0x");
404 while(c < end) {
405 dwarfstring_append_printf_u(&m3,
406 "%02x",0xff&*c);
407 ++c;
408 }
409 dwarfstring_append(&m3,"\n");
410 }
411 if (fe->fi_llvm_source) {
412 dwarfstring_append_printf_s(&m3,
413 "%-20s\n",
414 (char *) fe->fi_llvm_source);
415 }
416 if (fe->fi_gnu_subprogram_name) {
417 dwarfstring_append_printf_s(&m3,
418 "%-20s\n",
419 (char *) fe->fi_gnu_subprogram_name);
420 }
421 if (fe->fi_gnu_decl_file_present) {
422 Dwarf_Unsigned di = 0;
423 di = fe->fi_gnu_decl_file;
424 dwarfstring_append_printf_i(&m3,
425 " gnu decl file %d\n", di);
426 }
427 if (fe->fi_gnu_decl_line_present) {
428 Dwarf_Unsigned di = 0;
429 di = fe->fi_gnu_decl_line;
430 dwarfstring_append_printf_i(&m3,
431 " gnu decl line %d\n", di);
432 }
433 if (dwarfstring_strlen(&m3)) {
434 _dwarf_printf(dbg,dwarfstring_string(&m3));
435 dwarfstring_reset(&m3);
436 }
437 }
438 dwarfstring_destructor(&m3);
439 }
440
441 static void
442 print_file_entry_details(Dwarf_Debug dbg,
443 unsigned int line_version,
444 Dwarf_Line_Context line_context)
445 {
446 dwarfstring m5;
447
448 dwarfstring_constructor_static(&m5,locallinebuf,
449 sizeof(locallinebuf));
450 if (line_version == DW_LINE_VERSION5) {
451 Dwarf_Unsigned i = 0;
452 Dwarf_Unsigned dfcount =
453 line_context->lc_file_name_format_count;
454
455 dwarfstring_append_printf_u(&m5,
456 " file entry format count %u\n",dfcount);
457 for ( ; i < dfcount;++i) {
458 struct Dwarf_Unsigned_Pair_s *valpair = 0;
459 const char *tname = 0;
460 const char *fname = 0;
461 int res;
462
463 valpair = line_context->lc_file_format_values +i;
464 dwarfstring_append_printf_u(&m5,
465 " format [%2u] ",i);
466 res = dwarf_get_LNCT_name((unsigned int)
467 valpair->up_first,&tname);
468 if ( res != DW_DLV_OK) {
469 tname = "<unknown type>";
470 }
471 dwarfstring_append_printf_u(&m5,
472 " type 0x%" DW_PR_XZEROS DW_PR_DUx,
473 valpair->up_first);
474 dwarfstring_append_printf_s(&m5,
475 " %-20s\n",(char *)tname);
476 res = dwarf_get_FORM_name((unsigned int)
477 valpair->up_second,&fname);
478 if ( res != DW_DLV_OK) {
479 fname = "<unknown form>";
480 }
481 dwarfstring_append_printf_u(&m5,
482 " code 0x%"
483 DW_PR_XZEROS DW_PR_DUx,
484 valpair->up_second);
485 dwarfstring_append_printf_s(&m5, " %-20s\n",
486 (char *)fname);
487 _dwarf_printf(dbg,dwarfstring_string(&m5));
488 dwarfstring_reset(&m5);
489 }
490 dwarfstring_destructor(&m5);
491 print_just_file_entry_details(dbg,line_context);
492 } else {
493 print_just_file_entry_details(dbg,line_context);
494 dwarfstring_destructor(&m5);
495 }
496 }
497
498 static void
499 print_experimental_subprograms_list(Dwarf_Debug dbg,
500 Dwarf_Line_Context line_context)
501 {
502 /* Print the subprograms list. */
503 Dwarf_Unsigned count = line_context->lc_subprogs_count;
504 Dwarf_Unsigned exu = 0;
505 Dwarf_Subprog_Entry sub = line_context->lc_subprogs;
506 dwarfstring m6;
507
508 dwarfstring_constructor_static(&m6,locallinebuf,
509 sizeof(locallinebuf));
510 dwarfstring_append_printf_u(&m6,
511 " subprograms count %" DW_PR_DUu "\n",count);
512 if (count > 0) {
513 dwarfstring_append(&m6," indx file line name\n");
514 }
515 _dwarf_printf(dbg,dwarfstring_string(&m6));
516 dwarfstring_reset(&m6);
517 for (exu = 0 ; exu < count ; exu++,sub++) {
518 dwarfstring_append_printf_u(&m6,
519 " [%2" DW_PR_DUu,exu+1);
520 dwarfstring_append_printf_u(&m6,
521 "] %4" DW_PR_DUu,sub->ds_decl_file);
522 dwarfstring_append_printf_u(&m6,
523 " %4" DW_PR_DUu ,sub->ds_decl_line);
524 dwarfstring_append_printf_s(&m6,
525 " %s\n",(char *)sub->ds_subprog_name);
526 _dwarf_printf(dbg,dwarfstring_string(&m6));
527 dwarfstring_reset(&m6);
528 }
529 dwarfstring_destructor(&m6);
530 }
531
532 static void
533 do_line_print_now(Dwarf_Debug dbg,int line_version,
534 Dwarf_Small * comp_dir,
535 Dwarf_Line_Context line_context) ;
536 static void print_experimental_counts(Dwarf_Debug dbg,
537 int line_version,
538 Dwarf_Line_Context line_context);
539
540 static int print_actuals_and_locals(Dwarf_Debug dbg,
541 Dwarf_Line_Context line_context,
542 Dwarf_Unsigned bogus_bytes_count,
543 Dwarf_Small *bogus_bytes_ptr,
544 Dwarf_Small *orig_line_ptr,
545 Dwarf_Small *line_ptr,
546 Dwarf_Small *section_start,
547 Dwarf_Small *line_ptr_actuals,
548 Dwarf_Small *line_ptr_end,
549 Dwarf_Half address_size,
550 int * err_count_out,
551 Dwarf_Error *error);
552
553 /* return DW_DLV_OK if ok. else DW_DLV_NO_ENTRY or DW_DLV_ERROR
554 If err_count_out is non-NULL, this is a special 'check'
555 call. */
556 static int
557 _dwarf_internal_printlines(Dwarf_Die die,
558 int * err_count_out,
559 int only_line_header,
560 Dwarf_Error * error)
561 {
562 /* This pointer is used to scan the portion of the .debug_line
563 section for the current cu. */
564 Dwarf_Small *line_ptr = 0;
565 Dwarf_Small *orig_line_ptr = 0;
566
567 /* Pointer to a DW_AT_stmt_list attribute in case
568 it exists in the die. */
569 Dwarf_Attribute stmt_list_attr = 0;
570
571 /* Pointer to DW_AT_comp_dir attribute in die. */
572 Dwarf_Attribute comp_dir_attr = 0;
573
574 /* Pointer to name of compilation directory. */
575 Dwarf_Small *comp_dir = NULL;
576
577 /* Offset into .debug_line specified by a DW_AT_stmt_list
578 attribute. */
579 Dwarf_Unsigned line_offset = 0;
580
581 /* These variables are used to decode leb128 numbers. Leb128_num
582 holds the decoded number, and leb128_length is its length in
583 bytes. */
584 Dwarf_Half attrform = 0;
585
586 /* In case there are weird bytes 'after' the line table
587 prologue this lets us print something. This is a gcc
588 compiler bug and we expect the bytes count to be 12. */
589 Dwarf_Small* bogus_bytes_ptr = 0;
590 Dwarf_Unsigned bogus_bytes_count = 0;
591 Dwarf_Half address_size = 0;
592 Dwarf_Unsigned fission_offset = 0;
593 unsigned line_version = 0;
594
595 /* The Dwarf_Debug this die belongs to. */
596 Dwarf_Debug dbg = 0;
597 Dwarf_CU_Context cu_context = 0;
598 Dwarf_Line_Context line_context = 0;
599 int resattr = DW_DLV_ERROR;
600 int lres = DW_DLV_ERROR;
601 int res = DW_DLV_ERROR;
602 Dwarf_Small *line_ptr_actuals = 0;
603 Dwarf_Small *line_ptr_end = 0;
604 Dwarf_Small *section_start = 0;
605
606 /* ***** BEGIN CODE ***** */
607
608 if (error != NULL) {
609 *error = NULL;
610 }
611
612 CHECK_DIE(die, DW_DLV_ERROR);
613 cu_context = die->di_cu_context;
614 dbg = cu_context->cc_dbg;
615
616 res = _dwarf_load_section(dbg, &dbg->de_debug_line,error);
617 if (res != DW_DLV_OK) {
618 return res;
619 }
620 if (!dbg->de_debug_line.dss_size) {
621 return DW_DLV_NO_ENTRY;
622 }
623
624 address_size = _dwarf_get_address_size(dbg, die);
625 resattr = dwarf_attr(die, DW_AT_stmt_list, &stmt_list_attr,
626 error);
627 if (resattr != DW_DLV_OK) {
628 return resattr;
629 }
630 /* The list of relevant FORMs is small.
631 DW_FORM_data4, DW_FORM_data8, DW_FORM_sec_offset
632 */
633 lres = dwarf_whatform(stmt_list_attr,&attrform,error);
634 if (lres != DW_DLV_OK) {
635 dwarf_dealloc_attribute(stmt_list_attr);
636 return lres;
637 }
638 if (attrform != DW_FORM_data4 && attrform != DW_FORM_data8 &&
639 attrform != DW_FORM_sec_offset ) {
640 dwarf_dealloc_attribute(stmt_list_attr);
641 _dwarf_error(dbg, error, DW_DLE_LINE_OFFSET_BAD);
642 return DW_DLV_ERROR;
643 }
644 lres = dwarf_global_formref(stmt_list_attr, &line_offset, error);
645 if (lres != DW_DLV_OK) {
646 dwarf_dealloc_attribute(stmt_list_attr);
647 return lres;
648 }
649
650 if (line_offset >= dbg->de_debug_line.dss_size) {
651 dwarf_dealloc_attribute(stmt_list_attr);
652 _dwarf_error(dbg, error, DW_DLE_LINE_OFFSET_BAD);
653 return DW_DLV_ERROR;
654 }
655 section_start = dbg->de_debug_line.dss_data;
656 {
657 Dwarf_Unsigned fission_size = 0;
658 int resfis = _dwarf_get_fission_addition_die(die,
659 DW_SECT_LINE,
660 &fission_offset,&fission_size,error);
661 if (resfis != DW_DLV_OK) {
662 dwarf_dealloc_attribute(stmt_list_attr);
663 return resfis;
664 }
665 }
666
667 orig_line_ptr = section_start + line_offset + fission_offset;
668 line_ptr = orig_line_ptr;
669 dwarf_dealloc_attribute(stmt_list_attr);
670
671 /* If die has DW_AT_comp_dir attribute, get the string
672 that names the compilation directory. */
673 resattr = dwarf_attr(die, DW_AT_comp_dir, &comp_dir_attr, error);
674 if (resattr == DW_DLV_ERROR) {
675 return resattr;
676 }
677 if (resattr == DW_DLV_OK) {
678 int cres = DW_DLV_ERROR;
679 char *cdir = 0;
680
681 cres = dwarf_formstring(comp_dir_attr, &cdir, error);
682 if (cres == DW_DLV_ERROR) {
683 dwarf_dealloc_attribute(comp_dir_attr);
684 comp_dir_attr = 0;
685 return cres;
686 }
687 if (cres == DW_DLV_OK) {
688 comp_dir = (Dwarf_Small *) cdir;
689 }
690 }
691 if (comp_dir_attr) {
692 dwarf_dealloc_attribute(comp_dir_attr);
693 comp_dir_attr = 0;
694 }
695 line_context = (Dwarf_Line_Context)
696 _dwarf_get_alloc(dbg, DW_DLA_LINE_CONTEXT, 1);
697 if (line_context == NULL) {
698 _dwarf_error(dbg, error, DW_DLE_ALLOC_FAIL);
699 return DW_DLV_ERROR;
700 }
701 {
702 Dwarf_Small *newlinep = 0;
703 int dres = _dwarf_read_line_table_header(dbg,
704 cu_context,
705 section_start,
706 line_ptr,
707 dbg->de_debug_line.dss_size,
708 &newlinep,
709 line_context,
710 &bogus_bytes_ptr,
711 &bogus_bytes_count,
712 error,
713 err_count_out);
714 if (dres == DW_DLV_ERROR) {
715 dwarf_srclines_dealloc_b(line_context);
716 return dres;
717 }
718 if (dres == DW_DLV_NO_ENTRY) {
719 dwarf_srclines_dealloc_b(line_context);
720 return dres;
721 }
722 line_ptr_end = line_context->lc_line_ptr_end;
723 line_ptr = newlinep;
724 if (line_context->lc_actuals_table_offset > 0) {
725 line_ptr_actuals = line_context->lc_line_prologue_start +
726 line_context->lc_actuals_table_offset;
727 }
728 }
729 line_version = line_context->lc_version_number;
730 line_context->lc_compilation_directory = comp_dir;
731 if (only_line_header) {
732 /* Just checking for header errors, nothing more here.*/
733 dwarf_srclines_dealloc_b(line_context);
734 return DW_DLV_OK;
735 }
736 do_line_print_now(dbg,line_version,comp_dir,line_context);
737 print_include_directory_details(dbg,line_version,line_context);
738 print_file_entry_details(dbg,line_version,line_context);
739 print_experimental_counts(dbg, line_version,line_context);
740 res = print_actuals_and_locals(dbg, line_context,
741 bogus_bytes_count,bogus_bytes_ptr,
742 orig_line_ptr,
743 line_ptr,
744 section_start,
745 line_ptr_actuals,
746 line_ptr_end,
747 address_size,
748 err_count_out,
749 error);
750 if (res != DW_DLV_OK) {
751 return res;
752 }
753 return DW_DLV_OK;
754 }
755
756 static void
757 do_line_print_now(Dwarf_Debug dbg,
758 int line_version,
759 Dwarf_Small *comp_dir,
760 Dwarf_Line_Context line_context)
761 {
762 dwarfstring m7;
763 Dwarf_Unsigned i = 0;
764
765 dwarfstring_constructor(&m7);
766 dwarfstring_append_printf_i(&m7,
767 "total line info length %ld bytes,",
768 line_context->lc_total_length);
769
770 dwarfstring_append_printf_u(&m7,
771 " line offset 0x%" DW_PR_XZEROS DW_PR_DUx,
772 line_context->lc_section_offset);
773
774 dwarfstring_append_printf_u(&m7,
775 " %" DW_PR_DUu "\n",
776 line_context->lc_section_offset);
777
778 if (line_version <= DW_LINE_VERSION5) {
779 dwarfstring_append_printf_i(&m7,
780 " line table version %d\n",
781 (int) line_context->lc_version_number);
782 } else {
783 dwarfstring_append_printf_u(&m7,
784 " line table version 0x%x\n",
785 (int) line_context->lc_version_number);
786 }
787 if (line_version == DW_LINE_VERSION5) {
788 dwarfstring_append_printf_i(&m7,
789 " address size %d\n",
790 line_context->lc_address_size);
791 dwarfstring_append_printf_i(&m7,
792 " segment selector size %d\n",
793 line_context->lc_segment_selector_size);
794 }
795 _dwarf_printf(dbg,dwarfstring_string(&m7));
796 dwarfstring_reset(&m7);
797 dwarfstring_append_printf_i(&m7,
798 " line table length field length %d\n",
799 line_context->lc_length_field_length);
800 dwarfstring_append_printf_i(&m7,
801 " prologue length %d\n",
802 line_context->lc_prologue_length);
803 dwarfstring_append_printf_s(&m7,
804 " compilation_directory %s\n",
805 comp_dir ? ((char *) comp_dir) : "");
806
807 dwarfstring_append_printf_i(&m7,
808 " min instruction length %d\n",
809 line_context->lc_minimum_instruction_length);
810 _dwarf_printf(dbg,dwarfstring_string(&m7));
811 dwarfstring_reset(&m7);
812 if (line_version == DW_LINE_VERSION5 ||
813 line_version == DW_LINE_VERSION4 ||
814 line_version == EXPERIMENTAL_LINE_TABLES_VERSION) {
815 dwarfstring_append_printf_u(&m7,
816 " maximum ops per instruction %u\n",
817 line_context->lc_maximum_ops_per_instruction);
818 _dwarf_printf(dbg,dwarfstring_string(&m7));
819 dwarfstring_reset(&m7);
820 }
821 if (line_version == EXPERIMENTAL_LINE_TABLES_VERSION) {
822 dwarfstring_append_printf_u(&m7, " actuals table offset "
823 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
824 line_context->lc_actuals_table_offset);
825 dwarfstring_append_printf_u(&m7," logicals table offset "
826 "0x%" DW_PR_XZEROS DW_PR_DUx "\n",
827 line_context->lc_logicals_table_offset);
828 _dwarf_printf(dbg,dwarfstring_string(&m7));
829 dwarfstring_reset(&m7);
830 }
831 dwarfstring_append_printf_i(&m7,
832 " default is stmt %d\n",
833 (int)line_context->lc_default_is_stmt);
834 dwarfstring_append_printf_i(&m7,
835 " line base %d\n",
836 (int)line_context->lc_line_base);
837 dwarfstring_append_printf_i(&m7,
838 " line_range %d\n",
839 (int)line_context->lc_line_range);
840 dwarfstring_append_printf_i(&m7,
841 " opcode base %d\n",
842 (int)line_context->lc_opcode_base);
843 dwarfstring_append_printf_i(&m7,
844 " standard opcode count %d\n",
845 (int)line_context->lc_std_op_count);
846 _dwarf_printf(dbg,dwarfstring_string(&m7));
847 dwarfstring_reset(&m7);
848
849 for (i = 1; i < line_context->lc_opcode_base; i++) {
850 dwarfstring_append_printf_i(&m7,
851 " opcode[%2d] length", (int) i);
852 dwarfstring_append_printf_i(&m7,
853 " %d\n",
854 (int) line_context->lc_opcode_length_table[i - 1]);
855 _dwarf_printf(dbg,dwarfstring_string(&m7));
856 dwarfstring_reset(&m7);
857 }
858 dwarfstring_destructor(&m7);
859 }
860
861 static void
862 print_experimental_counts(Dwarf_Debug dbg, int line_version,
863 Dwarf_Line_Context line_context)
864 {
865 if (line_version == EXPERIMENTAL_LINE_TABLES_VERSION) {
866 print_experimental_subprograms_list(dbg,line_context);
867 }
868 }
869
870 static int
871 print_actuals_and_locals(Dwarf_Debug dbg,
872 Dwarf_Line_Context line_context,
873 Dwarf_Unsigned bogus_bytes_count,
874 Dwarf_Small *bogus_bytes_ptr,
875 Dwarf_Small *orig_line_ptr,
876 Dwarf_Small *line_ptr,
877 Dwarf_Small *section_start,
878 Dwarf_Small *line_ptr_actuals,
879 Dwarf_Small *line_ptr_end,
880 Dwarf_Half address_size,
881 int * err_count_out,
882 Dwarf_Error *error)
883 {
884 int res = 0;
885 dwarfstring m8;
886 Dwarf_Unsigned offset = 0;
887
888 dwarfstring_constructor(&m8);
889 if (bogus_bytes_count > 0) {
890 Dwarf_Unsigned wcount = bogus_bytes_count;
891 Dwarf_Unsigned boffset = bogus_bytes_ptr - section_start;
892
893 dwarfstring_append_printf_u(&m8,
894 "*** DWARF CHECK: the line table prologue header_length "
895 " is %" DW_PR_DUu " too high, we pretend it is smaller.",
896 wcount);
897 dwarfstring_append_printf_u(&m8,
898 "Section offset: 0x%"
899 DW_PR_XZEROS DW_PR_DUx,
900 boffset);
901 dwarfstring_append_printf_u(&m8,
902 " (%" DW_PR_DUu ") ***\n",
903 boffset);
904 *err_count_out += 1;
905 }
906 offset = line_ptr - section_start;
907 dwarfstring_append_printf_u(&m8,
908 " statement prog offset in section: 0x%"
909 DW_PR_XZEROS DW_PR_DUx,
910 offset);
911 dwarfstring_append_printf_u(&m8,
912 " (%" DW_PR_DUu ")\n",
913 offset);
914 _dwarf_printf(dbg,dwarfstring_string(&m8));
915 dwarfstring_reset(&m8);
916
917 {
918 Dwarf_Bool doaddrs = FALSE;
919 Dwarf_Bool dolines = TRUE;
920
921 if (!line_ptr_actuals) {
922 /* Normal single level line table. */
923
924 Dwarf_Bool is_single_table = TRUE;
925 Dwarf_Bool is_actuals_table = FALSE;
926 print_line_header(dbg, is_single_table, is_actuals_table);
927 res = read_line_table_program(dbg,
928 line_ptr, line_ptr_end, orig_line_ptr,
929 section_start,
930 line_context,
931 address_size, doaddrs, dolines,
932 is_single_table,
933 is_actuals_table,
934 error,
935 err_count_out);
936 if (res != DW_DLV_OK) {
937 dwarfstring_destructor(&m8);
938 dwarf_srclines_dealloc_b(line_context);
939 return res;
940 }
941 } else {
942 Dwarf_Bool is_single_table = FALSE;
943 Dwarf_Bool is_actuals_table = FALSE;
944 if (line_context->lc_version_number !=
945 EXPERIMENTAL_LINE_TABLES_VERSION) {
946 dwarf_srclines_dealloc_b(line_context);
947 dwarfstring_destructor(&m8);
948 _dwarf_error(dbg, error, DW_DLE_VERSION_STAMP_ERROR);
949 return DW_DLV_ERROR;
950 }
951 /* Read Logicals */
952 print_line_header(dbg, is_single_table, is_actuals_table);
953 res = read_line_table_program(dbg,
954 line_ptr, line_ptr_actuals, orig_line_ptr,
955 section_start,
956 line_context,
957 address_size, doaddrs, dolines,
958 is_single_table,
959 is_actuals_table,
960 error,err_count_out);
961 if (res != DW_DLV_OK) {
962 dwarfstring_destructor(&m8);
963 dwarf_srclines_dealloc_b(line_context);
964 return res;
965 }
966 if (line_context->lc_actuals_table_offset > 0) {
967 is_actuals_table = TRUE;
968 /* Read Actuals */
969
970 print_line_header(dbg, is_single_table,
971 is_actuals_table);
972 res = read_line_table_program(dbg,
973 line_ptr_actuals, line_ptr_end, orig_line_ptr,
974 section_start,
975 line_context,
976 address_size, doaddrs, dolines,
977 is_single_table,
978 is_actuals_table,
979 error,
980 err_count_out);
981 if (res != DW_DLV_OK) {
982 dwarfstring_destructor(&m8);
983 dwarf_srclines_dealloc_b(line_context);
984 return res;
985 }
986 }
987 }
988 }
989 dwarfstring_destructor(&m8);
990 dwarf_srclines_dealloc_b(line_context);
991 return DW_DLV_OK;
992 }
993
994 /* This is support for dwarfdump: making it possible
995 for clients wanting line detail info on stdout
996 to get that detail without including internal libdwarf
997 header information.
998 Caller passes in compilation unit DIE.
999
1000 These *print_lines() functions print two-level tables in full
1001 even when the user is not asking for both (ie, when
1002 the caller asked for dwarf_srclines().
1003 It was an accident, but after a short reflection
1004 this seems like a good idea for -vvv. */
1005 int
1006 dwarf_print_lines(Dwarf_Die die,
1007 Dwarf_Error * error,
1008 int *error_count)
1009 {
1010 int only_line_header = 0;
1011 int res = _dwarf_internal_printlines(die,
1012 error_count,
1013 only_line_header,error);
1014 return res;
1015 }
1016
1017 /* The check is in case we are not printing full line data,
1018 this gets some of the issues noted with .debug_line,
1019 but not all. Call dwarf_print_lines() to get all issues.
1020 Intended for apps like dwarfdump.
1021 dwarf_check_lineheader_b() new 14 April 2020.
1022 */
1023 int
1024 dwarf_check_lineheader_b(Dwarf_Die die, int *err_count_out,
1025 Dwarf_Error *error)
1026 {
1027 int res = 0;
1028
1029 int only_line_header = 1;
1030 res = _dwarf_internal_printlines(die,err_count_out,
1031 only_line_header,error);
1032 return res;
1033 }