Skip to content

Commit a936265

Browse files
bors[bot]liushuyu
andauthored
Merge #1150
1150: backend: handle link_section attribute r=philberty a=liushuyu - handle the `link_section` attribute Co-authored-by: liushuyu <liushuyu011@gmail.com>
2 parents 4337aea + f619470 commit a936265

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ HIRCompileBase::setup_attributes_on_fndecl (
5656
bool is_must_use
5757
= attr.get_path ().as_string ().compare ("must_use") == 0;
5858
bool is_cold = attr.get_path ().as_string ().compare ("cold") == 0;
59+
bool is_link_section
60+
= attr.get_path ().as_string ().compare ("link_section") == 0;
5961
if (is_inline)
6062
{
6163
handle_inline_attribute_on_fndecl (fndecl, attr);
@@ -68,6 +70,10 @@ HIRCompileBase::setup_attributes_on_fndecl (
6870
{
6971
handle_cold_attribute_on_fndecl (fndecl, attr);
7072
}
73+
else if (is_link_section)
74+
{
75+
handle_link_section_attribute_on_fndecl (fndecl, attr);
76+
}
7177
}
7278
}
7379

@@ -89,6 +95,31 @@ HIRCompileBase::handle_cold_attribute_on_fndecl (tree fndecl,
8995
"attribute %<cold%> does not accept any arguments");
9096
}
9197

98+
void
99+
HIRCompileBase::handle_link_section_attribute_on_fndecl (
100+
tree fndecl, const AST::Attribute &attr)
101+
{
102+
if (!attr.has_attr_input ())
103+
{
104+
rust_error_at (attr.get_locus (),
105+
"%<link_section%> expects exactly one argment");
106+
return;
107+
}
108+
109+
rust_assert (attr.get_attr_input ().get_attr_input_type ()
110+
== AST::AttrInput::AttrInputType::LITERAL);
111+
112+
auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
113+
const auto &msg_str = literal.get_literal ().as_string ();
114+
115+
if (decl_section_name (fndecl))
116+
{
117+
rust_warning_at (attr.get_locus (), 0, "section name redefined");
118+
}
119+
120+
set_decl_section_name (fndecl, msg_str.c_str ());
121+
}
122+
92123
void
93124
HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
94125
const AST::Attribute &attr)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ class HIRCompileBase
8888
static void handle_must_use_attribute_on_fndecl (tree fndecl,
8989
const AST::Attribute &attr);
9090

91+
static void
92+
handle_link_section_attribute_on_fndecl (tree fndecl,
93+
const AST::Attribute &attr);
94+
9195
static void setup_abi_options (tree fndecl, ABI abi);
9296

9397
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
@@ -31,6 +31,7 @@ static const BuiltinAttrDefinition __definitions[] = {
3131
{"doc", HIR_LOWERING},
3232
{"must_use", STATIC_ANALYSIS},
3333
{"lang", HIR_LOWERING},
34+
{"link_section", CODE_GENERATION},
3435
};
3536

3637
BuiltinAttributeMappings *
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[link_section = ".universe"]
2+
fn not_in_text() -> i32 {
3+
42
4+
}
5+
6+
fn main() -> i32 {
7+
// { dg-do compile }
8+
// { dg-options "-gdwarf-5 -dA -w" }
9+
not_in_text();
10+
// { dg-final { scan-assembler ".universe" } } */
11+
12+
0
13+
}

0 commit comments

Comments
 (0)