Skip to content

Commit 6047d9c

Browse files
committed
Always use ar_archive_writer for import libs
1 parent 27b93da commit 6047d9c

File tree

10 files changed

+118
-192
lines changed

10 files changed

+118
-192
lines changed

Diff for: Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,9 @@ dependencies = [
205205

206206
[[package]]
207207
name = "ar_archive_writer"
208-
version = "0.3.3"
208+
version = "0.4.0"
209209
source = "registry+https://github.com/rust-lang/crates.io-index"
210-
checksum = "3f2bcb7cf51decfbbfc7ef476e28b0775b13e5eb1190f8b7df145cd53d4f4374"
210+
checksum = "de11a9d32db3327f981143bdf699ade4d637c6887b13b97e6e91a9154666963c"
211211
dependencies = [
212212
"object 0.36.2",
213213
]
@@ -3427,6 +3427,7 @@ dependencies = [
34273427
name = "rustc_codegen_llvm"
34283428
version = "0.0.0"
34293429
dependencies = [
3430+
"ar_archive_writer",
34303431
"bitflags 2.5.0",
34313432
"itertools",
34323433
"libc",

Diff for: compiler/rustc_codegen_cranelift/src/archive.rs

+1-82
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
use std::borrow::Borrow;
2-
use std::fs;
3-
use std::path::Path;
4-
5-
use ar_archive_writer::{COFFShortExport, MachineTypes};
61
use rustc_codegen_ssa::back::archive::{
7-
create_mingw_dll_import_lib, ArArchiveBuilder, ArchiveBuilder, ArchiveBuilderBuilder,
8-
DEFAULT_OBJECT_READER,
2+
ArArchiveBuilder, ArchiveBuilder, ArchiveBuilderBuilder, DEFAULT_OBJECT_READER,
93
};
10-
use rustc_codegen_ssa::common::is_mingw_gnu_toolchain;
114
use rustc_session::Session;
125

136
pub(crate) struct ArArchiveBuilderBuilder;
@@ -16,78 +9,4 @@ impl ArchiveBuilderBuilder for ArArchiveBuilderBuilder {
169
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder + 'a> {
1710
Box::new(ArArchiveBuilder::new(sess, &DEFAULT_OBJECT_READER))
1811
}
19-
20-
fn create_dll_import_lib(
21-
&self,
22-
sess: &Session,
23-
lib_name: &str,
24-
import_name_and_ordinal_vector: Vec<(String, Option<u16>)>,
25-
output_path: &Path,
26-
) {
27-
if is_mingw_gnu_toolchain(&sess.target) {
28-
// The binutils linker used on -windows-gnu targets cannot read the import
29-
// libraries generated by LLVM: in our attempts, the linker produced an .EXE
30-
// that loaded but crashed with an AV upon calling one of the imported
31-
// functions. Therefore, use binutils to create the import library instead,
32-
// by writing a .DEF file to the temp dir and calling binutils's dlltool.
33-
create_mingw_dll_import_lib(
34-
sess,
35-
lib_name,
36-
import_name_and_ordinal_vector,
37-
output_path,
38-
);
39-
} else {
40-
let mut file =
41-
match fs::OpenOptions::new().write(true).create_new(true).open(&output_path) {
42-
Ok(file) => file,
43-
Err(error) => {
44-
sess.dcx().fatal(format!(
45-
"failed to create import library file `{path}`: {error}",
46-
path = output_path.display(),
47-
));
48-
}
49-
};
50-
51-
let machine = match sess.target.arch.borrow() {
52-
"x86" => MachineTypes::I386,
53-
"x86_64" => MachineTypes::AMD64,
54-
"arm" => MachineTypes::ARMNT,
55-
"aarch64" => MachineTypes::ARM64,
56-
_ => {
57-
sess.dcx().fatal(format!(
58-
"unsupported target architecture `{arch}`",
59-
arch = sess.target.arch,
60-
));
61-
}
62-
};
63-
64-
let exports = import_name_and_ordinal_vector
65-
.iter()
66-
.map(|(name, ordinal)| COFFShortExport {
67-
name: name.to_string(),
68-
ext_name: None,
69-
symbol_name: None,
70-
alias_target: None,
71-
ordinal: ordinal.unwrap_or(0),
72-
noname: ordinal.is_some(),
73-
data: false,
74-
private: false,
75-
constant: false,
76-
})
77-
.collect::<Vec<_>>();
78-
79-
if let Err(error) = ar_archive_writer::write_import_library(
80-
&mut file,
81-
lib_name,
82-
&exports,
83-
machine,
84-
!sess.target.is_like_msvc,
85-
) {
86-
sess.dcx().fatal(format!(
87-
"failed to create import library `{path}`: `{error}`",
88-
path = output_path.display(),
89-
));
90-
}
91-
}
92-
}
9312
}

Diff for: compiler/rustc_codegen_llvm/Cargo.toml

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ test = false
88

99
[dependencies]
1010
# tidy-alphabetical-start
11+
ar_archive_writer = "0.4.0"
1112
bitflags = "2.4.1"
1213
itertools = "0.12"
1314
libc = "0.2"
1415
measureme = "11"
15-
object = { version = "0.36.2", default-features = false, features = ["std", "read"] }
16+
object = { version = "0.36.2", default-features = false, features = [
17+
"std",
18+
"read",
19+
] }
1620
rustc-demangle = "0.1.21"
1721
rustc_ast = { path = "../rustc_ast" }
1822
rustc_attr = { path = "../rustc_attr" }
@@ -33,7 +37,7 @@ rustc_session = { path = "../rustc_session" }
3337
rustc_span = { path = "../rustc_span" }
3438
rustc_symbol_mangling = { path = "../rustc_symbol_mangling" }
3539
rustc_target = { path = "../rustc_target" }
36-
serde = { version = "1", features = [ "derive" ]}
40+
serde = { version = "1", features = ["derive"] }
3741
serde_json = "1"
3842
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
3943
tracing = "0.1"

Diff for: compiler/rustc_codegen_llvm/messages.ftl

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ codegen_llvm_dynamic_linking_with_lto =
55
.note = only 'staticlib', 'bin', and 'cdylib' outputs are supported with LTO
66
77
8-
codegen_llvm_error_creating_import_library =
9-
Error creating import library for {$lib_name}: {$error}
10-
118
codegen_llvm_fixed_x18_invalid_arch = the `-Zfixed-x18` flag is not supported on the `{$arch}` architecture
129
1310
codegen_llvm_from_llvm_diag = {$message}

Diff for: compiler/rustc_codegen_llvm/src/back/archive.rs

+3-91
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ use std::path::{Path, PathBuf};
55
use std::{io, mem, ptr, str};
66

77
use rustc_codegen_ssa::back::archive::{
8-
create_mingw_dll_import_lib, try_extract_macho_fat_archive, ArArchiveBuilder,
9-
ArchiveBuildFailure, ArchiveBuilder, ArchiveBuilderBuilder, ObjectReader, UnknownArchiveKind,
10-
DEFAULT_OBJECT_READER,
8+
try_extract_macho_fat_archive, ArArchiveBuilder, ArchiveBuildFailure, ArchiveBuilder,
9+
ArchiveBuilderBuilder, ObjectReader, UnknownArchiveKind, DEFAULT_OBJECT_READER,
1110
};
12-
use rustc_codegen_ssa::common;
1311
use rustc_session::Session;
14-
use tracing::trace;
1512

16-
use crate::errors::ErrorCreatingImportLibrary;
1713
use crate::llvm::archive_ro::{ArchiveRO, Child};
18-
use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport};
14+
use crate::llvm::{self, ArchiveKind};
1915

2016
/// Helper for adding many files to an archive.
2117
#[must_use = "must call build() to finish building the archive"]
@@ -44,18 +40,6 @@ fn is_relevant_child(c: &Child<'_>) -> bool {
4440
}
4541
}
4642

