1 /* $NetBSD: coffhdrfix.c,v 1.1 2005/12/29 15:20:09 tsutsui Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 /* fixup GNU binutils file offset error. */
35 #include <sys/types.h>
36 #include <sys/fcntl.h>
39 typedef uint8_t Coff_Byte
;
40 typedef uint8_t Coff_Half
[2];
41 typedef uint8_t Coff_Word
[4];
43 #define ECOFF_OMAGIC 0407
44 #define ECOFF_MAGIC_MIPSEB 0x0160
67 #define COFF_GET_HALF(w) ((w)[1] | ((w)[0] << 8))
68 #define COFF_SET_HALF(w,v) ((w)[1] = (uint8_t)(v), \
69 (w)[0] = (uint8_t)((v) >> 8))
70 #define COFF_GET_WORD(w) ((w)[3] | ((w)[2] << 8) | \
71 ((w)[1] << 16) | ((w)[0] << 24))
72 #define COFF_SET_WORD(w,v) ((w)[3] = (uint8_t)(v), \
73 (w)[2] = (uint8_t)((v) >> 8), \
74 (w)[1] = (uint8_t)((v) >> 16), \
75 (w)[0] = (uint8_t)((v) >> 24))
77 #define FILHSZ sizeof(struct coff_filehdr)
81 main(int argc
, char *argp
[])
83 int fd
, fdout
, fileoff
, i
;
84 struct coff_filehdr file
;
85 struct coff_aouthdr aout
;
92 if ((fd
= open(argp
[1], O_RDWR
)) < 0) {
96 read(fd
, &file
, sizeof file
);
97 read(fd
, &aout
, sizeof aout
);
99 if (COFF_GET_HALF(file
.f_magic
) != ECOFF_MAGIC_MIPSEB
) {
100 fprintf (stderr
, "not COFF file.\n");
103 if (COFF_GET_HALF(aout
.a_magic
) != ECOFF_OMAGIC
) {
104 fprintf (stderr
, "not OMAGIC.\n");
107 fprintf(stderr
, "File: magic: 0x%04x flags: 0x%04x\n",
108 COFF_GET_HALF(file
.f_magic
), COFF_GET_HALF(file
.f_flags
));
109 fprintf(stderr
, "Aout: magic: 0x%04x vstamp: %d\n",
110 COFF_GET_HALF(aout
.a_magic
), COFF_GET_HALF(aout
.a_vstamp
));
113 COFF_GET_HALF(file
.f_opthdr
) +
114 COFF_GET_HALF(file
.f_nscns
) * SCNHSZ
+ 7) & ~7;
115 fprintf(stderr
, "File offset: 0x%x\n", fileoff
);
118 if ((fdout
= open(argp
[2], O_CREAT
| O_TRUNC
| O_RDWR
, 0644)) < 0) {
122 fd
= open(argp
[1], O_RDWR
);
123 read(fd
, buf
, fileoff
);
124 write(fdout
, buf
, fileoff
);
125 lseek(fd
, 8, SEEK_CUR
);
126 while ((i
= read(fd
, buf
, 1024)) > 0)
127 write(fdout
, buf
, i
);