免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5937 | 回复: 2
打印 上一主题 下一主题

gcc inline-assembly问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-08-11 16:53 |只看该作者 |倒序浏览
Hi,

我最近在做一个mocking库的移植,GAS我不是很熟悉。编译时有问题,请教各位大侠,谢谢。

源码:
  1. #include "Mocker.h"
  2.    
  3.     namespace MockItNow
  4.     {
  5.         int g_jumpAddress;
  6.    
  7.         extern "C" void __declspec(naked) _penter()
  8.         {
  9.             // Prologue
  10.             __asm
  11.             {
  12.                 push ebp
  13.                 mov ebp, esp
  14.                 sub esp, __LOCAL_SIZE
  15.                 push eax
  16.                 push ebx
  17.                 push ecx
  18.                 push edx
  19.             }
  20.    
  21.             int* framePointer;
  22.             __asm mov dword ptr [framePointer], ebp
  23.    
  24.             int targetPointer;
  25.             __asm mov [targetPointer], ecx
  26.    
  27.             {
  28.                 int callingFunctionAddress = (*(framePointer + 1) - 5);
  29.    
  30.                 if (Mocker::IsAlive() == false || Mocker::Instance().ShouldStubFunction(callingFunctionAddress, targetPointer) == false)
  31.                 {
  32.                     // Standard epilogue. Don't stub the function
  33.                     __asm
  34.                     {
  35.                         pop edx
  36.                         pop ecx
  37.                         pop ebx
  38.                         pop eax
  39.                         mov esp, ebp
  40.                         pop ebp
  41.                         ret
  42.                     }
  43.                 }
  44.    
  45.                 Mocker::Instance().SetCurrentFunctionAddress(callingFunctionAddress);
  46.    
  47.                 g_jumpAddress = Mocker::Instance().GetFunctionJumpAddress();
  48.    
  49.                 // Non-standard epilogue. Stub the function
  50.                 __asm
  51.                 {
  52.                     pop edx
  53.                     pop ecx
  54.                     pop ebx
  55.                     pop eax
  56.                     mov esp, ebp
  57.                     pop ebp
  58.                     add esp, 4
  59.                     jmp g_jumpAddress
  60.                 }
  61.             }
  62.         }
  63.     }
复制代码
我修改的:
  1. 1 #include "Mocker.h"
  2.       2  
  3.       3 using namespace MockItNow;
  4.       4  
  5.       5 int g_jumpAddress;
  6.       6 int * g_pFrame;
  7.       7 int g_targetAddress;
  8.       8  
  9.       9 __attribute__((no_instrument_function))
  10.      10 void __cyg_profile_func_enter(void * pFun, void * pCallSite);
  11.      11  
  12.      12 __attribute__((no_instrument_function))
  13.      13 void __cyg_profile_func_exit(void * pFun, void * pCallSite);
  14.      14  
  15.      15 void __cyg_profile_func_enter(void * pFun, void * pCallSite)
  16.      16 {
  17.      17     // Prologue
  18.      18     asm volatile ("pushl %ebp\n\t"
  19.      19         "movl %esp, %ebp\n\t"
  20.      20         "subl __LOCAL_SIZE, %esp\n\t"
  21.      21         "pushl %eax\n\t"
  22.      22         "pushl %ebx\n\t"
  23.      23         "pushl %ecx\n\t"
  24.      24         "pushl %edx");
  25.      25  
  26.      26     asm volatile ("movl %ebp, $g_pFrame\n\t"
  27.      27         "movl %ecx, g_targetAddress");
  28.      28        
  29.      29     {   
  30.      30         int callingFunctionAddress = (*(g_pFrame + 1) - 5);
  31.      31         
  32.      32         if(Mocker::IsAlive() == false || Mocker::Instance().ShouldStubFunction(callingFunctionAddress, g_targetAddress) == false)
  33.      33         {   
  34.      34             // Standard epilogue. Don't stub the function
  35.      35             asm volatile ("popl %edx\n\t"
  36.      36                 "popl %ecx\n\t"
  37.      37                 "popl %ebx\n\t"
  38.      38                 "popl %eax\n\t"
  39.      39                 "movl %ebp, %esp\n\t"
  40.      40                 "popl %ebp\n\t"
  41.      41                 "ret");
  42.      42         }
  43.      43  
  44.      44         Mocker::Instance().SetCurrentFunctionAddress(callingFunctionAddress);
  45.      45  
  46.      46         g_jumpAddress = Mocker::Instance().GetFunctionJumpAddress();
  47.      47  
  48.      48         // Non-standard epilogue. Stub the function
  49.      49         asm volatile ("popl %edx\n\t"
  50.      50             "popl %ecx\n\t"
  51.      51             "popl %ebx\n\t"
  52.      52             "popl %eax\n\t"
  53.      53             "movl %ebp, %esp\n\t"
  54.      54             "popl %ebp\n\t"
  55.      55             "addl $4, %esp\n\t"
  56.      56             "jmp g_jumpAddress");
  57.      57     }
  58.      58 }
  59.      59  
  60.      60  
  61.      61 void __cyg_profile_func_exit(void * pFun, void * pCallSite)
  62.      62 {
  63.      63     // nothing
  64.      64 }
复制代码
编译报错:
  1.     make[1]: Entering directory `/cygdrive/d/tools/test/MockItNow2/MockItNow'
  2.     /bin/sh ./libtool  --tag=CXX   --mode=compile g++ -DHAVE_CONFIG_H -I.     -g -O2 -MT Thunk.lo -MD -MP -MF .deps/Thunk.Tpo -c -o Thunk.lo `test -f 'Win32/Thunk.cpp' || echo './'`Win32/Thunk.cpp
  3.     libtool: compile:  g++ -DHAVE_CONFIG_H -I. -g -O2 -MT Thunk.lo -MD -MP -MF .deps/Thunk.Tpo -c Win32/Thunk.cpp  -DDLL_EXPORT -DPIC -o .libs/Thunk.o
  4.     /tmp/ccRfDyJZ.s: Assembler messages:
  5.     /tmp/ccRfDyJZ.s:57: Error: operand type mismatch for `mov'
  6.     make[1]: *** [Thunk.lo] Error 1
  7.     make[1]: Leaving directory `/cygdrive/d/tools/test/MockItNow2/MockItNow'
  8.     make: *** [all] Error 2
复制代码

论坛徽章:
2
摩羯座
日期:2013-10-10 14:29:04天蝎座
日期:2014-01-03 09:14:49
2 [报告]
发表于 2011-08-15 12:48 |只看该作者
#     /tmp/ccRfDyJZ.s: Assembler messages:
#     /tmp/ccRfDyJZ.s:57: Error: operand type mismatch for `mov'

先用 -save-temps 选项把.s文件保存下来,然后看看57行是什么。

论坛徽章:
0
3 [报告]
发表于 2011-08-17 18:40 |只看该作者
回复 2# EricFisher
找到问题了。

  1. asm volatile ("movl %ebp, $g_pFrame\n\t"
  2.         "movl %ecx, g_targetAddress");
复制代码
应该改为:

  1. asm volatile ("movl %ebp, (g_pFrame)\n\t"
  2.         "movl %ecx, g_targetAddress");
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP