elf2ecoff.c revision 1.11 1 /* $NetBSD: elf2ecoff.c,v 1.11 1998/11/27 05:09:50 simonb Exp $ */
2
3 /*
4 * Copyright (c) 1997 Jonathan Stone
5 * All rights reserved.
6 * Copyright (c) 1995
7 * Ted Lemon (hereinafter referred to as the author)
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /* elf2ecoff.c
34
35 This program converts an elf executable to an ECOFF executable.
36 No symbol table is retained. This is useful primarily in building
37 net-bootable kernels for machines (e.g., DECstation and Alpha) which
38 only support the ECOFF object file format. */
39
40 #include <sys/types.h>
41 #include <err.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <sys/exec.h>
45 #include <sys/exec_elf.h>
46 #include <sys/exec_aout.h>
47 #include <stdio.h>
48 #include <sys/exec_ecoff.h>
49 #include <sys/errno.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <limits.h>
53
54
55 /* Elf Program segment permissions, in program header flags field */
56
57 #define PF_X (1 << 0)/* Segment is executable */
58 #define PF_W (1 << 1)/* Segment is writable */
59 #define PF_R (1 << 2)/* Segment is readable */
60 #define PF_MASKPROC 0xF0000000 /* Processor-specific reserved bits */
61
62
63 #define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
64
65 struct sect {
66 unsigned long vaddr;
67 unsigned long len;
68 };
69
70 struct elf_syms {
71 int nsymbols;
72 Elf32_Sym *elf_syms;
73 off_t stringsize;
74 char *stringtab;
75 };
76
77 struct ecoff_syms {
78 int nsymbols;
79 struct ecoff_extsym *ecoff_syms;
80 off_t stringsize;
81 char *stringtab;
82 };
83
84 int debug = 0;
85
86 int phcmp(Elf32_Phdr * h1, Elf32_Phdr * h2);
87
88
89 char *saveRead(int file, off_t offset, off_t len, char *name);
90 void safewrite(int outfile, void *buf, off_t len, const char *msg);
91 void copy(int, int, off_t, off_t);
92 void combine(struct sect * base, struct sect * new, int paddable);
93 void translate_syms(struct elf_syms *, struct ecoff_syms *);
94 void
95 elf_symbol_table_to_ecoff(int out, int in,
96 struct ecoff_exechdr * ep,
97 off_t symoff, off_t symsize,
98 off_t stroff, off_t strsize);
99
100
101 int
102 make_ecoff_section_hdrs(struct ecoff_exechdr * ep,
103 struct ecoff_scnhdr * esecs);
104
105 void
106 write_ecoff_symhdr(int outfile, struct ecoff_exechdr * ep,
107 struct ecoff_symhdr * symhdrp,
108 long nesyms, long extsymoff, long extstroff,
109 long strsize);
110
111 void pad16(int fd, int size, const char *msg);
112
113 extern int errno;
114 int *symTypeTable;
115
116
117
118
119 void
120 elf_read_syms(struct elf_syms * elfsymsp, int infile,
121 off_t symoff, off_t symsize, off_t stroff, off_t strsize);
122
123
124 int
125 main(int argc, char **argv, char **envp)
126 {
127 Elf32_Ehdr ex;
128 Elf32_Phdr *ph;
129 Elf32_Shdr *sh;
130 char *shstrtab;
131 int strtabix, symtabix;
132 int i, pad;
133 struct sect text, data, bss; /* a.out-compatible sections */
134 struct sect rdata, sdata, sbss; /* ECOFF-only sections */
135
136 struct ecoff_exechdr ep;
137 struct ecoff_scnhdr esecs[6];
138 struct ecoff_symhdr symhdr;
139
140 int infile, outfile;
141 unsigned long cur_vma = ULONG_MAX;
142 int symflag = 0;
143 int nsecs = 0;
144
145 text.len = data.len = bss.len = 0;
146 text.vaddr = data.vaddr = bss.vaddr = 0;
147
148 rdata.len = sdata.len = sbss.len = 0;
149 rdata.vaddr = sdata.vaddr = sbss.vaddr = 0;
150
151 /* Check args... */
152 if (argc < 3 || argc > 4) {
153 usage:
154 fprintf(stderr,
155 "usage: elf2ecoff <elf executable> <ECOFF executable> [-s]\n");
156 exit(1);
157 }
158 if (argc == 4) {
159 if (strcmp(argv[3], "-s"))
160 goto usage;
161 symflag = 1;
162 }
163 /* Try the input file... */
164 if ((infile = open(argv[1], O_RDONLY)) < 0) {
165 fprintf(stderr, "Can't open %s for read: %s\n",
166 argv[1], strerror(errno));
167 exit(1);
168 }
169 /* Read the header, which is at the beginning of the file... */
170 i = read(infile, &ex, sizeof ex);
171 if (i != sizeof ex) {
172 fprintf(stderr, "ex: %s: %s.\n",
173 argv[1], i ? strerror(errno) : "End of file reached");
174 exit(1);
175 }
176 /* Read the program headers... */
177 ph = (Elf32_Phdr *) saveRead(infile, ex.e_phoff,
178 ex.e_phnum * sizeof(Elf32_Phdr), "ph");
179 /* Read the section headers... */
180 sh = (Elf32_Shdr *) saveRead(infile, ex.e_shoff,
181 ex.e_shnum * sizeof(Elf32_Shdr), "sh");
182 /* Read in the section string table. */
183 shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
184 sh[ex.e_shstrndx].sh_size, "shstrtab");
185 /* Read in the section string table. */
186 shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset,
187 sh[ex.e_shstrndx].sh_size, "shstrtab");
188
189
190 /* Look for the symbol table and string table... Also map section
191 * indices to symbol types for a.out */
192 symtabix = 0;
193 strtabix = 0;
194 for (i = 0; i < ex.e_shnum; i++) {
195 char *name = shstrtab + sh[i].sh_name;
196 if (!strcmp(name, ".symtab"))
197 symtabix = i;
198 else
199 if (!strcmp(name, ".strtab"))
200 strtabix = i;
201
202 }
203
204 /* Figure out if we can cram the program header into an ECOFF
205 * header... Basically, we can't handle anything but loadable
206 * segments, but we can ignore some kinds of segments. We can't
207 * handle holes in the address space. Segments may be out of order,
208 * so we sort them first. */
209
210 qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr),
211 (int (*) (const void *, const void *)) phcmp);
212
213 for (i = 0; i < ex.e_phnum; i++) {
214 /* Section types we can ignore... */
215 if (ph[i].p_type == Elf_pt_null || ph[i].p_type == Elf_pt_note ||
216 ph[i].p_type == Elf_pt_phdr ||
217 ph[i].p_type == Elf_pt_mips_reginfo) {
218
219 if (debug) {
220 fprintf(stderr, " skipping PH %d type %d flags 0x%x\n",
221 i, ph[i].p_type, ph[i].p_flags);
222 }
223 continue;
224 }
225 /* Section types we can't handle... */
226 else
227 if (ph[i].p_type != Elf_pt_load) {
228 fprintf(stderr, "Program header %d type %d can't be converted.\n",
229 i, ph[i].p_type);
230 exit(1);
231 }
232 /* Writable (data) segment? */
233 if (ph[i].p_flags & PF_W) {
234 struct sect ndata, nbss;
235
236 ndata.vaddr = ph[i].p_vaddr;
237 ndata.len = ph[i].p_filesz;
238 nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz;
239 nbss.len = ph[i].p_memsz - ph[i].p_filesz;
240
241 if (debug) {
242 fprintf(stderr,
243 " combinining PH %d type %d flags 0x%x with data, ndata = %ld, nbss =%ld\n", i, ph[i].p_type, ph[i].p_flags, ndata.len, nbss.len);
244 }
245 combine(&data, &ndata, 0);
246 combine(&bss, &nbss, 1);
247 } else {
248 struct sect ntxt;
249
250 ntxt.vaddr = ph[i].p_vaddr;
251 ntxt.len = ph[i].p_filesz;
252 if (debug) {
253
254 fprintf(stderr,
255 " combinining PH %d type %d flags 0x%x with text, len = %ld\n",
256 i, ph[i].p_type, ph[i].p_flags, ntxt.len);
257 }
258 combine(&text, &ntxt, 0);
259 }
260 /* Remember the lowest segment start address. */
261 if (ph[i].p_vaddr < cur_vma)
262 cur_vma = ph[i].p_vaddr;
263 }
264
265 /* Sections must be in order to be converted... */
266 if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr ||
267 text.vaddr + text.len > data.vaddr || data.vaddr + data.len > bss.vaddr) {
268 fprintf(stderr, "Sections ordering prevents a.out conversion.\n");
269 exit(1);
270 }
271 /* If there's a data section but no text section, then the loader
272 * combined everything into one section. That needs to be the text
273 * section, so just make the data section zero length following text. */
274 if (data.len && !text.len) {
275 text = data;
276 data.vaddr = text.vaddr + text.len;
277 data.len = 0;
278 }
279 /* If there is a gap between text and data, we'll fill it when we copy
280 * the data, so update the length of the text segment as represented
281 * in a.out to reflect that, since a.out doesn't allow gaps in the
282 * program address space. */
283 if (text.vaddr + text.len < data.vaddr)
284 text.len = data.vaddr - text.vaddr;
285
286 /* We now have enough information to cons up an a.out header... */
287 ep.a.magic = ECOFF_OMAGIC;
288 ep.a.vstamp = 2 * 256 + 10; /* compatible with version 2.10 */
289 ep.a.tsize = text.len;
290 ep.a.dsize = data.len;
291 ep.a.bsize = bss.len;
292 ep.a.entry = ex.e_entry;
293 ep.a.text_start = text.vaddr;
294 ep.a.data_start = data.vaddr;
295 ep.a.bss_start = bss.vaddr;
296 ep.a.gprmask = 0xf3fffffe;
297 memset(&ep.a.cprmask, 0, sizeof ep.a.cprmask);
298 ep.a.gp_value = 0; /* unused. */
299
300 ep.f.f_magic = ECOFF_MAGIC_MIPSEL;
301 ep.f.f_nscns = 6;
302 ep.f.f_timdat = 0; /* bogus */
303 ep.f.f_symptr = 0;
304 ep.f.f_nsyms = sizeof(struct ecoff_symhdr);
305 ep.f.f_opthdr = sizeof ep.a;
306 ep.f.f_flags = 0x100f; /* Stripped, not sharable. */
307
308 memset(esecs, 0, sizeof(esecs));
309
310 /* Make ECOFF section headers, with empty stubs for
311 * .rdata/.sdata/.sbss. */
312 make_ecoff_section_hdrs(&ep, esecs);
313
314 nsecs = ep.f.f_nscns;
315
316 /* Make the output file... */
317 if ((outfile = open(argv[2], O_WRONLY | O_CREAT, 0777)) < 0) {
318 fprintf(stderr, "Unable to create %s: %s\n", argv[2], strerror(errno));
319 exit(1);
320 }
321 /* Truncate file... */
322 if (ftruncate(outfile, 0)) {
323 warn("ftruncate %s", argv[2]);
324 }
325 /* Write the headers... */
326 safewrite(outfile, &ep.f, sizeof(ep.f), "ep.f: write: %s\n");
327 if (debug)
328 fprintf(stderr, "wrote %d byte file header.\n", sizeof(ep.f));
329
330 safewrite(outfile, &ep.a, sizeof(ep.a), "ep.a: write: %s\n");
331 if (debug)
332 fprintf(stderr, "wrote %d byte a.out header.\n", sizeof(ep.a));
333
334 safewrite(outfile, &esecs, sizeof(esecs[0]) * nsecs,
335 "esecs: write: %s\n");
336 if (debug)
337 fprintf(stderr, "wrote %d bytes of section headers.\n",
338 sizeof(esecs[0]) * nsecs);
339
340
341 pad = ((sizeof ep.f + sizeof ep.a + sizeof esecs) & 15);
342 if (pad) {
343 pad = 16 - pad;
344 pad16(outfile, pad, "ipad: write: %s\n");
345 if (debug)
346 fprintf(stderr, "wrote %d byte pad.\n", pad);
347 }
348 /* Copy the loadable sections. Zero-fill any gaps less than 64k;
349 * complain about any zero-filling, and die if we're asked to
350 * zero-fill more than 64k. */
351 for (i = 0; i < ex.e_phnum; i++) {
352 /* Unprocessable sections were handled above, so just verify
353 * that the section can be loaded before copying. */
354 if (ph[i].p_type == Elf_pt_load && ph[i].p_filesz) {
355 if (cur_vma != ph[i].p_vaddr) {
356 unsigned long gap = ph[i].p_vaddr - cur_vma;
357 char obuf[1024];
358 if (gap > 65536) {
359 fprintf(stderr, "Intersegment gap (%ld bytes) too large.\n",
360 gap);
361 exit(1);
362 }
363 if (debug)
364 fprintf(stderr, "Warning: %ld byte intersegment gap.\n", gap);
365 memset(obuf, 0, sizeof obuf);
366 while (gap) {
367 int count = write(outfile, obuf, (gap > sizeof obuf
368 ? sizeof obuf : gap));
369 if (count < 0) {
370 fprintf(stderr, "Error writing gap: %s\n",
371 strerror(errno));
372 exit(1);
373 }
374 gap -= count;
375 }
376 }
377 if (debug)
378 fprintf(stderr, "writing %d bytes...\n", ph[i].p_filesz);
379 copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz);
380 cur_vma = ph[i].p_vaddr + ph[i].p_filesz;
381 }
382 }
383
384
385 if (debug)
386 fprintf(stderr, "writing syms at offset 0x%lx\n",
387 (u_long) ep.f.f_symptr + sizeof(symhdr));
388
389 /* Copy and translate the symbol table... */
390 elf_symbol_table_to_ecoff(outfile, infile, &ep,
391 sh[symtabix].sh_offset, sh[symtabix].sh_size,
392 sh[strtabix].sh_offset, sh[strtabix].sh_size);
393
394 /*
395 * Write a page of padding for boot PROMS that read entire pages.
396 * Without this, they may attempt to read past the end of the
397 * data section, incur an error, and refuse to boot.
398 */
399 {
400 char obuf[4096];
401 memset(obuf, 0, sizeof obuf);
402 if (write(outfile, obuf, sizeof(obuf)) != sizeof(obuf)) {
403 fprintf(stderr, "Error writing PROM padding: %s\n",
404 strerror(errno));
405 exit(1);
406 }
407 }
408
409 /* Looks like we won... */
410 exit(0);
411 }
412
413 void
414 copy(out, in, offset, size)
415 int out, in;
416 off_t offset, size;
417 {
418 char ibuf[4096];
419 int remaining, cur, count;
420
421 /* Go the the start of the ELF symbol table... */
422 if (lseek(in, offset, SEEK_SET) < 0) {
423 perror("copy: lseek");
424 exit(1);
425 }
426 remaining = size;
427 while (remaining) {
428 cur = remaining;
429 if (cur > sizeof ibuf)
430 cur = sizeof ibuf;
431 remaining -= cur;
432 if ((count = read(in, ibuf, cur)) != cur) {
433 fprintf(stderr, "copy: read: %s\n",
434 count ? strerror(errno) : "premature end of file");
435 exit(1);
436 }
437 safewrite(out, ibuf, cur, "copy: write: %s\n");
438 }
439 }
440 /* Combine two segments, which must be contiguous. If pad is true, it's
441 okay for there to be padding between. */
442 void
443 combine(base, new, pad)
444 struct sect *base, *new;
445 int pad;
446 {
447 if (!base->len)
448 *base = *new;
449 else
450 if (new->len) {
451 if (base->vaddr + base->len != new->vaddr) {
452 if (pad)
453 base->len = new->vaddr - base->vaddr;
454 else {
455 fprintf(stderr,
456 "Non-contiguous data can't be converted.\n");
457 exit(1);
458 }
459 }
460 base->len += new->len;
461 }
462 }
463
464 int
465 phcmp(h1, h2)
466 Elf32_Phdr *h1, *h2;
467 {
468 if (h1->p_vaddr > h2->p_vaddr)
469 return 1;
470 else
471 if (h1->p_vaddr < h2->p_vaddr)
472 return -1;
473 else
474 return 0;
475 }
476
477 char
478 *
479 saveRead(int file, off_t offset, off_t len, char *name)
480 {
481 char *tmp;
482 int count;
483 off_t off;
484 if ((off = lseek(file, offset, SEEK_SET)) < 0) {
485 fprintf(stderr, "%s: fseek: %s\n", name, strerror(errno));
486 exit(1);
487 }
488 if (!(tmp = (char *) malloc(len))) {
489 fprintf(stderr, "%s: Can't allocate %ld bytes.\n", name, (long) len);
490 exit(1);
491 }
492 count = read(file, tmp, len);
493 if (count != len) {
494 fprintf(stderr, "%s: read: %s.\n",
495 name, count ? strerror(errno) : "End of file reached");
496 exit(1);
497 }
498 return tmp;
499 }
500
501 void
502 safewrite(int outfile, void *buf, off_t len, const char *msg)
503 {
504 int written;
505 written = write(outfile, (char *) buf, len);
506 if (written != len) {
507 fprintf(stderr, msg, strerror(errno));
508 exit(1);
509 }
510 }
511
512
513 /*
514 * Output only three ECOFF sections, corresponding to ELF psecs
515 * for text, data, and bss.
516 */
517 int
518 make_ecoff_section_hdrs(ep, esecs)
519 struct ecoff_exechdr *ep;
520 struct ecoff_scnhdr *esecs;
521
522 {
523 ep->f.f_nscns = 6; /* XXX */
524
525 strcpy(esecs[0].s_name, ".text");
526 strcpy(esecs[1].s_name, ".data");
527 strcpy(esecs[2].s_name, ".bss");
528
529 esecs[0].s_paddr = esecs[0].s_vaddr = ep->a.text_start;
530 esecs[1].s_paddr = esecs[1].s_vaddr = ep->a.data_start;
531 esecs[2].s_paddr = esecs[2].s_vaddr = ep->a.bss_start;
532 esecs[0].s_size = ep->a.tsize;
533 esecs[1].s_size = ep->a.dsize;
534 esecs[2].s_size = ep->a.bsize;
535
536 esecs[0].s_scnptr = ECOFF_TXTOFF(ep);
537 esecs[1].s_scnptr = ECOFF_DATOFF(ep);
538 #if 0
539 esecs[2].s_scnptr = esecs[1].s_scnptr +
540 ECOFF_ROUND(esecs[1].s_size, ECOFF_SEGMENT_ALIGNMENT(ep));
541 #endif
542
543 esecs[0].s_relptr = esecs[1].s_relptr = esecs[2].s_relptr = 0;
544 esecs[0].s_lnnoptr = esecs[1].s_lnnoptr = esecs[2].s_lnnoptr = 0;
545 esecs[0].s_nreloc = esecs[1].s_nreloc = esecs[2].s_nreloc = 0;
546 esecs[0].s_nlnno = esecs[1].s_nlnno = esecs[2].s_nlnno = 0;
547
548 esecs[1].s_flags = 0x100; /* ECOFF rdata */
549 esecs[3].s_flags = 0x200; /* ECOFF sdata */
550 esecs[4].s_flags = 0x400; /* ECOFF sbss */
551
552 /*
553 * Set the symbol-table offset to point at the end of any
554 * sections we loaded above, so later code can use it to write
555 * symbol table info..
556 */
557 ep->f.f_symptr = esecs[1].s_scnptr + esecs[1].s_size;
558 return (ep->f.f_nscns);
559 }
560
561
562 /*
563 * Write the ECOFF symbol header.
564 * Guess at how big the symbol table will be.
565 * Mark all symbols as EXTERN (for now).
566 */
567 void
568 write_ecoff_symhdr(out, ep, symhdrp, nesyms, extsymoff, extstroff, strsize)
569 int out;
570 struct ecoff_exechdr *ep;
571 struct ecoff_symhdr *symhdrp;
572 long nesyms, extsymoff, extstroff, strsize;
573 {
574 if (debug)
575 fprintf(stderr, "writing symhdr for %ld entries at offset 0x%lx\n",
576 nesyms, (u_long) ep->f.f_symptr);
577
578 ep->f.f_nsyms = sizeof(struct ecoff_symhdr);
579
580 memset(symhdrp, 0, sizeof(*symhdrp));
581 symhdrp->esymMax = nesyms;
582 symhdrp->magic = 0x7009;/* XXX */
583 symhdrp->cbExtOffset = extsymoff;
584 symhdrp->cbSsExtOffset = extstroff;
585
586 symhdrp->issExtMax = strsize;
587 if (debug)
588 fprintf(stderr,
589 "ECOFF symhdr: symhdr %x, strsize %lx, symsize %lx\n",
590 sizeof(*symhdrp), strsize,
591 (nesyms * sizeof(struct ecoff_extsym)));
592
593 safewrite(out, symhdrp, sizeof(*symhdrp),
594 "writing symbol header: %s\n");
595 }
596
597
598 void
599 elf_read_syms(elfsymsp, in, symoff, symsize, stroff, strsize)
600 struct elf_syms *elfsymsp;
601 int in;
602 off_t symoff, symsize;
603 off_t stroff, strsize;
604 {
605 register int nsyms;
606 nsyms = symsize / sizeof(Elf32_Sym);
607
608 /* Suck in the ELF symbol list... */
609 elfsymsp->elf_syms = (Elf32_Sym *)
610 saveRead(in, symoff, nsyms * sizeof(Elf32_Sym),
611 "ELF symboltable");
612 elfsymsp->nsymbols = nsyms;
613
614 /* Suck in the ELF string table... */
615 elfsymsp->stringtab = (char *)
616 saveRead(in, stroff, strsize, "ELF string table");
617 elfsymsp->stringsize = strsize;
618 }
619
620
621 /*
622 *
623 */
624 void
625 elf_symbol_table_to_ecoff(out, in, ep, symoff, symsize, stroff, strsize)
626 int out, in;
627 struct ecoff_exechdr *ep;
628 off_t symoff, symsize;
629 off_t stroff, strsize;
630 {
631
632 struct elf_syms elfsymtab;
633 struct ecoff_syms ecoffsymtab;
634 register u_long ecoff_symhdr_off, symtaboff, stringtaboff;
635 register u_long nextoff, symtabsize, ecoff_strsize;
636 int nsyms;
637 struct ecoff_symhdr symhdr;
638 int padding;
639
640 /* Read in the ELF symbols. */
641 elf_read_syms(&elfsymtab, in, symoff, symsize, stroff, strsize);
642
643 /* Approximate translation to ECOFF. */
644 translate_syms(&elfsymtab, &ecoffsymtab);
645 nsyms = ecoffsymtab.nsymbols;
646
647 /* Compute output ECOFF symbol- and string-table offsets. */
648 ecoff_symhdr_off = ep->f.f_symptr;
649
650 nextoff = ecoff_symhdr_off + sizeof(struct ecoff_symhdr);
651 stringtaboff = nextoff;
652 ecoff_strsize = ECOFF_ROUND(ecoffsymtab.stringsize,
653 (ECOFF_SEGMENT_ALIGNMENT(ep)));
654
655
656 nextoff = stringtaboff + ecoff_strsize;
657 symtaboff = nextoff;
658 symtabsize = nsyms * sizeof(struct ecoff_extsym);
659 symtabsize = ECOFF_ROUND(symtabsize, ECOFF_SEGMENT_ALIGNMENT(ep));
660
661 /* Write out the symbol header ... */
662 write_ecoff_symhdr(out, ep, &symhdr, nsyms, symtaboff,
663 stringtaboff, ecoffsymtab.stringsize);
664
665 /* Write out the string table... */
666 padding = ecoff_strsize - ecoffsymtab.stringsize;
667 safewrite(out, ecoffsymtab.stringtab, ecoffsymtab.stringsize,
668 "string table: write: %s\n");
669 if (padding)
670 pad16(out, padding, "string table: padding: %s\n");
671
672
673 /* Write out the symbol table... */
674 padding = symtabsize - (nsyms * sizeof(struct ecoff_extsym));
675 safewrite(out, ecoffsymtab.ecoff_syms,
676 nsyms * sizeof(struct ecoff_extsym),
677 "symbol table: write: %s\n");
678 if (padding)
679 pad16(out, padding, "symbols: padding: %s\n");
680 }
681
682
683
684 /*
685 * In-memory translation of ELF symbosl to ECOFF.
686 */
687 void
688 translate_syms(elfp, ecoffp)
689 struct elf_syms *elfp;
690 struct ecoff_syms *ecoffp;
691 {
692
693 int i;
694 char *oldstringbase;
695 char *newstrings, *nsp;
696
697 int nsyms, idx;
698
699 nsyms = elfp->nsymbols;
700 oldstringbase = elfp->stringtab;
701
702 /* Allocate space for corresponding ECOFF symbols. */
703 memset(ecoffp, 0, sizeof(*ecoffp));
704
705 ecoffp->nsymbols = 0;
706 ecoffp->ecoff_syms = malloc(sizeof(struct ecoff_extsym) * nsyms);
707
708 /* we are going to be no bigger than the ELF symbol table. */
709 ecoffp->stringsize = elfp->stringsize;
710 ecoffp->stringtab = malloc(elfp->stringsize);
711
712 newstrings = (char *) ecoffp->stringtab;
713 nsp = (char *) ecoffp->stringtab;
714 if (!newstrings) {
715 fprintf(stderr, "No memory for new string table!\n");
716 exit(1);
717 }
718 /* Copy and translate symbols... */
719 idx = 0;
720 for (i = 0; i < nsyms; i++) {
721 int binding, type;
722
723 binding = ELF_SYM_BIND((elfp->elf_syms[i].st_info));
724 type = ELF_SYM_TYPE((elfp->elf_syms[i].st_info));
725
726 /* skip strange symbols */
727 if (binding == 0) {
728 continue;
729 }
730 /* Copy the symbol into the new table */
731 strcpy(nsp, oldstringbase + elfp->elf_syms[i].st_name);
732 ecoffp->ecoff_syms[idx].es_strindex = nsp - newstrings;
733 nsp += strlen(nsp) + 1;
734
735 /* translate symbol types to ECOFF XXX */
736 ecoffp->ecoff_syms[idx].es_type = 1;
737 ecoffp->ecoff_syms[idx].es_class = 5;
738
739 /* Symbol values in executables should be compatible. */
740 ecoffp->ecoff_syms[idx].es_value = elfp->elf_syms[i].st_value;
741 ecoffp->ecoff_syms[idx].es_symauxindex = 0xfffff;
742
743 idx++;
744 }
745
746 ecoffp->nsymbols = idx;
747 ecoffp->stringsize = nsp - newstrings;
748 }
749 /*
750 * pad to a 16-byte boundary
751 */
752 void
753 pad16(int fd, int size, const char *msg)
754 {
755 safewrite(fd, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0", size, msg);
756 }
757