doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
brandelf.c
Go to the documentation of this file.
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  * $FreeBSD: src/usr.bin/brandelf/brandelf.c,v 1.16 2000/07/02 03:34:08 imp Exp $
29  */
30 
31 #include <elf.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/errno.h>
38 #include <err.h>
39 
40 /* These are defined on FreeBSD, but not on Linux */
41 #ifndef ELFOSABI_SYSV
42 #define ELFOSABI_SYSV 0
43 #endif
44 #ifndef ELFOSABI_LINUX
45 #define ELFOSABI_LINUX 3
46 #endif
47 #ifndef ELFOSABI_HURD
48 #define ELFOSABI_HURD 4
49 #endif
50 #ifndef ELFOSABI_SOLARIS
51 #define ELFOSABI_SOLARIS 6
52 #endif
53 #ifndef ELFOSABI_FREEBSD
54 #define ELFOSABI_FREEBSD 9
55 #endif
56 
57 
58 static int elftype(const char *);
59 static const char *iselftype(int);
60 static void printelftypes(void);
61 static void usage __P((void));
62 
63 struct ELFtypes {
64  const char *str;
65  int value;
66 };
67 /* XXX - any more types? */
68 static struct ELFtypes elftypes[] = {
69  { "FreeBSD", ELFOSABI_FREEBSD },
70  { "Linux", ELFOSABI_LINUX },
71  { "Solaris", ELFOSABI_SOLARIS },
72  { "SVR4", ELFOSABI_SYSV }
73 };
74 
75 int
76 main(int argc, char **argv)
77 {
78 
79  const char *strtype = "FreeBSD";
80  int type = ELFOSABI_FREEBSD;
81  int retval = 0;
82  int ch, change = 0, verbose = 0, force = 0, listed = 0;
83 
84  while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
85  switch (ch) {
86  case 'f':
87  if (change)
88  errx(1, "f option incompatable with t option");
89  force = 1;
90  type = atoi(optarg);
91  if (errno == ERANGE || type < 0 || type > 255) {
92  warnx("invalid argument to option f: %s",
93  optarg);
94  usage();
95  }
96  break;
97  case 'l':
98  printelftypes();
99  listed = 1;
100  break;
101  case 'v':
102  verbose = 1;
103  break;
104  case 't':
105  if (force)
106  errx(1, "t option incompatable with f option");
107  change = 1;
108  strtype = optarg;
109  break;
110  default:
111  usage();
112  }
113  argc -= optind;
114  argv += optind;
115  if (!argc) {
116  if (listed)
117  exit(0);
118  else {
119  warnx("no file(s) specified");
120  usage();
121  }
122  }
123 
124  if (!force && (type = elftype(strtype)) == -1) {
125  warnx("invalid ELF type '%s'", strtype);
126  printelftypes();
127  usage();
128  }
129 
130  while (argc) {
131  int fd;
132  char buffer[EI_NIDENT];
133 
134  if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
135  warn("error opening file %s", argv[0]);
136  retval = 1;
137  goto fail;
138  }
139  if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
140  warnx("file '%s' too short", argv[0]);
141  retval = 1;
142  goto fail;
143  }
144  if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
145  buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
146  warnx("file '%s' is not ELF format", argv[0]);
147  retval = 1;
148  goto fail;
149  }
150  if (!change && !force) {
151  fprintf(stdout,
152  "File '%s' is of brand '%s' (%u).\n",
153  argv[0], iselftype(buffer[EI_OSABI]),
154  buffer[EI_OSABI]);
155  if (!iselftype(type)) {
156  warnx("ELF ABI Brand '%u' is unknown",
157  type);
158  printelftypes();
159  }
160  }
161  else {
162  buffer[EI_OSABI] = type;
163  lseek(fd, 0, SEEK_SET);
164  if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
165  warn("error writing %s %d", argv[0], fd);
166  retval = 1;
167  goto fail;
168  }
169  }
170 fail:
171  close(fd);
172  argc--;
173  argv++;
174  }
175 
176  return retval;
177 }
178 
179 static void
180 usage()
181 {
182 fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
183  exit(1);
184 }
185 
186 static const char *
187 iselftype(int elftype)
188 {
189  int elfwalk;
190 
191  for (elfwalk = 0;
192  elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
193  elfwalk++)
194  if (elftype == elftypes[elfwalk].value)
195  return elftypes[elfwalk].str;
196  return 0;
197 }
198 
199 static int
200 elftype(const char *elfstrtype)
201 {
202  int elfwalk;
203 
204  for (elfwalk = 0;
205  elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
206  elfwalk++)
207  if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
208  return elftypes[elfwalk].value;
209  return -1;
210 }
211 
212 static void
213 printelftypes()
214 {
215  int elfwalk;
216 
217  fprintf(stderr, "known ELF types are: ");
218  for (elfwalk = 0;
219  elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
220  elfwalk++)
221  fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
222  elftypes[elfwalk].value);
223  fprintf(stderr, "\n");
224 }
#define strcmp
Definition: Str.h:41
#define O_RDONLY
GLsizei const GLfloat * value
Definition: glext.h:3614
#define ELFOSABI_FREEBSD
Definition: brandelf.c:54
GLsizeiptr const GLvoid GLenum usage
Definition: glext.h:3112
GLuint GLuint GLsizei GLenum type
Definition: glext.h:2845
GLuint buffer
Definition: glext.h:3108
#define ELFOSABI_SOLARIS
Definition: brandelf.c:51
#define ELFOSABI_SYSV
Definition: brandelf.c:42
#define SEEK_SET
Definition: Unzip.cpp:129
#define ELFOSABI_LINUX
Definition: brandelf.c:45
int value
Definition: brandelf.c:65
const char * str
Definition: brandelf.c:64
int main(int argc, char **argv)
Definition: brandelf.c:76