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,