Skip to content

Commit 745d606

Browse files
author
bors-servo
authored
Auto merge of #1063 - liranringel:disable-mangling-if-link-name-is-specified, r=emilio
Tell LLVM to not mangle names if they're already mangled LLVM mangles the name by default but functions are already mangled because the `link_name` attribute's value is mangled. Prefixing the name with `\u{1}` should tell LLVM to not mangle it. I originally thought it's a bug in rustc, but it was clarified here: rust-lang/rust#45073
2 parents c7fe6b6 + f2b30c8 commit 745d606

File tree

75 files changed

+179
-211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+179
-211
lines changed

src/codegen/helpers.rs

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub mod attributes {
4545
}
4646

4747
pub fn link_name(name: &str) -> quote::Tokens {
48+
// LLVM mangles the name by default but it's already mangled.
49+
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
50+
let name = format!("\u{1}{}", name);
4851
quote! {
4952
#[link_name = #name]
5053
}

src/ir/context.rs

-18
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,6 @@ pub struct BindgenContext {
391391
/// bitfield allocation units computed. Drained in `compute_bitfield_units`.
392392
need_bitfield_allocation: Vec<ItemId>,
393393

394-
/// Whether we need the mangling hack which removes the prefixing underscore.
395-
needs_mangling_hack: bool,
396-
397394
/// The set of (`ItemId`s of) types that can't derive debug.
398395
///
399396
/// This is populated when we enter codegen by `compute_cannot_derive_debug`
@@ -558,15 +555,6 @@ impl BindgenContext {
558555
).expect("TranslationUnit::parse failed")
559556
};
560557

561-
// Mac os, iOS and Win32 need __ for mangled symbols but rust will
562-
// automatically prepend the extra _.
563-
//
564-
// We need to make sure that we don't include __ because rust will turn
565-
// into ___.
566-
let needs_mangling_hack = effective_target.contains("darwin") ||
567-
effective_target.contains("ios") ||
568-
effective_target == "i686-pc-win32";
569-
570558
let root_module = Self::build_root_module(ItemId(0));
571559
let root_module_id = root_module.id().as_module_id_unchecked();
572560

@@ -592,7 +580,6 @@ impl BindgenContext {
592580
codegen_items: None,
593581
used_template_parameters: None,
594582
need_bitfield_allocation: Default::default(),
595-
needs_mangling_hack: needs_mangling_hack,
596583
cannot_derive_debug: None,
597584
cannot_derive_default: None,
598585
cannot_derive_copy: None,
@@ -1415,11 +1402,6 @@ impl BindgenContext {
14151402
Item::new(id, None, None, id, ItemKind::Module(module))
14161403
}
14171404

1418-
/// Returns the target triple bindgen is running over.
1419-
pub fn needs_mangling_hack(&self) -> bool {
1420-
self.needs_mangling_hack
1421-
}
1422-
14231405
/// Get the root module.
14241406
pub fn root_module(&self) -> ModuleId {
14251407
self.root_module

src/ir/function.rs

+1-21
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,6 @@ fn get_abi(cc: CXCallingConv) -> Abi {
206206
}
207207
}
208208

209-
fn mangling_hack_if_needed(ctx: &BindgenContext, symbol: &mut String) {
210-
if ctx.needs_mangling_hack() {
211-
match symbol.chars().next().unwrap() {
212-
// Stripping leading underscore for all names on Darwin and
213-
// C linkage functions on Win32.
214-
'_' => {
215-
symbol.remove(0);
216-
}
217-
// Stop Rust from prepending underscore for variables on Win32.
218-
'?' => {
219-
symbol.insert(0, '\x01');
220-
}
221-
_ => {}
222-
}
223-
}
224-
}
225-
226209
/// Get the mangled name for the cursor's referent.
227210
pub fn cursor_mangling(
228211
ctx: &BindgenContext,
@@ -241,8 +224,7 @@ pub fn cursor_mangling(
241224
}
242225

243226
if let Ok(mut manglings) = cursor.cxx_manglings() {
244-
if let Some(mut m) = manglings.pop() {
245-
mangling_hack_if_needed(ctx, &mut m);
227+
if let Some(m) = manglings.pop() {
246228
return Some(m);
247229
}
248230
}
@@ -252,8 +234,6 @@ pub fn cursor_mangling(
252234
return None;
253235
}
254236

255-
mangling_hack_if_needed(ctx, &mut mangling);
256-
257237
if cursor.kind() == clang_sys::CXCursor_Destructor {
258238
// With old (3.8-) libclang versions, and the Itanium ABI, clang returns
259239
// the "destructor group 0" symbol, which means that it'll try to free

tests/expectations/tests/arg_keyword.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66

77
extern "C" {
8-
#[link_name = "_Z3fooPKc"]
8+
#[link_name = "\u{1}_Z3fooPKc"]
99
pub fn foo(type_: *const ::std::os::raw::c_char);
1010
}

tests/expectations/tests/bitfield-method-same-name.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ fn bindgen_test_layout_Foo() {
2424
);
2525
}
2626
extern "C" {
27-
#[link_name = "_ZN3Foo4typeEv"]
27+
#[link_name = "\u{1}_ZN3Foo4typeEv"]
2828
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char;
2929
}
3030
extern "C" {
31-
#[link_name = "_ZN3Foo9set_type_Ec"]
31+
#[link_name = "\u{1}_ZN3Foo9set_type_Ec"]
3232
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char);
3333
}
3434
extern "C" {
35-
#[link_name = "_ZN3Foo8set_typeEc"]
35+
#[link_name = "\u{1}_ZN3Foo8set_typeEc"]
3636
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char);
3737
}
3838
impl Clone for Foo {

tests/expectations/tests/bitfield_large_overflow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ impl Clone for _bindgen_ty_1 {
2828
}
2929
}
3030
extern "C" {
31-
#[link_name = "a"]
31+
#[link_name = "\u{1}a"]
3232
pub static mut a: _bindgen_ty_1;
3333
}

tests/expectations/tests/canonical_path_without_namespacing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ impl Clone for Bar {
2020
fn clone(&self) -> Self { *self }
2121
}
2222
extern "C" {
23-
#[link_name = "_Z3bazPN3foo3BarE"]
23+
#[link_name = "\u{1}_Z3bazPN3foo3BarE"]
2424
pub fn baz(arg1: *mut Bar);
2525
}

tests/expectations/tests/class.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,23 @@ fn bindgen_test_layout_RealAbstractionWithTonsOfMethods() {
245245
RealAbstractionWithTonsOfMethods ) ));
246246
}
247247
extern "C" {
248-
#[link_name = "_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
248+
#[link_name = "\u{1}_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
249249
pub fn RealAbstractionWithTonsOfMethods_bar(this:
250250
*const RealAbstractionWithTonsOfMethods);
251251
}
252252
extern "C" {
253-
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEv"]
253+
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEv"]
254254
pub fn RealAbstractionWithTonsOfMethods_bar1(this:
255255
*mut RealAbstractionWithTonsOfMethods);
256256
}
257257
extern "C" {
258-
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEi"]
258+
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEi"]
259259
pub fn RealAbstractionWithTonsOfMethods_bar2(this:
260260
*mut RealAbstractionWithTonsOfMethods,
261261
foo: ::std::os::raw::c_int);
262262
}
263263
extern "C" {
264-
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3staEv"]
264+
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3staEv"]
265265
pub fn RealAbstractionWithTonsOfMethods_sta();
266266
}
267267
impl Clone for RealAbstractionWithTonsOfMethods {

tests/expectations/tests/class_1_0.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -395,22 +395,22 @@ fn bindgen_test_layout_RealAbstractionWithTonsOfMethods() {
395395
);
396396
}
397397
extern "C" {
398-
#[link_name = "_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
398+
#[link_name = "\u{1}_ZNK32RealAbstractionWithTonsOfMethods3barEv"]
399399
pub fn RealAbstractionWithTonsOfMethods_bar(this: *const RealAbstractionWithTonsOfMethods);
400400
}
401401
extern "C" {
402-
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEv"]
402+
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEv"]
403403
pub fn RealAbstractionWithTonsOfMethods_bar1(this: *mut RealAbstractionWithTonsOfMethods);
404404
}
405405
extern "C" {
406-
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3barEi"]
406+
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3barEi"]
407407
pub fn RealAbstractionWithTonsOfMethods_bar2(
408408
this: *mut RealAbstractionWithTonsOfMethods,
409409
foo: ::std::os::raw::c_int,
410410
);
411411
}
412412
extern "C" {
413-
#[link_name = "_ZN32RealAbstractionWithTonsOfMethods3staEv"]
413+
#[link_name = "\u{1}_ZN32RealAbstractionWithTonsOfMethods3staEv"]
414414
pub fn RealAbstractionWithTonsOfMethods_sta();
415415
}
416416
impl Clone for RealAbstractionWithTonsOfMethods {

tests/expectations/tests/class_nested.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl Clone for A_C {
115115
}
116116
}
117117
extern "C" {
118-
#[link_name = "var"]
118+
#[link_name = "\u{1}var"]
119119
pub static mut var: A_B;
120120
}
121121
#[test]
@@ -131,7 +131,7 @@ fn __bindgen_test_layout_A_D_open0_int_close0_instantiation() {
131131
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 > ) ) );
132132
}
133133
extern "C" {
134-
#[link_name = "baz"]
134+
#[link_name = "\u{1}baz"]
135135
pub static mut baz: A_D<::std::os::raw::c_int>;
136136
}
137137
#[repr(C)]

