]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/link: generate .pdata PE section
authorqmuntal <quimmuntal@gmail.com>
Tue, 17 Jan 2023 07:15:33 +0000 (08:15 +0100)
committerQuim Muntal <quimmuntal@gmail.com>
Tue, 2 May 2023 07:42:50 +0000 (07:42 +0000)
This CL adds a .pdata section to the PE file generated by the Go linker.

The .pdata section is a standard section [1] that contains an array of
function table entries that are used for stack unwinding.
The table entries layout is taken from [2].

This CL just generates the table entries without any unwinding
information, which is enough to start doing some E2E tests
between the Go linker and the Win32 APIs.

The goal of the .pdata table is to allow Windows retrieve
unwind information for a function at a given PC. It does so by doing
a binary search on the table, looking for an entry that meets
BeginAddress >= PC < EndAddress.

Each table entry takes 12 bytes and only non-leaf functions with
frame pointer needs an entry on the .pdata table.
The result is that PE binaries will be ~0.7% bigger due to the unwind
information, a reasonable amount considering the benefits in
debuggability.

Updates #57302

[1] https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-pdata-section
[2] https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-runtime_function

Change-Id: If675d10c64452946dbab76709da20569651e3e9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/461738
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Than McIntosh <thanm@google.com>
Run-TryBot: Quim Muntal <quimmuntal@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
16 files changed:
src/cmd/link/internal/ld/asmb.go
src/cmd/link/internal/ld/data.go
src/cmd/link/internal/ld/lib.go
src/cmd/link/internal/ld/pe.go
src/cmd/link/internal/ld/seh.go [new file with mode: 0644]
src/cmd/link/internal/loader/loader.go
src/cmd/link/internal/sym/symkind.go
src/cmd/link/internal/sym/symkind_string.go
src/internal/syscall/windows/syscall_windows.go
src/internal/syscall/windows/zsyscall_windows.go
src/runtime/defs_windows_386.go
src/runtime/defs_windows_amd64.go
src/runtime/defs_windows_arm.go
src/runtime/defs_windows_arm64.go
src/runtime/export_windows_test.go
src/runtime/runtime-seh_windows_test.go [new file with mode: 0644]

index cd8927b0876d47440c96a9b4052baf93ca4ef427..fc088be51eee5b292fc2eabc0c728e4967841006 100644 (file)
@@ -60,6 +60,10 @@ func asmb(ctxt *Link) {
 
        writeParallel(&wg, dwarfblk, ctxt, Segdwarf.Fileoff, Segdwarf.Vaddr, Segdwarf.Filelen)
 
+       if Segpdata.Filelen > 0 {
+               writeParallel(&wg, pdatablk, ctxt, Segpdata.Fileoff, Segpdata.Vaddr, Segpdata.Filelen)
+       }
+
        wg.Wait()
 }
 
index d0efcdc05203a47cd7e53936bbd052d1dac4c1c6..849629ebe3c63e5a7bb33a0650419747224b3ba8 100644 (file)
@@ -1154,6 +1154,10 @@ func dwarfblk(ctxt *Link, out *OutBuf, addr int64, size int64) {
        writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, syms, addr, size, zeros[:])
 }
 
+func pdatablk(ctxt *Link, out *OutBuf, addr int64, size int64) {
+       writeBlocks(ctxt, out, ctxt.outSem, ctxt.loader, []loader.Sym{sehp.pdata}, addr, size, zeros[:])
+}
+
 var covCounterDataStartOff, covCounterDataLen uint64
 
 var zeros [512]byte
@@ -1649,6 +1653,8 @@ func (ctxt *Link) dodata(symGroupType []sym.SymKind) {
        // data/rodata (and related) symbols.
        state.allocateDataSections(ctxt)
 
+       state.allocateSEHSections(ctxt)
+
        // Create *sym.Section objects and assign symbols to sections for
        // DWARF symbols.
        state.allocateDwarfSections(ctxt)
@@ -1676,6 +1682,10 @@ func (ctxt *Link) dodata(symGroupType []sym.SymKind) {
                sect.Extnum = n
                n++
        }
+       for _, sect := range Segpdata.Sections {
+               sect.Extnum = n
+               n++
+       }
 }
 
 // allocateDataSectionForSym creates a new sym.Section into which a
