annotate loader.c @ 0:075d70197fc2

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