about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <bjorn3@users.noreply.github.com>2021-03-29 11:39:13 +0200
committerbjorn3 <bjorn3@users.noreply.github.com>2021-05-07 18:48:58 +0200
commit267d55d44acf6bb2821de39d4f868650ff0a9c7b (patch)
treec1962492dc5dd6e79d66591a1de5400b1d0de935
parente5f83d24aee866a14753a7cedbb4e301dfe5bef5 (diff)
downloadrust-267d55d44acf6bb2821de39d4f868650ff0a9c7b.tar.gz
rust-267d55d44acf6bb2821de39d4f868650ff0a9c7b.zip
Use the object crate for metadata reading
-rw-r--r--Cargo.lock9
-rw-r--r--compiler/rustc_codegen_cranelift/src/lib.rs2
-rw-r--r--compiler/rustc_codegen_cranelift/src/metadata.rs66
-rw-r--r--compiler/rustc_codegen_llvm/src/lib.rs2
-rw-r--r--compiler/rustc_codegen_llvm/src/metadata.rs89
-rw-r--r--compiler/rustc_codegen_ssa/Cargo.toml1
-rw-r--r--compiler/rustc_codegen_ssa/src/back/metadata.rs65
-rw-r--r--compiler/rustc_codegen_ssa/src/back/mod.rs1
8 files changed, 79 insertions, 156 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 77b917448e9..dc688e3a513 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2348,8 +2348,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397"
 dependencies = [
  "compiler_builtins",
+ "flate2",
  "rustc-std-workspace-alloc",
  "rustc-std-workspace-core",
+ "wasmparser",
 ]
 
 [[package]]
@@ -3700,6 +3702,7 @@ dependencies = [
  "itertools 0.9.0",
  "jobserver",
  "libc",
+ "object",
  "pathdiff",
  "rustc_apfloat",
  "rustc_ast",
@@ -5619,6 +5622,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "wasmparser"
+version = "0.57.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32fddd575d477c6e9702484139cf9f23dcd554b06d185ed0f56c857dd3a47aa6"
+
+[[package]]
 name = "winapi"
 version = "0.2.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/compiler/rustc_codegen_cranelift/src/lib.rs b/compiler/rustc_codegen_cranelift/src/lib.rs
index 32f40395702..1ade7c77d12 100644
--- a/compiler/rustc_codegen_cranelift/src/lib.rs
+++ b/compiler/rustc_codegen_cranelift/src/lib.rs
@@ -165,7 +165,7 @@ impl CodegenBackend for CraneliftCodegenBackend {
     }
 
     fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
-        Box::new(crate::metadata::CraneliftMetadataLoader)
+        Box::new(rustc_codegen_ssa::back::metadata::DefaultMetadataLoader)
     }
 
     fn provide(&self, _providers: &mut Providers) {}
diff --git a/compiler/rustc_codegen_cranelift/src/metadata.rs b/compiler/rustc_codegen_cranelift/src/metadata.rs
index 882232fde09..ab238244d68 100644
--- a/compiler/rustc_codegen_cranelift/src/metadata.rs
+++ b/compiler/rustc_codegen_cranelift/src/metadata.rs
@@ -1,73 +1,9 @@
-//! Reading and writing of the rustc metadata for rlibs and dylibs
+//! Writing of the rustc metadata for dylibs
 
-use std::fs::File;
-use std::path::Path;
-
-use rustc_codegen_ssa::METADATA_FILENAME;
-use rustc_data_structures::memmap::Mmap;
-use rustc_data_structures::owning_ref::OwningRef;
-use rustc_data_structures::rustc_erase_owner;
-use rustc_data_structures::sync::MetadataRef;
-use rustc_middle::middle::cstore::MetadataLoader;
 use rustc_middle::ty::TyCtxt;
-use rustc_target::spec::Target;
 
 use crate::backend::WriteMetadata;
 
