All of lore.kernel.org
 help / color / mirror / Atom feed
From: Warner Losh <imp@bsdimp.com>
To: qemu-devel@nongnu.org
Cc: kevans@freebsd.org, Warner Losh <imp@bsdimp.com>
Subject: [PATCH for 6.2 16/49] bsd-user: elfload: simplify bswap a bit.
Date: Sat,  7 Aug 2021 15:42:09 -0600	[thread overview]
Message-ID: <20210807214242.82385-17-imp@bsdimp.com> (raw)
In-Reply-To: <20210807214242.82385-1-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>
---
 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-07 21:51 UTC|newest]

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

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=20210807214242.82385-17-imp@bsdimp.com \
    --to=imp@bsdimp.com \
    --cc=kevans@freebsd.org \
    --cc=qemu-devel@nongnu.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.