unwind_128.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. From 0a186eafebf26ca01879827a4cc95cc274791334 Mon Sep 17 00:00:00 2001
  2. From: Bjorn Neergaard <bjorn@neersighted.com>
  3. Date: Sat, 9 Feb 2019 19:39:23 +0000
  4. Subject: [PATCH] Fix cross-compiling i686-pc-windows-gnu from Linux
  5. This is still very rough and serves as a proof-of-concept for fixing
  6. Linux -> 32-bit MinGW cross compilation workflow. Currently, clang and
  7. GCC's MinGW targets both only support DW2 (DWARF) or SJLJ (Set Jump Long
  8. Jump) unwinding on 32-bit Windows.
  9. The default for GCC (and the way it is shipped on every major distro) is
  10. to use SJLJ on Windows, as DWARF cannot traverse non-DWARF frames. This
  11. would work fine, except for the fact that libgcc (our C runtime on the
  12. MinGW platform) exports symbols under a different name when configured
  13. to use SJLJ-style unwinding, and uses a preprocessor macro internally to
  14. alias them.
  15. Because of this, we have to detect this scenario and link to the correct
  16. symbols ourselves. Linking has been tested with a full bootstrap on both
  17. x86_64-unknown-linux-gnu and i686-pc-windows-gnu, as well as
  18. cross-compilation of some of my own projects.
  19. Obviously, the detection is a bit unrefined. Right now we
  20. unconditionally use SJLJ when compiling Linux -> MinGW. I'd like to add
  21. feature detection using compiler build flags or autotools-style
  22. compilation and object analysis. Input on the best way to proceed here
  23. is welcome.
  24. Also, currently there is copy-pasted/duplicated code in libunwind.
  25. Ideally, this could be reduced, but this would likely require a
  26. rethinking of how iOS is special-cased above, to avoid further
  27. duplication. Input on how to best structure this file is requested.
  28. diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs
  29. index 11d9154ba6..bd8ff844f7 100644
  30. --- a/src/bootstrap/compile.rs
  31. +++ b/src/bootstrap/compile.rs
  32. @@ -154,6 +154,11 @@ pub fn std_cargo(builder: &Builder,
  33. } else {
  34. let mut features = builder.std_features();
  35. + // FIXME: Temporary detection of SJLJ MinGW compilers.
  36. + if builder.config.build.contains("linux") && target == "i686-pc-windows-gnu" {
  37. + features.push_str(" sjlj_eh");
  38. + }
  39. +
  40. // When doing a local rebuild we tell cargo that we're stage1 rather than
  41. // stage0. This works fine if the local rust and being-built rust have the
  42. // same view of what the default allocator is, but fails otherwise. Since
  43. diff --git a/src/libstd/Cargo.toml b/src/libstd/Cargo.toml
  44. index 5a2dce5930..e1c876f503 100644
  45. --- a/src/libstd/Cargo.toml
  46. +++ b/src/libstd/Cargo.toml
  47. @@ -50,3 +50,4 @@ force_alloc_system = []
  48. panic-unwind = ["panic_unwind"]
  49. profiler = ["profiler_builtins"]
  50. wasm_syscall = []
  51. +sjlj_eh = ["unwind/sjlj_eh"]
  52. diff --git a/src/libunwind/Cargo.toml b/src/libunwind/Cargo.toml
  53. index 4760461df6..27c7303604 100644
  54. --- a/src/libunwind/Cargo.toml
  55. +++ b/src/libunwind/Cargo.toml
  56. @@ -15,3 +15,6 @@ doc = false
  57. core = { path = "../libcore" }
  58. libc = { path = "../rustc/libc_shim" }
  59. compiler_builtins = { path = "../rustc/compiler_builtins_shim" }
  60. +
  61. +[features]
  62. +sjlj_eh = []
  63. diff --git a/src/libunwind/libunwind.rs b/src/libunwind/libunwind.rs
  64. index 73a259bd44..ff3404864f 100644
  65. --- a/src/libunwind/libunwind.rs
  66. +++ b/src/libunwind/libunwind.rs
  67. @@ -10,11 +10,6 @@
  68. #![allow(bad_style)]
  69. -macro_rules! cfg_if {
  70. - ( $( if #[cfg( $meta:meta )] { $($it1:item)* } else { $($it2:item)* } )* ) =>
  71. - ( $( $( #[cfg($meta)] $it1)* $( #[cfg(not($meta))] $it2)* )* )
  72. -}
  73. -
  74. use libc::{c_int, c_void, uintptr_t};
  75. #[repr(C)]
  76. @@ -83,8 +78,8 @@ pub enum _Unwind_Context {}
  77. pub type _Unwind_Exception_Cleanup_Fn = extern "C" fn(unwind_code: _Unwind_Reason_Code,
  78. exception: *mut _Unwind_Exception);
  79. extern "C" {
  80. - #[unwind(allowed)]
  81. - pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
  82. + #[cfg_attr(stage0, unwind)]
  83. + #[cfg_attr(not(stage0), unwind(allowed))]
  84. pub fn _Unwind_DeleteException(exception: *mut _Unwind_Exception);
  85. pub fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> *mut c_void;
  86. pub fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr;
  87. @@ -216,26 +211,52 @@ if #[cfg(all(any(target_os = "ios", target_os = "netbsd", not(target_arch = "arm
  88. pc
  89. }
  90. }
  91. +} // cfg_if!
  92. -if #[cfg(not(all(target_os = "ios", target_arch = "arm")))] {
  93. - // Not 32-bit iOS
  94. +cfg_if! {
  95. +if #[cfg(all(target_os = "ios", target_arch = "arm"))] {
  96. + // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
  97. extern "C" {
  98. - #[unwind(allowed)]
  99. - pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
  100. + #[cfg_attr(stage0, unwind)]
  101. + #[cfg_attr(not(stage0), unwind(allowed))]
  102. + pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
  103. + pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
  104. + }
  105. +
  106. + #[inline]
  107. + pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
  108. + _Unwind_SjLj_RaiseException(exc)
  109. + }
  110. +
  111. +} else if #[cfg(feature = "sjlj_eh")] {
  112. + extern "C" {
  113. + #[cfg_attr(stage0, unwind)]
  114. + #[cfg_attr(not(stage0), unwind(allowed))]
  115. + pub fn _Unwind_SjLj_Resume(e: *mut _Unwind_Exception) -> !;
  116. + pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
  117. pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
  118. trace_argument: *mut c_void)
  119. -> _Unwind_Reason_Code;
  120. }
  121. -} else {
  122. - // 32-bit iOS uses SjLj and does not provide _Unwind_Backtrace()
  123. - extern "C" {
  124. - #[unwind(allowed)]
  125. - pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
  126. +
  127. + #[inline]
  128. + pub unsafe fn _Unwind_Resume(exc: *mut _Unwind_Exception) -> ! {
  129. + _Unwind_SjLj_Resume(exc)
  130. }
  131. #[inline]
  132. pub unsafe fn _Unwind_RaiseException(exc: *mut _Unwind_Exception) -> _Unwind_Reason_Code {
  133. _Unwind_SjLj_RaiseException(exc)
  134. }
  135. +} else {
  136. + extern "C" {
  137. + #[cfg_attr(stage0, unwind)]
  138. + #[cfg_attr(not(stage0), unwind(allowed))]
  139. + pub fn _Unwind_Resume(exception: *mut _Unwind_Exception) -> !;
  140. + pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
  141. + pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
  142. + trace_argument: *mut c_void)
  143. + -> _Unwind_Reason_Code;
  144. + }
  145. }
  146. } // cfg_if!
  147. --
  148. 2.20.1