2 * A saved SBCL system is a .core file; the code here helps us accept
3 * such a file as input.
7 * This software is part of the SBCL system. See the README file for
10 * This software is derived from the CMU CL system, which was
11 * written at Carnegie Mellon University and released into the
12 * public domain. The software is in the public domain and is
13 * provided with absolutely no warranty. See the COPYING and CREDITS
14 * files for more information.
21 #include <sys/types.h>
40 #include "gc-internal.h"
42 unsigned char build_id[] =
43 #include "../../output/build-id.tmp"
47 open_binary(char *filename, int mode)
49 #ifdef LISP_FEATURE_WIN32
53 return open(filename, mode);
56 /* Search 'filename' for an embedded core. An SBCL core has, at the
57 * end of the file, a trailer containing the size of the core (an
58 * os_vm_offset_t) and a final signature word (the lispobj
59 * CORE_MAGIC). If this trailer is found at the end of the file, the
60 * start of the core can be determined from the core size.
62 * If an embedded core is present, this returns the offset into the
63 * file to load the core from, or -1 if no core is present. */
65 search_for_embedded_core(char *filename)
68 os_vm_offset_t lispobj_size = sizeof(lispobj);
69 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
70 os_vm_offset_t core_size, pos;
73 if ((fd = open_binary(filename, O_RDONLY)) < 0)
75 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
77 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
80 if (header == CORE_MAGIC) {
81 if (lseek(fd, -trailer_size, SEEK_END) < 0)
83 if (read(fd, &core_size, sizeof(os_vm_offset_t)) < 0)
86 if (lseek(fd, -(core_size + trailer_size), SEEK_END) < 0)
88 pos = lseek(fd, 0, SEEK_CUR);
90 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
93 if (header != CORE_MAGIC)
108 process_directory(int fd, u32 *ptr, int count, os_vm_offset_t file_offset)
110 struct ndir_entry *entry;
112 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
114 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
116 long id = entry->identifier;
117 long offset = os_vm_page_size * (1 + entry->data_page);
118 os_vm_address_t addr =
119 (os_vm_address_t) (os_vm_page_size * entry->address);
120 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
121 long len = os_vm_page_size * entry->page_count;
124 os_vm_address_t real_addr;
125 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
126 (long)len, (long)len, (unsigned long)addr));
127 real_addr = os_map(fd, offset + file_offset, addr, len);
128 if (real_addr != addr) {
129 lose("file mapped in wrong place! "
130 "(0x%08x != 0x%08lx)\n",
136 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
137 id, (unsigned long)free_pointer));
140 case DYNAMIC_CORE_SPACE_ID:
141 #ifdef LISP_FEATURE_GENCGC
142 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
143 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
144 (long)addr, (long)DYNAMIC_SPACE_START);
145 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
148 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
149 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
150 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
152 (long)DYNAMIC_0_SPACE_START,
153 (long)DYNAMIC_1_SPACE_START);
154 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
157 #if defined(ALLOCATION_POINTER)
158 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
160 dynamic_space_free_pointer = free_pointer;
162 /* For stop-and-copy GC, this will be whatever the GC was
163 * using at the time. With GENCGC, this will always be
164 * space 0. (We checked above that for GENCGC,
165 * addr==DYNAMIC_SPACE_START.) */
166 current_dynamic_space = (lispobj *)addr;
168 case STATIC_CORE_SPACE_ID:
169 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
170 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
171 (long)addr, (long)STATIC_SPACE_START);
172 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
175 case READ_ONLY_CORE_SPACE_ID:
176 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
177 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
178 (long)addr, (long)READ_ONLY_SPACE_START);
179 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
183 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
189 load_core_file(char *file, os_vm_offset_t file_offset)
191 lispobj *header, val, len, *ptr, remaining_len;
192 int fd = open_binary(file, O_RDONLY), count;
194 lispobj initial_function = NIL;
195 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
197 fprintf(stderr, "could not open file \"%s\"\n", file);
202 lseek(fd, file_offset, SEEK_SET);
203 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
205 count = read(fd, header, os_vm_page_size);
206 if (count < os_vm_page_size) {
207 lose("premature end of core file\n");
209 SHOW("successfully read first page of core");
214 if (val != CORE_MAGIC) {
215 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
219 SHOW("found CORE_MAGIC");
221 while (val != END_CORE_ENTRY_TYPE_CODE) {
224 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
225 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
226 (long)val, (long)remaining_len));
230 case END_CORE_ENTRY_TYPE_CODE:
231 SHOW("END_CORE_ENTRY_TYPE_CODE case");
234 case VERSION_CORE_ENTRY_TYPE_CODE:
235 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
236 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
237 lose("core file version (%d) != runtime library version (%d)\n",
239 SBCL_CORE_VERSION_INTEGER);
243 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
244 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
248 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
249 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
250 if (remaining_len != strlen((const char *)build_id))
251 goto losing_build_id;
252 for (i = 0; i < remaining_len; ++i) {
253 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
254 i, ptr[i], build_id[i]));
255 if (ptr[i] != build_id[i])
256 goto losing_build_id;
260 /* .core files are not binary-compatible between
261 * builds because we can't easily detect whether the
262 * sources were patched between the time the
263 * dumping-the-.core runtime was built and the time
264 * that the loading-the-.core runtime was built.
266 * (We could easily detect whether version.lisp-expr
267 * was changed, but people experimenting with patches
268 * don't necessarily update version.lisp-expr.) */
270 lose("can't load .core for different runtime, sorry\n");
273 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
274 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
275 process_directory(fd,
277 #ifndef LISP_FEATURE_ALPHA
278 remaining_len / (sizeof(struct ndir_entry) /
281 remaining_len / (sizeof(struct ndir_entry) /
287 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
288 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
289 initial_function = (lispobj)*ptr;
292 #ifdef LISP_FEATURE_GENCGC
293 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
296 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
300 lseek(fd, fdoffset + file_offset, SEEK_SET);
301 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
307 bytes_read -= sizeof(long);
308 page_table[offset++].first_object_offset = data[i++];
312 gencgc_partial_pickup = 1;
317 lose("unknown core file entry: %ld\n", (long)val);
320 ptr += remaining_len;
321 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
323 SHOW("about to free(header)");
325 SHOW("returning from load_core_file(..)");
326 return initial_function;