addnote.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #if 1
  25. /* values for IBM RS/6000 machines */
  26. 0xffffffff, /* real-mode = true */
  27. 0x00c00000, /* real-base, i.e. where we expect OF to be */
  28. 0xffffffff, /* real-size */
  29. 0xffffffff, /* virt-base */
  30. 0xffffffff, /* virt-size */
  31. 0x4000, /* load-base */
  32. #else
  33. /* values for longtrail CHRP */
  34. 0, /* real-mode = false */
  35. 0xffffffff, /* real-base */
  36. 0xffffffff, /* real-size */
  37. 0xffffffff, /* virt-base */
  38. 0xffffffff, /* virt-size */
  39. 0x00600000, /* load-base */
  40. #endif
  41. };
  42. unsigned char buf[512];
  43. #define GET_16BE(off) ((buf[off] << 8) + (buf[(off)+1]))
  44. #define GET_32BE(off) ((GET_16BE(off) << 16) + GET_16BE((off)+2))
  45. #define PUT_16BE(off, v) (buf[off] = ((v) >> 8) & 0xff, \
  46. buf[(off) + 1] = (v) & 0xff)
  47. #define PUT_32BE(off, v) (PUT_16BE((off), (v) >> 16), \
  48. PUT_16BE((off) + 2, (v)))
  49. /* Structure of an ELF file */
  50. #define E_IDENT 0 /* ELF header */
  51. #define E_PHOFF 28
  52. #define E_PHENTSIZE 42
  53. #define E_PHNUM 44
  54. #define E_HSIZE 52 /* size of ELF header */
  55. #define EI_MAGIC 0 /* offsets in E_IDENT area */
  56. #define EI_CLASS 4
  57. #define EI_DATA 5
  58. #define PH_TYPE 0 /* ELF program header */
  59. #define PH_OFFSET 4
  60. #define PH_FILESZ 16
  61. #define PH_HSIZE 32 /* size of program header */
  62. #define PT_NOTE 4 /* Program header type = note */
  63. #define ELFCLASS32 1
  64. #define ELFDATA2MSB 2
  65. unsigned char elf_magic[4] = { 0x7f, 'E', 'L', 'F' };
  66. int main(int ac, char **av)
  67. {
  68. int fd, n, i;
  69. int ph, ps, np;
  70. int nnote, ns;
  71. if (ac != 2) {
  72. fprintf(stderr, "Usage: %s elf-file\n", av[0]);
  73. exit(1);
  74. }
  75. fd = open(av[1], O_RDWR);
  76. if (fd < 0) {
  77. perror(av[1]);
  78. exit(1);
  79. }
  80. nnote = strlen(arch) + 1 + (N_DESCR + 3) * 4;
  81. n = read(fd, buf, sizeof(buf));
  82. if (n < 0) {
  83. perror("read");
  84. exit(1);
  85. }
  86. if (n < E_HSIZE || memcmp(&buf[E_IDENT+EI_MAGIC], elf_magic, 4) != 0)
  87. goto notelf;
  88. if (buf[E_IDENT+EI_CLASS] != ELFCLASS32
  89. || buf[E_IDENT+EI_DATA] != ELFDATA2MSB) {
  90. fprintf(stderr, "%s is not a big-endian 32-bit ELF image\n",
  91. av[1]);
  92. exit(1);
  93. }
  94. ph = GET_32BE(E_PHOFF);
  95. ps = GET_16BE(E_PHENTSIZE);
  96. np = GET_16BE(E_PHNUM);
  97. if (ph < E_HSIZE || ps < PH_HSIZE || np < 1)
  98. goto notelf;
  99. if (ph + (np + 1) * ps + nnote > n)
  100. goto nospace;
  101. for (i = 0; i < np; ++i) {
  102. if (GET_32BE(ph + PH_TYPE) == PT_NOTE) {
  103. fprintf(stderr, "%s already has a note entry\n",
  104. av[1]);
  105. exit(0);
  106. }
  107. ph += ps;
  108. }
  109. /* XXX check that the area we want to use is all zeroes */
  110. for (i = 0; i < ps + nnote; ++i)
  111. if (buf[ph + i] != 0)
  112. goto nospace;
  113. /* fill in the program header entry */
  114. ns = ph + ps;
  115. PUT_32BE(ph + PH_TYPE, PT_NOTE);
  116. PUT_32BE(ph + PH_OFFSET, ns);
  117. PUT_32BE(ph + PH_FILESZ, nnote);
  118. /* fill in the note area we point to */
  119. /* XXX we should probably make this a proper section */
  120. PUT_32BE(ns, strlen(arch) + 1);
  121. PUT_32BE(ns + 4, N_DESCR * 4);
  122. PUT_32BE(ns + 8, 0x1275);
  123. strcpy(&buf[ns + 12], arch);
  124. ns += 12 + strlen(arch) + 1;
  125. for (i = 0; i < N_DESCR; ++i)
  126. PUT_32BE(ns + i * 4, descr[i]);
  127. /* Update the number of program headers */
  128. PUT_16BE(E_PHNUM, np + 1);
  129. /* write back */
  130. lseek(fd, (long) 0, SEEK_SET);
  131. i = write(fd, buf, n);
  132. if (i < 0) {
  133. perror("write");
  134. exit(1);
  135. }
  136. if (i < n) {
  137. fprintf(stderr, "%s: write truncated\n", av[1]);
  138. exit(1);
  139. }
  140. exit(0);
  141. notelf:
  142. fprintf(stderr, "%s does not appear to be an ELF file\n", av[0]);
  143. exit(1);
  144. nospace:
  145. fprintf(stderr, "sorry, I can't find space in %s to put the note\n",
  146. av[0]);
  147. exit(1);
  148. }