# HG changeset patch # User Michael Spacefalcon # Date 1395467582 0 # Node ID c15cd3d695c0aadfca348a34ce304acf1b7ff211 # Parent 6799a5c57a49f2aefb1dc2f7a545e0cb8596e609 tiobjd: successful parsing of the section header table diff -r 6799a5c57a49 -r c15cd3d695c0 ticoff/Makefile --- a/ticoff/Makefile Sat Mar 22 02:29:22 2014 +0000 +++ b/ticoff/Makefile Sat Mar 22 05:53:02 2014 +0000 @@ -1,8 +1,8 @@ CC= gcc CFLAGS= -O2 PROG= tiobjd -OBJS= basics.o globals.o main.o -HDRS= filestruct.h globals.h +OBJS= basics.o globals.o main.o tables.o +HDRS= filestruct.h globals.h intstruct.h all: ${PROG} diff -r 6799a5c57a49 -r c15cd3d695c0 ticoff/globals.c --- a/ticoff/globals.c Sat Mar 22 02:29:22 2014 +0000 +++ b/ticoff/globals.c Sat Mar 22 05:53:02 2014 +0000 @@ -3,7 +3,6 @@ */ #include -#include "filestruct.h" char *objfilename; u_char *filemap; @@ -15,3 +14,5 @@ struct external_syment *symtab_raw; unsigned nsymtab; unsigned strtab_offset; + +struct internal_scnhdr *sections; diff -r 6799a5c57a49 -r c15cd3d695c0 ticoff/globals.h --- a/ticoff/globals.h Sat Mar 22 02:29:22 2014 +0000 +++ b/ticoff/globals.h Sat Mar 22 05:53:02 2014 +0000 @@ -12,3 +12,5 @@ extern struct external_syment *symtab_raw; extern unsigned nsymtab; extern unsigned strtab_offset; + +extern struct internal_scnhdr *sections; diff -r 6799a5c57a49 -r c15cd3d695c0 ticoff/intstruct.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/intstruct.h Sat Mar 22 05:53:02 2014 +0000 @@ -0,0 +1,15 @@ +/* + * The structures defined in this header file + * are internal to our utility. + */ + +struct internal_scnhdr { + char *name; + unsigned size; + unsigned data_offset; + unsigned reloc_offset; + unsigned line_offset; + unsigned nreloc; + unsigned nlineent; + unsigned flags; +}; diff -r 6799a5c57a49 -r c15cd3d695c0 ticoff/main.c --- a/ticoff/main.c Sat Mar 22 02:29:22 2014 +0000 +++ b/ticoff/main.c Sat Mar 22 05:53:02 2014 +0000 @@ -7,9 +7,9 @@ #include #include #include -#include "filestruct.h" #include "globals.h" +extern int cmd_sechdr(); extern int dump_filehdr_info(); static struct cmdtab { @@ -17,6 +17,7 @@ int (*func)(); } cmdtab[] = { {"hdr", dump_filehdr_info}, + {"sechdr", cmd_sechdr}, {0, 0} }; diff -r 6799a5c57a49 -r c15cd3d695c0 ticoff/tables.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ticoff/tables.c Sat Mar 22 05:53:02 2014 +0000 @@ -0,0 +1,95 @@ +/* + * This C module will contain functions for the initial parsing + * of the section and symbol tables. + */ + +#include +#include +#include +#include +#include +#include "filestruct.h" +#include "intstruct.h" +#include "globals.h" + +extern unsigned get_u16(), get_u32(); + +static char * +get_secorsym_name(ptr) + u_char *ptr; +{ + char *alloc; + + if (ptr[0]) { + if (ptr[7]) { + alloc = malloc(9); + if (!alloc) { + perror("malloc"); + exit(1); + } + bcopy(ptr, alloc, 8); + alloc[8] = '\0'; + return(alloc); + } else + return((char *) ptr); + } + if (ptr[1] || ptr[2] || ptr[3]) { + fprintf(stderr, "error: malformed name in %s at offset 0x%x\n", + objfilename, ptr - filemap); + exit(1); + } + return(filemap + strtab_offset + get_u32(ptr+4)); +} + +get_int_section_table() +{ + unsigned n; + + sections = malloc(sizeof(struct internal_scnhdr) * nsections); + if (!sections) { + perror("malloc"); + exit(1); + } + for (n = 0; n < nsections; n++) { + sections[n].name = get_secorsym_name(sections_raw[n].s_name); + if (get_u32(sections_raw[n].s_paddr)) + fprintf(stderr, + "warning: section #%u (%s) has nonzero paddr\n", + n, sections[n].name); + if (get_u32(sections_raw[n].s_vaddr)) + fprintf(stderr, + "warning: section #%u (%s) has nonzero vaddr\n", + n, sections[n].name); + sections[n].size = get_u32(sections_raw[n].s_size); + sections[n].data_offset = get_u32(sections_raw[n].s_scnptr); + sections[n].reloc_offset = get_u32(sections_raw[n].s_relptr); + sections[n].line_offset = get_u32(sections_raw[n].s_lnnoptr); + sections[n].nreloc = get_u32(sections_raw[n].s_nreloc); + sections[n].nlineent = get_u32(sections_raw[n].s_nlnno); + sections[n].flags = get_u32(sections_raw[n].s_flags); + if (get_u16(sections_raw[n].s_reserved)) + fprintf(stderr, + "warning: section #%u (%s): some nonzero value in s_reserved bytes\n", + n, sections[n].name); + if (get_u16(sections_raw[n].s_page)) + fprintf(stderr, + "warning: section #%u (%s): some nonzero value in s_page bytes\n", + n, sections[n].name); + } +} + +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); +}