mirror of
https://github.com/qemu/qemu.git
synced 2024-11-25 03:43:37 +08:00
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:
parent
d8fcdad2d6
commit
b62f790cfb
@ -88,20 +88,27 @@ static void bswap_ehdr(struct elfhdr *ehdr)
|
||||
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_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 */
|
||||
bswap32s(&phdr->p_flags); /* Segment flags */
|
||||
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_type);
|
||||
bswaptls(&shdr->sh_flags);
|
||||
@ -113,6 +120,7 @@ static void bswap_shdr(struct elf_shdr *shdr)
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user