objstrip.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * arch/alpha/boot/tools/objstrip.c
  3. *
  4. * Strip the object file headers/trailers from an executable (ELF or ECOFF).
  5. *
  6. * Copyright (C) 1996 David Mosberger-Tang.
  7. */
  8. /*
  9. * Converts an ECOFF or ELF object file into a bootable file. The
  10. * object file must be a OMAGIC file (i.e., data and bss follow immediately
  11. * behind the text). See DEC "Assembly Language Programmer's Guide"
  12. * documentation for details. The SRM boot process is documented in
  13. * the Alpha AXP Architecture Reference Manual, Second Edition by
  14. * Richard L. Sites and Richard T. Witek.
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <sys/fcntl.h>
  21. #include <sys/stat.h>
  22. #include <sys/types.h>
  23. #include <linux/a.out.h>
  24. #include <linux/coff.h>
  25. #include <linux/param.h>
  26. #ifdef __ELF__
  27. # include <linux/elf.h>
  28. #endif
  29. /* bootfile size must be multiple of BLOCK_SIZE: */
  30. #define BLOCK_SIZE 512
  31. const char * prog_name;
  32. static void
  33. usage (void)
  34. {
  35. fprintf(stderr,
  36. "usage: %s [-v] -p file primary\n"
  37. " %s [-vb] file [secondary]\n", prog_name, prog_name);
  38. exit(1);
  39. }
  40. int
  41. main (int argc, char *argv[])
  42. {
  43. size_t nwritten, tocopy, n, mem_size, fil_size, pad = 0;
  44. int fd, ofd, i, j, verbose = 0, primary = 0;
  45. char buf[8192], *inname;
  46. struct exec * aout; /* includes file & aout header */
  47. long offset;
  48. #ifdef __ELF__
  49. struct elfhdr *elf;
  50. struct elf_phdr *elf_phdr; /* program header */
  51. unsigned long long e_entry;
  52. #endif
  53. prog_name = argv[0];
  54. for (i = 1; i < argc && argv[i][0] == '-'; ++i) {
  55. for (j = 1; argv[i][j]; ++j) {
  56. switch (argv[i][j]) {
  57. case 'v':
  58. verbose = ~verbose;
  59. break;
  60. case 'b':
  61. pad = BLOCK_SIZE;
  62. break;
  63. case 'p':
  64. primary = 1; /* make primary bootblock */
  65. break;
  66. }
  67. }
  68. }
  69. if (i >= argc) {
  70. usage();
  71. }
  72. inname = argv[i++];
  73. fd = open(inname, O_RDONLY);
  74. if (fd == -1) {
  75. perror("open");
  76. exit(1);
  77. }
  78. ofd = 1;
  79. if (i < argc) {
  80. ofd = open(argv[i++], O_WRONLY | O_CREAT | O_TRUNC, 0666);
  81. if (ofd == -1) {
  82. perror("open");
  83. exit(1);
  84. }
  85. }
  86. if (primary) {
  87. /* generate bootblock for primary loader */
  88. unsigned long bb[64], sum = 0;
  89. struct stat st;
  90. off_t size;
  91. int i;
  92. if (ofd == 1) {
  93. usage();
  94. }
  95. if (fstat(fd, &st) == -1) {
  96. perror("fstat");
  97. exit(1);
  98. }
  99. size = (st.st_size + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1);
  100. memset(bb, 0, sizeof(bb));
  101. strcpy((char *) bb, "Linux SRM bootblock");
  102. bb[60] = size / BLOCK_SIZE; /* count */
  103. bb[61] = 1; /* starting sector # */
  104. bb[62] = 0; /* flags---must be 0 */
  105. for (i = 0; i < 63; ++i) {
  106. sum += bb[i];
  107. }
  108. bb[63] = sum;
  109. if (write(ofd, bb, sizeof(bb)) != sizeof(bb)) {
  110. perror("boot-block write");
  111. exit(1);
  112. }
  113. printf("%lu\n", size);
  114. return 0;
  115. }
  116. /* read and inspect exec header: */
  117. if (read(fd, buf, sizeof(buf)) < 0) {
  118. perror("read");
  119. exit(1);
  120. }
  121. #ifdef __ELF__
  122. elf = (struct elfhdr *) buf;
  123. if (elf->e_ident[0] == 0x7f && strncmp((char *)elf->e_ident + 1, "ELF", 3) == 0) {
  124. if (elf->e_type != ET_EXEC) {
  125. fprintf(stderr, "%s: %s is not an ELF executable\n",
  126. prog_name, inname);
  127. exit(1);
  128. }
  129. if (!elf_check_arch(elf)) {
  130. fprintf(stderr, "%s: is not for this processor (e_machine=%d)\n",
  131. prog_name, elf->e_machine);
  132. exit(1);
  133. }
  134. if (elf->e_phnum != 1) {
  135. fprintf(stderr,
  136. "%s: %d program headers (forgot to link with -N?)\n",
  137. prog_name, elf->e_phnum);
  138. }
  139. e_entry = elf->e_entry;
  140. lseek(fd, elf->e_phoff, SEEK_SET);
  141. if (read(fd, buf, sizeof(*elf_phdr)) != sizeof(*elf_phdr)) {
  142. perror("read");
  143. exit(1);
  144. }
  145. elf_phdr = (struct elf_phdr *) buf;
  146. offset = elf_phdr->p_offset;
  147. mem_size = elf_phdr->p_memsz;
  148. fil_size = elf_phdr->p_filesz;
  149. /* work around ELF bug: */
  150. if (elf_phdr->p_vaddr < e_entry) {
  151. unsigned long delta = e_entry - elf_phdr->p_vaddr;
  152. offset += delta;
  153. mem_size -= delta;
  154. fil_size -= delta;
  155. elf_phdr->p_vaddr += delta;
  156. }
  157. if (verbose) {
  158. fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",
  159. prog_name, (long) elf_phdr->p_vaddr,
  160. elf_phdr->p_vaddr + fil_size, offset);
  161. }
  162. } else
  163. #endif
  164. {
  165. aout = (struct exec *) buf;
  166. if (!(aout->fh.f_flags & COFF_F_EXEC)) {
  167. fprintf(stderr, "%s: %s is not in executable format\n",
  168. prog_name, inname);
  169. exit(1);
  170. }
  171. if (aout->fh.f_opthdr != sizeof(aout->ah)) {
  172. fprintf(stderr, "%s: %s has unexpected optional header size\n",
  173. prog_name, inname);
  174. exit(1);
  175. }
  176. if (N_MAGIC(*aout) != OMAGIC) {
  177. fprintf(stderr, "%s: %s is not an OMAGIC file\n",
  178. prog_name, inname);
  179. exit(1);
  180. }
  181. offset = N_TXTOFF(*aout);
  182. fil_size = aout->ah.tsize + aout->ah.dsize;
  183. mem_size = fil_size + aout->ah.bsize;
  184. if (verbose) {
  185. fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",
  186. prog_name, aout->ah.text_start,
  187. aout->ah.text_start + fil_size, offset);
  188. }
  189. }
  190. if (lseek(fd, offset, SEEK_SET) != offset) {
  191. perror("lseek");
  192. exit(1);
  193. }
  194. if (verbose) {
  195. fprintf(stderr, "%s: copying %lu byte from %s\n",
  196. prog_name, (unsigned long) fil_size, inname);
  197. }
  198. tocopy = fil_size;
  199. while (tocopy > 0) {
  200. n = tocopy;
  201. if (n > sizeof(buf)) {
  202. n = sizeof(buf);
  203. }
  204. tocopy -= n;
  205. if ((size_t) read(fd, buf, n) != n) {
  206. perror("read");
  207. exit(1);
  208. }
  209. do {
  210. nwritten = write(ofd, buf, n);
  211. if ((ssize_t) nwritten == -1) {
  212. perror("write");
  213. exit(1);
  214. }
  215. n -= nwritten;
  216. } while (n > 0);
  217. }
  218. if (pad) {
  219. mem_size = ((mem_size + pad - 1) / pad) * pad;
  220. }
  221. tocopy = mem_size - fil_size;
  222. if (tocopy > 0) {
  223. fprintf(stderr,
  224. "%s: zero-filling bss and aligning to %lu with %lu bytes\n",
  225. prog_name, pad, (unsigned long) tocopy);
  226. memset(buf, 0x00, sizeof(buf));
  227. do {
  228. n = tocopy;
  229. if (n > sizeof(buf)) {
  230. n = sizeof(buf);
  231. }
  232. nwritten = write(ofd, buf, n);
  233. if ((ssize_t) nwritten == -1) {
  234. perror("write");
  235. exit(1);
  236. }
  237. tocopy -= nwritten;
  238. } while (tocopy > 0);
  239. }
  240. return 0;
  241. }