hack-coff.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  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.
  6. *
  7. * Copyright (C) Paul Mackerras 1997.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <fcntl.h>
  13. #include <string.h>
  14. #include "rs6000.h"
  15. #define AOUT_MAGIC 0x010b
  16. #define get_16be(x) ((((unsigned char *)(x))[0] << 8) \
  17. + ((unsigned char *)(x))[1])
  18. #define put_16be(x, v) (((unsigned char *)(x))[0] = (v) >> 8, \
  19. ((unsigned char *)(x))[1] = (v) & 0xff)
  20. #define get_32be(x) ((((unsigned char *)(x))[0] << 24) \
  21. + (((unsigned char *)(x))[1] << 16) \
  22. + (((unsigned char *)(x))[2] << 8) \
  23. + ((unsigned char *)(x))[3])
  24. int
  25. main(int ac, char **av)
  26. {
  27. int fd;
  28. int i, nsect;
  29. int aoutsz;
  30. struct external_filehdr fhdr;
  31. AOUTHDR aout;
  32. struct external_scnhdr shdr;
  33. if (ac != 2) {
  34. fprintf(stderr, "Usage: hack-coff coff-file\n");
  35. exit(1);
  36. }
  37. if ((fd = open(av[1], 2)) == -1) {
  38. perror(av[2]);
  39. exit(1);
  40. }
  41. if (read(fd, &fhdr, sizeof(fhdr)) != sizeof(fhdr))
  42. goto readerr;
  43. i = get_16be(fhdr.f_magic);
  44. if (i != U802TOCMAGIC && i != U802WRMAGIC && i != U802ROMAGIC) {
  45. fprintf(stderr, "%s: not an xcoff file\n", av[1]);
  46. exit(1);
  47. }
  48. aoutsz = get_16be(fhdr.f_opthdr);
  49. if (read(fd, &aout, aoutsz) != aoutsz)
  50. goto readerr;
  51. nsect = get_16be(fhdr.f_nscns);
  52. for (i = 0; i < nsect; ++i) {
  53. if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
  54. goto readerr;
  55. if (strcmp(shdr.s_name, ".text") == 0) {
  56. put_16be(aout.o_snentry, i+1);
  57. put_16be(aout.o_sntext, i+1);
  58. } else if (strcmp(shdr.s_name, ".data") == 0) {
  59. put_16be(aout.o_sndata, i+1);
  60. } else if (strcmp(shdr.s_name, ".bss") == 0) {
  61. put_16be(aout.o_snbss, i+1);
  62. }
  63. }
  64. put_16be(aout.magic, AOUT_MAGIC);
  65. if (lseek(fd, (long) sizeof(struct external_filehdr), 0) == -1
  66. || write(fd, &aout, aoutsz) != aoutsz) {
  67. fprintf(stderr, "%s: write error\n", av[1]);
  68. exit(1);
  69. }
  70. close(fd);
  71. exit(0);
  72. readerr:
  73. fprintf(stderr, "%s: read error or file too short\n", av[1]);
  74. exit(1);
  75. }