|
1 | 1 | //! A helper class for dealing with static archives
|
2 | 2 |
|
3 |
| -use std::ffi::{CStr, CString}; |
| 3 | +use std::env; |
| 4 | +use std::ffi::{CStr, CString, OsString}; |
4 | 5 | use std::io;
|
5 | 6 | use std::mem;
|
6 | 7 | use std::path::{Path, PathBuf};
|
@@ -158,54 +159,127 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
|
158 | 159 | output_path.with_extension("lib")
|
159 | 160 | };
|
160 | 161 |
|
161 |
| - // we've checked for \0 characters in the library name already |
162 |
| - let dll_name_z = CString::new(lib_name).unwrap(); |
163 |
| - // All import names are Rust identifiers and therefore cannot contain \0 characters. |
164 |
| - // FIXME: when support for #[link_name] implemented, ensure that import.name values don't |
165 |
| - // have any \0 characters |
166 |
| - let import_name_and_ordinal_vector: Vec<(CString, Option<u16>)> = dll_imports |
| 162 | + let mingw_gnu_toolchain = self.config.sess.target.llvm_target.ends_with("pc-windows-gnu"); |
| 163 | + |
| 164 | + let import_name_and_ordinal_vector: Vec<(String, Option<u16>)> = dll_imports |
167 | 165 | .iter()
|
168 | 166 | .map(|import: &DllImport| {
|
169 | 167 | if self.config.sess.target.arch == "x86" {
|
170 |
| - (LlvmArchiveBuilder::i686_decorated_name(import), import.ordinal) |
| 168 | + ( |
| 169 | + LlvmArchiveBuilder::i686_decorated_name(import, mingw_gnu_toolchain), |
| 170 | + import.ordinal, |
| 171 | + ) |
171 | 172 | } else {
|
172 |
| - (CString::new(import.name.to_string()).unwrap(), import.ordinal) |
| 173 | + (import.name.to_string(), import.ordinal) |
173 | 174 | }
|
174 | 175 | })
|
175 | 176 | .collect();
|
176 | 177 |
|
177 |
| - let output_path_z = rustc_fs_util::path_to_c_string(&output_path); |
| 178 | + if mingw_gnu_toolchain { |
| 179 | + // The binutils linker used on -windows-gnu targets cannot read the import |
| 180 | + // libraries generated by LLVM: in our attempts, the linker produced an .EXE |
| 181 | + // that loaded but crashed with an AV upon calling one of the imported |
| 182 | + // functions. Therefore, use binutils to create the import library instead, |
| 183 | + // by writing a .DEF file to the temp dir and calling binutils's dlltool. |
| 184 | + let def_file_path = |
| 185 | + tmpdir.as_ref().join(format!("{}_imports", lib_name)).with_extension("def"); |
| 186 | + |
| 187 | + let def_file_content = format!( |
| 188 | + "EXPORTS\n{}", |
| 189 | + import_name_and_ordinal_vector |
| 190 | + .into_iter() |
| 191 | + .map(|(name, ordinal)| { |
| 192 | + match ordinal { |
| 193 | + Some(n) => format!("{} @{} NONAME", name, n), |
| 194 | + None => name, |
| 195 | + } |
| 196 | + }) |
| 197 | + .collect::<Vec<String>>() |
| 198 | + .join("\n") |
| 199 | + ); |
178 | 200 |
|
179 |
| - tracing::trace!("invoking LLVMRustWriteImportLibrary"); |
180 |
| - tracing::trace!(" dll_name {:#?}", dll_name_z); |
181 |
| - tracing::trace!(" output_path {}", output_path.display()); |
182 |
| - tracing::trace!( |
183 |
| - " import names: {}", |
184 |
| - dll_imports.iter().map(|import| import.name.to_string()).collect::<Vec<_>>().join(", "), |
185 |
| - ); |
| 201 | + match std::fs::write(&def_file_path, def_file_content) { |
| 202 | + Ok(_) => {} |
| 203 | + Err(e) => { |
| 204 | + self.config.sess.fatal(&format!("Error writing .DEF file: {}", e)); |
| 205 | + } |
| 206 | + }; |
186 | 207 |
|
187 |
| - let ffi_exports: Vec<LLVMRustCOFFShortExport> = import_name_and_ordinal_vector |
188 |
| - .iter() |
189 |
| - .map(|(name_z, ordinal)| LLVMRustCOFFShortExport::new(name_z.as_ptr(), *ordinal)) |
190 |
| - .collect(); |
191 |
| - let result = unsafe { |
192 |
| - crate::llvm::LLVMRustWriteImportLibrary( |
193 |
| - dll_name_z.as_ptr(), |
194 |
| - output_path_z.as_ptr(), |
195 |
| - ffi_exports.as_ptr(), |
196 |
| - ffi_exports.len(), |
197 |
| - llvm_machine_type(&self.config.sess.target.arch) as u16, |
198 |
| - !self.config.sess.target.is_like_msvc, |
199 |
| - ) |
200 |
| - }; |
| 208 | + let dlltool = find_binutils_dlltool(self.config.sess); |
| 209 | + let result = std::process::Command::new(dlltool) |
| 210 | + .args([ |
| 211 | + "-d", |
| 212 | + def_file_path.to_str().unwrap(), |
| 213 | + "-D", |
| 214 | + lib_name, |
| 215 | + "-l", |
| 216 | + output_path.to_str().unwrap(), |
| 217 | + ]) |
| 218 | + .output(); |
| 219 | + |
| 220 | + match result { |
| 221 | + Err(e) => { |
| 222 | + self.config.sess.fatal(&format!("Error calling dlltool: {}", e.to_string())); |
| 223 | + } |
| 224 | + Ok(output) if !output.status.success() => self.config.sess.fatal(&format!( |
| 225 | + "Dlltool could not create import library: {}\n{}", |
| 226 | + String::from_utf8_lossy(&output.stdout), |
| 227 | + String::from_utf8_lossy(&output.stderr) |
| 228 | + )), |
| 229 | + _ => {} |
| 230 | + } |
| 231 | + } else { |
| 232 | + // we've checked for \0 characters in the library name already |
| 233 | + let dll_name_z = CString::new(lib_name).unwrap(); |
| 234 | + |
| 235 | + let output_path_z = rustc_fs_util::path_to_c_string(&output_path); |
| 236 | + |
| 237 | + tracing::trace!("invoking LLVMRustWriteImportLibrary"); |
| 238 | + tracing::trace!(" dll_name {:#?}", dll_name_z); |
| 239 | + tracing::trace!(" output_path {}", output_path.display()); |
| 240 | + tracing::trace!( |
| 241 | + " import names: {}", |
| 242 | + dll_imports |
| 243 | + .iter() |
| 244 | + .map(|import| import.name.to_string()) |
| 245 | + .collect::<Vec<_>>() |
| 246 | + .join(", "), |
| 247 | + ); |
201 | 248 |
|
202 |
| - if result == crate::llvm::LLVMRustResult::Failure { |
203 |
| - self.config.sess.fatal(&format!( |
204 |
| - "Error creating import library for {}: {}", |
205 |
| - lib_name, |
206 |
| - llvm::last_error().unwrap_or("unknown LLVM error".to_string()) |
207 |
| - )); |
208 |
| - } |
| 249 | + // All import names are Rust identifiers and therefore cannot contain \0 characters. |
| 250 | + // FIXME: when support for #[link_name] is implemented, ensure that the import names |
| 251 | + // still don't contain any \0 characters. Also need to check that the names don't |
| 252 | + // contain substrings like " @" or "NONAME" that are keywords or otherwise reserved |
| 253 | + // in definition files. |
| 254 | + let cstring_import_name_and_ordinal_vector: Vec<(CString, Option<u16>)> = |
| 255 | + import_name_and_ordinal_vector |
| 256 | + .into_iter() |
| 257 | + .map(|(name, ordinal)| (CString::new(name).unwrap(), ordinal)) |
| 258 | + .collect(); |
| 259 | + |
| 260 | + let ffi_exports: Vec<LLVMRustCOFFShortExport> = cstring_import_name_and_ordinal_vector |
| 261 | + .iter() |
| 262 | + .map(|(name_z, ordinal)| LLVMRustCOFFShortExport::new(name_z.as_ptr(), *ordinal)) |
| 263 | + .collect(); |
| 264 | + let result = unsafe { |
| 265 | + crate::llvm::LLVMRustWriteImportLibrary( |
| 266 | + dll_name_z.as_ptr(), |
| 267 | + output_path_z.as_ptr(), |
| 268 | + ffi_exports.as_ptr(), |
| 269 | + ffi_exports.len(), |
| 270 | + llvm_machine_type(&self.config.sess.target.arch) as u16, |
| 271 | + !self.config.sess.target.is_like_msvc, |
| 272 | + ) |
| 273 | + }; |
| 274 | + |
| 275 | + if result == crate::llvm::LLVMRustResult::Failure { |
| 276 | + self.config.sess.fatal(&format!( |
| 277 | + "Error creating import library for {}: {}", |
| 278 | + lib_name, |
| 279 | + llvm::last_error().unwrap_or("unknown LLVM error".to_string()) |
| 280 | + )); |
| 281 | + } |
| 282 | + }; |
209 | 283 |
|
210 | 284 | self.add_archive(&output_path, |_| false).unwrap_or_else(|e| {
|
211 | 285 | self.config.sess.fatal(&format!(
|
@@ -332,22 +406,61 @@ impl<'a> LlvmArchiveBuilder<'a> {
|
332 | 406 | }
|
333 | 407 | }
|
334 | 408 |
|
335 |
| - fn i686_decorated_name(import: &DllImport) -> CString { |
| 409 | + fn i686_decorated_name(import: &DllImport, mingw: bool) -> String { |
336 | 410 | let name = import.name;
|
337 |
| - // We verified during construction that `name` does not contain any NULL characters, so the |
338 |
| - // conversion to CString is guaranteed to succeed. |
339 |
| - CString::new(match import.calling_convention { |
340 |
| - DllCallingConvention::C => format!("_{}", name), |
341 |
| - DllCallingConvention::Stdcall(arg_list_size) => format!("_{}@{}", name, arg_list_size), |
| 411 | + let prefix = if mingw { "" } else { "_" }; |
| 412 | + |
| 413 | + match import.calling_convention { |
| 414 | + DllCallingConvention::C => format!("{}{}", prefix, name), |
| 415 | + DllCallingConvention::Stdcall(arg_list_size) => { |
| 416 | + format!("{}{}@{}", prefix, name, arg_list_size) |
| 417 | + } |
342 | 418 | DllCallingConvention::Fastcall(arg_list_size) => format!("@{}@{}", name, arg_list_size),
|
343 | 419 | DllCallingConvention::Vectorcall(arg_list_size) => {
|
344 | 420 | format!("{}@@{}", name, arg_list_size)
|
345 | 421 | }
|
346 |
| - }) |
347 |
| - .unwrap() |
| 422 | + } |
348 | 423 | }
|
349 | 424 | }
|
350 | 425 |
|
351 | 426 | fn string_to_io_error(s: String) -> io::Error {
|
352 | 427 | io::Error::new(io::ErrorKind::Other, format!("bad archive: {}", s))
|
353 | 428 | }
|
| 429 | + |
| 430 | +fn find_binutils_dlltool(sess: &Session) -> OsString { |
| 431 | + assert!(sess.target.options.is_like_windows && !sess.target.options.is_like_msvc); |
| 432 | + if let Some(dlltool_path) = &sess.opts.debugging_opts.dlltool { |
| 433 | + return dlltool_path.clone().into_os_string(); |
| 434 | + } |
| 435 | + |
| 436 | + let mut tool_name: OsString = if sess.host.arch != sess.target.arch { |
| 437 | + // We are cross-compiling, so we need the tool with the prefix matching our target |
| 438 | + if sess.target.arch == "x86" { |
| 439 | + "i686-w64-mingw32-dlltool" |
| 440 | + } else { |
| 441 | + "x86_64-w64-mingw32-dlltool" |
| 442 | + } |
| 443 | + } else { |
| 444 | + // We are not cross-compiling, so we just want `dlltool` |
| 445 | + "dlltool" |
| 446 | + } |
| 447 | + .into(); |
| 448 | + |
| 449 | + if sess.host.options.is_like_windows { |
| 450 | + // If we're compiling on Windows, add the .exe suffix |
| 451 | + tool_name.push(".exe"); |
| 452 | + } |
| 453 | + |
| 454 | + // NOTE: it's not clear how useful it is to explicitly search PATH. |
| 455 | + for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) { |
| 456 | + let full_path = dir.join(&tool_name); |
| 457 | + if full_path.is_file() { |
| 458 | + return full_path.into_os_string(); |
| 459 | + } |
| 460 | + } |
| 461 | + |
| 462 | + // The user didn't specify the location of the dlltool binary, and we weren't able |
| 463 | + // to find the appropriate one on the PATH. Just return the name of the tool |
| 464 | + // and let the invocation fail with a hopefully useful error message. |
| 465 | + tool_name |
| 466 | +} |
0 commit comments