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 * $Id: brandelf.c,v 1.10 1998/04/21 02:44:12 eivind 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 <err.h> 38 39 static int iselftype(const char *); 40 static void usage __P((void)); 41 42 int 43 main(int argc, char **argv) 44 { 45 46 const char *type = "FreeBSD"; 47 int retval = 0; 48 int ch, change = 0, verbose = 0, force = 0; 49 50 while ((ch = getopt(argc, argv, "ft:v")) != -1) 51 switch (ch) { 52 case 'f': 53 force = 1; 54 break; 55 case 'v': 56 verbose = 1; 57 break; 58 case 't': 59 change = 1; 60 type = optarg; 61 break; 62 default: 63 usage(); 64 } 65 argc -= optind; 66 argv += optind; 67 if (!argc) 68 errx(1, "no file(s) specified"); 69 70 if (!force && !iselftype(type)) 71 errx(1, "invalid ELF type '%s'", type); 72 73 while (argc) { 74 int fd; 75 char buffer[EI_NIDENT]; 76 char string[(EI_NIDENT-EI_BRAND)+1]; 77 78 if ((fd = open(argv[0], change? O_RDWR: O_RDONLY, 0)) < 0) { 79 warn("error opening file %s", argv[0]); 80 retval = 1; 81 goto fail; 82 } 83 if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) { 84 warnx("file '%s' too short", argv[0]); 85 retval = 1; 86 goto fail; 87 } 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]); 91 retval = 1; 92 goto fail; 93 } 94 if (!change) { 95 bzero(string, sizeof(string)); 96 strncpy(string, &buffer[EI_BRAND], EI_NIDENT-EI_BRAND); 97 if (strlen(string)) { 98 fprintf(stdout, 99 "File '%s' is of brand '%s'.\n", 100 argv[0], string); 101 if (!force && !iselftype(string)) 102 warnx("Brand '%s' is unknown", 103 string); 104 } 105 else 106 fprintf(stdout, "File '%s' has no branding.\n", 107 argv[0]); 108 } 109 else { 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]); 114 retval = 1; 115 goto fail; 116 } 117 } 118 fail: 119 argc--; 120 argv++; 121 } 122 123 return retval; 124 } 125 126 static void 127 usage() 128 { 129 fprintf(stderr, "usage: brandelf [-f] [-v] [-t string] file ...\n"); 130 exit(1); 131 } 132 133 static int 134 iselftype(const char *elftype) { 135 /* XXX - any more types? */ 136 const char *elftypes[] = { "FreeBSD", "Linux", "SVR4" }; 137 int elfwalk; 138 139 for (elfwalk = 0; 140 elfwalk < sizeof(elftypes)/sizeof(elftypes[0]); 141 elfwalk++) 142 if (strcmp(elftype, elftypes[elfwalk]) == 0) 143 return 1; 144 return 0; 145 } 146