-/// The metadata loader used by cg_clif.
-///
-/// The metadata is stored in the same format as cg_llvm.
-///
-/// # Metadata location
-///
-/// <dl>
-/// <dt>rlib</dt>
-/// <dd>The metadata can be found in the `lib.rmeta` file inside of the ar archive.</dd>
-/// <dt>dylib</dt>
-/// <dd>The metadata can be found in the `.rustc` section of the shared library.</dd>
-/// </dl>
-pub(crate) struct CraneliftMetadataLoader;
-
-fn load_metadata_with(
-    path: &Path,
-    f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
-) -> Result<MetadataRef, String> {
-    let file = File::open(path).map_err(|e| format!("{:?}", e))?;
-    let data = unsafe { Mmap::map(file) }.map_err(|e| format!("{:?}", e))?;
-    let metadata = OwningRef::new(data).try_map(f)?;
-    return Ok(rustc_erase_owner!(metadata.map_owner_box()));
-}
-
-impl MetadataLoader for CraneliftMetadataLoader {
-    fn get_rlib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
-        load_metadata_with(path, |data| {
-            let archive = object::read::archive::ArchiveFile::parse(&*data)
-                .map_err(|e| format!("{:?}", e))?;
-
-            for entry_result in archive.members() {
-                let entry = entry_result.map_err(|e| format!("{:?}", e))?;
-                if entry.name() == METADATA_FILENAME.as_bytes() {
-                    return Ok(entry.data());
-                }
-            }
-
-            Err("couldn't find metadata entry".to_string())
-        })
-    }
-
-    fn get_dylib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
-        use object::{Object, ObjectSection};
-
-        load_metadata_with(path, |data| {
-            let file = object::File::parse(&data).map_err(|e| format!("parse: {:?}", e))?;
-            file.section_by_name(".rustc")
-                .ok_or("no .rustc section")?
-                .data()
-                .map_err(|e| format!("failed to read .rustc section: {:?}", e))
-        })
-    }
-}
-
 // Adapted from https://github.com/rust-lang/rust/blob/da573206f87b5510de4b0ee1a9c044127e409bd3/src/librustc_codegen_llvm/base.rs#L47-L112
 pub(crate) fn write_metadata<O: WriteMetadata>(tcx: TyCtxt<'_>, object: &mut O) {
     use snap::write::FrameEncoder;
diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs
index 5ca4b226c38..8f95afc1b91 100644
--- a/compiler/rustc_codegen_llvm/src/lib.rs
+++ b/compiler/rustc_codegen_llvm/src/lib.rs
@@ -250,7 +250,7 @@ impl CodegenBackend for LlvmCodegenBackend {
     }
 
     fn metadata_loader(&self) -> Box<MetadataLoaderDyn> {
-        Box::new(metadata::LlvmMetadataLoader)
+        Box::new(rustc_codegen_ssa::back::metadata::DefaultMetadataLoader)
     }
 
     fn provide(&self, providers: &mut ty::query::Providers) {
diff --git a/compiler/rustc_codegen_llvm/src/metadata.rs b/compiler/rustc_codegen_llvm/src/metadata.rs
index decc1e1f700..4bad93a7567 100644
--- a/compiler/rustc_codegen_llvm/src/metadata.rs
+++ b/compiler/rustc_codegen_llvm/src/metadata.rs
@@ -1,94 +1,9 @@
-use crate::llvm;
-use crate::llvm::archive_ro::ArchiveRO;
-use crate::llvm::{mk_section_iter, False, ObjectFile};
-use rustc_middle::middle::cstore::MetadataLoader;
 use rustc_target::spec::Target;
 
-use rustc_codegen_ssa::METADATA_FILENAME;
-use rustc_data_structures::owning_ref::OwningRef;
-use rustc_data_structures::rustc_erase_owner;
-use tracing::debug;
-
 use rustc_fs_util::path_to_c_string;
 use std::path::Path;
 use std::slice;
 
-pub use rustc_data_structures::sync::MetadataRef;
-
-pub struct LlvmMetadataLoader;
-
-impl MetadataLoader for LlvmMetadataLoader {
-    fn get_rlib_metadata(&self, _: &Target, filename: &Path) -> Result<MetadataRef, String> {
-        // Use ArchiveRO for speed here, it's backed by LLVM and uses mmap
-        // internally to read the file. We also avoid even using a memcpy by
-        // just keeping the archive along while the metadata is in use.
-        let archive =
-            ArchiveRO::open(filename).map(|ar| OwningRef::new(Box::new(ar))).map_err(|e| {
-                debug!("llvm didn't like `{}`: {}", filename.display(), e);
-                format!("failed to read rlib metadata in '{}': {}", filename.display(), e)
-            })?;
-        let buf: OwningRef<_, [u8]> = archive.try_map(|ar| {
-            ar.iter()
-                .filter_map(|s| s.ok())
-                .find(|sect| sect.name() == Some(METADATA_FILENAME))
-                .map(|s| s.data())
-                .ok_or_else(|| {
-                    debug!("didn't find '{}' in the archive", METADATA_FILENAME);
-                    format!("failed to read rlib metadata: '{}'", filename.display())
-                })
-        })?;
-        Ok(rustc_erase_owner!(buf))
-    }
-
-    fn get_dylib_metadata(&self, target: &Target, filename: &Path) -> Result<MetadataRef, String> {
-        unsafe {
-            let buf = path_to_c_string(filename);
-            let mb = llvm::LLVMRustCreateMemoryBufferWithContentsOfFile(buf.as_ptr())
-                .ok_or_else(|| format!("error reading library: '{}'", filename.display()))?;
-            let of =
-                ObjectFile::new(mb).map(|of| OwningRef::new(Box::new(of))).ok_or_else(|| {
-                    format!("provided path not an object file: '{}'", filename.display())
-                })?;
-            let buf = of.try_map(|of| search_meta_section(of, target, filename))?;
-            Ok(rustc_erase_owner!(buf))
-        }
-    }
-}
-
-fn search_meta_section<'a>(
-    of: &'a ObjectFile,
-    target: &Target,
-    filename: &Path,
-) -> Result<&'a [u8], String> {
-    unsafe {
-        let si = mk_section_iter(of.llof);
-        while llvm::LLVMIsSectionIteratorAtEnd(of.llof, si.llsi) == False {
-            let mut name_buf = None;
-            let name_len = llvm::LLVMRustGetSectionName(si.llsi, &mut name_buf);
-            let name = name_buf.map_or_else(
-                String::new, // We got a null ptr, ignore `name_len`.
-                |buf| {
-                    String::from_utf8(
-                        slice::from_raw_parts(buf.as_ptr() as *const u8, name_len as usize)
-                            .to_vec(),
-                    )
-                    .unwrap()
-                },
-            );
-            debug!("get_metadata_section: name {}", name);
-            if read_metadata_section_name(target) == name {
-                let cbuf = llvm::LLVMGetSectionContents(si.llsi);
-                let csz = llvm::LLVMGetSectionSize(si.llsi) as usize;
-                // The buffer is valid while the object file is around
-                let buf: &'a [u8] = slice::from_raw_parts(cbuf as *const u8, csz);
-                return Ok(buf);
-            }
-            llvm::LLVMMoveToNextSection(si.llsi);
-        }
-    }
-    Err(format!("metadata not found: '{}'", filename.display()))
-}
-
 pub fn metadata_section_name(target: &Target) -> &'static str {
     // Historical note:
     //
