1996-10-16 18:16:22 +00:00
|
|
|
|
/*-
|
2001-06-22 21:43:51 +00:00
|
|
|
|
* Copyright (c) 2000, 2001 David O'Brien
|
1996-10-16 18:16:22 +00:00
|
|
|
|
* Copyright (c) 1996 S<EFBFBD>ren Schmidt
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
|
* in this position and unchanged.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2002-06-02 20:05:59 +00:00
|
|
|
|
* derived from this software without specific prior written permission
|
1996-10-16 18:16:22 +00:00
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
*/
|
|
|
|
|
|
2001-12-03 00:00:22 +00:00
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
__FBSDID("$FreeBSD$");
|
2001-05-02 23:57:49 +00:00
|
|
|
|
|
2001-05-02 23:56:21 +00:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/elf_common.h>
|
2001-12-03 00:00:22 +00:00
|
|
|
|
#include <sys/errno.h>
|
|
|
|
|
#include <err.h>
|
1997-05-21 23:07:31 +00:00
|
|
|
|
#include <fcntl.h>
|
1996-10-16 18:16:22 +00:00
|
|
|
|
#include <stdio.h>
|
1997-05-21 23:07:31 +00:00
|
|
|
|
#include <stdlib.h>
|
1997-02-09 20:35:54 +00:00
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
1996-10-16 18:16:22 +00:00
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
|
static int elftype(const char *);
|
|
|
|
|
static const char *iselftype(int);
|
2000-01-04 02:33:54 +00:00
|
|
|
|
static void printelftypes(void);
|
2001-12-03 00:00:22 +00:00
|
|
|
|
static void usage(void);
|
1996-10-16 18:16:22 +00:00
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
|
struct ELFtypes {
|
|
|
|
|
const char *str;
|
|
|
|
|
int value;
|
|
|
|
|
};
|
|
|
|
|
/* XXX - any more types? */
|
|
|
|
|
static struct ELFtypes elftypes[] = {
|
|
|
|
|
{ "FreeBSD", ELFOSABI_FREEBSD },
|
2000-07-01 05:48:33 +00:00
|
|
|
|
{ "Linux", ELFOSABI_LINUX },
|
|
|
|
|
{ "Solaris", ELFOSABI_SOLARIS },
|
|
|
|
|
{ "SVR4", ELFOSABI_SYSV }
|
2000-04-18 02:39:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
1997-02-09 20:35:54 +00:00
|
|
|
|
int
|
1996-10-16 18:16:22 +00:00
|
|
|
|
main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
|
const char *strtype = "FreeBSD";
|
|
|
|
|
int type = ELFOSABI_FREEBSD;
|
1997-02-09 20:35:54 +00:00
|
|
|
|
int retval = 0;
|
2000-01-04 02:33:54 +00:00
|
|
|
|
int ch, change = 0, verbose = 0, force = 0, listed = 0;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
|
while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
|
1996-10-16 18:16:22 +00:00
|
|
|
|
switch (ch) {
|
1998-04-21 02:31:09 +00:00
|
|
|
|
case 'f':
|
2000-04-18 02:39:26 +00:00
|
|
|
|
if (change)
|
2001-02-06 12:05:58 +00:00
|
|
|
|
errx(1, "f option incompatible with t option");
|
1998-04-21 02:31:09 +00:00
|
|
|
|
force = 1;
|
2000-04-18 02:39:26 +00:00
|
|
|
|
type = atoi(optarg);
|
|
|
|
|
if (errno == ERANGE || type < 0 || type > 255) {
|
|
|
|
|
warnx("invalid argument to option f: %s",
|
|
|
|
|
optarg);
|
|
|
|
|
usage();
|
|
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
|
break;
|
2000-01-04 02:33:54 +00:00
|
|
|
|
case 'l':
|
|
|
|
|
printelftypes();
|
|
|
|
|
listed = 1;
|
|
|
|
|
break;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
case 'v':
|
|
|
|
|
verbose = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 't':
|
2000-04-18 02:39:26 +00:00
|
|
|
|
if (force)
|
2001-02-06 12:05:58 +00:00
|
|
|
|
errx(1, "t option incompatible with f option");
|
1996-10-16 18:16:22 +00:00
|
|
|
|
change = 1;
|
2000-04-18 02:39:26 +00:00
|
|
|
|
strtype = optarg;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
usage();
|
|
|
|
|
}
|
|
|
|
|
argc -= optind;
|
|
|
|
|
argv += optind;
|
2000-01-04 02:33:54 +00:00
|
|
|
|
if (!argc) {
|
|
|
|
|
if (listed)
|
|
|
|
|
exit(0);
|
|
|
|
|
else {
|
|
|
|
|
warnx("no file(s) specified");
|
|
|
|
|
usage();
|
|
|
|
|
}
|
|
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
|
if (!force && (type = elftype(strtype)) == -1) {
|
|
|
|
|
warnx("invalid ELF type '%s'", strtype);
|
2000-01-04 02:33:54 +00:00
|
|
|
|
printelftypes();
|
|
|
|
|
usage();
|
|
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
|
|
1996-10-16 18:16:22 +00:00
|
|
|
|
while (argc) {
|
|
|
|
|
int fd;
|
1997-05-21 23:07:31 +00:00
|
|
|
|
char buffer[EI_NIDENT];
|
1996-10-16 18:16:22 +00:00
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
|
if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
|
1997-08-23 15:51:14 +00:00
|
|
|
|
warn("error opening file %s", argv[0]);
|
1997-02-09 20:35:54 +00:00
|
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
1997-05-21 23:07:31 +00:00
|
|
|
|
if (read(fd, buffer, EI_NIDENT) < EI_NIDENT) {
|
1997-06-23 06:47:12 +00:00
|
|
|
|
warnx("file '%s' too short", argv[0]);
|
1997-02-09 20:35:54 +00:00
|
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
if (buffer[0] != ELFMAG0 || buffer[1] != ELFMAG1 ||
|
|
|
|
|
buffer[2] != ELFMAG2 || buffer[3] != ELFMAG3) {
|
1997-06-23 06:47:12 +00:00
|
|
|
|
warnx("file '%s' is not ELF format", argv[0]);
|
1997-02-09 20:35:54 +00:00
|
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
goto fail;
|
1998-04-21 02:31:09 +00:00
|
|
|
|
}
|
2000-04-18 02:39:26 +00:00
|
|
|
|
if (!change && !force) {
|
|
|
|
|
fprintf(stdout,
|
|
|
|
|
"File '%s' is of brand '%s' (%u).\n",
|
|
|
|
|
argv[0], iselftype(buffer[EI_OSABI]),
|
|
|
|
|
buffer[EI_OSABI]);
|
|
|
|
|
if (!iselftype(type)) {
|
|
|
|
|
warnx("ELF ABI Brand '%u' is unknown",
|
|
|
|
|
type);
|
|
|
|
|
printelftypes();
|
1996-10-16 18:16:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
2000-04-18 02:39:26 +00:00
|
|
|
|
buffer[EI_OSABI] = type;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
lseek(fd, 0, SEEK_SET);
|
1997-05-21 23:07:31 +00:00
|
|
|
|
if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
|
2000-04-18 02:39:26 +00:00
|
|
|
|
warn("error writing %s %d", argv[0], fd);
|
1997-06-23 06:47:12 +00:00
|
|
|
|
retval = 1;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
goto fail;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fail:
|
2000-07-02 03:34:08 +00:00
|
|
|
|
close(fd);
|
1996-10-16 18:16:22 +00:00
|
|
|
|
argc--;
|
|
|
|
|
argv++;
|
|
|
|
|
}
|
1997-02-09 20:35:54 +00:00
|
|
|
|
|
|
|
|
|
return retval;
|
1996-10-16 18:16:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-06-23 06:47:12 +00:00
|
|
|
|
static void
|
2001-12-03 00:00:22 +00:00
|
|
|
|
usage(void)
|
1996-10-16 18:16:22 +00:00
|
|
|
|
{
|
2000-04-18 02:39:26 +00:00
|
|
|
|
fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
|
1997-02-09 20:35:54 +00:00
|
|
|
|
exit(1);
|
1996-10-16 18:16:22 +00:00
|
|
|
|
}
|
1998-04-21 02:31:09 +00:00
|
|
|
|
|
2000-04-18 02:39:26 +00:00
|
|
|
|
static const char *
|
2001-06-22 21:43:51 +00:00
|
|
|
|
iselftype(int etype)
|
2000-04-18 02:39:26 +00:00
|
|
|
|
{
|
2001-06-22 21:43:51 +00:00
|
|
|
|
size_t elfwalk;
|
2000-04-18 02:39:26 +00:00
|
|
|
|
|
|
|
|
|
for (elfwalk = 0;
|
|
|
|
|
elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
|
|
|
|
|
elfwalk++)
|
2001-06-22 21:43:51 +00:00
|
|
|
|
if (etype == elftypes[elfwalk].value)
|
2000-04-18 02:39:26 +00:00
|
|
|
|
return elftypes[elfwalk].str;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2000-01-04 02:33:54 +00:00
|
|
|
|
|
1998-04-21 02:44:12 +00:00
|
|
|
|
static int
|
2000-04-18 02:39:26 +00:00
|
|
|
|
elftype(const char *elfstrtype)
|
2000-01-04 02:33:54 +00:00
|
|
|
|
{
|
2001-06-22 21:43:51 +00:00
|
|
|
|
size_t elfwalk;
|
1998-04-21 02:31:09 +00:00
|
|
|
|
|
|
|
|
|
for (elfwalk = 0;
|
|
|
|
|
elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
|
|
|
|
|
elfwalk++)
|
2000-04-18 02:39:26 +00:00
|
|
|
|
if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
|
|
|
|
|
return elftypes[elfwalk].value;
|
|
|
|
|
return -1;
|
1998-04-21 02:31:09 +00:00
|
|
|
|
}
|
2000-01-04 02:33:54 +00:00
|
|
|
|
|
|
|
|
|
static void
|
2001-12-03 00:00:22 +00:00
|
|
|
|
printelftypes(void)
|
2000-01-04 02:33:54 +00:00
|
|
|
|
{
|
2001-06-22 21:43:51 +00:00
|
|
|
|
size_t elfwalk;
|
2000-01-04 02:33:54 +00:00
|
|
|
|
|
|
|
|
|
fprintf(stderr, "known ELF types are: ");
|
|
|
|
|
for (elfwalk = 0;
|
|
|
|
|
elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
|
|
|
|
|
elfwalk++)
|
2000-04-18 02:39:26 +00:00
|
|
|
|
fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
|
|
|
|
|
elftypes[elfwalk].value);
|
2000-01-04 02:33:54 +00:00
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
}
|