loader.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /* Copyright 2001,2009-2015,2017-2018,2021
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <alignof.h>
  19. #include <assert.h>
  20. #include <byteswap.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <full-read.h>
  24. #include <string.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <verify.h>
  29. #ifdef HAVE_SYS_MMAN_H
  30. #include <sys/mman.h>
  31. #endif
  32. #include "boolean.h"
  33. #include "bytevectors.h"
  34. #include "elf.h"
  35. #include "eval.h"
  36. #include "extensions.h"
  37. #include "gsubr.h"
  38. #include "list.h"
  39. #include "pairs.h"
  40. #include "programs.h"
  41. #include "strings.h"
  42. #include "threads.h"
  43. #include "version.h"
  44. #include "loader.h"
  45. #ifdef __MINGW32__
  46. #include "posix-w32.h"
  47. #endif
  48. /* This file contains the loader for Guile's on-disk format: ELF with
  49. some custom tags in the dynamic segment. */
  50. #if SIZEOF_UINTPTR_T == 4
  51. #define Elf_Half Elf32_Half
  52. #define Elf_Word Elf32_Word
  53. #define Elf_Ehdr Elf32_Ehdr
  54. #define ELFCLASS ELFCLASS32
  55. #define Elf_Phdr Elf32_Phdr
  56. #define Elf_Dyn Elf32_Dyn
  57. #elif SIZEOF_UINTPTR_T == 8
  58. #define Elf_Half Elf64_Half
  59. #define Elf_Word Elf64_Word
  60. #define Elf_Ehdr Elf64_Ehdr
  61. #define ELFCLASS ELFCLASS64
  62. #define Elf_Phdr Elf64_Phdr
  63. #define Elf_Dyn Elf64_Dyn
  64. #else
  65. #error
  66. #endif
  67. #define DT_LOGUILE 0x37146000 /* Start of Guile-specific */
  68. #define DT_GUILE_GC_ROOT 0x37146000 /* Offset of GC roots */
  69. #define DT_GUILE_GC_ROOT_SZ 0x37146001 /* Size in machine words of GC
  70. roots */
  71. #define DT_GUILE_ENTRY 0x37146002 /* Address of entry thunk */
  72. #define DT_GUILE_VM_VERSION 0x37146003 /* Bytecode version */
  73. #define DT_GUILE_FRAME_MAPS 0x37146004 /* Frame maps */
  74. #define DT_HIGUILE 0x37146fff /* End of Guile-specific */
  75. #ifdef WORDS_BIGENDIAN
  76. #define ELFDATA ELFDATA2MSB
  77. #else
  78. #define ELFDATA ELFDATA2LSB
  79. #endif
  80. /* The page size. */
  81. static size_t page_size;
  82. static void register_elf (char *data, size_t len, char *frame_maps);
  83. enum bytecode_kind
  84. {
  85. BYTECODE_KIND_NONE,
  86. BYTECODE_KIND_GUILE_3_0
  87. };
  88. static SCM
  89. pointer_to_procedure (enum bytecode_kind bytecode_kind, char *ptr)
  90. {
  91. switch (bytecode_kind)
  92. {
  93. case BYTECODE_KIND_GUILE_3_0:
  94. {
  95. return scm_i_make_program ((uint32_t *) ptr);
  96. }
  97. case BYTECODE_KIND_NONE:
  98. default:
  99. abort ();
  100. }
  101. }
  102. static const char*
  103. check_elf_header (const Elf_Ehdr *header)
  104. {
  105. if (!(header->e_ident[EI_MAG0] == ELFMAG0
  106. && header->e_ident[EI_MAG1] == ELFMAG1
  107. && header->e_ident[EI_MAG2] == ELFMAG2
  108. && header->e_ident[EI_MAG3] == ELFMAG3))
  109. return "not an ELF file";
  110. if (header->e_ident[EI_CLASS] != ELFCLASS)
  111. return "ELF file does not have native word size";
  112. if (header->e_ident[EI_DATA] != ELFDATA)
  113. return "ELF file does not have native byte order";
  114. if (header->e_ident[EI_VERSION] != EV_CURRENT)
  115. return "bad ELF version";
  116. if (header->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
  117. return "unexpected OS ABI";
  118. if (header->e_ident[EI_ABIVERSION] != 0)
  119. return "unexpected ABI version";
  120. if (header->e_type != ET_DYN)
  121. return "unexpected ELF type";
  122. if (header->e_machine != EM_NONE)
  123. return "unexpected machine";
  124. if (header->e_version != EV_CURRENT)
  125. return "unexpected ELF version";
  126. if (header->e_ehsize != sizeof *header)
  127. return "unexpected header size";
  128. if (header->e_phentsize != sizeof (Elf_Phdr))
  129. return "unexpected program header size";
  130. return NULL;
  131. }
  132. #define IS_ALIGNED(offset, alignment) \
  133. (!((offset) & ((alignment) - 1)))
  134. #define ALIGN(offset, alignment) \
  135. ((offset + (alignment - 1)) & ~(alignment - 1))
  136. /* Return the alignment required by the ELF at DATA, of LEN bytes. */
  137. static size_t
  138. elf_alignment (const char *data, size_t len)
  139. {
  140. Elf_Ehdr *header;
  141. int i;
  142. size_t alignment = 8;
  143. if (len < sizeof(Elf_Ehdr))
  144. return alignment;
  145. header = (Elf_Ehdr *) data;
  146. if (header->e_phoff + header->e_phnum * header->e_phentsize >= len)
  147. return alignment;
  148. for (i = 0; i < header->e_phnum; i++)
  149. {
  150. Elf_Phdr *phdr;
  151. const char *phdr_addr = data + header->e_phoff + i * header->e_phentsize;
  152. if (!IS_ALIGNED ((uintptr_t) phdr_addr, alignof_type (Elf_Phdr)))
  153. return alignment;
  154. phdr = (Elf_Phdr *) phdr_addr;
  155. if (phdr->p_align & (phdr->p_align - 1))
  156. return alignment;
  157. if (phdr->p_align > alignment)
  158. alignment = phdr->p_align;
  159. }
  160. return alignment;
  161. }
  162. /* This function leaks the memory that it allocates. */
  163. static char*
  164. alloc_aligned (size_t len, unsigned alignment)
  165. {
  166. char *ret;
  167. if (alignment == 8)
  168. {
  169. /* FIXME: Assert that we actually have an 8-byte-aligned malloc. */
  170. ret = malloc (len);
  171. }
  172. #if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MAP_ANONYMOUS)
  173. else if (alignment == page_size)
  174. {
  175. ret = mmap (NULL, len, PROT_READ | PROT_WRITE,
  176. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  177. if (ret == MAP_FAILED)
  178. scm_syserror ("load-thunk-from-memory");
  179. }
  180. #endif
  181. else
  182. {
  183. if (len + alignment < len)
  184. abort ();
  185. ret = malloc (len + alignment - 1);
  186. if (!ret)
  187. abort ();
  188. ret = (char *) ALIGN ((uintptr_t) ret, (uintptr_t) alignment);
  189. }
  190. return ret;
  191. }
  192. static char*
  193. copy_and_align_elf_data (const char *data, size_t len)
  194. {
  195. size_t alignment;
  196. char *copy;
  197. alignment = elf_alignment (data, len);
  198. copy = alloc_aligned (len, alignment);
  199. memcpy(copy, data, len);
  200. return copy;
  201. }
  202. #ifdef HAVE_SYS_MMAN_H
  203. static int
  204. segment_flags_to_prot (Elf_Word flags)
  205. {
  206. int prot = 0;
  207. if (flags & PF_X)
  208. prot |= PROT_EXEC;
  209. if (flags & PF_W)
  210. prot |= PROT_WRITE;
  211. if (flags & PF_R)
  212. prot |= PROT_READ;
  213. return prot;
  214. }
  215. #endif
  216. static char*
  217. process_dynamic_segment (char *base, Elf_Phdr *dyn_phdr,
  218. SCM *init_out, SCM *entry_out, char **frame_maps_out)
  219. {
  220. char *dyn_addr = base + dyn_phdr->p_vaddr;
  221. Elf_Dyn *dyn = (Elf_Dyn *) dyn_addr;
  222. size_t i, dyn_size = dyn_phdr->p_memsz / sizeof (Elf_Dyn);
  223. char *init = 0, *gc_root = 0, *entry = 0, *frame_maps = 0;
  224. ptrdiff_t gc_root_size = 0;
  225. enum bytecode_kind bytecode_kind = BYTECODE_KIND_NONE;
  226. for (i = 0; i < dyn_size; i++)
  227. {
  228. if (dyn[i].d_tag == DT_NULL)
  229. break;
  230. switch (dyn[i].d_tag)
  231. {
  232. case DT_INIT:
  233. if (init)
  234. return "duplicate DT_INIT";
  235. init = base + dyn[i].d_un.d_val;
  236. break;
  237. case DT_GUILE_GC_ROOT:
  238. if (gc_root)
  239. return "duplicate DT_GUILE_GC_ROOT";
  240. gc_root = base + dyn[i].d_un.d_val;
  241. break;
  242. case DT_GUILE_GC_ROOT_SZ:
  243. if (gc_root_size)
  244. return "duplicate DT_GUILE_GC_ROOT_SZ";
  245. gc_root_size = dyn[i].d_un.d_val;
  246. break;
  247. case DT_GUILE_ENTRY:
  248. if (entry)
  249. return "duplicate DT_GUILE_ENTRY";
  250. entry = base + dyn[i].d_un.d_val;
  251. break;
  252. case DT_GUILE_VM_VERSION:
  253. if (bytecode_kind != BYTECODE_KIND_NONE)
  254. return "duplicate DT_GUILE_VM_VERSION";
  255. {
  256. uint16_t major = dyn[i].d_un.d_val >> 16;
  257. uint16_t minor = dyn[i].d_un.d_val & 0xffff;
  258. switch (major)
  259. {
  260. case 0x0300:
  261. bytecode_kind = BYTECODE_KIND_GUILE_3_0;
  262. if (minor < SCM_OBJCODE_MINIMUM_MINOR_VERSION)
  263. return "incompatible bytecode version";
  264. if (minor > SCM_OBJCODE_MINOR_VERSION)
  265. return "incompatible bytecode version";
  266. break;
  267. default:
  268. return "incompatible bytecode kind";
  269. }
  270. break;
  271. }
  272. case DT_GUILE_FRAME_MAPS:
  273. if (frame_maps)
  274. return "duplicate DT_GUILE_FRAME_MAPS";
  275. frame_maps = base + dyn[i].d_un.d_val;
  276. break;
  277. }
  278. }
  279. if (!entry)
  280. return "missing DT_GUILE_ENTRY";
  281. switch (bytecode_kind)
  282. {
  283. case BYTECODE_KIND_GUILE_3_0:
  284. if ((uintptr_t) init % 4)
  285. return "unaligned DT_INIT";
  286. if ((uintptr_t) entry % 4)
  287. return "unaligned DT_GUILE_ENTRY";
  288. break;
  289. case BYTECODE_KIND_NONE:
  290. default:
  291. return "missing DT_GUILE_VM_VERSION";
  292. }
  293. if (gc_root)
  294. GC_add_roots (gc_root, gc_root + gc_root_size);
  295. *init_out = init ? pointer_to_procedure (bytecode_kind, init) : SCM_BOOL_F;
  296. *entry_out = pointer_to_procedure (bytecode_kind, entry);
  297. *frame_maps_out = frame_maps;
  298. return NULL;
  299. }
  300. #define ABORT(msg) do { err_msg = msg; errno = 0; goto cleanup; } while (0)
  301. static SCM
  302. load_thunk_from_memory (char *data, size_t len, int is_read_only)
  303. #define FUNC_NAME "load-thunk-from-memory"
  304. {
  305. Elf_Ehdr *header;
  306. Elf_Phdr *ph;
  307. const char *err_msg = 0;
  308. size_t n, alignment = 8;
  309. int i;
  310. int dynamic_segment = -1;
  311. SCM init = SCM_BOOL_F, entry = SCM_BOOL_F;
  312. char *frame_maps = 0;
  313. errno = 0;
  314. if (len < sizeof *header)
  315. ABORT ("object file too small");
  316. header = (Elf_Ehdr*) data;
  317. if ((err_msg = check_elf_header (header)))
  318. {
  319. errno = 0; /* not an OS error */
  320. goto cleanup;
  321. }
  322. if (header->e_phnum == 0)
  323. ABORT ("no loadable segments");
  324. n = header->e_phnum;
  325. if (len < header->e_phoff + n * sizeof (Elf_Phdr))
  326. ABORT ("object file too small");
  327. ph = (Elf_Phdr*) (data + header->e_phoff);
  328. /* Check that the segment table is sane. */
  329. for (i = 0; i < n; i++)
  330. {
  331. if (ph[i].p_filesz != ph[i].p_memsz)
  332. ABORT ("expected p_filesz == p_memsz");
  333. if (!ph[i].p_flags)
  334. ABORT ("expected nonzero segment flags");
  335. if (ph[i].p_align < alignment)
  336. {
  337. if (ph[i].p_align % alignment)
  338. ABORT ("expected new alignment to be multiple of old");
  339. alignment = ph[i].p_align;
  340. }
  341. if (ph[i].p_type == PT_DYNAMIC)
  342. {
  343. if (dynamic_segment >= 0)
  344. ABORT ("expected only one PT_DYNAMIC segment");
  345. dynamic_segment = i;
  346. continue;
  347. }
  348. if (ph[i].p_type != PT_LOAD)
  349. ABORT ("unknown segment type");
  350. if (i == 0)
  351. {
  352. if (ph[i].p_vaddr != 0)
  353. ABORT ("first loadable vaddr is not 0");
  354. }
  355. else
  356. {
  357. if (ph[i].p_vaddr < ph[i-1].p_vaddr + ph[i-1].p_memsz)
  358. ABORT ("overlapping segments");
  359. if (ph[i].p_offset + ph[i].p_filesz > len)
  360. ABORT ("segment beyond end of byte array");
  361. }
  362. }
  363. if (dynamic_segment < 0)
  364. ABORT ("no PT_DYNAMIC segment");
  365. /* The ELF images that Guile currently emits have segments that are
  366. aligned on 64 KB boundaries, which might be larger than the actual
  367. page size (usually 4 KB). However Guile doesn't actually use the
  368. absolute addresses at all. All Guile needs is for the loaded image
  369. to be able to make the data section writable (for the mmap path),
  370. and for that the segment just needs to be page-aligned, and a page
  371. is always bigger than Guile's minimum alignment. Since we know
  372. (for the mmap path) that the base _is_ page-aligned, we proceed
  373. ahead even if the image alignment is greater than the page
  374. size. */
  375. if (!IS_ALIGNED ((uintptr_t) data, alignment)
  376. && !IS_ALIGNED (alignment, page_size))
  377. ABORT ("incorrectly aligned base");
  378. /* Allow writes to writable pages. */
  379. if (is_read_only)
  380. {
  381. #ifdef HAVE_SYS_MMAN_H
  382. for (i = 0; i < n; i++)
  383. {
  384. if (ph[i].p_type != PT_LOAD)
  385. continue;
  386. if (ph[i].p_flags == PF_R)
  387. continue;
  388. if (ph[i].p_align < page_size)
  389. continue;
  390. if (mprotect (data + ph[i].p_vaddr,
  391. ph[i].p_memsz,
  392. segment_flags_to_prot (ph[i].p_flags)))
  393. goto cleanup;
  394. }
  395. #else
  396. ABORT ("expected writable pages");
  397. #endif
  398. }
  399. if ((err_msg = process_dynamic_segment (data, &ph[dynamic_segment],
  400. &init, &entry, &frame_maps)))
  401. {
  402. errno = 0; /* not an OS error */
  403. goto cleanup;
  404. }
  405. if (scm_is_true (init))
  406. scm_call_0 (init);
  407. register_elf (data, len, frame_maps);
  408. /* Finally! Return the thunk. */
  409. return entry;
  410. cleanup:
  411. {
  412. if (errno)
  413. SCM_SYSERROR;
  414. scm_misc_error (FUNC_NAME, err_msg ? err_msg : "error loading ELF file",
  415. SCM_EOL);
  416. }
  417. }
  418. #undef FUNC_NAME
  419. static char*
  420. map_file_contents (int fd, size_t len, int *is_read_only)
  421. #define FUNC_NAME "load-thunk-from-file"
  422. {
  423. char *data;
  424. #ifdef HAVE_SYS_MMAN_H
  425. data = mmap (NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
  426. if (data == MAP_FAILED)
  427. SCM_SYSERROR;
  428. *is_read_only = 1;
  429. #else
  430. if (lseek (fd, 0, SEEK_SET) < 0)
  431. {
  432. int errno_save = errno;
  433. (void) close (fd);
  434. errno = errno_save;
  435. SCM_SYSERROR;
  436. }
  437. /* Given that we are using the read fallback, optimistically assume
  438. that the .go files were made with 8-byte alignment.
  439. alignment. */
  440. data = malloc (len);
  441. if (!data)
  442. {
  443. (void) close (fd);
  444. scm_misc_error (FUNC_NAME, "failed to allocate ~A bytes",
  445. scm_list_1 (scm_from_size_t (len)));
  446. }
  447. if (full_read (fd, data, len) != len)
  448. {
  449. int errno_save = errno;
  450. (void) close (fd);
  451. errno = errno_save;
  452. if (errno)
  453. SCM_SYSERROR;
  454. scm_misc_error (FUNC_NAME, "short read while loading objcode",
  455. SCM_EOL);
  456. }
  457. /* If our optimism failed, fall back. */
  458. {
  459. unsigned alignment = elf_alignment (data, len);
  460. if (alignment != 8)
  461. {
  462. char *copy = copy_and_align_elf_data (data, len);
  463. free (data);
  464. data = copy;
  465. }
  466. }
  467. *is_read_only = 0;
  468. #endif
  469. return data;
  470. }
  471. #undef FUNC_NAME
  472. SCM_DEFINE (scm_load_thunk_from_file, "load-thunk-from-file", 1, 0, 0,
  473. (SCM filename),
  474. "")
  475. #define FUNC_NAME s_scm_load_thunk_from_file
  476. {
  477. char *c_filename;
  478. int fd, is_read_only;
  479. off_t end;
  480. char *data;
  481. SCM_VALIDATE_STRING (1, filename);
  482. c_filename = scm_to_locale_string (filename);
  483. fd = open (c_filename, O_RDONLY | O_BINARY | O_CLOEXEC);
  484. free (c_filename);
  485. if (fd < 0) SCM_SYSERROR;
  486. end = lseek (fd, 0, SEEK_END);
  487. if (end < 0)
  488. SCM_SYSERROR;
  489. data = map_file_contents (fd, end, &is_read_only);
  490. (void) close (fd);
  491. return load_thunk_from_memory (data, end, is_read_only);
  492. }
  493. #undef FUNC_NAME
  494. SCM_DEFINE (scm_load_thunk_from_memory, "load-thunk-from-memory", 1, 0, 0,
  495. (SCM bv),
  496. "")
  497. #define FUNC_NAME s_scm_load_thunk_from_memory
  498. {
  499. char *data;
  500. size_t len;
  501. SCM_VALIDATE_BYTEVECTOR (1, bv);
  502. data = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
  503. len = SCM_BYTEVECTOR_LENGTH (bv);
  504. /* Copy data in order to align it, to trace its GC roots and
  505. writable sections, and to keep it in memory. */
  506. data = copy_and_align_elf_data (data, len);
  507. return load_thunk_from_memory (data, len, 0);
  508. }
  509. #undef FUNC_NAME
  510. struct mapped_elf_image
  511. {
  512. char *start;
  513. char *end;
  514. char *frame_maps;
  515. };
  516. static struct mapped_elf_image *mapped_elf_images = NULL;
  517. static size_t mapped_elf_images_count = 0;
  518. static size_t mapped_elf_images_allocated = 0;
  519. static size_t
  520. find_mapped_elf_insertion_index (char *ptr)
  521. {
  522. /* "mapped_elf_images_count" must never be dereferenced. */
  523. size_t start = 0, end = mapped_elf_images_count;
  524. while (start < end)
  525. {
  526. size_t n = start + (end - start) / 2;
  527. if (ptr < mapped_elf_images[n].end)
  528. end = n;
  529. else
  530. start = n + 1;
  531. }
  532. return start;
  533. }
  534. static void
  535. register_elf (char *data, size_t len, char *frame_maps)
  536. {
  537. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  538. {
  539. /* My kingdom for a generic growable sorted vector library. */
  540. if (mapped_elf_images_count == mapped_elf_images_allocated)
  541. {
  542. struct mapped_elf_image *prev;
  543. size_t n;
  544. if (mapped_elf_images_allocated)
  545. mapped_elf_images_allocated *= 2;
  546. else
  547. mapped_elf_images_allocated = 16;
  548. prev = mapped_elf_images;
  549. mapped_elf_images =
  550. scm_gc_malloc_pointerless (sizeof (*mapped_elf_images)
  551. * mapped_elf_images_allocated,
  552. "mapped elf images");
  553. for (n = 0; n < mapped_elf_images_count; n++)
  554. {
  555. mapped_elf_images[n].start = prev[n].start;
  556. mapped_elf_images[n].end = prev[n].end;
  557. mapped_elf_images[n].frame_maps = prev[n].frame_maps;
  558. }
  559. }
  560. {
  561. size_t end;
  562. size_t n = find_mapped_elf_insertion_index (data);
  563. for (end = mapped_elf_images_count; n < end; end--)
  564. {
  565. const struct mapped_elf_image *prev = &mapped_elf_images[end - 1];
  566. mapped_elf_images[end].start = prev->start;
  567. mapped_elf_images[end].end = prev->end;
  568. mapped_elf_images[end].frame_maps = prev->frame_maps;
  569. }
  570. mapped_elf_images_count++;
  571. mapped_elf_images[n].start = data;
  572. mapped_elf_images[n].end = data + len;
  573. mapped_elf_images[n].frame_maps = frame_maps;
  574. }
  575. }
  576. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  577. }
  578. static struct mapped_elf_image *
  579. find_mapped_elf_image_unlocked (char *ptr)
  580. {
  581. size_t n = find_mapped_elf_insertion_index ((char *) ptr);
  582. if (n < mapped_elf_images_count
  583. && mapped_elf_images[n].start <= ptr
  584. && ptr < mapped_elf_images[n].end)
  585. return &mapped_elf_images[n];
  586. return NULL;
  587. }
  588. static int
  589. find_mapped_elf_image (char *ptr, struct mapped_elf_image *image)
  590. {
  591. int result;
  592. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  593. {
  594. struct mapped_elf_image *img = find_mapped_elf_image_unlocked (ptr);
  595. if (img)
  596. {
  597. memcpy (image, img, sizeof (*image));
  598. result = 1;
  599. }
  600. else
  601. result = 0;
  602. }
  603. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  604. return result;
  605. }
  606. static SCM
  607. scm_find_mapped_elf_image (SCM ip)
  608. {
  609. struct mapped_elf_image image;
  610. if (find_mapped_elf_image ((char *) scm_to_uintptr_t (ip), &image))
  611. {
  612. signed char *data = (signed char *) image.start;
  613. size_t len = image.end - image.start;
  614. return scm_c_take_gc_bytevector (data, len, SCM_BOOL_F);
  615. }
  616. return SCM_BOOL_F;
  617. }
  618. static SCM
  619. scm_all_mapped_elf_images (void)
  620. {
  621. SCM result = SCM_EOL;
  622. scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
  623. {
  624. size_t n;
  625. for (n = 0; n < mapped_elf_images_count; n++)
  626. {
  627. signed char *data = (signed char *) mapped_elf_images[n].start;
  628. size_t len = mapped_elf_images[n].end - mapped_elf_images[n].start;
  629. result = scm_cons (scm_c_take_gc_bytevector (data, len, SCM_BOOL_F),
  630. result);
  631. }
  632. }
  633. scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
  634. return result;
  635. }
  636. struct frame_map_prefix
  637. {
  638. uint32_t text_offset;
  639. uint32_t maps_offset;
  640. };
  641. struct frame_map_header
  642. {
  643. uint32_t addr;
  644. uint32_t map_offset;
  645. };
  646. verify (sizeof (struct frame_map_prefix) == 8);
  647. verify (sizeof (struct frame_map_header) == 8);
  648. const uint8_t *
  649. scm_find_slot_map_unlocked (const uint32_t *ip)
  650. {
  651. struct mapped_elf_image *image;
  652. char *base;
  653. struct frame_map_prefix *prefix;
  654. struct frame_map_header *headers;
  655. uintptr_t addr = (uintptr_t) ip;
  656. size_t start, end;
  657. image = find_mapped_elf_image_unlocked ((char *) ip);
  658. if (!image || !image->frame_maps)
  659. return NULL;
  660. base = image->frame_maps;
  661. prefix = (struct frame_map_prefix *) base;
  662. headers = (struct frame_map_header *) (base + sizeof (*prefix));
  663. if (addr < ((uintptr_t) image->start) + prefix->text_offset)
  664. return NULL;
  665. addr -= ((uintptr_t) image->start) + prefix->text_offset;
  666. start = 0;
  667. end = (prefix->maps_offset - sizeof (*prefix)) / sizeof (*headers);
  668. if (end == 0 || addr > headers[end - 1].addr)
  669. return NULL;
  670. while (start < end)
  671. {
  672. size_t n = start + (end - start) / 2;
  673. if (addr == headers[n].addr)
  674. return (const uint8_t*) (base + headers[n].map_offset);
  675. else if (addr < headers[n].addr)
  676. end = n;
  677. else
  678. start = n + 1;
  679. }
  680. return NULL;
  681. }
  682. void
  683. scm_bootstrap_loader (void)
  684. {
  685. #ifndef __MINGW32__
  686. page_size = getpagesize ();
  687. #else
  688. page_size = getpagesize_w32 ();
  689. #endif
  690. /* page_size should be a power of two. */
  691. if (page_size & (page_size - 1))
  692. abort ();
  693. scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
  694. "scm_init_loader",
  695. (scm_t_extension_init_func)scm_init_loader, NULL);
  696. }
  697. void
  698. scm_init_loader (void)
  699. {
  700. #ifndef SCM_MAGIC_SNARFER
  701. #include "loader.x"
  702. #endif
  703. scm_c_define_gsubr ("find-mapped-elf-image", 1, 0, 0,
  704. (scm_t_subr) scm_find_mapped_elf_image);
  705. scm_c_define_gsubr ("all-mapped-elf-images", 0, 0, 0,
  706. (scm_t_subr) scm_all_mapped_elf_images);
  707. }