From 559f61486d2a20e399357d595f60a0421ddc8525 Mon Sep 17 00:00:00 2001 From: ru Date: Tue, 11 Mar 2003 13:48:58 +0000 Subject: Make this work on different endianness machines. Tested on: sparc64 : FreeBSD/i386 bootstrap loader, Revision 1.1 : (ru@panther.freebsd.org, Tue Mar 11 05:31:14 PST 2003) : Loading /boot/defaults/loader.conf --- usr.sbin/btxld/Makefile | 1 + usr.sbin/btxld/a.out.h | 86 ---------------------------------------- usr.sbin/btxld/btxld.c | 83 +++++++++++++++++++++----------------- usr.sbin/btxld/elfh.c | 103 ++++++++++++++++++++++++------------------------ 4 files changed, 100 insertions(+), 173 deletions(-) delete mode 100644 usr.sbin/btxld/a.out.h (limited to 'usr.sbin/btxld') diff --git a/usr.sbin/btxld/Makefile b/usr.sbin/btxld/Makefile index 32cf99d..912be22 100644 --- a/usr.sbin/btxld/Makefile +++ b/usr.sbin/btxld/Makefile @@ -3,5 +3,6 @@ PROG= btxld MAN= btxld.8 SRCS= btxld.c elfh.c +CFLAGS+=-I${.CURDIR}/../kgzip .include diff --git a/usr.sbin/btxld/a.out.h b/usr.sbin/btxld/a.out.h deleted file mode 100644 index 750c807..0000000 --- a/usr.sbin/btxld/a.out.h +++ /dev/null @@ -1,86 +0,0 @@ -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * 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 University of - * California, Berkeley and its contributors. - * 4. 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. - * - * from: @(#)exec.h 8.1 (Berkeley) 6/11/93 - * from: FreeBSD: src/sys/sys/imgact_aout.h,v 1.18 2002/09/05 07:54:03 bde Exp $ - * $FreeBSD$ - */ - -#include - -#define N_GETMAGIC(ex) \ - ( (ex).a_midmag & 0xffff ) -#define N_SETMAGIC(ex,mag,mid,flag) \ - ( (ex).a_midmag = (((flag) & 0x3f) <<26) | (((mid) & 0x03ff) << 16) | \ - ((mag) & 0xffff) ) - -#define N_GETMAGIC_NET(ex) \ - (ntohl((ex).a_midmag) & 0xffff) - -#define __LDPGSZ 4096 -#define N_ALIGN(ex,x) \ - (N_GETMAGIC(ex) == ZMAGIC || N_GETMAGIC(ex) == QMAGIC || \ - N_GETMAGIC_NET(ex) == ZMAGIC || N_GETMAGIC_NET(ex) == QMAGIC ? \ - ((x) + __LDPGSZ - 1) & ~(uint32_t)(__LDPGSZ - 1) : (x)) - -/* Valid magic number check. */ -#define N_BADMAG(ex) \ - (N_GETMAGIC(ex) != OMAGIC && N_GETMAGIC(ex) != NMAGIC && \ - N_GETMAGIC(ex) != ZMAGIC && N_GETMAGIC(ex) != QMAGIC && \ - N_GETMAGIC_NET(ex) != OMAGIC && N_GETMAGIC_NET(ex) != NMAGIC && \ - N_GETMAGIC_NET(ex) != ZMAGIC && N_GETMAGIC_NET(ex) != QMAGIC) - -/* - * Header prepended to each a.out file. - * only manipulate the a_midmag field via the - * N_SETMAGIC/N_GET{MAGIC,MID,FLAG} macros in a.out.h - */ - -struct exec { - uint32_t a_midmag; /* flags<<26 | mid<<16 | magic */ - uint32_t a_text; /* text segment size */ - uint32_t a_data; /* initialized data size */ - uint32_t a_bss; /* uninitialized data size */ - uint32_t a_syms; /* symbol table size */ - uint32_t a_entry; /* entry point */ - uint32_t a_trsize; /* text relocation size */ - uint32_t a_drsize; /* data relocation size */ -}; - -/* a_magic */ -#define OMAGIC 0407 /* old impure format */ -#define NMAGIC 0410 /* read-only text */ -#define ZMAGIC 0413 /* demand load format */ -#define QMAGIC 0314 /* "compact" demand load format */ - -/* a_mid */ -#define MID_ZERO 0 /* unknown - implementation dependent */ diff --git a/usr.sbin/btxld/btxld.c b/usr.sbin/btxld/btxld.c index d5750f2..9216031 100644 --- a/usr.sbin/btxld/btxld.c +++ b/usr.sbin/btxld/btxld.c @@ -42,9 +42,10 @@ static const char rcsid[] = #include #include -#include "a.out.h" #include "btx.h" #include "elfh.h" +#include "endian.h" +#include "i386_a.out.h" #define BTX_PATH "/sys/boot/i386/btx" @@ -191,7 +192,7 @@ static void btxld(const char *iname) { char name[FILENAME_MAX]; - struct btx_hdr btx; + struct btx_hdr btx, btxle; struct hdr ihdr, ohdr; unsigned int ldr_size, cwr; int fdi[3], fdo, i; @@ -259,7 +260,11 @@ btxld(const char *iname) seekx(fdo, ohdr.size += ohdr.text); break; case I_BTX: - writex(fdo, &btx, sizeof(btx)); + btxle = btx; + btxle.btx_pgctl = HTOLE16(btxle.btx_pgctl); + btxle.btx_textsz = HTOLE16(btxle.btx_textsz); + btxle.btx_entry = HTOLE32(btxle.btx_entry); + writex(fdo, &btxle, sizeof(btxle)); copy(fdi[i], fdo, btx.btx_textsz - sizeof(btx), sizeof(btx)); break; @@ -300,6 +305,9 @@ getbtx(int fd, struct btx_hdr * btx) btx->btx_magic[1] != BTX_MAG1 || btx->btx_magic[2] != BTX_MAG2) errx(1, "%s: Not a BTX kernel", fname); + btx->btx_pgctl = LE16TOH(btx->btx_pgctl); + btx->btx_textsz = LE16TOH(btx->btx_textsz); + btx->btx_entry = LE32TOH(btx->btx_entry); } /* @@ -309,7 +317,7 @@ static void gethdr(int fd, struct hdr *hdr) { struct stat sb; - const struct exec *ex; + const struct i386_exec *ex; const Elf32_Ehdr *ee; const Elf32_Phdr *ep; void *p; @@ -328,20 +336,20 @@ gethdr(int fd, struct hdr *hdr) switch (fmt) { case F_AOUT: ex = p; - if (hdr->size >= sizeof(struct exec) && !N_BADMAG(*ex)) { + if (hdr->size >= sizeof(struct i386_exec) && !I386_N_BADMAG(*ex)) { hdr->fmt = fmt; - x = N_GETMAGIC(*ex); + x = I386_N_GETMAGIC(*ex); if (x == OMAGIC || x == NMAGIC) { if (x == NMAGIC) Warn(fname, "Treating %s NMAGIC as OMAGIC", fmtlist[fmt]); hdr->flags |= IMPURE; } - hdr->text = ex->a_text; - hdr->data = ex->a_data; - hdr->bss = ex->a_bss; - hdr->entry = ex->a_entry; - if (ex->a_entry >= BTX_PGSIZE) + hdr->text = LE32TOH(ex->a_text); + hdr->data = LE32TOH(ex->a_data); + hdr->bss = LE32TOH(ex->a_bss); + hdr->entry = LE32TOH(ex->a_entry); + if (LE32TOH(ex->a_entry) >= BTX_PGSIZE) hdr->org = BTX_PGSIZE; } break; @@ -349,20 +357,21 @@ gethdr(int fd, struct hdr *hdr) ee = p; if (hdr->size >= sizeof(Elf32_Ehdr) && IS_ELF(*ee)) { hdr->fmt = fmt; - for (n = i = 0; i < ee->e_phnum; i++) { - ep = (void *)((uint8_t *)p + ee->e_phoff + - ee->e_phentsize * i); - if (ep->p_type == PT_LOAD) + for (n = i = 0; i < LE16TOH(ee->e_phnum); i++) { + ep = (void *)((uint8_t *)p + LE32TOH(ee->e_phoff) + + LE16TOH(ee->e_phentsize) * i); + if (LE32TOH(ep->p_type) == PT_LOAD) switch (n++) { case 0: - hdr->text = ep->p_filesz; - hdr->org = ep->p_paddr; - if (ep->p_flags & PF_W) + hdr->text = LE32TOH(ep->p_filesz); + hdr->org = LE32TOH(ep->p_paddr); + if (LE32TOH(ep->p_flags) & PF_W) hdr->flags |= IMPURE; break; case 1: - hdr->data = ep->p_filesz; - hdr->bss = ep->p_memsz - ep->p_filesz; + hdr->data = LE32TOH(ep->p_filesz); + hdr->bss = LE32TOH(ep->p_memsz) - + LE32TOH(ep->p_filesz); break; case 2: Warn(fname, @@ -370,7 +379,7 @@ gethdr(int fd, struct hdr *hdr) fmtlist[fmt]); } } - hdr->entry = ee->e_entry; + hdr->entry = LE32TOH(ee->e_entry); } } if (munmap(p, hdr->size)) @@ -383,31 +392,33 @@ gethdr(int fd, struct hdr *hdr) static void puthdr(int fd, struct hdr *hdr) { - struct exec ex; + struct i386_exec ex; struct elfh eh; switch (hdr->fmt) { case F_AOUT: memset(&ex, 0, sizeof(ex)); - N_SETMAGIC(ex, ZMAGIC, MID_ZERO, 0); - hdr->text = N_ALIGN(ex, hdr->text); - ex.a_text = hdr->text; - hdr->data = N_ALIGN(ex, hdr->data); - ex.a_data = hdr->data; - ex.a_entry = hdr->entry; + I386_N_SETMAGIC(ex, ZMAGIC, MID_ZERO, 0); + hdr->text = I386_N_ALIGN(ex, hdr->text); + ex.a_text = HTOLE32(hdr->text); + hdr->data = I386_N_ALIGN(ex, hdr->data); + ex.a_data = HTOLE32(hdr->data); + ex.a_entry = HTOLE32(hdr->entry); writex(fd, &ex, sizeof(ex)); - hdr->size = N_ALIGN(ex, sizeof(ex)); + hdr->size = I386_N_ALIGN(ex, sizeof(ex)); seekx(fd, hdr->size); break; case F_ELF: eh = elfhdr; - eh.e.e_entry = hdr->entry; - eh.p[0].p_vaddr = eh.p[0].p_paddr = hdr->org; - eh.p[0].p_filesz = eh.p[0].p_memsz = hdr->text; - eh.p[1].p_offset = eh.p[0].p_offset + eh.p[0].p_filesz; - eh.p[1].p_vaddr = eh.p[1].p_paddr = align(eh.p[0].p_paddr + - eh.p[0].p_memsz, 4); - eh.p[1].p_filesz = eh.p[1].p_memsz = hdr->data; + eh.e.e_entry = HTOLE32(hdr->entry); + eh.p[0].p_vaddr = eh.p[0].p_paddr = HTOLE32(hdr->org); + eh.p[0].p_filesz = eh.p[0].p_memsz = HTOLE32(hdr->text); + eh.p[1].p_offset = HTOLE32(LE32TOH(eh.p[0].p_offset) + + LE32TOH(eh.p[0].p_filesz)); + eh.p[1].p_vaddr = eh.p[1].p_paddr = + HTOLE32(align(LE32TOH(eh.p[0].p_paddr) + LE32TOH(eh.p[0].p_memsz), + 4)); + eh.p[1].p_filesz = eh.p[1].p_memsz = HTOLE32(hdr->data); eh.sh[2].sh_addr = eh.p[0].p_vaddr; eh.sh[2].sh_offset = eh.p[0].p_offset; eh.sh[2].sh_size = eh.p[0].p_filesz; diff --git a/usr.sbin/btxld/elfh.c b/usr.sbin/btxld/elfh.c index 0f0928b..75b7e33 100644 --- a/usr.sbin/btxld/elfh.c +++ b/usr.sbin/btxld/elfh.c @@ -30,6 +30,7 @@ #include #include "elfh.h" +#include "endian.h" #define SET_ME 0xeeeeeeee /* filled in by btxld */ @@ -43,80 +44,80 @@ const struct elfh elfhdr = { ELFCLASS32, ELFDATA2LSB, EV_CURRENT, 0, 'F', 'r', 'e', 'e', 'B', 'S', 'D', 0 }, - ET_EXEC, /* e_type */ - EM_386, /* e_machine */ - EV_CURRENT, /* e_version */ - SET_ME, /* e_entry */ - offsetof(struct elfh, p), /* e_phoff */ - offsetof(struct elfh, sh), /* e_shoff */ + HTOLE16(ET_EXEC), /* e_type */ + HTOLE16(EM_386), /* e_machine */ + HTOLE32(EV_CURRENT), /* e_version */ + HTOLE32(SET_ME), /* e_entry */ + HTOLE32(offsetof(struct elfh, p)), /* e_phoff */ + HTOLE32(offsetof(struct elfh, sh)), /* e_shoff */ 0, /* e_flags */ - sizeof(elfhdr.e), /* e_ehsize */ - sizeof(elfhdr.p[0]), /* e_phentsize */ - sizeof(elfhdr.p) / sizeof(elfhdr.p[0]), /* e_phnum */ - sizeof(elfhdr.sh[0]), /* e_shentsize */ - sizeof(elfhdr.sh) / sizeof(elfhdr.sh[0]), /* e_shnum */ - 1 /* e_shstrndx */ + HTOLE16(sizeof(elfhdr.e)), /* e_ehsize */ + HTOLE16(sizeof(elfhdr.p[0])), /* e_phentsize */ + HTOLE16(sizeof(elfhdr.p) / sizeof(elfhdr.p[0])), /* e_phnum */ + HTOLE16(sizeof(elfhdr.sh[0])), /* e_shentsize */ + HTOLE16(sizeof(elfhdr.sh) / sizeof(elfhdr.sh[0])), /* e_shnum */ + HTOLE16(1) /* e_shstrndx */ }, { { - PT_LOAD, /* p_type */ - sizeof(elfhdr), /* p_offset */ - SET_ME, /* p_vaddr */ - SET_ME, /* p_paddr */ - SET_ME, /* p_filesz */ - SET_ME, /* p_memsz */ - PF_R | PF_X, /* p_flags */ - 0x1000 /* p_align */ + HTOLE32(PT_LOAD), /* p_type */ + HTOLE32(sizeof(elfhdr)), /* p_offset */ + HTOLE32(SET_ME), /* p_vaddr */ + HTOLE32(SET_ME), /* p_paddr */ + HTOLE32(SET_ME), /* p_filesz */ + HTOLE32(SET_ME), /* p_memsz */ + HTOLE32(PF_R | PF_X), /* p_flags */ + HTOLE32(0x1000) /* p_align */ }, { - PT_LOAD, /* p_type */ - SET_ME, /* p_offset */ - SET_ME, /* p_vaddr */ - SET_ME, /* p_paddr */ - SET_ME, /* p_filesz */ - SET_ME, /* p_memsz */ - PF_R | PF_W, /* p_flags */ - 0x1000 /* p_align */ + HTOLE32(PT_LOAD), /* p_type */ + HTOLE32(SET_ME), /* p_offset */ + HTOLE32(SET_ME), /* p_vaddr */ + HTOLE32(SET_ME), /* p_paddr */ + HTOLE32(SET_ME), /* p_filesz */ + HTOLE32(SET_ME), /* p_memsz */ + HTOLE32(PF_R | PF_W), /* p_flags */ + HTOLE32(0x1000) /* p_align */ } }, { { - 0, SHT_NULL, 0, 0, 0, 0, SHN_UNDEF, 0, 0, 0 + 0, HTOLE32(SHT_NULL), 0, 0, 0, 0, HTOLE32(SHN_UNDEF), 0, 0, 0 }, { - 1, /* sh_name */ - SHT_STRTAB, /* sh_type */ + HTOLE32(1), /* sh_name */ + HTOLE32(SHT_STRTAB), /* sh_type */ 0, /* sh_flags */ 0, /* sh_addr */ - offsetof(struct elfh, shstrtab), /* sh_offset */ - sizeof(elfhdr.shstrtab), /* sh_size */ - SHN_UNDEF, /* sh_link */ + HTOLE32(offsetof(struct elfh, shstrtab)), /* sh_offset */ + HTOLE32(sizeof(elfhdr.shstrtab)), /* sh_size */ + HTOLE32(SHN_UNDEF), /* sh_link */ 0, /* sh_info */ - 1, /* sh_addralign */ + HTOLE32(1), /* sh_addralign */ 0 /* sh_entsize */ }, { - 0xb, /* sh_name */ - SHT_PROGBITS, /* sh_type */ - SHF_EXECINSTR | SHF_ALLOC, /* sh_flags */ - SET_ME, /* sh_addr */ - SET_ME, /* sh_offset */ - SET_ME, /* sh_size */ - SHN_UNDEF, /* sh_link */ + HTOLE32(0xb), /* sh_name */ + HTOLE32(SHT_PROGBITS), /* sh_type */ + HTOLE32(SHF_EXECINSTR | SHF_ALLOC), /* sh_flags */ + HTOLE32(SET_ME), /* sh_addr */ + HTOLE32(SET_ME), /* sh_offset */ + HTOLE32(SET_ME), /* sh_size */ + HTOLE32(SHN_UNDEF), /* sh_link */ 0, /* sh_info */ - 4, /* sh_addralign */ + HTOLE32(4), /* sh_addralign */ 0 /* sh_entsize */ }, { - 0x11, /* sh_name */ - SHT_PROGBITS, /* sh_type */ - SHF_ALLOC | SHF_WRITE, /* sh_flags */ - SET_ME, /* sh_addr */ - SET_ME, /* sh_offset */ - SET_ME, /* sh_size */ - SHN_UNDEF, /* sh_link */ + HTOLE32(0x11), /* sh_name */ + HTOLE32(SHT_PROGBITS), /* sh_type */ + HTOLE32(SHF_ALLOC | SHF_WRITE), /* sh_flags */ + HTOLE32(SET_ME), /* sh_addr */ + HTOLE32(SET_ME), /* sh_offset */ + HTOLE32(SET_ME), /* sh_size */ + HTOLE32(SHN_UNDEF), /* sh_link */ 0, /* sh_info */ - 4, /* sh_addralign */ + HTOLE32(4), /* sh_addralign */ 0 /* sh_entsize */ } }, -- cgit v1.1