All of lore.kernel.org
 help / color / mirror / Atom feed
From: imp@bsdimp.com
To: qemu-devel@nongnu.org
Cc: Richard Henderson <richard.henderson@linaro.org>,
	Warner Losh <imp@bsdimp.com>
Subject: [PATCH v2 14/43] bsd-user: elfload: simplify bswap a bit.
Date: Thu, 26 Aug 2021 15:11:32 -0600	[thread overview]
Message-ID: <20210826211201.98877-15-imp@bsdimp.com> (raw)
In-Reply-To: <20210826211201.98877-1-imp@bsdimp.com>

From: Warner Losh <imp@bsdimp.com>

Reduce the number of ifdefs by always calling the swapping routine, but
making them empty when swapping isn't needed.

Signed-off-by: Warner Losh <imp@bsdimp.com>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
---
 bsd-user/elfload.c | 97 ++++++++++++++++++++++------------------------
 1 file changed, 47 insertions(+), 50 deletions(-)

diff --git a/bsd-user/elfload.c b/bsd-user/elfload.c
index 9c34e2ffcb..8a6a72bf05 100644
--- a/bsd-user/elfload.c
+++ b/bsd-user/elfload.c
@@ -67,13 +67,13 @@
 
 static inline void memcpy_fromfs(void *to, const void *from, unsigned long n)
 {
-        memcpy(to, from, n);
+    memcpy(to, from, n);
 }
 
 #ifdef BSWAP_NEEDED
 static void bswap_ehdr(struct elfhdr *ehdr)
 {
-    bswap16s(&ehdr->e_type);                    /* Object file type */
+    bswap16s(&ehdr->e_type);            /* Object file type */
     bswap16s(&ehdr->e_machine);         /* Architecture */
     bswap32s(&ehdr->e_version);         /* Object file version */
     bswaptls(&ehdr->e_entry);           /* Entry point virtual address */
@@ -81,37 +81,45 @@ static void bswap_ehdr(struct elfhdr *ehdr)
     bswaptls(&ehdr->e_shoff);           /* Section header table file offset */
     bswap32s(&ehdr->e_flags);           /* Processor-specific flags */
     bswap16s(&ehdr->e_ehsize);          /* ELF header size in bytes */
-    bswap16s(&ehdr->e_phentsize);               /* Program header table entry size */
+    bswap16s(&ehdr->e_phentsize);       /* Program header table entry size */
     bswap16s(&ehdr->e_phnum);           /* Program header table entry count */
-    bswap16s(&ehdr->e_shentsize);               /* Section header table entry size */
+    bswap16s(&ehdr->e_shentsize);       /* Section header table entry size */
     bswap16s(&ehdr->e_shnum);           /* Section header table entry count */
-    bswap16s(&ehdr->e_shstrndx);                /* Section header string table index */
+    bswap16s(&ehdr->e_shstrndx);        /* Section header string table index */
 }
 
-static void bswap_phdr(struct elf_phdr *phdr)
+static void bswap_phdr(struct elf_phdr *phdr, int phnum)
 {
-    bswap32s(&phdr->p_type);                    /* Segment type */
-    bswaptls(&phdr->p_offset);          /* Segment file offset */
-    bswaptls(&phdr->p_vaddr);           /* Segment virtual address */
-    bswaptls(&phdr->p_paddr);           /* Segment physical address */
-    bswaptls(&phdr->p_filesz);          /* Segment size in file */
-    bswaptls(&phdr->p_memsz);           /* Segment size in memory */
-    bswap32s(&phdr->p_flags);           /* Segment flags */
-    bswaptls(&phdr->p_align);           /* Segment alignment */
+    int i;
+
+    for (i = 0; i < phnum; i++, phdr++) {
+        bswap32s(&phdr->p_type);        /* Segment type */
+        bswap32s(&phdr->p_flags);       /* Segment flags */
+        bswaptls(&phdr->p_offset);      /* Segment file offset */
+        bswaptls(&phdr->p_vaddr);       /* Segment virtual address */
+        bswaptls(&phdr->p_paddr);       /* Segment physical address */
+        bswaptls(&phdr->p_filesz);      /* Segment size in file */
+        bswaptls(&phdr->p_memsz);       /* Segment size in memory */
+        bswaptls(&phdr->p_align);       /* Segment alignment */
+    }
 }
 
