From owner-svn-src-all@freebsd.org Tue Apr 9 15:24:40 2019 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id F411D1531E01; Tue, 9 Apr 2019 15:24:39 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 96D506A9DC; Tue, 9 Apr 2019 15:24:39 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 536D6CF75; Tue, 9 Apr 2019 15:24:39 +0000 (UTC) (envelope-from trasz@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id x39FOdlx023300; Tue, 9 Apr 2019 15:24:39 GMT (envelope-from trasz@FreeBSD.org) Received: (from trasz@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id x39FOdPv023299; Tue, 9 Apr 2019 15:24:39 GMT (envelope-from trasz@FreeBSD.org) Message-Id: <201904091524.x39FOdPv023299@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: trasz set sender to trasz@FreeBSD.org using -f From: Edward Tomasz Napierala Date: Tue, 9 Apr 2019 15:24:39 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r346053 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: trasz X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 346053 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 96D506A9DC X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org X-Spamd-Result: default: False [-2.97 / 15.00]; local_wl_from(0.00)[FreeBSD.org]; NEURAL_HAM_MEDIUM(-0.99)[-0.994,0]; NEURAL_HAM_LONG(-1.00)[-1.000,0]; NEURAL_HAM_SHORT(-0.97)[-0.973,0]; ASN(0.00)[asn:11403, ipnet:2610:1c1:1::/48, country:US] X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Apr 2019 15:24:40 -0000 Author: trasz Date: Tue Apr 9 15:24:38 2019 New Revision: 346053 URL: https://svnweb.freebsd.org/changeset/base/346053 Log: Factor out section loading into a separate function. Reviewed by: kib MFC after: 2 weeks Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D19846 Modified: head/sys/kern/imgact_elf.c Modified: head/sys/kern/imgact_elf.c ============================================================================== --- head/sys/kern/imgact_elf.c Tue Apr 9 13:54:08 2019 (r346052) +++ head/sys/kern/imgact_elf.c Tue Apr 9 15:24:38 2019 (r346053) @@ -649,6 +649,45 @@ __elfN(load_section)(struct image_params *imgp, vm_oof return (0); } +static int +__elfN(load_sections)(struct image_params *imgp, const Elf_Ehdr *hdr, + const Elf_Phdr *phdr, u_long rbase, u_long *base_addrp) +{ + vm_prot_t prot; + u_long base_addr; + bool first; + int error, i; + + base_addr = 0; + first = true; + + for (i = 0; i < hdr->e_phnum; i++) { + if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0) + continue; + + /* Loadable segment */ + prot = __elfN(trans_prot)(phdr[i].p_flags); + error = __elfN(load_section)(imgp, phdr[i].p_offset, + (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, + phdr[i].p_memsz, phdr[i].p_filesz, prot); + if (error != 0) + return (error); + + /* + * Establish the base address if this is the first segment. + */ + if (first) { + base_addr = trunc_page(phdr[i].p_vaddr + rbase); + first = false; + } + } + + if (base_addrp != NULL) + *base_addrp = base_addr; + + return (0); +} + /* * Load the file "file" into memory. It may be either a shared object * or an executable. @@ -675,10 +714,9 @@ __elfN(load_file)(struct proc *p, const char *file, u_ struct nameidata *nd; struct vattr *attr; struct image_params *imgp; - vm_prot_t prot; u_long rbase; u_long base_addr = 0; - int error, i, numsegs; + int error; #ifdef CAPABILITY_MODE /* @@ -756,25 +794,10 @@ __elfN(load_file)(struct proc *p, const char *file, u_ goto fail; } - for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { - if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) { - /* Loadable segment */ - prot = __elfN(trans_prot)(phdr[i].p_flags); - error = __elfN(load_section)(imgp, phdr[i].p_offset, - (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase, - phdr[i].p_memsz, phdr[i].p_filesz, prot); - if (error != 0) - goto fail; - /* - * Establish the base address if this is the - * first segment. - */ - if (numsegs == 0) - base_addr = trunc_page(phdr[i].p_vaddr + - rbase); - numsegs++; - } - } + error = __elfN(load_sections)(imgp, hdr, phdr, rbase, &base_addr); + if (error != 0) + goto fail; + *addr = base_addr; *entry = (unsigned long)hdr->e_entry + rbase; @@ -998,7 +1021,6 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *i char *interp; Elf_Brandinfo *brand_info; struct sysentvec *sv; - vm_prot_t prot; u_long addr, baddr, et_dyn_addr, entry, proghdr; u_long maxalign, mapsz, maxv, maxv1; uint32_t fctl0; @@ -1055,6 +1077,17 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *i maxalign = phdr[i].p_align; mapsz += phdr[i].p_memsz; n++; + + /* + * If this segment contains the program headers, + * remember their virtual address for the AT_PHDR + * aux entry. Static binaries don't usually include + * a PT_PHDR entry. + */ + if (phdr[i].p_offset == 0 && + hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize + <= phdr[i].p_filesz) + proghdr = phdr[i].p_vaddr + hdr->e_phoff; break; case PT_INTERP: /* Path to interpreter */ @@ -1074,6 +1107,9 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *i __elfN(trans_prot)(phdr[i].p_flags); imgp->stack_sz = phdr[i].p_memsz; break; + case PT_PHDR: /* Program header table info */ + proghdr = phdr[i].p_vaddr; + break; } } @@ -1174,38 +1210,10 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *i if (error != 0) goto ret; - for (i = 0; i < hdr->e_phnum; i++) { - switch (phdr[i].p_type) { - case PT_LOAD: /* Loadable segment */ - if (phdr[i].p_memsz == 0) - break; - prot = __elfN(trans_prot)(phdr[i].p_flags); - error = __elfN(load_section)(imgp, phdr[i].p_offset, - (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr, - phdr[i].p_memsz, phdr[i].p_filesz, prot); - if (error != 0) - goto ret; + error = __elfN(load_sections)(imgp, hdr, phdr, et_dyn_addr, NULL); + if (error != 0) + goto ret; - /* - * If this segment contains the program headers, - * remember their virtual address for the AT_PHDR - * aux entry. Static binaries don't usually include - * a PT_PHDR entry. - */ - if (phdr[i].p_offset == 0 && - hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize - <= phdr[i].p_filesz) - proghdr = phdr[i].p_vaddr + hdr->e_phoff + - et_dyn_addr; - break; - case PT_PHDR: /* Program header table info */ - proghdr = phdr[i].p_vaddr + et_dyn_addr; - break; - default: - break; - } - } - error = __elfN(enforce_limits)(imgp, hdr, phdr, et_dyn_addr); if (error != 0) goto ret; @@ -1253,7 +1261,7 @@ __CONCAT(exec_, __elfN(imgact))(struct image_params *i */ elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); elf_auxargs->execfd = -1; - elf_auxargs->phdr = proghdr; + elf_auxargs->phdr = proghdr + et_dyn_addr; elf_auxargs->phent = hdr->e_phentsize; elf_auxargs->phnum = hdr->e_phnum; elf_auxargs->pagesz = PAGE_SIZE;