summaryrefslogtreecommitdiffstats
path: root/usr.bin/brandelf/brandelf.c
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>2000-04-18 02:39:26 +0000
committerobrien <obrien@FreeBSD.org>2000-04-18 02:39:26 +0000
commit0eac6bbc676f298cf518321aa0d24cc001c69611 (patch)
tree0215bcb520740db905cae767cb8c981d2e3f8a50 /usr.bin/brandelf/brandelf.c
parent2e1592d902d16ed9876e30509b0d925e1a30e2c7 (diff)
downloadFreeBSD-src-0eac6bbc676f298cf518321aa0d24cc001c69611.zip
FreeBSD-src-0eac6bbc676f298cf518321aa0d24cc001c69611.tar.gz
Change our ELF binary branding to something more acceptable to the Binutils
maintainers. After we established our branding method of writing upto 8 characters of the OS name into the ELF header in the padding; the Binutils maintainers and/or SCO (as USL) decided that instead the ELF header should grow two new fields -- EI_OSABI and EI_ABIVERSION. Each of these are an 8-bit unsigned integer. SCO has assigned official values for the EI_OSABI field. In addition to this, the Binutils maintainers and NetBSD decided that a better ELF branding method was to include ABI information in a ".note" ELF section. With this set of changes, we will now create ELF binaries branded using both "official" methods. Due to the complexity of adding a section to a binary, binaries branded with ``brandelf'' will only brand using the EI_OSABI method. Also due to the complexity of pulling a section out of an ELF file vs. poking around in the ELF header, our image activator only looks at the EI_OSABI header field. Note that a new kernel can still properly load old binaries except for Linux static binaries branded in our old method. * * For a short period of time, ``ld'' will also brand ELF binaries * using our old method. This is so people can still use kernel.old * with a new world. This support will be removed before 5.0-RELEASE, * and may not last anywhere upto the actual release. My expiration * time for this is about 6mo. *
Diffstat (limited to 'usr.bin/brandelf/brandelf.c')
-rw-r--r--usr.bin/brandelf/brandelf.c94
1 files changed, 61 insertions, 33 deletions
diff --git a/usr.bin/brandelf/brandelf.c b/usr.bin/brandelf/brandelf.c
index 65b473d..7993bf9 100644
--- a/usr.bin/brandelf/brandelf.c
+++ b/usr.bin/brandelf/brandelf.c
@@ -34,24 +34,46 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <sys/errno.h>
#include <err.h>
-static int iselftype(const char *);
+static int elftype(const char *);
+static const char *iselftype(int);
static void printelftypes(void);
static void usage __P((void));
+struct ELFtypes {
+ const char *str;
+ int value;
+};
+/* XXX - any more types? */
+static struct ELFtypes elftypes[] = {
+ { "FreeBSD", ELFOSABI_FREEBSD },
+ { "SVR4", ELFOSABI_SYSV },
+ { "Linux", ELFOSABI_LINUX }
+};
+
int
main(int argc, char **argv)
{
- const char *type = "FreeBSD";
+ const char *strtype = "FreeBSD";
+ int type = ELFOSABI_FREEBSD;
int retval = 0;
int ch, change = 0, verbose = 0, force = 0, listed = 0;
- while ((ch = getopt(argc, argv, "flt:v")) != -1)
+ while ((ch = getopt(argc, argv, "f:lt:v")) != -1)
switch (ch) {
case 'f':
+ if (change)
+ errx(1, "f option incompatable with t option");
force = 1;
+ type = atoi(optarg);
+ if (errno == ERANGE || type < 0 || type > 255) {
+ warnx("invalid argument to option f: %s",
+ optarg);
+ usage();
+ }
break;
case 'l':
printelftypes();
@@ -61,8 +83,10 @@ main(int argc, char **argv)
verbose = 1;
break;
case 't':
+ if (force)
+ errx(1, "t option incompatable with f option");
change = 1;
- type = optarg;
+ strtype = optarg;
break;
default:
usage();
@@ -78,8 +102,8 @@ main(int argc, char **argv)
}
}
- if (!force && !iselftype(type)) {
- warnx("invalid ELF type '%s'", type);
+ if (!force && (type = elftype(strtype)) == -1) {
+ warnx("invalid ELF type '%s'", strtype);
printelftypes();
usage();
}
@@ -87,9 +111,8 @@ main(int argc, char **argv)
while (argc) {
int fd;
char buffer[EI_NIDENT];
- char string[(EI_NIDENT-EI_BRAND)+1];
- if ((fd = open(argv[0], change? O_RDWR: O_RDONLY, 0)) < 0) {
+ if ((fd = open(argv[0], change || force ? O_RDWR : O_RDONLY, 0)) < 0) {
warn("error opening file %s", argv[0]);
retval = 1;
goto fail;
@@ -105,28 +128,22 @@ main(int argc, char **argv)
retval = 1;
goto fail;
}
- if (!change) {
- bzero(string, sizeof(string));
- strncpy(string, &buffer[EI_BRAND], EI_NIDENT-EI_BRAND);
- if (strlen(string)) {
- fprintf(stdout,
- "File '%s' is of brand '%s'.\n",
- argv[0], string);
- if (!force && !iselftype(string)) {
- warnx("Brand '%s' is unknown",
- string);
- printelftypes();
- }
+ 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();
}
- else
- fprintf(stdout, "File '%s' has no branding.\n",
- argv[0]);
}
else {
- strncpy(&buffer[EI_BRAND], type, EI_NIDENT-EI_BRAND);
+ buffer[EI_OSABI] = type;
lseek(fd, 0, SEEK_SET);
if (write(fd, buffer, EI_NIDENT) != EI_NIDENT) {
- warnx("error writing %s", argv[0]);
+ warn("error writing %s %d", argv[0], fd);
retval = 1;
goto fail;
}
@@ -142,24 +159,34 @@ fail:
static void
usage()
{
- fprintf(stderr, "usage: brandelf [-f] [-v] [-l] [-t string] file ...\n");
+fprintf(stderr, "usage: brandelf [-f ELF ABI number] [-v] [-l] [-t string] file ...\n");
exit(1);
}
-/* XXX - any more types? */
-static const char *elftypes[] = { "FreeBSD", "Linux", "SVR4" };
+static const char *
+iselftype(int elftype)
+{
+ int elfwalk;
+
+ for (elfwalk = 0;
+ elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
+ elfwalk++)
+ if (elftype == elftypes[elfwalk].value)
+ return elftypes[elfwalk].str;
+ return 0;
+}
static int
-iselftype(const char *elftype)
+elftype(const char *elfstrtype)
{
int elfwalk;
for (elfwalk = 0;
elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
elfwalk++)
- if (strcmp(elftype, elftypes[elfwalk]) == 0)
- return 1;
- return 0;
+ if (strcmp(elfstrtype, elftypes[elfwalk].str) == 0)
+ return elftypes[elfwalk].value;
+ return -1;
}
static void
@@ -171,6 +198,7 @@ printelftypes()
for (elfwalk = 0;
elfwalk < sizeof(elftypes)/sizeof(elftypes[0]);
elfwalk++)
- fprintf(stderr, "%s ", elftypes[elfwalk]);
+ fprintf(stderr, "%s(%u) ", elftypes[elfwalk].str,
+ elftypes[elfwalk].value);
fprintf(stderr, "\n");
}
OpenPOWER on IntegriCloud