make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / arch / ppc / boot / utils / hack-coff.c
1 /*
2  * BK Id: SCCS/s.hack-coff.c 1.9 06/05/01 22:20:10 paulus
3  */
4 /*
5  * hack-coff.c - hack the header of an xcoff file to fill in
6  * a few fields needed by the Open Firmware xcoff loader on
7  * Power Macs but not initialized by objcopy.
8  *
9  * Copyright (C) Paul Mackerras 1997.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version
14  * 2 of the License, or (at your option) any later version.
15  */
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include "rs6000.h"
20
21 #define AOUT_MAGIC      0x010b
22
23 #define get_16be(x)     ((((unsigned char *)(x))[0] << 8) \
24                          + ((unsigned char *)(x))[1])
25 #define put_16be(x, v)  (((unsigned char *)(x))[0] = (v) >> 8, \
26                          ((unsigned char *)(x))[1] = (v) & 0xff)
27 #define get_32be(x)     ((((unsigned char *)(x))[0] << 24) \
28                          + (((unsigned char *)(x))[1] << 16) \
29                          + (((unsigned char *)(x))[2] << 8) \
30                          + ((unsigned char *)(x))[3])
31
32 int
33 main(int ac, char **av)
34 {
35     int fd;
36     int i, nsect;
37     int aoutsz;
38     struct external_filehdr fhdr;
39     AOUTHDR aout;
40     struct external_scnhdr shdr;
41
42     if (ac != 2) {
43         fprintf(stderr, "Usage: hack-coff coff-file\n");
44         exit(1);
45     }
46     if ((fd = open(av[1], 2)) == -1) {
47         perror(av[2]);
48         exit(1);
49     }
50     if (read(fd, &fhdr, sizeof(fhdr)) != sizeof(fhdr))
51         goto readerr;
52     i = get_16be(fhdr.f_magic);
53     if (i != U802TOCMAGIC && i != U802WRMAGIC && i != U802ROMAGIC) {
54         fprintf(stderr, "%s: not an xcoff file\n", av[1]);
55         exit(1);
56     }
57     aoutsz = get_16be(fhdr.f_opthdr);
58     if (read(fd, &aout, aoutsz) != aoutsz)
59         goto readerr;
60     nsect = get_16be(fhdr.f_nscns);
61     for (i = 0; i < nsect; ++i) {
62         if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
63             goto readerr;
64         if (strcmp(shdr.s_name, ".text") == 0) {
65             put_16be(aout.o_snentry, i+1);
66             put_16be(aout.o_sntext, i+1);
67         } else if (strcmp(shdr.s_name, ".data") == 0) {
68             put_16be(aout.o_sndata, i+1);
69         } else if (strcmp(shdr.s_name, ".bss") == 0) {
70             put_16be(aout.o_snbss, i+1);
71         }
72     }
73     put_16be(aout.magic, AOUT_MAGIC);
74     if (lseek(fd, (long) sizeof(struct external_filehdr), 0) == -1
75         || write(fd, &aout, aoutsz) != aoutsz) {
76         fprintf(stderr, "%s: write error\n", av[1]);
77         exit(1);
78     }
79     close(fd);
80     exit(0);
81
82 readerr:
83     fprintf(stderr, "%s: read error or file too short\n", av[1]);
84     exit(1);
85 }