首页
社区
课程
招聘
Linux内核加载ELF文件源码分析
发表于: 2023-4-25 20:52 7471

Linux内核加载ELF文件源码分析

2023-4-25 20:52
7471

1)版本:V6.3-rc7,x86
2)elf文件加载源码:fs/binfmt_elf.c

Linux支持多种不同格式的可执行程序,这些可执行 程序的加载方式由linux\binfmts.h文件中的linux_binfmt结构体进行定义:

结构体定义了可执行程序的3中不同的加载模式:
图片描述
每一种系统支持的可执行文件都对应一个linux_binfmt对象,统一注册在一个链表中,通过register_binfmt和unregister_binfmt函数编辑链表。在执行可执行程序时,内核通过list_for_each_enrty遍历链表中注册的linux_binfmt对象,使用正确的加载方式进行加载。
elf文件的linux_binfmt对象结构如下,该结构体定义了elf文件由load_elf_binary函数加载:

程序首先读取了e_ident中的魔数并进行了校验,elf_ident是ELF文件最头部的一个长度为16字节的数组,不区分架构和系统位数。e_ident起始的4个字节固定为\0x7fELF,通过校验该位可以确定是否为elf文件。
然后识别文件是否为可执行文件或动态链接文件,ELF文件当前主要有4种格式,分别为可重定位文件(ET_REL)、可执行文件(ET_EXEC)、共享目标文件(ET_DYN)和core文件(ET_CORE)。load_elf_binary函数只负责解析exec和dyn文件。
最后还解析了文件依赖的系统架构等必要项。

程序头是描述与程序执行直接相关的目标文件结构信息,用于在文件中定位各个段的映像,同时包含其他一些用来为程序创建进程映像所必须的信息。

如果程序需要动态链接,则需要加载解释器段(PT_INTERP),程序遍历所有的程序头,识别到解释器段后,读取该段的内容。解释器段实际上是标明解释器程序文件路径的字符串,内核根据字符串指向的文件,使用open_exec函数打开解释器。

同样通过for循环遍历,如果识别到栈属性段(PT_GNU_STACK),根据程序头中的p_flags标志位判定栈的可执行属性。如果识别到处理器专用语义段(PT_LOPROC至PT_HIPROC之间),则调用arch_elf_pt_proc函数完成相应的配置。

解释器也是一个elf文件,这里读取解释器以便于后续操作

加载所有类型为PT_LOAD的段,当处理第1个PT_LOAD段时,如果文件为dyn类型,还需要对其进行地址随机化。随机化时还需要区分解释器或者其他普通so文件,对于解释器,为避免程序发生冲突,程序固定从ELF_ET_DYN_BASE开始计算偏移进行加载。

一切就绪后,通过elf_map函数建立用户空间虚拟地址空间与目标映像文件中段的映射

对于需要解释器的程序,需要先通过load_elf_interp函数装入解释器的映像,并将程序入口点设置为解释器的入口地址,对于不需要解释器的文件,直接读取elf_header中的入口点虚拟地址即可。

一切就绪后,通过START_THREAD进入新的程序入口。

struct linux_binfmt {
    struct list_head lh;
    struct module *module;
    int (*load_binary)(struct linux_binprm *);
    int (*load_shlib)(struct file *);
#ifdef CONFIG_COREDUMP
    int (*core_dump)(struct coredump_params *cprm);
    unsigned long min_coredump;   /* minimal dump size */
#endif
} __randomize_layout;
struct linux_binfmt {
    struct list_head lh;
    struct module *module;
    int (*load_binary)(struct linux_binprm *);
    int (*load_shlib)(struct file *);
#ifdef CONFIG_COREDUMP
    int (*core_dump)(struct coredump_params *cprm);
    unsigned long min_coredump;   /* minimal dump size */
#endif
} __randomize_layout;
static struct linux_binfmt elf_format = {
    .module     = THIS_MODULE,
    .load_binary    = load_elf_binary,
    .load_shlib = load_elf_library,
#ifdef CONFIG_COREDUMP
    .core_dump  = elf_core_dump,
    .min_coredump   = ELF_EXEC_PAGESIZE,
#endif
};
static struct linux_binfmt elf_format = {
    .module     = THIS_MODULE,
    .load_binary    = load_elf_binary,
    .load_shlib = load_elf_library,
#ifdef CONFIG_COREDUMP
    .core_dump  = elf_core_dump,
    .min_coredump   = ELF_EXEC_PAGESIZE,
#endif
};
struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf;
 
