# HG changeset patch # User Michael Spacefalcon # Date 1395773736 0 # Node ID 2eef88395908ee45dc9bc150a8d5547886c950b2 # Parent 10f3fbff5e97f40058460af7e0b84e0bf9adcd49 tiobjd: a little refactoring diff -r 10f3fbff5e97 -r 2eef88395908 ticoff/Makefile --- 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} diff -r 10f3fbff5e97 -r 2eef88395908 ticoff/basics.c --- 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 -#include -#include -#include #include #include -#include #include #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); +} diff -r 10f3fbff5e97 -r 2eef88395908 ticoff/lowlevel.c --- /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 +#include +#include +#include +#include +#include +#include +#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; +} diff -r 10f3fbff5e97 -r 2eef88395908 ticoff/tables.c --- 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;