149d3bc91SRichard Lowe /* 249d3bc91SRichard Lowe 3*07dc1947SRichard Lowe Copyright (C) 2000, 2004, 2006 Silicon Graphics, Inc. All Rights Reserved. 4*07dc1947SRichard Lowe Portions Copyright (C) 2009-2010 David Anderson. All Rights Reserved. 549d3bc91SRichard Lowe 649d3bc91SRichard Lowe This program is free software; you can redistribute it and/or modify it 749d3bc91SRichard Lowe under the terms of version 2.1 of the GNU Lesser General Public License 849d3bc91SRichard Lowe as published by the Free Software Foundation. 949d3bc91SRichard Lowe 1049d3bc91SRichard Lowe This program is distributed in the hope that it would be useful, but 1149d3bc91SRichard Lowe WITHOUT ANY WARRANTY; without even the implied warranty of 1249d3bc91SRichard Lowe MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 1349d3bc91SRichard Lowe 1449d3bc91SRichard Lowe Further, this software is distributed without any warranty that it is 1549d3bc91SRichard Lowe free of the rightful claim of any third person regarding infringement 1649d3bc91SRichard Lowe or the like. Any license provided herein, whether implied or 1749d3bc91SRichard Lowe otherwise, applies only to this software file. Patent licenses, if 1849d3bc91SRichard Lowe any, provided herein do not apply to combinations of this program with 1949d3bc91SRichard Lowe other software, or any other product whatsoever. 2049d3bc91SRichard Lowe 2149d3bc91SRichard Lowe You should have received a copy of the GNU Lesser General Public 2249d3bc91SRichard Lowe License along with this program; if not, write the Free Software 23*07dc1947SRichard Lowe Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301, 2449d3bc91SRichard Lowe USA. 2549d3bc91SRichard Lowe 26*07dc1947SRichard Lowe Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane, 2749d3bc91SRichard Lowe Mountain View, CA 94043, or: 2849d3bc91SRichard Lowe 2949d3bc91SRichard Lowe http://www.sgi.com 3049d3bc91SRichard Lowe 3149d3bc91SRichard Lowe For further information regarding this notice, see: 3249d3bc91SRichard Lowe 3349d3bc91SRichard Lowe http://oss.sgi.com/projects/GenInfo/NoticeExplan 3449d3bc91SRichard Lowe 3549d3bc91SRichard Lowe */ 3649d3bc91SRichard Lowe 3749d3bc91SRichard Lowe 3849d3bc91SRichard Lowe 3949d3bc91SRichard Lowe #define DW_EXTENDED_OPCODE 0 4049d3bc91SRichard Lowe 4149d3bc91SRichard Lowe /* 4249d3bc91SRichard Lowe This is used as the starting value for an algorithm 4349d3bc91SRichard Lowe to get the minimum difference between 2 values. 4449d3bc91SRichard Lowe UINT_MAX is used as our approximation to infinity. 4549d3bc91SRichard Lowe */ 4649d3bc91SRichard Lowe #define MAX_LINE_DIFF UINT_MAX 4749d3bc91SRichard Lowe 48*07dc1947SRichard Lowe /* This is for a sanity check on line 49*07dc1947SRichard Lowe table extended opcodes. 50*07dc1947SRichard Lowe It is entirely arbitrary, and 100 is surely too small if 51*07dc1947SRichard Lowe someone was inserting strings in the opcode. */ 52*07dc1947SRichard Lowe #define DW_LNE_LEN_MAX 100 53*07dc1947SRichard Lowe 5449d3bc91SRichard Lowe 5549d3bc91SRichard Lowe /* 5649d3bc91SRichard Lowe This structure is used to build a list of all the 5749d3bc91SRichard Lowe files that are used in the current compilation unit. 5849d3bc91SRichard Lowe All of the fields execpt fi_next have meanings that 5949d3bc91SRichard Lowe are obvious from section 6.2.4 of the Libdwarf Doc. 6049d3bc91SRichard Lowe */ 6149d3bc91SRichard Lowe struct Dwarf_File_Entry_s { 6249d3bc91SRichard Lowe /* Points to string naming the file. */ 6349d3bc91SRichard Lowe Dwarf_Small *fi_file_name; 6449d3bc91SRichard Lowe 6549d3bc91SRichard Lowe /* 6649d3bc91SRichard Lowe Index into the list of directories of the directory in which 6749d3bc91SRichard Lowe this file exits. */ 6849d3bc91SRichard Lowe Dwarf_Sword fi_dir_index; 6949d3bc91SRichard Lowe 7049d3bc91SRichard Lowe /* Time of last modification of the file. */ 7149d3bc91SRichard Lowe Dwarf_Unsigned fi_time_last_mod; 7249d3bc91SRichard Lowe 7349d3bc91SRichard Lowe /* Length in bytes of the file. */ 7449d3bc91SRichard Lowe Dwarf_Unsigned fi_file_length; 7549d3bc91SRichard Lowe 7649d3bc91SRichard Lowe /* Pointer for chaining file entries. */ 7749d3bc91SRichard Lowe Dwarf_File_Entry fi_next; 7849d3bc91SRichard Lowe }; 7949d3bc91SRichard Lowe 8049d3bc91SRichard Lowe 8149d3bc91SRichard Lowe typedef struct Dwarf_Line_Context_s *Dwarf_Line_Context; 8249d3bc91SRichard Lowe 8349d3bc91SRichard Lowe /* 8449d3bc91SRichard Lowe This structure provides the context in which the fields of 8549d3bc91SRichard Lowe a Dwarf_Line structure are interpreted. They come from the 8649d3bc91SRichard Lowe statement program prologue. **Updated by dwarf_srclines in 8749d3bc91SRichard Lowe dwarf_line.c. 8849d3bc91SRichard Lowe */ 8949d3bc91SRichard Lowe struct Dwarf_Line_Context_s { 9049d3bc91SRichard Lowe /* 9149d3bc91SRichard Lowe Points to a chain of entries providing info about source files 92*07dc1947SRichard Lowe for the current set of Dwarf_Line structures. File number 93*07dc1947SRichard Lowe 'li_file 1' is last on the list, the first list entry is the 94*07dc1947SRichard Lowe file numbered lc_file_entry_count. The numbering of the file 95*07dc1947SRichard Lowe names matches the dwarf2/3 line table specification file table 96*07dc1947SRichard Lowe and DW_LNE_define_file numbering rules. */ 9749d3bc91SRichard Lowe Dwarf_File_Entry lc_file_entries; 9849d3bc91SRichard Lowe /* 9949d3bc91SRichard Lowe Count of number of source files for this set of Dwarf_Line 10049d3bc91SRichard Lowe structures. */ 10149d3bc91SRichard Lowe Dwarf_Sword lc_file_entry_count; 10249d3bc91SRichard Lowe /* 10349d3bc91SRichard Lowe Points to the portion of .debug_line section that contains a 10449d3bc91SRichard Lowe list of strings naming the included directories. */ 10549d3bc91SRichard Lowe Dwarf_Small *lc_include_directories; 10649d3bc91SRichard Lowe 10749d3bc91SRichard Lowe /* Count of the number of included directories. */ 10849d3bc91SRichard Lowe Dwarf_Sword lc_include_directories_count; 10949d3bc91SRichard Lowe 11049d3bc91SRichard Lowe /* Count of the number of lines for this cu. */ 11149d3bc91SRichard Lowe Dwarf_Sword lc_line_count; 11249d3bc91SRichard Lowe 11349d3bc91SRichard Lowe /* Points to name of compilation directory. */ 11449d3bc91SRichard Lowe Dwarf_Small *lc_compilation_directory; 11549d3bc91SRichard Lowe 11649d3bc91SRichard Lowe Dwarf_Debug lc_dbg; 117*07dc1947SRichard Lowe 118*07dc1947SRichard Lowe Dwarf_Half lc_version_number; /* DWARF2/3 version number, 2 119*07dc1947SRichard Lowe for DWARF2, 3 for DWARF3. */ 12049d3bc91SRichard Lowe }; 12149d3bc91SRichard Lowe 12249d3bc91SRichard Lowe 12349d3bc91SRichard Lowe /* 12449d3bc91SRichard Lowe This structure defines a row of the line table. 12549d3bc91SRichard Lowe All of the fields except li_offset have the exact 12649d3bc91SRichard Lowe same meaning that is defined in Section 6.2.2 12749d3bc91SRichard Lowe of the Libdwarf Document. 12849d3bc91SRichard Lowe 12949d3bc91SRichard Lowe li_offset is used by _dwarf_addr_finder() which is called 13049d3bc91SRichard Lowe by rqs(1), an sgi utility for 'moving' shared libraries 13149d3bc91SRichard Lowe as if the static linker (ld) had linked the shared library 13249d3bc91SRichard Lowe at the newly-specified address. Most libdwarf-using 13349d3bc91SRichard Lowe apps will ignore li_offset and _dwarf_addr_finder(). 13449d3bc91SRichard Lowe 13549d3bc91SRichard Lowe */ 13649d3bc91SRichard Lowe struct Dwarf_Line_s { 13749d3bc91SRichard Lowe Dwarf_Addr li_address; /* pc value of machine instr */ 13849d3bc91SRichard Lowe union addr_or_line_s { 13949d3bc91SRichard Lowe struct li_inner_s { 14049d3bc91SRichard Lowe Dwarf_Sword li_file; /* int identifying src file */ 141*07dc1947SRichard Lowe /* li_file is a number 1-N, indexing into a conceptual 142*07dc1947SRichard Lowe source file table as described in dwarf2/3 spec line 143*07dc1947SRichard Lowe table doc. (see Dwarf_File_Entry lc_file_entries; and 144*07dc1947SRichard Lowe Dwarf_Sword lc_file_entry_count;) */ 145*07dc1947SRichard Lowe 14649d3bc91SRichard Lowe Dwarf_Sword li_line; /* source file line number. */ 14749d3bc91SRichard Lowe Dwarf_Half li_column; /* source file column number */ 148*07dc1947SRichard Lowe Dwarf_Small li_isa; 149*07dc1947SRichard Lowe 150*07dc1947SRichard Lowe /* To save space, use bit flags. */ 151*07dc1947SRichard Lowe /* indicate start of stmt */ 152*07dc1947SRichard Lowe unsigned char li_is_stmt:1; 153*07dc1947SRichard Lowe 154*07dc1947SRichard Lowe /* indicate start basic block */ 155*07dc1947SRichard Lowe unsigned char li_basic_block:1; 156*07dc1947SRichard Lowe 157*07dc1947SRichard Lowe /* first post sequence instr */ 158*07dc1947SRichard Lowe unsigned char li_end_sequence:1; 159*07dc1947SRichard Lowe 160*07dc1947SRichard Lowe unsigned char li_prologue_end:1; 161*07dc1947SRichard Lowe unsigned char li_epilogue_begin:1; 16249d3bc91SRichard Lowe } li_l_data; 16349d3bc91SRichard Lowe Dwarf_Off li_offset; /* for rqs */ 16449d3bc91SRichard Lowe } li_addr_line; 16549d3bc91SRichard Lowe Dwarf_Line_Context li_context; /* assoc Dwarf_Line_Context_s */ 16649d3bc91SRichard Lowe }; 16749d3bc91SRichard Lowe 16849d3bc91SRichard Lowe 169*07dc1947SRichard Lowe int _dwarf_line_address_offsets(Dwarf_Debug dbg, 17049d3bc91SRichard Lowe Dwarf_Die die, 17149d3bc91SRichard Lowe Dwarf_Addr ** addrs, 17249d3bc91SRichard Lowe Dwarf_Off ** offs, 17349d3bc91SRichard Lowe Dwarf_Unsigned * returncount, 17449d3bc91SRichard Lowe Dwarf_Error * err); 175*07dc1947SRichard Lowe int _dwarf_internal_srclines(Dwarf_Die die, 176*07dc1947SRichard Lowe Dwarf_Line ** linebuf, 177*07dc1947SRichard Lowe Dwarf_Signed * count, 178*07dc1947SRichard Lowe Dwarf_Bool doaddrs, 179*07dc1947SRichard Lowe Dwarf_Bool dolines, Dwarf_Error * error); 180*07dc1947SRichard Lowe 18149d3bc91SRichard Lowe 18249d3bc91SRichard Lowe 18349d3bc91SRichard Lowe /* The LOP, WHAT_IS_OPCODE stuff is here so it can 18449d3bc91SRichard Lowe be reused in 3 places. Seemed hard to keep 18549d3bc91SRichard Lowe the 3 places the same without an inline func or 18649d3bc91SRichard Lowe a macro. 18749d3bc91SRichard Lowe 18849d3bc91SRichard Lowe Handling the line section where the header and the 189*07dc1947SRichard Lowe file being processed do not match (unusual, but 19049d3bc91SRichard Lowe planned for in the design of .debug_line) 19149d3bc91SRichard Lowe is too tricky to recode this several times and keep 19249d3bc91SRichard Lowe it right. 193*07dc1947SRichard Lowe 194*07dc1947SRichard Lowe As it is the code starting up line-reading is duplicated 195*07dc1947SRichard Lowe and that is just wrong to do. FIXME! 19649d3bc91SRichard Lowe */ 19749d3bc91SRichard Lowe #define LOP_EXTENDED 1 19849d3bc91SRichard Lowe #define LOP_DISCARD 2 19949d3bc91SRichard Lowe #define LOP_STANDARD 3 20049d3bc91SRichard Lowe #define LOP_SPECIAL 4 20149d3bc91SRichard Lowe 202*07dc1947SRichard Lowe #define WHAT_IS_OPCODE(type,opcode,base,opcode_length,line_ptr,highest_std) \ 203*07dc1947SRichard Lowe if( (opcode) < (base) ) { \ 20449d3bc91SRichard Lowe /* we know we must treat as a standard op \ 20549d3bc91SRichard Lowe or a special case. \ 20649d3bc91SRichard Lowe */ \ 207*07dc1947SRichard Lowe if((opcode) == DW_EXTENDED_OPCODE) { \ 20849d3bc91SRichard Lowe type = LOP_EXTENDED; \ 209*07dc1947SRichard Lowe } else if( ((highest_std)+1) >= (base)) { \ 21049d3bc91SRichard Lowe /* == Standard case: compile of \ 21149d3bc91SRichard Lowe dwarf_line.c and object \ 21249d3bc91SRichard Lowe have same standard op codes set. \ 21349d3bc91SRichard Lowe \ 21449d3bc91SRichard Lowe > Special case: compile of dwarf_line.c\ 21549d3bc91SRichard Lowe has things in standard op codes list \ 21649d3bc91SRichard Lowe in dwarf.h header not \ 21749d3bc91SRichard Lowe in the object: handle this as a standard\ 21849d3bc91SRichard Lowe op code in switch below. \ 21949d3bc91SRichard Lowe The header special ops overlap the \ 22049d3bc91SRichard Lowe object standard ops. \ 22149d3bc91SRichard Lowe The new standard op codes will not \ 22249d3bc91SRichard Lowe appear in the object. \ 22349d3bc91SRichard Lowe */ \ 22449d3bc91SRichard Lowe type = LOP_STANDARD; \ 22549d3bc91SRichard Lowe } else { \ 22649d3bc91SRichard Lowe /* These are standard opcodes in the object\ 22749d3bc91SRichard Lowe ** that were not defined in the header \ 22849d3bc91SRichard Lowe ** at the time dwarf_line.c \ 22949d3bc91SRichard Lowe ** was compiled. Provides the ability of \ 23049d3bc91SRichard Lowe ** out-of-date dwarf reader to read newer \ 23149d3bc91SRichard Lowe ** line table data transparently. \ 23249d3bc91SRichard Lowe */ \ 23349d3bc91SRichard Lowe type = LOP_DISCARD; \ 23449d3bc91SRichard Lowe } \ 23549d3bc91SRichard Lowe \ 23649d3bc91SRichard Lowe } else { \ 23749d3bc91SRichard Lowe /* Is a special op code. \ 23849d3bc91SRichard Lowe */ \ 23949d3bc91SRichard Lowe type = LOP_SPECIAL; \ 24049d3bc91SRichard Lowe } 24149d3bc91SRichard Lowe 24249d3bc91SRichard Lowe /* The following is from the dwarf definition of 'ubyte' 24349d3bc91SRichard Lowe and is specifically mentioned in section 6.2.5.1, page 54 24449d3bc91SRichard Lowe of the Rev 2.0.0 dwarf specification. 24549d3bc91SRichard Lowe */ 24649d3bc91SRichard Lowe 24749d3bc91SRichard Lowe #define MAX_LINE_OP_CODE 255 248*07dc1947SRichard Lowe 249*07dc1947SRichard Lowe 250*07dc1947SRichard Lowe /* The following structs (Line_Table_File_Entry_s,Line_Table_Prefix_s) 251*07dc1947SRichard Lowe and functions allow refactoring common code into a single 252*07dc1947SRichard Lowe reader routine. 253*07dc1947SRichard Lowe */ 254*07dc1947SRichard Lowe /* There can be zero of more of these needed for 1 line prologue. */ 255*07dc1947SRichard Lowe struct Line_Table_File_Entry_s { 256*07dc1947SRichard Lowe Dwarf_Small *lte_filename; 257*07dc1947SRichard Lowe Dwarf_Unsigned lte_directory_index; 258*07dc1947SRichard Lowe Dwarf_Unsigned lte_last_modification_time; 259*07dc1947SRichard Lowe Dwarf_Unsigned lte_length_of_file; 260*07dc1947SRichard Lowe }; 261*07dc1947SRichard Lowe 262*07dc1947SRichard Lowe /* Data picked up from the line table prologue for a single 263*07dc1947SRichard Lowe CU. */ 264*07dc1947SRichard Lowe struct Line_Table_Prefix_s { 265*07dc1947SRichard Lowe 266*07dc1947SRichard Lowe /* pf_total_length is the value of the length field for the line 267*07dc1947SRichard Lowe table of this CU. So it does not count the length of itself (the 268*07dc1947SRichard Lowe length value) for consistency with the say lenghts recorded in 269*07dc1947SRichard Lowe DWARF2/3. */ 270*07dc1947SRichard Lowe Dwarf_Unsigned pf_total_length; 271*07dc1947SRichard Lowe 272*07dc1947SRichard Lowe /* Length of the initial length field itself. */ 273*07dc1947SRichard Lowe Dwarf_Half pf_length_field_length; 274*07dc1947SRichard Lowe 275*07dc1947SRichard Lowe /* The version is 2 for DWARF2, 3 for DWARF3 */ 276*07dc1947SRichard Lowe Dwarf_Half pf_version; 277*07dc1947SRichard Lowe 278*07dc1947SRichard Lowe Dwarf_Unsigned pf_prologue_length; 279*07dc1947SRichard Lowe Dwarf_Small pf_minimum_instruction_length; 280*07dc1947SRichard Lowe 281*07dc1947SRichard Lowe /* Start and end of this CU line area. pf_line_ptr_start + 282*07dc1947SRichard Lowe pf_total_length + pf_length_field_length == pf_line_ptr_end. 283*07dc1947SRichard Lowe Meaning pf_line_ptr_start is before the length info. */ 284*07dc1947SRichard Lowe Dwarf_Small *pf_line_ptr_start; 285*07dc1947SRichard Lowe Dwarf_Small *pf_line_ptr_end; 286*07dc1947SRichard Lowe 287*07dc1947SRichard Lowe /* Used to check that decoding of the line prologue is done right. */ 288*07dc1947SRichard Lowe Dwarf_Small *pf_line_prologue_start; 289*07dc1947SRichard Lowe 290*07dc1947SRichard Lowe Dwarf_Small pf_default_is_stmt; 291*07dc1947SRichard Lowe Dwarf_Sbyte pf_line_base; 292*07dc1947SRichard Lowe Dwarf_Small pf_line_range; 293*07dc1947SRichard Lowe 294*07dc1947SRichard Lowe /* Highest std opcode (+1). */ 295*07dc1947SRichard Lowe Dwarf_Small pf_opcode_base; 296*07dc1947SRichard Lowe 297*07dc1947SRichard Lowe /* pf_opcode_base -1 entries (each a count, normally the value of 298*07dc1947SRichard Lowe each entry is 0 or 1). */ 299*07dc1947SRichard Lowe Dwarf_Small *pf_opcode_length_table; 300*07dc1947SRichard Lowe 301*07dc1947SRichard Lowe Dwarf_Unsigned pf_include_directories_count; 302*07dc1947SRichard Lowe /* Array of pointers to dir strings. pf_include_directories_count 303*07dc1947SRichard Lowe entriesin the array. */ 304*07dc1947SRichard Lowe Dwarf_Small **pf_include_directories; 305*07dc1947SRichard Lowe 306*07dc1947SRichard Lowe /* Count of entries in line_table_file_entries array. */ 307*07dc1947SRichard Lowe Dwarf_Unsigned pf_files_count; 308*07dc1947SRichard Lowe struct Line_Table_File_Entry_s *pf_line_table_file_entries; 309*07dc1947SRichard Lowe 310*07dc1947SRichard Lowe /* The number to treat as standard ops. This is a special 311*07dc1947SRichard Lowe accomodation of gcc using the new standard opcodes but not 312*07dc1947SRichard Lowe updating the version number. It's legal dwarf2, but much better 313*07dc1947SRichard Lowe for the user to understand as dwarf3 when 'it looks ok'. */ 314*07dc1947SRichard Lowe Dwarf_Bool pf_std_op_count; 315*07dc1947SRichard Lowe 316*07dc1947SRichard Lowe }; 317*07dc1947SRichard Lowe 318*07dc1947SRichard Lowe void dwarf_init_line_table_prefix(struct Line_Table_Prefix_s *pf); 319*07dc1947SRichard Lowe void dwarf_free_line_table_prefix(struct Line_Table_Prefix_s *pf); 320*07dc1947SRichard Lowe 321*07dc1947SRichard Lowe int dwarf_read_line_table_prefix(Dwarf_Debug dbg, 322*07dc1947SRichard Lowe Dwarf_Small * data_start, 323*07dc1947SRichard Lowe Dwarf_Unsigned data_length, 324*07dc1947SRichard Lowe Dwarf_Small ** updated_data_start_out, 325*07dc1947SRichard Lowe struct Line_Table_Prefix_s *prefix_out, 326*07dc1947SRichard Lowe /* The following 2 arguments are solely for warning users 327*07dc1947SRichard Lowe * when there is a surprising 'gap' in the .debug_line info. */ 328*07dc1947SRichard Lowe Dwarf_Small ** bogus_bytes_ptr, 329*07dc1947SRichard Lowe Dwarf_Unsigned * bogus_bytes_count, 330*07dc1947SRichard Lowe Dwarf_Error * err, 331*07dc1947SRichard Lowe int * err_count_out); 332