Skip to content

Commit

Permalink
Auto merge of #107360 - bjorn3:fix_thin_archive_reading, r=wesleywiser
Browse files Browse the repository at this point in the history
Fix thin archive reading

This includes a revert of #105221 to restore fat archive reading with LlvmArchiveBuilder.

Should fix #107162, #107334 and google/shaderc-rs#133
  • Loading branch information
bors committed Jan 28, 2023
2 parents 6cd6bad + de363d5 commit 2527416
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 39 deletions.
18 changes: 13 additions & 5 deletions compiler/rustc_codegen_llvm/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use crate::errors::{
use crate::llvm::archive_ro::{ArchiveRO, Child};
use crate::llvm::{self, ArchiveKind, LLVMMachineType, LLVMRustCOFFShortExport};
use rustc_codegen_ssa::back::archive::{
get_native_object_symbols, ArArchiveBuilder, ArchiveBuildFailure, ArchiveBuilder,
ArchiveBuilderBuilder, UnknownArchiveKind,
get_native_object_symbols, try_extract_macho_fat_archive, ArArchiveBuilder,
ArchiveBuildFailure, ArchiveBuilder, ArchiveBuilderBuilder, UnknownArchiveKind,
};

use rustc_session::cstore::DllImport;
Expand Down Expand Up @@ -66,15 +66,21 @@ impl<'a> ArchiveBuilder<'a> for LlvmArchiveBuilder<'a> {
archive: &Path,
skip: Box<dyn FnMut(&str) -> bool + 'static>,
) -> io::Result<()> {
let archive_ro = match ArchiveRO::open(archive) {
let mut archive = archive.to_path_buf();
if self.sess.target.llvm_target.contains("-apple-macosx") {
if let Some(new_archive) = try_extract_macho_fat_archive(&self.sess, &archive)? {
archive = new_archive
}
}
let archive_ro = match ArchiveRO::open(&archive) {
Ok(ar) => ar,
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
};
if self.additions.iter().any(|ar| ar.path() == archive) {
return Ok(());
}
self.additions.push(Addition::Archive {
path: archive.to_path_buf(),
path: archive,
archive: archive_ro,
skip: Box::new(skip),
});
Expand Down Expand Up @@ -102,7 +108,9 @@ pub struct LlvmArchiveBuilderBuilder;

impl ArchiveBuilderBuilder for LlvmArchiveBuilderBuilder {
fn new_archive_builder<'a>(&self, sess: &'a Session) -> Box<dyn ArchiveBuilder<'a> + 'a> {
if sess.target.arch == "wasm32" || sess.target.arch == "wasm64" {
// FIXME use ArArchiveBuilder on most targets again once reading thin archives is
// implemented
if true || sess.target.arch == "wasm32" || sess.target.arch == "wasm64" {
Box::new(LlvmArchiveBuilder { sess, additions: Vec::new() })
} else {
Box::new(ArArchiveBuilder::new(sess, get_llvm_object_symbols))
Expand Down
70 changes: 36 additions & 34 deletions compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tempfile::Builder as TempFileBuilder;

use std::error::Error;
use std::fs::File;
use std::io;
use std::io::{self, Write};
use std::path::{Path, PathBuf};

// Re-exporting for rustc_codegen_llvm::back::archive
Expand Down Expand Up @@ -116,42 +116,51 @@ impl<'a> ArArchiveBuilder<'a> {
}
}

fn try_filter_fat_archs<'a>(
fn try_filter_fat_archs(
archs: object::read::Result<&[impl FatArch]>,
target_arch: object::Architecture,
archive_map_data: &'a [u8],
) -> io::Result<Option<(&'a [u8], u64)>> {
archive_path: &Path,
archive_map_data: &[u8],
) -> io::Result<Option<PathBuf>> {
let archs = archs.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;

let desired = match archs.iter().find(|a| a.architecture() == target_arch) {
Some(a) => a,
None => return Ok(None),
};

Ok(Some((
let (mut new_f, extracted_path) = tempfile::Builder::new()
.suffix(archive_path.file_name().unwrap())
.tempfile()?
.keep()
.unwrap();

new_f.write_all(
desired.data(archive_map_data).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?,
desired.offset().into(),
)))
)?;

Ok(Some(extracted_path))
}

pub fn try_extract_macho_fat_archive<'a>(
pub fn try_extract_macho_fat_archive(
sess: &Session,
archive_bytes: &'a [u8],
) -> io::Result<Option<(&'a [u8], u64)>> {
archive_path: &Path,
) -> io::Result<Option<PathBuf>> {
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
let target_arch = match sess.target.arch.as_ref() {
"aarch64" => object::Architecture::Aarch64,
"x86_64" => object::Architecture::X86_64,
_ => return Ok(None),
};

match object::macho::FatHeader::parse(archive_bytes) {
match object::macho::FatHeader::parse(&*archive_map) {
Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC => {
let archs = object::macho::FatHeader::parse_arch32(archive_bytes);
try_filter_fat_archs(archs, target_arch, archive_bytes)
let archs = object::macho::FatHeader::parse_arch32(&*archive_map);
try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map)
}
Ok(h) if h.magic.get(object::endian::BigEndian) == object::macho::FAT_MAGIC_64 => {
let archs = object::macho::FatHeader::parse_arch64(archive_bytes);
try_filter_fat_archs(archs, target_arch, archive_bytes)
let archs = object::macho::FatHeader::parse_arch64(&*archive_map);
try_filter_fat_archs(archs, target_arch, archive_path, &*archive_map)
}
// Not a FatHeader at all, just return None.
_ => Ok(None),
Expand All @@ -164,24 +173,21 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> {
archive_path: &Path,
mut skip: Box<dyn FnMut(&str) -> bool + 'static>,
) -> io::Result<()> {
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
let mut archive_path = archive_path.to_path_buf();
if self.sess.target.llvm_target.contains("-apple-macosx") {
if let Some(new_archive_path) =
try_extract_macho_fat_archive(&self.sess, &archive_path)?
{
archive_path = new_archive_path
}
}

if self.src_archives.iter().any(|archive| archive.0 == archive_path) {
return Ok(());
}

let (archive_bytes, offset) = if self.sess.target.llvm_target.contains("-apple-macosx") {
if let Some((sub_archive, archive_offset)) =
try_extract_macho_fat_archive(&self.sess, &*archive_map)?
{
(sub_archive, Some(archive_offset))
} else {
(&*archive_map, None)
}
} else {
(&*archive_map, None)
};

let archive = ArchiveFile::parse(&*archive_bytes)
let archive_map = unsafe { Mmap::map(File::open(&archive_path)?)? };
let archive = ArchiveFile::parse(&*archive_map)
.map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
let archive_index = self.src_archives.len();

Expand All @@ -190,13 +196,9 @@ impl<'a> ArchiveBuilder<'a> 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) {
let mut range = entry.file_range();
if let Some(offset) = offset {
range.0 += offset;
}
self.entries.push((
file_name.into_bytes(),
ArchiveEntry::FromArchive { archive_index, file_range: range },
ArchiveEntry::FromArchive { archive_index, file_range: entry.file_range() },
));
}
}
Expand Down

0 comments on commit 2527416

Please sign in to comment.