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