FreeCalypso > hg > freecalypso-reveng
comparison ticoff/lowlevel.c @ 74:2eef88395908
tiobjd: a little refactoring
| author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
|---|---|
| date | Tue, 25 Mar 2014 18:55:36 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 73:10f3fbff5e97 | 74:2eef88395908 |
|---|---|
| 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 } |
