annotate loader.c @ 2:747f68297ba5 default tip

add c language mach-O loader program. this program only analize, can't still load now.
author taiki <taiki@cr.ie.u-ryukyu.ac.jp>
date Tue, 04 Mar 2014 14:41:35 +0900
parents 1b796d0dd763
children
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
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
10 #define NAME_MAX_LENGTH 256
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
11 #define MAX_SEGMENT_NUM 256
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
12
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
13 struct mach_o_data {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
14 int mh64_size;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
15 int sc64_size;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
16 int sect64_size;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
17 };
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
18
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
19 struct data_count {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
20 int nsects_count;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
21 int sc_count;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
22 };
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
23
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
24 /* prototype */
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
25
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
26 __code analyze_mach_o(struct mach_o_data md, struct mach_header_64 *mh64, struct segment_command_64 *sc64, struct section_64 *sect64, struct data_count dc, char *head, int fp, int j);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
27
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
28 /* end */
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
29
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
30 __code finish(int fp, struct segment_command_64 *sc64, struct section_64 *sect64)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
31 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
32 close(fp);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
33 free(sc64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
34 free(sect64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
35 printf("close \n");
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
36 }
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
37
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
38 __code make_section(
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
39 struct mach_o_data md,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
40 struct mach_header_64 *mh64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
41 struct segment_command_64 *sc64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
42 struct section_64 *sect64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
43 struct data_count dc,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
44 char *head,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
45 char *c_sect64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
46 int fp,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
47 int scsect_count,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
48 int sectsize_count,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
49 int cmd_count)
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
50 {
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
51 if(scsect_count < sc64->nsects) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
52 if(sectsize_count < md.sect64_size) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
53 c_sect64[sectsize_count] = head[md.mh64_size + md.sc64_size * dc.sc_count + (dc.nsects_count + scsect_count) * md.sect64_size + sectsize_count];
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
54 sectsize_count++;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
55 goto make_section(md, mh64, sc64, sect64, dc, head, c_sect64, fp, scsect_count, sectsize_count, cmd_count);
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
56 }
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
57 sect64 = (struct section_64 *)c_sect64;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
58 printf("%s %s \n", sect64->segname, sect64->sectname);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
59 sect64 = NULL;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
60 scsect_count++;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
61 sectsize_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
62 goto make_section(md, mh64, sc64, sect64, dc, head, c_sect64, fp, scsect_count, sectsize_count, cmd_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
63 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
64 free(c_sect64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
65 dc.nsects_count += sc64->nsects;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
66 cmd_count++;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
67 goto analyze_mach_o(md, mh64, sc64, sect64, dc, head, fp, cmd_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
68 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
69
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
70 __code check_segment(
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
71 struct mach_o_data md,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
72 struct mach_header_64 *mh64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
73 struct segment_command_64 *sc64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
74 struct section_64 *sect64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
75 struct data_count dc,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
76 char *head,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
77 int fp,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
78 int scsect_count,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
79 int cmd_count)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
80 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
81 if (LC_SEGMENT_64 == sc64->cmd) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
82 printf("segment command :LC_SEGMENT_64");
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
83 printf("\tsegname :%s \n", sc64->segname);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
84 printf("\tvmaddr :%#llx \n", sc64->vmaddr);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
85 printf("\tvmsize :%lld \n", sc64->vmsize);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
86 printf("\tfileoff :%#llx \n", sc64->fileoff);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
87 printf("\tnumber of section :%d\n", sc64->nsects);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
88 dc.sc_count++;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
89 char *c_sect64 = (char *)malloc(sizeof(struct section_64 *));
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
90 int sectsize_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
91 goto make_section(md, mh64, sc64, sect64, dc, head, c_sect64, fp, scsect_count , sectsize_count, cmd_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
92 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
93 goto finish(fp, sc64, sect64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
94 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
95
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
96 __code make_segment_command(
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
97 struct mach_o_data md,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
98 struct mach_header_64 *mh64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
99 struct segment_command_64 *sc64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
100 struct section_64 *sect64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
101 struct data_count dc,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
102 char *head,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
103 char *c_sc64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
104 int fp,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
105 int scsize_count,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
106 int cmd_count)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
107 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
108 if (cmd_count < mh64->ncmds) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
109 if (scsize_count < md.sc64_size) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
110 c_sc64[scsize_count] = head[md.mh64_size + md.sc64_size * dc.sc_count + dc.nsects_count * md.sect64_size + scsize_count];
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
111 scsize_count++;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
112 goto make_segment_command(md, mh64, sc64, sect64, dc, head, c_sc64,fp, scsize_count, cmd_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
113 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
114 sc64 = (struct segment_command_64 *)c_sc64;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
115 int scsect_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
116 goto check_segment(md, mh64, sc64, sect64, dc, head, fp, scsect_count ,cmd_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
117 } else {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
118 free(c_sc64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
119 goto finish(fp, sc64, sect64);
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
120 }
075d70197fc2 add written files.
taiki
parents:
diff changeset
121 }
075d70197fc2 add written files.
taiki
parents:
diff changeset
122
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
123
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
124 __code analyze_mach_o(
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
125 struct mach_o_data md,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
126 struct mach_header_64 *mh64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
127 struct segment_command_64 *sc64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
128 struct section_64 *sect64,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
129 struct data_count dc,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
130 char *head,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
131 int fp,
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
132 int cmd_count)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
133 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
134 int scsize_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
135 char *c_sc64 = (char *)malloc(sizeof(struct segment_command_64));
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
136 goto make_segment_command(md, mh64, sc64, sect64, dc, head, c_sc64, fp, scsize_count, cmd_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
137 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
138
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
139 __code ready_analyzing(struct mach_header_64 *mh64, char *head, int fp)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
140 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
141 struct mach_o_data md;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
142 md.mh64_size = sizeof(struct mach_header_64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
143 md.sc64_size = sizeof(struct segment_command_64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
144 md.sect64_size = sizeof(struct section_64);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
145
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
146 struct segment_command_64 *sc64 = (struct segment_command_64 *)malloc(sizeof(struct segment_command_64));
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
147 struct section_64 *sect64 = (struct section_64 *)malloc(sizeof(struct section_64));
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
148
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
149 struct data_count dc;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
150 dc.nsects_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
151 dc.sc_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
152 int cmd_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
153 goto analyze_mach_o(md, mh64, sc64, sect64, dc, head,fp, cmd_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
154 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
155
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
156 __code judge_mach_o_file(struct mach_header_64 *mh64, char *head, char *filename, int fp)
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
157 {
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
158 printf("%s is executable file.\n", filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
159 printf("number of load commands : %d\n", mh64->ncmds);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
160 printf("size of cmds : %d\n", mh64->sizeofcmds);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
161
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
162 free(filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
163
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
164 if (CPU_TYPE_X86_64 == mh64->cputype) printf("CPU : x86_64\n");
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
165 goto ready_analyzing(mh64, head, fp);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
166 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
167
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
168 __code judge_filetype(struct mach_header_64 *mh64 , char *head, char *filename, int fp)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
169 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
170 if (MH_EXECUTE != mh64->filetype) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
171 fprintf(stderr, "This is not executable file.\n");
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
172 return;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
173 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
174 goto judge_mach_o_file(mh64, head, filename, fp);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
175 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
176
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
177 __code judge_header(int fp, struct stat sb, char *head, char *filename)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
178 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
179 struct mach_header_64 *mh64 = (struct mach_header_64 *)head;
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
180
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
181 if (MH_MAGIC_64 != mh64->magic) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
182 fprintf(stderr, "This is not mach header 64.\n");
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
183 return;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
184 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
185 if (MH_MAGIC_64 == mh64->magic)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
186 printf("%s is 64bit Mach-O file.\n", filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
187
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
188 goto judge_filetype(mh64, head, filename, fp);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
189 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
190
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
191 __code file_map(int fp, struct stat sb, char *filename)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
192 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
193 char *head = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fp, 0);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
194 goto judge_header(fp, sb, head, filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
195 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
196
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
197 __code get_fstat(int fp, char *filename)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
198 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
199 struct stat sb;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
200 fstat(fp, &sb);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
201 goto file_map(fp, sb, filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
202 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
203
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
204 __code file_open(char *filename)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
205 {
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
206 int fp;
075d70197fc2 add written files.
taiki
parents:
diff changeset
207 if ((fp = open(filename, O_RDONLY)) < 0) {
075d70197fc2 add written files.
taiki
parents:
diff changeset
208 fprintf(stderr, "can not open file\t: %s\n", filename);
075d70197fc2 add written files.
taiki
parents:
diff changeset
209 exit(1);
075d70197fc2 add written files.
taiki
parents:
diff changeset
210 }
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
211 goto get_fstat(fp, filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
212 }
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
213
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
214 __code option_reader(int argc, char *argv[], char filename[NAME_MAX_LENGTH], int i)
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
215 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
216 if (i < argc) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
217 if(argv[i][0] == '-') {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
218 if(strcmp(argv[i], "-name")==0) {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
219 strncpy(filename, argv[i], NAME_MAX_LENGTH);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
220 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
221 } else {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
222 strncpy(filename, argv[i], NAME_MAX_LENGTH);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
223 }
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
224 printf("read %s\n", filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
225 i++;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
226 goto option_reader(argc, argv, filename, i);
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
227 }
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
228 printf("use %s \n", filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
229 goto file_open(filename);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
230 }
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
231
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
232 void mach_o_loader(int argc, char *argv[])
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
233 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
234 int argv_count = 0;
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
235 char *filename = (char *)malloc(sizeof(char)*NAME_MAX_LENGTH);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
236 goto option_reader(argc, argv, filename, argv_count);
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
237 }
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
238
1
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
239 int main(int argc, char*argv[])
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
240 {
1b796d0dd763 display segment command and section in Mach-O executable.
taiki
parents: 0
diff changeset
241 mach_o_loader(argc, argv);
0
075d70197fc2 add written files.
taiki
parents:
diff changeset
242 return 0;
075d70197fc2 add written files.
taiki
parents:
diff changeset
243 }