.386
.MODEL flat,stdcall
OPTION casemap:none
include windows.inc
include user32.inc
include kernel32.inc
includelib user32.lib
includelib kernel32.lib
.data?
_tls_index dd ?
OPTION DOTNAME
;增加一个名为.tls的区段
.tls SEGMENT
_tls_start LABEL DWORD
dd 80H DUP ("slt.")
_tls_end LABEL DWORD
.tls ENDS
OPTION NODOTNAME
.data
__xl_a dd TlsCallBack0 ;TlsCallBack1 ;回调函数,可以多个...
__xl_z dd 0 ;null terminated list of pointers to callback procedures
MsgCaption db "Tls test",0
Msgtls db "in Tls",0
msghello db 'hello word!',0
;tls目录结构
TLS_DIRECTORY STRUCT
lpTlsDataStart LPDWORD ? ;copy block starting here
lpTlsDataEnd LPDWORD ? ;and ending here + block (size=ZeroFillSize) filled with 0 to
lpTlsIndex LPDWORD ? ;DS:[FS:[2CH]]+TlsIndex*4
lpTlsCallbacks LPDWORD ? ;pointer to 0 terminated array of pointers to callbacks
ZeroFillSize DWORD ? ;overall size=lpTlsDataEnd-lpTlsDataStart+ZeroFillSize
Characteristic DWORD ? ;reserved
TLS_DIRECTORY ENDS
PUBLIC _tls_used ;this name is required and must be PUBLIC!!!!
_tls_used TLS_DIRECTORY <_tls_start, _tls_end, _tls_index, __xl_a, 0, ?>
.code
start:
invoke MessageBox,NULL,addr msghello,addr MsgCaption,MB_OK
invoke ExitProcess,NULL
ret
OPTION DOTNAME
.code .tls ;;;把这段代码放到tls段中,当然你也可以不用这么做。。
OPTION NODOTNAME
;tls section
;Tls回调函数
TlsCallBack0 PROC hinstImg, fdwReason, lpvReserved
invoke MessageBox,NULL,addr Msgtls,addr MsgCaption,MB_OK
mov dword ptr[__xl_a],0
MOV EAX, TRUE
RET
TlsCallBack0 ENDP
end start
这是Immlep大侠,发出来的tls程序。建立了一个tls段和一个tls表。我在了解了一些tls原理之后发现。tls表是放在pe结构的IMAGE_OPTIONAL_HEADER.DataDirectory中。我搞不懂这段汇编是如何实现将tls表添入IMAGE_OPTIONAL_HEADER.DataDirectory的。
后来我把这段代码去掉PUBLIC _tls_used ,发现tls表在那个地址消失了。但据我所知道PUBLIC 指令是相同名字的段合并用的。
跟写入头文件有什么关系。我想象不出来。希望那位大侠能予以点拨。
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!