记录

1. GNU C提供__attribute__ 机制, __attribute__ 可以设置函数属性(Function Attribute )、变量属性(Variable Attribute )和类型属性(Type Attribute )

2. __attribute__ 机制可应用于constructor,GNU文档说明如下:

  • The constructor attribute causes the function to be called automatically before execution enters main (). Similarly, the destructor attribute causes the function to be called automatically after main () completes or exit () is called. Functions with these attributes are useful for initializing data that is used implicitly during the execution of the program.
  • You may provide an optional integer priority to control the order in which constructor and destructor functions are run. A constructor with a smaller priority number runs before a constructor with a larger priority number; the opposite relationship holds for destructors.

3. 当库被加载/卸载时,动态加载程序(ld.so或somesuch)会检查这些段(constructor段 或 destructor段)是否存在,如果存在,则调用其中引用的函数

4. 静态链接时需要加上--whole-archive(否则会因为没有被显式调用而被编译器优化掉不进行编译),但这样对-ffunction-sections, -fdata-sections,-Wl,-gc-sections就不生效了,导致编译出来的可执行档不会减小很多

5 每起一个进程, 都会调用一次constructor; 每kill一个进程,也会相应地调用一次 destructor

posted @ 2019-11-12 10:09  YouCompleteMe  阅读(71)  评论(0)    收藏  举报