2 Copyright (C) 2000-2006 Silicon Graphics, Inc. All Rights Reserved.
3 Portions Copyright (C) 2007-2010 David Anderson. All Rights Reserved.
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of version 2.1 of the GNU Lesser General Public License
7 as published by the Free Software Foundation.
9 This program is distributed in the hope that it would be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 Further, this software is distributed without any warranty that it is
14 free of the rightful claim of any third person regarding infringement
15 or the like. Any license provided herein, whether implied or
16 otherwise, applies only to this software file. Patent licenses, if
17 any, provided herein do not apply to combinations of this program with
18 other software, or any other product whatsoever.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this program; if not, write the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
25 Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane,
26 Mountain View, CA 94043, or:
30 For further information regarding this notice, see:
32 http://oss.sgi.com/projects/GenInfo/NoticeExplan
35 /* The address of the Free Software Foundation is
36 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
37 Boston, MA 02110-1301, USA.
38 SGI has moved from the Crittenden Lane address.
45 #include "dwarf_incl.h"
48 #include "dwarf_line.h"
51 is_path_separator(Dwarf_Small s
)
56 #ifdef HAVE_WINDOWS_PATH
64 /* Return 0 if false, 1 if true.
65 If HAVE_WINDOWS_PATH is defined we
66 attempt to handle windows full paths:
67 \\something or C:cwdpath.c
70 file_name_is_full_path(Dwarf_Small
*fname
)
72 Dwarf_Small firstc
= *fname
;
73 if(is_path_separator(firstc
)) {
80 #ifdef HAVE_WINDOWS_PATH
81 if((firstc
>= 'A' && firstc
<= 'Z') ||
82 (firstc
>= 'a' && firstc
<= 'z')) {
83 Dwarf_Small secondc
= fname
[1];
93 Although source files is supposed to return the
94 source files in the compilation-unit, it does
95 not look for any in the statement program. In
96 other words, it ignores those defined using the
97 extended opcode DW_LNE_define_file.
100 dwarf_srcfiles(Dwarf_Die die
,
102 Dwarf_Signed
* srcfilecount
, Dwarf_Error
* error
)
104 /* This pointer is used to scan the portion of the .debug_line
105 section for the current cu. */
106 Dwarf_Small
*line_ptr
;
108 /* Pointer to a DW_AT_stmt_list attribute in case it exists in the
110 Dwarf_Attribute stmt_list_attr
;
112 /* Pointer to DW_AT_comp_dir attribute in die. */
113 Dwarf_Attribute comp_dir_attr
;
115 /* Pointer to name of compilation directory. */
116 Dwarf_Small
*comp_dir
= 0;
118 /* Offset into .debug_line specified by a DW_AT_stmt_list
120 Dwarf_Unsigned line_offset
= 0;
122 /* This points to a block of char *'s, each of which points to a
124 char **ret_files
= 0;
126 /* The Dwarf_Debug this die belongs to. */
129 /* Used to chain the file names. */
130 Dwarf_Chain curr_chain
= NULL
;
131 Dwarf_Chain prev_chain
= NULL
;
132 Dwarf_Chain head_chain
= NULL
;
133 Dwarf_Half attrform
= 0;
134 int resattr
= DW_DLV_ERROR
;
135 int lres
= DW_DLV_ERROR
;
136 struct Line_Table_Prefix_s line_prefix
;
138 int res
= DW_DLV_ERROR
;
140 /* ***** BEGIN CODE ***** */
145 CHECK_DIE(die
, DW_DLV_ERROR
);
146 dbg
= die
->di_cu_context
->cc_dbg
;
148 resattr
= dwarf_attr(die
, DW_AT_stmt_list
, &stmt_list_attr
, error
);
149 if (resattr
!= DW_DLV_OK
) {
153 if (dbg
->de_debug_line
.dss_index
== 0) {
154 _dwarf_error(dbg
, error
, DW_DLE_DEBUG_LINE_NULL
);
155 return (DW_DLV_ERROR
);
158 res
= _dwarf_load_section(dbg
, &dbg
->de_debug_line
,error
);
159 if (res
!= DW_DLV_OK
) {
163 lres
= dwarf_whatform(stmt_list_attr
,&attrform
,error
);
164 if (lres
!= DW_DLV_OK
) {
167 if (attrform
!= DW_FORM_data4
&& attrform
!= DW_FORM_data8
&&
168 attrform
!= DW_FORM_sec_offset
) {
169 _dwarf_error(dbg
, error
, DW_DLE_LINE_OFFSET_BAD
);
170 return (DW_DLV_ERROR
);
172 lres
= dwarf_global_formref(stmt_list_attr
, &line_offset
, error
);
173 if (lres
!= DW_DLV_OK
) {
176 if (line_offset
>= dbg
->de_debug_line
.dss_size
) {
177 _dwarf_error(dbg
, error
, DW_DLE_LINE_OFFSET_BAD
);
178 return (DW_DLV_ERROR
);
180 line_ptr
= dbg
->de_debug_line
.dss_data
+ line_offset
;
181 dwarf_dealloc(dbg
, stmt_list_attr
, DW_DLA_ATTR
);
184 If die has DW_AT_comp_dir attribute, get the string that names
185 the compilation directory. */
186 resattr
= dwarf_attr(die
, DW_AT_comp_dir
, &comp_dir_attr
, error
);
187 if (resattr
== DW_DLV_ERROR
) {
190 if (resattr
== DW_DLV_OK
) {
191 int cres
= DW_DLV_ERROR
;
194 cres
= dwarf_formstring(comp_dir_attr
, &cdir
, error
);
195 if (cres
== DW_DLV_ERROR
) {
197 } else if (cres
== DW_DLV_OK
) {
198 comp_dir
= (Dwarf_Small
*) cdir
;
201 if (resattr
== DW_DLV_OK
) {
202 dwarf_dealloc(dbg
, comp_dir_attr
, DW_DLA_ATTR
);
204 dwarf_init_line_table_prefix(&line_prefix
);
206 Dwarf_Small
*line_ptr_out
= 0;
207 int dres
= dwarf_read_line_table_prefix(dbg
,
209 dbg
->de_debug_line
.dss_size
,
215 if (dres
== DW_DLV_ERROR
) {
216 dwarf_free_line_table_prefix(&line_prefix
);
219 if (dres
== DW_DLV_NO_ENTRY
) {
220 dwarf_free_line_table_prefix(&line_prefix
);
224 line_ptr
= line_ptr_out
;
227 for (i
= 0; i
< line_prefix
.pf_files_count
; ++i
) {
228 struct Line_Table_File_Entry_s
*fe
=
229 line_prefix
.pf_line_table_file_entries
+ i
;
230 char *file_name
= (char *) fe
->lte_filename
;
233 Dwarf_Unsigned dir_index
= fe
->lte_directory_index
;
235 if (dir_index
== 0) {
236 dir_name
= (char *) comp_dir
;
239 (char *) line_prefix
.pf_include_directories
[
240 fe
->lte_directory_index
- 1];
243 /* dir_name can be NULL if there is no DW_AT_comp_dir */
244 if(dir_name
== 0 || file_name_is_full_path((unsigned char *)file_name
)) {
245 /* This is safe because dwarf_dealloc is careful to not
246 dealloc strings which are part of the raw .debug_* data.
248 full_name
= file_name
;
250 full_name
= (char *) _dwarf_get_alloc(dbg
, DW_DLA_STRING
,
251 strlen(dir_name
) + 1 +
254 if (full_name
== NULL
) {
255 dwarf_free_line_table_prefix(&line_prefix
);
256 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
257 return (DW_DLV_ERROR
);
260 /* This is not careful to avoid // in the output, Nothing
261 forces a 'canonical' name format here. Unclear if this
262 needs to be fixed. */
263 strcpy(full_name
, dir_name
);
264 strcat(full_name
, "/");
265 strcat(full_name
, file_name
);
268 (Dwarf_Chain
) _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
269 if (curr_chain
== NULL
) {
270 dwarf_free_line_table_prefix(&line_prefix
);
271 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
272 return (DW_DLV_ERROR
);
274 curr_chain
->ch_item
= full_name
;
275 if (head_chain
== NULL
)
276 head_chain
= prev_chain
= curr_chain
;
278 prev_chain
->ch_next
= curr_chain
;
279 prev_chain
= curr_chain
;
283 curr_chain
= (Dwarf_Chain
) _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
284 if (curr_chain
== NULL
) {
285 dwarf_free_line_table_prefix(&line_prefix
);
286 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
287 return (DW_DLV_ERROR
);
293 if (line_prefix
.pf_files_count
== 0) {
296 dwarf_free_line_table_prefix(&line_prefix
);
297 return (DW_DLV_NO_ENTRY
);
300 ret_files
= (char **)
301 _dwarf_get_alloc(dbg
, DW_DLA_LIST
, line_prefix
.pf_files_count
);
302 if (ret_files
== NULL
) {
303 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
304 dwarf_free_line_table_prefix(&line_prefix
);
305 return (DW_DLV_ERROR
);
308 curr_chain
= head_chain
;
309 for (i
= 0; i
< line_prefix
.pf_files_count
; i
++) {
310 *(ret_files
+ i
) = curr_chain
->ch_item
;
311 prev_chain
= curr_chain
;
312 curr_chain
= curr_chain
->ch_next
;
313 dwarf_dealloc(dbg
, prev_chain
, DW_DLA_CHAIN
);
316 *srcfiles
= ret_files
;
317 *srcfilecount
= line_prefix
.pf_files_count
;
318 dwarf_free_line_table_prefix(&line_prefix
);
324 return DW_DLV_OK if ok. else DW_DLV_NO_ENTRY or DW_DLV_ERROR
327 _dwarf_internal_srclines(Dwarf_Die die
,
328 Dwarf_Line
** linebuf
,
329 Dwarf_Signed
* count
,
331 Dwarf_Bool dolines
, Dwarf_Error
* error
)
333 /* This pointer is used to scan the portion of the .debug_line
334 section for the current cu. */
335 Dwarf_Small
*line_ptr
= 0;
337 /* This points to the last byte of the .debug_line portion for the
339 Dwarf_Small
*line_ptr_end
= 0;
341 /* Pointer to a DW_AT_stmt_list attribute in case it exists in the
343 Dwarf_Attribute stmt_list_attr
= 0;
345 /* Pointer to DW_AT_comp_dir attribute in die. */
346 Dwarf_Attribute comp_dir_attr
= 0;
348 /* Pointer to name of compilation directory. */
349 Dwarf_Small
*comp_dir
= NULL
;
351 /* Offset into .debug_line specified by a DW_AT_stmt_list
353 Dwarf_Unsigned line_offset
= 0;
355 Dwarf_File_Entry file_entries
= 0;
357 /* These are the state machine state variables. */
358 Dwarf_Addr address
= 0;
361 Dwarf_Word column
= 0;
363 /* Phony init. See below for true initialization. */
364 Dwarf_Bool is_stmt
= false;
366 Dwarf_Bool basic_block
= false;
367 Dwarf_Bool prologue_end
= false;
368 Dwarf_Bool epilogue_begin
= false;
370 Dwarf_Bool end_sequence
= false;
372 /* These pointers are used to build the list of files names by this
373 cu. cur_file_entry points to the file name being added, and
374 prev_file_entry to the previous one. */
375 Dwarf_File_Entry cur_file_entry
, prev_file_entry
;
378 Dwarf_Sword file_entry_count
= 0;
380 /* This is the current opcode read from the statement program. */
381 Dwarf_Small opcode
= 0;
383 /* Pointer to a Dwarf_Line_Context_s structure that contains the
384 context such as file names and include directories for the set
385 of lines being generated. */
386 Dwarf_Line_Context line_context
= 0;
388 /* This is a pointer to the current line being added to the line
390 Dwarf_Line curr_line
= 0;
392 /* These variables are used to decode leb128 numbers. Leb128_num
393 holds the decoded number, and leb128_length is its length in
395 Dwarf_Word leb128_num
= 0;
396 Dwarf_Word leb128_length
= 0;
397 Dwarf_Sword advance_line
= 0;
399 /* This is the operand of the latest fixed_advance_pc extended
401 Dwarf_Half fixed_advance_pc
= 0;
403 /* Counts the number of lines in the line matrix. */
404 Dwarf_Sword line_count
= 0;
406 /* This is the length of an extended opcode instr. */
407 Dwarf_Word instr_length
= 0;
408 Dwarf_Small ext_opcode
= 0;
409 struct Line_Table_Prefix_s prefix
;
411 /* Used to chain together pointers to line table entries that are
412 later used to create a block of Dwarf_Line entries. */
413 Dwarf_Chain chain_line
= NULL
;
414 Dwarf_Chain head_chain
= NULL
;
415 Dwarf_Chain curr_chain
= NULL
;
417 /* This points to a block of Dwarf_Lines, a pointer to which is
418 returned in linebuf. */
419 Dwarf_Line
*block_line
= 0;
421 /* The Dwarf_Debug this die belongs to. */
423 int resattr
= DW_DLV_ERROR
;
424 int lres
= DW_DLV_ERROR
;
425 Dwarf_Half address_size
= 0;
427 int res
= DW_DLV_ERROR
;
429 /* ***** BEGIN CODE ***** */
433 CHECK_DIE(die
, DW_DLV_ERROR
);
434 dbg
= die
->di_cu_context
->cc_dbg
;
436 res
= _dwarf_load_section(dbg
, &dbg
->de_debug_line
,error
);
437 if (res
!= DW_DLV_OK
) {
440 address_size
= _dwarf_get_address_size(dbg
, die
);
441 resattr
= dwarf_attr(die
, DW_AT_stmt_list
, &stmt_list_attr
, error
);
442 if (resattr
!= DW_DLV_OK
) {
446 lres
= dwarf_formudata(stmt_list_attr
, &line_offset
, error
);
447 if (lres
!= DW_DLV_OK
) {
451 if (line_offset
>= dbg
->de_debug_line
.dss_size
) {
452 _dwarf_error(dbg
, error
, DW_DLE_LINE_OFFSET_BAD
);
453 return (DW_DLV_ERROR
);
455 line_ptr
= dbg
->de_debug_line
.dss_data
+ line_offset
;
456 dwarf_dealloc(dbg
, stmt_list_attr
, DW_DLA_ATTR
);
458 /* If die has DW_AT_comp_dir attribute, get the string that names
459 the compilation directory. */
460 resattr
= dwarf_attr(die
, DW_AT_comp_dir
, &comp_dir_attr
, error
);
461 if (resattr
== DW_DLV_ERROR
) {
464 if (resattr
== DW_DLV_OK
) {
465 int cres
= DW_DLV_ERROR
;
468 cres
= dwarf_formstring(comp_dir_attr
, &cdir
, error
);
469 if (cres
== DW_DLV_ERROR
) {
471 } else if (cres
== DW_DLV_OK
) {
472 comp_dir
= (Dwarf_Small
*) cdir
;
475 if (resattr
== DW_DLV_OK
) {
476 dwarf_dealloc(dbg
, comp_dir_attr
, DW_DLA_ATTR
);
478 dwarf_init_line_table_prefix(&prefix
);
481 Dwarf_Small
*newlinep
= 0;
482 int res
= dwarf_read_line_table_prefix(dbg
,
484 dbg
->de_debug_line
.dss_size
,
491 if (res
== DW_DLV_ERROR
) {
492 dwarf_free_line_table_prefix(&prefix
);
495 if (res
== DW_DLV_NO_ENTRY
) {
496 dwarf_free_line_table_prefix(&prefix
);
499 line_ptr_end
= prefix
.pf_line_ptr_end
;
504 /* Set up context structure for this set of lines. */
505 line_context
= (Dwarf_Line_Context
)
506 _dwarf_get_alloc(dbg
, DW_DLA_LINE_CONTEXT
, 1);
507 if (line_context
== NULL
) {
508 dwarf_free_line_table_prefix(&prefix
);
509 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
510 return (DW_DLV_ERROR
);
513 /* Fill out a Dwarf_File_Entry list as we use that to implement the
514 define_file operation. */
515 file_entries
= prev_file_entry
= NULL
;
516 for (i
= 0; i
< prefix
.pf_files_count
; ++i
) {
517 struct Line_Table_File_Entry_s
*pfxfile
=
518 prefix
.pf_line_table_file_entries
+ i
;
520 cur_file_entry
= (Dwarf_File_Entry
)
521 _dwarf_get_alloc(dbg
, DW_DLA_FILE_ENTRY
, 1);
522 if (cur_file_entry
== NULL
) {
523 dwarf_free_line_table_prefix(&prefix
);
524 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
525 return (DW_DLV_ERROR
);
528 cur_file_entry
->fi_file_name
= pfxfile
->lte_filename
;
529 cur_file_entry
->fi_dir_index
= pfxfile
->lte_directory_index
;
530 cur_file_entry
->fi_time_last_mod
=
531 pfxfile
->lte_last_modification_time
;
533 cur_file_entry
->fi_file_length
= pfxfile
->lte_length_of_file
;
535 if (file_entries
== NULL
)
536 file_entries
= cur_file_entry
;
538 prev_file_entry
->fi_next
= cur_file_entry
;
539 prev_file_entry
= cur_file_entry
;
545 /* Initialize the one state machine variable that depends on the
547 is_stmt
= prefix
.pf_default_is_stmt
;
550 /* Start of statement program. */
551 while (line_ptr
< line_ptr_end
) {
554 opcode
= *(Dwarf_Small
*) line_ptr
;
558 /* 'type' is the output */
559 WHAT_IS_OPCODE(type
, opcode
, prefix
.pf_opcode_base
,
560 prefix
.pf_opcode_length_table
, line_ptr
,
561 prefix
.pf_std_op_count
);
563 if (type
== LOP_DISCARD
) {
565 int opcnt
= prefix
.pf_opcode_length_table
[opcode
];
567 for (oc
= 0; oc
< opcnt
; oc
++) {
569 ** Read and discard operands we don't
571 ** arbitrary choice of unsigned read.
572 ** signed read would work as well.
574 Dwarf_Unsigned utmp2
;
576 DECODE_LEB128_UWORD(line_ptr
, utmp2
);
578 } else if (type
== LOP_SPECIAL
) {
579 /* This op code is a special op in the object, no matter
580 that it might fall into the standard op range in this
581 compile. That is, these are special opcodes between
582 opcode_base and MAX_LINE_OP_CODE. (including
583 opcode_base and MAX_LINE_OP_CODE) */
585 opcode
= opcode
- prefix
.pf_opcode_base
;
586 address
= address
+ prefix
.pf_minimum_instruction_length
*
587 (opcode
/ prefix
.pf_line_range
);
589 line
+ prefix
.pf_line_base
+
590 opcode
% prefix
.pf_line_range
;
594 (Dwarf_Line
) _dwarf_get_alloc(dbg
, DW_DLA_LINE
, 1);
595 if (curr_line
== NULL
) {
596 dwarf_free_line_table_prefix(&prefix
);
597 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
598 return (DW_DLV_ERROR
);
601 curr_line
->li_address
= address
;
602 curr_line
->li_addr_line
.li_l_data
.li_file
=
604 curr_line
->li_addr_line
.li_l_data
.li_line
=
606 curr_line
->li_addr_line
.li_l_data
.li_column
=
608 curr_line
->li_addr_line
.li_l_data
.li_is_stmt
= is_stmt
;
609 curr_line
->li_addr_line
.li_l_data
.li_basic_block
=
611 curr_line
->li_addr_line
.li_l_data
.li_end_sequence
=
612 curr_line
->li_addr_line
.li_l_data
.
613 li_epilogue_begin
= epilogue_begin
;
614 curr_line
->li_addr_line
.li_l_data
.li_prologue_end
=
616 curr_line
->li_addr_line
.li_l_data
.li_isa
= isa
;
617 curr_line
->li_context
= line_context
;
620 chain_line
= (Dwarf_Chain
)
621 _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
622 if (chain_line
== NULL
) {
623 dwarf_free_line_table_prefix(&prefix
);
624 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
625 return (DW_DLV_ERROR
);
627 chain_line
->ch_item
= curr_line
;
629 if (head_chain
== NULL
)
630 head_chain
= curr_chain
= chain_line
;
632 curr_chain
->ch_next
= chain_line
;
633 curr_chain
= chain_line
;
638 } else if (type
== LOP_STANDARD
) {
645 (Dwarf_Line
) _dwarf_get_alloc(dbg
,
648 if (curr_line
== NULL
) {
649 dwarf_free_line_table_prefix(&prefix
);
650 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
651 return (DW_DLV_ERROR
);
654 curr_line
->li_address
= address
;
655 curr_line
->li_addr_line
.li_l_data
.li_file
=
657 curr_line
->li_addr_line
.li_l_data
.li_line
=
659 curr_line
->li_addr_line
.li_l_data
.li_column
=
661 curr_line
->li_addr_line
.li_l_data
.li_is_stmt
=
663 curr_line
->li_addr_line
.li_l_data
.
664 li_basic_block
= basic_block
;
665 curr_line
->li_addr_line
.li_l_data
.
666 li_end_sequence
= end_sequence
;
667 curr_line
->li_context
= line_context
;
668 curr_line
->li_addr_line
.li_l_data
.
669 li_epilogue_begin
= epilogue_begin
;
670 curr_line
->li_addr_line
.li_l_data
.
671 li_prologue_end
= prologue_end
;
672 curr_line
->li_addr_line
.li_l_data
.li_isa
= isa
;
675 chain_line
= (Dwarf_Chain
)
676 _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
677 if (chain_line
== NULL
) {
678 dwarf_free_line_table_prefix(&prefix
);
679 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
680 return (DW_DLV_ERROR
);
682 chain_line
->ch_item
= curr_line
;
683 if (head_chain
== NULL
)
684 head_chain
= curr_chain
= chain_line
;
686 curr_chain
->ch_next
= chain_line
;
687 curr_chain
= chain_line
;
692 prologue_end
= false;
693 epilogue_begin
= false;
697 case DW_LNS_advance_pc
:{
698 Dwarf_Unsigned utmp2
;
700 DECODE_LEB128_UWORD(line_ptr
, utmp2
);
701 leb128_num
= (Dwarf_Word
) utmp2
;
704 prefix
.pf_minimum_instruction_length
*
709 case DW_LNS_advance_line
:{
712 DECODE_LEB128_SWORD(line_ptr
, stmp
);
713 advance_line
= (Dwarf_Sword
) stmp
;
714 line
= line
+ advance_line
;
718 case DW_LNS_set_file
:{
719 Dwarf_Unsigned utmp2
;
721 DECODE_LEB128_UWORD(line_ptr
, utmp2
);
722 file
= (Dwarf_Word
) utmp2
;
726 case DW_LNS_set_column
:{
727 Dwarf_Unsigned utmp2
;
729 DECODE_LEB128_UWORD(line_ptr
, utmp2
);
730 column
= (Dwarf_Word
) utmp2
;
734 case DW_LNS_negate_stmt
:{
740 case DW_LNS_set_basic_block
:{
746 case DW_LNS_const_add_pc
:{
747 opcode
= MAX_LINE_OP_CODE
- prefix
.pf_opcode_base
;
749 prefix
.pf_minimum_instruction_length
* (opcode
/
755 case DW_LNS_fixed_advance_pc
:{
757 READ_UNALIGNED(dbg
, fixed_advance_pc
, Dwarf_Half
,
758 line_ptr
, sizeof(Dwarf_Half
));
759 line_ptr
+= sizeof(Dwarf_Half
);
760 address
= address
+ fixed_advance_pc
;
765 case DW_LNS_set_prologue_end
:{
773 case DW_LNS_set_epilogue_begin
:{
774 epilogue_begin
= true;
779 case DW_LNS_set_isa
:{
780 Dwarf_Unsigned utmp2
;
782 DECODE_LEB128_UWORD(line_ptr
, utmp2
);
785 /* The value of the isa did not fit in our
786 local so we record it wrong. declare an
788 dwarf_free_line_table_prefix(&prefix
);
790 _dwarf_error(dbg
, error
,
791 DW_DLE_LINE_NUM_OPERANDS_BAD
);
792 return (DW_DLV_ERROR
);
798 } else if (type
== LOP_EXTENDED
) {
799 Dwarf_Unsigned utmp3
;
801 DECODE_LEB128_UWORD(line_ptr
, utmp3
);
802 instr_length
= (Dwarf_Word
) utmp3
;
803 /* Dwarf_Small is a ubyte and the extended opcode is a
804 ubyte, though not stated as clearly in the 2.0.0 spec as
806 ext_opcode
= *(Dwarf_Small
*) line_ptr
;
808 switch (ext_opcode
) {
810 case DW_LNE_end_sequence
:{
814 curr_line
= (Dwarf_Line
)
815 _dwarf_get_alloc(dbg
, DW_DLA_LINE
, 1);
816 if (curr_line
== NULL
) {
817 dwarf_free_line_table_prefix(&prefix
);
818 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
819 return (DW_DLV_ERROR
);
822 curr_line
->li_address
= address
;
823 curr_line
->li_addr_line
.li_l_data
.li_file
=
825 curr_line
->li_addr_line
.li_l_data
.li_line
=
827 curr_line
->li_addr_line
.li_l_data
.li_column
=
829 curr_line
->li_addr_line
.li_l_data
.li_is_stmt
=
830 prefix
.pf_default_is_stmt
;
831 curr_line
->li_addr_line
.li_l_data
.
832 li_basic_block
= basic_block
;
833 curr_line
->li_addr_line
.li_l_data
.
834 li_end_sequence
= end_sequence
;
835 curr_line
->li_context
= line_context
;
836 curr_line
->li_addr_line
.li_l_data
.
837 li_epilogue_begin
= epilogue_begin
;
838 curr_line
->li_addr_line
.li_l_data
.
839 li_prologue_end
= prologue_end
;
840 curr_line
->li_addr_line
.li_l_data
.li_isa
= isa
;
843 chain_line
= (Dwarf_Chain
)
844 _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
845 if (chain_line
== NULL
) {
846 dwarf_free_line_table_prefix(&prefix
);
847 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
848 return (DW_DLV_ERROR
);
850 chain_line
->ch_item
= curr_line
;
852 if (head_chain
== NULL
)
853 head_chain
= curr_chain
= chain_line
;
855 curr_chain
->ch_next
= chain_line
;
856 curr_chain
= chain_line
;
864 is_stmt
= prefix
.pf_default_is_stmt
;
866 end_sequence
= false;
867 prologue_end
= false;
868 epilogue_begin
= false;
874 case DW_LNE_set_address
:{
876 READ_UNALIGNED(dbg
, address
, Dwarf_Addr
,
877 line_ptr
, address_size
);
880 (Dwarf_Line
) _dwarf_get_alloc(dbg
,
883 if (curr_line
== NULL
) {
884 dwarf_free_line_table_prefix(&prefix
);
885 _dwarf_error(dbg
, error
,
887 return (DW_DLV_ERROR
);
890 curr_line
->li_address
= address
;
891 curr_line
->li_addr_line
.li_offset
=
892 line_ptr
- dbg
->de_debug_line
.dss_data
;
896 chain_line
= (Dwarf_Chain
)
897 _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
898 if (chain_line
== NULL
) {
899 dwarf_free_line_table_prefix(&prefix
);
900 _dwarf_error(dbg
, error
,
902 return (DW_DLV_ERROR
);
904 chain_line
->ch_item
= curr_line
;
906 if (head_chain
== NULL
)
907 head_chain
= curr_chain
= chain_line
;
909 curr_chain
->ch_next
= chain_line
;
910 curr_chain
= chain_line
;
914 line_ptr
+= address_size
;
920 case DW_LNE_define_file
:{
923 cur_file_entry
= (Dwarf_File_Entry
)
924 _dwarf_get_alloc(dbg
, DW_DLA_FILE_ENTRY
, 1);
925 if (cur_file_entry
== NULL
) {
926 dwarf_free_line_table_prefix(&prefix
);
927 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
928 return (DW_DLV_ERROR
);
931 cur_file_entry
->fi_file_name
=
932 (Dwarf_Small
*) line_ptr
;
934 line_ptr
+ strlen((char *) line_ptr
) + 1;
936 cur_file_entry
->fi_dir_index
= (Dwarf_Sword
)
937 _dwarf_decode_u_leb128(line_ptr
,
939 line_ptr
= line_ptr
+ leb128_length
;
941 cur_file_entry
->fi_time_last_mod
=
942 _dwarf_decode_u_leb128(line_ptr
,
944 line_ptr
= line_ptr
+ leb128_length
;
946 cur_file_entry
->fi_file_length
=
947 _dwarf_decode_u_leb128(line_ptr
,
949 line_ptr
= line_ptr
+ leb128_length
;
951 if (file_entries
== NULL
)
952 file_entries
= cur_file_entry
;
954 prev_file_entry
->fi_next
= cur_file_entry
;
955 prev_file_entry
= cur_file_entry
;
963 /* This is an extended op code we do not know about,
964 other than we know now many bytes it is
965 and the op code and the bytes of operand. */
966 Dwarf_Unsigned remaining_bytes
= instr_length
-1;
967 if(instr_length
< 1 || remaining_bytes
> DW_LNE_LEN_MAX
) {
968 dwarf_free_line_table_prefix(&prefix
);
969 _dwarf_error(dbg
, error
,
970 DW_DLE_LINE_EXT_OPCODE_BAD
);
971 return (DW_DLV_ERROR
);
973 line_ptr
+= remaining_bytes
;
981 block_line
= (Dwarf_Line
*)
982 _dwarf_get_alloc(dbg
, DW_DLA_LIST
, line_count
);
983 if (block_line
== NULL
) {
984 dwarf_free_line_table_prefix(&prefix
);
985 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
986 return (DW_DLV_ERROR
);
989 curr_chain
= head_chain
;
990 for (i
= 0; i
< line_count
; i
++) {
991 *(block_line
+ i
) = curr_chain
->ch_item
;
992 head_chain
= curr_chain
;
993 curr_chain
= curr_chain
->ch_next
;
994 dwarf_dealloc(dbg
, head_chain
, DW_DLA_CHAIN
);
997 line_context
->lc_file_entries
= file_entries
;
998 line_context
->lc_file_entry_count
= file_entry_count
;
999 line_context
->lc_include_directories_count
=
1000 prefix
.pf_include_directories_count
;
1001 if (prefix
.pf_include_directories_count
> 0) {
1002 /* This gets a pointer to the *first* include dir. The others
1003 follow directly with the standard DWARF2/3 NUL byte
1004 following the last. */
1005 line_context
->lc_include_directories
=
1006 prefix
.pf_include_directories
[0];
1009 line_context
->lc_line_count
= line_count
;
1010 line_context
->lc_compilation_directory
= comp_dir
;
1011 line_context
->lc_version_number
= prefix
.pf_version
;
1012 line_context
->lc_dbg
= dbg
;
1013 *count
= line_count
;
1015 *linebuf
= block_line
;
1016 dwarf_free_line_table_prefix(&prefix
);
1021 dwarf_srclines(Dwarf_Die die
,
1022 Dwarf_Line
** linebuf
,
1023 Dwarf_Signed
* linecount
, Dwarf_Error
* error
)
1025 Dwarf_Signed count
= 0;
1026 int res
= _dwarf_internal_srclines(die
, linebuf
, &count
,
1027 /* addrlist= */ false,
1028 /* linelist= */ true, error
);
1029 if (res
!= DW_DLV_OK
) {
1038 /* Every line table entry (except DW_DLE_end_sequence,
1039 which is returned using dwarf_lineendsequence())
1040 potentially has the begin-statement
1041 flag marked 'on'. This returns thru *return_bool,
1042 the begin-statement flag.
1046 dwarf_linebeginstatement(Dwarf_Line line
,
1047 Dwarf_Bool
* return_bool
, Dwarf_Error
* error
)
1049 if (line
== NULL
|| return_bool
== 0) {
1050 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1051 return (DW_DLV_ERROR
);
1054 *return_bool
= (line
->li_addr_line
.li_l_data
.li_is_stmt
);
1058 /* At the end of any contiguous line-table there may be
1059 a DW_LNE_end_sequence operator.
1060 This returns non-zero thru *return_bool
1061 if and only if this 'line' entry was a DW_LNE_end_sequence.
1063 Within a compilation unit or function there may be multiple
1064 line tables, each ending with a DW_LNE_end_sequence.
1065 Each table describes a contiguous region.
1066 Because compilers may split function code up in arbitrary ways
1067 compilers may need to emit multiple contigous regions (ie
1068 line tables) for a single function.
1069 See the DWARF3 spec section 6.2.
1072 dwarf_lineendsequence(Dwarf_Line line
,
1073 Dwarf_Bool
* return_bool
, Dwarf_Error
* error
)
1076 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1077 return (DW_DLV_ERROR
);
1080 *return_bool
= (line
->li_addr_line
.li_l_data
.li_end_sequence
);
1085 /* Each 'line' entry has a line-number.
1086 If the entry is a DW_LNE_end_sequence the line-number is
1087 meaningless (see dwarf_lineendsequence(), just above).
1090 dwarf_lineno(Dwarf_Line line
,
1091 Dwarf_Unsigned
* ret_lineno
, Dwarf_Error
* error
)
1093 if (line
== NULL
|| ret_lineno
== 0) {
1094 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1095 return (DW_DLV_ERROR
);
1098 *ret_lineno
= (line
->li_addr_line
.li_l_data
.li_line
);
1102 /* Each 'line' entry has a file-number, and index into the file table.
1103 If the entry is a DW_LNE_end_sequence the index is
1104 meaningless (see dwarf_lineendsequence(), just above).
1105 The file number returned is an index into the file table
1106 produced by dwarf_srcfiles(), but care is required: the
1107 li_file begins with 1 for real files, so that the li_file returned here
1108 is 1 greater than its index into the dwarf_srcfiles() output array.
1109 And entries from DW_LNE_define_file don't appear in
1110 the dwarf_srcfiles() output so file indexes from here may exceed
1111 the size of the dwarf_srcfiles() output array size.
1114 dwarf_line_srcfileno(Dwarf_Line line
,
1115 Dwarf_Unsigned
* ret_fileno
, Dwarf_Error
* error
)
1117 if (line
== NULL
|| ret_fileno
== 0) {
1118 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1119 return (DW_DLV_ERROR
);
1121 /* li_file must be <= line->li_context->lc_file_entry_count else it
1122 is trash. li_file 0 means not attributable to any source file
1123 per dwarf2/3 spec. */
1125 *ret_fileno
= (line
->li_addr_line
.li_l_data
.li_file
);
1130 /* Each 'line' entry has a line-address.
1131 If the entry is a DW_LNE_end_sequence the adddress
1132 is one-beyond the last address this contigous region
1133 covers, so the address is not inside the region,
1134 but is just outside it.
1137 dwarf_lineaddr(Dwarf_Line line
,
1138 Dwarf_Addr
* ret_lineaddr
, Dwarf_Error
* error
)
1140 if (line
== NULL
|| ret_lineaddr
== 0) {
1141 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1142 return (DW_DLV_ERROR
);
1145 *ret_lineaddr
= (line
->li_address
);
1150 /* Each 'line' entry has a column-within-line (offset
1151 within the line) where the
1153 If the entry is a DW_LNE_end_sequence the line-number is
1154 meaningless (see dwarf_lineendsequence(), just above).
1155 Lines of text begin at column 1. The value 0
1156 means the line begins at the left edge of the line.
1157 (See the DWARF3 spec, section 6.2.2).
1160 dwarf_lineoff(Dwarf_Line line
,
1161 Dwarf_Signed
* ret_lineoff
, Dwarf_Error
* error
)
1163 if (line
== NULL
|| ret_lineoff
== 0) {
1164 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1165 return (DW_DLV_ERROR
);
1169 (line
->li_addr_line
.li_l_data
.li_column
==
1170 0 ? -1 : line
->li_addr_line
.li_l_data
.li_column
);
1176 dwarf_linesrc(Dwarf_Line line
, char **ret_linesrc
, Dwarf_Error
* error
)
1179 Dwarf_File_Entry file_entry
;
1180 Dwarf_Small
*name_buffer
= 0;
1181 Dwarf_Small
*include_directories
= 0;
1182 Dwarf_Small include_direc_full_path
= 0;
1183 Dwarf_Small file_name_full_path
= 0;
1184 Dwarf_Debug dbg
= 0;
1185 unsigned int comp_dir_len
= 0;
1188 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1189 return (DW_DLV_ERROR
);
1192 if (line
->li_context
== NULL
) {
1193 _dwarf_error(NULL
, error
, DW_DLE_LINE_CONTEXT_NULL
);
1194 return (DW_DLV_ERROR
);
1196 dbg
= line
->li_context
->lc_dbg
;
1198 if (line
->li_addr_line
.li_l_data
.li_file
>
1199 line
->li_context
->lc_file_entry_count
) {
1200 _dwarf_error(dbg
, error
, DW_DLE_LINE_FILE_NUM_BAD
);
1201 return (DW_DLV_ERROR
);
1204 if (line
->li_addr_line
.li_l_data
.li_file
== 0) {
1205 /* No file name known: see dwarf2/3 spec. */
1206 _dwarf_error(dbg
, error
, DW_DLE_NO_FILE_NAME
);
1207 return (DW_DLV_ERROR
);
1209 file_entry
= line
->li_context
->lc_file_entries
;
1210 /* ASSERT: li_file > 0, dwarf correctness issue, see line table
1211 definition of dwarf2/3 spec. */
1212 /* Example: if li_file is 2 and lc_file_entry_count is 3,
1213 file_entry is file 3 (1 based), aka 2( 0 based) file_entry->next
1214 is file 2 (1 based), aka 1( 0 based) file_entry->next->next is
1215 file 1 (1 based), aka 0( 0 based) file_entry->next->next->next
1218 and this loop finds the file_entry we need (2 (1 based) in this
1219 case). Because lc_file_entries are in reverse order and
1220 effectively zero based as a count whereas li_file is 1 based. */
1221 for (i
= line
->li_addr_line
.li_l_data
.li_file
- 1; i
> 0; i
--)
1222 file_entry
= file_entry
->fi_next
;
1224 if (file_entry
->fi_file_name
== NULL
) {
1225 _dwarf_error(dbg
, error
, DW_DLE_NO_FILE_NAME
);
1226 return (DW_DLV_ERROR
);
1229 file_name_full_path
= file_name_is_full_path(file_entry
->fi_file_name
);
1230 if (file_name_full_path
) {
1231 *ret_linesrc
= ((char *) file_entry
->fi_file_name
);
1235 if (file_entry
->fi_dir_index
== 0) {
1237 /* dir_index of 0 means that the compilation was in the
1238 'current directory of compilation' */
1239 if (line
->li_context
->lc_compilation_directory
== NULL
) {
1240 /* we don't actually *have* a current directory of
1241 compilation: DW_AT_comp_dir was not present Rather than
1242 emitting DW_DLE_NO_COMP_DIR lets just make an empty name
1243 here. In other words, do the best we can with what we do
1244 have instead of reporting an error. _dwarf_error(dbg,
1245 error, DW_DLE_NO_COMP_DIR); return(DW_DLV_ERROR); */
1248 comp_dir_len
= strlen((char *)
1250 lc_compilation_directory
));
1254 _dwarf_get_alloc(line
->li_context
->lc_dbg
, DW_DLA_STRING
,
1256 strlen((char *) file_entry
->fi_file_name
) +
1258 if (name_buffer
== NULL
) {
1259 _dwarf_error(line
->li_context
->lc_dbg
, error
,
1261 return (DW_DLV_ERROR
);
1264 if (comp_dir_len
> 0) {
1265 /* if comp_dir_len is 0 we do not want to put a / in front
1266 of the fi_file_name as we just don't know anything. */
1267 strcpy((char *) name_buffer
,
1268 (char *) (line
->li_context
->
1269 lc_compilation_directory
));
1270 strcat((char *) name_buffer
, "/");
1272 strcat((char *) name_buffer
, (char *) file_entry
->fi_file_name
);
1273 *ret_linesrc
= ((char *) name_buffer
);
1277 if (file_entry
->fi_dir_index
>
1278 line
->li_context
->lc_include_directories_count
) {
1279 _dwarf_error(dbg
, error
, DW_DLE_INCL_DIR_NUM_BAD
);
1280 return (DW_DLV_ERROR
);
1283 include_directories
= line
->li_context
->lc_include_directories
;
1284 for (i
= file_entry
->fi_dir_index
- 1; i
> 0; i
--)
1285 include_directories
+= strlen((char *) include_directories
) + 1;
1287 if (line
->li_context
->lc_compilation_directory
) {
1288 comp_dir_len
= strlen((char *)
1289 (line
->li_context
->lc_compilation_directory
));
1291 /* No DW_AT_comp_dir present. Do the best we can without it. */
1295 include_direc_full_path
= file_name_is_full_path(include_directories
);
1296 name_buffer
= _dwarf_get_alloc(dbg
, DW_DLA_STRING
,
1297 (include_direc_full_path
? 0 : comp_dir_len
+ 1) +
1298 strlen((char *)include_directories
) + 1 +
1299 strlen((char *)file_entry
->fi_file_name
) + 1);
1300 if (name_buffer
== NULL
) {
1301 _dwarf_error(dbg
, error
, DW_DLE_ALLOC_FAIL
);
1302 return (DW_DLV_ERROR
);
1305 if (!include_direc_full_path
) {
1306 if (comp_dir_len
> 0) {
1307 strcpy((char *)name_buffer
,
1308 (char *)line
->li_context
->lc_compilation_directory
);
1309 /* Who provides the / needed after the compilation
1311 if (!is_path_separator(name_buffer
[comp_dir_len
- 1])) {
1312 /* Here we provide the / separator. It
1313 should work ok for Windows */
1314 /* Overwrite previous nul terminator with needed / */
1315 name_buffer
[comp_dir_len
] = '/';
1316 name_buffer
[comp_dir_len
+ 1] = 0;
1320 strcpy((char *) name_buffer
, "");
1322 strcat((char *) name_buffer
, (char *) include_directories
);
1323 strcat((char *) name_buffer
, "/");
1324 strcat((char *) name_buffer
, (char *) file_entry
->fi_file_name
);
1325 *ret_linesrc
= ((char *) name_buffer
);
1329 /* Every line table entry potentially has the basic-block-start
1330 flag marked 'on'. This returns thru *return_bool,
1331 the basic-block-start flag.
1334 dwarf_lineblock(Dwarf_Line line
,
1335 Dwarf_Bool
* return_bool
, Dwarf_Error
* error
)
1338 _dwarf_error(NULL
, error
, DW_DLE_DWARF_LINE_NULL
);
1339 return (DW_DLV_ERROR
);
1341 *return_bool
= (line
->li_addr_line
.li_l_data
.li_basic_block
);
1346 #if 0 /* Ignore this. This needs major
1349 This routine works by looking for exact matches between
1350 the current line address and pc, and crossovers from
1351 from less than pc value to greater than. At each line
1352 that satisfies the above, it records a pointer to the
1353 line, and the difference between the address and pc.
1354 It then scans these pointers and picks out those with
1355 the smallest difference between pc and address.
1358 dwarf_pclines(Dwarf_Debug dbg
,
1360 Dwarf_Line
** linebuf
,
1362 Dwarf_Signed
* linecount
, Dwarf_Error
* error
)
1365 Scans the line matrix for the current cu to which a pointer
1368 Dwarf_Line prev_line
;
1371 These flags are for efficiency reasons. Check_line is true
1372 initially, but set false when the address of the current line is
1373 greater than pc. It is set true only when the address of the
1374 current line falls below pc. This assumes that addresses within
1375 the same segment increase, and we are only interested in the
1376 switch from a less than pc address to a greater than. First_line
1377 is set true initially, but set false after the first line is
1378 scanned. This is to prevent looking at the address of previous
1379 line when slide is DW_DLS_BACKWARD, and the first line is being
1381 Dwarf_Bool check_line
, first_line
;
1384 Diff tracks the smallest difference a line address and the input
1386 Dwarf_Signed diff
, i
;
1389 For the slide = DW_DLS_BACKWARD case, pc_less is the value of
1390 the address of the line immediately preceding the first line
1391 that has value greater than pc. For the slide = DW_DLS_FORWARD
1392 case, pc_more is the values of address for the first line that
1393 is greater than pc. Diff is the difference between either of the
1394 these values and pc. */
1395 Dwarf_Addr pc_less
, pc_more
;
1398 Pc_line_buf points to a chain of pointers to lines of which
1399 those with a diff equal to the smallest difference will be
1401 Dwarf_Line
*pc_line_buf
, *pc_line
;
1404 Chain_count counts the number of lines in the above chain for
1405 which the diff is equal to the smallest difference This is the
1406 number returned by this routine. */
1407 Dwarf_Signed chain_count
;
1413 diff
= MAX_LINE_DIFF
;
1415 for (i
= 0; i
< dbg
->de_cu_line_count
; i
++) {
1417 line
= *(dbg
->de_cu_line_ptr
+ i
);
1418 prev_line
= first_line
? NULL
: *(dbg
->de_cu_line_ptr
+ i
- 1);
1420 if (line
->li_address
== pc
) {
1421 chain_ptr
= (struct chain
*)
1422 _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
1423 if (chain_ptr
== NULL
) {
1424 _dwarf_error(NULL
, error
, DW_DLE_ALLOC_FAIL
);
1425 return (DW_DLV_ERROR
);
1428 chain_ptr
->line
= line
;
1429 chain_ptr
->diff
= diff
= 0;
1430 chain_ptr
->next
= chain_head
;
1431 chain_head
= chain_ptr
;
1434 Look for crossover from less than pc address to greater
1436 if (check_line
&& line
->li_address
> pc
&&
1437 (first_line
? 0 : prev_line
->li_address
) < pc
)
1439 if (slide
== DW_DLS_BACKWARD
&& !first_line
) {
1440 pc_less
= prev_line
->li_address
;
1441 if (pc
- pc_less
<= diff
) {
1442 chain_ptr
= (struct chain
*)
1443 _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
1444 if (chain_ptr
== NULL
) {
1445 _dwarf_error(NULL
, error
, DW_DLE_ALLOC_FAIL
);
1446 return (DW_DLV_ERROR
);
1449 chain_ptr
->line
= prev_line
;
1450 chain_ptr
->diff
= diff
= pc
- pc_less
;
1451 chain_ptr
->next
= chain_head
;
1452 chain_head
= chain_ptr
;
1455 } else if (slide
== DW_DLS_FORWARD
) {
1456 pc_more
= line
->li_address
;
1457 if (pc_more
- pc
<= diff
) {
1458 chain_ptr
= (struct chain
*)
1459 _dwarf_get_alloc(dbg
, DW_DLA_CHAIN
, 1);
1460 if (chain_ptr
== NULL
) {
1461 _dwarf_error(NULL
, error
, DW_DLE_ALLOC_FAIL
);
1462 return (DW_DLV_ERROR
);
1465 chain_ptr
->line
= line
;
1466 chain_ptr
->diff
= diff
= pc_more
- pc
;
1467 chain_ptr
->next
= chain_head
;
1468 chain_head
= chain_ptr
;
1472 /* Check addresses only when they go */
1474 if (line
->li_address
< pc
)
1481 for (chain_ptr
= chain_head
; chain_ptr
!= NULL
;
1482 chain_ptr
= chain_ptr
->next
)
1483 if (chain_ptr
->diff
== diff
)
1486 pc_line_buf
= pc_line
= (Dwarf_Line
)
1487 _dwarf_get_alloc(dbg
, DW_DLA_LIST
, chain_count
);
1488 for (chain_ptr
= chain_head
; chain_ptr
!= NULL
;
1489 chain_ptr
= chain_ptr
->next
)
1490 if (chain_ptr
->diff
== diff
) {
1491 *pc_line
= chain_ptr
->line
;
1495 for (chain_ptr
= chain_head
; chain_ptr
!= NULL
;) {
1496 chain_head
= chain_ptr
;
1497 chain_ptr
= chain_ptr
->next
;
1498 dwarf_dealloc(dbg
, chain_head
, DW_DLA_CHAIN
);
1501 *linebuf
= pc_line_buf
;
1502 return (chain_count
);
1509 It's impossible for callers of dwarf_srclines() to get to and
1510 free all the resources (in particular, the li_context and its
1512 So this function, new July 2005, does it.
1516 dwarf_srclines_dealloc(Dwarf_Debug dbg
, Dwarf_Line
* linebuf
,
1521 struct Dwarf_Line_Context_s
*context
= 0;
1524 /* All these entries share a single context */
1525 context
= linebuf
[0]->li_context
;
1527 for (i
= 0; i
< count
; ++i
) {
1528 dwarf_dealloc(dbg
, linebuf
[i
], DW_DLA_LINE
);
1530 dwarf_dealloc(dbg
, linebuf
, DW_DLA_LIST
);
1533 Dwarf_File_Entry fe
= context
->lc_file_entries
;
1536 Dwarf_File_Entry fenext
= fe
->fi_next
;
1538 dwarf_dealloc(dbg
, fe
, DW_DLA_FILE_ENTRY
);
1541 dwarf_dealloc(dbg
, context
, DW_DLA_LINE_CONTEXT
);
1547 /* Operand counts per standard operand.
1548 The initial zero is for DW_LNS_copy.
1549 This is an economical way to verify we understand the table
1550 of standard-opcode-lengths in the line table prologue. */
1551 #define STANDARD_OPERAND_COUNT_DWARF2 9
1552 #define STANDARD_OPERAND_COUNT_DWARF3 12
1553 static unsigned char
1554 dwarf_standard_opcode_operand_count
[STANDARD_OPERAND_COUNT_DWARF3
] = {
1560 /* Following are new for DWARF3. */
1564 /* We have a normal standard opcode base, but
1565 an arm compiler emitted a non-standard table!
1566 This could lead to problems...
1567 ARM C/C++ Compiler, RVCT4.0 [Build 4
1568 00] seems to get the table wrong . */
1569 static unsigned char
1570 dwarf_arm_standard_opcode_operand_count
[STANDARD_OPERAND_COUNT_DWARF3
] = {
1575 0, /* <<< --- this is wrong */
1576 /* Following are new for DWARF3. */
1581 print_header_issue(Dwarf_Debug dbg
,
1583 Dwarf_Small
*data_start
,
1588 printf("*** DWARF CHECK: "
1589 "line table header: %s",
1591 if( data_start
>= dbg
->de_debug_line
.dss_data
&&
1592 (data_start
< (dbg
->de_debug_line
.dss_data
+
1593 dbg
->de_debug_line
.dss_size
))) {
1594 Dwarf_Unsigned off
= data_start
- dbg
->de_debug_line
.dss_data
;
1595 printf(" at .debug_line section offset 0x%" DW_PR_DUx
1596 " ( %" DW_PR_DUu
" ) ",
1599 printf(" (unknown section location) ");
1602 *err_count_out
+= 1;
1607 /* Common line table prefix reading code.
1608 Returns DW_DLV_OK, DW_DLV_ERROR.
1609 DW_DLV_NO_ENTRY cannot be returned, but callers should
1610 assume it is possible.
1612 The prefix_out area must be initialized properly before calling this.
1614 Has the side effect of allocating arrays which
1615 must be freed (see the Line_Table_Prefix_s struct which
1616 holds the pointers to space we allocate here).
1618 bogus_bytes_ptr and bogus_bytes are output values which
1619 let a print-program notify the user of some surprising bytes
1620 after a line table header and before the line table instructions.
1621 These can be ignored unless one is printing.
1622 And are ignored if NULL passed as the pointer.
1625 /* err_count_out may be NULL, in which case we
1626 make no attempt to count checking-type errors.
1627 Checking-type errors do not stop us, we just report them.
1630 dwarf_read_line_table_prefix(Dwarf_Debug dbg
,
1631 Dwarf_Small
* data_start
,
1632 Dwarf_Unsigned data_length
,
1633 Dwarf_Small
** updated_data_start_out
,
1634 struct Line_Table_Prefix_s
*prefix_out
,
1635 Dwarf_Small
** bogus_bytes_ptr
,
1636 Dwarf_Unsigned
*bogus_bytes
,
1640 Dwarf_Small
*line_ptr
= data_start
;
1641 Dwarf_Unsigned total_length
= 0;
1642 int local_length_size
= 0;
1643 int local_extension_size
= 0;
1644 Dwarf_Unsigned prologue_length
= 0;
1645 Dwarf_Half version
= 0;
1646 Dwarf_Unsigned directories_count
= 0;
1647 Dwarf_Unsigned directories_malloc
= 0;
1648 Dwarf_Unsigned files_count
= 0;
1649 Dwarf_Unsigned files_malloc
= 0;
1650 Dwarf_Small
*line_ptr_end
= 0;
1651 Dwarf_Small
*lp_begin
= 0;
1652 if(bogus_bytes_ptr
) *bogus_bytes_ptr
= 0;
1653 if(bogus_bytes
) *bogus_bytes
= 0;
1655 prefix_out
->pf_line_ptr_start
= line_ptr
;
1656 /* READ_AREA_LENGTH updates line_ptr for consumed bytes */
1657 READ_AREA_LENGTH(dbg
, total_length
, Dwarf_Unsigned
,
1658 line_ptr
, local_length_size
, local_extension_size
);
1661 line_ptr_end
= line_ptr
+ total_length
;
1662 prefix_out
->pf_line_ptr_end
= line_ptr_end
;
1663 prefix_out
->pf_length_field_length
= local_length_size
+
1664 local_extension_size
;
1665 /* ASSERT: prefix_out->pf_length_field_length == line_ptr
1666 -prefix_out->pf_line_ptr_start; */
1667 if (line_ptr_end
> dbg
->de_debug_line
.dss_data
+
1668 dbg
->de_debug_line
.dss_size
) {
1669 _dwarf_error(dbg
, err
, DW_DLE_DEBUG_LINE_LENGTH_BAD
);
1670 return (DW_DLV_ERROR
);
1672 if (line_ptr_end
> data_start
+ data_length
) {
1673 _dwarf_error(dbg
, err
, DW_DLE_DEBUG_LINE_LENGTH_BAD
);
1674 return (DW_DLV_ERROR
);
1676 prefix_out
->pf_total_length
= total_length
;
1678 READ_UNALIGNED(dbg
, version
, Dwarf_Half
,
1679 line_ptr
, sizeof(Dwarf_Half
));
1680 prefix_out
->pf_version
= version
;
1681 line_ptr
+= sizeof(Dwarf_Half
);
1682 if (version
!= CURRENT_VERSION_STAMP
&&
1683 version
!= CURRENT_VERSION_STAMP3
) {
1684 _dwarf_error(dbg
, err
, DW_DLE_VERSION_STAMP_ERROR
);
1685 return (DW_DLV_ERROR
);
1688 READ_UNALIGNED(dbg
, prologue_length
, Dwarf_Unsigned
,
1689 line_ptr
, local_length_size
);
1690 prefix_out
->pf_prologue_length
= prologue_length
;
1691 line_ptr
+= local_length_size
;
1692 prefix_out
->pf_line_prologue_start
= line_ptr
;
1694 prefix_out
->pf_minimum_instruction_length
=
1695 *(unsigned char *) line_ptr
;
1696 line_ptr
= line_ptr
+ sizeof(Dwarf_Small
);
1698 prefix_out
->pf_default_is_stmt
= *(unsigned char *) line_ptr
;
1699 line_ptr
= line_ptr
+ sizeof(Dwarf_Small
);
1701 prefix_out
->pf_line_base
= *(signed char *) line_ptr
;
1702 line_ptr
= line_ptr
+ sizeof(Dwarf_Sbyte
);
1704 prefix_out
->pf_line_range
= *(unsigned char *) line_ptr
;
1705 line_ptr
= line_ptr
+ sizeof(Dwarf_Small
);
1707 prefix_out
->pf_opcode_base
= *(unsigned char *) line_ptr
;
1708 line_ptr
= line_ptr
+ sizeof(Dwarf_Small
);
1710 /* Set up the array of standard opcode lengths. */
1711 /* We think this works ok even for cross-endian processing of
1712 objects. It might be wrong, we might need to specially process
1713 the array of ubyte into host order. */
1714 prefix_out
->pf_opcode_length_table
= line_ptr
;
1716 /* pf_opcode_base is one greater than the size of the array. */
1717 line_ptr
+= prefix_out
->pf_opcode_base
- 1;
1720 /* Determine (as best we can) whether the
1721 pf_opcode_length_table holds 9 or 12 standard-conforming
1722 entries. gcc4 upped to DWARF3's 12 without updating the
1724 int operand_ck_fail
= true;
1726 if (prefix_out
->pf_opcode_base
>= STANDARD_OPERAND_COUNT_DWARF3
) {
1727 int mismatch
= memcmp(dwarf_standard_opcode_operand_count
,
1728 prefix_out
->pf_opcode_length_table
,
1729 STANDARD_OPERAND_COUNT_DWARF3
);
1732 print_header_issue(dbg
,"standard-operands did not match",
1733 data_start
,err_count_out
);
1735 mismatch
= memcmp(dwarf_arm_standard_opcode_operand_count
,
1736 prefix_out
->pf_opcode_length_table
,
1737 STANDARD_OPERAND_COUNT_DWARF3
);
1738 if(!mismatch
&& err_count_out
) {
1739 print_header_issue(dbg
,"arm (incorrect) operands in use",
1740 data_start
,err_count_out
);
1746 print_header_issue(dbg
,
1747 "standard DWARF3 operands matched, but is DWARF2 linetable",
1748 data_start
,err_count_out
);
1751 operand_ck_fail
= false;
1752 prefix_out
->pf_std_op_count
=
1753 STANDARD_OPERAND_COUNT_DWARF3
;
1756 if (operand_ck_fail
) {
1757 if (prefix_out
->pf_opcode_base
>=
1758 STANDARD_OPERAND_COUNT_DWARF2
) {
1761 memcmp(dwarf_standard_opcode_operand_count
,
1762 prefix_out
->pf_opcode_length_table
,
1763 STANDARD_OPERAND_COUNT_DWARF2
);
1766 print_header_issue(dbg
,"standard-operands-lengths did not match",
1767 data_start
,err_count_out
);
1769 mismatch
= memcmp(dwarf_arm_standard_opcode_operand_count
,
1770 prefix_out
->pf_opcode_length_table
,
1771 STANDARD_OPERAND_COUNT_DWARF2
);
1772 if(!mismatch
&& err_count_out
) {
1773 print_header_issue(dbg
,"arm (incorrect) operand in use",
1774 data_start
,err_count_out
);
1779 operand_ck_fail
= false;
1780 prefix_out
->pf_std_op_count
=
1781 STANDARD_OPERAND_COUNT_DWARF2
;
1785 if (operand_ck_fail
) {
1786 /* Here we are not sure what the pf_std_op_count is. */
1787 _dwarf_error(dbg
, err
, DW_DLE_LINE_NUM_OPERANDS_BAD
);
1788 return (DW_DLV_ERROR
);
1791 /* At this point we no longer need to check operand counts. */
1794 directories_count
= 0;
1795 directories_malloc
= 5;
1796 prefix_out
->pf_include_directories
= malloc(sizeof(Dwarf_Small
*) *
1797 directories_malloc
);
1798 if (prefix_out
->pf_include_directories
== NULL
) {
1799 _dwarf_error(dbg
, err
, DW_DLE_ALLOC_FAIL
);
1800 return (DW_DLV_ERROR
);
1802 memset(prefix_out
->pf_include_directories
, 0,
1803 sizeof(Dwarf_Small
*) * directories_malloc
);
1805 while ((*(char *) line_ptr
) != '\0') {
1806 if (directories_count
>= directories_malloc
) {
1807 Dwarf_Unsigned expand
= 2 * directories_malloc
;
1808 Dwarf_Unsigned bytesalloc
= sizeof(Dwarf_Small
*) * expand
;
1809 Dwarf_Small
**newdirs
=
1810 realloc(prefix_out
->pf_include_directories
,
1814 _dwarf_error(dbg
, err
, DW_DLE_ALLOC_FAIL
);
1815 return (DW_DLV_ERROR
);
1817 /* Doubled size, zero out second half. */
1818 memset(newdirs
+ directories_malloc
, 0,
1819 sizeof(Dwarf_Small
*) * directories_malloc
);
1820 directories_malloc
= expand
;
1821 prefix_out
->pf_include_directories
= newdirs
;
1823 prefix_out
->pf_include_directories
[directories_count
] =
1825 line_ptr
= line_ptr
+ strlen((char *) line_ptr
) + 1;
1826 directories_count
++;
1828 prefix_out
->pf_include_directories_count
= directories_count
;
1833 prefix_out
->pf_line_table_file_entries
=
1834 malloc(sizeof(struct Line_Table_File_Entry_s
) * files_malloc
);
1835 if (prefix_out
->pf_line_table_file_entries
== NULL
) {
1836 _dwarf_error(dbg
, err
, DW_DLE_ALLOC_FAIL
);
1837 return (DW_DLV_ERROR
);
1839 memset(prefix_out
->pf_line_table_file_entries
, 0,
1840 sizeof(struct Line_Table_File_Entry_s
) * files_malloc
);
1842 while (*(char *) line_ptr
!= '\0') {
1843 Dwarf_Unsigned utmp
;
1844 Dwarf_Unsigned dir_index
= 0;
1845 Dwarf_Unsigned lastmod
= 0;
1846 Dwarf_Unsigned file_length
= 0;
1847 struct Line_Table_File_Entry_s
*curline
;
1848 Dwarf_Word leb128_length
= 0;
1851 if (files_count
>= files_malloc
) {
1852 Dwarf_Unsigned expand
= 2 * files_malloc
;
1853 struct Line_Table_File_Entry_s
*newfiles
=
1854 realloc(prefix_out
->pf_line_table_file_entries
,
1855 sizeof(struct Line_Table_File_Entry_s
) *
1858 _dwarf_error(dbg
, err
, DW_DLE_ALLOC_FAIL
);
1859 return (DW_DLV_ERROR
);
1861 memset(newfiles
+ files_malloc
, 0,
1862 sizeof(struct Line_Table_File_Entry_s
) *
1864 files_malloc
= expand
;
1865 prefix_out
->pf_line_table_file_entries
= newfiles
;
1867 curline
= prefix_out
->pf_line_table_file_entries
+ files_count
;
1869 curline
->lte_filename
= line_ptr
;
1870 line_ptr
= line_ptr
+ strlen((char *) line_ptr
) + 1;
1872 DECODE_LEB128_UWORD(line_ptr
, utmp
);
1873 dir_index
= (Dwarf_Sword
) utmp
;
1874 if (dir_index
> directories_count
) {
1875 _dwarf_error(dbg
, err
, DW_DLE_DIR_INDEX_BAD
);
1876 return (DW_DLV_ERROR
);
1878 curline
->lte_directory_index
= dir_index
;
1880 lastmod
= _dwarf_decode_u_leb128(line_ptr
, &leb128_length
);
1881 line_ptr
= line_ptr
+ leb128_length
;
1882 curline
->lte_last_modification_time
= lastmod
;
1884 /* Skip over file length. */
1885 file_length
= _dwarf_decode_u_leb128(line_ptr
, &leb128_length
);
1886 line_ptr
= line_ptr
+ leb128_length
;
1887 curline
->lte_length_of_file
= file_length
;
1892 prefix_out
->pf_files_count
= files_count
;
1893 /* Skip trailing nul byte */
1897 lp_begin
= prefix_out
->pf_line_prologue_start
+
1898 prefix_out
->pf_prologue_length
;
1899 if (line_ptr
!= lp_begin
) {
1900 if(line_ptr
> lp_begin
) {
1901 _dwarf_error(dbg
, err
, DW_DLE_LINE_PROLOG_LENGTH_BAD
);
1902 return (DW_DLV_ERROR
);
1904 /* Bug in compiler. These
1905 * bytes are really part of the instruction
1906 * stream. The prefix_out->pf_prologue_length is
1907 * wrong (12 too high). */
1908 if(bogus_bytes_ptr
) {
1909 *bogus_bytes_ptr
= line_ptr
;
1912 /* How far off things are. We expect the
1914 *bogus_bytes
= (lp_begin
- line_ptr
);
1917 /* Ignore the lp_begin calc. Assume line_ptr right.
1918 Making up for compiler bug. */
1919 lp_begin
= line_ptr
;
1923 *updated_data_start_out
= lp_begin
;
1928 /* Initialize the Line_Table_Prefix_s struct.
1929 memset is not guaranteed a portable initializer, but works
1930 fine for current architectures. AFAIK.
1933 dwarf_init_line_table_prefix(struct Line_Table_Prefix_s
*pf
)
1935 memset(pf
, 0, sizeof(*pf
));
1938 /* Free any malloc'd area. of the Line_Table_Prefix_s struct. */
1940 dwarf_free_line_table_prefix(struct Line_Table_Prefix_s
*pf
)
1942 if (pf
->pf_include_directories
) {
1943 free(pf
->pf_include_directories
);
1944 pf
->pf_include_directories
= 0;
1946 if (pf
->pf_line_table_file_entries
) {
1947 free(pf
->pf_line_table_file_entries
);
1948 pf
->pf_line_table_file_entries
= 0;