Skip to content

Commit

Permalink
bsd-user: elfload: simplify bswap a bit.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bsdimp committed Sep 10, 2021
1 parent d8fcdad commit b62f790
Showing 1 changed file with 47 additions and 50 deletions.
97 changes: 47 additions & 50 deletions bsd-user/elfload.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,51 +67,59 @@

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 */
bswaptls(&ehdr->e_phoff); /* Program header table file offset */
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)
Expand All @@ -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
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -560,19 +569,15 @@ 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
+ sizeof(sechdr) * sechdr.sh_link, SEEK_SET);
if (read(fd, &strtab, sizeof(strtab))
!= sizeof(strtab))
return;
#ifdef BSWAP_NEEDED
bswap_shdr(&strtab);
#endif
bswap_shdr(&strtab, 1);
goto found;
}
}
Expand Down Expand Up @@ -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 ||
Expand Down Expand Up @@ -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) ||
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b62f790

Please sign in to comment.