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