Debian Codegraph @

src/lib/libdwarf/dwarf_line.h

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