objstrip.c 6.1 KB

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