-static void bswap_shdr(struct elf_shdr *shdr)
+static void bswap_shdr(struct elf_shdr *shdr, int shnum)
 {
-    bswap32s(&shdr->sh_name);
-    bswap32s(&shdr->sh_type);
-    bswaptls(&shdr->sh_flags);
-    bswaptls(&shdr->sh_addr);
-    bswaptls(&shdr->sh_offset);
-    bswaptls(&shdr->sh_size);
-    bswap32s(&shdr->sh_link);
-    bswap32s(&shdr->sh_info);
-    bswaptls(&shdr->sh_addralign);
-    bswaptls(&shdr->sh_entsize);
+    int i;
+
+    for (i = 0; i < shnum; i++, shdr++) {
+        bswap32s(&shdr->sh_name);
+        bswap32s(&shdr->sh_type);
+        bswaptls(&shdr->sh_flags);
+        bswaptls(&shdr->sh_addr);
+        bswaptls(&shdr->sh_offset);
+        bswaptls(&shdr->sh_size);
+        bswap32s(&shdr->sh_link);
+        bswap32s(&shdr->sh_info);
+        bswaptls(&shdr->sh_addralign);
+        bswaptls(&shdr->sh_entsize);
+    }
 }
 
 static void bswap_sym(struct elf_sym *sym)
@@ -121,7 +129,15 @@ static void bswap_sym(struct elf_sym *sym)
     bswaptls(&sym->st_size);
     bswap16s(&sym->st_shndx);
 }