@@ -2148,6 +2158,16 @@ func (state *dodataState) allocateDwarfSections(ctxt *Link) {
        }
 }
 
+// allocateSEHSections allocate a sym.Section object for SEH
+// symbols, and assigns symbols to sections.
+func (state *dodataState) allocateSEHSections(ctxt *Link) {
+       if sehp.pdata > 0 {
+               sect := state.allocateDataSectionForSym(&Segpdata, sehp.pdata, 04)
+               state.assignDsymsToSection(sect, []loader.Sym{sehp.pdata}, sym.SRODATA, aligndatsize)
+               state.checkdatsize(sym.SPDATASECT)
+       }
+}
+
 type symNameSize struct {
        name string
        sz   int64
@@ -2684,6 +2704,21 @@ func (ctxt *Link) address() []*sym.Segment {
        // simply because right now we know where the BSS starts.
        Segdata.Filelen = bss.Vaddr - Segdata.Vaddr
 
+       if len(Segpdata.Sections) > 0 {
+               va = uint64(Rnd(int64(va), int64(*FlagRound)))
+               order = append(order, &Segpdata)
+               Segpdata.Rwx = 04
+               Segpdata.Vaddr = va
+               // Segpdata.Sections is intended to contain just one section.
+               // Loop through the slice anyway for consistency.
+               for _, s := range Segpdata.Sections {
+                       va = uint64(Rnd(int64(va), int64(s.Align)))
+                       s.Vaddr = va
+                       va += s.Length
+               }
+               Segpdata.Length = va - Segpdata.Vaddr
+       }
+
        va = uint64(Rnd(int64(va), int64(*FlagRound)))
        order = append(order, &Segdwarf)
        Segdwarf.Rwx = 06
@@ -2735,6 +2770,10 @@ func (ctxt *Link) address() []*sym.Segment {
                }
        }
 
+       if sect := ldr.SymSect(sehp.pdata); sect != nil {
+               ldr.AddToSymValue(sehp.pdata, int64(sect.Vaddr))
+       }
+
        if ctxt.BuildMode == BuildModeShared {
                s := ldr.LookupOrCreateSym("go:link.abihashbytes", 0)
                sect := ldr.SymSect(ldr.LookupOrCreateSym(".note.go.abihash", 0))
index c88a955a0c5979487668bd737cf5155be4ac2832..f1eff33c6e78b79d7ca7ccca39ea1542eddc64af 100644 (file)
@@ -328,8 +328,9 @@ var (
        Segrelrodata sym.Segment
        Segdata      sym.Segment
        Segdwarf     sym.Segment
+       Segpdata     sym.Segment // windows-only
 
-       Segments = []*sym.Segment{&Segtext, &Segrodata, &Segrelrodata, &Segdata, &Segdwarf}
+       Segments = []*sym.Segment{&Segtext, &Segrodata, &Segrelrodata, &Segdata, &Segdwarf, &Segpdata}
 )
 
 const pkgdef = "__.PKGDEF"
index a3bb47d232e9a9aacd221543e1f6c29e394bf777..b07f2763eb6591dd63311fec159ad0405c26d723 100644 (file)
@@ -433,6 +433,7 @@ type peFile struct {
        dataSect       *peSection
        bssSect        *peSection
        ctorsSect      *peSection
+       pdataSect      *peSection
        nextSectOffset uint32
        nextFileOffset uint32
        symtabOffset   int64 // offset to the start of symbol table
@@ -498,6 +499,25 @@ func (f *peFile) addDWARF() {
        }
 }
 
+// addSEH adds SEH information to the COFF file f.
+func (f *peFile) addSEH(ctxt *Link) {
+       if Segpdata.Length == 0 {
+               return
+       }
+       d := pefile.addSection(".pdata", int(Segpdata.Length), int(Segpdata.Length))
+       d.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
+       if ctxt.LinkMode == LinkExternal {
+               // Some gcc versions don't honor the default alignment for the .pdata section.
+               d.characteristics |= IMAGE_SCN_ALIGN_4BYTES
+       }
+       pefile.pdataSect = d
+       d.checkSegment(&Segpdata)
+       // TODO: remove extraSize once the dummy unwind info is removed from the .pdata section.
+       const extraSize = 12
+       pefile.dataDirectory[pe.IMAGE_DIRECTORY_ENTRY_EXCEPTION].VirtualAddress = d.virtualAddress + extraSize
+       pefile.dataDirectory[pe.IMAGE_DIRECTORY_ENTRY_EXCEPTION].Size = d.virtualSize - extraSize
+}
+
 // addInitArray adds .ctors COFF section to the file f.
 func (f *peFile) addInitArray(ctxt *Link) *peSection {
        // The size below was determined by the specification for array relocations,
@@ -593,15 +613,19 @@ func (f *peFile) emitRelocations(ctxt *Link) {
                return int(sect.Rellen / relocLen)
        }
 
-       sects := []struct {
+       type relsect struct {
                peSect *peSection
                seg    *sym.Segment
                syms   []loader.Sym
-       }{
+       }
+       sects := []relsect{
                {f.textSect, &Segtext, ctxt.Textp},
                {f.rdataSect, &Segrodata, ctxt.datap},
                {f.dataSect, &Segdata, ctxt.datap},
        }
+       if sehp.pdata != 0 {
+               sects = append(sects, relsect{f.pdataSect, &Segpdata, []loader.Sym{sehp.pdata}})
+       }
        for _, s := range sects {
                s.peSect.emitRelocations(ctxt.Out, func() int {
                        var n int
@@ -1595,6 +1619,7 @@ func addPEBaseReloc(ctxt *Link) {
 func (ctxt *Link) dope() {
        initdynimport(ctxt)
        initdynexport(ctxt)
+       writeSEH(ctxt)
 }
 
 func setpersrc(ctxt *Link, syms []loader.Sym) {
@@ -1689,6 +1714,7 @@ func asmbPe(ctxt *Link) {
                pefile.bssSect = b
        }
 
+       pefile.addSEH(ctxt)
        pefile.addDWARF()
 
        if ctxt.LinkMode == LinkExternal {
diff --git a/src/cmd/link/internal/ld/seh.go b/src/cmd/link/internal/ld/seh.go
new file mode 100644 (file)
index 0000000..b950847
--- /dev/null
@@ -0,0 +1,54 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package ld
+
+import (
+       "cmd/internal/sys"
+       "cmd/link/internal/loader"
+       "cmd/link/internal/sym"
+)
+
+var sehp struct {
+       pdata loader.Sym
+}
+
+func writeSEH(ctxt *Link) {
+       switch ctxt.Arch.Family {
+       case sys.AMD64:
+               writeSEHAMD64(ctxt)
+       }
+}
+
+func writeSEHAMD64(ctxt *Link) {
+       ldr := ctxt.loader
+       mkSecSym := func(name string, kind sym.SymKind) *loader.SymbolBuilder {
+               s := ldr.CreateSymForUpdate(name, 0)
+               s.SetType(kind)
+               s.SetAlign(4)
+               return s
+       }
+       pdata := mkSecSym(".pdata", sym.SPDATASECT)
+       // TODO: the following 12 bytes represent a dummy unwind info,
+       // remove once unwind infos are encoded in the .xdata section.
+       pdata.AddUint64(ctxt.Arch, 0)
+       pdata.AddUint32(ctxt.Arch, 0)
+       for _, s := range ctxt.Textp {
+               if fi := ldr.FuncInfo(s); !fi.Valid() || fi.TopFrame() {
+                       continue
+               }
+               uw := ldr.SEHUnwindSym(s)
+               if uw == 0 {
+                       continue
+               }
+
+               // Reference:
+               // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-runtime_function
+               pdata.AddPEImageRelativeAddrPlus(ctxt.Arch, s, 0)
+               pdata.AddPEImageRelativeAddrPlus(ctxt.Arch, s, ldr.SymSize(s))
+               // TODO: reference the .xdata symbol.
+               pdata.AddPEImageRelativeAddrPlus(ctxt.Arch, pdata.Sym(), 0)
+       }
+       sehp.pdata = pdata.Sym()
+}
index a989d143625c7ba1e6762707e8a9239e72452758..5fcbf160e0f94ded8ee83074c45ac8d8b3508095 100644 (file)
@@ -1646,6 +1646,16 @@ func (l *Loader) WasmImportSym(fnSymIdx Sym) (Sym, bool) {
        return 0, false
 }
 
+// SEHUnwindSym returns the auxiliary SEH unwind symbol associated with
+// a given function symbol.
+func (l *Loader) SEHUnwindSym(fnSymIdx Sym) Sym {
+       if l.SymType(fnSymIdx) != sym.STEXT {
+               log.Fatalf("error: non-function sym %d/%s t=%s passed to SEHUnwindSym", fnSymIdx, l.SymName(fnSymIdx), l.SymType(fnSymIdx).String())
+       }
+
+       return l.aux1(fnSymIdx, goobj.AuxSehUnwindInfo)
+}
+
 // GetFuncDwarfAuxSyms collects and returns the auxiliary DWARF
 // symbols associated with a given function symbol.  Prior to the
 // introduction of the loader, this was done purely using name
index db87212a174ce3ec324749d2ecb33ef37e6b25a0..acb96ad0ad224afd5ce392ca29611beb71fb5141 100644 (file)
@@ -126,6 +126,7 @@ const (
 
        // SEH symbol types
        SSEHUNWINDINFO
+       SPDATASECT
 )
 
 // AbiSymKindToSymKind maps values read from object files (which are
index 09508ce76602c510b68067396b3dbfc574caef2a..30de0a812f5a8527cbe2c98e8b72243a2ee31d9e 100644 (file)
@@ -68,11 +68,12 @@ func _() {
        _ = x[SDWARFLOC-57]
        _ = x[SDWARFLINES-58]
        _ = x[SSEHUNWINDINFO-59]
+       _ = x[SPDATASECT-60]
 }
 
-const _SymKind_name = "SxxxSTEXTSELFRXSECTSMACHOPLTSTYPESSTRINGSGOSTRINGSGOFUNCSGCBITSSRODATASFUNCTABSELFROSECTSTYPERELROSSTRINGRELROSGOSTRINGRELROSGOFUNCRELROSGCBITSRELROSRODATARELROSFUNCTABRELROSTYPELINKSITABLINKSSYMTABSPCLNTABSFirstWritableSBUILDINFOSELFSECTSMACHOSMACHOGOTSWINDOWSSELFGOTSNOPTRDATASINITARRSDATASXCOFFTOCSBSSSNOPTRBSSSLIBFUZZER_8BIT_COUNTERSCOVERAGE_COUNTERSCOVERAGE_AUXVARSTLSBSSSXREFSMACHOSYMSTRSMACHOSYMTABSMACHOINDIRECTPLTSMACHOINDIRECTGOTSFILEPATHSDYNIMPORTSHOSTOBJSUNDEFEXTSDWARFSECTSDWARFCUINFOSDWARFCONSTSDWARFFCNSDWARFABSFCNSDWARFTYPESDWARFVARSDWARFRANGESDWARFLOCSDWARFLINESSSEHUNWINDINFO"
+const _SymKind_name = "SxxxSTEXTSELFRXSECTSMACHOPLTSTYPESSTRINGSGOSTRINGSGOFUNCSGCBITSSRODATASFUNCTABSELFROSECTSTYPERELROSSTRINGRELROSGOSTRINGRELROSGOFUNCRELROSGCBITSRELROSRODATARELROSFUNCTABRELROSTYPELINKSITABLINKSSYMTABSPCLNTABSFirstWritableSBUILDINFOSELFSECTSMACHOSMACHOGOTSWINDOWSSELFGOTSNOPTRDATASINITARRSDATASXCOFFTOCSBSSSNOPTRBSSSLIBFUZZER_8BIT_COUNTERSCOVERAGE_COUNTERSCOVERAGE_AUXVARSTLSBSSSXREFSMACHOSYMSTRSMACHOSYMTABSMACHOINDIRECTPLTSMACHOINDIRECTGOTSFILEPATHSDYNIMPORTSHOSTOBJSUNDEFEXTSDWARFSECTSDWARFCUINFOSDWARFCONSTSDWARFFCNSDWARFABSFCNSDWARFTYPESDWARFVARSDWARFRANGESDWARFLOCSDWARFLINESSSEHUNWINDINFOSPDATASECT"
 
-var _SymKind_index = [...]uint16{0, 4, 9, 19, 28, 33, 40, 49, 56, 63, 70, 78, 88, 98, 110, 124, 136, 148, 160, 173, 182, 191, 198, 206, 220, 230, 238, 244, 253, 261, 268, 278, 286, 291, 300, 304, 313, 336, 353, 369, 376, 381, 393, 405, 422, 439, 448, 458, 466, 475, 485, 497, 508, 517, 529, 539, 548, 559, 568, 579, 593}
+var _SymKind_index = [...]uint16{0, 4, 9, 19, 28, 33, 40, 49, 56, 63, 70, 78, 88, 98, 110, 124, 136, 148, 160, 173, 182, 191, 198, 206, 220, 230, 238, 244, 253, 261, 268, 278, 286, 291, 300, 304, 313, 336, 353, 369, 376, 381, 393, 405, 422, 439, 448, 458, 466, 475, 485, 497, 508, 517, 529, 539, 548, 559, 568, 579, 593, 603}
 
 func (i SymKind) String() string {
        if i >= SymKind(len(_SymKind_index)-1) {
index 4ae9e4f1b2a9efb19ba583aec7cd15897c87d303..409b334bcbcad19365d50a45c3a4138f085e5c66 100644 (file)
@@ -399,3 +399,5 @@ type FILE_ID_BOTH_DIR_INFO struct {
 }
 
 //sys  GetVolumeInformationByHandle(file syscall.Handle, volumeNameBuffer *uint16, volumeNameSize uint32, volumeNameSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemNameBuffer *uint16, fileSystemNameSize uint32) (err error) = GetVolumeInformationByHandleW
+
+//sys  RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table *byte) (ret uintptr) = kernel32.RtlLookupFunctionEntry
index 3a7423a3049ebe99209586ecd030b98fe184e390..4a6ca406d170d25ce1ab08083803a0307452cd5a 100644 (file)
@@ -69,6 +69,7 @@ var (
        procModule32NextW                 = modkernel32.NewProc("Module32NextW")
        procMoveFileExW                   = modkernel32.NewProc("MoveFileExW")
        procMultiByteToWideChar           = modkernel32.NewProc("MultiByteToWideChar")
+       procRtlLookupFunctionEntry        = modkernel32.NewProc("RtlLookupFunctionEntry")
        procSetFileInformationByHandle    = modkernel32.NewProc("SetFileInformationByHandle")
        procUnlockFileEx                  = modkernel32.NewProc("UnlockFileEx")
        procVirtualQuery                  = modkernel32.NewProc("VirtualQuery")
@@ -289,6 +290,12 @@ func MultiByteToWideChar(codePage uint32, dwFlags uint32, str *byte, nstr int32,
        return
 }
 
+func RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table *byte) (ret uintptr) {
+       r0, _, _ := syscall.Syscall(procRtlLookupFunctionEntry.Addr(), 3, uintptr(pc), uintptr(unsafe.Pointer(baseAddress)), uintptr(unsafe.Pointer(table)))
+       ret = uintptr(r0)
+       return
+}
+
 func SetFileInformationByHandle(handle syscall.Handle, fileInformationClass uint32, buf uintptr, bufsize uint32) (err error) {
        r1, _, e1 := syscall.Syscall6(procSetFileInformationByHandle.Addr(), 4, uintptr(handle), uintptr(fileInformationClass), uintptr(buf), uintptr(bufsize), 0, 0)
        if r1 == 0 {
index 8d6c443a14f3fef8940be0c580f53d6c59200b26..b11b15554e56a5afa112957eea856b6d37e71a9e 100644 (file)
@@ -56,6 +56,9 @@ func (c *context) set_lr(x uintptr) {}
 func (c *context) set_ip(x uintptr) { c.eip = uint32(x) }
 func (c *context) set_sp(x uintptr) { c.esp = uint32(x) }
 
+// 386 does not have frame pointer register.
+func (c *context) set_fp(x uintptr) {}
+
 func prepareContextForSigResume(c *context) {
        c.edx = c.esp
        c.ecx = c.eip
index afa8a657b886627f765c8b4416eec9f01fb6828d..20c9c4d9325bfbf782aea933ef65fbc3ce47c02f 100644 (file)
@@ -69,6 +69,7 @@ func (c *context) set_lr(x uintptr) {}
 
 func (c *context) set_ip(x uintptr) { c.rip = uint64(x) }
 func (c *context) set_sp(x uintptr) { c.rsp = uint64(x) }
+func (c *context) set_fp(x uintptr) { c.rbp = uint64(x) }
 
 func prepareContextForSigResume(c *context) {
        c.r8 = c.rsp
index 21c7991519b0ed31a0c3115672c6021a36e8add0..7a18c95cf111377dc8b55a50a38c9806b3edea0f 100644 (file)
@@ -58,6 +58,9 @@ func (c *context) set_ip(x uintptr) { c.pc = uint32(x) }
 func (c *context) set_sp(x uintptr) { c.spr = uint32(x) }
 func (c *context) set_lr(x uintptr) { c.lrr = uint32(x) }
 
+// arm does not have frame pointer register.
+func (c *context) set_fp(x uintptr) {}
+
 func prepareContextForSigResume(c *context) {
        c.r0 = c.spr
        c.r1 = c.pc
index 6c71133b43e8255ea1fe7dc778cf63557157f873..ef2efb1bb3331bc4977feb07d45a23367b666392 100644 (file)
@@ -40,6 +40,7 @@ func (c *context) lr() uintptr { return uintptr(c.x[30]) }
 func (c *context) set_ip(x uintptr) { c.pc = uint64(x) }
 func (c *context) set_sp(x uintptr) { c.xsp = uint64(x) }
 func (c *context) set_lr(x uintptr) { c.x[30] = uint64(x) }
+func (c *context) set_fp(x uintptr) { c.x[29] = uint64(x) }
 
 func prepareContextForSigResume(c *context) {
        c.x[0] = c.xsp
index 332136b586fcdfd9c853591d40674e16b221852f..5b9f08fb798c3a1f6e5f65c8d091761e41185019 100644 (file)
@@ -20,3 +20,19 @@ func NumberOfProcessors() int32 {
        stdcall1(_GetSystemInfo, uintptr(unsafe.Pointer(&info)))
        return int32(info.dwnumberofprocessors)
 }
+
+type ContextStub struct {
+       context
+}
+
+func (c ContextStub) GetPC() uintptr {
+       return c.ip()
+}
+
+func NewContextStub() ContextStub {
+       var ctx context
+       ctx.set_ip(getcallerpc())
+       ctx.set_sp(getcallersp())
+       ctx.set_fp(getcallerfp())
+       return ContextStub{ctx}
+}
diff --git a/src/runtime/runtime-seh_windows_test.go b/src/runtime/runtime-seh_windows_test.go
new file mode 100644 (file)
index 0000000..23f5b87
--- /dev/null
@@ -0,0 +1,63 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+       "internal/abi"
+       "internal/syscall/windows"
+       "runtime"
+       "testing"
+)
+
+func sehf1() int {
+       return sehf1()
+}
+
+func sehf2() {}
+
+func TestSehLookupFunctionEntry(t *testing.T) {
+       if runtime.GOARCH != "amd64" {
+               t.Skip("skipping amd64-only test")
+       }
+       // This test checks that Win32 is able to retrieve
+       // function metadata stored in the .pdata section
+       // by the Go linker.
+       // Win32 unwinding will fail if this test fails,
+       // as RtlUnwindEx uses RtlLookupFunctionEntry internally.
+       // If that's the case, don't bother investigating further,
+       // first fix the .pdata generation.
+       sehf1pc := abi.FuncPCABIInternal(sehf1)
+       var fnwithframe func()
+       fnwithframe = func() {
+               fnwithframe()
+       }
+       fnwithoutframe := func() {}
+       tests := []struct {
+               name     string
+               pc       uintptr
+               hasframe bool
+       }{
+               {"no frame func", abi.FuncPCABIInternal(sehf2), false},
+               {"no func", sehf1pc - 1, false},
+               {"func at entry", sehf1pc, true},
+               {"func in prologue", sehf1pc + 1, true},
+               {"anonymous func with frame", abi.FuncPCABIInternal(fnwithframe), true},
+               {"anonymous func without frame", abi.FuncPCABIInternal(fnwithoutframe), false},
+               {"pc at func body", runtime.NewContextStub().GetPC(), true},
+       }
+       for _, tt := range tests {
+               var base uintptr
+               fn := windows.RtlLookupFunctionEntry(tt.pc, &base, nil)
+               if !tt.hasframe {
+                       if fn != 0 {
+                               t.Errorf("%s: unexpected frame", tt.name)
+                       }
+                       continue
+               }
+               if fn == 0 {
+                       t.Errorf("%s: missing frame", tt.name)
+               }
+       }
+}