From: Enes Cevik <[email protected]>

This patch introduces the `rustc_std_internal_symbol` attribute.
This attribute is required by the Rust standard library to prevent
name mangling for internal runtime symbols.

gcc/rust/ChangeLog:

        * backend/rust-compile-base.cc (should_mangle_item): Bypass
        mangling for rustc_std_internal_symbol.
        (HIRCompileBase::setup_fndecl): Dispatch the new attribute.
        (HIRCompileBase::handle_rustc_std_internal_symbol_attribute_on_fndecl):
        New function.
        * backend/rust-compile-base.h:
        (handle_rustc_std_internal_symbol_attribute_on_fndecl): New declaration.
        * util/rust-attribute-values.h (Attributes): Add
        RUSTC_STD_INTERNAL_SYMBOL constexpr.
        * util/rust-attributes.cc (__definitions): Register
        rustc_std_internal_symbol in the BuiltinAttributes list.
        * checks/errors/rust-builtin-attribute-checker.cc 
(rustc_std_internal_symbol):
        New function.
        (attribute_checking_handlers): Add rustc_std_internal_symbol function.

gcc/testsuite/ChangeLog:

        * rust/compile/rustc_std_internal_symbol1.rs: New test.
        * rust/compile/rustc_std_internal_symbol2.rs: New test.

Signed-off-by: Enes Cevik <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/ad4a6107284176c146e28edac131abeea566af5b

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4556

 gcc/rust/backend/rust-compile-base.cc         | 20 ++++++++-
 gcc/rust/backend/rust-compile-base.h          |  3 ++
 .../errors/rust-builtin-attribute-checker.cc  | 43 ++++++++++++-------
 gcc/rust/util/rust-attribute-values.h         |  2 +
 gcc/rust/util/rust-attributes.cc              |  1 +
 .../compile/rustc_std_internal_symbol1.rs     | 10 +++++
 .../compile/rustc_std_internal_symbol2.rs     |  7 +++
 7 files changed, 70 insertions(+), 16 deletions(-)
 create mode 100644 gcc/testsuite/rust/compile/rustc_std_internal_symbol1.rs
 create mode 100644 gcc/testsuite/rust/compile/rustc_std_internal_symbol2.rs

diff --git a/gcc/rust/backend/rust-compile-base.cc 
b/gcc/rust/backend/rust-compile-base.cc
index f566cc07b..1318b876e 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -51,7 +51,10 @@ bool inline should_mangle_item (const tree fndecl)
 {
   return lookup_attribute (Values::Attributes::NO_MANGLE,
                           DECL_ATTRIBUTES (fndecl))
-        == NULL_TREE;
+          == NULL_TREE
+        && lookup_attribute (Values::Attributes::RUSTC_STD_INTERNAL_SYMBOL,
+                             DECL_ATTRIBUTES (fndecl))
+             == NULL_TREE;
 }
 
 void
@@ -85,6 +88,8 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool 
is_main_entry_point,
       bool is_cold = attr_str == Values::Attributes::COLD;
       bool is_link_section = attr_str == Values::Attributes::LINK_SECTION;
       bool no_mangle = attr_str == Values::Attributes::NO_MANGLE;
+      bool is_std_internal
+       = attr_str == Values::Attributes::RUSTC_STD_INTERNAL_SYMBOL;
       bool is_deprecated = attr_str == Values::Attributes::DEPRECATED;
       bool is_proc_macro = attr_str == Values::Attributes::PROC_MACRO;
       bool is_proc_macro_attribute
@@ -116,6 +121,10 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool 
is_main_entry_point,
        {
          handle_no_mangle_attribute_on_fndecl (fndecl, attr);
        }
+      else if (is_std_internal)
+       {
+         handle_rustc_std_internal_symbol_attribute_on_fndecl (fndecl, attr);
+       }
       else if (is_proc_macro)
        {
          handle_bang_proc_macro_attribute_on_fndecl (fndecl, attr);
@@ -280,6 +289,15 @@ HIRCompileBase::handle_no_mangle_attribute_on_fndecl (
                 DECL_ATTRIBUTES (fndecl));
 }
 
+void
+HIRCompileBase::handle_rustc_std_internal_symbol_attribute_on_fndecl (
+  tree fndecl, const AST::Attribute &attr)
+{
+  DECL_ATTRIBUTES (fndecl)
+    = tree_cons (get_identifier 
(Values::Attributes::RUSTC_STD_INTERNAL_SYMBOL),
+                NULL_TREE, DECL_ATTRIBUTES (fndecl));
+}
+
 void
 HIRCompileBase::handle_deprecated_attribute_on_fndecl (
   tree fndecl, const AST::Attribute &attr)
diff --git a/gcc/rust/backend/rust-compile-base.h 
b/gcc/rust/backend/rust-compile-base.h
index 4a98eb46f..551a02e4d 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -152,6 +152,9 @@ protected:
   static void handle_no_mangle_attribute_on_fndecl (tree fndecl,
                                                    const AST::Attribute &attr);
 
+  static void handle_rustc_std_internal_symbol_attribute_on_fndecl (
+    tree fndecl, const AST::Attribute &attr);
+
   static void setup_abi_options (tree fndecl, ABI abi);
 
   static tree indirect_expression (tree expr, location_t locus);
diff --git a/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc 
b/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc
index 9078a1016..46b96f23a 100644
--- a/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc
+++ b/gcc/rust/checks/errors/rust-builtin-attribute-checker.cc
@@ -358,25 +358,38 @@ no_mangle (const AST::Attribute &attribute)
                   "must be of the form: %<#[no_mangle]%>");
     }
 }
