brandelf.c revision 62313
1/*-
2 * Copyright (c) 1996 S�ren Schmidt
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer
10 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software withough specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $FreeBSD: head/usr.bin/brandelf/brandelf.c 62313 2000-07-01 05:48:33Z green $
29 */
30
31#include <elf.h>
32#include <fcntl.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37#include <sys/errno.h>
38#include <err.h>
39
40static int elftype(const char *);
41static const char *iselftype(int);
42static void printelftypes(void);
43static void usage __P((void));
44
45struct ELFtypes {
46	const char *str;
47	int value;
48};
49/* XXX - any more types? */
50static struct ELFtypes elftypes[] = {
51	{ "FreeBSD",	ELFOSABI_FREEBSD },
52	{ "Linux",	ELFOSABI_LINUX },
53	{ "Solaris",	ELFOSABI_SOLARIS },
54	{ "SVR4",	ELFOSABI_SYSV }
55};
56
57int
58main(int argc, char **argv)
59{
60
61	const char *strtype = "FreeBSD";
62	int type = ELFOSABI_FREEBSD;
63	int retval = 0;
64	int ch, change = 0, verbose = 0, force = 0, listed = 0;
65
66	while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
67		switch (ch) {
68		case 'f':
69			if (change)
70				errx(1, "f option incompatable with t option");
71			force = 1;
72			type = atoi(optarg);
73			if (errno == ERANGE || type < 0 || type > 255) {
74				warnx("invalid argument to option f: %s",
75				    optarg);
76				usage();
77			}
78			break;
79		case 'l':
80			printelftypes();
81			listed = 1;
82			break;
83		case 'v':
84			verbose = 1;
85			break;
86		case 't':
87			if (force)
88				errx(1, "t option incompatable with f option");
89			change = 1;
90			strtype = optarg;
91			break;
92		default:
93			usage();
94	}
95	argc -= optind;
96	argv += optind;
97	if (!argc) {
98		if (listed)
99			exit(0);
100		else {
101			warnx("no file(s) specified");
102			usage();
103		}
104	}
105
106	if (!force && (type = elftype(strtype)) == -1) {
107		warnx("invalid ELF type '%s'", strtype);
108		printelftypes();
109		usage();
110	}
111
112	while (argc) {
113		int fd;
114		char buffer[EI_NIDENT];
115
116		if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
117			warn("error opening file %s", argv[0]);
118			retval = 1;
119			goto fail;
120		}
121		if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
122			warnx("file '%s' too short", argv[0]);
123			retval = 1;
124			goto fail;
125		}
126		if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
127		    buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
128			warnx("file '%s' is not ELF format", argv[0]);
129			retval = 1;
130			goto fail;
131		}
132		if (!change && !force) {
133			fprintf(stdout,
134				"File '%s' is of brand '%s' (%u).\n",
135				argv[0], iselftype(buffer[EI_OSABI]),
136				buffer[EI_OSABI]);
137			if (!iselftype(type)) {
138				warnx("ELF ABI Brand '%u' is unknown",
139				      type);
140				printelftypes();
141			}
142		}
143		else {
144			buffer[EI_OSABI] = type;
145			lseek(fd, 0, SEEK_SET);
146			if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
147				warn("error writing %s %d", argv[0], fd);
148				retval = 1;
149				goto fail;
150			}
151		}
152fail:
153		argc--;
154		argv++;
155	}
156
157	return retval;
158}
159
160static void
161usage()
162{
163fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
164	exit(1);
165}
166
167static const char *
168iselftype(int elftype)
169{
170	int elfwalk;
171
172	for (elfwalk = 0;
173	     elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
174	     elfwalk++)
175		if (elftype == elftypes[elfwalk].value)
176			return elftypes[elfwalk].str;
177	return 0;
178}
179
180static int
181elftype(const char *elfstrtype)
182{
183	int elfwalk;
184
185	for (elfwalk = 0;
186	     elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
187	     elfwalk++)
188		if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
189			return elftypes[elfwalk].value;
190	return -1;
191}
192
193static void
194printelftypes()
195{
196	int elfwalk;
197
198	fprintf(stderr, "known ELF types are: ");
199	for (elfwalk = 0;
200	     elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
201	     elfwalk++)
202		fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
203			elftypes[elfwalk].value);
204	fprintf(stderr, "\n");
205}
206