From nobody Sun Feb 8 12:38:27 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1630628362095495.389343902395; Thu, 2 Sep 2021 17:19:22 -0700 (PDT) Received: from localhost ([::1]:35648 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mLwvQ-0003cI-SA for importer@patchew.org; Thu, 02 Sep 2021 20:19:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59052) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mLwRU-0007LA-EB for qemu-devel@nongnu.org; Thu, 02 Sep 2021 19:48:24 -0400 Received: from mail-io1-xd2e.google.com ([2607:f8b0:4864:20::d2e]:40449) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mLwRR-0003Ec-PL for qemu-devel@nongnu.org; Thu, 02 Sep 2021 19:48:23 -0400 Received: by mail-io1-xd2e.google.com with SMTP id z1so4732677ioh.7 for ; Thu, 02 Sep 2021 16:48:21 -0700 (PDT) Received: from dune.bsdimp.com (50-253-99-174-static.hfc.comcastbusiness.net. [50.253.99.174]) by smtp.gmail.com with ESMTPSA id u10sm1740502ilg.15.2021.09.02.16.48.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Sep 2021 16:48:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CR10JlOlEAQKf3unljvg+q/iEV0fpRG0cHSgOERU434=; b=hKht9dXVZ0DVAEOk2kWcj2kAugX7N3rhgDjTM3o3fZ9Yd3RtMTV9cSx42TmTKTEflr QeWJPorhPdTVRCSyrqQX4/LChMSwdK9p2K+rS2QmWWT1Vsx29LZBgyp1PkEHQStOEb1Z f4l77xeuxv1yqRDn70tXle/71fei5U5xcGCSr9Aoq7gNOyH77TZ4IUf9U1SvKZSqsR3Q 89EjVMSH6MR76dLssJoaofLFqWXSYD2LH5EblbP9k61UnglTLgbwbZki44eWErZuv8i4 dbMux2k5+52JZ5e4Z7LV4gsfssggT8XpqFN/fwsYzGEqQc47xO693vmZ8JOeSWBNVCu7 NDiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=CR10JlOlEAQKf3unljvg+q/iEV0fpRG0cHSgOERU434=; b=bZizhePS7RaVkSptMxPTsrkrs9AcM64fP6e7RLtUhmRCCV4E4hzRtGXH+0djtmN9BD ZgxOCy3pJPgRBpkIWgqgZ7VdSMpdiQoS3rsD6iU9MvUMx5O0ujTuF77dtRY6bwMGjSqc v5cIpVE9AecFVKL0zQA7sbkKV/2xppVQ10jcQMhr5YlRYhN0gigFLWguomyXfBZ4Jc06 VzvJYryrglCvoZ7f74tv6L7K9dx7GoMRxyqwjfRxqN3umgqSXvowl7PVAVuqeva5gg9K u2T7JJHqalGhb3V5phjgdEqXcKgqnkeFCU8rGLKkhfongsX6Zgdn42xe67zm5n1zNO8n kAiw== X-Gm-Message-State: AOAM530g2vaSqIFn14es5RF2Yz3cZbNq+mZleKVqk5I21QQ3C+cIfPz7 /4+6trE64eHcndg/kBpPYg+u08GZc7BegA== X-Google-Smtp-Source: ABdhPJzQ+Tnba9C8sFOUzNL41k6x5lrL25ZDt0IcceBHmNiMpYeslTdZ2k2fCfkslWq+oV5bxmyEvg== X-Received: by 2002:a05:6602:200f:: with SMTP id y15mr729330iod.64.1630626500412; Thu, 02 Sep 2021 16:48:20 -0700 (PDT) From: imp@bsdimp.com To: qemu-devel@nongnu.org Subject: [PATCH v3 38/43] bsd-user: elfload.c style catch up patch Date: Thu, 2 Sep 2021 17:47:24 -0600 Message-Id: <20210902234729.76141-39-imp@bsdimp.com> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210902234729.76141-1-imp@bsdimp.com> References: <20210902234729.76141-1-imp@bsdimp.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::d2e; envelope-from=imp@bsdimp.com; helo=mail-io1-xd2e.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kevans@freebsd.org, Richard Henderson , Warner Losh , Warner Losh Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1630628362658100003 Content-Type: text/plain; charset="utf-8" From: Warner Losh Various style fixes to elfload.c that were too painful to make earlier in this series. Signed-off-by: Warner Losh Reviewed-by: Richard Henderson --- bsd-user/elfload.c | 208 ++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c index 05751f3ce7..3660961582 100644 --- a/bsd-user/elfload.c +++ b/bsd-user/elfload.c @@ -143,10 +143,12 @@ static abi_ulong copy_elf_strings(int argc, char **ar= gv, void **page, exit(-1); } tmp1 =3D tmp; - while (*tmp++); + while (*tmp++) { + continue; + } len =3D tmp - tmp1; if (p < len) { /* this shouldn't happen - 128kB */ - return 0; + return 0; } while (len) { --p; --tmp; --len; @@ -156,14 +158,14 @@ static abi_ulong copy_elf_strings(int argc, char **ar= gv, void **page, if (!pag) { pag =3D g_try_malloc0(TARGET_PAGE_SIZE); page[p / TARGET_PAGE_SIZE] =3D pag; - if (!pag) + if (!pag) { return 0; + } } } if (len =3D=3D 0 || offset =3D=3D 0) { *(pag + offset) =3D *tmp; - } - else { + } else { int bytes_to_copy =3D (len > offset) ? offset : len; tmp -=3D bytes_to_copy; p -=3D bytes_to_copy; @@ -182,16 +184,14 @@ static void setup_arg_pages(struct bsd_binprm *bprm, = struct image_info *info, abi_ulong stack_base, size; abi_long addr; =20 - /* Create enough stack to hold everything. If we don't use - * it for args, we'll use it for something else... + /* + * Create enough stack to hold everything. If we don't use it for arg= s, + * we'll use it for something else... */ size =3D target_dflssiz; stack_base =3D TARGET_USRSTACK - size; - addr =3D target_mmap(stack_base, - size + qemu_host_page_size, - PROT_READ | PROT_WRITE, - MAP_PRIVATE | MAP_ANON, - -1, 0); + addr =3D target_mmap(stack_base , size + qemu_host_page_size, + PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (addr =3D=3D -1) { perror("stk mmap"); exit(-1); @@ -210,55 +210,60 @@ static void setup_arg_pages(struct bsd_binprm *bprm, = struct image_info *info, =20 static void set_brk(abi_ulong start, abi_ulong end) { - /* page-align the start and end addresses... */ - start =3D HOST_PAGE_ALIGN(start); - end =3D HOST_PAGE_ALIGN(end); - if (end <=3D start) - return; - if (target_mmap(start, end - start, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) =3D=3D -= 1) { - perror("cannot mmap brk"); - exit(-1); - } + /* page-align the start and end addresses... */ + start =3D HOST_PAGE_ALIGN(start); + end =3D HOST_PAGE_ALIGN(end); + if (end <=3D start) { + return; + } + if (target_mmap(start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) =3D=3D -1) { + perror("cannot mmap brk"); + exit(-1); + } } =20 =20 -/* We need to explicitly zero any fractional pages after the data - section (i.e. bss). This would contain the junk from the file that - should not be in memory. */ +/* + * We need to explicitly zero any fractional pages after the data + * section (i.e. bss). This would contain the junk from the file that + * should not be in memory. + */ static void padzero(abi_ulong elf_bss, abi_ulong last_bss) { - abi_ulong nbyte; + abi_ulong nbyte; =20 - if (elf_bss >=3D last_bss) - return; + if (elf_bss >=3D last_bss) { + return; + } =20 - /* XXX: this is really a hack : if the real host page size is - smaller than the target page size, some pages after the end - of the file may not be mapped. A better fix would be to - patch target_mmap(), but it is more complicated as the file - size must be known */ - if (qemu_real_host_page_size < qemu_host_page_size) { - abi_ulong end_addr, end_addr1; - end_addr1 =3D REAL_HOST_PAGE_ALIGN(elf_bss); - end_addr =3D HOST_PAGE_ALIGN(elf_bss); - if (end_addr1 < end_addr) { - mmap((void *)g2h_untagged(end_addr1), end_addr - end_addr1, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0); - } + /* + * XXX: this is really a hack : if the real host page size is + * smaller than the target page size, some pages after the end + * of the file may not be mapped. A better fix would be to + * patch target_mmap(), but it is more complicated as the file + * size must be known. + */ + if (qemu_real_host_page_size < qemu_host_page_size) { + abi_ulong end_addr, end_addr1; + end_addr1 =3D REAL_HOST_PAGE_ALIGN(elf_bss); + end_addr =3D HOST_PAGE_ALIGN(elf_bss); + if (end_addr1 < end_addr) { + mmap((void *)g2h_untagged(end_addr1), end_addr - end_addr1, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0); } + } =20 - nbyte =3D elf_bss & (qemu_host_page_size - 1); - if (nbyte) { - nbyte =3D qemu_host_page_size - nbyte; - do { - /* FIXME - what to do if put_user() fails? */ - put_user_u8(0, elf_bss); - elf_bss++; - } while (--nbyte); - } + nbyte =3D elf_bss & (qemu_host_page_size - 1); + if (nbyte) { + nbyte =3D qemu_host_page_size - nbyte; + do { + /* FIXME - what to do if put_user() fails? */ + put_user_u8(0, elf_bss); + elf_bss++; + } while (--nbyte); + } } =20 static abi_ulong load_elf_interp(struct elfhdr *interp_elf_ex, @@ -280,23 +285,23 @@ static abi_ulong load_elf_interp(struct elfhdr *inter= p_elf_ex, =20 bswap_ehdr(interp_elf_ex); /* First of all, some simple consistency checks */ - if ((interp_elf_ex->e_type !=3D ET_EXEC && - interp_elf_ex->e_type !=3D ET_DYN) || - !elf_check_arch(interp_elf_ex->e_machine)) { + if ((interp_elf_ex->e_type !=3D ET_EXEC && interp_elf_ex->e_type !=3D = ET_DYN) || + !elf_check_arch(interp_elf_ex->e_machine)) { return ~((abi_ulong)0UL); } =20 =20 /* Now read in all of the header information */ - - if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZ= E) + if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZ= E) { return ~(abi_ulong)0UL; + } =20 - elf_phdata =3D (struct elf_phdr *) - malloc(sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); + elf_phdata =3D (struct elf_phdr *) malloc(sizeof(struct elf_phdr) * + interp_elf_ex->e_phnum); =20 - if (!elf_phdata) + if (!elf_phdata) { return ~((abi_ulong)0UL); + } =20 /* * If the size of this structure has changed, then punt, since @@ -309,9 +314,8 @@ static abi_ulong load_elf_interp(struct elfhdr *interp_= elf_ex, =20 retval =3D lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET); if (retval >=3D 0) { - retval =3D read(interpreter_fd, - (char *) elf_phdata, - sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); + retval =3D read(interpreter_fd, (char *) elf_phdata, + sizeof(struct elf_phdr) * interp_elf_ex->e_phnum); } if (retval < 0) { perror("load_elf_interp"); @@ -322,11 +326,12 @@ static abi_ulong load_elf_interp(struct elfhdr *inter= p_elf_ex, bswap_phdr(elf_phdata, interp_elf_ex->e_phnum); =20 if (interp_elf_ex->e_type =3D=3D ET_DYN) { - /* in order to avoid hardcoding the interpreter load - address in qemu, we allocate a big enough memory zone */ - error =3D target_mmap(0, INTERP_MAP_SIZE, - PROT_NONE, MAP_PRIVATE | MAP_ANON, - -1, 0); + /* + * In order to avoid hardcoding the interpreter load + * address in qemu, we allocate a big enough memory zone. + */ + error =3D target_mmap(0, INTERP_MAP_SIZE, PROT_NONE, + MAP_PRIVATE | MAP_ANON, -1, 0); if (error =3D=3D -1) { perror("mmap"); exit(-1); @@ -430,7 +435,7 @@ static const char *lookup_symbolxx(struct syminfo *s, t= arget_ulong orig_addr) struct elf_sym *syms =3D s->disas_symtab.elf64; #endif =20 - // binary search + /* binary search */ struct elf_sym *sym; =20 sym =3D bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), sy= mfind); @@ -446,9 +451,8 @@ static int symcmp(const void *s0, const void *s1) { struct elf_sym *sym0 =3D (struct elf_sym *)s0; struct elf_sym *sym1 =3D (struct elf_sym *)s1; - return (sym0->st_value < sym1->st_value) - ? -1 - : ((sym0->st_value > sym1->st_value) ? 1 : 0); + return (sym0->st_value < sym1->st_value) ? -1 : + ((sym0->st_value > sym1->st_value) ? 1 : 0); } =20 /* Best attempt to load symbols from this ELF object. */ @@ -462,23 +466,24 @@ static void load_symbols(struct elfhdr *hdr, int fd) =20 lseek(fd, hdr->e_shoff, SEEK_SET); for (i =3D 0; i < hdr->e_shnum; i++) { - if (read(fd, &sechdr, sizeof(sechdr)) !=3D sizeof(sechdr)) + if (read(fd, &sechdr, sizeof(sechdr)) !=3D sizeof(sechdr)) { return; + } bswap_shdr(&sechdr, 1); if (sechdr.sh_type =3D=3D SHT_SYMTAB) { symtab =3D sechdr; - lseek(fd, hdr->e_shoff - + sizeof(sechdr) * sechdr.sh_link, SEEK_SET); - if (read(fd, &strtab, sizeof(strtab)) - !=3D sizeof(strtab)) + lseek(fd, hdr->e_shoff + sizeof(sechdr) * sechdr.sh_link, + SEEK_SET); + if (read(fd, &strtab, sizeof(strtab)) !=3D sizeof(strtab)) { return; + } bswap_shdr(&strtab, 1); goto found; } } return; /* Shouldn't happen... */ =20 - found: +found: /* Now know where the strtab and symtab are. Snarf them. */ s =3D malloc(sizeof(*s)); syms =3D malloc(symtab.sh_size); @@ -506,7 +511,7 @@ static void load_symbols(struct elfhdr *hdr, int fd) i =3D 0; while (i < nsyms) { bswap_sym(syms + i); - // Throw away entries which we do not need. + /* Throw away entries which we do not need. */ if (syms[i].st_shndx =3D=3D SHN_UNDEF || syms[i].st_shndx >=3D SHN_LORESERVE || ELF_ST_TYPE(syms[i].st_info) !=3D STT_FUNC) { @@ -519,10 +524,12 @@ static void load_symbols(struct elfhdr *hdr, int fd) i++; } =20 - /* Attempt to free the storage associated with the local symbols - that we threw away. Whether or not this has any effect on the - memory allocation depends on the malloc implementation and how - many symbols we managed to discard. */ + /* + * Attempt to free the storage associated with the local symbols + * that we threw away. Whether or not this has any effect on the + * memory allocation depends on the malloc implementation and how + * many symbols we managed to discard. + */ new_syms =3D realloc(syms, nsyms * sizeof(*syms)); if (new_syms =3D=3D NULL) { free(s); @@ -578,19 +585,19 @@ int load_elf_binary(struct bsd_binprm *bprm, struct t= arget_pt_regs *regs, =20 /* First of all, some simple consistency checks */ if ((elf_ex.e_type !=3D ET_EXEC && elf_ex.e_type !=3D ET_DYN) || - (!elf_check_arch(elf_ex.e_machine))) { + (!elf_check_arch(elf_ex.e_machine))) { return -ENOEXEC; } =20 bprm->p =3D copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p); - bprm->p =3D copy_elf_strings(bprm->envc, bprm->envp, bprm->page,bprm->= p); - bprm->p =3D copy_elf_strings(bprm->argc, bprm->argv, bprm->page,bprm->= p); + bprm->p =3D copy_elf_strings(bprm->envc, bprm->envp, bprm->page, bprm-= >p); + bprm->p =3D copy_elf_strings(bprm->argc, bprm->argv, bprm->page, bprm-= >p); if (!bprm->p) { retval =3D -E2BIG; } =20 /* Now read in all of the header information */ - elf_phdata =3D (struct elf_phdr *)malloc(elf_ex.e_phentsize*elf_ex.e_p= hnum); + elf_phdata =3D (struct elf_phdr *)malloc(elf_ex.e_phentsize * elf_ex.e= _phnum); if (elf_phdata =3D=3D NULL) { return -ENOMEM; } @@ -609,7 +616,6 @@ int load_elf_binary(struct bsd_binprm *bprm, struct tar= get_pt_regs *regs, } =20 bswap_phdr(elf_phdata, elf_ex.e_phnum); - elf_ppnt =3D elf_phdata; =20 elf_bss =3D 0; @@ -622,23 +628,16 @@ int load_elf_binary(struct bsd_binprm *bprm, struct t= arget_pt_regs *regs, start_data =3D 0; end_data =3D 0; =20 - for (i =3D 0;i < elf_ex.e_phnum; i++) { + for (i =3D 0; i < elf_ex.e_phnum; i++) { if (elf_ppnt->p_type =3D=3D PT_INTERP) { - if (elf_interpreter !=3D NULL) - { + if (elf_interpreter !=3D NULL) { free(elf_phdata); free(elf_interpreter); close(bprm->fd); return -EINVAL; } =20 - /* This is the program interpreter used for - * shared libraries - for now assume that this - * is an a.out format binary - */ - elf_interpreter =3D (char *)malloc(elf_ppnt->p_filesz); - if (elf_interpreter =3D=3D NULL) { free(elf_phdata); close(bprm->fd); @@ -658,8 +657,7 @@ int load_elf_binary(struct bsd_binprm *bprm, struct tar= get_pt_regs *regs, retval =3D open(path(elf_interpreter), O_RDONLY); if (retval >=3D 0) { interpreter_fd =3D retval; - } - else { + } else { perror(elf_interpreter); exit(-1); /* retval =3D -errno; */ @@ -673,7 +671,7 @@ int load_elf_binary(struct bsd_binprm *bprm, struct tar= get_pt_regs *regs, } } if (retval >=3D 0) { - interp_elf_ex =3D *((struct elfhdr *) bprm->buf); /* elf e= xec-header */ + interp_elf_ex =3D *((struct elfhdr *) bprm->buf); } if (retval < 0) { perror("load_elf_binary3"); @@ -698,9 +696,10 @@ int load_elf_binary(struct bsd_binprm *bprm, struct ta= rget_pt_regs *regs, } } =20 - /* OK, we are done with that, now set up the arg stuff, - and then start this sucker up */ - + /* + * OK, we are done with that, now set up the arg stuff, and then start= this + * sucker up + */ if (!bprm->p) { free(elf_interpreter); free(elf_phdata); @@ -822,8 +821,9 @@ int load_elf_binary(struct bsd_binprm *bprm, struct tar= get_pt_regs *regs, =20 free(elf_phdata); =20 - if (qemu_log_enabled()) + if (qemu_log_enabled()) { load_symbols(&elf_ex, bprm->fd); + } =20 close(bprm->fd); =20 --=20 2.32.0