This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[PATCH] Add support for the Win32 hook prologue (try 2)
A number of Windows programs(Steam, Xfire and others) try to hook Win32 API
functions by replacing the first 5 bytes in the function. This causes
problems for Wine because the functions generated by gcc usually start with a
different opcode sequence than the one expected by these applications.
Starting with Windows XP SP2, Microsoft starts Win32 functions with this
sequence:
8b ff mov %edi, %edi
55 push %ebp
8b ec mov %esp, %ebp
The attached patch implements a function attribute that allows Wine to request
the same 5 bytes at the beginning of a function.
I tested the testuite on x86_64-pc-linux-gnu.
Changes from the first try:
* Moved testcase to gcc.target/i386
* restructured ix86_handle_abi_attribute
* Changed HAVE_AS_IX86_SWAP handling as suggested by rth
* ix86_function_msvc_prologue returns a bool, removed fntype != NULL check
* fixed indentation in ix86_expand_prologue(hopefully correct now)
2009-09-09: Stefan DÃsinger <stefan@codeweavers.com
* config/i386/i386.c, config/i386/i386.md: Add a new function
attribute msvc_prologue that starts functions with the same opcode
sequence used in most Win32 API functions
* gcc.target/i386/msvc_prologue.c: New testcase
* configure.ac: Test for swap suffix support in as
Index: gcc/doc/extend.texi
===================================================================
--- gcc/doc/extend.texi (revision 151613)
+++ gcc/doc/extend.texi (working copy)
@@ -2672,6 +2672,14 @@ when targeting Windows. On all other systems, the
Note, This feature is currently sorried out for Windows targets trying to
+@item msvc_prologue
+@cindex @code{msvc_prologue} attribute
+
+On 32 bit i[34567]86-*-* targets, you can use this function attribute to make
+gcc generate the "hot-patching" function prologue used in Win32 API
+functions in Microsoft Windows XP Service Pack 2 and newer. This requires
+support for the swap suffix in the assembler. (GNU Binutils 2.19.51 or later)
+
@item naked
@cindex function without a prologue/epilogue code
Use this attribute on the ARM, AVR, IP2K and SPU ports to indicate that
Index: gcc/configure.ac
===================================================================
--- gcc/configure.ac (revision 151613)
+++ gcc/configure.ac (working copy)
@@ -3036,6 +3036,12 @@ foo: nop
[AC_DEFINE(HAVE_AS_IX86_SAHF, 1,
[Define if your assembler supports the sahf mnemonic.])])
+ gcc_GAS_CHECK_FEATURE([swap suffix],
+ gcc_cv_as_ix86_swap,,,
+ [movl.s %esp, %ebp],,
+ [AC_DEFINE(HAVE_AS_IX86_SWAP, 1,
+ [Define if your assembler supports the swap suffix.])])
+
gcc_GAS_CHECK_FEATURE([different section symbol subtraction],
gcc_cv_as_ix86_diff_sect_delta,,,
[.section .rodata
Index: gcc/config/i386/i386.md
===================================================================
--- gcc/config/i386/i386.md (revision 151613)
+++ gcc/config/i386/i386.md (working copy)
@@ -237,6 +237,7 @@
(UNSPECV_RDTSC 18)
(UNSPECV_RDTSCP 19)
(UNSPECV_RDPMC 20)
+ (UNSPECV_VSWAPMOV 21)
])
;; Constants to represent pcomtrue/pcomfalse variants
@@ -15747,6 +15748,16 @@
(set_attr "length_immediate" "0")
(set_attr "modrm" "0")])
+(define_insn "vswapmov"
+ [(set (match_operand:SI 0 "register_operand" "=r")
+ (match_operand:SI 1 "register_operand" "r"))
+ (unspec_volatile [(const_int 0)] UNSPECV_VSWAPMOV)]
+ ""
+ "movl.s\t%1,%0"
+ [(set_attr "length" "2")
+ (set_attr "length_immediate" "0")
+ (set_attr "modrm" "0")])
+
;; Pad to 16-byte boundary, max skip in op0. Used to avoid
;; branch prediction penalty for the third jump in a 16-byte
;; block on K8.
Index: gcc/config/i386/i386.c
===================================================================
--- gcc/config/i386/i386.c (revision 151613)
+++ gcc/config/i386/i386.c (working copy)
@@ -4777,6 +4777,19 @@ ix86_function_type_abi (const_tree fntype)
return ix86_abi;
}
+static bool
+ix86_function_msvc_prologue (const_tree fntype)
+{
+ if (!TARGET_64BIT)
+ {
+ if(lookup_attribute ("msvc_prologue", TYPE_ATTRIBUTES (fntype)))
+ {
+ return true;
+ }
+ }
+ return false;
+}
+
static enum calling_abi
ix86_function_abi (const_tree fndecl)
{
@@ -8317,6 +8330,7 @@ ix86_expand_prologue (void)
bool pic_reg_used;
struct ix86_frame frame;
HOST_WIDE_INT allocate;
+ int gen_frame_pointer = frame_pointer_needed;
ix86_finalize_stack_realign_flags ();
@@ -8329,6 +8343,45 @@ ix86_expand_prologue (void)
ix86_compute_frame_layout (&frame);
+ if(ix86_function_msvc_prologue (TREE_TYPE (current_function_decl)))
+ {
+ rtx push, mov;
+ /* Make sure the function starts with
+ 8b ff movl.s %edi,%edi
+ 55 push %ebp
+ 8b ec movl.s %esp,%ebp
+
+ This matches the hookable function prologue in Win32 API functions in Microsoft Windows
+ XP Service Pack 2 and newer. Wine uses this to enable Windows apps to hook the Win32 API
+ functions provided by Wine */
+ insn = emit_insn(gen_vswapmov(gen_rtx_REG (Pmode, DI_REG), gen_rtx_REG (Pmode, DI_REG)));
+ push = emit_insn (gen_push (hard_frame_pointer_rtx));
+ mov = emit_insn(gen_vswapmov(hard_frame_pointer_rtx, stack_pointer_rtx));
+
+ if(frame_pointer_needed && !(crtl->drap_reg && crtl->stack_realign_needed))
+ {
+ /* The push %ebp and movl.s %esp, %ebp already set up the frame pointer. No need to do
+ this again. */
+ gen_frame_pointer = 0;
+ RTX_FRAME_RELATED_P (push) = 1;
+ RTX_FRAME_RELATED_P (mov) = 1;
+ if (ix86_cfa_state->reg == stack_pointer_rtx)
+ {
+ ix86_cfa_state->reg = hard_frame_pointer_rtx;
+ }
+ }
+ else
+ {
+ /* If the frame pointer is not needed, pop %ebp again. This could be optimized for cases where
+ ebp needs to be backed up for some other reason.
+
+ If stack realignment is needed, pop the base pointer again, align the stack, and later
+ regenerate the frame pointer setup. The frame pointer generated by the msvc prologue
+ is not aligned, so it can't be used */
+ insn = emit_insn ((*ix86_gen_pop1) (hard_frame_pointer_rtx));
+ }
+ }
+
/* Emit prologue code to adjust stack alignment and setup DRAP, in case
of DRAP is needed and stack realignment is really needed after reload */
if (crtl->drap_reg && crtl->stack_realign_needed)
@@ -8378,7 +8431,7 @@ ix86_expand_prologue (void)
/* Note: AT&T enter does NOT have reversed args. Enter is probably
slower on all targets. Also sdb doesn't like it. */
- if (frame_pointer_needed)
+ if (gen_frame_pointer)
{
insn = emit_insn (gen_push (hard_frame_pointer_rtx));
RTX_FRAME_RELATED_P (insn) = 1;
@@ -26069,34 +26122,43 @@ ix86_handle_abi_attribute (tree *node, tree name,
*no_add_attrs = true;
return NULL_TREE;
}
- if (!TARGET_64BIT)
+ if (TARGET_64BIT)
{
- warning (OPT_Wattributes, "%qE attribute only available for 64-bit",
- name);
- *no_add_attrs = true;
- return NULL_TREE;
- }
+ /* Can combine regparm with all attributes but fastcall. */
+ if (is_attribute_p ("ms_abi", name))
+ {
+ if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
+ {
+ error ("ms_abi and sysv_abi attributes are not compatible");
+ }
- /* Can combine regparm with all attributes but fastcall. */
- if (is_attribute_p ("ms_abi", name))
- {
- if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
+ return NULL_TREE;
+ }
+ else if (is_attribute_p ("sysv_abi", name))
{
- error ("ms_abi and sysv_abi attributes are not compatible");
- }
+ if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
+ {
+ error ("ms_abi and sysv_abi attributes are not compatible");
+ }
- return NULL_TREE;
+ return NULL_TREE;
+ }
}
- else if (is_attribute_p ("sysv_abi", name))
+ else
{
- if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
+ if (is_attribute_p ("msvc_prologue", name))
{
- error ("ms_abi and sysv_abi attributes are not compatible");
- }
-
- return NULL_TREE;
+#ifndef HAVE_AS_IX86_SWAP
+ sorry ("msvc_prologue attribute needs assembler swap suffix support",
+ name);
+#endif
+ return NULL_TREE;
+ }
}
+ warning (OPT_Wattributes, "%qE attribute only available for %d-bit",
+ name, TARGET_64BIT ? 32 : 64);
+ *no_add_attrs = true;
return NULL_TREE;
}
@@ -28985,6 +29047,7 @@ static const struct attribute_spec ix86_attribute_
/* ms_abi and sysv_abi calling convention function attributes. */
{ "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
{ "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
+ { "msvc_prologue", 0, 0, false, true, true, ix86_handle_abi_attribute },
/* End element. */
{ NULL, 0, 0, false, false, false, NULL }
};
--- /dev/null 2009-09-09 12:15:38.779548517 +0200
+++ gcc/testsuite/gcc.target/i386/msvc_prologue.c 2009-09-10 22:18:39.000000000 +0200
@@ -0,0 +1,28 @@
+/* Test that the msvc_prologue attribute generates the correct code. */
+
+/* { dg-do run } */
+/* { dg-options "-O2 -fomit-frame-pointer -m32" } */
+
+int __attribute__((__msvc_prologue__)) foo()
+{
+ unsigned char *ptr = (unsigned char *) foo;
+
+ /* The NOP mov must not be optimized away by optimizations.
+ The push %ebp, mov %esp, %ebp must not be removed by
+ -fomit-frame-pointer */
+
+ /* movl.s %edi, %edi */
+ if(*ptr++ != 0x8b) return 1;
+ if(*ptr++ != 0xff) return 1;
+ /* push %ebp */
+ if(*ptr++ != 0x55) return 1;
+ /* movl.s %esp, %ebp */
+ if(*ptr++ != 0x8b) return 1;
+ if(*ptr++ != 0xec) return 1;
+ return 0;
+}
+
+int main ()
+{
+ return foo();
+}