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