Skip to content

Commit af7622f

Browse files
committed
backend: handle no_mangle attribute
1 parent 1286acc commit af7622f

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@
3030
namespace Rust {
3131
namespace Compile {
3232

33+
bool
34+
should_mangle_item (const AST::AttrVec &attrs)
35+
{
36+
for (const auto &attr : attrs)
37+
{
38+
if (attr.get_path ().as_string ().compare ("no_mangle") == 0)
39+
{
40+
if (attr.has_attr_input ())
41+
rust_error_at (
42+
attr.get_locus (),
43+
"attribute %<no_mangle%> does not accept any arguments");
44+
return false;
45+
}
46+
}
47+
48+
return true;
49+
}
50+
3351
void
3452
HIRCompileBase::setup_attributes_on_fndecl (
3553
tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility,
@@ -58,6 +76,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
5876
bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
5977
bool is_link_section
6078
= attr.get_path ().as_string ().compare ("link_section") == 0;
79+
bool no_mangle = attr.get_path ().as_string ().compare ("no_mangle") == 0;
6180
if (is_inline)
6281
{
6382
handle_inline_attribute_on_fndecl (fndecl, attr);
@@ -74,6 +93,11 @@ HIRCompileBase::setup_attributes_on_fndecl (
7493
{
7594
handle_link_section_attribute_on_fndecl (fndecl, attr);
7695
}
96+
else if (no_mangle)
97+
{
98+
// we handled this in `should_mangle_item`
99+
continue;
100+
}
77101
}
78102
}
79103

@@ -396,7 +420,10 @@ HIRCompileBase::compile_function (
396420
// we don't mangle the main fn since we haven't implemented the main shim
397421
bool is_main_fn = fn_name.compare ("main") == 0;
398422
std::string asm_name = fn_name;
399-
if (!is_main_fn)
423+
// TODO(liushuyu): we should probably move this part to
424+
// `setup_attributes_on_fndecl` if possible
425+
bool should_mangle = should_mangle_item (outer_attrs);
426+
if (!is_main_fn && should_mangle)
400427
asm_name = ctx->mangle_item (fntype, *canonical_path);
401428

402429
unsigned int flags = 0;

gcc/rust/util/rust-attributes.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static const BuiltinAttrDefinition __definitions[] = {
3232
{"must_use", STATIC_ANALYSIS},
3333
{"lang", HIR_LOWERING},
3434
{"link_section", CODE_GENERATION},
35+
{"no_mangle", CODE_GENERATION},
3536
};
3637

3738
BuiltinAttributeMappings *

gcc/testsuite/rust/debug/no_mangle.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#[no_mangle]
2+
fn do_not_mangle() -> i32 {
3+
0
4+
}
5+
6+
fn please_mangle() {}
7+
8+
fn main() {
9+
// { dg-do compile }
10+
// { dg-options "-gdwarf-5 -dA" }
11+
let _ = do_not_mangle();
12+
please_mangle();
13+
// look for unmangled function name:
14+
// { dg-final { scan-assembler "do_not_mangle:" } } */
15+
// look for legacy mangled function name:
16+
// { dg-final { scan-assembler "13please_mangle" } } */
17+
}

0 commit comments

Comments
 (0)