47-
/// Map machine type strings to values of LLVM's MachineTypes enum.
48-
fn llvm_machine_type(cpu: &str) -> LLVMMachineType {
49-
match cpu {
50-
"x86_64" => LLVMMachineType::AMD64,
51-
"x86" => LLVMMachineType::I386,
52-
"aarch64" => LLVMMachineType::ARM64,
53-
"arm64ec" => LLVMMachineType::ARM64EC,
54-
"arm" => LLVMMachineType::ARM,
55-
_ => panic!("unsupported cpu type {cpu}"),
56-
}
57-
}
58-
5943
impl<'a> ArchiveBuilder for LlvmArchiveBuilder<'a> {
6044
fn add_archive(
6145
&mut self,
@@ -116,78 +100,6 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
116100
Box::new(ArArchiveBuilder::new(sess, &LLVM_OBJECT_READER))
117101
}
118102
}
119-
120-
fn create_dll_import_lib(
121-
&self,
122-
sess: &Session,
123-
lib_name: &str,
124-
import_name_and_ordinal_vector: Vec<(String, Option<u16>)>,
125-
output_path: &Path,
126-
) {
127-
if common::is_mingw_gnu_toolchain(&sess.target) {
128-
// The binutils linker used on -windows-gnu targets cannot read the import
129-
// libraries generated by LLVM: in our attempts, the linker produced an .EXE
130-
// that loaded but crashed with an AV upon calling one of the imported
131-
// functions. Therefore, use binutils to create the import library instead,
132-
// by writing a .DEF file to the temp dir and calling binutils's dlltool.
133-
create_mingw_dll_import_lib(
134-
sess,
135-
lib_name,
136-
import_name_and_ordinal_vector,
137-
output_path,
138-
);
139-
} else {
140-
// we've checked for \0 characters in the library name already
141-
let dll_name_z = CString::new(lib_name).unwrap();
142-
143-
let output_path_z = rustc_fs_util::path_to_c_string(&output_path);
144-
145-
trace!("invoking LLVMRustWriteImportLibrary");
146-
trace!(" dll_name {:#?}", dll_name_z);
147-
trace!(" output_path {}", output_path.display());
148-
trace!(
149-
" import names: {}",
150-
import_name_and_ordinal_vector
151-
.iter()
152-
.map(|(name, _ordinal)| name.clone())
153-
.collect::<Vec<_>>()
154-
.join(", "),
155-
);
156-
157-
// All import names are Rust identifiers and therefore cannot contain \0 characters.
158-
// FIXME: when support for #[link_name] is implemented, ensure that the import names
159-
// still don't contain any \0 characters. Also need to check that the names don't
160-
// contain substrings like " @" or "NONAME" that are keywords or otherwise reserved
161-
// in definition files.
162-
let cstring_import_name_and_ordinal_vector: Vec<(CString, Option<u16>)> =
163-
import_name_and_ordinal_vector
164-
.into_iter()
165-
.map(|(name, ordinal)| (CString::new(name).unwrap(), ordinal))
166-
.collect();
167-
168-
let ffi_exports: Vec<LLVMRustCOFFShortExport> = cstring_import_name_and_ordinal_vector
169-
.iter()
170-
.map(|(name_z, ordinal)| LLVMRustCOFFShortExport::new(name_z.as_ptr(), *ordinal))
171-
.collect();
172-
let result = unsafe {
173-
crate::llvm::LLVMRustWriteImportLibrary(
174-
dll_name_z.as_ptr(),
175-
output_path_z.as_ptr(),
176-
ffi_exports.as_ptr(),
177-
ffi_exports.len(),
178-
llvm_machine_type(&sess.target.arch) as u16,
179-
!sess.target.is_like_msvc,
180-
)
181-
};
182-
183-
if result == crate::llvm::LLVMRustResult::Failure {
184-
sess.dcx().emit_fatal(ErrorCreatingImportLibrary {
185-
lib_name,
186-
error: llvm::last_error().unwrap_or("unknown LLVM error".to_string()),
187-
});
188-
}
189-
}
190-
}
191103
}
192104

