FreeCalypso > hg > freecalypso-reveng
changeset 70:6799a5c57a49
tiobjd started
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Sat, 22 Mar 2014 02:29:22 +0000 |
parents | 10de8a00c519 |
children | c15cd3d695c0 |
files | .hgignore ticoff/Makefile ticoff/README ticoff/basics.c ticoff/filestruct.h ticoff/globals.c ticoff/globals.h ticoff/main.c |
diffstat | 8 files changed, 236 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Thu Mar 13 09:05:18 2014 +0000 +++ b/.hgignore Sat Mar 22 02:29:22 2014 +0000 @@ -23,3 +23,5 @@ ^pirollback/dumpjournal$ ^pirollback/inopath$ ^pirollback/rollback$ + +^ticoff/tiobjd$
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/Makefile Sat Mar 22 02:29:22 2014 +0000 @@ -0,0 +1,15 @@ +CC= gcc +CFLAGS= -O2 +PROG= tiobjd +OBJS= basics.o globals.o main.o +HDRS= filestruct.h globals.h + +all: ${PROG} + +${PROG}: ${OBJS} + ${CC} -o $@ ${OBJS} + +${OBJS}: ${HDRS} + +clean: + rm -f *.o ${PROG} *err
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/README Sat Mar 22 02:29:22 2014 +0000 @@ -0,0 +1,6 @@ +Here I'm going to build a standalone tool that reads linkable (not final) +object modules produced by TI's TMS470 toolchain, as found in GSM firmware +semi-sources, and produces disassembly listings that are well-fit for +understanding the function and interfaces of each object blob, and ultimately +replacing each of these blobs with functionally and interface-equivalent +new C code.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/basics.c Sat Mar 22 02:29:22 2014 +0000 @@ -0,0 +1,99 @@ +/* + * This C module implements the "basics" 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 <time.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; +} + +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; +} + +dump_filehdr_info() +{ + time_t timestamp; + struct tm *timedec; + + timestamp = get_u32(filehdr_struct->f_timdat); + timedec = gmtime(×tamp); + printf("timestamp: %d-%02d-%02dT%02d:%02d:%02dZ\n", + timedec->tm_year + 1900, timedec->tm_mon + 1, timedec->tm_mday, + timedec->tm_hour, timedec->tm_min, timedec->tm_sec); + printf("file flags: 0x%x\n", get_u16(filehdr_struct->f_flags)); + printf("%u sections, %u symtab entries\n", nsections, nsymtab); + return(0); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/filestruct.h Sat Mar 22 02:29:22 2014 +0000 @@ -0,0 +1,39 @@ +/* + * Here we are going to define the structures found in the COFF artifact + * file to be analyzed. + */ + +struct external_filehdr { + u_char f_magic[2]; /* magic number */ + u_char f_nscns[2]; /* number of sections */ + u_char f_timdat[4]; /* time & date stamp */ + u_char f_symptr[4]; /* file pointer to symtab */ + u_char f_nsyms[4]; /* number of symtab entries */ + u_char f_opthdr[2]; /* sizeof(optional hdr) */ + u_char f_flags[2]; /* flags */ + u_char f_target_id[2]; /* magic no. (TI COFF-specific) */ +}; + +struct external_scnhdr { + u_char s_name[8]; /* section name */ + u_char s_paddr[4]; /* physical address, aliased s_nlib */ + u_char s_vaddr[4]; /* virtual address */ + u_char s_size[4]; /* section size (in WORDS) */ + u_char s_scnptr[4]; /* file ptr to raw data for section */ + u_char s_relptr[4]; /* file ptr to relocation */ + u_char s_lnnoptr[4]; /* file ptr to line numbers */ + u_char s_nreloc[4]; /* number of relocation entries */ + u_char s_nlnno[4]; /* number of line number entries*/ + u_char s_flags[4]; /* flags */ + u_char s_reserved[2]; /* reserved */ + u_char s_page[2]; /* section page number (LOAD) */ +}; + +struct external_syment { + u_char e_name[8]; + u_char e_value[4]; + u_char e_scnum[2]; + u_char e_type[2]; + u_char e_sclass; + u_char e_numaux; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/globals.c Sat Mar 22 02:29:22 2014 +0000 @@ -0,0 +1,17 @@ +/* + * Definitions of global vars for the tiobjd program. + */ + +#include <sys/types.h> +#include "filestruct.h" + +char *objfilename; +u_char *filemap; +size_t objfile_tot_size; + +struct external_filehdr *filehdr_struct; +struct external_scnhdr *sections_raw; +unsigned nsections; +struct external_syment *symtab_raw; +unsigned nsymtab; +unsigned strtab_offset;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/globals.h Sat Mar 22 02:29:22 2014 +0000 @@ -0,0 +1,14 @@ +/* + * extern declarations of global vars for the tiobjd program. + */ + +extern char *objfilename; +extern u_char *filemap; +extern size_t objfile_tot_size; + +extern struct external_filehdr *filehdr_struct; +extern struct external_scnhdr *sections_raw; +extern unsigned nsections; +extern struct external_syment *symtab_raw; +extern unsigned nsymtab; +extern unsigned strtab_offset;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/main.c Sat Mar 22 02:29:22 2014 +0000 @@ -0,0 +1,44 @@ +/* + * tiobjd main() function and command dispatch + */ + +#include <sys/types.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include "filestruct.h" +#include "globals.h" + +extern int dump_filehdr_info(); + +static struct cmdtab { + char *cmd; + int (*func)(); +} cmdtab[] = { + {"hdr", dump_filehdr_info}, + {0, 0} +}; + +main(argc, argv) + char **argv; +{ + struct cmdtab *tp; + + if (argc != 3) { + fprintf(stderr, "usage: %s <objfile> <op>\n", argv[0]); + exit(1); + } + objfilename = argv[1]; + mmap_objfile(); + initial_parse_hdr(); + for (tp = cmdtab; tp->cmd; tp++) + if (!strcmp(tp->cmd, argv[2])) + break; + if (!tp->func) { + fprintf(stderr, "\"%s\": unknown or unimplemented command\n", + argv[2]); + exit(1); + } + return tp->func(); +}