Skip to content
Snippets Groups Projects
Unverified Commit 6cc34a22 authored by bors[bot]'s avatar bors[bot] Committed by GitHub
Browse files

Merge #453


453: xtask: Explicitly set OSABI r=mkroening a=mkroening



Co-authored-by: default avatarMartin Kröning <mkroening@posteo.net>
parents 6046c94e 6d92b17c
No related branches found
No related tags found
No related merge requests found
Pipeline #722249 passed
......@@ -5,7 +5,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
goblin = { version = "0.5", default-features = false, features = ["archive", "std"] }
goblin = { version = "0.5", default-features = false, features = ["archive", "elf32", "elf64", "std"] }
rustc_version = "0.4"
xflags = "0.2"
xshell = "0.2"
......@@ -10,7 +10,7 @@ use std::{
};
use anyhow::{anyhow, Result};
use goblin::archive::Archive;
use goblin::{archive::Archive, elf64::header};
use xshell::{cmd, Shell};
const RUSTFLAGS: &[&str] = &[
......@@ -51,6 +51,14 @@ impl flags::Build {
.args(self.profile_args())
.run()?;
let build_archive = self.build_archive();
let dist_archive = self.dist_archive();
sh.create_dir(dist_archive.parent().unwrap())?;
sh.copy_file(&build_archive, &dist_archive)?;
eprintln!("Setting OSABI");
self.set_osabi()?;
eprintln!("Exporting symbols");
self.export_syms()?;
......@@ -113,21 +121,33 @@ impl flags::Build {
}
}
fn export_syms(&self) -> Result<()> {
fn set_osabi(&self) -> Result<()> {
let sh = sh()?;
let archive_path = self.dist_archive();
let mut archive_bytes = sh.read_binary_file(&archive_path)?;
let archive = Archive::parse(&archive_bytes)?;
let input = self.build_archive();
let output = self.dist_archive();
sh.create_dir(output.parent().unwrap())?;
sh.copy_file(&input, &output)?;
let file_offsets = (0..archive.len())
.map(|i| archive.get_at(i).unwrap().offset)
.collect::<Vec<_>>();
let objcopy = binutil("objcopy")?;
for file_offset in file_offsets {
let file_offset = usize::try_from(file_offset).unwrap();
archive_bytes[file_offset + header::EI_OSABI] = header::ELFOSABI_STANDALONE;
}
sh.write_file(&archive_path, archive_bytes)?;
cmd!(sh, "{objcopy} --prefix-symbols=hermit_ {output}").run()?;
Ok(())
}
let archive_bytes = sh.read_binary_file(&input)?;
fn export_syms(&self) -> Result<()> {
let sh = sh()?;
let archive_path = self.dist_archive();
let archive_bytes = sh.read_binary_file(&archive_path)?;
let archive = Archive::parse(&archive_bytes)?;
let symbol_redefinitions = {
let sys_fns = archive
.summarize()
.into_iter()
......@@ -146,16 +166,24 @@ impl flags::Build {
]
.into_iter();
let symbol_redefinitions = explicit_exports
explicit_exports
.chain(sys_fns)
.map(|symbol| format!("hermit_{symbol} {symbol}\n"))
.collect::<String>();
.collect::<String>()
};
let exported_syms = self.exported_syms();
let redefine_syms_path = self.redefine_syms_path();
sh.write_file(&redefine_syms_path, &symbol_redefinitions)?;
sh.write_file(&exported_syms, &symbol_redefinitions)?;
let objcopy = binutil("objcopy")?;
cmd!(sh, "{objcopy} --prefix-symbols=hermit_ {archive_path}").run()?;
cmd!(
sh,
"{objcopy} --redefine-syms={redefine_syms_path} {archive_path}"
)
.run()?;
cmd!(sh, "{objcopy} --redefine-syms={exported_syms} {output}").run()?;
sh.remove_path(&redefine_syms_path)?;
Ok(())
}
......@@ -204,7 +232,7 @@ impl flags::Build {
dist_archive
}
fn exported_syms(&self) -> PathBuf {
fn redefine_syms_path(&self) -> PathBuf {
let mut redefine_syms_path = self.dist_dir();
redefine_syms_path.push("exported-syms");
redefine_syms_path
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment