Skip to content

Commit 00844f5

Browse files
authored
Merge pull request #991 from mkroening/instrument
feat: make ready for instrumentation
2 parents bb07a10 + 341233d commit 00844f5

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

xtask/src/archive.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashSet;
12
use std::env;
23
use std::fmt::Write;
34
use std::path::{Path, PathBuf};
@@ -41,21 +42,40 @@ impl Archive {
4142
Ok(symbols)
4243
}
4344

44-
pub fn retain_symbols<'a>(&self, symbols: impl Iterator<Item = &'a str>) -> Result<()> {
45+
pub fn retain_symbols(&self, mut exported_symbols: HashSet<&str>) -> Result<()> {
4546
let sh = crate::sh()?;
4647
let archive = self.as_ref();
47-
let prefix = archive.file_stem().unwrap().to_str().unwrap();
48-
49-
let symbol_renames = symbols.fold(String::new(), |mut output, symbol| {
50-
let _ = writeln!(output, "{prefix}_{symbol} {symbol}");
51-
output
52-
});
48+
let prefix = {
49+
let file_stem = archive.file_stem().unwrap().to_str().unwrap();
50+
file_stem.strip_prefix("lib").unwrap_or(file_stem)
51+
};
52+
53+
let all_symbols = {
54+
let nm = binutil("nm")?;
55+
let stdout = cmd!(sh, "{nm} --export-symbols {archive}").output()?.stdout;
56+
String::from_utf8(stdout)?
57+
};
58+
59+
let symbol_renames = all_symbols
60+
.lines()
61+
.fold(String::new(), |mut output, symbol| {
62+
if exported_symbols.remove(symbol) {
63+
return output;
64+
}
65+
66+
if let Some(symbol) = symbol.strip_prefix("_ZN") {
67+
let prefix_len = prefix.len();
68+
let _ = writeln!(output, "_ZN{symbol} _ZN{prefix_len}{prefix}{symbol}",);
69+
} else {
70+
let _ = writeln!(output, "{symbol} {prefix}_{symbol}");
71+
}
72+
output
73+
});
5374

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

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

6181
sh.remove_path(&rename_path)?;

xtask/src/build.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashSet;
12
use std::env::{self, VarError};
23

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

6263
dist_archive.append(&builtins)?;
6364

@@ -85,6 +86,7 @@ impl Build {
8586

8687
if self.instrument_mcount {
8788
rustflags.push("-Zinstrument-mcount");
89+
rustflags.push("-Cpasses=ee-instrument<post-inline>");
8890
}
8991

9092
if self.randomize_layout {
@@ -103,6 +105,7 @@ impl Build {
103105
let explicit_exports = [
104106
"_start",
105107
"__bss_start",
108+
"mcount",
106109
"runtime_entry",
107110
// lwIP functions (C runtime)
108111
"init_lwip",
@@ -118,7 +121,7 @@ impl Build {
118121

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

121-
archive.retain_symbols(symbols)?;
124+
archive.retain_symbols(symbols.collect::<HashSet<_>>())?;
122125

123126
Ok(())
124127
}

0 commit comments

Comments
 (0)