Skip to content

Commit f562d73

Browse files
bors[bot]liushuyu
andauthored
Merge #1149
1149: backend: handle no_mangle attribute r=philberty a=liushuyu - handle the `no_mangle` attribute Co-authored-by: liushuyu <liushuyu011@gmail.com>
2 parents 1286acc + a969ab6 commit f562d73

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,21 @@
2626

2727
#include "fold-const.h"
2828
#include "stringpool.h"
29+
#include "attribs.h"
2930

3031
namespace Rust {
3132
namespace Compile {
3233

34+
bool inline should_mangle_item (const tree fndecl)
35+
{
36+
return lookup_attribute ("no_mangle", DECL_ATTRIBUTES (fndecl)) == NULL_TREE;
37+
}
38+
3339
void
34-
HIRCompileBase::setup_attributes_on_fndecl (
35-
tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility,
36-
const HIR::FunctionQualifiers &qualifiers, const AST::AttrVec &attrs)
40+
HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
41+
HIR::Visibility &visibility,
42+
const HIR::FunctionQualifiers &qualifiers,
43+
const AST::AttrVec &attrs)
3744
{
3845
// if its the main fn or pub visibility mark its as DECL_PUBLIC
3946
// please see https://github.com/Rust-GCC/gccrs/pull/137
@@ -58,6 +65,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
5865
bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
5966
bool is_link_section
6067
= attr.get_path ().as_string ().compare ("link_section") == 0;
68+
bool no_mangle = attr.get_path ().as_string ().compare ("no_mangle") == 0;
6169
if (is_inline)
6270
{
6371
handle_inline_attribute_on_fndecl (fndecl, attr);
@@ -74,6 +82,10 @@ HIRCompileBase::setup_attributes_on_fndecl (
7482
{
7583
handle_link_section_attribute_on_fndecl (fndecl, attr);
7684
}
85+
else if (no_mangle)
86+
{
87+
handle_no_mangle_attribute_on_fndecl (fndecl, attr);
88+
}
7789
}
7890
}
7991

@@ -120,6 +132,21 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (
120132
set_decl_section_name (fndecl, msg_str.c_str ());
121133
}
122134

135+
void
136+
HIRCompileBase::handle_no_mangle_attribute_on_fndecl (
137+
tree fndecl, const AST::Attribute &attr)
138+
{
139+
if (attr.has_attr_input ())
140+
{
141+
rust_error_at (attr.get_locus (),
142+
"attribute %<no_mangle%> does not accept any arguments");
143+
return;
144+
}
145+
146+
DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("no_mangle"), NULL_TREE,
147+
DECL_ATTRIBUTES (fndecl));
148+
}
149+
123150
void
124151
HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
125152
const AST::Attribute &attr)
@@ -396,16 +423,21 @@ HIRCompileBase::compile_function (
396423
// we don't mangle the main fn since we haven't implemented the main shim
397424
bool is_main_fn = fn_name.compare ("main") == 0;
398425
std::string asm_name = fn_name;
399-
if (!is_main_fn)
400-
asm_name = ctx->mangle_item (fntype, *canonical_path);
401426

402427
unsigned int flags = 0;
403428
tree fndecl = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name,
404-
asm_name, flags, locus);
405-
setup_attributes_on_fndecl (fndecl, is_main_fn, visibility, qualifiers,
406-
outer_attrs);
429+
"" /* asm_name */, flags, locus);
430+
setup_fndecl (fndecl, is_main_fn, visibility, qualifiers, outer_attrs);
407431
setup_abi_options (fndecl, fntype->get_abi ());
408432

433+
// conditionally mangle the function name
434+
bool should_mangle = should_mangle_item (fndecl);
435+
if (!is_main_fn && should_mangle)
436+
asm_name = ctx->mangle_item (fntype, *canonical_path);
437+
SET_DECL_ASSEMBLER_NAME (fndecl,
438+
get_identifier_with_length (asm_name.data (),
439+
asm_name.length ()));
440+
409441
// insert into the context
410442
ctx->insert_function_decl (fntype, fndecl);
411443

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class HIRCompileBase
7575
tree resolve_unsized_adjustment (Resolver::Adjustment &adjustment,
7676
tree expression, Location locus);
7777

78-
static void setup_attributes_on_fndecl (
79-
tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility,
80-
const HIR::FunctionQualifiers &qualifiers, const AST::AttrVec &attrs);
78+
static void setup_fndecl (tree fndecl, bool is_main_entry_point,
79+
HIR::Visibility &visibility,
80+
const HIR::FunctionQualifiers &qualifiers,
81+
const AST::AttrVec &attrs);
8182

8283
static void handle_inline_attribute_on_fndecl (tree fndecl,
8384
const AST::Attribute &attr);
@@ -92,6 +93,9 @@ class HIRCompileBase
9293
handle_link_section_attribute_on_fndecl (tree fndecl,
9394
const AST::Attribute &attr);
9495

96+
static void handle_no_mangle_attribute_on_fndecl (tree fndecl,
97+
const AST::Attribute &attr);
98+
9599
static void setup_abi_options (tree fndecl, ABI abi);
96100

97101
static tree address_expression (tree, Location);

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)