一、实验内容
Linux内核如何装载和启动一个可执行程序
1、理解编译链接的过程,和 ELF 可执行文件格式,详细内容参考本周第一节;
2、编程使用 exec*库函数加载一个可执行文件,动态链接分为可执行程序装载时动态链接和运行时动态链接,编程练习动态链接库的这两种使用方式,详细内容参考本周第二节;
3、使用 gdb 跟踪分析一个 execve 系统调用内核处理函数 sys_execve ,验证您对 Linux 系统加载可执行程序所需处理过程的理解,详细内容参考本周第三节;推荐在实验楼 Linux 虚拟机环境下完成实验;
4、特别关注新的可执行程序是从哪里开始执行的?为什么 execve 系统调用返回后新的可执行程序能顺利执行?对于静态链接的可执行程序和动态链接的可执行程序 execve 系统调用返回时会有什么不同?
二、实验过程
1、编译链接的过程和 ELF 可执行文件格式
A、 以函数hello.c为例,编译链接的过程如下:
函数hello.c函数:
#include<stdio.h>
int main()
{printf("Hello,World!");
return 0;
}
编译链接命令:
gcc -E hello.c -o hello.i//预处理
gcc -S hello.i -o hello.s//编译
gcc -c hello.s -o hello.o//汇编
gcc hello.o -o hello//链接
执行过程如下所示:
上图中的命令“gcc -o hello.static hello.o -static”
静态编译出来的hello.static把C库里需要的东西也放到可执行文件里了。使用命令ls –l后
,可以看到hello只有8K,hello.static有大概800K。
B、ELF文件
a、 ELF文件的头部:使用命令readelf -h hello查看hello文件的头。ELF的头保存的是元数据,也就是路线图,描述了文件的组织情况。比如程序头表(program header table)告诉系统如何来创建一个进程的内存映像。section头表(section header table)包含描述文件sections的信息。每个section在这个表中有一个入口;每个入口给出了该section的名字,大小等等信息。ELF的剩余部分是sections,包括代码段,数据段。这些在程序变成进程映像时加载到内存的虚拟地址空间中,从ELF头开始加载。
b、ELF可执行文件中有三种主要的目标文件:
一个可重定位文件保存着代码和适当的数据,用来和其他的object文件一起创建一个可执行文件或者是一个共享文件。主要是.o文件。
一个可执行文件保存着一个用来执行的程勋;该文件指出了exec如何创建程序进程映像。
一个共享object文件保存着代码和合适地数据,用来被下面的两个链接器链接。第一个是链接编辑器,可以和其他的可重定位的共享object文件来创建其他的object。第二个是动态链接器,联合一个可执行文件和其他的共享object文件来创建一个进程映像。
2、动态链接
在编译时,不直接复制可执行代码,而是通过一系列符号和参数,在程序运行或加载时将这些信息传递给操作系统。操作系统将负责把需要的动态库加载到内存中,然后程序在运行到指定的代码时,去共享执行内存中已经加载的动态库去执行代码,最终达到运行时链接的目的。优点是多个程序可以共享同一段代码,而不需要在磁盘上存储多个复制。缺点是运行时加载,可能会影响程序的前期执行性能,而且对使用的库依赖性较高。
A、可执行程序装载时动态链接
shlibexample.h中定义了SharedLibApi()函数,shlibexample.c是对此函数的实现。用“gcc -shared shlibexample.c -o libshlibexample.so -m32”(在64位环境下执行时加上-32)生成.so文件。这样就生成了共享库文件。
shlibexample.h代码如下所示:
#ifndef _SH_LIB_EXAMPLE_H_
#define _SH_LIB_EXAMPLE_H_
#define SUCCESS 0
#define FAILURE (-1)
#ifdef __cplusplus {
extern "C"
#endif
int SharedLibApi();
#ifndef __cplusplus
}
#endif
#endif /* _SH_LIB_EXAMPLE_H_ */
shlibexample.c代码如下所示:
#include<stdio.h>
#include"shlibexample.h"
int SharedLibApi()
{
printf("This is a shared libary!\n");
return SUCCESS;
}
B、运行时动态链接
dllibexample.h定义了DynamicalLoadingLibApi()函数,dllibexample.c是对此函数的实现。同样使用命令"gcc -shared dllibexample.c -o libdllibexample.so
得到动态加载共享库。
dllibexample.h代码如下所示:
#ifndef _DL_LIB_EXAMPLE_H_
#define _DL_LIB_EXAMPLE_H_
#ifdef __cplusplus
extern "C" {
#endif
int DynamicalLoadingLibApi();
#ifndef __cplusplus
}
#endif
#endif /* _DL_LIB_EXAMPLE_H_ */
dllibexample.c代码如下所示:
#include<stdio.h>
#include"dllibexample.h"
#define SUCCESS 0
#define FAILURE (-1)
int DynamicalLoadingLibApi()
{
printf("This is a Dynamical Loading libary!\n");
return SUCCESS;
}
动态链接实例:
创建main函数,使其使用两种动态链接库。这里需要注意的是main函数中只include了shlibexample(共享库),没有include dllibexample(动态加载共享库),但是include了dlfcn。原因是前面加了共享库的接口文件,则可以直接调用共享库。但是如果要调用动态加载共享库,就要使用定义在dlfcn.h中的dlopen。
main函数代码如下:
#include <stdio.h>
#include "shlibexample.h"
#include <dlfcn.h>
int main()
{
printf("This is a Main program!\n");
/* Use Shared Lib */
printf("Calling SharedLibApi() function of libshlibexample.so!\n");
SharedLibApi(); //直接调用共享库
/* Use Dynamical Loading Lib */
void * handle = dlopen("libdllibexample.so",RTLD_NOW);//打开动态库并将其加载到内存
if(handle == NULL)
{
printf("Open Lib libdllibexample.so Error:%s\n",dlerror());
return FAILURE;
}
int (*func)(void);
char * error;
func = dlsym(handle,"DynamicalLoadingLibApi");
if((error = dlerror()) != NULL)
{
printf("DynamicalLoadingLibApi not found:%s\n",error);
return FAILURE;
}
printf("Calling DynamicalLoadingLibApi() function of libdllibexample.so!\n");
func();
dlclose(handle); //卸载库
return SUCCESS;
}
执行结果如下图所示:
三、代码分析
Linux提供了exec、execlp、execle、execv、execvp、execve等六个用以执行一个可执行文件的函数,统称为exec函数,差异在于对命令行参数和环境变量参数的传递方式不同。以上函数的本质都是调用系统调用sys_execve()来执行一个可执行文件。
整体的调用关系从前到后为:
sys_execve、do_execve、do_execve_common、exec_binprm、search_binary_handler、load_elf_binary、start_thread
可执行程序执行到execve的时候陷入到内核态,当系统调用从内核态返回到用户态时,eip直接跳转到ELF程序的入口地址,CPU也得到新的用户态堆栈(包含新程序的命令行参数和shell上下文环境)。这样,新程序就开始执行了。
当执行execve时,用execve的加载的可执行文件把当前进程的可执行程序给覆盖掉,execve的系统调用会返回新的可执行程序的起点——main函数。shell环境会执行execve,把命令行参数和环境变量都加载进来,当系统调用陷入到内核里面的时候,system call调用sys_execve。sys_execve函数分析如下:
//sys_execve
SYSCALL_DEFINE3(execve,
const char __user *, filename, //可执行程序的名称
const char __user *const __user *, argv, //程序的参数
const char __user *const __user *, envp) //环境变量
{
return do_execve(getname(filename), argv, envp);
}
//do_execve
int do_execve(struct filename *filename,
const char __user *const __user *__argv,
const char __user *const __user *__envp)
{
struct user_arg_ptr argv = { .ptr.native = __argv };
struct user_arg_ptr envp = { .ptr.native = __envp };
return do_execve_common(filename, argv, envp);
}
sys_execve函数中调用了do_execve_common,分析如下(代码中注释):
static int do_execve_common(struct filename *filename,
struct user_arg_ptr argv,
struct user_arg_ptr envp)
{
struct linux_binprm *bprm;
struct file *file;
struct files_struct *displaced;
int retval;
...
bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);//在堆上分配一个linux_binprm结构体
...
file = do_open_exec(filename);//打开需要加载的可执行文件,file中就包含了打开的可执行文件的信息
...
bprm->file = file; //赋值file指针
bprm->filename = bprm->interp = filename->name;//赋值文件名
retval = bprm_mm_init(bprm); //创建进程的内存地址空间
...
bprm->argc = count(argv, MAX_ARG_STRINGS);//赋值参数个数
...
bprm->envc = count(envp, MAX_ARG_STRINGS);//赋值环境变量个数
...
retval = copy_strings_kernel(1, &bprm->filename, bprm); //从内核空间获取文件路径;
...
bprm->exec = bprm->p; //p为当前内存页最高地址
retval = copy_strings(bprm->envc, envp, bprm);//把环境变量拷贝到bprm中
...
retval = copy_strings(bprm->argc, argv, bprm);//把命令行参数拷贝到bprm中
...
retval = exec_binprm(bprm);//处理可执行文件
...
return retval;
}
linux_binprm结构体保存要执行文件的相关信息, 如:文件的头128字节、文件名、命令行参数、环境变量、文件路径、内存描述符信息等。
exec_binprm函数保存当前的pid,其中ret = search_binary_handler(bprm);
调用 search_binary_handler 寻找可执行文件的相应处理函数。
int search_binary_handler(struct linux_binprm *bprm)
{
bool need_retry = IS_ENABLED(CONFIG_MODULES);
struct linux_binfmt *fmt;
int retval;
...
read_lock(&binfmt_lock);
list_for_each_entry(fmt, &formats, lh) { //遍历文件解析链表
if (!try_module_get(fmt->module))
continue;
read_unlock(&binfmt_lock);
bprm->recursion_depth++;
//解析elf格式执行的位置
retval = fmt->load_binary(bprm);// 加载可执行文件的处理函数
read_lock(&binfmt_lock);
...
}
return retval;
linux_binfmt结构体定义了一些函数指针,不同的Linux可接受的目标文件格式(如load_binary,load_shlib,core_dump)采用不同的函数来进行目标文件的装载。每一个linux_binfmt结构体对应一种二进制程序处理方法。这些结构体实例会通过init_elf_binfmt
以注册的方式加入到内核对应的format链表中去,通过register_binfmt()
和unregister_binfmt()
在链表中插入和删除对象。
struct linux_binfmt {
struct list_head lh;
struct module *module;
int (*load_binary)(struct linux_binprm *);//用于加载一个新的进程(通过读取可执行文件中的信息)
int (*load_shlib)(struct file *); //用于动态加载共享库
int (*core_dump)(struct coredump_params *cprm);//在core文件中保存当前进程的上下文
unsigned long min_coredump;
};
目标文件的格式是ELF,所以retval = fmt->load_binary(bprm);
中load_binary实际上调用load_elf_binary完成ELF二进制映像的认领、装入和启动。load_elf_binary这个函数指针被包含在一个名为elf_format的结构体中:
static structlinux_binfmt elf_format = {
.module =THIS_MODULE,
.load_binary = load_elf_binary, //函数指针
.load_shlib = load_elf_library,
.core_dump = elf_core_dump,
.min_coredump = ELF_EXEC_PAGESIZE,
};
全局变量elf_format赋给了一个指针,在init_elf_binfmt里把变量注册注册到文件解析链表中,就可以在链表里找到相应的文件格式。继续分析load_elf_binary:
static int load_elf_binary(struct linux_binprm *bprm)
{
...
if (elf_interpreter) { // 动态链接的处理
...
} else { // 静态链接的处理
elf_entry =loc->elf_ex.e_entry;
...
}
}
...
//将ELF文件映射到进程空间中,execve系统调用返回用户态后进程就拥有了新的代码段、数据段。
current->mm->end_code = end_code;
current->mm->start_code =start_code;
current->mm->start_data =start_data;
current->mm->end_data = end_data;
current->mm->start_stack =bprm->p;
...
start_thread(regs, elf_entry, bprm->p);
}
ELF文件中的Entry point address字段指明了程序入口地址,这个地址一般是0x8048000(0x8048000以上的是内核段内存)。该入口地址被解析后存放在elf_ex.e_entry中,所以静态链接程序的起始位置就是elf_entry。 这个函数中还有一个关键点start_thread,regs中为系统调用时SAVE_ALL宏压入内核栈的部分。new_ip的值等于参数elf_entry的值,即把ELF文件中定义的main函数起始地址赋值给eip寄存器,进程返回到用户态时的执行位置从原来的int 0x80的下一条指令变成了new_ip的位置。start_thread如下所示:
start_thread(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp)
{
set_user_gs(regs, 0);
regs->fs = 0;
regs->ds = __USER_DS;
regs->es = __USER_DS;
regs->ss = __USER_DS;
regs->cs = __USER_CS;
regs->ip = new_ip;
regs->sp = new_sp;
regs->flags = X86_EFLAGS_IF;
set_thread_flag(TIF_NOTIFY_RESUME);
}
四、静态链接可执行文件的调试
用test_exe.c覆盖test.c,增加了一句MenuConfig()执行一个程序。
代码如下所示:
int Exec(int argc, char *argv[])
{
int pid;
/* fork another process */
pid = fork();
if (pid < 0)
{
/* error occurred */
fprintf(stderr,"Fork Failed!");
exit(-1);
}
else if (pid == 0)
{
/* child process */
printf("This is Child Process!\n");
execlp("/hello","hello",NULL);
}
else
{
/* parent process */
printf("This is Parent Process!\n");
/* parent will wait for the child to complete*/
wait(NULL);
printf("Child Complete!\n");
}
}
运行makefile,makefile做了一些修改,编译了hello.c,在生成根文件系统的时候,把init和hello都放到rootfs.img内。这样在执行execve时就自动的加载hello可执行文件,运行结果如下所示:
设置断点分析,在之前分析的关键点处设置断点,一句一句向下跟踪,并且对照执行过程。追踪到start_thread
,用po new_ip
,得到的是0x804887f。
在命令框输入“readelf –h hello
”命令,可以看到这个可执行程序它的入口点地址也是0x804887f,结果如下图所示: