Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * hack-coff.c - hack the header of an xcoff file to fill in
0004  * a few fields needed by the Open Firmware xcoff loader on
0005  * Power Macs but not initialized by objcopy.
0006  *
0007  * Copyright (C) Paul Mackerras 1997.
0008  */
0009 #include <stdio.h>
0010 #include <stdlib.h>
0011 #include <unistd.h>
0012 #include <fcntl.h>
0013 #include <string.h>
0014 #include "rs6000.h"
0015 
0016 #define AOUT_MAGIC  0x010b
0017 
0018 #define get_16be(x) ((((unsigned char *)(x))[0] << 8) \
0019              + ((unsigned char *)(x))[1])
0020 #define put_16be(x, v)  (((unsigned char *)(x))[0] = (v) >> 8, \
0021              ((unsigned char *)(x))[1] = (v) & 0xff)
0022 #define get_32be(x) ((((unsigned char *)(x))[0] << 24) \
0023              + (((unsigned char *)(x))[1] << 16) \
0024              + (((unsigned char *)(x))[2] << 8) \
0025              + ((unsigned char *)(x))[3])
0026 
0027 int
0028 main(int ac, char **av)
0029 {
0030     int fd;
0031     int i, nsect;
0032     int aoutsz;
0033     struct external_filehdr fhdr;
0034     AOUTHDR aout;
0035     struct external_scnhdr shdr;
0036 
0037     if (ac != 2) {
0038     fprintf(stderr, "Usage: hack-coff coff-file\n");
0039     exit(1);
0040     }
0041     if ((fd = open(av[1], 2)) == -1) {
0042     perror(av[2]);
0043     exit(1);
0044     }
0045     if (read(fd, &fhdr, sizeof(fhdr)) != sizeof(fhdr))
0046     goto readerr;
0047     i = get_16be(fhdr.f_magic);
0048     if (i != U802TOCMAGIC && i != U802WRMAGIC && i != U802ROMAGIC) {
0049     fprintf(stderr, "%s: not an xcoff file\n", av[1]);
0050     exit(1);
0051     }
0052     aoutsz = get_16be(fhdr.f_opthdr);
0053     if (read(fd, &aout, aoutsz) != aoutsz)
0054     goto readerr;
0055     nsect = get_16be(fhdr.f_nscns);
0056     for (i = 0; i < nsect; ++i) {
0057     if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
0058         goto readerr;
0059     if (strcmp(shdr.s_name, ".text") == 0) {
0060         put_16be(aout.o_snentry, i+1);
0061         put_16be(aout.o_sntext, i+1);
0062     } else if (strcmp(shdr.s_name, ".data") == 0) {
0063         put_16be(aout.o_sndata, i+1);
0064     } else if (strcmp(shdr.s_name, ".bss") == 0) {
0065         put_16be(aout.o_snbss, i+1);
0066     }
0067     }
0068     put_16be(aout.magic, AOUT_MAGIC);
0069     if (lseek(fd, (long) sizeof(struct external_filehdr), 0) == -1
0070     || write(fd, &aout, aoutsz) != aoutsz) {
0071     fprintf(stderr, "%s: write error\n", av[1]);
0072     exit(1);
0073     }
0074     close(fd);
0075     exit(0);
0076 
0077 readerr:
0078     fprintf(stderr, "%s: read error or file too short\n", av[1]);
0079     exit(1);
0080 }