objstrip.c 6.1 KB

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