changeset 0:075d70197fc2

add written files.
author taiki
date Sat, 02 Feb 2013 22:15:35 +0900
parents
children 1b796d0dd763
files Makefile loader.c sample/Makefile sample/sample.c
diffstat 4 files changed, 108 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Sat Feb 02 22:15:35 2013 +0900
@@ -0,0 +1,11 @@
+CC = cbc-gcc-4.6.0
+
+TARGET = loader
+
+CFLAGES = -Wall -O2 -g -o
+
+$(TARGET): loader.c
+	$(CC) $(CFLAGES) $(TARGET) loader.c 
+
+clean:
+	rm $(TARGET)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loader.c	Sat Feb 02 22:15:35 2013 +0900
@@ -0,0 +1,74 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <mach-o/loader.h>
+
+#define NAME_MAX_LENGTH 128
+#define MAX_SEGMENT_NUM 128
+
+
+void option_reader(int argc, char *argv[], char *filename)
+{
+    int i;
+    for (i=0; i<argc; i++) {
+        if(argv[i][0] == '-') {
+            if(strcmp(argv[i], "-name")==0) {
+                i++;
+                strncpy(filename ,argv[i], NAME_MAX_LENGTH);
+                printf("read %s\n", filename);
+            }
+        } else {
+            strncpy(filename ,argv[i], NAME_MAX_LENGTH);
+        }
+    }
+}
+
+int main(int argc, char*argv[])
+{
+    char filename[NAME_MAX_LENGTH];
+    option_reader(argc, argv, filename);
+
+    int fp;
+    if ((fp = open(filename, O_RDONLY)) < 0) {
+        fprintf(stderr, "can not open file\t: %s\n", filename);
+        exit(1);
+    }
+
+    struct stat sb;
+    fstat(fp, &sb);
+
+    char *head = mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, fp, 0);
+
+    struct mach_header_64 *mh64 = (struct mach_header_64 *)head;
+
+    if (MH_MAGIC_64 != mh64->magic) {
+       fprintf(stderr, "This is not mach header 64.\n");
+       return 0;
+    }
+
+    printf("%s is 64bit Mach-O file.\n", filename);
+
+    if (MH_EXECUTE != mh64->filetype) {
+        fprintf(stderr, "This is not executable file.\n");
+        return 0;
+    }
+
+    printf("%s is executable file.\n", filename);
+
+    if (CPU_TYPE_X86_64 == mh64->cputype) printf("CPU : x86_64\n"); 
+
+    int i = 0;
+    int mh64_size = sizeof(struct mach_header_64);
+    int lc_size = sizeof(struct load_command);
+    for (;i < mh64->ncmds; i++) {
+        struct load_command * lc = (struct load_command*)head + lc_size * i;
+    }
+
+    close(fp);
+
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample/Makefile	Sat Feb 02 22:15:35 2013 +0900
@@ -0,0 +1,15 @@
+CC = cbc-gcc-4.6.0
+CFLAGS = -O2 \
+		-Wall \
+		-g \
+		-c
+
+TARGET = sample
+
+$(TARGET) : sample.o
+
+sample.o: sample.c
+	$(CC) $(CFLAGS) -o $@ $^
+
+clean:
+	rm -r *.o $(TARGET) 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sample/sample.c	Sat Feb 02 22:15:35 2013 +0900
@@ -0,0 +1,8 @@
+int main()
+{
+    int a = 0;
+    int b = 2;
+    int c = 3;
+
+    a = b + c;
+}