loader.c 21 KB

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