1*7fd79137SRobert Mustacchi /* 2*7fd79137SRobert Mustacchi 3*7fd79137SRobert Mustacchi Copyright (C) 2000, 2004, 2006 Silicon Graphics, Inc. All Rights Reserved. 4*7fd79137SRobert Mustacchi Portions Copyright (C) 2009-2010 David Anderson. All Rights Reserved. 5*7fd79137SRobert Mustacchi 6*7fd79137SRobert Mustacchi This program is free software; you can redistribute it and/or modify it 7*7fd79137SRobert Mustacchi under the terms of version 2.1 of the GNU Lesser General Public License 8*7fd79137SRobert Mustacchi as published by the Free Software Foundation. 9*7fd79137SRobert Mustacchi 10*7fd79137SRobert Mustacchi This program is distributed in the hope that it would be useful, but 11*7fd79137SRobert Mustacchi WITHOUT ANY WARRANTY; without even the implied warranty of 12*7fd79137SRobert Mustacchi MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13*7fd79137SRobert Mustacchi 14*7fd79137SRobert Mustacchi Further, this software is distributed without any warranty that it is 15*7fd79137SRobert Mustacchi free of the rightful claim of any third person regarding infringement 16*7fd79137SRobert Mustacchi or the like. Any license provided herein, whether implied or 17*7fd79137SRobert Mustacchi otherwise, applies only to this software file. Patent licenses, if 18*7fd79137SRobert Mustacchi any, provided herein do not apply to combinations of this program with 19*7fd79137SRobert Mustacchi other software, or any other product whatsoever. 20*7fd79137SRobert Mustacchi 21*7fd79137SRobert Mustacchi You should have received a copy of the GNU Lesser General Public 22*7fd79137SRobert Mustacchi License along with this program; if not, write the Free Software 23*7fd79137SRobert Mustacchi Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301, 24*7fd79137SRobert Mustacchi USA. 25*7fd79137SRobert Mustacchi 26*7fd79137SRobert Mustacchi Contact information: Silicon Graphics, Inc., 1500 Crittenden Lane, 27*7fd79137SRobert Mustacchi Mountain View, CA 94043, or: 28*7fd79137SRobert Mustacchi 29*7fd79137SRobert Mustacchi http://www.sgi.com 30*7fd79137SRobert Mustacchi 31*7fd79137SRobert Mustacchi For further information regarding this notice, see: 32*7fd79137SRobert Mustacchi 33*7fd79137SRobert Mustacchi http://oss.sgi.com/projects/GenInfo/NoticeExplan 34*7fd79137SRobert Mustacchi 35*7fd79137SRobert Mustacchi */ 36*7fd79137SRobert Mustacchi 37*7fd79137SRobert Mustacchi 38*7fd79137SRobert Mustacchi 39*7fd79137SRobert Mustacchi #define DW_EXTENDED_OPCODE 0 40*7fd79137SRobert Mustacchi 41*7fd79137SRobert Mustacchi /* 42*7fd79137SRobert Mustacchi This is used as the starting value for an algorithm 43*7fd79137SRobert Mustacchi to get the minimum difference between 2 values. 44*7fd79137SRobert Mustacchi UINT_MAX is used as our approximation to infinity. 45*7fd79137SRobert Mustacchi */ 46*7fd79137SRobert Mustacchi #define MAX_LINE_DIFF UINT_MAX 47*7fd79137SRobert Mustacchi 48*7fd79137SRobert Mustacchi /* This is for a sanity check on line 49*7fd79137SRobert Mustacchi table extended opcodes. 50*7fd79137SRobert Mustacchi It is entirely arbitrary, and 100 is surely too small if 51*7fd79137SRobert Mustacchi someone was inserting strings in the opcode. */ 52*7fd79137SRobert Mustacchi #define DW_LNE_LEN_MAX 100 53*7fd79137SRobert Mustacchi 54*7fd79137SRobert Mustacchi 55*7fd79137SRobert Mustacchi /* 56*7fd79137SRobert Mustacchi This structure is used to build a list of all the 57*7fd79137SRobert Mustacchi files that are used in the current compilation unit. 58*7fd79137SRobert Mustacchi All of the fields execpt fi_next have meanings that 59*7fd79137SRobert Mustacchi are obvious from section 6.2.4 of the Libdwarf Doc. 60*7fd79137SRobert Mustacchi */ 61*7fd79137SRobert Mustacchi struct Dwarf_File_Entry_s { 62*7fd79137SRobert Mustacchi /* Points to string naming the file. */ 63*7fd79137SRobert Mustacchi Dwarf_Small *fi_file_name; 64*7fd79137SRobert Mustacchi 65*7fd79137SRobert Mustacchi /* 66*7fd79137SRobert Mustacchi Index into the list of directories of the directory in which 67*7fd79137SRobert Mustacchi this file exits. */ 68*7fd79137SRobert Mustacchi Dwarf_Sword fi_dir_index; 69*7fd79137SRobert Mustacchi 70*7fd79137SRobert Mustacchi /* Time of last modification of the file. */ 71*7fd79137SRobert Mustacchi Dwarf_Unsigned fi_time_last_mod; 72*7fd79137SRobert Mustacchi 73*7fd79137SRobert Mustacchi /* Length in bytes of the file. */ 74*7fd79137SRobert Mustacchi Dwarf_Unsigned fi_file_length; 75*7fd79137SRobert Mustacchi 76*7fd79137SRobert Mustacchi /* Pointer for chaining file entries. */ 77*7fd79137SRobert Mustacchi Dwarf_File_Entry fi_next; 78*7fd79137SRobert Mustacchi }; 79*7fd79137SRobert Mustacchi 80*7fd79137SRobert Mustacchi 81*7fd79137SRobert Mustacchi typedef struct Dwarf_Line_Context_s *Dwarf_Line_Context; 82*7fd79137SRobert Mustacchi 83*7fd79137SRobert Mustacchi /* 84*7fd79137SRobert Mustacchi This structure provides the context in which the fields of 85*7fd79137SRobert Mustacchi a Dwarf_Line structure are interpreted. They come from the 86*7fd79137SRobert Mustacchi statement program prologue. **Updated by dwarf_srclines in 87*7fd79137SRobert Mustacchi dwarf_line.c. 88*7fd79137SRobert Mustacchi */ 89*7fd79137SRobert Mustacchi struct Dwarf_Line_Context_s { 90*7fd79137SRobert Mustacchi /* 91*7fd79137SRobert Mustacchi Points to a chain of entries providing info about source files 92*7fd79137SRobert Mustacchi for the current set of Dwarf_Line structures. File number 93*7fd79137SRobert Mustacchi 'li_file 1' is last on the list, the first list entry is the 94*7fd79137SRobert Mustacchi file numbered lc_file_entry_count. The numbering of the file 95*7fd79137SRobert Mustacchi names matches the dwarf2/3 line table specification file table 96*7fd79137SRobert Mustacchi and DW_LNE_define_file numbering rules. */ 97*7fd79137SRobert Mustacchi Dwarf_File_Entry lc_file_entries; 98*7fd79137SRobert Mustacchi /* 99*7fd79137SRobert Mustacchi Count of number of source files for this set of Dwarf_Line 100*7fd79137SRobert Mustacchi structures. */ 101*7fd79137SRobert Mustacchi Dwarf_Sword lc_file_entry_count; 102*7fd79137SRobert Mustacchi /* 103*7fd79137SRobert Mustacchi Points to the portion of .debug_line section that contains a 104*7fd79137SRobert Mustacchi list of strings naming the included directories. */ 105*7fd79137SRobert Mustacchi Dwarf_Small *lc_include_directories; 106*7fd79137SRobert Mustacchi 107*7fd79137SRobert Mustacchi /* Count of the number of included directories. */ 108*7fd79137SRobert Mustacchi Dwarf_Sword lc_include_directories_count; 109*7fd79137SRobert Mustacchi 110*7fd79137SRobert Mustacchi /* Count of the number of lines for this cu. */ 111*7fd79137SRobert Mustacchi Dwarf_Sword lc_line_count; 112*7fd79137SRobert Mustacchi 113*7fd79137SRobert Mustacchi /* Points to name of compilation directory. */ 114*7fd79137SRobert Mustacchi Dwarf_Small *lc_compilation_directory; 115*7fd79137SRobert Mustacchi 116*7fd79137SRobert Mustacchi Dwarf_Debug lc_dbg; 117*7fd79137SRobert Mustacchi 118*7fd79137SRobert Mustacchi Dwarf_Half lc_version_number; /* DWARF2/3 version number, 2 119*7fd79137SRobert Mustacchi for DWARF2, 3 for DWARF3. */ 120*7fd79137SRobert Mustacchi }; 121*7fd79137SRobert Mustacchi 122*7fd79137SRobert Mustacchi 123*7fd79137SRobert Mustacchi /* 124*7fd79137SRobert Mustacchi This structure defines a row of the line table. 125*7fd79137SRobert Mustacchi All of the fields except li_offset have the exact 126*7fd79137SRobert Mustacchi same meaning that is defined in Section 6.2.2 127*7fd79137SRobert Mustacchi of the Libdwarf Document. 128*7fd79137SRobert Mustacchi 129*7fd79137SRobert Mustacchi li_offset is used by _dwarf_addr_finder() which is called 130*7fd79137SRobert Mustacchi by rqs(1), an sgi utility for 'moving' shared libraries 131*7fd79137SRobert Mustacchi as if the static linker (ld) had linked the shared library 132*7fd79137SRobert Mustacchi at the newly-specified address. Most libdwarf-using 133*7fd79137SRobert Mustacchi apps will ignore li_offset and _dwarf_addr_finder(). 134*7fd79137SRobert Mustacchi 135*7fd79137SRobert Mustacchi */ 136*7fd79137SRobert Mustacchi struct Dwarf_Line_s { 137*7fd79137SRobert Mustacchi Dwarf_Addr li_address; /* pc value of machine instr */ 138*7fd79137SRobert Mustacchi union addr_or_line_s { 139*7fd79137SRobert Mustacchi struct li_inner_s { 140*7fd79137SRobert Mustacchi Dwarf_Sword li_file; /* int identifying src file */ 141*7fd79137SRobert Mustacchi /* li_file is a number 1-N, indexing into a conceptual 142*7fd79137SRobert Mustacchi source file table as described in dwarf2/3 spec line 143*7fd79137SRobert Mustacchi table doc. (see Dwarf_File_Entry lc_file_entries; and 144*7fd79137SRobert Mustacchi Dwarf_Sword lc_file_entry_count;) */ 145*7fd79137SRobert Mustacchi 146*7fd79137SRobert Mustacchi Dwarf_Sword li_line; /* source file line number. */ 147*7fd79137SRobert Mustacchi Dwarf_Half li_column; /* source file column number */ 148*7fd79137SRobert Mustacchi Dwarf_Small li_isa; 149*7fd79137SRobert Mustacchi 150*7fd79137SRobert Mustacchi /* To save space, use bit flags. */ 151*7fd79137SRobert Mustacchi /* indicate start of stmt */ 152*7fd79137SRobert Mustacchi unsigned char li_is_stmt:1; 153*7fd79137SRobert Mustacchi 154*7fd79137SRobert Mustacchi /* indicate start basic block */ 155*7fd79137SRobert Mustacchi unsigned char li_basic_block:1; 156*7fd79137SRobert Mustacchi 157*7fd79137SRobert Mustacchi /* first post sequence instr */ 158*7fd79137SRobert Mustacchi unsigned char li_end_sequence:1; 159*7fd79137SRobert Mustacchi 160*7fd79137SRobert Mustacchi unsigned char li_prologue_end:1; 161*7fd79137SRobert Mustacchi unsigned char li_epilogue_begin:1; 162*7fd79137SRobert Mustacchi } li_l_data; 163*7fd79137SRobert Mustacchi Dwarf_Off li_offset; /* for rqs */ 164*7fd79137SRobert Mustacchi } li_addr_line; 165*7fd79137SRobert Mustacchi Dwarf_Line_Context li_context; /* assoc Dwarf_Line_Context_s */ 166*7fd79137SRobert Mustacchi }; 167*7fd79137SRobert Mustacchi 168*7fd79137SRobert Mustacchi 169*7fd79137SRobert Mustacchi int _dwarf_line_address_offsets(Dwarf_Debug dbg, 170*7fd79137SRobert Mustacchi Dwarf_Die die, 171*7fd79137SRobert Mustacchi Dwarf_Addr ** addrs, 172*7fd79137SRobert Mustacchi Dwarf_Off ** offs, 173*7fd79137SRobert Mustacchi Dwarf_Unsigned * returncount, 174*7fd79137SRobert Mustacchi Dwarf_Error * err); 175*7fd79137SRobert Mustacchi int _dwarf_internal_srclines(Dwarf_Die die, 176*7fd79137SRobert Mustacchi Dwarf_Line ** linebuf, 177*7fd79137SRobert Mustacchi Dwarf_Signed * count, 178*7fd79137SRobert Mustacchi Dwarf_Bool doaddrs, 179*7fd79137SRobert Mustacchi Dwarf_Bool dolines, Dwarf_Error * error); 180*7fd79137SRobert Mustacchi 181*7fd79137SRobert Mustacchi 182*7fd79137SRobert Mustacchi 183*7fd79137SRobert Mustacchi /* The LOP, WHAT_IS_OPCODE stuff is here so it can 184*7fd79137SRobert Mustacchi be reused in 3 places. Seemed hard to keep 185*7fd79137SRobert Mustacchi the 3 places the same without an inline func or 186*7fd79137SRobert Mustacchi a macro. 187*7fd79137SRobert Mustacchi 188*7fd79137SRobert Mustacchi Handling the line section where the header and the 189*7fd79137SRobert Mustacchi file being processed do not match (unusual, but 190*7fd79137SRobert Mustacchi planned for in the design of .debug_line) 191*7fd79137SRobert Mustacchi is too tricky to recode this several times and keep 192*7fd79137SRobert Mustacchi it right. 193*7fd79137SRobert Mustacchi 194*7fd79137SRobert Mustacchi As it is the code starting up line-reading is duplicated 195*7fd79137SRobert Mustacchi and that is just wrong to do. FIXME! 196*7fd79137SRobert Mustacchi */ 197*7fd79137SRobert Mustacchi #define LOP_EXTENDED 1 198*7fd79137SRobert Mustacchi #define LOP_DISCARD 2 199*7fd79137SRobert Mustacchi #define LOP_STANDARD 3 200*7fd79137SRobert Mustacchi #define LOP_SPECIAL 4 201*7fd79137SRobert Mustacchi 202*7fd79137SRobert Mustacchi #define WHAT_IS_OPCODE(type,opcode,base,opcode_length,line_ptr,highest_std) \ 203*7fd79137SRobert Mustacchi if( (opcode) < (base) ) { \ 204*7fd79137SRobert Mustacchi /* we know we must treat as a standard op \ 205*7fd79137SRobert Mustacchi or a special case. \ 206*7fd79137SRobert Mustacchi */ \ 207*7fd79137SRobert Mustacchi if((opcode) == DW_EXTENDED_OPCODE) { \ 208*7fd79137SRobert Mustacchi type = LOP_EXTENDED; \ 209*7fd79137SRobert Mustacchi } else if( ((highest_std)+1) >= (base)) { \ 210*7fd79137SRobert Mustacchi /* == Standard case: compile of \ 211*7fd79137SRobert Mustacchi dwarf_line.c and object \ 212*7fd79137SRobert Mustacchi have same standard op codes set. \ 213*7fd79137SRobert Mustacchi \ 214*7fd79137SRobert Mustacchi > Special case: compile of dwarf_line.c\ 215*7fd79137SRobert Mustacchi has things in standard op codes list \ 216*7fd79137SRobert Mustacchi in dwarf.h header not \ 217*7fd79137SRobert Mustacchi in the object: handle this as a standard\ 218*7fd79137SRobert Mustacchi op code in switch below. \ 219*7fd79137SRobert Mustacchi The header special ops overlap the \ 220*7fd79137SRobert Mustacchi object standard ops. \ 221*7fd79137SRobert Mustacchi The new standard op codes will not \ 222*7fd79137SRobert Mustacchi appear in the object. \ 223*7fd79137SRobert Mustacchi */ \ 224*7fd79137SRobert Mustacchi type = LOP_STANDARD; \ 225*7fd79137SRobert Mustacchi } else { \ 226*7fd79137SRobert Mustacchi /* These are standard opcodes in the object\ 227*7fd79137SRobert Mustacchi ** that were not defined in the header \ 228*7fd79137SRobert Mustacchi ** at the time dwarf_line.c \ 229*7fd79137SRobert Mustacchi ** was compiled. Provides the ability of \ 230*7fd79137SRobert Mustacchi ** out-of-date dwarf reader to read newer \ 231*7fd79137SRobert Mustacchi ** line table data transparently. \ 232*7fd79137SRobert Mustacchi */ \ 233*7fd79137SRobert Mustacchi type = LOP_DISCARD; \ 234*7fd79137SRobert Mustacchi } \ 235*7fd79137SRobert Mustacchi \ 236*7fd79137SRobert Mustacchi } else { \ 237*7fd79137SRobert Mustacchi /* Is a special op code. \ 238*7fd79137SRobert Mustacchi */ \ 239*7fd79137SRobert Mustacchi type = LOP_SPECIAL; \ 240*7fd79137SRobert Mustacchi } 241*7fd79137SRobert Mustacchi 242*7fd79137SRobert Mustacchi /* The following is from the dwarf definition of 'ubyte' 243*7fd79137SRobert Mustacchi and is specifically mentioned in section 6.2.5.1, page 54 244*7fd79137SRobert Mustacchi of the Rev 2.0.0 dwarf specification. 245*7fd79137SRobert Mustacchi */ 246*7fd79137SRobert Mustacchi 247*7fd79137SRobert Mustacchi #define MAX_LINE_OP_CODE 255 248*7fd79137SRobert Mustacchi 249*7fd79137SRobert Mustacchi 250*7fd79137SRobert Mustacchi /* The following structs (Line_Table_File_Entry_s,Line_Table_Prefix_s) 251*7fd79137SRobert Mustacchi and functions allow refactoring common code into a single 252*7fd79137SRobert Mustacchi reader routine. 253*7fd79137SRobert Mustacchi */ 254*7fd79137SRobert Mustacchi /* There can be zero of more of these needed for 1 line prologue. */ 255*7fd79137SRobert Mustacchi struct Line_Table_File_Entry_s { 256*7fd79137SRobert Mustacchi Dwarf_Small *lte_filename; 257*7fd79137SRobert Mustacchi Dwarf_Unsigned lte_directory_index; 258*7fd79137SRobert Mustacchi Dwarf_Unsigned lte_last_modification_time; 259*7fd79137SRobert Mustacchi Dwarf_Unsigned lte_length_of_file; 260*7fd79137SRobert Mustacchi }; 261*7fd79137SRobert Mustacchi 262*7fd79137SRobert Mustacchi /* Data picked up from the line table prologue for a single 263*7fd79137SRobert Mustacchi CU. */ 264*7fd79137SRobert Mustacchi struct Line_Table_Prefix_s { 265*7fd79137SRobert Mustacchi 266*7fd79137SRobert Mustacchi /* pf_total_length is the value of the length field for the line 267*7fd79137SRobert Mustacchi table of this CU. So it does not count the length of itself (the 268*7fd79137SRobert Mustacchi length value) for consistency with the say lenghts recorded in 269*7fd79137SRobert Mustacchi DWARF2/3. */ 270*7fd79137SRobert Mustacchi Dwarf_Unsigned pf_total_length; 271*7fd79137SRobert Mustacchi 272*7fd79137SRobert Mustacchi /* Length of the initial length field itself. */ 273*7fd79137SRobert Mustacchi Dwarf_Half pf_length_field_length; 274*7fd79137SRobert Mustacchi 275*7fd79137SRobert Mustacchi /* The version is 2 for DWARF2, 3 for DWARF3 */ 276*7fd79137SRobert Mustacchi Dwarf_Half pf_version; 277*7fd79137SRobert Mustacchi 278*7fd79137SRobert Mustacchi Dwarf_Unsigned pf_prologue_length; 279*7fd79137SRobert Mustacchi Dwarf_Small pf_minimum_instruction_length; 280*7fd79137SRobert Mustacchi 281*7fd79137SRobert Mustacchi /* Start and end of this CU line area. pf_line_ptr_start + 282*7fd79137SRobert Mustacchi pf_total_length + pf_length_field_length == pf_line_ptr_end. 283*7fd79137SRobert Mustacchi Meaning pf_line_ptr_start is before the length info. */ 284*7fd79137SRobert Mustacchi Dwarf_Small *pf_line_ptr_start; 285*7fd79137SRobert Mustacchi Dwarf_Small *pf_line_ptr_end; 286*7fd79137SRobert Mustacchi 287*7fd79137SRobert Mustacchi /* Used to check that decoding of the line prologue is done right. */ 288*7fd79137SRobert Mustacchi Dwarf_Small *pf_line_prologue_start; 289*7fd79137SRobert Mustacchi 290*7fd79137SRobert Mustacchi Dwarf_Small pf_default_is_stmt; 291*7fd79137SRobert Mustacchi Dwarf_Sbyte pf_line_base; 292*7fd79137SRobert Mustacchi Dwarf_Small pf_line_range; 293*7fd79137SRobert Mustacchi 294*7fd79137SRobert Mustacchi /* Highest std opcode (+1). */ 295*7fd79137SRobert Mustacchi Dwarf_Small pf_opcode_base; 296*7fd79137SRobert Mustacchi 297*7fd79137SRobert Mustacchi /* pf_opcode_base -1 entries (each a count, normally the value of 298*7fd79137SRobert Mustacchi each entry is 0 or 1). */ 299*7fd79137SRobert Mustacchi Dwarf_Small *pf_opcode_length_table; 300*7fd79137SRobert Mustacchi 301*7fd79137SRobert Mustacchi Dwarf_Unsigned pf_include_directories_count; 302*7fd79137SRobert Mustacchi /* Array of pointers to dir strings. pf_include_directories_count 303*7fd79137SRobert Mustacchi entriesin the array. */ 304*7fd79137SRobert Mustacchi Dwarf_Small **pf_include_directories; 305*7fd79137SRobert Mustacchi 306*7fd79137SRobert Mustacchi /* Count of entries in line_table_file_entries array. */ 307*7fd79137SRobert Mustacchi Dwarf_Unsigned pf_files_count; 308*7fd79137SRobert Mustacchi struct Line_Table_File_Entry_s *pf_line_table_file_entries; 309*7fd79137SRobert Mustacchi 310*7fd79137SRobert Mustacchi /* The number to treat as standard ops. This is a special 311*7fd79137SRobert Mustacchi accomodation of gcc using the new standard opcodes but not 312*7fd79137SRobert Mustacchi updating the version number. It's legal dwarf2, but much better 313*7fd79137SRobert Mustacchi for the user to understand as dwarf3 when 'it looks ok'. */ 314*7fd79137SRobert Mustacchi Dwarf_Bool pf_std_op_count; 315*7fd79137SRobert Mustacchi 316*7fd79137SRobert Mustacchi }; 317*7fd79137SRobert Mustacchi 318*7fd79137SRobert Mustacchi void dwarf_init_line_table_prefix(struct Line_Table_Prefix_s *pf); 319*7fd79137SRobert Mustacchi void dwarf_free_line_table_prefix(struct Line_Table_Prefix_s *pf); 320*7fd79137SRobert Mustacchi 321*7fd79137SRobert Mustacchi int dwarf_read_line_table_prefix(Dwarf_Debug dbg, 322*7fd79137SRobert Mustacchi Dwarf_Small * data_start, 323*7fd79137SRobert Mustacchi Dwarf_Unsigned data_length, 324*7fd79137SRobert Mustacchi Dwarf_Small ** updated_data_start_out, 325*7fd79137SRobert Mustacchi struct Line_Table_Prefix_s *prefix_out, 326*7fd79137SRobert Mustacchi /* The following 2 arguments are solely for warning users 327*7fd79137SRobert Mustacchi * when there is a surprising 'gap' in the .debug_line info. */ 328*7fd79137SRobert Mustacchi Dwarf_Small ** bogus_bytes_ptr, 329*7fd79137SRobert Mustacchi Dwarf_Unsigned * bogus_bytes_count, 330*7fd79137SRobert Mustacchi Dwarf_Error * err, 331*7fd79137SRobert Mustacchi int * err_count_out); 332