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.
29 * $FreeBSD: src/usr.bin/brandelf/brandelf.c,v 1.13.2.4 2001/07/11 23:59:11 obrien Exp $
30 * $DragonFly: src/usr.bin/brandelf/brandelf.c,v 1.3 2003/10/02 17:42:25 hmp Exp $
33 #include <sys/types.h>
34 #include <sys/elf_common.h>
40 #include <sys/errno.h>
43 static int elftype(const char *);
44 static const char *iselftype(int);
45 static void printelftypes(void);
46 static void usage(void);
52 /* XXX - any more types? */
53 static struct ELFtypes elftypes
[] = {
54 { "FreeBSD", ELFOSABI_FREEBSD
},
55 { "Linux", ELFOSABI_LINUX
},
56 { "Solaris", ELFOSABI_SOLARIS
},
57 { "SVR4", ELFOSABI_SYSV
}
61 main(int argc
, char **argv
)
64 const char *strtype
= "FreeBSD";
65 int type
= ELFOSABI_FREEBSD
;
67 int ch
, change
= 0, verbose
= 0, force
= 0, listed
= 0;
69 while ((ch
= getopt(argc
, argv
, "f:lt:v")) != -1)
73 errx(1, "f option incompatible with t option");
76 if (errno
== ERANGE
|| type
< 0 || type
> 255) {
77 warnx("invalid argument to option f: %s",
91 errx(1, "t option incompatible with f option");
104 warnx("no file(s) specified");
109 if (!force
&& (type
= elftype(strtype
)) == -1) {
110 warnx("invalid ELF type '%s'", strtype
);
117 char buffer
[EI_NIDENT
];
119 if ((fd
= open(argv
[0], change
|| force
? O_RDWR
: O_RDONLY
, 0)) < 0) {
120 warn("error opening file %s", argv
[0]);
124 if (read(fd
, buffer
, EI_NIDENT
) < EI_NIDENT
) {
125 warnx("file '%s' too short", argv
[0]);
129 if (buffer
[0] != ELFMAG0
|| buffer
[1] != ELFMAG1
||
130 buffer
[2] != ELFMAG2
|| buffer
[3] != ELFMAG3
) {
131 warnx("file '%s' is not ELF format", argv
[0]);
135 if (!change
&& !force
) {
137 "File '%s' is of brand '%s' (%u).\n",
138 argv
[0], iselftype(buffer
[EI_OSABI
]),
140 if (!iselftype(type
)) {
141 warnx("ELF ABI Brand '%u' is unknown",
147 buffer
[EI_OSABI
] = type
;
148 lseek(fd
, 0, SEEK_SET
);
149 if (write(fd
, buffer
, EI_NIDENT
) != EI_NIDENT
) {
150 warn("error writing %s %d", argv
[0], fd
);
167 fprintf(stderr
, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
177 elfwalk
< sizeof(elftypes
)/sizeof(elftypes
[0]);
179 if (etype
== elftypes
[elfwalk
].value
)
180 return elftypes
[elfwalk
].str
;
185 elftype(const char *elfstrtype
)
190 elfwalk
< sizeof(elftypes
)/sizeof(elftypes
[0]);
192 if (strcmp(elfstrtype
, elftypes
[elfwalk
].str
) == 0)
193 return elftypes
[elfwalk
].value
;
202 fprintf(stderr
, "known ELF types are: ");
204 elfwalk
< sizeof(elftypes
)/sizeof(elftypes
[0]);
206 fprintf(stderr
, "%s(%u) ", elftypes
[elfwalk
].str
,
207 elftypes
[elfwalk
].value
);
208 fprintf(stderr
, "\n");