retval = -ENOEXEC;
/* First of all, some simple consistency checks */
if (memcmp(elf_ex->e_ident, ELFMAG, SELFMAG) != 0)
    goto out;
 
    if (elf_ex->e_type != ET_EXEC && elf_ex->e_type != ET_DYN)
        goto out;
    if (!elf_check_arch(elf_ex))
        goto out;
    if (elf_check_fdpic(elf_ex))
        goto out;
    if (!bprm->file->f_op->mmap)
        goto out;
struct elfhdr *elf_ex = (struct elfhdr *)bprm->buf;
 
retval = -ENOEXEC;
/* First of all, some simple consistency checks */
if (memcmp(elf_ex->e_ident, ELFMAG, SELFMAG) != 0)
    goto out;
 
    if (elf_ex->e_type != ET_EXEC && elf_ex->e_type != ET_DYN)
        goto out;
    if (!elf_check_arch(elf_ex))
        goto out;
    if (elf_check_fdpic(elf_ex))
        goto out;
    if (!bprm->file->f_op->mmap)
        goto out;
static struct elf_phdr *load_elf_phdrs(const struct elfhdr *elf_ex,
                       struct file *elf_file)
{
    struct elf_phdr *elf_phdata = NULL;
    int retval = -1;
    unsigned int size;
 
    /*
     * If the size of this structure has changed, then punt, since
     * we will be doing the wrong thing.
     */
    if (elf_ex->e_phentsize != sizeof(struct elf_phdr))
        goto out;
 
    /* Sanity check the number of program headers... */
    /* ...and their total size. */
    size = sizeof(struct elf_phdr) * elf_ex->e_phnum;
    if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN)
        goto out;
 
    elf_phdata = kmalloc(size, GFP_KERNEL);
    if (!elf_phdata)
        goto out;
 
    /* Read in the program headers */
    retval = elf_read(elf_file, elf_phdata, size, elf_ex->e_phoff);
 
out:
    if (retval) {
        kfree(elf_phdata);
        elf_phdata = NULL;
    }
    return elf_phdata;
}
static struct elf_phdr *load_elf_phdrs(const struct elfhdr *elf_ex,
                       struct file *elf_file)
{
    struct elf_phdr *elf_phdata = NULL;
    int retval = -1;
    unsigned int size;
 
    /*
     * If the size of this structure has changed, then punt, since
     * we will be doing the wrong thing.
     */
    if (elf_ex->e_phentsize != sizeof(struct elf_phdr))
        goto out;
 
    /* Sanity check the number of program headers... */
    /* ...and their total size. */
    size = sizeof(struct elf_phdr) * elf_ex->e_phnum;
    if (size == 0 || size > 65536 || size > ELF_MIN_ALIGN)
        goto out;
 
    elf_phdata = kmalloc(size, GFP_KERNEL);
    if (!elf_phdata)
        goto out;
 
