Linux内核研究系列之可执行文件格式我们知道linux环境下不是所有的二进制文件都有相同的格式,linux系统使用二进制文件的处理程序来实现对不同二进制格式文件的分别处理。二进制处理程序通过内嵌在文件开头的“特征序列”(一个特殊的字节序列)来识别文件,有时也会通过文件名的一些特征,例如ELF文件以’E’’L’’F’字符开头,java文件以0xcafebabe开始前四个字节。 Linux用sys_execve装入可执行二进制文件。 1.当前linux版本(2.2)提供以下几种二进制文件处理程序:
相关内容见 相关内容在 相关内容 相关内容见 详细参见 详细参见 在继续向下介绍之前,我们必须先来认识一个数据结构linux_binfmt; struct linux_binfmt{struct linux_binfmt * next; long *use_count; int (*load_binary)(struct linux_binprm *,struct pt_regs *regs); int (*load_shlib)(int fd); int (*core_dump)(long signr,struct pt_regs *regs); }; 在linux_binfmt中包含两个重要指向函数的指针,load_binary装入可执行代码,load_shlib装入共享库。Core_dump是个转储函数指针。 很显然,由next构成一个链表,表头则由formats指向,如下图: static struct linux_binfmt *formats=(struct linux_binfmt *)NULL;系统为每个不同的文件格式定义了一个相应的对象: static struct linux_binfmt elf_format;static struct linux_binfmt java_format; static struct linux_binfmt em86_format; …… linux_binfmt的链表(由formats指向)则就是由这些不同的文件格式的 linux_binfmt构成的一个链表。而对不同格式二进制文件的处理程序则通过注册在相应的linux_binfmt中的函数来实行,实际这里已经使用了面向对象的思想,而这种情况在linux的内核源码中经常可以看到。 例如对于ELF文件格式: 当使用“装入函数的指针”使,指针指向装入函数分别为: load_elf_binary(struct linux_binprm *bprm,struct pt_regs *regs);static int load_elf_library(int fd); 以上函数在 而相应的elf_format 被定义成: static struct linux_bmfmt elf_format={#ifndef MODULE NULL,NULL,load_elf_binary,load_elf_library,elf_core_dump #else NULL,&mod_use_count,load_elf_binary,load_elf_library, elf_core_dump() #endif }; 以此为参考我们自然也就可以想到其他文件格式的定义形式。 『注』Java的load函数为load_java,em86的load函数为load_em86,script 的load函数为load_script,他们没有提供library的load函数。 2.下面我们来看看关于这几个数据结构的主要操作:
extern int register_binfmt(struct linux_binfmt *); extern int unregister_binfmt(struct linux_binfmt *); extern int init_elf_binfmt(void); extern int init_aout_binfmt(void); extern int init_script_binfmt(void); extern int init_java_binfmt(void); extern int init_em86_binfmt(void); extern int init_misc_binfmt(void); 以上函数在 init函数的作用很简单,只是调用register_binfmt函数将自己的文件格式加入formats链表。 do_load_elf_binary,do_load_elf_library,do_load_java…… extern int search_binary_handler(struct linux_binprm*,struct pt_regs *); 这两个函数主要就涉及到另一个数据结构linux_binprm,她吗那就下次再说吧,到这里大家应该对linux下几种二进制文件处理程序有个大体的印象了,具体细节去看看源代码吧。 好了,这次就到这里了,该休息了。 上一篇:轻轻松松产生 Makefile 下一篇:Linux下C编程介绍 更多相关文章
|
推荐文章
精彩文章
|