tests/expectations/tests/class_static.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ pub struct MyClass {
1010
pub _address: u8,
1111
}
1212
extern "C" {
13-
#[link_name = "_ZN7MyClass7exampleE"]
13+
#[link_name = "\u{1}_ZN7MyClass7exampleE"]
1414
pub static mut MyClass_example: *const ::std::os::raw::c_int;
1515
}
1616
extern "C" {
17-
#[link_name = "_ZN7MyClass26example_check_no_collisionE"]
17+
#[link_name = "\u{1}_ZN7MyClass26example_check_no_collisionE"]
1818
pub static mut MyClass_example_check_no_collision:
1919
*const ::std::os::raw::c_int;
2020
}
@@ -29,6 +29,6 @@ impl Clone for MyClass {
2929
fn clone(&self) -> Self { *self }
3030
}
3131
extern "C" {
32-
#[link_name = "_ZL26example_check_no_collision"]
32+
#[link_name = "\u{1}_ZL26example_check_no_collision"]
3333
pub static mut example_check_no_collision: *const ::std::os::raw::c_int;
3434
}

tests/expectations/tests/class_with_typedef.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,19 @@ fn bindgen_test_layout_C() {
4747
! ( other_ptr ) ));
4848
}
4949
extern "C" {
50-
#[link_name = "_ZN1C6methodEi"]
50+
#[link_name = "\u{1}_ZN1C6methodEi"]
5151
pub fn C_method(this: *mut C, c: C_MyInt);
5252
}
5353
extern "C" {
54-
#[link_name = "_ZN1C9methodRefERi"]
54+
#[link_name = "\u{1}_ZN1C9methodRefERi"]
5555
pub fn C_methodRef(this: *mut C, c: *mut C_MyInt);
5656
}
5757
extern "C" {
58-
#[link_name = "_ZN1C16complexMethodRefERPKc"]
58+
#[link_name = "\u{1}_ZN1C16complexMethodRefERPKc"]
5959
pub fn C_complexMethodRef(this: *mut C, c: *mut C_Lookup);
6060
}
6161
extern "C" {
62-
#[link_name = "_ZN1C13anotherMethodEi"]
62+
#[link_name = "\u{1}_ZN1C13anotherMethodEi"]
6363
pub fn C_anotherMethod(this: *mut C, c: AnotherInt);
6464
}
6565
impl Clone for C {

tests/expectations/tests/complex_global.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ pub struct __BindgenComplex<T> {
1111
pub im: T,
1212
}
1313
extern "C" {
14-
#[link_name = "globalValueFloat"]
14+
#[link_name = "\u{1}globalValueFloat"]
1515
pub static mut globalValueFloat: __BindgenComplex<f32>;
1616
}
1717
extern "C" {
18-
#[link_name = "globalValueDouble"]
18+
#[link_name = "\u{1}globalValueDouble"]
1919
pub static mut globalValueDouble: __BindgenComplex<f64>;
2020
}
2121
extern "C" {
22-
#[link_name = "globalValueLongDouble"]
22+
#[link_name = "\u{1}globalValueLongDouble"]
2323
pub static mut globalValueLongDouble: __BindgenComplex<f64>;
2424
}

tests/expectations/tests/constify-module-enums-types.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ impl Default for Bar {
165165
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
166166
}
167167
extern "C" {
168-
#[link_name = "_Z5func13fooPS_PS0_"]
168+
#[link_name = "\u{1}_Z5func13fooPS_PS0_"]
169169
pub fn func1(arg1: foo::Type, arg2: *mut foo::Type,
170170
arg3: *mut *mut foo::Type) -> *mut foo::Type;
171171
}
172172
extern "C" {
173-
#[link_name = "_Z5func23fooPS_PS0_"]
173+
#[link_name = "\u{1}_Z5func23fooPS_PS0_"]
174174
pub fn func2(arg1: foo_alias1, arg2: *mut foo_alias1,
175175
arg3: *mut *mut foo_alias1) -> *mut foo_alias1;
176176
}
@@ -184,10 +184,10 @@ impl <T> Default for Thing<T> {
184184
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
185185
}
186186
extern "C" {
187-
#[link_name = "_Z5func35ThingI3fooE"]
187+
#[link_name = "\u{1}_Z5func35ThingI3fooE"]
188188
pub fn func3(arg1: Thing<foo::Type>) -> foo::Type;
189189
}
190190
extern "C" {
191-
#[link_name = "_Z5func45ThingIS_I3fooEE"]
191+
#[link_name = "\u{1}_Z5func45ThingIS_I3fooEE"]
192192
pub fn func4(arg1: Thing<Thing<foo::Type>>) -> foo::Type;
193193
}

tests/expectations/tests/constructor-tp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn bindgen_test_layout_Bar() {
2222
"Alignment of " , stringify ! ( Bar ) ));
2323
}
2424
extern "C" {
25-
#[link_name = "_ZN3BarC1Ev"]
25+
#[link_name = "\u{1}_ZN3BarC1Ev"]
2626
pub fn Bar_Bar(this: *mut Bar);
2727
}
2828
impl Clone for Bar {

tests/expectations/tests/constructors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ fn bindgen_test_layout_TestOverload() {
1717
"Alignment of " , stringify ! ( TestOverload ) ));
1818
}
1919
extern "C" {
20-
#[link_name = "_ZN12TestOverloadC1Ei"]
20+
#[link_name = "\u{1}_ZN12TestOverloadC1Ei"]
2121
pub fn TestOverload_TestOverload(this: *mut TestOverload,
2222
arg1: ::std::os::raw::c_int);
2323
}
2424
extern "C" {
25-
#[link_name = "_ZN12TestOverloadC1Ed"]
25+
#[link_name = "\u{1}_ZN12TestOverloadC1Ed"]
2626
pub fn TestOverload_TestOverload1(this: *mut TestOverload, arg1: f64);
2727
}
2828
impl Clone for TestOverload {
@@ -55,7 +55,7 @@ fn bindgen_test_layout_TestPublicNoArgs() {
5555
( "Alignment of " , stringify ! ( TestPublicNoArgs ) ));
5656
}
5757
extern "C" {
58-
#[link_name = "_ZN16TestPublicNoArgsC1Ev"]
58+
#[link_name = "\u{1}_ZN16TestPublicNoArgsC1Ev"]
5959
pub fn TestPublicNoArgs_TestPublicNoArgs(this: *mut TestPublicNoArgs);
6060
}
6161
impl Clone for TestPublicNoArgs {

tests/expectations/tests/decl_extern_int_twice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66

77
extern "C" {
8-
#[link_name = "foo"]
8+
#[link_name = "\u{1}foo"]
99
pub static mut foo: ::std::os::raw::c_int;
1010
}

tests/expectations/tests/decl_ptr_to_array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66

77
extern "C" {
8-
#[link_name = "foo"]
8+
#[link_name = "\u{1}foo"]
99
pub static mut foo: *mut [::std::os::raw::c_int; 1usize];
1010
}

tests/expectations/tests/default-template-parameter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ fn __bindgen_test_layout_Foo_open0_bool__int_close0_instantiation() {
3737
);
3838
}
3939
extern "C" {
40-
#[link_name = "_ZL3bar"]
40+
#[link_name = "\u{1}_ZL3bar"]
4141
pub static mut bar: Foo<bool, ::std::os::raw::c_int>;
4242
}

