fixcoff.c revision 1.1
11.1Swrstuden/*	$NetBSD: fixcoff.c,v 1.1 1999/11/23 01:32:37 wrstuden 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.1Swrstuden * 3. Neither the 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.1Swrstudenextern char *__progname;
521.1Swrstuden
531.1Swrstudenvoid
541.1Swrstudenusage(prog)
551.1Swrstuden	char	*prog;
561.1Swrstuden{
571.1Swrstuden	fprintf(stderr, "Usage: %s [-h] | [<file to fix>]\n", prog);
581.1Swrstuden}
591.1Swrstuden
601.1Swrstudenvoid
611.1Swrstudenhelp(prog)
621.1Swrstuden	char	*prog;
631.1Swrstuden{
641.1Swrstuden	fprintf(stderr, "%s\tis designed to fix the xcoff headers in a\n", prog);
651.1Swrstuden	fprintf(stderr,
661.1Swrstuden"\tbinary generated using objcopy from a non-xcoff source.\n");
671.1Swrstuden	usage(prog);
681.1Swrstuden	exit(0);
691.1Swrstuden}
701.1Swrstuden
711.1Swrstudenmain(argc, argv)
721.1Swrstuden	int		argc;
731.1Swrstuden	char * const	*argv;
741.1Swrstuden{
751.1Swrstuden	int	fd, i, n, ch;
761.1Swrstuden	struct	external_filehdr	efh;
771.1Swrstuden	AOUTHDR				aoh;
781.1Swrstuden	struct	external_scnhdr		shead;
791.1Swrstuden
801.1Swrstuden	while ((ch = getopt(argc, argv, "h")) != -1)
811.1Swrstuden	    switch (ch) {
821.1Swrstuden		case 'h':
831.1Swrstuden		help(__progname);
841.1Swrstuden	}
851.1Swrstuden
861.1Swrstuden	argc -= optind;
871.1Swrstuden	argv += optind;
881.1Swrstuden
891.1Swrstuden	if (argc != 1) {
901.1Swrstuden		usage(__progname);
911.1Swrstuden		exit(1);
921.1Swrstuden	}
931.1Swrstuden
941.1Swrstuden	if ((fd = open(argv[0], O_RDWR, 0)) == -1)
951.1Swrstuden		err(i, "%s", argv[0]);
961.1Swrstuden
971.1Swrstuden	/*
981.1Swrstuden	 * Make sure it looks like an xcoff file..
991.1Swrstuden	 */
1001.1Swrstuden	if (read(fd, &efh, sizeof(efh)) != sizeof(efh))
1011.1Swrstuden		err(1, "%s reading header", argv[0]);
1021.1Swrstuden
1031.1Swrstuden	i = ntohs(*(u_int16_t *)efh.f_magic);
1041.1Swrstuden	if ((i != U802WRMAGIC) && (i != U802ROMAGIC) && (i != U802TOCMAGIC))
1051.1Swrstuden		errx(1, "%s: not a valid xcoff file", argv[0]);
1061.1Swrstuden
1071.1Swrstuden	/* Does the AOUT "Optional header" make sence? */
1081.1Swrstuden	i = ntohs(*(u_int16_t *)efh.f_opthdr);
1091.1Swrstuden
1101.1Swrstuden	if (i == SMALL_AOUTSZ)
1111.1Swrstuden		errx(1, "%s: file has small \"optional\" header, inappropriate for use with %s", argv[0], __progname);
1121.1Swrstuden	else if (i != AOUTSZ)
1131.1Swrstuden		errx(1, "%s: invalid \"optional\" header", argv[0]);
1141.1Swrstuden
1151.1Swrstuden	if (read(fd, &aoh, i) != i)
1161.1Swrstuden		err(1, "%s reading \"optional\" header", argv[0]);
1171.1Swrstuden
1181.1Swrstuden	/* Now start filing in the AOUT header */
1191.1Swrstuden	*(u_int16_t *)aoh.magic = htons(RS6K_AOUTHDR_ZMAGIC);
1201.1Swrstuden	n = ntohs(*(u_int16_t *)efh.f_nscns);
1211.1Swrstuden
1221.1Swrstuden	for (i = 0; i < n; i++) {
1231.1Swrstuden		if (read(fd, &shead, sizeof(shead)) != sizeof(shead))
1241.1Swrstuden			err(1, "%s reading section headers", argv[0]);
1251.1Swrstuden		if (strcmp(shead.s_name, ".text") == 0) {
1261.1Swrstuden			*(u_int16_t *)(aoh.o_snentry) = htons(i+1);
1271.1Swrstuden			*(u_int16_t *)(aoh.o_sntext) = htons(i+1);
1281.1Swrstuden		} else if (strcmp(shead.s_name, ".data") == 0) {
1291.1Swrstuden			*(u_int16_t *)(aoh.o_sndata) = htons(i+1);
1301.1Swrstuden		} else if (strcmp(shead.s_name, ".bss") == 0) {
1311.1Swrstuden			*(u_int16_t *)(aoh.o_snbss) = htons(i+1);
1321.1Swrstuden		}
1331.1Swrstuden	}
1341.1Swrstuden
1351.1Swrstuden	/* now write it out */
1361.1Swrstuden	if (pwrite(fd, &aoh, sizeof(aoh), sizeof(struct external_filehdr))
1371.1Swrstuden			!= sizeof(aoh))
1381.1Swrstuden		err(1, "%s writing modified header", argv[0]);
1391.1Swrstuden	close(fd);
1401.1Swrstuden	exit(0);
1411.1Swrstuden}
142