1 /* $NetBSD: fixcoff.c,v 1.10 2006/04/07 02:34:55 gdamore Exp $ */
4 * Copyright (c) 1999 National Aeronautics & Space Administration
7 * This software was written by William Studenmund of the
8 * Numerical Aerospace Similation Facility, NASA Ames Research Center.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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.
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
41 * Partially inspired by hack-coff, written by Paul Mackerras.
44 #if HAVE_NBTOOL_CONFIG_H
45 #include "nbtool_config.h"
53 #if HAVE_NBTOOL_CONFIG_H
56 #define htobe16(x) (x)
59 __htobe16(unsigned short x
)
61 return (((x
& 0xff00) >> 8) | ((x
& 0x00ff) << 8));
63 #define htobe16(x) __htobe16(x)
64 #endif /* WORDS_BIGENDIAN */
66 #else /* HAVE_NBTOOL_CONFIG_H */
67 #include <sys/endian.h>
68 #endif /* HAVE_NBTOOL_CONFIG_H */
71 #define U802WRMAGIC 0730
72 #define U802ROMAGIC 0735
73 #define U802TOCMAGIC 0737
105 #define SMALL_AOUTSZ 28
121 #define RS6K_AOUTHDR_ZMAGIC 0x010B
126 fprintf(stderr
, "Usage: %s [-h] | [<file to fix>]\n", prog
);
132 fprintf(stderr
, "%s\tis designed to fix the xcoff headers in a\n",prog
);
134 "\tbinary generated using objcopy from a non-xcoff source.\n");
139 main(int argc
, char * const *argv
)
144 struct sectionhdr sh
;
146 while ((ch
= getopt(argc
, argv
, "h")) != -1)
156 usage(getprogname());
160 if ((fd
= open(argv
[0], O_RDWR
, 0)) == -1)
161 err(i
, "%s", argv
[0]);
164 * Make sure it looks like an xcoff file..
166 if (read(fd
, &fh
, sizeof(fh
)) != sizeof(fh
))
167 err(1, "%s reading header", argv
[0]);
169 i
= be16toh(*(uint16_t *)fh
.f_magic
);
170 if ((i
!= U802WRMAGIC
) && (i
!= U802ROMAGIC
) && (i
!= U802TOCMAGIC
))
171 errx(1, "%s: not a valid xcoff file", argv
[0]);
173 /* Does the AOUT "Optional header" make sense? */
174 i
= be16toh(*(uint16_t *)fh
.f_opthdr
);
176 if (i
== SMALL_AOUTSZ
)
177 errx(1, "%s: file has small \"optional\" header, inappropriate for use with %s", argv
[0], getprogname());
178 else if (i
!= sizeof(aoh
))
179 errx(1, "%s: invalid \"optional\" header", argv
[0]);
181 if (read(fd
, &aoh
, i
) != i
)
182 err(1, "%s reading \"optional\" header", argv
[0]);
184 /* Now start filing in the AOUT header */
185 *(uint16_t *)aoh
.magic
= htobe16(RS6K_AOUTHDR_ZMAGIC
);
186 n
= be16toh(*(uint16_t *)fh
.f_nsect
);
188 for (i
= 0; i
< n
; i
++) {
189 if (read(fd
, &sh
, sizeof(sh
)) != sizeof(sh
))
190 err(1, "%s reading section headers", argv
[0]);
191 if (strcmp(sh
.s_name
, ".text") == 0) {
192 *(uint16_t *)(aoh
.o_snentry
) = htobe16(i
+1);
193 *(uint16_t *)(aoh
.o_sntext
) = htobe16(i
+1);
194 } else if (strcmp(sh
.s_name
, ".data") == 0) {
195 *(uint16_t *)(aoh
.o_sndata
) = htobe16(i
+1);
196 } else if (strcmp(sh
.s_name
, ".bss") == 0) {
197 *(uint16_t *)(aoh
.o_snbss
) = htobe16(i
+1);
201 /* now write it out */
202 if (pwrite(fd
, &aoh
, sizeof(aoh
), sizeof(struct filehdr
))
204 err(1, "%s writing modified header", argv
[0]);