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>
36 #include "gc-internal.h"
38 unsigned char build_id[] =
39 #include "../../output/build-id.tmp"
43 open_binary(char *filename, int mode)
45 #ifdef LISP_FEATURE_WIN32
49 return open(filename, mode);
52 /* Search 'filename' for an embedded core. An SBCL core has, at the
53 * end of the file, a trailer containing the size of the core (an
54 * os_vm_offset_t) and a final signature word (the lispobj
55 * CORE_MAGIC). If this trailer is found at the end of the file, the
56 * start of the core can be determined from the core size.
58 * If an embedded core is present, this returns the offset into the
59 * file to load the core from, or -1 if no core is present. */
61 search_for_embedded_core(char *filename)
64 os_vm_offset_t lispobj_size = sizeof(lispobj);
65 os_vm_offset_t trailer_size = lispobj_size + sizeof(os_vm_offset_t);
66 os_vm_offset_t core_size, pos;
69 if ((fd = open_binary(filename, O_RDONLY)) < 0)
71 if (lseek(fd, -lispobj_size, SEEK_END) < 0)
73 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
76 if (header == CORE_MAGIC) {
77 if (lseek(fd, -trailer_size, SEEK_END) < 0)
79 if (read(fd, &core_size, sizeof(os_vm_offset_t)) < 0)
82 if (lseek(fd, -(core_size + trailer_size), SEEK_END) < 0)
84 pos = lseek(fd, 0, SEEK_CUR);
86 if (read(fd, &header, (size_t)lispobj_size) < lispobj_size)
89 if (header != CORE_MAGIC)
104 process_directory(int fd, u32 *ptr, int count, os_vm_offset_t file_offset)
106 struct ndir_entry *entry;
108 FSHOW((stderr, "/process_directory(..), count=%d\n", count));
110 for (entry = (struct ndir_entry *) ptr; --count>= 0; ++entry) {
112 long id = entry->identifier;
113 long offset = os_vm_page_size * (1 + entry->data_page);
114 os_vm_address_t addr =
115 (os_vm_address_t) (os_vm_page_size * entry->address);
116 lispobj *free_pointer = (lispobj *) addr + entry->nwords;
117 long len = os_vm_page_size * entry->page_count;
120 os_vm_address_t real_addr;
121 FSHOW((stderr, "/mapping %ld(0x%lx) bytes at 0x%lx\n",
122 (long)len, (long)len, (unsigned long)addr));
123 real_addr = os_map(fd, offset + file_offset, addr, len);
124 if (real_addr != addr) {
125 lose("file mapped in wrong place! "
126 "(0x%08x != 0x%08lx)\n",
132 FSHOW((stderr, "/space id = %ld, free pointer = 0x%lx\n",
133 id, (unsigned long)free_pointer));
136 case DYNAMIC_CORE_SPACE_ID:
137 #ifdef LISP_FEATURE_GENCGC
138 if (addr != (os_vm_address_t)DYNAMIC_SPACE_START) {
139 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx \n",
140 (long)addr, (long)DYNAMIC_SPACE_START);
141 lose("core/runtime address mismatch: DYNAMIC_SPACE_START\n");
144 if ((addr != (os_vm_address_t)DYNAMIC_0_SPACE_START) &&
145 (addr != (os_vm_address_t)DYNAMIC_1_SPACE_START)) {
146 fprintf(stderr, "in core: 0x%lx; in runtime: 0x%lx or 0x%lx\n",
148 (long)DYNAMIC_0_SPACE_START,
149 (long)DYNAMIC_1_SPACE_START);
150 lose("warning: core/runtime address mismatch: DYNAMIC_SPACE_START\n");
153 #if defined(ALLOCATION_POINTER)
154 SetSymbolValue(ALLOCATION_POINTER, (lispobj)free_pointer,0);
156 dynamic_space_free_pointer = free_pointer;
158 /* For stop-and-copy GC, this will be whatever the GC was
159 * using at the time. With GENCGC, this will always be
160 * space 0. (We checked above that for GENCGC,
161 * addr==DYNAMIC_SPACE_START.) */
162 current_dynamic_space = (lispobj *)addr;
164 case STATIC_CORE_SPACE_ID:
165 if (addr != (os_vm_address_t)STATIC_SPACE_START) {
166 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
167 (long)addr, (long)STATIC_SPACE_START);
168 lose("core/runtime address mismatch: STATIC_SPACE_START\n");
171 case READ_ONLY_CORE_SPACE_ID:
172 if (addr != (os_vm_address_t)READ_ONLY_SPACE_START) {
173 fprintf(stderr, "in core: 0x%lx - in runtime: 0x%lx\n",
174 (long)addr, (long)READ_ONLY_SPACE_START);
175 lose("core/runtime address mismatch: READ_ONLY_SPACE_START\n");
179 lose("unknown space ID %ld addr 0x%lx\n", id, (long)addr);
185 load_core_file(char *file, os_vm_offset_t file_offset)
187 lispobj *header, val, len, *ptr, remaining_len;
188 int fd = open_binary(file, O_RDONLY), count;
190 lispobj initial_function = NIL;
191 FSHOW((stderr, "/entering load_core_file(%s)\n", file));
193 fprintf(stderr, "could not open file \"%s\"\n", file);
198 lseek(fd, file_offset, SEEK_SET);
199 header = calloc(os_vm_page_size / sizeof(u32), sizeof(u32));
201 count = read(fd, header, os_vm_page_size);
202 if (count < os_vm_page_size) {
203 lose("premature end of core file\n");
205 SHOW("successfully read first page of core");
210 if (val != CORE_MAGIC) {
211 lose("invalid magic number in core: 0x%lx should have been 0x%x.\n",
215 SHOW("found CORE_MAGIC");
217 while (val != END_CORE_ENTRY_TYPE_CODE) {
220 remaining_len = len - 2; /* (-2 to cancel the two ++ operations) */
221 FSHOW((stderr, "/val=0x%ld, remaining_len=0x%ld\n",
222 (long)val, (long)remaining_len));
226 case END_CORE_ENTRY_TYPE_CODE:
227 SHOW("END_CORE_ENTRY_TYPE_CODE case");
230 case VERSION_CORE_ENTRY_TYPE_CODE:
231 SHOW("VERSION_CORE_ENTRY_TYPE_CODE case");
232 if (*ptr != SBCL_CORE_VERSION_INTEGER) {
233 lose("core file version (%d) != runtime library version (%d)\n",
235 SBCL_CORE_VERSION_INTEGER);
239 case BUILD_ID_CORE_ENTRY_TYPE_CODE:
240 SHOW("BUILD_ID_CORE_ENTRY_TYPE_CODE case");
244 FSHOW((stderr, "build_id[]=\"%s\"\n", build_id));
245 FSHOW((stderr, "remaining_len = %d\n", remaining_len));
246 if (remaining_len != strlen((const char *)build_id))
247 goto losing_build_id;
248 for (i = 0; i < remaining_len; ++i) {
249 FSHOW((stderr, "ptr[%d] = char = %d, expected=%d\n",
250 i, ptr[i], build_id[i]));
251 if (ptr[i] != build_id[i])
252 goto losing_build_id;
256 /* .core files are not binary-compatible between
257 * builds because we can't easily detect whether the
258 * sources were patched between the time the
259 * dumping-the-.core runtime was built and the time
260 * that the loading-the-.core runtime was built.
262 * (We could easily detect whether version.lisp-expr
263 * was changed, but people experimenting with patches
264 * don't necessarily update version.lisp-expr.) */
266 lose("can't load .core for different runtime, sorry\n");
269 case NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE:
270 SHOW("NEW_DIRECTORY_CORE_ENTRY_TYPE_CODE case");
271 process_directory(fd,
273 #ifndef LISP_FEATURE_ALPHA
274 remaining_len / (sizeof(struct ndir_entry) /
277 remaining_len / (sizeof(struct ndir_entry) /
283 case INITIAL_FUN_CORE_ENTRY_TYPE_CODE:
284 SHOW("INITIAL_FUN_CORE_ENTRY_TYPE_CODE case");
285 initial_function = (lispobj)*ptr;
288 #ifdef LISP_FEATURE_GENCGC
289 case PAGE_TABLE_CORE_ENTRY_TYPE_CODE:
292 size_t fdoffset = (*(ptr+1) + 1) * (os_vm_page_size);
296 lseek(fd, fdoffset + file_offset, SEEK_SET);
297 while ((bytes_read = read(fd, data, (size < 4096 ? size : 4096 )))
303 bytes_read -= sizeof(long);
304 page_table[offset++].first_object_offset = data[i++];
308 gencgc_partial_pickup = 1;
313 lose("unknown core file entry: %ld\n", (long)val);
316 ptr += remaining_len;
317 FSHOW((stderr, "/new ptr=%lx\n", (unsigned long)ptr));
319 SHOW("about to free(header)");
321 SHOW("returning from load_core_file(..)");
322 return initial_function;