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