Source file src/cmd/link/testdata/pe-seh/main.go
1 // Copyright 2026 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 /* 8 #define WIN32_LEAN_AND_MEAN 9 #include <windows.h> 10 11 // Use assembly to ensure .pdata refers to a named symbol within .xdata, 12 // rather than the section symbol plus an addend. This exercises the 13 // linker's interior-symbol handling independently of the C compiler's unwind layout. 14 __asm__( 15 // Match the order of the manually emitted and compiler-generated .pdata. 16 ".text\n" 17 ".globl seh_test_function, seh_test_unwind\n" 18 "seh_test_function: nop; ret\n" 19 "seh_test_function_end:\n" 20 21 // Keep the handler in its own section, referenced only by unwind data. 22 ".section .text$h,\"xr\"\n" 23 "seh_test_handler: movl $1, %eax; ret\n" // ExceptionContinueSearch 24 25 ".section .xdata,\"dr\"\n" 26 ".long 0\n" // Give the interior symbol a nonzero offset. 27 "seh_test_unwind: .long 9\n" // Version 1, UNW_FLAG_EHANDLER, no unwind codes. 28 ".rva seh_test_handler\n" 29 30 ".section .pdata,\"dr\"\n" 31 ".rva seh_test_function, seh_test_function_end, seh_test_unwind\n" 32 ".text\n" 33 ); 34 35 extern void seh_test_function(void); 36 37 static const char *check(void) { 38 CONTEXT context = {0}; 39 DWORD64 stack[1] = {0}; 40 DWORD64 imageBase, frame; 41 PVOID handlerData; 42 context.ContextFlags = CONTEXT_FULL; 43 context.Rip = (DWORD64)seh_test_function; 44 context.Rsp = (DWORD64)stack; 45 PRUNTIME_FUNCTION function = RtlLookupFunctionEntry(context.Rip, &imageBase, NULL); 46 if (function == NULL) 47 return "missing runtime function entry"; 48 PEXCEPTION_ROUTINE handler = RtlVirtualUnwind(UNW_FLAG_EHANDLER, 49 imageBase, context.Rip, function, &context, &handlerData, &frame, NULL); 50 if (handler == NULL || (DWORD64)handler == imageBase) 51 return "exception handler was discarded"; 52 if (handler(NULL, NULL, &context, NULL) != ExceptionContinueSearch) 53 return "unexpected exception handler result"; 54 return NULL; 55 } 56 */ 57 import "C" 58 59 func main() { 60 if err := C.check(); err != nil { 61 panic(C.GoString(err)) 62 } 63 } 64