public inbox for gentoo-commits@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-03 23:19 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-03 23:19 UTC (permalink / raw
  To: gentoo-commits

commit:     a66ab9b88abf14b353a0720791e055bd901d1c6f
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Sat Jan  3 23:20:03 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Sat Jan  3 23:20:03 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=a66ab9b8

misc/elf-abi: read arch:abi:endian from an ELF, bug #534206

---
 misc/elf-abi/.gitignore |   2 +
 misc/elf-abi/Makefile   |  18 +++
 misc/elf-abi/elf-abi.c  | 314 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 334 insertions(+)

diff --git a/misc/elf-abi/.gitignore b/misc/elf-abi/.gitignore
new file mode 100644
index 0000000..7ec9e50
--- /dev/null
+++ b/misc/elf-abi/.gitignore
@@ -0,0 +1,2 @@
+elf-abi
+!Makefile

diff --git a/misc/elf-abi/Makefile b/misc/elf-abi/Makefile
new file mode 100644
index 0000000..8a75d55
--- /dev/null
+++ b/misc/elf-abi/Makefile
@@ -0,0 +1,18 @@
+CFLAGS ?= -O2 -pipe -g
+CFLAGS += -Wall
+PWD = $$(pwd)
+
+all: elf-abi
+
+elf-abi: elf-abi.c
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $< $(LDLIBS)
+
+PREFIX = /usr
+BINDIR = $(PREFIX)/bin
+install: elf-abi
+	install -m 755 -D elf-abi $(DESTDIR)$(BINDIR)/$<
+
+clean:
+	rm -f *.o *~ elf-abi
+
+.PHONY: all clean install

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
new file mode 100644
index 0000000..589d458
--- /dev/null
+++ b/misc/elf-abi/elf-abi.c
@@ -0,0 +1,314 @@
+/* Copyright 2015 Gentoo Foundation
+ * Distributed under the terms of the GNU General Public License v2
+ *
+ * Compute a multilib ABI identifier, as discussed here:
+ *
+ * 	https://bugs.gentoo.org/show_bug.cgi?id=534206
+ *
+ * Supported identifiers:
+ *
+ *	alpha_32
+ *	arm_{oabi32,32,64}
+ *	hppa_32,64
+ *	ia_64
+ *	m68k_32
+ *	mips_{n32,n64,o32}
+ *	ppc_{32,64}
+ *	s390_32
+ *	sh_32
+ *	sparc_{32,64}
+ *	x86_{32,x32,64}
+ *
+ * NOTES:
+ *
+ * * The ABIs referenced by some of the above *_32 and *_64 identifiers
+ *   may be imaginary, but they are listed anyway, since the goal is to
+ *   establish a naming convention that is as consistent and uniform as
+ *   possible.
+ *
+ * * The Elf header's e_ident[EI_OSABI] byte is completely ignored,
+ *   since OS-independence is one of the goals. The assumption is that,
+ *   for given installation, we are only interested in tracking multilib
+ *   ABIs for a single OS.
+ *
+ * REFERENCE:
+ *
+ *   http://www.sco.com/developers/gabi/2000-07-17/ch4.eheader.html
+ *
+ *
+ * Copyright 2015 Anthony G. Basile - <blueness@gentoo.org>
+ * Copyright 2015 Zac Medico - <zmedico@gentoo.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include <err.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <endian.h>
+
+/* We steal this from <elf.h> but don't include it so as to not increase our dependancies. */
+#define	ELFMAG		"\177ELF"
+
+#define ELFCLASS32	1		/* 32-bit objects */
+#define ELFCLASS64	2		/* 64-bit objects */
+#define ELFDATA2LSB	1		/* 2's complement, little endian */
+#define ELFDATA2MSB	2		/* 2's complement, big endian */
+
+#define EM_SPARC	 2		/* SUN SPARC */
+#define EM_386		 3		/* Intel 80386 */
+#define EM_68K		 4		/* Motorola m68k family */
+#define EM_MIPS		 8		/* MIPS R3000 big-endian */
+#define EM_MIPS_RS3_LE	10		/* MIPS R3000 little-endian */
+#define EM_PARISC	15		/* HPPA */
+#define EM_SPARC32PLUS	18		/* Sun's "v8plus" */
+#define EM_PPC		20		/* PowerPC */
+#define EM_PPC64	21		/* PowerPC 64-bit */
+#define EM_S390		22		/* IBM S390 */
+#define EM_ARM		40		/* ARM */
+#define EM_FAKE_ALPHA	41		/* Digital Alpha */
+#define EM_SH		42		/* Hitachi SH */
+#define EM_SPARCV9	43		/* SPARC v9 64-bit */
+#define EM_IA_64	50		/* Intel Merced */
+#define EM_X86_64	62		/* AMD x86-64 architecture */
+#define EM_AARCH64	183		/* ARM AARCH64 */
+#define EM_ALPHA	0x9026
+
+#define EF_MIPS_ABI2		32
+#define EF_MIPS_ABI_ON32	64
+/*
+E_MIPS_ABI_O32     = 0x00001000
+E_MIPS_ABI_O64     = 0x00002000
+E_MIPS_ABI_EABI32  = 0x00003000
+E_MIPS_ABI_EABI64  = 0x00004000
+*/
+
+#define EF_ARM_NEW_ABI		0x80
+#define EF_ARM_OLD_ABI		0x100
+
+
+/*
+def compute_suffix_mips(elf_header):
+	name = None
+	mips_abi = elf_header.e_flags & EF_MIPS_ABI
+	if mips_abi:
+		if mips_abi == E_MIPS_ABI_O32:
+			name = "o32"
+		elif mips_abi == E_MIPS_ABI_O64:
+			name = "o64"
+		elif mips_abi == E_MIPS_ABI_EABI32:
+			name = "eabi32"
+		elif mips_abi == E_MIPS_ABI_EABI64:
+			name = "eabi64"
+
+	elif elf_header.e_flags & EF_MIPS_ABI2:
+		name = "n32"
+	elif elf_header.ei_class == ELFCLASS64:
+		name = "n64"
+
+	return name
+
+def compute_multilib_id(elf_header):
+	prefix = machine_prefix_map.get(elf_header.e_machine)
+	suffix = None
+
+	if prefix == "mips":
+		suffix = compute_suffix_mips(elf_header)
+	elif elf_header.ei_class == ELFCLASS64:
+		suffix = "64"
+	elif elf_header.ei_class == ELFCLASS32:
+		if elf_header.e_machine == EM_X86_64:
+			suffix = "x32"
+		else:
+			suffix = "32"
+
+	if prefix is None or suffix is None:
+		multilib_id = None
+	else:
+		multilib_id = "%s_%s" % (prefix, suffix)
+
+	return multilib_id
+*/
+
+
+#define bswap_16(x) ((uint16_t)((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
+
+/*
+#define bswap_32(x) \
+     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
+      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
+*/
+
+#define MAX_IDENT	32
+
+int
+main(int argc, char* argv[])
+{
+	char ident[MAX_IDENT];		/* The Gentoo standard arch_abi identifier.		*/
+	char *arch;			/* The Gentoo architecture identifier.			*/
+	int fd;
+	struct stat s;
+	char magic[4];			/* magic number at the begining of the file		*/
+	uint8_t ei_class, width;	/* width = 8 bytes for 32-bits, 16 bytes for 64-bits.	*/
+	uint8_t ei_data, endian;	/* endian = 0 for little, 1 for big endian.		*/
+	uint16_t e_machine;		/* Size is Elf32_Half or Elf64_Half.  Both are 2 bytes.	*/
+	uint32_t e_flags;		/* Size is Elf32_Word or Elf64_Word.  Both are 4 bytes.	*/
+	uint64_t e_flags_offset;	/* Wide enough for either 32 or 64 bits.		*/
+
+	/* Is the second parameter a regular file? */
+	if (argc != 2)
+		errx(1, "Usage: %s <file>", argv[0]);
+	if (stat(argv[1], &s) == -1)
+		errx(1, "%s does not exist", argv[1]);
+	if (!S_ISREG(s.st_mode))
+		errx(1, "%s is not a regular file", argv[1]);
+
+	/* Can we read it and is it an ELF object? */
+	if ((fd = open(argv[1], O_RDONLY)) == -1)
+		err(1, "failed to open %s", argv[1]);
+	if (read(fd, magic, 4) == -1)
+		err(1, "read() magic failed");
+	if (strncmp(magic, ELFMAG, 4) != 0)
+		errx(1, "%s is not an ELF object", argv[1]);
+
+
+	/* 32 or 64 bits? */
+	if (read(fd, &ei_class, 1) == -1)
+		err(1, "read() ei_class failed");
+	switch (ei_class) {
+		case ELFCLASS32:
+			width = 4;
+			printf("32 bit\n");
+			break;
+		case ELFCLASS64:
+			width = 8;
+			printf("64 bit\n");
+			break;
+		default:
+			width = 0;
+			printf("Unknown bit\n");
+	}
+
+	/* Little or Big Endian */
+	if (read(fd, &ei_data, 1) == -1)
+		err(1, "read() ei_data failed");
+	switch (ei_data) {
+		case ELFDATA2LSB:
+			endian = 0;
+			printf("Little Endian\n");
+			break;
+		case ELFDATA2MSB:
+			endian = 1;
+			printf("Big Endian\n");
+			break;
+		default:
+			endian = -1;
+			printf("Unknown Endian\n");
+	}
+
+	/* seek to e_macine = 16 bytes (e_ident[])) + 2 bytes (e_type which is Elf32_Half/Elf64_Half) */
+	if (lseek(fd, 18, SEEK_SET) == -1)
+		err(1, "lseek() e_machine failed");
+
+	/* What is the arch? */
+	if (read(fd, &e_machine, 2) == -1)
+		err(1, "read() e_machine failed");
+	//if (endian == 0)
+	//	e_machine = bswap_16(e_machine);
+	printf("Machine =%d\n", e_machine);
+
+	switch(e_machine) {
+		case EM_ALPHA:
+		case EM_FAKE_ALPHA:
+			arch = "alpha";
+			break;
+		case EM_X86_64:
+			arch = "amd64";
+			break;
+		case EM_ARM:
+			arch = "arm";
+			break;
+		case EM_AARCH64:
+			arch = "arm64";
+			break;
+		case EM_68K:
+			arch = "m68k";
+			break;
+		case EM_MIPS_RS3_LE:
+		case EM_MIPS:
+			arch = "mips";
+			break;
+		case EM_IA_64:
+			arch = "ia64";
+			break;
+		case EM_PARISC:
+			arch = "hppa";
+			break;
+		case EM_PPC:
+			arch = "ppc";
+			break;
+		case EM_PPC64:
+			arch = "ppc64";
+			break;
+		case EM_S390:
+			arch = "s390";
+			break;
+		case EM_SH:
+			arch = "sh";
+			break;
+		case EM_SPARC32PLUS:
+		case EM_SPARCV9:
+		case EM_SPARC:
+			arch = "sparc";
+			break;
+		case EM_386:
+			arch = "x86";
+			break;
+		default:
+			arch = "unknown";
+	}
+
+	printf("%s\n", arch);
+
+
+	/*
+	e_data_offset = 
+	if (lseek(fd, 18, SEEK_SET) == -1)
+		err(1, "lseek() e_machine failed");
+	if (read(fd, &e_flags, 2) == -1)
+		err(1, "read() e_machine failed");
+	*/
+
+	memset(ident, 0, MAX_IDENT);
+
+	close(fd);
+	exit(EXIT_SUCCESS);
+}
+
+/*
+	# E_ENTRY + 3 * sizeof(uintN)
+	e_flags_offset = E_ENTRY + 3 * width // 8
+	f.seek(e_flags_offset)
+	e_flags = uint32(f.read(4))
+
+	return _elf_header(ei_class, ei_data, e_machine, e_flags)
+
+	multilib_id = compute_multilib_id(elf_header)
+*/


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-03 23:49 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-03 23:49 UTC (permalink / raw
  To: gentoo-commits

commit:     244969d6eaa011c87bcba5d1f982d546de715f08
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Sat Jan  3 23:50:11 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Sat Jan  3 23:50:11 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=244969d6

misc/elf-abi: read() gets the correct endian order

---
 misc/elf-abi/elf-abi.c | 37 ++++++++++---------------------------
 1 file changed, 10 insertions(+), 27 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index 589d458..32e3851 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -103,7 +103,6 @@ E_MIPS_ABI_EABI64  = 0x00004000
 #define EF_ARM_NEW_ABI		0x80
 #define EF_ARM_OLD_ABI		0x100
 
-
 /*
 def compute_suffix_mips(elf_header):
 	name = None
@@ -147,15 +146,6 @@ def compute_multilib_id(elf_header):
 	return multilib_id
 */
 
-
-#define bswap_16(x) ((uint16_t)((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
-
-/*
-#define bswap_32(x) \
-     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
-      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
-*/
-
 #define MAX_IDENT	32
 
 int
@@ -206,7 +196,7 @@ main(int argc, char* argv[])
 			printf("Unknown bit\n");
 	}
 
-	/* Little or Big Endian */
+	/* Little or Big Endian? */
 	if (read(fd, &ei_data, 1) == -1)
 		err(1, "read() ei_data failed");
 	switch (ei_data) {
@@ -223,17 +213,13 @@ main(int argc, char* argv[])
 			printf("Unknown Endian\n");
 	}
 
-	/* seek to e_macine = 16 bytes (e_ident[])) + 2 bytes (e_type which is Elf32_Half/Elf64_Half) */
+	/* Seek to e_macine = 16 bytes (e_ident[])) + 2 bytes (e_type which is Elf32_Half/Elf64_Half) */
 	if (lseek(fd, 18, SEEK_SET) == -1)
 		err(1, "lseek() e_machine failed");
 
 	/* What is the arch? */
 	if (read(fd, &e_machine, 2) == -1)
 		err(1, "read() e_machine failed");
-	//if (endian == 0)
-	//	e_machine = bswap_16(e_machine);
-	printf("Machine =%d\n", e_machine);
-
 	switch(e_machine) {
 		case EM_ALPHA:
 		case EM_FAKE_ALPHA:
@@ -284,10 +270,8 @@ main(int argc, char* argv[])
 		default:
 			arch = "unknown";
 	}
-
 	printf("%s\n", arch);
 
-
 	/*
 	e_data_offset = 
 	if (lseek(fd, 18, SEEK_SET) == -1)
@@ -295,14 +279,7 @@ main(int argc, char* argv[])
 	if (read(fd, &e_flags, 2) == -1)
 		err(1, "read() e_machine failed");
 	*/
-
-	memset(ident, 0, MAX_IDENT);
-
-	close(fd);
-	exit(EXIT_SUCCESS);
-}
-
-/*
+	/*
 	# E_ENTRY + 3 * sizeof(uintN)
 	e_flags_offset = E_ENTRY + 3 * width // 8
 	f.seek(e_flags_offset)
@@ -311,4 +288,10 @@ main(int argc, char* argv[])
 	return _elf_header(ei_class, ei_data, e_machine, e_flags)
 
 	multilib_id = compute_multilib_id(elf_header)
-*/
+	*/
+
+	memset(ident, 0, MAX_IDENT);
+
+	close(fd);
+	exit(EXIT_SUCCESS);
+}


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-03 23:53 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-03 23:53 UTC (permalink / raw
  To: gentoo-commits

commit:     55440d11bbf3fd36388ab9135d640f66bd4d1ce9
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Sat Jan  3 23:50:11 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Sat Jan  3 23:53:43 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=55440d11

misc/elf-abi: read() gets the correct endian order

---
 misc/elf-abi/elf-abi.c | 38 ++++++++++----------------------------
 1 file changed, 10 insertions(+), 28 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index 589d458..aa383c4 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -62,7 +62,6 @@
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <endian.h>
 
 /* We steal this from <elf.h> but don't include it so as to not increase our dependancies. */
 #define	ELFMAG		"\177ELF"
@@ -103,7 +102,6 @@ E_MIPS_ABI_EABI64  = 0x00004000
 #define EF_ARM_NEW_ABI		0x80
 #define EF_ARM_OLD_ABI		0x100
 
-
 /*
 def compute_suffix_mips(elf_header):
 	name = None
@@ -147,15 +145,6 @@ def compute_multilib_id(elf_header):
 	return multilib_id
 */
 
-
-#define bswap_16(x) ((uint16_t)((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
-
-/*
-#define bswap_32(x) \
-     ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
-      (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
-*/
-
 #define MAX_IDENT	32
 
 int
@@ -206,7 +195,7 @@ main(int argc, char* argv[])
 			printf("Unknown bit\n");
 	}
 
