Subject: Re: elf2ecoff for Indigo kernel
To: Gerald Heinig <Gerald.Heinig@post.rwth-aachen.de>
From: Rafal Boni <rafal.boni@eDial.com>
List: port-sgimips
Date: 02/01/2001 21:15:29
In message <3A79F38B.10C83587@post.rwth-aachen.de>, Gerald writes: 

-> Hello Rafal (or anyone else..)
-> 
-> Could you point me to where to get the elf2ecoff prog that does the 
-> correct little-endian to big-endian conversion.
-> I've now compiled and linked an Indigo kernel and I'd like to try 
-> it out on my Indigo.

Attached is what I've been using, though I admit I haven't gotten a kernel
yet booting on my I2 (between PROM refusing to tftp, sash loading things in
the wrong places, and my lack of hacking time 8-).

You should be able to drop this in anywhere (it's been a while), but I stuck
in in arch/sgimips/stand/elf2ecoff.  I should also disclaim any credit for 
writing this... it's a hacked up copy of the netbsd elf2ecoff I found on 
someone's Linux/MIPS page.

Let me know how it works,
--rafal

# This is a shell archive.  Save it in a file, remove anything before
# this line, and then unpack it by entering "sh file".  Note, it may
# create directories; files and directories will be owned by you and
# have default permissions.
#
# This archive contains:
#
#	Makefile
#	ecoff.h
#	elf.h
#	elf2ecoff.c
#
echo x - Makefile
sed 's/^X//' >Makefile << 'END-of-Makefile'
X#	$NetBSD: Makefile,v 1.7 1999/06/09 02:54:18 simonb Exp $
X#	from: @(#)Makefile	5.4 (Berkeley) 5/11/90
X
X
XPROG=	elf2ecoff
XNOMAN=
X
XCFLAGS+= -I. -g
X
X.include <bsd.prog.mk>
END-of-Makefile
echo x - ecoff.h
sed 's/^X//' >ecoff.h << 'END-of-ecoff.h'
X/*
X * Some ECOFF definitions.
X */
Xtypedef struct filehdr {
X        unsigned short  f_magic;        /* magic number */
X        unsigned short  f_nscns;        /* number of sections */
X        long            f_timdat;       /* time & date stamp */
X        long            f_symptr;       /* file pointer to symbolic header */
X        long            f_nsyms;        /* sizeof(symbolic hdr) */
X        unsigned short  f_opthdr;       /* sizeof(optional hdr) */
X        unsigned short  f_flags;        /* flags */
X} FILHDR;
X#define FILHSZ  sizeof(FILHDR)
X
X#define OMAGIC         0407
X#define MIPSEBMAGIC    0x160
X#define MIPSELMAGIC    0x162
X
Xtypedef struct scnhdr {
X        char            s_name[8];      /* section name */
X        long            s_paddr;        /* physical address, aliased s_nlib */
X        long            s_vaddr;        /* virtual address */
X        long            s_size;         /* section size */
X        long            s_scnptr;       /* file ptr to raw data for section */
X        long            s_relptr;       /* file ptr to relocation */
X        long            s_lnnoptr;      /* file ptr to gp histogram */
X        unsigned short  s_nreloc;       /* number of relocation entries */
X        unsigned short  s_nlnno;        /* number of gp histogram entries */
X        long            s_flags;        /* flags */
X} SCNHDR;
X#define SCNHSZ         sizeof(SCNHDR)
X#define SCNROUND       ((long)16)
X
Xtypedef struct aouthdr {
X        short   magic;          /* see above                            */
X        short   vstamp;         /* version stamp                        */
X        long    tsize;          /* text size in bytes, padded to DW bdry*/
X        long    dsize;          /* initialized data "  "                */
X        long    bsize;          /* uninitialized data "   "             */
X        long    entry;          /* entry pt.                            */
X        long    text_start;     /* base of text used for this file      */
X        long    data_start;     /* base of data used for this file      */
X        long    bss_start;      /* base of bss used for this file       */
X        long    gprmask;        /* general purpose register mask        */
X        long    cprmask[4];     /* co-processor register masks          */
X        long    gp_value;       /* the gp value used for this object    */
X} AOUTHDR;
X#define AOUTHSZ sizeof(AOUTHDR)
X
X#define OMAGIC         0407
X#define NMAGIC         0410
X#define ZMAGIC         0413
X#define SMAGIC         0411
X#define LIBMAGIC        0443
X
X#define N_TXTOFF(f, a) \
X ((a).magic == ZMAGIC || (a).magic == LIBMAGIC ? 0 : \
X  ((a).vstamp < 23 ? \
X   ((FILHSZ + AOUTHSZ + (f).f_nscns * SCNHSZ + 7) & 0xfffffff8) : \
X   ((FILHSZ + AOUTHSZ + (f).f_nscns * SCNHSZ + SCNROUND-1) & ~(SCNROUND-1)) ) )
X#define N_DATOFF(f, a) \
X  N_TXTOFF(f, a) + (a).tsize;
END-of-ecoff.h
echo x - elf.h
sed 's/^X//' >elf.h << 'END-of-elf.h'
X/*	$NetBSD: exec_elf.h,v 1.39 2000/07/26 02:04:53 mycroft Exp $	*/
X
X/*-
X * Copyright (c) 1994 The NetBSD Foundation, Inc.
X * All rights reserved.
X *
X * This code is derived from software contributed to The NetBSD Foundation
X * by Christos Zoulas.
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. All advertising materials mentioning features or use of this software
X *    must display the following acknowledgement:
X *	This product includes software developed by the NetBSD
X *	Foundation, Inc. and its contributors.
X * 4. Neither the name of The NetBSD Foundation nor the names of its
X *    contributors may be used to endorse or promote products derived
X *    from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
X * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
X * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
X * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
X * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
X * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
X * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
X * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
X * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
X * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
X * POSSIBILITY OF SUCH DAMAGE.
X */
X
X#ifndef _SYS_EXEC_ELF_H_
X#define	_SYS_EXEC_ELF_H_
X
X#include <sys/inttypes.h>
X
Xtypedef	uint8_t  	Elf_Byte;
X
Xtypedef	uint32_t	Elf32_Addr;
X#define	ELF32_FSZ_ADDR	4
Xtypedef	uint32_t Elf32_Off;
X#define	ELF32_FSZ_OFF	4
Xtypedef	int32_t   Elf32_Sword;
X#define	ELF32_FSZ_SWORD	4
Xtypedef	uint32_t Elf32_Word;
X#define	ELF32_FSZ_WORD	4
Xtypedef	uint16_t Elf32_Half;
X#define	ELF32_FSZ_HALF	2
X
Xtypedef	uint64_t	Elf64_Addr;
X#define	ELF64_FSZ_ADDR	8
Xtypedef	uint64_t	Elf64_Off;
X#define	ELF64_FSZ_OFF	8
Xtypedef	int32_t	Elf64_Shalf;
X#define	ELF64_FSZ_SHALF	4
X#ifdef __sparc_v9__
Xtypedef	int32_t	Elf64_Sword;
X#define	ELF64_FSZ_SWORD	4
Xtypedef	uint32_t	Elf64_Word;
X#define	ELF64_FSZ_WORD	4
X#else
Xtypedef	int64_t	Elf64_Sword;
X#define	ELF64_FSZ_SWORD	8
Xtypedef	uint64_t	Elf64_Word;
X#define	ELF64_FSZ_WORD	8
X#endif
Xtypedef	int64_t	Elf64_Sxword;
X#define	ELF64_FSZ_XWORD	8
Xtypedef	uint64_t	Elf64_Xword;
X#define	ELF64_FSZ_XWORD	8
Xtypedef	uint32_t	Elf64_Half;
X#define	ELF64_FSZ_HALF	4
Xtypedef	uint16_t	Elf64_Quarter;
X#define	ELF64_FSZ_QUARTER 2
X
X/*
X * ELF Header
X */
X#define	ELF_NIDENT	16
X
Xtypedef struct {
X	unsigned char	e_ident[ELF_NIDENT];	/* Id bytes */
X	Elf32_Half	e_type;			/* file type */
X	Elf32_Half	e_machine;		/* machine type */
X	Elf32_Word	e_version;		/* version number */
X	Elf32_Addr	e_entry;		/* entry point */
X	Elf32_Off	e_phoff;		/* Program hdr offset */
X	Elf32_Off	e_shoff;		/* Section hdr offset */
X	Elf32_Word	e_flags;		/* Processor flags */
X	Elf32_Half      e_ehsize;		/* sizeof ehdr */
X	Elf32_Half      e_phentsize;		/* Program header entry size */
X	Elf32_Half      e_phnum;		/* Number of program headers */
X	Elf32_Half      e_shentsize;		/* Section header entry size */
X	Elf32_Half      e_shnum;		/* Number of section headers */
X	Elf32_Half      e_shstrndx;		/* String table index */
X} Elf32_Ehdr;
X
Xtypedef struct {
X	unsigned char	e_ident[ELF_NIDENT];	/* Id bytes */
X	Elf64_Quarter	e_type;			/* file type */
X	Elf64_Quarter	e_machine;		/* machine type */
X	Elf64_Half	e_version;		/* version number */
X	Elf64_Addr	e_entry;		/* entry point */
X	Elf64_Off	e_phoff;		/* Program hdr offset */
X	Elf64_Off	e_shoff;		/* Section hdr offset */
X	Elf64_Half	e_flags;		/* Processor flags */
X	Elf64_Quarter	e_ehsize;		/* sizeof ehdr */
X	Elf64_Quarter	e_phentsize;		/* Program header entry size */
X	Elf64_Quarter	e_phnum;		/* Number of program headers */
X	Elf64_Quarter	e_shentsize;		/* Section header entry size */
X	Elf64_Quarter	e_shnum;		/* Number of section headers */
X	Elf64_Quarter	e_shstrndx;		/* String table index */
X} Elf64_Ehdr;
X
X/* e_ident offsets */
X#define	EI_MAG0		0	/* '\177' */
X#define	EI_MAG1		1	/* 'E'    */
X#define	EI_MAG2		2	/* 'L'    */
X#define	EI_MAG3		3	/* 'F'    */
X#define	EI_CLASS	4	/* File class */
X#define	EI_DATA		5	/* Data encoding */
X#define	EI_VERSION	6	/* File version */
X#define	EI_OSABI	7	/* Operating system/ABI identification */
X#define	EI_ABIVERSION	8	/* ABI version */
X#define	EI_PAD		9	/* Start of padding bytes up to EI_NIDENT*/
X
X/* e_ident[ELFMAG0,ELFMAG3] */
X#define	ELFMAG0		0x7f
X#define	ELFMAG1		'E'
X#define	ELFMAG2		'L'
X#define	ELFMAG3		'F'
X#define	ELFMAG		"\177ELF"
X#define	SELFMAG		4
X
X/* e_ident[EI_CLASS] */
X#define	ELFCLASSNONE	0	/* Invalid class */
X#define	ELFCLASS32	1	/* 32-bit objects */
X#define	ELFCLASS64	2	/* 64-bit objects */
X#define	ELFCLASSNUM	3
X
X/* e_ident[EI_DATA] */
X#define	ELFDATANONE	0	/* Invalid data encoding */
X#define	ELFDATA2LSB	1	/* 2's complement values, LSB first */
X#define	ELFDATA2MSB	2	/* 2's complement values, MSB first */
X
X/* e_ident[EI_VERSION] */
X#define	EV_NONE		0	/* Invalid version */
X#define	EV_CURRENT	1	/* Current version */
X#define	EV_NUM		2
X
X/* e_ident[EI_OSABI] */
X#define	ELFOSABI_SYSV		0	/* UNIX System V ABI */
X#define	ELFOSABI_HPUX		1	/* HP-UX operating system */
X#define	ELFOSABI_STANDALONE	255	/* Standalone (embedded) application */
X
X/* e_type */
X#define	ET_NONE		0	/* No file type */
X#define	ET_REL		1	/* Relocatable file */
X#define	ET_EXEC		2	/* Executable file */
X#define	ET_DYN		3	/* Shared object file */
X#define	ET_CORE		4	/* Core file */
X#define	ET_NUM		5
X
X#define	ET_LOOS		0xfe00	/* Operating system specific range */
X#define	ET_HIOS		0xfeff
X#define	ET_LOPROC	0xff00	/* Processor-specific range */
X#define	ET_HIPROC	0xffff
X
X/* e_machine */
X#define	EM_NONE		0	/* No machine */
X#define	EM_M32		1	/* AT&T WE 32100 */
X#define	EM_SPARC	2	/* SPARC */
X#define	EM_386		3	/* Intel 80386 */
X#define	EM_68K		4	/* Motorola 68000 */
X#define	EM_88K		5	/* Motorola 88000 */
X#define	EM_486		6	/* Intel 80486 */
X#define	EM_860		7	/* Intel 80860 */
X#define	EM_MIPS		8	/* MIPS I Architecture */
X#define	EM_S370		9	/* Amdahl UTS on System/370 */
X#define	EM_MIPS_RS3_LE	10	/* MIPS RS3000 Little-endian */
X#define	EM_RS6000	11	/* IBM RS/6000 XXX reserved */
X#define	EM_PARISC	15	/* Hewlett-Packard PA-RISC */
X#define	EM_NCUBE	16	/* NCube XXX reserved */
X#define	EM_VPP500	17	/* Fujitsu VPP500 */
X#define	EM_SPARC32PLUS	18	/* Enhanced instruction set SPARC */
X#define	EM_960		19	/* Intel 80960 */
X#define	EM_PPC		20	/* PowerPC */
X#define	EM_V800		36	/* NEC V800 */
X#define	EM_FR20		37	/* Fujitsu FR20 */
X#define	EM_RH32		38	/* TRW RH-32 */
X#define	EM_RCE		39	/* Motorola RCE */
X#define	EM_ARM		40	/* Advanced RISC Machines ARM */
X#define	EM_ALPHA	41	/* DIGITAL Alpha */
X#define	EM_SH		42	/* Hitachi Super-H */
X#define	EM_SPARCV9	43	/* SPARC Version 9 */
X#define	EM_TRICORE	44	/* Siemens Tricore */
X#define	EM_ARC		45	/* Argonaut RISC Core */
X#define	EM_H8_300	46	/* Hitachi H8/300 */
X#define	EM_H8_300H	47	/* Hitachi H8/300H */
X#define	EM_H8S		48	/* Hitachi H8S */
X#define	EM_H8_500	49	/* Hitachi H8/500 */
X#define	EM_IA_64	50	/* Intel Merced Processor */
X#define	EM_MIPS_X	51	/* Stanford MIPS-X */
X#define	EM_COLDFIRE	52	/* Motorola Coldfire */
X#define	EM_68HC12	53	/* Motorola MC68HC12 */
X#define	EM_VAX		75	/* DIGITAL VAX */
X#define	EM_ALPHA_EXP	36902	/* used by NetBSD/alpha; obsolete */
X#define	EM_NUM		36903
X
X/*
X * Program Header
X */
Xtypedef struct {
X	Elf32_Word	p_type;		/* entry type */
X	Elf32_Off	p_offset;	/* offset */
X	Elf32_Addr	p_vaddr;	/* virtual address */
X	Elf32_Addr	p_paddr;	/* physical address */
X	Elf32_Word	p_filesz;	/* file size */
X	Elf32_Word	p_memsz;	/* memory size */
X	Elf32_Word	p_flags;	/* flags */
X	Elf32_Word	p_align;	/* memory & file alignment */
X} Elf32_Phdr;
X
Xtypedef struct {
X	Elf64_Half	p_type;		/* entry type */
X	Elf64_Half	p_flags;	/* flags */
X	Elf64_Off	p_offset;	/* offset */
X	Elf64_Addr	p_vaddr;	/* virtual address */
X	Elf64_Addr	p_paddr;	/* physical address */
X	Elf64_Xword	p_filesz;	/* file size */
X	Elf64_Xword	p_memsz;	/* memory size */
X	Elf64_Xword	p_align;	/* memory & file alignment */
X} Elf64_Phdr;
X
X/* p_type */
X#define	PT_NULL		0		/* Program header table entry unused */
X#define	PT_LOAD		1		/* Loadable program segment */
X#define	PT_DYNAMIC	2		/* Dynamic linking information */
X#define	PT_INTERP	3		/* Program interpreter */
X#define	PT_NOTE		4		/* Auxiliary information */
X#define	PT_SHLIB	5		/* Reserved, unspecified semantics */
X#define	PT_PHDR		6		/* Entry for header table itself */
X#define	PT_NUM		7
X
X/* p_flags */
X#define	PF_R		0x4	/* Segment is readable */
X#define	PF_W		0x2	/* Segment is writable */
X#define	PF_X		0x1	/* Segment is executable */
X
X#define	PF_MASKOS	0x0ff00000	/* Opersting system specific values */
X#define	PF_MASKPROC	0xf0000000	/* Processor-specific values */
X
X#define	PT_LOPROC	0x70000000	/* Processor-specific range */
X#define	PT_HIPROC	0x7fffffff
X
X#define	PT_MIPS_REGINFO	0x70000000
X
X/*
X * Section Headers
X */
Xtypedef struct {
X	Elf32_Word	sh_name;	/* section name (.shstrtab index) */
X	Elf32_Word	sh_type;	/* section type */
X	Elf32_Word	sh_flags;	/* section flags */
X	Elf32_Addr	sh_addr;	/* virtual address */
X	Elf32_Off	sh_offset;	/* file offset */
X	Elf32_Word	sh_size;	/* section size */
X	Elf32_Word	sh_link;	/* link to another */
X	Elf32_Word	sh_info;	/* misc info */
X	Elf32_Word	sh_addralign;	/* memory alignment */
X	Elf32_Word	sh_entsize;	/* table entry size */
X} Elf32_Shdr;
X
Xtypedef struct {
X	Elf64_Half	sh_name;	/* section name (.shstrtab index) */
X	Elf64_Half	sh_type;	/* section type */
X	Elf64_Xword	sh_flags;	/* section flags */
X	Elf64_Addr	sh_addr;	/* virtual address */
X	Elf64_Off	sh_offset;	/* file offset */
X	Elf64_Xword	sh_size;	/* section size */
X	Elf64_Half	sh_link;	/* link to another */
X	Elf64_Half	sh_info;	/* misc info */
X	Elf64_Xword	sh_addralign;	/* memory alignment */
X	Elf64_Xword	sh_entsize;	/* table entry size */
X} Elf64_Shdr;
X
X/* sh_type */
X#define	SHT_NULL	0
X#define	SHT_PROGBITS	1
X#define	SHT_SYMTAB	2
X#define	SHT_STRTAB	3
X#define	SHT_RELA	4
X#define	SHT_HASH	5
X#define	SHT_DYNAMIC	6
X#define	SHT_NOTE	7
X#define	SHT_NOBITS	8
X#define	SHT_REL		9
X#define	SHT_SHLIB	10
X#define	SHT_DYNSYM	11
X#define	SHT_NUM		12
X
X#define	SHT_LOOS	0x60000000	/* Operating system specific range */
X#define	SHT_HIOS	0x6fffffff
X#define	SHT_LOPROC	0x70000000	/* Processor-specific range */
X#define	SHT_HIPROC	0x7fffffff
X#define	SHT_LOUSER	0x80000000	/* Application-specific range */
X#define	SHT_HIUSER	0xffffffff
X
X/* sh_flags */
X#define	SHF_WRITE	0x1		/* Section contains writable data */
X#define	SHF_ALLOC	0x2		/* Section occupies memory */
X#define	SHF_EXECINSTR	0x4		/* Section contains executable insns */
X
X#define	SHF_MASKOS	0x0f000000	/* Operating system specific values */
X#define	SHF_MASKPROC	0xf0000000	/* Processor-specific values */
X
X/*
X * Symbol Table
X */
Xtypedef struct {
X	Elf32_Word	st_name;	/* Symbol name (.symtab index) */
X	Elf32_Word	st_value;	/* value of symbol */
X	Elf32_Word	st_size;	/* size of symbol */
X	Elf_Byte	st_info;	/* type / binding attrs */
X	Elf_Byte	st_other;	/* unused */
X	Elf32_Half	st_shndx;	/* section index of symbol */
X} Elf32_Sym;
X
Xtypedef struct {
X	Elf64_Half	st_name;	/* Symbol name (.symtab index) */
X	Elf_Byte	st_info;	/* type / binding attrs */
X	Elf_Byte	st_other;	/* unused */
X	Elf64_Quarter	st_shndx;	/* section index of symbol */
X	Elf64_Addr	st_value;	/* value of symbol */
X	Elf64_Xword	st_size;	/* size of symbol */
X} Elf64_Sym;
X
X/* Symbol Table index of the undefined symbol */
X#define	ELF_SYM_UNDEFINED	0
X
X/* st_info: Symbol Bindings */
X#define	STB_LOCAL		0	/* local symbol */
X#define	STB_GLOBAL		1	/* global symbol */
X#define	STB_WEAK		2	/* weakly defined global symbol */
X#define	STB_NUM			3
X
X#define	STB_LOOS		10	/* Operating system specific range */
X#define	STB_HIOS		12
X#define	STB_LOPROC		13	/* Processor-specific range */
X#define	STB_HIPROC		15
X
X/* st_info: Symbol Types */
X#define	STT_NOTYPE		0	/* Type not specified */
X#define	STT_OBJECT		1	/* Associated with a data object */
X#define	STT_FUNC		2	/* Associated with a function */
X#define	STT_SECTION		3	/* Associated with a section */
X#define	STT_FILE		4	/* Associated with a file name */
X#define	STT_NUM			5
X
X#define	STT_LOOS		10	/* Operating system specific range */
X#define	STT_HIOS		12
X#define	STT_LOPROC		13	/* Processor-specific range */
X#define	STT_HIPROC		15
X
X/* st_info utility macros */
X#define	ELF32_ST_BIND(info)		((Elf32_Word)(info) >> 4)
X#define	ELF32_ST_TYPE(info)		((Elf32_Word)(info) & 0xf)
X#define	ELF32_ST_INFO(bind,type)	((Elf_Byte)(((bind) << 4) | ((type) & 0xf)))
X
X#define	ELF64_ST_BIND(info)		((Elf64_Xword)(info) >> 4)
X#define	ELF64_ST_TYPE(info)		((Elf64_Xword)(info) & 0xf)
X#define	ELF64_ST_INFO(bind,type)	((Elf_Byte)(((bind) << 4) | ((type) & 0xf)))
X
X/*
X * Special section indexes
X */
X#define	SHN_UNDEF	0		/* Undefined section */
X
X#define	SHN_LORESERVE	0xff00		/* Reserved range */
X#define	SHN_ABS		0xfff1		/*  Absolute symbols */
X#define	SHN_COMMON	0xfff2		/*  Common symbols */
X#define	SHN_HIRESERVE	0xffff
X
X#define	SHN_LOPROC	0xff00		/* Processor-specific range */
X#define	SHN_HIPROC	0xff1f
X#define	SHN_LOOS	0xff20		/* Operating system specific range */
X#define	SHN_HIOS	0xff3f
X
X#define	SHN_MIPS_ACOMMON 0xff00
X#define	SHN_MIPS_TEXT	0xff01
X#define	SHN_MIPS_DATA	0xff02
X#define	SHN_MIPS_SCOMMON 0xff03
X
X/*
X * Relocation Entries
X */
Xtypedef struct {
X	Elf32_Word	r_offset;	/* where to do it */
X	Elf32_Word	r_info;		/* index & type of relocation */
X} Elf32_Rel;
X
Xtypedef struct {
X	Elf32_Word	r_offset;	/* where to do it */
X	Elf32_Word	r_info;		/* index & type of relocation */
X	Elf32_Sword	r_addend;	/* adjustment value */
X} Elf32_RelA;
X
X/* r_info utility macros */
X#define	ELF32_R_SYM(info)	((info) >> 8)
X#define	ELF32_R_TYPE(info)	((info) & 0xff)
X#define	ELF32_R_INFO(sym, type)	(((sym) << 8) + (unsigned char)(type))
X
Xtypedef struct {
X	Elf64_Addr	r_offset;	/* where to do it */
X	Elf64_Xword	r_info;		/* index & type of relocation */
X} Elf64_Rel;
X
Xtypedef struct {
X	Elf64_Addr	r_offset;	/* where to do it */
X	Elf64_Xword	r_info;		/* index & type of relocation */
X	Elf64_Sxword	r_addend;	/* adjustment value */
X} Elf64_RelA;
X
X/* r_info utility macros */
X#define	ELF64_R_SYM(info)	((info) >> 32)
X#define	ELF64_R_TYPE(info)	((info) & 0xffffffff)
X#define	ELF64_R_INFO(sym,type)	(((sym) << 32) + (type))
X
X/*
X * Dynamic Section structure array
X */
Xtypedef struct {
X	Elf32_Word	d_tag;		/* entry tag value */
X	union {
X	    Elf32_Addr	d_ptr;
X	    Elf32_Word	d_val;
X	} d_un;
X} Elf32_Dyn;
X
Xtypedef struct {
X	Elf64_Xword	d_tag;		/* entry tag value */
X	union {
X	    Elf64_Addr	d_ptr;
X	    Elf64_Xword	d_val;
X	} d_un;
X} Elf64_Dyn;
X
X/* d_tag */
X#define	DT_NULL		0	/* Marks end of dynamic array */
X#define	DT_NEEDED	1	/* Name of needed library (DT_STRTAB offset) */
X#define	DT_PLTRELSZ	2	/* Size, in bytes, of relocations in PLT */
X#define	DT_PLTGOT	3	/* Address of PLT and/or GOT */
X#define	DT_HASH		4	/* Address of symbol hash table */
X#define	DT_STRTAB	5	/* Address of string table */
X#define	DT_SYMTAB	6	/* Address of symbol table */
X#define	DT_RELA		7	/* Address of Rela relocation table */
X#define	DT_RELASZ	8	/* Size, in bytes, of DT_RELA table */
X#define	DT_RELAENT	9	/* Size, in bytes, of one DT_RELA entry */
X#define	DT_STRSZ	10	/* Size, in bytes, of DT_STRTAB table */
X#define	DT_SYMENT	11	/* Size, in bytes, of one DT_SYMTAB entry */
X#define	DT_INIT		12	/* Address of initialization function */
X#define	DT_FINI		13	/* Address of termination function */
X#define	DT_SONAME	14	/* Shared object name (DT_STRTAB offset) */
X#define	DT_RPATH	15	/* Library search path (DT_STRTAB offset) */
X#define	DT_SYMBOLIC	16	/* Start symbol search within local object */
X#define	DT_REL		17	/* Address of Rel relocation table */
X#define	DT_RELSZ	18	/* Size, in bytes, of DT_REL table */
X#define	DT_RELENT	19	/* Size, in bytes, of one DT_REL entry */
X#define	DT_PLTREL	20 	/* Type of PLT relocation entries */
X#define	DT_DEBUG	21	/* Used for debugging; unspecified */
X#define	DT_TEXTREL	22	/* Relocations might modify non-writable seg */
X#define	DT_JMPREL	23	/* Address of relocations associated with PLT */
X#define	DT_BIND_NOW	24	/* Process all relocations at load-time */
X#define	DT_INIT_ARRAY	25	/* Address of initialization function array */
X#define	DT_FINI_ARRAY	26	/* Size, in bytes, of DT_INIT_ARRAY array */
X#define	DT_INIT_ARRAYSZ	27	/* Address of termination function array */
X#define	DT_FINI_ARRAYSZ	28	/* Size, in bytes, of DT_FINI_ARRAY array*/
X#define	DT_NUM		29
X
X#define	DT_LOOS		0x60000000	/* Operating system specific range */
X#define	DT_HIOS		0x6fffffff
X#define	DT_LOPROC	0x70000000	/* Processor-specific range */
X#define	DT_HIPROC	0x7fffffff
X
X/*
X * Auxiliary Vectors
X */
Xtypedef struct {
X	Elf32_Word	a_type;				/* 32-bit id */
X	Elf32_Word	a_v;				/* 32-bit id */
X} Aux32Info;
X
Xtypedef struct {
X	Elf64_Half	a_type;				/* 32-bit id */
X	Elf64_Xword	a_v;				/* 64-bit id */
X} Aux64Info;
X
X/* a_type */
X#define	AT_NULL		0	/* Marks end of array */
X#define	AT_IGNORE	1	/* No meaning, a_un is undefined */
X#define	AT_EXECFD	2	/* Open file descriptor of object file */
X#define	AT_PHDR		3	/* &phdr[0] */
X#define	AT_PHENT	4	/* sizeof(phdr[0]) */
X#define	AT_PHNUM	5	/* # phdr entries */
X#define	AT_PAGESZ	6	/* PAGESIZE */
X#define	AT_BASE		7	/* Interpreter base addr */
X#define	AT_FLAGS	8	/* Processor flags */
X#define	AT_ENTRY	9	/* Entry address of executable */
X#define	AT_DCACHEBSIZE	10	/* Data cache block size */
X#define	AT_ICACHEBSIZE	11	/* Instruction cache block size */
X#define	AT_UCACHEBSIZE	12	/* Unified cache block size */
X
X	/* Vendor specific */
X#define	AT_MIPS_NOTELF	10	/* XXX a_val != 0 -> MIPS XCOFF executable */
X
X#define	AT_SUN_UID	2000	/* euid */
X#define	AT_SUN_RUID	2001	/* ruid */
X#define	AT_SUN_GID	2002	/* egid */
X#define	AT_SUN_RGID	2003	/* rgid */
X
X	/* Solaris kernel specific */
X#define	AT_SUN_LDELF	2004	/* dynamic linker's ELF header */
X#define	AT_SUN_LDSHDR	2005	/* dynamic linker's section header */
X#define	AT_SUN_LDNAME	2006	/* dynamic linker's name */
X#define	AT_SUN_LPGSIZE	2007	/* large pagesize */
X
X	/* Other information */
X#define	AT_SUN_PLATFORM	2008	/* sysinfo(SI_PLATFORM) */
X#define	AT_SUN_HWCAP	2009	/* process hardware capabilities */
X#define	AT_SUN_IFLUSH	2010	/* do we need to flush the instruction cache? */
X#define	AT_SUN_CPU	2011	/* cpu name */
X	/* ibcs2 emulation band aid */
X#define	AT_SUN_EMUL_ENTRY 2012	/* coff entry point */
X#define	AT_SUN_EMUL_EXECFD 2013	/* coff file descriptor */
X	/* Executable's fully resolved name */
X#define	AT_SUN_EXECNAME	2014
X
X/*
X * Note Headers
X */
Xtypedef struct {
X	Elf32_Word n_namesz;
X	Elf32_Word n_descsz;
X	Elf32_Word n_type;
X} Elf32_Nhdr;
X
Xtypedef struct {
X	Elf64_Half n_namesz;
X	Elf64_Half n_descsz;
X	Elf64_Half n_type;
X} Elf64_Nhdr;
X
X#define	ELF_NOTE_TYPE_OSVERSION		1
X
X/* NetBSD-specific note type: OS Version.  desc is 4-byte NetBSD integer. */
X#define	ELF_NOTE_NETBSD_TYPE_OSVERSION	ELF_NOTE_TYPE_OSVERSION
X
X/* NetBSD-specific note type: Emulation name.  desc is emul name string. */
X#define	ELF_NOTE_NETBSD_TYPE_EMULNAME	2
X
X/* NetBSD-specific note name and description sizes */
X#define	ELF_NOTE_NETBSD_NAMESZ		7
X#define	ELF_NOTE_NETBSD_DESCSZ		4
X/* NetBSD-specific note name */
X#define	ELF_NOTE_NETBSD_NAME		"NetBSD\0\0"
X
X/* GNU-specific note name and description sizes */
X#define	ELF_NOTE_GNU_NAMESZ		4
X#define	ELF_NOTE_GNU_DESCSZ		4
X/* GNU-specific note name */
X#define	ELF_NOTE_GNU_NAME		"GNU\0"
X
X/* GNU-specific OS/version value stuff */
X#define	ELF_NOTE_GNU_OSMASK		(uint32_t)0xff000000
X#define	ELF_NOTE_GNU_OSLINUX		(uint32_t)0x01000000
X#define	ELF_NOTE_GNU_OSMACH		(uint32_t)0x00000000
X
X#if defined(ELFSIZE)
X#define	CONCAT(x,y)	__CONCAT(x,y)
X#define	ELFNAME(x)	CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
X#define	ELFNAME2(x,y)	CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
X#define	ELFNAMEEND(x)	CONCAT(x,CONCAT(_elf,ELFSIZE))
X#define	ELFDEFNNAME(x)	CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
X#endif
X
X#include <machine/elf_machdep.h>
X
X#if defined(ELFSIZE) && (ELFSIZE == 32)
X#define	Elf_Ehdr	Elf32_Ehdr
X#define	Elf_Phdr	Elf32_Phdr
X#define	Elf_Shdr	Elf32_Shdr
X#define	Elf_Sym		Elf32_Sym
X#define	Elf_Rel		Elf32_Rel
X#define	Elf_RelA	Elf32_RelA
X#define	Elf_Dyn		Elf32_Dyn
X#define	Elf_Word	Elf32_Word
X#define	Elf_Sword	Elf32_Sword
X#define	Elf_Addr	Elf32_Addr
X#define	Elf_Off		Elf32_Off
X#define	Elf_Nhdr	Elf32_Nhdr
X
X#define	ELF_R_SYM	ELF32_R_SYM
X#define	ELF_R_TYPE	ELF32_R_TYPE
X#define	ELFCLASS	ELFCLASS32
X
X#define	ELF_ST_BIND	ELF32_ST_BIND
X#define	ELF_ST_TYPE	ELF32_ST_TYPE
X#define	ELF_ST_INFO	ELF32_ST_INFO
X
X#define	AuxInfo		Aux32Info
X#elif defined(ELFSIZE) && (ELFSIZE == 64)
X#define	Elf_Ehdr	Elf64_Ehdr
X#define	Elf_Phdr	Elf64_Phdr
X#define	Elf_Shdr	Elf64_Shdr
X#define	Elf_Sym		Elf64_Sym
X#define	Elf_Rel		Elf64_Rel
X#define	Elf_RelA	Elf64_RelA
X#define	Elf_Dyn		Elf64_Dyn
X#define	Elf_Word	Elf64_Word
X#define	Elf_Sword	Elf64_Sword
X#define	Elf_Addr	Elf64_Addr
X#define	Elf_Off		Elf64_Off
X#define	Elf_Nhdr	Elf64_Nhdr
X
X#define	ELF_R_SYM	ELF64_R_SYM
X#define	ELF_R_TYPE	ELF64_R_TYPE
X#define	ELFCLASS	ELFCLASS64
X
X#define	ELF_ST_BIND	ELF64_ST_BIND
X#define	ELF_ST_TYPE	ELF64_ST_TYPE
X#define	ELF_ST_INFO	ELF64_ST_INFO
X
X#define	AuxInfo		Aux64Info
X#endif
X
X#ifdef _KERNEL
X
X#define ELF_AUX_ENTRIES	8		/* Size of aux array passed to loader */
X#define ELF32_NO_ADDR	(~(Elf32_Addr)0) /* Indicates addr. not yet filled in */
X#define ELF64_NO_ADDR	(~(Elf64_Addr)0) /* Indicates addr. not yet filled in */
X
X#if defined(ELFSIZE) && (ELFSIZE == 64)
X#define ELF_NO_ADDR	ELF64_NO_ADDR
X#elif defined(ELFSIZE) && (ELFSIZE == 32)
X#define ELF_NO_ADDR	ELF32_NO_ADDR
X#endif
X
X#if defined(ELFSIZE)
Xstruct elf_args {
X        Elf_Addr  arg_entry;      /* program entry point */
X        Elf_Addr  arg_interp;     /* Interpreter load address */
X        Elf_Addr  arg_phaddr;     /* program header address */
X        Elf_Addr  arg_phentsize;  /* Size of program header */
X        Elf_Addr  arg_phnum;      /* Number of program headers */
X};
X#endif
X
X#ifndef _LKM
X#include "opt_execfmt.h"
X#endif
X
X#ifdef EXEC_ELF32
Xint	exec_elf32_makecmds __P((struct proc *, struct exec_package *));
Xint	elf32_read_from __P((struct proc *, struct vnode *, u_long,
X	    caddr_t, int));
Xvoid	*elf32_copyargs __P((struct exec_package *, struct ps_strings *,
X	    void *, void *));
X#endif
X
X#ifdef EXEC_ELF64
Xint	exec_elf64_makecmds __P((struct proc *, struct exec_package *));
Xint	elf64_read_from __P((struct proc *, struct vnode *, u_long,
X	    caddr_t, int));
Xvoid	*elf64_copyargs __P((struct exec_package *, struct ps_strings *,
X	    void *, void *));
X#endif
X
X/* common */
Xint	exec_elf_setup_stack __P((struct proc *, struct exec_package *));
X
X#endif /* _KERNEL */
X
X#endif /* !_SYS_EXEC_ELF_H_ */
END-of-elf.h
echo x - elf2ecoff.c
sed 's/^X//' >elf2ecoff.c << 'END-of-elf2ecoff.c'
X/*
X * Copyright (c) 1995
X *     Ted Lemon (hereinafter referred to as the author)
X *
X * Redistribution and use in source and binary forms, with or without
X * modification, are permitted provided that the following conditions
X * are met:
X * 1. Redistributions of source code must retain the above copyright
X *    notice, this list of conditions and the following disclaimer.
X * 2. Redistributions in binary form must reproduce the above copyright
X *    notice, this list of conditions and the following disclaimer in the
X *    documentation and/or other materials provided with the distribution.
X * 3. The name of the author may not be used to endorse or promote products
X *    derived from this software without specific prior written permission.
X *
X * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
X * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
X * SUCH DAMAGE.
X */
X
X/* elf2ecoff.c
X
X   This program converts an elf executable to an ECOFF executable.
X   No symbol table is retained.   This is useful primarily in building
X   net-bootable kernels for machines (e.g., DECstation and Alpha) which
X   only support the ECOFF object file format. */
X
X#include <stdio.h>
X#include <string.h>
X#include <errno.h>
X#include <sys/types.h>
X#include <fcntl.h>
X#include <unistd.h>
X#include <limits.h>
X
X#include "elf.h"
X#include "ecoff.h"
X
X/*
X * Some extra ELF definitions
X * MIPS hosts may already have definitions, so we define it conditional.
X */
X#ifndef PT_MIPS_REGINFO
X#define PT_MIPS_REGINFO 0x70000000      /* Register usage information */
X#endif
X
X/* -------------------------------------------------------------------- */
X
Xstruct sect {
X  unsigned long vaddr;
X  unsigned long len;
X};
X
Xint phcmp ();
Xchar *saveRead (int file, off_t offset, off_t len, char *name);
Xint copy (int, int, off_t, off_t);
Xint translate_syms (int, int, off_t, off_t, off_t, off_t);
Xvoid convert_elf_hdr (Elf32_Ehdr *);
Xvoid convert_elf_phdrs (Elf32_Phdr *, int);
Xvoid convert_elf_shdrs (Elf32_Shdr *, int);
Xvoid convert_ecoff_filehdr(struct filehdr *);
Xvoid convert_ecoff_aouthdr(struct aouthdr *);
Xvoid convert_ecoff_esecs(struct scnhdr *, int);
Xextern int errno;
Xint *symTypeTable;
Xint must_convert_endian = 0;
Xint format_bigendian = 0;
X
Xmain (int argc, char **argv, char **envp)
X{
X  Elf32_Ehdr ex;
X  Elf32_Phdr *ph;
X  Elf32_Shdr *sh;
X  Elf32_Sym *symtab;
X  char *shstrtab;
X  int strtabix, symtabix;
X  int i, pad;
X  struct sect text, data, bss;
X  struct filehdr efh;
X  struct aouthdr eah;
X  struct scnhdr esecs [6];
X  int infile, outfile;
X  unsigned long cur_vma = ULONG_MAX;
X  int addflag = 0;
X  int nosecs;
X
X  text.len = data.len = bss.len = 0;
X  text.vaddr = data.vaddr = bss.vaddr = 0;
X
X  /* Check args... */
X  if (argc < 3 || argc > 4)
X    {
X    usage:
X      fprintf (stderr,
X              "usage: elf2aout <elf executable> <a.out executable> [-a]\n");
X      exit (1);
X    }
X  if (argc == 4)
X    {
X      if (strcmp (argv [3], "-a"))
X       goto usage;
X      addflag = 1;
X    }
X
X  /* Try the input file... */
X  if ((infile = open (argv [1], O_RDONLY)) < 0)
X    {
X      fprintf (stderr, "Can't open %s for read: %s\n",
X              argv [1], strerror (errno));
X      exit (1);
X    }
X
X  /* Read the header, which is at the beginning of the file... */
X  i = read (infile, &ex, sizeof ex);
X  if (i != sizeof ex)
X    {
X      fprintf (stderr, "ex: %s: %s.\n",
X              argv [1], i ? strerror (errno) : "End of file reached");
X      exit (1);
X    }
X
X  if (ex.e_ident[EI_DATA] == ELFDATA2MSB)
X       format_bigendian = 1;
X
X  if (ntohs (0xaa55) == 0xaa55) {
X       if (!format_bigendian)
X               must_convert_endian = 1;
X  } else {
X       if (format_bigendian)
X               must_convert_endian = 1;
X  }
X  if (must_convert_endian)
X       convert_elf_hdr (&ex);
X
X  /* Read the program headers... */
X  ph = (Elf32_Phdr *)saveRead (infile, ex.e_phoff,
X                               ex.e_phnum * sizeof (Elf32_Phdr), "ph");
X  if (must_convert_endian)
X       convert_elf_phdrs (ph, ex.e_phnum);
X  /* Read the section headers... */
X  sh = (Elf32_Shdr *)saveRead (infile, ex.e_shoff,
X                               ex.e_shnum * sizeof (Elf32_Shdr), "sh");
X  if (must_convert_endian)
X       convert_elf_shdrs (sh, ex.e_shnum);
X  /* Read in the section string table. */
X  shstrtab = saveRead (infile, sh [ex.e_shstrndx].sh_offset,
X                      sh [ex.e_shstrndx].sh_size, "shstrtab");
X
X  /* Figure out if we can cram the program header into an ECOFF
X     header...  Basically, we can't handle anything but loadable
X     segments, but we can ignore some kinds of segments.  We can't
X     handle holes in the address space.  Segments may be out of order,
X     so we sort them first. */
X
X  qsort (ph, ex.e_phnum, sizeof (Elf32_Phdr), phcmp);
X
X  for (i = 0; i < ex.e_phnum; i++)
X    {
X      /* Section types we can ignore... */
X      if (ph [i].p_type == PT_NULL || ph [i].p_type == PT_NOTE ||
X         ph [i].p_type == PT_PHDR || ph [i].p_type == PT_MIPS_REGINFO)
X       continue;
X      /* Section types we can't handle... */
X      else if (ph [i].p_type != PT_LOAD)
X        {
X         fprintf (stderr, "Program header %d type %d can't be converted.\n", i, ph[i].p_type);
X         exit (1);
X       }
X      /* Writable (data) segment? */
X      if (ph [i].p_flags & PF_W)
X       {
X         struct sect ndata, nbss;
X
X         ndata.vaddr = ph [i].p_vaddr;
X         ndata.len = ph [i].p_filesz;
X         nbss.vaddr = ph [i].p_vaddr + ph [i].p_filesz;
X         nbss.len = ph [i].p_memsz - ph [i].p_filesz;
X
X         combine (&data, &ndata, 0);
X         combine (&bss, &nbss, 1);
X       }
X      else
X       {
X         struct sect ntxt;
X
X         ntxt.vaddr = ph [i].p_vaddr;
X         ntxt.len = ph [i].p_filesz;
X
X         combine (&text, &ntxt, 0);
X       }
X      /* Remember the lowest segment start address. */
X      if (ph [i].p_vaddr < cur_vma)
X       cur_vma = ph [i].p_vaddr;
X    }
X
X  /* Sections must be in order to be converted... */
X  if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
X      text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr)
X    {
X      fprintf (stderr, "Sections ordering prevents a.out conversion.\n");
X      exit (1);
X    }
X
X  /* If there's a data section but no text section, then the loader
X     combined everything into one section.   That needs to be the
X     text section, so just make the data section zero length following
X     text. */
X  if (data.len && !text.len)
X    {
X      text = data;
X      data.vaddr = text.vaddr + text.len;
X      data.len = 0;
X    }
X
X  /* If there is a gap between text and data, we'll fill it when we copy
X     the data, so update the length of the text segment as represented in
X     a.out to reflect that, since a.out doesn't allow gaps in the program
X     address space. */
X  if (text.vaddr + text.len < data.vaddr)
X    text.len = data.vaddr - text.vaddr;
X
X  /* We now have enough information to cons up an a.out header... */
X  eah.magic = OMAGIC;
X  eah.vstamp = 200;
X  eah.tsize = text.len;
X  eah.dsize = data.len;
X  eah.bsize = bss.len;
X  eah.entry = ex.e_entry;
X  eah.text_start = text.vaddr;
X  eah.data_start = data.vaddr;
X  eah.bss_start = bss.vaddr;
X  eah.gprmask = 0xf3fffffe;
X  memset (&eah.cprmask, '\0', sizeof eah.cprmask);
X  eah.gp_value = 0; /* unused. */
X
X  if (format_bigendian)
X    efh.f_magic = MIPSEBMAGIC;
X  else
X    efh.f_magic = MIPSELMAGIC;
X  if (addflag)
X    nosecs = 6;
X  else
X    nosecs = 3;
X  efh.f_nscns = nosecs;
X  efh.f_timdat = 0;    /* bogus */
X  efh.f_symptr = 0;
X  efh.f_nsyms = 0;
X  efh.f_opthdr = sizeof eah;
X  efh.f_flags = 0x100f; /* Stripped, not sharable. */
X
X  memset (esecs, 0, sizeof esecs);
X  strcpy (esecs [0].s_name, ".text");
X  strcpy (esecs [1].s_name, ".data");
X  strcpy (esecs [2].s_name, ".bss");
X  if (addflag) {
X    strcpy (esecs [3].s_name, ".rdata");
X    strcpy (esecs [4].s_name, ".sdata");
X    strcpy (esecs [5].s_name, ".sbss");
X  }
X  esecs [0].s_paddr = esecs [0].s_vaddr = eah.text_start;
X  esecs [1].s_paddr = esecs [1].s_vaddr = eah.data_start;
X  esecs [2].s_paddr = esecs [2].s_vaddr = eah.bss_start;
X  if (addflag) {
X    esecs [3].s_paddr = esecs [3].s_vaddr = 0;
X    esecs [4].s_paddr = esecs [4].s_vaddr = 0;
X    esecs [5].s_paddr = esecs [5].s_vaddr = 0;
X  }
X  esecs [0].s_size = eah.tsize;
X  esecs [1].s_size = eah.dsize;
X  esecs [2].s_size = eah.bsize;
X  if (addflag) {
X    esecs [3].s_size = 0;
X    esecs [4].s_size = 0;
X    esecs [5].s_size = 0;
X  }
X  esecs [0].s_scnptr = N_TXTOFF (efh, eah);
X  esecs [1].s_scnptr = N_DATOFF (efh, eah);
X#define ECOFF_SEGMENT_ALIGNMENT(a) 0x10
X#define ECOFF_ROUND(s,a) (((s)+(a)-1)&~((a)-1))
X  esecs [2].s_scnptr = esecs [1].s_scnptr +
X         ECOFF_ROUND (esecs [1].s_size, ECOFF_SEGMENT_ALIGNMENT (&eah));
X  if (addflag) {
X    esecs [3].s_scnptr = 0;
X    esecs [4].s_scnptr = 0;
X    esecs [5].s_scnptr = 0;
X  }
X  esecs [0].s_relptr = esecs [1].s_relptr
X         = esecs [2].s_relptr = 0;
X  esecs [0].s_lnnoptr = esecs [1].s_lnnoptr
X         = esecs [2].s_lnnoptr = 0;
X  esecs [0].s_nreloc = esecs [1].s_nreloc = esecs [2].s_nreloc = 0;
X  esecs [0].s_nlnno = esecs [1].s_nlnno = esecs [2].s_nlnno = 0;
X  if (addflag) {
X    esecs [3].s_relptr = esecs [4].s_relptr
X         = esecs [5].s_relptr = 0;
X    esecs [3].s_lnnoptr = esecs [4].s_lnnoptr
X         = esecs [5].s_lnnoptr = 0;
X    esecs [3].s_nreloc = esecs [4].s_nreloc = esecs [5].s_nreloc = 0;
X    esecs [3].s_nlnno = esecs [4].s_nlnno = esecs [5].s_nlnno = 0;
X  }
X  esecs [0].s_flags = 0x20;
X  esecs [1].s_flags = 0x40;
X  esecs [2].s_flags = 0x82;
X  if (addflag) {
X    esecs [3].s_flags = 0x100;
X    esecs [4].s_flags = 0x200;
X    esecs [5].s_flags = 0x400;
X  }
X
X  /* Make the output file... */
X  if ((outfile = open (argv [2], O_WRONLY | O_CREAT, 0777)) < 0)
X    {
X      fprintf (stderr, "Unable to create %s: %s\n", argv [2], strerror (errno));
X      exit (1);
X    }
X
X  if (must_convert_endian)
X       convert_ecoff_filehdr (&efh);
X  /* Write the headers... */
X  i = write (outfile, &efh, sizeof efh);
X  if (i != sizeof efh)
X    {
X      perror ("efh: write");
X      exit (1);
X
X    for (i = 0; i < nosecs; i++)
X      {
X        printf ("Section %d: %s phys %x  size %x  file offset %x\n",
X             i, esecs [i].s_name, esecs [i].s_paddr,
X             esecs [i].s_size, esecs [i].s_scnptr);
X      }
X    }
X  fprintf (stderr, "wrote %d byte file header.\n", i);
X
X  if (must_convert_endian)
X       convert_ecoff_aouthdr (&eah);
X  i = write (outfile, &eah, sizeof eah);
X  if (i != sizeof eah)
X    {
X      perror ("eah: write");
X      exit (1);
X    }
X  fprintf (stderr, "wrote %d byte a.out header.\n", i);
X
X  if (must_convert_endian)
X       convert_ecoff_esecs (&esecs[0], nosecs);
X  i = write (outfile, &esecs, nosecs * sizeof(struct scnhdr));
X  if (i != nosecs * sizeof(struct scnhdr))
X    {
X      perror ("esecs: write");
X      exit (1);
X    }
X  fprintf (stderr, "wrote %d bytes of section headers.\n", i);
X
X  if (pad = ((sizeof efh + sizeof eah + nosecs * sizeof(struct scnhdr)) & 15))
X    {
X      pad = 16 - pad;
X      i = write (outfile, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", pad);
X      if (i < 0)
X       {
X         perror ("ipad: write");
X         exit (1);
X       }
X      fprintf (stderr, "wrote %d byte pad.\n", i);
X    }
X
X  /* Copy the loadable sections.   Zero-fill any gaps less than 64k;
X     complain about any zero-filling, and die if we're asked to zero-fill
X     more than 64k. */
X  for (i = 0; i < ex.e_phnum; i++)
X    {
X      /* Unprocessable sections were handled above, so just verify that
X        the section can be loaded before copying. */
X      if (ph [i].p_type == PT_LOAD && ph [i].p_filesz)
X       {
X         if (cur_vma != ph [i].p_vaddr)
X           {
X             unsigned long gap = ph [i].p_vaddr - cur_vma;
X             char obuf [1024];
X             if (gap > 65536)
X               {
X                 fprintf (stderr, "Intersegment gap (%d bytes) too large.\n",
X                          gap);
X                 exit (1);
X               }
X             fprintf (stderr, "Warning: %d byte intersegment gap.\n", gap);
X             memset (obuf, 0, sizeof obuf);
X             while (gap)
X               {
X                 int count = write (outfile, obuf, (gap > sizeof obuf
X                                                    ? sizeof obuf : gap));
X                 if (count < 0)
X                   {
X                     fprintf (stderr, "Error writing gap: %s\n",
X                              strerror (errno));
X                     exit (1);
X                   }
X                 gap -= count;
X               }
X           }
Xfprintf (stderr, "writing %d bytes...\n", ph [i].p_filesz);
X         copy (outfile, infile, ph [i].p_offset, ph [i].p_filesz);
X         cur_vma = ph [i].p_vaddr + ph [i].p_filesz;
X       }
X    }
X
X   /*
X     * Write a page of padding for boot PROMS that read entire pages.
X     * Without this, they may attempt to read past the end of the
X     * data section, incur an error, and refuse to boot.
X     */
X    {
X       char obuf[4096];
X       memset(obuf, 0, sizeof obuf);
X       if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) {
X           fprintf(stderr, "Error writing PROM padding: %s\n",
X                   strerror(errno));
X           exit(1);
X       }
X    }
X
X  /* Looks like we won... */
X  exit (0);
X}
X
Xcopy (out, in, offset, size)
X     int out, in;
X     off_t offset, size;
X{
X  char ibuf [4096];
X  int remaining, cur, count;
X
X  /* Go the the start of the ELF symbol table... */
X  if (lseek (in, offset, SEEK_SET) < 0)
X    {
X      perror ("copy: lseek");
X      exit (1);
X    }
X
X  remaining = size;
X  while (remaining)
X    {
X      cur = remaining;
X      if (cur > sizeof ibuf)
X       cur = sizeof ibuf;
X      remaining -= cur;
X      if ((count = read (in, ibuf, cur)) != cur)
X       {
X         fprintf (stderr, "copy: read: %s\n",
X                  count ? strerror (errno) : "premature end of file");
X         exit (1);
X       }
X      if ((count = write (out, ibuf, cur)) != cur)
X       {
X         perror ("copy: write");
X         exit (1);
X       }
X    }
X}
X
X/* Combine two segments, which must be contiguous.   If pad is true, it's
X   okay for there to be padding between. */
Xcombine (base, new, pad)
X     struct sect *base, *new;
X     int pad;
X{
X  if (!base -> len)
X    *base = *new;
X  else if (new -> len)
X    {
X      if (base -> vaddr + base -> len != new -> vaddr)
X       {
X         if (pad)
X           base -> len = new -> vaddr - base -> vaddr;
X         else
X           {
X             fprintf (stderr,
X                      "Non-contiguous data can't be converted.\n");
X             exit (1);
X           }
X       }
X      base -> len += new -> len;
X    }
X}
X
Xphcmp (h1, h2)
X     Elf32_Phdr *h1, *h2;
X{
X  if (h1 -> p_vaddr > h2 -> p_vaddr)
X    return 1;
X  else if (h1 -> p_vaddr < h2 -> p_vaddr)
X    return -1;
X  else
X    return 0;
X}
X
Xchar *saveRead (int file, off_t offset, off_t len, char *name)
X{
X  char *tmp;
X  int count;
X  off_t off;
X  if ((off = lseek (file, offset, SEEK_SET)) < 0)
X    {
X      fprintf (stderr, "%s: fseek: %s\n", name, strerror (errno));
X      exit (1);
X    }
X  if (!(tmp = (char *)malloc (len)))
X    {
X      fprintf (stderr, "%s: Can't allocate %d bytes.\n", name, len);
X      exit (1);
X    }
X  count = read (file, tmp, len);
X  if (count != len)
X    {
X      fprintf (stderr, "%s: read: %s.\n",
X              name, count ? strerror (errno) : "End of file reached");
X      exit (1);
X    }
X  return tmp;
X}
X
X#define swab16(x) \
X       ((unsigned short)( \
X               (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
X               (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
X
X#define swab32(x) \
X       ((unsigned int)( \
X               (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
X               (((unsigned int)(x) & (unsigned int)0x0000ff00UL) <<  8) | \
X               (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >>  8) | \
X               (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
X
Xvoid convert_elf_hdr (Elf32_Ehdr *e)
X{
X       e->e_type      = swab16(e->e_type);
X       e->e_machine   = swab16(e->e_machine);
X       e->e_version   = swab32(e->e_version);
X       e->e_entry     = swab32(e->e_entry);
X       e->e_phoff     = swab32(e->e_phoff);
X       e->e_shoff     = swab32(e->e_shoff);
X       e->e_flags     = swab32(e->e_flags);
X       e->e_ehsize    = swab16(e->e_ehsize);
X       e->e_phentsize = swab16(e->e_phentsize);
X       e->e_phnum     = swab16(e->e_phnum);
X       e->e_shentsize = swab16(e->e_shentsize);
X       e->e_shnum     = swab16(e->e_shnum);
X       e->e_shstrndx  = swab16(e->e_shstrndx);
X}
X
Xvoid convert_elf_phdrs (Elf32_Phdr *p, int num)
X{
X       int i;
X
X       for (i = 0; i < num; i++,p++) {
X               p->p_type   = swab32(p->p_type);
X               p->p_offset = swab32(p->p_offset);
X               p->p_vaddr  = swab32(p->p_vaddr);
X               p->p_paddr  = swab32(p->p_paddr);
X               p->p_filesz = swab32(p->p_filesz);
X               p->p_memsz  = swab32(p->p_memsz);
X               p->p_flags  = swab32(p->p_flags);
X               p->p_align  = swab32(p->p_align);
X       }
X
X}
X
Xvoid convert_elf_shdrs (Elf32_Shdr *s, int num)
X{
X       int i;
X
X       for (i = 0; i < num; i++,s++) {
X               s->sh_name      = swab32(s->sh_name);
X               s->sh_type      = swab32(s->sh_type);
X               s->sh_flags     = swab32(s->sh_flags);
X               s->sh_addr      = swab32(s->sh_addr);
X               s->sh_offset    = swab32(s->sh_offset);
X               s->sh_size      = swab32(s->sh_size);
X               s->sh_link      = swab32(s->sh_link);
X               s->sh_info      = swab32(s->sh_info);
X               s->sh_addralign = swab32(s->sh_addralign);
X               s->sh_entsize   = swab32(s->sh_entsize);
X       }
X}
X
Xvoid convert_ecoff_filehdr(struct filehdr *f)
X{
X       f->f_magic  = swab16(f->f_magic);
X       f->f_nscns  = swab16(f->f_nscns);
X       f->f_timdat = swab32(f->f_timdat);
X       f->f_symptr = swab32(f->f_symptr);
X       f->f_nsyms  = swab32(f->f_nsyms);
X       f->f_opthdr = swab16(f->f_opthdr);
X       f->f_flags  = swab16(f->f_flags);
X}
X
Xvoid convert_ecoff_aouthdr(struct aouthdr *a)
X{
X       a->magic      = swab16(a->magic);
X       a->vstamp     = swab16(a->vstamp);
X       a->tsize      = swab32(a->tsize);
X       a->dsize      = swab32(a->dsize);
X       a->bsize      = swab32(a->bsize);
X       a->entry      = swab32(a->entry);
X       a->text_start = swab32(a->text_start);
X       a->data_start = swab32(a->data_start);
X       a->bss_start  = swab32(a->bss_start);
X       a->gprmask    = swab32(a->gprmask);
X       a->cprmask[0] = swab32(a->cprmask[0]);
X       a->cprmask[1] = swab32(a->cprmask[1]);
X       a->cprmask[2] = swab32(a->cprmask[2]);
X       a->cprmask[3] = swab32(a->cprmask[3]);
X       a->gp_value   = swab32(a->gp_value);
X}
X
Xvoid convert_ecoff_esecs(struct scnhdr *s, int num)
X{
X       int i;
X
X       for (i = 0; i < num; i++, s++) {
X               s->s_paddr   = swab32(s->s_paddr);
X               s->s_vaddr   = swab32(s->s_vaddr);
X               s->s_size    = swab32(s->s_size);
X               s->s_scnptr  = swab32(s->s_scnptr);
X               s->s_relptr  = swab32(s->s_relptr);
X               s->s_lnnoptr = swab32(s->s_lnnoptr);
X               s->s_nreloc  = swab16(s->s_nreloc);
X               s->s_nlnno   = swab16(s->s_nlnno);
X               s->s_flags   = swab32(s->s_flags);
X       }
X}
END-of-elf2ecoff.c
exit

----
Rafal Boni                                              rafal.boni@eDial.com
 PGP key C7D3024C, print EA49 160D F5E4 C46A 9E91  524E 11E0 7133 C7D3 024C
    Need to get a hold of me?  http://800.edial.com/rafal.boni@eDial.com