在 main 之前执行代码的方法, 也可用于共享库初始化
时间:2006-03-25 来源:ammer
1. Add to .init section, be called in .init before __do_global_ctors_aux
CODE:
int oinit(void)
{
__asm__ (".section .init \n"
"bl oinit \n\t" /* for arm, or "call oinit \n\t" for x86
".section .text"); /* you can also put this out of the function */
printf("In o init by insert to .init section");
}
2. Add to .ctors section, will be called by function __do_global_ctors_aux
CODE:
static void *__libinit __attribute__((section(".ctors"))) = libinit;
int libinit(void)
{
......
}
3. Use constructor attribute(alost added to .ctors section as method 2):
void func(void) __attribute__((constructor));
void func(void)
{
printf("In lib's Constructor.\n");
}
CODE:
int oinit(void)
{
__asm__ (".section .init \n"
"bl oinit \n\t" /* for arm, or "call oinit \n\t" for x86
".section .text"); /* you can also put this out of the function */
printf("In o init by insert to .init section");
}
2. Add to .ctors section, will be called by function __do_global_ctors_aux
CODE:
static void *__libinit __attribute__((section(".ctors"))) = libinit;
int libinit(void)
{
......
}
3. Use constructor attribute(alost added to .ctors section as method 2):
void func(void) __attribute__((constructor));
void func(void)
{
printf("In lib's Constructor.\n");
}
相关阅读 更多 +