@@ -106,7 +21,3 @@ pub fn metadata_section_name(target: &Target) -> &'static str {
 
     if target.is_like_osx { "__DATA,.rustc" } else { ".rustc" }
 }
-
-fn read_metadata_section_name(_target: &Target) -> &'static str {
-    ".rustc"
-}
diff --git a/compiler/rustc_codegen_ssa/Cargo.toml b/compiler/rustc_codegen_ssa/Cargo.toml
index 7a3d715df6d..d7d91d09cfa 100644
--- a/compiler/rustc_codegen_ssa/Cargo.toml
+++ b/compiler/rustc_codegen_ssa/Cargo.toml
@@ -16,6 +16,7 @@ libc = "0.2.50"
 jobserver = "0.1.22"
 tempfile = "3.2"
 pathdiff = "0.2.0"
+object = "0.22.0"
 
 rustc_serialize = { path = "../rustc_serialize" }
 rustc_ast = { path = "../rustc_ast" }
diff --git a/compiler/rustc_codegen_ssa/src/back/metadata.rs b/compiler/rustc_codegen_ssa/src/back/metadata.rs
new file mode 100644
index 00000000000..096098fb260
--- /dev/null
+++ b/compiler/rustc_codegen_ssa/src/back/metadata.rs
@@ -0,0 +1,65 @@
+//! Reading of the rustc metadata for rlibs and dylibs
+
+use std::fs::File;
+use std::path::Path;
+
+use rustc_data_structures::memmap::Mmap;
+use rustc_data_structures::owning_ref::OwningRef;
+use rustc_data_structures::rustc_erase_owner;
+use rustc_data_structures::sync::MetadataRef;
+use rustc_middle::middle::cstore::MetadataLoader;
+use rustc_target::spec::Target;
+
+use crate::METADATA_FILENAME;
+
+/// The default metadata loader. This is used by cg_llvm and cg_clif.
+///
+/// # Metadata location
+///
+/// <dl>
+/// <dt>rlib</dt>
+/// <dd>The metadata can be found in the `lib.rmeta` file inside of the ar archive.</dd>
+/// <dt>dylib</dt>
+/// <dd>The metadata can be found in the `.rustc` section of the shared library.</dd>
+/// </dl>
+pub struct DefaultMetadataLoader;
+
+fn load_metadata_with(
+    path: &Path,
+    f: impl for<'a> FnOnce(&'a [u8]) -> Result<&'a [u8], String>,
+) -> Result<MetadataRef, String> {
+    let file = File::open(path).map_err(|e| format!("{:?}", e))?;
+    let data = unsafe { Mmap::map(file) }.map_err(|e| format!("{:?}", e))?;
+    let metadata = OwningRef::new(data).try_map(f)?;
+    return Ok(rustc_erase_owner!(metadata.map_owner_box()));
+}
+
+impl MetadataLoader for DefaultMetadataLoader {
+    fn get_rlib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
+        load_metadata_with(path, |data| {
+            let archive = object::read::archive::ArchiveFile::parse(&*data)
+                .map_err(|e| format!("{:?}", e))?;
+
+            for entry_result in archive.members() {
+                let entry = entry_result.map_err(|e| format!("{:?}", e))?;
+                if entry.name() == METADATA_FILENAME.as_bytes() {
+                    return Ok(entry.data());
+                }
+            }
+
+            Err("couldn't find metadata entry".to_string())
+        })
+    }
+
+    fn get_dylib_metadata(&self, _target: &Target, path: &Path) -> Result<MetadataRef, String> {
+        use object::{Object, ObjectSection};
+
+        load_metadata_with(path, |data| {
+            let file = object::File::parse(&data).map_err(|e| format!("parse: {:?}", e))?;
+            file.section_by_name(".rustc")
+                .ok_or("no .rustc section")?
+                .data()
+                .map_err(|e| format!("failed to read .rustc section: {:?}", e))
+        })
+    }
+}
diff --git a/compiler/rustc_codegen_ssa/src/back/mod.rs b/compiler/rustc_codegen_ssa/src/back/mod.rs
index 20ca503d43f..d11ed54eb20 100644
--- a/compiler/rustc_codegen_ssa/src/back/mod.rs
+++ b/compiler/rustc_codegen_ssa/src/back/mod.rs
@@ -3,6 +3,7 @@ pub mod command;
 pub mod link;
 pub mod linker;
 pub mod lto;
+pub mod metadata;
 pub mod rpath;
 pub mod symbol_export;
 pub mod write;