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 without 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.25 2005/05/21 09:55:04 ru Exp $
32 #include <sys/types.h>
33 #include <sys/elf_common.h>
34 #include <sys/errno.h>
42 static int elftype(const char *);
43 static const char *iselftype(int);
44 static void printelftypes(void);
45 static void usage(void);
51 /* XXX - any more types? */
52 static struct ELFtypes elftypes
[] = {
53 { "FreeBSD", ELFOSABI_FREEBSD
},
54 { "Linux", ELFOSABI_LINUX
},
55 { "Solaris", ELFOSABI_SOLARIS
},
56 { "SVR4", ELFOSABI_SYSV
}
60 main(int argc
, char **argv
)
63 const char *strtype
= "FreeBSD";
64 int type
= ELFOSABI_FREEBSD
;
66 int ch
, change
= 0, force
= 0, listed
= 0;
68 while ((ch
= getopt(argc
, argv
, "f:lt:v")) != -1)
72 errx(1, "f option incompatible with t option");
75 if (errno
== ERANGE
|| type
< 0 || type
> 255) {
76 warnx("invalid argument to option f: %s",
90 errx(1, "t option incompatible with f option");
103 warnx("no file(s) specified");
108 if (!force
&& (type
= elftype(strtype
)) == -1) {
109 warnx("invalid ELF type '%s'", strtype
);
116 char buffer
[EI_NIDENT
];
118 if ((fd
= open(argv
[0], change
|| force
? O_RDWR
: O_RDONLY
, 0)) < 0) {
119 warn("error opening file %s", argv
[0]);
123 if (read(fd
, buffer
, EI_NIDENT
) < EI_NIDENT
) {
124 warnx("file '%s' too short", argv
[0]);
128 if (buffer
[0] != ELFMAG0
|| buffer
[1] != ELFMAG1
||
129 buffer
[2] != ELFMAG2
|| buffer
[3] != ELFMAG3
) {
130 warnx("file '%s' is not ELF format", argv
[0]);
134 if (!change
&& !force
) {
136 "File '%s' is of brand '%s' (%u).\n",
137 argv
[0], iselftype(buffer
[EI_OSABI
]),
139 if (!iselftype(type
)) {
140 warnx("ELF ABI Brand '%u' is unknown",
146 buffer
[EI_OSABI
] = type
;
147 lseek(fd
, 0, SEEK_SET
);
148 if (write(fd
, buffer
, EI_NIDENT
) != EI_NIDENT
) {
149 warn("error writing %s %d", argv
[0], fd
);
167 "usage: brandelf [-lv] [-f ELF_ABI_number] [-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 (strcasecmp(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");