Commit 093ac15a authored by Alex Brainman's avatar Alex Brainman

debug/pe: better error messages

Updates #15345

Change-Id: Iae35d3e378cbc8157ba1ff91e4971ed4515a5e5c
Reviewed-on: https://go-review.googlesource.com/22394Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 758431fe
...@@ -8,7 +8,6 @@ package pe ...@@ -8,7 +8,6 @@ package pe
import ( import (
"debug/dwarf" "debug/dwarf"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
...@@ -58,6 +57,8 @@ var ( ...@@ -58,6 +57,8 @@ var (
sizeofOptionalHeader64 = uint16(binary.Size(OptionalHeader64{})) sizeofOptionalHeader64 = uint16(binary.Size(OptionalHeader64{}))
) )
// TODO(brainman): add Load function, as a replacement for NewFile, that does not call removeAuxSymbols (for performance)
// NewFile creates a new File for accessing a PE binary in an underlying reader. // NewFile creates a new File for accessing a PE binary in an underlying reader.
func NewFile(r io.ReaderAt) (*File, error) { func NewFile(r io.ReaderAt) (*File, error) {
f := new(File) f := new(File)
...@@ -73,7 +74,7 @@ func NewFile(r io.ReaderAt) (*File, error) { ...@@ -73,7 +74,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
var sign [4]byte var sign [4]byte
r.ReadAt(sign[:], signoff) r.ReadAt(sign[:], signoff)
if !(sign[0] == 'P' && sign[1] == 'E' && sign[2] == 0 && sign[3] == 0) { if !(sign[0] == 'P' && sign[1] == 'E' && sign[2] == 0 && sign[3] == 0) {
return nil, errors.New("Invalid PE File Format.") return nil, fmt.Errorf("Invalid PE COFF file signature of %v.", sign)
} }
base = signoff + 4 base = signoff + 4
} else { } else {
...@@ -83,8 +84,10 @@ func NewFile(r io.ReaderAt) (*File, error) { ...@@ -83,8 +84,10 @@ func NewFile(r io.ReaderAt) (*File, error) {
if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil { if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil {
return nil, err return nil, err
} }
if f.FileHeader.Machine != IMAGE_FILE_MACHINE_UNKNOWN && f.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64 && f.FileHeader.Machine != IMAGE_FILE_MACHINE_I386 { switch f.FileHeader.Machine {
return nil, errors.New("Invalid PE File Format.") case IMAGE_FILE_MACHINE_UNKNOWN, IMAGE_FILE_MACHINE_AMD64, IMAGE_FILE_MACHINE_I386:
default:
return nil, fmt.Errorf("Unrecognised COFF file header machine value of 0x%x.", f.FileHeader.Machine)
} }
var err error var err error
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment