hack-coff.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #define AOUT_MAGIC 0x010b
  20. #define get_16be(x) ((((unsigned char *)(x))[0] << 8) \
  21. + ((unsigned char *)(x))[1])
  22. #define put_16be(x, v) (((unsigned char *)(x))[0] = (v) >> 8, \
  23. ((unsigned char *)(x))[1] = (v) & 0xff)
  24. #define get_32be(x) ((((unsigned char *)(x))[0] << 24) \
  25. + (((unsigned char *)(x))[1] << 16) \
  26. + (((unsigned char *)(x))[2] << 8) \
  27. + ((unsigned char *)(x))[3])
  28. int
  29. main(int ac, char **av)
  30. {
  31. int fd;
  32. int i, nsect;
  33. int aoutsz;
  34. struct external_filehdr fhdr;
  35. AOUTHDR aout;
  36. struct external_scnhdr shdr;
  37. if (ac != 2) {
  38. fprintf(stderr, "Usage: hack-coff coff-file\n");
  39. exit(1);
  40. }
  41. if ((fd = open(av[1], 2)) == -1) {
  42. perror(av[2]);
  43. exit(1);
  44. }
  45. if (read(fd, &fhdr, sizeof(fhdr)) != sizeof(fhdr))
  46. goto readerr;
  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]);
  50. exit(1);
  51. }
  52. aoutsz = get_16be(fhdr.f_opthdr);
  53. if (read(fd, &aout, aoutsz) != aoutsz)
  54. goto readerr;
  55. nsect = get_16be(fhdr.f_nscns);
  56. for (i = 0; i < nsect; ++i) {
  57. if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr))
  58. goto readerr;
  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);
  66. }
  67. }
  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]);
  72. exit(1);
  73. }
  74. close(fd);
  75. exit(0);
  76. readerr:
  77. fprintf(stderr, "%s: read error or file too short\n", av[1]);
  78. exit(1);
  79. }