PERFORCE change 28254 for review
Peter Wemm
peter at FreeBSD.org
Sat Apr 5 13:39:09 PST 2003
http://perforce.freebsd.org/chv.cgi?CH=28254
Change 28254 by peter at peter_overcee on 2003/04/05 13:38:52
Checkpoint. Evil hacks to fit an elf64 loader into an i386 loader.
Not finished yet. Stolen from imgact_elf{,32,64}.c.
Affected files ...
.. //depot/projects/hammer/sys/boot/common/Makefile.inc#3 edit
.. //depot/projects/hammer/sys/boot/common/load_elf.c#4 edit
.. //depot/projects/hammer/sys/boot/common/load_elf32.c#1 add
.. //depot/projects/hammer/sys/boot/common/load_elf64.c#1 add
.. //depot/projects/hammer/sys/i386/include/elf.h#2 edit
Differences ...
==== //depot/projects/hammer/sys/boot/common/Makefile.inc#3 (text+ko) ====
@@ -1,9 +1,19 @@
# $FreeBSD: src/sys/boot/common/Makefile.inc,v 1.14 2002/08/29 02:02:27 peter Exp $
SRCS+= bcache.c boot.c commands.c console.c devopen.c interp.c
-SRCS+= interp_backslash.c interp_parse.c load_elf.c ls.c misc.c
+SRCS+= interp_backslash.c interp_parse.c ls.c misc.c
SRCS+= module.c panic.c
+.if ${MACHINE_ARCH} == "i386" || ${MACHINE_ARCH} == "powerpc"
+SRCS+= load_elf32.c load_elf64.c
+.endif
+.if ${MACHINE_ARCH} == "powerpc"
+SRCS+= load_elf32.c
+.endif
+.if ${MACHINE_ARCH} == "sparc64" || ${MACHINE_ARCH} == "ia64" || ${MACHINE_ARCH} == "alpha"
+SRCS+= load_elf32.c
+.endif
+
.if defined(LOADER_NET_SUPPORT)
SRCS+= dev_net.c
.endif
==== //depot/projects/hammer/sys/boot/common/load_elf.c#4 (text+ko) ====
@@ -62,17 +62,17 @@
vm_offset_t off;
} *elf_file_t;
-static int elf_loadimage(struct preloaded_file *mp, elf_file_t ef, vm_offset_t loadaddr);
-static int elf_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, vm_offset_t loadaddr);
+static int __elfN(lookup_symbol)(struct preloaded_file *mp, elf_file_t ef, const char* name, Elf_Sym* sym);
#ifdef __sparc__
-static void elf_reloc_ptr(struct preloaded_file *mp, elf_file_t ef,
+static void __elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
void *p, void *val, size_t len);
#endif
-static int elf_parse_modmetadata(struct preloaded_file *mp, elf_file_t ef);
+static int __elfN(parse_modmetadata)(struct preloaded_file *mp, elf_file_t ef);
static char *fake_modname(const char *name);
-const char *elf_kerneltype = "elf kernel";
-const char *elf_moduletype = "elf module";
+const char *__elfN(kerneltype) = __XSTRING(__ELF_WORD_SIZE) "kernel";
+const char *__elfN(moduletype) = __XSTRING(__ELF_WORD_SIZE) "module";
/*
* Attempt to load the file (file) as an ELF module. It will be stored at
@@ -80,7 +80,7 @@
* will be saved in (result).
*/
int
-elf_loadfile(char *filename, vm_offset_t dest, struct preloaded_file **result)
+__elfN(loadfile)(char *filename, vm_offset_t dest, struct preloaded_file **result)
{
struct preloaded_file *fp, *kfp;
struct elf_file ef;
@@ -134,12 +134,12 @@
if (ehdr->e_type == ET_DYN) {
/* Looks like a kld module */
if (kfp == NULL) {
- printf("elf_loadfile: can't load module before kernel\n");
+ printf(__XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module before kernel\n");
err = EPERM;
goto oerr;
}
- if (strcmp(elf_kerneltype, kfp->f_type)) {
- printf("elf_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
+ if (strcmp(__elfN(kerneltype), kfp->f_type)) {
+ printf(__XSTRING(__ELF_WORD_SIZE) "_loadfile: can't load module with kernel type '%s'\n", kfp->f_type);
err = EPERM;
goto oerr;
}
@@ -155,7 +155,7 @@
} else if (ehdr->e_type == ET_EXEC) {
/* Looks like a kernel */
if (kfp != NULL) {
- printf("elf_loadfile: kernel already loaded\n");
+ printf(__XSTRING(__ELF_WORD_SIZE) "_loadfile: kernel already loaded\n");
err = EPERM;
goto oerr;
}
@@ -164,7 +164,7 @@
*/
dest = (vm_offset_t) ehdr->e_entry;
if (dest == 0) {
- printf("elf_loadfile: not a kernel (maybe static binary?)\n");
+ printf(__XSTRING(__ELF_WORD_SIZE) "_loadfile: not a kernel (maybe static binary?)\n");
err = EPERM;
goto oerr;
}
@@ -180,14 +180,14 @@
*/
fp = file_alloc();
if (fp == NULL) {
- printf("elf_loadfile: cannot allocate module info\n");
+ printf(__XSTRING(__ELF_WORD_SIZE) "_loadfile: cannot allocate module info\n");
err = EPERM;
goto out;
}
if (ef.kernel)
setenv("kernelname", filename, 1);
fp->f_name = strdup(filename);
- fp->f_type = strdup(ef.kernel ? elf_kerneltype : elf_moduletype);
+ fp->f_type = strdup(ef.kernel ? __elfN(kerneltype) : __elfN(moduletype));
#ifdef ELF_VERBOSE
if (ef.kernel)
@@ -196,7 +196,7 @@
printf("%s ", filename);
#endif
- fp->f_size = elf_loadimage(fp, &ef, dest);
+ fp->f_size = __elfN(loadimage)(fp, &ef, dest);
if (fp->f_size == 0 || fp->f_addr == 0)
goto ioerr;
@@ -224,7 +224,7 @@
* the Elf header, load the image at (off)
*/
static int
-elf_loadimage(struct preloaded_file *fp, elf_file_t ef, vm_offset_t off)
+__elfN(loadimage)(struct preloaded_file *fp, elf_file_t ef, vm_offset_t off)
{
int i;
u_int j;
@@ -261,7 +261,7 @@
ef->off = off;
if ((ehdr->e_phoff + ehdr->e_phnum * sizeof(*phdr)) > ef->firstlen) {
- printf("elf_loadimage: program header not within first page\n");
+ printf(__XSTRING(__ELF_WORD_SIZE) "_loadimage: program header not within first page\n");
goto out;
}
phdr = (Elf_Phdr *)(ef->firstpage + ehdr->e_phoff);
@@ -295,12 +295,12 @@
if (phdr[i].p_filesz > fpcopy) {
if (lseek(ef->fd, (off_t)(phdr[i].p_offset + fpcopy),
SEEK_SET) == -1) {
- printf("\nelf_loadexec: cannot seek\n");
+ printf("\n" __XSTRING(__ELF_WORD_SIZE) "_loadexec: cannot seek\n");
goto out;
}
if (archsw.arch_readin(ef->fd, phdr[i].p_vaddr + off + fpcopy,
phdr[i].p_filesz - fpcopy) != (ssize_t)(phdr[i].p_filesz - fpcopy)) {
- printf("\nelf_loadexec: archsw.readin failed\n");
+ printf("\n" __XSTRING(__ELF_WORD_SIZE) "_loadexec: archsw.readin failed\n");
goto out;
}
}
@@ -315,7 +315,7 @@
/* no archsw.arch_bzero */
buf = malloc(PAGE_SIZE);
if (buf == NULL) {
- printf("\nelf_loadimage: malloc() failed\n");
+ printf("\n" __XSTRING(__ELF_WORD_SIZE) "_loadimage: malloc() failed\n");
goto out;
}
bzero(buf, PAGE_SIZE);
@@ -353,12 +353,12 @@
if (shdr == NULL)
goto nosyms;
if (lseek(ef->fd, (off_t)ehdr->e_shoff, SEEK_SET) == -1) {
- printf("\nelf_loadimage: cannot lseek() to section headers");
+ printf("\n" __XSTRING(__ELF_WORD_SIZE) "_loadimage: cannot lseek() to section headers");
goto nosyms;
}
result = read(ef->fd, shdr, chunk);
if (result < 0 || (size_t)result != chunk) {
- printf("\nelf_loadimage: read section headers failed");
+ printf("\n" __XSTRING(__ELF_WORD_SIZE) "_loadimage: read section headers failed");
goto nosyms;
}
symtabindex = -1;
@@ -423,14 +423,14 @@
#endif
if (lseek(ef->fd, (off_t)shdr[i].sh_offset, SEEK_SET) == -1) {
- printf("\nelf_loadimage: could not seek for symbols - skipped!");
+ printf("\n" __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_loadimage: could not read symbols - skipped!");
+ printf("\n" __XSTRING(__ELF_WORD_SIZE) "_loadimage: could not read symbols - skipped!");
lastaddr = ssym;
ssym = 0;
goto nosyms;
@@ -513,7 +513,7 @@
COPYOUT(ef->hashtab + 1, &ef->nchains, sizeof(ef->nchains));
ef->buckets = ef->hashtab + 2;
ef->chains = ef->buckets + ef->nbuckets;
- if (elf_parse_modmetadata(fp, ef) == 0)
+ if (__elfN(parse_modmetadata)(fp, ef) == 0)
goto out;
if (ef->kernel) /* kernel must not depend on anything */
@@ -559,7 +559,7 @@
}
int
-elf_parse_modmetadata(struct preloaded_file *fp, elf_file_t ef)
+__elfN(parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef)
{
struct mod_metadata md;
struct mod_depend *mdepend;
@@ -568,10 +568,10 @@
char *s, *v, **p, **p_stop;
int modcnt, minfolen;
- if (elf_lookup_symbol(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
+ if (__elfN(lookup_symbol)(fp, ef, "__start_set_modmetadata_set", &sym) != 0)
return ENOENT;
p = (char **)(sym.st_value + ef->off);
- if (elf_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_stop = (char **)(sym.st_value + ef->off);
@@ -579,13 +579,13 @@
while (p < p_stop) {
COPYOUT(p, &v, sizeof(v));
#ifdef __sparc64__
- elf_reloc_ptr(fp, ef, p, &v, sizeof(v));
+ __elfN(reloc_ptr)(fp, ef, p, &v, sizeof(v));
#else
v += ef->off;
#endif
COPYOUT(v, &md, sizeof(md));
#ifdef __sparc64__
- elf_reloc_ptr(fp, ef, v, &md, sizeof(md));
+ __elfN(reloc_ptr)(fp, ef, v, &md, sizeof(md));
#else
md.md_cval += ef->off;
md.md_data += ef->off;
@@ -639,9 +639,9 @@
return h;
}
-static const char elf_bad_symtable[] = "elf_lookup_symbol: corrupt symbol table\n";
+static const char __elfN(bad_symtable)[] = __XSTRING(__ELF_WORD_SIZE) "_lookup_symbol: corrupt symbol table\n";
int
-elf_lookup_symbol(struct preloaded_file *fp, elf_file_t ef, const char* name,
+__elfN(lookup_symbol)(struct preloaded_file *fp, elf_file_t ef, const char* name,
Elf_Sym *symp)
{
Elf_Hashelt symnum;
@@ -654,13 +654,13 @@
while (symnum != STN_UNDEF) {
if (symnum >= ef->nchains) {
- printf(elf_bad_symtable);
+ printf(__elfN(bad_symtable));
return ENOENT;
}
COPYOUT(ef->symtab + symnum, &sym, sizeof(sym));
if (sym.st_name == 0) {
- printf(elf_bad_symtable);
+ printf(__elfN(bad_symtable));
return ENOENT;
}
@@ -688,7 +688,7 @@
* the image in-place, because this is done by kern_linker later on.
*/
static void
-elf_reloc_ptr(struct preloaded_file *mp, elf_file_t ef,
+__elfN(reloc_ptr)(struct preloaded_file *mp, elf_file_t ef,
void *p, void *val, size_t len)
{
Elf_Addr off = (Elf_Addr)p - ef->off, word;
==== //depot/projects/hammer/sys/i386/include/elf.h#2 (text+ko) ====
@@ -34,8 +34,12 @@
*/
#include <sys/elf32.h> /* Definitions common to all 32 bit architectures. */
+#include <sys/elf64.h> /* Definitions common to all 64 bit architectures. */
+#ifndef __ELF_WORD_SIZE
#define __ELF_WORD_SIZE 32 /* Used by <sys/elf_generic.h> */
+#endif
+
#include <sys/elf_generic.h>
#define ELF_ARCH EM_386
@@ -58,6 +62,11 @@
} a_un;
} Elf32_Auxinfo;
+/* Fake for x86-64 loader support */
+typedef struct {
+ int fake;
+} Elf64_Auxinfo;
+
__ElfType(Auxinfo);
/* Values for a_type. */
More information about the p4-projects
mailing list