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