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