--
Best regards,
LIU Hao

From 96b596ec89405a6c35b106f4b1ed16cf4ba997f0 Mon Sep 17 00:00:00 2001
From: LIU Hao <[email protected]>
Date: Thu, 31 Oct 2024 11:25:15 +0800
Subject: [PATCH] headers/intrin-impl: Fix segment accessors

First, these intrins read from and write to thread-local memory, so the asm
statement shall be `volatile`.

Second, for Intel syntax, this commit removes superfluous prefixes in front of
segment register names.

Third, previously `Offset` was cast to a pointer and dereferenced, and then
passed to inline assembly as a memory operand using the `m` constraint. This
was a pure hack. GCC assumes that a memory operand should belong in the DS
segment, so it appeared to reference unknown memory, and caused warnings like

  intrin-impl.h:849:1: warning: array subscript 0 is outside array bounds of
  'long long unsigned int[0]' [-Warray-bounds=]
  849 | __buildreadseg(__readgsqword, unsigned __int64, "gs", "q")
      | ^~~~~~~~~~~~~~

This commit passes the address by register instead. For Intel syntax, there is
no way to print the `DWORD PTR` thing, so unfortunately the value also has to
be passed by register. It's suboptimal, but should be safe.

For x86-64, the use of a 32-bit address requires an address size override
prefix. However, it's deliberate, as zero-extending a 32-bit register (like
`mov edi, edi`) would require two additional bytes.

Signed-off-by: LIU Hao <[email protected]>
---
 mingw-w64-headers/include/psdk_inc/intrin-impl.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/mingw-w64-headers/include/psdk_inc/intrin-impl.h b/mingw-w64-headers/include/psdk_inc/intrin-impl.h
index a62822a39..25043ad4b 100644
--- a/mingw-w64-headers/include/psdk_inc/intrin-impl.h
+++ b/mingw-w64-headers/include/psdk_inc/intrin-impl.h
@@ -230,9 +230,9 @@ Parameters: (FunctionName, DataType, Segment)
 
 #define __buildreadseg(x, y, z, a) y x(unsigned __LONG32 Offset) { \
     y ret; \
-    __asm__ ("mov{" a " %%" z ":%[offset], %[ret] | %[ret], %%" z ":%[offset]}" \
+    __asm__ volatile ("mov{" a " %%" z ":(%[offset]), %[ret] | %[ret], " z ":[%[offset]] }" \
         : [ret] "=r" (ret) \
-        : [offset] "m" ((*(y *) (size_t) Offset))); \
+        : [offset] "r" (Offset)); \
     return ret; \
 }
 
@@ -247,9 +247,9 @@ Parameters: (FunctionName, DataType, Segment)
    */
 
 #define __buildwriteseg(x, y, z, a) void x(unsigned __LONG32 Offset, y Data) { \
-    __asm__ ("mov{" a " %[Data], %%" z ":%[offset] | %%" z ":%[offset], %[Data]}" \
-        : [offset] "=m" ((*(y *) (size_t) Offset)) \
-        : [Data] "ri" (Data)); \
+    __asm__ volatile ("mov{" a " %[Data], %%" z ":(%[offset]) | " z ":[%[offset]], %[Data] }" \
+        : : [offset] "r" (Offset), \
+            [Data] "r" (Data)); \
 }
 
 /* This macro is used by _BitScanForward, _BitScanForward64, _BitScanReverse _BitScanReverse64
-- 
2.43.0

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to