From db68a19b619ffc4b4ee9d1118d064d184d0bcd37 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:29:32 +0000 Subject: [PATCH] Fix review comments and other improvements --- .../rustc_codegen_llvm/src/back/archive.rs | 4 +-- .../rustc_codegen_ssa/src/back/archive.rs | 6 ++-- tests/run-make/staticlib-thin-archive/bin.rs | 7 +---- .../run-make/staticlib-thin-archive/rmake.rs | 31 ++++++++++++++----- .../staticlib-thin-archive/rust_archive.rs | 4 --- .../staticlib-thin-archive/rust_lib.rs | 6 ++++ 6 files changed, 34 insertions(+), 24 deletions(-) delete mode 100644 tests/run-make/staticlib-thin-archive/rust_archive.rs create mode 100644 tests/run-make/staticlib-thin-archive/rust_lib.rs diff --git a/compiler/rustc_codegen_llvm/src/back/archive.rs b/compiler/rustc_codegen_llvm/src/back/archive.rs index 0a8728c385c..60e63462548 100644 --- a/compiler/rustc_codegen_llvm/src/back/archive.rs +++ b/compiler/rustc_codegen_llvm/src/back/archive.rs @@ -108,8 +108,8 @@ impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder { fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box { // Keeping LlvmArchiveBuilder around in case of a regression caused by using // ArArchiveBuilder. - // FIXME remove a couple of months after #128936 gets merged in case no - // regression is found. + // FIXME(#128955) remove a couple of months after #128936 gets merged in case + // no regression is found. if false { Box::new(LlvmArchiveBuilder { sess, additions: Vec::new() }) } else { diff --git a/compiler/rustc_codegen_ssa/src/back/archive.rs b/compiler/rustc_codegen_ssa/src/back/archive.rs index a35aff096a4..8eb44d12016 100644 --- a/compiler/rustc_codegen_ssa/src/back/archive.rs +++ b/compiler/rustc_codegen_ssa/src/back/archive.rs @@ -308,10 +308,8 @@ impl<'a> ArchiveBuilder for ArArchiveBuilder<'a> { .map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?; if !skip(&file_name) { if entry.is_thin() { - self.entries.push(( - file_name.clone().into_bytes(), - ArchiveEntry::File(PathBuf::from(file_name)), - )); + let member_path = archive_path.parent().unwrap().join(Path::new(&file_name)); + self.entries.push((file_name.into_bytes(), ArchiveEntry::File(member_path))); } else { self.entries.push(( file_name.into_bytes(), diff --git a/tests/run-make/staticlib-thin-archive/bin.rs b/tests/run-make/staticlib-thin-archive/bin.rs index b37107d9c94..97a2751f20b 100644 --- a/tests/run-make/staticlib-thin-archive/bin.rs +++ b/tests/run-make/staticlib-thin-archive/bin.rs @@ -1,10 +1,5 @@ -#[link(name = "rust_archive", kind = "static")] -extern "C" { - fn simple_fn(); -} - fn main() { unsafe { - simple_fn(); + rust_lib::simple_fn(); } } diff --git a/tests/run-make/staticlib-thin-archive/rmake.rs b/tests/run-make/staticlib-thin-archive/rmake.rs index 137dffcc33f..f7e3e43d535 100644 --- a/tests/run-make/staticlib-thin-archive/rmake.rs +++ b/tests/run-make/staticlib-thin-archive/rmake.rs @@ -1,13 +1,28 @@ -// Regression test for https://github.com/rust-lang/rust/issues/107407 +// Regression test for https://github.com/rust-lang/rust/issues/107407 which +// checks that rustc can read thin archive. Before the object crate added thin +// archive support rustc would add emit object files to the staticlib and after +// the object crate added thin archive support it would previously crash the +// compiler due to a missing special case for thin archive members. +use std::path::Path; -use run_make_support::{llvm_ar, rustc, static_lib_name}; +use run_make_support::{llvm_ar, rust_lib_name, 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")*/ + std::fs::create_dir("archive").unwrap(); + + // Build a thin archive + rustc().input("simple_obj.rs").emit("obj").output("archive/simple_obj.o").run(); + llvm_ar() + .obj_to_thin_ar() + .output_input( + Path::new("archive").join(static_lib_name("thin_archive")), + "archive/simple_obj.o", + ) .run(); + + // Build an rlib which includes the members of this thin archive + rustc().input("rust_lib.rs").library_search_path("archive").run(); + + // Build a binary which requires a symbol from the thin archive + rustc().input("bin.rs").extern_("rust_lib", rust_lib_name("rust_lib")).run(); } diff --git a/tests/run-make/staticlib-thin-archive/rust_archive.rs b/tests/run-make/staticlib-thin-archive/rust_archive.rs deleted file mode 100644 index 3d11f02df20..00000000000 --- a/tests/run-make/staticlib-thin-archive/rust_archive.rs +++ /dev/null @@ -1,4 +0,0 @@ -#![crate_type = "staticlib"] - -#[link(name = "thin_archive", kind = "static")] -extern "C" {} diff --git a/tests/run-make/staticlib-thin-archive/rust_lib.rs b/tests/run-make/staticlib-thin-archive/rust_lib.rs new file mode 100644 index 00000000000..c76b0f25433 --- /dev/null +++ b/tests/run-make/staticlib-thin-archive/rust_lib.rs @@ -0,0 +1,6 @@ +#![crate_type = "rlib"] + +#[link(name = "thin_archive", kind = "static")] +extern "C" { + pub fn simple_fn(); +}