[BACK]Return to fixcoff.c CVS log [TXT][DIR] Up to [local] / sys / arch / macppc / stand / boot.mac

Annotation of sys/arch/macppc/stand/boot.mac/fixcoff.c, Revision 1.1

1.1     ! nbrk        1: /*     $OpenBSD: */
        !             2: /*     $NetBSD: fixcoff.c,v 1.10 2006/04/07 02:34:55 gdamore Exp $ */
        !             3:
        !             4: /*
        !             5:  * Copyright (c) 1999 National Aeronautics & Space Administration
        !             6:  * All rights reserved.
        !             7:  *
        !             8:  * This software was written by William Studenmund of the
        !             9:  * Numerical Aerospace Similation Facility, NASA Ames Research Center.
        !            10:  *
        !            11:  * Redistribution and use in source and binary forms, with or without
        !            12:  * modification, are permitted provided that the following conditions
        !            13:  * are met:
        !            14:  * 1. Redistributions of source code must retain the above copyright
        !            15:  *    notice, this list of conditions and the following disclaimer.
        !            16:  * 2. Redistributions in binary form must reproduce the above copyright
        !            17:  *    notice, this list of conditions and the following disclaimer in the
        !            18:  *    documentation and/or other materials provided with the distribution.
        !            19:  * 3. Neither the name of the National Aeronautics & Space Administration
        !            20:  *    nor the names of its contributors may be used to endorse or promote
        !            21:  *    products derived from this software without specific prior written
        !            22:  *    permission.
        !            23:  *
        !            24:  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
        !            25:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
        !            26:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
        !            27:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
        !            28:  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
        !            29:  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
        !            30:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
        !            31:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
        !            32:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
        !            33:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        !            34:  * POSSIBILITY OF SUCH DAMAGE.
        !            35:  */
        !            36:
        !            37: /*
        !            38:  * This program fixes up the extended xcoff headers generated when an elf
        !            39:  * file is turned into an xcoff one with the current objcopy. It should
        !            40:  * go away someday, when objcopy will correctly fix up the output xcoff
        !            41:  *
        !            42:  * Partially inspired by hack-coff, written by Paul Mackerras.
        !            43:  */
        !            44:
        !            45: #include <stdio.h>
        !            46: #include <stdlib.h>
        !            47: #include <unistd.h>
        !            48: #include <fcntl.h>
        !            49:
        !            50: #include <sys/endian.h>
        !            51:
        !            52: struct filehdr {
        !            53: #define U802WRMAGIC     0730
        !            54: #define U802ROMAGIC     0735
        !            55: #define U802TOCMAGIC    0737
        !            56:        char f_magic[2];
        !            57:        char f_nsect[2];
        !            58:        char f_time[4];
        !            59:        char f_symtab[4];
        !            60:        char f_nsyms[4];
        !            61:        char f_opthdr[2];
        !            62:        char f_flags[2];
        !            63: };
        !            64:
        !            65: struct sectionhdr {
        !            66:        char    s_name[8];
        !            67:        char    s_paddr[4];
        !            68:        char    s_vaddr[4];
        !            69:        char    s_size[4];
        !            70:        char    s_section[4];
        !            71:        char    s_reloc[4];
        !            72:        char    s_lineno[4];
        !            73:        char    s_nreloc[2];
        !            74:        char    s_nlineno[2];
        !            75:        char    s_flags[4];
        !            76: };
        !            77:
        !            78: struct aouthdr {
        !            79:        char    magic[2];
        !            80:        char    vstamp[2];
        !            81:        char    tsize[4];
        !            82:        char    dsize[4];
        !            83:        char    bsize[4];
        !            84:        char    entry[4];
        !            85:        char    text_start[4];
        !            86:        char    data_start[4];
        !            87: #define SMALL_AOUTSZ   28
        !            88:        char    o_toc[4];
        !            89:        char    o_snentry[2];
        !            90:        char    o_sntext[2];
        !            91:        char    o_sndata[2];
        !            92:        char    o_sntoc[2];
        !            93:        char    o_snloader[2];
        !            94:        char    o_snbss[2];
        !            95:        char    o_algntext[2];
        !            96:        char    o_algndata[2];
        !            97:        char    o_modtype[2];
        !            98:        char    o_cputype[2];
        !            99:        char    o_maxstack[4];
        !           100:        char    o_maxdata[4];
        !           101:        char    o_resv2[12];
        !           102: };
        !           103: #define RS6K_AOUTHDR_ZMAGIC     0x010B
        !           104:
        !           105: char *progname;
        !           106:
        !           107: void
        !           108: usage(char *prog)
        !           109: {
        !           110:        fprintf(stderr, "Usage: %s [-h] | [<file to fix>]\n", prog);
        !           111: }
        !           112:
        !           113: void
        !           114: help(char *prog)
        !           115: {
        !           116:        fprintf(stderr, "%s\tis designed to fix the xcoff headers in a\n",prog);
        !           117:        fprintf(stderr,
        !           118: "\tbinary generated using objcopy from a non-xcoff source.\n");
        !           119:        usage(prog);
        !           120:        exit(0);
        !           121: }
        !           122:
        !           123: main(int argc, char *argv[])
        !           124: {
        !           125:        int     fd, i, n, ch;
        !           126:        struct  filehdr fh;
        !           127:        struct  aouthdr aoh;
        !           128:        struct  sectionhdr sh;
        !           129:
        !           130:        progname = argv[0];
        !           131:        while ((ch = getopt(argc, argv, "h")) != -1)
        !           132:            switch (ch) {
        !           133:                case 'h':
        !           134:                help(progname);
        !           135:        }
        !           136:
        !           137:        argc -= optind;
        !           138:        argv += optind;
        !           139:
        !           140:        if (argc != 1) {
        !           141:                usage(progname);
        !           142:                exit(1);
        !           143:        }
        !           144:
        !           145:        if ((fd = open(argv[0], O_RDWR, 0)) == -1)
        !           146:                err(i, "%s", argv[0]);
        !           147:
        !           148:        /*
        !           149:         * Make sure it looks like an xcoff file..
        !           150:         */
        !           151:        if (read(fd, &fh, sizeof(fh)) != sizeof(fh))
        !           152:                err(1, "%s reading header", argv[0]);
        !           153:
        !           154:        i = betoh16(*(uint16_t *)fh.f_magic);
        !           155:        if ((i != U802WRMAGIC) && (i != U802ROMAGIC) && (i != U802TOCMAGIC))
        !           156:                errx(1, "%s: not a valid xcoff file", argv[0]);
        !           157:
        !           158:        /* Does the AOUT "Optional header" make sense? */
        !           159:        i = betoh16(*(uint16_t *)fh.f_opthdr);
        !           160:
        !           161:        if (i == SMALL_AOUTSZ)
        !           162:                errx(1, "%s: file has small \"optional\" header, inappropriate for use with %s", argv[0], progname);
        !           163:        else if (i != sizeof(aoh))
        !           164:                errx(1, "%s: invalid \"optional\" header", argv[0]);
        !           165:
        !           166:        if (read(fd, &aoh, i) != i)
        !           167:                err(1, "%s reading \"optional\" header", argv[0]);
        !           168:
        !           169:        /* Now start filing in the AOUT header */
        !           170:        *(uint16_t *)aoh.magic = htobe16(RS6K_AOUTHDR_ZMAGIC);
        !           171:        n = betoh16(*(uint16_t *)fh.f_nsect);
        !           172:
        !           173:        for (i = 0; i < n; i++) {
        !           174:                if (read(fd, &sh, sizeof(sh)) != sizeof(sh))
        !           175:                        err(1, "%s reading section headers", argv[0]);
        !           176:                if (strcmp(sh.s_name, ".text") == 0) {
        !           177:                        *(uint16_t *)(aoh.o_snentry) = htobe16(i+1);
        !           178:                        *(uint16_t *)(aoh.o_sntext) = htobe16(i+1);
        !           179:                } else if (strcmp(sh.s_name, ".data") == 0) {
        !           180:                        *(uint16_t *)(aoh.o_sndata) = htobe16(i+1);
        !           181:                } else if (strcmp(sh.s_name, ".bss") == 0) {
        !           182:                        *(uint16_t *)(aoh.o_snbss) = htobe16(i+1);
        !           183:                }
        !           184:        }
        !           185:
        !           186:        /* now write it out */
        !           187:        if (pwrite(fd, &aoh, sizeof(aoh), sizeof(struct filehdr)) !=
        !           188:            sizeof(aoh))
        !           189:                err(1, "%s writing modified header", argv[0]);
        !           190:        close(fd);
        !           191:        exit(0);
        !           192: }

CVSweb