exit (1);
}
-/* Update .p_vaddr and .sh_addr as if the code was JITted to ADDR. */
+/* Rename jit_function_XXXX to match idx */
static void
-update_locations (const void *const addr, int idx)
+update_name (const void *const addr, int idx)
{
const ElfW (Ehdr) *const ehdr = (ElfW (Ehdr) *)addr;
ElfW (Shdr) *const shdr = (ElfW (Shdr) *)((char *)addr + ehdr->e_shoff);
ElfW (Phdr) *const phdr = (ElfW (Phdr) *)((char *)addr + ehdr->e_phoff);
int i;
- for (i = 0; i < ehdr->e_phnum; ++i)
- if (phdr[i].p_type == PT_LOAD)
- phdr[i].p_vaddr += (ElfW (Addr))addr;
-
for (i = 0; i < ehdr->e_shnum; ++i)
{
if (shdr[i].sh_type == SHT_STRTAB)
if (strcmp (p, "jit_function_XXXX") == 0)
sprintf (p, "jit_function_%04d", idx);
}
-
- if (shdr[i].sh_flags & SHF_ALLOC)
- shdr[i].sh_addr += (ElfW (Addr))addr;
}
}
#define MAIN main
#endif
+/* Must be defined by .exp file when compiling to know
+ what address to map the ELF binary to. */
+#ifndef LOAD_ADDRESS
+#error "Must define LOAD_ADDRESS"
+#endif
+#ifndef LOAD_INCREMENT
+#error "Must define LOAD_INCREMENT"
+#endif
+
/* Used to spin waiting for GDB. */
volatile int wait_for_gdb = ATTACH;
#define WAIT_FOR_GDB do {} while (wait_for_gdb)
exit (1);
}
- const void *const addr = mmap (0, st.st_size, PROT_READ|PROT_WRITE,
- MAP_PRIVATE, fd, 0);
+ void *load_addr = (void *) (size_t) (LOAD_ADDRESS + (i - 1) * LOAD_INCREMENT);
+ const void *const addr = mmap (load_addr, st.st_size, PROT_READ|PROT_WRITE,
+ MAP_PRIVATE | MAP_FIXED, fd, 0);
struct jit_code_entry *const entry = calloc (1, sizeof (*entry));
if (addr == MAP_FAILED)
exit (1);
}
- update_locations (addr, i);
+ update_name (addr, i);
/* Link entry at the end of the list. */
entry->symfile_addr = (const char *)addr;
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# Magic constants used to calculate a starting address when linking
+# "jit" shared libraries. When loaded, will be mapped by jit-elf-main
+# to the same address.
+
+set jit_load_address 0x7000000
+set jit_load_increment 0x1000000
+
# Compile jit-elf-main.c as an executable.
#
# BINSUFFIX is appended to the binary name.
# On success, return 0.
# On failure, return -1.
proc compile_jit_main {main_srcfile main_binfile options} {
- set options [concat $options debug]
+ global jit_load_address jit_load_increment
+
+ set options [concat \
+ $options \
+ additional_flags=-DLOAD_ADDRESS=$jit_load_address \
+ additional_flags=-DLOAD_INCREMENT=$jit_load_increment \
+ debug]
if { [gdb_compile ${main_srcfile} ${main_binfile} \
executable $options] != "" } {
# On success, return 0.
# On failure, return -1.
proc compile_jit_elf_main_as_so {main_solib_srcfile main_solib_binfile options} {
- set options [concat $options debug]
+ global jit_load_address jit_load_increment
+
+ set options [list \
+ additional_flags="-DMAIN=jit_dl_main" \
+ additional_flags=-DLOAD_ADDRESS=$jit_load_address \
+ additional_flags=-DLOAD_INCREMENT=$jit_load_increment \
+ debug]
if { [gdb_compile_shlib ${main_solib_srcfile} ${main_solib_binfile} \
$options] != "" } {
# On success, return a list of target path to the shared libraries.
# On failure, return -1.
proc compile_and_download_n_jit_so {jit_solib_basename jit_solib_srcfile count} {
+ global jit_load_address jit_load_increment
set binfiles_target {}
for {set i 1} {$i <= $count} {incr i} {
# do symbol renaming by munging on ELF symbol table, and that
# wouldn't work for .debug sections. Also, output for "info
# function" changes when debug info is present.
- if { [gdb_compile_shlib ${jit_solib_srcfile} ${binfile} {}] != "" } {
+ set addr [format 0x%x [expr $jit_load_address + $jit_load_increment * [expr $i-1]]]
+ # Using -Ttext-segment flag to ask linked to relocate everything
+ # in the compiled shared library against a fixed base address. Combined
+ # with mapping the resulting binary to the same fixed base it allows
+ # to dynamically execute functions from it without any further adjustments.
+ set options [list \
+ additional_flags=-Xlinker \
+ additional_flags=-Ttext-segment=$addr]
+ if { [gdb_compile_shlib ${jit_solib_srcfile} ${binfile} $options] != "" } {
untested "failed to compile ${jit_solib_basename}.c as a shared library"
return -1
}