Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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