Skip to content

Commit 83bbe9f

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 83bbe9f

4 files changed

Lines changed: 36 additions & 18 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: 14 additions & 15 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 | Tool::Readobj | Tool::Readelf => {
413+
postprocess::demangle(&output.stdout)
414+
}
413415
Tool::Size => postprocess::size(&output.stdout),
414416
};
415417

@@ -448,22 +450,19 @@ 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-
if target_artifact.is_some() {
456-
bail!("Can only have one matching artifact but found several");
457-
}
458-
459-
target_artifact = Some(artifact);
455+
&& build_type.matches(&artifact) =>
456+
{
457+
if target_artifact.is_some() {
458+
bail!("Can only have one matching artifact but found several");
460459
}
460+
461+
target_artifact = Some(artifact);
461462
}
462-
Message::CompilerMessage(msg) => {
463-
if !quiet || verbose > 1 {
464-
if let Some(rendered) = msg.message.rendered {
465-
eprint!("{rendered}");
466-
}
463+
Message::CompilerMessage(msg) if (!quiet || verbose > 1) => {
464+
if let Some(rendered) = msg.message.rendered {
465+
eprint!("{rendered}");
467466
}
468467
}
469468
_ => (),

src/tool.rs

Lines changed: 9 additions & 3 deletions
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,9 +114,13 @@ 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 => {
116-
true
117-
}
117+
Self::Nm
118+
| Self::Objcopy
119+
| Self::Objdump
120+
| Self::Readobj
121+
| Self::Readelf
122+
| Self::Size
123+
| Self::Strip => true,
118124
}
119125
}
120126
}

0 commit comments

Comments
 (0)