1d8000daaSEd Maste /*-
2d8000daaSEd Maste * Copyright (c) 2008 Hyogeol Lee
3d8000daaSEd Maste * Copyright (c) 2000, 2001 David O'Brien
4656f49f8SEd Maste * Copyright (c) 1996 Søren Schmidt
5d8000daaSEd Maste * All rights reserved.
6d8000daaSEd Maste *
7d8000daaSEd Maste * Redistribution and use in source and binary forms, with or without
8d8000daaSEd Maste * modification, are permitted provided that the following conditions
9d8000daaSEd Maste * are met:
10d8000daaSEd Maste * 1. Redistributions of source code must retain the above copyright
11d8000daaSEd Maste * notice, this list of conditions and the following disclaimer
12d8000daaSEd Maste * in this position and unchanged.
13d8000daaSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
14d8000daaSEd Maste * notice, this list of conditions and the following disclaimer in the
15d8000daaSEd Maste * documentation and/or other materials provided with the distribution.
16d8000daaSEd Maste * 3. The name of the author may not be used to endorse or promote products
17d8000daaSEd Maste * derived from this software without specific prior written permission
18d8000daaSEd Maste *
19d8000daaSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20d8000daaSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21d8000daaSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22d8000daaSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23d8000daaSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24d8000daaSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25d8000daaSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26d8000daaSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27d8000daaSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28d8000daaSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29d8000daaSEd Maste */
30d8000daaSEd Maste
31d8000daaSEd Maste #include <sys/types.h>
32d8000daaSEd Maste #include <sys/stat.h>
33d8000daaSEd Maste #include <err.h>
34d8000daaSEd Maste #include <errno.h>
35d8000daaSEd Maste #include <fcntl.h>
36d8000daaSEd Maste #include <gelf.h>
37d8000daaSEd Maste #include <getopt.h>
38d8000daaSEd Maste #include <libelf.h>
39d8000daaSEd Maste #include <libelftc.h>
40d8000daaSEd Maste #include <stdio.h>
41d8000daaSEd Maste #include <stdlib.h>
42d8000daaSEd Maste #include <string.h>
43d8000daaSEd Maste #include <unistd.h>
44d8000daaSEd Maste
45d8000daaSEd Maste #include "_elftc.h"
46d8000daaSEd Maste
47*b6b6f9ccSEd Maste ELFTC_VCSID("$Id: brandelf.c 3440 2016-04-07 14:51:47Z emaste $");
48d8000daaSEd Maste
49d8000daaSEd Maste static int elftype(const char *);
50d8000daaSEd Maste static const char *iselftype(int);
51d8000daaSEd Maste static void printelftypes(void);
52d8000daaSEd Maste static void printversion(void);
53d8000daaSEd Maste static void usage(void);
54d8000daaSEd Maste
55d8000daaSEd Maste struct ELFtypes {
56d8000daaSEd Maste const char *str;
57d8000daaSEd Maste int value;
58d8000daaSEd Maste };
59d8000daaSEd Maste /* XXX - any more types? */
60d8000daaSEd Maste static struct ELFtypes elftypes[] = {
61d8000daaSEd Maste { "86Open", ELFOSABI_86OPEN },
62d8000daaSEd Maste { "AIX", ELFOSABI_AIX },
63d8000daaSEd Maste { "ARM", ELFOSABI_ARM },
64d8000daaSEd Maste { "AROS", ELFOSABI_AROS },
65*b6b6f9ccSEd Maste { "CloudABI", ELFOSABI_CLOUDABI },
66d8000daaSEd Maste { "FreeBSD", ELFOSABI_FREEBSD },
67d8000daaSEd Maste { "GNU", ELFOSABI_GNU },
68d8000daaSEd Maste { "HP/UX", ELFOSABI_HPUX},
69d8000daaSEd Maste { "Hurd", ELFOSABI_HURD },
70d8000daaSEd Maste { "IRIX", ELFOSABI_IRIX },
71d8000daaSEd Maste { "Linux", ELFOSABI_GNU },
72d8000daaSEd Maste { "Modesto", ELFOSABI_MODESTO },
73d8000daaSEd Maste { "NSK", ELFOSABI_NSK },
74d8000daaSEd Maste { "NetBSD", ELFOSABI_NETBSD},
75d8000daaSEd Maste { "None", ELFOSABI_NONE},
76d8000daaSEd Maste { "OpenBSD", ELFOSABI_OPENBSD },
77d8000daaSEd Maste { "OpenVMS", ELFOSABI_OPENVMS },
78d8000daaSEd Maste { "Standalone", ELFOSABI_STANDALONE },
79d8000daaSEd Maste { "SVR4", ELFOSABI_NONE },
80d8000daaSEd Maste { "Solaris", ELFOSABI_SOLARIS },
81d8000daaSEd Maste { "Tru64", ELFOSABI_TRU64 }
82d8000daaSEd Maste };
83d8000daaSEd Maste
84d8000daaSEd Maste static struct option brandelf_longopts[] = {
85d8000daaSEd Maste { "help", no_argument, NULL, 'h' },
86d8000daaSEd Maste { "version", no_argument, NULL, 'V' },
87d8000daaSEd Maste { NULL, 0, NULL, 0 }
88d8000daaSEd Maste };
89d8000daaSEd Maste
90d8000daaSEd Maste int
main(int argc,char ** argv)91d8000daaSEd Maste main(int argc, char **argv)
92d8000daaSEd Maste {
93d8000daaSEd Maste GElf_Ehdr ehdr;
94d8000daaSEd Maste Elf *elf;
95d8000daaSEd Maste Elf_Kind kind;
96d8000daaSEd Maste int type = ELFOSABI_NONE;
97d8000daaSEd Maste int retval = 0;
98d8000daaSEd Maste int ch, change = 0, force = 0, listed = 0;
99d8000daaSEd Maste
100d8000daaSEd Maste if (elf_version(EV_CURRENT) == EV_NONE)
101d8000daaSEd Maste errx(EXIT_FAILURE, "elf_version error");
102d8000daaSEd Maste
103d8000daaSEd Maste while ((ch = getopt_long(argc, argv, "Vf:hlt:v", brandelf_longopts,
104d8000daaSEd Maste NULL)) != -1)
105d8000daaSEd Maste switch (ch) {
106d8000daaSEd Maste case 'f':
107d8000daaSEd Maste if (change)
108d8000daaSEd Maste errx(EXIT_FAILURE, "ERROR: the -f option is "
109d8000daaSEd Maste "incompatible with the -t option.");
110d8000daaSEd Maste force = 1;
111d8000daaSEd Maste type = atoi(optarg);
112d8000daaSEd Maste if (errno == ERANGE || type < 0 || type > 255) {
113d8000daaSEd Maste warnx("ERROR: invalid argument to option "
114d8000daaSEd Maste "-f: %s", optarg);
115d8000daaSEd Maste usage();
116d8000daaSEd Maste }
117d8000daaSEd Maste break;
118d8000daaSEd Maste case 'h':
119d8000daaSEd Maste usage();
120d8000daaSEd Maste break;
121d8000daaSEd Maste case 'l':
122d8000daaSEd Maste printelftypes();
123d8000daaSEd Maste listed = 1;
124d8000daaSEd Maste break;
125d8000daaSEd Maste case 'v':
126d8000daaSEd Maste /* This flag is ignored. */
127d8000daaSEd Maste break;
128d8000daaSEd Maste case 't':
129d8000daaSEd Maste if (force)
130d8000daaSEd Maste errx(EXIT_FAILURE, "the -t option is "
131d8000daaSEd Maste "incompatible with the -f option.");
132d8000daaSEd Maste if ((type = elftype(optarg)) == -1) {
133d8000daaSEd Maste warnx("ERROR: invalid ELF type '%s'", optarg);
134d8000daaSEd Maste usage();
135d8000daaSEd Maste }
136d8000daaSEd Maste
137d8000daaSEd Maste change = 1;
138d8000daaSEd Maste break;
139d8000daaSEd Maste case 'V':
140d8000daaSEd Maste printversion();
141d8000daaSEd Maste break;
142d8000daaSEd Maste default:
143d8000daaSEd Maste usage();
144d8000daaSEd Maste }
145d8000daaSEd Maste argc -= optind;
146d8000daaSEd Maste argv += optind;
147d8000daaSEd Maste if (!argc) {
148d8000daaSEd Maste if (listed)
149d8000daaSEd Maste exit(0);
150d8000daaSEd Maste else {
151d8000daaSEd Maste warnx("no file(s) specified");
152d8000daaSEd Maste usage();
153d8000daaSEd Maste }
154d8000daaSEd Maste }
155d8000daaSEd Maste
156d8000daaSEd Maste while (argc) {
157d8000daaSEd Maste int fd;
158d8000daaSEd Maste
159d8000daaSEd Maste elf = NULL;
160d8000daaSEd Maste
161d8000daaSEd Maste if ((fd = open(argv[0], (change || force) ? O_RDWR :
162d8000daaSEd Maste O_RDONLY, 0)) < 0) {
163d8000daaSEd Maste warn("error opening file %s", argv[0]);
164d8000daaSEd Maste retval = 1;
165d8000daaSEd Maste goto fail;
166d8000daaSEd Maste }
167d8000daaSEd Maste
168d8000daaSEd Maste if ((elf = elf_begin(fd, (change || force) ? ELF_C_RDWR :
169d8000daaSEd Maste ELF_C_READ, NULL)) == NULL) {
170d8000daaSEd Maste warnx("elf_begin failed: %s", elf_errmsg(-1));
171d8000daaSEd Maste retval = 1;
172d8000daaSEd Maste goto fail;
173d8000daaSEd Maste }
174d8000daaSEd Maste
175d8000daaSEd Maste if ((kind = elf_kind(elf)) != ELF_K_ELF) {
176d8000daaSEd Maste if (kind == ELF_K_AR)
177d8000daaSEd Maste warnx("file '%s' is an archive.", argv[0]);
178d8000daaSEd Maste else
179d8000daaSEd Maste warnx("file '%s' is not an ELF file.",
180d8000daaSEd Maste argv[0]);
181d8000daaSEd Maste retval = 1;
182d8000daaSEd Maste goto fail;
183d8000daaSEd Maste }
184d8000daaSEd Maste
185d8000daaSEd Maste if (gelf_getehdr(elf, &ehdr) == NULL) {
186d8000daaSEd Maste warnx("gelf_getehdr: %s", elf_errmsg(-1));
187d8000daaSEd Maste retval = 1;
188d8000daaSEd Maste goto fail;
189d8000daaSEd Maste }
190d8000daaSEd Maste
191d8000daaSEd Maste if (!change && !force) {
192d8000daaSEd Maste fprintf(stdout,
193d8000daaSEd Maste "File '%s' is of brand '%s' (%u).\n",
194d8000daaSEd Maste argv[0], iselftype(ehdr.e_ident[EI_OSABI]),
195d8000daaSEd Maste ehdr.e_ident[EI_OSABI]);
196d8000daaSEd Maste if (!iselftype(type)) {
197d8000daaSEd Maste warnx("ELF ABI Brand '%u' is unknown",
198d8000daaSEd Maste type);
199d8000daaSEd Maste printelftypes();
200d8000daaSEd Maste }
201d8000daaSEd Maste } else {
202d8000daaSEd Maste
203d8000daaSEd Maste /*
204d8000daaSEd Maste * Keep the existing layout of the ELF object.
205d8000daaSEd Maste */
206d8000daaSEd Maste if (elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT) == 0) {
207d8000daaSEd Maste warnx("elf_flagelf failed: %s",
208d8000daaSEd Maste elf_errmsg(-1));
209d8000daaSEd Maste retval = 1;
210d8000daaSEd Maste goto fail;
211d8000daaSEd Maste }
212d8000daaSEd Maste
213d8000daaSEd Maste /*
214d8000daaSEd Maste * Update the ABI type.
215d8000daaSEd Maste */
216839529caSEd Maste ehdr.e_ident[EI_OSABI] = (unsigned char) type;
217d8000daaSEd Maste if (gelf_update_ehdr(elf, &ehdr) == 0) {
218d8000daaSEd Maste warnx("gelf_update_ehdr error: %s",
219d8000daaSEd Maste elf_errmsg(-1));
220d8000daaSEd Maste retval = 1;
221d8000daaSEd Maste goto fail;
222d8000daaSEd Maste }
223d8000daaSEd Maste
224d8000daaSEd Maste /*
225d8000daaSEd Maste * Write back changes.
226d8000daaSEd Maste */
227d8000daaSEd Maste if (elf_update(elf, ELF_C_WRITE) == -1) {
228d8000daaSEd Maste warnx("elf_update error: %s", elf_errmsg(-1));
229d8000daaSEd Maste retval = 1;
230d8000daaSEd Maste goto fail;
231d8000daaSEd Maste }
232d8000daaSEd Maste }
233d8000daaSEd Maste fail:
234d8000daaSEd Maste
235d8000daaSEd Maste if (elf)
236d8000daaSEd Maste elf_end(elf);
237d8000daaSEd Maste
238d8000daaSEd Maste if (fd >= 0 && close(fd) == -1) {
239d8000daaSEd Maste warnx("%s: close error", argv[0]);
240d8000daaSEd Maste retval = 1;
241d8000daaSEd Maste }
242d8000daaSEd Maste
243d8000daaSEd Maste argc--;
244d8000daaSEd Maste argv++;
245d8000daaSEd Maste }
246d8000daaSEd Maste
247d8000daaSEd Maste return (retval);
248d8000daaSEd Maste }
249d8000daaSEd Maste
250d8000daaSEd Maste #define USAGE_MESSAGE "\
251d8000daaSEd Maste Usage: %s [options] file...\n\
252d8000daaSEd Maste Set or display the ABI field for an ELF object.\n\n\
253d8000daaSEd Maste Supported options are:\n\
254d8000daaSEd Maste -f NUM Set the ELF ABI to the number 'NUM'.\n\
255d8000daaSEd Maste -h | --help Print a usage message and exit.\n\
256d8000daaSEd Maste -l List known ELF ABI names.\n\
257d8000daaSEd Maste -t ABI Set the ELF ABI to the value named by \"ABI\".\n\
258d8000daaSEd Maste -V | --version Print a version identifier and exit.\n"
259d8000daaSEd Maste
260d8000daaSEd Maste static void
usage(void)261d8000daaSEd Maste usage(void)
262d8000daaSEd Maste {
263d8000daaSEd Maste (void) fprintf(stderr, USAGE_MESSAGE, ELFTC_GETPROGNAME());
264d8000daaSEd Maste exit(1);
265d8000daaSEd Maste }
266d8000daaSEd Maste
267d8000daaSEd Maste static void
printversion(void)268d8000daaSEd Maste printversion(void)
269d8000daaSEd Maste {
270d8000daaSEd Maste (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
271d8000daaSEd Maste exit(0);
272d8000daaSEd Maste }
273d8000daaSEd Maste
274d8000daaSEd Maste static const char *
iselftype(int etype)275d8000daaSEd Maste iselftype(int etype)
276d8000daaSEd Maste {
277d8000daaSEd Maste size_t elfwalk;
278d8000daaSEd Maste
279d8000daaSEd Maste for (elfwalk = 0;
280d8000daaSEd Maste elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
281d8000daaSEd Maste elfwalk++)
282d8000daaSEd Maste if (etype == elftypes[elfwalk].value)
283d8000daaSEd Maste return (elftypes[elfwalk].str);
284d8000daaSEd Maste return (0);
285d8000daaSEd Maste }
286d8000daaSEd Maste
287d8000daaSEd Maste static int
elftype(const char * elfstrtype)288d8000daaSEd Maste elftype(const char *elfstrtype)
289d8000daaSEd Maste {
290d8000daaSEd Maste size_t elfwalk;
291d8000daaSEd Maste
292d8000daaSEd Maste for (elfwalk = 0;
293d8000daaSEd Maste elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
294d8000daaSEd Maste elfwalk++)
295d8000daaSEd Maste if (strcasecmp(elfstrtype, elftypes[elfwalk].str) == 0)
296d8000daaSEd Maste return (elftypes[elfwalk].value);
297d8000daaSEd Maste return (-1);
298d8000daaSEd Maste }
299d8000daaSEd Maste
300d8000daaSEd Maste static void
printelftypes(void)301d8000daaSEd Maste printelftypes(void)
302d8000daaSEd Maste {
303d8000daaSEd Maste size_t elfwalk;
304d8000daaSEd Maste
305d8000daaSEd Maste (void) printf("Known ELF types are: ");
306d8000daaSEd Maste for (elfwalk = 0;
307d8000daaSEd Maste elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
308d8000daaSEd Maste elfwalk++)
309d8000daaSEd Maste (void) printf("%s(%u) ", elftypes[elfwalk].str,
310d8000daaSEd Maste elftypes[elfwalk].value);
311d8000daaSEd Maste (void) printf("\n");
312d8000daaSEd Maste }
313