Skip to content

Commit 950124c

Browse files
committed
Add support for cargo-readelf command
Allows developers to use the `cargo readelf` sub-command. Signed-off-by: James Roy <rruuaanng@outlook.com>
1 parent 57bef93 commit 950124c

4 files changed

Lines changed: 24 additions & 10 deletions

File tree

src/bin/cargo-readelf.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const EXAMPLES: &str = "
2+
3+
EXAMPLES
4+
5+
`cargo readelf --bin app -- -t` - Display the section details
6+
`cargo readelf --bin app -- -s` - Display the symbol table";
7+
8+
fn main() {
9+
cargo_binutils::Tool::Readelf.cargo_exec(Some(EXAMPLES))
10+
}

src/bin/rust-readelf.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
cargo_binutils::Tool::Readelf.rust_exec()
3+
}

src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub fn run(tool: Tool, matches: &ArgMatches) -> Result<i32> {
373373
// make the artifact path relative. This makes the path that the
374374
// tool will print easier to read. eg. `libfoo.rlib` instead of
375375
// `/home/user/rust/project/target/$T/debug/libfoo.rlib`.
376-
Tool::Objdump | Tool::Nm | Tool::Readobj | Tool::Size => {
376+
Tool::Objdump | Tool::Nm | Tool::Readobj | Tool::Readelf | Tool::Size => {
377377
lltool
378378
.current_dir(file.parent().unwrap())
379379
.arg(file.file_name().unwrap());
@@ -409,7 +409,9 @@ pub fn run(tool: Tool, matches: &ArgMatches) -> Result<i32> {
409409
| Tool::Objcopy
410410
| Tool::Profdata
411411
| Tool::Strip => output.stdout.into(),
412-
Tool::Nm | Tool::Objdump | Tool::Readobj => postprocess::demangle(&output.stdout),
412+
Tool::Nm | Tool::Objdump
413+
| Tool::Readobj
414+
| Tool::Readelf => postprocess::demangle(&output.stdout),
413415
Tool::Size => postprocess::size(&output.stdout),
414416
};
415417

@@ -448,24 +450,21 @@ fn cargo_build(matches: &ArgMatches, metadata: &Metadata) -> Result<Option<Artif
448450
let mut target_artifact: Option<Artifact> = None;
449451
for message in messages {
450452
match message? {
451-
Message::CompilerArtifact(artifact) => {
453+
Message::CompilerArtifact(artifact)
452454
if metadata.workspace_members.contains(&artifact.package_id)
453-
&& build_type.matches(&artifact)
454-
{
455+
&& build_type.matches(&artifact) => {
455456
if target_artifact.is_some() {
456457
bail!("Can only have one matching artifact but found several");
457458
}
458459

459460
target_artifact = Some(artifact);
460461
}
461-
}
462-
Message::CompilerMessage(msg) => {
463-
if !quiet || verbose > 1 {
462+
Message::CompilerMessage(msg)
463+
if (!quiet || verbose > 1) => {
464464
if let Some(rendered) = msg.message.rendered {
465465
eprint!("{rendered}");
466466
}
467467
}
468-
}
469468
_ => (),
470469
}
471470
}

src/tool.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub enum Tool {
1818
Objdump,
1919
Profdata,
2020
Readobj,
21+
Readelf,
2122
Size,
2223
Strip,
2324
}
@@ -35,6 +36,7 @@ impl Tool {
3536
Self::Objdump => "objdump",
3637
Self::Profdata => "profdata",
3738
Self::Readobj => "readobj",
39+
Self::Readelf => "readelf",
3840
Self::Size => "size",
3941
Self::Strip => "strip",
4042
}
@@ -112,7 +114,7 @@ impl Tool {
112114
pub const fn needs_build(self) -> bool {
113115
match self {
114116
Self::Ar | Self::As | Self::Cov | Self::Lld | Self::Profdata => false,
115-
Self::Nm | Self::Objcopy | Self::Objdump | Self::Readobj | Self::Size | Self::Strip => {
117+
Self::Nm | Self::Objcopy | Self::Objdump | Self::Readobj | Self::Readelf | Self::Size | Self::Strip => {
116118
true
117119
}
118120
}

0 commit comments

Comments
 (0)