1 /*
2   Copyright (C) 2000, 2004, 2006 Silicon Graphics, Inc.  All Rights Reserved.
3   Portions Copyright (C) 2009-2015 David Anderson. All Rights Reserved.
4   Portions Copyright (C) 2010-2012 SN Systems Ltd. All Rights Reserved.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of version 2.1 of the GNU Lesser General Public License
8   as published by the Free Software Foundation.
9 
10   This program is distributed in the hope that it would be useful, but
11   WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 
14   Further, this software is distributed without any warranty that it is
15   free of the rightful claim of any third person regarding infringement
16   or the like.  Any license provided herein, whether implied or
17   otherwise, applies only to this software file.  Patent licenses, if
18   any, provided herein do not apply to combinations of this program with
19   other software, or any other product whatsoever.
20 
21   You should have received a copy of the GNU Lesser General Public
22   License along with this program; if not, write the Free Software
23   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston MA 02110-1301,
24   USA.
25 
26 */
27 
28 
29 
30 #define DW_EXTENDED_OPCODE   0
31 
32 /*
33     This is used as the starting value for an algorithm
34     to get the minimum difference between 2 values.
35     UINT_MAX is used as our approximation to infinity.
36 */
37 #define MAX_LINE_DIFF       UINT_MAX
38 
39 /* This is for a sanity check on line
40    table extended opcodes.
41    It is entirely arbitrary, and 100 is surely too small if
42    someone was inserting strings in the opcode. */
43 #define DW_LNE_LEN_MAX   100
44 
45 
46 /*
47     This structure is used to build a list of all the
48     files that are used in the current compilation unit.
49     All of the fields execpt fi_next have meanings that
50     are obvious from section 6.2.4 of the Libdwarf Doc.
51     Because of DW_LNE_define_file we
52     make this a list, not an array.
53 */
54 struct Dwarf_File_Entry_s {
55     struct Dwarf_File_Entry_s *fi_next;
56 
57     /* Points to string naming the file. */
58     Dwarf_Small *fi_file_name;
59 
60     /*  Index into the list of directories of the directory in which
61         this file exits. */
62     Dwarf_Word fi_dir_index;
63 
64     /* Time of last modification of the file. */
65     Dwarf_Unsigned fi_time_last_mod;
66 
67     /* Length in bytes of the file. */
68     Dwarf_Unsigned fi_file_length;
69 };
70 
71 /*  Part of two-level line tables support. */
72 struct Dwarf_Subprog_Entry_s {
73     Dwarf_Small *ds_subprog_name;
74     Dwarf_Unsigned ds_decl_file;
75     Dwarf_Unsigned ds_decl_line;
76 };
77 
78 typedef struct Dwarf_Subprog_Entry_s *Dwarf_Subprog_Entry;
79 
80 
81 /*
82     This structure provides the context in which the fields of
83     a Dwarf_Line structure are interpreted.  They come from the
84     statement program prologue.  **Updated by dwarf_srclines in
85     dwarf_line.c.
86 
87     lc_magic will be DW_CONTEXT_MAGIC unless there is a serious
88     programming error somewhere.
89     It's set zero when a Line_Context is deallocated.
90     Any other value indicates there is bug somewhere.
91 */
92 #define DW_CONTEXT_MAGIC 0xd00d1111
93 struct Dwarf_Line_Context_s {
94     unsigned    lc_magic;
95 
96     /*  lc_new_style_access is non-zero if this was allocated
97         via a dwarf_srclines_b() call or equivalent.
98         Otherwise this is 0.  */
99     unsigned char lc_new_style_access;
100 
101     /* The section offset (in .debug_line
102         or .debug_line.dwo of the line table */
103     Dwarf_Unsigned lc_section_offset;
104 
105     /*  2 for DWARF2, 3 for DWARF3, 4 for DWARF4, 5 for DWARF5.
106         0xf006 for experimental two-level line tables. */
107     Dwarf_Half lc_version_number;
108 
109     /* Total length of the line data for this CU */
110     Dwarf_Unsigned lc_total_length;
111 
112     /* Length of the initial length field itself. */
113     Dwarf_Half lc_length_field_length;
114 
115     /* address size and segment sizefields new in DWARF5 header.  */
116     Dwarf_Small lc_address_size;
117     Dwarf_Small lc_segment_selector_size;
118 
119     Dwarf_Unsigned lc_prologue_length;
120     Dwarf_Unsigned lc_actuals_table_offset;
121     Dwarf_Unsigned lc_logicals_table_offset;
122     Dwarf_Small lc_minimum_instruction_length;
123 
124     /*  Start and end of this CU line area. pf_line_ptr_start +
125         pf_total_length + pf_length_field_length == pf_line_ptr_end.
126         Meaning lc_line_ptr_start is before the length info. */
127     Dwarf_Small *lc_line_ptr_start;
128     Dwarf_Small *lc_line_ptr_end;
129     /*  Start of the lines themselves. */
130     Dwarf_Small *lc_line_ptr_lines;
131 
132     /* Used to check that decoding of the line prologue is done right. */
133     Dwarf_Small *lc_line_prologue_start;
134 
135     Dwarf_Small lc_default_is_stmt;
136     Dwarf_Ubyte lc_maximum_ops_per_instruction; /*DWARF5*/
137     Dwarf_Sbyte lc_line_base;
138     Dwarf_Small lc_line_range;
139 
140     /* Highest std opcode (+1).  */
141     Dwarf_Small lc_opcode_base;
142     /*  pf_opcode_base -1 entries (each a count, normally the value of
143         each entry is 0 or 1). */
144     Dwarf_Small *lc_opcode_length_table;
145 
146     /*  The number to treat as standard ops. This is a special
147         accomodation of gcc using the new standard opcodes but not
148         updating the version number. It's legal dwarf2, but much better
149         for the user to understand as dwarf3 when 'it looks ok'. */
150     Dwarf_Small lc_std_op_count;
151 
152     /*  Points to a singly-linked list of entries providing info
153         about source files
154         for the current set of Dwarf_Line structures.
155         The initial  entry on the list is 'file 1' per DWARF rules.
156         And so on.  lc_last_entry points at the last entry
157         in the list (so we can easily expand the list).
158         It's a list (not a table) since we may encounter
159         DW_LNE_define_file entries. */
160     Dwarf_File_Entry lc_file_entries;
161     Dwarf_File_Entry lc_last_entry;
162     /*  Count of number of source files for this set of Dwarf_Line
163         structures. */
164     Dwarf_Word lc_file_entry_count;
165 
166 
167     /*  Points to the portion of .debug_line section that
168         contains a list of strings naming the included
169         directories.  Do not free().
170         An array of pointers to strings.  */
171     Dwarf_Small **lc_include_directories;
172     /*  Count of the number of included directories. */
173     Dwarf_Word lc_include_directories_count;
174 
175 
176     /*  Points to an array of subprogram entries.
177         With Two level line tables this may be non-zero.
178         An array of Dwarf_Subprogram_Entry_s structs. */
179     Dwarf_Subprog_Entry lc_subprogs;
180 
181     /*  Count of the number of subprogram entries
182         With Two level line tables this may be non-zero. */
183     Dwarf_Word lc_subprogs_count;
184 
185     /*  Count of the number of lines for this cu. */
186     Dwarf_Word lc_line_count;
187 
188     /*  Points to name of compilation directory.
189         That string is in a .debug section so
190         do not free this. */
191     Dwarf_Small *lc_compilation_directory;
192 
193     Dwarf_Debug lc_dbg;
194 
195     /*  zero table count is skeleton, or just missing names.
196         1 is standard table.
197         2 means two-level table (experimantal)
198         Other is a bug somewhere.  */
199     Dwarf_Small lc_table_count;
200     Dwarf_Bool lc_is_single_table;
201 
202     /* For standard line tables  the logicals are
203         the only tables and linecount_actuals is 0. */
204     Dwarf_Line   *lc_linebuf_logicals;
205     Dwarf_Unsigned lc_linecount_logicals;
206 
207     /* Non-zero only if two-level table with actuals */
208     Dwarf_Line   *lc_linebuf_actuals;
209     Dwarf_Unsigned lc_linecount_actuals;
210 };
211 
212 
213 
214 /*  The line table set of registers.
215     The state machine state variables.
216     Using names from the DWARF documentation
217     but preceded by lr_.  */
218 struct Dwarf_Line_Registers_s {
219     Dwarf_Addr lr_address;        /* DWARF2 */
220     Dwarf_Word lr_file ;          /* DWARF2 */
221     Dwarf_Word lr_line ;          /* DWARF2 */
222     Dwarf_Word lr_column ;        /* DWARF2 */
223     Dwarf_Bool lr_is_stmt;        /* DWARF2 */
224     Dwarf_Bool lr_basic_block;    /* DWARF2 */
225     Dwarf_Bool lr_end_sequence;   /* DWARF2 */
226     Dwarf_Bool lr_prologue_end;   /* DWARF3 */
227     Dwarf_Bool lr_epilogue_begin; /* DWARF3 */
228     Dwarf_Small lr_isa;           /* DWARF3 */
229     Dwarf_Unsigned lr_op_index;      /* DWARF4, operation within VLIW instruction. */
230     Dwarf_Unsigned lr_discriminator; /* DWARF4 */
231     Dwarf_Unsigned lr_call_context;       /* EXPERIMENTAL */
232     Dwarf_Unsigned lr_subprogram;     /* EXPERIMENTAL */
233 };
234 typedef struct Dwarf_Line_Registers_s *Dwarf_Line_Registers;
235 void _dwarf_set_line_table_regs_default_values(Dwarf_Line_Registers regs,
236     Dwarf_Bool is_stmt);
237 
238 
239 
240 /*
241     This structure defines a row of the line table.
242     All of the fields except li_offset have the exact
243     same meaning that is defined in Section 6.2.2
244     of the Libdwarf Document.
245 
246     li_offset is used by _dwarf_addr_finder() which is called
247     by rqs(1), an sgi utility for 'moving' shared libraries
248     as if the static linker (ld) had linked the shared library
249     at the newly-specified address.  Most libdwarf-using
250     apps will ignore li_offset and _dwarf_addr_finder().
251 
252 */
253 struct Dwarf_Line_s {
254     Dwarf_Addr li_address;  /* pc value of machine instr */
255     union addr_or_line_s {
256         struct li_inner_s {
257             /* New as of DWARF4 */
258             Dwarf_Unsigned li_discriminator;
259 
260             /*  int identifying src file
261                 li_file is a number 1-N, indexing into a conceptual
262                 source file table as described in dwarf2/3 spec line
263                 table doc. (see Dwarf_File_Entry lc_file_entries; and
264                 Dwarf_Word lc_file_entry_count;) */
265             Dwarf_Word li_file;
266 
267             /*  In single-level table is line number in source file. 1-N
268                 In logicals table is not used.
269                 In actuals table is index into logicals table.  1-N*/
270             Dwarf_Word li_line;
271 
272             Dwarf_Half li_column; /* source file column number  1-N */
273             Dwarf_Small li_isa;   /* New as of DWARF4. */
274 
275             /*  Two-level line tables.
276                 Is index from logicals table
277                 into logicals table. 1-N */
278             Dwarf_Unsigned li_call_context;
279 
280             /*  Two-level line tables.
281                 is index into subprograms table. 1-N */
282             Dwarf_Unsigned li_subprogram;
283 
284             /* To save space, use bit flags. */
285             /* indicate start of stmt */
286             unsigned li_is_stmt:1;
287 
288             /* indicate start basic block */
289             unsigned li_basic_block:1;
290 
291             /* first post sequence instr */
292             unsigned li_end_sequence:1;
293 
294             unsigned li_prologue_end:1;
295             unsigned li_epilogue_begin:1;
296 
297             /* Mark a line record as being DW_LNS_set_address. */
298             unsigned li_is_addr_set:1;
299         } li_l_data;
300 #ifdef __sgi /* SGI IRIX ONLY */
301         Dwarf_Off li_offset;  /* for SGI IRIX rqs only*/
302 #endif /* __sgi */
303     } li_addr_line;
304     Dwarf_Line_Context li_context; /* assoc Dwarf_Line_Context_s */
305 
306     /*  Set only on the actuals table of a two-level line table.
307         Assists in the dealloc code.
308     */
309     Dwarf_Bool li_is_actuals_table;
310 };
311 
312 
313 int _dwarf_line_address_offsets(Dwarf_Debug dbg,
314     Dwarf_Die die,
315     Dwarf_Addr ** addrs,
316     Dwarf_Off ** offs,
317     Dwarf_Unsigned * returncount,
318     Dwarf_Error * err);
319 int _dwarf_internal_srclines(Dwarf_Die die,
320     Dwarf_Bool old_interface,
321     Dwarf_Unsigned * version,
322     Dwarf_Small     * table_count,
323     Dwarf_Line_Context *line_context,
324     Dwarf_Line ** linebuf,
325     Dwarf_Signed * count,
326     Dwarf_Line ** linebuf_actuals,
327     Dwarf_Signed * count_actuals,
328     Dwarf_Bool doaddrs,
329     Dwarf_Bool dolines,
330     Dwarf_Error * error);
331 
332 /*  The LOP, WHAT_IS_OPCODE stuff is here so it can
333     be reused in 3 places.  Seemed hard to keep
334     the 3 places the same without an inline func or
335     a macro.
336 
337     Handling the line section where the header and the
338     file being processed do not match (unusual, but
339     planned for in the  design of .debug_line)
340     is too tricky to recode this several times and keep
341     it right.
342 
343     As it is the code starting up line-reading is duplicated
344     and that is just wrong to do. FIXME!
345 */
346 #define LOP_EXTENDED 1
347 #define LOP_DISCARD  2
348 #define LOP_STANDARD 3
349 #define LOP_SPECIAL  4
350 
351 #define WHAT_IS_OPCODE(type,opcode,base,opcode_length,line_ptr,highest_std) \
352     if ((opcode) < (base)) {                             \
353         /*  we know we must treat as a standard op       \
354             or a special case. */                        \
355         if ((opcode) == DW_EXTENDED_OPCODE) {            \
356             type = LOP_EXTENDED;                         \
357         } else if ((highest_std+1) >= (base)) {        \
358             /*  == Standard case: compile of             \
359                 dwarf_line.c and object                  \
360                 have same standard op codes set.         \
361                 == Special case: compile of dwarf_line.c \
362                 has things in standard op codes list     \
363                 in dwarf.h header not                    \
364                 in the object: handle this as a standard \
365                 op code in switch below.                 \
366                 The header special ops overlap the       \
367                 object standard ops.                     \
368                 The new standard op codes will not       \
369                 appear in the object. */                 \
370             type = LOP_STANDARD;                         \
371         } else  {                                        \
372             /* These are standard opcodes in the object  \
373             ** that were not defined  in the header      \
374             ** at the time dwarf_line.c                  \
375             ** was compiled. Provides the ability of     \
376             ** out-of-date dwarf reader to read newer    \
377             ** line table data transparently.            \
378             */                                           \
379             type = LOP_DISCARD;                          \
380         }                                                \
381     } else {                                             \
382         /* Is  a special op code. */                     \
383         type =  LOP_SPECIAL;                             \
384     }
385 
386 /*  The following is from  the dwarf definition of 'ubyte'
387     and is specifically  mentioned in section  6.2.5.1, page 54
388     of the Rev 2.0.0 dwarf specification.
389 */
390 
391 #define MAX_LINE_OP_CODE  255
392 
393 /* Operand counts per standard operand.
394    The initial zero is for DW_LNS_copy.
395    This is an economical way to verify we understand the table
396    of standard-opcode-lengths in the line table prologue.  */
397 #define STANDARD_OPERAND_COUNT_DWARF2 9
398 #define STANDARD_OPERAND_COUNT_DWARF3 12
399 /* For two-level line tables, we have three additional standard opcodes. */
400 #define STANDARD_OPERAND_COUNT_TWO_LEVEL 15
401 
402 void _dwarf_print_header_issue(Dwarf_Debug dbg,
403     const char *specific_msg,
404     Dwarf_Small *data_start,
405     Dwarf_Signed value,
406     unsigned index,
407     unsigned tabv,
408     unsigned linetabv,
409     int *err_count_out);
410 int _dwarf_decode_line_string_form(Dwarf_Debug dbg,
411     Dwarf_Unsigned form,
412     Dwarf_Unsigned offset_size,
413     Dwarf_Small **line_ptr,
414     Dwarf_Small *line_ptr_end,
415     char **return_str,
416     Dwarf_Error * error);
417 int _dwarf_decode_line_udata_form(Dwarf_Debug dbg,
418     Dwarf_Unsigned form,
419     Dwarf_Small **line_ptr,
420     Dwarf_Unsigned *return_val,
421     Dwarf_Small *line_end_ptr,
422     Dwarf_Error * error);
423 
424 void _dwarf_update_chain_list( Dwarf_Chain chain_line,
425     Dwarf_Chain *head_chain, Dwarf_Chain *curr_chain);
426 void _dwarf_free_chain_entries(Dwarf_Debug dbg,Dwarf_Chain head,int count);
427 
428 int _dwarf_line_context_constructor(Dwarf_Debug dbg, void *m);
429 void _dwarf_line_context_destructor(void *m);
430 
431 void _dwarf_print_line_context_record(Dwarf_Debug dbg,
432     Dwarf_Line_Context line_context);
433 void _dwarf_context_src_files_destroy(Dwarf_Line_Context context);
434 int _dwarf_add_to_files_list(Dwarf_Line_Context context, Dwarf_File_Entry fe);
435