Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demangle invocation block symbols #197

Merged
merged 1 commit into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/cppfilt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ fn find_mangled(haystack: &[u8]) -> Option<usize> {

for i in 0..haystack.len() - 1 {
if haystack[i] == b'_' {
let next = haystack[i + 1];
if next == b'Z' || next == b'_' && haystack.get(i + 2) == Some(&b'Z') {
return Some(i);
match (haystack[i + 1], haystack.get(i + 2), haystack.get(i + 3), haystack.get(i + 4)) {
(b'Z', _, _, _)
| (b'_', Some(b'Z'), _, _)
| (b'_', Some(b'_'), Some(b'Z'), _) => return Some(i),
| (b'_', Some(b'_'), Some(b'_'), Some(b'Z')) => return Some(i),
_ => (),
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,12 +1299,21 @@ macro_rules! define_vocabulary {
///
/// ```text
/// <mangled-name> ::= _Z <encoding> [<clone-suffix>]*
/// ::= ___Z <encoding> <block_invoke>
/// ::= <type>
///
/// <block_invoke> ::= _block_invoke
/// ::= _block_invoke<decimal-digit>+
/// ::= _block_invoke_<decimal-digit>+
/// ```
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum MangledName {
/// The encoding of the mangled symbol name.
Encoding(Encoding, Vec<CloneSuffix>),

/// The encoding of the mangled symbol name.
BlockInvoke(Encoding, Option<isize>),

/// A top-level type. Technically not allowed by the standard, however in
/// practice this can happen, and is tested for by libiberty.
Type(TypeHandle),
Expand All @@ -1329,6 +1338,23 @@ impl Parse for MangledName {
return Ok((MangledName::Encoding(encoding, clone_suffixes), tail));
}

if let Ok(tail) = consume(b"___Z", input).or_else(|_| consume(b"____Z", input)) {
let (encoding, tail) = Encoding::parse(ctx, subs, tail)?;
let tail = consume(b"_block_invoke", tail)?;

let tail_opt = match consume(b"_", tail) {
Ok(tail) => Some(parse_number(10, false, tail)?),
Err(_) => parse_number(10, false, tail).ok(),
};

let (digits, tail) = match tail_opt {
Some((digits, tail)) => (Some(digits), tail),
None => (None, tail),
};

return Ok((MangledName::BlockInvoke(encoding, digits), tail));
}

if let Ok(tail) = consume(b"_GLOBAL_", input) {
let (global_ctor_dtor, tail) = GlobalCtorDtor::parse(ctx, subs, tail)?;
return Ok((MangledName::GlobalCtorDtor(global_ctor_dtor), tail));
Expand Down Expand Up @@ -1362,6 +1388,11 @@ where
}
Ok(())
},
MangledName::BlockInvoke(ref enc, _) => {
write!(ctx, "invocation function for block in ")?;
enc.demangle(ctx, scope)?;
Ok(())
}
MangledName::Type(ref ty) => ty.demangle(ctx, scope),
MangledName::GlobalCtorDtor(ref gcd) => gcd.demangle(ctx, scope),
}
Expand Down
5 changes: 5 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,3 +506,8 @@ demangles!(
_ZNK1QssERKS_,
"Q::operator<=>(Q const&) const"
);
// Taken from https://git.llvm.org/klaus/libcxxabi/commit/5dd173b3792e868a7ebfa699d156f24075eafc01.diff
demangles!(
___ZN19URLConnectionClient33_clientInterface_cancelConnectionEP16dispatch_queue_sU13block_pointerFvvE_block_invoke14,
"invocation function for block in URLConnectionClient::_clientInterface_cancelConnection(dispatch_queue_s*, void () block_pointer)"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe

Suggested change
"invocation function for block in URLConnectionClient::_clientInterface_cancelConnection(dispatch_queue_s*, void () block_pointer)"
"invocation function for block 14 in URLConnectionClient::_clientInterface_cancelConnection(dispatch_queue_s*, void () block_pointer)"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming -- LLVM demangles without that number. Do you still prefer me to add it in?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you still prefer me to add it in?

I guess not.

);