coffhdrfix.c revision 1.2 1 1.2 martin /* $NetBSD: coffhdrfix.c,v 1.2 2008/04/28 20:23:18 martin Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by UCHIYAMA Yasushi.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui *
19 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tsutsui */
31 1.1 tsutsui
32 1.1 tsutsui /* fixup GNU binutils file offset error. */
33 1.1 tsutsui
34 1.1 tsutsui #include <stdio.h>
35 1.1 tsutsui #include <sys/types.h>
36 1.1 tsutsui #include <sys/fcntl.h>
37 1.1 tsutsui #include <sys/mman.h>
38 1.1 tsutsui
39 1.1 tsutsui typedef uint8_t Coff_Byte;
40 1.1 tsutsui typedef uint8_t Coff_Half[2];
41 1.1 tsutsui typedef uint8_t Coff_Word[4];
42 1.1 tsutsui
43 1.1 tsutsui #define ECOFF_OMAGIC 0407
44 1.1 tsutsui #define ECOFF_MAGIC_MIPSEB 0x0160
45 1.1 tsutsui
46 1.1 tsutsui struct coff_filehdr {
47 1.1 tsutsui Coff_Half f_magic;
48 1.1 tsutsui Coff_Half f_nscns;
49 1.1 tsutsui Coff_Word f_timdat;
50 1.1 tsutsui Coff_Word f_symptr;
51 1.1 tsutsui Coff_Word f_nsyms;
52 1.1 tsutsui Coff_Half f_opthdr;
53 1.1 tsutsui Coff_Half f_flags;
54 1.1 tsutsui };
55 1.1 tsutsui
56 1.1 tsutsui struct coff_aouthdr {
57 1.1 tsutsui Coff_Half a_magic;
58 1.1 tsutsui Coff_Half a_vstamp;
59 1.1 tsutsui Coff_Word a_tsize;
60 1.1 tsutsui Coff_Word a_dsize;
61 1.1 tsutsui Coff_Word a_bsize;
62 1.1 tsutsui Coff_Word a_entry;
63 1.1 tsutsui Coff_Word a_tstart;
64 1.1 tsutsui Coff_Word a_dstart;
65 1.1 tsutsui };
66 1.1 tsutsui
67 1.1 tsutsui #define COFF_GET_HALF(w) ((w)[1] | ((w)[0] << 8))
68 1.1 tsutsui #define COFF_SET_HALF(w,v) ((w)[1] = (uint8_t)(v), \
69 1.1 tsutsui (w)[0] = (uint8_t)((v) >> 8))
70 1.1 tsutsui #define COFF_GET_WORD(w) ((w)[3] | ((w)[2] << 8) | \
71 1.1 tsutsui ((w)[1] << 16) | ((w)[0] << 24))
72 1.1 tsutsui #define COFF_SET_WORD(w,v) ((w)[3] = (uint8_t)(v), \
73 1.1 tsutsui (w)[2] = (uint8_t)((v) >> 8), \
74 1.1 tsutsui (w)[1] = (uint8_t)((v) >> 16), \
75 1.1 tsutsui (w)[0] = (uint8_t)((v) >> 24))
76 1.1 tsutsui
77 1.1 tsutsui #define FILHSZ sizeof(struct coff_filehdr)
78 1.1 tsutsui #define SCNHSZ 40
79 1.1 tsutsui
80 1.1 tsutsui int
81 1.1 tsutsui main(int argc, char *argp[])
82 1.1 tsutsui {
83 1.1 tsutsui int fd, fdout, fileoff, i;
84 1.1 tsutsui struct coff_filehdr file;
85 1.1 tsutsui struct coff_aouthdr aout;
86 1.1 tsutsui char fname[256];
87 1.1 tsutsui char buf[1024];
88 1.1 tsutsui
89 1.1 tsutsui if (argc < 3)
90 1.1 tsutsui return 0;
91 1.1 tsutsui
92 1.1 tsutsui if ((fd = open(argp[1], O_RDWR)) < 0) {
93 1.1 tsutsui perror(0);
94 1.1 tsutsui return 0;
95 1.1 tsutsui }
96 1.1 tsutsui read(fd, &file, sizeof file);
97 1.1 tsutsui read(fd, &aout, sizeof aout);
98 1.1 tsutsui
99 1.1 tsutsui if (COFF_GET_HALF(file.f_magic) != ECOFF_MAGIC_MIPSEB) {
100 1.1 tsutsui fprintf (stderr, "not COFF file.\n");
101 1.1 tsutsui return 0;
102 1.1 tsutsui }
103 1.1 tsutsui if (COFF_GET_HALF(aout.a_magic) != ECOFF_OMAGIC) {
104 1.1 tsutsui fprintf (stderr, "not OMAGIC.\n");
105 1.1 tsutsui return 0;
106 1.1 tsutsui }
107 1.1 tsutsui fprintf(stderr, "File: magic: 0x%04x flags: 0x%04x\n",
108 1.1 tsutsui COFF_GET_HALF(file.f_magic), COFF_GET_HALF(file.f_flags));
109 1.1 tsutsui fprintf(stderr, "Aout: magic: 0x%04x vstamp: %d\n",
110 1.1 tsutsui COFF_GET_HALF(aout.a_magic), COFF_GET_HALF(aout.a_vstamp));
111 1.1 tsutsui
112 1.1 tsutsui fileoff = (FILHSZ +
113 1.1 tsutsui COFF_GET_HALF(file.f_opthdr) +
114 1.1 tsutsui COFF_GET_HALF(file.f_nscns) * SCNHSZ + 7) & ~7;
115 1.1 tsutsui fprintf(stderr, "File offset: 0x%x\n", fileoff);
116 1.1 tsutsui close(fd);
117 1.1 tsutsui
118 1.1 tsutsui if ((fdout = open(argp[2], O_CREAT | O_TRUNC | O_RDWR, 0644)) < 0) {
119 1.1 tsutsui perror (0);
120 1.1 tsutsui return 0;
121 1.1 tsutsui }
122 1.1 tsutsui fd = open(argp[1], O_RDWR);
123 1.1 tsutsui read(fd, buf, fileoff);
124 1.1 tsutsui write(fdout, buf, fileoff);
125 1.1 tsutsui lseek(fd, 8, SEEK_CUR);
126 1.1 tsutsui while ((i = read(fd, buf, 1024)) > 0)
127 1.1 tsutsui write(fdout, buf, i);
128 1.1 tsutsui close(fd);
129 1.1 tsutsui close(fdout);
130 1.1 tsutsui
131 1.1 tsutsui return 0;
132 1.1 tsutsui }
133