From patchwork Tue Feb 18 12:43:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Strasuns, Mihails" X-Patchwork-Id: 38206 Received: (qmail 113630 invoked by alias); 18 Feb 2020 12:42:53 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 113551 invoked by uid 89); 18 Feb 2020 12:42:52 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT autolearn=ham version=3.3.1 spammy=Must, spin X-HELO: mga03.intel.com Received: from mga03.intel.com (HELO mga03.intel.com) (134.134.136.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 18 Feb 2020 12:42:50 +0000 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Feb 2020 04:42:49 -0800 Received: from labpc7920x-08.iul.intel.com (HELO localhost) ([172.28.49.153]) by fmsmga001-auth.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 18 Feb 2020 04:42:49 -0800 From: Mihails Strasuns To: gdb-patches@sourceware.org Cc: Mihails Strasuns Subject: [PATCH 6/7] [gdb/testsuite] use -Ttext-segment for jit-elf tests Date: Tue, 18 Feb 2020 13:43:38 +0100 Message-Id: <20200218124339.11270-7-mihails.strasuns@intel.com> In-Reply-To: <20200218124339.11270-1-mihails.strasuns@intel.com> References: <20200218124339.11270-1-mihails.strasuns@intel.com> MIME-Version: 1.0 X-IsSubscribed: yes 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 * 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 Change-Id: Ifdd70d2838d9235e5d4fb49b7cafd03cb4865751 Signed-off-by: Mihails Strasuns --- gdb/testsuite/gdb.base/jit-elf-main.c | 25 ++++++++++++++----------- gdb/testsuite/lib/jit-elf-helpers.exp | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/gdb/testsuite/gdb.base/jit-elf-main.c b/gdb/testsuite/gdb.base/jit-elf-main.c index 0e4b2e9a40..09b9a90ada 100644 --- a/gdb/testsuite/gdb.base/jit-elf-main.c +++ b/gdb/testsuite/gdb.base/jit-elf-main.c @@ -51,20 +51,16 @@ usage () 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 while (wait_for_gdb) @@ -137,7 +139,8 @@ MAIN (int argc, char *argv[]) exit (1); } - const void *const addr = mmap (0, st.st_size, PROT_READ|PROT_WRITE, + 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, fd, 0); struct jit_code_entry *const entry = calloc (1, sizeof (*entry)); @@ -147,7 +150,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 f63694b8f5..cff47ec73c 100644 --- a/gdb/testsuite/lib/jit-elf-helpers.exp +++ b/gdb/testsuite/lib/jit-elf-helpers.exp @@ -13,15 +13,25 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +# 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 proc compile_jit_main {binsuffix options} { global srcdir subdir testfile srcfile binfile + global load_address load_increment set testfile jit-elf-main set srcfile ${testfile}.c set binfile [standard_output_file $testfile$binsuffix] set options [concat \ $options \ + additional_flags=-DLOAD_ADDRESS=$load_address \ + additional_flags=-DLOAD_INCREMENT=$load_increment \ debug] if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" \ executable $options] != "" } { @@ -33,12 +43,15 @@ proc compile_jit_main {binsuffix options} { proc compile_jit_main_as_so {binsuffix options} { global srcdir subdir testfile srcfile binfile + global load_address load_increment set testfile jit-elf-main set srcfile ${testfile}.c set binfile [standard_output_file $testfile$binsuffix] set options [concat \ $options \ additional_flags="-DMAIN=jit_dl_main" \ + additional_flags=-DLOAD_ADDRESS=$load_address \ + additional_flags=-DLOAD_INCREMENT=$load_increment \ debug] if { [gdb_compile_shlib "${srcdir}/${subdir}/${srcfile}" "${binfile}" \ $options] != "" } { @@ -51,6 +64,7 @@ proc compile_jit_main_as_so {binsuffix options} { proc compile_n_jit_so {count binsuffix options} { global srcdir subdir solib_binfile_targets + global load_address load_increment set solib_binfile_targets {} set solib_testfile jit-elf-solib @@ -63,6 +77,11 @@ proc compile_n_jit_so {count binsuffix options} { # 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. + set addr [format 0x%x [expr $load_address + $load_increment * [expr $i-1]]] + set options [concat \ + $options \ + additional_flags=-Xlinker \ + additional_flags=-Ttext-segment=$addr] if { [gdb_compile_shlib $solib_srcfile $solib_binfile $options] != "" } { untested "Failure to compile $solib_binfile_test_msg" }