1 /* $NetBSD: elf2aout.c,v 1.23 2019/05/19 09:14:13 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1995 5 * Ted Lemon (hereinafter referred to as the author) 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 /* elf2aout.c 32 33 This program converts an elf executable to a NetBSD a.out executable. 34 The minimal symbol table is copied, but the debugging symbols and 35 other informational sections are not. */ 36 37 #if HAVE_NBTOOL_CONFIG_H 38 #include "nbtool_config.h" 39 #endif 40 41 #ifndef TARGET_BYTE_ORDER 42 #define TARGET_BYTE_ORDER BYTE_ORDER 43 #endif 44 45 #include <sys/types.h> 46 #include <sys/exec_aout.h> 47 #include <sys/exec_elf.h> 48 49 #include <a.out.h> 50 #include <err.h> 51 #include <errno.h> 52 #include <fcntl.h> 53 #include <limits.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <unistd.h> 58 59 60 struct sect { 61 /* should be unsigned long, but assume no a.out binaries on LP64 */ 62 uint32_t vaddr; 63 uint32_t len; 64 }; 65 66 static void combine(struct sect *, struct sect *, int); 67 static int phcmp(const void *, const void *); 68 static void *saveRead(int file, off_t offset, size_t len, const char *name); 69 static void copy(int, int, off_t, off_t); 70 static void translate_syms(int, int, off_t, off_t, off_t, off_t); 71 72 #if TARGET_BYTE_ORDER != BYTE_ORDER 73 static void bswap32_region(int32_t* , int); 74 #endif 75 76 static int *symTypeTable; 77 static int debug; 78 79 static __dead void 80 usage(void) 81 { 82 fprintf(stderr, "Usage: %s [-Os] <elf executable> <a.out executable>\n", 83 getprogname()); 84 exit(EXIT_FAILURE); 85 } 86 87 static const struct { 88 const char *n; 89 int v; 90 } nv[] = { 91 { ".text", N_TEXT }, 92 { ".rodata", N_TEXT }, 93 { ".data", N_DATA }, 94 { ".sdata", N_DATA }, 95 { ".lit4", N_DATA }, 96 { ".lit8", N_DATA }, 97 { ".bss", N_BSS }, 98 { ".sbss", N_BSS }, 99 }; 100 101 static int 102 get_symtab_type(const char *name) 103 { 104 size_t i; 105 for (i = 0; i < __arraycount(nv); i++) { 106 if (strcmp(name, nv[i].n) == 0) 107 return nv[i].v; 108 } 109 if (debug) 110 warnx("section `%s' is not handled\n", name); 111 return 0; 112 } 113 114 static uint32_t 115 get_mid(const Elf32_Ehdr *ex) 116 { 117 switch (ex->e_machine) { 118 #ifdef notyet 119 case EM_AARCH64: 120 return MID_AARCH64; 121 case EM_ALPHA: 122 return MID_ALPHA; 123 #endif 124 case EM_ARM: 125 return MID_ARM6; 126 #ifdef notyet 127 case EM_PARISC: 128 return MID_HPPA; 129 #endif 130 case EM_386: 131 return MID_I386; 132 case EM_68K: 133 return MID_M68K; 134 case EM_OR1K: 135 return MID_OR1K; 136 case EM_MIPS: 137 if (ex->e_ident[EI_DATA] == ELFDATA2LSB) 138 return MID_PMAX; 139 else 140 return MID_MIPS; 141 case EM_PPC: 142 return MID_POWERPC; 143 #ifdef notyet 144 case EM_PPC64: 145 return MID_POWERPC64; 146 break; 147 #endif 148 case EM_RISCV: 149 return MID_RISCV; 150 case EM_SH: 151 return MID_SH3; 152 case EM_SPARC: 153 case EM_SPARC32PLUS: 154 case EM_SPARCV9: 155 if (ex->e_ident[EI_CLASS] == ELFCLASS32) 156 return MID_SPARC; 157 #ifdef notyet 158 return MID_SPARC64; 159 case EM_X86_64: 160 return MID_X86_64; 161 #else 162 break; 163 #endif 164 case EM_VAX: 165 return MID_VAX; 166 case EM_NONE: 167 return MID_ZERO; 168 default: 169 break; 170 } 171 if (debug) 172 warnx("Unsupported machine `%d'", ex->e_machine); 173 return MID_ZERO; 174 } 175 176 static unsigned char 177 get_type(Elf32_Half shndx) 178 { 179 switch (shndx) { 180 case SHN_UNDEF: 181 return N_UNDF; 182 case SHN_ABS: 183 return N_ABS; 184 case SHN_COMMON: 185 case SHN_MIPS_ACOMMON: 186 return N_COMM; 187 default: 188 return (unsigned char)symTypeTable[shndx]; 189 } 190 } 191 192 int 193 main(int argc, char **argv) 194 { 195 Elf32_Ehdr ex; 196 Elf32_Phdr *ph; 197 Elf32_Shdr *sh; 198 char *shstrtab; 199 ssize_t i, strtabix, symtabix; 200 struct sect text, data, bss; 201 struct exec aex; 202 int infile, outfile; 203 uint32_t cur_vma = UINT32_MAX; 204 uint32_t mid; 205 int symflag = 0, c; 206 unsigned long magic = ZMAGIC; 207 208 strtabix = symtabix = 0; 209 text.len = data.len = bss.len = 0; 210 text.vaddr = data.vaddr = bss.vaddr = 0; 211 212 while ((c = getopt(argc, argv, "dOs")) != -1) { 213 switch (c) { 214 case 'd': 215 debug++; 216 break; 217 case 's': 218 symflag = 1; 219 break; 220 case 'O': 221 magic = OMAGIC; 222 break; 223 case '?': 224 default: 225 usage: 226 usage(); 227 } 228 } 229 230 argc -= optind; 231 argv += optind; 232 233 /* Check args... */ 234 if (argc != 2) 235 goto usage; 236 237 238 /* Try the input file... */ 239 if ((infile = open(argv[0], O_RDONLY)) < 0) 240 err(EXIT_FAILURE, "Can't open `%s' for read", argv[0]); 241 242 /* Read the header, which is at the beginning of the file... */ 243 i = read(infile, &ex, sizeof ex); 244 if (i != sizeof ex) { 245 if (i == -1) 246 err(EXIT_FAILURE, "Error reading `%s'", argv[1]); 247 else 248 errx(EXIT_FAILURE, "End of file reading `%s'", argv[1]); 249 } 250 #if TARGET_BYTE_ORDER != BYTE_ORDER 251 ex.e_type = bswap16(ex.e_type); 252 ex.e_machine = bswap16(ex.e_machine); 253 ex.e_version = bswap32(ex.e_version); 254 ex.e_entry = bswap32(ex.e_entry); 255 ex.e_phoff = bswap32(ex.e_phoff); 256 ex.e_shoff = bswap32(ex.e_shoff); 257 ex.e_flags = bswap32(ex.e_flags); 258 ex.e_ehsize = bswap16(ex.e_ehsize); 259 ex.e_phentsize = bswap16(ex.e_phentsize); 260 ex.e_phnum = bswap16(ex.e_phnum); 261 ex.e_shentsize = bswap16(ex.e_shentsize); 262 ex.e_shnum = bswap16(ex.e_shnum); 263 ex.e_shstrndx = bswap16(ex.e_shstrndx); 264 #endif 265 // Not yet 266 if (ex.e_ident[EI_CLASS] == ELFCLASS64) 267 errx(EXIT_FAILURE, "Only 32 bit is supported"); 268 269 /* Read the program headers... */ 270 ph = saveRead(infile, ex.e_phoff, 271 (size_t)ex.e_phnum * sizeof(Elf32_Phdr), "ph"); 272 #if TARGET_BYTE_ORDER != BYTE_ORDER 273 bswap32_region((int32_t*)ph, sizeof(Elf32_Phdr) * ex.e_phnum); 274 #endif 275 /* Read the section headers... */ 276 sh = saveRead(infile, ex.e_shoff, 277 (size_t)ex.e_shnum * sizeof(Elf32_Shdr), "sh"); 278 #if TARGET_BYTE_ORDER != BYTE_ORDER 279 bswap32_region((int32_t*)sh, sizeof(Elf32_Shdr) * ex.e_shnum); 280 #endif 281 /* Read in the section string table. */ 282 shstrtab = saveRead(infile, sh[ex.e_shstrndx].sh_offset, 283 (size_t)sh[ex.e_shstrndx].sh_size, "shstrtab"); 284 285 /* Find space for a table matching ELF section indices to a.out symbol 286 * types. */ 287 symTypeTable = malloc(ex.e_shnum * sizeof(int)); 288 if (symTypeTable == NULL) 289 err(EXIT_FAILURE, "symTypeTable: can't allocate"); 290 memset(symTypeTable, 0, ex.e_shnum * sizeof(int)); 291 292 /* Look for the symbol table and string table... Also map section 293 * indices to symbol types for a.out */ 294 for (i = 0; i < ex.e_shnum; i++) { 295 char *name = shstrtab + sh[i].sh_name; 296 if (!strcmp(name, ".symtab")) 297 symtabix = i; 298 else if (!strcmp(name, ".strtab")) 299 strtabix = i; 300 else 301 symTypeTable[i] = get_symtab_type(name); 302 } 303 304 /* Figure out if we can cram the program header into an a.out 305 * header... Basically, we can't handle anything but loadable 306 * segments, but we can ignore some kinds of segments. We can't 307 * handle holes in the address space, and we handle start addresses 308 * other than 0x1000 by hoping that the loader will know where to load 309 * - a.out doesn't have an explicit load address. Segments may be 310 * out of order, so we sort them first. */ 311 qsort(ph, ex.e_phnum, sizeof(Elf32_Phdr), phcmp); 312 for (i = 0; i < ex.e_phnum; i++) { 313 /* Section types we can ignore... */ 314 if (ph[i].p_type == PT_NULL || ph[i].p_type == PT_NOTE || 315 ph[i].p_type == PT_PHDR || ph[i].p_type == PT_MIPS_REGINFO) 316 continue; 317 /* Section types we can't handle... */ 318 if (ph[i].p_type == PT_TLS) { 319 if (debug) 320 warnx("Can't handle TLS section"); 321 continue; 322 } 323 if (ph[i].p_type != PT_LOAD) 324 errx(EXIT_FAILURE, "Program header %zd " 325 "type %d can't be converted.", i, ph[i].p_type); 326 /* Writable (data) segment? */ 327 if (ph[i].p_flags & PF_W) { 328 struct sect ndata, nbss; 329 330 ndata.vaddr = ph[i].p_vaddr; 331 ndata.len = ph[i].p_filesz; 332 nbss.vaddr = ph[i].p_vaddr + ph[i].p_filesz; 333 nbss.len = ph[i].p_memsz - ph[i].p_filesz; 334 335 combine(&data, &ndata, 0); 336 combine(&bss, &nbss, 1); 337 } else { 338 struct sect ntxt; 339 340 ntxt.vaddr = ph[i].p_vaddr; 341 ntxt.len = ph[i].p_filesz; 342 343 combine(&text, &ntxt, 0); 344 } 345 /* Remember the lowest segment start address. */ 346 if (ph[i].p_vaddr < cur_vma) 347 cur_vma = ph[i].p_vaddr; 348 } 349 350 /* Sections must be in order to be converted... */ 351 if (text.vaddr > data.vaddr || data.vaddr > bss.vaddr || 352 text.vaddr + text.len > data.vaddr || 353 data.vaddr + data.len > bss.vaddr) 354 errx(EXIT_FAILURE, "Sections ordering prevents a.out " 355 "conversion."); 356 /* If there's a data section but no text section, then the loader 357 * combined everything into one section. That needs to be the text 358 * section, so just make the data section zero length following text. */ 359 if (data.len && text.len == 0) { 360 text = data; 361 data.vaddr = text.vaddr + text.len; 362 data.len = 0; 363 } 364 /* If there is a gap between text and data, we'll fill it when we copy 365 * the data, so update the length of the text segment as represented 366 * in a.out to reflect that, since a.out doesn't allow gaps in the 367 * program address space. */ 368 if (text.vaddr + text.len < data.vaddr) 369 text.len = data.vaddr - text.vaddr; 370 371 /* We now have enough information to cons up an a.out header... */ 372 mid = get_mid(&ex); 373 aex.a_midmag = (u_long)htonl(((u_long)symflag << 26) 374 | ((u_long)mid << 16) | magic); 375 376 aex.a_text = text.len; 377 aex.a_data = data.len; 378 aex.a_bss = bss.len; 379 aex.a_entry = ex.e_entry; 380 aex.a_syms = (sizeof(struct nlist) * 381 (symtabix != -1 ? sh[symtabix].sh_size / sizeof(Elf32_Sym) : 0)); 382 aex.a_trsize = 0; 383 aex.a_drsize = 0; 384 #if TARGET_BYTE_ORDER != BYTE_ORDER 385 aex.a_text = bswap32(aex.a_text); 386 aex.a_data = bswap32(aex.a_data); 387 aex.a_bss = bswap32(aex.a_bss); 388 aex.a_entry = bswap32(aex.a_entry); 389 aex.a_syms = bswap32(aex.a_syms); 390 aex.a_trsize = bswap32(aex.a_trsize); 391 aex.a_drsize = bswap32(aex.a_drsize); 392 #endif 393 394 /* Make the output file... */ 395 if ((outfile = open(argv[1], O_WRONLY | O_CREAT, 0777)) < 0) 396 err(EXIT_FAILURE, "Unable to create `%s'", argv[1]); 397 /* Truncate file... */ 398 if (ftruncate(outfile, 0)) { 399 warn("ftruncate %s", argv[1]); 400 } 401 /* Write the header... */ 402 i = write(outfile, &aex, sizeof aex); 403 if (i != sizeof aex) 404 err(EXIT_FAILURE, "Can't write `%s'", argv[1]); 405 /* Copy the loadable sections. Zero-fill any gaps less than 64k; 406 * complain about any zero-filling, and die if we're asked to 407 * zero-fill more than 64k. */ 408 for (i = 0; i < ex.e_phnum; i++) { 409 /* Unprocessable sections were handled above, so just verify 410 * that the section can be loaded before copying. */ 411 if (ph[i].p_type == PT_LOAD && ph[i].p_filesz) { 412 if (cur_vma != ph[i].p_vaddr) { 413 uint32_t gap = ph[i].p_vaddr - cur_vma; 414 char obuf[1024]; 415 if (gap > 65536) 416 errx(EXIT_FAILURE, 417 "Intersegment gap (%u bytes) too large", gap); 418 if (debug) 419 warnx("%u byte intersegment gap", gap); 420 memset(obuf, 0, sizeof obuf); 421 while (gap) { 422 ssize_t count = write(outfile, obuf, 423 (gap > sizeof obuf 424 ? sizeof obuf : gap)); 425 if (count < 0) 426 err(EXIT_FAILURE, 427 "Error writing gap"); 428 gap -= (uint32_t)count; 429 } 430 } 431 copy(outfile, infile, ph[i].p_offset, ph[i].p_filesz); 432 cur_vma = ph[i].p_vaddr + ph[i].p_filesz; 433 } 434 } 435 436 /* Copy and translate the symbol table... */ 437 translate_syms(outfile, infile, 438 sh[symtabix].sh_offset, sh[symtabix].sh_size, 439 sh[strtabix].sh_offset, sh[strtabix].sh_size); 440 441 free(ph); 442 free(sh); 443 free(shstrtab); 444 free(symTypeTable); 445 /* Looks like we won... */ 446 return EXIT_SUCCESS; 447 } 448 /* translate_syms (out, in, offset, size) 449 450 Read the ELF symbol table from in at offset; translate it into a.out 451 nlist format and write it to out. */ 452 453 void 454 translate_syms(int out, int in, off_t symoff, off_t symsize, 455 off_t stroff, off_t strsize) 456 { 457 #define SYMS_PER_PASS 64 458 Elf32_Sym inbuf[64]; 459 struct nlist outbuf[64]; 460 ssize_t i, remaining, cur; 461 char *oldstrings; 462 char *newstrings, *nsp; 463 size_t newstringsize; 464 uint32_t stringsizebuf; 465 466 /* Zero the unused fields in the output buffer.. */ 467 memset(outbuf, 0, sizeof outbuf); 468 469 /* Find number of symbols to process... */ 470 remaining = (ssize_t)(symsize / (off_t)sizeof(Elf32_Sym)); 471 472 /* Suck in the old string table... */ 473 oldstrings = saveRead(in, stroff, (size_t)strsize, "string table"); 474 475 /* 476 * Allocate space for the new one. We will increase the space if 477 * this is too small 478 */ 479 newstringsize = (size_t)(strsize + remaining); 480 newstrings = malloc(newstringsize); 481 if (newstrings == NULL) 482 err(EXIT_FAILURE, "No memory for new string table!"); 483 /* Initialize the table pointer... */ 484 nsp = newstrings; 485 486 /* Go the start of the ELF symbol table... */ 487 if (lseek(in, symoff, SEEK_SET) < 0) 488 err(EXIT_FAILURE, "Can't seek"); 489 /* Translate and copy symbols... */ 490 for (; remaining; remaining -= cur) { 491 cur = remaining; 492 if (cur > SYMS_PER_PASS) 493 cur = SYMS_PER_PASS; 494 if ((i = read(in, inbuf, (size_t)cur * sizeof(Elf32_Sym))) 495 != cur * (ssize_t)sizeof(Elf32_Sym)) { 496 if (i < 0) 497 err(EXIT_FAILURE, "%s: read error", __func__); 498 else 499 errx(EXIT_FAILURE, "%s: premature end of file", 500 __func__); 501 } 502 /* Do the translation... */ 503 for (i = 0; i < cur; i++) { 504 int binding, type; 505 size_t off, len; 506 507 #if TARGET_BYTE_ORDER != BYTE_ORDER 508 inbuf[i].st_name = bswap32(inbuf[i].st_name); 509 inbuf[i].st_value = bswap32(inbuf[i].st_value); 510 inbuf[i].st_size = bswap32(inbuf[i].st_size); 511 inbuf[i].st_shndx = bswap16(inbuf[i].st_shndx); 512 #endif 513 off = (size_t)(nsp - newstrings); 514 515 /* length of this symbol with leading '_' and trailing '\0' */ 516 len = strlen(oldstrings + inbuf[i].st_name) + 1 + 1; 517 518 /* Does it fit? If not make more space */ 519 if (newstringsize - off < len) { 520 char *nns; 521 522 newstringsize += (size_t)(remaining) * len; 523 nns = realloc(newstrings, newstringsize); 524 if (nns == NULL) 525 err(EXIT_FAILURE, "No memory for new string table!"); 526 newstrings = nns; 527 nsp = newstrings + off; 528 } 529 /* Copy the symbol into the new table, but prepend an 530 * underscore. */ 531 *nsp = '_'; 532 strcpy(nsp + 1, oldstrings + inbuf[i].st_name); 533 outbuf[i].n_un.n_strx = nsp - newstrings + 4; 534 nsp += len; 535 536 type = ELF32_ST_TYPE(inbuf[i].st_info); 537 binding = ELF32_ST_BIND(inbuf[i].st_info); 538 539 /* Convert ELF symbol type/section/etc info into a.out 540 * type info. */ 541 if (type == STT_FILE) 542 outbuf[i].n_type = N_FN; 543 else 544 outbuf[i].n_type = get_type(inbuf[i].st_shndx); 545 if (binding == STB_GLOBAL) 546 outbuf[i].n_type |= N_EXT; 547 /* Symbol values in executables should be compatible. */ 548 outbuf[i].n_value = inbuf[i].st_value; 549 #if TARGET_BYTE_ORDER != BYTE_ORDER 550 outbuf[i].n_un.n_strx = bswap32(outbuf[i].n_un.n_strx); 551 outbuf[i].n_desc = bswap16(outbuf[i].n_desc); 552 outbuf[i].n_value = bswap32(outbuf[i].n_value); 553 #endif 554 } 555 /* Write out the symbols... */ 556 if ((i = write(out, outbuf, (size_t)cur * sizeof(struct nlist))) 557 != cur * (ssize_t)sizeof(struct nlist)) 558 err(EXIT_FAILURE, "%s: write failed", __func__); 559 } 560 /* Write out the string table length... */ 561 stringsizebuf = (uint32_t)newstringsize; 562 #if TARGET_BYTE_ORDER != BYTE_ORDER 563 stringsizebuf = bswap32(stringsizebuf); 564 #endif 565 if (write(out, &stringsizebuf, sizeof stringsizebuf) 566 != sizeof stringsizebuf) 567 err(EXIT_FAILURE, "%s: newstringsize: write failed", __func__); 568 /* Write out the string table... */ 569 if (write(out, newstrings, newstringsize) != (ssize_t)newstringsize) 570 err(EXIT_FAILURE, "%s: newstrings: write failed", __func__); 571 free(newstrings); 572 free(oldstrings); 573 } 574 575 static void 576 copy(int out, int in, off_t offset, off_t size) 577 { 578 char ibuf[4096]; 579 ssize_t remaining, cur, count; 580 581 /* Go to the start of the segment... */ 582 if (lseek(in, offset, SEEK_SET) < 0) 583 err(EXIT_FAILURE, "%s: lseek failed", __func__); 584 if (size > SSIZE_MAX) 585 err(EXIT_FAILURE, "%s: can not copy this much", __func__); 586 remaining = (ssize_t)size; 587 while (remaining) { 588 cur = remaining; 589 if (cur > (int)sizeof ibuf) 590 cur = sizeof ibuf; 591 remaining -= cur; 592 if ((count = read(in, ibuf, (size_t)cur)) != cur) { 593 if (count < 0) 594 err(EXIT_FAILURE, "%s: read error", __func__); 595 else 596 errx(EXIT_FAILURE, "%s: premature end of file", 597 __func__); 598 } 599 if ((count = write(out, ibuf, (size_t)cur)) != cur) 600 err(EXIT_FAILURE, "%s: write failed", __func__); 601 } 602 } 603 604 /* Combine two segments, which must be contiguous. If pad is true, it's 605 okay for there to be padding between. */ 606 static void 607 combine(struct sect *base, struct sect *new, int pad) 608 { 609 610 if (base->len == 0) 611 *base = *new; 612 else 613 if (new->len) { 614 if (base->vaddr + base->len != new->vaddr) { 615 if (pad) 616 base->len = new->vaddr - base->vaddr; 617 else 618 errx(EXIT_FAILURE, "Non-contiguous " 619 "data can't be converted"); 620 } 621 base->len += new->len; 622 } 623 } 624 625 static int 626 phcmp(const void *vh1, const void *vh2) 627 { 628 const Elf32_Phdr *h1, *h2; 629 630 h1 = (const Elf32_Phdr *)vh1; 631 h2 = (const Elf32_Phdr *)vh2; 632 633 if (h1->p_vaddr > h2->p_vaddr) 634 return 1; 635 else 636 if (h1->p_vaddr < h2->p_vaddr) 637 return -1; 638 else 639 return 0; 640 } 641 642 static void * 643 saveRead(int file, off_t offset, size_t len, const char *name) 644 { 645 char *tmp; 646 ssize_t count; 647 off_t off; 648 649 if ((off = lseek(file, offset, SEEK_SET)) < 0) 650 errx(EXIT_FAILURE, "%s: seek failed", name); 651 if ((tmp = malloc(len)) == NULL) 652 errx(EXIT_FAILURE, 653 "%s: Can't allocate %jd bytes.", name, (intmax_t)len); 654 count = read(file, tmp, len); 655 if ((size_t)count != len) { 656 if (count < 0) 657 err(EXIT_FAILURE, "%s: read error", name); 658 else 659 errx(EXIT_FAILURE, "%s: premature end of file", 660 name); 661 } 662 return tmp; 663 } 664 665 #if TARGET_BYTE_ORDER != BYTE_ORDER 666 /* swap a 32bit region */ 667 static void 668 bswap32_region(int32_t* p, int len) 669 { 670 size_t i; 671 672 for (i = 0; i < len / sizeof(int32_t); i++, p++) 673 *p = bswap32(*p); 674 } 675 #endif 676