elf2aout.c revision 1.23 1 1.23 wiz /* $NetBSD: elf2aout.c,v 1.23 2019/05/19 09:14:13 wiz Exp $ */
2 1.2 jonathan
3 1.1 jonathan /*
4 1.1 jonathan * Copyright (c) 1995
5 1.1 jonathan * Ted Lemon (hereinafter referred to as the author)
6 1.1 jonathan *
7 1.1 jonathan * Redistribution and use in source and binary forms, with or without
8 1.1 jonathan * modification, are permitted provided that the following conditions
9 1.1 jonathan * are met:
10 1.1 jonathan * 1. Redistributions of source code must retain the above copyright
11 1.1 jonathan * notice, this list of conditions and the following disclaimer.
12 1.1 jonathan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jonathan * notice, this list of conditions and the following disclaimer in the
14 1.1 jonathan * documentation and/or other materials provided with the distribution.
15 1.1 jonathan * 3. The name of the author may not be used to endorse or promote products
16 1.1 jonathan * derived from this software without specific prior written permission.
17 1.1 jonathan *
18 1.1 jonathan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
19 1.1 jonathan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 1.1 jonathan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 1.1 jonathan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
22 1.1 jonathan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 1.1 jonathan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 1.1 jonathan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 1.1 jonathan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 1.1 jonathan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 1.1 jonathan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 1.1 jonathan * SUCH DAMAGE.
29 1.1 jonathan */
30 1.1 jonathan
31 1.1 jonathan /* elf2aout.c
32 1.1 jonathan
33 1.1 jonathan This program converts an elf executable to a NetBSD a.out executable.
34 1.1 jonathan The minimal symbol table is copied, but the debugging symbols and
35 1.1 jonathan other informational sections are not. */
36 1.1 jonathan
37 1.15 tsutsui #if HAVE_NBTOOL_CONFIG_H
38 1.15 tsutsui #include "nbtool_config.h"
39 1.15 tsutsui #endif
40 1.15 tsutsui
41 1.15 tsutsui #ifndef TARGET_BYTE_ORDER
42 1.15 tsutsui #define TARGET_BYTE_ORDER BYTE_ORDER
43 1.15 tsutsui #endif
44 1.15 tsutsui
45 1.1 jonathan #include <sys/types.h>
46 1.5 lukem #include <sys/exec_aout.h>
47 1.5 lukem #include <sys/exec_elf.h>
48 1.5 lukem
49 1.5 lukem #include <a.out.h>
50 1.5 lukem #include <err.h>
51 1.5 lukem #include <errno.h>
52 1.1 jonathan #include <fcntl.h>
53 1.5 lukem #include <limits.h>
54 1.1 jonathan #include <stdio.h>
55 1.5 lukem #include <stdlib.h>
56 1.1 jonathan #include <string.h>
57 1.5 lukem #include <unistd.h>
58 1.1 jonathan
59 1.3 jonathan
60 1.1 jonathan struct sect {
61 1.15 tsutsui /* should be unsigned long, but assume no a.out binaries on LP64 */
62 1.15 tsutsui uint32_t vaddr;
63 1.15 tsutsui uint32_t len;
64 1.1 jonathan };
65 1.1 jonathan
66 1.22 christos static void combine(struct sect *, struct sect *, int);
67 1.22 christos static int phcmp(const void *, const void *);
68 1.22 christos static void *saveRead(int file, off_t offset, size_t len, const char *name);
69 1.22 christos static void copy(int, int, off_t, off_t);
70 1.22 christos static void translate_syms(int, int, off_t, off_t, off_t, off_t);
71 1.5 lukem
72 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
73 1.22 christos static void bswap32_region(int32_t* , int);
74 1.15 tsutsui #endif
75 1.15 tsutsui
76 1.22 christos static int *symTypeTable;
77 1.22 christos static int debug;
78 1.22 christos
79 1.22 christos static __dead void
80 1.22 christos usage(void)
81 1.22 christos {
82 1.23 wiz fprintf(stderr, "Usage: %s [-Os] <elf executable> <a.out executable>\n",
83 1.22 christos getprogname());
84 1.22 christos exit(EXIT_FAILURE);
85 1.22 christos }
86 1.22 christos
87 1.22 christos static const struct {
88 1.22 christos const char *n;
89 1.22 christos int v;
90 1.22 christos } nv[] = {
91 1.22 christos { ".text", N_TEXT },
92 1.22 christos { ".rodata", N_TEXT },
93 1.22 christos { ".data", N_DATA },
94 1.22 christos { ".sdata", N_DATA },
95 1.22 christos { ".lit4", N_DATA },
96 1.22 christos { ".lit8", N_DATA },
97 1.22 christos { ".bss", N_BSS },
98 1.22 christos { ".sbss", N_BSS },
99 1.22 christos };
100 1.22 christos
101 1.22 christos static int
102 1.22 christos get_symtab_type(const char *name)
103 1.22 christos {
104 1.22 christos size_t i;
105 1.22 christos for (i = 0; i < __arraycount(nv); i++) {
106 1.22 christos if (strcmp(name, nv[i].n) == 0)
107 1.22 christos return nv[i].v;
108 1.22 christos }
109 1.22 christos if (debug)
110 1.22 christos warnx("section `%s' is not handled\n", name);
111 1.22 christos return 0;
112 1.22 christos }
113 1.22 christos
114 1.22 christos static uint32_t
115 1.22 christos get_mid(const Elf32_Ehdr *ex)
116 1.22 christos {
117 1.22 christos switch (ex->e_machine) {
118 1.22 christos #ifdef notyet
119 1.22 christos case EM_AARCH64:
120 1.22 christos return MID_AARCH64;
121 1.22 christos case EM_ALPHA:
122 1.22 christos return MID_ALPHA;
123 1.22 christos #endif
124 1.22 christos case EM_ARM:
125 1.22 christos return MID_ARM6;
126 1.22 christos #ifdef notyet
127 1.22 christos case EM_PARISC:
128 1.22 christos return MID_HPPA;
129 1.22 christos #endif
130 1.22 christos case EM_386:
131 1.22 christos return MID_I386;
132 1.22 christos case EM_68K:
133 1.22 christos return MID_M68K;
134 1.22 christos case EM_OR1K:
135 1.22 christos return MID_OR1K;
136 1.22 christos case EM_MIPS:
137 1.22 christos if (ex->e_ident[EI_DATA] == ELFDATA2LSB)
138 1.22 christos return MID_PMAX;
139 1.22 christos else
140 1.22 christos return MID_MIPS;
141 1.22 christos case EM_PPC:
142 1.22 christos return MID_POWERPC;
143 1.22 christos #ifdef notyet
144 1.22 christos case EM_PPC64:
145 1.22 christos return MID_POWERPC64;
146 1.22 christos break;
147 1.22 christos #endif
148 1.22 christos case EM_RISCV:
149 1.22 christos return MID_RISCV;
150 1.22 christos case EM_SH:
151 1.22 christos return MID_SH3;
152 1.22 christos case EM_SPARC:
153 1.22 christos case EM_SPARC32PLUS:
154 1.22 christos case EM_SPARCV9:
155 1.22 christos if (ex->e_ident[EI_CLASS] == ELFCLASS32)
156 1.22 christos return MID_SPARC;
157 1.22 christos #ifdef notyet
158 1.22 christos return MID_SPARC64;
159 1.22 christos case EM_X86_64:
160 1.22 christos return MID_X86_64;
161 1.22 christos #else
162 1.22 christos break;
163 1.22 christos #endif
164 1.22 christos case EM_VAX:
165 1.22 christos return MID_VAX;
166 1.22 christos case EM_NONE:
167 1.22 christos return MID_ZERO;
168 1.22 christos default:
169 1.22 christos break;
170 1.22 christos }
171 1.22 christos if (debug)
172 1.22 christos warnx("Unsupported machine `%d'", ex->e_machine);
173 1.22 christos return MID_ZERO;
174 1.22 christos }
175 1.22 christos
176 1.22 christos static unsigned char
177 1.22 christos get_type(Elf32_Half shndx)
178 1.22 christos {
179 1.22 christos switch (shndx) {
180 1.22 christos case SHN_UNDEF:
181 1.22 christos return N_UNDF;
182 1.22 christos case SHN_ABS:
183 1.22 christos return N_ABS;
184 1.22 christos case SHN_COMMON:
185 1.22 christos case SHN_MIPS_ACOMMON:
186 1.22 christos return N_COMM;
187 1.22 christos default:
188 1.22 christos return (unsigned char)symTypeTable[shndx];
189 1.22 christos }
190 1.22 christos }
191 1.5 lukem
192 1.5 lukem int
193 1.5 lukem main(int argc, char **argv)
194 1.1 jonathan {
195 1.5 lukem Elf32_Ehdr ex;
196 1.5 lukem Elf32_Phdr *ph;
197 1.5 lukem Elf32_Shdr *sh;
198 1.5 lukem char *shstrtab;
199 1.17 christos ssize_t i, strtabix, symtabix;
200 1.5 lukem struct sect text, data, bss;
201 1.5 lukem struct exec aex;
202 1.5 lukem int infile, outfile;
203 1.15 tsutsui uint32_t cur_vma = UINT32_MAX;
204 1.15 tsutsui uint32_t mid;
205 1.22 christos int symflag = 0, c;
206 1.22 christos unsigned long magic = ZMAGIC;
207 1.5 lukem
208 1.5 lukem strtabix = symtabix = 0;
209 1.5 lukem text.len = data.len = bss.len = 0;
210 1.5 lukem text.vaddr = data.vaddr = bss.vaddr = 0;
211 1.5 lukem
212 1.22 christos while ((c = getopt(argc, argv, "dOs")) != -1) {
213 1.22 christos switch (c) {
214 1.22 christos case 'd':
215 1.22 christos debug++;
216 1.22 christos break;
217 1.22 christos case 's':
218 1.22 christos symflag = 1;
219 1.22 christos break;
220 1.22 christos case 'O':
221 1.22 christos magic = OMAGIC;
222 1.22 christos break;
223 1.22 christos case '?':
224 1.22 christos default:
225 1.22 christos usage:
226 1.22 christos usage();
227 1.22 christos }
228 1.22 christos }
229 1.22 christos
230 1.22 christos argc -= optind;
231 1.22 christos argv += optind;
232 1.22 christos
233 1.5 lukem /* Check args... */
234 1.22 christos if (argc != 2)
235 1.22 christos goto usage;
236 1.22 christos
237 1.22 christos
238 1.5 lukem /* Try the input file... */
239 1.22 christos if ((infile = open(argv[0], O_RDONLY)) < 0)
240 1.22 christos err(EXIT_FAILURE, "Can't open `%s' for read", argv[0]);
241 1.17 christos
242 1.5 lukem /* Read the header, which is at the beginning of the file... */
243 1.5 lukem i = read(infile, &ex, sizeof ex);
244 1.5 lukem if (i != sizeof ex) {
245 1.17 christos if (i == -1)
246 1.17 christos err(EXIT_FAILURE, "Error reading `%s'", argv[1]);
247 1.17 christos else
248 1.17 christos errx(EXIT_FAILURE, "End of file reading `%s'", argv[1]);
249 1.5 lukem }
250 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
251 1.15 tsutsui ex.e_type = bswap16(ex.e_type);
252 1.15 tsutsui ex.e_machine = bswap16(ex.e_machine);
253 1.15 tsutsui ex.e_version = bswap32(ex.e_version);
254 1.15 tsutsui ex.e_entry = bswap32(ex.e_entry);
255 1.15 tsutsui ex.e_phoff = bswap32(ex.e_phoff);
256 1.15 tsutsui ex.e_shoff = bswap32(ex.e_shoff);
257 1.15 tsutsui ex.e_flags = bswap32(ex.e_flags);
258 1.15 tsutsui ex.e_ehsize = bswap16(ex.e_ehsize);
259 1.15 tsutsui ex.e_phentsize = bswap16(ex.e_phentsize);
260 1.15 tsutsui ex.e_phnum = bswap16(ex.e_phnum);
261 1.15 tsutsui ex.e_shentsize = bswap16(ex.e_shentsize);
262 1.15 tsutsui ex.e_shnum = bswap16(ex.e_shnum);
263 1.15 tsutsui ex.e_shstrndx = bswap16(ex.e_shstrndx);
264 1.15 tsutsui #endif
265 1.22 christos // Not yet
266 1.22 christos if (ex.e_ident[EI_CLASS] == ELFCLASS64)
267 1.22 christos errx(EXIT_FAILURE, "Only 32 bit is supported");
268 1.22 christos
269 1.5 lukem /* Read the program headers... */
270 1.17 christos ph = saveRead(infile, ex.e_phoff,
271 1.17 christos (size_t)ex.e_phnum * sizeof(Elf32_Phdr), "ph");
272 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
273 1.15 tsutsui bswap32_region((int32_t*)ph, sizeof(Elf32_Phdr) * ex.e_phnum);
274 1.15 tsutsui #endif
275 1.5 lukem /* Read the section headers... */
276 1.17 christos sh = saveRead(infile, ex.e_shoff,
277 1.17 christos (size_t)ex.e_shnum * sizeof(Elf32_Shdr), "sh");
278 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
279 1.15 tsutsui bswap32_region((int32_t*)sh, sizeof(Elf32_Shdr) * ex.e_shnum);
280 1.15 tsutsui #endif
281 1.5 lukem /* Read in the section string table. */
282 1.5 lukem shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
283 1.17 christos (size_t)sh[ex.e_shstrndx].sh_size, "shstrtab");
284 1.5 lukem
285 1.5 lukem /* Find space for a table matching ELF section indices to a.out symbol
286 1.5 lukem * types. */
287 1.13 tsutsui symTypeTable = malloc(ex.e_shnum * sizeof(int));
288 1.17 christos if (symTypeTable == NULL)
289 1.17 christos err(EXIT_FAILURE, "symTypeTable: can't allocate");
290 1.5 lukem memset(symTypeTable, 0, ex.e_shnum * sizeof(int));
291 1.5 lukem
292 1.5 lukem /* Look for the symbol table and string table... Also map section
293 1.5 lukem * indices to symbol types for a.out */
294 1.5 lukem for (i = 0; i < ex.e_shnum; i++) {
295 1.5 lukem char *name = shstrtab + sh[i].sh_name;
296 1.5 lukem if (!strcmp(name, ".symtab"))
297 1.5 lukem symtabix = i;
298 1.22 christos else if (!strcmp(name, ".strtab"))
299 1.22 christos strtabix = i;
300 1.5 lukem else
301 1.22 christos symTypeTable[i] = get_symtab_type(name);
302 1.5 lukem }
303 1.5 lukem
304 1.5 lukem /* Figure out if we can cram the program header into an a.out
305 1.5 lukem * header... Basically, we can't handle anything but loadable
306 1.5 lukem * segments, but we can ignore some kinds of segments. We can't
307 1.5 lukem * handle holes in the address space, and we handle start addresses
308 1.5 lukem * other than 0x1000 by hoping that the loader will know where to load
309 1.5 lukem * - a.out doesn't have an explicit load address. Segments may be
310 1.5 lukem * out of order, so we sort them first. */
311 1.5 lukem qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp);
312 1.5 lukem for (i = 0; i < ex.e_phnum; i++) {
313 1.5 lukem /* Section types we can ignore... */
314 1.7 drochner if (ph[i].p_type == PT_NULL || ph[i].p_type == PT_NOTE ||
315 1.7 drochner ph[i].p_type == PT_PHDR || ph[i].p_type == PT_MIPS_REGINFO)
316 1.5 lukem continue;
317 1.5 lukem /* Section types we can't handle... */
318 1.22 christos if (ph[i].p_type == PT_TLS) {
319 1.22 christos if (debug)
320 1.22 christos warnx("Can't handle TLS section");
321 1.22 christos continue;
322 1.22 christos }
323 1.22 christos if (ph[i].p_type != PT_LOAD)
324 1.22 christos errx(EXIT_FAILURE, "Program header %zd "
325 1.22 christos "type %d can't be converted.", i, ph[i].p_type);
326 1.5 lukem /* Writable (data) segment? */
327 1.5 lukem if (ph[i].p_flags & PF_W) {
328 1.5 lukem struct sect ndata, nbss;
329 1.5 lukem
330 1.5 lukem ndata.vaddr = ph[i].p_vaddr;
331 1.5 lukem ndata.len = ph[i].p_filesz;
332 1.5 lukem nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
333 1.5 lukem nbss.len = ph[i].p_memsz - ph[i].p_filesz;
334 1.5 lukem
335 1.5 lukem combine(&data, &ndata, 0);
336 1.5 lukem combine(&bss, &nbss, 1);
337 1.5 lukem } else {
338 1.5 lukem struct sect ntxt;
339 1.5 lukem
340 1.5 lukem ntxt.vaddr = ph[i].p_vaddr;
341 1.5 lukem ntxt.len = ph[i].p_filesz;
342 1.5 lukem
343 1.5 lukem combine(&text, &ntxt, 0);
344 1.1 jonathan }
345 1.5 lukem /* Remember the lowest segment start address. */
346 1.5 lukem if (ph[i].p_vaddr < cur_vma)
347 1.5 lukem cur_vma = ph[i].p_vaddr;
348 1.5 lukem }
349 1.5 lukem
350 1.5 lukem /* Sections must be in order to be converted... */
351 1.5 lukem if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
352 1.17 christos text.vaddr + text.len > data.vaddr ||
353 1.17 christos data.vaddr + data.len > bss.vaddr)
354 1.17 christos errx(EXIT_FAILURE, "Sections ordering prevents a.out "
355 1.17 christos "conversion.");
356 1.5 lukem /* If there's a data section but no text section, then the loader
357 1.5 lukem * combined everything into one section. That needs to be the text
358 1.5 lukem * section, so just make the data section zero length following text. */
359 1.13 tsutsui if (data.len && text.len == 0) {
360 1.5 lukem text = data;
361 1.5 lukem data.vaddr = text.vaddr + text.len;
362 1.5 lukem data.len = 0;
363 1.5 lukem }
364 1.5 lukem /* If there is a gap between text and data, we'll fill it when we copy
365 1.5 lukem * the data, so update the length of the text segment as represented
366 1.5 lukem * in a.out to reflect that, since a.out doesn't allow gaps in the
367 1.5 lukem * program address space. */
368 1.5 lukem if (text.vaddr + text.len < data.vaddr)
369 1.5 lukem text.len = data.vaddr - text.vaddr;
370 1.5 lukem
371 1.5 lukem /* We now have enough information to cons up an a.out header... */
372 1.22 christos mid = get_mid(&ex);
373 1.20 skrll aex.a_midmag = (u_long)htonl(((u_long)symflag << 26)
374 1.22 christos | ((u_long)mid << 16) | magic);
375 1.13 tsutsui
376 1.5 lukem aex.a_text = text.len;
377 1.5 lukem aex.a_data = data.len;
378 1.5 lukem aex.a_bss = bss.len;
379 1.5 lukem aex.a_entry = ex.e_entry;
380 1.5 lukem aex.a_syms = (sizeof(struct nlist) *
381 1.22 christos (symtabix != -1 ? sh[symtabix].sh_size / sizeof(Elf32_Sym) : 0));
382 1.5 lukem aex.a_trsize = 0;
383 1.5 lukem aex.a_drsize = 0;
384 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
385 1.15 tsutsui aex.a_text = bswap32(aex.a_text);
386 1.15 tsutsui aex.a_data = bswap32(aex.a_data);
387 1.15 tsutsui aex.a_bss = bswap32(aex.a_bss);
388 1.15 tsutsui aex.a_entry = bswap32(aex.a_entry);
389 1.15 tsutsui aex.a_syms = bswap32(aex.a_syms);
390 1.15 tsutsui aex.a_trsize = bswap32(aex.a_trsize);
391 1.15 tsutsui aex.a_drsize = bswap32(aex.a_drsize);
392 1.15 tsutsui #endif
393 1.5 lukem
394 1.5 lukem /* Make the output file... */
395 1.22 christos if ((outfile = open(argv[1], O_WRONLY | O_CREAT, 0777)) < 0)
396 1.22 christos err(EXIT_FAILURE, "Unable to create `%s'", argv[1]);
397 1.6 simonb /* Truncate file... */
398 1.6 simonb if (ftruncate(outfile, 0)) {
399 1.22 christos warn("ftruncate %s", argv[1]);
400 1.6 simonb }
401 1.5 lukem /* Write the header... */
402 1.5 lukem i = write(outfile, &aex, sizeof aex);
403 1.17 christos if (i != sizeof aex)
404 1.22 christos err(EXIT_FAILURE, "Can't write `%s'", argv[1]);
405 1.5 lukem /* Copy the loadable sections. Zero-fill any gaps less than 64k;
406 1.5 lukem * complain about any zero-filling, and die if we're asked to
407 1.5 lukem * zero-fill more than 64k. */
408 1.5 lukem for (i = 0; i < ex.e_phnum; i++) {
409 1.5 lukem /* Unprocessable sections were handled above, so just verify
410 1.5 lukem * that the section can be loaded before copying. */
411 1.7 drochner if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) {
412 1.5 lukem if (cur_vma != ph[i].p_vaddr) {
413 1.15 tsutsui uint32_t gap = ph[i].p_vaddr - cur_vma;
414 1.5 lukem char obuf[1024];
415 1.5 lukem if (gap > 65536)
416 1.17 christos errx(EXIT_FAILURE,
417 1.17 christos "Intersegment gap (%u bytes) too large", gap);
418 1.22 christos if (debug)
419 1.22 christos warnx("%u byte intersegment gap", gap);
420 1.5 lukem memset(obuf, 0, sizeof obuf);
421 1.5 lukem while (gap) {
422 1.17 christos ssize_t count = write(outfile, obuf,
423 1.17 christos (gap > sizeof obuf
424 1.17 christos ? sizeof obuf : gap));
425 1.17 christos if (count < 0)
426 1.17 christos err(EXIT_FAILURE,
427 1.17 christos "Error writing gap");
428 1.17 christos gap -= (uint32_t)count;
429 1.5 lukem }
430 1.5 lukem }
431 1.5 lukem copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
432 1.5 lukem cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
433 1.1 jonathan }
434 1.5 lukem }
435 1.5 lukem
436 1.5 lukem /* Copy and translate the symbol table... */
437 1.5 lukem translate_syms(outfile, infile,
438 1.5 lukem sh[symtabix].sh_offset, sh[symtabix].sh_size,
439 1.5 lukem sh[strtabix].sh_offset, sh[strtabix].sh_size);
440 1.1 jonathan
441 1.17 christos free(ph);
442 1.17 christos free(sh);
443 1.17 christos free(shstrtab);
444 1.17 christos free(symTypeTable);
445 1.5 lukem /* Looks like we won... */
446 1.17 christos return EXIT_SUCCESS;
447 1.1 jonathan }
448 1.1 jonathan /* translate_syms (out, in, offset, size)
449 1.1 jonathan
450 1.1 jonathan Read the ELF symbol table from in at offset; translate it into a.out
451 1.1 jonathan nlist format and write it to out. */
452 1.1 jonathan
453 1.5 lukem void
454 1.13 tsutsui translate_syms(int out, int in, off_t symoff, off_t symsize,
455 1.13 tsutsui off_t stroff, off_t strsize)
456 1.1 jonathan {
457 1.5 lukem #define SYMS_PER_PASS 64
458 1.5 lukem Elf32_Sym inbuf[64];
459 1.5 lukem struct nlist outbuf[64];
460 1.17 christos ssize_t i, remaining, cur;
461 1.5 lukem char *oldstrings;
462 1.5 lukem char *newstrings, *nsp;
463 1.20 skrll size_t newstringsize;
464 1.20 skrll uint32_t stringsizebuf;
465 1.5 lukem
466 1.5 lukem /* Zero the unused fields in the output buffer.. */
467 1.5 lukem memset(outbuf, 0, sizeof outbuf);
468 1.5 lukem
469 1.5 lukem /* Find number of symbols to process... */
470 1.19 martin remaining = (ssize_t)(symsize / (off_t)sizeof(Elf32_Sym));
471 1.5 lukem
472 1.5 lukem /* Suck in the old string table... */
473 1.17 christos oldstrings = saveRead(in, stroff, (size_t)strsize, "string table");
474 1.5 lukem
475 1.20 skrll /*
476 1.20 skrll * Allocate space for the new one. We will increase the space if
477 1.20 skrll * this is too small
478 1.20 skrll */
479 1.17 christos newstringsize = (size_t)(strsize + remaining);
480 1.13 tsutsui newstrings = malloc(newstringsize);
481 1.17 christos if (newstrings == NULL)
482 1.17 christos err(EXIT_FAILURE, "No memory for new string table!");
483 1.5 lukem /* Initialize the table pointer... */
484 1.5 lukem nsp = newstrings;
485 1.5 lukem
486 1.10 soren /* Go the start of the ELF symbol table... */
487 1.17 christos if (lseek(in, symoff, SEEK_SET) < 0)
488 1.17 christos err(EXIT_FAILURE, "Can't seek");
489 1.5 lukem /* Translate and copy symbols... */
490 1.20 skrll for (; remaining; remaining -= cur) {
491 1.5 lukem cur = remaining;
492 1.5 lukem if (cur > SYMS_PER_PASS)
493 1.5 lukem cur = SYMS_PER_PASS;
494 1.17 christos if ((i = read(in, inbuf, (size_t)cur * sizeof(Elf32_Sym)))
495 1.15 tsutsui != cur * (ssize_t)sizeof(Elf32_Sym)) {
496 1.5 lukem if (i < 0)
497 1.17 christos err(EXIT_FAILURE, "%s: read error", __func__);
498 1.5 lukem else
499 1.17 christos errx(EXIT_FAILURE, "%s: premature end of file",
500 1.17 christos __func__);
501 1.5 lukem }
502 1.5 lukem /* Do the translation... */
503 1.5 lukem for (i = 0; i < cur; i++) {
504 1.5 lukem int binding, type;
505 1.20 skrll size_t off, len;
506 1.5 lukem
507 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
508 1.15 tsutsui inbuf[i].st_name = bswap32(inbuf[i].st_name);
509 1.15 tsutsui inbuf[i].st_value = bswap32(inbuf[i].st_value);
510 1.15 tsutsui inbuf[i].st_size = bswap32(inbuf[i].st_size);
511 1.15 tsutsui inbuf[i].st_shndx = bswap16(inbuf[i].st_shndx);
512 1.15 tsutsui #endif
513 1.20 skrll off = (size_t)(nsp - newstrings);
514 1.20 skrll
515 1.20 skrll /* length of this symbol with leading '_' and trailing '\0' */
516 1.20 skrll len = strlen(oldstrings + inbuf[i].st_name) + 1 + 1;
517 1.20 skrll
518 1.20 skrll /* Does it fit? If not make more space */
519 1.20 skrll if (newstringsize - off < len) {
520 1.20 skrll char *nns;
521 1.20 skrll
522 1.20 skrll newstringsize += (size_t)(remaining) * len;
523 1.20 skrll nns = realloc(newstrings, newstringsize);
524 1.20 skrll if (nns == NULL)
525 1.20 skrll err(EXIT_FAILURE, "No memory for new string table!");
526 1.20 skrll newstrings = nns;
527 1.20 skrll nsp = newstrings + off;
528 1.20 skrll }
529 1.5 lukem /* Copy the symbol into the new table, but prepend an
530 1.5 lukem * underscore. */
531 1.5 lukem *nsp = '_';
532 1.5 lukem strcpy(nsp + 1, oldstrings + inbuf[i].st_name);
533 1.5 lukem outbuf[i].n_un.n_strx = nsp - newstrings + 4;
534 1.20 skrll nsp += len;
535 1.5 lukem
536 1.7 drochner type = ELF32_ST_TYPE(inbuf[i].st_info);
537 1.7 drochner binding = ELF32_ST_BIND(inbuf[i].st_info);
538 1.5 lukem
539 1.5 lukem /* Convert ELF symbol type/section/etc info into a.out
540 1.5 lukem * type info. */
541 1.7 drochner if (type == STT_FILE)
542 1.5 lukem outbuf[i].n_type = N_FN;
543 1.5 lukem else
544 1.22 christos outbuf[i].n_type = get_type(inbuf[i].st_shndx);
545 1.7 drochner if (binding == STB_GLOBAL)
546 1.5 lukem outbuf[i].n_type |= N_EXT;
547 1.5 lukem /* Symbol values in executables should be compatible. */
548 1.5 lukem outbuf[i].n_value = inbuf[i].st_value;
549 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
550 1.15 tsutsui outbuf[i].n_un.n_strx = bswap32(outbuf[i].n_un.n_strx);
551 1.15 tsutsui outbuf[i].n_desc = bswap16(outbuf[i].n_desc);
552 1.15 tsutsui outbuf[i].n_value = bswap32(outbuf[i].n_value);
553 1.15 tsutsui #endif
554 1.5 lukem }
555 1.5 lukem /* Write out the symbols... */
556 1.17 christos if ((i = write(out, outbuf, (size_t)cur * sizeof(struct nlist)))
557 1.17 christos != cur * (ssize_t)sizeof(struct nlist))
558 1.17 christos err(EXIT_FAILURE, "%s: write failed", __func__);
559 1.5 lukem }
560 1.5 lukem /* Write out the string table length... */
561 1.21 skrll stringsizebuf = (uint32_t)newstringsize;
562 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
563 1.15 tsutsui stringsizebuf = bswap32(stringsizebuf);
564 1.15 tsutsui #endif
565 1.15 tsutsui if (write(out, &stringsizebuf, sizeof stringsizebuf)
566 1.17 christos != sizeof stringsizebuf)
567 1.17 christos err(EXIT_FAILURE, "%s: newstringsize: write failed", __func__);
568 1.5 lukem /* Write out the string table... */
569 1.17 christos if (write(out, newstrings, newstringsize) != (ssize_t)newstringsize)
570 1.17 christos err(EXIT_FAILURE, "%s: newstrings: write failed", __func__);
571 1.17 christos free(newstrings);
572 1.16 martin free(oldstrings);
573 1.1 jonathan }
574 1.5 lukem
575 1.22 christos static void
576 1.13 tsutsui copy(int out, int in, off_t offset, off_t size)
577 1.1 jonathan {
578 1.5 lukem char ibuf[4096];
579 1.17 christos ssize_t remaining, cur, count;
580 1.1 jonathan
581 1.20 skrll /* Go to the start of the segment... */
582 1.17 christos if (lseek(in, offset, SEEK_SET) < 0)
583 1.17 christos err(EXIT_FAILURE, "%s: lseek failed", __func__);
584 1.18 martin if (size > SSIZE_MAX)
585 1.18 martin err(EXIT_FAILURE, "%s: can not copy this much", __func__);
586 1.18 martin remaining = (ssize_t)size;
587 1.5 lukem while (remaining) {
588 1.5 lukem cur = remaining;
589 1.15 tsutsui if (cur > (int)sizeof ibuf)
590 1.5 lukem cur = sizeof ibuf;
591 1.5 lukem remaining -= cur;
592 1.17 christos if ((count = read(in, ibuf, (size_t)cur)) != cur) {
593 1.17 christos if (count < 0)
594 1.17 christos err(EXIT_FAILURE, "%s: read error", __func__);
595 1.17 christos else
596 1.17 christos errx(EXIT_FAILURE, "%s: premature end of file",
597 1.17 christos __func__);
598 1.5 lukem }
599 1.17 christos if ((count = write(out, ibuf, (size_t)cur)) != cur)
600 1.17 christos err(EXIT_FAILURE, "%s: write failed", __func__);
601 1.1 jonathan }
602 1.1 jonathan }
603 1.22 christos
604 1.1 jonathan /* Combine two segments, which must be contiguous. If pad is true, it's
605 1.1 jonathan okay for there to be padding between. */
606 1.22 christos static void
607 1.13 tsutsui combine(struct sect *base, struct sect *new, int pad)
608 1.1 jonathan {
609 1.13 tsutsui
610 1.13 tsutsui if (base->len == 0)
611 1.5 lukem *base = *new;
612 1.5 lukem else
613 1.5 lukem if (new->len) {
614 1.5 lukem if (base->vaddr + base->len != new->vaddr) {
615 1.5 lukem if (pad)
616 1.5 lukem base->len = new->vaddr - base->vaddr;
617 1.17 christos else
618 1.17 christos errx(EXIT_FAILURE, "Non-contiguous "
619 1.17 christos "data can't be converted");
620 1.5 lukem }
621 1.5 lukem base->len += new->len;
622 1.5 lukem }
623 1.1 jonathan }
624 1.1 jonathan
625 1.22 christos static int
626 1.13 tsutsui phcmp(const void *vh1, const void *vh2)
627 1.1 jonathan {
628 1.12 dogcow const Elf32_Phdr *h1, *h2;
629 1.13 tsutsui
630 1.13 tsutsui h1 = (const Elf32_Phdr *)vh1;
631 1.13 tsutsui h2 = (const Elf32_Phdr *)vh2;
632 1.5 lukem
633 1.5 lukem if (h1->p_vaddr > h2->p_vaddr)
634 1.5 lukem return 1;
635 1.5 lukem else
636 1.5 lukem if (h1->p_vaddr < h2->p_vaddr)
637 1.5 lukem return -1;
638 1.5 lukem else
639 1.5 lukem return 0;
640 1.1 jonathan }
641 1.1 jonathan
642 1.22 christos static void *
643 1.17 christos saveRead(int file, off_t offset, size_t len, const char *name)
644 1.1 jonathan {
645 1.5 lukem char *tmp;
646 1.17 christos ssize_t count;
647 1.5 lukem off_t off;
648 1.17 christos
649 1.17 christos if ((off = lseek(file, offset, SEEK_SET)) < 0)
650 1.17 christos errx(EXIT_FAILURE, "%s: seek failed", name);
651 1.13 tsutsui if ((tmp = malloc(len)) == NULL)
652 1.17 christos errx(EXIT_FAILURE,
653 1.17 christos "%s: Can't allocate %jd bytes.", name, (intmax_t)len);
654 1.5 lukem count = read(file, tmp, len);
655 1.17 christos if ((size_t)count != len) {
656 1.17 christos if (count < 0)
657 1.17 christos err(EXIT_FAILURE, "%s: read error", name);
658 1.17 christos else
659 1.17 christos errx(EXIT_FAILURE, "%s: premature end of file",
660 1.17 christos name);
661 1.5 lukem }
662 1.5 lukem return tmp;
663 1.1 jonathan }
664 1.15 tsutsui
665 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
666 1.15 tsutsui /* swap a 32bit region */
667 1.22 christos static void
668 1.15 tsutsui bswap32_region(int32_t* p, int len)
669 1.15 tsutsui {
670 1.15 tsutsui size_t i;
671 1.15 tsutsui
672 1.15 tsutsui for (i = 0; i < len / sizeof(int32_t); i++, p++)
673 1.15 tsutsui *p = bswap32(*p);
674 1.15 tsutsui }
675 1.15 tsutsui #endif
676