FreeCalypso > hg > freecalypso-reveng
comparison arm7dis/common.c @ 86:537cf2245d98
beginning of ARM7 disassembler
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Fri, 28 Mar 2014 07:01:27 +0000 |
parents | |
children | f7fba8518fa2 |
comparison
equal
deleted
inserted
replaced
85:3c420895f68f | 86:537cf2245d98 |
---|---|
1 #include <sys/types.h> | |
2 #include <sys/file.h> | |
3 #include <sys/stat.h> | |
4 #include <sys/mman.h> | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <unistd.h> | |
8 | |
9 char *binfilename; | |
10 u_char *filemap; | |
11 u_long disasm_len, base_vma; | |
12 | |
13 common_init(argc, argv, instr_size) | |
14 char **argv; | |
15 { | |
16 int fd; | |
17 struct stat st; | |
18 u_long fileoff; | |
19 | |
20 if (argc < 2 || argc > 5) { | |
21 fprintf(stderr, | |
22 "usage: %s binfile [file-offset [len [vaddr]]]\n", | |
23 argv[0]); | |
24 exit(1); | |
25 } | |
26 binfilename = argv[1]; | |
27 fd = open(binfilename, O_RDONLY); | |
28 if (fd < 0) { | |
29 perror(binfilename); | |
30 exit(1); | |
31 } | |
32 fstat(fd, &st); | |
33 if (!S_ISREG(st.st_mode)) { | |
34 fprintf(stderr, "error: %s is not a regular file\n", | |
35 binfilename); | |
36 exit(1); | |
37 } | |
38 if (argc > 2) | |
39 fileoff = strtoul(argv[2], 0, 0); | |
40 else | |
41 fileoff = 0; | |
42 if (fileoff > st.st_size) { | |
43 fprintf(stderr, | |
44 "error: specified file offset is past the end of file\n"); | |
45 exit(1); | |
46 } | |
47 if (argc > 3) { | |
48 disasm_len = strtoul(argv[3], 0, 0); | |
49 if (disasm_len > st.st_size - fileoff) { | |
50 fprintf(stderr, | |
51 "error: specified length is past the end of file\n"); | |
52 exit(1); | |
53 } | |
54 } else | |
55 disasm_len = st.st_size - fileoff; | |
56 if (disasm_len & (instr_size - 1)) { | |
57 fprintf(stderr, | |
58 "error: length of region to be disassembled must be a multiple of %d bytes\n", | |
59 instr_size); | |
60 exit(1); | |
61 } | |
62 filemap = mmap(NULL, disasm_len, PROT_READ, MAP_PRIVATE, fd, fileoff); | |
63 if (filemap == MAP_FAILED) { | |
64 perror("mmap"); | |
65 exit(1); | |
66 } | |
67 close(fd); | |
68 if (argc > 4) | |
69 base_vma = strtoul(argv[4], 0, 0); | |
70 else | |
71 base_vma = fileoff; | |
72 return(0); | |
73 } | |
74 | |
75 unsigned | |
76 get_u16(ptr) | |
77 u_char *ptr; | |
78 { | |
79 return ptr[0] | ptr[1] << 8; | |
80 } | |
81 | |
82 unsigned | |
83 get_u32(ptr) | |
84 u_char *ptr; | |
85 { | |
86 return ptr[0] | ptr[1] << 8 | ptr[2] << 16 | ptr[3] << 24; | |
87 } |