1076b1902SAdrien Destugues /*-
2076b1902SAdrien Destugues * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3076b1902SAdrien Destugues *
4076b1902SAdrien Destugues * Copyright (c) 2002 Jake Burkholder
5076b1902SAdrien Destugues * All rights reserved.
6076b1902SAdrien Destugues *
7076b1902SAdrien Destugues * Redistribution and use in source and binary forms, with or without
8076b1902SAdrien Destugues * modification, are permitted provided that the following conditions
9076b1902SAdrien Destugues * are met:
10076b1902SAdrien Destugues * 1. Redistributions of source code must retain the above copyright
11076b1902SAdrien Destugues * notice, this list of conditions and the following disclaimer.
12076b1902SAdrien Destugues * 2. Redistributions in binary form must reproduce the above copyright
13076b1902SAdrien Destugues * notice, this list of conditions and the following disclaimer in the
14076b1902SAdrien Destugues * documentation and/or other materials provided with the distribution.
15076b1902SAdrien Destugues *
16076b1902SAdrien Destugues * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17076b1902SAdrien Destugues * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18076b1902SAdrien Destugues * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19076b1902SAdrien Destugues * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20076b1902SAdrien Destugues * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21076b1902SAdrien Destugues * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22076b1902SAdrien Destugues * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23076b1902SAdrien Destugues * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24076b1902SAdrien Destugues * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25076b1902SAdrien Destugues * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26076b1902SAdrien Destugues * SUCH DAMAGE.
27076b1902SAdrien Destugues */
28076b1902SAdrien Destugues
29076b1902SAdrien Destugues #include <sys/cdefs.h>
30076b1902SAdrien Destugues
31076b1902SAdrien Destugues #include <sys/types.h>
32076b1902SAdrien Destugues #include <sys/mman.h>
33076b1902SAdrien Destugues #include <sys/stat.h>
34076b1902SAdrien Destugues
35076b1902SAdrien Destugues #include <err.h>
36076b1902SAdrien Destugues #include <fcntl.h>
37076b1902SAdrien Destugues #include <stdio.h>
38076b1902SAdrien Destugues #include <stdlib.h>
39076b1902SAdrien Destugues #include <string.h>
40076b1902SAdrien Destugues #include <unistd.h>
41076b1902SAdrien Destugues
42076b1902SAdrien Destugues #include <elf.h>
43076b1902SAdrien Destugues
44*15e39e1cSFrançois Revol int elf2aout32(void *v, int fd);
45*15e39e1cSFrançois Revol int elf2aout64(void *v, int fd);
46*15e39e1cSFrançois Revol
47076b1902SAdrien Destugues #define xe16toh(x) ((data == ELFDATA2MSB) ? be16toh(x) : le16toh(x))
48076b1902SAdrien Destugues #define xe32toh(x) ((data == ELFDATA2MSB) ? be32toh(x) : le32toh(x))
49076b1902SAdrien Destugues #define xe64toh(x) ((data == ELFDATA2MSB) ? be64toh(x) : le64toh(x))
50076b1902SAdrien Destugues #define htoxe32(x) ((data == ELFDATA2MSB) ? htobe32(x) : htole32(x))
51076b1902SAdrien Destugues
52076b1902SAdrien Destugues struct exec {
53076b1902SAdrien Destugues u_int32_t a_magic;
54076b1902SAdrien Destugues u_int32_t a_text;
55076b1902SAdrien Destugues u_int32_t a_data;
56076b1902SAdrien Destugues u_int32_t a_bss;
57076b1902SAdrien Destugues u_int32_t a_syms;
58076b1902SAdrien Destugues u_int32_t a_entry;
59076b1902SAdrien Destugues u_int32_t a_trsize;
60076b1902SAdrien Destugues u_int32_t a_drsize;
61076b1902SAdrien Destugues };
62*15e39e1cSFrançois Revol
63*15e39e1cSFrançois Revol /* we only support OMAGIC */
64*15e39e1cSFrançois Revol #define OMAGIC 0407
65076b1902SAdrien Destugues
66076b1902SAdrien Destugues static void usage(void);
67076b1902SAdrien Destugues
68*15e39e1cSFrançois Revol /* parts from NetBSD */
69*15e39e1cSFrançois Revol
70*15e39e1cSFrançois Revol #define MID_ZERO 0x000 /* unknown - implementation dependent */
71*15e39e1cSFrançois Revol #define MID_SUN010 0x001 /* sun 68010/68020 binary */
72*15e39e1cSFrançois Revol #define MID_SUN020 0x002 /* sun 68020-only binary */
73*15e39e1cSFrançois Revol
74*15e39e1cSFrançois Revol #define MID_PC386 0x064 /* 386 PC binary. (so quoth BFD) */
75*15e39e1cSFrançois Revol
76*15e39e1cSFrançois Revol #define MID_I386 0x086 /* i386 BSD binary */
77*15e39e1cSFrançois Revol #define MID_M68K 0x087 /* m68k BSD binary with 8K page sizes */
78*15e39e1cSFrançois Revol #define MID_M68K4K 0x088 /* m68k BSD binary with 4K page sizes */
79*15e39e1cSFrançois Revol #define MID_NS32532 0x089 /* ns32532 */
80*15e39e1cSFrançois Revol #define MID_SPARC 0x08a /* sparc */
81*15e39e1cSFrançois Revol #define MID_PMAX 0x08b /* pmax */
82*15e39e1cSFrançois Revol #define MID_VAX1K 0x08c /* VAX 1K page size binaries */
83*15e39e1cSFrançois Revol #define MID_ALPHA 0x08d /* Alpha BSD binary */
84*15e39e1cSFrançois Revol #define MID_MIPS 0x08e /* big-endian MIPS */
85*15e39e1cSFrançois Revol #define MID_ARM6 0x08f /* ARM6 */
86*15e39e1cSFrançois Revol #define MID_M680002K 0x090 /* m68000 with 2K page sizes */
87*15e39e1cSFrançois Revol #define MID_SH3 0x091 /* SH3 */
88*15e39e1cSFrançois Revol
89*15e39e1cSFrançois Revol #define MID_POWERPC64 0x094 /* big-endian PowerPC 64 */
90*15e39e1cSFrançois Revol #define MID_POWERPC 0x095 /* big-endian PowerPC */
91*15e39e1cSFrançois Revol #define MID_VAX 0x096 /* VAX */
92*15e39e1cSFrançois Revol #define MID_MIPS1 0x097 /* MIPS1 */
93*15e39e1cSFrançois Revol #define MID_MIPS2 0x098 /* MIPS2 */
94*15e39e1cSFrançois Revol #define MID_M88K 0x099 /* m88k BSD */
95*15e39e1cSFrançois Revol #define MID_HPPA 0x09a /* HP PARISC */
96*15e39e1cSFrançois Revol #define MID_SH5_64 0x09b /* LP64 SH5 */
97*15e39e1cSFrançois Revol #define MID_SPARC64 0x09c /* LP64 sparc */
98*15e39e1cSFrançois Revol #define MID_X86_64 0x09d /* AMD x86-64 */
99*15e39e1cSFrançois Revol #define MID_SH5_32 0x09e /* ILP32 SH5 */
100*15e39e1cSFrançois Revol #define MID_IA64 0x09f /* Itanium */
101*15e39e1cSFrançois Revol
102*15e39e1cSFrançois Revol #define MID_AARCH64 0x0b7 /* ARM AARCH64 */
103*15e39e1cSFrançois Revol #define MID_OR1K 0x0b8 /* OpenRISC 1000 */
104*15e39e1cSFrançois Revol #define MID_RISCV 0x0b9 /* Risc-V */
105*15e39e1cSFrançois Revol
106*15e39e1cSFrançois Revol #define MID_HP200 0x0c8 /* hp200 (68010) BSD binary */
107*15e39e1cSFrançois Revol
108*15e39e1cSFrançois Revol #define MID_HP300 0x12c /* hp300 (68020+68881) BSD binary */
109*15e39e1cSFrançois Revol
110*15e39e1cSFrançois Revol #define MID_HPUX800 0x20b /* hp800 HP-UX binary */
111*15e39e1cSFrançois Revol #define MID_HPUX 0x20c /* hp200/300 HP-UX binary */
112*15e39e1cSFrançois Revol
113*15e39e1cSFrançois Revol //(ex->e_machine, ex->e_ident[EI_DATA], ex->e_ident[EI_CLASS])
114*15e39e1cSFrançois Revol static uint32_t
get_mid(int m,int e,int c)115*15e39e1cSFrançois Revol get_mid(int m, int e, int c)
116*15e39e1cSFrançois Revol {
117*15e39e1cSFrançois Revol switch (m) {
118*15e39e1cSFrançois Revol case EM_AARCH64:
119*15e39e1cSFrançois Revol return MID_AARCH64;
120*15e39e1cSFrançois Revol case EM_ALPHA:
121*15e39e1cSFrançois Revol return MID_ALPHA;
122*15e39e1cSFrançois Revol case EM_ARM:
123*15e39e1cSFrançois Revol return MID_ARM6;
124*15e39e1cSFrançois Revol case EM_PARISC:
125*15e39e1cSFrançois Revol return MID_HPPA;
126*15e39e1cSFrançois Revol case EM_386:
127*15e39e1cSFrançois Revol return MID_I386;
128*15e39e1cSFrançois Revol case EM_68K:
129*15e39e1cSFrançois Revol return MID_M68K;
130*15e39e1cSFrançois Revol /* case EM_OR1K:
131*15e39e1cSFrançois Revol return MID_OR1K;*/
132*15e39e1cSFrançois Revol case EM_MIPS:
133*15e39e1cSFrançois Revol if (e == ELFDATA2LSB)
134*15e39e1cSFrançois Revol return MID_PMAX;
135*15e39e1cSFrançois Revol else
136*15e39e1cSFrançois Revol return MID_MIPS;
137*15e39e1cSFrançois Revol case EM_PPC:
138*15e39e1cSFrançois Revol return MID_POWERPC;
139*15e39e1cSFrançois Revol case EM_PPC64:
140*15e39e1cSFrançois Revol return MID_POWERPC64;
141*15e39e1cSFrançois Revol break;
142*15e39e1cSFrançois Revol case EM_RISCV:
143*15e39e1cSFrançois Revol return MID_RISCV;
144*15e39e1cSFrançois Revol case EM_SH:
145*15e39e1cSFrançois Revol return MID_SH3;
146*15e39e1cSFrançois Revol case EM_SPARC:
147*15e39e1cSFrançois Revol case EM_SPARC32PLUS:
148*15e39e1cSFrançois Revol case EM_SPARCV9:
149*15e39e1cSFrançois Revol if (c == ELFCLASS32)
150*15e39e1cSFrançois Revol return MID_SPARC;
151*15e39e1cSFrançois Revol return MID_SPARC64;
152*15e39e1cSFrançois Revol case EM_X86_64:
153*15e39e1cSFrançois Revol return MID_X86_64;
154*15e39e1cSFrançois Revol case EM_VAX:
155*15e39e1cSFrançois Revol return MID_VAX;
156*15e39e1cSFrançois Revol case EM_NONE:
157*15e39e1cSFrançois Revol return MID_ZERO;
158*15e39e1cSFrançois Revol default:
159*15e39e1cSFrançois Revol break;
160*15e39e1cSFrançois Revol }
161*15e39e1cSFrançois Revol return MID_ZERO;
162*15e39e1cSFrançois Revol }
163*15e39e1cSFrançois Revol
164076b1902SAdrien Destugues int
elf2aout32(void * v,int fd)165*15e39e1cSFrançois Revol elf2aout32(void *v, int fd)
166*15e39e1cSFrançois Revol {
167*15e39e1cSFrançois Revol Elf32_Half phentsize;
168*15e39e1cSFrançois Revol Elf32_Half phnum;
169*15e39e1cSFrançois Revol Elf32_Word filesz;
170*15e39e1cSFrançois Revol Elf32_Word memsz;
171*15e39e1cSFrançois Revol Elf32_Addr entry;
172*15e39e1cSFrançois Revol Elf32_Off offset;
173*15e39e1cSFrançois Revol Elf32_Off phoff;
174*15e39e1cSFrançois Revol Elf32_Word type;
175*15e39e1cSFrançois Revol
176*15e39e1cSFrançois Revol Elf32_Phdr *p;
177*15e39e1cSFrançois Revol Elf32_Ehdr *e = v;
178*15e39e1cSFrançois Revol
179*15e39e1cSFrançois Revol unsigned char data = e->e_ident[EI_DATA];
180*15e39e1cSFrançois Revol struct exec a;
181*15e39e1cSFrançois Revol int i;
182*15e39e1cSFrançois Revol uint32_t mid;
183*15e39e1cSFrançois Revol
184*15e39e1cSFrançois Revol mid = get_mid(xe16toh(e->e_machine), e->e_ident[EI_DATA], e->e_ident[EI_CLASS]);
185*15e39e1cSFrançois Revol phentsize = xe16toh(e->e_phentsize);
186*15e39e1cSFrançois Revol if (phentsize != sizeof(*p))
187*15e39e1cSFrançois Revol errx(1, "phdr size mismatch");
188*15e39e1cSFrançois Revol
189*15e39e1cSFrançois Revol entry = xe32toh(e->e_entry);
190*15e39e1cSFrançois Revol phoff = xe32toh(e->e_phoff);
191*15e39e1cSFrançois Revol phnum = xe16toh(e->e_phnum);
192*15e39e1cSFrançois Revol p = (Elf32_Phdr *)((char *)e + phoff);
193*15e39e1cSFrançois Revol bzero(&a, sizeof(a));
194*15e39e1cSFrançois Revol for (i = 0; i < phnum; i++) {
195*15e39e1cSFrançois Revol type = xe32toh(p[i].p_type);
196*15e39e1cSFrançois Revol switch (type) {
197*15e39e1cSFrançois Revol case PT_LOAD:
198*15e39e1cSFrançois Revol if (a.a_magic != 0)
199*15e39e1cSFrançois Revol errx(1, "too many loadable segments");
200*15e39e1cSFrançois Revol filesz = xe32toh(p[i].p_filesz);
201*15e39e1cSFrançois Revol memsz = xe32toh(p[i].p_memsz);
202*15e39e1cSFrançois Revol offset = xe32toh(p[i].p_offset);
203*15e39e1cSFrançois Revol a.a_magic = htoxe32(((uint32_t)mid << 16) | OMAGIC);
204*15e39e1cSFrançois Revol a.a_text = htoxe32(filesz);
205*15e39e1cSFrançois Revol a.a_bss = htoxe32(memsz - filesz);
206*15e39e1cSFrançois Revol a.a_entry = htoxe32(entry);
207*15e39e1cSFrançois Revol if (write(fd, &a, sizeof(a)) != sizeof(a) ||
208*15e39e1cSFrançois Revol write(fd, (char *)e + offset, filesz) != (ssize_t)filesz)
209*15e39e1cSFrançois Revol err(1, NULL);
210*15e39e1cSFrançois Revol break;
211*15e39e1cSFrançois Revol default:
212*15e39e1cSFrançois Revol break;
213*15e39e1cSFrançois Revol }
214*15e39e1cSFrançois Revol }
215*15e39e1cSFrançois Revol return (0);
216*15e39e1cSFrançois Revol }
217*15e39e1cSFrançois Revol
218*15e39e1cSFrançois Revol
219*15e39e1cSFrançois Revol int
elf2aout64(void * v,int fd)220*15e39e1cSFrançois Revol elf2aout64(void *v, int fd)
221076b1902SAdrien Destugues {
222076b1902SAdrien Destugues Elf64_Half phentsize;
223076b1902SAdrien Destugues Elf64_Half phnum;
224076b1902SAdrien Destugues Elf64_Xword filesz;
225076b1902SAdrien Destugues Elf64_Xword memsz;
226076b1902SAdrien Destugues Elf64_Addr entry;
227076b1902SAdrien Destugues Elf64_Off offset;
228076b1902SAdrien Destugues Elf64_Off phoff;
229076b1902SAdrien Destugues Elf64_Word type;
230*15e39e1cSFrançois Revol
231*15e39e1cSFrançois Revol Elf64_Phdr *p;
232*15e39e1cSFrançois Revol Elf64_Ehdr *e = v;
233*15e39e1cSFrançois Revol
234*15e39e1cSFrançois Revol unsigned char data = e->e_ident[EI_DATA];
235*15e39e1cSFrançois Revol struct exec a;
236*15e39e1cSFrançois Revol int i;
237*15e39e1cSFrançois Revol uint32_t mid;
238*15e39e1cSFrançois Revol
239*15e39e1cSFrançois Revol mid = get_mid(xe16toh(e->e_machine), e->e_ident[EI_DATA], e->e_ident[EI_CLASS]);
240*15e39e1cSFrançois Revol phentsize = xe16toh(e->e_phentsize);
241*15e39e1cSFrançois Revol if (phentsize != sizeof(*p))
242*15e39e1cSFrançois Revol errx(1, "phdr size mismatch");
243*15e39e1cSFrançois Revol
244*15e39e1cSFrançois Revol entry = xe64toh(e->e_entry);
245*15e39e1cSFrançois Revol phoff = xe64toh(e->e_phoff);
246*15e39e1cSFrançois Revol phnum = xe16toh(e->e_phnum);
247*15e39e1cSFrançois Revol p = (Elf64_Phdr *)((char *)e + phoff);
248*15e39e1cSFrançois Revol bzero(&a, sizeof(a));
249*15e39e1cSFrançois Revol for (i = 0; i < phnum; i++) {
250*15e39e1cSFrançois Revol type = xe32toh(p[i].p_type);
251*15e39e1cSFrançois Revol switch (type) {
252*15e39e1cSFrançois Revol case PT_LOAD:
253*15e39e1cSFrançois Revol if (a.a_magic != 0)
254*15e39e1cSFrançois Revol errx(1, "too many loadable segments");
255*15e39e1cSFrançois Revol filesz = xe64toh(p[i].p_filesz);
256*15e39e1cSFrançois Revol memsz = xe64toh(p[i].p_memsz);
257*15e39e1cSFrançois Revol offset = xe64toh(p[i].p_offset);
258*15e39e1cSFrançois Revol a.a_magic = htoxe32(((uint32_t)mid << 16) | OMAGIC);
259*15e39e1cSFrançois Revol a.a_text = htoxe32(filesz);
260*15e39e1cSFrançois Revol a.a_bss = htoxe32(memsz - filesz);
261*15e39e1cSFrançois Revol a.a_entry = htoxe32(entry);
262*15e39e1cSFrançois Revol if (write(fd, &a, sizeof(a)) != sizeof(a) ||
263*15e39e1cSFrançois Revol write(fd, (char *)e + offset, filesz) != (ssize_t)filesz)
264*15e39e1cSFrançois Revol err(1, NULL);
265*15e39e1cSFrançois Revol break;
266*15e39e1cSFrançois Revol default:
267*15e39e1cSFrançois Revol break;
268*15e39e1cSFrançois Revol }
269*15e39e1cSFrançois Revol }
270*15e39e1cSFrançois Revol return (0);
271*15e39e1cSFrançois Revol }
272*15e39e1cSFrançois Revol
273*15e39e1cSFrançois Revol
274*15e39e1cSFrançois Revol /*
275*15e39e1cSFrançois Revol * elf to a.out converter for freebsd/sparc64 bootblocks.
276*15e39e1cSFrançois Revol */
277*15e39e1cSFrançois Revol int
main(int ac,char ** av)278*15e39e1cSFrançois Revol main(int ac, char **av)
279*15e39e1cSFrançois Revol {
280076b1902SAdrien Destugues unsigned char data;
281076b1902SAdrien Destugues struct stat sb;
282076b1902SAdrien Destugues Elf64_Ehdr *e;
283076b1902SAdrien Destugues void *v;
284076b1902SAdrien Destugues int efd;
285076b1902SAdrien Destugues int fd;
286076b1902SAdrien Destugues int c;
287076b1902SAdrien Destugues
288076b1902SAdrien Destugues fd = STDIN_FILENO;
289076b1902SAdrien Destugues while ((c = getopt(ac, av, "o:")) != -1)
290076b1902SAdrien Destugues switch (c) {
291076b1902SAdrien Destugues case 'o':
292076b1902SAdrien Destugues if ((fd = open(optarg, O_CREAT|O_RDWR, 0644)) < 0)
293076b1902SAdrien Destugues err(1, "%s", optarg);
294076b1902SAdrien Destugues break;
295076b1902SAdrien Destugues case '?':
296076b1902SAdrien Destugues default:
297076b1902SAdrien Destugues usage();
298076b1902SAdrien Destugues }
299076b1902SAdrien Destugues ac -= optind;
300076b1902SAdrien Destugues av += optind;
301076b1902SAdrien Destugues if (ac == 0)
302076b1902SAdrien Destugues usage();
303076b1902SAdrien Destugues
304076b1902SAdrien Destugues if ((efd = open(*av, O_RDONLY)) < 0 || fstat(efd, &sb) < 0)
305076b1902SAdrien Destugues err(1, NULL);
306076b1902SAdrien Destugues v = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, efd, 0);
307076b1902SAdrien Destugues if ((e = v) == MAP_FAILED)
308076b1902SAdrien Destugues err(1, NULL);
309076b1902SAdrien Destugues
310076b1902SAdrien Destugues if (!IS_ELF(*e))
311076b1902SAdrien Destugues errx(1, "not an elf file");
312*15e39e1cSFrançois Revol if (e->e_ident[EI_CLASS] != ELFCLASS64 && e->e_ident[EI_CLASS] != ELFCLASS32)
313076b1902SAdrien Destugues errx(1, "wrong class");
314076b1902SAdrien Destugues data = e->e_ident[EI_DATA];
315076b1902SAdrien Destugues if (data != ELFDATA2MSB && data != ELFDATA2LSB)
316076b1902SAdrien Destugues errx(1, "wrong data format");
317076b1902SAdrien Destugues if (e->e_ident[EI_VERSION] != EV_CURRENT)
318076b1902SAdrien Destugues errx(1, "wrong elf version");
319076b1902SAdrien Destugues
320*15e39e1cSFrançois Revol if (e->e_ident[EI_CLASS] == ELFCLASS64)
321*15e39e1cSFrançois Revol return elf2aout64(v, fd);
322*15e39e1cSFrançois Revol else
323*15e39e1cSFrançois Revol return elf2aout32(v, fd);
324076b1902SAdrien Destugues }
325076b1902SAdrien Destugues
326076b1902SAdrien Destugues static void
usage(void)327076b1902SAdrien Destugues usage(void)
328076b1902SAdrien Destugues {
329076b1902SAdrien Destugues
330076b1902SAdrien Destugues fprintf(stderr, "usage: elf2aout [-o outfile] infile\n");
331076b1902SAdrien Destugues exit(1);
332076b1902SAdrien Destugues }
333