3 回答
TA贡献1840条经验 获得超5个赞
本页提供有关重大认识constructor和destructor属性的实施和内ELF内,让他们工作的章节。在消化了这里提供的信息之后,我编译了一些额外的信息,并(借用上面Michael Ambrus的部分示例)创建了一个示例来说明概念并帮助我学习。下面提供这些结果以及示例源。
如此线程中所述,constructor和destructor属性在目标文件的.ctors和.dtors部分中创建条目。您可以使用以下三种方式之一在任一部分中放置对函数的引用。(1)使用任何一个section属性; (2)constructor和destructor属性或(3)内联汇编调用(在Ambrus的答案中引用了链接)。
使用constructor和destructor属性允许您另外为构造函数/析构函数分配优先级,以在main()调用之前或返回之后控制其执行顺序。给定的优先级值越低,执行优先级越高(在main()之前的较高优先级之前执行的优先级较低 - 以及在main()之后的较高优先级之后执行)。您提供的优先级值必须大于100编译器保留0-100之间的优先级值才能实现。A constructor或destructor具有优先级的指定在没有优先级的情况下执行constructor或destructor指定。
随着“部分的”属性或者与内联汇编,你也可以将在函数引用.init和.finiELF代码段,将任何构造的任何析构函数之前和之后,分别执行。放置在该.init部分中的函数引用调用的任何函数将在函数引用之前执行(像往常一样)。
我试图在下面的例子中说明每一个:
#include <stdio.h>#include <stdlib.h>/* test function utilizing attribute 'section' ".ctors"/".dtors"
to create constuctors/destructors without assigned priority.
(provided by Michael Ambrus in earlier answer)
*/#define SECTION( S ) __attribute__ ((section ( S )))void test (void) {printf("\n\ttest() utilizing -- (.section .ctors/.dtors) w/o priority\n");}void (*funcptr1)(void) SECTION(".ctors") =test;void (*funcptr2)(void) SECTION(".ctors") =test;void (*funcptr3)(void) SECTION(".dtors") =test;/* functions constructX, destructX use attributes 'constructor' and
'destructor' to create prioritized entries in the .ctors, .dtors
ELF sections, respectively.
NOTE: priorities 0-100 are reserved
*/void construct1 () __attribute__ ((constructor (101)));void construct2 () __attribute__ ((constructor (102)));void destruct1 () __attribute__ ((destructor (101)));void destruct2 () __attribute__ ((destructor (102)));/* init_some_function() - called by elf_init()
*/int init_some_function () {
printf ("\n init_some_function() called by elf_init()\n");
return 1;}/* elf_init uses inline-assembly to place itself in the ELF .init section.
*/int elf_init (void){
__asm__ (".section .init \n call elf_init \n .section .text\n");
if(!init_some_function ())
{
exit (1);
}
printf ("\n elf_init() -- (.section .init)\n");
return 1;}/*
function definitions for constructX and destructX
*/void construct1 () {
printf ("\n construct1() constructor -- (.section .ctors) priority 101\n");}void construct2 () {
printf ("\n construct2() constructor -- (.section .ctors) priority 102\n");}void destruct1 () {
printf ("\n destruct1() destructor -- (.section .dtors) priority 101\n\n");}void destruct2 () {
printf ("\n destruct2() destructor -- (.section .dtors) priority 102\n");}/* main makes no function call to any of the functions declared above
*/intmain (int argc, char *argv[]) {
printf ("\n\t [ main body of program ]\n");
return 0;}输出:
init_some_function() called by elf_init() elf_init() -- (.section .init) construct1() constructor -- (.section .ctors) priority 101 construct2() constructor -- (.section .ctors) priority 102 test() utilizing -- (.section .ctors/.dtors) w/o priority test() utilizing -- (.section .ctors/.dtors) w/o priority [ main body of program ] test() utilizing -- (.section .ctors/.dtors) w/o priority destruct2() destructor -- (.section .dtors) priority 102 destruct1() destructor -- (.section .dtors) priority 101
该示例有助于巩固构造函数/析构函数的行为,希望它对其他人也有用。
- 3 回答
- 0 关注
- 1160 浏览
添加回答
举报
