comparison loader.c @ 0:075d70197fc2

add written files.
author taiki
date Sat, 02 Feb 2013 22:15:35 +0900
parents
children 1b796d0dd763
comparison
equal deleted inserted replaced
-1:000000000000 0:075d70197fc2
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <mach-o/loader.h>
9
10 #define NAME_MAX_LENGTH 128
11 #define MAX_SEGMENT_NUM 128
12
13
14 void option_reader(int argc, char *argv[], char *filename)
15 {
16 int i;
17 for (i=0; i<argc; i++) {
18 if(argv[i][0] == '-') {
19 if(strcmp(argv[i], "-name")==0) {
20 i++;
21 strncpy(filename ,argv[i], NAME_MAX_LENGTH);
22 printf("read %s\n", filename);
23 }
24 } else {
25 strncpy(filename ,argv[i], NAME_MAX_LENGTH);
26 }
27 }
28 }
29
30 int main(int argc, char*argv[])
31 {
32 char filename[NAME_MAX_LENGTH];
33 option_reader(argc, argv, filename);
34
35 int fp;
36 if ((fp = open(filename, O_RDONLY)) < 0) {
37 fprintf(stderr, "can not open file\t: %s\n", filename);
38 exit(1);
39 }
40
41 struct stat sb;
42 fstat(fp, &sb);
43
44 char *head = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fp, 0);
45
46 struct mach_header_64 *mh64 = (struct mach_header_64 *)head;
47
48 if (MH_MAGIC_64 != mh64->magic) {
49 fprintf(stderr, "This is not mach header 64.\n");
50 return 0;
51 }
52
53 printf("%s is 64bit Mach-O file.\n", filename);
54
55 if (MH_EXECUTE != mh64->filetype) {
56 fprintf(stderr, "This is not executable file.\n");
57 return 0;
58 }
59
60 printf("%s is executable file.\n", filename);
61
62 if (CPU_TYPE_X86_64 == mh64->cputype) printf("CPU : x86_64\n");
63
64 int i = 0;
65 int mh64_size = sizeof(struct mach_header_64);
66 int lc_size = sizeof(struct load_command);
67 for (;i < mh64->ncmds; i++) {
68 struct load_command * lc = (struct load_command*)head + lc_size * i;
69 }
70
71 close(fp);
72
73 return 0;
74 }