loader.c 21 KB

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