2 * Copyright (c) 1996 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
30 static const char rcsid[] =
34 #include <sys/types.h>
35 #include <sys/elf_common.h>
41 #include <sys/errno.h>
44 static int elftype(const char *);
45 static const char *iselftype(int);
46 static void printelftypes(void);
47 static void usage __P((void));
53 /* XXX - any more types? */
54 static struct ELFtypes elftypes[] = {
55 { "FreeBSD", ELFOSABI_FREEBSD },
56 { "Linux", ELFOSABI_LINUX },
57 { "Solaris", ELFOSABI_SOLARIS },
58 { "SVR4", ELFOSABI_SYSV }
62 main(int argc, char **argv)
65 const char *strtype = "FreeBSD";
66 int type = ELFOSABI_FREEBSD;
68 int ch, change = 0, verbose = 0, force = 0, listed = 0;
70 while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
74 errx(1, "f option incompatible with t option");
77 if (errno == ERANGE || type < 0 || type > 255) {
78 warnx("invalid argument to option f: %s",
92 errx(1, "t option incompatible with f option");
105 warnx("no file(s) specified");
110 if (!force && (type = elftype(strtype)) == -1) {
111 warnx("invalid ELF type '%s'", strtype);
118 char buffer[EI_NIDENT];
120 if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
121 warn("error opening file %s", argv[0]);
125 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
126 warnx("file '%s' too short", argv[0]);
130 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
131 buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
132 warnx("file '%s' is not ELF format", argv[0]);
136 if (!change && !force) {
138 "File '%s' is of brand '%s' (%u).\n",
139 argv[0], iselftype(buffer[EI_OSABI]),
141 if (!iselftype(type)) {
142 warnx("ELF ABI Brand '%u' is unknown",
148 buffer[EI_OSABI] = type;
149 lseek(fd, 0, SEEK_SET);
150 if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
151 warn("error writing %s %d", argv[0], fd);
168 fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
173 iselftype(int elftype)
178 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
180 if (elftype == elftypes[elfwalk].value)
181 return elftypes[elfwalk].str;
186 elftype(const char *elfstrtype)
191 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
193 if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
194 return elftypes[elfwalk].value;
203 fprintf(stderr, "known ELF types are: ");
205 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
207 fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
208 elftypes[elfwalk].value);
209 fprintf(stderr, "\n");