-	/* Little or Big Endian */
+	/* Little or Big Endian? */
 	if (read(fd, &ei_data, 1) == -1)
 		err(1, "read() ei_data failed");
 	switch (ei_data) {
@@ -223,17 +212,13 @@ main(int argc, char* argv[])
 			printf("Unknown Endian\n");
 	}
 
-	/* seek to e_macine = 16 bytes (e_ident[])) + 2 bytes (e_type which is Elf32_Half/Elf64_Half) */
+	/* Seek to e_macine = 16 bytes (e_ident[])) + 2 bytes (e_type which is Elf32_Half/Elf64_Half) */
 	if (lseek(fd, 18, SEEK_SET) == -1)
 		err(1, "lseek() e_machine failed");
 
 	/* What is the arch? */
 	if (read(fd, &e_machine, 2) == -1)
 		err(1, "read() e_machine failed");
-	//if (endian == 0)
-	//	e_machine = bswap_16(e_machine);
-	printf("Machine =%d\n", e_machine);
-
 	switch(e_machine) {
 		case EM_ALPHA:
 		case EM_FAKE_ALPHA:
@@ -284,10 +269,8 @@ main(int argc, char* argv[])
 		default:
 			arch = "unknown";
 	}
-
 	printf("%s\n", arch);
 
