changeset 86:537cf2245d98

beginning of ARM7 disassembler
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 28 Mar 2014 07:01:27 +0000
parents 3c420895f68f
children f7fba8518fa2
files arm7dis/armdis.c arm7dis/common.c
diffstat 2 files changed, 123 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arm7dis/armdis.c	Fri Mar 28 07:01:27 2014 +0000
@@ -0,0 +1,36 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+extern char *binfilename;
+extern u_char *filemap;
+extern u_long disasm_len, base_vma;
+
+extern unsigned get_u16(), get_u32();
+
+void
+arm_disasm_line(off)
+	u_long off;
+{
+	u_long word;
+
+	word = get_u32(filemap + off);
+	printf("%8x:\t%08x\t", base_vma + off, word);
+	if ((word >> 28) == 0xF) {
+		printf("invalid\n");
+		return;
+	}
+
+
+}
+
+main(argc, argv)
+	char **argv;
+{
+	u_long off;
+
+	common_init(argc, argv, 4);
+	for (off = 0; off < disasm_len; off += 4)
+		arm_disasm_line(off);
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/arm7dis/common.c	Fri Mar 28 07:01:27 2014 +0000
@@ -0,0 +1,87 @@
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+char *binfilename;
+u_char *filemap;
+u_long disasm_len, base_vma;
+
+common_init(argc, argv, instr_size)
+	char **argv;
+{
+	int fd;
+	struct stat st;
+	u_long fileoff;
+
+	if (argc < 2 || argc > 5) {
+		fprintf(stderr,
+			"usage: %s binfile [file-offset [len [vaddr]]]\n",
+			argv[0]);
+		exit(1);
+	}
+	binfilename = argv[1];
+	fd = open(binfilename, O_RDONLY);
+	if (fd < 0) {
+		perror(binfilename);
+		exit(1);
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a regular file\n",
+			binfilename);
+		exit(1);
+	}
+	if (argc > 2)
+		fileoff = strtoul(argv[2], 0, 0);
+	else
+		fileoff = 0;
+	if (fileoff > st.st_size) {
+		fprintf(stderr,
+		    "error: specified file offset is past the end of file\n");
+		exit(1);
+	}
+	if (argc > 3) {
+		disasm_len = strtoul(argv[3], 0, 0);
+		if (disasm_len > st.st_size - fileoff) {
+			fprintf(stderr,
+			  "error: specified length is past the end of file\n");
+			exit(1);
+		}
+	} else
+		disasm_len = st.st_size - fileoff;
+	if (disasm_len & (instr_size - 1)) {
+		fprintf(stderr,
+"error: length of region to be disassembled must be a multiple of %d bytes\n",
+			instr_size);
+		exit(1);
+	}
+	filemap = mmap(NULL, disasm_len, PROT_READ, MAP_PRIVATE, fd, fileoff);
+	if (filemap == MAP_FAILED) {
+		perror("mmap");
+		exit(1);
+	}
+	close(fd);
+	if (argc > 4)
+		base_vma = strtoul(argv[4], 0, 0);
+	else
+		base_vma = fileoff;
+	return(0);
+}
+
+unsigned
+get_u16(ptr)
+	u_char *ptr;
+{
+	return ptr[0] | ptr[1] << 8;
+}
+
+unsigned
+get_u32(ptr)
+	u_char *ptr;
+{
+	return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
+}