Skip to content

Commit 054323d

Browse files
committed
debug/pe: rework reading of aux symbols to fix endianity problems
This patch reworks CL 394534 to fix things so that reading auxiliary symbol info works properly in a cross-endian mode (running debug/pe-based tool on a big-endian system). The previous implementation read in all symbol records using the primary symbol format, then just used a pointer cast to convert to the auxiliary format, which doesn't play well if host and target have different endianness. Fixes #52079. Change-Id: I143d94d9313a265f11ca7befd254bdb150698834 Reviewed-on: https://go-review.googlesource.com/c/go/+/397485 Run-TryBot: Than McIntosh <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Alex Brainman <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent f495b36 commit 054323d

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/debug/pe/symbol.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ type COFFSymbol struct {
2323
NumberOfAuxSymbols uint8
2424
}
2525

26+
// readCOFFSymbols reads in the symbol table for a PE file, returning
27+
// a slice of COFFSymbol objects. The PE format includes both primary
28+
// symbols (whose fields are described by COFFSymbol above) and
29+
// auxiliary symbols; all symbols are 18 bytes in size. The auxiliary
30+
// symbols for a given primary symbol are placed following it in the
31+
// array, e.g.
32+
//
33+
// ...
34+
// k+0: regular sym k
35+
// k+1: 1st aux symbol for k
36+
// k+2: 2nd aux symbol for k
37+
// k+3: regular sym k+3
38+
// k+4: 1st aux symbol for k+3
39+
// k+5: regular sym k+5
40+
// k+6: regular sym k+6
41+
//
42+
// The PE format allows for several possible aux symbol formats. For
43+
// more info see:
44+
//
45+
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#auxiliary-symbol-records
46+
//
47+
// At the moment this package only provides APIs for looking at
48+
// aux symbols of format 5 (associated with section definition symbols).
2649
func readCOFFSymbols(fh *FileHeader, r io.ReadSeeker) ([]COFFSymbol, error) {
2750
if fh.PointerToSymbolTable == 0 {
2851
return nil, nil
@@ -35,9 +58,31 @@ func readCOFFSymbols(fh *FileHeader, r io.ReadSeeker) ([]COFFSymbol, error) {
3558
return nil, fmt.Errorf("fail to seek to symbol table: %v", err)
3659
}
3760
syms := make([]COFFSymbol, fh.NumberOfSymbols)
38-
err = binary.Read(r, binary.LittleEndian, syms)
39-
if err != nil {
40-
return nil, fmt.Errorf("fail to read symbol table: %v", err)
61+
naux := 0
62+
for k := range syms {
63+
if naux == 0 {
64+
// Read a primary symbol.
65+
err = binary.Read(r, binary.LittleEndian, &syms[k])
66+
if err != nil {
67+
return nil, fmt.Errorf("fail to read symbol table: %v", err)
68+
}
69+
// Record how many auxiliary symbols it has.
70+
naux = int(syms[k].NumberOfAuxSymbols)
71+
} else {
72+
// Read an aux symbol. At the moment we assume all
73+
// aux symbols are format 5 (obviously this doesn't always
74+
// hold; more cases will be needed below if more aux formats
75+
// are supported in the future).
76+
naux--
77+
aux := (*COFFSymbolAuxFormat5)(unsafe.Pointer(&syms[k]))
78+
err = binary.Read(r, binary.LittleEndian, aux)
79+
if err != nil {
80+
return nil, fmt.Errorf("fail to read symbol table: %v", err)
81+
}
82+
}
83+
}
84+
if naux != 0 {
85+
return nil, fmt.Errorf("fail to read symbol table: %d aux symbols unread", naux)
4186
}
4287
return syms, nil
4388
}

src/debug/pe/symbols_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package pe
66

77
import (
88
"fmt"
9-
"runtime"
109
"testing"
1110
)
1211

@@ -18,13 +17,6 @@ type testpoint struct {
1817
}
1918

2019
func TestReadCOFFSymbolAuxInfo(t *testing.T) {
21-
22-
switch runtime.GOARCH {
23-
case "mips", "mips64", "ppc64", "s390x":
24-
t.Skipf("Skipping on %s (big endian) until issue #52079 fixed",
25-
runtime.GOARCH)
26-
}
27-
2820
testpoints := map[int]testpoint{
2921
39: testpoint{
3022
name: ".rdata$.refptr.__native_startup_lock",

0 commit comments

Comments
 (0)