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