Skip to content

Commit 43cfd78

Browse files
cmd/link, runtime, debug/gosym: move pclntab magic to internal/abi
Change-Id: I2d3c41b0e61b994d7b04bd16a791fd226dc45269 Reviewed-on: https://go-review.googlesource.com/c/go/+/720302 Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 312b203 commit 43cfd78

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

src/cmd/link/internal/ld/pcln.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ func (state *pclntab) generatePCHeader(ctxt *Link) {
261261

262262
// Write header.
263263
// Keep in sync with runtime/symtab.go:pcHeader and package debug/gosym.
264-
header.SetUint32(ctxt.Arch, 0, 0xfffffff1)
264+
header.SetUint32(ctxt.Arch, 0, uint32(abi.CurrentPCLnTabMagic))
265265
header.SetUint8(ctxt.Arch, 6, uint8(ctxt.Arch.MinLC))
266266
header.SetUint8(ctxt.Arch, 7, uint8(ctxt.Arch.PtrSize))
267267
off := header.SetUint(ctxt.Arch, 8, uint64(state.nfunc))

src/debug/gosym/pclntab.go

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package gosym
1111
import (
1212
"bytes"
1313
"encoding/binary"
14+
"internal/abi"
1415
"sort"
1516
"sync"
1617
)
@@ -174,13 +175,6 @@ func (t *LineTable) isGo12() bool {
174175
return t.version >= ver12
175176
}
176177

177-
const (
178-
go12magic = 0xfffffffb
179-
go116magic = 0xfffffffa
180-
go118magic = 0xfffffff0
181-
go120magic = 0xfffffff1
182-
)
183-
184178
// uintptr returns the pointer-sized value encoded at b.
185179
// The pointer size is dictated by the table being read.
186180
func (t *LineTable) uintptr(b []byte) uint64 {
@@ -220,24 +214,29 @@ func (t *LineTable) parsePclnTab() {
220214
}
221215

222216
var possibleVersion version
223-
leMagic := binary.LittleEndian.Uint32(t.Data)
224-
beMagic := binary.BigEndian.Uint32(t.Data)
217+
218+
// The magic numbers are chosen such that reading the value with
219+
// a different endianness does not result in the same value.
220+
// That lets us the magic number to determine the endianness.
221+
leMagic := abi.PCLnTabMagic(binary.LittleEndian.Uint32(t.Data))
222+
beMagic := abi.PCLnTabMagic(binary.BigEndian.Uint32(t.Data))
223+
225224
switch {
226-
case leMagic == go12magic:
225+
case leMagic == abi.Go12PCLnTabMagic:
227226
t.binary, possibleVersion = binary.LittleEndian, ver12
228-
case beMagic == go12magic:
227+
case beMagic == abi.Go12PCLnTabMagic:
229228
t.binary, possibleVersion = binary.BigEndian, ver12
230-
case leMagic == go116magic:
229+
case leMagic == abi.Go116PCLnTabMagic:
231230
t.binary, possibleVersion = binary.LittleEndian, ver116
232-
case beMagic == go116magic:
231+
case beMagic == abi.Go116PCLnTabMagic:
233232
t.binary, possibleVersion = binary.BigEndian, ver116
234-
case leMagic == go118magic:
233+
case leMagic == abi.Go118PCLnTabMagic:
235234
t.binary, possibleVersion = binary.LittleEndian, ver118
236-
case beMagic == go118magic:
235+
case beMagic == abi.Go118PCLnTabMagic:
237236
t.binary, possibleVersion = binary.BigEndian, ver118
238-
case leMagic == go120magic:
237+
case leMagic == abi.Go120PCLnTabMagic:
239238
t.binary, possibleVersion = binary.LittleEndian, ver120
240-
case beMagic == go120magic:
239+
case beMagic == abi.Go120PCLnTabMagic:
241240
t.binary, possibleVersion = binary.BigEndian, ver120
242241
default:
243242
return

src/internal/abi/symtab.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,36 @@
44

55
package abi
66

7+
// PCLnTabMagic is the version at the start of the PC/line table.
8+
// This is the start of the .pclntab section, and is also runtime.pcHeader.
9+
// The magic numbers are chosen such that reading the value with
10+
// a different endianness does not result in the same value.
11+
// That lets us the magic number to determine the endianness.
12+
type PCLnTabMagic uint32
13+
14+
const (
15+
// Initial PCLnTabMagic value used in Go 1.2 through Go 1.15.
16+
Go12PCLnTabMagic PCLnTabMagic = 0xfffffffb
17+
// PCLnTabMagic value used in Go 1.16 through Go 1.17.
18+
// Several fields added to header (CL 241598).
19+
Go116PCLnTabMagic PCLnTabMagic = 0xfffffffa
20+
// PCLnTabMagic value used in Go 1.18 through Go 1.19.
21+
// Entry PC of func data changed from address to offset (CL 351463).
22+
Go118PCLnTabMagic PCLnTabMagic = 0xfffffff0
23+
// PCLnTabMagic value used in Go 1.20 and later.
24+
// A ":" was added to generated symbol names (#37762).
25+
Go120PCLnTabMagic PCLnTabMagic = 0xfffffff1
26+
27+
// CurrentPCLnTabMagic is the value emitted by the current toolchain.
28+
// This is written by the linker to the pcHeader and read by the
29+
// runtime and debug/gosym (and external tools like Delve).
30+
//
31+
// Change this value when updating the pclntab version.
32+
// Changing this exported value is OK because is an
33+
// internal package.
34+
CurrentPCLnTabMagic = Go120PCLnTabMagic
35+
)
36+
737
// A FuncFlag records bits about a function, passed to the runtime.
838
type FuncFlag uint8
939

src/runtime/symtab.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,12 @@ func (f *_func) funcInfo() funcInfo {
374374

375375
// pcHeader holds data used by the pclntab lookups.
376376
type pcHeader struct {
377-
magic uint32 // 0xFFFFFFF1
378-
pad1, pad2 uint8 // 0,0
379-
minLC uint8 // min instruction size
380-
ptrSize uint8 // size of a ptr in bytes
381-
nfunc int // number of functions in the module
382-
nfiles uint // number of entries in the file tab
377+
magic abi.PCLnTabMagic // abi.Go1NNPcLnTabMagic
378+
pad1, pad2 uint8 // 0,0
379+
minLC uint8 // min instruction size
380+
ptrSize uint8 // size of a ptr in bytes
381+
nfunc int // number of functions in the module
382+
nfiles uint // number of entries in the file tab
383383

384384
// The next field used to be textStart. This is no longer stored
385385
// as it requires a relocation. Code should use the moduledata text
@@ -623,7 +623,7 @@ const debugPcln = false
623623
func moduledataverify1(datap *moduledata) {
624624
// Check that the pclntab's format is valid.
625625
hdr := datap.pcHeader
626-
if hdr.magic != 0xfffffff1 || hdr.pad1 != 0 || hdr.pad2 != 0 ||
626+
if hdr.magic != abi.CurrentPCLnTabMagic || hdr.pad1 != 0 || hdr.pad2 != 0 ||
627627
hdr.minLC != sys.PCQuantum || hdr.ptrSize != goarch.PtrSize {
628628
println("runtime: pcHeader: magic=", hex(hdr.magic), "pad1=", hdr.pad1, "pad2=", hdr.pad2,
629629
"minLC=", hdr.minLC, "ptrSize=", hdr.ptrSize, "pluginpath=", datap.pluginpath)

0 commit comments

Comments
 (0)