2 * Copyright (c) 2002 Jake Burkholder
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/elf64.h>
32 #include <sys/endian.h>
41 #define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
42 #define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
43 #define xe64toh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
44 #define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
56 #define A_MAGIC 0x01030107
58 static void usage(void);
61 * elf to a.out converter for freebsd/sparc64 bootblocks.
64 main(int ac, char **av)
87 while ((c = getopt(ac, av, "o:")) != -1)
90 if ((fd = open(optarg, O_CREAT|O_RDWR, 0644)) < 0)
102 if ((efd = open(*av, O_RDONLY)) < 0 || fstat(efd, &sb) < 0)
104 v = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, efd, 0);
105 if ((e = v) == MAP_FAILED)
109 errx(1, "not an elf file");
110 if (e->e_ident[EI_CLASS] != ELFCLASS64)
111 errx(1, "wrong class");
112 data = e->e_ident[EI_DATA];
113 if (data != ELFDATA2MSB && data != ELFDATA2LSB)
114 errx(1, "wrong data format");
115 if (e->e_ident[EI_VERSION] != EV_CURRENT)
116 errx(1, "wrong elf version");
117 machine = xe16toh(e->e_machine);
118 if (machine != EM_SPARCV9 && machine != EM_ALPHA)
119 errx(1, "wrong machine type");
120 phentsize = xe16toh(e->e_phentsize);
121 if (phentsize != sizeof(*p))
122 errx(1, "phdr size mismatch");
124 entry = xe64toh(e->e_entry);
125 phoff = xe64toh(e->e_phoff);
126 phnum = xe16toh(e->e_phnum);
127 p = (Elf64_Phdr *)((char *)e + phoff);
128 bzero(&a, sizeof(a));
129 for (i = 0; i < phnum; i++) {
130 type = xe32toh(p[i].p_type);
134 errx(1, "too many loadable segments");
135 filesz = xe64toh(p[i].p_filesz);
136 memsz = xe64toh(p[i].p_memsz);
137 offset = xe64toh(p[i].p_offset);
138 a.a_magic = htoxe32(A_MAGIC);
139 a.a_text = htoxe32(filesz);
140 a.a_bss = htoxe32(memsz - filesz);
141 a.a_entry = htoxe32(entry);
142 if (write(fd, &a, sizeof(a)) != sizeof(a) ||
143 write(fd, (char *)e + offset, filesz) != (ssize_t)filesz)
158 errx(1, "usage: elf2aout [-o outfile] infile");