I am afraid they are not template code. That means if nx is set for data section, they can not be executed. thanks, ray ________________________________ From: Ard Biesheuvel Sent: Thursday, March 30, 2023 7:47:03 PM To: devel@edk2.groups.io ; Ni, Ray ; Rebecca Cran Cc: Andrew Fish ; Kinney, Michael D ; Liu, Zhiguang Subject: Re: [edk2-devel] Is Xcode5ExceptionHandlerAsm.nasm still needed? (cc Rebecca) On Thu, 30 Mar 2023 at 12:48, Ard Biesheuvel wrote: > > On Thu, 30 Mar 2023 at 12:16, Ni, Ray wrote: > > > > Andrew, > > > > In UefiCpuPkg\Library\CpuExceptionHandlerLib\X64\, there are two nasm files: ExceptionHandlerAsm.nasm and the other XCODE version. > > > > > > > > The major diff between the two is the second operand in “mov rax, ASM_PFX(CommonInterruptEntry)” is patched at runtime by code, instead of relying on linker/loader to fix it. > > > > Can I know more background why it’s needed for XCODE? > > > > > > > > Given Apple is switching away from X86 CPU, is the XCODE version still needed? > > > > > > > > + Mike because I found another commit by you for bug: 565 – Fix X64 XCODE5/NASM compatibility issue in UefiCpuPkg MpInitLib (tianocore.org). > > > > > > > Yes, we still need it, also for non-Xcode clang + lld > > The problem is that the little code templates use absolute addressing > to refer to the jump targets. This is necessary because these > templates are copied into the vector table, and so they are moved > independently from the code they refer to, and so relative addressing > is not an option here. > > One thing I haven't tried yet is to emit the template code into .data > instead of .text, which /should/ be fine given that the template code > is never executed directly, only the copied versions are executed. I had a quick go at this, and the change below appears to work: it moves the template code into .data, and changes the absolute references to relative ones in the code that executes from where it gets loaded. I'm not sure how to test this, though. --- a/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm +++ b/UefiCpuPkg/Library/CpuExceptionHandlerLib/X64/ExceptionHandlerAsm.nasm @@ -27,7 +27,6 @@ extern ASM_PFX(CommonExceptionHandler) SECTION .data DEFAULT REL -SECTION .text ALIGN 8 @@ -51,6 +50,9 @@ HookAfterStubHeaderBegin: push rax mov rax, HookAfterStubHeaderEnd jmp rax + +SECTION .text + HookAfterStubHeaderEnd: mov rax, rsp and sp, 0xfff0 ; make sure 16-byte aligned for exception context @@ -276,8 +278,7 @@ DrFinish: ; and make sure RSP is 16-byte aligned ; sub rsp, 4 * 8 + 8 - mov rax, ASM_PFX(CommonExceptionHandler) - call rax + call ASM_PFX(CommonExceptionHandler) add rsp, 4 * 8 + 8 cli @@ -384,10 +385,10 @@ DoIret: ; comments here for definition of address map global ASM_PFX(AsmGetTemplateAddressMap) ASM_PFX(AsmGetTemplateAddressMap): - mov rax, AsmIdtVectorBegin + lea rax, AsmIdtVectorBegin mov qword [rcx], rax mov qword [rcx + 0x8], (AsmIdtVectorEnd - AsmIdtVectorBegin) / 32 - mov rax, HookAfterStubHeaderBegin + lea rax, HookAfterStubHeaderBegin mov qword [rcx + 0x10], rax ret