    /* Read in the program headers */
    retval = elf_read(elf_file, elf_phdata, size, elf_ex->e_phoff);
 
out:
    if (retval) {
        kfree(elf_phdata);
        elf_phdata = NULL;
    }
    return elf_phdata;
}
          elf_ppnt = elf_phdata;
    for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++) {
        char *elf_interpreter;
 
        if (elf_ppnt->p_type == PT_GNU_PROPERTY) {
            elf_property_phdata = elf_ppnt;
            continue;
        }
 
        if (elf_ppnt->p_type != PT_INTERP)
            continue;
 
        /*
         * This is the program interpreter used for shared libraries -
         * for now assume that this is an a.out format binary.
         */
        retval = -ENOEXEC;
        if (elf_ppnt->p_filesz > PATH_MAX || elf_ppnt->p_filesz < 2)
            goto out_free_ph;
 
        retval = -ENOMEM;
        elf_interpreter = kmalloc(elf_ppnt->p_filesz, GFP_KERNEL);
        if (!elf_interpreter)
            goto out_free_ph;
 
        retval = elf_read(bprm->file, elf_interpreter, elf_ppnt->p_filesz,
                  elf_ppnt->p_offset);
        if (retval < 0)
            goto out_free_interp;
        /* make sure path is NULL terminated */
        retval = -ENOEXEC;
        if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
            goto out_free_interp;
 
        interpreter = open_exec(elf_interpreter);
        kfree(elf_interpreter);
        retval = PTR_ERR(interpreter);
        if (IS_ERR(interpreter))
            goto out_free_ph;
 
        /*
         * If the binary is not readable then enforce mm->dumpable = 0
         * regardless of the interpreter's permissions.
         */
        would_dump(bprm, interpreter);
 
        interp_elf_ex = kmalloc(sizeof(*interp_elf_ex), GFP_KERNEL);
        if (!interp_elf_ex) {
            retval = -ENOMEM;
            goto out_free_file;
        }
 
        /* Get the exec headers */
        retval = elf_read(interpreter, interp_elf_ex,
                  sizeof(*interp_elf_ex), 0);
        if (retval < 0)
            goto out_free_dentry;
 
        break;
 
out_free_interp:
        kfree(elf_interpreter);
        goto out_free_ph;
    }
          elf_ppnt = elf_phdata;
    for (i = 0; i < elf_ex->e_phnum; i++, elf_ppnt++) {
        char *elf_interpreter;
 
        if (elf_ppnt->p_type == PT_GNU_PROPERTY) {
            elf_property_phdata = elf_ppnt;
            continue;
        }
 
        if (elf_ppnt->p_type != PT_INTERP)
            continue;
 
        /*
         * This is the program interpreter used for shared libraries -
         * for now assume that this is an a.out format binary.
         */
        retval = -ENOEXEC;
        if (elf_ppnt->p_filesz > PATH_MAX || elf_ppnt->p_filesz < 2)
            goto out_free_ph;
 
        retval = -ENOMEM;
        elf_interpreter = kmalloc(elf_ppnt->p_filesz, GFP_KERNEL);
        if (!elf_interpreter)
            goto out_free_ph;
 
        retval = elf_read(bprm->file, elf_interpreter, elf_ppnt->p_filesz,
                  elf_ppnt->p_offset);
        if (retval < 0)
            goto out_free_interp;
        /* make sure path is NULL terminated */
        retval = -ENOEXEC;
        if (elf_interpreter[elf_ppnt->p_filesz - 1] != '\0')
            goto out_free_interp;
 
        interpreter = open_exec(elf_interpreter);
        kfree(elf_interpreter);
        retval = PTR_ERR(interpreter);
        if (IS_ERR(interpreter))
            goto out_free_ph;
 
        /*
         * If the binary is not readable then enforce mm->dumpable = 0
         * regardless of the interpreter's permissions.
         */
        would_dump(bprm, interpreter);
 
        interp_elf_ex = kmalloc(sizeof(*interp_elf_ex), GFP_KERNEL);
        if (!interp_elf_ex) {
            retval = -ENOMEM;
            goto out_free_file;
        }
 
        /* Get the exec headers */
        retval = elf_read(interpreter, interp_elf_ex,
                  sizeof(*interp_elf_ex), 0);

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 2
支持
分享
最新回复 (0)
游客
登录 | 注册 方可回帖
返回
//