-#endif
+
+#else /* ! BSWAP_NEEDED */
+
+static void bswap_ehdr(struct elfhdr *ehdr) { }
+static void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
+static void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
+static void bswap_sym(struct elf_sym *sym) { }
+
+#endif /* ! BSWAP_NEEDED */
 
 /*
  * 'copy_elf_strings()' copies argument/envelope strings from user
@@ -367,9 +383,7 @@ static abi_ulong load_elf_interp(struct elfhdr *interp_elf_ex,
     last_bss = 0;
     error = 0;
 
-#ifdef BSWAP_NEEDED
     bswap_ehdr(interp_elf_ex);
-#endif
     /* First of all, some simple consistency checks */
     if ((interp_elf_ex->e_type != ET_EXEC &&
          interp_elf_ex->e_type != ET_DYN) ||
@@ -410,12 +424,7 @@ static abi_ulong load_elf_interp(struct elfhdr *interp_elf_ex,
         free(elf_phdata);
         return retval;
     }
-#ifdef BSWAP_NEEDED
-    eppnt = elf_phdata;
-    for (i = 0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
-        bswap_phdr(eppnt);
-    }
-#endif
+    bswap_phdr(elf_phdata, interp_elf_ex->e_phnum);
 
     if (interp_elf_ex->e_type == ET_DYN) {
         /* in order to avoid hardcoding the interpreter load
@@ -560,9 +569,7 @@ static void load_symbols(struct elfhdr *hdr, int fd)
     for (i = 0; i < hdr->e_shnum; i++) {
         if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
             return;
-#ifdef BSWAP_NEEDED
-        bswap_shdr(&sechdr);
-#endif
+        bswap_shdr(&sechdr, 1);
         if (sechdr.sh_type == SHT_SYMTAB) {
             symtab = sechdr;
             lseek(fd, hdr->e_shoff
@@ -570,9 +577,7 @@ static void load_symbols(struct elfhdr *hdr, int fd)
             if (read(fd, &strtab, sizeof(strtab))
                 != sizeof(strtab))
                 return;
-#ifdef BSWAP_NEEDED
-            bswap_shdr(&strtab);
-#endif
+            bswap_shdr(&strtab, 1);
             goto found;
         }
     }
@@ -605,9 +610,7 @@ static void load_symbols(struct elfhdr *hdr, int fd)
 
     i = 0;
     while (i < nsyms) {
-#ifdef BSWAP_NEEDED
         bswap_sym(syms + i);
-#endif
         // Throw away entries which we do not need.
         if (syms[i].st_shndx == SHN_UNDEF ||
                 syms[i].st_shndx >= SHN_LORESERVE ||
@@ -679,9 +682,7 @@ int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
     load_addr = 0;
     load_bias = 0;
     elf_ex = *((struct elfhdr *) bprm->buf);          /* exec-header */
-#ifdef BSWAP_NEEDED
     bswap_ehdr(&elf_ex);
-#endif
 
     /* First of all, some simple consistency checks */
     if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
@@ -715,12 +716,8 @@ int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
         return -errno;
     }
 
-#ifdef BSWAP_NEEDED
-    elf_ppnt = elf_phdata;
-    for (i = 0; i < elf_ex.e_phnum; i++, elf_ppnt++) {
-        bswap_phdr(elf_ppnt);
-    }
-#endif
+    bswap_phdr(elf_phdata, elf_ex.e_phnum);
+
     elf_ppnt = elf_phdata;
 
     elf_bss = 0;
-- 
2.32.0



  parent reply	other threads:[~2021-08-26 21:30 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-26 21:11 [PATCH v2 00/43] bsd-user updates to run hello world imp
2021-08-26 21:11 ` [PATCH v2 01/43] bsd-user: remove sparc and sparc64 imp
2021-08-26 21:11 ` [PATCH v2 02/43] bsd-user: add copyright header to elfload.c imp
2021-08-26 21:11 ` [PATCH v2 03/43] bsd-user: Add Stacey's copyright to main.c imp
2021-08-26 21:11 ` [PATCH v2 04/43] bsd-user: add license to bsdload.c imp
2021-08-26 21:11 ` [PATCH v2 05/43] bsd-user: style nits: bsdload.c whitespace to qemu standard imp
2021-08-26 21:11 ` [PATCH v2 06/43] bsd-user: Remove all non-x86 code from elfload.c imp
2021-08-26 21:11 ` [PATCH v2 07/43] bsd-user: move arch specific defines out of elfload.c imp
2021-08-27  4:19   ` Philippe Mathieu-Daudé
2021-08-27 15:50     ` Warner Losh
2021-08-26 21:11 ` [PATCH v2 08/43] bsd-user: pass the bsd_param into loader_exec imp
2021-08-27  4:22   ` Philippe Mathieu-Daudé
2021-08-27 15:49     ` Warner Losh
2021-08-26 21:11 ` [PATCH v2 09/43] bsd-user: Fix calculation of size to allocate imp
2021-08-26 21:11 ` [PATCH v2 10/43] bsd-user: implement path searching imp
2021-08-26 21:11 ` [PATCH v2 11/43] bsd-user: Eliminate elf personality imp
2021-08-26 21:11 ` [PATCH v2 12/43] bsd-user: remove a.out support imp
2021-08-27  4:27   ` Philippe Mathieu-Daudé
2021-08-26 21:11 ` [PATCH v2 13/43] bsd-user: TARGET_NGROUPS unused in this file, remove imp
2021-08-26 21:11 ` imp [this message]
2021-08-26 21:11 ` [PATCH v2 15/43] bsd-user: assume pthreads and support of __thread imp
2021-08-27  4:35   ` Philippe Mathieu-Daudé
2021-08-26 21:11 ` [PATCH v2 16/43] bsd-user: add host-os.h imp
2021-08-26 21:11 ` [PATCH v2 17/43] bsd-user: Include host-os.h from main imp
2021-08-26 21:11 ` [PATCH v2 18/43] bsd-user: save the path to the qemu emulator imp
2021-08-26 21:11 ` [PATCH v2 19/43] bsd-user: start to move target CPU functions to target_arch* imp
2021-08-27  4:39   ` Philippe Mathieu-Daudé
2021-08-27 15:41     ` Warner Losh
2021-08-26 21:11 ` [PATCH v2 20/43] bsd-user: Move per-cpu code into target_arch_cpu.h imp
2021-08-26 21:11 ` [PATCH v2 21/43] bsd-user: pull in target_arch_thread.h update target_arch_elf.h imp
2021-08-26 21:11 ` [PATCH v2 22/43] bsd-user: Include more things in qemu.h imp
2021-08-26 21:11 ` [PATCH v2 23/43] bsd-user: define max args in terms of pages imp
2021-08-26 21:11 ` [PATCH v2 24/43] bsd-user: Create target specific vmparam.h imp
2021-08-26 21:11 ` [PATCH v2 25/43] bsd-user: Add architecture specific signal tramp code imp
2021-08-26 21:11 ` [PATCH v2 26/43] bsd-user: *BSD specific siginfo defintions imp
2021-08-26 21:11 ` [PATCH v2 27/43] bsd-user: Move stack initializtion into a per-os file imp
2021-08-26 21:11 ` [PATCH v2 28/43] bsd-user: Add system independent stack, data and text limiting imp
2021-08-26 21:11 ` [PATCH v2 29/43] bsd-user: elf cleanup imp
2021-08-26 21:11 ` [PATCH v2 30/43] bsd-user: Remove dead #ifdefs from elfload.c imp
2021-08-27  4:42   ` Philippe Mathieu-Daudé
2021-08-27 15:02     ` Warner Losh
2021-08-27 15:58       ` Philippe Mathieu-Daudé
2021-08-27 16:28         ` Warner Losh
2021-08-26 21:11 ` [PATCH v2 31/43] bsd-user: Rewrite target system call definintion glue imp
2021-08-26 21:11 ` [PATCH v2 32/43] bsd-user: Make cpu_model and cpu_type visible to all of main.c imp
2021-08-26 21:11 ` [PATCH v2 33/43] bsd-user: update debugging in mmap.c imp
2021-08-26 21:11 ` [PATCH v2 34/43] bsd-user: Add target_arch_reg to describe a target's register set imp
2021-08-26 21:11 ` [PATCH v2 35/43] bsd-user: Add target_os_user.h to capture the user/kernel structures imp
2021-08-26 21:11 ` [PATCH v2 36/43] bsd-user: add stubbed out core dump support imp
2021-08-26 21:11 ` [PATCH v2 37/43] bsd-user: elfload.c style catch up patch imp
2021-08-26 21:11 ` [PATCH v2 38/43] bsd-user: Refactor load_elf_sections and is_target_elf_binary imp
2021-08-27 21:23   ` Warner Losh
2021-08-26 21:11 ` [PATCH v2 39/43] bsd-user: move qemu_log to later in the file imp
2021-08-26 21:11 ` [PATCH v2 40/43] bsd-user: Implement interlock for atomic operations imp
2021-08-26 21:11 ` [PATCH v2 41/43] bsd-user: Implement cpu_copy() helper routine imp
2021-08-27  4:47   ` Philippe Mathieu-Daudé
2021-08-27 14:56     ` Warner Losh
2021-08-27 16:00       ` Philippe Mathieu-Daudé
2021-08-27 16:30         ` Warner Losh
2021-08-26 21:12 ` [PATCH v2 42/43] bsd-user: Add '-0 argv0' option to bsd-user/main.c imp
2021-08-27  4:48   ` Philippe Mathieu-Daudé
2021-08-27 14:52     ` Warner Losh
2021-08-26 21:12 ` [PATCH v2 43/43] bsd-user: Update mapping to handle reserved and starting conditions imp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210826211201.98877-15-imp@bsdimp.com \
    --to=imp@bsdimp.com \
    --cc=qemu-devel@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.