addnote.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Program to hack in a PT_NOTE program header entry in an ELF file.
  3. * This is needed for OF on RS/6000s to load an image correctly.
  4. * Note that OF needs a program header entry for the note, not an
  5. * ELF section.
  6. *
  7. * Copyright 2000 Paul Mackerras.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Usage: addnote zImage
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <fcntl.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. char arch[] = "PowerPC";
  22. #define N_DESCR 6
  23. unsigned int descr[N_DESCR] = {
  24. 0xffffffff, /* real-mode = true */
  25. 0x00c00000, /* real-base, i.e. where we expect OF to be */
  26. 0xffffffff, /* real-size */
  27. 0xffffffff, /* virt-base */
  28. 0xffffffff, /* virt-size */
  29. 0x4000, /* load-base */
  30. };
  31. unsigned char buf[512];
  32. #define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
  33. #define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2))
  34. #define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \
  35. buf[(off) + 1] = (v) & 0xff)
  36. #define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \
  37. PUT_16BE((off) + 2, (v)))
  38. /* Structure of an ELF file */
  39. #define E_IDENT 0 /* ELF header */
  40. #define E_PHOFF 28
  41. #define E_PHENTSIZE 42
  42. #define E_PHNUM 44
  43. #define E_HSIZE 52 /* size of ELF header */
  44. #define EI_MAGIC 0 /* offsets in E_IDENT area */
  45. #define EI_CLASS 4
  46. #define EI_DATA 5
  47. #define PH_TYPE 0 /* ELF program header */
  48. #define PH_OFFSET 4
  49. #define PH_FILESZ 16
  50. #define PH_HSIZE 32 /* size of program header */
  51. #define PT_NOTE 4 /* Program header type = note */
  52. #define ELFCLASS32 1
  53. #define ELFDATA2MSB 2
  54. unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
  55. int
  56. main(int ac, char **av)
  57. {
  58. int fd, n, i;
  59. int ph, ps, np;
  60. int nnote, ns;
  61. if (ac != 2) {
  62. fprintf(stderr, "Usage: %s elf-file\n", av[0]);
  63. exit(1);
  64. }
  65. fd = open(av[1], O_RDWR);
  66. if (fd < 0) {
  67. perror(av[1]);
  68. exit(1);
  69. }
  70. nnote = strlen(arch) + 1 + (N_DESCR + 3) * 4;
  71. n = read(fd, buf, sizeof(buf));
  72. if (n < 0) {
  73. perror("read");
  74. exit(1);
  75. }
  76. if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
  77. goto notelf;
  78. if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
  79. || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
  80. fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
  81. av[1]);
  82. exit(1);
  83. }
  84. ph = GET_32BE(E_PHOFF);
  85. ps = GET_16BE(E_PHENTSIZE);
  86. np = GET_16BE(E_PHNUM);
  87. if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
  88. goto notelf;
  89. if (ph + (np + 1) * ps + nnote > n)
  90. goto nospace;
  91. for (i = 0; i < np; ++i) {
  92. if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
  93. fprintf(stderr, "%s already has a note entry\n",
  94. av[1]);
  95. exit(0);
  96. }
  97. ph += ps;
  98. }
  99. /* XXX check that the area we want to use is all zeroes */
  100. for (i = 0; i < ps + nnote; ++i)
  101. if (buf[ph + i] != 0)
  102. goto nospace;
  103. /* fill in the program header entry */
  104. ns = ph + ps;
  105. PUT_32BE(ph + PH_TYPE, PT_NOTE);
  106. PUT_32BE(ph + PH_OFFSET, ns);
  107. PUT_32BE(ph + PH_FILESZ, nnote);
  108. /* fill in the note area we point to */
  109. /* XXX we should probably make this a proper section */
  110. PUT_32BE(ns, strlen(arch) + 1);
  111. PUT_32BE(ns + 4, N_DESCR * 4);
  112. PUT_32BE(ns + 8, 0x1275);
  113. strcpy(&buf[ns + 12], arch);
  114. ns += 12 + strlen(arch) + 1;
  115. for (i = 0; i < N_DESCR; ++i)
  116. PUT_32BE(ns + i * 4, descr[i]);
  117. /* Update the number of program headers */
  118. PUT_16BE(E_PHNUM, np + 1);
  119. /* write back */
  120. lseek(fd, (long) 0, SEEK_SET);
  121. i = write(fd, buf, n);
  122. if (i < 0) {
  123. perror("write");
  124. exit(1);
  125. }
  126. if (i < n) {
  127. fprintf(stderr, "%s: write truncated\n", av[1]);
  128. exit(1);
  129. }
  130. exit(0);
  131. notelf:
  132. fprintf(stderr, "%s does not appear to be an ELF file\n", av[0]);
  133. exit(1);
  134. nospace:
  135. fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
  136. av[0]);
  137. exit(1);
  138. }