^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Copyright (c) 1995
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Ted Lemon (hereinafter referred to as the author)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * modification, are permitted provided that the following conditions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * 1. Redistributions of source code must retain the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * notice, this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * 3. The name of the author may not be used to endorse or promote products
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * derived from this software without specific prior written permission.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /* elf2ecoff.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) This program converts an elf executable to an ECOFF executable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) No symbol table is retained. This is useful primarily in building
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) net-bootable kernels for machines (e.g., DECstation and Alpha) which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) only support the ECOFF object file format. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #include <elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #include <limits.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #include <netinet/in.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #include "ecoff.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Some extra ELF definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define PT_MIPS_REGINFO 0x70000000 /* Register usage information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define PT_MIPS_ABIFLAGS 0x70000003 /* Records ABI related flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* -------------------------------------------------------------------- */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct sect {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) uint32_t vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) uint32_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int *symTypeTable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int must_convert_endian;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) int format_bigendian;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static void copy(int out, int in, off_t offset, off_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) char ibuf[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int remaining, cur, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* Go to the start of the ELF symbol table... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (lseek(in, offset, SEEK_SET) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) perror("copy: lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) remaining = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) while (remaining) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) cur = remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (cur > sizeof ibuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) cur = sizeof ibuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) remaining -= cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if ((count = read(in, ibuf, cur)) != cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) fprintf(stderr, "copy: read: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) count ? strerror(errno) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) "premature end of file");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if ((count = write(out, ibuf, cur)) != cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) perror("copy: write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * Combine two segments, which must be contiguous. If pad is true, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * okay for there to be padding between.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static void combine(struct sect *base, struct sect *new, int pad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!base->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *base = *new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) else if (new->len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (base->vaddr + base->len != new->vaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (pad)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) base->len = new->vaddr - base->vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "Non-contiguous data can't be converted.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) base->len += new->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static int phcmp(const void *v1, const void *v2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) const Elf32_Phdr *h1 = v1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) const Elf32_Phdr *h2 = v2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (h1->p_vaddr > h2->p_vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) else if (h1->p_vaddr < h2->p_vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static char *saveRead(int file, off_t offset, off_t len, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) char *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) off_t off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if ((off = lseek(file, offset, SEEK_SET)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (!(tmp = (char *) malloc(len))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) count = read(file, tmp, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (count != len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) fprintf(stderr, "%s: read: %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) count ? strerror(errno) : "End of file reached");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define swab16(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) ((uint16_t)( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) (((uint16_t)(x) & (uint16_t)0xff00U) >> 8) ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define swab32(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) ((unsigned int)( \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24) ))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static void convert_elf_hdr(Elf32_Ehdr * e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) e->e_type = swab16(e->e_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) e->e_machine = swab16(e->e_machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) e->e_version = swab32(e->e_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) e->e_entry = swab32(e->e_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) e->e_phoff = swab32(e->e_phoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) e->e_shoff = swab32(e->e_shoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) e->e_flags = swab32(e->e_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) e->e_ehsize = swab16(e->e_ehsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) e->e_phentsize = swab16(e->e_phentsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) e->e_phnum = swab16(e->e_phnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) e->e_shentsize = swab16(e->e_shentsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) e->e_shnum = swab16(e->e_shnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) e->e_shstrndx = swab16(e->e_shstrndx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static void convert_elf_phdrs(Elf32_Phdr * p, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) for (i = 0; i < num; i++, p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) p->p_type = swab32(p->p_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) p->p_offset = swab32(p->p_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) p->p_vaddr = swab32(p->p_vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) p->p_paddr = swab32(p->p_paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) p->p_filesz = swab32(p->p_filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) p->p_memsz = swab32(p->p_memsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) p->p_flags = swab32(p->p_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) p->p_align = swab32(p->p_align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static void convert_elf_shdrs(Elf32_Shdr * s, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) for (i = 0; i < num; i++, s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) s->sh_name = swab32(s->sh_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) s->sh_type = swab32(s->sh_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) s->sh_flags = swab32(s->sh_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) s->sh_addr = swab32(s->sh_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) s->sh_offset = swab32(s->sh_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) s->sh_size = swab32(s->sh_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) s->sh_link = swab32(s->sh_link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) s->sh_info = swab32(s->sh_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) s->sh_addralign = swab32(s->sh_addralign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) s->sh_entsize = swab32(s->sh_entsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) static void convert_ecoff_filehdr(struct filehdr *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) f->f_magic = swab16(f->f_magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) f->f_nscns = swab16(f->f_nscns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) f->f_timdat = swab32(f->f_timdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) f->f_symptr = swab32(f->f_symptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) f->f_nsyms = swab32(f->f_nsyms);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) f->f_opthdr = swab16(f->f_opthdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) f->f_flags = swab16(f->f_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static void convert_ecoff_aouthdr(struct aouthdr *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) a->magic = swab16(a->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) a->vstamp = swab16(a->vstamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) a->tsize = swab32(a->tsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) a->dsize = swab32(a->dsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) a->bsize = swab32(a->bsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) a->entry = swab32(a->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) a->text_start = swab32(a->text_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) a->data_start = swab32(a->data_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) a->bss_start = swab32(a->bss_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) a->gprmask = swab32(a->gprmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) a->cprmask[0] = swab32(a->cprmask[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) a->cprmask[1] = swab32(a->cprmask[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) a->cprmask[2] = swab32(a->cprmask[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) a->cprmask[3] = swab32(a->cprmask[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) a->gp_value = swab32(a->gp_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static void convert_ecoff_esecs(struct scnhdr *s, int num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) for (i = 0; i < num; i++, s++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) s->s_paddr = swab32(s->s_paddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) s->s_vaddr = swab32(s->s_vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) s->s_size = swab32(s->s_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) s->s_scnptr = swab32(s->s_scnptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) s->s_relptr = swab32(s->s_relptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) s->s_lnnoptr = swab32(s->s_lnnoptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) s->s_nreloc = swab16(s->s_nreloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) s->s_nlnno = swab16(s->s_nlnno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) s->s_flags = swab32(s->s_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) Elf32_Ehdr ex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) Elf32_Phdr *ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) Elf32_Shdr *sh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) int i, pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) struct sect text, data, bss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct filehdr efh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) struct aouthdr eah;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct scnhdr esecs[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) int infile, outfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) uint32_t cur_vma = UINT32_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int addflag = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int nosecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) text.len = data.len = bss.len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) text.vaddr = data.vaddr = bss.vaddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* Check args... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (argc < 3 || argc > 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) usage:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) "usage: elf2ecoff <elf executable> <ecoff executable> [-a]\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (argc == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (strcmp(argv[3], "-a"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto usage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) addflag = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) /* Try the input file... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if ((infile = open(argv[1], O_RDONLY)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) fprintf(stderr, "Can't open %s for read: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) argv[1], strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* Read the header, which is at the beginning of the file... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) i = read(infile, &ex, sizeof ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) if (i != sizeof ex) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) fprintf(stderr, "ex: %s: %s.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) argv[1],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) i ? strerror(errno) : "End of file reached");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ex.e_ident[EI_DATA] == ELFDATA2MSB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) format_bigendian = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (ntohs(0xaa55) == 0xaa55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!format_bigendian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) must_convert_endian = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (format_bigendian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) must_convert_endian = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (must_convert_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) convert_elf_hdr(&ex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /* Read the program headers... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) ex.e_phnum * sizeof(Elf32_Phdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) "ph");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (must_convert_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) convert_elf_phdrs(ph, ex.e_phnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Read the section headers... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) ex.e_shnum * sizeof(Elf32_Shdr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) "sh");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (must_convert_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) convert_elf_shdrs(sh, ex.e_shnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /* Figure out if we can cram the program header into an ECOFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) header... Basically, we can't handle anything but loadable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) segments, but we can ignore some kinds of segments. We can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) handle holes in the address space. Segments may be out of order,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) so we sort them first. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) for (i = 0; i < ex.e_phnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* Section types we can ignore... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) switch (ph[i].p_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case PT_NULL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) case PT_NOTE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) case PT_PHDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) case PT_MIPS_REGINFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) case PT_MIPS_ABIFLAGS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) case PT_LOAD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /* Writable (data) segment? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (ph[i].p_flags & PF_W) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) struct sect ndata, nbss;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) ndata.vaddr = ph[i].p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) ndata.len = ph[i].p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) nbss.len = ph[i].p_memsz - ph[i].p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) combine(&data, &ndata, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) combine(&bss, &nbss, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct sect ntxt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) ntxt.vaddr = ph[i].p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ntxt.len = ph[i].p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) combine(&text, &ntxt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* Remember the lowest segment start address. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (ph[i].p_vaddr < cur_vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) cur_vma = ph[i].p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* Section types we can't handle... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) "Program header %d type %d can't be converted.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ex.e_phnum, ph[i].p_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) /* Sections must be in order to be converted... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) text.vaddr + text.len > data.vaddr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) || data.vaddr + data.len > bss.vaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) "Sections ordering prevents a.out conversion.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) /* If there's a data section but no text section, then the loader
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) combined everything into one section. That needs to be the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) text section, so just make the data section zero length following
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) text. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (data.len && !text.len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) text = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) data.vaddr = text.vaddr + text.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) data.len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) /* If there is a gap between text and data, we'll fill it when we copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) the data, so update the length of the text segment as represented in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) a.out to reflect that, since a.out doesn't allow gaps in the program
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) address space. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (text.vaddr + text.len < data.vaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) text.len = data.vaddr - text.vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) /* We now have enough information to cons up an a.out header... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) eah.magic = OMAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) eah.vstamp = 200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) eah.tsize = text.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) eah.dsize = data.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) eah.bsize = bss.len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) eah.entry = ex.e_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) eah.text_start = text.vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) eah.data_start = data.vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) eah.bss_start = bss.vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) eah.gprmask = 0xf3fffffe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) memset(&eah.cprmask, '\0', sizeof eah.cprmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) eah.gp_value = 0; /* unused. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (format_bigendian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) efh.f_magic = MIPSEBMAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) efh.f_magic = MIPSELMAGIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (addflag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) nosecs = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) nosecs = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) efh.f_nscns = nosecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) efh.f_timdat = 0; /* bogus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) efh.f_symptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) efh.f_nsyms = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) efh.f_opthdr = sizeof eah;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) efh.f_flags = 0x100f; /* Stripped, not sharable. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) memset(esecs, 0, sizeof esecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) strcpy(esecs[0].s_name, ".text");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) strcpy(esecs[1].s_name, ".data");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) strcpy(esecs[2].s_name, ".bss");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) if (addflag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) strcpy(esecs[3].s_name, ".rdata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) strcpy(esecs[4].s_name, ".sdata");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) strcpy(esecs[5].s_name, ".sbss");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) esecs[0].s_paddr = esecs[0].s_vaddr = eah.text_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) esecs[1].s_paddr = esecs[1].s_vaddr = eah.data_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) esecs[2].s_paddr = esecs[2].s_vaddr = eah.bss_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (addflag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) esecs[3].s_paddr = esecs[3].s_vaddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) esecs[4].s_paddr = esecs[4].s_vaddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) esecs[5].s_paddr = esecs[5].s_vaddr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) esecs[0].s_size = eah.tsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) esecs[1].s_size = eah.dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) esecs[2].s_size = eah.bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) if (addflag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) esecs[3].s_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) esecs[4].s_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) esecs[5].s_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) esecs[0].s_scnptr = N_TXTOFF(efh, eah);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) esecs[1].s_scnptr = N_DATOFF(efh, eah);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) #define ECOFF_SEGMENT_ALIGNMENT(a) 0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) #define ECOFF_ROUND(s, a) (((s)+(a)-1)&~((a)-1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) esecs[2].s_scnptr = esecs[1].s_scnptr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) ECOFF_ROUND(esecs[1].s_size, ECOFF_SEGMENT_ALIGNMENT(&eah));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) if (addflag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) esecs[3].s_scnptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) esecs[4].s_scnptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) esecs[5].s_scnptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) esecs[0].s_relptr = esecs[1].s_relptr = esecs[2].s_relptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) esecs[0].s_lnnoptr = esecs[1].s_lnnoptr = esecs[2].s_lnnoptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) esecs[0].s_nreloc = esecs[1].s_nreloc = esecs[2].s_nreloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) esecs[0].s_nlnno = esecs[1].s_nlnno = esecs[2].s_nlnno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (addflag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) esecs[3].s_relptr = esecs[4].s_relptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) = esecs[5].s_relptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) esecs[3].s_lnnoptr = esecs[4].s_lnnoptr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) = esecs[5].s_lnnoptr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) esecs[3].s_nreloc = esecs[4].s_nreloc = esecs[5].s_nreloc =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) esecs[3].s_nlnno = esecs[4].s_nlnno = esecs[5].s_nlnno = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) esecs[0].s_flags = 0x20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) esecs[1].s_flags = 0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) esecs[2].s_flags = 0x82;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (addflag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) esecs[3].s_flags = 0x100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) esecs[4].s_flags = 0x200;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) esecs[5].s_flags = 0x400;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) /* Make the output file... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) fprintf(stderr, "Unable to create %s: %s\n", argv[2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) if (must_convert_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) convert_ecoff_filehdr(&efh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) /* Write the headers... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) i = write(outfile, &efh, sizeof efh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) if (i != sizeof efh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) perror("efh: write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) for (i = 0; i < nosecs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) printf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) ("Section %d: %s phys %"PRIx32" size %"PRIx32"\t file offset %"PRIx32"\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) i, esecs[i].s_name, esecs[i].s_paddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) esecs[i].s_size, esecs[i].s_scnptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) fprintf(stderr, "wrote %d byte file header.\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (must_convert_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) convert_ecoff_aouthdr(&eah);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) i = write(outfile, &eah, sizeof eah);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) if (i != sizeof eah) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) perror("eah: write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) fprintf(stderr, "wrote %d byte a.out header.\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) if (must_convert_endian)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) convert_ecoff_esecs(&esecs[0], nosecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) i = write(outfile, &esecs, nosecs * sizeof(struct scnhdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) if (i != nosecs * sizeof(struct scnhdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) perror("esecs: write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) fprintf(stderr, "wrote %d bytes of section headers.\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) pad = (sizeof(efh) + sizeof(eah) + nosecs * sizeof(struct scnhdr)) & 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (pad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) pad = 16 - pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) i = write(outfile, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (i < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) perror("ipad: write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) fprintf(stderr, "wrote %d byte pad.\n", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * Copy the loadable sections. Zero-fill any gaps less than 64k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * complain about any zero-filling, and die if we're asked to zero-fill
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * more than 64k.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) for (i = 0; i < ex.e_phnum; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) /* Unprocessable sections were handled above, so just verify that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) the section can be loaded before copying. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) if (cur_vma != ph[i].p_vaddr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) uint32_t gap = ph[i].p_vaddr - cur_vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) char obuf[1024];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (gap > 65536) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) "Intersegment gap (%"PRId32" bytes) too large.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) gap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) "Warning: %d byte intersegment gap.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) gap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) memset(obuf, 0, sizeof obuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) while (gap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) int count =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) write(outfile, obuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) (gap >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) sizeof obuf ? sizeof
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) obuf : gap));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) if (count < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) "Error writing gap: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) gap -= count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) fprintf(stderr, "writing %d bytes...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) ph[i].p_filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) copy(outfile, infile, ph[i].p_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) ph[i].p_filesz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) * Write a page of padding for boot PROMS that read entire pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) * Without this, they may attempt to read past the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) * data section, incur an error, and refuse to boot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) char obuf[4096];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) memset(obuf, 0, sizeof obuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) fprintf(stderr, "Error writing PROM padding: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) /* Looks like we won... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }