]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/brandelf/brandelf.c
This commit was generated by cvs2svn to compensate for changes in r78527,
[FreeBSD/FreeBSD.git] / usr.bin / brandelf / brandelf.c
1 /*-
2  * Copyright (c) 1996 Søren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #ifndef lint
30 static const char rcsid[] =
31   "$FreeBSD$";
32 #endif /* not lint */
33
34 #include <sys/types.h>
35 #include <sys/elf_common.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sys/errno.h>
42 #include <err.h>
43
44 static int elftype(const char *);
45 static const char *iselftype(int);
46 static void printelftypes(void);
47 static void usage __P((void));
48
49 struct ELFtypes {
50         const char *str;
51         int value;
52 };
53 /* XXX - any more types? */
54 static struct ELFtypes elftypes[] = {
55         { "FreeBSD",    ELFOSABI_FREEBSD },
56         { "Linux",      ELFOSABI_LINUX },
57         { "Solaris",    ELFOSABI_SOLARIS },
58         { "SVR4",       ELFOSABI_SYSV }
59 };
60
61 int
62 main(int argc, char **argv)
63 {
64
65         const char *strtype = "FreeBSD";
66         int type = ELFOSABI_FREEBSD;
67         int retval = 0;
68         int ch, change = 0, verbose = 0, force = 0, listed = 0;
69
70         while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
71                 switch (ch) {
72                 case 'f':
73                         if (change)
74                                 errx(1, "f option incompatible with t option");
75                         force = 1;
76                         type = atoi(optarg);
77                         if (errno == ERANGE || type < 0 || type > 255) {
78                                 warnx("invalid argument to option f: %s",
79                                     optarg);
80                                 usage();
81                         }
82                         break;
83                 case 'l':
84                         printelftypes();
85                         listed = 1;
86                         break;
87                 case 'v':
88                         verbose = 1;
89                         break;
90                 case 't':
91                         if (force)
92                                 errx(1, "t option incompatible with f option");
93                         change = 1;
94                         strtype = optarg;
95                         break;
96                 default:
97                         usage();
98         }
99         argc -= optind;
100         argv += optind;
101         if (!argc) {
102                 if (listed)
103                         exit(0);
104                 else {
105                         warnx("no file(s) specified");
106                         usage();
107                 }
108         }
109
110         if (!force && (type = elftype(strtype)) == -1) {
111                 warnx("invalid ELF type '%s'", strtype);
112                 printelftypes();
113                 usage();
114         }
115
116         while (argc) {
117                 int fd;
118                 char buffer[EI_NIDENT];
119
120                 if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
121                         warn("error opening file %s", argv[0]);
122                         retval = 1;
123                         goto fail;
124                 }
125                 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
126                         warnx("file '%s' too short", argv[0]);
127                         retval = 1;
128                         goto fail;
129                 }
130                 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
131                     buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
132                         warnx("file '%s' is not ELF format", argv[0]);
133                         retval = 1;
134                         goto fail;
135                 }
136                 if (!change && !force) {
137                         fprintf(stdout,
138                                 "File '%s' is of brand '%s' (%u).\n",
139                                 argv[0], iselftype(buffer[EI_OSABI]),
140                                 buffer[EI_OSABI]);
141                         if (!iselftype(type)) {
142                                 warnx("ELF ABI Brand '%u' is unknown",
143                                       type);
144                                 printelftypes();
145                         }
146                 }
147                 else {
148                         buffer[EI_OSABI] = type;
149                         lseek(fd, 0, SEEK_SET);
150                         if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
151                                 warn("error writing %s %d", argv[0], fd);
152                                 retval = 1;
153                                 goto fail;
154                         }
155                 }
156 fail:
157                 close(fd);
158                 argc--;
159                 argv++;
160         }
161
162         return retval;
163 }
164
165 static void
166 usage()
167 {
168 fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
169         exit(1);
170 }
171
172 static const char *
173 iselftype(int elftype)
174 {
175         int elfwalk;
176
177         for (elfwalk = 0;
178              elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
179              elfwalk++)
180                 if (elftype == elftypes[elfwalk].value)
181                         return elftypes[elfwalk].str;
182         return 0;
183 }
184
185 static int
186 elftype(const char *elfstrtype)
187 {
188         int elfwalk;
189
190         for (elfwalk = 0;
191              elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
192              elfwalk++)
193                 if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
194                         return elftypes[elfwalk].value;
195         return -1;
196 }
197
198 static void
199 printelftypes()
200 {
201         int elfwalk;
202
203         fprintf(stderr, "known ELF types are: ");
204         for (elfwalk = 0;
205              elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
206              elfwalk++)
207                 fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str, 
208                         elftypes[elfwalk].value);
209         fprintf(stderr, "\n");
210 }