elf2aout.c revision 1.16 1 1.16 martin /* $NetBSD: elf2aout.c,v 1.16 2016/03/06 15:44:06 martin 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.13 tsutsui void combine(struct sect *, struct sect *, int);
67 1.13 tsutsui int phcmp(const void *, const void *);
68 1.13 tsutsui char *saveRead(int file, off_t offset, off_t len, const char *name);
69 1.13 tsutsui void copy(int, int, off_t, off_t);
70 1.13 tsutsui 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.15 tsutsui void bswap32_region(int32_t* , int);
74 1.15 tsutsui #endif
75 1.15 tsutsui
76 1.5 lukem int *symTypeTable;
77 1.5 lukem
78 1.5 lukem int
79 1.5 lukem main(int argc, char **argv)
80 1.1 jonathan {
81 1.5 lukem Elf32_Ehdr ex;
82 1.5 lukem Elf32_Phdr *ph;
83 1.5 lukem Elf32_Shdr *sh;
84 1.5 lukem char *shstrtab;
85 1.5 lukem int strtabix, symtabix;
86 1.5 lukem int i;
87 1.5 lukem struct sect text, data, bss;
88 1.5 lukem struct exec aex;
89 1.5 lukem int infile, outfile;
90 1.15 tsutsui uint32_t cur_vma = UINT32_MAX;
91 1.15 tsutsui uint32_t mid;
92 1.5 lukem int symflag = 0;
93 1.5 lukem
94 1.5 lukem strtabix = symtabix = 0;
95 1.5 lukem text.len = data.len = bss.len = 0;
96 1.5 lukem text.vaddr = data.vaddr = bss.vaddr = 0;
97 1.5 lukem
98 1.5 lukem /* Check args... */
99 1.5 lukem if (argc < 3 || argc > 4) {
100 1.5 lukem usage:
101 1.5 lukem fprintf(stderr,
102 1.5 lukem "usage: elf2aout <elf executable> <a.out executable> [-s]\n");
103 1.5 lukem exit(1);
104 1.5 lukem }
105 1.5 lukem if (argc == 4) {
106 1.5 lukem if (strcmp(argv[3], "-s"))
107 1.5 lukem goto usage;
108 1.5 lukem symflag = 1;
109 1.5 lukem }
110 1.5 lukem /* Try the input file... */
111 1.5 lukem if ((infile = open(argv[1], O_RDONLY)) < 0) {
112 1.5 lukem fprintf(stderr, "Can't open %s for read: %s\n",
113 1.5 lukem argv[1], strerror(errno));
114 1.5 lukem exit(1);
115 1.5 lukem }
116 1.5 lukem /* Read the header, which is at the beginning of the file... */
117 1.5 lukem i = read(infile, &ex, sizeof ex);
118 1.5 lukem if (i != sizeof ex) {
119 1.5 lukem fprintf(stderr, "ex: %s: %s.\n",
120 1.5 lukem argv[1], i ? strerror(errno) : "End of file reached");
121 1.5 lukem exit(1);
122 1.5 lukem }
123 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
124 1.15 tsutsui ex.e_type = bswap16(ex.e_type);
125 1.15 tsutsui ex.e_machine = bswap16(ex.e_machine);
126 1.15 tsutsui ex.e_version = bswap32(ex.e_version);
127 1.15 tsutsui ex.e_entry = bswap32(ex.e_entry);
128 1.15 tsutsui ex.e_phoff = bswap32(ex.e_phoff);
129 1.15 tsutsui ex.e_shoff = bswap32(ex.e_shoff);
130 1.15 tsutsui ex.e_flags = bswap32(ex.e_flags);
131 1.15 tsutsui ex.e_ehsize = bswap16(ex.e_ehsize);
132 1.15 tsutsui ex.e_phentsize = bswap16(ex.e_phentsize);
133 1.15 tsutsui ex.e_phnum = bswap16(ex.e_phnum);
134 1.15 tsutsui ex.e_shentsize = bswap16(ex.e_shentsize);
135 1.15 tsutsui ex.e_shnum = bswap16(ex.e_shnum);
136 1.15 tsutsui ex.e_shstrndx = bswap16(ex.e_shstrndx);
137 1.15 tsutsui #endif
138 1.5 lukem /* Read the program headers... */
139 1.5 lukem ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff,
140 1.5 lukem ex.e_phnum * sizeof(Elf32_Phdr), "ph");
141 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
142 1.15 tsutsui bswap32_region((int32_t*)ph, sizeof(Elf32_Phdr) * ex.e_phnum);
143 1.15 tsutsui #endif
144 1.5 lukem /* Read the section headers... */
145 1.5 lukem sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff,
146 1.5 lukem ex.e_shnum * sizeof(Elf32_Shdr), "sh");
147 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
148 1.15 tsutsui bswap32_region((int32_t*)sh, sizeof(Elf32_Shdr) * ex.e_shnum);
149 1.15 tsutsui #endif
150 1.5 lukem /* Read in the section string table. */
151 1.5 lukem shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
152 1.5 lukem sh[ex.e_shstrndx].sh_size, "shstrtab");
153 1.5 lukem
154 1.5 lukem /* Find space for a table matching ELF section indices to a.out symbol
155 1.5 lukem * types. */
156 1.13 tsutsui symTypeTable = malloc(ex.e_shnum * sizeof(int));
157 1.13 tsutsui if (symTypeTable == NULL) {
158 1.5 lukem fprintf(stderr, "symTypeTable: can't allocate.\n");
159 1.5 lukem exit(1);
160 1.5 lukem }
161 1.5 lukem memset(symTypeTable, 0, ex.e_shnum * sizeof(int));
162 1.5 lukem
163 1.5 lukem /* Look for the symbol table and string table... Also map section
164 1.5 lukem * indices to symbol types for a.out */
165 1.5 lukem for (i = 0; i < ex.e_shnum; i++) {
166 1.5 lukem char *name = shstrtab + sh[i].sh_name;
167 1.5 lukem if (!strcmp(name, ".symtab"))
168 1.5 lukem symtabix = i;
169 1.5 lukem else
170 1.5 lukem if (!strcmp(name, ".strtab"))
171 1.5 lukem strtabix = i;
172 1.5 lukem else
173 1.5 lukem if (!strcmp(name, ".text") || !strcmp(name, ".rodata"))
174 1.5 lukem symTypeTable[i] = N_TEXT;
175 1.5 lukem else
176 1.5 lukem if (!strcmp(name, ".data") || !strcmp(name, ".sdata") ||
177 1.5 lukem !strcmp(name, ".lit4") || !strcmp(name, ".lit8"))
178 1.5 lukem symTypeTable[i] = N_DATA;
179 1.5 lukem else
180 1.5 lukem if (!strcmp(name, ".bss") || !strcmp(name, ".sbss"))
181 1.5 lukem symTypeTable[i] = N_BSS;
182 1.5 lukem }
183 1.5 lukem
184 1.5 lukem /* Figure out if we can cram the program header into an a.out
185 1.5 lukem * header... Basically, we can't handle anything but loadable
186 1.5 lukem * segments, but we can ignore some kinds of segments. We can't
187 1.5 lukem * handle holes in the address space, and we handle start addresses
188 1.5 lukem * other than 0x1000 by hoping that the loader will know where to load
189 1.5 lukem * - a.out doesn't have an explicit load address. Segments may be
190 1.5 lukem * out of order, so we sort them first. */
191 1.5 lukem qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp);
192 1.5 lukem for (i = 0; i < ex.e_phnum; i++) {
193 1.5 lukem /* Section types we can ignore... */
194 1.7 drochner if (ph[i].p_type == PT_NULL || ph[i].p_type == PT_NOTE ||
195 1.7 drochner ph[i].p_type == PT_PHDR || ph[i].p_type == PT_MIPS_REGINFO)
196 1.5 lukem continue;
197 1.5 lukem /* Section types we can't handle... */
198 1.5 lukem else
199 1.7 drochner if (ph[i].p_type != PT_LOAD)
200 1.5 lukem errx(1, "Program header %d type %d can't be converted.", i, ph[i].p_type);
201 1.5 lukem /* Writable (data) segment? */
202 1.5 lukem if (ph[i].p_flags & PF_W) {
203 1.5 lukem struct sect ndata, nbss;
204 1.5 lukem
205 1.5 lukem ndata.vaddr = ph[i].p_vaddr;
206 1.5 lukem ndata.len = ph[i].p_filesz;
207 1.5 lukem nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
208 1.5 lukem nbss.len = ph[i].p_memsz - ph[i].p_filesz;
209 1.5 lukem
210 1.5 lukem combine(&data, &ndata, 0);
211 1.5 lukem combine(&bss, &nbss, 1);
212 1.5 lukem } else {
213 1.5 lukem struct sect ntxt;
214 1.5 lukem
215 1.5 lukem ntxt.vaddr = ph[i].p_vaddr;
216 1.5 lukem ntxt.len = ph[i].p_filesz;
217 1.5 lukem
218 1.5 lukem combine(&text, &ntxt, 0);
219 1.1 jonathan }
220 1.5 lukem /* Remember the lowest segment start address. */
221 1.5 lukem if (ph[i].p_vaddr < cur_vma)
222 1.5 lukem cur_vma = ph[i].p_vaddr;
223 1.5 lukem }
224 1.5 lukem
225 1.5 lukem /* Sections must be in order to be converted... */
226 1.5 lukem if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
227 1.5 lukem text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr) {
228 1.5 lukem fprintf(stderr, "Sections ordering prevents a.out conversion.\n");
229 1.5 lukem exit(1);
230 1.5 lukem }
231 1.5 lukem /* If there's a data section but no text section, then the loader
232 1.5 lukem * combined everything into one section. That needs to be the text
233 1.5 lukem * section, so just make the data section zero length following text. */
234 1.13 tsutsui if (data.len && text.len == 0) {
235 1.5 lukem text = data;
236 1.5 lukem data.vaddr = text.vaddr + text.len;
237 1.5 lukem data.len = 0;
238 1.5 lukem }
239 1.5 lukem /* If there is a gap between text and data, we'll fill it when we copy
240 1.5 lukem * the data, so update the length of the text segment as represented
241 1.5 lukem * in a.out to reflect that, since a.out doesn't allow gaps in the
242 1.5 lukem * program address space. */
243 1.5 lukem if (text.vaddr + text.len < data.vaddr)
244 1.5 lukem text.len = data.vaddr - text.vaddr;
245 1.5 lukem
246 1.5 lukem /* We now have enough information to cons up an a.out header... */
247 1.14 tsutsui switch (ex.e_machine) {
248 1.14 tsutsui case EM_SPARC:
249 1.14 tsutsui mid = MID_SPARC;
250 1.14 tsutsui break;
251 1.14 tsutsui case EM_386:
252 1.14 tsutsui mid = MID_PC386;
253 1.14 tsutsui break;
254 1.14 tsutsui case EM_68K:
255 1.14 tsutsui mid = MID_M68K;
256 1.14 tsutsui break;
257 1.14 tsutsui case EM_MIPS:
258 1.14 tsutsui if (ex.e_ident[EI_DATA] == ELFDATA2LSB)
259 1.14 tsutsui mid = MID_PMAX;
260 1.14 tsutsui else
261 1.14 tsutsui mid = MID_MIPS;
262 1.14 tsutsui break;
263 1.14 tsutsui case EM_PPC:
264 1.14 tsutsui mid = MID_POWERPC;
265 1.14 tsutsui break;
266 1.14 tsutsui case EM_ARM:
267 1.14 tsutsui mid = MID_ARM6;
268 1.14 tsutsui break;
269 1.14 tsutsui case EM_VAX:
270 1.14 tsutsui mid = MID_VAX;
271 1.14 tsutsui break;
272 1.14 tsutsui case EM_NONE:
273 1.14 tsutsui default:
274 1.14 tsutsui mid = MID_ZERO;
275 1.14 tsutsui }
276 1.14 tsutsui aex.a_midmag = htonl((symflag << 26) | (mid << 16) | OMAGIC);
277 1.13 tsutsui
278 1.5 lukem aex.a_text = text.len;
279 1.5 lukem aex.a_data = data.len;
280 1.5 lukem aex.a_bss = bss.len;
281 1.5 lukem aex.a_entry = ex.e_entry;
282 1.5 lukem aex.a_syms = (sizeof(struct nlist) *
283 1.5 lukem (symtabix != -1
284 1.5 lukem ? sh[symtabix].sh_size / sizeof(Elf32_Sym) : 0));
285 1.5 lukem aex.a_trsize = 0;
286 1.5 lukem aex.a_drsize = 0;
287 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
288 1.15 tsutsui aex.a_text = bswap32(aex.a_text);
289 1.15 tsutsui aex.a_data = bswap32(aex.a_data);
290 1.15 tsutsui aex.a_bss = bswap32(aex.a_bss);
291 1.15 tsutsui aex.a_entry = bswap32(aex.a_entry);
292 1.15 tsutsui aex.a_syms = bswap32(aex.a_syms);
293 1.15 tsutsui aex.a_trsize = bswap32(aex.a_trsize);
294 1.15 tsutsui aex.a_drsize = bswap32(aex.a_drsize);
295 1.15 tsutsui #endif
296 1.5 lukem
297 1.5 lukem /* Make the output file... */
298 1.5 lukem if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) {
299 1.5 lukem fprintf(stderr, "Unable to create %s: %s\n", argv[2], strerror(errno));
300 1.5 lukem exit(1);
301 1.5 lukem }
302 1.6 simonb /* Truncate file... */
303 1.6 simonb if (ftruncate(outfile, 0)) {
304 1.6 simonb warn("ftruncate %s", argv[2]);
305 1.6 simonb }
306 1.5 lukem /* Write the header... */
307 1.5 lukem i = write(outfile, &aex, sizeof aex);
308 1.5 lukem if (i != sizeof aex) {
309 1.5 lukem perror("aex: write");
310 1.5 lukem exit(1);
311 1.5 lukem }
312 1.5 lukem /* Copy the loadable sections. Zero-fill any gaps less than 64k;
313 1.5 lukem * complain about any zero-filling, and die if we're asked to
314 1.5 lukem * zero-fill more than 64k. */
315 1.5 lukem for (i = 0; i < ex.e_phnum; i++) {
316 1.5 lukem /* Unprocessable sections were handled above, so just verify
317 1.5 lukem * that the section can be loaded before copying. */
318 1.7 drochner if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) {
319 1.5 lukem if (cur_vma != ph[i].p_vaddr) {
320 1.15 tsutsui uint32_t gap = ph[i].p_vaddr - cur_vma;
321 1.5 lukem char obuf[1024];
322 1.5 lukem if (gap > 65536)
323 1.5 lukem errx(1,
324 1.5 lukem "Intersegment gap (%ld bytes) too large.", (long) gap);
325 1.6 simonb #ifdef DEBUG
326 1.5 lukem warnx("Warning: %ld byte intersegment gap.",
327 1.5 lukem (long)gap);
328 1.6 simonb #endif
329 1.5 lukem memset(obuf, 0, sizeof obuf);
330 1.5 lukem while (gap) {
331 1.5 lukem int count = write(outfile, obuf, (gap > sizeof obuf
332 1.5 lukem ? sizeof obuf : gap));
333 1.5 lukem if (count < 0) {
334 1.5 lukem fprintf(stderr, "Error writing gap: %s\n",
335 1.5 lukem strerror(errno));
336 1.5 lukem exit(1);
337 1.5 lukem }
338 1.5 lukem gap -= count;
339 1.5 lukem }
340 1.5 lukem }
341 1.5 lukem copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
342 1.5 lukem cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
343 1.1 jonathan }
344 1.5 lukem }
345 1.5 lukem
346 1.5 lukem /* Copy and translate the symbol table... */
347 1.5 lukem translate_syms(outfile, infile,
348 1.5 lukem sh[symtabix].sh_offset, sh[symtabix].sh_size,
349 1.5 lukem sh[strtabix].sh_offset, sh[strtabix].sh_size);
350 1.1 jonathan
351 1.5 lukem /* Looks like we won... */
352 1.5 lukem exit(0);
353 1.1 jonathan }
354 1.1 jonathan /* translate_syms (out, in, offset, size)
355 1.1 jonathan
356 1.1 jonathan Read the ELF symbol table from in at offset; translate it into a.out
357 1.1 jonathan nlist format and write it to out. */
358 1.1 jonathan
359 1.5 lukem void
360 1.13 tsutsui translate_syms(int out, int in, off_t symoff, off_t symsize,
361 1.13 tsutsui off_t stroff, off_t strsize)
362 1.1 jonathan {
363 1.5 lukem #define SYMS_PER_PASS 64
364 1.5 lukem Elf32_Sym inbuf[64];
365 1.5 lukem struct nlist outbuf[64];
366 1.5 lukem int i, remaining, cur;
367 1.5 lukem char *oldstrings;
368 1.5 lukem char *newstrings, *nsp;
369 1.15 tsutsui int newstringsize, stringsizebuf;
370 1.5 lukem
371 1.5 lukem /* Zero the unused fields in the output buffer.. */
372 1.5 lukem memset(outbuf, 0, sizeof outbuf);
373 1.5 lukem
374 1.5 lukem /* Find number of symbols to process... */
375 1.5 lukem remaining = symsize / sizeof(Elf32_Sym);
376 1.5 lukem
377 1.5 lukem /* Suck in the old string table... */
378 1.5 lukem oldstrings = saveRead(in, stroff, strsize, "string table");
379 1.5 lukem
380 1.5 lukem /* Allocate space for the new one. XXX We make the wild assumption
381 1.5 lukem * that no two symbol table entries will point at the same place in
382 1.5 lukem * the string table - if that assumption is bad, this could easily
383 1.5 lukem * blow up. */
384 1.5 lukem newstringsize = strsize + remaining;
385 1.13 tsutsui newstrings = malloc(newstringsize);
386 1.13 tsutsui if (newstrings == NULL) {
387 1.5 lukem fprintf(stderr, "No memory for new string table!\n");
388 1.5 lukem exit(1);
389 1.5 lukem }
390 1.5 lukem /* Initialize the table pointer... */
391 1.5 lukem nsp = newstrings;
392 1.5 lukem
393 1.10 soren /* Go the start of the ELF symbol table... */
394 1.5 lukem if (lseek(in, symoff, SEEK_SET) < 0) {
395 1.5 lukem perror("translate_syms: lseek");
396 1.5 lukem exit(1);
397 1.5 lukem }
398 1.5 lukem /* Translate and copy symbols... */
399 1.5 lukem while (remaining) {
400 1.5 lukem cur = remaining;
401 1.5 lukem if (cur > SYMS_PER_PASS)
402 1.5 lukem cur = SYMS_PER_PASS;
403 1.5 lukem remaining -= cur;
404 1.5 lukem if ((i = read(in, inbuf, cur * sizeof(Elf32_Sym)))
405 1.15 tsutsui != cur * (ssize_t)sizeof(Elf32_Sym)) {
406 1.5 lukem if (i < 0)
407 1.5 lukem perror("translate_syms");
408 1.5 lukem else
409 1.5 lukem fprintf(stderr, "translate_syms: premature end of file.\n");
410 1.5 lukem exit(1);
411 1.5 lukem }
412 1.5 lukem /* Do the translation... */
413 1.5 lukem for (i = 0; i < cur; i++) {
414 1.5 lukem int binding, type;
415 1.5 lukem
416 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
417 1.15 tsutsui inbuf[i].st_name = bswap32(inbuf[i].st_name);
418 1.15 tsutsui inbuf[i].st_value = bswap32(inbuf[i].st_value);
419 1.15 tsutsui inbuf[i].st_size = bswap32(inbuf[i].st_size);
420 1.15 tsutsui inbuf[i].st_shndx = bswap16(inbuf[i].st_shndx);
421 1.15 tsutsui #endif
422 1.5 lukem /* Copy the symbol into the new table, but prepend an
423 1.5 lukem * underscore. */
424 1.5 lukem *nsp = '_';
425 1.5 lukem strcpy(nsp + 1, oldstrings + inbuf[i].st_name);
426 1.5 lukem outbuf[i].n_un.n_strx = nsp - newstrings + 4;
427 1.5 lukem nsp += strlen(nsp) + 1;
428 1.5 lukem
429 1.7 drochner type = ELF32_ST_TYPE(inbuf[i].st_info);
430 1.7 drochner binding = ELF32_ST_BIND(inbuf[i].st_info);
431 1.5 lukem
432 1.5 lukem /* Convert ELF symbol type/section/etc info into a.out
433 1.5 lukem * type info. */
434 1.7 drochner if (type == STT_FILE)
435 1.5 lukem outbuf[i].n_type = N_FN;
436 1.5 lukem else
437 1.7 drochner if (inbuf[i].st_shndx == SHN_UNDEF)
438 1.5 lukem outbuf[i].n_type = N_UNDF;
439 1.5 lukem else
440 1.7 drochner if (inbuf[i].st_shndx == SHN_ABS)
441 1.5 lukem outbuf[i].n_type = N_ABS;
442 1.5 lukem else
443 1.7 drochner if (inbuf[i].st_shndx == SHN_COMMON ||
444 1.7 drochner inbuf[i].st_shndx == SHN_MIPS_ACOMMON)
445 1.5 lukem outbuf[i].n_type = N_COMM;
446 1.5 lukem else
447 1.5 lukem outbuf[i].n_type = symTypeTable[inbuf[i].st_shndx];
448 1.7 drochner if (binding == STB_GLOBAL)
449 1.5 lukem outbuf[i].n_type |= N_EXT;
450 1.5 lukem /* Symbol values in executables should be compatible. */
451 1.5 lukem outbuf[i].n_value = inbuf[i].st_value;
452 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
453 1.15 tsutsui outbuf[i].n_un.n_strx = bswap32(outbuf[i].n_un.n_strx);
454 1.15 tsutsui outbuf[i].n_desc = bswap16(outbuf[i].n_desc);
455 1.15 tsutsui outbuf[i].n_value = bswap32(outbuf[i].n_value);
456 1.15 tsutsui #endif
457 1.5 lukem }
458 1.5 lukem /* Write out the symbols... */
459 1.5 lukem if ((i = write(out, outbuf, cur * sizeof(struct nlist)))
460 1.15 tsutsui != cur * (ssize_t)sizeof(struct nlist)) {
461 1.5 lukem fprintf(stderr, "translate_syms: write: %s\n", strerror(errno));
462 1.5 lukem exit(1);
463 1.5 lukem }
464 1.5 lukem }
465 1.5 lukem /* Write out the string table length... */
466 1.15 tsutsui stringsizebuf = newstringsize;
467 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
468 1.15 tsutsui stringsizebuf = bswap32(stringsizebuf);
469 1.15 tsutsui #endif
470 1.15 tsutsui if (write(out, &stringsizebuf, sizeof stringsizebuf)
471 1.15 tsutsui != sizeof stringsizebuf) {
472 1.5 lukem fprintf(stderr,
473 1.5 lukem "translate_syms: newstringsize: %s\n", strerror(errno));
474 1.5 lukem exit(1);
475 1.5 lukem }
476 1.5 lukem /* Write out the string table... */
477 1.5 lukem if (write(out, newstrings, newstringsize) != newstringsize) {
478 1.5 lukem fprintf(stderr, "translate_syms: newstrings: %s\n", strerror(errno));
479 1.5 lukem exit(1);
480 1.5 lukem }
481 1.16 martin free(oldstrings);
482 1.1 jonathan }
483 1.5 lukem
484 1.5 lukem void
485 1.13 tsutsui copy(int out, int in, off_t offset, off_t size)
486 1.1 jonathan {
487 1.5 lukem char ibuf[4096];
488 1.5 lukem int remaining, cur, count;
489 1.1 jonathan
490 1.11 simonb /* Go to the start of the ELF symbol table... */
491 1.5 lukem if (lseek(in, offset, SEEK_SET) < 0) {
492 1.5 lukem perror("copy: lseek");
493 1.5 lukem exit(1);
494 1.5 lukem }
495 1.5 lukem remaining = size;
496 1.5 lukem while (remaining) {
497 1.5 lukem cur = remaining;
498 1.15 tsutsui if (cur > (int)sizeof ibuf)
499 1.5 lukem cur = sizeof ibuf;
500 1.5 lukem remaining -= cur;
501 1.5 lukem if ((count = read(in, ibuf, cur)) != cur) {
502 1.5 lukem fprintf(stderr, "copy: read: %s\n",
503 1.5 lukem count ? strerror(errno) : "premature end of file");
504 1.5 lukem exit(1);
505 1.5 lukem }
506 1.5 lukem if ((count = write(out, ibuf, cur)) != cur) {
507 1.5 lukem perror("copy: write");
508 1.5 lukem exit(1);
509 1.5 lukem }
510 1.1 jonathan }
511 1.1 jonathan }
512 1.1 jonathan /* Combine two segments, which must be contiguous. If pad is true, it's
513 1.1 jonathan okay for there to be padding between. */
514 1.5 lukem void
515 1.13 tsutsui combine(struct sect *base, struct sect *new, int pad)
516 1.1 jonathan {
517 1.13 tsutsui
518 1.13 tsutsui if (base->len == 0)
519 1.5 lukem *base = *new;
520 1.5 lukem else
521 1.5 lukem if (new->len) {
522 1.5 lukem if (base->vaddr + base->len != new->vaddr) {
523 1.5 lukem if (pad)
524 1.5 lukem base->len = new->vaddr - base->vaddr;
525 1.5 lukem else {
526 1.5 lukem fprintf(stderr,
527 1.5 lukem "Non-contiguous data can't be converted.\n");
528 1.5 lukem exit(1);
529 1.5 lukem }
530 1.5 lukem }
531 1.5 lukem base->len += new->len;
532 1.5 lukem }
533 1.1 jonathan }
534 1.1 jonathan
535 1.3 jonathan int
536 1.13 tsutsui phcmp(const void *vh1, const void *vh2)
537 1.1 jonathan {
538 1.12 dogcow const Elf32_Phdr *h1, *h2;
539 1.13 tsutsui
540 1.13 tsutsui h1 = (const Elf32_Phdr *)vh1;
541 1.13 tsutsui h2 = (const Elf32_Phdr *)vh2;
542 1.5 lukem
543 1.5 lukem if (h1->p_vaddr > h2->p_vaddr)
544 1.5 lukem return 1;
545 1.5 lukem else
546 1.5 lukem if (h1->p_vaddr < h2->p_vaddr)
547 1.5 lukem return -1;
548 1.5 lukem else
549 1.5 lukem return 0;
550 1.1 jonathan }
551 1.1 jonathan
552 1.13 tsutsui char *
553 1.12 dogcow saveRead(int file, off_t offset, off_t len, const char *name)
554 1.1 jonathan {
555 1.5 lukem char *tmp;
556 1.5 lukem int count;
557 1.5 lukem off_t off;
558 1.5 lukem if ((off = lseek(file, offset, SEEK_SET)) < 0) {
559 1.5 lukem fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno));
560 1.5 lukem exit(1);
561 1.5 lukem }
562 1.13 tsutsui if ((tmp = malloc(len)) == NULL)
563 1.5 lukem errx(1, "%s: Can't allocate %ld bytes.", name, (long)len);
564 1.5 lukem count = read(file, tmp, len);
565 1.5 lukem if (count != len) {
566 1.5 lukem fprintf(stderr, "%s: read: %s.\n",
567 1.5 lukem name, count ? strerror(errno) : "End of file reached");
568 1.5 lukem exit(1);
569 1.5 lukem }
570 1.5 lukem return tmp;
571 1.1 jonathan }
572 1.15 tsutsui
573 1.15 tsutsui #if TARGET_BYTE_ORDER != BYTE_ORDER
574 1.15 tsutsui /* swap a 32bit region */
575 1.15 tsutsui void
576 1.15 tsutsui bswap32_region(int32_t* p, int len)
577 1.15 tsutsui {
578 1.15 tsutsui size_t i;
579 1.15 tsutsui
580 1.15 tsutsui for (i = 0; i < len / sizeof(int32_t); i++, p++)
581 1.15 tsutsui *p = bswap32(*p);
582 1.15 tsutsui }
583 1.15 tsutsui #endif
584