2 * Copyright (c) 2000, 2001 David O'Brien
3 * Copyright (c) 1996 Søren Schmidt
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software withough specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 static const char rcsid[] =
32 "$FreeBSD: src/usr.bin/brandelf/brandelf.c,v 1.13.2.4 2001/07/11 23:59:11 obrien Exp $";
35 #include <sys/types.h>
36 #include <sys/elf_common.h>
42 #include <sys/errno.h>
45 static int elftype(const char *);
46 static const char *iselftype(int);
47 static void printelftypes(void);
48 static void usage __P((void));
54 /* XXX - any more types? */
55 static struct ELFtypes elftypes[] = {
56 { "FreeBSD", ELFOSABI_FREEBSD },
57 { "Linux", ELFOSABI_LINUX },
58 { "Solaris", ELFOSABI_SOLARIS },
59 { "SVR4", ELFOSABI_SYSV }
63 main(int argc, char **argv)
66 const char *strtype = "FreeBSD";
67 int type = ELFOSABI_FREEBSD;
69 int ch, change = 0, verbose = 0, force = 0, listed = 0;
71 while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
75 errx(1, "f option incompatible with t option");
78 if (errno == ERANGE || type < 0 || type > 255) {
79 warnx("invalid argument to option f: %s",
93 errx(1, "t option incompatible with f option");
106 warnx("no file(s) specified");
111 if (!force && (type = elftype(strtype)) == -1) {
112 warnx("invalid ELF type '%s'", strtype);
119 char buffer[EI_NIDENT];
121 if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
122 warn("error opening file %s", argv[0]);
126 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
127 warnx("file '%s' too short", argv[0]);
131 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
132 buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
133 warnx("file '%s' is not ELF format", argv[0]);
137 if (!change && !force) {
139 "File '%s' is of brand '%s' (%u).\n",
140 argv[0], iselftype(buffer[EI_OSABI]),
142 if (!iselftype(type)) {
143 warnx("ELF ABI Brand '%u' is unknown",
149 buffer[EI_OSABI] = type;
150 lseek(fd, 0, SEEK_SET);
151 if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
152 warn("error writing %s %d", argv[0], fd);
169 fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
179 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
181 if (etype == elftypes[elfwalk].value)
182 return elftypes[elfwalk].str;
187 elftype(const char *elfstrtype)
192 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
194 if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
195 return elftypes[elfwalk].value;
204 fprintf(stderr, "known ELF types are: ");
206 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
208 fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
209 elftypes[elfwalk].value);
210 fprintf(stderr, "\n");