The open source OpenXR runtime
1/* dwarf.c -- Get file/line information from DWARF for backtraces.
2 Copyright (C) 2012-2021 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
15 distribution.
16
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
20
21THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31POSSIBILITY OF SUCH DAMAGE. */
32
33#include "config.h"
34
35#include <errno.h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/types.h>
39
40#include "filenames.hpp"
41
42#include "backtrace.hpp"
43#include "internal.hpp"
44
45namespace tracy
46{
47
48/* DWARF constants. */
49
50enum dwarf_tag {
51 DW_TAG_entry_point = 0x3,
52 DW_TAG_compile_unit = 0x11,
53 DW_TAG_inlined_subroutine = 0x1d,
54 DW_TAG_subprogram = 0x2e,
55 DW_TAG_skeleton_unit = 0x4a,
56};
57
58enum dwarf_form {
59 DW_FORM_addr = 0x01,
60 DW_FORM_block2 = 0x03,
61 DW_FORM_block4 = 0x04,
62 DW_FORM_data2 = 0x05,
63 DW_FORM_data4 = 0x06,
64 DW_FORM_data8 = 0x07,
65 DW_FORM_string = 0x08,
66 DW_FORM_block = 0x09,
67 DW_FORM_block1 = 0x0a,
68 DW_FORM_data1 = 0x0b,
69 DW_FORM_flag = 0x0c,
70 DW_FORM_sdata = 0x0d,
71 DW_FORM_strp = 0x0e,
72 DW_FORM_udata = 0x0f,
73 DW_FORM_ref_addr = 0x10,
74 DW_FORM_ref1 = 0x11,
75 DW_FORM_ref2 = 0x12,
76 DW_FORM_ref4 = 0x13,
77 DW_FORM_ref8 = 0x14,
78 DW_FORM_ref_udata = 0x15,
79 DW_FORM_indirect = 0x16,
80 DW_FORM_sec_offset = 0x17,
81 DW_FORM_exprloc = 0x18,
82 DW_FORM_flag_present = 0x19,
83 DW_FORM_ref_sig8 = 0x20,
84 DW_FORM_strx = 0x1a,
85 DW_FORM_addrx = 0x1b,
86 DW_FORM_ref_sup4 = 0x1c,
87 DW_FORM_strp_sup = 0x1d,
88 DW_FORM_data16 = 0x1e,
89 DW_FORM_line_strp = 0x1f,
90 DW_FORM_implicit_const = 0x21,
91 DW_FORM_loclistx = 0x22,
92 DW_FORM_rnglistx = 0x23,
93 DW_FORM_ref_sup8 = 0x24,
94 DW_FORM_strx1 = 0x25,
95 DW_FORM_strx2 = 0x26,
96 DW_FORM_strx3 = 0x27,
97 DW_FORM_strx4 = 0x28,
98 DW_FORM_addrx1 = 0x29,
99 DW_FORM_addrx2 = 0x2a,
100 DW_FORM_addrx3 = 0x2b,
101 DW_FORM_addrx4 = 0x2c,
102 DW_FORM_GNU_addr_index = 0x1f01,
103 DW_FORM_GNU_str_index = 0x1f02,
104 DW_FORM_GNU_ref_alt = 0x1f20,
105 DW_FORM_GNU_strp_alt = 0x1f21
106};
107
108enum dwarf_attribute {
109 DW_AT_sibling = 0x01,
110 DW_AT_location = 0x02,
111 DW_AT_name = 0x03,
112 DW_AT_ordering = 0x09,
113 DW_AT_subscr_data = 0x0a,
114 DW_AT_byte_size = 0x0b,
115 DW_AT_bit_offset = 0x0c,
116 DW_AT_bit_size = 0x0d,
117 DW_AT_element_list = 0x0f,
118 DW_AT_stmt_list = 0x10,
119 DW_AT_low_pc = 0x11,
120 DW_AT_high_pc = 0x12,
121 DW_AT_language = 0x13,
122 DW_AT_member = 0x14,
123 DW_AT_discr = 0x15,
124 DW_AT_discr_value = 0x16,
125 DW_AT_visibility = 0x17,
126 DW_AT_import = 0x18,
127 DW_AT_string_length = 0x19,
128 DW_AT_common_reference = 0x1a,
129 DW_AT_comp_dir = 0x1b,
130 DW_AT_const_value = 0x1c,
131 DW_AT_containing_type = 0x1d,
132 DW_AT_default_value = 0x1e,
133 DW_AT_inline = 0x20,
134 DW_AT_is_optional = 0x21,
135 DW_AT_lower_bound = 0x22,
136 DW_AT_producer = 0x25,
137 DW_AT_prototyped = 0x27,
138 DW_AT_return_addr = 0x2a,
139 DW_AT_start_scope = 0x2c,
140 DW_AT_bit_stride = 0x2e,
141 DW_AT_upper_bound = 0x2f,
142 DW_AT_abstract_origin = 0x31,
143 DW_AT_accessibility = 0x32,
144 DW_AT_address_class = 0x33,
145 DW_AT_artificial = 0x34,
146 DW_AT_base_types = 0x35,
147 DW_AT_calling_convention = 0x36,
148 DW_AT_count = 0x37,
149 DW_AT_data_member_location = 0x38,
150 DW_AT_decl_column = 0x39,
151 DW_AT_decl_file = 0x3a,
152 DW_AT_decl_line = 0x3b,
153 DW_AT_declaration = 0x3c,
154 DW_AT_discr_list = 0x3d,
155 DW_AT_encoding = 0x3e,
156 DW_AT_external = 0x3f,
157 DW_AT_frame_base = 0x40,
158 DW_AT_friend = 0x41,
159 DW_AT_identifier_case = 0x42,
160 DW_AT_macro_info = 0x43,
161 DW_AT_namelist_items = 0x44,
162 DW_AT_priority = 0x45,
163 DW_AT_segment = 0x46,
164 DW_AT_specification = 0x47,
165 DW_AT_static_link = 0x48,
166 DW_AT_type = 0x49,
167 DW_AT_use_location = 0x4a,
168 DW_AT_variable_parameter = 0x4b,
169 DW_AT_virtuality = 0x4c,
170 DW_AT_vtable_elem_location = 0x4d,
171 DW_AT_allocated = 0x4e,
172 DW_AT_associated = 0x4f,
173 DW_AT_data_location = 0x50,
174 DW_AT_byte_stride = 0x51,
175 DW_AT_entry_pc = 0x52,
176 DW_AT_use_UTF8 = 0x53,
177 DW_AT_extension = 0x54,
178 DW_AT_ranges = 0x55,
179 DW_AT_trampoline = 0x56,
180 DW_AT_call_column = 0x57,
181 DW_AT_call_file = 0x58,
182 DW_AT_call_line = 0x59,
183 DW_AT_description = 0x5a,
184 DW_AT_binary_scale = 0x5b,
185 DW_AT_decimal_scale = 0x5c,
186 DW_AT_small = 0x5d,
187 DW_AT_decimal_sign = 0x5e,
188 DW_AT_digit_count = 0x5f,
189 DW_AT_picture_string = 0x60,
190 DW_AT_mutable = 0x61,
191 DW_AT_threads_scaled = 0x62,
192 DW_AT_explicit = 0x63,
193 DW_AT_object_pointer = 0x64,
194 DW_AT_endianity = 0x65,
195 DW_AT_elemental = 0x66,
196 DW_AT_pure = 0x67,
197 DW_AT_recursive = 0x68,
198 DW_AT_signature = 0x69,
199 DW_AT_main_subprogram = 0x6a,
200 DW_AT_data_bit_offset = 0x6b,
201 DW_AT_const_expr = 0x6c,
202 DW_AT_enum_class = 0x6d,
203 DW_AT_linkage_name = 0x6e,
204 DW_AT_string_length_bit_size = 0x6f,
205 DW_AT_string_length_byte_size = 0x70,
206 DW_AT_rank = 0x71,
207 DW_AT_str_offsets_base = 0x72,
208 DW_AT_addr_base = 0x73,
209 DW_AT_rnglists_base = 0x74,
210 DW_AT_dwo_name = 0x76,
211 DW_AT_reference = 0x77,
212 DW_AT_rvalue_reference = 0x78,
213 DW_AT_macros = 0x79,
214 DW_AT_call_all_calls = 0x7a,
215 DW_AT_call_all_source_calls = 0x7b,
216 DW_AT_call_all_tail_calls = 0x7c,
217 DW_AT_call_return_pc = 0x7d,
218 DW_AT_call_value = 0x7e,
219 DW_AT_call_origin = 0x7f,
220 DW_AT_call_parameter = 0x80,
221 DW_AT_call_pc = 0x81,
222 DW_AT_call_tail_call = 0x82,
223 DW_AT_call_target = 0x83,
224 DW_AT_call_target_clobbered = 0x84,
225 DW_AT_call_data_location = 0x85,
226 DW_AT_call_data_value = 0x86,
227 DW_AT_noreturn = 0x87,
228 DW_AT_alignment = 0x88,
229 DW_AT_export_symbols = 0x89,
230 DW_AT_deleted = 0x8a,
231 DW_AT_defaulted = 0x8b,
232 DW_AT_loclists_base = 0x8c,
233 DW_AT_lo_user = 0x2000,
234 DW_AT_hi_user = 0x3fff,
235 DW_AT_MIPS_fde = 0x2001,
236 DW_AT_MIPS_loop_begin = 0x2002,
237 DW_AT_MIPS_tail_loop_begin = 0x2003,
238 DW_AT_MIPS_epilog_begin = 0x2004,
239 DW_AT_MIPS_loop_unroll_factor = 0x2005,
240 DW_AT_MIPS_software_pipeline_depth = 0x2006,
241 DW_AT_MIPS_linkage_name = 0x2007,
242 DW_AT_MIPS_stride = 0x2008,
243 DW_AT_MIPS_abstract_name = 0x2009,
244 DW_AT_MIPS_clone_origin = 0x200a,
245 DW_AT_MIPS_has_inlines = 0x200b,
246 DW_AT_HP_block_index = 0x2000,
247 DW_AT_HP_unmodifiable = 0x2001,
248 DW_AT_HP_prologue = 0x2005,
249 DW_AT_HP_epilogue = 0x2008,
250 DW_AT_HP_actuals_stmt_list = 0x2010,
251 DW_AT_HP_proc_per_section = 0x2011,
252 DW_AT_HP_raw_data_ptr = 0x2012,
253 DW_AT_HP_pass_by_reference = 0x2013,
254 DW_AT_HP_opt_level = 0x2014,
255 DW_AT_HP_prof_version_id = 0x2015,
256 DW_AT_HP_opt_flags = 0x2016,
257 DW_AT_HP_cold_region_low_pc = 0x2017,
258 DW_AT_HP_cold_region_high_pc = 0x2018,
259 DW_AT_HP_all_variables_modifiable = 0x2019,
260 DW_AT_HP_linkage_name = 0x201a,
261 DW_AT_HP_prof_flags = 0x201b,
262 DW_AT_HP_unit_name = 0x201f,
263 DW_AT_HP_unit_size = 0x2020,
264 DW_AT_HP_widened_byte_size = 0x2021,
265 DW_AT_HP_definition_points = 0x2022,
266 DW_AT_HP_default_location = 0x2023,
267 DW_AT_HP_is_result_param = 0x2029,
268 DW_AT_sf_names = 0x2101,
269 DW_AT_src_info = 0x2102,
270 DW_AT_mac_info = 0x2103,
271 DW_AT_src_coords = 0x2104,
272 DW_AT_body_begin = 0x2105,
273 DW_AT_body_end = 0x2106,
274 DW_AT_GNU_vector = 0x2107,
275 DW_AT_GNU_guarded_by = 0x2108,
276 DW_AT_GNU_pt_guarded_by = 0x2109,
277 DW_AT_GNU_guarded = 0x210a,
278 DW_AT_GNU_pt_guarded = 0x210b,
279 DW_AT_GNU_locks_excluded = 0x210c,
280 DW_AT_GNU_exclusive_locks_required = 0x210d,
281 DW_AT_GNU_shared_locks_required = 0x210e,
282 DW_AT_GNU_odr_signature = 0x210f,
283 DW_AT_GNU_template_name = 0x2110,
284 DW_AT_GNU_call_site_value = 0x2111,
285 DW_AT_GNU_call_site_data_value = 0x2112,
286 DW_AT_GNU_call_site_target = 0x2113,
287 DW_AT_GNU_call_site_target_clobbered = 0x2114,
288 DW_AT_GNU_tail_call = 0x2115,
289 DW_AT_GNU_all_tail_call_sites = 0x2116,
290 DW_AT_GNU_all_call_sites = 0x2117,
291 DW_AT_GNU_all_source_call_sites = 0x2118,
292 DW_AT_GNU_macros = 0x2119,
293 DW_AT_GNU_deleted = 0x211a,
294 DW_AT_GNU_dwo_name = 0x2130,
295 DW_AT_GNU_dwo_id = 0x2131,
296 DW_AT_GNU_ranges_base = 0x2132,
297 DW_AT_GNU_addr_base = 0x2133,
298 DW_AT_GNU_pubnames = 0x2134,
299 DW_AT_GNU_pubtypes = 0x2135,
300 DW_AT_GNU_discriminator = 0x2136,
301 DW_AT_GNU_locviews = 0x2137,
302 DW_AT_GNU_entry_view = 0x2138,
303 DW_AT_VMS_rtnbeg_pd_address = 0x2201,
304 DW_AT_use_GNAT_descriptive_type = 0x2301,
305 DW_AT_GNAT_descriptive_type = 0x2302,
306 DW_AT_GNU_numerator = 0x2303,
307 DW_AT_GNU_denominator = 0x2304,
308 DW_AT_GNU_bias = 0x2305,
309 DW_AT_upc_threads_scaled = 0x3210,
310 DW_AT_PGI_lbase = 0x3a00,
311 DW_AT_PGI_soffset = 0x3a01,
312 DW_AT_PGI_lstride = 0x3a02,
313 DW_AT_APPLE_optimized = 0x3fe1,
314 DW_AT_APPLE_flags = 0x3fe2,
315 DW_AT_APPLE_isa = 0x3fe3,
316 DW_AT_APPLE_block = 0x3fe4,
317 DW_AT_APPLE_major_runtime_vers = 0x3fe5,
318 DW_AT_APPLE_runtime_class = 0x3fe6,
319 DW_AT_APPLE_omit_frame_ptr = 0x3fe7,
320 DW_AT_APPLE_property_name = 0x3fe8,
321 DW_AT_APPLE_property_getter = 0x3fe9,
322 DW_AT_APPLE_property_setter = 0x3fea,
323 DW_AT_APPLE_property_attribute = 0x3feb,
324 DW_AT_APPLE_objc_complete_type = 0x3fec,
325 DW_AT_APPLE_property = 0x3fed
326};
327
328enum dwarf_line_number_op {
329 DW_LNS_extended_op = 0x0,
330 DW_LNS_copy = 0x1,
331 DW_LNS_advance_pc = 0x2,
332 DW_LNS_advance_line = 0x3,
333 DW_LNS_set_file = 0x4,
334 DW_LNS_set_column = 0x5,
335 DW_LNS_negate_stmt = 0x6,
336 DW_LNS_set_basic_block = 0x7,
337 DW_LNS_const_add_pc = 0x8,
338 DW_LNS_fixed_advance_pc = 0x9,
339 DW_LNS_set_prologue_end = 0xa,
340 DW_LNS_set_epilogue_begin = 0xb,
341 DW_LNS_set_isa = 0xc,
342};
343
344enum dwarf_extended_line_number_op {
345 DW_LNE_end_sequence = 0x1,
346 DW_LNE_set_address = 0x2,
347 DW_LNE_define_file = 0x3,
348 DW_LNE_set_discriminator = 0x4,
349};
350
351enum dwarf_line_number_content_type {
352 DW_LNCT_path = 0x1,
353 DW_LNCT_directory_index = 0x2,
354 DW_LNCT_timestamp = 0x3,
355 DW_LNCT_size = 0x4,
356 DW_LNCT_MD5 = 0x5,
357 DW_LNCT_lo_user = 0x2000,
358 DW_LNCT_hi_user = 0x3fff
359};
360
361enum dwarf_range_list_entry {
362 DW_RLE_end_of_list = 0x00,
363 DW_RLE_base_addressx = 0x01,
364 DW_RLE_startx_endx = 0x02,
365 DW_RLE_startx_length = 0x03,
366 DW_RLE_offset_pair = 0x04,
367 DW_RLE_base_address = 0x05,
368 DW_RLE_start_end = 0x06,
369 DW_RLE_start_length = 0x07
370};
371
372enum dwarf_unit_type {
373 DW_UT_compile = 0x01,
374 DW_UT_type = 0x02,
375 DW_UT_partial = 0x03,
376 DW_UT_skeleton = 0x04,
377 DW_UT_split_compile = 0x05,
378 DW_UT_split_type = 0x06,
379 DW_UT_lo_user = 0x80,
380 DW_UT_hi_user = 0xff
381};
382
383#if !defined(HAVE_DECL_STRNLEN) || !HAVE_DECL_STRNLEN
384
385/* If strnlen is not declared, provide our own version. */
386
387static size_t
388xstrnlen (const char *s, size_t maxlen)
389{
390 size_t i;
391
392 for (i = 0; i < maxlen; ++i)
393 if (s[i] == '\0')
394 break;
395 return i;
396}
397
398#define strnlen xstrnlen
399
400#endif
401
402/* A buffer to read DWARF info. */
403
404struct dwarf_buf
405{
406 /* Buffer name for error messages. */
407 const char *name;
408 /* Start of the buffer. */
409 const unsigned char *start;
410 /* Next byte to read. */
411 const unsigned char *buf;
412 /* The number of bytes remaining. */
413 size_t left;
414 /* Whether the data is big-endian. */
415 int is_bigendian;
416 /* Error callback routine. */
417 backtrace_error_callback error_callback;
418 /* Data for error_callback. */
419 void *data;
420 /* Non-zero if we've reported an underflow error. */
421 int reported_underflow;
422};
423
424/* A single attribute in a DWARF abbreviation. */
425
426struct attr
427{
428 /* The attribute name. */
429 enum dwarf_attribute name;
430 /* The attribute form. */
431 enum dwarf_form form;
432 /* The attribute value, for DW_FORM_implicit_const. */
433 int64_t val;
434};
435
436/* A single DWARF abbreviation. */
437
438struct abbrev
439{
440 /* The abbrev code--the number used to refer to the abbrev. */
441 uint64_t code;
442 /* The entry tag. */
443 enum dwarf_tag tag;
444 /* Non-zero if this abbrev has child entries. */
445 int has_children;
446 /* The number of attributes. */
447 size_t num_attrs;
448 /* The attributes. */
449 struct attr *attrs;
450};
451
452/* The DWARF abbreviations for a compilation unit. This structure
453 only exists while reading the compilation unit. Most DWARF readers
454 seem to a hash table to map abbrev ID's to abbrev entries.
455 However, we primarily care about GCC, and GCC simply issues ID's in
456 numerical order starting at 1. So we simply keep a sorted vector,
457 and try to just look up the code. */
458
459struct abbrevs
460{
461 /* The number of abbrevs in the vector. */
462 size_t num_abbrevs;
463 /* The abbrevs, sorted by the code field. */
464 struct abbrev *abbrevs;
465};
466
467/* The different kinds of attribute values. */
468
469enum attr_val_encoding
470{
471 /* No attribute value. */
472 ATTR_VAL_NONE,
473 /* An address. */
474 ATTR_VAL_ADDRESS,
475 /* An index into the .debug_addr section, whose value is relative to
476 * the DW_AT_addr_base attribute of the compilation unit. */
477 ATTR_VAL_ADDRESS_INDEX,
478 /* A unsigned integer. */
479 ATTR_VAL_UINT,
480 /* A sigd integer. */
481 ATTR_VAL_SINT,
482 /* A string. */
483 ATTR_VAL_STRING,
484 /* An index into the .debug_str_offsets section. */
485 ATTR_VAL_STRING_INDEX,
486 /* An offset to other data in the containing unit. */
487 ATTR_VAL_REF_UNIT,
488 /* An offset to other data within the .debug_info section. */
489 ATTR_VAL_REF_INFO,
490 /* An offset to other data within the alt .debug_info section. */
491 ATTR_VAL_REF_ALT_INFO,
492 /* An offset to data in some other section. */
493 ATTR_VAL_REF_SECTION,
494 /* A type signature. */
495 ATTR_VAL_REF_TYPE,
496 /* An index into the .debug_rnglists section. */
497 ATTR_VAL_RNGLISTS_INDEX,
498 /* A block of data (not represented). */
499 ATTR_VAL_BLOCK,
500 /* An expression (not represented). */
501 ATTR_VAL_EXPR,
502};
503
504/* An attribute value. */
505
506struct attr_val
507{
508 /* How the value is stored in the field u. */
509 enum attr_val_encoding encoding;
510 union
511 {
512 /* ATTR_VAL_ADDRESS*, ATTR_VAL_UINT, ATTR_VAL_REF*. */
513 uint64_t uint;
514 /* ATTR_VAL_SINT. */
515 int64_t sint;
516 /* ATTR_VAL_STRING. */
517 const char *string;
518 /* ATTR_VAL_BLOCK not stored. */
519 } u;
520};
521
522/* The line number program header. */
523
524struct line_header
525{
526 /* The version of the line number information. */
527 int version;
528 /* Address size. */
529 int addrsize;
530 /* The minimum instruction length. */
531 unsigned int min_insn_len;
532 /* The maximum number of ops per instruction. */
533 unsigned int max_ops_per_insn;
534 /* The line base for special opcodes. */
535 int line_base;
536 /* The line range for special opcodes. */
537 unsigned int line_range;
538 /* The opcode base--the first special opcode. */
539 unsigned int opcode_base;
540 /* Opcode lengths, indexed by opcode - 1. */
541 const unsigned char *opcode_lengths;
542 /* The number of directory entries. */
543 size_t dirs_count;
544 /* The directory entries. */
545 const char **dirs;
546 /* The number of filenames. */
547 size_t filenames_count;
548 /* The filenames. */
549 const char **filenames;
550};
551
552/* A format description from a line header. */
553
554struct line_header_format
555{
556 int lnct; /* LNCT code. */
557 enum dwarf_form form; /* Form of entry data. */
558};
559
560/* Map a single PC value to a file/line. We will keep a vector of
561 these sorted by PC value. Each file/line will be correct from the
562 PC up to the PC of the next entry if there is one. We allocate one
563 extra entry at the end so that we can use bsearch. */
564
565struct line
566{
567 /* PC. */
568 uintptr_t pc;
569 /* File name. Many entries in the array are expected to point to
570 the same file name. */
571 const char *filename;
572 /* Line number. */
573 int lineno;
574 /* Index of the object in the original array read from the DWARF
575 section, before it has been sorted. The index makes it possible
576 to use Quicksort and maintain stability. */
577 int idx;
578};
579
580/* A growable vector of line number information. This is used while
581 reading the line numbers. */
582
583struct line_vector
584{
585 /* Memory. This is an array of struct line. */
586 struct backtrace_vector vec;
587 /* Number of valid mappings. */
588 size_t count;
589};
590
591/* A function described in the debug info. */
592
593struct function
594{
595 /* The name of the function. */
596 const char *name;
597 /* If this is an inlined function, the filename of the call
598 site. */
599 const char *caller_filename;
600 /* If this is an inlined function, the line number of the call
601 site. */
602 int caller_lineno;
603 /* Map PC ranges to inlined functions. */
604 struct function_addrs *function_addrs;
605 size_t function_addrs_count;
606};
607
608/* An address range for a function. This maps a PC value to a
609 specific function. */
610
611struct function_addrs
612{
613 /* Range is LOW <= PC < HIGH. */
614 uint64_t low;
615 uint64_t high;
616 /* Function for this address range. */
617 struct function *function;
618};
619
620/* A growable vector of function address ranges. */
621
622struct function_vector
623{
624 /* Memory. This is an array of struct function_addrs. */
625 struct backtrace_vector vec;
626 /* Number of address ranges present. */
627 size_t count;
628};
629
630/* A DWARF compilation unit. This only holds the information we need
631 to map a PC to a file and line. */
632
633struct unit
634{
635 /* The first entry for this compilation unit. */
636 const unsigned char *unit_data;
637 /* The length of the data for this compilation unit. */
638 size_t unit_data_len;
639 /* The offset of UNIT_DATA from the start of the information for
640 this compilation unit. */
641 size_t unit_data_offset;
642 /* Offset of the start of the compilation unit from the start of the
643 .debug_info section. */
644 size_t low_offset;
645 /* Offset of the end of the compilation unit from the start of the
646 .debug_info section. */
647 size_t high_offset;
648 /* DWARF version. */
649 int version;
650 /* Whether unit is DWARF64. */
651 int is_dwarf64;
652 /* Address size. */
653 int addrsize;
654 /* Offset into line number information. */
655 off_t lineoff;
656 /* Offset of compilation unit in .debug_str_offsets. */
657 uint64_t str_offsets_base;
658 /* Offset of compilation unit in .debug_addr. */
659 uint64_t addr_base;
660 /* Offset of compilation unit in .debug_rnglists. */
661 uint64_t rnglists_base;
662 /* Primary source file. */
663 const char *filename;
664 /* Compilation command working directory. */
665 const char *comp_dir;
666 /* Absolute file name, only set if needed. */
667 const char *abs_filename;
668 /* The abbreviations for this unit. */
669 struct abbrevs abbrevs;
670
671 /* The fields above this point are read in during initialization and
672 may be accessed freely. The fields below this point are read in
673 as needed, and therefore require care, as different threads may
674 try to initialize them simultaneously. */
675
676 /* PC to line number mapping. This is NULL if the values have not
677 been read. This is (struct line *) -1 if there was an error
678 reading the values. */
679 struct line *lines;
680 /* Number of entries in lines. */
681 size_t lines_count;
682 /* PC ranges to function. */
683 struct function_addrs *function_addrs;
684 size_t function_addrs_count;
685};
686
687/* An address range for a compilation unit. This maps a PC value to a
688 specific compilation unit. Note that we invert the representation
689 in DWARF: instead of listing the units and attaching a list of
690 ranges, we list the ranges and have each one point to the unit.
691 This lets us do a binary search to find the unit. */
692
693struct unit_addrs
694{
695 /* Range is LOW <= PC < HIGH. */
696 uint64_t low;
697 uint64_t high;
698 /* Compilation unit for this address range. */
699 struct unit *u;
700};
701
702/* A growable vector of compilation unit address ranges. */
703
704struct unit_addrs_vector
705{
706 /* Memory. This is an array of struct unit_addrs. */
707 struct backtrace_vector vec;
708 /* Number of address ranges present. */
709 size_t count;
710};
711
712/* A growable vector of compilation unit pointer. */
713
714struct unit_vector
715{
716 struct backtrace_vector vec;
717 size_t count;
718};
719
720/* The information we need to map a PC to a file and line. */
721
722struct dwarf_data
723{
724 /* The data for the next file we know about. */
725 struct dwarf_data *next;
726 /* The data for .gnu_debugaltlink. */
727 struct dwarf_data *altlink;
728 /* The base address for this file. */
729 uintptr_t base_address;
730 /* A sorted list of address ranges. */
731 struct unit_addrs *addrs;
732 /* Number of address ranges in list. */
733 size_t addrs_count;
734 /* A sorted list of units. */
735 struct unit **units;
736 /* Number of units in the list. */
737 size_t units_count;
738 /* The unparsed DWARF debug data. */
739 struct dwarf_sections dwarf_sections;
740 /* Whether the data is big-endian or not. */
741 int is_bigendian;
742 /* A vector used for function addresses. We keep this here so that
743 we can grow the vector as we read more functions. */
744 struct function_vector fvec;
745};
746
747/* Report an error for a DWARF buffer. */
748
749static void
750dwarf_buf_error (struct dwarf_buf *buf, const char *msg, int errnum)
751{
752 char b[200];
753
754 snprintf (b, sizeof b, "%s in %s at %d",
755 msg, buf->name, (int) (buf->buf - buf->start));
756 buf->error_callback (buf->data, b, errnum);
757}
758
759/* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on
760 error. */
761
762static int
763require (struct dwarf_buf *buf, size_t count)
764{
765 if (buf->left >= count)
766 return 1;
767
768 if (!buf->reported_underflow)
769 {
770 dwarf_buf_error (buf, "DWARF underflow", 0);
771 buf->reported_underflow = 1;
772 }
773
774 return 0;
775}
776
777/* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on
778 error. */
779
780static int
781advance (struct dwarf_buf *buf, size_t count)
782{
783 if (!require (buf, count))
784 return 0;
785 buf->buf += count;
786 buf->left -= count;
787 return 1;
788}
789
790/* Read one zero-terminated string from BUF and advance past the string. */
791
792static const char *
793read_string (struct dwarf_buf *buf)
794{
795 const char *p = (const char *)buf->buf;
796 size_t len = strnlen (p, buf->left);
797
798 /* - If len == left, we ran out of buffer before finding the zero terminator.
799 Generate an error by advancing len + 1.
800 - If len < left, advance by len + 1 to skip past the zero terminator. */
801 size_t count = len + 1;
802
803 if (!advance (buf, count))
804 return NULL;
805
806 return p;
807}
808
809/* Read one byte from BUF and advance 1 byte. */
810
811static unsigned char
812read_byte (struct dwarf_buf *buf)
813{
814 const unsigned char *p = buf->buf;
815
816 if (!advance (buf, 1))
817 return 0;
818 return p[0];
819}
820
821/* Read a signed char from BUF and advance 1 byte. */
822
823static signed char
824read_sbyte (struct dwarf_buf *buf)
825{
826 const unsigned char *p = buf->buf;
827
828 if (!advance (buf, 1))
829 return 0;
830 return (*p ^ 0x80) - 0x80;
831}
832
833/* Read a uint16 from BUF and advance 2 bytes. */
834
835static uint16_t
836read_uint16 (struct dwarf_buf *buf)
837{
838 const unsigned char *p = buf->buf;
839
840 if (!advance (buf, 2))
841 return 0;
842 if (buf->is_bigendian)
843 return ((uint16_t) p[0] << 8) | (uint16_t) p[1];
844 else
845 return ((uint16_t) p[1] << 8) | (uint16_t) p[0];
846}
847
848/* Read a 24 bit value from BUF and advance 3 bytes. */
849
850static uint32_t
851read_uint24 (struct dwarf_buf *buf)
852{
853 const unsigned char *p = buf->buf;
854
855 if (!advance (buf, 3))
856 return 0;
857 if (buf->is_bigendian)
858 return (((uint32_t) p[0] << 16) | ((uint32_t) p[1] << 8)
859 | (uint32_t) p[2]);
860 else
861 return (((uint32_t) p[2] << 16) | ((uint32_t) p[1] << 8)
862 | (uint32_t) p[0]);
863}
864
865/* Read a uint32 from BUF and advance 4 bytes. */
866
867static uint32_t
868read_uint32 (struct dwarf_buf *buf)
869{
870 const unsigned char *p = buf->buf;
871
872 if (!advance (buf, 4))
873 return 0;
874 if (buf->is_bigendian)
875 return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16)
876 | ((uint32_t) p[2] << 8) | (uint32_t) p[3]);
877 else
878 return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16)
879 | ((uint32_t) p[1] << 8) | (uint32_t) p[0]);
880}
881
882/* Read a uint64 from BUF and advance 8 bytes. */
883
884static uint64_t
885read_uint64 (struct dwarf_buf *buf)
886{
887 const unsigned char *p = buf->buf;
888
889 if (!advance (buf, 8))
890 return 0;
891 if (buf->is_bigendian)
892 return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48)
893 | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32)
894 | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16)
895 | ((uint64_t) p[6] << 8) | (uint64_t) p[7]);
896 else
897 return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48)
898 | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32)
899 | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16)
900 | ((uint64_t) p[1] << 8) | (uint64_t) p[0]);
901}
902
903/* Read an offset from BUF and advance the appropriate number of
904 bytes. */
905
906static uint64_t
907read_offset (struct dwarf_buf *buf, int is_dwarf64)
908{
909 if (is_dwarf64)
910 return read_uint64 (buf);
911 else
912 return read_uint32 (buf);
913}
914
915/* Read an address from BUF and advance the appropriate number of
916 bytes. */
917
918static uint64_t
919read_address (struct dwarf_buf *buf, int addrsize)
920{
921 switch (addrsize)
922 {
923 case 1:
924 return read_byte (buf);
925 case 2:
926 return read_uint16 (buf);
927 case 4:
928 return read_uint32 (buf);
929 case 8:
930 return read_uint64 (buf);
931 default:
932 dwarf_buf_error (buf, "unrecognized address size", 0);
933 return 0;
934 }
935}
936
937/* Return whether a value is the highest possible address, given the
938 address size. */
939
940static int
941is_highest_address (uint64_t address, int addrsize)
942{
943 switch (addrsize)
944 {
945 case 1:
946 return address == (unsigned char) -1;
947 case 2:
948 return address == (uint16_t) -1;
949 case 4:
950 return address == (uint32_t) -1;
951 case 8:
952 return address == (uint64_t) -1;
953 default:
954 return 0;
955 }
956}
957
958/* Read an unsigned LEB128 number. */
959
960static uint64_t
961read_uleb128 (struct dwarf_buf *buf)
962{
963 uint64_t ret;
964 unsigned int shift;
965 int overflow;
966 unsigned char b;
967
968 ret = 0;
969 shift = 0;
970 overflow = 0;
971 do
972 {
973 const unsigned char *p;
974
975 p = buf->buf;
976 if (!advance (buf, 1))
977 return 0;
978 b = *p;
979 if (shift < 64)
980 ret |= ((uint64_t) (b & 0x7f)) << shift;
981 else if (!overflow)
982 {
983 dwarf_buf_error (buf, "LEB128 overflows uint64_t", 0);
984 overflow = 1;
985 }
986 shift += 7;
987 }
988 while ((b & 0x80) != 0);
989
990 return ret;
991}
992
993/* Read a signed LEB128 number. */
994
995static int64_t
996read_sleb128 (struct dwarf_buf *buf)
997{
998 uint64_t val;
999 unsigned int shift;
1000 int overflow;
1001 unsigned char b;
1002
1003 val = 0;
1004 shift = 0;
1005 overflow = 0;
1006 do
1007 {
1008 const unsigned char *p;
1009
1010 p = buf->buf;
1011 if (!advance (buf, 1))
1012 return 0;
1013 b = *p;
1014 if (shift < 64)
1015 val |= ((uint64_t) (b & 0x7f)) << shift;
1016 else if (!overflow)
1017 {
1018 dwarf_buf_error (buf, "signed LEB128 overflows uint64_t", 0);
1019 overflow = 1;
1020 }
1021 shift += 7;
1022 }
1023 while ((b & 0x80) != 0);
1024
1025 if ((b & 0x40) != 0 && shift < 64)
1026 val |= ((uint64_t) -1) << shift;
1027
1028 return (int64_t) val;
1029}
1030
1031/* Return the length of an LEB128 number. */
1032
1033static size_t
1034leb128_len (const unsigned char *p)
1035{
1036 size_t ret;
1037
1038 ret = 1;
1039 while ((*p & 0x80) != 0)
1040 {
1041 ++p;
1042 ++ret;
1043 }
1044 return ret;
1045}
1046
1047/* Read initial_length from BUF and advance the appropriate number of bytes. */
1048
1049static uint64_t
1050read_initial_length (struct dwarf_buf *buf, int *is_dwarf64)
1051{
1052 uint64_t len;
1053
1054 len = read_uint32 (buf);
1055 if (len == 0xffffffff)
1056 {
1057 len = read_uint64 (buf);
1058 *is_dwarf64 = 1;
1059 }
1060 else
1061 *is_dwarf64 = 0;
1062
1063 return len;
1064}
1065
1066/* Free an abbreviations structure. */
1067
1068static void
1069free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs,
1070 backtrace_error_callback error_callback, void *data)
1071{
1072 size_t i;
1073
1074 for (i = 0; i < abbrevs->num_abbrevs; ++i)
1075 backtrace_free (state, abbrevs->abbrevs[i].attrs,
1076 abbrevs->abbrevs[i].num_attrs * sizeof (struct attr),
1077 error_callback, data);
1078 backtrace_free (state, abbrevs->abbrevs,
1079 abbrevs->num_abbrevs * sizeof (struct abbrev),
1080 error_callback, data);
1081 abbrevs->num_abbrevs = 0;
1082 abbrevs->abbrevs = NULL;
1083}
1084
1085/* Read an attribute value. Returns 1 on success, 0 on failure. If
1086 the value can be represented as a uint64_t, sets *VAL and sets
1087 *IS_VALID to 1. We don't try to store the value of other attribute
1088 forms, because we don't care about them. */
1089
1090static int
1091read_attribute (enum dwarf_form form, uint64_t implicit_val,
1092 struct dwarf_buf *buf, int is_dwarf64, int version,
1093 int addrsize, const struct dwarf_sections *dwarf_sections,
1094 struct dwarf_data *altlink, struct attr_val *val)
1095{
1096 /* Avoid warnings about val.u.FIELD may be used uninitialized if
1097 this function is inlined. The warnings aren't valid but can
1098 occur because the different fields are set and used
1099 conditionally. */
1100 memset (val, 0, sizeof *val);
1101
1102 switch (form)
1103 {
1104 case DW_FORM_addr:
1105 val->encoding = ATTR_VAL_ADDRESS;
1106 val->u.uint = read_address (buf, addrsize);
1107 return 1;
1108 case DW_FORM_block2:
1109 val->encoding = ATTR_VAL_BLOCK;
1110 return advance (buf, read_uint16 (buf));
1111 case DW_FORM_block4:
1112 val->encoding = ATTR_VAL_BLOCK;
1113 return advance (buf, read_uint32 (buf));
1114 case DW_FORM_data2:
1115 val->encoding = ATTR_VAL_UINT;
1116 val->u.uint = read_uint16 (buf);
1117 return 1;
1118 case DW_FORM_data4:
1119 val->encoding = ATTR_VAL_UINT;
1120 val->u.uint = read_uint32 (buf);
1121 return 1;
1122 case DW_FORM_data8:
1123 val->encoding = ATTR_VAL_UINT;
1124 val->u.uint = read_uint64 (buf);
1125 return 1;
1126 case DW_FORM_data16:
1127 val->encoding = ATTR_VAL_BLOCK;
1128 return advance (buf, 16);
1129 case DW_FORM_string:
1130 val->encoding = ATTR_VAL_STRING;
1131 val->u.string = read_string (buf);
1132 return val->u.string == NULL ? 0 : 1;
1133 case DW_FORM_block:
1134 val->encoding = ATTR_VAL_BLOCK;
1135 return advance (buf, read_uleb128 (buf));
1136 case DW_FORM_block1:
1137 val->encoding = ATTR_VAL_BLOCK;
1138 return advance (buf, read_byte (buf));
1139 case DW_FORM_data1:
1140 val->encoding = ATTR_VAL_UINT;
1141 val->u.uint = read_byte (buf);
1142 return 1;
1143 case DW_FORM_flag:
1144 val->encoding = ATTR_VAL_UINT;
1145 val->u.uint = read_byte (buf);
1146 return 1;
1147 case DW_FORM_sdata:
1148 val->encoding = ATTR_VAL_SINT;
1149 val->u.sint = read_sleb128 (buf);
1150 return 1;
1151 case DW_FORM_strp:
1152 {
1153 uint64_t offset;
1154
1155 offset = read_offset (buf, is_dwarf64);
1156 if (offset >= dwarf_sections->size[DEBUG_STR])
1157 {
1158 dwarf_buf_error (buf, "DW_FORM_strp out of range", 0);
1159 return 0;
1160 }
1161 val->encoding = ATTR_VAL_STRING;
1162 val->u.string =
1163 (const char *) dwarf_sections->data[DEBUG_STR] + offset;
1164 return 1;
1165 }
1166 case DW_FORM_line_strp:
1167 {
1168 uint64_t offset;
1169
1170 offset = read_offset (buf, is_dwarf64);
1171 if (offset >= dwarf_sections->size[DEBUG_LINE_STR])
1172 {
1173 dwarf_buf_error (buf, "DW_FORM_line_strp out of range", 0);
1174 return 0;
1175 }
1176 val->encoding = ATTR_VAL_STRING;
1177 val->u.string =
1178 (const char *) dwarf_sections->data[DEBUG_LINE_STR] + offset;
1179 return 1;
1180 }
1181 case DW_FORM_udata:
1182 val->encoding = ATTR_VAL_UINT;
1183 val->u.uint = read_uleb128 (buf);
1184 return 1;
1185 case DW_FORM_ref_addr:
1186 val->encoding = ATTR_VAL_REF_INFO;
1187 if (version == 2)
1188 val->u.uint = read_address (buf, addrsize);
1189 else
1190 val->u.uint = read_offset (buf, is_dwarf64);
1191 return 1;
1192 case DW_FORM_ref1:
1193 val->encoding = ATTR_VAL_REF_UNIT;
1194 val->u.uint = read_byte (buf);
1195 return 1;
1196 case DW_FORM_ref2:
1197 val->encoding = ATTR_VAL_REF_UNIT;
1198 val->u.uint = read_uint16 (buf);
1199 return 1;
1200 case DW_FORM_ref4:
1201 val->encoding = ATTR_VAL_REF_UNIT;
1202 val->u.uint = read_uint32 (buf);
1203 return 1;
1204 case DW_FORM_ref8:
1205 val->encoding = ATTR_VAL_REF_UNIT;
1206 val->u.uint = read_uint64 (buf);
1207 return 1;
1208 case DW_FORM_ref_udata:
1209 val->encoding = ATTR_VAL_REF_UNIT;
1210 val->u.uint = read_uleb128 (buf);
1211 return 1;
1212 case DW_FORM_indirect:
1213 {
1214 uint64_t form;
1215
1216 form = read_uleb128 (buf);
1217 if (form == DW_FORM_implicit_const)
1218 {
1219 dwarf_buf_error (buf,
1220 "DW_FORM_indirect to DW_FORM_implicit_const",
1221 0);
1222 return 0;
1223 }
1224 return read_attribute ((enum dwarf_form) form, 0, buf, is_dwarf64,
1225 version, addrsize, dwarf_sections, altlink,
1226 val);
1227 }
1228 case DW_FORM_sec_offset:
1229 val->encoding = ATTR_VAL_REF_SECTION;
1230 val->u.uint = read_offset (buf, is_dwarf64);
1231 return 1;
1232 case DW_FORM_exprloc:
1233 val->encoding = ATTR_VAL_EXPR;
1234 return advance (buf, read_uleb128 (buf));
1235 case DW_FORM_flag_present:
1236 val->encoding = ATTR_VAL_UINT;
1237 val->u.uint = 1;
1238 return 1;
1239 case DW_FORM_ref_sig8:
1240 val->encoding = ATTR_VAL_REF_TYPE;
1241 val->u.uint = read_uint64 (buf);
1242 return 1;
1243 case DW_FORM_strx: case DW_FORM_strx1: case DW_FORM_strx2:
1244 case DW_FORM_strx3: case DW_FORM_strx4:
1245 {
1246 uint64_t offset;
1247
1248 switch (form)
1249 {
1250 case DW_FORM_strx:
1251 offset = read_uleb128 (buf);
1252 break;
1253 case DW_FORM_strx1:
1254 offset = read_byte (buf);
1255 break;
1256 case DW_FORM_strx2:
1257 offset = read_uint16 (buf);
1258 break;
1259 case DW_FORM_strx3:
1260 offset = read_uint24 (buf);
1261 break;
1262 case DW_FORM_strx4:
1263 offset = read_uint32 (buf);
1264 break;
1265 default:
1266 /* This case can't happen. */
1267 return 0;
1268 }
1269 val->encoding = ATTR_VAL_STRING_INDEX;
1270 val->u.uint = offset;
1271 return 1;
1272 }
1273 case DW_FORM_addrx: case DW_FORM_addrx1: case DW_FORM_addrx2:
1274 case DW_FORM_addrx3: case DW_FORM_addrx4:
1275 {
1276 uint64_t offset;
1277
1278 switch (form)
1279 {
1280 case DW_FORM_addrx:
1281 offset = read_uleb128 (buf);
1282 break;
1283 case DW_FORM_addrx1:
1284 offset = read_byte (buf);
1285 break;
1286 case DW_FORM_addrx2:
1287 offset = read_uint16 (buf);
1288 break;
1289 case DW_FORM_addrx3:
1290 offset = read_uint24 (buf);
1291 break;
1292 case DW_FORM_addrx4:
1293 offset = read_uint32 (buf);
1294 break;
1295 default:
1296 /* This case can't happen. */
1297 return 0;
1298 }
1299 val->encoding = ATTR_VAL_ADDRESS_INDEX;
1300 val->u.uint = offset;
1301 return 1;
1302 }
1303 case DW_FORM_ref_sup4:
1304 val->encoding = ATTR_VAL_REF_SECTION;
1305 val->u.uint = read_uint32 (buf);
1306 return 1;
1307 case DW_FORM_ref_sup8:
1308 val->encoding = ATTR_VAL_REF_SECTION;
1309 val->u.uint = read_uint64 (buf);
1310 return 1;
1311 case DW_FORM_implicit_const:
1312 val->encoding = ATTR_VAL_UINT;
1313 val->u.uint = implicit_val;
1314 return 1;
1315 case DW_FORM_loclistx:
1316 /* We don't distinguish this from DW_FORM_sec_offset. It
1317 * shouldn't matter since we don't care about loclists. */
1318 val->encoding = ATTR_VAL_REF_SECTION;
1319 val->u.uint = read_uleb128 (buf);
1320 return 1;
1321 case DW_FORM_rnglistx:
1322 val->encoding = ATTR_VAL_RNGLISTS_INDEX;
1323 val->u.uint = read_uleb128 (buf);
1324 return 1;
1325 case DW_FORM_GNU_addr_index:
1326 val->encoding = ATTR_VAL_REF_SECTION;
1327 val->u.uint = read_uleb128 (buf);
1328 return 1;
1329 case DW_FORM_GNU_str_index:
1330 val->encoding = ATTR_VAL_REF_SECTION;
1331 val->u.uint = read_uleb128 (buf);
1332 return 1;
1333 case DW_FORM_GNU_ref_alt:
1334 val->u.uint = read_offset (buf, is_dwarf64);
1335 if (altlink == NULL)
1336 {
1337 val->encoding = ATTR_VAL_NONE;
1338 return 1;
1339 }
1340 val->encoding = ATTR_VAL_REF_ALT_INFO;
1341 return 1;
1342 case DW_FORM_strp_sup: case DW_FORM_GNU_strp_alt:
1343 {
1344 uint64_t offset;
1345
1346 offset = read_offset (buf, is_dwarf64);
1347 if (altlink == NULL)
1348 {
1349 val->encoding = ATTR_VAL_NONE;
1350 return 1;
1351 }
1352 if (offset >= altlink->dwarf_sections.size[DEBUG_STR])
1353 {
1354 dwarf_buf_error (buf, "DW_FORM_strp_sup out of range", 0);
1355 return 0;
1356 }
1357 val->encoding = ATTR_VAL_STRING;
1358 val->u.string =
1359 (const char *) altlink->dwarf_sections.data[DEBUG_STR] + offset;
1360 return 1;
1361 }
1362 default:
1363 dwarf_buf_error (buf, "unrecognized DWARF form", -1);
1364 return 0;
1365 }
1366}
1367
1368/* If we can determine the value of a string attribute, set *STRING to
1369 point to the string. Return 1 on success, 0 on error. If we don't
1370 know the value, we consider that a success, and we don't change
1371 *STRING. An error is only reported for some sort of out of range
1372 offset. */
1373
1374static int
1375resolve_string (const struct dwarf_sections *dwarf_sections, int is_dwarf64,
1376 int is_bigendian, uint64_t str_offsets_base,
1377 const struct attr_val *val,
1378 backtrace_error_callback error_callback, void *data,
1379 const char **string)
1380{
1381 switch (val->encoding)
1382 {
1383 case ATTR_VAL_STRING:
1384 *string = val->u.string;
1385 return 1;
1386
1387 case ATTR_VAL_STRING_INDEX:
1388 {
1389 uint64_t offset;
1390 struct dwarf_buf offset_buf;
1391
1392 offset = val->u.uint * (is_dwarf64 ? 8 : 4) + str_offsets_base;
1393 if (offset + (is_dwarf64 ? 8 : 4)
1394 > dwarf_sections->size[DEBUG_STR_OFFSETS])
1395 {
1396 error_callback (data, "DW_FORM_strx value out of range", 0);
1397 return 0;
1398 }
1399
1400 offset_buf.name = ".debug_str_offsets";
1401 offset_buf.start = dwarf_sections->data[DEBUG_STR_OFFSETS];
1402 offset_buf.buf = dwarf_sections->data[DEBUG_STR_OFFSETS] + offset;
1403 offset_buf.left = dwarf_sections->size[DEBUG_STR_OFFSETS] - offset;
1404 offset_buf.is_bigendian = is_bigendian;
1405 offset_buf.error_callback = error_callback;
1406 offset_buf.data = data;
1407 offset_buf.reported_underflow = 0;
1408
1409 offset = read_offset (&offset_buf, is_dwarf64);
1410 if (offset >= dwarf_sections->size[DEBUG_STR])
1411 {
1412 dwarf_buf_error (&offset_buf,
1413 "DW_FORM_strx offset out of range",
1414 0);
1415 return 0;
1416 }
1417 *string = (const char *) dwarf_sections->data[DEBUG_STR] + offset;
1418 return 1;
1419 }
1420
1421 default:
1422 return 1;
1423 }
1424}
1425
1426/* Set *ADDRESS to the real address for a ATTR_VAL_ADDRESS_INDEX.
1427 Return 1 on success, 0 on error. */
1428
1429static int
1430resolve_addr_index (const struct dwarf_sections *dwarf_sections,
1431 uint64_t addr_base, int addrsize, int is_bigendian,
1432 uint64_t addr_index,
1433 backtrace_error_callback error_callback, void *data,
1434 uint64_t *address)
1435{
1436 uint64_t offset;
1437 struct dwarf_buf addr_buf;
1438
1439 offset = addr_index * addrsize + addr_base;
1440 if (offset + addrsize > dwarf_sections->size[DEBUG_ADDR])
1441 {
1442 error_callback (data, "DW_FORM_addrx value out of range", 0);
1443 return 0;
1444 }
1445
1446 addr_buf.name = ".debug_addr";
1447 addr_buf.start = dwarf_sections->data[DEBUG_ADDR];
1448 addr_buf.buf = dwarf_sections->data[DEBUG_ADDR] + offset;
1449 addr_buf.left = dwarf_sections->size[DEBUG_ADDR] - offset;
1450 addr_buf.is_bigendian = is_bigendian;
1451 addr_buf.error_callback = error_callback;
1452 addr_buf.data = data;
1453 addr_buf.reported_underflow = 0;
1454
1455 *address = read_address (&addr_buf, addrsize);
1456 return 1;
1457}
1458
1459/* Compare a unit offset against a unit for bsearch. */
1460
1461static int
1462units_search (const void *vkey, const void *ventry)
1463{
1464 const size_t *key = (const size_t *) vkey;
1465 const struct unit *entry = *((const struct unit *const *) ventry);
1466 size_t offset;
1467
1468 offset = *key;
1469 if (offset < entry->low_offset)
1470 return -1;
1471 else if (offset >= entry->high_offset)
1472 return 1;
1473 else
1474 return 0;
1475}
1476
1477/* Find a unit in PU containing OFFSET. */
1478
1479static struct unit *
1480find_unit (struct unit **pu, size_t units_count, size_t offset)
1481{
1482 struct unit **u;
1483 u = (struct unit**)bsearch (&offset, pu, units_count, sizeof (struct unit *), units_search);
1484 return u == NULL ? NULL : *u;
1485}
1486
1487/* Compare function_addrs for qsort. When ranges are nested, make the
1488 smallest one sort last. */
1489
1490static int
1491function_addrs_compare (const void *v1, const void *v2)
1492{
1493 const struct function_addrs *a1 = (const struct function_addrs *) v1;
1494 const struct function_addrs *a2 = (const struct function_addrs *) v2;
1495
1496 if (a1->low < a2->low)
1497 return -1;
1498 if (a1->low > a2->low)
1499 return 1;
1500 if (a1->high < a2->high)
1501 return 1;
1502 if (a1->high > a2->high)
1503 return -1;
1504 return strcmp (a1->function->name, a2->function->name);
1505}
1506
1507/* Compare a PC against a function_addrs for bsearch. We always
1508 allocate an entra entry at the end of the vector, so that this
1509 routine can safely look at the next entry. Note that if there are
1510 multiple ranges containing PC, which one will be returned is
1511 unpredictable. We compensate for that in dwarf_fileline. */
1512
1513static int
1514function_addrs_search (const void *vkey, const void *ventry)
1515{
1516 const uintptr_t *key = (const uintptr_t *) vkey;
1517 const struct function_addrs *entry = (const struct function_addrs *) ventry;
1518 uintptr_t pc;
1519
1520 pc = *key;
1521 if (pc < entry->low)
1522 return -1;
1523 else if (pc > (entry + 1)->low)
1524 return 1;
1525 else
1526 return 0;
1527}
1528
1529/* Add a new compilation unit address range to a vector. This is
1530 called via add_ranges. Returns 1 on success, 0 on failure. */
1531
1532static int
1533add_unit_addr (struct backtrace_state *state, void *rdata,
1534 uint64_t lowpc, uint64_t highpc,
1535 backtrace_error_callback error_callback, void *data,
1536 void *pvec)
1537{
1538 struct unit *u = (struct unit *) rdata;
1539 struct unit_addrs_vector *vec = (struct unit_addrs_vector *) pvec;
1540 struct unit_addrs *p;
1541
1542 /* Try to merge with the last entry. */
1543 if (vec->count > 0)
1544 {
1545 p = (struct unit_addrs *) vec->vec.base + (vec->count - 1);
1546 if ((lowpc == p->high || lowpc == p->high + 1)
1547 && u == p->u)
1548 {
1549 if (highpc > p->high)
1550 p->high = highpc;
1551 return 1;
1552 }
1553 }
1554
1555 p = ((struct unit_addrs *)
1556 backtrace_vector_grow (state, sizeof (struct unit_addrs),
1557 error_callback, data, &vec->vec));
1558 if (p == NULL)
1559 return 0;
1560
1561 p->low = lowpc;
1562 p->high = highpc;
1563 p->u = u;
1564
1565 ++vec->count;
1566
1567 return 1;
1568}
1569
1570/* Compare unit_addrs for qsort. When ranges are nested, make the
1571 smallest one sort last. */
1572
1573static int
1574unit_addrs_compare (const void *v1, const void *v2)
1575{
1576 const struct unit_addrs *a1 = (const struct unit_addrs *) v1;
1577 const struct unit_addrs *a2 = (const struct unit_addrs *) v2;
1578
1579 if (a1->low < a2->low)
1580 return -1;
1581 if (a1->low > a2->low)
1582 return 1;
1583 if (a1->high < a2->high)
1584 return 1;
1585 if (a1->high > a2->high)
1586 return -1;
1587 if (a1->u->lineoff < a2->u->lineoff)
1588 return -1;
1589 if (a1->u->lineoff > a2->u->lineoff)
1590 return 1;
1591 return 0;
1592}
1593
1594/* Compare a PC against a unit_addrs for bsearch. We always allocate
1595 an entry entry at the end of the vector, so that this routine can
1596 safely look at the next entry. Note that if there are multiple
1597 ranges containing PC, which one will be returned is unpredictable.
1598 We compensate for that in dwarf_fileline. */
1599
1600static int
1601unit_addrs_search (const void *vkey, const void *ventry)
1602{
1603 const uintptr_t *key = (const uintptr_t *) vkey;
1604 const struct unit_addrs *entry = (const struct unit_addrs *) ventry;
1605 uintptr_t pc;
1606
1607 pc = *key;
1608 if (pc < entry->low)
1609 return -1;
1610 else if (pc > (entry + 1)->low)
1611 return 1;
1612 else
1613 return 0;
1614}
1615
1616/* Sort the line vector by PC. We want a stable sort here to maintain
1617 the order of lines for the same PC values. Since the sequence is
1618 being sorted in place, their addresses cannot be relied on to
1619 maintain stability. That is the purpose of the index member. */
1620
1621static int
1622line_compare (const void *v1, const void *v2)
1623{
1624 const struct line *ln1 = (const struct line *) v1;
1625 const struct line *ln2 = (const struct line *) v2;
1626
1627 if (ln1->pc < ln2->pc)
1628 return -1;
1629 else if (ln1->pc > ln2->pc)
1630 return 1;
1631 else if (ln1->idx < ln2->idx)
1632 return -1;
1633 else if (ln1->idx > ln2->idx)
1634 return 1;
1635 else
1636 return 0;
1637}
1638
1639/* Find a PC in a line vector. We always allocate an extra entry at
1640 the end of the lines vector, so that this routine can safely look
1641 at the next entry. Note that when there are multiple mappings for
1642 the same PC value, this will return the last one. */
1643
1644static int
1645line_search (const void *vkey, const void *ventry)
1646{
1647 const uintptr_t *key = (const uintptr_t *) vkey;
1648 const struct line *entry = (const struct line *) ventry;
1649 uintptr_t pc;
1650
1651 pc = *key;
1652 if (pc < entry->pc)
1653 return -1;
1654 else if (pc >= (entry + 1)->pc)
1655 return 1;
1656 else
1657 return 0;
1658}
1659
1660/* Sort the abbrevs by the abbrev code. This function is passed to
1661 both qsort and bsearch. */
1662
1663static int
1664abbrev_compare (const void *v1, const void *v2)
1665{
1666 const struct abbrev *a1 = (const struct abbrev *) v1;
1667 const struct abbrev *a2 = (const struct abbrev *) v2;
1668
1669 if (a1->code < a2->code)
1670 return -1;
1671 else if (a1->code > a2->code)
1672 return 1;
1673 else
1674 {
1675 /* This really shouldn't happen. It means there are two
1676 different abbrevs with the same code, and that means we don't
1677 know which one lookup_abbrev should return. */
1678 return 0;
1679 }
1680}
1681
1682/* Read the abbreviation table for a compilation unit. Returns 1 on
1683 success, 0 on failure. */
1684
1685static int
1686read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset,
1687 const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size,
1688 int is_bigendian, backtrace_error_callback error_callback,
1689 void *data, struct abbrevs *abbrevs)
1690{
1691 struct dwarf_buf abbrev_buf;
1692 struct dwarf_buf count_buf;
1693 size_t num_abbrevs;
1694
1695 abbrevs->num_abbrevs = 0;
1696 abbrevs->abbrevs = NULL;
1697
1698 if (abbrev_offset >= dwarf_abbrev_size)
1699 {
1700 error_callback (data, "abbrev offset out of range", 0);
1701 return 0;
1702 }
1703
1704 abbrev_buf.name = ".debug_abbrev";
1705 abbrev_buf.start = dwarf_abbrev;
1706 abbrev_buf.buf = dwarf_abbrev + abbrev_offset;
1707 abbrev_buf.left = dwarf_abbrev_size - abbrev_offset;
1708 abbrev_buf.is_bigendian = is_bigendian;
1709 abbrev_buf.error_callback = error_callback;
1710 abbrev_buf.data = data;
1711 abbrev_buf.reported_underflow = 0;
1712
1713 /* Count the number of abbrevs in this list. */
1714
1715 count_buf = abbrev_buf;
1716 num_abbrevs = 0;
1717 while (read_uleb128 (&count_buf) != 0)
1718 {
1719 if (count_buf.reported_underflow)
1720 return 0;
1721 ++num_abbrevs;
1722 // Skip tag.
1723 read_uleb128 (&count_buf);
1724 // Skip has_children.
1725 read_byte (&count_buf);
1726 // Skip attributes.
1727 while (read_uleb128 (&count_buf) != 0)
1728 {
1729 uint64_t form;
1730
1731 form = read_uleb128 (&count_buf);
1732 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1733 read_sleb128 (&count_buf);
1734 }
1735 // Skip form of last attribute.
1736 read_uleb128 (&count_buf);
1737 }
1738
1739 if (count_buf.reported_underflow)
1740 return 0;
1741
1742 if (num_abbrevs == 0)
1743 return 1;
1744
1745 abbrevs->abbrevs = ((struct abbrev *)
1746 backtrace_alloc (state,
1747 num_abbrevs * sizeof (struct abbrev),
1748 error_callback, data));
1749 if (abbrevs->abbrevs == NULL)
1750 return 0;
1751 abbrevs->num_abbrevs = num_abbrevs;
1752 memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev));
1753
1754 num_abbrevs = 0;
1755 while (1)
1756 {
1757 uint64_t code;
1758 struct abbrev a;
1759 size_t num_attrs;
1760 struct attr *attrs;
1761
1762 if (abbrev_buf.reported_underflow)
1763 goto fail;
1764
1765 code = read_uleb128 (&abbrev_buf);
1766 if (code == 0)
1767 break;
1768
1769 a.code = code;
1770 a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf);
1771 a.has_children = read_byte (&abbrev_buf);
1772
1773 count_buf = abbrev_buf;
1774 num_attrs = 0;
1775 while (read_uleb128 (&count_buf) != 0)
1776 {
1777 uint64_t form;
1778
1779 ++num_attrs;
1780 form = read_uleb128 (&count_buf);
1781 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1782 read_sleb128 (&count_buf);
1783 }
1784
1785 if (num_attrs == 0)
1786 {
1787 attrs = NULL;
1788 read_uleb128 (&abbrev_buf);
1789 read_uleb128 (&abbrev_buf);
1790 }
1791 else
1792 {
1793 attrs = ((struct attr *)
1794 backtrace_alloc (state, num_attrs * sizeof *attrs,
1795 error_callback, data));
1796 if (attrs == NULL)
1797 goto fail;
1798 num_attrs = 0;
1799 while (1)
1800 {
1801 uint64_t name;
1802 uint64_t form;
1803
1804 name = read_uleb128 (&abbrev_buf);
1805 form = read_uleb128 (&abbrev_buf);
1806 if (name == 0)
1807 break;
1808 attrs[num_attrs].name = (enum dwarf_attribute) name;
1809 attrs[num_attrs].form = (enum dwarf_form) form;
1810 if ((enum dwarf_form) form == DW_FORM_implicit_const)
1811 attrs[num_attrs].val = read_sleb128 (&abbrev_buf);
1812 else
1813 attrs[num_attrs].val = 0;
1814 ++num_attrs;
1815 }
1816 }
1817
1818 a.num_attrs = num_attrs;
1819 a.attrs = attrs;
1820
1821 abbrevs->abbrevs[num_abbrevs] = a;
1822 ++num_abbrevs;
1823 }
1824
1825 backtrace_qsort (abbrevs->abbrevs, abbrevs->num_abbrevs,
1826 sizeof (struct abbrev), abbrev_compare);
1827
1828 return 1;
1829
1830 fail:
1831 free_abbrevs (state, abbrevs, error_callback, data);
1832 return 0;
1833}
1834
1835/* Return the abbrev information for an abbrev code. */
1836
1837static const struct abbrev *
1838lookup_abbrev (struct abbrevs *abbrevs, uint64_t code,
1839 backtrace_error_callback error_callback, void *data)
1840{
1841 struct abbrev key;
1842 void *p;
1843
1844 /* With GCC, where abbrevs are simply numbered in order, we should
1845 be able to just look up the entry. */
1846 if (code - 1 < abbrevs->num_abbrevs
1847 && abbrevs->abbrevs[code - 1].code == code)
1848 return &abbrevs->abbrevs[code - 1];
1849
1850 /* Otherwise we have to search. */
1851 memset (&key, 0, sizeof key);
1852 key.code = code;
1853 p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs,
1854 sizeof (struct abbrev), abbrev_compare);
1855 if (p == NULL)
1856 {
1857 error_callback (data, "invalid abbreviation code", 0);
1858 return NULL;
1859 }
1860 return (const struct abbrev *) p;
1861}
1862
1863/* This struct is used to gather address range information while
1864 reading attributes. We use this while building a mapping from
1865 address ranges to compilation units and then again while mapping
1866 from address ranges to function entries. Normally either
1867 lowpc/highpc is set or ranges is set. */
1868
1869struct pcrange {
1870 uint64_t lowpc; /* The low PC value. */
1871 int have_lowpc; /* Whether a low PC value was found. */
1872 int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */
1873 uint64_t highpc; /* The high PC value. */
1874 int have_highpc; /* Whether a high PC value was found. */
1875 int highpc_is_relative; /* Whether highpc is relative to lowpc. */
1876 int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */
1877 uint64_t ranges; /* Offset in ranges section. */
1878 int have_ranges; /* Whether ranges is valid. */
1879 int ranges_is_index; /* Whether ranges is DW_FORM_rnglistx. */
1880};
1881
1882/* Update PCRANGE from an attribute value. */
1883
1884static void
1885update_pcrange (const struct attr* attr, const struct attr_val* val,
1886 struct pcrange *pcrange)
1887{
1888 switch (attr->name)
1889 {
1890 case DW_AT_low_pc:
1891 if (val->encoding == ATTR_VAL_ADDRESS)
1892 {
1893 pcrange->lowpc = val->u.uint;
1894 pcrange->have_lowpc = 1;
1895 }
1896 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1897 {
1898 pcrange->lowpc = val->u.uint;
1899 pcrange->have_lowpc = 1;
1900 pcrange->lowpc_is_addr_index = 1;
1901 }
1902 break;
1903
1904 case DW_AT_high_pc:
1905 if (val->encoding == ATTR_VAL_ADDRESS)
1906 {
1907 pcrange->highpc = val->u.uint;
1908 pcrange->have_highpc = 1;
1909 }
1910 else if (val->encoding == ATTR_VAL_UINT)
1911 {
1912 pcrange->highpc = val->u.uint;
1913 pcrange->have_highpc = 1;
1914 pcrange->highpc_is_relative = 1;
1915 }
1916 else if (val->encoding == ATTR_VAL_ADDRESS_INDEX)
1917 {
1918 pcrange->highpc = val->u.uint;
1919 pcrange->have_highpc = 1;
1920 pcrange->highpc_is_addr_index = 1;
1921 }
1922 break;
1923
1924 case DW_AT_ranges:
1925 if (val->encoding == ATTR_VAL_UINT
1926 || val->encoding == ATTR_VAL_REF_SECTION)
1927 {
1928 pcrange->ranges = val->u.uint;
1929 pcrange->have_ranges = 1;
1930 }
1931 else if (val->encoding == ATTR_VAL_RNGLISTS_INDEX)
1932 {
1933 pcrange->ranges = val->u.uint;
1934 pcrange->have_ranges = 1;
1935 pcrange->ranges_is_index = 1;
1936 }
1937 break;
1938
1939 default:
1940 break;
1941 }
1942}
1943
1944/* Call ADD_RANGE for a low/high PC pair. Returns 1 on success, 0 on
1945 error. */
1946
1947static int
1948add_low_high_range (struct backtrace_state *state,
1949 const struct dwarf_sections *dwarf_sections,
1950 uintptr_t base_address, int is_bigendian,
1951 struct unit *u, const struct pcrange *pcrange,
1952 int (*add_range) (struct backtrace_state *state,
1953 void *rdata, uint64_t lowpc,
1954 uint64_t highpc,
1955 backtrace_error_callback error_callback,
1956 void *data, void *vec),
1957 void *rdata,
1958 backtrace_error_callback error_callback, void *data,
1959 void *vec)
1960{
1961 uint64_t lowpc;
1962 uint64_t highpc;
1963
1964 lowpc = pcrange->lowpc;
1965 if (pcrange->lowpc_is_addr_index)
1966 {
1967 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1968 is_bigendian, lowpc, error_callback, data,
1969 &lowpc))
1970 return 0;
1971 }
1972
1973 highpc = pcrange->highpc;
1974 if (pcrange->highpc_is_addr_index)
1975 {
1976 if (!resolve_addr_index (dwarf_sections, u->addr_base, u->addrsize,
1977 is_bigendian, highpc, error_callback, data,
1978 &highpc))
1979 return 0;
1980 }
1981 if (pcrange->highpc_is_relative)
1982 highpc += lowpc;
1983
1984 /* Add in the base address of the module when recording PC values,
1985 so that we can look up the PC directly. */
1986 lowpc += base_address;
1987 highpc += base_address;
1988
1989 return add_range (state, rdata, lowpc, highpc, error_callback, data, vec);
1990}
1991
1992/* Call ADD_RANGE for each range read from .debug_ranges, as used in
1993 DWARF versions 2 through 4. */
1994
1995static int
1996add_ranges_from_ranges (
1997 struct backtrace_state *state,
1998 const struct dwarf_sections *dwarf_sections,
1999 uintptr_t base_address, int is_bigendian,
2000 struct unit *u, uint64_t base,
2001 const struct pcrange *pcrange,
2002 int (*add_range) (struct backtrace_state *state, void *rdata,
2003 uint64_t lowpc, uint64_t highpc,
2004 backtrace_error_callback error_callback, void *data,
2005 void *vec),
2006 void *rdata,
2007 backtrace_error_callback error_callback, void *data,
2008 void *vec)
2009{
2010 struct dwarf_buf ranges_buf;
2011
2012 if (pcrange->ranges >= dwarf_sections->size[DEBUG_RANGES])
2013 {
2014 error_callback (data, "ranges offset out of range", 0);
2015 return 0;
2016 }
2017
2018 ranges_buf.name = ".debug_ranges";
2019 ranges_buf.start = dwarf_sections->data[DEBUG_RANGES];
2020 ranges_buf.buf = dwarf_sections->data[DEBUG_RANGES] + pcrange->ranges;
2021 ranges_buf.left = dwarf_sections->size[DEBUG_RANGES] - pcrange->ranges;
2022 ranges_buf.is_bigendian = is_bigendian;
2023 ranges_buf.error_callback = error_callback;
2024 ranges_buf.data = data;
2025 ranges_buf.reported_underflow = 0;
2026
2027 while (1)
2028 {
2029 uint64_t low;
2030 uint64_t high;
2031
2032 if (ranges_buf.reported_underflow)
2033 return 0;
2034
2035 low = read_address (&ranges_buf, u->addrsize);
2036 high = read_address (&ranges_buf, u->addrsize);
2037
2038 if (low == 0 && high == 0)
2039 break;
2040
2041 if (is_highest_address (low, u->addrsize))
2042 base = high;
2043 else
2044 {
2045 if (!add_range (state, rdata,
2046 low + base + base_address,
2047 high + base + base_address,
2048 error_callback, data, vec))
2049 return 0;
2050 }
2051 }
2052
2053 if (ranges_buf.reported_underflow)
2054 return 0;
2055
2056 return 1;
2057}
2058
2059/* Call ADD_RANGE for each range read from .debug_rnglists, as used in
2060 DWARF version 5. */
2061
2062static int
2063add_ranges_from_rnglists (
2064 struct backtrace_state *state,
2065 const struct dwarf_sections *dwarf_sections,
2066 uintptr_t base_address, int is_bigendian,
2067 struct unit *u, uint64_t base,
2068 const struct pcrange *pcrange,
2069 int (*add_range) (struct backtrace_state *state, void *rdata,
2070 uint64_t lowpc, uint64_t highpc,
2071 backtrace_error_callback error_callback, void *data,
2072 void *vec),
2073 void *rdata,
2074 backtrace_error_callback error_callback, void *data,
2075 void *vec)
2076{
2077 uint64_t offset;
2078 struct dwarf_buf rnglists_buf;
2079
2080 if (!pcrange->ranges_is_index)
2081 offset = pcrange->ranges;
2082 else
2083 offset = u->rnglists_base + pcrange->ranges * (u->is_dwarf64 ? 8 : 4);
2084 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
2085 {
2086 error_callback (data, "rnglists offset out of range", 0);
2087 return 0;
2088 }
2089
2090 rnglists_buf.name = ".debug_rnglists";
2091 rnglists_buf.start = dwarf_sections->data[DEBUG_RNGLISTS];
2092 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
2093 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
2094 rnglists_buf.is_bigendian = is_bigendian;
2095 rnglists_buf.error_callback = error_callback;
2096 rnglists_buf.data = data;
2097 rnglists_buf.reported_underflow = 0;
2098
2099 if (pcrange->ranges_is_index)
2100 {
2101 offset = read_offset (&rnglists_buf, u->is_dwarf64);
2102 offset += u->rnglists_base;
2103 if (offset >= dwarf_sections->size[DEBUG_RNGLISTS])
2104 {
2105 error_callback (data, "rnglists index offset out of range", 0);
2106 return 0;
2107 }
2108 rnglists_buf.buf = dwarf_sections->data[DEBUG_RNGLISTS] + offset;
2109 rnglists_buf.left = dwarf_sections->size[DEBUG_RNGLISTS] - offset;
2110 }
2111
2112 while (1)
2113 {
2114 unsigned char rle;
2115
2116 rle = read_byte (&rnglists_buf);
2117 if (rle == DW_RLE_end_of_list)
2118 break;
2119 switch (rle)
2120 {
2121 case DW_RLE_base_addressx:
2122 {
2123 uint64_t index;
2124
2125 index = read_uleb128 (&rnglists_buf);
2126 if (!resolve_addr_index (dwarf_sections, u->addr_base,
2127 u->addrsize, is_bigendian, index,
2128 error_callback, data, &base))
2129 return 0;
2130 }
2131 break;
2132
2133 case DW_RLE_startx_endx:
2134 {
2135 uint64_t index;
2136 uint64_t low;
2137 uint64_t high;
2138
2139 index = read_uleb128 (&rnglists_buf);
2140 if (!resolve_addr_index (dwarf_sections, u->addr_base,
2141 u->addrsize, is_bigendian, index,
2142 error_callback, data, &low))
2143 return 0;
2144 index = read_uleb128 (&rnglists_buf);
2145 if (!resolve_addr_index (dwarf_sections, u->addr_base,
2146 u->addrsize, is_bigendian, index,
2147 error_callback, data, &high))
2148 return 0;
2149 if (!add_range (state, rdata, low + base_address,
2150 high + base_address, error_callback, data,
2151 vec))
2152 return 0;
2153 }
2154 break;
2155
2156 case DW_RLE_startx_length:
2157 {
2158 uint64_t index;
2159 uint64_t low;
2160 uint64_t length;
2161
2162 index = read_uleb128 (&rnglists_buf);
2163 if (!resolve_addr_index (dwarf_sections, u->addr_base,
2164 u->addrsize, is_bigendian, index,
2165 error_callback, data, &low))
2166 return 0;
2167 length = read_uleb128 (&rnglists_buf);
2168 low += base_address;
2169 if (!add_range (state, rdata, low, low + length,
2170 error_callback, data, vec))
2171 return 0;
2172 }
2173 break;
2174
2175 case DW_RLE_offset_pair:
2176 {
2177 uint64_t low;
2178 uint64_t high;
2179
2180 low = read_uleb128 (&rnglists_buf);
2181 high = read_uleb128 (&rnglists_buf);
2182 if (!add_range (state, rdata, low + base + base_address,
2183 high + base + base_address,
2184 error_callback, data, vec))
2185 return 0;
2186 }
2187 break;
2188
2189 case DW_RLE_base_address:
2190 base = read_address (&rnglists_buf, u->addrsize);
2191 break;
2192
2193 case DW_RLE_start_end:
2194 {
2195 uint64_t low;
2196 uint64_t high;
2197
2198 low = read_address (&rnglists_buf, u->addrsize);
2199 high = read_address (&rnglists_buf, u->addrsize);
2200 if (!add_range (state, rdata, low + base_address,
2201 high + base_address, error_callback, data,
2202 vec))
2203 return 0;
2204 }
2205 break;
2206
2207 case DW_RLE_start_length:
2208 {
2209 uint64_t low;
2210 uint64_t length;
2211
2212 low = read_address (&rnglists_buf, u->addrsize);
2213 length = read_uleb128 (&rnglists_buf);
2214 low += base_address;
2215 if (!add_range (state, rdata, low, low + length,
2216 error_callback, data, vec))
2217 return 0;
2218 }
2219 break;
2220
2221 default:
2222 dwarf_buf_error (&rnglists_buf, "unrecognized DW_RLE value", -1);
2223 return 0;
2224 }
2225 }
2226
2227 if (rnglists_buf.reported_underflow)
2228 return 0;
2229
2230 return 1;
2231}
2232
2233/* Call ADD_RANGE for each lowpc/highpc pair in PCRANGE. RDATA is
2234 passed to ADD_RANGE, and is either a struct unit * or a struct
2235 function *. VEC is the vector we are adding ranges to, and is
2236 either a struct unit_addrs_vector * or a struct function_vector *.
2237 Returns 1 on success, 0 on error. */
2238
2239static int
2240add_ranges (struct backtrace_state *state,
2241 const struct dwarf_sections *dwarf_sections,
2242 uintptr_t base_address, int is_bigendian,
2243 struct unit *u, uint64_t base, const struct pcrange *pcrange,
2244 int (*add_range) (struct backtrace_state *state, void *rdata,
2245 uint64_t lowpc, uint64_t highpc,
2246 backtrace_error_callback error_callback,
2247 void *data, void *vec),
2248 void *rdata,
2249 backtrace_error_callback error_callback, void *data,
2250 void *vec)
2251{
2252 if (pcrange->have_lowpc && pcrange->have_highpc)
2253 return add_low_high_range (state, dwarf_sections, base_address,
2254 is_bigendian, u, pcrange, add_range, rdata,
2255 error_callback, data, vec);
2256
2257 if (!pcrange->have_ranges)
2258 {
2259 /* Did not find any address ranges to add. */
2260 return 1;
2261 }
2262
2263 if (u->version < 5)
2264 return add_ranges_from_ranges (state, dwarf_sections, base_address,
2265 is_bigendian, u, base, pcrange, add_range,
2266 rdata, error_callback, data, vec);
2267 else
2268 return add_ranges_from_rnglists (state, dwarf_sections, base_address,
2269 is_bigendian, u, base, pcrange, add_range,
2270 rdata, error_callback, data, vec);
2271}
2272
2273/* Find the address range covered by a compilation unit, reading from
2274 UNIT_BUF and adding values to U. Returns 1 if all data could be
2275 read, 0 if there is some error. */
2276
2277static int
2278find_address_ranges (struct backtrace_state *state, uintptr_t base_address,
2279 struct dwarf_buf *unit_buf,
2280 const struct dwarf_sections *dwarf_sections,
2281 int is_bigendian, struct dwarf_data *altlink,
2282 backtrace_error_callback error_callback, void *data,
2283 struct unit *u, struct unit_addrs_vector *addrs,
2284 enum dwarf_tag *unit_tag)
2285{
2286 while (unit_buf->left > 0)
2287 {
2288 uint64_t code;
2289 const struct abbrev *abbrev;
2290 struct pcrange pcrange;
2291 struct attr_val name_val;
2292 int have_name_val;
2293 struct attr_val comp_dir_val;
2294 int have_comp_dir_val;
2295 size_t i;
2296
2297 code = read_uleb128 (unit_buf);
2298 if (code == 0)
2299 return 1;
2300
2301 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
2302 if (abbrev == NULL)
2303 return 0;
2304
2305 if (unit_tag != NULL)
2306 *unit_tag = abbrev->tag;
2307
2308 memset (&pcrange, 0, sizeof pcrange);
2309 memset (&name_val, 0, sizeof name_val);
2310 have_name_val = 0;
2311 memset (&comp_dir_val, 0, sizeof comp_dir_val);
2312 have_comp_dir_val = 0;
2313 for (i = 0; i < abbrev->num_attrs; ++i)
2314 {
2315 struct attr_val val;
2316
2317 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
2318 unit_buf, u->is_dwarf64, u->version,
2319 u->addrsize, dwarf_sections, altlink, &val))
2320 return 0;
2321
2322 switch (abbrev->attrs[i].name)
2323 {
2324 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
2325 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
2326 break;
2327
2328 case DW_AT_stmt_list:
2329 if ((abbrev->tag == DW_TAG_compile_unit
2330 || abbrev->tag == DW_TAG_skeleton_unit)
2331 && (val.encoding == ATTR_VAL_UINT
2332 || val.encoding == ATTR_VAL_REF_SECTION))
2333 u->lineoff = val.u.uint;
2334 break;
2335
2336 case DW_AT_name:
2337 if (abbrev->tag == DW_TAG_compile_unit
2338 || abbrev->tag == DW_TAG_skeleton_unit)
2339 {
2340 name_val = val;
2341 have_name_val = 1;
2342 }
2343 break;
2344
2345 case DW_AT_comp_dir:
2346 if (abbrev->tag == DW_TAG_compile_unit
2347 || abbrev->tag == DW_TAG_skeleton_unit)
2348 {
2349 comp_dir_val = val;
2350 have_comp_dir_val = 1;
2351 }
2352 break;
2353
2354 case DW_AT_str_offsets_base:
2355 if ((abbrev->tag == DW_TAG_compile_unit
2356 || abbrev->tag == DW_TAG_skeleton_unit)
2357 && val.encoding == ATTR_VAL_REF_SECTION)
2358 u->str_offsets_base = val.u.uint;
2359 break;
2360
2361 case DW_AT_addr_base:
2362 if ((abbrev->tag == DW_TAG_compile_unit
2363 || abbrev->tag == DW_TAG_skeleton_unit)
2364 && val.encoding == ATTR_VAL_REF_SECTION)
2365 u->addr_base = val.u.uint;
2366 break;
2367
2368 case DW_AT_rnglists_base:
2369 if ((abbrev->tag == DW_TAG_compile_unit
2370 || abbrev->tag == DW_TAG_skeleton_unit)
2371 && val.encoding == ATTR_VAL_REF_SECTION)
2372 u->rnglists_base = val.u.uint;
2373 break;
2374
2375 default:
2376 break;
2377 }
2378 }
2379
2380 // Resolve strings after we're sure that we have seen
2381 // DW_AT_str_offsets_base.
2382 if (have_name_val)
2383 {
2384 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2385 u->str_offsets_base, &name_val,
2386 error_callback, data, &u->filename))
2387 return 0;
2388 }
2389 if (have_comp_dir_val)
2390 {
2391 if (!resolve_string (dwarf_sections, u->is_dwarf64, is_bigendian,
2392 u->str_offsets_base, &comp_dir_val,
2393 error_callback, data, &u->comp_dir))
2394 return 0;
2395 }
2396
2397 if (abbrev->tag == DW_TAG_compile_unit
2398 || abbrev->tag == DW_TAG_subprogram
2399 || abbrev->tag == DW_TAG_skeleton_unit)
2400 {
2401 if (!add_ranges (state, dwarf_sections, base_address,
2402 is_bigendian, u, pcrange.lowpc, &pcrange,
2403 add_unit_addr, (void *) u, error_callback, data,
2404 (void *) addrs))
2405 return 0;
2406
2407 /* If we found the PC range in the DW_TAG_compile_unit or
2408 DW_TAG_skeleton_unit, we can stop now. */
2409 if ((abbrev->tag == DW_TAG_compile_unit
2410 || abbrev->tag == DW_TAG_skeleton_unit)
2411 && (pcrange.have_ranges
2412 || (pcrange.have_lowpc && pcrange.have_highpc)))
2413 return 1;
2414 }
2415
2416 if (abbrev->has_children)
2417 {
2418 if (!find_address_ranges (state, base_address, unit_buf,
2419 dwarf_sections, is_bigendian, altlink,
2420 error_callback, data, u, addrs, NULL))
2421 return 0;
2422 }
2423 }
2424
2425 return 1;
2426}
2427
2428/* Build a mapping from address ranges to the compilation units where
2429 the line number information for that range can be found. Returns 1
2430 on success, 0 on failure. */
2431
2432static int
2433build_address_map (struct backtrace_state *state, uintptr_t base_address,
2434 const struct dwarf_sections *dwarf_sections,
2435 int is_bigendian, struct dwarf_data *altlink,
2436 backtrace_error_callback error_callback, void *data,
2437 struct unit_addrs_vector *addrs,
2438 struct unit_vector *unit_vec)
2439{
2440 struct dwarf_buf info;
2441 struct backtrace_vector units;
2442 size_t units_count;
2443 size_t i;
2444 struct unit **pu;
2445 size_t unit_offset = 0;
2446 struct unit_addrs *pa;
2447
2448 memset (&addrs->vec, 0, sizeof addrs->vec);
2449 memset (&unit_vec->vec, 0, sizeof unit_vec->vec);
2450 addrs->count = 0;
2451 unit_vec->count = 0;
2452
2453 /* Read through the .debug_info section. FIXME: Should we use the
2454 .debug_aranges section? gdb and addr2line don't use it, but I'm
2455 not sure why. */
2456
2457 info.name = ".debug_info";
2458 info.start = dwarf_sections->data[DEBUG_INFO];
2459 info.buf = info.start;
2460 info.left = dwarf_sections->size[DEBUG_INFO];
2461 info.is_bigendian = is_bigendian;
2462 info.error_callback = error_callback;
2463 info.data = data;
2464 info.reported_underflow = 0;
2465
2466 memset (&units, 0, sizeof units);
2467 units_count = 0;
2468
2469 while (info.left > 0)
2470 {
2471 const unsigned char *unit_data_start;
2472 uint64_t len;
2473 int is_dwarf64;
2474 struct dwarf_buf unit_buf;
2475 int version;
2476 int unit_type;
2477 uint64_t abbrev_offset;
2478 int addrsize;
2479 struct unit *u;
2480 enum dwarf_tag unit_tag;
2481
2482 if (info.reported_underflow)
2483 goto fail;
2484
2485 unit_data_start = info.buf;
2486
2487 len = read_initial_length (&info, &is_dwarf64);
2488 unit_buf = info;
2489 unit_buf.left = len;
2490
2491 if (!advance (&info, len))
2492 goto fail;
2493
2494 version = read_uint16 (&unit_buf);
2495 if (version < 2 || version > 5)
2496 {
2497 dwarf_buf_error (&unit_buf, "unrecognized DWARF version", -1);
2498 goto fail;
2499 }
2500
2501 if (version < 5)
2502 unit_type = 0;
2503 else
2504 {
2505 unit_type = read_byte (&unit_buf);
2506 if (unit_type == DW_UT_type || unit_type == DW_UT_split_type)
2507 {
2508 /* This unit doesn't have anything we need. */
2509 continue;
2510 }
2511 }
2512
2513 pu = ((struct unit **)
2514 backtrace_vector_grow (state, sizeof (struct unit *),
2515 error_callback, data, &units));
2516 if (pu == NULL)
2517 goto fail;
2518
2519 u = ((struct unit *)
2520 backtrace_alloc (state, sizeof *u, error_callback, data));
2521 if (u == NULL)
2522 goto fail;
2523
2524 *pu = u;
2525 ++units_count;
2526
2527 if (version < 5)
2528 addrsize = 0; /* Set below. */
2529 else
2530 addrsize = read_byte (&unit_buf);
2531
2532 memset (&u->abbrevs, 0, sizeof u->abbrevs);
2533 abbrev_offset = read_offset (&unit_buf, is_dwarf64);
2534 if (!read_abbrevs (state, abbrev_offset,
2535 dwarf_sections->data[DEBUG_ABBREV],
2536 dwarf_sections->size[DEBUG_ABBREV],
2537 is_bigendian, error_callback, data, &u->abbrevs))
2538 goto fail;
2539
2540 if (version < 5)
2541 addrsize = read_byte (&unit_buf);
2542
2543 switch (unit_type)
2544 {
2545 case 0:
2546 break;
2547 case DW_UT_compile: case DW_UT_partial:
2548 break;
2549 case DW_UT_skeleton: case DW_UT_split_compile:
2550 read_uint64 (&unit_buf); /* dwo_id */
2551 break;
2552 default:
2553 break;
2554 }
2555
2556 u->low_offset = unit_offset;
2557 unit_offset += len + (is_dwarf64 ? 12 : 4);
2558 u->high_offset = unit_offset;
2559 u->unit_data = unit_buf.buf;
2560 u->unit_data_len = unit_buf.left;
2561 u->unit_data_offset = unit_buf.buf - unit_data_start;
2562 u->version = version;
2563 u->is_dwarf64 = is_dwarf64;
2564 u->addrsize = addrsize;
2565 u->filename = NULL;
2566 u->comp_dir = NULL;
2567 u->abs_filename = NULL;
2568 u->lineoff = 0;
2569 u->str_offsets_base = 0;
2570 u->addr_base = 0;
2571 u->rnglists_base = 0;
2572
2573 /* The actual line number mappings will be read as needed. */
2574 u->lines = NULL;
2575 u->lines_count = 0;
2576 u->function_addrs = NULL;
2577 u->function_addrs_count = 0;
2578
2579 if (!find_address_ranges (state, base_address, &unit_buf, dwarf_sections,
2580 is_bigendian, altlink, error_callback, data,
2581 u, addrs, &unit_tag))
2582 goto fail;
2583
2584 if (unit_buf.reported_underflow)
2585 goto fail;
2586 }
2587 if (info.reported_underflow)
2588 goto fail;
2589
2590 /* Add a trailing addrs entry, but don't include it in addrs->count. */
2591 pa = ((struct unit_addrs *)
2592 backtrace_vector_grow (state, sizeof (struct unit_addrs),
2593 error_callback, data, &addrs->vec));
2594 if (pa == NULL)
2595 goto fail;
2596 pa->low = 0;
2597 --pa->low;
2598 pa->high = pa->low;
2599 pa->u = NULL;
2600
2601 unit_vec->vec = units;
2602 unit_vec->count = units_count;
2603 return 1;
2604
2605 fail:
2606 if (units_count > 0)
2607 {
2608 pu = (struct unit **) units.base;
2609 for (i = 0; i < units_count; i++)
2610 {
2611 free_abbrevs (state, &pu[i]->abbrevs, error_callback, data);
2612 backtrace_free (state, pu[i], sizeof **pu, error_callback, data);
2613 }
2614 backtrace_vector_free (state, &units, error_callback, data);
2615 }
2616 if (addrs->count > 0)
2617 {
2618 backtrace_vector_free (state, &addrs->vec, error_callback, data);
2619 addrs->count = 0;
2620 }
2621 return 0;
2622}
2623
2624/* Add a new mapping to the vector of line mappings that we are
2625 building. Returns 1 on success, 0 on failure. */
2626
2627static int
2628add_line (struct backtrace_state *state, struct dwarf_data *ddata,
2629 uintptr_t pc, const char *filename, int lineno,
2630 backtrace_error_callback error_callback, void *data,
2631 struct line_vector *vec)
2632{
2633 struct line *ln;
2634
2635 /* If we are adding the same mapping, ignore it. This can happen
2636 when using discriminators. */
2637 if (vec->count > 0)
2638 {
2639 ln = (struct line *) vec->vec.base + (vec->count - 1);
2640 if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno)
2641 return 1;
2642 }
2643
2644 ln = ((struct line *)
2645 backtrace_vector_grow (state, sizeof (struct line), error_callback,
2646 data, &vec->vec));
2647 if (ln == NULL)
2648 return 0;
2649
2650 /* Add in the base address here, so that we can look up the PC
2651 directly. */
2652 ln->pc = pc + ddata->base_address;
2653
2654 ln->filename = filename;
2655 ln->lineno = lineno;
2656 ln->idx = vec->count;
2657
2658 ++vec->count;
2659
2660 return 1;
2661}
2662
2663/* Free the line header information. */
2664
2665static void
2666free_line_header (struct backtrace_state *state, struct line_header *hdr,
2667 backtrace_error_callback error_callback, void *data)
2668{
2669 if (hdr->dirs_count != 0)
2670 backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *),
2671 error_callback, data);
2672 backtrace_free (state, hdr->filenames,
2673 hdr->filenames_count * sizeof (char *),
2674 error_callback, data);
2675}
2676
2677/* Read the directories and file names for a line header for version
2678 2, setting fields in HDR. Return 1 on success, 0 on failure. */
2679
2680static int
2681read_v2_paths (struct backtrace_state *state, struct unit *u,
2682 struct dwarf_buf *hdr_buf, struct line_header *hdr)
2683{
2684 const unsigned char *p;
2685 const unsigned char *pend;
2686 size_t i;
2687
2688 /* Count the number of directory entries. */
2689 hdr->dirs_count = 0;
2690 p = hdr_buf->buf;
2691 pend = p + hdr_buf->left;
2692 while (p < pend && *p != '\0')
2693 {
2694 p += strnlen((const char *) p, pend - p) + 1;
2695 ++hdr->dirs_count;
2696 }
2697
2698 /* The index of the first entry in the list of directories is 1. Index 0 is
2699 used for the current directory of the compilation. To simplify index
2700 handling, we set entry 0 to the compilation unit directory. */
2701 ++hdr->dirs_count;
2702 hdr->dirs = ((const char **)
2703 backtrace_alloc (state,
2704 hdr->dirs_count * sizeof (const char *),
2705 hdr_buf->error_callback,
2706 hdr_buf->data));
2707 if (hdr->dirs == NULL)
2708 return 0;
2709
2710 hdr->dirs[0] = u->comp_dir;
2711 i = 1;
2712 while (*hdr_buf->buf != '\0')
2713 {
2714 if (hdr_buf->reported_underflow)
2715 return 0;
2716
2717 hdr->dirs[i] = read_string (hdr_buf);
2718 if (hdr->dirs[i] == NULL)
2719 return 0;
2720 ++i;
2721 }
2722 if (!advance (hdr_buf, 1))
2723 return 0;
2724
2725 /* Count the number of file entries. */
2726 hdr->filenames_count = 0;
2727 p = hdr_buf->buf;
2728 pend = p + hdr_buf->left;
2729 while (p < pend && *p != '\0')
2730 {
2731 p += strnlen ((const char *) p, pend - p) + 1;
2732 p += leb128_len (p);
2733 p += leb128_len (p);
2734 p += leb128_len (p);
2735 ++hdr->filenames_count;
2736 }
2737
2738 /* The index of the first entry in the list of file names is 1. Index 0 is
2739 used for the DW_AT_name of the compilation unit. To simplify index
2740 handling, we set entry 0 to the compilation unit file name. */
2741 ++hdr->filenames_count;
2742 hdr->filenames = ((const char **)
2743 backtrace_alloc (state,
2744 hdr->filenames_count * sizeof (char *),
2745 hdr_buf->error_callback,
2746 hdr_buf->data));
2747 if (hdr->filenames == NULL)
2748 return 0;
2749 hdr->filenames[0] = u->filename;
2750 i = 1;
2751 while (*hdr_buf->buf != '\0')
2752 {
2753 const char *filename;
2754 uint64_t dir_index;
2755
2756 if (hdr_buf->reported_underflow)
2757 return 0;
2758
2759 filename = read_string (hdr_buf);
2760 if (filename == NULL)
2761 return 0;
2762 dir_index = read_uleb128 (hdr_buf);
2763 if (IS_ABSOLUTE_PATH (filename)
2764 || (dir_index < hdr->dirs_count && hdr->dirs[dir_index] == NULL))
2765 hdr->filenames[i] = filename;
2766 else
2767 {
2768 const char *dir;
2769 size_t dir_len;
2770 size_t filename_len;
2771 char *s;
2772
2773 if (dir_index < hdr->dirs_count)
2774 dir = hdr->dirs[dir_index];
2775 else
2776 {
2777 dwarf_buf_error (hdr_buf,
2778 ("invalid directory index in "
2779 "line number program header"),
2780 0);
2781 return 0;
2782 }
2783 dir_len = strlen (dir);
2784 filename_len = strlen (filename);
2785 s = ((char *) backtrace_alloc (state, dir_len + filename_len + 2,
2786 hdr_buf->error_callback,
2787 hdr_buf->data));
2788 if (s == NULL)
2789 return 0;
2790 memcpy (s, dir, dir_len);
2791 /* FIXME: If we are on a DOS-based file system, and the
2792 directory or the file name use backslashes, then we
2793 should use a backslash here. */
2794 s[dir_len] = '/';
2795 memcpy (s + dir_len + 1, filename, filename_len + 1);
2796 hdr->filenames[i] = s;
2797 }
2798
2799 /* Ignore the modification time and size. */
2800 read_uleb128 (hdr_buf);
2801 read_uleb128 (hdr_buf);
2802
2803 ++i;
2804 }
2805
2806 return 1;
2807}
2808
2809/* Read a single version 5 LNCT entry for a directory or file name in a
2810 line header. Sets *STRING to the resulting name, ignoring other
2811 data. Return 1 on success, 0 on failure. */
2812
2813static int
2814read_lnct (struct backtrace_state *state, struct dwarf_data *ddata,
2815 struct unit *u, struct dwarf_buf *hdr_buf,
2816 const struct line_header *hdr, size_t formats_count,
2817 const struct line_header_format *formats, const char **string)
2818{
2819 size_t i;
2820 const char *dir;
2821 const char *path;
2822
2823 dir = NULL;
2824 path = NULL;
2825 for (i = 0; i < formats_count; i++)
2826 {
2827 struct attr_val val;
2828
2829 if (!read_attribute (formats[i].form, 0, hdr_buf, u->is_dwarf64,
2830 u->version, hdr->addrsize, &ddata->dwarf_sections,
2831 ddata->altlink, &val))
2832 return 0;
2833 switch (formats[i].lnct)
2834 {
2835 case DW_LNCT_path:
2836 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
2837 ddata->is_bigendian, u->str_offsets_base,
2838 &val, hdr_buf->error_callback, hdr_buf->data,
2839 &path))
2840 return 0;
2841 break;
2842 case DW_LNCT_directory_index:
2843 if (val.encoding == ATTR_VAL_UINT)
2844 {
2845 if (val.u.uint >= hdr->dirs_count)
2846 {
2847 dwarf_buf_error (hdr_buf,
2848 ("invalid directory index in "
2849 "line number program header"),
2850 0);
2851 return 0;
2852 }
2853 dir = hdr->dirs[val.u.uint];
2854 }
2855 break;
2856 default:
2857 /* We don't care about timestamps or sizes or hashes. */
2858 break;
2859 }
2860 }
2861
2862 if (path == NULL)
2863 {
2864 dwarf_buf_error (hdr_buf,
2865 "missing file name in line number program header",
2866 0);
2867 return 0;
2868 }
2869
2870 if (dir == NULL)
2871 *string = path;
2872 else
2873 {
2874 size_t dir_len;
2875 size_t path_len;
2876 char *s;
2877
2878 dir_len = strlen (dir);
2879 path_len = strlen (path);
2880 s = (char *) backtrace_alloc (state, dir_len + path_len + 2,
2881 hdr_buf->error_callback, hdr_buf->data);
2882 if (s == NULL)
2883 return 0;
2884 memcpy (s, dir, dir_len);
2885 /* FIXME: If we are on a DOS-based file system, and the
2886 directory or the path name use backslashes, then we should
2887 use a backslash here. */
2888 s[dir_len] = '/';
2889 memcpy (s + dir_len + 1, path, path_len + 1);
2890 *string = s;
2891 }
2892
2893 return 1;
2894}
2895
2896/* Read a set of DWARF 5 line header format entries, setting *PCOUNT
2897 and *PPATHS. Return 1 on success, 0 on failure. */
2898
2899static int
2900read_line_header_format_entries (struct backtrace_state *state,
2901 struct dwarf_data *ddata,
2902 struct unit *u,
2903 struct dwarf_buf *hdr_buf,
2904 struct line_header *hdr,
2905 size_t *pcount,
2906 const char ***ppaths)
2907{
2908 size_t formats_count;
2909 struct line_header_format *formats;
2910 size_t paths_count;
2911 const char **paths;
2912 size_t i;
2913 int ret;
2914
2915 formats_count = read_byte (hdr_buf);
2916 if (formats_count == 0)
2917 formats = NULL;
2918 else
2919 {
2920 formats = ((struct line_header_format *)
2921 backtrace_alloc (state,
2922 (formats_count
2923 * sizeof (struct line_header_format)),
2924 hdr_buf->error_callback,
2925 hdr_buf->data));
2926 if (formats == NULL)
2927 return 0;
2928
2929 for (i = 0; i < formats_count; i++)
2930 {
2931 formats[i].lnct = (int) read_uleb128(hdr_buf);
2932 formats[i].form = (enum dwarf_form) read_uleb128 (hdr_buf);
2933 }
2934 }
2935
2936 paths_count = read_uleb128 (hdr_buf);
2937 if (paths_count == 0)
2938 {
2939 *pcount = 0;
2940 *ppaths = NULL;
2941 ret = 1;
2942 goto exit;
2943 }
2944
2945 paths = ((const char **)
2946 backtrace_alloc (state, paths_count * sizeof (const char *),
2947 hdr_buf->error_callback, hdr_buf->data));
2948 if (paths == NULL)
2949 {
2950 ret = 0;
2951 goto exit;
2952 }
2953 for (i = 0; i < paths_count; i++)
2954 {
2955 if (!read_lnct (state, ddata, u, hdr_buf, hdr, formats_count,
2956 formats, &paths[i]))
2957 {
2958 backtrace_free (state, paths,
2959 paths_count * sizeof (const char *),
2960 hdr_buf->error_callback, hdr_buf->data);
2961 ret = 0;
2962 goto exit;
2963 }
2964 }
2965
2966 *pcount = paths_count;
2967 *ppaths = paths;
2968
2969 ret = 1;
2970
2971 exit:
2972 if (formats != NULL)
2973 backtrace_free (state, formats,
2974 formats_count * sizeof (struct line_header_format),
2975 hdr_buf->error_callback, hdr_buf->data);
2976
2977 return ret;
2978}
2979
2980/* Read the line header. Return 1 on success, 0 on failure. */
2981
2982static int
2983read_line_header (struct backtrace_state *state, struct dwarf_data *ddata,
2984 struct unit *u, int is_dwarf64, struct dwarf_buf *line_buf,
2985 struct line_header *hdr)
2986{
2987 uint64_t hdrlen;
2988 struct dwarf_buf hdr_buf;
2989
2990 hdr->version = read_uint16 (line_buf);
2991 if (hdr->version < 2 || hdr->version > 5)
2992 {
2993 dwarf_buf_error (line_buf, "unsupported line number version", -1);
2994 return 0;
2995 }
2996
2997 if (hdr->version < 5)
2998 hdr->addrsize = u->addrsize;
2999 else
3000 {
3001 hdr->addrsize = read_byte (line_buf);
3002 /* We could support a non-zero segment_selector_size but I doubt
3003 we'll ever see it. */
3004 if (read_byte (line_buf) != 0)
3005 {
3006 dwarf_buf_error (line_buf,
3007 "non-zero segment_selector_size not supported",
3008 -1);
3009 return 0;
3010 }
3011 }
3012
3013 hdrlen = read_offset (line_buf, is_dwarf64);
3014
3015 hdr_buf = *line_buf;
3016 hdr_buf.left = hdrlen;
3017
3018 if (!advance (line_buf, hdrlen))
3019 return 0;
3020
3021 hdr->min_insn_len = read_byte (&hdr_buf);
3022 if (hdr->version < 4)
3023 hdr->max_ops_per_insn = 1;
3024 else
3025 hdr->max_ops_per_insn = read_byte (&hdr_buf);
3026
3027 /* We don't care about default_is_stmt. */
3028 read_byte (&hdr_buf);
3029
3030 hdr->line_base = read_sbyte (&hdr_buf);
3031 hdr->line_range = read_byte (&hdr_buf);
3032
3033 hdr->opcode_base = read_byte (&hdr_buf);
3034 hdr->opcode_lengths = hdr_buf.buf;
3035 if (!advance (&hdr_buf, hdr->opcode_base - 1))
3036 return 0;
3037
3038 if (hdr->version < 5)
3039 {
3040 if (!read_v2_paths (state, u, &hdr_buf, hdr))
3041 return 0;
3042 }
3043 else
3044 {
3045 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
3046 &hdr->dirs_count,
3047 &hdr->dirs))
3048 return 0;
3049 if (!read_line_header_format_entries (state, ddata, u, &hdr_buf, hdr,
3050 &hdr->filenames_count,
3051 &hdr->filenames))
3052 return 0;
3053 }
3054
3055 if (hdr_buf.reported_underflow)
3056 return 0;
3057
3058 return 1;
3059}
3060
3061/* Read the line program, adding line mappings to VEC. Return 1 on
3062 success, 0 on failure. */
3063
3064static int
3065read_line_program (struct backtrace_state *state, struct dwarf_data *ddata,
3066 const struct line_header *hdr, struct dwarf_buf *line_buf,
3067 struct line_vector *vec)
3068{
3069 uint64_t address;
3070 unsigned int op_index;
3071 const char *reset_filename;
3072 const char *filename;
3073 int lineno;
3074
3075 address = 0;
3076 op_index = 0;
3077 if (hdr->filenames_count > 1)
3078 reset_filename = hdr->filenames[1];
3079 else
3080 reset_filename = "";
3081 filename = reset_filename;
3082 lineno = 1;
3083 while (line_buf->left > 0)
3084 {
3085 unsigned int op;
3086
3087 op = read_byte (line_buf);
3088 if (op >= hdr->opcode_base)
3089 {
3090 unsigned int advance;
3091
3092 /* Special opcode. */
3093 op -= hdr->opcode_base;
3094 advance = op / hdr->line_range;
3095 address += (hdr->min_insn_len * (op_index + advance)
3096 / hdr->max_ops_per_insn);
3097 op_index = (op_index + advance) % hdr->max_ops_per_insn;
3098 lineno += hdr->line_base + (int) (op % hdr->line_range);
3099 add_line (state, ddata, address, filename, lineno,
3100 line_buf->error_callback, line_buf->data, vec);
3101 }
3102 else if (op == DW_LNS_extended_op)
3103 {
3104 uint64_t len;
3105
3106 len = read_uleb128 (line_buf);
3107 op = read_byte (line_buf);
3108 switch (op)
3109 {
3110 case DW_LNE_end_sequence:
3111 /* FIXME: Should we mark the high PC here? It seems
3112 that we already have that information from the
3113 compilation unit. */
3114 address = 0;
3115 op_index = 0;
3116 filename = reset_filename;
3117 lineno = 1;
3118 break;
3119 case DW_LNE_set_address:
3120 address = read_address (line_buf, hdr->addrsize);
3121 break;
3122 case DW_LNE_define_file:
3123 {
3124 const char *f;
3125 unsigned int dir_index;
3126
3127 f = read_string (line_buf);
3128 if (f == NULL)
3129 return 0;
3130 dir_index = read_uleb128 (line_buf);
3131 /* Ignore that time and length. */
3132 read_uleb128 (line_buf);
3133 read_uleb128 (line_buf);
3134 if (IS_ABSOLUTE_PATH (f))
3135 filename = f;
3136 else
3137 {
3138 const char *dir;
3139 size_t dir_len;
3140 size_t f_len;
3141 char *p;
3142
3143 if (dir_index < hdr->dirs_count)
3144 dir = hdr->dirs[dir_index];
3145 else
3146 {
3147 dwarf_buf_error (line_buf,
3148 ("invalid directory index "
3149 "in line number program"),
3150 0);
3151 return 0;
3152 }
3153 dir_len = strlen (dir);
3154 f_len = strlen (f);
3155 p = ((char *)
3156 backtrace_alloc (state, dir_len + f_len + 2,
3157 line_buf->error_callback,
3158 line_buf->data));
3159 if (p == NULL)
3160 return 0;
3161 memcpy (p, dir, dir_len);
3162 /* FIXME: If we are on a DOS-based file system,
3163 and the directory or the file name use
3164 backslashes, then we should use a backslash
3165 here. */
3166 p[dir_len] = '/';
3167 memcpy (p + dir_len + 1, f, f_len + 1);
3168 filename = p;
3169 }
3170 }
3171 break;
3172 case DW_LNE_set_discriminator:
3173 /* We don't care about discriminators. */
3174 read_uleb128 (line_buf);
3175 break;
3176 default:
3177 if (!advance (line_buf, len - 1))
3178 return 0;
3179 break;
3180 }
3181 }
3182 else
3183 {
3184 switch (op)
3185 {
3186 case DW_LNS_copy:
3187 add_line (state, ddata, address, filename, lineno,
3188 line_buf->error_callback, line_buf->data, vec);
3189 break;
3190 case DW_LNS_advance_pc:
3191 {
3192 uint64_t advance;
3193
3194 advance = read_uleb128 (line_buf);
3195 address += (hdr->min_insn_len * (op_index + advance)
3196 / hdr->max_ops_per_insn);
3197 op_index = (op_index + advance) % hdr->max_ops_per_insn;
3198 }
3199 break;
3200 case DW_LNS_advance_line:
3201 lineno += (int) read_sleb128 (line_buf);
3202 break;
3203 case DW_LNS_set_file:
3204 {
3205 uint64_t fileno;
3206
3207 fileno = read_uleb128 (line_buf);
3208 if (fileno >= hdr->filenames_count)
3209 {
3210 dwarf_buf_error (line_buf,
3211 ("invalid file number in "
3212 "line number program"),
3213 0);
3214 return 0;
3215 }
3216 filename = hdr->filenames[fileno];
3217 }
3218 break;
3219 case DW_LNS_set_column:
3220 read_uleb128 (line_buf);
3221 break;
3222 case DW_LNS_negate_stmt:
3223 break;
3224 case DW_LNS_set_basic_block:
3225 break;
3226 case DW_LNS_const_add_pc:
3227 {
3228 unsigned int advance;
3229
3230 op = 255 - hdr->opcode_base;
3231 advance = op / hdr->line_range;
3232 address += (hdr->min_insn_len * (op_index + advance)
3233 / hdr->max_ops_per_insn);
3234 op_index = (op_index + advance) % hdr->max_ops_per_insn;
3235 }
3236 break;
3237 case DW_LNS_fixed_advance_pc:
3238 address += read_uint16 (line_buf);
3239 op_index = 0;
3240 break;
3241 case DW_LNS_set_prologue_end:
3242 break;
3243 case DW_LNS_set_epilogue_begin:
3244 break;
3245 case DW_LNS_set_isa:
3246 read_uleb128 (line_buf);
3247 break;
3248 default:
3249 {
3250 unsigned int i;
3251
3252 for (i = hdr->opcode_lengths[op - 1]; i > 0; --i)
3253 read_uleb128 (line_buf);
3254 }
3255 break;
3256 }
3257 }
3258 }
3259
3260 return 1;
3261}
3262
3263/* Read the line number information for a compilation unit. Returns 1
3264 on success, 0 on failure. */
3265
3266static int
3267read_line_info (struct backtrace_state *state, struct dwarf_data *ddata,
3268 backtrace_error_callback error_callback, void *data,
3269 struct unit *u, struct line_header *hdr, struct line **lines,
3270 size_t *lines_count)
3271{
3272 struct line_vector vec;
3273 struct dwarf_buf line_buf;
3274 uint64_t len;
3275 int is_dwarf64;
3276 struct line *ln;
3277
3278 memset (&vec.vec, 0, sizeof vec.vec);
3279 vec.count = 0;
3280
3281 memset (hdr, 0, sizeof *hdr);
3282
3283 if (u->lineoff != (off_t) (size_t) u->lineoff
3284 || (size_t) u->lineoff >= ddata->dwarf_sections.size[DEBUG_LINE])
3285 {
3286 error_callback (data, "unit line offset out of range", 0);
3287 goto fail;
3288 }
3289
3290 line_buf.name = ".debug_line";
3291 line_buf.start = ddata->dwarf_sections.data[DEBUG_LINE];
3292 line_buf.buf = ddata->dwarf_sections.data[DEBUG_LINE] + u->lineoff;
3293 line_buf.left = ddata->dwarf_sections.size[DEBUG_LINE] - u->lineoff;
3294 line_buf.is_bigendian = ddata->is_bigendian;
3295 line_buf.error_callback = error_callback;
3296 line_buf.data = data;
3297 line_buf.reported_underflow = 0;
3298
3299 len = read_initial_length (&line_buf, &is_dwarf64);
3300 line_buf.left = len;
3301
3302 if (!read_line_header (state, ddata, u, is_dwarf64, &line_buf, hdr))
3303 goto fail;
3304
3305 if (!read_line_program (state, ddata, hdr, &line_buf, &vec))
3306 goto fail;
3307
3308 if (line_buf.reported_underflow)
3309 goto fail;
3310
3311 if (vec.count == 0)
3312 {
3313 /* This is not a failure in the sense of a generating an error,
3314 but it is a failure in that sense that we have no useful
3315 information. */
3316 goto fail;
3317 }
3318
3319 /* Allocate one extra entry at the end. */
3320 ln = ((struct line *)
3321 backtrace_vector_grow (state, sizeof (struct line), error_callback,
3322 data, &vec.vec));
3323 if (ln == NULL)
3324 goto fail;
3325 ln->pc = (uintptr_t) -1;
3326 ln->filename = NULL;
3327 ln->lineno = 0;
3328 ln->idx = 0;
3329
3330 if (!backtrace_vector_release (state, &vec.vec, error_callback, data))
3331 goto fail;
3332
3333 ln = (struct line *) vec.vec.base;
3334 backtrace_qsort (ln, vec.count, sizeof (struct line), line_compare);
3335
3336 *lines = ln;
3337 *lines_count = vec.count;
3338
3339 return 1;
3340
3341 fail:
3342 backtrace_vector_free (state, &vec.vec, error_callback, data);
3343 free_line_header (state, hdr, error_callback, data);
3344 *lines = (struct line *) (uintptr_t) -1;
3345 *lines_count = 0;
3346 return 0;
3347}
3348
3349static const char *read_referenced_name (struct dwarf_data *, struct unit *,
3350 uint64_t, backtrace_error_callback,
3351 void *);
3352
3353/* Read the name of a function from a DIE referenced by ATTR with VAL. */
3354
3355static const char *
3356read_referenced_name_from_attr (struct dwarf_data *ddata, struct unit *u,
3357 struct attr *attr, struct attr_val *val,
3358 backtrace_error_callback error_callback,
3359 void *data)
3360{
3361 switch (attr->name)
3362 {
3363 case DW_AT_abstract_origin:
3364 case DW_AT_specification:
3365 break;
3366 default:
3367 return NULL;
3368 }
3369
3370 if (attr->form == DW_FORM_ref_sig8)
3371 return NULL;
3372
3373 if (val->encoding == ATTR_VAL_REF_INFO)
3374 {
3375 struct unit *unit
3376 = find_unit (ddata->units, ddata->units_count,
3377 val->u.uint);
3378 if (unit == NULL)
3379 return NULL;
3380
3381 uint64_t offset = val->u.uint - unit->low_offset;
3382 return read_referenced_name (ddata, unit, offset, error_callback, data);
3383 }
3384
3385 if (val->encoding == ATTR_VAL_UINT
3386 || val->encoding == ATTR_VAL_REF_UNIT)
3387 return read_referenced_name (ddata, u, val->u.uint, error_callback, data);
3388
3389 if (val->encoding == ATTR_VAL_REF_ALT_INFO)
3390 {
3391 struct unit *alt_unit
3392 = find_unit (ddata->altlink->units, ddata->altlink->units_count,
3393 val->u.uint);
3394 if (alt_unit == NULL)
3395 return NULL;
3396
3397 uint64_t offset = val->u.uint - alt_unit->low_offset;
3398 return read_referenced_name (ddata->altlink, alt_unit, offset,
3399 error_callback, data);
3400 }
3401
3402 return NULL;
3403}
3404
3405/* Read the name of a function from a DIE referenced by a
3406 DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within
3407 the same compilation unit. */
3408
3409static const char *
3410read_referenced_name (struct dwarf_data *ddata, struct unit *u,
3411 uint64_t offset, backtrace_error_callback error_callback,
3412 void *data)
3413{
3414 struct dwarf_buf unit_buf;
3415 uint64_t code;
3416 const struct abbrev *abbrev;
3417 const char *ret;
3418 size_t i;
3419
3420 /* OFFSET is from the start of the data for this compilation unit.
3421 U->unit_data is the data, but it starts U->unit_data_offset bytes
3422 from the beginning. */
3423
3424 if (offset < u->unit_data_offset
3425 || offset - u->unit_data_offset >= u->unit_data_len)
3426 {
3427 error_callback (data,
3428 "abstract origin or specification out of range",
3429 0);
3430 return NULL;
3431 }
3432
3433 offset -= u->unit_data_offset;
3434
3435 unit_buf.name = ".debug_info";
3436 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3437 unit_buf.buf = u->unit_data + offset;
3438 unit_buf.left = u->unit_data_len - offset;
3439 unit_buf.is_bigendian = ddata->is_bigendian;
3440 unit_buf.error_callback = error_callback;
3441 unit_buf.data = data;
3442 unit_buf.reported_underflow = 0;
3443
3444 code = read_uleb128 (&unit_buf);
3445 if (code == 0)
3446 {
3447 dwarf_buf_error (&unit_buf,
3448 "invalid abstract origin or specification",
3449 0);
3450 return NULL;
3451 }
3452
3453 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3454 if (abbrev == NULL)
3455 return NULL;
3456
3457 ret = NULL;
3458 for (i = 0; i < abbrev->num_attrs; ++i)
3459 {
3460 struct attr_val val;
3461
3462 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3463 &unit_buf, u->is_dwarf64, u->version, u->addrsize,
3464 &ddata->dwarf_sections, ddata->altlink, &val))
3465 return NULL;
3466
3467 switch (abbrev->attrs[i].name)
3468 {
3469 case DW_AT_name:
3470 /* Third name preference: don't override. A name we found in some
3471 other way, will normally be more useful -- e.g., this name is
3472 normally not mangled. */
3473 if (ret != NULL)
3474 break;
3475 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3476 ddata->is_bigendian, u->str_offsets_base,
3477 &val, error_callback, data, &ret))
3478 return NULL;
3479 break;
3480
3481 case DW_AT_linkage_name:
3482 case DW_AT_MIPS_linkage_name:
3483 /* First name preference: override all. */
3484 {
3485 const char *s;
3486
3487 s = NULL;
3488 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3489 ddata->is_bigendian, u->str_offsets_base,
3490 &val, error_callback, data, &s))
3491 return NULL;
3492 if (s != NULL)
3493 return s;
3494 }
3495 break;
3496
3497 case DW_AT_specification:
3498 /* Second name preference: override DW_AT_name, don't override
3499 DW_AT_linkage_name. */
3500 {
3501 const char *name;
3502
3503 name = read_referenced_name_from_attr (ddata, u, &abbrev->attrs[i],
3504 &val, error_callback, data);
3505 if (name != NULL)
3506 ret = name;
3507 }
3508 break;
3509
3510 default:
3511 break;
3512 }
3513 }
3514
3515 return ret;
3516}
3517
3518/* Add a range to a unit that maps to a function. This is called via
3519 add_ranges. Returns 1 on success, 0 on error. */
3520
3521static int
3522add_function_range (struct backtrace_state *state, void *rdata,
3523 uint64_t lowpc, uint64_t highpc,
3524 backtrace_error_callback error_callback, void *data,
3525 void *pvec)
3526{
3527 struct function *function = (struct function *) rdata;
3528 struct function_vector *vec = (struct function_vector *) pvec;
3529 struct function_addrs *p;
3530
3531 if (vec->count > 0)
3532 {
3533 p = (struct function_addrs *) vec->vec.base + (vec->count - 1);
3534 if ((lowpc == p->high || lowpc == p->high + 1)
3535 && function == p->function)
3536 {
3537 if (highpc > p->high)
3538 p->high = highpc;
3539 return 1;
3540 }
3541 }
3542
3543 p = ((struct function_addrs *)
3544 backtrace_vector_grow (state, sizeof (struct function_addrs),
3545 error_callback, data, &vec->vec));
3546 if (p == NULL)
3547 return 0;
3548
3549 p->low = lowpc;
3550 p->high = highpc;
3551 p->function = function;
3552
3553 ++vec->count;
3554
3555 return 1;
3556}
3557
3558/* Read one entry plus all its children. Add function addresses to
3559 VEC. Returns 1 on success, 0 on error. */
3560
3561static int
3562read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata,
3563 struct unit *u, uint64_t base, struct dwarf_buf *unit_buf,
3564 const struct line_header *lhdr,
3565 backtrace_error_callback error_callback, void *data,
3566 struct function_vector *vec_function,
3567 struct function_vector *vec_inlined)
3568{
3569 while (unit_buf->left > 0)
3570 {
3571 uint64_t code;
3572 const struct abbrev *abbrev;
3573 int is_function;
3574 struct function *function;
3575 struct function_vector *vec;
3576 size_t i;
3577 struct pcrange pcrange;
3578 int have_linkage_name;
3579
3580 code = read_uleb128 (unit_buf);
3581 if (code == 0)
3582 return 1;
3583
3584 abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data);
3585 if (abbrev == NULL)
3586 return 0;
3587
3588 is_function = (abbrev->tag == DW_TAG_subprogram
3589 || abbrev->tag == DW_TAG_entry_point
3590 || abbrev->tag == DW_TAG_inlined_subroutine);
3591
3592 if (abbrev->tag == DW_TAG_inlined_subroutine)
3593 vec = vec_inlined;
3594 else
3595 vec = vec_function;
3596
3597 function = NULL;
3598 if (is_function)
3599 {
3600 function = ((struct function *)
3601 backtrace_alloc (state, sizeof *function,
3602 error_callback, data));
3603 if (function == NULL)
3604 return 0;
3605 memset (function, 0, sizeof *function);
3606 }
3607
3608 memset (&pcrange, 0, sizeof pcrange);
3609 have_linkage_name = 0;
3610 for (i = 0; i < abbrev->num_attrs; ++i)
3611 {
3612 struct attr_val val;
3613
3614 if (!read_attribute (abbrev->attrs[i].form, abbrev->attrs[i].val,
3615 unit_buf, u->is_dwarf64, u->version,
3616 u->addrsize, &ddata->dwarf_sections,
3617 ddata->altlink, &val))
3618 return 0;
3619
3620 /* The compile unit sets the base address for any address
3621 ranges in the function entries. */
3622 if ((abbrev->tag == DW_TAG_compile_unit
3623 || abbrev->tag == DW_TAG_skeleton_unit)
3624 && abbrev->attrs[i].name == DW_AT_low_pc)
3625 {
3626 if (val.encoding == ATTR_VAL_ADDRESS)
3627 base = val.u.uint;
3628 else if (val.encoding == ATTR_VAL_ADDRESS_INDEX)
3629 {
3630 if (!resolve_addr_index (&ddata->dwarf_sections,
3631 u->addr_base, u->addrsize,
3632 ddata->is_bigendian, val.u.uint,
3633 error_callback, data, &base))
3634 return 0;
3635 }
3636 }
3637
3638 if (is_function)
3639 {
3640 switch (abbrev->attrs[i].name)
3641 {
3642 case DW_AT_call_file:
3643 if (val.encoding == ATTR_VAL_UINT)
3644 {
3645 if (val.u.uint >= lhdr->filenames_count)
3646 {
3647 dwarf_buf_error (unit_buf,
3648 ("invalid file number in "
3649 "DW_AT_call_file attribute"),
3650 0);
3651 return 0;
3652 }
3653 function->caller_filename = lhdr->filenames[val.u.uint];
3654 }
3655 break;
3656
3657 case DW_AT_call_line:
3658 if (val.encoding == ATTR_VAL_UINT)
3659 function->caller_lineno = val.u.uint;
3660 break;
3661
3662 case DW_AT_abstract_origin:
3663 case DW_AT_specification:
3664 /* Second name preference: override DW_AT_name, don't override
3665 DW_AT_linkage_name. */
3666 if (have_linkage_name)
3667 break;
3668 {
3669 const char *name;
3670
3671 name
3672 = read_referenced_name_from_attr (ddata, u,
3673 &abbrev->attrs[i], &val,
3674 error_callback, data);
3675 if (name != NULL)
3676 function->name = name;
3677 }
3678 break;
3679
3680 case DW_AT_name:
3681 /* Third name preference: don't override. */
3682 if (function->name != NULL)
3683 break;
3684 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3685 ddata->is_bigendian,
3686 u->str_offsets_base, &val,
3687 error_callback, data, &function->name))
3688 return 0;
3689 break;
3690
3691 case DW_AT_linkage_name:
3692 case DW_AT_MIPS_linkage_name:
3693 /* First name preference: override all. */
3694 {
3695 const char *s;
3696
3697 s = NULL;
3698 if (!resolve_string (&ddata->dwarf_sections, u->is_dwarf64,
3699 ddata->is_bigendian,
3700 u->str_offsets_base, &val,
3701 error_callback, data, &s))
3702 return 0;
3703 if (s != NULL)
3704 {
3705 function->name = s;
3706 have_linkage_name = 1;
3707 }
3708 }
3709 break;
3710
3711 case DW_AT_low_pc: case DW_AT_high_pc: case DW_AT_ranges:
3712 update_pcrange (&abbrev->attrs[i], &val, &pcrange);
3713 break;
3714
3715 default:
3716 break;
3717 }
3718 }
3719 }
3720
3721 /* If we couldn't find a name for the function, we have no use
3722 for it. */
3723 if (is_function && function->name == NULL)
3724 {
3725 backtrace_free (state, function, sizeof *function,
3726 error_callback, data);
3727 is_function = 0;
3728 }
3729
3730 if (is_function)
3731 {
3732 if (pcrange.have_ranges
3733 || (pcrange.have_lowpc && pcrange.have_highpc))
3734 {
3735 if (!add_ranges (state, &ddata->dwarf_sections,
3736 ddata->base_address, ddata->is_bigendian,
3737 u, base, &pcrange, add_function_range,
3738 (void *) function, error_callback, data,
3739 (void *) vec))
3740 return 0;
3741 }
3742 else
3743 {
3744 backtrace_free (state, function, sizeof *function,
3745 error_callback, data);
3746 is_function = 0;
3747 }
3748 }
3749
3750 if (abbrev->has_children)
3751 {
3752 if (!is_function)
3753 {
3754 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3755 error_callback, data, vec_function,
3756 vec_inlined))
3757 return 0;
3758 }
3759 else
3760 {
3761 struct function_vector fvec;
3762
3763 /* Gather any information for inlined functions in
3764 FVEC. */
3765
3766 memset (&fvec, 0, sizeof fvec);
3767
3768 if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr,
3769 error_callback, data, vec_function,
3770 &fvec))
3771 return 0;
3772
3773 if (fvec.count > 0)
3774 {
3775 struct function_addrs *p;
3776 struct function_addrs *faddrs;
3777
3778 /* Allocate a trailing entry, but don't include it
3779 in fvec.count. */
3780 p = ((struct function_addrs *)
3781 backtrace_vector_grow (state,
3782 sizeof (struct function_addrs),
3783 error_callback, data,
3784 &fvec.vec));
3785 if (p == NULL)
3786 return 0;
3787 p->low = 0;
3788 --p->low;
3789 p->high = p->low;
3790 p->function = NULL;
3791
3792 if (!backtrace_vector_release (state, &fvec.vec,
3793 error_callback, data))
3794 return 0;
3795
3796 faddrs = (struct function_addrs *) fvec.vec.base;
3797 backtrace_qsort (faddrs, fvec.count,
3798 sizeof (struct function_addrs),
3799 function_addrs_compare);
3800
3801 function->function_addrs = faddrs;
3802 function->function_addrs_count = fvec.count;
3803 }
3804 }
3805 }
3806 }
3807
3808 return 1;
3809}
3810
3811/* Read function name information for a compilation unit. We look
3812 through the whole unit looking for function tags. */
3813
3814static void
3815read_function_info (struct backtrace_state *state, struct dwarf_data *ddata,
3816 const struct line_header *lhdr,
3817 backtrace_error_callback error_callback, void *data,
3818 struct unit *u, struct function_vector *fvec,
3819 struct function_addrs **ret_addrs,
3820 size_t *ret_addrs_count)
3821{
3822 struct function_vector lvec;
3823 struct function_vector *pfvec;
3824 struct dwarf_buf unit_buf;
3825 struct function_addrs *p;
3826 struct function_addrs *addrs;
3827 size_t addrs_count;
3828
3829 /* Use FVEC if it is not NULL. Otherwise use our own vector. */
3830 if (fvec != NULL)
3831 pfvec = fvec;
3832 else
3833 {
3834 memset (&lvec, 0, sizeof lvec);
3835 pfvec = &lvec;
3836 }
3837
3838 unit_buf.name = ".debug_info";
3839 unit_buf.start = ddata->dwarf_sections.data[DEBUG_INFO];
3840 unit_buf.buf = u->unit_data;
3841 unit_buf.left = u->unit_data_len;
3842 unit_buf.is_bigendian = ddata->is_bigendian;
3843 unit_buf.error_callback = error_callback;
3844 unit_buf.data = data;
3845 unit_buf.reported_underflow = 0;
3846
3847 while (unit_buf.left > 0)
3848 {
3849 if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr,
3850 error_callback, data, pfvec, pfvec))
3851 return;
3852 }
3853
3854 if (pfvec->count == 0)
3855 return;
3856
3857 /* Allocate a trailing entry, but don't include it in
3858 pfvec->count. */
3859 p = ((struct function_addrs *)
3860 backtrace_vector_grow (state, sizeof (struct function_addrs),
3861 error_callback, data, &pfvec->vec));
3862 if (p == NULL)
3863 return;
3864 p->low = 0;
3865 --p->low;
3866 p->high = p->low;
3867 p->function = NULL;
3868
3869 addrs_count = pfvec->count;
3870
3871 if (fvec == NULL)
3872 {
3873 if (!backtrace_vector_release (state, &lvec.vec, error_callback, data))
3874 return;
3875 addrs = (struct function_addrs *) pfvec->vec.base;
3876 }
3877 else
3878 {
3879 /* Finish this list of addresses, but leave the remaining space in
3880 the vector available for the next function unit. */
3881 addrs = ((struct function_addrs *)
3882 backtrace_vector_finish (state, &fvec->vec,
3883 error_callback, data));
3884 if (addrs == NULL)
3885 return;
3886 fvec->count = 0;
3887 }
3888
3889 backtrace_qsort (addrs, addrs_count, sizeof (struct function_addrs),
3890 function_addrs_compare);
3891
3892 *ret_addrs = addrs;
3893 *ret_addrs_count = addrs_count;
3894}
3895
3896/* See if PC is inlined in FUNCTION. If it is, print out the inlined
3897 information, and update FILENAME and LINENO for the caller.
3898 Returns whatever CALLBACK returns, or 0 to keep going. */
3899
3900static int
3901report_inlined_functions (uintptr_t pc, struct function *function, const char* comp_dir,
3902 backtrace_full_callback callback, void *data,
3903 const char **filename, int *lineno)
3904{
3905 struct function_addrs *p;
3906 struct function_addrs *match;
3907 struct function *inlined;
3908 int ret;
3909
3910 if (function->function_addrs_count == 0)
3911 return 0;
3912
3913 /* Our search isn't safe if pc == -1, as that is the sentinel
3914 value. */
3915 if (pc + 1 == 0)
3916 return 0;
3917
3918 p = ((struct function_addrs *)
3919 bsearch (&pc, function->function_addrs,
3920 function->function_addrs_count,
3921 sizeof (struct function_addrs),
3922 function_addrs_search));
3923 if (p == NULL)
3924 return 0;
3925
3926 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
3927 sorted by low, so if pc > p->low we are at the end of a range of
3928 function_addrs with the same low value. If pc == p->low walk
3929 forward to the end of the range with that low value. Then walk
3930 backward and use the first range that includes pc. */
3931 while (pc == (p + 1)->low)
3932 ++p;
3933 match = NULL;
3934 while (1)
3935 {
3936 if (pc < p->high)
3937 {
3938 match = p;
3939 break;
3940 }
3941 if (p == function->function_addrs)
3942 break;
3943 if ((p - 1)->low < p->low)
3944 break;
3945 --p;
3946 }
3947 if (match == NULL)
3948 return 0;
3949
3950 /* We found an inlined call. */
3951
3952 inlined = match->function;
3953
3954 /* Report any calls inlined into this one. */
3955 ret = report_inlined_functions (pc, inlined, comp_dir, callback, data,
3956 filename, lineno);
3957 if (ret != 0)
3958 return ret;
3959
3960 /* Report this inlined call. */
3961 if (*filename[0] != '/' && comp_dir)
3962 {
3963 char buf[1024];
3964 snprintf (buf, 1024, "%s/%s", comp_dir, *filename);
3965 ret = callback (data, pc, match->low, buf, *lineno, inlined->name);
3966 }
3967 else
3968 {
3969 ret = callback (data, pc, match->low, *filename, *lineno, inlined->name);
3970 }
3971 if (ret != 0)
3972 return ret;
3973
3974 /* Our caller will report the caller of the inlined function; tell
3975 it the appropriate filename and line number. */
3976 *filename = inlined->caller_filename;
3977 *lineno = inlined->caller_lineno;
3978
3979 return 0;
3980}
3981
3982/* Look for a PC in the DWARF mapping for one module. On success,
3983 call CALLBACK and return whatever it returns. On error, call
3984 ERROR_CALLBACK and return 0. Sets *FOUND to 1 if the PC is found,
3985 0 if not. */
3986
3987static int
3988dwarf_lookup_pc (struct backtrace_state *state, struct dwarf_data *ddata,
3989 uintptr_t pc, backtrace_full_callback callback,
3990 backtrace_error_callback error_callback, void *data,
3991 int *found)
3992{
3993 struct unit_addrs *entry;
3994 int found_entry;
3995 struct unit *u;
3996 int new_data;
3997 struct line *lines;
3998 struct line *ln;
3999 struct function_addrs *p;
4000 struct function_addrs *fmatch;
4001 struct function *function;
4002 const char *filename;
4003 int lineno;
4004 int ret;
4005
4006 *found = 1;
4007
4008 /* Find an address range that includes PC. Our search isn't safe if
4009 PC == -1, as we use that as a sentinel value, so skip the search
4010 in that case. */
4011 entry = (ddata->addrs_count == 0 || pc + 1 == 0
4012 ? NULL
4013 : (struct unit_addrs*)bsearch (&pc, ddata->addrs, ddata->addrs_count,
4014 sizeof (struct unit_addrs), unit_addrs_search));
4015
4016 if (entry == NULL)
4017 {
4018 *found = 0;
4019 return 0;
4020 }
4021
4022 /* Here pc >= entry->low && pc < (entry + 1)->low. The unit_addrs
4023 are sorted by low, so if pc > p->low we are at the end of a range
4024 of unit_addrs with the same low value. If pc == p->low walk
4025 forward to the end of the range with that low value. Then walk
4026 backward and use the first range that includes pc. */
4027 while (pc == (entry + 1)->low)
4028 ++entry;
4029 found_entry = 0;
4030 while (1)
4031 {
4032 if (pc < entry->high)
4033 {
4034 found_entry = 1;
4035 break;
4036 }
4037 if (entry == ddata->addrs)
4038 break;
4039 if ((entry - 1)->low < entry->low)
4040 break;
4041 --entry;
4042 }
4043 if (!found_entry)
4044 {
4045 *found = 0;
4046 return 0;
4047 }
4048
4049 /* We need the lines, lines_count, function_addrs,
4050 function_addrs_count fields of u. If they are not set, we need
4051 to set them. When running in threaded mode, we need to allow for
4052 the possibility that some other thread is setting them
4053 simultaneously. */
4054
4055 u = entry->u;
4056 lines = u->lines;
4057
4058 /* Skip units with no useful line number information by walking
4059 backward. Useless line number information is marked by setting
4060 lines == -1. */
4061 while (entry > ddata->addrs
4062 && pc >= (entry - 1)->low
4063 && pc < (entry - 1)->high)
4064 {
4065 if (state->threaded)
4066 lines = (struct line *) backtrace_atomic_load_pointer (&u->lines);
4067
4068 if (lines != (struct line *) (uintptr_t) -1)
4069 break;
4070
4071 --entry;
4072
4073 u = entry->u;
4074 lines = u->lines;
4075 }
4076
4077 if (state->threaded)
4078 lines = backtrace_atomic_load_pointer (&u->lines);
4079
4080 new_data = 0;
4081 if (lines == NULL)
4082 {
4083 struct function_addrs *function_addrs;
4084 size_t function_addrs_count;
4085 struct line_header lhdr;
4086 size_t count;
4087
4088 /* We have never read the line information for this unit. Read
4089 it now. */
4090
4091 function_addrs = NULL;
4092 function_addrs_count = 0;
4093 if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr,
4094 &lines, &count))
4095 {
4096 struct function_vector *pfvec;
4097
4098 /* If not threaded, reuse DDATA->FVEC for better memory
4099 consumption. */
4100 if (state->threaded)
4101 pfvec = NULL;
4102 else
4103 pfvec = &ddata->fvec;
4104 read_function_info (state, ddata, &lhdr, error_callback, data,
4105 entry->u, pfvec, &function_addrs,
4106 &function_addrs_count);
4107 free_line_header (state, &lhdr, error_callback, data);
4108 new_data = 1;
4109 }
4110
4111 /* Atomically store the information we just read into the unit.
4112 If another thread is simultaneously writing, it presumably
4113 read the same information, and we don't care which one we
4114 wind up with; we just leak the other one. We do have to
4115 write the lines field last, so that the acquire-loads above
4116 ensure that the other fields are set. */
4117
4118 if (!state->threaded)
4119 {
4120 u->lines_count = count;
4121 u->function_addrs = function_addrs;
4122 u->function_addrs_count = function_addrs_count;
4123 u->lines = lines;
4124 }
4125 else
4126 {
4127 backtrace_atomic_store_size_t (&u->lines_count, count);
4128 backtrace_atomic_store_pointer (&u->function_addrs, function_addrs);
4129 backtrace_atomic_store_size_t (&u->function_addrs_count,
4130 function_addrs_count);
4131 backtrace_atomic_store_pointer (&u->lines, lines);
4132 }
4133 }
4134
4135 /* Now all fields of U have been initialized. */
4136
4137 if (lines == (struct line *) (uintptr_t) -1)
4138 {
4139 /* If reading the line number information failed in some way,
4140 try again to see if there is a better compilation unit for
4141 this PC. */
4142 if (new_data)
4143 return dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
4144 data, found);
4145 return callback (data, pc, 0, NULL, 0, NULL);
4146 }
4147
4148 /* Search for PC within this unit. */
4149
4150 ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count,
4151 sizeof (struct line), line_search);
4152 if (ln == NULL)
4153 {
4154 /* The PC is between the low_pc and high_pc attributes of the
4155 compilation unit, but no entry in the line table covers it.
4156 This implies that the start of the compilation unit has no
4157 line number information. */
4158
4159 if (entry->u->abs_filename == NULL)
4160 {
4161 const char *filename;
4162
4163 filename = entry->u->filename;
4164 if (filename != NULL
4165 && !IS_ABSOLUTE_PATH (filename)
4166 && entry->u->comp_dir != NULL)
4167 {
4168 size_t filename_len;
4169 const char *dir;
4170 size_t dir_len;
4171 char *s;
4172
4173 filename_len = strlen (filename);
4174 dir = entry->u->comp_dir;
4175 dir_len = strlen (dir);
4176 s = (char *) backtrace_alloc (state, dir_len + filename_len + 2,
4177 error_callback, data);
4178 if (s == NULL)
4179 {
4180 *found = 0;
4181 return 0;
4182 }
4183 memcpy (s, dir, dir_len);
4184 /* FIXME: Should use backslash if DOS file system. */
4185 s[dir_len] = '/';
4186 memcpy (s + dir_len + 1, filename, filename_len + 1);
4187 filename = s;
4188 }
4189 entry->u->abs_filename = filename;
4190 }
4191
4192 return callback (data, pc, 0, entry->u->abs_filename, 0, NULL);
4193 }
4194
4195 /* Search for function name within this unit. */
4196
4197 if (entry->u->function_addrs_count == 0)
4198 return callback (data, pc, 0, ln->filename, ln->lineno, NULL);
4199
4200 p = ((struct function_addrs *)
4201 bsearch (&pc, entry->u->function_addrs,
4202 entry->u->function_addrs_count,
4203 sizeof (struct function_addrs),
4204 function_addrs_search));
4205 if (p == NULL)
4206 return callback (data, pc, 0, ln->filename, ln->lineno, NULL);
4207
4208 /* Here pc >= p->low && pc < (p + 1)->low. The function_addrs are
4209 sorted by low, so if pc > p->low we are at the end of a range of
4210 function_addrs with the same low value. If pc == p->low walk
4211 forward to the end of the range with that low value. Then walk
4212 backward and use the first range that includes pc. */
4213 while (pc == (p + 1)->low)
4214 ++p;
4215 fmatch = NULL;
4216 while (1)
4217 {
4218 if (pc < p->high)
4219 {
4220 fmatch = p;
4221 break;
4222 }
4223 if (p == entry->u->function_addrs)
4224 break;
4225 if ((p - 1)->low < p->low)
4226 break;
4227 --p;
4228 }
4229 if (fmatch == NULL)
4230 return callback (data, pc, 0, ln->filename, ln->lineno, NULL);
4231
4232 function = fmatch->function;
4233
4234 filename = ln->filename;
4235 lineno = ln->lineno;
4236
4237 ret = report_inlined_functions (pc, function, entry->u->comp_dir, callback, data,
4238 &filename, &lineno);
4239 if (ret != 0)
4240 return ret;
4241
4242 if (filename[0] != '/' && entry->u->comp_dir)
4243 {
4244 char buf[1024];
4245 snprintf (buf, 1024, "%s/%s", entry->u->comp_dir, filename);
4246 return callback (data, pc, fmatch->low, buf, lineno, function->name);
4247 }
4248 else
4249 {
4250 return callback (data, pc, fmatch->low, filename, lineno, function->name);
4251 }
4252}
4253
4254
4255/* Return the file/line information for a PC using the DWARF mapping
4256 we built earlier. */
4257
4258static int
4259dwarf_fileline (struct backtrace_state *state, uintptr_t pc,
4260 backtrace_full_callback callback,
4261 backtrace_error_callback error_callback, void *data)
4262{
4263 struct dwarf_data *ddata;
4264 int found;
4265 int ret;
4266
4267 if (!state->threaded)
4268 {
4269 for (ddata = (struct dwarf_data *) state->fileline_data;
4270 ddata != NULL;
4271 ddata = ddata->next)
4272 {
4273 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
4274 data, &found);
4275 if (ret != 0 || found)
4276 return ret;
4277 }
4278 }
4279 else
4280 {
4281 struct dwarf_data **pp;
4282
4283 pp = (struct dwarf_data **) (void *) &state->fileline_data;
4284 while (1)
4285 {
4286 ddata = backtrace_atomic_load_pointer (pp);
4287 if (ddata == NULL)
4288 break;
4289
4290 ret = dwarf_lookup_pc (state, ddata, pc, callback, error_callback,
4291 data, &found);
4292 if (ret != 0 || found)
4293 return ret;
4294
4295 pp = &ddata->next;
4296 }
4297 }
4298
4299 /* FIXME: See if any libraries have been dlopen'ed. */
4300
4301 return callback (data, pc, 0, NULL, 0, NULL);
4302}
4303
4304/* Initialize our data structures from the DWARF debug info for a
4305 file. Return NULL on failure. */
4306
4307static struct dwarf_data *
4308build_dwarf_data (struct backtrace_state *state,
4309 uintptr_t base_address,
4310 const struct dwarf_sections *dwarf_sections,
4311 int is_bigendian,
4312 struct dwarf_data *altlink,
4313 backtrace_error_callback error_callback,
4314 void *data)
4315{
4316 struct unit_addrs_vector addrs_vec;
4317 struct unit_addrs *addrs;
4318 size_t addrs_count;
4319 struct unit_vector units_vec;
4320 struct unit **units;
4321 size_t units_count;
4322 struct dwarf_data *fdata;
4323
4324 if (!build_address_map (state, base_address, dwarf_sections, is_bigendian,
4325 altlink, error_callback, data, &addrs_vec,
4326 &units_vec))
4327 return NULL;
4328
4329 if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data))
4330 return NULL;
4331 if (!backtrace_vector_release (state, &units_vec.vec, error_callback, data))
4332 return NULL;
4333 addrs = (struct unit_addrs *) addrs_vec.vec.base;
4334 units = (struct unit **) units_vec.vec.base;
4335 addrs_count = addrs_vec.count;
4336 units_count = units_vec.count;
4337 backtrace_qsort (addrs, addrs_count, sizeof (struct unit_addrs),
4338 unit_addrs_compare);
4339 /* No qsort for units required, already sorted. */
4340
4341 fdata = ((struct dwarf_data *)
4342 backtrace_alloc (state, sizeof (struct dwarf_data),
4343 error_callback, data));
4344 if (fdata == NULL)
4345 return NULL;
4346
4347 fdata->next = NULL;
4348 fdata->altlink = altlink;
4349 fdata->base_address = base_address;
4350 fdata->addrs = addrs;
4351 fdata->addrs_count = addrs_count;
4352 fdata->units = units;
4353 fdata->units_count = units_count;
4354 fdata->dwarf_sections = *dwarf_sections;
4355 fdata->is_bigendian = is_bigendian;
4356 memset (&fdata->fvec, 0, sizeof fdata->fvec);
4357
4358 return fdata;
4359}
4360
4361/* Build our data structures from the DWARF sections for a module.
4362 Set FILELINE_FN and STATE->FILELINE_DATA. Return 1 on success, 0
4363 on failure. */
4364
4365int
4366backtrace_dwarf_add (struct backtrace_state *state,
4367 uintptr_t base_address,
4368 const struct dwarf_sections *dwarf_sections,
4369 int is_bigendian,
4370 struct dwarf_data *fileline_altlink,
4371 backtrace_error_callback error_callback,
4372 void *data, fileline *fileline_fn,
4373 struct dwarf_data **fileline_entry)
4374{
4375 struct dwarf_data *fdata;
4376
4377 fdata = build_dwarf_data (state, base_address, dwarf_sections, is_bigendian,
4378 fileline_altlink, error_callback, data);
4379 if (fdata == NULL)
4380 return 0;
4381
4382 if (fileline_entry != NULL)
4383 *fileline_entry = fdata;
4384
4385 if (!state->threaded)
4386 {
4387 struct dwarf_data **pp;
4388
4389 for (pp = (struct dwarf_data **) (void *) &state->fileline_data;
4390 *pp != NULL;
4391 pp = &(*pp)->next)
4392 ;
4393 *pp = fdata;
4394 }
4395 else
4396 {
4397 while (1)
4398 {
4399 struct dwarf_data **pp;
4400
4401 pp = (struct dwarf_data **) (void *) &state->fileline_data;
4402
4403 while (1)
4404 {
4405 struct dwarf_data *p;
4406
4407 p = backtrace_atomic_load_pointer (pp);
4408
4409 if (p == NULL)
4410 break;
4411
4412 pp = &p->next;
4413 }
4414
4415 if (__sync_bool_compare_and_swap (pp, NULL, fdata))
4416 break;
4417 }
4418 }
4419
4420 *fileline_fn = dwarf_fileline;
4421
4422 return 1;
4423}
4424
4425}