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