GCC Attribute
Table of Contents
1. GCC Attribute
1.1. alias
1.2. constructor
1.4. may_alias
1.5. naked
1.6. noreturn
1.7. optimize
void foo() __attribute__((optimize(1))) {}
1.8. section
1.9. tls_model
1.10. visibility
__attribute__ ((visibility ("default"))) void f1 () {} __attribute__ ((visibility ("hidden"))) void f2 () {} __attribute__ ((visibility ("protected"))) void f2 () {} __attribute__ ((visibility ("hidden"))) int f3 = 0;
$> gcc -shared test.c $> readelf -a ./a.out 0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_deregisterTMCloneTab 2: 0000000000000000 0 NOTYPE WEAK DEFAULT UND __gmon_start__ 3: 0000000000000000 0 NOTYPE WEAK DEFAULT UND _ITM_registerTMCloneTable 4: 0000000000000000 0 FUNC WEAK DEFAULT UND __cxa_finalize@GLIBC_2.2.5 (2) 5: 0000000000201020 0 NOTYPE GLOBAL DEFAULT 20 _edata 6: 0000000000201028 0 NOTYPE GLOBAL DEFAULT 21 _end 7: 00000000000005ba 7 FUNC GLOBAL DEFAULT 11 f1 <-------------- 只有 f1 在 dynsym 中 8: 0000000000201020 0 NOTYPE GLOBAL DEFAULT 21 __bss_start 9: 00000000000004a8 0 FUNC GLOBAL DEFAULT 8 _init 10: 00000000000005c8 0 FUNC GLOBAL DEFAULT 12 _fini
通过 Version Script, 也可以控制符号的 visibility. 另外, linkage 也会影响符号的 visibility, 例如 inline, static 等关键字
其中 `protected` visibility 功能与 DT_SYMBOLIC 类似