bsd-user: elfload: simplify bswap a bit.

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>
This commit is contained in:
Warner Losh 2021-08-02 22:59:12 -06:00
parent d8fcdad2d6
commit b62f790cfb

View File

@ -88,20 +88,27 @@ static void bswap_ehdr(struct elfhdr *ehdr)
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)
{ {
int i;
for (i = 0; i < phnum; i++, phdr++) {
bswap32s(&phdr->p_type); /* Segment type */ bswap32s(&phdr->p_type); /* Segment type */
bswap32s(&phdr->p_flags); /* Segment flags */
bswaptls(&phdr->p_offset); /* Segment file offset */ bswaptls(&phdr->p_offset); /* Segment file offset */
bswaptls(&phdr->p_vaddr); /* Segment virtual address */ bswaptls(&phdr->p_vaddr); /* Segment virtual address */
bswaptls(&phdr->p_paddr); /* Segment physical address */ bswaptls(&phdr->p_paddr); /* Segment physical address */
bswaptls(&phdr->p_filesz); /* Segment size in file */ bswaptls(&phdr->p_filesz); /* Segment size in file */
bswaptls(&phdr->p_memsz); /* Segment size in memory */ bswaptls(&phdr->p_memsz); /* Segment size in memory */
bswap32s(&phdr->p_flags); /* Segment flags */
bswaptls(&phdr->p_align); /* Segment alignment */ bswaptls(&phdr->p_align); /* Segment alignment */
} }
}
static void bswap_shdr(struct elf_shdr *shdr) static void bswap_shdr(struct elf_shdr *shdr, int shnum)
{ {
int i;
for (i = 0; i < shnum; i++, shdr++) {
bswap32s(&shdr->sh_name); bswap32s(&shdr->sh_name);
bswap32s(&shdr->sh_type); bswap32s(&shdr->sh_type);
bswaptls(&shdr->sh_flags); bswaptls(&shdr->sh_flags);
@ -113,6 +120,7 @@ static void bswap_shdr(struct elf_shdr *shdr)
bswaptls(&shdr->sh_addralign); bswaptls(&shdr->sh_addralign);
bswaptls(&shdr->sh_entsize); bswaptls(&shdr->sh_entsize);
} }
}
static void bswap_sym(struct elf_sym *sym) static void bswap_sym(struct elf_sym *sym)
{ {
@ -121,7 +129,15 @@ static void bswap_sym(struct elf_sym *sym)
bswaptls(&sym->st_size); bswaptls(&sym->st_size);
bswap16s(&sym->st_shndx); 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 * '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; last_bss = 0;
error = 0; error = 0;
#ifdef BSWAP_NEEDED
bswap_ehdr(interp_elf_ex); bswap_ehdr(interp_elf_ex);
#endif
/* First of all, some simple consistency checks */ /* First of all, some simple consistency checks */
if ((interp_elf_ex->e_type != ET_EXEC && if ((interp_elf_ex->e_type != ET_EXEC &&
interp_elf_ex->e_type != ET_DYN) || 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); free(elf_phdata);
return retval; return retval;
} }
#ifdef BSWAP_NEEDED bswap_phdr(elf_phdata, interp_elf_ex->e_phnum);
eppnt = elf_phdata;
for (i = 0; i<interp_elf_ex->e_phnum; i++, eppnt++) {
bswap_phdr(eppnt);
}
#endif
if (interp_elf_ex->e_type == ET_DYN) { if (interp_elf_ex->e_type == ET_DYN) {
/* in order to avoid hardcoding the interpreter load /* 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++) { for (i = 0; i < hdr->e_shnum; i++) {
if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr)) if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr))
return; return;
#ifdef BSWAP_NEEDED bswap_shdr(&sechdr, 1);
bswap_shdr(&sechdr);
#endif
if (sechdr.sh_type == SHT_SYMTAB) { if (sechdr.sh_type == SHT_SYMTAB) {
symtab = sechdr; symtab = sechdr;
lseek(fd, hdr->e_shoff lseek(fd, hdr->e_shoff
@ -570,9 +577,7 @@ static void load_symbols(struct elfhdr *hdr, int fd)
if (read(fd, &strtab, sizeof(strtab)) if (read(fd, &strtab, sizeof(strtab))
!= sizeof(strtab)) != sizeof(strtab))
return; return;
#ifdef BSWAP_NEEDED bswap_shdr(&strtab, 1);
bswap_shdr(&strtab);
#endif
goto found; goto found;
} }
} }
@ -605,9 +610,7 @@ static void load_symbols(struct elfhdr *hdr, int fd)
i = 0; i = 0;
while (i < nsyms) { while (i < nsyms) {
#ifdef BSWAP_NEEDED
bswap_sym(syms + i); bswap_sym(syms + i);
#endif
// Throw away entries which we do not need. // Throw away entries which we do not need.
if (syms[i].st_shndx == SHN_UNDEF || if (syms[i].st_shndx == SHN_UNDEF ||
syms[i].st_shndx >= SHN_LORESERVE || 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_addr = 0;
load_bias = 0; load_bias = 0;
elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */ elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
#ifdef BSWAP_NEEDED
bswap_ehdr(&elf_ex); bswap_ehdr(&elf_ex);
#endif
/* First of all, some simple consistency checks */ /* First of all, some simple consistency checks */
if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) || 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; return -errno;
} }
#ifdef BSWAP_NEEDED bswap_phdr(elf_phdata, elf_ex.e_phnum);
elf_ppnt = elf_phdata;
for (i = 0; i < elf_ex.e_phnum; i++, elf_ppnt++) {
bswap_phdr(elf_ppnt);
}
#endif
elf_ppnt = elf_phdata; elf_ppnt = elf_phdata;
elf_bss = 0; elf_bss = 0;