2 * Copyright (c) 2008 Hyogeol Lee
3 * Copyright (c) 2000, 2001 David O'Brien
4 * Copyright (c) 1996 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in this position and unchanged.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
47 ELFTC_VCSID("$Id: brandelf.c 3440 2016-04-07 14:51:47Z emaste $");
49 static int elftype(const char *);
50 static const char *iselftype(int);
51 static void printelftypes(void);
52 static void printversion(void);
53 static void usage(void);
59 /* XXX - any more types? */
60 static struct ELFtypes elftypes[] = {
61 { "86Open", ELFOSABI_86OPEN },
62 { "AIX", ELFOSABI_AIX },
63 { "ARM", ELFOSABI_ARM },
64 { "AROS", ELFOSABI_AROS },
65 { "CloudABI", ELFOSABI_CLOUDABI },
66 { "FreeBSD", ELFOSABI_FREEBSD },
67 { "GNU", ELFOSABI_GNU },
68 { "HP/UX", ELFOSABI_HPUX},
69 { "Hurd", ELFOSABI_HURD },
70 { "IRIX", ELFOSABI_IRIX },
71 { "Linux", ELFOSABI_GNU },
72 { "Modesto", ELFOSABI_MODESTO },
73 { "NSK", ELFOSABI_NSK },
74 { "NetBSD", ELFOSABI_NETBSD},
75 { "None", ELFOSABI_NONE},
76 { "OpenBSD", ELFOSABI_OPENBSD },
77 { "OpenVMS", ELFOSABI_OPENVMS },
78 { "Standalone", ELFOSABI_STANDALONE },
79 { "SVR4", ELFOSABI_NONE },
80 { "Solaris", ELFOSABI_SOLARIS },
81 { "Tru64", ELFOSABI_TRU64 }
84 static struct option brandelf_longopts[] = {
85 { "help", no_argument, NULL, 'h' },
86 { "version", no_argument, NULL, 'V' },
91 main(int argc, char **argv)
96 int type = ELFOSABI_NONE;
98 int ch, change = 0, force = 0, listed = 0;
100 if (elf_version(EV_CURRENT) == EV_NONE)
101 errx(EXIT_FAILURE, "elf_version error");
103 while ((ch = getopt_long(argc, argv, "Vf:hlt:v", brandelf_longopts,
108 errx(EXIT_FAILURE, "ERROR: the -f option is "
109 "incompatible with the -t option.");
112 if (errno == ERANGE || type < 0 || type > 255) {
113 warnx("ERROR: invalid argument to option "
126 /* This flag is ignored. */
130 errx(EXIT_FAILURE, "the -t option is "
131 "incompatible with the -f option.");
132 if ((type = elftype(optarg)) == -1) {
133 warnx("ERROR: invalid ELF type '%s'", optarg);
151 warnx("no file(s) specified");
161 if ((fd = open(argv[0], (change || force) ? O_RDWR :
163 warn("error opening file %s", argv[0]);
168 if ((elf = elf_begin(fd, (change || force) ? ELF_C_RDWR :
169 ELF_C_READ, NULL)) == NULL) {
170 warnx("elf_begin failed: %s", elf_errmsg(-1));
175 if ((kind = elf_kind(elf)) != ELF_K_ELF) {
176 if (kind == ELF_K_AR)
177 warnx("file '%s' is an archive.", argv[0]);
179 warnx("file '%s' is not an ELF file.",
185 if (gelf_getehdr(elf, &ehdr) == NULL) {
186 warnx("gelf_getehdr: %s", elf_errmsg(-1));
191 if (!change && !force) {
193 "File '%s' is of brand '%s' (%u).\n",
194 argv[0], iselftype(ehdr.e_ident[EI_OSABI]),
195 ehdr.e_ident[EI_OSABI]);
196 if (!iselftype(type)) {
197 warnx("ELF ABI Brand '%u' is unknown",
204 * Keep the existing layout of the ELF object.
206 if (elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT) == 0) {
207 warnx("elf_flagelf failed: %s",
214 * Update the ABI type.
216 ehdr.e_ident[EI_OSABI] = (unsigned char) type;
217 if (gelf_update_ehdr(elf, &ehdr) == 0) {
218 warnx("gelf_update_ehdr error: %s",
225 * Write back changes.
227 if (elf_update(elf, ELF_C_WRITE) == -1) {
228 warnx("elf_update error: %s", elf_errmsg(-1));
238 if (fd >= 0 && close(fd) == -1) {
239 warnx("%s: close error", argv[0]);
250 #define USAGE_MESSAGE "\
251 Usage: %s [options] file...\n\
252 Set or display the ABI field for an ELF object.\n\n\
253 Supported options are:\n\
254 -f NUM Set the ELF ABI to the number 'NUM'.\n\
255 -h | --help Print a usage message and exit.\n\
256 -l List known ELF ABI names.\n\
257 -t ABI Set the ELF ABI to the value named by \"ABI\".\n\
258 -V | --version Print a version identifier and exit.\n"
263 (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
270 (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
280 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
282 if (etype == elftypes[elfwalk].value)
283 return (elftypes[elfwalk].str);
288 elftype(const char *elfstrtype)
293 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
295 if (strcasecmp(elfstrtype, elftypes[elfwalk].str) == 0)
296 return (elftypes[elfwalk].value);
305 (void) printf("Known ELF types are: ");
307 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
309 (void) printf("%s(%u) ", elftypes[elfwalk].str,
310 elftypes[elfwalk].value);