-
 	/*
 	e_data_offset = 
 	if (lseek(fd, 18, SEEK_SET) == -1)
@@ -295,14 +278,7 @@ main(int argc, char* argv[])
 	if (read(fd, &e_flags, 2) == -1)
 		err(1, "read() e_machine failed");
 	*/
-
-	memset(ident, 0, MAX_IDENT);
-
-	close(fd);
-	exit(EXIT_SUCCESS);
-}
-
-/*
+	/*
 	# E_ENTRY + 3 * sizeof(uintN)
 	e_flags_offset = E_ENTRY + 3 * width // 8
 	f.seek(e_flags_offset)
@@ -311,4 +287,10 @@ main(int argc, char* argv[])
 	return _elf_header(ei_class, ei_data, e_machine, e_flags)
 
 	multilib_id = compute_multilib_id(elf_header)
-*/
+	*/
+
+	memset(ident, 0, MAX_IDENT);
+
+	close(fd);
+	exit(EXIT_SUCCESS);
+}


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-05 16:23 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-05 16:23 UTC (permalink / raw
  To: gentoo-commits

commit:     c7217fa43806228ff46af1e21242ce4dd82a06a6
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Mon Jan  5 16:24:44 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Mon Jan  5 16:24:44 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=c7217fa4

misc/elf-abi: code cleanup

---
 misc/elf-abi/elf-abi.c | 281 +++++++++++++++++++++++--------------------------
 1 file changed, 130 insertions(+), 151 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index aa383c4..6b3a9c2 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -7,15 +7,15 @@
  *
  * Supported identifiers:
  *
- *	alpha_32
- *	arm_{oabi32,32,64}
- *	hppa_32,64
- *	ia_64
- *	m68k_32
+ *	alpha_{32,64}
+ *	arm_{32,64}
+ *	hppa_{32,64}
+ *	ia_{32,64}
+ *	m68k_{32,64}
  *	mips_{n32,n64,o32}
  *	ppc_{32,64}
- *	s390_32
- *	sh_32
+ *	s390_{32,64}
+ *	sh_{32,64}
  *	sparc_{32,64}
  *	x86_{32,x32,64}
  *
@@ -35,7 +35,6 @@
  *
  *   http://www.sco.com/developers/gabi/2000-07-17/ch4.eheader.html
  *
- *
  * Copyright 2015 Anthony G. Basile - <blueness@gentoo.org>
  * Copyright 2015 Zac Medico - <zmedico@gentoo.org>
  *
@@ -102,64 +101,91 @@ E_MIPS_ABI_EABI64  = 0x00004000
 #define EF_ARM_NEW_ABI		0x80
 #define EF_ARM_OLD_ABI		0x100
 
-/*
-def compute_suffix_mips(elf_header):
-	name = None
-	mips_abi = elf_header.e_flags & EF_MIPS_ABI
-	if mips_abi:
-		if mips_abi == E_MIPS_ABI_O32:
-			name = "o32"
-		elif mips_abi == E_MIPS_ABI_O64:
-			name = "o64"
-		elif mips_abi == E_MIPS_ABI_EABI32:
-			name = "eabi32"
-		elif mips_abi == E_MIPS_ABI_EABI64:
-			name = "eabi64"
-
-	elif elf_header.e_flags & EF_MIPS_ABI2:
-		name = "n32"
-	elif elf_header.ei_class == ELFCLASS64:
-		name = "n64"
 
-	return name
-
-def compute_multilib_id(elf_header):
-	prefix = machine_prefix_map.get(elf_header.e_machine)
-	suffix = None
+int
+get_wordsize(uint8_t ei_class)
+{
+	switch (ei_class) {
+		case ELFCLASS32:
+			return 32;
+		case ELFCLASS64:
+			return 64;
+		default:
+			return 0;
+	}
+}
 
-	if prefix == "mips":
-		suffix = compute_suffix_mips(elf_header)
-	elif elf_header.ei_class == ELFCLASS64:
-		suffix = "64"
-	elif elf_header.ei_class == ELFCLASS32:
-		if elf_header.e_machine == EM_X86_64:
-			suffix = "x32"
-		else:
-			suffix = "32"
 
-	if prefix is None or suffix is None:
-		multilib_id = None
-	else:
-		multilib_id = "%s_%s" % (prefix, suffix)
+int
+get_endian(uint8_t ei_data)
+{
+	switch (ei_data) {
+		case ELFDATA2LSB:
+			return -1;
+		case ELFDATA2MSB:
+			return 1;
+		default:
+			return 0;
+	}
+}
 
-	return multilib_id
-*/
 
-#define MAX_IDENT	32
+char *
+get_abi(uint16_t e_machine, int width, uint32_t e_flags)
+{
+	switch(e_machine) {
+		case EM_ALPHA:
+		case EM_FAKE_ALPHA:
+			return "alpha";
+		case EM_X86_64:
+			return "amd64";
+		case EM_ARM:
+			return "arm";
+		case EM_AARCH64:
+			return "arm64";
+		case EM_68K:
+			return "m68k";
+		case EM_MIPS_RS3_LE:
+		case EM_MIPS:
+			return "mips";
+		case EM_IA_64:
+			return "ia64";
+		case EM_PARISC:
+			return "hppa";
+		case EM_PPC:
+			return "ppc";
+		case EM_PPC64:
+			return "ppc64";
+		case EM_S390:
+			return "s390";
+		case EM_SH:
+			return "sh";
+		case EM_SPARC32PLUS:
+		case EM_SPARCV9:
+		case EM_SPARC:
+			return "sparc";
+		case EM_386:
+			return "x86";
+		default:
+			return "unknown_arch";
+	}
+}
 
 int
 main(int argc, char* argv[])
 {
-	char ident[MAX_IDENT];		/* The Gentoo standard arch_abi identifier.		*/
-	char *arch;			/* The Gentoo architecture identifier.			*/
-	int fd;
-	struct stat s;
+	int width;			/* Machine word size.  Either 32 or 64 bits.		*/
+	int endian;			/* Endian, -1 = little, 1 = big				*/
+	char *abi;			/* Abi name from glibc's <bits/syscall.h>		*/
+
+	int fd;				/* file descriptor for opened Elf object.		*/
+	struct stat s;			/* stat on opened Elf object.				*/
 	char magic[4];			/* magic number at the begining of the file		*/
-	uint8_t ei_class, width;	/* width = 8 bytes for 32-bits, 16 bytes for 64-bits.	*/
-	uint8_t ei_data, endian;	/* endian = 0 for little, 1 for big endian.		*/
+	uint8_t ei_class;		/* ei_class is one byte of e_ident[]			*/
+	uint8_t ei_data;		/* ei_data is one byte of e_ident[]			*/
 	uint16_t e_machine;		/* Size is Elf32_Half or Elf64_Half.  Both are 2 bytes.	*/
 	uint32_t e_flags;		/* Size is Elf32_Word or Elf64_Word.  Both are 4 bytes.	*/
-	uint64_t e_flags_offset;	/* Wide enough for either 32 or 64 bits.		*/
+	uint64_t e_machine_offset, e_flags_offset;  /* Wide enough for either 32 or 64 bits.	*/
 
 	/* Is the second parameter a regular file? */
 	if (argc != 2)
@@ -177,120 +203,73 @@ main(int argc, char* argv[])
 	if (strncmp(magic, ELFMAG, 4) != 0)
 		errx(1, "%s is not an ELF object", argv[1]);
 
-
 	/* 32 or 64 bits? */
 	if (read(fd, &ei_class, 1) == -1)
 		err(1, "read() ei_class failed");
-	switch (ei_class) {
-		case ELFCLASS32:
-			width = 4;
-			printf("32 bit\n");
-			break;
-		case ELFCLASS64:
-			width = 8;
-			printf("64 bit\n");
-			break;
-		default:
-			width = 0;
-			printf("Unknown bit\n");
-	}
+	width = get_wordsize(ei_class);
 
 	/* Little or Big Endian? */
 	if (read(fd, &ei_data, 1) == -1)
 		err(1, "read() ei_data failed");
-	switch (ei_data) {
-		case ELFDATA2LSB:
-			endian = 0;
-			printf("Little Endian\n");
-			break;
-		case ELFDATA2MSB:
-			endian = 1;
-			printf("Big Endian\n");
-			break;
-		default:
-			endian = -1;
-			printf("Unknown Endian\n");
-	}
+	endian = get_endian(ei_data);
 
-	/* Seek to e_macine = 16 bytes (e_ident[])) + 2 bytes (e_type which is Elf32_Half/Elf64_Half) */
-	if (lseek(fd, 18, SEEK_SET) == -1)
-		err(1, "lseek() e_machine failed");
+	/*
+	All Elf files begin with the following Elf header:
+	unsigned char   e_ident[EI_NIDENT];	- 16 bytes
+	Elf32_Half      e_type;			- 2 bytes
+	Elf32_Half      e_machine;		- 2 bytes
+	Elf32_Word      e_version;		- 4 bytes
+	Elf32_Addr      e_entry;		- 4 bytes or 8 bytes for Elf64
+	Elf32_Off       e_phoff;		- 4 bytes or 8 bytes for Elf64
+	Elf32_Off       e_shoff;		- 4 bytes or 8 bytes for Elf64
+	Elf32_Word      e_flags;		- 4 bytes
+
+	Seek to e_machine = 18 bytes.
+	Seek to e_flags   = 36 bytes (Elf32) or 48 bytes (Elf64)
+	*/
+	e_machine_offset = 18;
+	if (width == 32)
+		e_flags_offset = 36;
+	else
+		e_flags_offset = 48;
 
-	/* What is the arch? */
+	/* What is the abi? */
+	if (lseek(fd, e_machine_offset, SEEK_SET) == -1)
+		err(1, "lseek() e_machine failed");
 	if (read(fd, &e_machine, 2) == -1)
 		err(1, "read() e_machine failed");
-	switch(e_machine) {
-		case EM_ALPHA:
-		case EM_FAKE_ALPHA:
-			arch = "alpha";
-			break;
-		case EM_X86_64:
-			arch = "amd64";
-			break;
-		case EM_ARM:
-			arch = "arm";
-			break;
-		case EM_AARCH64:
-			arch = "arm64";
-			break;
-		case EM_68K:
-			arch = "m68k";
-			break;
-		case EM_MIPS_RS3_LE:
-		case EM_MIPS:
-			arch = "mips";
-			break;
-		case EM_IA_64:
-			arch = "ia64";
-			break;
-		case EM_PARISC:
-			arch = "hppa";
-			break;
-		case EM_PPC:
-			arch = "ppc";
-			break;
-		case EM_PPC64:
-			arch = "ppc64";
-			break;
-		case EM_S390:
-			arch = "s390";
-			break;
-		case EM_SH:
-			arch = "sh";
-			break;
-		case EM_SPARC32PLUS:
-		case EM_SPARCV9:
-		case EM_SPARC:
-			arch = "sparc";
-			break;
-		case EM_386:
-			arch = "x86";
-			break;
-		default:
-			arch = "unknown";
-	}
-	printf("%s\n", arch);
 
-	/*
-	e_data_offset = 
-	if (lseek(fd, 18, SEEK_SET) == -1)
-		err(1, "lseek() e_machine failed");
+	if (lseek(fd, e_flags_offset, SEEK_SET) == -1)
+		err(1, "lseek() e_flags failed");
 	if (read(fd, &e_flags, 2) == -1)
-		err(1, "read() e_machine failed");
-	*/
-	/*
-	# E_ENTRY + 3 * sizeof(uintN)
-	e_flags_offset = E_ENTRY + 3 * width // 8
-	f.seek(e_flags_offset)
-	e_flags = uint32(f.read(4))
+		err(1, "read() e_flags failed");
 
-	return _elf_header(ei_class, ei_data, e_machine, e_flags)
+	abi = get_abi(e_machine, width, e_flags);
 
-	multilib_id = compute_multilib_id(elf_header)
-	*/
+	printf("%s\n", abi);
 
-	memset(ident, 0, MAX_IDENT);
 
+/*
+	if (!strcmp(arch, "mips")) ;
+		switch (e_flags & EF_MIPS_ABI) {
+			case E_MIPS_ABI_O32:
+				abi = "o32";
+				break;
+
+		if mips_abi == E_MIPS_ABI_O32:
+			name = "o32"
+		elif mips_abi == E_MIPS_ABI_O64:
+			name = "o64"
+		elif mips_abi == E_MIPS_ABI_EABI32:
+			name = "eabi32"
+		elif mips_abi == E_MIPS_ABI_EABI64:
+			name = "eabi64"
+	elif elf_header.e_flags & EF_MIPS_ABI2:
+		name = "n32"
+	elif elf_header.ei_class == ELFCLASS64:
+		name = "n64"
+		}
+*/
 	close(fd);
 	exit(EXIT_SUCCESS);
 }


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-05 16:26 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-05 16:26 UTC (permalink / raw
  To: gentoo-commits

commit:     710cbb8d24db7bbc64af06921ba281b752e5ef99
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Mon Jan  5 16:27:26 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Mon Jan  5 16:27:26 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=710cbb8d

misc/elf-abi: remove endian code

---
 misc/elf-abi/elf-abi.c | 23 -----------------------
 1 file changed, 23 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index 6b3a9c2..0a271ba 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -67,8 +67,6 @@
 
 #define ELFCLASS32	1		/* 32-bit objects */
 #define ELFCLASS64	2		/* 64-bit objects */
-#define ELFDATA2LSB	1		/* 2's complement, little endian */
-#define ELFDATA2MSB	2		/* 2's complement, big endian */
 
 #define EM_SPARC	 2		/* SUN SPARC */
 #define EM_386		 3		/* Intel 80386 */
@@ -116,20 +114,6 @@ get_wordsize(uint8_t ei_class)
 }
 
 
-int
-get_endian(uint8_t ei_data)
-{
-	switch (ei_data) {
-		case ELFDATA2LSB:
-			return -1;
-		case ELFDATA2MSB:
-			return 1;
-		default:
-			return 0;
-	}
-}
-
-
 char *
 get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 {
@@ -175,14 +159,12 @@ int
 main(int argc, char* argv[])
 {
 	int width;			/* Machine word size.  Either 32 or 64 bits.		*/
-	int endian;			/* Endian, -1 = little, 1 = big				*/
 	char *abi;			/* Abi name from glibc's <bits/syscall.h>		*/
 
 	int fd;				/* file descriptor for opened Elf object.		*/
 	struct stat s;			/* stat on opened Elf object.				*/
 	char magic[4];			/* magic number at the begining of the file		*/
 	uint8_t ei_class;		/* ei_class is one byte of e_ident[]			*/
-	uint8_t ei_data;		/* ei_data is one byte of e_ident[]			*/
 	uint16_t e_machine;		/* Size is Elf32_Half or Elf64_Half.  Both are 2 bytes.	*/
 	uint32_t e_flags;		/* Size is Elf32_Word or Elf64_Word.  Both are 4 bytes.	*/
 	uint64_t e_machine_offset, e_flags_offset;  /* Wide enough for either 32 or 64 bits.	*/
@@ -208,11 +190,6 @@ main(int argc, char* argv[])
 		err(1, "read() ei_class failed");
 	width = get_wordsize(ei_class);
 
-	/* Little or Big Endian? */
-	if (read(fd, &ei_data, 1) == -1)
-		err(1, "read() ei_data failed");
-	endian = get_endian(ei_data);
-
 	/*
 	All Elf files begin with the following Elf header:
 	unsigned char   e_ident[EI_NIDENT];	- 16 bytes


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-06 15:13 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-06 15:13 UTC (permalink / raw
  To: gentoo-commits

commit:     c51a1996485f2836677d1a829e314e2a1fd85327
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Tue Jan  6 15:14:56 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Tue Jan  6 15:14:56 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=c51a1996

misc/elf-abi: add logic for mips abis

---
 misc/elf-abi/elf-abi.c | 147 ++++++++++++++++++++++++++++---------------------
 1 file changed, 85 insertions(+), 62 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index 0a271ba..fb53391 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -7,25 +7,21 @@
  *
  * Supported identifiers:
  *
- *	alpha_{32,64}
- *	arm_{32,64}
- *	hppa_{32,64}
- *	ia_{32,64}
- *	m68k_{32,64}
+ *	alpha_64
+ *	arm_{oabi,eabi}
+ *	arm_64
+ *	hppa_32
+ *	ia_64
+ *	m68k_32
  *	mips_{n32,n64,o32}
  *	ppc_{32,64}
  *	s390_{32,64}
- *	sh_{32,64}
+ *	sh_32
  *	sparc_{32,64}
  *	x86_{32,x32,64}
  *
  * NOTES:
  *
- * * The ABIs referenced by some of the above *_32 and *_64 identifiers
- *   may be imaginary, but they are listed anyway, since the goal is to
- *   establish a naming convention that is as consistent and uniform as
- *   possible.
- *
  * * The Elf header's e_ident[EI_OSABI] byte is completely ignored,
  *   since OS-independence is one of the goals. The assumption is that,
  *   for given installation, we are only interested in tracking multilib
@@ -85,19 +81,13 @@
 #define EM_IA_64	50		/* Intel Merced */
 #define EM_X86_64	62		/* AMD x86-64 architecture */
 #define EM_AARCH64	183		/* ARM AARCH64 */
-#define EM_ALPHA	0x9026
+#define EM_ALPHA	0x9026		/* Unofficial, but needed in Gentoo */
+#define EM_HPPA		0x0F00		/* Unofficial, but needed in Gentoo */
 
-#define EF_MIPS_ABI2		32
-#define EF_MIPS_ABI_ON32	64
-/*
-E_MIPS_ABI_O32     = 0x00001000
-E_MIPS_ABI_O64     = 0x00002000
-E_MIPS_ABI_EABI32  = 0x00003000
-E_MIPS_ABI_EABI64  = 0x00004000
-*/
+#define EF_MIPS_ABI		0x0000F000
+#define E_MIPS_ABI_O32		0x00001000
 
-#define EF_ARM_NEW_ABI		0x80
-#define EF_ARM_OLD_ABI		0x100
+#define EF_ARM_EABIMASK		0XFF000000
 
 
 int
@@ -117,44 +107,100 @@ get_wordsize(uint8_t ei_class)
 char *
 get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 {
+	/* The following arrives at the abstract ABI name by a process of elimination based on the assumption
+	 * that we are only interested in the ABIs supported in Gentoo.  If a new ABIs is added, you need to
+	 * rethink the logic to avoid false positives/negatives.
+	 */
 	switch(e_machine) {
+
+		/* alpha: We support only one 64-bit ABI. */
 		case EM_ALPHA:
 		case EM_FAKE_ALPHA:
-			return "alpha";
+			return "alpha_64";
+
+		/* amd64 + x86: We support X86-64, X86-X32, and X86-32 ABI. The first
+		 * two are 64-bit ABIs and the third is 32-bit.  All three will run on
+		 * amd64 architecture, but only the 32-bit will run on the x86 family.
+		 */
 		case EM_X86_64:
-			return "amd64";
+			if (width == 64)
+				return "x86_64";
+			else
+				return "x86_x32";
+		case EM_386:
+			return "x86_32";
+
+		/* arm: We support two 32-bit ABIs, eabi and oabi. */
 		case EM_ARM:
-			return "arm";
+			if (e_flags & EF_ARM_EABIMASK)
+				return "arm_eabi";
+			else
+				return "arm_oabi";
+
+		/* arm64: We support only one 64-bit ABI. */
 		case EM_AARCH64:
-			return "arm64";
+			return "arm_64";
+
+		/* m68k: We support only one 32-bit ABI. */
 		case EM_68K:
-			return "m68k";
+			return "m68k_32";
+
+		/* mips: We support o32, n32 and n64.  The first is 32-bits and the
+		 * latter two are 64-bit ABIs.
+		 */
 		case EM_MIPS_RS3_LE:
 		case EM_MIPS:
-			return "mips";
+			if (width == 64)
+				return "mips_n64";
+			else
+				if ((e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
+					return "mips_o32";
+				else
+					return "mips_n32";
+
+		/* ia64: We support only one 64-bit ABI. */
 		case EM_IA_64:
-			return "ia64";
+			return "ia_64";
+
+		/* hppa: We support only one 32-bit ABI. */
 		case EM_PARISC:
-			return "hppa";
+		case EM_HPPA:
+			return "hppa_32";
+
+		/* ppc: We support only one 32-bit ABI. */
 		case EM_PPC:
-			return "ppc";
+			return "ppc_32";
+
+		/* ppc64: We support only one 64-bit ABI. */
 		case EM_PPC64:
-			return "ppc64";
+			return "ppc_64";
+
+		/* s390: We support one 32-bit and one 64-bit ABI. */
 		case EM_S390:
-			return "s390";
+			if (width == 64)
+				return "s390_64";
+			else
+				return "s390_32";
+
+		/* sh: We support only one 32-bit ABI. */
 		case EM_SH:
-			return "sh";
+			return "sh_32";
+
+		/* sparc: We support one 32-bit and one 64-bit ABI. */
 		case EM_SPARC32PLUS:
 		case EM_SPARCV9:
 		case EM_SPARC:
-			return "sparc";
-		case EM_386:
-			return "x86";
+			if (width == 64)
+				return "sparc_64";
+			else
+				return "sparc_32";
+
 		default:
-			return "unknown_arch";
+			return "unknown";
 	}
 }
 
+
 int
 main(int argc, char* argv[])
 {
@@ -218,35 +264,12 @@ main(int argc, char* argv[])
 
 	if (lseek(fd, e_flags_offset, SEEK_SET) == -1)
 		err(1, "lseek() e_flags failed");
-	if (read(fd, &e_flags, 2) == -1)
+	if (read(fd, &e_flags, 4) == -1)
 		err(1, "read() e_flags failed");
 
 	abi = get_abi(e_machine, width, e_flags);
-
 	printf("%s\n", abi);
 
-
-/*
-	if (!strcmp(arch, "mips")) ;
-		switch (e_flags & EF_MIPS_ABI) {
-			case E_MIPS_ABI_O32:
-				abi = "o32";
-				break;
-
-		if mips_abi == E_MIPS_ABI_O32:
-			name = "o32"
-		elif mips_abi == E_MIPS_ABI_O64:
-			name = "o64"
-		elif mips_abi == E_MIPS_ABI_EABI32:
-			name = "eabi32"
-		elif mips_abi == E_MIPS_ABI_EABI64:
-			name = "eabi64"
-	elif elf_header.e_flags & EF_MIPS_ABI2:
-		name = "n32"
-	elif elf_header.ei_class == ELFCLASS64:
-		name = "n64"
-		}
-*/
 	close(fd);
 	exit(EXIT_SUCCESS);
 }


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-06 17:26 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-06 17:26 UTC (permalink / raw
  To: gentoo-commits

commit:     51e620eef945c3af2c2d8c92fedbacb99ba55d3d
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Tue Jan  6 17:27:12 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Tue Jan  6 17:27:12 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=51e620ee

misc/elf-abi: sort out EM numbers for gentoo -- except mips

---
 misc/elf-abi/elf-abi.c | 116 +++++++++++++++++++++++--------------------------
 1 file changed, 55 insertions(+), 61 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index fb53391..13ec542 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -58,50 +58,38 @@
 #include <unistd.h>
 #include <fcntl.h>
 
-/* We steal this from <elf.h> but don't include it so as to not increase our dependancies. */
+/* We reorder the big endian EM_ numbers to avoid also reading endian information from
+ * the Elf file.  For a mapping between common macine names and EM_ see <elf.h>.  For a
+ * more complete mapping, see elfutil's machines[] defined in libebl/eblopenbackend.c.
+ */
 #define	ELFMAG		"\177ELF"
 
 #define ELFCLASS32	1		/* 32-bit objects */
 #define ELFCLASS64	2		/* 64-bit objects */
 
-#define EM_SPARC	 2		/* SUN SPARC */
-#define EM_386		 3		/* Intel 80386 */
-#define EM_68K		 4		/* Motorola m68k family */
+#define EM_ALPHA	0x9026		/* alpha */
+#define EM_ARM		40		/* arm */
+#define EM_AARCH64	183		/* arm64 */
+#define EM_PARISC_BE	0x0F00		/* hppa - big endian reordering of EM_PARISC = 15 */
+#define EM_IA_64	50		/* ia64 */
+#define EM_68K_BE	0x0400		/* m68k - big endian reordering of EM_68K = 4 */
+
 #define EM_MIPS		 8		/* MIPS R3000 big-endian */
 #define EM_MIPS_RS3_LE	10		/* MIPS R3000 little-endian */
-#define EM_PARISC	15		/* HPPA */
-#define EM_SPARC32PLUS	18		/* Sun's "v8plus" */
-#define EM_PPC		20		/* PowerPC */
-#define EM_PPC64	21		/* PowerPC 64-bit */
-#define EM_S390		22		/* IBM S390 */
-#define EM_ARM		40		/* ARM */
-#define EM_FAKE_ALPHA	41		/* Digital Alpha */
-#define EM_SH		42		/* Hitachi SH */
-#define EM_SPARCV9	43		/* SPARC v9 64-bit */
-#define EM_IA_64	50		/* Intel Merced */
-#define EM_X86_64	62		/* AMD x86-64 architecture */
-#define EM_AARCH64	183		/* ARM AARCH64 */
-#define EM_ALPHA	0x9026		/* Unofficial, but needed in Gentoo */
-#define EM_HPPA		0x0F00		/* Unofficial, but needed in Gentoo */
 
-#define EF_MIPS_ABI		0x0000F000
-#define E_MIPS_ABI_O32		0x00001000
+#define EM_PPC_BE	0x1400		/* ppc - big endian reordering of EM_PPC = 20 */
+#define EM_PPC64_BE	0x1500		/* ppc64 - bit endian reordering of EM_PPC64 = 21 */
+#define EM_S390_BE	0x1600		/* s390 - big endian reordering of EM_S390 */
+#define EM_SH		42		/* Hitachi SH */
+#define EM_SPARC32_BE	0x1200		/* sparc - big endian reordering of EM_SPARC32PLUS = 18 */
+#define EM_SPARC64_BE	0x2B00		/* sparc - big endian reordinger of EM_SPARCV9 = 43 */
+#define EM_386		 3		/* x86 */
+#define EM_X86_64	62		/* amd64 */
 
-#define EF_ARM_EABIMASK		0XFF000000
-
-
-int
-get_wordsize(uint8_t ei_class)
-{
-	switch (ei_class) {
-		case ELFCLASS32:
-			return 32;
-		case ELFCLASS64:
-			return 64;
-		default:
-			return 0;
-	}
-}
+/* The arm and mips ABI flags housed in e_flags */
+#define EF_MIPS_ABI		0x0000F000	/* Mask for mips ABI */
+#define E_MIPS_ABI_O32		0x00001000	/* Value for o32                */
+#define EF_ARM_EABIMASK		0XFF000000	/* Mask for arm EABI - we dont' destinguish versions */
 
 
 char *
@@ -115,21 +103,8 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 
 		/* alpha: We support only one 64-bit ABI. */
 		case EM_ALPHA:
-		case EM_FAKE_ALPHA:
 			return "alpha_64";
 
-		/* amd64 + x86: We support X86-64, X86-X32, and X86-32 ABI. The first
-		 * two are 64-bit ABIs and the third is 32-bit.  All three will run on
-		 * amd64 architecture, but only the 32-bit will run on the x86 family.
-		 */
-		case EM_X86_64:
-			if (width == 64)
-				return "x86_64";
-			else
-				return "x86_x32";
-		case EM_386:
-			return "x86_32";
-
 		/* arm: We support two 32-bit ABIs, eabi and oabi. */
 		case EM_ARM:
 			if (e_flags & EF_ARM_EABIMASK)
@@ -142,7 +117,7 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 			return "arm_64";
 
 		/* m68k: We support only one 32-bit ABI. */
-		case EM_68K:
+		case EM_68K_BE:
 			return "m68k_32";
 
 		/* mips: We support o32, n32 and n64.  The first is 32-bits and the
@@ -163,20 +138,19 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 			return "ia_64";
 
 		/* hppa: We support only one 32-bit ABI. */
-		case EM_PARISC:
-		case EM_HPPA:
+		case EM_PARISC_BE:
 			return "hppa_32";
 
 		/* ppc: We support only one 32-bit ABI. */
-		case EM_PPC:
+		case EM_PPC_BE:
 			return "ppc_32";
 
 		/* ppc64: We support only one 64-bit ABI. */
-		case EM_PPC64:
+		case EM_PPC64_BE:
 			return "ppc_64";
 
 		/* s390: We support one 32-bit and one 64-bit ABI. */
-		case EM_S390:
+		case EM_S390_BE:
 			if (width == 64)
 				return "s390_64";
 			else
@@ -187,13 +161,22 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 			return "sh_32";
 
 		/* sparc: We support one 32-bit and one 64-bit ABI. */
-		case EM_SPARC32PLUS:
-		case EM_SPARCV9:
-		case EM_SPARC:
+		case EM_SPARC32_BE:
+			return "sparc_32";
+		case EM_SPARC64_BE:
+			return "sparc_64";
+
+		/* amd64 + x86: We support X86-64, X86-X32, and X86-32 ABI. The first
+		 * two are 64-bit ABIs and the third is 32-bit.  All three will run on
+		 * amd64 architecture, but only the 32-bit will run on the x86 family.
+		 */
+		case EM_386:
+			return "x86_32";
+		case EM_X86_64:
 			if (width == 64)
-				return "sparc_64";
+				return "x86_64";
 			else
-				return "sparc_32";
+				return "x86_x32";
 
 		default:
 			return "unknown";
@@ -231,10 +214,19 @@ main(int argc, char* argv[])
 	if (strncmp(magic, ELFMAG, 4) != 0)
 		errx(1, "%s is not an ELF object", argv[1]);
 
-	/* 32 or 64 bits? */
+	/* 32 or 64 bits machine word size? */
 	if (read(fd, &ei_class, 1) == -1)
 		err(1, "read() ei_class failed");
-	width = get_wordsize(ei_class);
+	switch (ei_class) {
+		case ELFCLASS32:
+			width = 32;
+			break;
+		case ELFCLASS64:
+			width = 64;
+			break;
+		default:
+			errx(1, "Unknown machine word size.");
+	}
 
 	/*
 	All Elf files begin with the following Elf header:
@@ -261,6 +253,8 @@ main(int argc, char* argv[])
 		err(1, "lseek() e_machine failed");
 	if (read(fd, &e_machine, 2) == -1)
 		err(1, "read() e_machine failed");
+	printf("%x\n", e_machine);
+	printf("%d\n", e_machine);
 
 	if (lseek(fd, e_flags_offset, SEEK_SET) == -1)
 		err(1, "lseek() e_flags failed");


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-06 18:22 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-06 18:22 UTC (permalink / raw
  To: gentoo-commits

commit:     a306769b5d55e5ee95f206930352de15e760140c
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Tue Jan  6 18:23:45 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Tue Jan  6 18:23:45 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=a306769b

misc/elf-abi: sort out mips

---
 misc/elf-abi/elf-abi.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index 13ec542..5f47212 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -74,8 +74,8 @@
 #define EM_IA_64	50		/* ia64 */
 #define EM_68K_BE	0x0400		/* m68k - big endian reordering of EM_68K = 4 */
 
-#define EM_MIPS		 8		/* MIPS R3000 big-endian */
-#define EM_MIPS_RS3_LE	10		/* MIPS R3000 little-endian */
+#define EM_MIPS		8		/* mips */
+#define EM_MIPS_BE	0x800		/* mips - big endian reordering of EM_MIPS = 8 */
 
 #define EM_PPC_BE	0x1400		/* ppc - big endian reordering of EM_PPC = 20 */
 #define EM_PPC64_BE	0x1500		/* ppc64 - bit endian reordering of EM_PPC64 = 21 */
@@ -83,12 +83,12 @@
 #define EM_SH		42		/* Hitachi SH */
 #define EM_SPARC32_BE	0x1200		/* sparc - big endian reordering of EM_SPARC32PLUS = 18 */
 #define EM_SPARC64_BE	0x2B00		/* sparc - big endian reordinger of EM_SPARCV9 = 43 */
-#define EM_386		 3		/* x86 */
+#define EM_386		3		/* x86 */
 #define EM_X86_64	62		/* amd64 */
 
 /* The arm and mips ABI flags housed in e_flags */
-#define EF_MIPS_ABI		0x0000F000	/* Mask for mips ABI */
-#define E_MIPS_ABI_O32		0x00001000	/* Value for o32                */
+#define EF_MIPS_ABI2		0x00000020	/* Mask for mips ABI */
+#define EF_MIPS_ABI2_BE		0x20000000	/* Mask for mips ABI */
 #define EF_ARM_EABIMASK		0XFF000000	/* Mask for arm EABI - we dont' destinguish versions */
 
 
@@ -123,15 +123,23 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 		/* mips: We support o32, n32 and n64.  The first is 32-bits and the
 		 * latter two are 64-bit ABIs.
 		 */
-		case EM_MIPS_RS3_LE:
 		case EM_MIPS:
 			if (width == 64)
 				return "mips_n64";
 			else
-				if ((e_flags & EF_MIPS_ABI) == E_MIPS_ABI_O32)
-					return "mips_o32";
+				if (e_flags & EF_MIPS_ABI2)
+					return "mips_n32";
 				else
+					return "mips_o32";
+
+		case EM_MIPS_BE:
+			if (width == 64)
+				return "mips_n64";
+			else
+				if (e_flags & EF_MIPS_ABI2_BE)
 					return "mips_n32";
+				else
+					return "mips_o32";
 
 		/* ia64: We support only one 64-bit ABI. */
 		case EM_IA_64:
@@ -253,8 +261,6 @@ main(int argc, char* argv[])
 		err(1, "lseek() e_machine failed");
 	if (read(fd, &e_machine, 2) == -1)
 		err(1, "read() e_machine failed");
-	printf("%x\n", e_machine);
-	printf("%d\n", e_machine);
 
 	if (lseek(fd, e_flags_offset, SEEK_SET) == -1)
 		err(1, "lseek() e_flags failed");


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-06 20:47 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-06 20:47 UTC (permalink / raw
  To: gentoo-commits

commit:     b92d4495d9ee7661b8c5b75648821291b5f5cbfd
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Tue Jan  6 20:43:01 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Tue Jan  6 20:43:01 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=b92d4495

misc/elf-abi: manually read little/big endian half or full words

When reading on a native system, read() transparently reads two
byte or four bytes in the correct byte order and returns the correct
value.  But if you read() a word in a big endian file from a little
endian system or vice versa, you get the wrong value.  We wrap
read() in read_endian() to compensate for this.

---
 misc/elf-abi/elf-abi.c | 133 +++++++++++++++++++++++++++++++------------------
 1 file changed, 84 insertions(+), 49 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index 5f47212..f4408cb 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -58,39 +58,62 @@
 #include <unistd.h>
 #include <fcntl.h>
 
-/* We reorder the big endian EM_ numbers to avoid also reading endian information from
- * the Elf file.  For a mapping between common macine names and EM_ see <elf.h>.  For a
- * more complete mapping, see elfutil's machines[] defined in libebl/eblopenbackend.c.
+/* For a mapping between common macine names and EM_ number see <elf.h>.  For a more
+ * complete mapping, see elfutil's machines[] defined in libebl/eblopenbackend.c.
  */
 #define	ELFMAG		"\177ELF"
 
 #define ELFCLASS32	1		/* 32-bit objects */
 #define ELFCLASS64	2		/* 64-bit objects */
+#define ELFDATA2LSB	1		/* 2's complement, little endian */
+#define ELFDATA2MSB	2		/* 2's complement, big endian */
 
 #define EM_ALPHA	0x9026		/* alpha */
 #define EM_ARM		40		/* arm */
 #define EM_AARCH64	183		/* arm64 */
-#define EM_PARISC_BE	0x0F00		/* hppa - big endian reordering of EM_PARISC = 15 */
+#define EM_PARISC	15		/* hppa */
 #define EM_IA_64	50		/* ia64 */
-#define EM_68K_BE	0x0400		/* m68k - big endian reordering of EM_68K = 4 */
-
+#define EM_68K		4		/* m68k */
 #define EM_MIPS		8		/* mips */
-#define EM_MIPS_BE	0x800		/* mips - big endian reordering of EM_MIPS = 8 */
-
-#define EM_PPC_BE	0x1400		/* ppc - big endian reordering of EM_PPC = 20 */
-#define EM_PPC64_BE	0x1500		/* ppc64 - bit endian reordering of EM_PPC64 = 21 */
-#define EM_S390_BE	0x1600		/* s390 - big endian reordering of EM_S390 */
+#define EM_PPC		20		/* ppc */
+#define EM_PPC64	21		/* ppc64 */
+#define EM_S390		22		/* s390 */
 #define EM_SH		42		/* Hitachi SH */
-#define EM_SPARC32_BE	0x1200		/* sparc - big endian reordering of EM_SPARC32PLUS = 18 */
-#define EM_SPARC64_BE	0x2B00		/* sparc - big endian reordinger of EM_SPARCV9 = 43 */
+#define EM_SPARC32PLUS	18		/* sparc 32-bit */
+#define EM_SPARCV9	43		/* sparc 64-bit */
 #define EM_386		3		/* x86 */
 #define EM_X86_64	62		/* amd64 */
 
 /* The arm and mips ABI flags housed in e_flags */
-#define EF_MIPS_ABI2		0x00000020	/* Mask for mips ABI */
-#define EF_MIPS_ABI2_BE		0x20000000	/* Mask for mips ABI */
+#define EF_MIPS_ABI2		0x00000020	/* Mask for mips n32 ABI */
 #define EF_ARM_EABIMASK		0XFF000000	/* Mask for arm EABI - we dont' destinguish versions */
 
+int
+get_wordsize(uint8_t ei_class)
+{
+	switch (ei_class) {
+		case ELFCLASS32:
+			return 32;
+		case ELFCLASS64:
+			return 64;
+		default:
+			errx(1, "Unknown machine word size.");
+	}
+}
+
+int
+get_endian(uint8_t ei_data)
+{
+	switch (ei_data) {
+		case ELFDATA2LSB:
+			return 0;
+		case ELFDATA2MSB:
+			return 1;
+		default:
+			errx(1, "Unknown endian.");
+	}
+}
+
 
 char *
 get_abi(uint16_t e_machine, int width, uint32_t e_flags)
@@ -117,7 +140,7 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 			return "arm_64";
 
 		/* m68k: We support only one 32-bit ABI. */
-		case EM_68K_BE:
+		case EM_68K:
 			return "m68k_32";
 
 		/* mips: We support o32, n32 and n64.  The first is 32-bits and the
@@ -132,33 +155,25 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 				else
 					return "mips_o32";
 
-		case EM_MIPS_BE:
-			if (width == 64)
-				return "mips_n64";
-			else
-				if (e_flags & EF_MIPS_ABI2_BE)
-					return "mips_n32";
-				else
-					return "mips_o32";
 
 		/* ia64: We support only one 64-bit ABI. */
 		case EM_IA_64:
 			return "ia_64";
 
 		/* hppa: We support only one 32-bit ABI. */
-		case EM_PARISC_BE:
+		case EM_PARISC:
 			return "hppa_32";
 
 		/* ppc: We support only one 32-bit ABI. */
-		case EM_PPC_BE:
+		case EM_PPC:
 			return "ppc_32";
 
 		/* ppc64: We support only one 64-bit ABI. */
-		case EM_PPC64_BE:
+		case EM_PPC64:
 			return "ppc_64";
 
 		/* s390: We support one 32-bit and one 64-bit ABI. */
-		case EM_S390_BE:
+		case EM_S390:
 			if (width == 64)
 				return "s390_64";
 			else
@@ -169,9 +184,9 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 			return "sh_32";
 
 		/* sparc: We support one 32-bit and one 64-bit ABI. */
-		case EM_SPARC32_BE:
+		case EM_SPARC32PLUS:
 			return "sparc_32";
-		case EM_SPARC64_BE:
+		case EM_SPARCV9:
 			return "sparc_64";
 
 		/* amd64 + x86: We support X86-64, X86-X32, and X86-32 ABI. The first
@@ -192,16 +207,42 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 }
 
 
+/* Sublte point about read():  If you run elf-abi from a little endian machine on an	*/
+/* Elf object on a big endian (eg. if you are cross compiling), then you get the wrong	*/
+/* byte order.  If howerver, you read it natively, you get it right.  We'll wrap read()	*/
+/* with our own version which reads one byte at a time and corrects this.		*/
+ssize_t
+read_endian(int fd, void *buf, size_t count, int endian)
+{
+	ssize_t i;
+	uint8_t data;
+
+	for(i = 0; i < count; i++) {
+		if (read(fd, &data, 1) == -1)
+			errx(1, "read() ei_class failed");
+		if (endian)
+			((uint8_t *)buf)[count-i-1] = data;
+		else
+			((uint8_t *)buf)[i] = data;
+	}
+
+	return count;
+}
+
+
 int
 main(int argc, char* argv[])
 {
 	int width;			/* Machine word size.  Either 32 or 64 bits.		*/
+	int endian;			/* Endian, 0 = little, 1 = big				*/
 	char *abi;			/* Abi name from glibc's <bits/syscall.h>		*/
 
 	int fd;				/* file descriptor for opened Elf object.		*/
 	struct stat s;			/* stat on opened Elf object.				*/
 	char magic[4];			/* magic number at the begining of the file		*/
 	uint8_t ei_class;		/* ei_class is one byte of e_ident[]			*/
+	uint8_t ei_data;		/* ei_data is one byte of e_ident[]			*/
+
 	uint16_t e_machine;		/* Size is Elf32_Half or Elf64_Half.  Both are 2 bytes.	*/
 	uint32_t e_flags;		/* Size is Elf32_Word or Elf64_Word.  Both are 4 bytes.	*/
 	uint64_t e_machine_offset, e_flags_offset;  /* Wide enough for either 32 or 64 bits.	*/
@@ -216,25 +257,21 @@ main(int argc, char* argv[])
 
 	/* Can we read it and is it an ELF object? */
 	if ((fd = open(argv[1], O_RDONLY)) == -1)
-		err(1, "failed to open %s", argv[1]);
+		errx(1, "failed to open %s", argv[1]);
 	if (read(fd, magic, 4) == -1)
-		err(1, "read() magic failed");
+		errx(1, "read() magic failed");
 	if (strncmp(magic, ELFMAG, 4) != 0)
 		errx(1, "%s is not an ELF object", argv[1]);
 
 	/* 32 or 64 bits machine word size? */
 	if (read(fd, &ei_class, 1) == -1)
-		err(1, "read() ei_class failed");
-	switch (ei_class) {
-		case ELFCLASS32:
-			width = 32;
-			break;
-		case ELFCLASS64:
-			width = 64;
-			break;
-		default:
-			errx(1, "Unknown machine word size.");
-	}
+		errx(1, "read() ei_class failed");
+	width = get_wordsize(ei_class);
+
+	/* Little or Big Endian? */
+	if (read(fd, &ei_data, 1) == -1)
+		errx(1, "read() ei_data failed");
+	endian = get_endian(ei_data);
 
 	/*
 	All Elf files begin with the following Elf header:
@@ -258,14 +295,12 @@ main(int argc, char* argv[])
 
 	/* What is the abi? */
 	if (lseek(fd, e_machine_offset, SEEK_SET) == -1)
-		err(1, "lseek() e_machine failed");
-	if (read(fd, &e_machine, 2) == -1)
-		err(1, "read() e_machine failed");
+		errx(1, "lseek() e_machine failed");
+	read_endian(fd, &e_machine, 2, endian);
 
 	if (lseek(fd, e_flags_offset, SEEK_SET) == -1)
-		err(1, "lseek() e_flags failed");
-	if (read(fd, &e_flags, 4) == -1)
-		err(1, "read() e_flags failed");
+		errx(1, "lseek() e_flags failed");
+	read_endian(fd, &e_flags, 4, endian);
 
 	abi = get_abi(e_machine, width, e_flags);
 	printf("%s\n", abi);


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-06 21:34 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-06 21:34 UTC (permalink / raw
  To: gentoo-commits

commit:     cb2af57d4d0230dbb38f314c24f4118f9dbd679c
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Tue Jan  6 21:35:38 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Tue Jan  6 21:35:38 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=cb2af57d

misc/elf-abi: fixup read_endian()

---
 misc/elf-abi/elf-abi.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index f4408cb..3be6aae 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -211,22 +211,23 @@ get_abi(uint16_t e_machine, int width, uint32_t e_flags)
 /* Elf object on a big endian (eg. if you are cross compiling), then you get the wrong	*/
 /* byte order.  If howerver, you read it natively, you get it right.  We'll wrap read()	*/
 /* with our own version which reads one byte at a time and corrects this.		*/
-ssize_t
-read_endian(int fd, void *buf, size_t count, int endian)
+uint64_t
+read_endian(int fd, size_t count, int endian)
 {
 	ssize_t i;
 	uint8_t data;
+	uint64_t value = 0;
 
 	for(i = 0; i < count; i++) {
 		if (read(fd, &data, 1) == -1)
 			errx(1, "read() ei_class failed");
 		if (endian)
-			((uint8_t *)buf)[count-i-1] = data;
+			value += data << 8 * (count-i-1);
 		else
-			((uint8_t *)buf)[i] = data;
+			value += data << 8 * i;
 	}
 
-	return count;
+	return value;
 }
 
 
@@ -296,11 +297,11 @@ main(int argc, char* argv[])
 	/* What is the abi? */
 	if (lseek(fd, e_machine_offset, SEEK_SET) == -1)
 		errx(1, "lseek() e_machine failed");
-	read_endian(fd, &e_machine, 2, endian);
+	e_machine = (uint16_t)read_endian(fd, 2, endian);
 
 	if (lseek(fd, e_flags_offset, SEEK_SET) == -1)
 		errx(1, "lseek() e_flags failed");
-	read_endian(fd, &e_flags, 4, endian);
+	e_flags = (uint32_t)read_endian(fd, 4, endian);
 
 	abi = get_abi(e_machine, width, e_flags);
 	printf("%s\n", abi);


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/
@ 2015-01-06 21:43 Anthony G. Basile
  0 siblings, 0 replies; 11+ messages in thread
From: Anthony G. Basile @ 2015-01-06 21:43 UTC (permalink / raw
  To: gentoo-commits

commit:     666190ea1a94474f73c3e5efcb271432152c6767
Author:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
AuthorDate: Tue Jan  6 21:44:11 2015 +0000
Commit:     Anthony G. Basile <blueness <AT> gentoo <DOT> org>
CommitDate: Tue Jan  6 21:44:11 2015 +0000
URL:        http://sources.gentoo.org/gitweb/?p=proj/elfix.git;a=commit;h=666190ea

misc/elf-abi.c: cleanup comments

---
 misc/elf-abi/elf-abi.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/misc/elf-abi/elf-abi.c b/misc/elf-abi/elf-abi.c
index 3be6aae..81e5e16 100644
--- a/misc/elf-abi/elf-abi.c
+++ b/misc/elf-abi/elf-abi.c
@@ -78,13 +78,12 @@
 #define EM_PPC		20		/* ppc */
 #define EM_PPC64	21		/* ppc64 */
 #define EM_S390		22		/* s390 */
-#define EM_SH		42		/* Hitachi SH */
-#define EM_SPARC32PLUS	18		/* sparc 32-bit */
-#define EM_SPARCV9	43		/* sparc 64-bit */
+#define EM_SH		42		/* sh */
+#define EM_SPARC32PLUS	18		/* sparc (32-bit) */
+#define EM_SPARCV9	43		/* sparc (64-bit) */
 #define EM_386		3		/* x86 */
 #define EM_X86_64	62		/* amd64 */
 
-/* The arm and mips ABI flags housed in e_flags */
 #define EF_MIPS_ABI2		0x00000020	/* Mask for mips n32 ABI */
 #define EF_ARM_EABIMASK		0XFF000000	/* Mask for arm EABI - we dont' destinguish versions */
 
@@ -218,9 +217,12 @@ read_endian(int fd, size_t count, int endian)
 	uint8_t data;
 	uint64_t value = 0;
 
+	if (count > 8)
+		errx(1, "Max width exceeded in read_endian()");
+
 	for(i = 0; i < count; i++) {
 		if (read(fd, &data, 1) == -1)
-			errx(1, "read() ei_class failed");
+			errx(1, "read() in read_endian() failed");
 		if (endian)
 			value += data << 8 * (count-i-1);
 		else
@@ -243,7 +245,6 @@ main(int argc, char* argv[])
 	char magic[4];			/* magic number at the begining of the file		*/
 	uint8_t ei_class;		/* ei_class is one byte of e_ident[]			*/
 	uint8_t ei_data;		/* ei_data is one byte of e_ident[]			*/
-
 	uint16_t e_machine;		/* Size is Elf32_Half or Elf64_Half.  Both are 2 bytes.	*/
 	uint32_t e_flags;		/* Size is Elf32_Word or Elf64_Word.  Both are 4 bytes.	*/
 	uint64_t e_machine_offset, e_flags_offset;  /* Wide enough for either 32 or 64 bits.	*/
@@ -261,7 +262,7 @@ main(int argc, char* argv[])
 		errx(1, "failed to open %s", argv[1]);
 	if (read(fd, magic, 4) == -1)
 		errx(1, "read() magic failed");
-	if (strncmp(magic, ELFMAG, 4) != 0)
+	if (strncmp(magic, ELFMAG, 4))
 		errx(1, "%s is not an ELF object", argv[1]);
 
 	/* 32 or 64 bits machine word size? */


^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2015-01-06 21:43 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-01-06 15:13 [gentoo-commits] proj/elfix:master commit in: misc/elf-abi/ Anthony G. Basile
  -- strict thread matches above, loose matches on Subject: below --
2015-01-06 21:43 Anthony G. Basile
2015-01-06 21:34 Anthony G. Basile
2015-01-06 20:47 Anthony G. Basile
2015-01-06 18:22 Anthony G. Basile
2015-01-06 17:26 Anthony G. Basile
2015-01-05 16:26 Anthony G. Basile
2015-01-05 16:23 Anthony G. Basile
2015-01-03 23:53 Anthony G. Basile
2015-01-03 23:49 Anthony G. Basile
2015-01-03 23:19 Anthony G. Basile

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox