public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Björn Schäpers" <gcc@hazardy.de>
To: Eli Zaretskii <eliz@gnu.org>, Gabriel Ravier <gabravier@gmail.com>
Cc: gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org
Subject: Re: [PATCH 3/4] libbacktrace: work with aslr on windows
Date: Mon, 20 Nov 2023 20:57:38 +0100	[thread overview]
Message-ID: <cf2682f0-6b4c-04b0-a6da-ca4da9388fab@hazardy.de> (raw)
In-Reply-To: <83pmb8ktbe.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1957 bytes --]

An updated version, using neither A or W, but just the macro.

Am 21.01.2023 um 12:42 schrieb Eli Zaretskii:
>> Date: Sat, 21 Jan 2023 11:47:42 +0100
>> Cc: gcc@hazardy.de, gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org
>> From: Gabriel Ravier <gabravier@gmail.com>
>>
>>
>> On 1/21/23 05:05, Eli Zaretskii wrote:
>>>> Date: Fri, 20 Jan 2023 21:39:56 +0100
>>>> Cc: gcc@hazardy.de, gcc-patches@gcc.gnu.org, gcc@gcc.gnu.org
>>>> From: Gabriel Ravier <gabravier@gmail.com>
>>>>
>>>>>> - using wide APIs with Windows is generally considered to be a best
>>>>>> practice, even when not strictly needed (and in this case I can't see
>>>>>> any problem with doing so, unless maybe we want to code to work with
>>>>>> Windows 95 or something like that...)
>>>>> There's no reason to forcibly break GDB on platforms where wide APIs
>>>>> are not available.
>>>> Are there even any platforms that have GetModuleHandleA but not
>>>> GetModuleHandleW ? MSDN states that Windows XP and Windows Server 2003
>>>> are the first versions to support both of the APIs, so if this is
>>>> supposed to work on Windows 98, for instance, whether we're using
>>>> GetModuleHandleA or GetModuleHandleW won't matter.
>>> I'm not sure I follow the logic.  A program that calls
>>> GetModuleHandleW will refuse to start on Windows that doesn't have
>>> that API.  So any version before XP is automatically excluded the
>>> moment you use code which calls that API directly (i.e. not through a
>>> function pointer or somesuch).
>> A program that calls GetModuleHandleA will also refuse to start on
>> Windows if it doesn't have that API. The set of Windows versions that do
>> not have GetModuleHandleA is, according to MSDN, the same as the set of
>> Windows versions that do not have GetModuleHandleW.
> 
> MSDN lies (because it wants to pretend that older versions don't
> exist).  Try this much more useful site:
> 
>    http://winapi.freetechsecrets.com/win32/WIN32GetModuleHandle.htm

[-- Attachment #2: 0002-libbacktrace-work-with-aslr-on-windows.patch --]
[-- Type: text/plain, Size: 2121 bytes --]

From 52cbe06b1c165172191f66ff7e55a49adecf661d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern@hazardy.de>
Date: Sun, 30 Apr 2023 23:52:37 +0200
Subject: [PATCH 2/3] libbacktrace: work with aslr on windows
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

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 <bjoern@hazardy.de>
---
 libbacktrace/pecoff.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

diff --git a/libbacktrace/pecoff.c b/libbacktrace/pecoff.c
index 56af4828e27..9cc13b47947 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 <windows.h>
+#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) GetModuleHandle (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,
-- 
2.42.1


  reply	other threads:[~2023-11-20 19:57 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-20 10:54 [PATCH 1/4] libbacktrace: change all pc related variables to uintptr_t Björn Schäpers
2023-01-20 10:54 ` [PATCH 2/4] libbacktrace: detect executable path on windows Björn Schäpers
2023-01-23 23:00   ` Ian Lance Taylor
2023-01-24 13:11     ` Eli Zaretskii
2023-01-24 14:35       ` Ian Lance Taylor
2023-01-24 16:52         ` Eli Zaretskii
2023-01-24 17:58           ` Ian Lance Taylor
2023-01-24 18:11             ` Eli Zaretskii
2023-01-24 18:32               ` Ian Lance Taylor
2023-02-05  9:20                 ` Björn Schäpers
2023-02-06  0:22                   ` Ian Lance Taylor
2023-11-20 19:56                     ` Björn Schäpers
2023-11-29 22:05                       ` Ian Lance Taylor
2023-01-24 21:00           ` Björn Schäpers
2023-01-20 10:54 ` [PATCH 3/4] libbacktrace: work with aslr " Björn Schäpers
2023-01-20 13:39   ` Eli Zaretskii
2023-01-20 16:46     ` Gabriel Ravier
2023-01-20 19:19       ` Eli Zaretskii
2023-01-20 20:39         ` Gabriel Ravier
2023-01-21  4:05           ` Eli Zaretskii
2023-01-21  9:18             ` LIU Hao
2023-01-21  9:25               ` Eli Zaretskii
2023-01-21 10:47             ` Gabriel Ravier
2023-01-21 11:42               ` Eli Zaretskii
2023-11-20 19:57                 ` Björn Schäpers [this message]
2023-11-20 20:07                   ` Eli Zaretskii
2023-11-21 19:35                     ` Björn Schäpers
2023-11-22  1:13                       ` LIU Hao
2023-11-30 19:25                   ` Ian Lance Taylor
2023-01-20 10:54 ` [PATCH 4/4] libbacktrace: get debug information for loaded dlls Björn Schäpers
2023-11-30 19:53   ` Ian Lance Taylor
2023-11-30 20:16     ` Eli Zaretskii
2024-01-02 23:12     ` Björn Schäpers
2024-01-04 22:33       ` [PATCH 5/4] libbacktrace: improve getting " Björn Schäpers
2024-01-06 22:15         ` [PATCH 6/4] libbacktrace: Add loaded dlls after initialize Björn Schäpers
2024-01-07  6:50           ` Eli Zaretskii
     [not found]             ` <4cb3a2a5-c0b3-40c8-b460-f21d65a9aea2@hazardy.de>
2024-01-07 14:46               ` Eli Zaretskii
2024-01-07 16:07                 ` Björn Schäpers
2024-01-07 17:03                   ` Eli Zaretskii
2024-01-09 20:02                     ` Björn Schäpers
2024-01-10 12:34                       ` Eli Zaretskii
2024-03-15 20:41                         ` Björn Schäpers
2024-01-23 22:37         ` [PATCH 5/4] libbacktrace: improve getting debug information for loaded dlls Ian Lance Taylor
2024-01-25 19:53           ` Björn Schäpers
2024-01-25 22:04             ` Ian Lance Taylor
2024-03-15 20:40               ` Björn Schäpers
2024-04-25 20:14                 ` Björn Schäpers
2024-04-28 18:16                   ` Ian Lance Taylor
2024-05-02 19:23                     ` Björn Schäpers
2024-05-03 22:27                       ` Ian Lance Taylor
2024-01-23 21:24       ` [PATCH 4/4] libbacktrace: get " Björn Schäpers
2023-01-20 22:25 ` [PATCH 1/4] libbacktrace: change all pc related variables to uintptr_t Ian Lance Taylor
2023-01-23 20:17   ` Björn Schäpers

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=cf2682f0-6b4c-04b0-a6da-ca4da9388fab@hazardy.de \
    --to=gcc@hazardy.de \
    --cc=eliz@gnu.org \
    --cc=gabravier@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc@gcc.gnu.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).