0.9.10.16:
[sbcl.git] / src / runtime / x86-darwin-fix-rospace.c
1 /* ppc-darwin-fix-rospace.c - fix the segment and section output by ppc-darwin-mkrospace.c to have the correct size */
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <mach-o/loader.h>
7 #include <sys/types.h>
8 #include <sys/mman.h>
9 #include <sys/stat.h>
10 #include <sys/uio.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13
14 #include "sbcl.h"
15 #include "runtime.h"
16
17 #include "ppc-darwin-spacelist.h"
18
19 int main(int argc, char** argv)
20 {
21   int fd;
22   int i, spacei;
23   struct stat filestat;
24   struct mach_header *header;
25   void* file_pointer;
26   size_t filesize;
27   size_t old_commands_size;
28   char* window;
29   size_t segments_size;
30
31   /* Open the SBCL binary */
32   fd = open("sbcl", O_RDWR, 0); /* wr unx prgrmmrs, we cn't spll */
33   if (fd == -1) {
34     perror(argv[0]);
35     return -1;
36   }
37   if (fstat(fd, &filestat) == -1) {
38     close(fd);
39     perror(argv[0]);
40     return -1;
41   }
42   filesize = filestat.st_size;
43   if ((int) (file_pointer = mmap(NULL, filesize, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0)) == -1) {
44     perror(argv[0]);
45     close(fd);
46     return -1;
47   }
48
49   segments_size = 0;
50   spacei = 0;
51   header = (struct mach_header*) file_pointer;
52   window = (char*) (header + 1);
53   for (i = 0; i < header->ncmds; i++) {
54     struct load_command* current_segment;
55     current_segment = (struct load_command*) window;
56     segments_size += current_segment->cmdsize;
57     window += current_segment->cmdsize;
58     if (current_segment->cmd == LC_SEGMENT) {
59       int is_sbcl;
60       struct segment_command* seg = (struct segment_command*) current_segment;
61       struct section* sectptr;
62       int j, max;
63       max = seg->nsects;
64       if (strncmp("SBCL", seg->segname, 4) == 0) {
65         is_sbcl = 1;
66         seg->vmsize = space_sizes[spacei];
67       } else {
68         is_sbcl = 0;
69       }
70       seg++;
71       sectptr = (struct section*) seg;
72       for (j = 0; j < max; j++) {
73         if (is_sbcl) {
74           sectptr->size = space_sizes[spacei];
75           spacei++;
76         }
77         sectptr++;
78       }
79     }
80   }
81
82   munmap(file_pointer, filesize);
83   close(fd);
84   return 0;
85 }