Skip to content

Commit 2d92c49

Browse files
committed
Add export_name attribute and lower it
The rust language requires the export_name attribute to change the name of a symbol within the assembly whilst keeping a different name at the language level. This is used multiple times within rust-for-linux. gcc/rust/ChangeLog: * backend/rust-compile-base.cc: Change the assembly name on functions with the export_name attribute. Do not mangle the name. * util/rust-attribute-values.h: Add export_name attribute value. * util/rust-attributes.cc: Add export_name attribute to builtin attribute list. gcc/testsuite/ChangeLog: * rust/compile/export_name.rs: New test. Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
1 parent 4d60acd commit 2d92c49

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

gcc/rust/backend/rust-compile-base.cc

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,18 +702,38 @@ HIRCompileBase::compile_function (
702702
/* So that 'MAIN_NAME_P' works. */
703703
main_identifier_node = get_identifier (ir_symbol_name.c_str ());
704704
}
705+
// Local name because fn_name is not mutable.
705706
std::string asm_name = fn_name;
706707

708+
// conditionally mangle the function name
709+
bool should_mangle = true;
710+
711+
auto get_export_name = [] (AST::Attribute &attr) {
712+
return attr.get_path ().as_string () == Values::Attributes::EXPORT_NAME;
713+
};
714+
auto export_name_attr
715+
= std::find_if (outer_attrs.begin (), outer_attrs.end (), get_export_name);
716+
717+
tl::optional<std::string> backend_asm_name = tl::nullopt;
718+
719+
if (export_name_attr != outer_attrs.end ())
720+
{
721+
asm_name
722+
= Analysis::Attributes::extract_string_literal (*export_name_attr)
723+
.value (); // Checked within attribute checker
724+
backend_asm_name = asm_name;
725+
should_mangle = false;
726+
}
727+
707728
unsigned int flags = 0;
708729
tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name,
709-
tl::nullopt /* asm_name */, flags, locus);
730+
backend_asm_name, flags, locus);
710731

711732
setup_fndecl (fndecl, is_main_fn, fntype->has_substitutions_defined (),
712733
visibility, qualifiers, outer_attrs);
713734
setup_abi_options (fndecl, get_abi (outer_attrs, qualifiers));
714735

715-
// conditionally mangle the function name
716-
bool should_mangle = should_mangle_item (fndecl);
736+
should_mangle &= should_mangle_item (fndecl);
717737
if (!is_main_fn && should_mangle)
718738
asm_name = ctx->mangle_item (fntype, canonical_path);
719739
SET_DECL_ASSEMBLER_NAME (fndecl,

gcc/rust/util/rust-attribute-values.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Attributes
3939
static constexpr auto &LINK_NAME = "link_name";
4040
static constexpr auto &LINK_SECTION = "link_section";
4141
static constexpr auto &NO_MANGLE = "no_mangle";
42+
static constexpr auto &EXPORT_NAME = "export_name";
4243
static constexpr auto &REPR = "repr";
4344
static constexpr auto &RUSTC_BUILTIN_MACRO = "rustc_builtin_macro";
4445
static constexpr auto &RUSTC_MACRO_TRANSPARENCY = "rustc_macro_transparency";

gcc/rust/util/rust-attributes.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static const BuiltinAttrDefinition __definitions[]
8181
{Attrs::LINK_NAME, CODE_GENERATION},
8282
{Attrs::LINK_SECTION, CODE_GENERATION},
8383
{Attrs::NO_MANGLE, CODE_GENERATION},
84+
{Attrs::EXPORT_NAME, CODE_GENERATION},
8485
{Attrs::REPR, CODE_GENERATION},
8586
{Attrs::RUSTC_BUILTIN_MACRO, EXPANSION},
8687
{Attrs::RUSTC_MACRO_TRANSPARENCY, EXPANSION},
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[export_name = "other_name"]
2+
fn func() {}
3+
// { dg-final { scan-assembler "other_name" } }
4+
5+
fn main() {
6+
func();
7+
}

0 commit comments

Comments
 (0)