pf2 0.9.0 → 1.0.0.alpha1

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