- 论坛徽章:
- 0
|
本帖最后由 71v5 于 2014-06-26 15:13 编辑
[module_register_init]-初始化execsw数组:- /*********************************************************************
- * static moduledata_t elf32_mod = {
- "elf32", //name成员
- elf32_modevent,//evhand成员
- (void *)&elf32_execsw //priv成员
- };
- 参数描述:
- arg:&elf32_mod。
- *********************************/
- 109 void
- 110 module_register_init(const void *arg)
- 111 {
- 112 const moduledata_t *data = (const moduledata_t *)arg;
- 113 int error;
- 114 module_t mod;
- 115
- /**********************************************************************************
- * "9.2-可执行文件格式-向内核注册-阶段01"中已经创建了name为"elf32"的
- struct module对象,这里简记为elf32_module,并且已经对其成员进行了
- 初始化,也将其链接到了对应的链表上。
- 117-118:此时mod指向elf32_module。
- #define MOD_EVENT(mod, type) (mod)->handler((mod), (type), (mod)->arg)
- 123:这里的调用为elf32_modevent(mod,MOD_LOAD,&elf32_execsw);
- 宏EXEC_SET展开过程定义了函数elf32_modevent,一般情况下,该函数可以简化为
- 下面的函数调用:
- exec_register(&elf32_execsw);
- 124-132:elf32_modevent函数执行失败。
- 133-147:elf32_modevent执行成功,进行一些无关紧要的处理。
- **********************************************/
- 116 mtx_lock(&Giant);
- 117 MOD_SLOCK;
- 118 mod = module_lookupbyname(data->name);
- 119 if (mod == NULL)
- 120 panic("module_register_init: module named %s not found\n",
- 121 data->name);
- 122 MOD_SUNLOCK;
- 123 error = MOD_EVENT(mod, MOD_LOAD);
- 124 if (error) {
- 125 MOD_EVENT(mod, MOD_UNLOAD);
- 126 MOD_XLOCK;
- 127 module_release(mod);
- 128 MOD_XUNLOCK;
- 129 printf("module_register_init: MOD_LOAD (%s, %p, %p) error"
- 130 " %d\n", data->name, (void *)data->evhand, data->priv,
- 131 error);
- 132 } else {
- 133 MOD_XLOCK;
- 134 if (mod->file) {
- 135 /*
- 136 * Once a module is succesfully loaded, move
- 137 * it to the head of the module list for this
- 138 * linker file. This resorts the list so that
- 139 * when the kernel linker iterates over the
- 140 * modules to unload them, it will unload them
- 141 * in the reverse order they were loaded.
- 142 */
- 143 TAILQ_REMOVE(&mod->file->modules, mod, flink);
- 144 TAILQ_INSERT_HEAD(&mod->file->modules, mod, flink);
- 145 }
- 146 MOD_XUNLOCK;
- 147 }
- 148 mtx_unlock(&Giant);
- 149 }
复制代码 [exec_register函数]:- /*****************************************************************************************
- * 该函数主要初始化非常重要的指针数组execsw,execve系统调用会使用该数组。
- static const struct execsw **execsw
- 函数exec_register没有社会么特别重要的操作,就是初始化数组execsw,这里不做过多的分析。
- 这里是以elf可执行文件格式来分析其初始化过程的,其它可执行文件格式类似,当全部都完成
- 了初始化后,非常重要的数组execsw被建立,这里数组元素的顺序不是很清楚,但是不影响说明
- 问题,就假设为下面的顺序:
- execsw[0] = &linux_execsw;
- execsw[1] = &coff_execsw;
- execsw[2] = &gzip_execsw;
- execsw[3] = &aout_execsw;
- execsw[4] = &shell_execsw;
- execsw[5] = &svr4_execsw;
- execsw[6] = &elf32_execsw;
- ******************************************/
-
- 1458 /*
- 1459 * Exec handler registration
- 1460 */
- 1461 int
- 1462 exec_register(execsw_arg)
- 1463 const struct execsw *execsw_arg;
- 1464 {
- 1465 const struct execsw **es, **xs, **newexecsw;
- 1466 int count = 2; /* New slot and trailing NULL */
- 1467
- 1468 if (execsw)
- 1469 for (es = execsw; *es; es++)
- 1470 count++;
- 1471 newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
- 1472 if (newexecsw == NULL)
- 1473 return (ENOMEM);
- 1474 xs = newexecsw;
- 1475 if (execsw)
- 1476 for (es = execsw; *es; es++)
- 1477 *xs++ = *es;
- 1478 *xs++ = execsw_arg;
- 1479 *xs = NULL;
- 1480 if (execsw)
- 1481 free(execsw, M_TEMP);
- 1482 execsw = newexecsw;
- 1483 return (0);
- 1484 }
复制代码 |
|