Skip to content

Commit

Permalink
Make --demangle/--nodemangle affect more things
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlattimore committed Feb 24, 2025
1 parent eb74c35 commit 87d8ca6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion libwild/src/elf_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2598,7 +2598,7 @@ fn write_internal_symbols<S: StorageModel>(
.with_context(|| {
format!(
"symbol `{}` in section `{}` that we're not going to output {resolution:?}",
symbol_name,
layout.symbol_db.symbol_name_for_display(symbol_id),
layout.output_sections.display_name(section_id)
)
})?;
Expand Down
11 changes: 3 additions & 8 deletions libwild/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ trait SymbolRequestHandler<'data, S: StorageModel>: std::fmt::Display + HandlerD
) -> Result {
bail!(
"Cannot perform copy relocation for undefined symbol `{}`",
symbol_db.symbol_name(symbol_id)?
symbol_db.symbol_name_for_display(symbol_id)
);
}
}
Expand Down Expand Up @@ -2546,13 +2546,8 @@ fn process_relocation<S: StorageModel, A: Arch>(
) {
let symbol_name = symbol_db.symbol_name_for_display(symbol_id);
resources.report_error(anyhow::anyhow!(
"Undefined symbol {}, referenced by {}",
if args.demangle {
symbol_name.to_string()
} else {
String::from_utf8_lossy(symbol_name.bytes()).to_string()
},
object.input
"Undefined symbol {symbol_name}, referenced by {}",
object.input,
));
}
}
Expand Down
2 changes: 1 addition & 1 deletion libwild/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl<'data> VersionedSymbolName<'data> {
impl Display for UnversionedSymbolName<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Ok(s) = std::str::from_utf8(self.bytes) {
symbolic_demangle::demangle(s).fmt(f)
Display::fmt(s, f)
} else {
write!(f, "INVALID UTF-8({:?})", self.bytes)
}
Expand Down
36 changes: 29 additions & 7 deletions libwild/src/symbol_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ pub(crate) struct SymbolIdRange {
num_symbols: usize,
}

pub(crate) struct SymbolNameDisplay<'data> {
name: Option<UnversionedSymbolName<'data>>,
demangle: bool,
}

impl SymbolIdRange {
pub(crate) fn prelude(num_symbols: usize) -> SymbolIdRange {
SymbolIdRange {
Expand Down Expand Up @@ -388,12 +393,11 @@ impl<'data, S: StorageModel> SymbolDb<'data, S> {
}
}

pub(crate) fn symbol_name_for_display(
&self,
symbol_id: SymbolId,
) -> UnversionedSymbolName<'data> {
self.symbol_name(symbol_id)
.unwrap_or_else(|_| UnversionedSymbolName::new(b"??"))
pub(crate) fn symbol_name_for_display(&self, symbol_id: SymbolId) -> SymbolNameDisplay<'data> {
SymbolNameDisplay {
name: self.symbol_name(symbol_id).ok(),
demangle: self.args.demangle,
}
}

pub(crate) fn symbol_name(&self, symbol_id: SymbolId) -> Result<UnversionedSymbolName<'data>> {
Expand Down Expand Up @@ -873,7 +877,7 @@ impl<S: StorageModel> std::fmt::Display for SymbolDebug<'_, '_, S> {
ParsedInput::Epilogue(_) => write!(f, "<unnamed custom-section symbol>")?,
}
} else {
write!(f, "symbol `{symbol_name}`")?;
write!(f, "symbol `{}`", self.db.symbol_name_for_display(symbol_id))?;
}
write!(
f,
Expand Down Expand Up @@ -1045,3 +1049,21 @@ impl Display for RawSymbolName<'_> {
Ok(())
}
}

impl Display for SymbolNameDisplay<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(name) = self.name {
if let Ok(s) = std::str::from_utf8(name.bytes()) {
if self.demangle {
Display::fmt(&symbolic_demangle::demangle(s), f)
} else {
Display::fmt(s, f)
}
} else {
write!(f, "INVALID UTF-8({:?})", name.bytes())
}
} else {
write!(f, "SYMBOL-READ-ERROR")
}
}
}

0 comments on commit 87d8ca6

Please sign in to comment.