1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * hack-coff.c - hack the header of an xcoff file to fill in
4 * a few fields needed by the Open Firmware xcoff loader on
5 * Power Macs but not initialized by objcopy.
7 * Copyright (C) Paul Mackerras 1997.
16 #define AOUT_MAGIC 0x010b
18 #define get_16be(x) ((((unsigned char *)(x))[0] << 8) \
19 + ((unsigned char *)(x))[1])
20 #define put_16be(x, v) (((unsigned char *)(x))[0] = (v) >> 8, \
21 ((unsigned char *)(x))[1] = (v) & 0xff)
22 #define get_32be(x) ((((unsigned char *)(x))[0] << 24) \
23 + (((unsigned char *)(x))[1] << 16) \
24 + (((unsigned char *)(x))[2] << 8) \
25 + ((unsigned char *)(x))[3])
28 main(int ac
, char **av
)
33 struct external_filehdr fhdr
;
35 struct external_scnhdr shdr
;
38 fprintf(stderr
, "Usage: hack-coff coff-file\n");
41 if ((fd
= open(av
[1], 2)) == -1) {
45 if (read(fd
, &fhdr
, sizeof(fhdr
)) != sizeof(fhdr
))
47 i
= get_16be(fhdr
.f_magic
);
48 if (i
!= U802TOCMAGIC
&& i
!= U802WRMAGIC
&& i
!= U802ROMAGIC
) {
49 fprintf(stderr
, "%s: not an xcoff file\n", av
[1]);
52 aoutsz
= get_16be(fhdr
.f_opthdr
);
53 if (read(fd
, &aout
, aoutsz
) != aoutsz
)
55 nsect
= get_16be(fhdr
.f_nscns
);
56 for (i
= 0; i
< nsect
; ++i
) {
57 if (read(fd
, &shdr
, sizeof(shdr
)) != sizeof(shdr
))
59 if (strcmp(shdr
.s_name
, ".text") == 0) {
60 put_16be(aout
.o_snentry
, i
+1);
61 put_16be(aout
.o_sntext
, i
+1);
62 } else if (strcmp(shdr
.s_name
, ".data") == 0) {
63 put_16be(aout
.o_sndata
, i
+1);
64 } else if (strcmp(shdr
.s_name
, ".bss") == 0) {
65 put_16be(aout
.o_snbss
, i
+1);
68 put_16be(aout
.magic
, AOUT_MAGIC
);
69 if (lseek(fd
, (long) sizeof(struct external_filehdr
), 0) == -1
70 || write(fd
, &aout
, aoutsz
) != aoutsz
) {
71 fprintf(stderr
, "%s: write error\n", av
[1]);
78 fprintf(stderr
, "%s: read error or file too short\n", av
[1]);