annotate ticoff/basics.c @ 70:6799a5c57a49

tiobjd started
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sat, 22 Mar 2014 02:29:22 +0000
parents
children 10f3fbff5e97
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
70
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
1 /*
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
2 * This C module implements the "basics" of TI COFF image analysis.
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
3 */
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
4
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
5 #include <sys/types.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
6 #include <sys/file.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
7 #include <sys/stat.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
8 #include <sys/mman.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
9 #include <stdio.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
10 #include <stdlib.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
11 #include <unistd.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
12 #include <time.h>
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
13 #include "filestruct.h"
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
14 #include "globals.h"
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
15
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
16 mmap_objfile()
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
17 {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
18 int fd;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
19 struct stat st;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
20
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
21 fd = open(objfilename, O_RDONLY);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
22 if (fd < 0) {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
23 perror(objfilename);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
24 exit(1);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
25 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
26 fstat(fd, &st);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
27 if (!S_ISREG(st.st_mode)) {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
28 fprintf(stderr, "error: %s is not a regular file\n",
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
29 objfilename);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
30 exit(1);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
31 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
32 objfile_tot_size = st.st_size;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
33 filemap = mmap(NULL, objfile_tot_size, PROT_READ, MAP_PRIVATE, fd, 0L);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
34 if (filemap == MAP_FAILED) {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
35 perror("mmap");
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
36 exit(1);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
37 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
38 close(fd);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
39 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
40
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
41 unsigned
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
42 get_u16(ptr)
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
43 u_char *ptr;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
44 {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
45 return ptr[0] | ptr[1] << 8;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
46 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
47
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
48 unsigned
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
49 get_u32(ptr)
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
50 u_char *ptr;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
51 {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
52 return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
53 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
54
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
55 initial_parse_hdr()
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
56 {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
57 unsigned symtab_offset;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
58
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
59 filehdr_struct = (struct external_filehdr *) filemap;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
60 if (get_u16(filehdr_struct->f_magic) != 0xC2) {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
61 fprintf(stderr, "error: %s is not a TI COFF2 object\n",
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
62 objfilename);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
63 exit(1);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
64 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
65 if (get_u16(filehdr_struct->f_target_id) != 0x97) {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
66 fprintf(stderr, "error: TI COFF object %s is not for TMS470\n",
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
67 objfilename);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
68 exit(1);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
69 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
70 if (get_u16(filehdr_struct->f_opthdr)) {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
71 fprintf(stderr,
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
72 "error: %s has the \"optional\" header present\n",
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
73 objfilename);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
74 exit(1);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
75 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
76 sections_raw = (struct external_scnhdr *)
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
77 (filemap + sizeof(struct external_filehdr));
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
78 nsections = get_u16(filehdr_struct->f_nscns);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
79 symtab_offset = get_u32(filehdr_struct->f_symptr);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
80 symtab_raw = (struct external_syment *)(filemap + symtab_offset);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
81 nsymtab = get_u32(filehdr_struct->f_nsyms);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
82 strtab_offset = symtab_offset +
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
83 sizeof(struct external_syment) * nsymtab;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
84 }
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
85
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
86 dump_filehdr_info()
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
87 {
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
88 time_t timestamp;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
89 struct tm *timedec;
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
90
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
91 timestamp = get_u32(filehdr_struct->f_timdat);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
92 timedec = gmtime(&timestamp);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
93 printf("timestamp: %d-%02d-%02dT%02d:%02d:%02dZ\n",
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
94 timedec->tm_year + 1900, timedec->tm_mon + 1, timedec->tm_mday,
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
95 timedec->tm_hour, timedec->tm_min, timedec->tm_sec);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
96 printf("file flags: 0x%x\n", get_u16(filehdr_struct->f_flags));
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
97 printf("%u sections, %u symtab entries\n", nsections, nsymtab);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
98 return(0);
6799a5c57a49 tiobjd started
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff changeset
99 }