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