Skip to content

Commit 9329776

Browse files
committed
[Backtracing] Use C++ interop to fix elf.h issue on Linux.
We have some problems on Linux where Glibc pulls in `<elf.h>` and then we end up with conflicting definitions. Fix by using C++ interop and putting our definitions into a namespace. rdar://137201928
1 parent 293842a commit 9329776

File tree

19 files changed

+603
-114
lines changed

19 files changed

+603
-114
lines changed

include/swift/Runtime/CrashInfo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
namespace swift {
2525
namespace runtime {
2626
namespace backtrace {
27-
extern "C" {
2827
#endif
2928

3029
// Note: The "pointers" below are pointers in a different process's address
@@ -89,7 +88,6 @@ struct thread {
8988
#endif
9089

9190
#ifdef __cplusplus
92-
} // extern "C"
9391
} // namespace backtrace
9492
} // namespace runtime
9593
} // namespace swift

stdlib/public/Backtracing/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ set(BACKTRACING_SOURCES
3636
Image.swift
3737
ImageSource.swift
3838
MemoryImageSource.swift
39+
Libc.swift
3940
MemoryReader.swift
4041
ProcMapsScanner.swift
4142
Registers.swift
43+
Runtime.swift
4244
SymbolicatedBacktrace.swift
4345
Utils.swift
4446
Win32Extras.cpp
@@ -47,6 +49,7 @@ set(BACKTRACING_SOURCES
4749
)
4850

4951
set(BACKTRACING_COMPILE_FLAGS
52+
"-cxx-interoperability-mode=default"
5053
"-Xfrontend;-experimental-spi-only-imports"
5154
"-Xcc;-I${SWIFT_SOURCE_DIR}/include"
5255
"-Xcc;-I${CMAKE_BINARY_DIR}/include"

stdlib/public/Backtracing/Compression.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ enum CompressedImageSourceError: Error {
5050
case outputOverrun
5151
}
5252

53+
let zlib_stream_init = swift.runtime.zlib_stream_init
54+
let lzma_stream_init = swift.runtime.lzma_stream_init
55+
5356
// .. CompressedStream .........................................................
5457

5558
protocol CompressedStream {

stdlib/public/Backtracing/Context.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,27 @@
1919

2020
import Swift
2121

22-
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
22+
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
2323
internal import Darwin
24+
#elseif os(Windows)
25+
internal import ucrt
26+
#elseif canImport(Glibc)
27+
internal import Glibc
28+
#elseif canImport(Musl)
29+
internal import Musl
30+
#endif
31+
32+
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
2433
internal import BacktracingImpl.OS.Darwin
25-
#elseif os(Linux)
26-
internal import BacktracingImpl.OS.Linux
2734
#endif
2835

2936
internal import BacktracingImpl.FixedLayout
3037

38+
typealias x86_64_gprs = swift.runtime.backtrace.x86_64_gprs
39+
typealias i386_gprs = swift.runtime.backtrace.i386_gprs
40+
typealias arm64_gprs = swift.runtime.backtrace.arm64_gprs
41+
typealias arm_gprs = swift.runtime.backtrace.arm_gprs
42+
3143
@_spi(Contexts) public enum ContextError: Error {
3244
case unableToFormTLSAddress
3345
}

stdlib/public/Backtracing/Dwarf.swift

Lines changed: 253 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,259 @@
2020
import Swift
2121

2222
internal import BacktracingImpl.ImageFormats.Dwarf
23-
internal import BacktracingImpl.Runtime
23+
24+
// .. Use *our* Dwarf definitions ..............................................
25+
26+
// To avoid confusion with other similar sets of definitions, we've put ours
27+
// into a C++ wrapper, which means we need aliases here.
28+
29+
typealias Dwarf_Byte = swift.runtime.Dwarf_Byte
30+
typealias Dwarf_Half = swift.runtime.Dwarf_Half
31+
typealias Dwarf_Word = swift.runtime.Dwarf_Word
32+
typealias Dwarf_Xword = swift.runtime.Dwarf_Xword
33+
typealias Dwarf_Sbyte = swift.runtime.Dwarf_Sbyte
34+
typealias Dwarf_Sword = swift.runtime.Dwarf_Sword
35+
typealias Dwarf_Sxword = swift.runtime.Dwarf_Sxword
36+
37+
typealias Dwarf32_Offset = swift.runtime.Dwarf32_Offset
38+
typealias Dwarf32_Size = swift.runtime.Dwarf32_Size
39+
typealias Dwarf32_Length = swift.runtime.Dwarf32_Length
40+
41+
typealias Dwarf64_Offset = swift.runtime.Dwarf64_Offset
42+
typealias Dwarf64_Size = swift.runtime.Dwarf64_Size
43+
typealias Dwarf64_Length = swift.runtime.Dwarf64_Length
44+
45+
typealias Dwarf_UnitType = swift.runtime.Dwarf_UnitType
46+
typealias Dwarf_Tag = swift.runtime.Dwarf_Tag
47+
typealias Dwarf_ChildDetermination = swift.runtime.Dwarf_ChildDetermination
48+
typealias Dwarf_Attribute = swift.runtime.Dwarf_Attribute
49+
typealias Dwarf_Form = swift.runtime.Dwarf_Form
50+
51+
let DW_OP_addr = swift.runtime.DW_OP_addr
52+
let DW_OP_deref = swift.runtime.DW_OP_deref
53+
let DW_OP_const1u = swift.runtime.DW_OP_const1u
54+
let DW_OP_const1s = swift.runtime.DW_OP_const1s
55+
let DW_OP_const2u = swift.runtime.DW_OP_const2u
56+
let DW_OP_const2s = swift.runtime.DW_OP_const2s
57+
let DW_OP_const4u = swift.runtime.DW_OP_const4u
58+
let DW_OP_const4s = swift.runtime.DW_OP_const4s
59+
let DW_OP_const8u = swift.runtime.DW_OP_const8u
60+
let DW_OP_const8s = swift.runtime.DW_OP_const8s
61+
let DW_OP_constu = swift.runtime.DW_OP_constu
62+
let DW_OP_consts = swift.runtime.DW_OP_consts
63+
let DW_OP_dup = swift.runtime.DW_OP_dup
64+
let DW_OP_drop = swift.runtime.DW_OP_drop
65+
let DW_OP_over = swift.runtime.DW_OP_over
66+
let DW_OP_pick = swift.runtime.DW_OP_pick
67+
let DW_OP_swap = swift.runtime.DW_OP_swap
68+
let DW_OP_rot = swift.runtime.DW_OP_rot
69+
let DW_OP_xderef = swift.runtime.DW_OP_xderef
70+
let DW_OP_abs = swift.runtime.DW_OP_abs
71+
let DW_OP_and = swift.runtime.DW_OP_and
72+
let DW_OP_div = swift.runtime.DW_OP_div
73+
let DW_OP_minus = swift.runtime.DW_OP_minus
74+
let DW_OP_mod = swift.runtime.DW_OP_mod
75+
let DW_OP_mul = swift.runtime.DW_OP_mul
76+
let DW_OP_neg = swift.runtime.DW_OP_neg
77+
let DW_OP_not = swift.runtime.DW_OP_not
78+
let DW_OP_or = swift.runtime.DW_OP_or
79+
let DW_OP_plus = swift.runtime.DW_OP_plus
80+
let DW_OP_plus_uconst = swift.runtime.DW_OP_plus_uconst
81+
let DW_OP_shl = swift.runtime.DW_OP_shl
82+
let DW_OP_shr = swift.runtime.DW_OP_shr
83+
let DW_OP_shra = swift.runtime.DW_OP_shra
84+
let DW_OP_xor = swift.runtime.DW_OP_xor
85+
let DW_OP_bra = swift.runtime.DW_OP_bra
86+
let DW_OP_eq = swift.runtime.DW_OP_eq
87+
let DW_OP_ge = swift.runtime.DW_OP_ge
88+
let DW_OP_gt = swift.runtime.DW_OP_gt
89+
let DW_OP_le = swift.runtime.DW_OP_le
90+
let DW_OP_lt = swift.runtime.DW_OP_lt
91+
let DW_OP_ne = swift.runtime.DW_OP_ne
92+
let DW_OP_skip = swift.runtime.DW_OP_skip
93+
let DW_OP_lit0 = swift.runtime.DW_OP_lit0
94+
let DW_OP_lit1 = swift.runtime.DW_OP_lit1
95+
let DW_OP_lit2 = swift.runtime.DW_OP_lit2
96+
let DW_OP_lit3 = swift.runtime.DW_OP_lit3
97+
let DW_OP_lit4 = swift.runtime.DW_OP_lit4
98+
let DW_OP_lit5 = swift.runtime.DW_OP_lit5
99+
let DW_OP_lit6 = swift.runtime.DW_OP_lit6
100+
let DW_OP_lit7 = swift.runtime.DW_OP_lit7
101+
let DW_OP_lit8 = swift.runtime.DW_OP_lit8
102+
let DW_OP_lit9 = swift.runtime.DW_OP_lit9
103+
let DW_OP_lit10 = swift.runtime.DW_OP_lit10
104+
let DW_OP_lit11 = swift.runtime.DW_OP_lit11
105+
let DW_OP_lit12 = swift.runtime.DW_OP_lit12
106+
let DW_OP_lit13 = swift.runtime.DW_OP_lit13
107+
let DW_OP_lit14 = swift.runtime.DW_OP_lit14
108+
let DW_OP_lit15 = swift.runtime.DW_OP_lit15
109+
let DW_OP_lit16 = swift.runtime.DW_OP_lit16
110+
let DW_OP_lit17 = swift.runtime.DW_OP_lit17
111+
let DW_OP_lit18 = swift.runtime.DW_OP_lit18
112+
let DW_OP_lit19 = swift.runtime.DW_OP_lit19
113+
let DW_OP_lit20 = swift.runtime.DW_OP_lit20
114+
let DW_OP_lit21 = swift.runtime.DW_OP_lit21
115+
let DW_OP_lit22 = swift.runtime.DW_OP_lit22
116+
let DW_OP_lit23 = swift.runtime.DW_OP_lit23
117+
let DW_OP_lit24 = swift.runtime.DW_OP_lit24
118+
let DW_OP_lit25 = swift.runtime.DW_OP_lit25
119+
let DW_OP_lit26 = swift.runtime.DW_OP_lit26
120+
let DW_OP_lit27 = swift.runtime.DW_OP_lit27
121+
let DW_OP_lit28 = swift.runtime.DW_OP_lit28
122+
let DW_OP_lit29 = swift.runtime.DW_OP_lit29
123+
let DW_OP_lit30 = swift.runtime.DW_OP_lit30
124+
let DW_OP_lit31 = swift.runtime.DW_OP_lit31
125+
126+
let DW_OP_reg0 = swift.runtime.DW_OP_reg0
127+
let DW_OP_reg1 = swift.runtime.DW_OP_reg1
128+
let DW_OP_reg2 = swift.runtime.DW_OP_reg2
129+
let DW_OP_reg3 = swift.runtime.DW_OP_reg3
130+
let DW_OP_reg4 = swift.runtime.DW_OP_reg4
131+
let DW_OP_reg5 = swift.runtime.DW_OP_reg5
132+
let DW_OP_reg6 = swift.runtime.DW_OP_reg6
133+
let DW_OP_reg7 = swift.runtime.DW_OP_reg7
134+
let DW_OP_reg8 = swift.runtime.DW_OP_reg8
135+
let DW_OP_reg9 = swift.runtime.DW_OP_reg9
136+
let DW_OP_reg10 = swift.runtime.DW_OP_reg10
137+
let DW_OP_reg11 = swift.runtime.DW_OP_reg11
138+
let DW_OP_reg12 = swift.runtime.DW_OP_reg12
139+
let DW_OP_reg13 = swift.runtime.DW_OP_reg13
140+
let DW_OP_reg14 = swift.runtime.DW_OP_reg14
141+
let DW_OP_reg15 = swift.runtime.DW_OP_reg15
142+
let DW_OP_reg16 = swift.runtime.DW_OP_reg16
143+
let DW_OP_reg17 = swift.runtime.DW_OP_reg17
144+
let DW_OP_reg18 = swift.runtime.DW_OP_reg18
145+
let DW_OP_reg19 = swift.runtime.DW_OP_reg19
146+
let DW_OP_reg20 = swift.runtime.DW_OP_reg20
147+
let DW_OP_reg21 = swift.runtime.DW_OP_reg21
148+
let DW_OP_reg22 = swift.runtime.DW_OP_reg22
149+
let DW_OP_reg23 = swift.runtime.DW_OP_reg23
150+
let DW_OP_reg24 = swift.runtime.DW_OP_reg24
151+
let DW_OP_reg25 = swift.runtime.DW_OP_reg25
152+
let DW_OP_reg26 = swift.runtime.DW_OP_reg26
153+
let DW_OP_reg27 = swift.runtime.DW_OP_reg27
154+
let DW_OP_reg28 = swift.runtime.DW_OP_reg28
155+
let DW_OP_reg29 = swift.runtime.DW_OP_reg29
156+
let DW_OP_reg30 = swift.runtime.DW_OP_reg30
157+
let DW_OP_reg31 = swift.runtime.DW_OP_reg31
158+
159+
let DW_OP_breg0 = swift.runtime.DW_OP_breg0
160+
let DW_OP_breg1 = swift.runtime.DW_OP_breg1
161+
let DW_OP_breg2 = swift.runtime.DW_OP_breg2
162+
let DW_OP_breg3 = swift.runtime.DW_OP_breg3
163+
let DW_OP_breg4 = swift.runtime.DW_OP_breg4
164+
let DW_OP_breg5 = swift.runtime.DW_OP_breg5
165+
let DW_OP_breg6 = swift.runtime.DW_OP_breg6
166+
let DW_OP_breg7 = swift.runtime.DW_OP_breg7
167+
let DW_OP_breg8 = swift.runtime.DW_OP_breg8
168+
let DW_OP_breg9 = swift.runtime.DW_OP_breg9
169+
let DW_OP_breg10 = swift.runtime.DW_OP_breg10
170+
let DW_OP_breg11 = swift.runtime.DW_OP_breg11
171+
let DW_OP_breg12 = swift.runtime.DW_OP_breg12
172+
let DW_OP_breg13 = swift.runtime.DW_OP_breg13
173+
let DW_OP_breg14 = swift.runtime.DW_OP_breg14
174+
let DW_OP_breg15 = swift.runtime.DW_OP_breg15
175+
let DW_OP_breg16 = swift.runtime.DW_OP_breg16
176+
let DW_OP_breg17 = swift.runtime.DW_OP_breg17
177+
let DW_OP_breg18 = swift.runtime.DW_OP_breg18
178+
let DW_OP_breg19 = swift.runtime.DW_OP_breg19
179+
let DW_OP_breg20 = swift.runtime.DW_OP_breg20
180+
let DW_OP_breg21 = swift.runtime.DW_OP_breg21
181+
let DW_OP_breg22 = swift.runtime.DW_OP_breg22
182+
let DW_OP_breg23 = swift.runtime.DW_OP_breg23
183+
let DW_OP_breg24 = swift.runtime.DW_OP_breg24
184+
let DW_OP_breg25 = swift.runtime.DW_OP_breg25
185+
let DW_OP_breg26 = swift.runtime.DW_OP_breg26
186+
let DW_OP_breg27 = swift.runtime.DW_OP_breg27
187+
let DW_OP_breg28 = swift.runtime.DW_OP_breg28
188+
let DW_OP_breg29 = swift.runtime.DW_OP_breg29
189+
let DW_OP_breg30 = swift.runtime.DW_OP_breg30
190+
let DW_OP_breg31 = swift.runtime.DW_OP_breg31
191+
let DW_OP_regx = swift.runtime.DW_OP_regx
192+
let DW_OP_fbreg = swift.runtime.DW_OP_fbreg
193+
let DW_OP_bregx = swift.runtime.DW_OP_bregx
194+
let DW_OP_piece = swift.runtime.DW_OP_piece
195+
let DW_OP_deref_size = swift.runtime.DW_OP_deref_size
196+
let DW_OP_xderef_size = swift.runtime.DW_OP_xderef_size
197+
let DW_OP_nop = swift.runtime.DW_OP_nop
198+
let DW_OP_push_object_address = swift.runtime.DW_OP_push_object_address
199+
let DW_OP_call2 = swift.runtime.DW_OP_call2
200+
let DW_OP_call4 = swift.runtime.DW_OP_call4
201+
let DW_OP_call_ref = swift.runtime.DW_OP_call_ref
202+
let DW_OP_form_tls_address = swift.runtime.DW_OP_form_tls_address
203+
let DW_OP_call_frame_cfa = swift.runtime.DW_OP_call_frame_cfa
204+
let DW_OP_bit_piece = swift.runtime.DW_OP_bit_piece
205+
let DW_OP_implicit_value = swift.runtime.DW_OP_implicit_value
206+
let DW_OP_stack_value = swift.runtime.DW_OP_stack_value
207+
let DW_OP_implicit_pointer = swift.runtime.DW_OP_implicit_pointer
208+
let DW_OP_addrx = swift.runtime.DW_OP_addrx
209+
let DW_OP_constx = swift.runtime.DW_OP_constx
210+
let DW_OP_entry_value = swift.runtime.DW_OP_entry_value
211+
let DW_OP_const_type = swift.runtime.DW_OP_const_type
212+
let DW_OP_regval_type = swift.runtime.DW_OP_regval_type
213+
let DW_OP_deref_type = swift.runtime.DW_OP_deref_type
214+
let DW_OP_xderef_type = swift.runtime.DW_OP_xderef_type
215+
let DW_OP_convert = swift.runtime.DW_OP_convert
216+
let DW_OP_reinterpret = swift.runtime.DW_OP_reinterpret
217+
let DW_OP_lo_user = swift.runtime.DW_OP_lo_user
218+
let DW_OP_hi_user = swift.runtime.DW_OP_hi_user
219+
220+
typealias Dwarf_LNS_Opcode = swift.runtime.Dwarf_LNS_Opcode
221+
typealias Dwarf_LNE_Opcode = swift.runtime.Dwarf_LNE_Opcode
222+
typealias Dwarf_Lhdr_Format = swift.runtime.Dwarf_Lhdr_Format
223+
224+
typealias DWARF32_Lhdr = swift.runtime.DWARF32_Lhdr
225+
typealias DWARF64_Lhdr = swift.runtime.DWARF64_Lhdr
226+
227+
let DW_CFA_advance_loc = swift.runtime.DW_CFA_advance_loc
228+
let DW_CFA_offset = swift.runtime.DW_CFA_offset
229+
let DW_CFA_restore = swift.runtime.DW_CFA_restore
230+
let DW_CFA_nop = swift.runtime.DW_CFA_nop
231+
let DW_CFA_set_loc = swift.runtime.DW_CFA_set_loc
232+
let DW_CFA_advance_loc1 = swift.runtime.DW_CFA_advance_loc1
233+
let DW_CFA_advance_loc2 = swift.runtime.DW_CFA_advance_loc2
234+
let DW_CFA_advance_loc4 = swift.runtime.DW_CFA_advance_loc4
235+
let DW_CFA_offset_extended = swift.runtime.DW_CFA_offset_extended
236+
let DW_CFA_restore_extended = swift.runtime.DW_CFA_restore_extended
237+
let DW_CFA_undefined = swift.runtime.DW_CFA_undefined
238+
let DW_CFA_same_value = swift.runtime.DW_CFA_same_value
239+
let DW_CFA_register = swift.runtime.DW_CFA_register
240+
let DW_CFA_remember_state = swift.runtime.DW_CFA_remember_state
241+
let DW_CFA_restore_state = swift.runtime.DW_CFA_restore_state
242+
let DW_CFA_def_cfa = swift.runtime.DW_CFA_def_cfa
243+
let DW_CFA_def_cfa_register = swift.runtime.DW_CFA_def_cfa_register
244+
let DW_CFA_def_cfa_offset = swift.runtime.DW_CFA_def_cfa_offset
245+
let DW_CFA_def_cfa_expression = swift.runtime.DW_CFA_def_cfa_expression
246+
let DW_CFA_expression = swift.runtime.DW_CFA_expression
247+
let DW_CFA_offset_extended_sf = swift.runtime.DW_CFA_offset_extended_sf
248+
let DW_CFA_def_cfa_sf = swift.runtime.DW_CFA_def_cfa_sf
249+
let DW_CFA_def_cfa_offset_sf = swift.runtime.DW_CFA_def_cfa_offset_sf
250+
let DW_CFA_val_offset = swift.runtime.DW_CFA_val_offset
251+
let DW_CFA_val_offset_sf = swift.runtime.DW_CFA_val_offset_sf
252+
let DW_CFA_val_expression = swift.runtime.DW_CFA_val_expression
253+
let DW_CFA_lo_user = swift.runtime.DW_CFA_lo_user
254+
let DW_CFA_hi_user = swift.runtime.DW_CFA_hi_user
255+
256+
typealias Dwarf_RLE_Entry = swift.runtime.Dwarf_RLE_Entry
257+
typealias Dwarf32_CIEHdr = swift.runtime.Dwarf32_CIEHdr
258+
typealias Dwarf64_CIEHdr = swift.runtime.Dwarf64_CIEHdr
259+
typealias Dwarf32_FDEHdr = swift.runtime.Dwarf32_FDEHdr
260+
261+
typealias EHFrameHdr = swift.runtime.EHFrameHdr
262+
typealias EHFrameEncoding = swift.runtime.EHFrameEncoding
263+
264+
let DW_EH_PE_omit = swift.runtime.DW_EH_PE_omit
265+
let DW_EH_PE_uleb128 = swift.runtime.DW_EH_PE_uleb128
266+
let DW_EH_PE_udata2 = swift.runtime.DW_EH_PE_udata2
267+
let DW_EH_PE_udata4 = swift.runtime.DW_EH_PE_udata4
268+
let DW_EH_PE_udata8 = swift.runtime.DW_EH_PE_udata8
269+
let DW_EH_PE_sleb128 = swift.runtime.DW_EH_PE_sleb128
270+
let DW_EH_PE_sdata2 = swift.runtime.DW_EH_PE_sdata2
271+
let DW_EH_PE_sdata4 = swift.runtime.DW_EH_PE_sdata4
272+
let DW_EH_PE_sdata8 = swift.runtime.DW_EH_PE_sdata8
273+
let DW_EH_PE_absptr = swift.runtime.DW_EH_PE_absptr
274+
let DW_EH_PE_pcrel = swift.runtime.DW_EH_PE_pcrel
275+
let DW_EH_PE_datarel = swift.runtime.DW_EH_PE_datarel
24276

25277
// .. Dwarf specific errors ....................................................
26278

0 commit comments

Comments
 (0)