From patchwork Tue Mar 3 16:22:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 38382 Received: (qmail 41172 invoked by alias); 3 Mar 2020 16:22:27 -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 41128 invoked by uid 89); 3 Mar 2020 16:22:27 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.6 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=HX-Languages-Length:3227 X-HELO: sonic312-26.consmr.mail.ir2.yahoo.com Received: from sonic312-26.consmr.mail.ir2.yahoo.com (HELO sonic312-26.consmr.mail.ir2.yahoo.com) (77.238.178.97) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 03 Mar 2020 16:22:25 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.de; s=s2048; t=1583252543; bh=Q8wDeA5XoGQ25ghL8FSHYBNi/faFzYfJCKBUbULkCP8=; h=From:To:Subject:Date:References:From:Subject; b=Rfjyu8s0QyTK0FavBXECci/mWrd5TZ5u+mIiXdDwqwcvjltQComq62KMNsGHXLYWUxAeAUb/Y+JeXGKTM1RWhjOHwbrUiEE4BgNg7Hh485kk5RB1etUwkan8ls+bKMs0Z1UIyKLSUQThN2aOWGfqmrWSYSmIb4YKk5OmbJHtzsC3qB49phC1tpMmPvkyyGzWIsjshoZCM7rDQWU6AoSkGQZwrwIvZJYs0y63+6AAF3UKOmg2K5nWXM66r0BcQPLdFLZYiVzpa4gyI8h9G9NYCSg90A1/lvROrOuNaBlUzxMtpqoQtOVGi6Hx1R9MVDAe3kOgwfmRFtLy6d/M6RujUw== Received: from sonic.gate.mail.ne1.yahoo.com by sonic312.consmr.mail.ir2.yahoo.com with HTTP; Tue, 3 Mar 2020 16:22:23 +0000 Received: by smtp430.mail.ir2.yahoo.com (Oath Hermes SMTP Server) with ESMTPA ID dd28c2cc264400f938680e208cdc359d; Tue, 03 Mar 2020 16:22:22 +0000 (UTC) X-Patchwork-Original-From: "Hannes Domani via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Hannes Domani To: gdb-patches@sourceware.org Subject: [PATCH v3] Rebase executable to match relocated base address Date: Tue, 3 Mar 2020 17:22:07 +0100 Message-Id: <20200303162207.17473-1-ssbssa@yahoo.de> MIME-Version: 1.0 References: <20200303162207.17473-1-ssbssa.ref@yahoo.de> Content-Length: 3311 X-IsSubscribed: yes Windows executables linked with -dynamicbase get a new base address when loaded, which makes debugging impossible if the executable isn't also rebased in gdb. The new base address is read from the Process Environment Block. gdb/ChangeLog: 2020-03-03 Hannes Domani * windows-tdep.c (windows_solib_create_inferior_hook): New function. (windows_init_abi): Set and use windows_so_ops. --- v2: This version now no longer needs the fake auxv entry. v3: - Use of explicit comparison operators. - Use copy of solib_target_so_ops. --- gdb/windows-tdep.c | 55 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c index 6eef3fbd96..4e5d8303ca 100644 --- a/gdb/windows-tdep.c +++ b/gdb/windows-tdep.c @@ -34,6 +34,9 @@ #include "solib.h" #include "solib-target.h" #include "gdbcore.h" +#include "coff/internal.h" +#include "libcoff.h" +#include "solist.h" /* Windows signal numbers differ between MinGW flavors and between those and Cygwin. The below enumeration was gleaned from the @@ -812,6 +815,53 @@ windows_get_siginfo_type (struct gdbarch *gdbarch) return siginfo_type; } +/* Implement the "solib_create_inferior_hook" target_so_ops method. */ + +static void +windows_solib_create_inferior_hook (int from_tty) +{ + CORE_ADDR exec_base = 0; + + /* Find base address of main executable in + TIB->process_environment_block->image_base_address. */ + struct gdbarch *gdbarch = target_gdbarch (); + enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); + int ptr_bytes; + int peb_offset; /* Offset of process_environment_block in TIB. */ + int base_offset; /* Offset of image_base_address in PEB. */ + if (gdbarch_ptr_bit (gdbarch) == 32) + { + ptr_bytes = 4; + peb_offset = 48; + base_offset = 8; + } + else + { + ptr_bytes = 8; + peb_offset = 96; + base_offset = 16; + } + CORE_ADDR tlb; + gdb_byte buf[8]; + if (target_get_tib_address (inferior_ptid, &tlb) + && !target_read_memory (tlb + peb_offset, buf, ptr_bytes)) + { + CORE_ADDR peb = extract_unsigned_integer (buf, ptr_bytes, byte_order); + if (!target_read_memory (peb + base_offset, buf, ptr_bytes)) + exec_base = extract_unsigned_integer (buf, ptr_bytes, byte_order); + } + + /* Rebase executable if the base address changed because of ASLR. */ + if (symfile_objfile != nullptr && exec_base != 0) + { + CORE_ADDR vmaddr = pe_data (exec_bfd)->pe_opthdr.ImageBase; + if (vmaddr != exec_base) + objfile_rebase (symfile_objfile, exec_base - vmaddr); + } +} + +static struct target_so_ops windows_so_ops; + /* To be called from the various GDB_OSABI_CYGWIN handlers for the various Windows architectures and machine types. */ @@ -830,7 +880,10 @@ windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch) set_gdbarch_gdb_signal_to_target (gdbarch, windows_gdb_signal_to_target); - set_solib_ops (gdbarch, &solib_target_so_ops); + windows_so_ops = solib_target_so_ops; + windows_so_ops.solib_create_inferior_hook + = windows_solib_create_inferior_hook; + set_solib_ops (gdbarch, &windows_so_ops); set_gdbarch_get_siginfo_type (gdbarch, windows_get_siginfo_type); }