changeset 74:2eef88395908

tiobjd: a little refactoring
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 25 Mar 2014 18:55:36 +0000
parents 10f3fbff5e97
children 1a23ff9a81de
files ticoff/Makefile ticoff/basics.c ticoff/lowlevel.c ticoff/tables.c
diffstat 4 files changed, 114 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/ticoff/Makefile	Tue Mar 25 18:34:03 2014 +0000
+++ b/ticoff/Makefile	Tue Mar 25 18:55:36 2014 +0000
@@ -1,7 +1,7 @@
 CC=	gcc
 CFLAGS=	-O2
 PROG=	tiobjd
-OBJS=	basics.o globals.o main.o tables.o
+OBJS=	basics.o globals.o lowlevel.o main.o tables.o
 HDRS=	filestruct.h globals.h intstruct.h
 
 all:	${PROG}
--- a/ticoff/basics.c	Tue Mar 25 18:34:03 2014 +0000
+++ b/ticoff/basics.c	Tue Mar 25 18:55:36 2014 +0000
@@ -1,98 +1,16 @@
 /*
- * This C module implements the "basics" of TI COFF image analysis.
+ * This C module implements some "basic" dump commands
  */
 
 #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 <time.h>
 #include "filestruct.h"
+#include "intstruct.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;
-}
+extern unsigned get_u16(), get_u32();
 
 dump_filehdr_info()
 {
@@ -108,3 +26,19 @@
 	printf("%u sections, %u symtab entries\n", nsections, nsymtab);
 	return(0);
 }
+
+cmd_sechdr()
+{
+	unsigned n;
+	struct internal_scnhdr *inf;
+
+	get_int_section_table();
+	for (n = 0; n < nsections; n++) {
+		inf = sections + n;
+		printf("#%d: %s size=%u, flags=0x%x\n", n, inf->name,
+			inf->size, inf->flags);
+		printf("\t%u reloc, %u line entries\n",
+			inf->nreloc, inf->nlineent);
+	}
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ticoff/lowlevel.c	Tue Mar 25 18:55:36 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;
+}
--- a/ticoff/tables.c	Tue Mar 25 18:34:03 2014 +0000
+++ b/ticoff/tables.c	Tue Mar 25 18:55:36 2014 +0000
@@ -78,22 +78,6 @@
 	}
 }
 
-cmd_sechdr()
-{
-	unsigned n;
-	struct internal_scnhdr *inf;
-
-	get_int_section_table();
-	for (n = 0; n < nsections; n++) {
-		inf = sections + n;
-		printf("#%d: %s size=%u, flags=0x%x\n", n, inf->name,
-			inf->size, inf->flags);
-		printf("\t%u reloc, %u line entries\n",
-			inf->nreloc, inf->nlineent);
-	}
-	exit(0);
-}
-
 get_int_symbol_table()
 {
 	unsigned n;