2 * Copyright (c) 1996 Søren Schmidt
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
39 static int iselftype(const char *);
40 static void usage __P((void));
43 main(int argc, char **argv)
46 const char *type = "FreeBSD";
48 int ch, change = 0, verbose = 0, force = 0;
50 while ((ch = getopt(argc, argv, "ft:v")) != -1)
68 errx(1, "no file(s) specified");
70 if (!force && !iselftype(type))
71 errx(1, "invalid ELF type '%s'", type);
75 char buffer[EI_NIDENT];
76 char string[(EI_NIDENT-EI_BRAND)+1];
78 if ((fd = open(argv[0], change? O_RDWR: O_RDONLY, 0)) < 0) {
79 warn("error opening file %s", argv[0]);
83 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
84 warnx("file '%s' too short", argv[0]);
88 if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
89 buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
90 warnx("file '%s' is not ELF format", argv[0]);
95 bzero(string, sizeof(string));
96 strncpy(string, &buffer[EI_BRAND], EI_NIDENT-EI_BRAND);
99 "File '%s' is of brand '%s'.\n",
101 if (!force && !iselftype(string))
102 warnx("Brand '%s' is unknown",
106 fprintf(stdout, "File '%s' has no branding.\n",
110 strncpy(&buffer[EI_BRAND], type, EI_NIDENT-EI_BRAND);
111 lseek(fd, 0, SEEK_SET);
112 if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
113 warnx("error writing %s", argv[0]);
129 fprintf(stderr, "usage: brandelf [-f] [-v] [-t string] file ...\n");
134 iselftype(const char *elftype) {
135 /* XXX - any more types? */
136 const char *elftypes[] = { "FreeBSD", "Linux", "SVR4" };
140 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
142 if (strcmp(elftype, elftypes[elfwalk]) == 0)