当我编译
__attribute__((section(".text"))) const int x = 42;
int main(){ return x; }
Run Code Online (Sandbox Code Playgroud)
使用 gcc(适用于 tinycc 和 clang),我得到
Warning: ignoring changed section attributes for .text
.
警告的原因是什么,如何在仍然保留(始终只读)数据的同时消除它.text
?
显然:
__attribute__((section(".text#"))) const int x = 42;
Run Code Online (Sandbox Code Playgroud)
参考:https ://gcc.gnu.org/ml/gcc-help/2010-09/msg00088.html,其中回答者解释:
__attribute__ ((section(".text")))
将使 gcc,"aw",@progbits
在.text
更改部分属性之后发出 。如果您使用:(__attribute__ ((section(".text#")))
注意额外的'#'
)这个后缀将在程序集中注释并且警告将消失
在那种情况下,相关变量不是const
,因此对同一问题尤其不明智(如另一位响应者所述)。在您的情况下,它是 a const
,因此访问"a"
而不是"aw"
- 然而仍然可能是不明智的。