Download Yasm User Manual
Transcript
CHAPTER 16. WIN64: PE32+ (MICROSOFT WIN64) OBJECT FILES [endprolog] Signals the end of the prologue; must be in the first 255 bytes of the function. endproc_frame Used at the end of functions started with proc_frame. Example 16.1 shows how these primitives are used (this is based on an example provided in Microsoft Visual Studio 2005 documentation). Example 16.1 Win64 Unwind Primitives PROC_FRAME db push [pushreg sub [allocstack lea [setframe movdqa [savexmm128 mov [savereg mov [savereg [endprolog] sample 0x48 rbp rbp] rsp,0x40 0x40] rbp,[rsp+0x20] rbp,0x20] [rbp],xmm7 xmm7, 0x20] [rbp+0x18],rsi rsi,0x38] [rsp+0x10],rdi rdi, 0x10] ; ; ; ; ; ; ; ; ; ; ; ; ; emit a REX prefix to enable hot-patching save prospective frame pointer create unwind data for this rbp register push allocate stack space create unwind data for this stack allocation assign the frame pointer with a bias of 32 create unwind data for a frame register in rbp save a non-volatile XMM register create unwind data for an XMM register save save rsi create unwind data for a save of rsi save rdi create unwind data for a save of rdi ; We can change the stack pointer outside of the prologue because we ; have a frame pointer. If we didn’t have one this would be illegal. ; A frame pointer is needed because of this stack pointer modification. sub mov mov rsp,0x60 rax,0 rax,[rax] ; we are free to modify the stack pointer ; we can unwind this access violation movdqa mov mov xmm7,[rbp] rsi,[rbp+0x18] rdi,[rbp-0x10] ; restore the registers that weren’t saved ; with a push; this is not part of the ; official epilog rsp,[rbp+0x20] rbp ; This is the official epilog lea pop ret ENDPROC_FRAME 16.2.6 Yasm Macros for Formal Stack Operations From the descriptions of the YASM primitives given earlier it can be seen that there is a close relationship between each normal stack operation and the related primitive needed to generate its unwind data. In consequence it is sensible to provide a set of macros that perform both operations in a single macro call. Yasm provides the following macros that combine the two operations. proc_frame name Generates a function table entry in .pdata and unwind information in .xdata. alloc_stack n Allocates a stack area of n bytes. save_reg reg , loc Saves a non-volatile register reg at offset loc on the stack. push_reg reg Pushes a non-volatile register reg on the stack. rex_push_reg reg Pushes a non-volatile register reg on the stack using a 2 byte push instruction. save_xmm128 reg , loc Saves a non-volatile XMM register reg at offset loc on the stack. 96