当前位置: 代码迷 >> 汇编语言 >> windows TLS 反汇编解决办法
  详细解决方案

windows TLS 反汇编解决办法

热度:424   发布时间:2016-05-02 04:56:36.0
windows TLS 反汇编
    最近看了一段代码关于TLS的:
     __declspec(thread) int t = 1;
    int main()
    {
        t = 2;
        return 0;
    }
    其反汇编代码如下:
     _main:
          push    ebp
          mov    ebp,esp
          mov    eax,dword ptr [__tls_index]
          mov    ecx,dword ptr fs:[__tls_array]
          mov    edx,dword ptr [ecx+eax*4]
          mov    dword ptr _t[edx], 2
       
          xor     eax,eax
          pop    ebp
          ret
     (出处:程序员的自我修养)
    谁能给我解释一下这段汇编代码吗?结合TLS实现更好

------解决方案--------------------

_main:
  push ebp
  mov ebp,esp
  mov eax,dword ptr [__tls_index]     //取得线程中tls变量的索引,这里是t的索引
  mov ecx,dword ptr fs:[__tls_array]  //取出线程中tls变量数组的地址,每个线程的该地址
  mov edx,dword ptr [ecx+eax*4]       //均放在fs:[xxx]指定的位置
  mov dword ptr _t[edx], 2            //设置其值为2
    
  xor eax,eax
  pop ebp
  ret

------解决方案--------------------
以下是我截取的windows 7 x64 teb结构:

 dt nt!_TEB
   +0x000 NtTib            : _NT_TIB
   +0x038 EnvironmentPointer : Ptr64 Void
   +0x040 ClientId         : _CLIENT_ID
   +0x050 ActiveRpcHandle  : Ptr64 Void
   +0x058 ThreadLocalStoragePointer : Ptr64 Void
   +0x060 ProcessEnvironmentBlock : Ptr64 _PEB
   +0x068 LastErrorValue   : Uint4B
   +0x06c CountOfOwnedCriticalSections : Uint4B
   +0x070 CsrClientThread  : Ptr64 Void
   +0x078 Win32ThreadInfo  : Ptr64 Void
   +0x080 User32Reserved   : [26] Uint4B
   +0x0e8 UserReserved     : [5] Uint4B
   +0x100 WOW32Reserved    : Ptr64 Void
   +0x108 CurrentLocale    : Uint4B
   +0x10c FpSoftwareStatusRegister : Uint4B
   +0x110 SystemReserved1  : [54] Ptr64 Void
  相关解决方案