zoukankan      html  css  js  c++  java
  • Linux 链接详解(2)

    可执行文件加载执行过程:

    上一节我们说到ELF文件格式,静态库的符号解析和重定位的内容。这一节我们来分析一下可执行文件。

    由上一节我们知道可执行文件也是ELF文件,当程序被加载器加载到内存时是按照ELF格式去解析,然后把可执行文件的不同节加载到虚拟地址空间中。我们看一下32位下的进程虚拟地址模型:

    可执行文件的信息被加载到了虚拟地址空间。根据ELF信息找到程序的入口地址就可以执行进程了。我们看一下linux 3.2 内核代码 fs/exec.c中调用可执行文件的大致过程。

    static int do_execve_common(const char *filename,
                    struct user_arg_ptr argv,
                    struct user_arg_ptr envp,
                    struct pt_regs *regs)
    {
        struct linux_binprm *bprm;
        struct file *file;
        struct files_struct *displaced;
        bool clear_in_exec;
        int retval;
        const struct cred *cred = current_cred();
            ...
            ...   
        retval = search_binary_handler(bprm,regs);  //搜索可执行文件处理接口
        if (retval < 0)
            goto out;
    }
    
    int search_binary_handler(struct linux_binprm *bprm,struct pt_regs *regs)
    {
        unsigned int depth = bprm->recursion_depth;
        int try,retval;
        struct linux_binfmt *fmt;
        pid_t old_pid;
    
            .......
        retval = -ENOENT;
        for (try=0; try<2; try++) {
            read_lock(&binfmt_lock);
            list_for_each_entry(fmt, &formats, lh) {
                int (*fn)(struct linux_binprm *, struct pt_regs *) = fmt->load_binary; 
                     //找到可执行文件加载入口
                if (!fn)
                    continue;
        }
     }
    }    
    
    typedef struct elf64_hdr {
      unsigned char    e_ident[EI_NIDENT];    /* ELF "magic number" */
      Elf64_Half e_type;
      Elf64_Half e_machine;
      Elf64_Word e_version;
      Elf64_Addr e_entry;        /* Entry point virtual address */
      Elf64_Off e_phoff;        /* Program header table file offset */
      Elf64_Off e_shoff;        /* Section header table file offset */
      Elf64_Word e_flags;
      Elf64_Half e_ehsize;
      Elf64_Half e_phentsize;
      Elf64_Half e_phnum;
      Elf64_Half e_shentsize;
      Elf64_Half e_shnum;
      Elf64_Half e_shstrndx;
    } Elf64_Ehdr;
    
    load_binary  可执行目标文件对应  binfmt_elf.c 文件
    static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
    {
    //解析elf文件 做段映射
    struct {
            struct elfhdr elf_ex;
            struct elfhdr interp_elf_ex;
        } *loc;
    
        loc = kmalloc(sizeof(*loc), GFP_KERNEL);
        if (!loc) {
            retval = -ENOMEM;
            goto out_ret;
        }
        
        /* Get the exec-header */
        loc->elf_ex = *((struct elfhdr *)bprm->buf);
    
        retval = -ENOEXEC;
        /* First of all, some simple consistency checks */
        if (memcmp(loc->elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
            goto out;
    
        if (loc->elf_ex.e_type != ET_EXEC && loc->elf_ex.e_type != ET_DYN)
            goto out;
        if (!elf_check_arch(&loc->elf_ex))
            goto out;
        if (!bprm->file->f_op || !bprm->file->f_op->mmap)
            goto out;
    
        /* Now read in all of the header information */
        if (loc->elf_ex.e_phentsize != sizeof(struct elf_phdr))
            goto out;
        if (loc->elf_ex.e_phnum < 1 ||
             loc->elf_ex.e_phnum > 65536U / sizeof(struct elf_phdr))
            goto out;
        size = loc->elf_ex.e_phnum * sizeof(struct elf_phdr);
        retval = -ENOMEM;
        elf_phdata = kmalloc(size, GFP_KERNEL);
        if (!elf_phdata)
            goto out;
    
        retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
                     (char *)elf_phdata, size);
        if (retval != size) {
            if (retval >= 0)
                retval = -EIO;
            goto out_free_ph;
        }
    
        elf_ppnt = elf_phdata;
        elf_bss = 0;
        elf_brk = 0;
    
        start_code = ~0UL;
        end_code = 0;
        start_data = 0;
        end_data = 0;
    
        for (i = 0; i < loc->elf_ex.e_phnum; i++) {
            if (elf_ppnt->p_type == PT_INTERP) {
                /* 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 = kernel_read(bprm->file, elf_ppnt->p_offset,
                             elf_interpreter,
                             elf_ppnt->p_filesz);
                if (retval != elf_ppnt->p_filesz) {
                    if (retval >= 0)
                        retval = -EIO;
                    goto out_free_interp;
                }
                /* make sure path is NULL terminated */
                retval = -ENOEXEC;
                if (elf_interpreter[elf_ppnt->p_filesz - 1] != '')
                    goto out_free_interp;
    
                interpreter = open_exec(elf_interpreter);
                retval = PTR_ERR(interpreter);
                if (IS_ERR(interpreter))
                    goto out_free_interp;
    
                /*
                 * If the binary is not readable then enforce
                 * mm->dumpable = 0 regardless of the interpreter's
                 * permissions.
                 */
                would_dump(bprm, interpreter);
    
                /* Get the exec headers */
                retval = kernel_read(interpreter, 0,
                             (void *)&loc->interp_elf_ex,
                             sizeof(loc->interp_elf_ex));
                if (retval != sizeof(loc->interp_elf_ex)) {
                    if (retval >= 0)
                        retval = -EIO;
                    goto out_free_dentry;
                }
    
                break;
            }
            elf_ppnt++;
        }
    
        elf_ppnt = elf_phdata;
        for (i = 0; i < loc->elf_ex.e_phnum; i++, elf_ppnt++)
            if (elf_ppnt->p_type == PT_GNU_STACK) {
                if (elf_ppnt->p_flags & PF_X)
                    executable_stack = EXSTACK_ENABLE_X;
                else
                    executable_stack = EXSTACK_DISABLE_X;
                break;
            }
    
        /* Some simple consistency checks for the interpreter */
        if (elf_interpreter) {
            retval = -ELIBBAD;
            /* Not an ELF interpreter */
            if (memcmp(loc->interp_elf_ex.e_ident, ELFMAG, SELFMAG) != 0)
                goto out_free_dentry;
            /* Verify the interpreter has a valid arch */
            if (!elf_check_arch(&loc->interp_elf_ex))
                goto out_free_dentry;
        }
    
        /* Flush all traces of the currently running executable */
        retval = flush_old_exec(bprm);
        if (retval)
            goto out_free_dentry;
    
        /* OK, This is the point of no return */
        current->flags &= ~PF_FORKNOEXEC;
        current->mm->def_flags = def_flags;
    
        /* Do this immediately, since STACK_TOP as used in setup_arg_pages
           may depend on the personality.  */
        SET_PERSONALITY(loc->elf_ex);
        if (elf_read_implies_exec(loc->elf_ex, executable_stack))
            current->personality |= READ_IMPLIES_EXEC;
    
        if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
            current->flags |= PF_RANDOMIZE;
    
        setup_new_exec(bprm);
    
        /* Do this so that we can load the interpreter, if need be.  We will
           change some of these later */
        current->mm->free_area_cache = current->mm->mmap_base;
        current->mm->cached_hole_size = 0;
        retval = setup_arg_pages(bprm, randomize_stack_top(STACK_TOP),
                     executable_stack);
        if (retval < 0) {
            send_sig(SIGKILL, current, 0);
            goto out_free_dentry;
        }
        
        current->mm->start_stack = bprm->p;
    
        /* Now we do a little grungy work by mmapping the ELF image into
           the correct location in memory. */
        for(i = 0, elf_ppnt = elf_phdata;
            i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
            int elf_prot = 0, elf_flags;
            unsigned long k, vaddr;
            unsigned long total_size = 0;
    
            if (elf_ppnt->p_type != PT_LOAD)
                continue;
    
            if (unlikely (elf_brk > elf_bss)) {
                unsigned long nbyte;
                    
                /* There was a PT_LOAD segment with p_memsz > p_filesz
                   before this one. Map anonymous pages, if needed,
                   and clear the area.  */
                retval = set_brk(elf_bss + load_bias,
                         elf_brk + load_bias);
                if (retval) {
                    send_sig(SIGKILL, current, 0);
                    goto out_free_dentry;
                }
                nbyte = ELF_PAGEOFFSET(elf_bss);
                if (nbyte) {
                    nbyte = ELF_MIN_ALIGN - nbyte;
                    if (nbyte > elf_brk - elf_bss)
                        nbyte = elf_brk - elf_bss;
                    if (clear_user((void __user *)elf_bss +
                                load_bias, nbyte)) {
                        /*
                         * This bss-zeroing can fail if the ELF
                         * file specifies odd protections. So
                         * we don't check the return value
                         */
                    }
                }
            }
    
            if (elf_ppnt->p_flags & PF_R)
                elf_prot |= PROT_READ;
            if (elf_ppnt->p_flags & PF_W)
                elf_prot |= PROT_WRITE;
            if (elf_ppnt->p_flags & PF_X)
                elf_prot |= PROT_EXEC;
    
            elf_flags = MAP_PRIVATE | MAP_DENYWRITE | MAP_EXECUTABLE;
    
            vaddr = elf_ppnt->p_vaddr;
            if (loc->elf_ex.e_type == ET_EXEC || load_addr_set) {
                elf_flags |= MAP_FIXED;
            } else if (loc->elf_ex.e_type == ET_DYN) {
                /* Try and get dynamic programs out of the way of the
                 * default mmap base, as well as whatever program they
                 * might try to exec.  This is because the brk will
                 * follow the loader, and is not movable.  */
    #if defined(CONFIG_X86) || defined(CONFIG_ARM)
                /* Memory randomization might have been switched off
                 * in runtime via sysctl.
                 * If that is the case, retain the original non-zero
                 * load_bias value in order to establish proper
                 * non-randomized mappings.
                 */
                if (current->flags & PF_RANDOMIZE)
                    load_bias = 0;
                else
                    load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
    #else
                load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
    #endif
                total_size = total_mapping_size(elf_phdata,
                                loc->elf_ex.e_phnum);
                if (!total_size) {
                    retval = -EINVAL;
                    goto out_free_dentry;
                }
            }
    
            error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
                    elf_prot, elf_flags, total_size);
            if (BAD_ADDR(error)) {
                send_sig(SIGKILL, current, 0);
                retval = IS_ERR((void *)error) ?
                    PTR_ERR((void*)error) : -EINVAL;
                goto out_free_dentry;
            }
    
            if (!load_addr_set) {
                load_addr_set = 1;
                load_addr = (elf_ppnt->p_vaddr - elf_ppnt->p_offset);
                if (loc->elf_ex.e_type == ET_DYN) {
                    load_bias += error -
                                 ELF_PAGESTART(load_bias + vaddr);
                    load_addr += load_bias;
                    reloc_func_desc = load_bias;
                }
            }
            k = elf_ppnt->p_vaddr;
            if (k < start_code)
                start_code = k;
            if (start_data < k)
                start_data = k;
    
            /*
             * Check to see if the section's size will overflow the
             * allowed task size. Note that p_filesz must always be
             * <= p_memsz so it is only necessary to check p_memsz.
             */
            if (BAD_ADDR(k) || elf_ppnt->p_filesz > elf_ppnt->p_memsz ||
                elf_ppnt->p_memsz > TASK_SIZE ||
                TASK_SIZE - elf_ppnt->p_memsz < k) {
                /* set_brk can never work. Avoid overflows. */
                send_sig(SIGKILL, current, 0);
                retval = -EINVAL;
                goto out_free_dentry;
            }
    
            k = elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
    
            if (k > elf_bss)
                elf_bss = k;
            if ((elf_ppnt->p_flags & PF_X) && end_code < k)
                end_code = k;
            if (end_data < k)
                end_data = k;
            k = elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
            if (k > elf_brk)
                elf_brk = k;
        }
    
        loc->elf_ex.e_entry += load_bias;
        elf_bss += load_bias;
        elf_brk += load_bias;
        start_code += load_bias;
        end_code += load_bias;
        start_data += load_bias;
        end_data += load_bias;
    
        /* Calling set_brk effectively mmaps the pages that we need
         * for the bss and break sections.  We must do this before
         * mapping in the interpreter, to make sure it doesn't wind
         * up getting placed where the bss needs to go.
         */
        retval = set_brk(elf_bss, elf_brk);
        if (retval) {
            send_sig(SIGKILL, current, 0);
            goto out_free_dentry;
        }
        if (likely(elf_bss != elf_brk) && unlikely(padzero(elf_bss))) {
            send_sig(SIGSEGV, current, 0);
            retval = -EFAULT; /* Nobody gets to see this, but.. */
            goto out_free_dentry;
        }
    
        if (elf_interpreter) {
            unsigned long uninitialized_var(interp_map_addr);
    
            elf_entry = load_elf_interp(&loc->interp_elf_ex,
                            interpreter,
                            &interp_map_addr,
                            load_bias);
            if (!IS_ERR((void *)elf_entry)) {
                /*
                 * load_elf_interp() returns relocation
                 * adjustment
                 */
                interp_load_addr = elf_entry;
                elf_entry += loc->interp_elf_ex.e_entry;
            }
            if (BAD_ADDR(elf_entry)) {
                force_sig(SIGSEGV, current);
                retval = IS_ERR((void *)elf_entry) ?
                        (int)elf_entry : -EINVAL;
                goto out_free_dentry;
            }
            reloc_func_desc = interp_load_addr;
    
            allow_write_access(interpreter);
            fput(interpreter);
            kfree(elf_interpreter);
        } else {
            elf_entry = loc->elf_ex.e_entry;
            if (BAD_ADDR(elf_entry)) {
                force_sig(SIGSEGV, current);
                retval = -EINVAL;
                goto out_free_dentry;
            }
        }
    
        kfree(elf_phdata);
    
        set_binfmt(&elf_format);
    
    #ifdef ARCH_HAS_SETUP_ADDITIONAL_PAGES
        retval = arch_setup_additional_pages(bprm, !!elf_interpreter);
        if (retval < 0) {
            send_sig(SIGKILL, current, 0);
            goto out;
        }
    #endif /* ARCH_HAS_SETUP_ADDITIONAL_PAGES */
    
        install_exec_creds(bprm);
        current->flags &= ~PF_FORKNOEXEC;
        retval = create_elf_tables(bprm, &loc->elf_ex,
                  load_addr, interp_load_addr);
        if (retval < 0) {
            send_sig(SIGKILL, current, 0);
            goto out;
        }
        /* N.B. passed_fileno might not be initialized? */
        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;
    
    #ifdef arch_randomize_brk
        if ((current->flags & PF_RANDOMIZE) && (randomize_va_space > 1)) {
            current->mm->brk = current->mm->start_brk =
                arch_randomize_brk(current->mm);
    #ifdef CONFIG_COMPAT_BRK
            current->brk_randomized = 1;
    #endif
        }
    #endif
    
        if (current->personality & MMAP_PAGE_ZERO) {
            /* Why this, you ask???  Well SVr4 maps page 0 as read-only,
               and some applications "depend" upon this behavior.
               Since we do not have the power to recompile these, we
               emulate the SVr4 behavior. Sigh. */
            down_write(&current->mm->mmap_sem);
            error = do_mmap(NULL, 0, PAGE_SIZE, PROT_READ | PROT_EXEC,
                    MAP_FIXED | MAP_PRIVATE, 0);
            up_write(&current->mm->mmap_sem);
        }
            //开始执行新进程 
        start_thread(regs, elf_entry, bprm->p);
        retval = 0;
    out:
        kfree(loc);
    out_ret:
        return retval;
    
        /* error cleanup */
    out_free_dentry:
        allow_write_access(interpreter);
        if (interpreter)
            fput(interpreter);
    out_free_interp:
        kfree(elf_interpreter);
    out_free_ph:
        kfree(elf_phdata);
        goto out;
    }
    View Code

    我们仔细看一下这里

            elf_phdata = kmalloc(size, GFP_KERNEL);
        if (!elf_phdata)
            goto out;
            //读取可执行文件的程序头
        retval = kernel_read(bprm->file, loc->elf_ex.e_phoff,
                     (char *)elf_phdata, size);
        if (retval != size) {
            if (retval >= 0)
                retval = -EIO;
            goto out_free_ph;
        }

    readelf 看一下可执行文件的程序头(x86_64)

    程序头描述了可执行文件到虚拟地址的映射关系。 这里还可以看到INTERP段 请求解释器/lib64/ld-linux-x86-64.so.2.  它被用来加载和定位动态库, 这里的可执行文件对标准c库是动态链接, 所以会请求这个文件。

    for (i = 0; i < loc->elf_ex.e_phnum; i++) {
                   //检查是否有需要加载的解释器
            if (elf_ppnt->p_type == PT_INTERP) {
                /* 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 = kernel_read(bprm->file, elf_ppnt->p_offset,
                             elf_interpreter,
                             elf_ppnt->p_filesz);
                if (retval != elf_ppnt->p_filesz) {
                    if (retval >= 0)
                        retval = -EIO;
                    goto out_free_interp;
                }
                /* make sure path is NULL terminated */
                retval = -ENOEXEC;
                if (elf_interpreter[elf_ppnt->p_filesz - 1] != '')
                    goto out_free_interp;
                             //打开解释器
                interpreter = open_exec(elf_interpreter);
                retval = PTR_ERR(interpreter);
                if (IS_ERR(interpreter))
                    goto out_free_interp;
    
                /*
                 * If the binary is not readable then enforce
                 * mm->dumpable = 0 regardless of the interpreter's
                 * permissions.
                 */
                would_dump(bprm, interpreter);
    
                /* Get the exec headers */
                             //得到解释器头部
                retval = kernel_read(interpreter, 0,
                             (void *)&loc->interp_elf_ex,
                             sizeof(loc->interp_elf_ex));
                if (retval != sizeof(loc->interp_elf_ex)) {
                    if (retval >= 0)
                        retval = -EIO;
                    goto out_free_dentry;
                }
    
                break;
            }
            elf_ppnt++;
        }
       //PT_GNU_STACK 判断栈中是否有可执行代码
      ... .... .... ..

    然后结下来的工作就是检查和确定装入地址开始装入了

    /* Now we do a little grungy work by mmapping the ELF image into
           the correct location in memory. */
        for(i = 0, elf_ppnt = elf_phdata;
            i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
            int elf_prot = 0, elf_flags;
            unsigned long k, vaddr;
            unsigned long total_size = 0;
    
            if (elf_ppnt->p_type != PT_LOAD)
                continue;
    
            if (unlikely (elf_brk > elf_bss)) {
                unsigned long nbyte;
                     。。。。。。。。
            error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
                    elf_prot, elf_flags, total_size);
            if (BAD_ADDR(error)) {
                send_sig(SIGKILL, current, 0);
                retval = IS_ERR((void *)error) ?
                    PTR_ERR((void*)error) : -EINVAL;
                goto out_free_dentry;
            }
                    //从elf中读取程序入口地址  开始执行
                        start_thread(regs, elf_entry, bprm->p);
                         retval = 0;            
    }

    执行的过程:

    当加载器执行时, 就会创建上图所示虚拟存储器镜像(图为32位系统)。在程序段头表的指导下将可执行文件的代码段和数据段映射到虚拟地址的虚拟地址空间,接下来加载器跳转到_start 入口处依次执行  : __libc_init_first  ------>  _init  -------> atexit -------> main ------>_exit   首先初始化c库。调用初始化代码,注册退出函数  执行main 函数 最后执行退出函数。关于这块可以参考: http://blog.chinaunix.net/uid-24774106-id-3450789.html

    到这里可执行文件执行过程已经简单了解了,整个过程与进程的创建执行有关,加载分析ELF并不复杂,其中还牵涉到动态库的链接和使用后续会单独看一下动态库加载过程。

  • 相关阅读:
    第一阶段冲刺——4
    Day 1 测试流程--H模型
    【疑问】待解决
    Zookeeper 入门(一)
    【基础组件1】Flume入门(一)
    埋点测试、埋点接口测试
    单点登录 VS 多点登录
    web系统原理
    测试用例总结篇(一)
    功能测试心得(二)
  • 原文地址:https://www.cnblogs.com/MaAce/p/7851008.html
Copyright © 2011-2022 走看看