Skip to content

Commit 3c8dafb

Browse files
committed
ir: Force the D1 destructor symbol even in older libclang versions.
1 parent afacb8d commit 3c8dafb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/ir/function.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,36 @@ pub fn cursor_mangling(ctx: &BindgenContext,
131131
}
132132

133133
// Try to undo backend linkage munging (prepended _, generally)
134+
//
135+
// TODO(emilio): This is wrong when the target system is not the host
136+
// system. See https://github.com/servo/rust-bindgen/issues/593
134137
if cfg!(target_os = "macos") {
135138
mangling.remove(0);
136139
}
137140

141+
if cursor.kind() == clang_sys::CXCursor_Destructor {
142+
// With old (3.8-) libclang versions, and the Itanium ABI, clang returns
143+
// the "destructor group 0" symbol, which means that it'll try to free
144+
// memory, which definitely isn't what we want.
145+
//
146+
// Explicitly force the destructor group 1 symbol.
147+
//
148+
// See http://refspecs.linuxbase.org/cxxabi-1.83.html#mangling-special
149+
// for the reference, and http://stackoverflow.com/a/6614369/1091587 for
150+
// a more friendly explanation.
151+
//
152+
// We don't need to do this for constructors since clang seems to always
153+
// have returned the C1 constructor.
154+
//
155+
// FIXME(emilio): Can a legit symbol in other ABIs end with this string?
156+
// I don't think so, but if it can this would become a linker error
157+
// anyway, not an invalid free at runtime.
158+
if mangling.ends_with("D0Ev") {
159+
mangling.truncate(mangling.len() - 4);
160+
mangling.push_str("D1Ev");
161+
}
162+
}
163+
138164
Some(mangling)
139165
}
140166

0 commit comments

Comments
 (0)