comparison ticoff/basics.c @ 74:2eef88395908

tiobjd: a little refactoring
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Tue, 25 Mar 2014 18:55:36 +0000
parents 10f3fbff5e97
children 1a23ff9a81de
comparison
equal deleted inserted replaced
73:10f3fbff5e97 74:2eef88395908
1 /* 1 /*
2 * This C module implements the "basics" of TI COFF image analysis. 2 * This C module implements some "basic" dump commands
3 */ 3 */
4 4
5 #include <sys/types.h> 5 #include <sys/types.h>
6 #include <sys/file.h>
7 #include <sys/stat.h>
8 #include <sys/mman.h>
9 #include <stdio.h> 6 #include <stdio.h>
10 #include <stdlib.h> 7 #include <stdlib.h>
11 #include <unistd.h>
12 #include <time.h> 8 #include <time.h>
13 #include "filestruct.h" 9 #include "filestruct.h"
10 #include "intstruct.h"
14 #include "globals.h" 11 #include "globals.h"
15 12
16 mmap_objfile() 13 extern unsigned get_u16(), get_u32();
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 get_s16(ptr)
49 u_char *ptr;
50 {
51 int i;
52
53 i = ptr[0] | ptr[1] << 8;
54 if (i >= 32768)
55 i -= 65536;
56 return(i);
57 }
58
59 unsigned
60 get_u32(ptr)
61 u_char *ptr;
62 {
63 return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
64 }
65
66 initial_parse_hdr()
67 {
68 unsigned symtab_offset;
69
70 filehdr_struct = (struct external_filehdr *) filemap;
71 if (get_u16(filehdr_struct->f_magic) != 0xC2) {
72 fprintf(stderr, "error: %s is not a TI COFF2 object\n",
73 objfilename);
74 exit(1);
75 }
76 if (get_u16(filehdr_struct->f_target_id) != 0x97) {
77 fprintf(stderr, "error: TI COFF object %s is not for TMS470\n",
78 objfilename);
79 exit(1);
80 }
81 if (get_u16(filehdr_struct->f_opthdr)) {
82 fprintf(stderr,
83 "error: %s has the \"optional\" header present\n",
84 objfilename);
85 exit(1);
86 }
87 sections_raw = (struct external_scnhdr *)
88 (filemap + sizeof(struct external_filehdr));
89 nsections = get_u16(filehdr_struct->f_nscns);
90 symtab_offset = get_u32(filehdr_struct->f_symptr);
91 symtab_raw = (struct external_syment *)(filemap + symtab_offset);
92 nsymtab = get_u32(filehdr_struct->f_nsyms);
93 strtab_offset = symtab_offset +
94 sizeof(struct external_syment) * nsymtab;
95 }
96 14
97 dump_filehdr_info() 15 dump_filehdr_info()
98 { 16 {
99 time_t timestamp; 17 time_t timestamp;
100 struct tm *timedec; 18 struct tm *timedec;
106 timedec->tm_hour, timedec->tm_min, timedec->tm_sec); 24 timedec->tm_hour, timedec->tm_min, timedec->tm_sec);
107 printf("file flags: 0x%x\n", get_u16(filehdr_struct->f_flags)); 25 printf("file flags: 0x%x\n", get_u16(filehdr_struct->f_flags));
108 printf("%u sections, %u symtab entries\n", nsections, nsymtab); 26 printf("%u sections, %u symtab entries\n", nsections, nsymtab);
109 return(0); 27 return(0);
110 } 28 }
29
30 cmd_sechdr()
31 {
32 unsigned n;
33 struct internal_scnhdr *inf;
34
35 get_int_section_table();
36 for (n = 0; n < nsections; n++) {
37 inf = sections + n;
38 printf("#%d: %s size=%u, flags=0x%x\n", n, inf->name,
39 inf->size, inf->flags);
40 printf("\t%u reloc, %u line entries\n",
41 inf->nreloc, inf->nlineent);
42 }
43 exit(0);
44 }