Skip to content

Commit

Permalink
Merge pull request #991 from mkroening/instrument
Browse files Browse the repository at this point in the history
feat: make ready for instrumentation
  • Loading branch information
mkroening authored Nov 30, 2023
2 parents bb07a10 + 341233d commit 00844f5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
36 changes: 28 additions & 8 deletions xtask/src/archive.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::env;
use std::fmt::Write;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -41,21 +42,40 @@ impl Archive {
Ok(symbols)
}

pub fn retain_symbols<'a>(&self, symbols: impl Iterator<Item = &'a str>) -> Result<()> {
pub fn retain_symbols(&self, mut exported_symbols: HashSet<&str>) -> Result<()> {
let sh = crate::sh()?;
let archive = self.as_ref();
let prefix = archive.file_stem().unwrap().to_str().unwrap();

let symbol_renames = symbols.fold(String::new(), |mut output, symbol| {
let _ = writeln!(output, "{prefix}_{symbol} {symbol}");
output
});
let prefix = {
let file_stem = archive.file_stem().unwrap().to_str().unwrap();
file_stem.strip_prefix("lib").unwrap_or(file_stem)
};

let all_symbols = {
let nm = binutil("nm")?;
let stdout = cmd!(sh, "{nm} --export-symbols {archive}").output()?.stdout;
String::from_utf8(stdout)?
};

let symbol_renames = all_symbols
.lines()
.fold(String::new(), |mut output, symbol| {
if exported_symbols.remove(symbol) {
return output;
}

if let Some(symbol) = symbol.strip_prefix("_ZN") {
let prefix_len = prefix.len();
let _ = writeln!(output, "_ZN{symbol} _ZN{prefix_len}{prefix}{symbol}",);
} else {
let _ = writeln!(output, "{symbol} {prefix}_{symbol}");
}
output
});

let rename_path = archive.with_extension("redefine-syms");
sh.write_file(&rename_path, symbol_renames)?;

let objcopy = binutil("objcopy")?;
cmd!(sh, "{objcopy} --prefix-symbols={prefix}_ {archive}").run()?;
cmd!(sh, "{objcopy} --redefine-syms={rename_path} {archive}").run()?;

sh.remove_path(&rename_path)?;
Expand Down
7 changes: 5 additions & 2 deletions xtask/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::env::{self, VarError};

use anyhow::Result;
Expand Down Expand Up @@ -57,7 +58,7 @@ impl Build {
eprintln!("Exporting hermit-builtins symbols");
let builtins = self.cargo_build.artifact.builtins_archive();
let builtin_symbols = sh.read_file("hermit-builtins/exports")?;
builtins.retain_symbols(builtin_symbols.lines())?;
builtins.retain_symbols(builtin_symbols.lines().collect::<HashSet<_>>())?;

dist_archive.append(&builtins)?;

Expand Down Expand Up @@ -85,6 +86,7 @@ impl Build {

if self.instrument_mcount {
rustflags.push("-Zinstrument-mcount");
rustflags.push("-Cpasses=ee-instrument<post-inline>");
}

if self.randomize_layout {
Expand All @@ -103,6 +105,7 @@ impl Build {
let explicit_exports = [
"_start",
"__bss_start",
"mcount",
"runtime_entry",
// lwIP functions (C runtime)
"init_lwip",
Expand All @@ -118,7 +121,7 @@ impl Build {

let symbols = explicit_exports.chain(syscall_symbols.iter().map(String::as_str));

archive.retain_symbols(symbols)?;
archive.retain_symbols(symbols.collect::<HashSet<_>>())?;

Ok(())
}
Expand Down

0 comments on commit 00844f5

Please sign in to comment.