From patchwork Tue Dec 6 19:50:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= X-Patchwork-Id: 30481 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3019159wrr; Tue, 6 Dec 2022 11:52:11 -0800 (PST) X-Google-Smtp-Source: AA0mqf6jXZu/ly8YhMIvZ/jEGlTQGP4bVrTdEV7oDCCB+AU2LAuMIzSy1zfG8CLZoypKUpzLFo+R X-Received: by 2002:a17:907:d40c:b0:7bb:c496:ca45 with SMTP id vi12-20020a170907d40c00b007bbc496ca45mr48262382ejc.464.1670356330965; Tue, 06 Dec 2022 11:52:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670356330; cv=none; d=google.com; s=arc-20160816; b=kfmJw5ODAVY8deLHlaZ9yVg46BrJncyrms7rV3/9r49zfjeg8S49yHxsVreIv1b2GG MwnZ0t5R1L652D3oqh2QpliCYZQQhssbDQLLRtRTajPvWkc+ULjqMrw0nE7EVGAfYniQ 3pF2lBDwTA5qIWLjQLqby/bq+R7xvd9bgYeNvyAZL2suX8SpOe/kPFN1UcNFThbPMESC Bd1AmsqCfNXjbKW/k4WVI5vkP5puv22hsOAkDx2LuaTvivBFPkB0005S3tjcuq09ffD3 07iNXXw4UJUfKI9MDG46BD04OYJfN7k5YppIFRZApk+xdgh8901jM+EivyI7XcdjmZlM goQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dmarc-filter :delivered-to; bh=er0r9I0u/J6B+XILO0c/KtQbRacwkCtHK2QB1+ynBIQ=; b=UlDrChbjCMulaiOfq8uDz9J9hfOP10DbbTvpEug7OWkrAmZikPYT7RiGQGWg2yJr7r j/Nr0h7w8XJt9LmK/XIMUCNqA8rSlDrXZyXkD4WYsFa78Mf5jddrRRwPwuzAh6O1BI/q vb6UBjYM9bmniq+r7Gtbw3Iwsu9znFJCnVGE6SCvsZN71J/hh4bV0F2KhMTleiWoRPJc +rVqel2twlSjEfHM9X+HxGWykmK0cRq4pklqf5edp+DciQVejS0t2l0BAt3tvM6ai31a 3J7STzDO9I+3aj0CeyL0VXCmLnntLmcs4iVw0+oN+ciq8/A2RLezENMQNh4X4zxtIEJz Ebog== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m13-20020a056402430d00b004619e210510si1785565edc.150.2022.12.06.11.52.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 11:52:10 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B76AF382B3E3 for ; Tue, 6 Dec 2022 19:51:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.hazardy.de (mail.hazardy.de [78.94.181.132]) by sourceware.org (Postfix) with ESMTPS id 4368F383B6F1 for ; Tue, 6 Dec 2022 19:50:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4368F383B6F1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hazardy.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hazardy.de Received: from NB-372.intranet.mimot.com (unknown [78.94.181.132]) by mail.hazardy.de (Postfix) with ESMTPSA id 360C4700276; Tue, 6 Dec 2022 20:50:36 +0100 (CET) From: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= To: gcc-patches@gcc.gnu.org, iant@golang.org Subject: [PATCH 1/4] libbacktrace: change all pc related variables to uintptr_t Date: Tue, 6 Dec 2022 20:50:25 +0100 Message-Id: <20221206195028.37104-1-gcc@hazardy.de> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751495559708086346?= X-GMAIL-MSGID: =?utf-8?q?1751495559708086346?= From: Björn Schäpers It's the right thing to do, since the PC shouldn't go out of the uintptr_t domain, and in backtrace_pcinfo the pc is uintptr_t. This is a preparation for a following patch. Tested on x86_64-linux and i686-w64-mingw32. -- >8 -- * dwarf.c: changed variables holding pc values from uint64_t to uintptr_t. Signed-off-by: Björn Schäpers --- libbacktrace/dwarf.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/libbacktrace/dwarf.c b/libbacktrace/dwarf.c index 45cc9e77e40..0707ccddd3e 100644 --- a/libbacktrace/dwarf.c +++ b/libbacktrace/dwarf.c @@ -274,8 +274,8 @@ struct function struct function_addrs { /* Range is LOW <= PC < HIGH. */ - uint64_t low; - uint64_t high; + uintptr_t low; + uintptr_t high; /* Function for this address range. */ struct function *function; }; @@ -356,8 +356,8 @@ struct unit struct unit_addrs { /* Range is LOW <= PC < HIGH. */ - uint64_t low; - uint64_t high; + uintptr_t low; + uintptr_t high; /* Compilation unit for this address range. */ struct unit *u; }; @@ -1094,7 +1094,7 @@ resolve_addr_index (const struct dwarf_sections *dwarf_sections, uint64_t addr_base, int addrsize, int is_bigendian, uint64_t addr_index, backtrace_error_callback error_callback, void *data, - uint64_t *address) + uintptr_t *address) { uint64_t offset; struct dwarf_buf addr_buf; @@ -1194,7 +1194,7 @@ function_addrs_search (const void *vkey, const void *ventry) static int add_unit_addr (struct backtrace_state *state, void *rdata, - uint64_t lowpc, uint64_t highpc, + uintptr_t lowpc, uintptr_t highpc, backtrace_error_callback error_callback, void *data, void *pvec) { @@ -1530,10 +1530,10 @@ lookup_abbrev (struct abbrevs *abbrevs, uint64_t code, lowpc/highpc is set or ranges is set. */ struct pcrange { - uint64_t lowpc; /* The low PC value. */ + uintptr_t lowpc; /* The low PC value. */ int have_lowpc; /* Whether a low PC value was found. */ int lowpc_is_addr_index; /* Whether lowpc is in .debug_addr. */ - uint64_t highpc; /* The high PC value. */ + uintptr_t highpc; /* The high PC value. */ int have_highpc; /* Whether a high PC value was found. */ int highpc_is_relative; /* Whether highpc is relative to lowpc. */ int highpc_is_addr_index; /* Whether highpc is in .debug_addr. */ @@ -1613,16 +1613,16 @@ add_low_high_range (struct backtrace_state *state, uintptr_t base_address, int is_bigendian, struct unit *u, const struct pcrange *pcrange, int (*add_range) (struct backtrace_state *state, - void *rdata, uint64_t lowpc, - uint64_t highpc, + void *rdata, uintptr_t lowpc, + uintptr_t highpc, backtrace_error_callback error_callback, void *data, void *vec), void *rdata, backtrace_error_callback error_callback, void *data, void *vec) { - uint64_t lowpc; - uint64_t highpc; + uintptr_t lowpc; + uintptr_t highpc; lowpc = pcrange->lowpc; if (pcrange->lowpc_is_addr_index) @@ -1663,7 +1663,7 @@ add_ranges_from_ranges ( struct unit *u, uint64_t base, const struct pcrange *pcrange, int (*add_range) (struct backtrace_state *state, void *rdata, - uint64_t lowpc, uint64_t highpc, + uintptr_t lowpc, uintptr_t highpc, backtrace_error_callback error_callback, void *data, void *vec), void *rdata, @@ -1727,10 +1727,10 @@ add_ranges_from_rnglists ( struct backtrace_state *state, const struct dwarf_sections *dwarf_sections, uintptr_t base_address, int is_bigendian, - struct unit *u, uint64_t base, + struct unit *u, uintptr_t base, const struct pcrange *pcrange, int (*add_range) (struct backtrace_state *state, void *rdata, - uint64_t lowpc, uint64_t highpc, + uintptr_t lowpc, uintptr_t highpc, backtrace_error_callback error_callback, void *data, void *vec), void *rdata, @@ -1796,8 +1796,8 @@ add_ranges_from_rnglists ( case DW_RLE_startx_endx: { uint64_t index; - uint64_t low; - uint64_t high; + uintptr_t low; + uintptr_t high; index = read_uleb128 (&rnglists_buf); if (!resolve_addr_index (dwarf_sections, u->addr_base, @@ -1819,8 +1819,8 @@ add_ranges_from_rnglists ( case DW_RLE_startx_length: { uint64_t index; - uint64_t low; - uint64_t length; + uintptr_t low; + uintptr_t length; index = read_uleb128 (&rnglists_buf); if (!resolve_addr_index (dwarf_sections, u->addr_base, @@ -1905,7 +1905,7 @@ add_ranges (struct backtrace_state *state, uintptr_t base_address, int is_bigendian, struct unit *u, uint64_t base, const struct pcrange *pcrange, int (*add_range) (struct backtrace_state *state, void *rdata, - uint64_t lowpc, uint64_t highpc, + uintptr_t lowpc, uintptr_t highpc, backtrace_error_callback error_callback, void *data, void *vec), void *rdata, @@ -3183,7 +3183,7 @@ read_referenced_name (struct dwarf_data *ddata, struct unit *u, static int add_function_range (struct backtrace_state *state, void *rdata, - uint64_t lowpc, uint64_t highpc, + uintptr_t lowpc, uintptr_t highpc, backtrace_error_callback error_callback, void *data, void *pvec) { @@ -3223,7 +3223,7 @@ add_function_range (struct backtrace_state *state, void *rdata, static int read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata, - struct unit *u, uint64_t base, struct dwarf_buf *unit_buf, + struct unit *u, uintptr_t base, struct dwarf_buf *unit_buf, const struct line_header *lhdr, backtrace_error_callback error_callback, void *data, struct function_vector *vec_function, From patchwork Tue Dec 6 19:50:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= X-Patchwork-Id: 30483 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3019328wrr; Tue, 6 Dec 2022 11:52:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf60WumsL7CUoKdIu8bF93l+ZCFwOeDSfOdTxgzq9mjd+tDlT9bY+2FuBdX4Pq8xcPrhNi+k X-Received: by 2002:a05:6402:54:b0:46a:c420:6da7 with SMTP id f20-20020a056402005400b0046ac4206da7mr46215142edu.202.1670356361246; Tue, 06 Dec 2022 11:52:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670356361; cv=none; d=google.com; s=arc-20160816; b=D59lYqoqagukOr+/+PDT3UVcOq7q9KTOlgkBC6MdEhBqZuxeoBBzKoytfqdET2qWAs 1dNNYz38h5y6gPR4wCMTHprX5QFqzsjNSZDLQm0eB9P/Rr01K2D0AoIk+EdI4WcKgzQC x8H0q7m948pG46GXhBlkWm32WjFnNc4CSVlm+I9a5LvzQzY3h7NoEsaIBmqIBdmhLGDC EeZkdmpc5u6faWV/BQ//2XIl/SOCeGPvF3u7cVXLN4BF8pQsKT+FwFah0T51DzW8pqMf nMo3z6+CJ9isot7mLgNOPZNFbZvTnrxWTHxyj3IV6rsRmOw9r9AJMJTYwWBihL/aa9h7 tc9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dmarc-filter:delivered-to; bh=lZwTuffxMGl6NvjHVBEMH+tQMewZG5eSJGxTNh9z2vM=; b=xYBe7TDMo15pNDMxSeTVyn+QMSboMLQMhjBxOSZSDbzmg5SnRncbW2qdbQGCkRG/CV oMeIUJHGfTArpWTaKt3pb2tQHqaqHlEZQ7+yezn6j++OVi3ZIJOSAhNKs3dxOBs1xuUf G0BVhkmWPNP055oMrhwlAUweur+2ehddCdW0adOalQFA8XQtPLRj8MYtJH2fvBXuZCW6 PeIFGF9u7KTghS9Q3QUB10f6bd13tDSE6FGIbOYsc89idIgvsPDSfv4nHqjQCNdaOz/J ATfZYDfafLVRc2bZZ1EyqEzquHIBt9cPEIMXf4Z9ay9ZfIBGMhdL397LrUh4Ye73puXu pyGg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id q20-20020a056402041400b0046b0205ab58si2369939edv.580.2022.12.06.11.52.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 11:52:41 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 176703833A19 for ; Tue, 6 Dec 2022 19:51:16 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.hazardy.de (mail.hazardy.de [78.94.181.132]) by sourceware.org (Postfix) with ESMTPS id 438C8383B6F4 for ; Tue, 6 Dec 2022 19:50:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 438C8383B6F4 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hazardy.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hazardy.de Received: from NB-372.intranet.mimot.com (unknown [78.94.181.132]) by mail.hazardy.de (Postfix) with ESMTPSA id 723A37002D6; Tue, 6 Dec 2022 20:50:36 +0100 (CET) From: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= To: gcc-patches@gcc.gnu.org, iant@golang.org Subject: [PATCH 2/4] libbacktrace: detect executable path on windows Date: Tue, 6 Dec 2022 20:50:26 +0100 Message-Id: <20221206195028.37104-2-gcc@hazardy.de> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221206195028.37104-1-gcc@hazardy.de> References: <20221206195028.37104-1-gcc@hazardy.de> MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751495592319193716?= X-GMAIL-MSGID: =?utf-8?q?1751495592319193716?= From: Björn Schäpers This is actually needed so that libstdc++'s implementation to be able to work on windows. Tested on x86_64-linux and i686-w64-mingw32. -- >8 -- * configure.ac: Add a check for windows.h. * configure, config.h.in: Regenerate. * fileline.c: Add windows_get_executable_path. * fileline.c (fileline_initialiez): Add a pass using windows_get_executable_path. Signed-off-by: Björn Schäpers --- libbacktrace/configure.ac | 2 ++ libbacktrace/fileline.c | 43 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/libbacktrace/configure.ac b/libbacktrace/configure.ac index 1daaa2f62d2..b5feb29bcdc 100644 --- a/libbacktrace/configure.ac +++ b/libbacktrace/configure.ac @@ -377,6 +377,8 @@ if test "$have_loadquery" = "yes"; then AC_DEFINE(HAVE_LOADQUERY, 1, [Define if AIX loadquery is available.]) fi +AC_CHECK_HEADERS(windows.h) + # Check for the fcntl function. if test -n "${with_target_subdir}"; then case "${host}" in diff --git a/libbacktrace/fileline.c b/libbacktrace/fileline.c index a40cd498114..73c2c8e8bc9 100644 --- a/libbacktrace/fileline.c +++ b/libbacktrace/fileline.c @@ -47,6 +47,18 @@ POSSIBILITY OF SUCH DAMAGE. */ #include #endif +#ifdef HAVE_WINDOWS_H +#ifndef WIN32_MEAN_AND_LEAN +#define WIN32_MEAN_AND_LEAN +#endif + +#ifndef NOMINMAX +#define NOMINMAX +#endif + +#include +#endif + #include "backtrace.h" #include "internal.h" @@ -155,6 +167,28 @@ macho_get_executable_path (struct backtrace_state *state, #endif /* !defined (HAVE_MACH_O_DYLD_H) */ +#ifdef HAVE_WINDOWS_H + +static char * +windows_get_executable_path (char *buf, backtrace_error_callback error_callback, + void *data) +{ + if (GetModuleFileNameA (NULL, buf, MAX_PATH - 1) == 0) + { + error_callback (data, + "could not get the filename of the current executable", + (int) GetLastError ()); + return NULL; + } + return buf; +} + +#else /* !defined (HAVE_WINDOWS_H) */ + +#define windows_get_executable_path(buf, error_callback, data) NULL + +#endif /* !defined (HAVE_WINDOWS_H) */ + /* Initialize the fileline information from the executable. Returns 1 on success, 0 on failure. */ @@ -168,7 +202,11 @@ fileline_initialize (struct backtrace_state *state, int called_error_callback; int descriptor; const char *filename; +#ifdef HAVE_WINDOWS_H + char buf[MAX_PATH]; +#else char buf[64]; +#endif if (!state->threaded) failed = state->fileline_initialization_failed; @@ -192,7 +230,7 @@ fileline_initialize (struct backtrace_state *state, descriptor = -1; called_error_callback = 0; - for (pass = 0; pass < 8; ++pass) + for (pass = 0; pass < 9; ++pass) { int does_not_exist; @@ -224,6 +262,9 @@ fileline_initialize (struct backtrace_state *state, case 7: filename = macho_get_executable_path (state, error_callback, data); break; + case 8: + filename = windows_get_executable_path (buf, error_callback, data); + break; default: abort (); } From patchwork Tue Dec 6 19:50:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= X-Patchwork-Id: 30482 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3019179wrr; Tue, 6 Dec 2022 11:52:14 -0800 (PST) X-Google-Smtp-Source: AA0mqf4CBY7wDrMwJrvN2PhaYKrhWZe9Tgdw7Hcz1aJEhtd0fbnYf9AghI8gngS72HMzKXmB2SF5 X-Received: by 2002:a17:906:e2d4:b0:7c1:532:f420 with SMTP id gr20-20020a170906e2d400b007c10532f420mr5340699ejb.679.1670356334616; Tue, 06 Dec 2022 11:52:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670356334; cv=none; d=google.com; s=arc-20160816; b=HMy/coa/T0mOQJF3Wq4l7NYSU1VKlbP6VN3NnGCzckaIenCfj0e2XFu9A/2jlbhZgl vztvv9HfSqfocIlvDWLCo5hFWeIccNeKplUMuiqMeiOerFkpQsmKonqKadZXFB5MMGhv Q6NT6c4SteWvCXpLb6lzAi6JGW5lzuoiEKGlunaVBoSD98hso1eGEo6/9CFymF8ngmZ4 JPCpq8fvTOVGLqnPDWKjEVWX4MtjN2i23EUBnMgvVl6ykPcrSvi1qKTFrtTQBCsNp12G FTJE2+R9pwGtSG/HiiKY6L2RIdUsXxmR7umrEk+ORRJzjzNqSxvOfUcuc5xvgAJP3/hv xPeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dmarc-filter:delivered-to; bh=4M8ObylRJfGvNaemd4kyd0yRgikcz0LJno69sIT8KQI=; b=kE6grEa4oO6bt1CV+Ny5zib0mbjnUP5oI2bcXVibC8LFaGlRGt5uQqGE/uYiKXos1h h3AHPrMdz4SiAmbRCU8Yr15/OtqPElGQquoVijAYVgUi+kbR4IgaPwmWoC8oDaMjZBgj Bm55SypN+YaPLgpYtWw3gtym4apiskBX8/kKYDydzNX1v7WipyjhZunapKr+ztv2mq2a oDobiCb7pyP8lQtR5FF2l1K/JCKWroC6LjpschDZJQk6SEORJZl6nLzHwFQfRvQWHA21 jaboZgoc/rPkQah5y9W8vlSKsN+Z/S3KEm2LAWV05iGKAwCK59hQ0FmYWm1e5xL9KMyU 016A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id nb41-20020a1709071ca900b007aed5ffeffbsi6697445ejc.78.2022.12.06.11.52.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 11:52:14 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 92D9838F66C6 for ; Tue, 6 Dec 2022 19:51:09 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.hazardy.de (mail.hazardy.de [78.94.181.132]) by sourceware.org (Postfix) with ESMTPS id C3A28383B6FA for ; Tue, 6 Dec 2022 19:50:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C3A28383B6FA Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hazardy.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hazardy.de Received: from NB-372.intranet.mimot.com (unknown [78.94.181.132]) by mail.hazardy.de (Postfix) with ESMTPSA id 8DC937009F2; Tue, 6 Dec 2022 20:50:36 +0100 (CET) From: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= To: gcc-patches@gcc.gnu.org, iant@golang.org Subject: [PATCH 3/4] libbacktrace: work with aslr on windows Date: Tue, 6 Dec 2022 20:50:27 +0100 Message-Id: <20221206195028.37104-3-gcc@hazardy.de> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221206195028.37104-1-gcc@hazardy.de> References: <20221206195028.37104-1-gcc@hazardy.de> MIME-Version: 1.0 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751495564140525841?= X-GMAIL-MSGID: =?utf-8?q?1751495564140525841?= From: Björn Schäpers Any underflow which might happen, will be countered by an overflow in dwarf.c. Tested on x86_64-linux and i686-w64-mingw32. -- >8 -- Fixes https://github.com/ianlancetaylor/libbacktrace/issues/89 and https://github.com/ianlancetaylor/libbacktrace/issues/82. * pecoff.c (coff_add): Set the base_address of the module, to find the debug information on moved applications. Signed-off-by: Björn Schäpers --- libbacktrace/pecoff.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/libbacktrace/pecoff.c b/libbacktrace/pecoff.c index 87b3c0cc647..296f1357b5f 100644 --- a/libbacktrace/pecoff.c +++ b/libbacktrace/pecoff.c @@ -39,6 +39,18 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "backtrace.h" #include "internal.h" +#ifdef HAVE_WINDOWS_H +#ifndef WIN32_MEAN_AND_LEAN +#define WIN32_MEAN_AND_LEAN +#endif + +#ifndef NOMINMAX +#define NOMINMAX +#endif + +#include +#endif + /* Coff file header. */ typedef struct { @@ -610,6 +622,8 @@ coff_add (struct backtrace_state *state, int descriptor, int debug_view_valid; int is_64; uintptr_t image_base; + uintptr_t base_address = 0; + uintptr_t module_handle; struct dwarf_sections dwarf_sections; *found_sym = 0; @@ -856,7 +870,12 @@ coff_add (struct backtrace_state *state, int descriptor, + (sections[i].offset - min_offset)); } - if (!backtrace_dwarf_add (state, /* base_address */ 0, &dwarf_sections, +#ifdef HAVE_WINDOWS_H + module_handle = (uintptr_t) GetModuleHandleW (NULL); + base_address = module_handle - image_base; +#endif + + if (!backtrace_dwarf_add (state, base_address, &dwarf_sections, 0, /* FIXME: is_bigendian */ NULL, /* altlink */ error_callback, data, fileline_fn, From patchwork Tue Dec 6 19:50:28 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= X-Patchwork-Id: 30480 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3019156wrr; Tue, 6 Dec 2022 11:52:09 -0800 (PST) X-Google-Smtp-Source: AA0mqf7B84ubK82+wpElVGmyPGh9rIPORciH5JMoV6PkafjyIOMH4AM5GpSPinOYTswgSH7oEE8c X-Received: by 2002:a17:906:2355:b0:7c0:e6da:e25a with SMTP id m21-20020a170906235500b007c0e6dae25amr11167668eja.22.1670356329634; Tue, 06 Dec 2022 11:52:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670356329; cv=none; d=google.com; s=arc-20160816; b=kODNYP+9uUQFuXzz8BKoadmJX3r0IqmBaQS/h+ekTd2ReUlSNfD7oK7Bra9Zq9VJRY Fkwn17sVpxJ2S7HsXmI1+cLBpviU/eBZbJ8bzXV73TyCcpTfVQhRMHt0StpeOjPbFP5A BM0kM/m3uTCDbTQTrLurZI1epb2HET4itoLvccDIGxqmSEq6E6aFUW0ZuG/MnOCZgNvK cHtOY65Rjted58rBYpkvDzrft3E7S2R7eHzsurGqNSqy8dy8YjidS6mCzrf09rxSKKP8 ZgLyHmR0v5bYIuNjnq4ZbZ+ITpOmu4GHdutP+YPicdh5EAU7FdANb08c2mVP88qkY2up ADGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dmarc-filter:delivered-to; bh=iaXDAdfhD7kVYSziRMvZEgDC1bCbldNtT/3FbZq/Lss=; b=iqWH/H6KccwbeYAUSPgZ0tHtNCV69LEKjoW+AvBlXr9n3kN2Q5heNWFf8H7taUHKFu aLKn/YmyRied5oJqpxoh5YHu/fQT/q36+VjDWZVuLHLHymYQ15yf7JCbTEGxHqMh8uuH qwijRzOcpBTcZqeK3f+iFpr70kBPijk4leZBpk3B5nIhsDmTKubI1Ag4LWNqQHu/htb5 r+w/q5G9RAmOnKf6nm5l7ItiDUMeq/9P3HTsd3SAIfpTGVH0URumVc3U+3cS/NjGnsqQ BAC8uXT1Mq2u7kTXuWA5qub/sBED0ad3Jr8qANlczfqLYnwe4IGFk1Y809uz5KOhsuIz o0hw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id hs39-20020a1709073ea700b0078e1246e7d6si3267085ejc.748.2022.12.06.11.52.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 11:52:09 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8EDB63839D30 for ; Tue, 6 Dec 2022 19:51:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail.hazardy.de (mail.hazardy.de [78.94.181.132]) by sourceware.org (Postfix) with ESMTPS id C71FB383B6F9 for ; Tue, 6 Dec 2022 19:50:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C71FB383B6F9 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hazardy.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=hazardy.de Received: from NB-372.intranet.mimot.com (unknown [78.94.181.132]) by mail.hazardy.de (Postfix) with ESMTPSA id 4D79C700B61; Tue, 6 Dec 2022 20:50:37 +0100 (CET) From: =?utf-8?q?Bj=C3=B6rn_Sch=C3=A4pers?= To: gcc-patches@gcc.gnu.org, iant@golang.org Subject: [PATCH 4/4] libbacktrace: get debug information for loaded dlls Date: Tue, 6 Dec 2022 20:50:28 +0100 Message-Id: <20221206195028.37104-4-gcc@hazardy.de> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221206195028.37104-1-gcc@hazardy.de> References: <20221206195028.37104-1-gcc@hazardy.de> MIME-Version: 1.0 X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751495558667644333?= X-GMAIL-MSGID: =?utf-8?q?1751495558667644333?= From: Björn Schäpers Fixes https://github.com/ianlancetaylor/libbacktrace/issues/53, except that libraries loaded after the backtrace_initialize are not handled. But as far as I can see that's the same for elf. Tested on x86_64-linux and i686-w64-mingw32. -- >8 -- * pecoff.c (coff_add): New argument for the module handle of the file, to get the base address. * pecoff.c (backtrace_initialize): Iterate over loaded libraries and call coff_add. Signed-off-by: Björn Schäpers --- libbacktrace/pecoff.c | 76 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 4 deletions(-) diff --git a/libbacktrace/pecoff.c b/libbacktrace/pecoff.c index 296f1357b5f..40395109e51 100644 --- a/libbacktrace/pecoff.c +++ b/libbacktrace/pecoff.c @@ -49,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #endif #include +#include #endif /* Coff file header. */ @@ -592,7 +593,8 @@ coff_syminfo (struct backtrace_state *state, uintptr_t addr, static int coff_add (struct backtrace_state *state, int descriptor, backtrace_error_callback error_callback, void *data, - fileline *fileline_fn, int *found_sym, int *found_dwarf) + fileline *fileline_fn, int *found_sym, int *found_dwarf, + uintptr_t module_handle ATTRIBUTE_UNUSED) { struct backtrace_view fhdr_view; off_t fhdr_off; @@ -623,7 +625,6 @@ coff_add (struct backtrace_state *state, int descriptor, int is_64; uintptr_t image_base; uintptr_t base_address = 0; - uintptr_t module_handle; struct dwarf_sections dwarf_sections; *found_sym = 0; @@ -871,7 +872,6 @@ coff_add (struct backtrace_state *state, int descriptor, } #ifdef HAVE_WINDOWS_H - module_handle = (uintptr_t) GetModuleHandleW (NULL); base_address = module_handle - image_base; #endif @@ -914,12 +914,80 @@ backtrace_initialize (struct backtrace_state *state, int found_sym; int found_dwarf; fileline coff_fileline_fn; + uintptr_t module_handle = 0; + +#ifdef HAVE_WINDOWS_H + DWORD i; + DWORD module_count; + DWORD bytes_needed_for_modules; + HMODULE *modules; + char module_name[MAX_PATH]; + int module_found_sym; + fileline module_fileline_fn; + + module_handle = (uintptr_t) GetModuleHandleW (NULL); +#endif ret = coff_add (state, descriptor, error_callback, data, - &coff_fileline_fn, &found_sym, &found_dwarf); + &coff_fileline_fn, &found_sym, &found_dwarf, module_handle); if (!ret) return 0; +#ifdef HAVE_WINDOWS_H + module_count = 1000; + alloc_modules: + modules = backtrace_alloc (state, module_count * sizeof(HMODULE), + error_callback, data); + if (modules == NULL) + goto skip_modules; + if (!EnumProcessModules (GetCurrentProcess (), modules, module_count, + &bytes_needed_for_modules)) + { + error_callback(data, "Could not enumerate process modules", + (int) GetLastError ()); + goto free_modules; + } + if (bytes_needed_for_modules > module_count * sizeof(HMODULE)) + { + backtrace_free (state, modules, module_count * sizeof(HMODULE), + error_callback, data); + // Add an extra of 2, if some module is loaded in another thread. + module_count = bytes_needed_for_modules / sizeof(HMODULE) + 2; + modules = NULL; + goto alloc_modules; + } + + for (i = 0; i < bytes_needed_for_modules / sizeof(HMODULE); ++i) + { + if (GetModuleFileNameA (modules[i], module_name, MAX_PATH - 1)) + { + if (strcmp (filename, module_name) == 0) + continue; + + module_handle = (uintptr_t) GetModuleHandleA (module_name); + if (module_handle == 0) + continue; + + descriptor = backtrace_open (module_name, error_callback, data, NULL); + if (descriptor < 0) + continue; + + coff_add (state, descriptor, error_callback, data, + &module_fileline_fn, &module_found_sym, &found_dwarf, + module_handle); + if (module_found_sym) + found_sym = 1; + } + } + + free_modules: + if (modules) + backtrace_free(state, modules, module_count * sizeof(HMODULE), + error_callback, data); + modules = NULL; + skip_modules: +#endif + if (!state->threaded) { if (found_sym)