diff leo-obj/tool/lowlevel.c @ 130:87b82398a08b

leo-obj project subtree started, tiobjd tool moved into it
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 06 Apr 2014 22:14:39 +0000
parents ticoff/lowlevel.c@2eef88395908
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/leo-obj/tool/lowlevel.c	Sun Apr 06 22:14:39 2014 +0000
@@ -0,0 +1,94 @@
+/*
+ * This C module implements the low-level steps of TI COFF image analysis.
+ */
+
+#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>
+#include "filestruct.h"
+#include "globals.h"
+
+mmap_objfile()
+{
+	int fd;
+	struct stat st;
+
+	fd = open(objfilename, O_RDONLY);
+	if (fd < 0) {
+		perror(objfilename);
+		exit(1);
+	}
+	fstat(fd, &st);
+	if (!S_ISREG(st.st_mode)) {
+		fprintf(stderr, "error: %s is not a regular file\n",
+			objfilename);
+		exit(1);
+	}
+	objfile_tot_size = st.st_size;
+	filemap = mmap(NULL, objfile_tot_size, PROT_READ, MAP_PRIVATE, fd, 0L);
+	if (filemap == MAP_FAILED) {
+		perror("mmap");
+		exit(1);
+	}
+	close(fd);
+}
+
+unsigned
+get_u16(ptr)
+	u_char *ptr;
+{
+	return ptr[0] | ptr[1] << 8;
+}
+
+get_s16(ptr)
+	u_char *ptr;
+{
+	int i;
+
+	i = ptr[0] | ptr[1] << 8;
+	if (i >= 32768)
+		i -= 65536;
+	return(i);
+}
+
+unsigned
+get_u32(ptr)
+	u_char *ptr;
+{
+	return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
+}
+
+initial_parse_hdr()
+{
+	unsigned symtab_offset;
+
+	filehdr_struct = (struct external_filehdr *) filemap;
+	if (get_u16(filehdr_struct->f_magic) != 0xC2) {
+		fprintf(stderr, "error: %s is not a TI COFF2 object\n",
+			objfilename);
+		exit(1);
+	}
+	if (get_u16(filehdr_struct->f_target_id) != 0x97) {
+		fprintf(stderr, "error: TI COFF object %s is not for TMS470\n",
+			objfilename);
+		exit(1);
+	}
+	if (get_u16(filehdr_struct->f_opthdr)) {
+		fprintf(stderr,
+			"error: %s has the \"optional\" header present\n",
+			objfilename);
+		exit(1);
+	}
+	sections_raw = (struct external_scnhdr *)
+				(filemap + sizeof(struct external_filehdr));
+	nsections = get_u16(filehdr_struct->f_nscns);
+	symtab_offset = get_u32(filehdr_struct->f_symptr);
+	symtab_raw = (struct external_syment *)(filemap + symtab_offset);
+	nsymtab = get_u32(filehdr_struct->f_nsyms);
+	strtab_offset = symtab_offset +
+				sizeof(struct external_syment) * nsymtab;
+}