海湾合作委员会内联汇编JMP地址;裸体功能 [英] gcc inlined assembly jmp address; Naked functions

查看:331
本文介绍了海湾合作委员会内联汇编JMP地址;裸体功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以JMP使用Visual Studio 2012中。当它涉及到的gcc / MinGW的,我不知道我跳是正确的地址。

I can jmp to an address using Visual Studio 2012.. When it comes to gcc/mingw, I cannot tell if my jump is correct.

我怎么可以跳转到一个地址在GCC?

How can I jump to an address in gcc?

我试过:

__declspec(naked) void DXHook_D3DPERF_BeginEvent()
{
    #ifdef _MSC_VER  //If using visual studio..
    __asm{jmp[Addr]} //Jump to: Address stored in Addr.
    #else            //else using gcc..
    __asm("jmp *%0"
          : /*No Outputs*/
          : "r" (Addr)
          : "%eax");
    #endif
}

这是正确的?此外,有没有办法让GCC停止打扰我一下:

Is this correct? Also, is there a way to get gcc to stop bothering me about:

warning: 'naked' attribute directive ignored.

为什么它忽略我赤裸的属性?

Why does it ignore my naked attribute?

推荐答案

TL; DR 在海湾合作委员会,这是的仅适用于:ARM,AVR,系列mCore,MSP430,NDS32,RL78,RX和SPU港口

TL;DR In GCC, this is only available on: ARM, AVR, MCORE, MSP430, NDS32, RL78, RX and SPU ports

不会提供的 86

<一个href=\"http://stackoverflow.com/questions/8374449/equivalent-for-gccs-naked-attribute/8375416#8375416\">Workaround (由布兰登在评论中提出的)。

Workaround (proposed by Brendon in the comments).

完整的答案

(这个答案的其余部分假定您使用的是支持的目标)

这是因为你使用的是Windows属性语法, __ declspec 与海湾合作委员会。

That is because you are using the Windows attribute syntax, __declspec with GCC.

__ declspec MSDN参考报价C $ C> :

Quote from the MSDN reference on __declspec:

扩展属性语法简化和标准化的 Microsoft特定扩展到C和C ++语言。

The extended attribute syntax simplifies and standardizes Microsoft-specific extensions to the C and C++ languages.

您应该使用 GCC功能属性语法代替或并行

另外请注意,从这个GCC文章

注:语义并不Windows和这个海湾合作委员会之间的同
  功能 - 例如,__declspec(dllexport)的无效​​(*富)(无效),并
  无效(__declspec(dllexport)的*富)(无效)的意思是完全不同的事情
  而它产生的有关无法适用警告
  属性非类型的海湾合作​​委员会。

Note: The semantics are not the same between Windows and this GCC feature - for example, __declspec(dllexport) void (*foo)(void) and void (__declspec(dllexport) *foo)(void) mean quite different things whereas this generates a warning about not being able to apply attributes to non-types on GCC.

所以也可能是您正在使用的GCC的 __ declspec 语法方式的问题(如果它甚至支持的话)。

So there could also be an issue with the way you are using the __declspec syntax in GCC (if it even supports it).

您还应该注意的是,只有 __ declspec 属性,它海合会国家支持是 __ declspec(dllexport)的(如上述已经提到的 GCC属性语法链接)。

You should also note that the only __declspec attribute that GCC states it supports is the __declspec(dllexport) (as stated in the already mentioned GCC attribute syntax link).

因此​​,让我们来看看一个通用的解决您的问题,但首先我们需要了解实际的 GCC属性语法并找到如下:

So let's look for a generic solution to your problem but first we will need to read about the actual GCC attribute syntax and find the following:

这是属性说明符列表可能在声明符之前立即出现
  在声明符在一个逗号分隔的列表(比第一个除外)
  使用单列出了多个标识符声明
  符和限定词。这样的属性说明符仅适用于
  符前的声明符出现。例如,在

An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators in a declaration of more than one identifier using a single list of specifiers and qualifiers. Such attribute specifiers apply only to the identifier before whose declarator they appear. For example, in

 __attribute__((noreturn)) void d0 (void),
     __attribute__((format(printf, 1, 2))) d1 (const char *, ...),
      d2 (void)


  
  

noreturn属性适用于声明的所有功能;该
  格式属性仅适用于D1。

the noreturn attribute applies to all the functions declared; the format attribute only applies to d1.

所以,问题的解决方案会是这样的:

So the solution to your problem would be something like the following:

#ifdef __GNUC__
#define ATTRIBUTE_NAKED __attribute__((naked))
#else
#define ATTRIBUTE_NAKED __declspec(naked)
#endif

ATTRIBUTE_NAKED void DXHook_D3DPERF_BeginEvent()
{
    #ifdef _MSC_VER  //If using visual studio..
    __asm{jmp[Addr]} //Jump to: Address stored in Addr.
    #else            //else using gcc..
    __asm("jmp *%0"
          : /*No Outputs*/
          : "r" (Addr)
          : "%eax");
    #endif
}

编辑:

要注意,这个属性是平台具体是很重要的。我引述:

It is important to note that this attribute is platform specific. And I quote:

此属性可在ARM,AVR,系列mCore,MSP430,NDS32,RL78,RX和SPU端口。它可以让编译器构建
  必要的函数声明,同时允许的身体
  功能是组装code。指定的功能将不会有
  编译器生成的序言/结尾序列。只有基本ASM
  语句可以安全地被包含在裸功能(请参阅基本ASM)。
  虽然使用扩展ASM或基本ASM和C code可混合
  出现工作,它们不能依赖于可靠地工作,并且
  不支持的。

This attribute is available on the ARM, AVR, MCORE, MSP430, NDS32, RL78, RX and SPU ports. It allows the compiler to construct the requisite function declaration, while allowing the body of the function to be assembly code. The specified function will not have prologue/epilogue sequences generated by the compiler. Only basic asm statements can safely be included in naked functions (see Basic Asm). While using extended asm or a mixture of basic asm and C code may appear to work, they cannot be depended upon to work reliably and are not supported.

GCC文档功能属性引用。

边注

铛可能进一步阅读的属性可以帮助(大多与GCC兼容),但< A HREF =htt​​p://clang-developers.42468.n3.nabble.com/naked-attribute-td4031883.html相对=nofollow>这些评论似乎表明匹配GCC行为的愿望。

Possible further reading on clang attributes could help (mostly compatible with GCC) but these comments seem to suggest a desire to match GCC behavior.

这篇关于海湾合作委员会内联汇编JMP地址;裸体功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