mirror of
https://git.FreeBSD.org/src.git
synced 2025-01-27 16:39:08 +00:00
load_elf.c: Use consistent indentation
As noted in D14267 load_elf.c has a variety of indentation styles. Move to standard 8 column hard tab indents, 4 space second level indents. Also includes some whitespace cleanups found by clang-format.
This commit is contained in:
parent
9ec35e3cb4
commit
6b6324985a
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=329745
@ -73,8 +73,10 @@ typedef struct elf_file {
|
||||
u_int64_t off;
|
||||
} *elf_file_t;
|
||||
|
||||
static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef, u_int64_t loadaddr);
|
||||
static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
|
||||
static int __elfN(loadimage)(struct preloaded_file *mp, elf_file_t ef,
|
||||
u_int64_t loadaddr);
|
||||
static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef,
|
||||
const char* name, Elf_Sym* sym);
|
||||
static int __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
|
||||
Elf_Addr p, void *val, size_t len);
|
||||
static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef,
|
||||
@ -237,7 +239,8 @@ __elfN(load_elf_header)(char *filename, elf_file_t ef)
|
||||
if (err)
|
||||
goto error;
|
||||
|
||||
if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) { /* Machine ? */
|
||||
if (ehdr->e_version != EV_CURRENT || ehdr->e_machine != ELF_TARG_MACH) {
|
||||
/* Machine ? */
|
||||
err = EFTYPE;
|
||||
goto error;
|
||||
}
|
||||
@ -301,24 +304,26 @@ __elfN(loadfile_raw)(char *filename, u_int64_t dest,
|
||||
if (ef.kernel || ehdr->e_type == ET_EXEC) {
|
||||
/* Looks like a kernel */
|
||||
if (kfp != NULL) {
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadfile: kernel already loaded\n");
|
||||
err = EPERM;
|
||||
goto oerr;
|
||||
}
|
||||
/*
|
||||
* Calculate destination address based on kernel entrypoint.
|
||||
*
|
||||
* For ARM, the destination address is independent of any values in the
|
||||
* elf header (an ARM kernel can be loaded at any 2MB boundary), so we
|
||||
* leave dest set to the value calculated by archsw.arch_loadaddr() and
|
||||
* passed in to this function.
|
||||
* For ARM, the destination address is independent of any values
|
||||
* in the elf header (an ARM kernel can be loaded at any 2MB
|
||||
* boundary), so we leave dest set to the value calculated by
|
||||
* archsw.arch_loadaddr() and passed in to this function.
|
||||
*/
|
||||
#ifndef __arm__
|
||||
if (ehdr->e_type == ET_EXEC)
|
||||
dest = (ehdr->e_entry & ~PAGE_MASK);
|
||||
#endif
|
||||
if ((ehdr->e_entry & ~PAGE_MASK) == 0) {
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadfile: not a kernel (maybe static binary?)\n");
|
||||
err = EPERM;
|
||||
goto oerr;
|
||||
}
|
||||
@ -327,17 +332,21 @@ __elfN(loadfile_raw)(char *filename, u_int64_t dest,
|
||||
} else if (ehdr->e_type == ET_DYN) {
|
||||
/* Looks like a kld module */
|
||||
if (multiboot != 0) {
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module as multiboot\n");
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadfile: can't load module as multiboot\n");
|
||||
err = EPERM;
|
||||
goto oerr;
|
||||
}
|
||||
if (kfp == NULL) {
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadfile: can't load module before kernel\n");
|
||||
err = EPERM;
|
||||
goto oerr;
|
||||
}
|
||||
if (strcmp(__elfN(kerneltype), kfp->f_type)) {
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadfile: can't load module with kernel type '%s'\n",
|
||||
kfp->f_type);
|
||||
err = EPERM;
|
||||
goto oerr;
|
||||
}
|
||||
@ -359,7 +368,8 @@ __elfN(loadfile_raw)(char *filename, u_int64_t dest,
|
||||
*/
|
||||
fp = file_alloc();
|
||||
if (fp == NULL) {
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadfile: cannot allocate module info\n");
|
||||
err = EPERM;
|
||||
goto out;
|
||||
}
|
||||
@ -374,7 +384,8 @@ __elfN(loadfile_raw)(char *filename, u_int64_t dest,
|
||||
|
||||
#ifdef ELF_VERBOSE
|
||||
if (ef.kernel)
|
||||
printf("%s entry at 0x%jx\n", filename, (uintmax_t)ehdr->e_entry);
|
||||
printf("%s entry at 0x%jx\n", filename,
|
||||
(uintmax_t)ehdr->e_entry);
|
||||
#else
|
||||
printf("%s ", filename);
|
||||
#endif
|
||||
@ -441,25 +452,27 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
if (ehdr->e_type == ET_EXEC) {
|
||||
#if defined(__i386__) || defined(__amd64__)
|
||||
#if __ELF_WORD_SIZE == 64
|
||||
off = - (off & 0xffffffffff000000ull);/* x86_64 relocates after locore */
|
||||
/* x86_64 relocates after locore */
|
||||
off = - (off & 0xffffffffff000000ull);
|
||||
#else
|
||||
off = - (off & 0xff000000u); /* i386 relocates after locore */
|
||||
/* i386 relocates after locore */
|
||||
off = - (off & 0xff000000u);
|
||||
#endif
|
||||
#elif defined(__powerpc__)
|
||||
/*
|
||||
* On the purely virtual memory machines like e500, the kernel is
|
||||
* linked against its final VA range, which is most often not
|
||||
* available at the loader stage, but only after kernel initializes
|
||||
* and completes its VM settings. In such cases we cannot use p_vaddr
|
||||
* field directly to load ELF segments, but put them at some
|
||||
* 'load-time' locations.
|
||||
* On the purely virtual memory machines like e500, the kernel
|
||||
* is linked against its final VA range, which is most often
|
||||
* not available at the loader stage, but only after kernel
|
||||
* initializes and completes its VM settings. In such cases we
|
||||
* cannot use p_vaddr field directly to load ELF segments, but
|
||||
* put them at some 'load-time' locations.
|
||||
*/
|
||||
if (off & 0xf0000000u) {
|
||||
off = -(off & 0xf0000000u);
|
||||
/*
|
||||
* XXX the physical load address should not be hardcoded. Note
|
||||
* that the Book-E kernel assumes that it's loaded at a 16MB
|
||||
* boundary for now...
|
||||
* XXX the physical load address should not be
|
||||
* hardcoded. Note that the Book-E kernel assumes that
|
||||
* it's loaded at a 16MB boundary for now...
|
||||
*/
|
||||
off += 0x01000000;
|
||||
ehdr->e_entry += off;
|
||||
@ -470,25 +483,28 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
off = 0;
|
||||
#elif defined(__arm__) && !defined(EFI)
|
||||
/*
|
||||
* The elf headers in arm kernels specify virtual addresses in all
|
||||
* header fields, even the ones that should be physical addresses.
|
||||
* We assume the entry point is in the first page, and masking the page
|
||||
* offset will leave us with the virtual address the kernel was linked
|
||||
* at. We subtract that from the load offset, making 'off' into the
|
||||
* value which, when added to a virtual address in an elf header,
|
||||
* translates it to a physical address. We do the va->pa conversion on
|
||||
* the entry point address in the header now, so that later we can
|
||||
* launch the kernel by just jumping to that address.
|
||||
* The elf headers in arm kernels specify virtual addresses in
|
||||
* all header fields, even the ones that should be physical
|
||||
* addresses. We assume the entry point is in the first page,
|
||||
* and masking the page offset will leave us with the virtual
|
||||
* address the kernel was linked at. We subtract that from the
|
||||
* load offset, making 'off' into the value which, when added
|
||||
* to a virtual address in an elf header, translates it to a
|
||||
* physical address. We do the va->pa conversion on the entry
|
||||
* point address in the header now, so that later we can launch
|
||||
* the kernel by just jumping to that address.
|
||||
*
|
||||
* When booting from UEFI the copyin and copyout functions handle
|
||||
* adjusting the location relative to the first virtual address.
|
||||
* Because of this there is no need to adjust the offset or entry
|
||||
* point address as these will both be handled by the efi code.
|
||||
* When booting from UEFI the copyin and copyout functions
|
||||
* handle adjusting the location relative to the first virtual
|
||||
* address. Because of this there is no need to adjust the
|
||||
* offset or entry point address as these will both be handled
|
||||
* by the efi code.
|
||||
*/
|
||||
off -= ehdr->e_entry & ~PAGE_MASK;
|
||||
ehdr->e_entry += off;
|
||||
#ifdef ELF_VERBOSE
|
||||
printf("ehdr->e_entry 0x%08x, va<->pa off %llx\n", ehdr->e_entry, off);
|
||||
printf("ehdr->e_entry 0x%08x, va<->pa off %llx\n",
|
||||
ehdr->e_entry, off);
|
||||
#endif
|
||||
#else
|
||||
off = 0; /* other archs use direct mapped kernels */
|
||||
@ -500,7 +516,8 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
__elfN(relocation_offset) = off;
|
||||
|
||||
if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
|
||||
printf("elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadimage: program header not within first page\n");
|
||||
goto out;
|
||||
}
|
||||
phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
|
||||
@ -524,7 +541,8 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
} else {
|
||||
printf("data=0x%lx", (long)phdr[i].p_filesz);
|
||||
if (phdr[i].p_filesz < phdr[i].p_memsz)
|
||||
printf("+0x%lx", (long)(phdr[i].p_memsz -phdr[i].p_filesz));
|
||||
printf("+0x%lx", (long)(phdr[i].p_memsz -
|
||||
phdr[i].p_filesz));
|
||||
printf(" ");
|
||||
}
|
||||
#endif
|
||||
@ -536,7 +554,8 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
}
|
||||
if (phdr[i].p_filesz > fpcopy) {
|
||||
if (kern_pread(ef->fd, phdr[i].p_vaddr + off + fpcopy,
|
||||
phdr[i].p_filesz - fpcopy, phdr[i].p_offset + fpcopy) != 0) {
|
||||
phdr[i].p_filesz - fpcopy,
|
||||
phdr[i].p_offset + fpcopy) != 0) {
|
||||
printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadimage: read failed\n");
|
||||
goto out;
|
||||
@ -562,7 +581,8 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
|
||||
if (firstaddr == 0 || firstaddr > (phdr[i].p_vaddr + off))
|
||||
firstaddr = phdr[i].p_vaddr + off;
|
||||
if (lastaddr == 0 || lastaddr < (phdr[i].p_vaddr + off + phdr[i].p_memsz))
|
||||
if (lastaddr == 0 || lastaddr <
|
||||
(phdr[i].p_vaddr + off + phdr[i].p_memsz))
|
||||
lastaddr = phdr[i].p_vaddr + off + phdr[i].p_memsz;
|
||||
}
|
||||
lastaddr = roundup(lastaddr, sizeof(long));
|
||||
@ -596,17 +616,19 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
*/
|
||||
chunk = shdr[ehdr->e_shstrndx].sh_size;
|
||||
if (chunk) {
|
||||
shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset, chunk);
|
||||
shstr = alloc_pread(ef->fd, shdr[ehdr->e_shstrndx].sh_offset,
|
||||
chunk);
|
||||
if (shstr) {
|
||||
for (i = 0; i < ehdr->e_shnum; i++) {
|
||||
if (strcmp(shstr + shdr[i].sh_name, ".ctors") != 0)
|
||||
if (strcmp(shstr + shdr[i].sh_name,
|
||||
".ctors") != 0)
|
||||
continue;
|
||||
ctors = shdr[i].sh_addr;
|
||||
file_addmetadata(fp, MODINFOMD_CTORS_ADDR, sizeof(ctors),
|
||||
&ctors);
|
||||
file_addmetadata(fp, MODINFOMD_CTORS_ADDR,
|
||||
sizeof(ctors), &ctors);
|
||||
size = shdr[i].sh_size;
|
||||
file_addmetadata(fp, MODINFOMD_CTORS_SIZE, sizeof(size),
|
||||
&size);
|
||||
file_addmetadata(fp, MODINFOMD_CTORS_SIZE,
|
||||
sizeof(size), &size);
|
||||
break;
|
||||
}
|
||||
free(shstr);
|
||||
@ -677,7 +699,8 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
#ifdef ELF_VERBOSE
|
||||
printf("\n%s: 0x%jx@0x%jx -> 0x%jx-0x%jx", secname,
|
||||
(uintmax_t)shdr[i].sh_size, (uintmax_t)shdr[i].sh_offset,
|
||||
(uintmax_t)lastaddr, (uintmax_t)(lastaddr + shdr[i].sh_size));
|
||||
(uintmax_t)lastaddr,
|
||||
(uintmax_t)(lastaddr + shdr[i].sh_size));
|
||||
#else
|
||||
if (i == symstrindex)
|
||||
printf("+");
|
||||
@ -685,14 +708,17 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
#endif
|
||||
|
||||
if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
|
||||
printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not seek for symbols - skipped!");
|
||||
printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadimage: could not seek for symbols - skipped!");
|
||||
lastaddr = ssym;
|
||||
ssym = 0;
|
||||
goto nosyms;
|
||||
}
|
||||
result = archsw.arch_readin(ef->fd, lastaddr, shdr[i].sh_size);
|
||||
if (result < 0 || (size_t)result != shdr[i].sh_size) {
|
||||
printf("\nelf" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped! (%ju != %ju)", (uintmax_t)result,
|
||||
printf("\nelf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_loadimage: could not read symbols - skipped! "
|
||||
"(%ju != %ju)", (uintmax_t)result,
|
||||
(uintmax_t)shdr[i].sh_size);
|
||||
lastaddr = ssym;
|
||||
ssym = 0;
|
||||
@ -736,7 +762,8 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
if (phdr[i].p_type == PT_DYNAMIC) {
|
||||
php = phdr + i;
|
||||
adp = php->p_vaddr;
|
||||
file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp), &adp);
|
||||
file_addmetadata(fp, MODINFOMD_DYNAMIC, sizeof(adp),
|
||||
&adp);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -758,25 +785,30 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
break;
|
||||
switch (dp[i].d_tag) {
|
||||
case DT_HASH:
|
||||
ef->hashtab = (Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
ef->hashtab =
|
||||
(Elf_Hashelt*)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
break;
|
||||
case DT_STRTAB:
|
||||
ef->strtab = (char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
ef->strtab =
|
||||
(char *)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
break;
|
||||
case DT_STRSZ:
|
||||
ef->strsz = dp[i].d_un.d_val;
|
||||
break;
|
||||
case DT_SYMTAB:
|
||||
ef->symtab = (Elf_Sym*)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
ef->symtab =
|
||||
(Elf_Sym *)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
break;
|
||||
case DT_REL:
|
||||
ef->rel = (Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
ef->rel =
|
||||
(Elf_Rel *)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
break;
|
||||
case DT_RELSZ:
|
||||
ef->relsz = dp[i].d_un.d_val;
|
||||
break;
|
||||
case DT_RELA:
|
||||
ef->rela = (Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
ef->rela =
|
||||
(Elf_Rela *)(uintptr_t)(dp[i].d_un.d_ptr + off);
|
||||
break;
|
||||
case DT_RELASZ:
|
||||
ef->relasz = dp[i].d_un.d_val;
|
||||
@ -793,10 +825,12 @@ __elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off)
|
||||
ef->buckets = ef->hashtab + 2;
|
||||
ef->chains = ef->buckets + ef->nbuckets;
|
||||
|
||||
if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
|
||||
if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set",
|
||||
&sym) != 0)
|
||||
return 0;
|
||||
p_start = sym.st_value + ef->off;
|
||||
if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set", &sym) != 0)
|
||||
if (__elfN(lookup_symbol)(fp, ef, "__stop_set_modmetadata_set",
|
||||
&sym) != 0)
|
||||
return ENOENT;
|
||||
p_end = sym.st_value + ef->off;
|
||||
|
||||
@ -1031,7 +1065,8 @@ __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
|
||||
error = __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
|
||||
if (error == EOPNOTSUPP) {
|
||||
md.md_cval += ef->off;
|
||||
md.md_data = (void *)((uintptr_t)md.md_data + (uintptr_t)ef->off);
|
||||
md.md_data = (void *)((uintptr_t)md.md_data +
|
||||
(uintptr_t)ef->off);
|
||||
} else if (error != 0)
|
||||
return (error);
|
||||
#endif
|
||||
@ -1045,10 +1080,12 @@ __elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef,
|
||||
mdepend = malloc(minfolen);
|
||||
if (mdepend == NULL)
|
||||
return ENOMEM;
|
||||
COPYOUT((vm_offset_t)md.md_data, mdepend, sizeof(*mdepend));
|
||||
COPYOUT((vm_offset_t)md.md_data, mdepend,
|
||||
sizeof(*mdepend));
|
||||
strcpy((char*)(mdepend + 1), s);
|
||||
free(s);
|
||||
file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, mdepend);
|
||||
file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen,
|
||||
mdepend);
|
||||
free(mdepend);
|
||||
break;
|
||||
case MDT_VERSION:
|
||||
@ -1084,10 +1121,11 @@ elf_hash(const char *name)
|
||||
return h;
|
||||
}
|
||||
|
||||
static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
|
||||
static const char __elfN(bad_symtable)[] = "elf" __XSTRING(__ELF_WORD_SIZE)
|
||||
"_lookup_symbol: corrupt symbol table\n";
|
||||
int
|
||||
__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
|
||||
Elf_Sym *symp)
|
||||
__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef,
|
||||
const char* name, Elf_Sym *symp)
|
||||
{
|
||||
Elf_Hashelt symnum;
|
||||
Elf_Sym sym;
|
||||
|
Loading…
Reference in New Issue
Block a user