public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Mihails Strasuns <mihails.strasuns@intel.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v5 3/5] [gdb/testsuite] use -Ttext-segment for jit-elf tests
Date: Mon, 11 May 2020 12:28:53 +0200	[thread overview]
Message-ID: <20200511102855.4987-3-mihails.strasuns@intel.com> (raw)
In-Reply-To: <20200511102855.4987-1-mihails.strasuns@intel.com>

Removes the need to manually relocate loaded ELF binary by using a fixed
constant as both mmap base address and as a requested first segment
address supplied to the linker.

In future will enable JIT tests with a valid DWARF debug info.  Current
tests still need to compile without a debug info though, because they do
a function name modification.

gdb/testsuite/ChangeLog:

2020-02-18  Mihails Strasuns  <mihails.strasuns@intel.com>

	* lib/jit-elf-helpers.exp: Supply -Ttext-segment linker flag and
	  define LOAD_ADDRESS/LOAD_INCREMENT macros for the compiled binaries.
	* gdb.base/jit-elf-main.c: Use LOAD_ADDRESS/LOAD_INCREMENT to
	  calculate the mmap address.
---
 gdb/testsuite/gdb.base/jit-elf-main.c | 27 +++++++++++++-----------
 gdb/testsuite/lib/jit-elf-helpers.exp | 30 ++++++++++++++++++++++++---
 2 files changed, 42 insertions(+), 15 deletions(-)

diff --git a/gdb/testsuite/gdb.base/jit-elf-main.c b/gdb/testsuite/gdb.base/jit-elf-main.c
index acfd17d417..be7185ef1a 100644
--- a/gdb/testsuite/gdb.base/jit-elf-main.c
+++ b/gdb/testsuite/gdb.base/jit-elf-main.c
@@ -51,20 +51,16 @@ usage (void)
   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)
@@ -81,9 +77,6 @@ update_locations (const void *const addr, int idx)
             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;
     }
 }
 
@@ -96,6 +89,15 @@ update_locations (const void *const addr, int idx)
 #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)
@@ -139,8 +141,9 @@ MAIN (int argc, char *argv[])
 	  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)
@@ -149,7 +152,7 @@ MAIN (int argc, char *argv[])
 	  exit (1);
 	}
 
-      update_locations (addr, i);
+      update_name (addr, i);
 
       /* Link entry at the end of the list.  */
       entry->symfile_addr = (const char *)addr;
diff --git a/gdb/testsuite/lib/jit-elf-helpers.exp b/gdb/testsuite/lib/jit-elf-helpers.exp
index de6ac46f05..246d0dfcea 100644
--- a/gdb/testsuite/lib/jit-elf-helpers.exp
+++ b/gdb/testsuite/lib/jit-elf-helpers.exp
@@ -13,6 +13,13 @@
 # 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 load_address   0x7000000
+set load_increment 0x1000000
+
 # Compiles jit-elf-main.c as a regular executable
 
 # Compile jit-elf-main.c as an executable.
@@ -23,7 +30,13 @@
 # On success, return 0.
 # On failure, return -1.
 proc compile_jit_main {main_srcfile main_binfile options} {
-    set options [concat $options debug]
+    global load_address load_increment
+
+    set options [concat \
+	$options \
+	additional_flags=-DLOAD_ADDRESS=$load_address \
+	additional_flags=-DLOAD_INCREMENT=$load_increment \
+	debug]
 
     if { [gdb_compile ${main_srcfile} ${main_binfile} \
 	  executable $options] != "" } {
@@ -41,7 +54,13 @@ proc compile_jit_main {main_srcfile main_binfile 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 load_address load_increment
+
+    set options [list \
+	additional_flags="-DMAIN=jit_dl_main" \
+	additional_flags=-DLOAD_ADDRESS=$load_address \
+	additional_flags=-DLOAD_INCREMENT=$load_increment \
+	debug]
 
     if { [gdb_compile_shlib ${main_solib_srcfile} ${main_solib_binfile} \
 	    $options] != "" } {
@@ -58,6 +77,7 @@ proc compile_jit_elf_main_as_so {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 load_address load_increment
     set binfiles_target {}
 
     for {set i 1} {$i <= $count} {incr i} {
@@ -67,7 +87,11 @@ proc compile_and_download_n_jit_so {jit_solib_basename jit_solib_srcfile count}
 	# 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 $load_address + $load_increment * [expr $i-1]]]
+	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
 	}
-- 
2.26.2

Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928


  parent reply	other threads:[~2020-05-11 10:26 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <167782>
2020-05-11 10:28 ` [PATCH v5 1/5] [gdb/testsuite] use args as lib list " Mihails Strasuns
2020-05-11 10:28   ` [PATCH v5 2/5] [gdb/testsuite] add lib/jit-elf-helpers.exp Mihails Strasuns
2020-05-12  0:23     ` Simon Marchi
2020-05-11 10:28   ` Mihails Strasuns [this message]
2020-05-12  0:28     ` [PATCH v5 3/5] [gdb/testsuite] use -Ttext-segment for jit-elf tests Simon Marchi
2020-05-11 10:28   ` [PATCH v5 4/5] [gdb/testsuite] define jit function name via macro Mihails Strasuns
2020-05-12  0:31     ` Simon Marchi
2020-05-11 10:28   ` [PATCH v5 5/5] [gdb/testsuite] add jit-elf-util.h and run jit function Mihails Strasuns
2020-05-12  0:37     ` Simon Marchi
2020-05-12  0:17   ` [PATCH v5 1/5] [gdb/testsuite] use args as lib list for jit-elf tests Simon Marchi
2020-05-12  7:51     ` Strasuns, Mihails
2020-05-12 14:13       ` Simon Marchi
2020-05-12 14:15         ` Simon Marchi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200511102855.4987-3-mihails.strasuns@intel.com \
    --to=mihails.strasuns@intel.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).