+void
+rustc_std_internal_symbol (const AST::Attribute &attribute)
+{
+  if (attribute.has_attr_input ())
+    {
+      rust_error_at (attribute.get_locus (),
+                    "malformed %<rustc_std_internal_symbol%> attribute input");
+      rust_inform (attribute.get_locus (),
+                  "must be of the form: %<#[rustc_std_internal_symbol]%>");
+    }
+}
 
 } // namespace handlers
 
 const std::unordered_map<std::string, std::function<void (AST::Attribute &)>>
-  attribute_checking_handlers
-  = {{Attrs::DOC, handlers::doc},
-     {Attrs::DEPRECATED, handlers::deprecated},
-     {Attrs::LINK_SECTION, handlers::link_section},
-     {Attrs::EXPORT_NAME, handlers::export_name},
-     {Attrs::NO_MANGLE, handlers::no_mangle},
-     {Attrs::ALLOW, handlers::lint},
-     {Attrs::DENY, handlers::lint},
-     {Attrs::WARN, handlers::lint},
-     {Attrs::FORBID, handlers::lint},
-     {Attrs::LINK_NAME, handlers::link_name},
-     {Attrs::PROC_MACRO_DERIVE, handlers::proc_macro_derive},
-     {Attrs::PROC_MACRO, handlers::proc_macro},
-     {Attrs::PROC_MACRO_ATTRIBUTE, handlers::proc_macro},
-     {Attrs::TARGET_FEATURE, handlers::target_feature}};
+  attribute_checking_handlers = {
+    {Attrs::DOC, handlers::doc},
+    {Attrs::DEPRECATED, handlers::deprecated},
+    {Attrs::LINK_SECTION, handlers::link_section},
+    {Attrs::EXPORT_NAME, handlers::export_name},
+    {Attrs::NO_MANGLE, handlers::no_mangle},
+    {Attrs::ALLOW, handlers::lint},
+    {Attrs::DENY, handlers::lint},
+    {Attrs::WARN, handlers::lint},
+    {Attrs::FORBID, handlers::lint},
+    {Attrs::LINK_NAME, handlers::link_name},
+    {Attrs::PROC_MACRO_DERIVE, handlers::proc_macro_derive},
+    {Attrs::PROC_MACRO, handlers::proc_macro},
+    {Attrs::PROC_MACRO_ATTRIBUTE, handlers::proc_macro},
+    {Attrs::TARGET_FEATURE, handlers::target_feature},
+    {Attrs::RUSTC_STD_INTERNAL_SYMBOL, handlers::rustc_std_internal_symbol},
+};
 
 tl::optional<std::function<void (AST::Attribute &)>>
 lookup_handler (std::string attr_name)
diff --git a/gcc/rust/util/rust-attribute-values.h 
b/gcc/rust/util/rust-attribute-values.h
index 332d65e80..02d915f1a 100644
--- a/gcc/rust/util/rust-attribute-values.h
+++ b/gcc/rust/util/rust-attribute-values.h
@@ -44,6 +44,8 @@ public:
   static constexpr auto &NO_STD = "no_std";
   static constexpr auto &LINK_SECTION = "link_section";
   static constexpr auto &NO_MANGLE = "no_mangle";
+  static constexpr auto &RUSTC_STD_INTERNAL_SYMBOL
+    = "rustc_std_internal_symbol";
   static constexpr auto &EXPORT_NAME = "export_name";
   static constexpr auto &REPR = "repr";
   static constexpr auto &RUSTC_BUILTIN_MACRO = "rustc_builtin_macro";
diff --git a/gcc/rust/util/rust-attributes.cc b/gcc/rust/util/rust-attributes.cc
index ad57f5695..7042f8bd1 100644
--- a/gcc/rust/util/rust-attributes.cc
+++ b/gcc/rust/util/rust-attributes.cc
@@ -44,6 +44,7 @@ static const BuiltinAttrDefinition __definitions[]
      {Attrs::LINK_NAME, CODE_GENERATION},
      {Attrs::LINK_SECTION, CODE_GENERATION},
      {Attrs::NO_MANGLE, CODE_GENERATION},
+     {Attrs::RUSTC_STD_INTERNAL_SYMBOL, CODE_GENERATION},
      {Attrs::EXPORT_NAME, CODE_GENERATION},
      {Attrs::REPR, CODE_GENERATION},
      {Attrs::RUSTC_BUILTIN_MACRO, EXPANSION},
diff --git a/gcc/testsuite/rust/compile/rustc_std_internal_symbol1.rs 
b/gcc/testsuite/rust/compile/rustc_std_internal_symbol1.rs
new file mode 100644
index 000000000..00ad2286a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/rustc_std_internal_symbol1.rs
@@ -0,0 +1,10 @@
+// { dg-additional-options "-fdump-tree-gimple" }
+#![feature(no_core, rustc_attrs)]
+#![no_core]
+
+#[rustc_std_internal_symbol]
+pub fn _foo() -> i32 {
+    0
+}
+
+// { dg-final { scan-tree-dump "_foo" "gimple" } }
diff --git a/gcc/testsuite/rust/compile/rustc_std_internal_symbol2.rs 
b/gcc/testsuite/rust/compile/rustc_std_internal_symbol2.rs
new file mode 100644
index 000000000..f9e61ea2a
--- /dev/null
+++ b/gcc/testsuite/rust/compile/rustc_std_internal_symbol2.rs
@@ -0,0 +1,7 @@
+#![feature(no_core, rustc_attrs)]
+#![no_core]
+
+#[rustc_std_internal_symbol(invalid_argument)] // { dg-error "malformed 
.rustc_std_internal_symbol. attribute input" }
+pub fn _bar() -> i32 {
+    0
+}
-- 
2.54.0

Reply via email to