2013-03-28 05:09:53 -04:00
|
|
|
|
/* Copyright (C) 2001, 2009, 2010, 2011, 2012
|
2015-10-28 16:40:53 +00:00
|
|
|
|
* 2013, 2014, 2015 Free Software Foundation, Inc.
|
2001-04-16 03:43:48 +00:00
|
|
|
|
*
|
2009-05-26 22:23:44 +02:00
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2009-06-17 00:22:09 +01:00
|
|
|
|
* modify it under the terms of the GNU Lesser General Public License
|
|
|
|
|
|
* as published by the Free Software Foundation; either version 3 of
|
|
|
|
|
|
* the License, or (at your option) any later version.
|
2001-04-16 03:43:48 +00:00
|
|
|
|
*
|
2009-06-17 00:22:09 +01:00
|
|
|
|
* This library is distributed in the hope that it will be useful, but
|
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
2009-05-26 22:23:44 +02:00
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
|
* Lesser General Public License for more details.
|
2001-04-16 03:43:48 +00:00
|
|
|
|
*
|
2009-05-26 22:23:44 +02:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2009-06-17 00:22:09 +01:00
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
|
|
* 02110-1301 USA
|
2009-05-26 22:23:44 +02:00
|
|
|
|
*/
|
2001-04-16 03:43:48 +00:00
|
|
|
|
|
2008-05-20 11:46:52 +02:00
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
|
|
# include <config.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2001-04-16 03:43:48 +00:00
|
|
|
|
#include <string.h>
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
#include <unistd.h>
|
2011-04-01 12:10:42 +02:00
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2001-04-16 03:43:48 +00:00
|
|
|
|
#include <sys/mman.h>
|
2011-04-01 12:10:42 +02:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2001-04-16 03:43:48 +00:00
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
#include <sys/types.h>
|
2005-04-25 16:56:18 +00:00
|
|
|
|
#include <assert.h>
|
2009-11-15 20:53:21 +01:00
|
|
|
|
#include <alignof.h>
|
Complete cross-compilation support.
* module/system/base/target.scm (%target-endianness, %target-word-size):
New fluids.
(%native-word-size): New variable.
(with-target): Set these fluids.
(cpu-endianness, cpu-word-size, triplet-cpu, triplet-vendor,
triplet-os): New procedures.
(target-cpu, target-vendor, target-os): Use them.
(target-endianness, target-word-size): Refer to the corresponding
fluid.
* libguile/objcodes.c (target_endianness_var, target_word_size_var): New
global variables.
(NATIVE_ENDIANNESS): New macro.
(target_endianness, target_word_size, to_native_order): New functions.
(make_objcode_from_file): Use `scm_bytecode_to_native_objcode' instead
of `scm_bytecode_to_objcode'.
(bytecode_to_objcode): New function, based on `scm_bytecode_to_objcode',
with the addition of an `endianness' and `word_size' parameters.
(scm_bytecode_to_objcode): Use it.
(scm_bytecode_to_native_objcode): New function.
(scm_write_objcode): Use `target_word_size' and `target_endianness'.
Convert OBJCODE's len and meta-len to native byte order.
(scm_init_objcodes): Initialize `target_endianness_var' and
`target_word_size_var'.
* libguile/objcodes.h (scm_bytecode_to_native_objcode): New declaration.
* libguile/vm.c (really_make_boot_program): Use
`scm_bytecode_to_native_objcode' instead of `scm_bytecode_to_objcode'.
* test-suite/tests/asm-to-bytecode.test (%objcode-cookie-size): New
variable.
(test-target): New procedure.
("cross-compilation"): Add `test-target' calls and the "unknown
target" test.
2011-11-21 22:08:22 +01:00
|
|
|
|
#include <byteswap.h>
|
2014-01-26 20:55:04 +01:00
|
|
|
|
#include <verify.h>
|
2001-04-16 03:43:48 +00:00
|
|
|
|
|
2011-04-01 12:10:42 +02:00
|
|
|
|
#include <full-read.h>
|
|
|
|
|
|
|
2009-05-26 22:23:44 +02:00
|
|
|
|
#include "_scm.h"
|
2012-06-01 21:33:46 +02:00
|
|
|
|
#include "elf.h"
|
2001-04-16 03:43:48 +00:00
|
|
|
|
#include "programs.h"
|
2013-11-19 21:45:07 +01:00
|
|
|
|
#include "loader.h"
|
2001-04-16 03:43:48 +00:00
|
|
|
|
|
2012-06-22 13:35:55 +02:00
|
|
|
|
/* This file contains the loader for Guile's on-disk format: ELF with
|
|
|
|
|
|
some custom tags in the dynamic segment. */
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
|
|
|
|
|
#if SIZEOF_SCM_T_BITS == 4
|
|
|
|
|
|
#define Elf_Half Elf32_Half
|
|
|
|
|
|
#define Elf_Word Elf32_Word
|
|
|
|
|
|
#define Elf_Ehdr Elf32_Ehdr
|
|
|
|
|
|
#define ELFCLASS ELFCLASS32
|
|
|
|
|
|
#define Elf_Phdr Elf32_Phdr
|
|
|
|
|
|
#define Elf_Dyn Elf32_Dyn
|
|
|
|
|
|
#elif SIZEOF_SCM_T_BITS == 8
|
|
|
|
|
|
#define Elf_Half Elf64_Half
|
|
|
|
|
|
#define Elf_Word Elf64_Word
|
|
|
|
|
|
#define Elf_Ehdr Elf64_Ehdr
|
|
|
|
|
|
#define ELFCLASS ELFCLASS64
|
|
|
|
|
|
#define Elf_Phdr Elf64_Phdr
|
|
|
|
|
|
#define Elf_Dyn Elf64_Dyn
|
|
|
|
|
|
#else
|
|
|
|
|
|
#error
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#define DT_LOGUILE 0x37146000 /* Start of Guile-specific */
|
|
|
|
|
|
#define DT_GUILE_GC_ROOT 0x37146000 /* Offset of GC roots */
|
|
|
|
|
|
#define DT_GUILE_GC_ROOT_SZ 0x37146001 /* Size in machine words of GC
|
|
|
|
|
|
roots */
|
|
|
|
|
|
#define DT_GUILE_ENTRY 0x37146002 /* Address of entry thunk */
|
2013-11-19 20:02:15 +01:00
|
|
|
|
#define DT_GUILE_VM_VERSION 0x37146003 /* Bytecode version */
|
2014-01-26 20:55:04 +01:00
|
|
|
|
#define DT_GUILE_FRAME_MAPS 0x37146004 /* Frame maps */
|
2012-06-01 21:33:46 +02:00
|
|
|
|
#define DT_HIGUILE 0x37146fff /* End of Guile-specific */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
|
|
#define ELFDATA ELFDATA2MSB
|
|
|
|
|
|
#else
|
|
|
|
|
|
#define ELFDATA ELFDATA2LSB
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2017-01-08 23:39:42 +01:00
|
|
|
|
/* The page size. */
|
|
|
|
|
|
static size_t page_size;
|
|
|
|
|
|
|
2014-01-26 20:55:04 +01:00
|
|
|
|
static void register_elf (char *data, size_t len, char *frame_maps);
|
2013-04-28 14:42:01 +02:00
|
|
|
|
|
2012-06-01 21:33:46 +02:00
|
|
|
|
enum bytecode_kind
|
|
|
|
|
|
{
|
|
|
|
|
|
BYTECODE_KIND_NONE,
|
2017-05-22 14:13:20 +02:00
|
|
|
|
BYTECODE_KIND_GUILE_3_0
|
2012-06-01 21:33:46 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static SCM
|
|
|
|
|
|
pointer_to_procedure (enum bytecode_kind bytecode_kind, char *ptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (bytecode_kind)
|
|
|
|
|
|
{
|
2017-05-22 14:13:20 +02:00
|
|
|
|
case BYTECODE_KIND_GUILE_3_0:
|
2013-05-23 14:52:29 +02:00
|
|
|
|
{
|
2013-11-19 18:57:31 +01:00
|
|
|
|
return scm_i_make_program ((scm_t_uint32 *) ptr);
|
2013-05-23 14:52:29 +02:00
|
|
|
|
}
|
2012-06-01 21:33:46 +02:00
|
|
|
|
case BYTECODE_KIND_NONE:
|
|
|
|
|
|
default:
|
|
|
|
|
|
abort ();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const char*
|
|
|
|
|
|
check_elf_header (const Elf_Ehdr *header)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!(header->e_ident[EI_MAG0] == ELFMAG0
|
|
|
|
|
|
&& header->e_ident[EI_MAG1] == ELFMAG1
|
|
|
|
|
|
&& header->e_ident[EI_MAG2] == ELFMAG2
|
|
|
|
|
|
&& header->e_ident[EI_MAG3] == ELFMAG3))
|
|
|
|
|
|
return "not an ELF file";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_ident[EI_CLASS] != ELFCLASS)
|
|
|
|
|
|
return "ELF file does not have native word size";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_ident[EI_DATA] != ELFDATA)
|
|
|
|
|
|
return "ELF file does not have native byte order";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_ident[EI_VERSION] != EV_CURRENT)
|
|
|
|
|
|
return "bad ELF version";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_ident[EI_OSABI] != ELFOSABI_STANDALONE)
|
|
|
|
|
|
return "unexpected OS ABI";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_ident[EI_ABIVERSION] != 0)
|
|
|
|
|
|
return "unexpected ABI version";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_type != ET_DYN)
|
|
|
|
|
|
return "unexpected ELF type";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_machine != EM_NONE)
|
|
|
|
|
|
return "unexpected machine";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_version != EV_CURRENT)
|
|
|
|
|
|
return "unexpected ELF version";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_ehsize != sizeof *header)
|
|
|
|
|
|
return "unexpected header size";
|
|
|
|
|
|
|
|
|
|
|
|
if (header->e_phentsize != sizeof (Elf_Phdr))
|
|
|
|
|
|
return "unexpected program header size";
|
|
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
#define IS_ALIGNED(offset, alignment) \
|
|
|
|
|
|
(!((offset) & ((alignment) - 1)))
|
|
|
|
|
|
#define ALIGN(offset, alignment) \
|
|
|
|
|
|
((offset + (alignment - 1)) & ~(alignment - 1))
|
|
|
|
|
|
|
|
|
|
|
|
/* Return the alignment required by the ELF at DATA, of LEN bytes. */
|
|
|
|
|
|
static size_t
|
|
|
|
|
|
elf_alignment (const char *data, size_t len)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
2013-04-28 14:23:20 +02:00
|
|
|
|
Elf_Ehdr *header;
|
|
|
|
|
|
int i;
|
|
|
|
|
|
size_t alignment = 8;
|
|
|
|
|
|
|
|
|
|
|
|
if (len < sizeof(Elf_Ehdr))
|
|
|
|
|
|
return alignment;
|
|
|
|
|
|
header = (Elf_Ehdr *) data;
|
|
|
|
|
|
if (header->e_phoff + header->e_phnum * header->e_phentsize >= len)
|
|
|
|
|
|
return alignment;
|
|
|
|
|
|
for (i = 0; i < header->e_phnum; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
Elf_Phdr *phdr;
|
|
|
|
|
|
const char *phdr_addr = data + header->e_phoff + i * header->e_phentsize;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (!IS_ALIGNED ((scm_t_uintptr) phdr_addr, alignof_type (Elf_Phdr)))
|
|
|
|
|
|
return alignment;
|
|
|
|
|
|
phdr = (Elf_Phdr *) phdr_addr;
|
|
|
|
|
|
|
|
|
|
|
|
if (phdr->p_align & (phdr->p_align - 1))
|
|
|
|
|
|
return alignment;
|
|
|
|
|
|
|
|
|
|
|
|
if (phdr->p_align > alignment)
|
|
|
|
|
|
alignment = phdr->p_align;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return alignment;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
/* This function leaks the memory that it allocates. */
|
|
|
|
|
|
static char*
|
|
|
|
|
|
alloc_aligned (size_t len, unsigned alignment)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
char *ret;
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (alignment == 8)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* FIXME: Assert that we actually have an 8-byte-aligned malloc. */
|
|
|
|
|
|
ret = malloc (len);
|
|
|
|
|
|
}
|
2017-01-08 23:39:42 +01:00
|
|
|
|
#if defined(HAVE_SYS_MMAN_H) && defined(HAVE_MAP_ANONYMOUS)
|
|
|
|
|
|
else if (alignment == page_size)
|
2013-04-28 14:23:20 +02:00
|
|
|
|
{
|
2017-01-08 23:39:42 +01:00
|
|
|
|
ret = mmap (NULL, len, PROT_READ | PROT_WRITE,
|
|
|
|
|
|
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (ret == MAP_FAILED)
|
2017-01-08 23:39:42 +01:00
|
|
|
|
scm_syserror ("load-thunk-from-memory");
|
2013-04-28 14:23:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (len + alignment < len)
|
|
|
|
|
|
abort ();
|
|
|
|
|
|
|
|
|
|
|
|
ret = malloc (len + alignment - 1);
|
|
|
|
|
|
if (!ret)
|
|
|
|
|
|
abort ();
|
2017-01-08 23:41:12 +01:00
|
|
|
|
ret = (char *) ALIGN ((scm_t_uintptr) ret, (scm_t_uintptr) alignment);
|
2013-04-28 14:23:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
|
}
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
static char*
|
|
|
|
|
|
copy_and_align_elf_data (const char *data, size_t len)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t alignment;
|
|
|
|
|
|
char *copy;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
alignment = elf_alignment (data, len);
|
|
|
|
|
|
copy = alloc_aligned (len, alignment);
|
|
|
|
|
|
memcpy(copy, data, len);
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
return copy;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
2012-06-01 21:33:46 +02:00
|
|
|
|
static int
|
2013-04-28 14:23:20 +02:00
|
|
|
|
segment_flags_to_prot (Elf_Word flags)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
2013-04-28 14:23:20 +02:00
|
|
|
|
int prot = 0;
|
|
|
|
|
|
|
|
|
|
|
|
if (flags & PF_X)
|
|
|
|
|
|
prot |= PROT_EXEC;
|
|
|
|
|
|
if (flags & PF_W)
|
|
|
|
|
|
prot |= PROT_WRITE;
|
|
|
|
|
|
if (flags & PF_R)
|
|
|
|
|
|
prot |= PROT_READ;
|
|
|
|
|
|
|
|
|
|
|
|
return prot;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
2013-04-28 14:23:20 +02:00
|
|
|
|
#endif
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
|
|
|
|
|
static char*
|
|
|
|
|
|
process_dynamic_segment (char *base, Elf_Phdr *dyn_phdr,
|
2014-01-26 20:55:04 +01:00
|
|
|
|
SCM *init_out, SCM *entry_out, char **frame_maps_out)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
char *dyn_addr = base + dyn_phdr->p_vaddr;
|
|
|
|
|
|
Elf_Dyn *dyn = (Elf_Dyn *) dyn_addr;
|
|
|
|
|
|
size_t i, dyn_size = dyn_phdr->p_memsz / sizeof (Elf_Dyn);
|
2014-01-26 20:55:04 +01:00
|
|
|
|
char *init = 0, *gc_root = 0, *entry = 0, *frame_maps = 0;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
scm_t_ptrdiff gc_root_size = 0;
|
|
|
|
|
|
enum bytecode_kind bytecode_kind = BYTECODE_KIND_NONE;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < dyn_size; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dyn[i].d_tag == DT_NULL)
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
switch (dyn[i].d_tag)
|
|
|
|
|
|
{
|
|
|
|
|
|
case DT_INIT:
|
|
|
|
|
|
if (init)
|
|
|
|
|
|
return "duplicate DT_INIT";
|
|
|
|
|
|
init = base + dyn[i].d_un.d_val;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DT_GUILE_GC_ROOT:
|
|
|
|
|
|
if (gc_root)
|
|
|
|
|
|
return "duplicate DT_GUILE_GC_ROOT";
|
|
|
|
|
|
gc_root = base + dyn[i].d_un.d_val;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DT_GUILE_GC_ROOT_SZ:
|
|
|
|
|
|
if (gc_root_size)
|
|
|
|
|
|
return "duplicate DT_GUILE_GC_ROOT_SZ";
|
|
|
|
|
|
gc_root_size = dyn[i].d_un.d_val;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case DT_GUILE_ENTRY:
|
|
|
|
|
|
if (entry)
|
|
|
|
|
|
return "duplicate DT_GUILE_ENTRY";
|
|
|
|
|
|
entry = base + dyn[i].d_un.d_val;
|
|
|
|
|
|
break;
|
2013-11-19 20:02:15 +01:00
|
|
|
|
case DT_GUILE_VM_VERSION:
|
2012-06-01 21:33:46 +02:00
|
|
|
|
if (bytecode_kind != BYTECODE_KIND_NONE)
|
2013-11-19 20:02:15 +01:00
|
|
|
|
return "duplicate DT_GUILE_VM_VERSION";
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
scm_t_uint16 major = dyn[i].d_un.d_val >> 16;
|
|
|
|
|
|
scm_t_uint16 minor = dyn[i].d_un.d_val & 0xffff;
|
2013-05-23 14:52:29 +02:00
|
|
|
|
switch (major)
|
|
|
|
|
|
{
|
2017-05-22 14:13:20 +02:00
|
|
|
|
case 0x0300:
|
|
|
|
|
|
bytecode_kind = BYTECODE_KIND_GUILE_3_0;
|
2017-04-18 21:37:36 +02:00
|
|
|
|
if (minor < SCM_OBJCODE_MINIMUM_MINOR_VERSION)
|
|
|
|
|
|
return "incompatible bytecode version";
|
2017-05-22 14:13:20 +02:00
|
|
|
|
if (minor > SCM_OBJCODE_MINOR_VERSION)
|
2013-05-23 14:52:29 +02:00
|
|
|
|
return "incompatible bytecode version";
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
return "incompatible bytecode kind";
|
|
|
|
|
|
}
|
2012-06-01 21:33:46 +02:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
2014-01-26 20:55:04 +01:00
|
|
|
|
case DT_GUILE_FRAME_MAPS:
|
|
|
|
|
|
if (frame_maps)
|
|
|
|
|
|
return "duplicate DT_GUILE_FRAME_MAPS";
|
|
|
|
|
|
frame_maps = base + dyn[i].d_un.d_val;
|
|
|
|
|
|
break;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!entry)
|
|
|
|
|
|
return "missing DT_GUILE_ENTRY";
|
|
|
|
|
|
|
2013-05-23 14:52:29 +02:00
|
|
|
|
switch (bytecode_kind)
|
|
|
|
|
|
{
|
2017-05-22 14:13:20 +02:00
|
|
|
|
case BYTECODE_KIND_GUILE_3_0:
|
2013-05-23 14:52:29 +02:00
|
|
|
|
if ((scm_t_uintptr) init % 4)
|
|
|
|
|
|
return "unaligned DT_INIT";
|
|
|
|
|
|
if ((scm_t_uintptr) entry % 4)
|
|
|
|
|
|
return "unaligned DT_GUILE_ENTRY";
|
|
|
|
|
|
break;
|
|
|
|
|
|
case BYTECODE_KIND_NONE:
|
|
|
|
|
|
default:
|
2013-11-19 20:02:15 +01:00
|
|
|
|
return "missing DT_GUILE_VM_VERSION";
|
2013-05-23 14:52:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-06-01 21:33:46 +02:00
|
|
|
|
if (gc_root)
|
|
|
|
|
|
GC_add_roots (gc_root, gc_root + gc_root_size);
|
|
|
|
|
|
|
2013-05-23 14:52:29 +02:00
|
|
|
|
*init_out = init ? pointer_to_procedure (bytecode_kind, init) : SCM_BOOL_F;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
*entry_out = pointer_to_procedure (bytecode_kind, entry);
|
2014-01-26 20:55:04 +01:00
|
|
|
|
*frame_maps_out = frame_maps;
|
|
|
|
|
|
|
2012-06-01 21:33:46 +02:00
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define ABORT(msg) do { err_msg = msg; goto cleanup; } while (0)
|
|
|
|
|
|
|
|
|
|
|
|
static SCM
|
2013-04-28 14:23:20 +02:00
|
|
|
|
load_thunk_from_memory (char *data, size_t len, int is_read_only)
|
|
|
|
|
|
#define FUNC_NAME "load-thunk-from-memory"
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
2013-04-28 14:23:20 +02:00
|
|
|
|
Elf_Ehdr *header;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
Elf_Phdr *ph;
|
|
|
|
|
|
const char *err_msg = 0;
|
2013-04-28 14:23:20 +02:00
|
|
|
|
size_t n, alignment = 8;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
int i;
|
|
|
|
|
|
int dynamic_segment = -1;
|
|
|
|
|
|
SCM init = SCM_BOOL_F, entry = SCM_BOOL_F;
|
2014-01-26 20:55:04 +01:00
|
|
|
|
char *frame_maps = 0;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2017-05-22 14:13:20 +02:00
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (len < sizeof *header)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
ABORT ("object file too small");
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
header = (Elf_Ehdr*) data;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if ((err_msg = check_elf_header (header)))
|
2012-06-01 21:33:46 +02:00
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (header->e_phnum == 0)
|
|
|
|
|
|
ABORT ("no loadable segments");
|
|
|
|
|
|
n = header->e_phnum;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (len < header->e_phoff + n * sizeof (Elf_Phdr))
|
2012-06-01 21:33:46 +02:00
|
|
|
|
ABORT ("object file too small");
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
ph = (Elf_Phdr*) (data + header->e_phoff);
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
/* Check that the segment table is sane. */
|
2012-06-01 21:33:46 +02:00
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ph[i].p_filesz != ph[i].p_memsz)
|
|
|
|
|
|
ABORT ("expected p_filesz == p_memsz");
|
|
|
|
|
|
|
|
|
|
|
|
if (!ph[i].p_flags)
|
|
|
|
|
|
ABORT ("expected nonzero segment flags");
|
|
|
|
|
|
|
|
|
|
|
|
if (ph[i].p_align < alignment)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ph[i].p_align % alignment)
|
|
|
|
|
|
ABORT ("expected new alignment to be multiple of old");
|
|
|
|
|
|
alignment = ph[i].p_align;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ph[i].p_type == PT_DYNAMIC)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dynamic_segment >= 0)
|
|
|
|
|
|
ABORT ("expected only one PT_DYNAMIC segment");
|
|
|
|
|
|
dynamic_segment = i;
|
2014-02-17 14:15:30 +01:00
|
|
|
|
continue;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-02-17 14:15:30 +01:00
|
|
|
|
if (ph[i].p_type != PT_LOAD)
|
|
|
|
|
|
ABORT ("unknown segment type");
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (i == 0)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (ph[i].p_vaddr != 0)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
ABORT ("first loadable vaddr is not 0");
|
|
|
|
|
|
}
|
2013-04-28 14:23:20 +02:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ph[i].p_vaddr < ph[i-1].p_vaddr + ph[i-1].p_memsz)
|
|
|
|
|
|
ABORT ("overlapping segments");
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (ph[i].p_offset + ph[i].p_filesz > len)
|
|
|
|
|
|
ABORT ("segment beyond end of byte array");
|
|
|
|
|
|
}
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (dynamic_segment < 0)
|
|
|
|
|
|
ABORT ("no PT_DYNAMIC segment");
|
|
|
|
|
|
|
2017-03-08 22:39:29 +01:00
|
|
|
|
/* The ELF images that Guile currently emits have segments that are
|
|
|
|
|
|
aligned on 64 KB boundaries, which might be larger than the actual
|
|
|
|
|
|
page size (usually 4 KB). However Guile doesn't actually use the
|
|
|
|
|
|
absolute addresses at all. All Guile needs is for the loaded image
|
|
|
|
|
|
to be able to make the data section writable (for the mmap path),
|
|
|
|
|
|
and for that the segment just needs to be page-aligned, and a page
|
|
|
|
|
|
is always bigger than Guile's minimum alignment. Since we know
|
|
|
|
|
|
(for the mmap path) that the base _is_ page-aligned, we proceed
|
|
|
|
|
|
ahead even if the image alignment is greater than the page
|
|
|
|
|
|
size. */
|
|
|
|
|
|
if (!IS_ALIGNED ((scm_t_uintptr) data, alignment)
|
|
|
|
|
|
&& !IS_ALIGNED (alignment, page_size))
|
2013-04-28 14:23:20 +02:00
|
|
|
|
ABORT ("incorrectly aligned base");
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
/* Allow writes to writable pages. */
|
|
|
|
|
|
if (is_read_only)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
2013-04-28 14:23:20 +02:00
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
2014-02-17 14:15:30 +01:00
|
|
|
|
if (ph[i].p_type != PT_LOAD)
|
|
|
|
|
|
continue;
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (ph[i].p_flags == PF_R)
|
|
|
|
|
|
continue;
|
2017-03-08 22:39:29 +01:00
|
|
|
|
if (ph[i].p_align < page_size)
|
2013-04-28 14:23:20 +02:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (mprotect (data + ph[i].p_vaddr,
|
|
|
|
|
|
ph[i].p_memsz,
|
|
|
|
|
|
segment_flags_to_prot (ph[i].p_flags)))
|
|
|
|
|
|
goto cleanup;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
2013-04-28 14:23:20 +02:00
|
|
|
|
#else
|
|
|
|
|
|
ABORT ("expected writable pages");
|
|
|
|
|
|
#endif
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if ((err_msg = process_dynamic_segment (data, &ph[dynamic_segment],
|
2014-01-26 20:55:04 +01:00
|
|
|
|
&init, &entry, &frame_maps)))
|
2012-06-01 21:33:46 +02:00
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
|
|
|
|
if (scm_is_true (init))
|
|
|
|
|
|
scm_call_0 (init);
|
|
|
|
|
|
|
2014-01-26 20:55:04 +01:00
|
|
|
|
register_elf (data, len, frame_maps);
|
2013-04-28 14:42:01 +02:00
|
|
|
|
|
2012-06-01 21:33:46 +02:00
|
|
|
|
/* Finally! Return the thunk. */
|
|
|
|
|
|
return entry;
|
|
|
|
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
|
|
{
|
|
|
|
|
|
if (errno)
|
|
|
|
|
|
SCM_SYSERROR;
|
|
|
|
|
|
scm_misc_error (FUNC_NAME, err_msg ? err_msg : "error loading ELF file",
|
|
|
|
|
|
SCM_EOL);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
static char*
|
|
|
|
|
|
map_file_contents (int fd, size_t len, int *is_read_only)
|
|
|
|
|
|
#define FUNC_NAME "load-thunk-from-file"
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
char *data;
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
#ifdef HAVE_SYS_MMAN_H
|
|
|
|
|
|
data = mmap (NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
|
|
|
|
|
if (data == MAP_FAILED)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
SCM_SYSERROR;
|
2013-04-28 14:23:20 +02:00
|
|
|
|
*is_read_only = 1;
|
|
|
|
|
|
#else
|
2017-03-03 09:50:27 -08:00
|
|
|
|
if (lseek (fd, 0, SEEK_SET) < 0)
|
2013-04-28 14:23:20 +02:00
|
|
|
|
{
|
|
|
|
|
|
int errno_save = errno;
|
|
|
|
|
|
(void) close (fd);
|
|
|
|
|
|
errno = errno_save;
|
|
|
|
|
|
SCM_SYSERROR;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Given that we are using the read fallback, optimistically assume
|
|
|
|
|
|
that the .go files were made with 8-byte alignment.
|
|
|
|
|
|
alignment. */
|
2017-03-03 09:50:27 -08:00
|
|
|
|
data = malloc (len);
|
2013-04-28 14:23:20 +02:00
|
|
|
|
if (!data)
|
|
|
|
|
|
{
|
|
|
|
|
|
(void) close (fd);
|
|
|
|
|
|
scm_misc_error (FUNC_NAME, "failed to allocate ~A bytes",
|
2017-03-03 09:50:27 -08:00
|
|
|
|
scm_list_1 (scm_from_size_t (len)));
|
2013-04-28 14:23:20 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-03 09:50:27 -08:00
|
|
|
|
if (full_read (fd, data, len) != len)
|
2012-06-01 21:33:46 +02:00
|
|
|
|
{
|
|
|
|
|
|
int errno_save = errno;
|
|
|
|
|
|
(void) close (fd);
|
|
|
|
|
|
errno = errno_save;
|
|
|
|
|
|
if (errno)
|
|
|
|
|
|
SCM_SYSERROR;
|
|
|
|
|
|
scm_misc_error (FUNC_NAME, "short read while loading objcode",
|
|
|
|
|
|
SCM_EOL);
|
|
|
|
|
|
}
|
2013-04-28 14:23:20 +02:00
|
|
|
|
|
|
|
|
|
|
/* If our optimism failed, fall back. */
|
|
|
|
|
|
{
|
2017-03-03 09:50:27 -08:00
|
|
|
|
unsigned alignment = elf_alignment (data, len);
|
2013-04-28 14:23:20 +02:00
|
|
|
|
|
|
|
|
|
|
if (alignment != 8)
|
|
|
|
|
|
{
|
2017-03-03 09:50:27 -08:00
|
|
|
|
char *copy = copy_and_align_elf_data (data, len);
|
2013-04-28 14:23:20 +02:00
|
|
|
|
free (data);
|
|
|
|
|
|
data = copy;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
*is_read_only = 0;
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_load_thunk_from_file, "load-thunk-from-file", 1, 0, 0,
|
|
|
|
|
|
(SCM filename),
|
|
|
|
|
|
"")
|
|
|
|
|
|
#define FUNC_NAME s_scm_load_thunk_from_file
|
|
|
|
|
|
{
|
|
|
|
|
|
char *c_filename;
|
2013-04-28 14:23:20 +02:00
|
|
|
|
int fd, is_read_only;
|
|
|
|
|
|
off_t end;
|
|
|
|
|
|
char *data;
|
2012-06-01 21:33:46 +02:00
|
|
|
|
|
|
|
|
|
|
SCM_VALIDATE_STRING (1, filename);
|
|
|
|
|
|
|
|
|
|
|
|
c_filename = scm_to_locale_string (filename);
|
2013-03-28 05:09:53 -04:00
|
|
|
|
fd = open (c_filename, O_RDONLY | O_BINARY | O_CLOEXEC);
|
2012-06-01 21:33:46 +02:00
|
|
|
|
free (c_filename);
|
|
|
|
|
|
if (fd < 0) SCM_SYSERROR;
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
end = lseek (fd, 0, SEEK_END);
|
|
|
|
|
|
if (end < 0)
|
|
|
|
|
|
SCM_SYSERROR;
|
|
|
|
|
|
|
|
|
|
|
|
data = map_file_contents (fd, end, &is_read_only);
|
|
|
|
|
|
|
|
|
|
|
|
(void) close (fd);
|
|
|
|
|
|
|
|
|
|
|
|
return load_thunk_from_memory (data, end, is_read_only);
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
|
|
|
|
|
SCM_DEFINE (scm_load_thunk_from_memory, "load-thunk-from-memory", 1, 0, 0,
|
|
|
|
|
|
(SCM bv),
|
|
|
|
|
|
"")
|
|
|
|
|
|
#define FUNC_NAME s_scm_load_thunk_from_memory
|
|
|
|
|
|
{
|
2013-04-28 14:23:20 +02:00
|
|
|
|
char *data;
|
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
2012-06-01 21:33:46 +02:00
|
|
|
|
SCM_VALIDATE_BYTEVECTOR (1, bv);
|
|
|
|
|
|
|
2013-04-28 14:23:20 +02:00
|
|
|
|
data = (char *) SCM_BYTEVECTOR_CONTENTS (bv);
|
|
|
|
|
|
len = SCM_BYTEVECTOR_LENGTH (bv);
|
|
|
|
|
|
|
|
|
|
|
|
/* Copy data in order to align it, to trace its GC roots and
|
|
|
|
|
|
writable sections, and to keep it in memory. */
|
|
|
|
|
|
|
|
|
|
|
|
data = copy_and_align_elf_data (data, len);
|
|
|
|
|
|
|
|
|
|
|
|
return load_thunk_from_memory (data, len, 0);
|
2012-06-01 21:33:46 +02:00
|
|
|
|
}
|
|
|
|
|
|
#undef FUNC_NAME
|
|
|
|
|
|
|
2001-04-16 03:43:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
2013-04-28 14:42:01 +02:00
|
|
|
|
struct mapped_elf_image
|
|
|
|
|
|
{
|
|
|
|
|
|
char *start;
|
|
|
|
|
|
char *end;
|
2014-01-26 20:55:04 +01:00
|
|
|
|
char *frame_maps;
|
2013-04-28 14:42:01 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static struct mapped_elf_image *mapped_elf_images = NULL;
|
|
|
|
|
|
static size_t mapped_elf_images_count = 0;
|
|
|
|
|
|
static size_t mapped_elf_images_allocated = 0;
|
|
|
|
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
|
|
find_mapped_elf_insertion_index (char *ptr)
|
|
|
|
|
|
{
|
|
|
|
|
|
/* "mapped_elf_images_count" must never be dereferenced. */
|
|
|
|
|
|
size_t start = 0, end = mapped_elf_images_count;
|
|
|
|
|
|
|
|
|
|
|
|
while (start < end)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t n = start + (end - start) / 2;
|
|
|
|
|
|
|
|
|
|
|
|
if (ptr < mapped_elf_images[n].end)
|
|
|
|
|
|
end = n;
|
|
|
|
|
|
else
|
|
|
|
|
|
start = n + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return start;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2014-01-26 20:55:04 +01:00
|
|
|
|
register_elf (char *data, size_t len, char *frame_maps)
|
2013-04-28 14:42:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
|
|
|
|
|
|
{
|
|
|
|
|
|
/* My kingdom for a generic growable sorted vector library. */
|
|
|
|
|
|
if (mapped_elf_images_count == mapped_elf_images_allocated)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct mapped_elf_image *prev;
|
|
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
|
|
|
|
if (mapped_elf_images_allocated)
|
|
|
|
|
|
mapped_elf_images_allocated *= 2;
|
|
|
|
|
|
else
|
|
|
|
|
|
mapped_elf_images_allocated = 16;
|
|
|
|
|
|
|
|
|
|
|
|
prev = mapped_elf_images;
|
|
|
|
|
|
mapped_elf_images =
|
|
|
|
|
|
scm_gc_malloc_pointerless (sizeof (*mapped_elf_images)
|
|
|
|
|
|
* mapped_elf_images_allocated,
|
|
|
|
|
|
"mapped elf images");
|
|
|
|
|
|
|
|
|
|
|
|
for (n = 0; n < mapped_elf_images_count; n++)
|
|
|
|
|
|
{
|
|
|
|
|
|
mapped_elf_images[n].start = prev[n].start;
|
|
|
|
|
|
mapped_elf_images[n].end = prev[n].end;
|
2014-01-26 20:55:04 +01:00
|
|
|
|
mapped_elf_images[n].frame_maps = prev[n].frame_maps;
|
2013-04-28 14:42:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t end;
|
|
|
|
|
|
size_t n = find_mapped_elf_insertion_index (data);
|
|
|
|
|
|
|
|
|
|
|
|
for (end = mapped_elf_images_count; n < end; end--)
|
|
|
|
|
|
{
|
2014-01-26 20:55:04 +01:00
|
|
|
|
const struct mapped_elf_image *prev = &mapped_elf_images[end - 1];
|
|
|
|
|
|
mapped_elf_images[end].start = prev->start;
|
|
|
|
|
|
mapped_elf_images[end].end = prev->end;
|
|
|
|
|
|
mapped_elf_images[end].frame_maps = prev->frame_maps;
|
2013-04-28 14:42:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
mapped_elf_images_count++;
|
|
|
|
|
|
|
|
|
|
|
|
mapped_elf_images[n].start = data;
|
|
|
|
|
|
mapped_elf_images[n].end = data + len;
|
2014-01-26 20:55:04 +01:00
|
|
|
|
mapped_elf_images[n].frame_maps = frame_maps;
|
2013-04-28 14:42:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-26 20:55:04 +01:00
|
|
|
|
static struct mapped_elf_image *
|
|
|
|
|
|
find_mapped_elf_image_unlocked (char *ptr)
|
2013-04-28 14:42:01 +02:00
|
|
|
|
{
|
2014-01-26 20:55:04 +01:00
|
|
|
|
size_t n = find_mapped_elf_insertion_index ((char *) ptr);
|
|
|
|
|
|
|
|
|
|
|
|
if (n < mapped_elf_images_count
|
|
|
|
|
|
&& mapped_elf_images[n].start <= ptr
|
|
|
|
|
|
&& ptr < mapped_elf_images[n].end)
|
|
|
|
|
|
return &mapped_elf_images[n];
|
|
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
|
find_mapped_elf_image (char *ptr, struct mapped_elf_image *image)
|
|
|
|
|
|
{
|
|
|
|
|
|
int result;
|
2013-04-28 14:42:01 +02:00
|
|
|
|
|
|
|
|
|
|
scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
|
|
|
|
|
|
{
|
2014-01-26 20:55:04 +01:00
|
|
|
|
struct mapped_elf_image *img = find_mapped_elf_image_unlocked (ptr);
|
|
|
|
|
|
if (img)
|
2013-04-28 14:42:01 +02:00
|
|
|
|
{
|
2014-01-26 20:55:04 +01:00
|
|
|
|
memcpy (image, img, sizeof (*image));
|
|
|
|
|
|
result = 1;
|
2013-04-28 14:42:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
2014-01-26 20:55:04 +01:00
|
|
|
|
result = 0;
|
2013-04-28 14:42:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-26 20:55:04 +01:00
|
|
|
|
static SCM
|
|
|
|
|
|
scm_find_mapped_elf_image (SCM ip)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct mapped_elf_image image;
|
|
|
|
|
|
|
|
|
|
|
|
if (find_mapped_elf_image ((char *) scm_to_uintptr_t (ip), &image))
|
|
|
|
|
|
{
|
|
|
|
|
|
signed char *data = (signed char *) image.start;
|
|
|
|
|
|
size_t len = image.end - image.start;
|
|
|
|
|
|
|
|
|
|
|
|
return scm_c_take_gc_bytevector (data, len, SCM_BOOL_F);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return SCM_BOOL_F;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-11-07 17:53:04 +01:00
|
|
|
|
static SCM
|
|
|
|
|
|
scm_all_mapped_elf_images (void)
|
|
|
|
|
|
{
|
|
|
|
|
|
SCM result = SCM_EOL;
|
|
|
|
|
|
|
|
|
|
|
|
scm_i_pthread_mutex_lock (&scm_i_misc_mutex);
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t n;
|
|
|
|
|
|
for (n = 0; n < mapped_elf_images_count; n++)
|
|
|
|
|
|
{
|
|
|
|
|
|
signed char *data = (signed char *) mapped_elf_images[n].start;
|
|
|
|
|
|
size_t len = mapped_elf_images[n].end - mapped_elf_images[n].start;
|
|
|
|
|
|
result = scm_cons (scm_c_take_gc_bytevector (data, len, SCM_BOOL_F),
|
|
|
|
|
|
result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
scm_i_pthread_mutex_unlock (&scm_i_misc_mutex);
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-01-26 20:55:04 +01:00
|
|
|
|
struct frame_map_prefix
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_t_uint32 text_offset;
|
|
|
|
|
|
scm_t_uint32 maps_offset;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct frame_map_header
|
|
|
|
|
|
{
|
|
|
|
|
|
scm_t_uint32 addr;
|
|
|
|
|
|
scm_t_uint32 map_offset;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
verify (sizeof (struct frame_map_prefix) == 8);
|
|
|
|
|
|
verify (sizeof (struct frame_map_header) == 8);
|
|
|
|
|
|
|
|
|
|
|
|
const scm_t_uint8 *
|
2015-10-28 16:40:53 +00:00
|
|
|
|
scm_find_slot_map_unlocked (const scm_t_uint32 *ip)
|
2014-01-26 20:55:04 +01:00
|
|
|
|
{
|
|
|
|
|
|
struct mapped_elf_image *image;
|
|
|
|
|
|
char *base;
|
|
|
|
|
|
struct frame_map_prefix *prefix;
|
|
|
|
|
|
struct frame_map_header *headers;
|
|
|
|
|
|
scm_t_uintptr addr = (scm_t_uintptr) ip;
|
|
|
|
|
|
size_t start, end;
|
|
|
|
|
|
|
|
|
|
|
|
image = find_mapped_elf_image_unlocked ((char *) ip);
|
|
|
|
|
|
if (!image || !image->frame_maps)
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
base = image->frame_maps;
|
|
|
|
|
|
prefix = (struct frame_map_prefix *) base;
|
|
|
|
|
|
headers = (struct frame_map_header *) (base + sizeof (*prefix));
|
|
|
|
|
|
|
|
|
|
|
|
if (addr < ((scm_t_uintptr) image->start) + prefix->text_offset)
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
addr -= ((scm_t_uintptr) image->start) + prefix->text_offset;
|
|
|
|
|
|
|
|
|
|
|
|
start = 0;
|
|
|
|
|
|
end = (prefix->maps_offset - sizeof (*prefix)) / sizeof (*headers);
|
|
|
|
|
|
|
|
|
|
|
|
if (end == 0 || addr > headers[end - 1].addr)
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
while (start < end)
|
|
|
|
|
|
{
|
|
|
|
|
|
size_t n = start + (end - start) / 2;
|
|
|
|
|
|
|
|
|
|
|
|
if (addr == headers[n].addr)
|
|
|
|
|
|
return (const scm_t_uint8*) (base + headers[n].map_offset);
|
|
|
|
|
|
else if (addr < headers[n].addr)
|
|
|
|
|
|
end = n;
|
|
|
|
|
|
else
|
|
|
|
|
|
start = n + 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2001-04-16 03:43:48 +00:00
|
|
|
|
|
|
|
|
|
|
void
|
2013-11-19 21:45:07 +01:00
|
|
|
|
scm_bootstrap_loader (void)
|
2001-04-16 03:43:48 +00:00
|
|
|
|
{
|
2017-01-08 23:39:42 +01:00
|
|
|
|
page_size = getpagesize ();
|
|
|
|
|
|
/* page_size should be a power of two. */
|
|
|
|
|
|
if (page_size & (page_size - 1))
|
|
|
|
|
|
abort ();
|
|
|
|
|
|
|
2010-03-16 21:18:12 +01:00
|
|
|
|
scm_c_register_extension ("libguile-" SCM_EFFECTIVE_VERSION,
|
2013-11-19 21:45:07 +01:00
|
|
|
|
"scm_init_loader",
|
|
|
|
|
|
(scm_t_extension_init_func)scm_init_loader, NULL);
|
big reorg of scheme modules -- e.g. programs.c -> (system vm program)
This reorganization kills the ugly module-export-all hacks in
bootstrap.scm and core.scm. In fact, it gets rid of core.scm entirely,
breaking out its functionality into separate files.
* module/system/vm/trace.scm:
* module/system/vm/profile.scm:
* module/system/vm/disasm.scm:
* module/system/vm/debug.scm:
* module/system/vm/conv.scm:
* module/system/vm/assemble.scm:
* module/system/repl/repl.scm:
* module/system/repl/common.scm:
* module/system/base/compile.scm:
* module/system/repl/command.scm: Update for changes, and fix a bug in
procedure-documentation.
* module/system/vm/bootstrap.scm: Just call scm_bootstrap_vm, which
handles setting load-compiled for us.
* module/system/vm/core.scm: Removed, functionality folded into other
modules.
* module/system/vm/frame.scm: Export the C frame procedures here; also
move scheme functions from core.scm here.
* module/system/vm/instruction.scm: New file, exports procedures from
instructions.c.
* module/system/vm/objcode.scm: New file, exports procedures from
objcodes.c.
* module/system/vm/program.scm: New file, exports procedures from
programs.c, and some scheme functions originally from core.scm.
* module/system/vm/vm.scm: New file, from vm.c and core.scm.
* src/Makefile.am (libguile_vm_la_SOURCES): Add bootstrap.h.
* src/bootstrap.h: New file, prototypes scm_bootstrap_vm (), which the
scm_init_* functions call.
* src/frames.h:
* src/frames.c (scm_init_frames):
* src/frames.c (scm_bootstrap_frames):
* src/vm.h:
* src/instructions.h:
* src/instructions.c (scm_init_instructions):
* src/instructions.c (scm_bootstrap_instructions):
* src/objcodes.h:
* src/objcodes.c (scm_bootstrap_objcodes):
* src/objcodes.c (scm_init_objcodes):
* src/programs.h:
* src/programs.c (scm_bootstrap_programs):
* src/programs.c (scm_init_programs):
* src/vm.c (scm_bootstrap_vm):
* src/vm.c (scm_init_vm): Call scm_bootstrap_vm() before doing anything
in an init function. Bootstrap_vm will call bootstrap_instructions(),
etc to initialize types, then set load-compiled to point to
load-compiled/vm.
* src/vm.c (scm_load_compiled_with_vm): Code to load .go files, if
they're present.
2008-08-07 13:11:27 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2013-11-19 21:45:07 +01:00
|
|
|
|
scm_init_loader (void)
|
big reorg of scheme modules -- e.g. programs.c -> (system vm program)
This reorganization kills the ugly module-export-all hacks in
bootstrap.scm and core.scm. In fact, it gets rid of core.scm entirely,
breaking out its functionality into separate files.
* module/system/vm/trace.scm:
* module/system/vm/profile.scm:
* module/system/vm/disasm.scm:
* module/system/vm/debug.scm:
* module/system/vm/conv.scm:
* module/system/vm/assemble.scm:
* module/system/repl/repl.scm:
* module/system/repl/common.scm:
* module/system/base/compile.scm:
* module/system/repl/command.scm: Update for changes, and fix a bug in
procedure-documentation.
* module/system/vm/bootstrap.scm: Just call scm_bootstrap_vm, which
handles setting load-compiled for us.
* module/system/vm/core.scm: Removed, functionality folded into other
modules.
* module/system/vm/frame.scm: Export the C frame procedures here; also
move scheme functions from core.scm here.
* module/system/vm/instruction.scm: New file, exports procedures from
instructions.c.
* module/system/vm/objcode.scm: New file, exports procedures from
objcodes.c.
* module/system/vm/program.scm: New file, exports procedures from
programs.c, and some scheme functions originally from core.scm.
* module/system/vm/vm.scm: New file, from vm.c and core.scm.
* src/Makefile.am (libguile_vm_la_SOURCES): Add bootstrap.h.
* src/bootstrap.h: New file, prototypes scm_bootstrap_vm (), which the
scm_init_* functions call.
* src/frames.h:
* src/frames.c (scm_init_frames):
* src/frames.c (scm_bootstrap_frames):
* src/vm.h:
* src/instructions.h:
* src/instructions.c (scm_init_instructions):
* src/instructions.c (scm_bootstrap_instructions):
* src/objcodes.h:
* src/objcodes.c (scm_bootstrap_objcodes):
* src/objcodes.c (scm_init_objcodes):
* src/programs.h:
* src/programs.c (scm_bootstrap_programs):
* src/programs.c (scm_init_programs):
* src/vm.c (scm_bootstrap_vm):
* src/vm.c (scm_init_vm): Call scm_bootstrap_vm() before doing anything
in an init function. Bootstrap_vm will call bootstrap_instructions(),
etc to initialize types, then set load-compiled to point to
load-compiled/vm.
* src/vm.c (scm_load_compiled_with_vm): Code to load .go files, if
they're present.
2008-08-07 13:11:27 +02:00
|
|
|
|
{
|
2001-04-16 03:43:48 +00:00
|
|
|
|
#ifndef SCM_MAGIC_SNARFER
|
2013-11-19 21:45:07 +01:00
|
|
|
|
#include "libguile/loader.x"
|
2001-04-16 03:43:48 +00:00
|
|
|
|
#endif
|
static opcodes; refactor program/objcode division; use new assembly pipeline
* gdbinit: Untested attempts to get the stack fondling macros to deal
with the new program representation.
* libguile/frames.c (scm_vm_frame_arguments, scm_vm_frame_source)
(scm_vm_frame_local_ref, scm_vm_frame_local_set_x): SCM_PROGRAM_DATA is
a struct scm_objcode*.
* libguile/instructions.h:
* libguile/instructions.c: Hide the instruction table and the struct
scm_instruction structure; all access to instructions now goes through
procedures. This is because instructions are no longer in a packed
array indexed by opcode. Also, declare a mask that all instructions
should fit in.
* libguile/objcodes.h:
* libguile/objcodes.c: Rewrite so that object code directly maps its
arity and length from its bytecode. This makes it unnecessary to keep
this information in programs, allowing programs to be simple conses
between the code (objcodes) and data (the object table and the closure
variables).
* libguile/programs.c (scm_make_program): Rework so that make-program
takes objcode, an object table, and externals as arguments. It's much
clearer this way, and we avoid malloc().
* libguile/stacks.c (is_vm_bootstrap_frame): Update for program/objcode
changes.
* libguile/vm-engine.c (vm_run): Initialize the jump table on the first
run, with the opcodes declared in the instruction sources, and with bad
instructions raising an error instead of wandering off into the
Unknown.
* libguile/vm-engine.h (FETCH_LENGTH): Always represent lengths as 3
bytes. The old code was too error-prone.
(NEXT_JUMP): Mask the instruction with SCM_VM_INSTRUCTION_MASK.
(NEW_FRAME): Update for program/objcode changes.
* libguile/vm-expand.h (VM_DEFINE_FUNCTION, VM_DEFINE_INSTRUCTION)
(VM_DEFINE_LOADER): Update so that we explicitly specify opcodes, so
that we have a stable bytecode API.
* libguile/vm-i-loader.c: Update license to LGPLv2+. Explicitly declare
opcodes.
(load-integer): Use an int instead of a long as the accumulator; still
need to revisit this code at some point, I think.
(load-program): Simplify, thankfully!! Just creates the objcode slice
and rolls with it.
* libguile/vm-i-scheme.c: Number the opcodes explicitly.
* libguile/vm-i-system.c: Update license to LGPLv2+. Explicitly declare
opcodes.
(make-closure): Update for new program API.
* libguile/vm.c (vm_make_boot_program): Update for new program/objcode
API. Still a bit ugly.
(scm_load_compiled_with_vm): Update for new program/objcode API.
* module/language/assembly.scm (byte-length): Fix byte-length calculation
for loaders, and load-program.
(code-pack, code-unpack): Start to move things from (system vm conv)
here.
(object->code, code->object): More things from conv.scm.
* module/language/glil.scm (<glil-program>): Add a new field,
closure-level.
(make-glil-program, compute-closure-level): Calculate the "closure
level" when making a glil program. This is the maximum depth of
external binding refs in this closure.
(unparse-glil): Fix label serialization.
* module/language/glil/compile-assembly.scm (make-meta): Prepend #f for
the meta's object table, though maybe in the future we can avoid
creating assembly in the first place.
(assoc-ref-or-acons, object-index-and-alist): GRRR! Caught again by the
different sets of arguments to assoc and assoc-ref!
(glil->assembly): Attempt to make the <glil-program> case more
readable, and fix the bugs. Sorry I don't know how to comment this
change any more than this.
(glil->assembly): For <glil-module> serialize the whole key, not just
the name.
(dump-object): subprogram-code is already a list. Serialize integers as
strings, not u8vectors. Fix the order of lists and vectors.
* module/language/glil/spec.scm (glil): Switch orders, so we prefer glil
-> assembly -> objcode. Actually glil->objcode doesn't work any more,
needs to be removed I think.
* module/language/objcode/spec.scm (objcode->value):
s/objcode->program/make-program/.
* module/language/scheme/inline.scm: Add acons inline.
* module/system/vm/conv.scm (make-byte-decoder): Skip the first 8 bytes,
they are header. Handle subprograms properly. Still needs help though.
(decode-length): Lengths are always 3 bytes now.
* module/system/vm/disasm.scm: Superficial changes to keep things
working. I'd like to fix this better in the future.
* module/system/vm/frame.scm (bootstrap-frame?): Fixes for
program-bytecode.
* module/system/vm/program.scm: Export make-program. It's program-objcode
now, no more program-bytecode.
* module/system/vm/vm.scm (vm-load): Use make-program.
* test-suite/tests/asm-to-bytecode.test: New test, very minimal.
* module/system/vm/objcode.scm: Export word-size, byte-order, and
write-objcode.
2009-01-29 21:09:04 +01:00
|
|
|
|
|
2013-04-28 14:42:01 +02:00
|
|
|
|
scm_c_define_gsubr ("find-mapped-elf-image", 1, 0, 0,
|
|
|
|
|
|
(scm_t_subr) scm_find_mapped_elf_image);
|
2013-11-07 17:53:04 +01:00
|
|
|
|
scm_c_define_gsubr ("all-mapped-elf-images", 0, 0, 0,
|
|
|
|
|
|
(scm_t_subr) scm_all_mapped_elf_images);
|
2001-04-16 03:43:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
Local Variables:
|
|
|
|
|
|
c-file-style: "gnu"
|
|
|
|
|
|
End:
|
|
|
|
|
|
*/
|