Skip to content

Tell LLVM to not mangle names if they're already mangled #1063

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

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
3 changes: 3 additions & 0 deletions src/codegen/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub mod attributes {
}

pub fn link_name(name: &str) -> quote::Tokens {
// LLVM mangles the name by default but it's already mangled.
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
let name = format!("\u{1}{}", name);
quote! {
#[link_name = #name]
}
Expand Down
18 changes: 0 additions & 18 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,6 @@ pub struct BindgenContext {
/// bitfield allocation units computed. Drained in `compute_bitfield_units`.
need_bitfield_allocation: Vec<ItemId>,

/// Whether we need the mangling hack which removes the prefixing underscore.
needs_mangling_hack: bool,

/// The set of (`ItemId`s of) types that can't derive debug.
///
/// This is populated when we enter codegen by `compute_cannot_derive_debug`
Expand Down Expand Up @@ -558,15 +555,6 @@ impl BindgenContext {
).expect("TranslationUnit::parse failed")
};

// Mac os, iOS and Win32 need __ for mangled symbols but rust will
// automatically prepend the extra _.
//
// We need to make sure that we don't include __ because rust will turn
// into ___.
let needs_mangling_hack = effective_target.contains("darwin") ||
effective_target.contains("ios") ||
effective_target == "i686-pc-win32";

let root_module = Self::build_root_module(ItemId(0));
let root_module_id = root_module.id().as_module_id_unchecked();

Expand All @@ -592,7 +580,6 @@ impl BindgenContext {
codegen_items: None,
used_template_parameters: None,
need_bitfield_allocation: Default::default(),
needs_mangling_hack: needs_mangling_hack,
cannot_derive_debug: None,
cannot_derive_default: None,
cannot_derive_copy: None,
Expand Down Expand Up @@ -1415,11 +1402,6 @@ impl BindgenContext {
Item::new(id, None, None, id, ItemKind::Module(module))
}

/// Returns the target triple bindgen is running over.
pub fn needs_mangling_hack(&self) -> bool {
self.needs_mangling_hack
}

/// Get the root module.
pub fn root_module(&self) -> ModuleId {
self.root_module
Expand Down
22 changes: 1 addition & 21 deletions src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,6 @@ fn get_abi(cc: CXCallingConv) -> Abi {
}
}

fn mangling_hack_if_needed(ctx: &BindgenContext, symbol: &mut String) {
if ctx.needs_mangling_hack() {
match symbol.chars().next().unwrap() {
// Stripping leading underscore for all names on Darwin and
// C linkage functions on Win32.
'_' => {
symbol.remove(0);
}
// Stop Rust from prepending underscore for variables on Win32.
'?' => {
symbol.insert(0, '\x01');
}
_ => {}
}
}
}

/// Get the mangled name for the cursor's referent.
pub fn cursor_mangling(
ctx: &BindgenContext,
Expand All @@ -241,8 +224,7 @@ pub fn cursor_mangling(
}

if let Ok(mut manglings) = cursor.cxx_manglings() {
if let Some(mut m) = manglings.pop() {
mangling_hack_if_needed(ctx, &mut m);
if let Some(m) = manglings.pop() {
return Some(m);
}
}
Expand All @@ -252,8 +234,6 @@ pub fn cursor_mangling(
return None;
}

mangling_hack_if_needed(ctx, &mut mangling);

if cursor.kind() == clang_sys::CXCursor_Destructor {
// With old (3.8-) libclang versions, and the Itanium ABI, clang returns
// the "destructor group 0" symbol, which means that it'll try to free
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/arg_keyword.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@


extern "C" {
#[link_name = "_Z3fooPKc"]
#[link_name = "\u{1}_Z3fooPKc"]
pub fn foo(type_: *const ::std::os::raw::c_char);
}
6 changes: 3 additions & 3 deletions tests/expectations/tests/bitfield-method-same-name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ fn bindgen_test_layout_Foo() {
);
}
extern "C" {
#[link_name = "_ZN3Foo4typeEv"]
#[link_name = "\u{1}_ZN3Foo4typeEv"]
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char;
}
extern "C" {
#[link_name = "_ZN3Foo9set_type_Ec"]
#[link_name = "\u{1}_ZN3Foo9set_type_Ec"]
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char);
}
extern "C" {
#[link_name = "_ZN3Foo8set_typeEc"]
#[link_name = "\u{1}_ZN3Foo8set_typeEc"]
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char);
}
impl Clone for Foo {
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/bitfield_large_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ impl Clone for _bindgen_ty_1 {
}
}
extern "C" {
#[link_name = "a"]
#[link_name = "\u{1}a"]
pub static mut a: _bindgen_ty_1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ impl Clone for Bar {
fn clone(&self) -> Self { *self }
}
extern "C" {
#[link_name = "_Z3bazPN3foo3BarE"]
#[link_name = "\u{1}_Z3bazPN3foo3BarE"]
pub fn baz(arg1: *mut Bar);
}
8 changes: 4 additions & 4 deletions tests/expectations/tests/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,23 @@ fn bindgen_test_layout_RealAbstractionWithTonsOfMethods() {
RealAbstractionWithTonsOfMethods ) ));
}
extern "C" {
#[link_name = "_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
#[link_name = "\u{1}_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
pub fn RealAbstractionWithTonsOfMethods_bar(this:
*const RealAbstractionWithTonsOfMethods);
}
extern "C" {
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEv"]
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEv"]
pub fn RealAbstractionWithTonsOfMethods_bar1(this:
*mut RealAbstractionWithTonsOfMethods);
}
extern "C" {
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEi"]
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEi"]
pub fn RealAbstractionWithTonsOfMethods_bar2(this:
*mut RealAbstractionWithTonsOfMethods,
foo: ::std::os::raw::c_int);
}
extern "C" {
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3staEv"]
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3staEv"]
pub fn RealAbstractionWithTonsOfMethods_sta();
}
impl Clone for RealAbstractionWithTonsOfMethods {
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/tests/class_1_0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,22 +395,22 @@ fn bindgen_test_layout_RealAbstractionWithTonsOfMethods() {
);
}
extern "C" {
#[link_name = "_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
#[link_name = "\u{1}_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
pub fn RealAbstractionWithTonsOfMethods_bar(this: *const RealAbstractionWithTonsOfMethods);
}
extern "C" {
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEv"]
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEv"]
pub fn RealAbstractionWithTonsOfMethods_bar1(this: *mut RealAbstractionWithTonsOfMethods);
}
extern "C" {
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEi"]
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEi"]
pub fn RealAbstractionWithTonsOfMethods_bar2(
this: *mut RealAbstractionWithTonsOfMethods,
foo: ::std::os::raw::c_int,
);
}
extern "C" {
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3staEv"]
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3staEv"]
pub fn RealAbstractionWithTonsOfMethods_sta();
}
impl Clone for RealAbstractionWithTonsOfMethods {
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/class_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Clone for A_C {
}
}
extern "C" {
#[link_name = "var"]
#[link_name = "\u{1}var"]
pub static mut var: A_B;
}
#[test]
Expand All @@ -131,7 +131,7 @@ fn __bindgen_test_layout_A_D_open0_int_close0_instantiation() {
assert_eq ! ( :: std :: mem :: align_of :: < A_D < :: std :: os :: raw :: c_int > > ( ) , 4usize , concat ! ( "Alignment of template specialization: " , stringify ! ( A_D < :: std :: os :: raw :: c_int > ) ) );
}
extern "C" {
#[link_name = "baz"]
#[link_name = "\u{1}baz"]
pub static mut baz: A_D<::std::os::raw::c_int>;
}
#[repr(C)]
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/class_static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub struct MyClass {
pub _address: u8,
}
extern "C" {
#[link_name = "_ZN7MyClass7exampleE"]
#[link_name = "\u{1}_ZN7MyClass7exampleE"]
pub static mut MyClass_example: *const ::std::os::raw::c_int;
}
extern "C" {
#[link_name = "_ZN7MyClass26example_check_no_collisionE"]
#[link_name = "\u{1}_ZN7MyClass26example_check_no_collisionE"]
pub static mut MyClass_example_check_no_collision:
*const ::std::os::raw::c_int;
}
Expand All @@ -29,6 +29,6 @@ impl Clone for MyClass {
fn clone(&self) -> Self { *self }
}
extern "C" {
#[link_name = "_ZL26example_check_no_collision"]
#[link_name = "\u{1}_ZL26example_check_no_collision"]
pub static mut example_check_no_collision: *const ::std::os::raw::c_int;
}
8 changes: 4 additions & 4 deletions tests/expectations/tests/class_with_typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ fn bindgen_test_layout_C() {
! ( other_ptr ) ));
}
extern "C" {
#[link_name = "_ZN1C6methodEi"]
#[link_name = "\u{1}_ZN1C6methodEi"]
pub fn C_method(this: *mut C, c: C_MyInt);
}
extern "C" {
#[link_name = "_ZN1C9methodRefERi"]
#[link_name = "\u{1}_ZN1C9methodRefERi"]
pub fn C_methodRef(this: *mut C, c: *mut C_MyInt);
}
extern "C" {
#[link_name = "_ZN1C16complexMethodRefERPKc"]
#[link_name = "\u{1}_ZN1C16complexMethodRefERPKc"]
pub fn C_complexMethodRef(this: *mut C, c: *mut C_Lookup);
}
extern "C" {
#[link_name = "_ZN1C13anotherMethodEi"]
#[link_name = "\u{1}_ZN1C13anotherMethodEi"]
pub fn C_anotherMethod(this: *mut C, c: AnotherInt);
}
impl Clone for C {
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/complex_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ pub struct __BindgenComplex<T> {
pub im: T,
}
extern "C" {
#[link_name = "globalValueFloat"]
#[link_name = "\u{1}globalValueFloat"]
pub static mut globalValueFloat: __BindgenComplex<f32>;
}
extern "C" {
#[link_name = "globalValueDouble"]
#[link_name = "\u{1}globalValueDouble"]
pub static mut globalValueDouble: __BindgenComplex<f64>;
}
extern "C" {
#[link_name = "globalValueLongDouble"]
#[link_name = "\u{1}globalValueLongDouble"]
pub static mut globalValueLongDouble: __BindgenComplex<f64>;
}
8 changes: 4 additions & 4 deletions tests/expectations/tests/constify-module-enums-types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ impl Default for Bar {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
extern "C" {
#[link_name = "_Z5func13fooPS_PS0_"]
#[link_name = "\u{1}_Z5func13fooPS_PS0_"]
pub fn func1(arg1: foo::Type, arg2: *mut foo::Type,
arg3: *mut *mut foo::Type) -> *mut foo::Type;
}
extern "C" {
#[link_name = "_Z5func23fooPS_PS0_"]
#[link_name = "\u{1}_Z5func23fooPS_PS0_"]
pub fn func2(arg1: foo_alias1, arg2: *mut foo_alias1,
arg3: *mut *mut foo_alias1) -> *mut foo_alias1;
}
Expand All @@ -184,10 +184,10 @@ impl <T> Default for Thing<T> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
extern "C" {
#[link_name = "_Z5func35ThingI3fooE"]
#[link_name = "\u{1}_Z5func35ThingI3fooE"]
pub fn func3(arg1: Thing<foo::Type>) -> foo::Type;
}
extern "C" {
#[link_name = "_Z5func45ThingIS_I3fooEE"]
#[link_name = "\u{1}_Z5func45ThingIS_I3fooEE"]
pub fn func4(arg1: Thing<Thing<foo::Type>>) -> foo::Type;
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/constructor-tp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn bindgen_test_layout_Bar() {
"Alignment of " , stringify ! ( Bar ) ));
}
extern "C" {
#[link_name = "_ZN3BarC1Ev"]
#[link_name = "\u{1}_ZN3BarC1Ev"]
pub fn Bar_Bar(this: *mut Bar);
}
impl Clone for Bar {
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ fn bindgen_test_layout_TestOverload() {
"Alignment of " , stringify ! ( TestOverload ) ));
}
extern "C" {
#[link_name = "_ZN12TestOverloadC1Ei"]
#[link_name = "\u{1}_ZN12TestOverloadC1Ei"]
pub fn TestOverload_TestOverload(this: *mut TestOverload,
arg1: ::std::os::raw::c_int);
}
extern "C" {
#[link_name = "_ZN12TestOverloadC1Ed"]
#[link_name = "\u{1}_ZN12TestOverloadC1Ed"]
pub fn TestOverload_TestOverload1(this: *mut TestOverload, arg1: f64);
}
impl Clone for TestOverload {
Expand Down Expand Up @@ -55,7 +55,7 @@ fn bindgen_test_layout_TestPublicNoArgs() {
( "Alignment of " , stringify ! ( TestPublicNoArgs ) ));
}
extern "C" {
#[link_name = "_ZN16TestPublicNoArgsC1Ev"]
#[link_name = "\u{1}_ZN16TestPublicNoArgsC1Ev"]
pub fn TestPublicNoArgs_TestPublicNoArgs(this: *mut TestPublicNoArgs);
}
impl Clone for TestPublicNoArgs {
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/decl_extern_int_twice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@


extern "C" {
#[link_name = "foo"]
#[link_name = "\u{1}foo"]
pub static mut foo: ::std::os::raw::c_int;
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/decl_ptr_to_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@


extern "C" {
#[link_name = "foo"]
#[link_name = "\u{1}foo"]
pub static mut foo: *mut [::std::os::raw::c_int; 1usize];
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/default-template-parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ fn __bindgen_test_layout_Foo_open0_bool__int_close0_instantiation() {
);
}
extern "C" {
#[link_name = "_ZL3bar"]
#[link_name = "\u{1}_ZL3bar"]
pub static mut bar: Foo<bool, ::std::os::raw::c_int>;
}
6 changes: 3 additions & 3 deletions tests/expectations/tests/derive-bitfield-method-same-name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ fn bindgen_test_layout_Foo() {
);
}
extern "C" {
#[link_name = "_ZN3Foo4typeEv"]
#[link_name = "\u{1}_ZN3Foo4typeEv"]
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char;
}
extern "C" {
#[link_name = "_ZN3Foo9set_type_Ec"]
#[link_name = "\u{1}_ZN3Foo9set_type_Ec"]
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char);
}
extern "C" {
#[link_name = "_ZN3Foo8set_typeEc"]
#[link_name = "\u{1}_ZN3Foo8set_typeEc"]
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char);
}
impl Clone for Foo {
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/elaborated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@

pub type whatever_whatever_t = ::std::os::raw::c_int;
extern "C" {
#[link_name = "_Z9somethingPKi"]
#[link_name = "\u{1}_Z9somethingPKi"]
pub fn something(wat: *const whatever_whatever_t);
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/enum_and_vtable_mangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ impl Default for C {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
extern "C" {
#[link_name = "_ZN1C5matchEv"]
#[link_name = "\u{1}_ZN1C5matchEv"]
pub fn C_match(this: *mut ::std::os::raw::c_void);
}
Loading