Skip to content

Commit 093ac15

Browse files
committed
debug/pe: better error messages
Updates #15345 Change-Id: Iae35d3e378cbc8157ba1ff91e4971ed4515a5e5c Reviewed-on: https://go-review.googlesource.com/22394 Reviewed-by: David Crawshaw <[email protected]> Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 758431f commit 093ac15

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/debug/pe/file.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package pe
88
import (
99
"debug/dwarf"
1010
"encoding/binary"
11-
"errors"
1211
"fmt"
1312
"io"
1413
"os"
@@ -58,6 +57,8 @@ var (
5857
sizeofOptionalHeader64 = uint16(binary.Size(OptionalHeader64{}))
5958
)
6059

60+
// TODO(brainman): add Load function, as a replacement for NewFile, that does not call removeAuxSymbols (for performance)
61+
6162
// NewFile creates a new File for accessing a PE binary in an underlying reader.
6263
func NewFile(r io.ReaderAt) (*File, error) {
6364
f := new(File)
@@ -73,7 +74,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
7374
var sign [4]byte
7475
r.ReadAt(sign[:], signoff)
7576
if !(sign[0] == 'P' && sign[1] == 'E' && sign[2] == 0 && sign[3] == 0) {
76-
return nil, errors.New("Invalid PE File Format.")
77+
return nil, fmt.Errorf("Invalid PE COFF file signature of %v.", sign)
7778
}
7879
base = signoff + 4
7980
} else {
@@ -83,8 +84,10 @@ func NewFile(r io.ReaderAt) (*File, error) {
8384
if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil {
8485
return nil, err
8586
}
86-
if f.FileHeader.Machine != IMAGE_FILE_MACHINE_UNKNOWN && f.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64 && f.FileHeader.Machine != IMAGE_FILE_MACHINE_I386 {
87-
return nil, errors.New("Invalid PE File Format.")
87+
switch f.FileHeader.Machine {
88+
case IMAGE_FILE_MACHINE_UNKNOWN, IMAGE_FILE_MACHINE_AMD64, IMAGE_FILE_MACHINE_I386:
89+
default:
90+
return nil, fmt.Errorf("Unrecognised COFF file header machine value of 0x%x.", f.FileHeader.Machine)
8891
}
8992

9093
var err error

0 commit comments

Comments
 (0)