Skip to content

Commit 03d49b6

Browse files
Molot2032emilio
authored andcommitted
Use link_name for dynamic library loading
1 parent d95359f commit 03d49b6

File tree

6 files changed

+44
-37
lines changed

6 files changed

+44
-37
lines changed

bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen/codegen/dyngen.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ impl DynamicItems {
131131
pub(crate) fn push_func(
132132
&mut self,
133133
ident: &Ident,
134+
symbol: &str,
134135
abi: ClangAbi,
135136
is_variadic: bool,
136137
is_required: bool,
@@ -181,11 +182,12 @@ impl DynamicItems {
181182
}
182183

183184
// N.B: Unwrap the signature upon construction if it is required to be resolved.
184-
let ident_str = codegen::helpers::ast_ty::cstr_expr(ident.to_string());
185+
let symbol_cstr =
186+
codegen::helpers::ast_ty::cstr_expr(symbol.to_string());
185187
let library_get = if ctx.options().wrap_unsafe_ops {
186-
quote!(unsafe { __library.get(#ident_str) })
188+
quote!(unsafe { __library.get(#symbol_cstr) })
187189
} else {
188-
quote!(__library.get(#ident_str))
190+
quote!(__library.get(#symbol_cstr))
189191
};
190192

191193
self.constructor_inits.push(if is_required {
@@ -206,6 +208,7 @@ impl DynamicItems {
206208
pub fn push_var(
207209
&mut self,
208210
ident: &Ident,
211+
symbol: &str,
209212
ty: &TokenStream,
210213
is_required: bool,
211214
wrap_unsafe_ops: bool,
@@ -231,12 +234,13 @@ impl DynamicItems {
231234
}
232235
});
233236

234-
let ident_str = codegen::helpers::ast_ty::cstr_expr(ident.to_string());
237+
let symbol_cstr =
238+
codegen::helpers::ast_ty::cstr_expr(symbol.to_string());
235239

236240
let library_get = if wrap_unsafe_ops {
237-
quote!(unsafe { __library.get::<*mut #ty>(#ident_str) })
241+
quote!(unsafe { __library.get::<*mut #ty>(#symbol_cstr) })
238242
} else {
239-
quote!(__library.get::<*mut #ty>(#ident_str))
243+
quote!(__library.get::<*mut #ty>(#symbol_cstr))
240244
};
241245

242246
let qmark = if is_required { quote!(?) } else { quote!() };

bindgen/codegen/mod.rs

+25-22
Original file line numberDiff line numberDiff line change
@@ -779,20 +779,20 @@ impl CodeGenerator for Var {
779779
}
780780
}
781781
} else {
782-
// If necessary, apply a `#[link_name]` attribute
783-
if let Some(link_name) = self.link_name() {
784-
attrs.push(attributes::link_name::<false>(link_name));
785-
} else {
782+
let symbol: &str = self.link_name().unwrap_or_else(|| {
786783
let link_name =
787784
self.mangled_name().unwrap_or_else(|| self.name());
788-
if !utils::names_will_be_identical_after_mangling(
785+
if utils::names_will_be_identical_after_mangling(
789786
&canonical_name,
790787
link_name,
791788
None,
792789
) {
790+
canonical_name.as_str()
791+
} else {
793792
attrs.push(attributes::link_name::<false>(link_name));
793+
link_name
794794
}
795-
}
795+
});
796796

797797
let maybe_mut = if self.is_const() {
798798
quote! {}
@@ -816,6 +816,7 @@ impl CodeGenerator for Var {
816816
if ctx.options().dynamic_library_name.is_some() {
817817
result.dynamic_items().push_var(
818818
&canonical_ident,
819+
symbol,
819820
&self
820821
.ty()
821822
.to_rust_ty_or_opaque(ctx, &())
@@ -4639,21 +4640,19 @@ impl CodeGenerator for Function {
46394640
write!(&mut canonical_name, "{times_seen}").unwrap();
46404641
}
46414642

4642-
let mut has_link_name_attr = false;
4643-
if let Some(link_name) = self.link_name() {
4644-
attributes.push(attributes::link_name::<false>(link_name));
4645-
has_link_name_attr = true;
4646-
} else {
4647-
let link_name = mangled_name.unwrap_or(name);
4648-
if !is_dynamic_function &&
4649-
!utils::names_will_be_identical_after_mangling(
4650-
&canonical_name,
4651-
link_name,
4652-
Some(abi),
4653-
)
4654-
{
4643+
let link_name_attr = self.link_name().or_else(|| {
4644+
let mangled_name = mangled_name.unwrap_or(name);
4645+
(!utils::names_will_be_identical_after_mangling(
4646+
&canonical_name,
4647+
mangled_name,
4648+
Some(abi),
4649+
))
4650+
.then(|| mangled_name)
4651+
});
4652+
4653+
if let Some(link_name) = link_name_attr {
4654+
if !is_dynamic_function {
46554655
attributes.push(attributes::link_name::<false>(link_name));
4656-
has_link_name_attr = true;
46574656
}
46584657
}
46594658

@@ -4665,8 +4664,9 @@ impl CodeGenerator for Function {
46654664
quote! { #[link(wasm_import_module = #name)] }
46664665
});
46674666

4668-
let should_wrap =
4669-
is_internal && ctx.options().wrap_static_fns && !has_link_name_attr;
4667+
let should_wrap = is_internal &&
4668+
ctx.options().wrap_static_fns &&
4669+
link_name_attr.is_none();
46704670

46714671
if should_wrap {
46724672
let name = canonical_name.clone() + ctx.wrap_static_fns_suffix();
@@ -4732,11 +4732,14 @@ impl CodeGenerator for Function {
47324732

47334733
// If we're doing dynamic binding generation, add to the dynamic items.
47344734
if is_dynamic_function {
4735+
let ident_str = ident.to_string();
4736+
let symbol = link_name_attr.unwrap_or(&ident_str);
47354737
let args_identifiers =
47364738
utils::fnsig_argument_identifiers(ctx, signature);
47374739
let ret_ty = utils::fnsig_return_ty(ctx, signature);
47384740
result.dynamic_items().push_func(
47394741
&ident,
4742+
symbol,
47404743
abi,
47414744
signature.is_variadic(),
47424745
ctx.options().dynamic_link_require_all,

0 commit comments

Comments
 (0)