https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122786
Bug ID: 122786
Summary: [modules] section attribute in interface modules are
not handled correctly
Product: gcc
Version: 15.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: loki at loki dot codes
Target Milestone: ---
Section attributes ( `[[gnu::section(".name")]]` and
`__attribute__((section(".name")))`) on template classes and their members are
read and streamed correctly in module interfaces, but their side-effects are
not re-applied when importing the module.
Section attributes normally have side-effects during instantiation that update
the symbol table (via set_decl_section_name()). This is not happening in the
module case. While the attributes are stored in the streamed DECL_ATTRIBUTES
and the information ends up in the BMI, during import and instantiation, the
side-effects are not applied.
This PoC patch fixes this issue, but as i am not very familiar with the gcc
codebase, it may have unwanted side effects or edge cases.
```
diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 2f19b2b485d..0dd405dd2ac 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -8960,6 +8960,20 @@ trees_in::decl_value ()
set_decl_tls_model (decl, model);
}
+ /* Apply section attribute side-effects. The section attribute is stored
+ in DECL_ATTRIBUTES and gets streamed as part of the decl, but we need
+ to apply set_decl_section_name to update the symtab. This is similar
+ to how TLS model needs explicit handling above. */
+ if (is_new && VAR_OR_FUNCTION_DECL_P (inner))
+ {
+ if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
+ {
+ tree section_name = TREE_VALUE (TREE_VALUE (attr));
+ if (TREE_CODE (section_name) == STRING_CST)
+ set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
+ }
+ }
+
if (!NAMESPACE_SCOPE_P (inner)
&& ((TREE_CODE (inner) == TYPE_DECL
&& !is_typedef
```