1 /* $NetBSD: fixcoff.c,v 1.4 2002/09/17 03:55:40 briggs Exp $ */ 2 3 /* 4 * Copyright (c) 1999 National Aeronautics & Space Administration 5 * All rights reserved. 6 * 7 * This software was written by William Studenmund of the 8 * Numerical Aerospace Similation Facility, NASA Ames Research Center. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the National Aeronautics & Space Administration 19 * nor the names of its contributors may be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION 24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB- 27 * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 28 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 /* 37 * This program fixes up the extended xcoff headers generated when an elf 38 * file is turned into an xcoff one with the current objcopy. It should 39 * go away someday, when objcopy will correctly fix up the output xcoff 40 * 41 * Partially inspired by hack-coff, written by Paul Mackerras. 42 */ 43 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 #include <fcntl.h> 48 49 #include "../../../../../gnu/dist/toolchain/include/coff/rs6000.h" 50 51 void 52 usage(prog) 53 char *prog; 54 { 55 fprintf(stderr, "Usage: %s [-h] | [<file to fix>]\n", prog); 56 } 57 58 void 59 help(prog) 60 char *prog; 61 { 62 fprintf(stderr, "%s\tis designed to fix the xcoff headers in a\n", prog); 63 fprintf(stderr, 64 "\tbinary generated using objcopy from a non-xcoff source.\n"); 65 usage(prog); 66 exit(0); 67 } 68 69 main(argc, argv) 70 int argc; 71 char * const *argv; 72 { 73 int fd, i, n, ch; 74 struct external_filehdr efh; 75 AOUTHDR aoh; 76 struct external_scnhdr shead; 77 78 while ((ch = getopt(argc, argv, "h")) != -1) 79 switch (ch) { 80 case 'h': 81 help(getprogname()); 82 } 83 84 argc -= optind; 85 argv += optind; 86 87 if (argc != 1) { 88 usage(getprogname()); 89 exit(1); 90 } 91 92 if ((fd = open(argv[0], O_RDWR, 0)) == -1) 93 err(i, "%s", argv[0]); 94 95 /* 96 * Make sure it looks like an xcoff file.. 97 */ 98 if (read(fd, &efh, sizeof(efh)) != sizeof(efh)) 99 err(1, "%s reading header", argv[0]); 100 101 i = ntohs(*(u_int16_t *)efh.f_magic); 102 if ((i != U802WRMAGIC) && (i != U802ROMAGIC) && (i != U802TOCMAGIC)) 103 errx(1, "%s: not a valid xcoff file", argv[0]); 104 105 /* Does the AOUT "Optional header" make sence? */ 106 i = ntohs(*(u_int16_t *)efh.f_opthdr); 107 108 if (i == SMALL_AOUTSZ) 109 errx(1, "%s: file has small \"optional\" header, inappropriate for use with %s", argv[0], getprogname()); 110 else if (i != AOUTSZ) 111 errx(1, "%s: invalid \"optional\" header", argv[0]); 112 113 if (read(fd, &aoh, i) != i) 114 err(1, "%s reading \"optional\" header", argv[0]); 115 116 /* Now start filing in the AOUT header */ 117 *(u_int16_t *)aoh.magic = htons(RS6K_AOUTHDR_ZMAGIC); 118 n = ntohs(*(u_int16_t *)efh.f_nscns); 119 120 for (i = 0; i < n; i++) { 121 if (read(fd, &shead, sizeof(shead)) != sizeof(shead)) 122 err(1, "%s reading section headers", argv[0]); 123 if (strcmp(shead.s_name, ".text") == 0) { 124 *(u_int16_t *)(aoh.o_snentry) = htons(i+1); 125 *(u_int16_t *)(aoh.o_sntext) = htons(i+1); 126 } else if (strcmp(shead.s_name, ".data") == 0) { 127 *(u_int16_t *)(aoh.o_sndata) = htons(i+1); 128 } else if (strcmp(shead.s_name, ".bss") == 0) { 129 *(u_int16_t *)(aoh.o_snbss) = htons(i+1); 130 } 131 } 132 133 /* now write it out */ 134 if (pwrite(fd, &aoh, sizeof(aoh), sizeof(struct external_filehdr)) 135 != sizeof(aoh)) 136 err(1, "%s writing modified header", argv[0]); 137 close(fd); 138 exit(0); 139 } 140