use llvm_readobj in run-make test instead of nm

This commit is contained in:
Oneirical 2024-06-18 14:57:00 -04:00
parent c1597f9039
commit 977d3f6f96
3 changed files with 28 additions and 63 deletions

View File

@ -2,8 +2,8 @@
use crate::{env_var, Command}; use crate::{env_var, Command};
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available /// Construct a new `llvm-readobj` invocation with the `GNU` output style.
/// at `$LLVM_BIN_DIR/llvm-readobj`. /// This assumes that `llvm-readobj` is available at `$LLVM_BIN_DIR/llvm-readobj`.
#[track_caller] #[track_caller]
pub fn llvm_readobj() -> LlvmReadobj { pub fn llvm_readobj() -> LlvmReadobj {
LlvmReadobj::new() LlvmReadobj::new()
@ -70,13 +70,24 @@ pub fn llvm_bin_dir() -> PathBuf {
} }
impl LlvmReadobj { impl LlvmReadobj {
/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available /// Construct a new `llvm-readobj` invocation with the `GNU` output style.
/// at `$LLVM_BIN_DIR/llvm-readobj`. /// This assumes that `llvm-readobj` is available at `$LLVM_BIN_DIR/llvm-readobj`.
#[track_caller] #[track_caller]
pub fn new() -> Self { pub fn new() -> Self {
let llvm_readobj = llvm_bin_dir().join("llvm-readobj"); let llvm_readobj = llvm_bin_dir().join("llvm-readobj");
let cmd = Command::new(llvm_readobj); let cmd = Command::new(llvm_readobj);
Self { cmd } let mut readobj = Self { cmd };
readobj.elf_output_style("GNU");
readobj
}
/// Specify the format of the ELF information.
///
/// Valid options are `LLVM` (default), `GNU`, and `JSON`.
pub fn elf_output_style(&mut self, style: &str) -> &mut Self {
self.cmd.arg("--elf-output-style");
self.cmd.arg(style);
self
} }
/// Provide an input file. /// Provide an input file.
@ -90,6 +101,13 @@ pub fn file_header(&mut self) -> &mut Self {
self.cmd.arg("--file-header"); self.cmd.arg("--file-header");
self self
} }
/// Specify the section to display.
pub fn section(&mut self, section: &str) -> &mut Self {
self.cmd.arg("--string-dump");
self.cmd.arg(section);
self
}
} }
impl LlvmProfdata { impl LlvmProfdata {

View File

@ -1,50 +0,0 @@
use crate::{fs_wrapper, object};
use object::{Object, ObjectSection};
use std::path::Path;
#[derive(Debug)]
pub struct Nm<'a> {
file: Option<object::File<'a>>,
}
pub fn nm() -> Nm {
Nm::new()
}
impl Nm {
/// Construct a bare `nm` invocation.
pub fn new() -> Self {
Self { file: None }
}
/// Specify the file to analyze the symbols of.
pub fn input<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
&mut Self {
file: Some(
object::File::parse(fs_wrapper::read(path))
.expect(format!("Failed to parse ELF file at {:?}", path.as_ref().display())),
),
}
}
/// Collect all symbols of an object file into a String.
pub fn collect_symbols(&self) -> String {
let object_file = self.file;
let mut symbols_str = String::new();
for section in object_file.sections() {
if let Ok(object::read::elf::SymbolTable(st)) =
section.parse::<object::read::elf::SymbolTable<'_, '_>>()
{
for symbol in st.symbols() {
symbols_str.push_str(&format!(
"{:016x} {:?} {}\n",
symbol.address(),
symbol.kind(),
symbol.name()
));
}
}
}
symbols_str
}
}

View File

@ -5,15 +5,12 @@
// emitted inside the object files. // emitted inside the object files.
// See https://github.com/rust-lang/rust/issues/51671 // See https://github.com/rust-lang/rust/issues/51671
use run_make_support::{nm, rustc, tmp_dir}; use run_make_support::{llvm_readobj, rustc};
fn main() { fn main() {
rustc().emit("obj").input("app.rs").run(); rustc().emit("obj").input("app.rs").run();
//FIXME(Oneirical): This should eventually be rmake_out_path let out = llvm_readobj().input("app.o").arg("--symbols").run();
let nm = nm(tmp_dir().join("app.o")); out.assert_stdout_contains("rust_begin_unwind");
assert!( out.assert_stdout_contains("rust_eh_personality");
nm.contains("rust_begin_unwind") out.assert_stdout_contains("__rg_oom");
&& nm.contains("rust_eh_personality")
&& nm.contains("__rg_oom")
);
} }