addnote.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:4k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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