loader.c 21 KB

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