1. Compile宏控制
位于include/linux/init.h
/* These are for everybody (although not all archs will actually
discard it in modules) */
#define __init __section(.init.text) __cold notrace
#define __initdata __section(.init.data)
#define __initconst __section(.init.rodata)
#define __exitdata __section(.exit.data)
#define __exit_call __used __section(.exitcall.exit)
·首先需要简要说明一下GCC的驱动程序:

#define __init __section(.init.text) __cold notrace
__init用于标记函数,放在.init.text section,标记为初始化的函数,表明该函数供在初始化期间使用。在模块装载之后,模块装载就会将初始化函数扔掉。这样可以将该函数占用的内存释放出来。__initdata用于标记数据
#define __cold __attribute__((__cold__))
__cold告诉编译器这个函数很可能不被执行到
#define notrace __attribute__((no_instrument_function))
notrace如同GCC的-finstrument-functions() 参数的作用是在程序中加入hook,让它在每次进入和退出函数的时候分别调用这个函数
#define __used __attribute__((__used__)) 意味着code必须发动附有该宏的函数
__exit修饰词标记函数,只在模块卸载时使用。如果模块被直接编进内核则该函数就不会被调用。如果内核编译时没有包含该模块,则此标记的函数将被简单地丢弃。
#define __ref __section(.ref.text) noinline
#define __refdata __section(.ref.data)
#define __refconst __section(.ref.rodata)
#define noinline __attribute__((noinline)) 阻止该函数被内联
#define __exit __section(.exit.text) __exitused __cold notrace
/* Used for HOTPLUG */
#define __devinit __section(.devinit.text) __cold notrace
#define __devinitdata __section(.devinit.data)
#define __devinitconst __section(.devinit.rodata)
#define __devexit __section(.devexit.text) __exitused __cold notrace
#define

本文详细介绍了Linux内核初始化过程中的关键宏定义,包括`__init`、`__exit`、`__cold`和`notrace`等,它们分别用于标记初始化函数和数据,确保在不同阶段有效利用内存。同时,文章讲解了初始化宏`__define_initcall`如何声明和放置initcall_t函数指针,并探讨了`.initcall.init`段在TCM中的作用,揭示了Linux内核启动时代码执行的流程。
最低0.47元/天 解锁文章
1437

被折叠的 条评论
为什么被折叠?