193105
// The object crate doesn't know how to get symbols for LLVM bitcode and COFF bigobj files.

Diff for: compiler/rustc_codegen_llvm/src/errors.rs

-7
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,6 @@ pub(crate) enum PossibleFeature<'a> {
3939
None,
4040
}
4141

42-
#[derive(Diagnostic)]
43-
#[diag(codegen_llvm_error_creating_import_library)]
44-
pub(crate) struct ErrorCreatingImportLibrary<'a> {
45-
pub lib_name: &'a str,
46-
pub error: String,
47-
}
48-
4942
#[derive(Diagnostic)]
5043
#[diag(codegen_llvm_symbol_already_defined)]
5144
pub(crate) struct SymbolAlreadyDefined<'a> {

Diff for: compiler/rustc_codegen_ssa/Cargo.toml

+12-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55

66
[dependencies]
77
# tidy-alphabetical-start
8-
ar_archive_writer = "0.3.3"
8+
ar_archive_writer = "0.4.0"
99
arrayvec = { version = "0.7", default-features = false }
1010
bitflags = "2.4.1"
1111
cc = "1.0.90"
@@ -52,7 +52,17 @@ libc = "0.2.50"
5252
[dependencies.object]
5353
version = "0.36.2"
5454
default-features = false
55-
features = ["read_core", "elf", "macho", "pe", "xcoff", "unaligned", "archive", "write", "wasm"]
55+
features = [
56+
"read_core",
57+
"elf",
58+
"macho",
59+
"pe",
60+
"xcoff",
61+
"unaligned",
62+
"archive",
63+
"write",
64+
"wasm",
65+
]
5666

5767
[target.'cfg(windows)'.dependencies.windows]
5868
version = "0.52.0"

Diff for: compiler/rustc_codegen_ssa/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,6 @@ codegen_ssa_use_cargo_directive = use the `cargo:rustc-link-lib` directive to sp
343343
codegen_ssa_version_script_write_failure = failed to write version script: {$error}
344344
345345
codegen_ssa_visual_studio_not_installed = you may need to install Visual Studio build tools with the "C++ build tools" workload
346+
347+
codegen_ssa_error_creating_import_library =
348+
Error creating import library for {$lib_name}: {$error}

0 commit comments

Comments
 (0)