xref: /netbsd-src/sys/arch/macppc/stand/fixcoff/fixcoff.c (revision 81e0d2b0af8485d94ed5da487d4253841a2e6e45)
1 /*	$NetBSD: fixcoff.c,v 1.8 2003/12/11 00:27:42 matt 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 #if HAVE_NBTOOL_CONFIG_H
45 #include "nbtool_config.h"
46 #endif
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <sys/endian.h>
53 
54 struct filehdr {
55 #define U802WRMAGIC     0730
56 #define U802ROMAGIC     0735
57 #define U802TOCMAGIC    0737
58 	char f_magic[2];
59 	char f_nsect[2];
60 	char f_time[4];
61 	char f_symtab[4];
62 	char f_nsyms[4];
63 	char f_opthdr[2];
64 	char f_flags[2];
65 };
66 
67 struct sectionhdr {
68 	char	s_name[8];
69 	char	s_paddr[4];
70 	char	s_vaddr[4];
71 	char	s_size[4];
72 	char	s_section[4];
73 	char	s_reloc[4];
74 	char	s_lineno[4];
75 	char	s_nreloc[2];
76 	char	s_nlineno[2];
77 	char	s_flags[4];
78 };
79 
80 struct aouthdr {
81 	char	magic[2];
82 	char	vstamp[2];
83 	char	tsize[4];
84 	char	dsize[4];
85 	char	bsize[4];
86 	char	entry[4];
87 	char	text_start[4];
88 	char	data_start[4];
89 #define SMALL_AOUTSZ	28
90 	char	o_toc[4];
91 	char	o_snentry[2];
92 	char	o_sntext[2];
93 	char	o_sndata[2];
94 	char	o_sntoc[2];
95 	char	o_snloader[2];
96 	char	o_snbss[2];
97 	char	o_algntext[2];
98 	char	o_algndata[2];
99 	char	o_modtype[2];
100 	char	o_cputype[2];
101 	char	o_maxstack[4];
102 	char	o_maxdata[4];
103 	char	o_resv2[12];
104 };
105 #define RS6K_AOUTHDR_ZMAGIC     0x010B
106 
107 void
108 usage(prog)
109 	char	*prog;
110 {
111 	fprintf(stderr, "Usage: %s [-h] | [<file to fix>]\n", prog);
112 }
113 
114 void
115 help(prog)
116 	char	*prog;
117 {
118 	fprintf(stderr, "%s\tis designed to fix the xcoff headers in a\n",prog);
119 	fprintf(stderr,
120 "\tbinary generated using objcopy from a non-xcoff source.\n");
121 	usage(prog);
122 	exit(0);
123 }
124 
125 main(argc, argv)
126 	int		argc;
127 	char * const	*argv;
128 {
129 	int	fd, i, n, ch;
130 	struct	filehdr	fh;
131 	struct	aouthdr aoh;
132 	struct	sectionhdr sh;
133 
134 	while ((ch = getopt(argc, argv, "h")) != -1)
135 	    switch (ch) {
136 		case 'h':
137 		help(getprogname());
138 	}
139 
140 	argc -= optind;
141 	argv += optind;
142 
143 	if (argc != 1) {
144 		usage(getprogname());
145 		exit(1);
146 	}
147 
148 	if ((fd = open(argv[0], O_RDWR, 0)) == -1)
149 		err(i, "%s", argv[0]);
150 
151 	/*
152 	 * Make sure it looks like an xcoff file..
153 	 */
154 	if (read(fd, &fh, sizeof(fh)) != sizeof(fh))
155 		err(1, "%s reading header", argv[0]);
156 
157 	i = be16toh(*(uint16_t *)fh.f_magic);
158 	if ((i != U802WRMAGIC) && (i != U802ROMAGIC) && (i != U802TOCMAGIC))
159 		errx(1, "%s: not a valid xcoff file", argv[0]);
160 
161 	/* Does the AOUT "Optional header" make sense? */
162 	i = be16toh(*(uint16_t *)fh.f_opthdr);
163 
164 	if (i == SMALL_AOUTSZ)
165 		errx(1, "%s: file has small \"optional\" header, inappropriate for use with %s", argv[0], getprogname());
166 	else if (i != sizeof(aoh))
167 		errx(1, "%s: invalid \"optional\" header", argv[0]);
168 
169 	if (read(fd, &aoh, i) != i)
170 		err(1, "%s reading \"optional\" header", argv[0]);
171 
172 	/* Now start filing in the AOUT header */
173 	*(uint16_t *)aoh.magic = htobe16(RS6K_AOUTHDR_ZMAGIC);
174 	n = be16toh(*(uint16_t *)fh.f_nsect);
175 
176 	for (i = 0; i < n; i++) {
177 		if (read(fd, &sh, sizeof(sh)) != sizeof(sh))
178 			err(1, "%s reading section headers", argv[0]);
179 		if (strcmp(sh.s_name, ".text") == 0) {
180 			*(uint16_t *)(aoh.o_snentry) = htobe16(i+1);
181 			*(uint16_t *)(aoh.o_sntext) = htobe16(i+1);
182 		} else if (strcmp(sh.s_name, ".data") == 0) {
183 			*(uint16_t *)(aoh.o_sndata) = htobe16(i+1);
184 		} else if (strcmp(sh.s_name, ".bss") == 0) {
185 			*(uint16_t *)(aoh.o_snbss) = htobe16(i+1);
186 		}
187 	}
188 
189 	/* now write it out */
190 	if (pwrite(fd, &aoh, sizeof(aoh), sizeof(struct filehdr))
191 			!= sizeof(aoh))
192 		err(1, "%s writing modified header", argv[0]);
193 	close(fd);
194 	exit(0);
195 }
196