-
Notifications
You must be signed in to change notification settings - Fork 17.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/link: generate .pdata PE section
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 <[email protected]> Reviewed-by: Alex Brainman <[email protected]> Reviewed-by: Than McIntosh <[email protected]> Run-TryBot: Quim Muntal <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
- Loading branch information
Showing
16 changed files
with
237 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.