]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/link/internal/ld/seh.go
cmd/link: generate .pdata PE section
[gostls13.git] / src / cmd / link / internal / ld / seh.go
1 // Copyright 2023 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 ld
6
7 import (
8         "cmd/internal/sys"
9         "cmd/link/internal/loader"
10         "cmd/link/internal/sym"
11 )
12
13 var sehp struct {
14         pdata loader.Sym
15 }
16
17 func writeSEH(ctxt *Link) {
18         switch ctxt.Arch.Family {
19         case sys.AMD64:
20                 writeSEHAMD64(ctxt)
21         }
22 }
23
24 func writeSEHAMD64(ctxt *Link) {
25         ldr := ctxt.loader
26         mkSecSym := func(name string, kind sym.SymKind) *loader.SymbolBuilder {
27                 s := ldr.CreateSymForUpdate(name, 0)
28                 s.SetType(kind)
29                 s.SetAlign(4)
30                 return s
31         }
32         pdata := mkSecSym(".pdata", sym.SPDATASECT)
33         // TODO: the following 12 bytes represent a dummy unwind info,
34         // remove once unwind infos are encoded in the .xdata section.
35         pdata.AddUint64(ctxt.Arch, 0)
36         pdata.AddUint32(ctxt.Arch, 0)
37         for _, s := range ctxt.Textp {
38                 if fi := ldr.FuncInfo(s); !fi.Valid() || fi.TopFrame() {
39                         continue
40                 }
41                 uw := ldr.SEHUnwindSym(s)
42                 if uw == 0 {
43                         continue
44                 }
45
46                 // Reference:
47                 // https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#struct-runtime_function
48                 pdata.AddPEImageRelativeAddrPlus(ctxt.Arch, s, 0)
49                 pdata.AddPEImageRelativeAddrPlus(ctxt.Arch, s, ldr.SymSize(s))
50                 // TODO: reference the .xdata symbol.
51                 pdata.AddPEImageRelativeAddrPlus(ctxt.Arch, pdata.Sym(), 0)
52         }
53         sehp.pdata = pdata.Sym()
54 }