1 |
gbeauche |
1.1 |
/* |
2 |
|
|
* lowmem.c - enable access to low memory globals on Darwin |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2003 Michael Z. Sliczniak |
5 |
|
|
* |
6 |
gbeauche |
1.3 |
* Basilisk II (C) 1997-2005 Christian Bauer |
7 |
gbeauche |
1.1 |
* |
8 |
|
|
* This program is free software; you can redistribute it and/or modify |
9 |
|
|
* it under the terms of the GNU General Public License as published by |
10 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
11 |
|
|
* (at your option) any later version. |
12 |
|
|
* |
13 |
|
|
* This program is distributed in the hope that it will be useful, |
14 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
|
|
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
19 |
|
|
* along with this program; if not, write to the Free Software |
20 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#include <sys/types.h> |
24 |
|
|
#include <sys/mman.h> |
25 |
|
|
#include <fcntl.h> |
26 |
|
|
#include <errno.h> |
27 |
|
|
#include <stdio.h> |
28 |
asvitkine |
1.4 |
#include <stdlib.h> |
29 |
gbeauche |
1.1 |
#include <string.h> |
30 |
|
|
#include <mach/vm_prot.h> |
31 |
|
|
#include <mach-o/loader.h> |
32 |
|
|
|
33 |
|
|
static const char progname[] = "lowmem"; |
34 |
|
|
|
35 |
|
|
/* |
36 |
|
|
* Under Mach there is very little assumed about the memory map of object |
37 |
|
|
* files. It is the job of the loader to create the initial memory map of an |
38 |
|
|
* executable. In a Mach-O executable there will be numerous loader commands |
39 |
|
|
* that the loader must process. Some of these will create the initial memory |
40 |
|
|
* map used by the executable. Under Darwin the static object file linker, |
41 |
|
|
* ld, automatically adds the __PAGEZERO segment to all executables. The |
42 |
|
|
* default size of this segment is the page size of the target system and |
43 |
|
|
* the initial and maximum permissions are set to allow no access. This is so |
44 |
|
|
* that all programs fault on a NULL pointer dereference. Arguably this is |
45 |
|
|
* incorrect and the maximum permissions shoould be rwx so that programs can |
46 |
|
|
* change this default behavior. Then programs could be written that assume |
47 |
|
|
* a null string at the null address, which was the convention on some |
48 |
|
|
* systems. In our case we need to have 8K mapped at zero for the low memory |
49 |
|
|
* globals and this program modifies the segment load command in the |
50 |
|
|
* basiliskII executable so that it can be used for data. |
51 |
|
|
*/ |
52 |
|
|
|
53 |
|
|
int |
54 |
|
|
main(int argc, const char *argv[]) |
55 |
|
|
{ |
56 |
|
|
int fd; |
57 |
|
|
char *addr; |
58 |
|
|
struct mach_header *machhead; |
59 |
|
|
struct segment_command *sc_cmd; |
60 |
|
|
|
61 |
|
|
if (argc != 2) { |
62 |
|
|
(void)fprintf(stderr, "Usage: %s executable\n", progname); |
63 |
|
|
exit(1); |
64 |
|
|
} |
65 |
|
|
|
66 |
|
|
fd = open(argv[1], O_RDWR, 0); |
67 |
|
|
if (fd == -1) { |
68 |
|
|
(void)fprintf(stderr, "%s: could not open %s: %s\n", |
69 |
|
|
progname, argv[1], strerror(errno)); |
70 |
|
|
exit(1); |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
/* |
74 |
|
|
* Size does not really matter, it will be rounded-up to a multiple |
75 |
|
|
* of the page size automatically. |
76 |
|
|
*/ |
77 |
|
|
addr = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, |
78 |
|
|
MAP_FILE | MAP_SHARED, fd, 0); |
79 |
|
|
if (addr == NULL) { |
80 |
|
|
(void)fprintf(stderr, "%s: could not mmap %s: %s\n", |
81 |
|
|
progname, argv[1], strerror(errno)); |
82 |
|
|
exit(1); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
/* |
86 |
|
|
* Check to see if the Mach-O magic bytes are in the header. |
87 |
|
|
* If we cared about cross compiling we would also check against |
88 |
|
|
* MH_CIGAM and then change the endianness with every access, but |
89 |
|
|
* we do not care about that. |
90 |
|
|
*/ |
91 |
|
|
machhead = (void *)addr; |
92 |
|
|
if (machhead->magic != MH_MAGIC) { |
93 |
|
|
(void)fprintf(stderr, "%s: %s does not appear to be a Mach-O object file\n", |
94 |
|
|
progname, argv[1]); |
95 |
|
|
exit(1); |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
if (machhead->filetype != MH_EXECUTE) { |
99 |
|
|
(void)fprintf(stderr, "%s: %s does not appear to be an executable file\n", |
100 |
|
|
progname, argv[1]); |
101 |
|
|
exit(1); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
if (machhead->ncmds == 0) { |
105 |
|
|
(void)fprintf(stderr, "%s: %s does not contain any load commands\n", |
106 |
|
|
progname, argv[1]); |
107 |
|
|
exit(1); |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
sc_cmd = (void *)&machhead[1]; |
111 |
|
|
if (sc_cmd->cmd != LC_SEGMENT){ |
112 |
|
|
(void)fprintf(stderr, "%s: load segment not first command in %s\n", |
113 |
|
|
progname, argv[1]); |
114 |
|
|
exit(1); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
if (strncmp(sc_cmd->segname, "__PAGEZERO", |
118 |
|
|
sizeof (*sc_cmd->segname))) { |
119 |
|
|
(void)fprintf(stderr, "%s: zero page not first segment in %s\n", |
120 |
|
|
progname, argv[1]); |
121 |
|
|
exit(1); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
/* change the permissions */ |
125 |
|
|
sc_cmd->maxprot = VM_PROT_ALL; |
126 |
|
|
sc_cmd->initprot = VM_PROT_ALL; |
127 |
|
|
|
128 |
|
|
/* |
129 |
|
|
* We do not make __PAGEZERO 8K in this program because then |
130 |
|
|
* all of the offsets would be wrong in the object file after |
131 |
|
|
* this segment. Instead we use the -pagezero_size option |
132 |
|
|
* to link the executable. |
133 |
|
|
*/ |
134 |
|
|
if (msync(addr, 0x1000, MS_SYNC) == -1) { |
135 |
|
|
(void)fprintf(stderr, "%s: could not sync %s: %s\n", |
136 |
|
|
progname, argv[1], strerror(errno)); |
137 |
|
|
exit(1); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
if (munmap(addr, 0x1000) == -1) { |
141 |
|
|
(void)fprintf(stderr, "%s: could not unmap %s: %s\n", |
142 |
|
|
progname, argv[1], strerror(errno)); |
143 |
|
|
exit(1); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
(void)close(fd); |
147 |
|
|
|
148 |
|
|
exit(0); |
149 |
|
|
} |