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