Skip to content

Commit a101063

Browse files
authored
Let LLVM mangle the link name of static wrappers (#2448)
1 parent b3239c5 commit a101063

File tree

6 files changed

+22
-28
lines changed

6 files changed

+22
-28
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@
169169
* The source file generated when the `--wrap-static-fns` flag is enabled now
170170
contains `#include` directives with all the input headers and all the source
171171
code added with the `header_contents` method.
172+
* The source file generated when the `--wrap-static-fns` flag no longer uses
173+
`asm` labeling and the link name of static wrapper functions is allowed to
174+
be mangled.
172175
## Removed
173176
* The following deprecated flags were removed: `--use-msvc-mangling`,
174177
`--rustfmt-bindings` and `--size_t-is-usize`.

bindgen-tests/tests/expectations/tests/generated/wrap_static_fns.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,11 @@
22

33
// Static wrappers
44

5-
int foo__extern(void) asm("foo__extern");
65
int foo__extern(void) { return foo(); }
7-
int bar__extern(void) asm("bar__extern");
86
int bar__extern(void) { return bar(); }
9-
int takes_ptr__extern(int *arg) asm("takes_ptr__extern");
107
int takes_ptr__extern(int *arg) { return takes_ptr(arg); }
11-
int takes_fn_ptr__extern(int (*f) (int)) asm("takes_fn_ptr__extern");
128
int takes_fn_ptr__extern(int (*f) (int)) { return takes_fn_ptr(f); }
13-
int takes_fn__extern(int (f) (int)) asm("takes_fn__extern");
149
int takes_fn__extern(int (f) (int)) { return takes_fn(f); }
15-
int takes_alias__extern(func f) asm("takes_alias__extern");
1610
int takes_alias__extern(func f) { return takes_alias(f); }
17-
int takes_qualified__extern(const int *const *arg) asm("takes_qualified__extern");
1811
int takes_qualified__extern(const int *const *arg) { return takes_qualified(arg); }
19-
enum foo takes_enum__extern(const enum foo f) asm("takes_enum__extern");
2012
enum foo takes_enum__extern(const enum foo f) { return takes_enum(f); }

bindgen-tests/tests/expectations/tests/wrap-static-fns.rs

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen/codegen/helpers.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use quote::TokenStreamExt;
77

88
pub(crate) mod attributes {
99
use proc_macro2::{Ident, Span, TokenStream};
10-
use std::str::FromStr;
10+
use std::{borrow::Cow, str::FromStr};
1111

1212
pub(crate) fn repr(which: &str) -> TokenStream {
1313
let which = Ident::new(which, Span::call_site());
@@ -62,10 +62,15 @@ pub(crate) mod attributes {
6262
}
6363
}
6464

65-
pub(crate) fn link_name(name: &str) -> TokenStream {
65+
pub(crate) fn link_name<const MANGLE: bool>(name: &str) -> TokenStream {
6666
// LLVM mangles the name by default but it's already mangled.
6767
// Prefixing the name with \u{1} should tell LLVM to not mangle it.
68-
let name = format!("\u{1}{}", name);
68+
let name: Cow<'_, str> = if MANGLE {
69+
name.into()
70+
} else {
71+
format!("\u{1}{}", name).into()
72+
};
73+
6974
quote! {
7075
#[link_name = #name]
7176
}

bindgen/codegen/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl CodeGenerator for Var {
751751
link_name,
752752
None,
753753
) {
754-
attrs.push(attributes::link_name(link_name));
754+
attrs.push(attributes::link_name::<false>(link_name));
755755
}
756756

757757
let maybe_mut = if self.is_const() {
@@ -4155,7 +4155,7 @@ impl CodeGenerator for Function {
41554155
Some(abi),
41564156
)
41574157
{
4158-
attributes.push(attributes::link_name(link_name));
4158+
attributes.push(attributes::link_name::<false>(link_name));
41594159
has_link_name_attr = true;
41604160
}
41614161

@@ -4169,7 +4169,7 @@ impl CodeGenerator for Function {
41694169

41704170
if is_internal && ctx.options().wrap_static_fns && !has_link_name_attr {
41714171
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();
4172-
attributes.push(attributes::link_name(&name));
4172+
attributes.push(attributes::link_name::<true>(&name));
41734173
}
41744174

41754175
let ident = ctx.rust_ident(canonical_name);

bindgen/codegen/serialize.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,6 @@ impl<'a> CSerialize<'a> for Function {
105105
// The function's return type
106106
let ret_ty = signature.return_type();
107107

108-
// Write `ret_ty wrap_name(args) asm("wrap_name");`
109-
ret_ty.serialize(ctx, (), stack, writer)?;
110-
write!(writer, " {}(", wrap_name)?;
111-
serialize_args(&args, ctx, writer)?;
112-
writeln!(writer, ") asm(\"{}\");", wrap_name)?;
113-
114108
// Write `ret_ty wrap_name(args) { return name(arg_names)' }`
115109
ret_ty.serialize(ctx, (), stack, writer)?;
116110
write!(writer, " {}(", wrap_name)?;

0 commit comments

Comments
 (0)