Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / brandelf / brandelf.c
1 /*-
2  * Copyright (c) 2000, 2001 David O'Brien
3  * Copyright (c) 1996 Søren Schmidt
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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
17  *
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.
28  */
29
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD: src/usr.bin/brandelf/brandelf.c,v 1.13.2.4 2001/07/11 23:59:11 obrien Exp $";
33 #endif /* not lint */
34
35 #include <sys/types.h>
36 #include <sys/elf_common.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <sys/errno.h>
43 #include <err.h>
44
45 static int elftype(const char *);
46 static const char *iselftype(int);
47 static void printelftypes(void);
48 static void usage __P((void));
49
50 struct ELFtypes {
51         const char *str;
52         int value;
53 };
54 /* XXX - any more types? */
55 static struct ELFtypes elftypes[] = {
56         { "FreeBSD",    ELFOSABI_FREEBSD },
57         { "Linux",      ELFOSABI_LINUX },
58         { "Solaris",    ELFOSABI_SOLARIS },
59         { "SVR4",       ELFOSABI_SYSV }
60 };
61
62 int
63 main(int argc, char **argv)
64 {
65
66         const char *strtype = "FreeBSD";
67         int type = ELFOSABI_FREEBSD;
68         int retval = 0;
69         int ch, change = 0, verbose = 0, force = 0, listed = 0;
70
71         while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
72                 switch (ch) {
73                 case 'f':
74                         if (change)
75                                 errx(1, "f option incompatible with t option");
76                         force = 1;
77                         type = atoi(optarg);
78                         if (errno == ERANGE || type < 0 || type > 255) {
79                                 warnx("invalid argument to option f: %s",
80                                     optarg);
81                                 usage();
82                         }
83                         break;
84                 case 'l':
85                         printelftypes();
86                         listed = 1;
87                         break;
88                 case 'v':
89                         verbose = 1;
90                         break;
91                 case 't':
92                         if (force)
93                                 errx(1, "t option incompatible with f option");
94                         change = 1;
95                         strtype = optarg;
96                         break;
97                 default:
98                         usage();
99         }
100         argc -= optind;
101         argv += optind;
102         if (!argc) {
103                 if (listed)
104                         exit(0);
105                 else {
106                         warnx("no file(s) specified");
107                         usage();
108                 }
109         }
110
111         if (!force && (type = elftype(strtype)) == -1) {
112                 warnx("invalid ELF type '%s'", strtype);
113                 printelftypes();
114                 usage();
115         }
116
117         while (argc) {
118                 int fd;
119                 char buffer[EI_NIDENT];
120
121                 if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
122                         warn("error opening file %s", argv[0]);
123                         retval = 1;
124                         goto fail;
125                 }
126                 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
127                         warnx("file '%s' too short", argv[0]);
128                         retval = 1;
129                         goto fail;
130                 }
131                 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
132                     buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
133                         warnx("file '%s' is not ELF format", argv[0]);
134                         retval = 1;
135                         goto fail;
136                 }
137                 if (!change && !force) {
138                         fprintf(stdout,
139                                 "File '%s' is of brand '%s' (%u).\n",
140                                 argv[0], iselftype(buffer[EI_OSABI]),
141                                 buffer[EI_OSABI]);
142                         if (!iselftype(type)) {
143                                 warnx("ELF ABI Brand '%u' is unknown",
144                                       type);
145                                 printelftypes();
146                         }
147                 }
148                 else {
149                         buffer[EI_OSABI] = type;
150                         lseek(fd, 0, SEEK_SET);
151                         if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
152                                 warn("error writing %s %d", argv[0], fd);
153                                 retval = 1;
154                                 goto fail;
155                         }
156                 }
157 fail:
158                 close(fd);
159                 argc--;
160                 argv++;
161         }
162
163         return retval;
164 }
165
166 static void
167 usage()
168 {
169 fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
170         exit(1);
171 }
172
173 static const char *
174 iselftype(int etype)
175 {
176         size_t elfwalk;
177
178         for (elfwalk = 0;
179              elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
180              elfwalk++)
181                 if (etype == elftypes[elfwalk].value)
182                         return elftypes[elfwalk].str;
183         return 0;
184 }
185
186 static int
187 elftype(const char *elfstrtype)
188 {
189         size_t elfwalk;
190
191         for (elfwalk = 0;
192              elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
193              elfwalk++)
194                 if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
195                         return elftypes[elfwalk].value;
196         return -1;
197 }
198
199 static void
200 printelftypes()
201 {
202         size_t elfwalk;
203
204         fprintf(stderr, "known ELF types are: ");
205         for (elfwalk = 0;
206              elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
207              elfwalk++)
208                 fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str, 
209                         elftypes[elfwalk].value);
210         fprintf(stderr, "\n");
211 }