diff options
-rw-r--r-- | sys/arch/amd64/include/loadfile_machdep.h | 6 | ||||
-rw-r--r-- | sys/arch/amd64/stand/boot/Makefile | 5 | ||||
-rw-r--r-- | sys/arch/amd64/stand/boot/conf.c | 5 | ||||
-rw-r--r-- | sys/arch/amd64/stand/cdboot/Makefile | 5 | ||||
-rw-r--r-- | sys/arch/amd64/stand/cdboot/conf.c | 4 | ||||
-rw-r--r-- | sys/arch/amd64/stand/libsa/elf32.c | 32 | ||||
-rw-r--r-- | sys/arch/amd64/stand/libsa/elf64.c | 32 | ||||
-rw-r--r-- | sys/arch/amd64/stand/pxeboot/Makefile | 5 | ||||
-rw-r--r-- | sys/arch/amd64/stand/pxeboot/conf.c | 5 | ||||
-rw-r--r-- | sys/arch/i386/stand/boot/conf.c | 5 | ||||
-rw-r--r-- | sys/arch/i386/stand/cdboot/conf.c | 4 | ||||
-rw-r--r-- | sys/arch/i386/stand/libsa/Makefile | 4 | ||||
-rw-r--r-- | sys/arch/i386/stand/libsa/elf32.c | 32 | ||||
-rw-r--r-- | sys/arch/i386/stand/libsa/elf64.c | 32 | ||||
-rw-r--r-- | sys/arch/i386/stand/pxeboot/conf.c | 5 | ||||
-rw-r--r-- | sys/lib/libsa/loadfile.c | 218 | ||||
-rw-r--r-- | sys/lib/libsa/loadfile_elf.c | 250 |
17 files changed, 436 insertions, 213 deletions
diff --git a/sys/arch/amd64/include/loadfile_machdep.h b/sys/arch/amd64/include/loadfile_machdep.h index 7de50b522ee..e41c0ba09bd 100644 --- a/sys/arch/amd64/include/loadfile_machdep.h +++ b/sys/arch/amd64/include/loadfile_machdep.h @@ -1,5 +1,5 @@ /* XXX - DSR */ -/* $OpenBSD: loadfile_machdep.h,v 1.1 2004/01/28 01:39:39 mickey Exp $ */ +/* $OpenBSD: loadfile_machdep.h,v 1.2 2007/05/30 01:25:43 tom Exp $ */ /* $NetBSD: loadfile_machdep.h,v 1.1 1999/04/29 03:17:12 tsubai Exp $ */ /*- @@ -39,6 +39,10 @@ */ #define BOOT_ELF +#define BOOT_ELF32 +#define BOOT_ELF64 + +/* Keep a default ELFSIZE */ #define ELFSIZE 64 #define LOAD_KERNEL (LOAD_ALL & ~LOAD_TEXTA) diff --git a/sys/arch/amd64/stand/boot/Makefile b/sys/arch/amd64/stand/boot/Makefile index 43519da62fb..5bbae80bbe7 100644 --- a/sys/arch/amd64/stand/boot/Makefile +++ b/sys/arch/amd64/stand/boot/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.6 2006/10/30 18:28:57 jmc Exp $ +# $OpenBSD: Makefile,v 1.7 2007/05/30 01:25:43 tom Exp $ .include "${.CURDIR}/../Makefile.inc" @@ -33,7 +33,8 @@ SRCS+= alloc.c exit.c getfile.c gets.c globals.c strcmp.c strlen.c \ strerror.c strncpy.c strtol.c strtoll.c ctime.c strlcpy.c strlcat.c # io routines SRCS+= close.c closeall.c dev.c disklabel.c dkcksum.c fstat.c ioctl.c lseek.c \ - open.c read.c stat.c write.c cread.c readdir.c cons.c loadfile.c + open.c read.c stat.c write.c cread.c readdir.c cons.c loadfile.c \ + elf32.c elf64.c # boot filesystems SRCS+= ufs.c # gcc support diff --git a/sys/arch/amd64/stand/boot/conf.c b/sys/arch/amd64/stand/boot/conf.c index c7af2054447..145f7b4d705 100644 --- a/sys/arch/amd64/stand/boot/conf.c +++ b/sys/arch/amd64/stand/boot/conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: conf.c,v 1.11 2007/04/28 19:23:10 deraadt Exp $ */ +/* $OpenBSD: conf.c,v 1.12 2007/05/30 01:25:43 tom Exp $ */ /* * Copyright (c) 1996 Michael Shalayeff @@ -24,7 +24,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * */ #include <sys/types.h> @@ -42,7 +41,7 @@ #include <biosdev.h> #include <dev/cons.h> -const char version[] = "2.15"; +const char version[] = "3.00"; int debug = 1; diff --git a/sys/arch/amd64/stand/cdboot/Makefile b/sys/arch/amd64/stand/cdboot/Makefile index 98f06fdb5bc..21469c17ed6 100644 --- a/sys/arch/amd64/stand/cdboot/Makefile +++ b/sys/arch/amd64/stand/cdboot/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.9 2007/05/29 08:16:54 deraadt Exp $ +# $OpenBSD: Makefile,v 1.10 2007/05/30 01:25:43 tom Exp $ .include "${.CURDIR}/../Makefile.inc" @@ -35,7 +35,8 @@ SRCS+= alloc.c exit.c strcmp.c strlen.c \ # io routines # not required: ioctl.c write.c SRCS+= close.c closeall.c dev.c disklabel.c dkcksum.c fstat.c lseek.c \ - open.c read.c stat.c cread.c readdir.c cons.c loadfile.c + open.c read.c stat.c cread.c readdir.c cons.c loadfile.c \ + elf32.c elf64.c # boot filesystems SRCS+= ufs.c cd9660.c # gcc support diff --git a/sys/arch/amd64/stand/cdboot/conf.c b/sys/arch/amd64/stand/cdboot/conf.c index 0bddf2cb83e..7172579a38d 100644 --- a/sys/arch/amd64/stand/cdboot/conf.c +++ b/sys/arch/amd64/stand/cdboot/conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: conf.c,v 1.11 2007/05/27 18:38:33 tom Exp $ */ +/* $OpenBSD: conf.c,v 1.12 2007/05/30 01:25:43 tom Exp $ */ /* * Copyright (c) 2004 Tom Cosgrove @@ -42,7 +42,7 @@ #include <biosdev.h> #include <dev/cons.h> -const char version[] = "1.10"; +const char version[] = "2.00"; int debug = 1; diff --git a/sys/arch/amd64/stand/libsa/elf32.c b/sys/arch/amd64/stand/libsa/elf32.c new file mode 100644 index 00000000000..f943067411c --- /dev/null +++ b/sys/arch/amd64/stand/libsa/elf32.c @@ -0,0 +1,32 @@ +/* $OpenBSD: elf32.c,v 1.1 2007/05/30 01:25:43 tom Exp $ */ + +/* + * Copyright (c) 2007 Tom Cosgrove <tom@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <lib/libkern/libkern.h> +#include <lib/libsa/stand.h> + +#include <sys/param.h> +#include <sys/exec.h> + +#include "../../../../lib/libsa/loadfile.h" + +#undef ELFSIZE +#define ELFSIZE 32 + +#include <sys/exec_elf.h> + +#include "../../../../lib/libsa/loadfile_elf.c" diff --git a/sys/arch/amd64/stand/libsa/elf64.c b/sys/arch/amd64/stand/libsa/elf64.c new file mode 100644 index 00000000000..d7d11c843c4 --- /dev/null +++ b/sys/arch/amd64/stand/libsa/elf64.c @@ -0,0 +1,32 @@ +/* $OpenBSD: elf64.c,v 1.1 2007/05/30 01:25:43 tom Exp $ */ + +/* + * Copyright (c) 2007 Tom Cosgrove <tom@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <lib/libkern/libkern.h> +#include <lib/libsa/stand.h> + +#include <sys/param.h> +#include <sys/exec.h> + +#include "../../../../lib/libsa/loadfile.h" + +#undef ELFSIZE +#define ELFSIZE 64 + +#include <sys/exec_elf.h> + +#include "../../../../lib/libsa/loadfile_elf.c" diff --git a/sys/arch/amd64/stand/pxeboot/Makefile b/sys/arch/amd64/stand/pxeboot/Makefile index e200e1b2138..801ff654c3e 100644 --- a/sys/arch/amd64/stand/pxeboot/Makefile +++ b/sys/arch/amd64/stand/pxeboot/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.6 2007/05/27 18:38:33 tom Exp $ +# $OpenBSD: Makefile,v 1.7 2007/05/30 01:25:43 tom Exp $ .include "${.CURDIR}/../Makefile.inc" @@ -34,7 +34,8 @@ SRCS+= alloc.c exit.c getfile.c gets.c globals.c strcmp.c strlen.c \ strerror.c strncpy.c strtol.c strtoll.c ctime.c strlcpy.c strlcat.c # io routines SRCS+= close.c closeall.c dev.c disklabel.c dkcksum.c fstat.c ioctl.c lseek.c \ - read.c stat.c write.c cread.c readdir.c cons.c loadfile.c + read.c stat.c write.c cread.c readdir.c cons.c loadfile.c \ + elf32.c elf64.c # network routines SRCS+= ether.c netif.c rpc.c # network info services diff --git a/sys/arch/amd64/stand/pxeboot/conf.c b/sys/arch/amd64/stand/pxeboot/conf.c index 5ef3af4a59e..78f17a4ef37 100644 --- a/sys/arch/amd64/stand/pxeboot/conf.c +++ b/sys/arch/amd64/stand/pxeboot/conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: conf.c,v 1.14 2007/05/27 18:38:33 tom Exp $ */ +/* $OpenBSD: conf.c,v 1.15 2007/05/30 01:25:43 tom Exp $ */ /* * Copyright (c) 2004 Tom Cosgrove @@ -25,7 +25,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * */ #include <sys/types.h> @@ -45,7 +44,7 @@ #include "pxeboot.h" #include "pxe_net.h" -const char version[] = "1.13"; +const char version[] = "2.00"; int debug = 0; #undef _TEST diff --git a/sys/arch/i386/stand/boot/conf.c b/sys/arch/i386/stand/boot/conf.c index 0539fc234f0..dd9eccdbf8a 100644 --- a/sys/arch/i386/stand/boot/conf.c +++ b/sys/arch/i386/stand/boot/conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: conf.c,v 1.36 2007/04/27 10:08:34 tom Exp $ */ +/* $OpenBSD: conf.c,v 1.37 2007/05/30 01:25:43 tom Exp $ */ /* * Copyright (c) 1996 Michael Shalayeff @@ -24,7 +24,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * */ #include <sys/types.h> @@ -43,7 +42,7 @@ #include <dev/cons.h> #include "debug.h" -const char version[] = "2.14"; +const char version[] = "3.00"; int debug = 1; diff --git a/sys/arch/i386/stand/cdboot/conf.c b/sys/arch/i386/stand/cdboot/conf.c index 241fdec2fdc..56b5b5c788e 100644 --- a/sys/arch/i386/stand/cdboot/conf.c +++ b/sys/arch/i386/stand/cdboot/conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: conf.c,v 1.8 2007/04/27 10:08:34 tom Exp $ */ +/* $OpenBSD: conf.c,v 1.9 2007/05/30 01:25:43 tom Exp $ */ /* * Copyright (c) 2004 Tom Cosgrove @@ -43,7 +43,7 @@ #include <dev/cons.h> #include "debug.h" -const char version[] = "1.07"; +const char version[] = "2.00"; int debug = 1; #undef _TEST diff --git a/sys/arch/i386/stand/libsa/Makefile b/sys/arch/i386/stand/libsa/Makefile index e43cf52d7cf..edff079b1f6 100644 --- a/sys/arch/i386/stand/libsa/Makefile +++ b/sys/arch/i386/stand/libsa/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.46 2006/09/18 21:14:15 mpf Exp $ +# $OpenBSD: Makefile,v 1.47 2007/05/30 01:25:43 tom Exp $ .include "${.CURDIR}/../Makefile.inc" @@ -8,7 +8,7 @@ S=${.CURDIR}/../../../.. SADIR=${.CURDIR}/.. # i386 stuff (so, it will possibly load in the same 64k) -SRCS+= machdep.c dev_i386.c exec_i386.c cmd_i386.c loadfile.c +SRCS+= machdep.c dev_i386.c exec_i386.c cmd_i386.c loadfile.c elf32.c elf64.c .if defined(DEBUGFLAGS) && !empty(DEBUGFLAGS:M-D_TEST) SRCS+= unixdev.c unixsys.S nullfs.c memprobe.c diff --git a/sys/arch/i386/stand/libsa/elf32.c b/sys/arch/i386/stand/libsa/elf32.c new file mode 100644 index 00000000000..f943067411c --- /dev/null +++ b/sys/arch/i386/stand/libsa/elf32.c @@ -0,0 +1,32 @@ +/* $OpenBSD: elf32.c,v 1.1 2007/05/30 01:25:43 tom Exp $ */ + +/* + * Copyright (c) 2007 Tom Cosgrove <tom@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <lib/libkern/libkern.h> +#include <lib/libsa/stand.h> + +#include <sys/param.h> +#include <sys/exec.h> + +#include "../../../../lib/libsa/loadfile.h" + +#undef ELFSIZE +#define ELFSIZE 32 + +#include <sys/exec_elf.h> + +#include "../../../../lib/libsa/loadfile_elf.c" diff --git a/sys/arch/i386/stand/libsa/elf64.c b/sys/arch/i386/stand/libsa/elf64.c new file mode 100644 index 00000000000..d7d11c843c4 --- /dev/null +++ b/sys/arch/i386/stand/libsa/elf64.c @@ -0,0 +1,32 @@ +/* $OpenBSD: elf64.c,v 1.1 2007/05/30 01:25:43 tom Exp $ */ + +/* + * Copyright (c) 2007 Tom Cosgrove <tom@openbsd.org> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <lib/libkern/libkern.h> +#include <lib/libsa/stand.h> + +#include <sys/param.h> +#include <sys/exec.h> + +#include "../../../../lib/libsa/loadfile.h" + +#undef ELFSIZE +#define ELFSIZE 64 + +#include <sys/exec_elf.h> + +#include "../../../../lib/libsa/loadfile_elf.c" diff --git a/sys/arch/i386/stand/pxeboot/conf.c b/sys/arch/i386/stand/pxeboot/conf.c index c194d8b16b3..d707b25930d 100644 --- a/sys/arch/i386/stand/pxeboot/conf.c +++ b/sys/arch/i386/stand/pxeboot/conf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: conf.c,v 1.12 2007/04/27 10:08:34 tom Exp $ */ +/* $OpenBSD: conf.c,v 1.13 2007/05/30 01:25:43 tom Exp $ */ /* * Copyright (c) 2004 Tom Cosgrove @@ -25,7 +25,6 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * */ #include <sys/types.h> @@ -46,7 +45,7 @@ #include "pxeboot.h" #include "pxe_net.h" -const char version[] = "1.12"; +const char version[] = "2.00"; int debug = 1; #undef _TEST diff --git a/sys/lib/libsa/loadfile.c b/sys/lib/libsa/loadfile.c index a1c1230c9be..4dc70bcf7da 100644 --- a/sys/lib/libsa/loadfile.c +++ b/sys/lib/libsa/loadfile.c @@ -1,5 +1,5 @@ /* $NetBSD: loadfile.c,v 1.10 2000/12/03 02:53:04 tsutsui Exp $ */ -/* $OpenBSD: loadfile.c,v 1.12 2007/01/04 07:09:30 miod Exp $ */ +/* $OpenBSD: loadfile.c,v 1.13 2007/05/30 01:25:43 tom Exp $ */ /*- * Copyright (c) 1997 The NetBSD Foundation, Inc. @@ -94,15 +94,25 @@ #include <sys/exec_ecoff.h> static int coff_exec(int, struct ecoff_exechdr *, u_long *, int); #endif -#ifdef BOOT_ELF -#include <sys/exec_elf.h> -static int elf_exec(int, Elf_Ehdr *, u_long *, int); -#endif #ifdef BOOT_AOUT #include <sys/exec_aout.h> static int aout_exec(int, struct exec *, u_long *, int); #endif +#ifdef BOOT_ELF +#include <sys/exec_elf.h> +#if defined(BOOT_ELF32) && defined(BOOT_ELF64) +/* + * Both defined, so elf32_exec() and elf64_exec() need to be separately + * created (can't do it by including loadfile_elf.c here). + */ +int elf32_exec(int, Elf32_Ehdr *, u_long *, int); +int elf64_exec(int, Elf64_Ehdr *, u_long *, int); +#else +#include "loadfile_elf.c" +#endif +#endif + /* * Open 'filename', read in program and and return 0 if ok 1 on error. * Fill in marks @@ -114,8 +124,11 @@ loadfile(const char *fname, u_long *marks, int flags) #ifdef BOOT_ECOFF struct ecoff_exechdr coff; #endif -#ifdef BOOT_ELF - Elf_Ehdr elf; +#if defined(BOOT_ELF32) || (defined(BOOT_ELF) && ELFSIZE == 32) + Elf32_Ehdr elf32; +#endif +#if defined(BOOT_ELF64) || (defined(BOOT_ELF) && ELFSIZE == 64) + Elf64_Ehdr elf64; #endif #ifdef BOOT_AOUT struct exec aout; @@ -142,10 +155,16 @@ loadfile(const char *fname, u_long *marks, int flags) rval = coff_exec(fd, &hdr.coff, marks, flags); } else #endif -#ifdef BOOT_ELF - if (memcmp(hdr.elf.e_ident, ELFMAG, SELFMAG) == 0 && - hdr.elf.e_ident[EI_CLASS] == ELFCLASS) { - rval = elf_exec(fd, &hdr.elf, marks, flags); +#if defined(BOOT_ELF32) || (defined(BOOT_ELF) && ELFSIZE == 32) + if (memcmp(hdr.elf32.e_ident, ELFMAG, SELFMAG) == 0 && + hdr.elf32.e_ident[EI_CLASS] == ELFCLASS32) { + rval = elf32_exec(fd, &hdr.elf32, marks, flags); + } else +#endif +#if defined(BOOT_ELF64) || (defined(BOOT_ELF) && ELFSIZE == 64) + if (memcmp(hdr.elf64.e_ident, ELFMAG, SELFMAG) == 0 && + hdr.elf64.e_ident[EI_CLASS] == ELFCLASS64) { + rval = elf64_exec(fd, &hdr.elf64, marks, flags); } else #endif #ifdef BOOT_AOUT @@ -254,183 +273,6 @@ coff_exec(int fd, struct ecoff_exechdr *coff, u_long *marks, int flags) } #endif /* BOOT_ECOFF */ -#ifdef BOOT_ELF -static int -elf_exec(int fd, Elf_Ehdr *elf, u_long *marks, int flags) -{ - Elf_Shdr *shp; - Elf_Phdr *phdr; - Elf_Off off; - int i; - size_t sz; - int first; - int havesyms; - paddr_t minp = ~0, maxp = 0, pos = 0; - paddr_t offset = marks[MARK_START], shpp, elfp; - - sz = elf->e_phnum * sizeof(Elf_Phdr); - phdr = ALLOC(sz); - - if (lseek(fd, elf->e_phoff, SEEK_SET) == -1) { - WARN(("lseek phdr")); - FREE(phdr, sz); - return 1; - } - if (read(fd, phdr, sz) != sz) { - WARN(("read program headers")); - FREE(phdr, sz); - return 1; - } - - for (first = 1, i = 0; i < elf->e_phnum; i++) { - - if (phdr[i].p_type != PT_LOAD || - (phdr[i].p_flags & (PF_W|PF_R|PF_X)) == 0) - continue; - -#define IS_TEXT(p) (p.p_flags & PF_X) -#define IS_DATA(p) ((p.p_flags & PF_X) == 0) -#define IS_BSS(p) (p.p_filesz < p.p_memsz) - /* - * XXX: Assume first address is lowest - */ - if ((IS_TEXT(phdr[i]) && (flags & LOAD_TEXT)) || - (IS_DATA(phdr[i]) && (flags & LOAD_DATA))) { - - /* Read in segment. */ - PROGRESS(("%s%lu", first ? "" : "+", - (u_long)phdr[i].p_filesz)); - - if (lseek(fd, phdr[i].p_offset, SEEK_SET) == -1) { - WARN(("lseek text")); - FREE(phdr, sz); - return 1; - } - if (READ(fd, phdr[i].p_vaddr, phdr[i].p_filesz) != - phdr[i].p_filesz) { - WARN(("read text")); - FREE(phdr, sz); - return 1; - } - first = 0; - - } - if ((IS_TEXT(phdr[i]) && (flags & (LOAD_TEXT|COUNT_TEXT))) || - (IS_DATA(phdr[i]) && (flags & (LOAD_DATA|COUNT_TEXT)))) { - pos = phdr[i].p_vaddr; - if (minp > pos) - minp = pos; - pos += phdr[i].p_filesz; - if (maxp < pos) - maxp = pos; - } - - /* Zero out bss. */ - if (IS_BSS(phdr[i]) && (flags & LOAD_BSS)) { - PROGRESS(("+%lu", - (u_long)(phdr[i].p_memsz - phdr[i].p_filesz))); - BZERO((phdr[i].p_vaddr + phdr[i].p_filesz), - phdr[i].p_memsz - phdr[i].p_filesz); - } - if (IS_BSS(phdr[i]) && (flags & (LOAD_BSS|COUNT_BSS))) { - pos += phdr[i].p_memsz - phdr[i].p_filesz; - if (maxp < pos) - maxp = pos; - } - } - FREE(phdr, sz); - - /* - * Copy the ELF and section headers. - */ - elfp = maxp = roundup(maxp, sizeof(long)); - if (flags & (LOAD_HDR|COUNT_HDR)) - maxp += sizeof(Elf_Ehdr); - - if (flags & (LOAD_SYM|COUNT_SYM)) { - if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) { - WARN(("lseek section headers")); - return 1; - } - sz = elf->e_shnum * sizeof(Elf_Shdr); - shp = ALLOC(sz); - - if (read(fd, shp, sz) != sz) { - WARN(("read section headers")); - FREE(shp, sz); - return 1; - } - - shpp = maxp; - maxp += roundup(sz, sizeof(long)); - - /* - * Now load the symbol sections themselves. Make sure the - * sections are aligned. Don't bother with string tables if - * there are no symbol sections. - */ - off = roundup((sizeof(Elf_Ehdr) + sz), sizeof(long)); - - for (havesyms = i = 0; i < elf->e_shnum; i++) - if (shp[i].sh_type == SHT_SYMTAB) - havesyms = 1; - - for (first = 1, i = 0; i < elf->e_shnum; i++) { - if (shp[i].sh_type == SHT_SYMTAB || - shp[i].sh_type == SHT_STRTAB) { - if (havesyms && (flags & LOAD_SYM)) { - PROGRESS(("%s%ld", first ? " [" : "+", - (u_long)shp[i].sh_size)); - if (lseek(fd, shp[i].sh_offset, - SEEK_SET) == -1) { - WARN(("lseek symbols")); - FREE(shp, sz); - return 1; - } - if (READ(fd, maxp, shp[i].sh_size) != - shp[i].sh_size) { - WARN(("read symbols")); - FREE(shp, sz); - return 1; - } - } - maxp += roundup(shp[i].sh_size, - sizeof(long)); - shp[i].sh_offset = off; - off += roundup(shp[i].sh_size, sizeof(long)); - first = 0; - } - } - if (flags & LOAD_SYM) { - BCOPY(shp, shpp, sz); - - if (havesyms && first == 0) - PROGRESS(("]")); - } - FREE(shp, sz); - } - - /* - * Frob the copied ELF header to give information relative - * to elfp. - */ - if (flags & LOAD_HDR) { - elf->e_phoff = 0; - elf->e_shoff = sizeof(Elf_Ehdr); - elf->e_phentsize = 0; - elf->e_phnum = 0; - BCOPY(elf, elfp, sizeof(*elf)); - } - - marks[MARK_START] = LOADADDR(minp); - marks[MARK_ENTRY] = LOADADDR(elf->e_entry); - marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */ - marks[MARK_SYM] = LOADADDR(elfp); - marks[MARK_END] = LOADADDR(maxp); - return 0; -} -#endif /* BOOT_ELF */ - #ifdef BOOT_AOUT static int aout_exec(int fd, struct exec *x, u_long *marks, int flags) diff --git a/sys/lib/libsa/loadfile_elf.c b/sys/lib/libsa/loadfile_elf.c new file mode 100644 index 00000000000..016aeb18eda --- /dev/null +++ b/sys/lib/libsa/loadfile_elf.c @@ -0,0 +1,250 @@ +/* $NetBSD: loadfile.c,v 1.10 2000/12/03 02:53:04 tsutsui Exp $ */ +/* $OpenBSD: loadfile_elf.c,v 1.1 2007/05/30 01:25:43 tom Exp $ */ + +/*- + * Copyright (c) 1997 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, + * NASA Ames Research Center and by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Copyright (c) 1992, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Ralph Campbell. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * @(#)boot.c 8.1 (Berkeley) 6/10/93 + */ + +int ELFNAME(exec)(int, Elf_Ehdr *, u_long *, int); + +int +ELFNAME(exec)(int fd, Elf_Ehdr *elf, u_long *marks, int flags) +{ + Elf_Shdr *shp; + Elf_Phdr *phdr; + Elf_Off off; + int i; + size_t sz; + int first; + int havesyms; + paddr_t minp = ~0, maxp = 0, pos = 0; + paddr_t offset = marks[MARK_START], shpp, elfp; + + sz = elf->e_phnum * sizeof(Elf_Phdr); + phdr = ALLOC(sz); + + if (lseek(fd, (off_t)elf->e_phoff, SEEK_SET) == -1) { + WARN(("lseek phdr")); + FREE(phdr, sz); + return 1; + } + if (read(fd, phdr, sz) != sz) { + WARN(("read program headers")); + FREE(phdr, sz); + return 1; + } + + for (first = 1, i = 0; i < elf->e_phnum; i++) { + + if (phdr[i].p_type != PT_LOAD || + (phdr[i].p_flags & (PF_W|PF_R|PF_X)) == 0) + continue; + +#define IS_TEXT(p) (p.p_flags & PF_X) +#define IS_DATA(p) ((p.p_flags & PF_X) == 0) +#define IS_BSS(p) (p.p_filesz < p.p_memsz) + /* + * XXX: Assume first address is lowest + */ + if ((IS_TEXT(phdr[i]) && (flags & LOAD_TEXT)) || + (IS_DATA(phdr[i]) && (flags & LOAD_DATA))) { + + /* Read in segment. */ + PROGRESS(("%s%lu", first ? "" : "+", + (u_long)phdr[i].p_filesz)); + + if (lseek(fd, (off_t)phdr[i].p_offset, SEEK_SET) == -1) { + WARN(("lseek text")); + FREE(phdr, sz); + return 1; + } + if (READ(fd, phdr[i].p_vaddr, phdr[i].p_filesz) != + phdr[i].p_filesz) { + WARN(("read text")); + FREE(phdr, sz); + return 1; + } + first = 0; + + } + if ((IS_TEXT(phdr[i]) && (flags & (LOAD_TEXT|COUNT_TEXT))) || + (IS_DATA(phdr[i]) && (flags & (LOAD_DATA|COUNT_TEXT)))) { + pos = phdr[i].p_vaddr; + if (minp > pos) + minp = pos; + pos += phdr[i].p_filesz; + if (maxp < pos) + maxp = pos; + } + + /* Zero out bss. */ + if (IS_BSS(phdr[i]) && (flags & LOAD_BSS)) { + PROGRESS(("+%lu", + (u_long)(phdr[i].p_memsz - phdr[i].p_filesz))); + BZERO((phdr[i].p_vaddr + phdr[i].p_filesz), + phdr[i].p_memsz - phdr[i].p_filesz); + } + if (IS_BSS(phdr[i]) && (flags & (LOAD_BSS|COUNT_BSS))) { + pos += phdr[i].p_memsz - phdr[i].p_filesz; + if (maxp < pos) + maxp = pos; + } + } + FREE(phdr, sz); + + /* + * Copy the ELF and section headers. + */ + elfp = maxp = roundup(maxp, sizeof(long)); + if (flags & (LOAD_HDR|COUNT_HDR)) + maxp += sizeof(Elf_Ehdr); + + if (flags & (LOAD_SYM|COUNT_SYM)) { + if (lseek(fd, (off_t)elf->e_shoff, SEEK_SET) == -1) { + WARN(("lseek section headers")); + return 1; + } + sz = elf->e_shnum * sizeof(Elf_Shdr); + shp = ALLOC(sz); + + if (read(fd, shp, sz) != sz) { + WARN(("read section headers")); + FREE(shp, sz); + return 1; + } + + shpp = maxp; + maxp += roundup(sz, sizeof(long)); + + /* + * Now load the symbol sections themselves. Make sure the + * sections are aligned. Don't bother with string tables if + * there are no symbol sections. + */ + off = roundup((sizeof(Elf_Ehdr) + sz), sizeof(long)); + + for (havesyms = i = 0; i < elf->e_shnum; i++) + if (shp[i].sh_type == SHT_SYMTAB) + havesyms = 1; + + for (first = 1, i = 0; i < elf->e_shnum; i++) { + if (shp[i].sh_type == SHT_SYMTAB || + shp[i].sh_type == SHT_STRTAB) { + if (havesyms && (flags & LOAD_SYM)) { + PROGRESS(("%s%ld", first ? " [" : "+", + (u_long)shp[i].sh_size)); + if (lseek(fd, (off_t)shp[i].sh_offset, + SEEK_SET) == -1) { + WARN(("lseek symbols")); + FREE(shp, sz); + return 1; + } + if (READ(fd, maxp, shp[i].sh_size) != + shp[i].sh_size) { + WARN(("read symbols")); + FREE(shp, sz); + return 1; + } + } + maxp += roundup(shp[i].sh_size, + sizeof(long)); + shp[i].sh_offset = off; + off += roundup(shp[i].sh_size, sizeof(long)); + first = 0; + } + } + if (flags & LOAD_SYM) { + BCOPY(shp, shpp, sz); + + if (havesyms && first == 0) + PROGRESS(("]")); + } + FREE(shp, sz); + } + + /* + * Frob the copied ELF header to give information relative + * to elfp. + */ + if (flags & LOAD_HDR) { + elf->e_phoff = 0; + elf->e_shoff = sizeof(Elf_Ehdr); + elf->e_phentsize = 0; + elf->e_phnum = 0; + BCOPY(elf, elfp, sizeof(*elf)); + } + + marks[MARK_START] = LOADADDR(minp); + marks[MARK_ENTRY] = LOADADDR(elf->e_entry); + marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */ + marks[MARK_SYM] = LOADADDR(elfp); + marks[MARK_END] = LOADADDR(maxp); + return 0; +} |