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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 0 additions & 18 deletions
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

Lines changed: 1 addition & 21 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 2 deletions
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)]

0 commit comments

Comments
 (0)