comparison leo-obj/tool/lowlevel.c @ 130:87b82398a08b

leo-obj project subtree started, tiobjd tool moved into it
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 06 Apr 2014 22:14:39 +0000
parents ticoff/lowlevel.c@2eef88395908
children
comparison
equal deleted inserted replaced
129:597143ba1c37 130:87b82398a08b
1 /*
2 * This C module implements the low-level steps 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 "filestruct.h"
13 #include "globals.h"
14
15 mmap_objfile()
16 {
17 int fd;
18 struct stat st;
19
20 fd = open(objfilename, O_RDONLY);
21 if (fd < 0) {
22 perror(objfilename);
23 exit(1);
24 }
25 fstat(fd, &st);
26 if (!S_ISREG(st.st_mode)) {
27 fprintf(stderr, "error: %s is not a regular file\n",
28 objfilename);
29 exit(1);
30 }
31 objfile_tot_size = st.st_size;
32 filemap = mmap(NULL, objfile_tot_size, PROT_READ, MAP_PRIVATE, fd, 0L);
33 if (filemap == MAP_FAILED) {
34 perror("mmap");
35 exit(1);
36 }
37 close(fd);
38 }
39
40 unsigned
41 get_u16(ptr)
42 u_char *ptr;
43 {
44 return ptr[0] | ptr[1] << 8;
45 }
46
47 get_s16(ptr)
48 u_char *ptr;
49 {
50 int i;
51
52 i = ptr[0] | ptr[1] << 8;
53 if (i >= 32768)
54 i -= 65536;
55 return(i);
56 }
57
58 unsigned
59 get_u32(ptr)
60 u_char *ptr;
61 {
62 return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24;
63 }
64
65 initial_parse_hdr()
66 {
67 unsigned symtab_offset;
68
69 filehdr_struct = (struct external_filehdr *) filemap;
70 if (get_u16(filehdr_struct->f_magic) != 0xC2) {
71 fprintf(stderr, "error: %s is not a TI COFF2 object\n",
72 objfilename);
73 exit(1);
74 }
75 if (get_u16(filehdr_struct->f_target_id) != 0x97) {
76 fprintf(stderr, "error: TI COFF object %s is not for TMS470\n",
77 objfilename);
78 exit(1);
79 }
80 if (get_u16(filehdr_struct->f_opthdr)) {
81 fprintf(stderr,
82 "error: %s has the \"optional\" header present\n",
83 objfilename);
84 exit(1);
85 }
86 sections_raw = (struct external_scnhdr *)
87 (filemap + sizeof(struct external_filehdr));
88 nsections = get_u16(filehdr_struct->f_nscns);
89 symtab_offset = get_u32(filehdr_struct->f_symptr);
90 symtab_raw = (struct external_syment *)(filemap + symtab_offset);
91 nsymtab = get_u32(filehdr_struct->f_nsyms);
92 strtab_offset = symtab_offset +
93 sizeof(struct external_syment) * nsymtab;
94 }