Debian Codegraph @

Contrib/System/Source/Call-amd64.S

raw
1 ;#
2 ;# This file is a part of NSIS.
3 ;#
4 ;# Copyright (C) 2014-2026 Anders Kjersem
5 ;#
6 ;# Licensed under the zlib/libpng license (the "License");
7 ;# you may not use this file except in compliance with the License.
8 ;#
9 ;# Licence details can be found in the file COPYING.
10 ;#
11 ;# This software is provided 'as-is', without any express or implied
12 ;# warranty.
13 ;#
14 ;#
15 ;# MASM:
16 ;# ml64.exe /c Call-amd64.S
17 ;#
18 ;# Notes:
19 ;# * MASM does not accept 0x* constants and GAS does not accept *h constants in Intel mode, must use decimal!
20 ;#
21
22 ; .if 0
23 ;# MASM
24
25 SECTION_DATA equ .data
26 SECTION_CODE equ .code
27
28 FUNC_DECL MACRO name
29 name PROC
30 ENDM
31 FUNC_END MACRO name
32 name ENDP
33 ENDM
34
35 ;# ~MASM
36 IF 0
37 ; .else
38 ;# GNU
39 .intel_syntax noprefix
40 .set __GNU__,1
41
42 #define IFDEF .ifdef
43 #define ELSE .else
44 #define ENDIF .endif
45
46 #define SECTION_DATA .data
47 #define SECTION_CODE .text
48
49 #define END .end
50 #define EXTERN .extern
51
52 .macro FUNC_DECL name
53 .global \name
54 .func \name
55 \name:
56 .endm
57 .macro FUNC_END name
58 .endfunc
59 .endm
60
61 ;# ~GNU
62 ENDIF
63
64
65 IFDEF SYSTEM_LOG_DEBUG
66 EXTERN __imp_IsDebuggerPresent : PROC
67 ENDIF
68
69 EXTERN GetProcOffset : PROC
70 EXTERN GetParamsOffset : PROC
71 EXTERN GetSizeOfProcParam : PROC
72 EXTERN GetValueOffsetParam : PROC
73 EXTERN SetCallProcResultValues : PROC
74
75
76 SECTION_CODE
77
78
79 FUNC_DECL CallProc2 ;# rcx=SystemProc* edx=ParamCount
80 mov [rsp+8], r12
81 mov [rsp+16], r13
82 mov [rsp+24], r14
83 ;#mov [rsp+32], r15
84
85 ;# The stack is unaligned on function entry. We have to calculate the required
86 ;# stack size for our parameters + maybe 8 padding bytes to end up 16 byte aligned.
87 IFDEF __GNU__
88 #define pSystemProc r14
89 ELSE
90 pSystemProc equ r14
91 ENDIF
92 mov pSystemProc, rcx ;# Save SystemProc*
93 ;# Not required since we zero-extend eax: xor rax, rax
94 mov r13d, edx ;# Save ParamCount
95 imul eax, edx, 8
96 and edx, 1
97 jnz noparamalignpadding
98 lea eax, [eax+8] ;# sizeof(params) + 8 will make us 16 byte aligned
99 noparamalignpadding:
100 cmp eax, 40 ;# The ABI guarantees shadow space for the 4 register parameters
101 ja computedstacksize
102 mov eax, 40 ;# Minimum (4*8) + 8 to align
103 computedstacksize:
104 mov r12d, eax ;# Save stack size (Zero-extended mov)
105 sub rsp, r12
106
107 IFDEF SYSTEM_LOG_DEBUG
108 ;# BUGBUG: Remove this
109 call qword ptr [__imp_IsDebuggerPresent]
110 test eax, eax
111 jz nodbgbrk
112 int 3
113 nodbgbrk:
114 ENDIF
115
116 ;# We are going to set all stack parameters including the first 4,
117 ;# it does not hurt to do that and it allows us to copy them to
118 ;# their registers without reading pSystemProc->Params[1..3] again
119 call GetSizeOfProcParam
120 mov r9, rax ;# Store sizeof(ProcParameter)
121 call GetValueOffsetParam
122 mov r8, rax ;# Store FIELD_OFFSET(ProcParameter,Value)
123 call GetParamsOffset
124 lea r10, [pSystemProc+rax] ;# Store pSystemProc+FIELD_OFFSET(SystemProc,Params)
125 mov ecx, r13d ;# Zero-extended mov
126 test rcx, rcx
127 jz callthefunc
128 setparameter:
129 mov rax, r9
130 mul rcx ;# rax = sizeof(ProcParameter) * paramidx (paramidx is 1 based because the return value is stored in Params[0])
131 add rax, r10 ;# rax += pSystemProc->Params
132 mov rax, qword ptr [rax+r8] ;# rax = pSystemProc->Params[paramidx].Value
133 dec rcx
134 mov [rsp+(8*rcx)], rax
135 inc rcx
136 loop setparameter
137 ;# The 4 parameter registers are all volatile so we might as well assign all of them:
138 ;# setparam4:
139 ;# cmp r13d, 4
140 ;# jb setparam3
141 mov r9, [rsp+(8*3)]
142 ;# setparam3:
143 ;# cmp r13d, 3
144 ;# jb setparam2
145 mov r8, [rsp+(8*2)]
146 ;# setparam2:
147 ;# cmp r13d, 2
148 ;# jb setparam1
149 mov rdx, [rsp+(8*1)]
150 ;# setparam1:
151 ;# cmp r13d, 1
152 ;# jb callthefunc
153 mov rcx, [rsp+(8*0)]
154
155 callthefunc:
156 call GetProcOffset
157 mov r10, qword ptr [pSystemProc+rax]
158 xor rax, rax ;# Fix bug #1535007
159 call r10
160 mov rcx, pSystemProc
161 mov rdx, rax ;# Return value
162 call SetCallProcResultValues ;# Store GetLastError() and return value
163 ;# mov rax, pSystemProc has been performed by SetCallProcResultValues
164
165 ;# Epilog:
166 ;# http://msdn.microsoft.com/en-us/library/tawsa7cb claims that only
167 ;# add/lea rsp and pop is valid in the epilog. Unwind might fail on our version?
168 add rsp, r12 ;# Restore stack
169 ;# Restore nonvolatile registers:
170 mov r12, [rsp+8]
171 mov r13, [rsp+16]
172 mov r14, [rsp+24]
173 ;#mov r15, [rsp+32]
174 ret
175 FUNC_END CallProc2
176
177
178 END