tests/expectations/tests/derive-bitfield-method-same-name.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ fn bindgen_test_layout_Foo() {
3939
);
4040
}
4141
extern "C" {
42-
#[link_name = "_ZN3Foo4typeEv"]
42+
#[link_name = "\u{1}_ZN3Foo4typeEv"]
4343
pub fn Foo_type(this: *mut Foo) -> ::std::os::raw::c_char;
4444
}
4545
extern "C" {
46-
#[link_name = "_ZN3Foo9set_type_Ec"]
46+
#[link_name = "\u{1}_ZN3Foo9set_type_Ec"]
4747
pub fn Foo_set_type_(this: *mut Foo, c: ::std::os::raw::c_char);
4848
}
4949
extern "C" {
50-
#[link_name = "_ZN3Foo8set_typeEc"]
50+
#[link_name = "\u{1}_ZN3Foo8set_typeEc"]
5151
pub fn Foo_set_type(this: *mut Foo, c: ::std::os::raw::c_char);
5252
}
5353
impl Clone for Foo {

tests/expectations/tests/elaborated.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66

77
pub type whatever_whatever_t = ::std::os::raw::c_int;
88
extern "C" {
9-
#[link_name = "_Z9somethingPKi"]
9+
#[link_name = "\u{1}_Z9somethingPKi"]
1010
pub fn something(wat: *const whatever_whatever_t);
1111
}

tests/expectations/tests/enum_and_vtable_mangling.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ impl Default for C {
3535
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
3636
}
3737
extern "C" {
38-
#[link_name = "_ZN1C5matchEv"]
38+
#[link_name = "\u{1}_ZN1C5matchEv"]
3939
pub fn C_match(this: *mut ::std::os::raw::c_void);
4040
}

0 commit comments

Comments
 (0)