fixcoff.c revision 1.3
11.3Scgd/* $NetBSD: fixcoff.c,v 1.3 2001/02/19 22:48:58 cgd Exp $ */ 21.1Swrstuden 31.1Swrstuden/* 41.1Swrstuden * Copyright (c) 1999 National Aeronautics & Space Administration 51.1Swrstuden * All rights reserved. 61.1Swrstuden * 71.1Swrstuden * This software was written by William Studenmund of the 81.1Swrstuden * Numerical Aerospace Similation Facility, NASA Ames Research Center. 91.1Swrstuden * 101.1Swrstuden * Redistribution and use in source and binary forms, with or without 111.1Swrstuden * modification, are permitted provided that the following conditions 121.1Swrstuden * are met: 131.1Swrstuden * 1. Redistributions of source code must retain the above copyright 141.1Swrstuden * notice, this list of conditions and the following disclaimer. 151.1Swrstuden * 2. Redistributions in binary form must reproduce the above copyright 161.1Swrstuden * notice, this list of conditions and the following disclaimer in the 171.1Swrstuden * documentation and/or other materials provided with the distribution. 181.2Ssoren * 3. Neither the name of the National Aeronautics & Space Administration 191.1Swrstuden * nor the names of its contributors may be used to endorse or promote 201.1Swrstuden * products derived from this software without specific prior written 211.1Swrstuden * permission. 221.1Swrstuden * 231.1Swrstuden * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION 241.1Swrstuden * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 251.1Swrstuden * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 261.1Swrstuden * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB- 271.1Swrstuden * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 281.1Swrstuden * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 291.1Swrstuden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 301.1Swrstuden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 311.1Swrstuden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 321.1Swrstuden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 331.1Swrstuden * POSSIBILITY OF SUCH DAMAGE. 341.1Swrstuden */ 351.1Swrstuden 361.1Swrstuden/* 371.1Swrstuden * This program fixes up the extended xcoff headers generated when an elf 381.1Swrstuden * file is turned into an xcoff one with the current objcopy. It should 391.1Swrstuden * go away someday, when objcopy will correctly fix up the output xcoff 401.1Swrstuden * 411.1Swrstuden * Partially inspired by hack-coff, written by Paul Mackerras. 421.1Swrstuden */ 431.1Swrstuden 441.1Swrstuden#include <stdio.h> 451.1Swrstuden#include <stdlib.h> 461.1Swrstuden#include <unistd.h> 471.1Swrstuden#include <fcntl.h> 481.1Swrstuden 491.1Swrstuden#include "../../../../../gnu/dist/include/coff/rs6000.h" 501.1Swrstuden 511.1Swrstudenvoid 521.1Swrstudenusage(prog) 531.1Swrstuden char *prog; 541.1Swrstuden{ 551.1Swrstuden fprintf(stderr, "Usage: %s [-h] | [<file to fix>]\n", prog); 561.1Swrstuden} 571.1Swrstuden 581.1Swrstudenvoid 591.1Swrstudenhelp(prog) 601.1Swrstuden char *prog; 611.1Swrstuden{ 621.1Swrstuden fprintf(stderr, "%s\tis designed to fix the xcoff headers in a\n", prog); 631.1Swrstuden fprintf(stderr, 641.1Swrstuden"\tbinary generated using objcopy from a non-xcoff source.\n"); 651.1Swrstuden usage(prog); 661.1Swrstuden exit(0); 671.1Swrstuden} 681.1Swrstuden 691.1Swrstudenmain(argc, argv) 701.1Swrstuden int argc; 711.1Swrstuden char * const *argv; 721.1Swrstuden{ 731.1Swrstuden int fd, i, n, ch; 741.1Swrstuden struct external_filehdr efh; 751.1Swrstuden AOUTHDR aoh; 761.1Swrstuden struct external_scnhdr shead; 771.1Swrstuden 781.1Swrstuden while ((ch = getopt(argc, argv, "h")) != -1) 791.1Swrstuden switch (ch) { 801.1Swrstuden case 'h': 811.3Scgd help(getprogname()); 821.1Swrstuden } 831.1Swrstuden 841.1Swrstuden argc -= optind; 851.1Swrstuden argv += optind; 861.1Swrstuden 871.1Swrstuden if (argc != 1) { 881.3Scgd usage(getprogname()); 891.1Swrstuden exit(1); 901.1Swrstuden } 911.1Swrstuden 921.1Swrstuden if ((fd = open(argv[0], O_RDWR, 0)) == -1) 931.1Swrstuden err(i, "%s", argv[0]); 941.1Swrstuden 951.1Swrstuden /* 961.1Swrstuden * Make sure it looks like an xcoff file.. 971.1Swrstuden */ 981.1Swrstuden if (read(fd, &efh, sizeof(efh)) != sizeof(efh)) 991.1Swrstuden err(1, "%s reading header", argv[0]); 1001.1Swrstuden 1011.1Swrstuden i = ntohs(*(u_int16_t *)efh.f_magic); 1021.1Swrstuden if ((i != U802WRMAGIC) && (i != U802ROMAGIC) && (i != U802TOCMAGIC)) 1031.1Swrstuden errx(1, "%s: not a valid xcoff file", argv[0]); 1041.1Swrstuden 1051.1Swrstuden /* Does the AOUT "Optional header" make sence? */ 1061.1Swrstuden i = ntohs(*(u_int16_t *)efh.f_opthdr); 1071.1Swrstuden 1081.1Swrstuden if (i == SMALL_AOUTSZ) 1091.3Scgd errx(1, "%s: file has small \"optional\" header, inappropriate for use with %s", argv[0], getprogname()); 1101.1Swrstuden else if (i != AOUTSZ) 1111.1Swrstuden errx(1, "%s: invalid \"optional\" header", argv[0]); 1121.1Swrstuden 1131.1Swrstuden if (read(fd, &aoh, i) != i) 1141.1Swrstuden err(1, "%s reading \"optional\" header", argv[0]); 1151.1Swrstuden 1161.1Swrstuden /* Now start filing in the AOUT header */ 1171.1Swrstuden *(u_int16_t *)aoh.magic = htons(RS6K_AOUTHDR_ZMAGIC); 1181.1Swrstuden n = ntohs(*(u_int16_t *)efh.f_nscns); 1191.1Swrstuden 1201.1Swrstuden for (i = 0; i < n; i++) { 1211.1Swrstuden if (read(fd, &shead, sizeof(shead)) != sizeof(shead)) 1221.1Swrstuden err(1, "%s reading section headers", argv[0]); 1231.1Swrstuden if (strcmp(shead.s_name, ".text") == 0) { 1241.1Swrstuden *(u_int16_t *)(aoh.o_snentry) = htons(i+1); 1251.1Swrstuden *(u_int16_t *)(aoh.o_sntext) = htons(i+1); 1261.1Swrstuden } else if (strcmp(shead.s_name, ".data") == 0) { 1271.1Swrstuden *(u_int16_t *)(aoh.o_sndata) = htons(i+1); 1281.1Swrstuden } else if (strcmp(shead.s_name, ".bss") == 0) { 1291.1Swrstuden *(u_int16_t *)(aoh.o_snbss) = htons(i+1); 1301.1Swrstuden } 1311.1Swrstuden } 1321.1Swrstuden 1331.1Swrstuden /* now write it out */ 1341.1Swrstuden if (pwrite(fd, &aoh, sizeof(aoh), sizeof(struct external_filehdr)) 1351.1Swrstuden != sizeof(aoh)) 1361.1Swrstuden err(1, "%s writing modified header", argv[0]); 1371.1Swrstuden close(fd); 1381.1Swrstuden exit(0); 1391.1Swrstuden} 140