Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reading thin archives in ArArchiveBuilder #128936

Merged
merged 9 commits into from
Aug 15, 2024
4 changes: 1 addition & 3 deletions compiler/rustc_codegen_llvm/src/back/archive.rs
jieyouxu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ pub struct LlvmArchiveBuilderBuilder;

impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder + 'a> {
// FIXME use ArArchiveBuilder on most targets again once reading thin archives is
// implemented
if true {
if false {
Box::new(LlvmArchiveBuilder { sess, additions: Vec::new() })
} else {
Box::new(ArArchiveBuilder::new(sess, &LLVM_OBJECT_READER))
bjorn3 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
15 changes: 11 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,10 +307,17 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> {
let file_name = String::from_utf8(entry.name().to_vec())
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
if !skip(&file_name) {
self.entries.push((
file_name.into_bytes(),
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
));
if entry.is_thin() {
self.entries.push((
file_name.clone().into_bytes(),
ArchiveEntry::File(PathBuf::from(file_name)),
));
} else {
self.entries.push((
file_name.into_bytes(),
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
));
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/tools/run-make-support/src/external_deps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ impl LlvmAr {
self
}

/// Like `obj_to_ar` except creating a thin archive.
pub fn obj_to_thin_ar(&mut self) -> &mut Self {
self.cmd.arg("rcus").arg("--thin");
self
}

/// Extract archive members back to files.
pub fn extract(&mut self) -> &mut Self {
self.cmd.arg("x");
Expand Down
10 changes: 10 additions & 0 deletions tests/run-make/staticlib-thin-archive/bin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#[link(name = "rust_archive", kind = "static")]
extern "C" {
fn simple_fn();
}

fn main() {
unsafe {
simple_fn();
}
}
13 changes: 13 additions & 0 deletions tests/run-make/staticlib-thin-archive/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Regression test for https://github.com/rust-lang/rust/issues/107407
jieyouxu marked this conversation as resolved.
Show resolved Hide resolved

use run_make_support::{llvm_ar, rustc, static_lib_name};

fn main() {
rustc().input("simple_obj.rs").emit("obj").run();
llvm_ar().obj_to_thin_ar().output_input(static_lib_name("thin_archive"), "simple_obj.o").run();
rustc().input("rust_archive.rs").run();
// Disable lld as it ignores the symbol table in the archive file.
rustc()
.input("bin.rs") /*.arg("-Zlinker-features=-lld")*/
bjorn3 marked this conversation as resolved.
Show resolved Hide resolved
.run();
jieyouxu marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions tests/run-make/staticlib-thin-archive/rust_archive.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![crate_type = "staticlib"]

#[link(name = "thin_archive", kind = "static")]
extern "C" {}
4 changes: 4 additions & 0 deletions tests/run-make/staticlib-thin-archive/simple_obj.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![crate_type = "staticlib"]

#[no_mangle]
extern "C" fn simple_fn() {}
Loading