From 921b2eba52906fc8b9bc4a8744dab63678f5ed3a Mon Sep 17 00:00:00 2001 From: Michael Munday Date: Mon, 21 Mar 2016 13:30:50 -0400 Subject: debug/gosym: accept PC quantum of 2 (for s390x) Needed for the header check to accept the header generated for s390x as Go 1.2 style rather than Go 1.1 style. Change-Id: I7b3713d4cc7514cfc58f947a45702348f6d7b824 Reviewed-on: https://go-review.googlesource.com/20966 Reviewed-by: Minux Ma --- src/debug/gosym/pclntab.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/debug') diff --git a/src/debug/gosym/pclntab.go b/src/debug/gosym/pclntab.go index 01a9f11f05..291f102262 100644 --- a/src/debug/gosym/pclntab.go +++ b/src/debug/gosym/pclntab.go @@ -167,7 +167,7 @@ func (t *LineTable) go12Init() { // Check header: 4-byte magic, two zeros, pc quantum, pointer size. t.go12 = -1 // not Go 1.2 until proven otherwise if len(t.Data) < 16 || t.Data[4] != 0 || t.Data[5] != 0 || - (t.Data[6] != 1 && t.Data[6] != 4) || // pc quantum + (t.Data[6] != 1 && t.Data[6] != 2 && t.Data[6] != 4) || // pc quantum (t.Data[7] != 4 && t.Data[7] != 8) { // pointer size return } -- cgit v1.3 From 381e5eee39edfb3a43e294023957aff05e9ff349 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 13 Apr 2016 04:35:37 +0000 Subject: all: use new io.SeekFoo constants instead of os.SEEK_FOO Automated change. Fixes #15269 Change-Id: I8deb2ac0101d3f7c390467ceb0a1561b72edbb2f Reviewed-on: https://go-review.googlesource.com/21962 Run-TryBot: Brad Fitzpatrick Reviewed-by: Andrew Gerrand TryBot-Result: Gobot Gobot --- src/archive/tar/reader.go | 5 ++--- src/archive/zip/reader.go | 2 +- src/bytes/reader_test.go | 21 ++++++++++----------- src/debug/elf/file.go | 8 ++++---- src/debug/pe/file.go | 8 ++++---- src/net/http/fs.go | 10 +++++----- src/net/sendfile_dragonfly.go | 2 +- src/net/sendfile_freebsd.go | 2 +- src/net/sendfile_solaris.go | 2 +- src/os/exec/exec_test.go | 2 +- src/strings/reader_test.go | 21 ++++++++++----------- 11 files changed, 40 insertions(+), 43 deletions(-) (limited to 'src/debug') diff --git a/src/archive/tar/reader.go b/src/archive/tar/reader.go index c8cb69a178..741fe0152b 100644 --- a/src/archive/tar/reader.go +++ b/src/archive/tar/reader.go @@ -13,7 +13,6 @@ import ( "io" "io/ioutil" "math" - "os" "strconv" "strings" "time" @@ -523,10 +522,10 @@ func (tr *Reader) skipUnread() error { // io.Seeker, but calling Seek always returns an error and performs // no action. Thus, we try an innocent seek to the current position // to see if Seek is really supported. - pos1, err := sr.Seek(0, os.SEEK_CUR) + pos1, err := sr.Seek(0, io.SeekCurrent) if err == nil { // Seek seems supported, so perform the real Seek. - pos2, err := sr.Seek(dataSkip-1, os.SEEK_CUR) + pos2, err := sr.Seek(dataSkip-1, io.SeekCurrent) if err != nil { tr.err = err return tr.err diff --git a/src/archive/zip/reader.go b/src/archive/zip/reader.go index d741d105dc..f6c3ead3be 100644 --- a/src/archive/zip/reader.go +++ b/src/archive/zip/reader.go @@ -87,7 +87,7 @@ func (z *Reader) init(r io.ReaderAt, size int64) error { z.File = make([]*File, 0, end.directoryRecords) z.Comment = end.comment rs := io.NewSectionReader(r, 0, size) - if _, err = rs.Seek(int64(end.directoryOffset), os.SEEK_SET); err != nil { + if _, err = rs.Seek(int64(end.directoryOffset), io.SeekStart); err != nil { return err } buf := bufio.NewReader(rs) diff --git a/src/bytes/reader_test.go b/src/bytes/reader_test.go index add985d57e..9341cd5b45 100644 --- a/src/bytes/reader_test.go +++ b/src/bytes/reader_test.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/ioutil" - "os" "sync" "testing" ) @@ -24,15 +23,15 @@ func TestReader(t *testing.T) { wantpos int64 seekerr string }{ - {seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"}, - {seek: os.SEEK_SET, off: 1, n: 1, want: "1"}, - {seek: os.SEEK_CUR, off: 1, wantpos: 3, n: 2, want: "34"}, - {seek: os.SEEK_SET, off: -1, seekerr: "bytes.Reader.Seek: negative position"}, - {seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33}, - {seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1}, - {seek: os.SEEK_SET, n: 5, want: "01234"}, - {seek: os.SEEK_CUR, n: 5, want: "56789"}, - {seek: os.SEEK_END, off: -1, n: 1, wantpos: 9, want: "9"}, + {seek: io.SeekStart, off: 0, n: 20, want: "0123456789"}, + {seek: io.SeekStart, off: 1, n: 1, want: "1"}, + {seek: io.SeekCurrent, off: 1, wantpos: 3, n: 2, want: "34"}, + {seek: io.SeekStart, off: -1, seekerr: "bytes.Reader.Seek: negative position"}, + {seek: io.SeekStart, off: 1 << 33, wantpos: 1 << 33}, + {seek: io.SeekCurrent, off: 1, wantpos: 1<<33 + 1}, + {seek: io.SeekStart, n: 5, want: "01234"}, + {seek: io.SeekCurrent, n: 5, want: "56789"}, + {seek: io.SeekEnd, off: -1, n: 1, wantpos: 9, want: "9"}, } for i, tt := range tests { @@ -63,7 +62,7 @@ func TestReader(t *testing.T) { func TestReadAfterBigSeek(t *testing.T) { r := NewReader([]byte("0123456789")) - if _, err := r.Seek(1<<31+5, os.SEEK_SET); err != nil { + if _, err := r.Seek(1<<31+5, io.SeekStart); err != nil { t.Fatal(err) } if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF { diff --git a/src/debug/elf/file.go b/src/debug/elf/file.go index c28a964b73..8fbf23fe5a 100644 --- a/src/debug/elf/file.go +++ b/src/debug/elf/file.go @@ -269,7 +269,7 @@ func NewFile(r io.ReaderAt) (*File, error) { switch f.Class { case ELFCLASS32: hdr := new(Header32) - sr.Seek(0, os.SEEK_SET) + sr.Seek(0, io.SeekStart) if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { return nil, err } @@ -288,7 +288,7 @@ func NewFile(r io.ReaderAt) (*File, error) { shstrndx = int(hdr.Shstrndx) case ELFCLASS64: hdr := new(Header64) - sr.Seek(0, os.SEEK_SET) + sr.Seek(0, io.SeekStart) if err := binary.Read(sr, f.ByteOrder, hdr); err != nil { return nil, err } @@ -315,7 +315,7 @@ func NewFile(r io.ReaderAt) (*File, error) { f.Progs = make([]*Prog, phnum) for i := 0; i < phnum; i++ { off := phoff + int64(i)*int64(phentsize) - sr.Seek(off, os.SEEK_SET) + sr.Seek(off, io.SeekStart) p := new(Prog) switch f.Class { case ELFCLASS32: @@ -359,7 +359,7 @@ func NewFile(r io.ReaderAt) (*File, error) { names := make([]uint32, shnum) for i := 0; i < shnum; i++ { off := shoff + int64(i)*int64(shentsize) - sr.Seek(off, os.SEEK_SET) + sr.Seek(off, io.SeekStart) s := new(Section) switch f.Class { case ELFCLASS32: diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index 1acc368e1b..c68ff1bdce 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -150,7 +150,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } else { base = int64(0) } - sr.Seek(base, os.SEEK_SET) + sr.Seek(base, io.SeekStart) if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil { return nil, err } @@ -161,7 +161,7 @@ func NewFile(r io.ReaderAt) (*File, error) { var ss []byte if f.FileHeader.NumberOfSymbols > 0 { // Get COFF string table, which is located at the end of the COFF symbol table. - sr.Seek(int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols), os.SEEK_SET) + sr.Seek(int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols), io.SeekStart) var l uint32 if err := binary.Read(sr, binary.LittleEndian, &l); err != nil { return nil, err @@ -172,7 +172,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } // Process COFF symbol table. - sr.Seek(int64(f.FileHeader.PointerToSymbolTable), os.SEEK_SET) + sr.Seek(int64(f.FileHeader.PointerToSymbolTable), io.SeekStart) aux := uint8(0) for i := 0; i < int(f.FileHeader.NumberOfSymbols); i++ { cs := new(COFFSymbol) @@ -203,7 +203,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } // Read optional header. - sr.Seek(base, os.SEEK_SET) + sr.Seek(base, io.SeekStart) if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil { return nil, err } diff --git a/src/net/http/fs.go b/src/net/http/fs.go index 5546d37516..c7a58a61df 100644 --- a/src/net/http/fs.go +++ b/src/net/http/fs.go @@ -121,11 +121,11 @@ func dirList(w ResponseWriter, f File) { // Note that *os.File implements the io.ReadSeeker interface. func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker) { sizeFunc := func() (int64, error) { - size, err := content.Seek(0, os.SEEK_END) + size, err := content.Seek(0, io.SeekEnd) if err != nil { return 0, errSeeker } - _, err = content.Seek(0, os.SEEK_SET) + _, err = content.Seek(0, io.SeekStart) if err != nil { return 0, errSeeker } @@ -166,7 +166,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, var buf [sniffLen]byte n, _ := io.ReadFull(content, buf[:]) ctype = DetectContentType(buf[:n]) - _, err := content.Seek(0, os.SEEK_SET) // rewind to output whole file + _, err := content.Seek(0, io.SeekStart) // rewind to output whole file if err != nil { Error(w, "seeker can't seek", StatusInternalServerError) return @@ -213,7 +213,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, // A response to a request for a single range MUST NOT // be sent using the multipart/byteranges media type." ra := ranges[0] - if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil { + if _, err := content.Seek(ra.start, io.SeekStart); err != nil { Error(w, err.Error(), StatusRequestedRangeNotSatisfiable) return } @@ -236,7 +236,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time, pw.CloseWithError(err) return } - if _, err := content.Seek(ra.start, os.SEEK_SET); err != nil { + if _, err := content.Seek(ra.start, io.SeekStart); err != nil { pw.CloseWithError(err) return } diff --git a/src/net/sendfile_dragonfly.go b/src/net/sendfile_dragonfly.go index 17021c3801..d4b825c370 100644 --- a/src/net/sendfile_dragonfly.go +++ b/src/net/sendfile_dragonfly.go @@ -53,7 +53,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { // use the current position of the file -- if you pass it offset 0, it starts // from offset 0. There's no way to tell it "start from current position", so // we have to manage that explicitly. - pos, err := f.Seek(0, os.SEEK_CUR) + pos, err := f.Seek(0, io.SeekCurrent) if err != nil { return 0, err, false } diff --git a/src/net/sendfile_freebsd.go b/src/net/sendfile_freebsd.go index f7a8529560..18cbb27b53 100644 --- a/src/net/sendfile_freebsd.go +++ b/src/net/sendfile_freebsd.go @@ -53,7 +53,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { // use the current position of the file -- if you pass it offset 0, it starts // from offset 0. There's no way to tell it "start from current position", so // we have to manage that explicitly. - pos, err := f.Seek(0, os.SEEK_CUR) + pos, err := f.Seek(0, io.SeekCurrent) if err != nil { return 0, err, false } diff --git a/src/net/sendfile_solaris.go b/src/net/sendfile_solaris.go index 20d2cddeea..add70c3147 100644 --- a/src/net/sendfile_solaris.go +++ b/src/net/sendfile_solaris.go @@ -57,7 +57,7 @@ func sendFile(c *netFD, r io.Reader) (written int64, err error, handled bool) { // use the current position of the file -- if you pass it offset 0, it starts // from offset 0. There's no way to tell it "start from current position", so // we have to manage that explicitly. - pos, err := f.Seek(0, os.SEEK_CUR) + pos, err := f.Seek(0, io.SeekCurrent) if err != nil { return 0, err, false } diff --git a/src/os/exec/exec_test.go b/src/os/exec/exec_test.go index 1f2fd12add..ed2721bb5e 100644 --- a/src/os/exec/exec_test.go +++ b/src/os/exec/exec_test.go @@ -479,7 +479,7 @@ func TestExtraFiles(t *testing.T) { if err != nil { t.Fatalf("Write: %v", err) } - _, err = tf.Seek(0, os.SEEK_SET) + _, err = tf.Seek(0, io.SeekStart) if err != nil { t.Fatalf("Seek: %v", err) } diff --git a/src/strings/reader_test.go b/src/strings/reader_test.go index 7bca2e89a1..6e9d904b9d 100644 --- a/src/strings/reader_test.go +++ b/src/strings/reader_test.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "io/ioutil" - "os" "strings" "sync" "testing" @@ -25,15 +24,15 @@ func TestReader(t *testing.T) { wantpos int64 seekerr string }{ - {seek: os.SEEK_SET, off: 0, n: 20, want: "0123456789"}, - {seek: os.SEEK_SET, off: 1, n: 1, want: "1"}, - {seek: os.SEEK_CUR, off: 1, wantpos: 3, n: 2, want: "34"}, - {seek: os.SEEK_SET, off: -1, seekerr: "strings.Reader.Seek: negative position"}, - {seek: os.SEEK_SET, off: 1 << 33, wantpos: 1 << 33}, - {seek: os.SEEK_CUR, off: 1, wantpos: 1<<33 + 1}, - {seek: os.SEEK_SET, n: 5, want: "01234"}, - {seek: os.SEEK_CUR, n: 5, want: "56789"}, - {seek: os.SEEK_END, off: -1, n: 1, wantpos: 9, want: "9"}, + {seek: io.SeekStart, off: 0, n: 20, want: "0123456789"}, + {seek: io.SeekStart, off: 1, n: 1, want: "1"}, + {seek: io.SeekCurrent, off: 1, wantpos: 3, n: 2, want: "34"}, + {seek: io.SeekStart, off: -1, seekerr: "strings.Reader.Seek: negative position"}, + {seek: io.SeekStart, off: 1 << 33, wantpos: 1 << 33}, + {seek: io.SeekCurrent, off: 1, wantpos: 1<<33 + 1}, + {seek: io.SeekStart, n: 5, want: "01234"}, + {seek: io.SeekCurrent, n: 5, want: "56789"}, + {seek: io.SeekEnd, off: -1, n: 1, wantpos: 9, want: "9"}, } for i, tt := range tests { @@ -64,7 +63,7 @@ func TestReader(t *testing.T) { func TestReadAfterBigSeek(t *testing.T) { r := strings.NewReader("0123456789") - if _, err := r.Seek(1<<31+5, os.SEEK_SET); err != nil { + if _, err := r.Seek(1<<31+5, io.SeekStart); err != nil { t.Fatal(err) } if n, err := r.Read(make([]byte, 10)); n != 0 || err != io.EOF { -- cgit v1.3 From 0da4dbe2322eb3b6224df35ce3e9fc83f104762b Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Thu, 14 Apr 2016 19:09:36 -0700 Subject: all: remove unnecessary type conversions cmd and runtime were handled separately, and I'm intentionally skipped syscall. This is the rest of the standard library. CL generated mechanically with github.com/mdempsky/unconvert. Change-Id: I9e0eff886974dedc37adb93f602064b83e469122 Reviewed-on: https://go-review.googlesource.com/22104 Reviewed-by: Brad Fitzpatrick Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot --- src/archive/tar/reader.go | 8 ++++---- src/archive/tar/writer.go | 2 +- src/bytes/reader.go | 2 +- src/compress/bzip2/bzip2.go | 8 ++++---- src/compress/flate/huffman_bit_writer.go | 2 +- src/compress/flate/reverse_bits.go | 2 +- src/compress/lzw/writer.go | 2 +- src/crypto/des/block.go | 2 +- src/crypto/tls/handshake_messages.go | 4 ++-- src/debug/dwarf/buf.go | 2 +- src/debug/dwarf/line.go | 2 +- src/debug/dwarf/typeunit.go | 4 ++-- src/debug/elf/elf.go | 4 ++-- src/debug/elf/file.go | 32 ++++++++++++++++---------------- src/debug/gosym/pclntab.go | 4 ++-- src/debug/gosym/symtab.go | 4 ++-- src/encoding/asn1/marshal.go | 6 +++--- src/encoding/binary/binary.go | 2 +- src/encoding/gob/encode.go | 2 +- src/go/ast/ast.go | 2 +- src/image/color/ycbcr.go | 18 +++++++++--------- src/image/draw/draw.go | 8 ++++---- src/math/big/float.go | 4 ++-- src/math/big/natconv.go | 2 +- src/math/big/ratconv.go | 2 +- src/net/interface_bsd.go | 4 ++-- src/net/mail/message.go | 2 +- src/net/parse.go | 4 ++-- src/os/exec_windows.go | 2 +- src/os/file_windows.go | 14 +++++++------- src/os/stat_darwin.go | 2 +- src/os/stat_dragonfly.go | 4 ++-- src/os/stat_freebsd.go | 2 +- src/os/stat_linux.go | 2 +- src/os/stat_nacl.go | 2 +- src/os/stat_netbsd.go | 4 ++-- src/os/stat_openbsd.go | 4 ++-- src/os/stat_plan9.go | 2 +- src/os/stat_solaris.go | 4 ++-- src/os/stat_windows.go | 2 +- src/os/types_windows.go | 2 +- src/reflect/type.go | 4 ++-- src/reflect/value.go | 26 +++++++++++++------------- src/regexp/onepass.go | 2 +- src/strconv/extfloat.go | 4 ++-- src/strings/reader.go | 2 +- src/sync/pool.go | 4 ++-- src/time/time.go | 8 ++++---- src/time/zoneinfo_windows.go | 2 +- src/unicode/letter.go | 2 +- 50 files changed, 120 insertions(+), 120 deletions(-) (limited to 'src/debug') diff --git a/src/archive/tar/reader.go b/src/archive/tar/reader.go index 741fe0152b..b924eeb568 100644 --- a/src/archive/tar/reader.go +++ b/src/archive/tar/reader.go @@ -306,7 +306,7 @@ func mergePAX(hdr *Header, headers map[string]string) error { if err != nil { return err } - hdr.Size = int64(size) + hdr.Size = size default: if strings.HasPrefix(k, paxXattr) { if hdr.Xattrs == nil { @@ -346,7 +346,7 @@ func parsePAXTime(t string) (time.Time, error) { // Right truncate nano_buf = nano_buf[:maxNanoSecondIntSize] } - nanoseconds, err = strconv.ParseInt(string(nano_buf), 10, 0) + nanoseconds, err = strconv.ParseInt(nano_buf, 10, 0) if err != nil { return time.Time{}, err } @@ -378,14 +378,14 @@ func parsePAX(r io.Reader) (map[string]string, error) { } sbuf = residual - keyStr := string(key) + keyStr := key if keyStr == paxGNUSparseOffset || keyStr == paxGNUSparseNumBytes { // GNU sparse format 0.0 special key. Write to sparseMap instead of using the headers map. sparseMap.WriteString(value) sparseMap.Write([]byte{','}) } else { // Normal key. Set the value in the headers map. - headers[keyStr] = string(value) + headers[keyStr] = value } } if sparseMap.Len() != 0 { diff --git a/src/archive/tar/writer.go b/src/archive/tar/writer.go index 600ee4be09..944b2d4952 100644 --- a/src/archive/tar/writer.go +++ b/src/archive/tar/writer.go @@ -278,7 +278,7 @@ func (tw *Writer) writeHeader(hdr *Header, allowPax bool) error { return err } } - tw.nb = int64(hdr.Size) + tw.nb = hdr.Size tw.pad = (blockSize - (tw.nb % blockSize)) % blockSize _, tw.err = tw.w.Write(header) diff --git a/src/bytes/reader.go b/src/bytes/reader.go index 7aa30578b3..aa39890f3b 100644 --- a/src/bytes/reader.go +++ b/src/bytes/reader.go @@ -114,7 +114,7 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) { case 0: abs = offset case 1: - abs = int64(r.i) + offset + abs = r.i + offset case 2: abs = int64(len(r.s)) + offset default: diff --git a/src/compress/bzip2/bzip2.go b/src/compress/bzip2/bzip2.go index 90e9aebab6..42788443bc 100644 --- a/src/compress/bzip2/bzip2.go +++ b/src/compress/bzip2/bzip2.go @@ -75,7 +75,7 @@ func (bz2 *reader) setup(needMagic bool) error { } bz2.fileCRC = 0 - bz2.blockSize = 100 * 1000 * (int(level) - '0') + bz2.blockSize = 100 * 1000 * (level - '0') if bz2.blockSize > len(bz2.tt) { bz2.tt = make([]uint32, bz2.blockSize) } @@ -293,7 +293,7 @@ func (bz2 *reader) readBlock() (err error) { if c >= numHuffmanTrees { return StructuralError("tree index too large") } - treeIndexes[i] = uint8(mtfTreeDecoder.Decode(c)) + treeIndexes[i] = mtfTreeDecoder.Decode(c) } // The list of symbols for the move-to-front transform is taken from @@ -399,7 +399,7 @@ func (bz2 *reader) readBlock() (err error) { return StructuralError("repeats past end of block") } for i := 0; i < repeat; i++ { - b := byte(mtf.First()) + b := mtf.First() bz2.tt[bufIndex] = uint32(b) bz2.c[b]++ bufIndex++ @@ -420,7 +420,7 @@ func (bz2 *reader) readBlock() (err error) { // it's always referenced with a run-length of 1. Thus 0 // doesn't need to be encoded and we have |v-1| in the next // line. - b := byte(mtf.Decode(int(v - 1))) + b := mtf.Decode(int(v - 1)) if bufIndex >= bz2.blockSize { return StructuralError("data exceeds block size") } diff --git a/src/compress/flate/huffman_bit_writer.go b/src/compress/flate/huffman_bit_writer.go index 23f242f88e..d0206e59cf 100644 --- a/src/compress/flate/huffman_bit_writer.go +++ b/src/compress/flate/huffman_bit_writer.go @@ -436,7 +436,7 @@ func (w *huffmanBitWriter) writeBlock(tokens []token, eof bool, input []byte) { } dynamicHeader := int64(3+5+5+4+(3*numCodegens)) + w.codegenEncoding.bitLength(w.codegenFreq[:]) + - int64(extraBits) + + extraBits + int64(w.codegenFreq[16]*2) + int64(w.codegenFreq[17]*3) + int64(w.codegenFreq[18]*7) diff --git a/src/compress/flate/reverse_bits.go b/src/compress/flate/reverse_bits.go index c1a02720d1..6b222900c1 100644 --- a/src/compress/flate/reverse_bits.go +++ b/src/compress/flate/reverse_bits.go @@ -44,5 +44,5 @@ func reverseUint16(v uint16) uint16 { } func reverseBits(number uint16, bitLength byte) uint16 { - return reverseUint16(number << uint8(16-bitLength)) + return reverseUint16(number << (16 - bitLength)) } diff --git a/src/compress/lzw/writer.go b/src/compress/lzw/writer.go index 7367c29651..6ddb335f31 100644 --- a/src/compress/lzw/writer.go +++ b/src/compress/lzw/writer.go @@ -119,7 +119,7 @@ func (e *encoder) incHi() error { if err := e.write(e, clear); err != nil { return err } - e.width = uint(e.litWidth) + 1 + e.width = e.litWidth + 1 e.hi = clear + 1 e.overflow = clear << 1 for i := range e.table { diff --git a/src/crypto/des/block.go b/src/crypto/des/block.go index 26355a22e7..99338d62a6 100644 --- a/src/crypto/des/block.go +++ b/src/crypto/des/block.go @@ -72,7 +72,7 @@ func init() { for i := 0; i < 4; i++ { for j := 0; j < 16; j++ { f := uint64(sBoxes[s][i][j]) << (4 * (7 - uint(s))) - f = permuteBlock(uint64(f), permutationFunction[:]) + f = permuteBlock(f, permutationFunction[:]) feistelBox[s][16*i+j] = uint32(f) } } diff --git a/src/crypto/tls/handshake_messages.go b/src/crypto/tls/handshake_messages.go index 13d013a594..3f9a63b110 100644 --- a/src/crypto/tls/handshake_messages.go +++ b/src/crypto/tls/handshake_messages.go @@ -214,7 +214,7 @@ func (m *clientHelloMsg) marshal() []byte { z[4] = byte(l) z = z[5:] for _, pointFormat := range m.supportedPoints { - z[0] = byte(pointFormat) + z[0] = pointFormat z = z[1:] } } @@ -589,7 +589,7 @@ func (m *serverHelloMsg) marshal() []byte { z := x[39+len(m.sessionId):] z[0] = uint8(m.cipherSuite >> 8) z[1] = uint8(m.cipherSuite) - z[2] = uint8(m.compressionMethod) + z[2] = m.compressionMethod z = z[3:] if numExtensions > 0 { diff --git a/src/debug/dwarf/buf.go b/src/debug/dwarf/buf.go index 7443043c11..24d266db10 100644 --- a/src/debug/dwarf/buf.go +++ b/src/debug/dwarf/buf.go @@ -157,7 +157,7 @@ func (b *buf) addr() uint64 { case 4: return uint64(b.uint32()) case 8: - return uint64(b.uint64()) + return b.uint64() } b.error("unknown address size") return 0 diff --git a/src/debug/dwarf/line.go b/src/debug/dwarf/line.go index b3b91ade62..ed82feef92 100644 --- a/src/debug/dwarf/line.go +++ b/src/debug/dwarf/line.go @@ -361,7 +361,7 @@ func (r *LineReader) step(entry *LineEntry) bool { // Special opcode [DWARF2 6.2.5.1, DWARF4 6.2.5.1] adjustedOpcode := opcode - r.opcodeBase r.advancePC(adjustedOpcode / r.lineRange) - lineDelta := r.lineBase + int(adjustedOpcode)%r.lineRange + lineDelta := r.lineBase + adjustedOpcode%r.lineRange r.state.Line += lineDelta goto emit } diff --git a/src/debug/dwarf/typeunit.go b/src/debug/dwarf/typeunit.go index ed42547386..652e02d917 100644 --- a/src/debug/dwarf/typeunit.go +++ b/src/debug/dwarf/typeunit.go @@ -76,7 +76,7 @@ func (d *Data) parseTypes(name string, types []byte) error { data: b.bytes(int(n - (b.off - hdroff))), atable: atable, asize: int(asize), - vers: int(vers), + vers: vers, is64: dwarf64, }, toff: Offset(toff), @@ -101,7 +101,7 @@ func (d *Data) sigToType(sig uint64) (Type, error) { b := makeBuf(d, tu, tu.name, tu.off, tu.data) r := &typeUnitReader{d: d, tu: tu, b: b} - t, err := d.readType(tu.name, r, Offset(tu.toff), make(map[Offset]Type), nil) + t, err := d.readType(tu.name, r, tu.toff, make(map[Offset]Type), nil) if err != nil { return nil, err } diff --git a/src/debug/elf/elf.go b/src/debug/elf/elf.go index af881c2495..3f43d4d896 100644 --- a/src/debug/elf/elf.go +++ b/src/debug/elf/elf.go @@ -2060,8 +2060,8 @@ type Rela32 struct { Addend int32 /* Addend. */ } -func R_SYM32(info uint32) uint32 { return uint32(info >> 8) } -func R_TYPE32(info uint32) uint32 { return uint32(info & 0xff) } +func R_SYM32(info uint32) uint32 { return info >> 8 } +func R_TYPE32(info uint32) uint32 { return info & 0xff } func R_INFO32(sym, typ uint32) uint32 { return sym<<8 | typ } // ELF32 Symbol. diff --git a/src/debug/elf/file.go b/src/debug/elf/file.go index 8fbf23fe5a..c173ea9331 100644 --- a/src/debug/elf/file.go +++ b/src/debug/elf/file.go @@ -294,7 +294,7 @@ func NewFile(r io.ReaderAt) (*File, error) { } f.Type = Type(hdr.Type) f.Machine = Machine(hdr.Machine) - f.Entry = uint64(hdr.Entry) + f.Entry = hdr.Entry if v := Version(hdr.Version); v != f.Version { return nil, &FormatError{0, "mismatched ELF version", v} } @@ -341,12 +341,12 @@ func NewFile(r io.ReaderAt) (*File, error) { p.ProgHeader = ProgHeader{ Type: ProgType(ph.Type), Flags: ProgFlag(ph.Flags), - Off: uint64(ph.Off), - Vaddr: uint64(ph.Vaddr), - Paddr: uint64(ph.Paddr), - Filesz: uint64(ph.Filesz), - Memsz: uint64(ph.Memsz), - Align: uint64(ph.Align), + Off: ph.Off, + Vaddr: ph.Vaddr, + Paddr: ph.Paddr, + Filesz: ph.Filesz, + Memsz: ph.Memsz, + Align: ph.Align, } } p.sr = io.NewSectionReader(r, int64(p.Off), int64(p.Filesz)) @@ -374,8 +374,8 @@ func NewFile(r io.ReaderAt) (*File, error) { Addr: uint64(sh.Addr), Offset: uint64(sh.Off), FileSize: uint64(sh.Size), - Link: uint32(sh.Link), - Info: uint32(sh.Info), + Link: sh.Link, + Info: sh.Info, Addralign: uint64(sh.Addralign), Entsize: uint64(sh.Entsize), } @@ -388,13 +388,13 @@ func NewFile(r io.ReaderAt) (*File, error) { s.SectionHeader = SectionHeader{ Type: SectionType(sh.Type), Flags: SectionFlag(sh.Flags), - Offset: uint64(sh.Off), - FileSize: uint64(sh.Size), - Addr: uint64(sh.Addr), - Link: uint32(sh.Link), - Info: uint32(sh.Info), - Addralign: uint64(sh.Addralign), - Entsize: uint64(sh.Entsize), + Offset: sh.Off, + FileSize: sh.Size, + Addr: sh.Addr, + Link: sh.Link, + Info: sh.Info, + Addralign: sh.Addralign, + Entsize: sh.Entsize, } } s.sr = io.NewSectionReader(r, int64(s.Offset), int64(s.FileSize)) diff --git a/src/debug/gosym/pclntab.go b/src/debug/gosym/pclntab.go index 291f102262..e859d5aed5 100644 --- a/src/debug/gosym/pclntab.go +++ b/src/debug/gosym/pclntab.go @@ -207,8 +207,8 @@ func (t *LineTable) go12Funcs() []Func { funcs := make([]Func, n) for i := range funcs { f := &funcs[i] - f.Entry = uint64(t.uintptr(t.functab[2*i*int(t.ptrsize):])) - f.End = uint64(t.uintptr(t.functab[(2*i+2)*int(t.ptrsize):])) + f.Entry = t.uintptr(t.functab[2*i*int(t.ptrsize):]) + f.End = t.uintptr(t.functab[(2*i+2)*int(t.ptrsize):]) info := t.Data[t.uintptr(t.functab[(2*i+1)*int(t.ptrsize):]):] f.LineTable = t f.FrameSize = int(t.binary.Uint32(info[t.ptrsize+2*4:])) diff --git a/src/debug/gosym/symtab.go b/src/debug/gosym/symtab.go index 49e154fd8e..c8fa9a0b38 100644 --- a/src/debug/gosym/symtab.go +++ b/src/debug/gosym/symtab.go @@ -294,8 +294,8 @@ func NewTable(symtab []byte, pcln *LineTable) (*Table, error) { t.Syms = t.Syms[0 : n+1] ts := &t.Syms[n] ts.Type = s.typ - ts.Value = uint64(s.value) - ts.GoType = uint64(s.gotype) + ts.Value = s.value + ts.GoType = s.gotype switch s.typ { default: // rewrite name to use . instead of ยท (c2 b7) diff --git a/src/encoding/asn1/marshal.go b/src/encoding/asn1/marshal.go index 2b796c4e75..30797ef099 100644 --- a/src/encoding/asn1/marshal.go +++ b/src/encoding/asn1/marshal.go @@ -315,9 +315,9 @@ func marshalUTCTime(out *forkableWriter, t time.Time) (err error) { switch { case 1950 <= year && year < 2000: - err = marshalTwoDigits(out, int(year-1900)) + err = marshalTwoDigits(out, year-1900) case 2000 <= year && year < 2050: - err = marshalTwoDigits(out, int(year-2000)) + err = marshalTwoDigits(out, year-2000) default: return StructuralError{"cannot represent time as UTCTime"} } @@ -435,7 +435,7 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter return out.WriteByte(0) } case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - return marshalInt64(out, int64(v.Int())) + return marshalInt64(out, v.Int()) case reflect.Struct: t := v.Type() diff --git a/src/encoding/binary/binary.go b/src/encoding/binary/binary.go index ada5768695..46c6add062 100644 --- a/src/encoding/binary/binary.go +++ b/src/encoding/binary/binary.go @@ -269,7 +269,7 @@ func Write(w io.Writer, order ByteOrder, data interface{}) error { case *uint8: b[0] = *v case uint8: - b[0] = byte(v) + b[0] = v case []uint8: bs = v case *int16: diff --git a/src/encoding/gob/encode.go b/src/encoding/gob/encode.go index 2b3a556eac..50cd6adb46 100644 --- a/src/encoding/gob/encode.go +++ b/src/encoding/gob/encode.go @@ -127,7 +127,7 @@ func (state *encoderState) encodeInt(i int64) { } else { x = uint64(i << 1) } - state.encodeUint(uint64(x)) + state.encodeUint(x) } // encOp is the signature of an encoding operator for a given type. diff --git a/src/go/ast/ast.go b/src/go/ast/ast.go index 5ab4283826..cca2d48bbd 100644 --- a/src/go/ast/ast.go +++ b/src/go/ast/ast.go @@ -99,7 +99,7 @@ func (g *CommentGroup) Text() string { } comments := make([]string, len(g.List)) for i, c := range g.List { - comments[i] = string(c.Text) + comments[i] = c.Text } lines := make([]string, 0, 10) // most comments are less than 10 lines diff --git a/src/image/color/ycbcr.go b/src/image/color/ycbcr.go index d2c5b569a7..2e985fece1 100644 --- a/src/image/color/ycbcr.go +++ b/src/image/color/ycbcr.go @@ -237,10 +237,10 @@ func RGBToCMYK(r, g, b uint8) (uint8, uint8, uint8, uint8) { // CMYKToRGB converts a CMYK quadruple to an RGB triple. func CMYKToRGB(c, m, y, k uint8) (uint8, uint8, uint8) { - w := uint32(0xffff - uint32(k)*0x101) - r := uint32(0xffff-uint32(c)*0x101) * w / 0xffff - g := uint32(0xffff-uint32(m)*0x101) * w / 0xffff - b := uint32(0xffff-uint32(y)*0x101) * w / 0xffff + w := 0xffff - uint32(k)*0x101 + r := (0xffff - uint32(c)*0x101) * w / 0xffff + g := (0xffff - uint32(m)*0x101) * w / 0xffff + b := (0xffff - uint32(y)*0x101) * w / 0xffff return uint8(r >> 8), uint8(g >> 8), uint8(b >> 8) } @@ -256,11 +256,11 @@ func (c CMYK) RGBA() (uint32, uint32, uint32, uint32) { // This code is a copy of the CMYKToRGB function above, except that it // returns values in the range [0, 0xffff] instead of [0, 0xff]. - w := uint32(0xffff - uint32(c.K)*0x101) - r := uint32(0xffff-uint32(c.C)*0x101) * w / 0xffff - g := uint32(0xffff-uint32(c.M)*0x101) * w / 0xffff - b := uint32(0xffff-uint32(c.Y)*0x101) * w / 0xffff - return uint32(r), uint32(g), uint32(b), 0xffff + w := 0xffff - uint32(c.K)*0x101 + r := (0xffff - uint32(c.C)*0x101) * w / 0xffff + g := (0xffff - uint32(c.M)*0x101) * w / 0xffff + b := (0xffff - uint32(c.Y)*0x101) * w / 0xffff + return r, g, b, 0xffff } // CMYKModel is the Model for CMYK colors. diff --git a/src/image/draw/draw.go b/src/image/draw/draw.go index 94e3575663..6a16cd39cf 100644 --- a/src/image/draw/draw.go +++ b/src/image/draw/draw.go @@ -634,10 +634,10 @@ func drawPaletted(dst Image, r image.Rectangle, src image.Image, sp image.Point, if !floydSteinberg { continue } - er -= int32(palette[bestIndex][0]) - eg -= int32(palette[bestIndex][1]) - eb -= int32(palette[bestIndex][2]) - ea -= int32(palette[bestIndex][3]) + er -= palette[bestIndex][0] + eg -= palette[bestIndex][1] + eb -= palette[bestIndex][2] + ea -= palette[bestIndex][3] } else { out.R = uint16(er) diff --git a/src/math/big/float.go b/src/math/big/float.go index 4b8ad388d3..7a9c2b3dfb 100644 --- a/src/math/big/float.go +++ b/src/math/big/float.go @@ -1008,9 +1008,9 @@ func (x *Float) Float64() (float64, Accuracy) { if r.form == inf || e > emax { // overflow if x.neg { - return float64(math.Inf(-1)), Below + return math.Inf(-1), Below } - return float64(math.Inf(+1)), Above + return math.Inf(+1), Above } // e <= emax diff --git a/src/math/big/natconv.go b/src/math/big/natconv.go index d2ce667fb6..e216bd288c 100644 --- a/src/math/big/natconv.go +++ b/src/math/big/natconv.go @@ -302,7 +302,7 @@ func (x nat) itoa(neg bool, base int) []byte { } } else { - bb, ndigits := maxPow(Word(b)) + bb, ndigits := maxPow(b) // construct table of successive squares of bb*leafSize to use in subdivisions // result (table != nil) <=> (len(x) > leafSize > 0) diff --git a/src/math/big/ratconv.go b/src/math/big/ratconv.go index 57df124e88..7c127f8585 100644 --- a/src/math/big/ratconv.go +++ b/src/math/big/ratconv.go @@ -178,7 +178,7 @@ func scanExponent(r io.ByteScanner, binExpOk bool) (exp int64, base int, err err } break // i > 0 } - digits = append(digits, byte(ch)) + digits = append(digits, ch) } // i > 0 => we have at least one digit diff --git a/src/net/interface_bsd.go b/src/net/interface_bsd.go index b173fbcefc..17c6dd3dcd 100644 --- a/src/net/interface_bsd.go +++ b/src/net/interface_bsd.go @@ -61,13 +61,13 @@ func newLink(m *syscall.InterfaceMessage) (*Interface, error) { m.Data = m.Data[unsafe.Offsetof(sa.Data):] var name [syscall.IFNAMSIZ]byte for i := 0; i < int(sa.Nlen); i++ { - name[i] = byte(m.Data[i]) + name[i] = m.Data[i] } ifi.Name = string(name[:sa.Nlen]) ifi.MTU = int(m.Header.Data.Mtu) addr := make([]byte, sa.Alen) for i := 0; i < int(sa.Alen); i++ { - addr[i] = byte(m.Data[int(sa.Nlen)+i]) + addr[i] = m.Data[int(sa.Nlen)+i] } ifi.HardwareAddr = addr[:sa.Alen] } diff --git a/src/net/mail/message.go b/src/net/mail/message.go index 9e3a103a4f..b40a314e33 100644 --- a/src/net/mail/message.go +++ b/src/net/mail/message.go @@ -477,7 +477,7 @@ func (p *addrParser) consumeAtom(dot bool, permissive bool) (atom string, err er if i < p.len() && p.s[i] > 127 { return "", errNonASCII } - atom, p.s = string(p.s[:i]), p.s[i:] + atom, p.s = p.s[:i], p.s[i:] if !permissive { if strings.HasPrefix(atom, ".") { return "", errors.New("mail: leading dot in atom") diff --git a/src/net/parse.go b/src/net/parse.go index eaaa1edf30..ed82a7769b 100644 --- a/src/net/parse.go +++ b/src/net/parse.go @@ -105,14 +105,14 @@ func splitAtBytes(s string, t string) []string { for i := 0; i < len(s); i++ { if byteIndex(t, s[i]) >= 0 { if last < i { - a[n] = string(s[last:i]) + a[n] = s[last:i] n++ } last = i + 1 } } if last < len(s) { - a[n] = string(s[last:]) + a[n] = s[last:] n++ } return a[0:n] diff --git a/src/os/exec_windows.go b/src/os/exec_windows.go index 3264271b2e..72b5a93199 100644 --- a/src/os/exec_windows.go +++ b/src/os/exec_windows.go @@ -104,7 +104,7 @@ func init() { defer syscall.LocalFree(syscall.Handle(uintptr(unsafe.Pointer(argv)))) Args = make([]string, argc) for i, v := range (*argv)[:argc] { - Args[i] = string(syscall.UTF16ToString((*v)[:])) + Args[i] = syscall.UTF16ToString((*v)[:]) } } diff --git a/src/os/file_windows.go b/src/os/file_windows.go index 7d04477d42..137f24a0a9 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -181,9 +181,9 @@ func (file *file) close() error { } var e error if file.isdir() { - e = syscall.FindClose(syscall.Handle(file.fd)) + e = syscall.FindClose(file.fd) } else { - e = syscall.CloseHandle(syscall.Handle(file.fd)) + e = syscall.CloseHandle(file.fd) } var err error if e != nil { @@ -216,7 +216,7 @@ func (file *File) readdir(n int) (fi []FileInfo, err error) { d := &file.dirinfo.data for n != 0 && !file.dirinfo.isempty { if file.dirinfo.needdata { - e := syscall.FindNextFile(syscall.Handle(file.fd), d) + e := syscall.FindNextFile(file.fd, d) if e != nil { if e == syscall.ERROR_NO_MORE_FILES { break @@ -230,7 +230,7 @@ func (file *File) readdir(n int) (fi []FileInfo, err error) { } } file.dirinfo.needdata = true - name := string(syscall.UTF16ToString(d.FileName[0:])) + name := syscall.UTF16ToString(d.FileName[0:]) if name == "." || name == ".." { // Useless names continue } @@ -288,7 +288,7 @@ func (f *File) readConsole(b []byte) (n int, err error) { } wchars := make([]uint16, nwc) pwc := &wchars[0] - nwc, err = windows.MultiByteToWideChar(acp, 2, pmb, int32(nmb), pwc, int32(nwc)) + nwc, err = windows.MultiByteToWideChar(acp, 2, pmb, int32(nmb), pwc, nwc) if err != nil { return 0, err } @@ -335,7 +335,7 @@ func (f *File) pread(b []byte, off int64) (n int, err error) { Offset: uint32(off), } var done uint32 - e = syscall.ReadFile(syscall.Handle(f.fd), b, &done, &o) + e = syscall.ReadFile(f.fd, b, &done, &o) if e != nil { if e == syscall.ERROR_HANDLE_EOF { // end of file @@ -415,7 +415,7 @@ func (f *File) pwrite(b []byte, off int64) (n int, err error) { Offset: uint32(off), } var done uint32 - e = syscall.WriteFile(syscall.Handle(f.fd), b, &done, &o) + e = syscall.WriteFile(f.fd, b, &done, &o) if e != nil { return 0, e } diff --git a/src/os/stat_darwin.go b/src/os/stat_darwin.go index 9dc7a99fb7..74214cefa4 100644 --- a/src/os/stat_darwin.go +++ b/src/os/stat_darwin.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtimespec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_dragonfly.go b/src/os/stat_dragonfly.go index 69e63230eb..217bc6726d 100644 --- a/src/os/stat_dragonfly.go +++ b/src/os/stat_dragonfly.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, ts.Nsec) } // For testing. diff --git a/src/os/stat_freebsd.go b/src/os/stat_freebsd.go index e9d38aa722..bab4ffa798 100644 --- a/src/os/stat_freebsd.go +++ b/src/os/stat_freebsd.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtimespec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_linux.go b/src/os/stat_linux.go index 69e63230eb..d36afa9ffd 100644 --- a/src/os/stat_linux.go +++ b/src/os/stat_linux.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_nacl.go b/src/os/stat_nacl.go index d3bed14e43..0c53f2faa4 100644 --- a/src/os/stat_nacl.go +++ b/src/os/stat_nacl.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtime, fs.sys.MtimeNsec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { diff --git a/src/os/stat_netbsd.go b/src/os/stat_netbsd.go index e9d38aa722..11ebcacab8 100644 --- a/src/os/stat_netbsd.go +++ b/src/os/stat_netbsd.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtimespec) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, int64(ts.Nsec)) } // For testing. diff --git a/src/os/stat_openbsd.go b/src/os/stat_openbsd.go index 69e63230eb..9df2d7f773 100644 --- a/src/os/stat_openbsd.go +++ b/src/os/stat_openbsd.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, int64(ts.Nsec)) } // For testing. diff --git a/src/os/stat_plan9.go b/src/os/stat_plan9.go index a2df5fe139..96f056c111 100644 --- a/src/os/stat_plan9.go +++ b/src/os/stat_plan9.go @@ -20,7 +20,7 @@ func sameFile(fs1, fs2 *fileStat) bool { func fileInfoFromStat(d *syscall.Dir) FileInfo { fs := &fileStat{ name: d.Name, - size: int64(d.Length), + size: d.Length, modTime: time.Unix(int64(d.Mtime), 0), sys: d, } diff --git a/src/os/stat_solaris.go b/src/os/stat_solaris.go index 69e63230eb..217bc6726d 100644 --- a/src/os/stat_solaris.go +++ b/src/os/stat_solaris.go @@ -11,7 +11,7 @@ import ( func fillFileStatFromSys(fs *fileStat, name string) { fs.name = basename(name) - fs.size = int64(fs.sys.Size) + fs.size = fs.sys.Size fs.modTime = timespecToTime(fs.sys.Mtim) fs.mode = FileMode(fs.sys.Mode & 0777) switch fs.sys.Mode & syscall.S_IFMT { @@ -42,7 +42,7 @@ func fillFileStatFromSys(fs *fileStat, name string) { } func timespecToTime(ts syscall.Timespec) time.Time { - return time.Unix(int64(ts.Sec), int64(ts.Nsec)) + return time.Unix(ts.Sec, ts.Nsec) } // For testing. diff --git a/src/os/stat_windows.go b/src/os/stat_windows.go index b8f97ad60a..e55eeb0fdd 100644 --- a/src/os/stat_windows.go +++ b/src/os/stat_windows.go @@ -35,7 +35,7 @@ func (file *File) Stat() (FileInfo, error) { } var d syscall.ByHandleFileInformation - err = syscall.GetFileInformationByHandle(syscall.Handle(file.fd), &d) + err = syscall.GetFileInformationByHandle(file.fd, &d) if err != nil { return nil, &PathError{"GetFileInformationByHandle", file.name, err} } diff --git a/src/os/types_windows.go b/src/os/types_windows.go index 900d444b0e..ad4e863fcb 100644 --- a/src/os/types_windows.go +++ b/src/os/types_windows.go @@ -73,7 +73,7 @@ func (fs *fileStat) loadFileId() error { } defer syscall.CloseHandle(h) var i syscall.ByHandleFileInformation - err = syscall.GetFileInformationByHandle(syscall.Handle(h), &i) + err = syscall.GetFileInformationByHandle(h, &i) if err != nil { return err } diff --git a/src/reflect/type.go b/src/reflect/type.go index 3c7affcd7f..b8c778cc2b 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -967,7 +967,7 @@ func (t *rtype) Out(i int) Type { } func (t *funcType) in() []*rtype { - uadd := uintptr(unsafe.Sizeof(*t)) + uadd := unsafe.Sizeof(*t) if t.tflag&tflagUncommon != 0 { uadd += unsafe.Sizeof(uncommonType{}) } @@ -975,7 +975,7 @@ func (t *funcType) in() []*rtype { } func (t *funcType) out() []*rtype { - uadd := uintptr(unsafe.Sizeof(*t)) + uadd := unsafe.Sizeof(*t) if t.tflag&tflagUncommon != 0 { uadd += unsafe.Sizeof(uncommonType{}) } diff --git a/src/reflect/value.go b/src/reflect/value.go index d72c14e9e1..d4d317436a 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -666,7 +666,7 @@ func (v Value) Cap() int { case Array: return v.typ.Len() case Chan: - return int(chancap(v.pointer())) + return chancap(v.pointer()) case Slice: // Slice is always bigger than a word; assume flagIndir. return (*sliceHeader)(v.ptr).Cap @@ -885,7 +885,7 @@ func (v Value) Int() int64 { case Int32: return int64(*(*int32)(p)) case Int64: - return int64(*(*int64)(p)) + return *(*int64)(p) } panic(&ValueError{"reflect.Value.Int", v.kind()}) } @@ -1436,7 +1436,7 @@ func (v Value) SetCap(n int) { v.mustBeAssignable() v.mustBe(Slice) s := (*sliceHeader)(v.ptr) - if n < int(s.Len) || n > int(s.Cap) { + if n < s.Len || n > s.Cap { panic("reflect: slice capacity out of range in SetCap") } s.Cap = n @@ -1538,7 +1538,7 @@ func (v Value) Slice(i, j int) Value { case Slice: typ = (*sliceType)(unsafe.Pointer(v.typ)) s := (*sliceHeader)(v.ptr) - base = unsafe.Pointer(s.Data) + base = s.Data cap = s.Cap case String: @@ -1710,7 +1710,7 @@ func (v Value) Uint() uint64 { case Uint32: return uint64(*(*uint32)(p)) case Uint64: - return uint64(*(*uint64)(p)) + return *(*uint64)(p) case Uintptr: return uint64(*(*uintptr)(p)) } @@ -2267,13 +2267,13 @@ func makeInt(f flag, bits uint64, t Type) Value { ptr := unsafe_New(typ) switch typ.size { case 1: - *(*uint8)(unsafe.Pointer(ptr)) = uint8(bits) + *(*uint8)(ptr) = uint8(bits) case 2: - *(*uint16)(unsafe.Pointer(ptr)) = uint16(bits) + *(*uint16)(ptr) = uint16(bits) case 4: - *(*uint32)(unsafe.Pointer(ptr)) = uint32(bits) + *(*uint32)(ptr) = uint32(bits) case 8: - *(*uint64)(unsafe.Pointer(ptr)) = bits + *(*uint64)(ptr) = bits } return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} } @@ -2285,9 +2285,9 @@ func makeFloat(f flag, v float64, t Type) Value { ptr := unsafe_New(typ) switch typ.size { case 4: - *(*float32)(unsafe.Pointer(ptr)) = float32(v) + *(*float32)(ptr) = float32(v) case 8: - *(*float64)(unsafe.Pointer(ptr)) = v + *(*float64)(ptr) = v } return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} } @@ -2299,9 +2299,9 @@ func makeComplex(f flag, v complex128, t Type) Value { ptr := unsafe_New(typ) switch typ.size { case 8: - *(*complex64)(unsafe.Pointer(ptr)) = complex64(v) + *(*complex64)(ptr) = complex64(v) case 16: - *(*complex128)(unsafe.Pointer(ptr)) = v + *(*complex128)(ptr) = v } return Value{typ, ptr, f | flagIndir | flag(typ.Kind())} } diff --git a/src/regexp/onepass.go b/src/regexp/onepass.go index 5b82f9666e..4991954820 100644 --- a/src/regexp/onepass.go +++ b/src/regexp/onepass.go @@ -450,7 +450,7 @@ func makeOnePass(p *onePassProg) *onePassProg { for !instQueue.empty() { visitQueue.clear() pc := instQueue.next() - if !check(uint32(pc), m) { + if !check(pc, m) { p = notOnePass break } diff --git a/src/strconv/extfloat.go b/src/strconv/extfloat.go index 019b4eebdc..7033e96c39 100644 --- a/src/strconv/extfloat.go +++ b/src/strconv/extfloat.go @@ -311,9 +311,9 @@ func (f *extFloat) AssignDecimal(mantissa uint64, exp10 int, neg bool, trunc boo var extrabits uint if f.exp <= denormalExp { // f.mant * 2^f.exp is smaller than 2^(flt.bias+1). - extrabits = uint(63 - flt.mantbits + 1 + uint(denormalExp-f.exp)) + extrabits = 63 - flt.mantbits + 1 + uint(denormalExp-f.exp) } else { - extrabits = uint(63 - flt.mantbits) + extrabits = 63 - flt.mantbits } halfway := uint64(1) << (extrabits - 1) diff --git a/src/strings/reader.go b/src/strings/reader.go index 737873c099..74eed4d574 100644 --- a/src/strings/reader.go +++ b/src/strings/reader.go @@ -113,7 +113,7 @@ func (r *Reader) Seek(offset int64, whence int) (int64, error) { case 0: abs = offset case 1: - abs = int64(r.i) + offset + abs = r.i + offset case 2: abs = int64(len(r.s)) + offset default: diff --git a/src/sync/pool.go b/src/sync/pool.go index 4fb1a1af9d..2acf505f3c 100644 --- a/src/sync/pool.go +++ b/src/sync/pool.go @@ -179,8 +179,8 @@ func (p *Pool) pinSlow() *poolLocal { // If GOMAXPROCS changes between GCs, we re-allocate the array and lose the old one. size := runtime.GOMAXPROCS(0) local := make([]poolLocal, size) - atomic.StorePointer((*unsafe.Pointer)(&p.local), unsafe.Pointer(&local[0])) // store-release - atomic.StoreUintptr(&p.localSize, uintptr(size)) // store-release + atomic.StorePointer(&p.local, unsafe.Pointer(&local[0])) // store-release + atomic.StoreUintptr(&p.localSize, uintptr(size)) // store-release return &local[pid] } diff --git a/src/time/time.go b/src/time/time.go index 4b9a0db730..92d635eec5 100644 --- a/src/time/time.go +++ b/src/time/time.go @@ -606,7 +606,7 @@ func (d Duration) Hours() float64 { // Add returns the time t+d. func (t Time) Add(d Duration) Time { t.sec += int64(d / 1e9) - nsec := int32(t.nsec) + int32(d%1e9) + nsec := t.nsec + int32(d%1e9) if nsec >= 1e9 { t.sec++ nsec -= 1e9 @@ -623,7 +623,7 @@ func (t Time) Add(d Duration) Time { // will be returned. // To compute t-d for a duration d, use t.Add(-d). func (t Time) Sub(u Time) Duration { - d := Duration(t.sec-u.sec)*Second + Duration(int32(t.nsec)-int32(u.nsec)) + d := Duration(t.sec-u.sec)*Second + Duration(t.nsec-u.nsec) // Check for overflow or underflow. switch { case u.Add(d).Equal(t): @@ -1125,7 +1125,7 @@ func (t Time) Round(d Duration) Time { // but it's still here in case we change our minds. func div(t Time, d Duration) (qmod2 int, r Duration) { neg := false - nsec := int32(t.nsec) + nsec := t.nsec if t.sec < 0 { // Operate on absolute value. neg = true @@ -1159,7 +1159,7 @@ func div(t Time, d Duration) (qmod2 int, r Duration) { tmp := (sec >> 32) * 1e9 u1 := tmp >> 32 u0 := tmp << 32 - tmp = uint64(sec&0xFFFFFFFF) * 1e9 + tmp = (sec & 0xFFFFFFFF) * 1e9 u0x, u0 := u0, u0+tmp if u0 < u0x { u1++ diff --git a/src/time/zoneinfo_windows.go b/src/time/zoneinfo_windows.go index bcb8ccd563..c753119d5d 100644 --- a/src/time/zoneinfo_windows.go +++ b/src/time/zoneinfo_windows.go @@ -83,7 +83,7 @@ func extractCAPS(desc string) string { var short []rune for _, c := range desc { if 'A' <= c && c <= 'Z' { - short = append(short, rune(c)) + short = append(short, c) } } return string(short) diff --git a/src/unicode/letter.go b/src/unicode/letter.go index 8443ee51a2..ffa083eb57 100644 --- a/src/unicode/letter.go +++ b/src/unicode/letter.go @@ -217,7 +217,7 @@ func to(_case int, r rune, caseRange []CaseRange) rune { m := lo + (hi-lo)/2 cr := caseRange[m] if rune(cr.Lo) <= r && r <= rune(cr.Hi) { - delta := rune(cr.Delta[_case]) + delta := cr.Delta[_case] if delta > MaxRune { // In an Upper-Lower sequence, which always starts with // an UpperCase letter, the real deltas always look like: -- cgit v1.3 From 5183ad696c708ab5fc65006413019b1ef96aa91b Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Mon, 18 Apr 2016 16:42:17 +1000 Subject: debug/pe: add some documentation and TODO No code changes. Just moved ImportDirectory next to ImportedSymbols. And moved useless FormatError to the bottom of file.go. Updates #15345 Change-Id: I91ff243cefd18008b1c5ee9ec4326583deee431b Reviewed-on: https://go-review.googlesource.com/22182 Reviewed-by: David Crawshaw Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- src/debug/pe/file.go | 52 +++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) (limited to 'src/debug') diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index c68ff1bdce..bfc4cf8a18 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -59,16 +59,6 @@ type Symbol struct { StorageClass uint8 } -type ImportDirectory struct { - OriginalFirstThunk uint32 - TimeDateStamp uint32 - ForwarderChain uint32 - Name uint32 - FirstThunk uint32 - - dll string -} - // Data reads and returns the contents of the PE section. func (s *Section) Data() ([]byte, error) { dat := make([]byte, s.sr.Size()) @@ -82,21 +72,6 @@ func (s *Section) Data() ([]byte, error) { // Open returns a new ReadSeeker reading the PE section. func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) } -type FormatError struct { - off int64 - msg string - val interface{} -} - -func (e *FormatError) Error() string { - msg := e.msg - if e.val != nil { - msg += fmt.Sprintf(" '%v'", e.val) - } - msg += fmt.Sprintf(" in record at byte %#x", e.off) - return msg -} - // Open opens the named file using os.Open and prepares it for use as a PE binary. func Open(name string) (*File, error) { f, err := os.Open(name) @@ -320,6 +295,18 @@ func (f *File) DWARF() (*dwarf.Data, error) { return dwarf.New(abbrev, nil, nil, info, line, nil, ranges, str) } +// TODO(brainman): document ImportDirectory once we decide what to do with it. + +type ImportDirectory struct { + OriginalFirstThunk uint32 + TimeDateStamp uint32 + ForwarderChain uint32 + Name uint32 + FirstThunk uint32 + + dll string +} + // ImportedSymbols returns the names of all symbols // referred to by the binary f that are expected to be // satisfied by other libraries at dynamic load time. @@ -347,6 +334,12 @@ func (f *File) ImportedSymbols() ([]string, error) { } ida = append(ida, dt) } + // TODO(brainman): this needs to be rewritten + // ds.Data() return contets of .idata section. Why store in variable called "names"? + // Why we are retrieving it second time? We already have it in "d", and it is not modified anywhere. + // getString does not extracts a string from symbol string table (as getString doco says). + // Why ds.Data() called again and again in the loop? + // Needs test before rewrite. names, _ := ds.Data() var all []string for _, dt := range ida { @@ -395,3 +388,12 @@ func (f *File) ImportedLibraries() ([]string, error) { // cgo -dynimport don't use this for windows PE, so just return. return nil, nil } + +// FormatError is unused. +// The type is retained for compatibility. +type FormatError struct { +} + +func (e *FormatError) Error() string { + return "unknown error" +} -- cgit v1.3 From d697a9d5d7d75cecd8d49b95ed9a0d1f2f3e8ed4 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Mon, 18 Apr 2016 16:12:48 +1000 Subject: debug/pe: introduce StringTable type PE specification requires that long section and symbol names are stored in PE string table. Introduce StringTable that implements this functionality. Only string table reading is implemented. Updates #15345 Change-Id: Ib9638617f2ab1881ad707111d96fc68b0e47340e Reviewed-on: https://go-review.googlesource.com/22181 TryBot-Result: Gobot Gobot Reviewed-by: Alex Brainman --- src/debug/pe/file.go | 16 +++++++------ src/debug/pe/string.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 src/debug/pe/string.go (limited to 'src/debug') diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index bfc4cf8a18..f7b74e92a4 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -21,6 +21,7 @@ type File struct { OptionalHeader interface{} // of type *OptionalHeader32 or *OptionalHeader64 Sections []*Section Symbols []*Symbol + StringTable StringTable closer io.Closer } @@ -133,6 +134,14 @@ func NewFile(r io.ReaderAt) (*File, error) { return nil, errors.New("Invalid PE File Format.") } + var err error + + // Read string table. + f.StringTable, err = readStringTable(&f.FileHeader, sr) + if err != nil { + return nil, err + } + var ss []byte if f.FileHeader.NumberOfSymbols > 0 { // Get COFF string table, which is located at the end of the COFF symbol table. @@ -237,13 +246,6 @@ func NewFile(r io.ReaderAt) (*File, error) { return f, nil } -func cstring(b []byte) string { - var i int - for i = 0; i < len(b) && b[i] != 0; i++ { - } - return string(b[0:i]) -} - // getString extracts a string from symbol string table. func getString(section []byte, start int) (string, bool) { if start < 0 || start >= len(section) { diff --git a/src/debug/pe/string.go b/src/debug/pe/string.go new file mode 100644 index 0000000000..f0928d09c5 --- /dev/null +++ b/src/debug/pe/string.go @@ -0,0 +1,64 @@ +// Copyright 2016 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 pe + +import ( + "encoding/binary" + "fmt" + "io" +) + +// TODO(brainman): return error from cstring and see what errors we get and what to do about it + +// cstring converts ASCII byte sequence b to string. It stops once it finds 0. +func cstring(b []byte) string { + var i int + for i = 0; i < len(b) && b[i] != 0; i++ { + } + return string(b[:i]) +} + +// StringTable is a COFF string table. +type StringTable []byte + +func readStringTable(fh *FileHeader, r io.ReadSeeker) (StringTable, error) { + // COFF string table is located right after COFF symbol table. + offset := fh.PointerToSymbolTable + COFFSymbolSize*fh.NumberOfSymbols + _, err := r.Seek(int64(offset), io.SeekStart) + if err != nil { + return nil, fmt.Errorf("fail to seek to string table: %v", err) + } + var l uint32 + err = binary.Read(r, binary.LittleEndian, &l) + if err != nil { + return nil, fmt.Errorf("fail to read string table length: %v", err) + } + // string table length includes itself + if l <= 4 { + return nil, nil + } + l -= 4 + buf := make([]byte, l) + _, err = io.ReadFull(r, buf) + if err != nil { + return nil, fmt.Errorf("fail to read string table: %v", err) + } + return StringTable(buf), nil +} + +// TODO(brainman): decide if start parameter should be int instead of uint32 + +// String extracts string from COFF string table st at offset start. +func (st StringTable) String(start uint32) (string, error) { + // start includes 4 bytes of string table length + if start < 4 { + return "", fmt.Errorf("offset %d is before the start of string table", start) + } + start -= 4 + if int(start) > len(st) { + return "", fmt.Errorf("offset %d is beyond the end of string table", start) + } + return cstring(st[start:]), nil +} -- cgit v1.3 From 731531980a36f1fa6434c947c54daf8ba530a65f Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Wed, 20 Apr 2016 13:02:41 +1000 Subject: debug/pe: move some code into section.go and symbol.go Just moving code. No code changes. Updates #15345 Change-Id: I89c257b7aae4fbd78ce59a42909ecb3ff493659d Reviewed-on: https://go-review.googlesource.com/22300 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/debug/pe/file.go | 47 ------------------------------------- src/debug/pe/pe.go | 24 ------------------- src/debug/pe/section.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ src/debug/pe/symbol.go | 24 +++++++++++++++++++ 4 files changed, 85 insertions(+), 71 deletions(-) create mode 100644 src/debug/pe/section.go create mode 100644 src/debug/pe/symbol.go (limited to 'src/debug') diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index f7b74e92a4..3affd25185 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -26,53 +26,6 @@ type File struct { closer io.Closer } -type SectionHeader struct { - Name string - VirtualSize uint32 - VirtualAddress uint32 - Size uint32 - Offset uint32 - PointerToRelocations uint32 - PointerToLineNumbers uint32 - NumberOfRelocations uint16 - NumberOfLineNumbers uint16 - Characteristics uint32 -} - -type Section struct { - SectionHeader - - // Embed ReaderAt for ReadAt method. - // Do not embed SectionReader directly - // to avoid having Read and Seek. - // If a client wants Read and Seek it must use - // Open() to avoid fighting over the seek offset - // with other clients. - io.ReaderAt - sr *io.SectionReader -} - -type Symbol struct { - Name string - Value uint32 - SectionNumber int16 - Type uint16 - StorageClass uint8 -} - -// Data reads and returns the contents of the PE section. -func (s *Section) Data() ([]byte, error) { - dat := make([]byte, s.sr.Size()) - n, err := s.sr.ReadAt(dat, 0) - if n == len(dat) { - err = nil - } - return dat[0:n], err -} - -// Open returns a new ReadSeeker reading the PE section. -func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) } - // Open opens the named file using os.Open and prepares it for use as a PE binary. func Open(name string) (*File, error) { f, err := os.Open(name) diff --git a/src/debug/pe/pe.go b/src/debug/pe/pe.go index 6af40e2c78..8050d59c70 100644 --- a/src/debug/pe/pe.go +++ b/src/debug/pe/pe.go @@ -86,30 +86,6 @@ type OptionalHeader64 struct { DataDirectory [16]DataDirectory } -type SectionHeader32 struct { - Name [8]uint8 - VirtualSize uint32 - VirtualAddress uint32 - SizeOfRawData uint32 - PointerToRawData uint32 - PointerToRelocations uint32 - PointerToLineNumbers uint32 - NumberOfRelocations uint16 - NumberOfLineNumbers uint16 - Characteristics uint32 -} - -const COFFSymbolSize = 18 - -type COFFSymbol struct { - Name [8]uint8 - Value uint32 - SectionNumber int16 - Type uint16 - StorageClass uint8 - NumberOfAuxSymbols uint8 -} - const ( IMAGE_FILE_MACHINE_UNKNOWN = 0x0 IMAGE_FILE_MACHINE_AM33 = 0x1d3 diff --git a/src/debug/pe/section.go b/src/debug/pe/section.go new file mode 100644 index 0000000000..31cff272f3 --- /dev/null +++ b/src/debug/pe/section.go @@ -0,0 +1,61 @@ +// Copyright 2016 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 pe + +import ( + "io" +) + +type SectionHeader32 struct { + Name [8]uint8 + VirtualSize uint32 + VirtualAddress uint32 + SizeOfRawData uint32 + PointerToRawData uint32 + PointerToRelocations uint32 + PointerToLineNumbers uint32 + NumberOfRelocations uint16 + NumberOfLineNumbers uint16 + Characteristics uint32 +} + +type SectionHeader struct { + Name string + VirtualSize uint32 + VirtualAddress uint32 + Size uint32 + Offset uint32 + PointerToRelocations uint32 + PointerToLineNumbers uint32 + NumberOfRelocations uint16 + NumberOfLineNumbers uint16 + Characteristics uint32 +} + +type Section struct { + SectionHeader + + // Embed ReaderAt for ReadAt method. + // Do not embed SectionReader directly + // to avoid having Read and Seek. + // If a client wants Read and Seek it must use + // Open() to avoid fighting over the seek offset + // with other clients. + io.ReaderAt + sr *io.SectionReader +} + +// Data reads and returns the contents of the PE section. +func (s *Section) Data() ([]byte, error) { + dat := make([]byte, s.sr.Size()) + n, err := s.sr.ReadAt(dat, 0) + if n == len(dat) { + err = nil + } + return dat[0:n], err +} + +// Open returns a new ReadSeeker reading the PE section. +func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) } diff --git a/src/debug/pe/symbol.go b/src/debug/pe/symbol.go new file mode 100644 index 0000000000..559174838c --- /dev/null +++ b/src/debug/pe/symbol.go @@ -0,0 +1,24 @@ +// Copyright 2016 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 pe + +const COFFSymbolSize = 18 + +type COFFSymbol struct { + Name [8]uint8 + Value uint32 + SectionNumber int16 + Type uint16 + StorageClass uint8 + NumberOfAuxSymbols uint8 +} + +type Symbol struct { + Name string + Value uint32 + SectionNumber int16 + Type uint16 + StorageClass uint8 +} -- cgit v1.3 From 285a18436d480ef91b2af236d5ddd2fa7fa49de8 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Wed, 20 Apr 2016 15:58:55 +1000 Subject: debug/pe: pretty section.go code Introduce (*SectionHeader32).fullName and add documentation comments. Updates #15345 Change-Id: I8f3b8ab9492642d62e7aad010c91c68daea3f14b Reviewed-on: https://go-review.googlesource.com/22301 Reviewed-by: Ian Lance Taylor Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- src/debug/pe/file.go | 10 +++------- src/debug/pe/section.go | 27 ++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src/debug') diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index 3affd25185..73b7c1cba2 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -12,7 +12,6 @@ import ( "fmt" "io" "os" - "strconv" ) // A File represents an open PE file. @@ -172,12 +171,9 @@ func NewFile(r io.ReaderAt) (*File, error) { if err := binary.Read(sr, binary.LittleEndian, sh); err != nil { return nil, err } - var name string - if sh.Name[0] == '\x2F' { - si, _ := strconv.Atoi(cstring(sh.Name[1:])) - name, _ = getString(ss, si) - } else { - name = cstring(sh.Name[0:]) + name, err := sh.fullName(f.StringTable) + if err != nil { + return nil, err } s := new(Section) s.SectionHeader = SectionHeader{ diff --git a/src/debug/pe/section.go b/src/debug/pe/section.go index 31cff272f3..ded3ec4393 100644 --- a/src/debug/pe/section.go +++ b/src/debug/pe/section.go @@ -6,8 +6,10 @@ package pe import ( "io" + "strconv" ) +// SectionHeader32 represents real PE COFF section header. type SectionHeader32 struct { Name [8]uint8 VirtualSize uint32 @@ -21,6 +23,22 @@ type SectionHeader32 struct { Characteristics uint32 } +// fullName finds real name of section sh. Normally name is stored +// in sh.Name, but if it is longer then 8 characters, it is stored +// in COFF string table st instead. +func (sh *SectionHeader32) fullName(st StringTable) (string, error) { + if sh.Name[0] != '/' { + return cstring(sh.Name[:]), nil + } + i, err := strconv.Atoi(cstring(sh.Name[1:])) + if err != nil { + return "", err + } + return st.String(uint32(i)) +} + +// SectionHeader is similar to SectionHeader32 with Name +// field replaced by Go string. type SectionHeader struct { Name string VirtualSize uint32 @@ -34,6 +52,7 @@ type SectionHeader struct { Characteristics uint32 } +// Section provides access to PE COFF section. type Section struct { SectionHeader @@ -47,7 +66,7 @@ type Section struct { sr *io.SectionReader } -// Data reads and returns the contents of the PE section. +// Data reads and returns the contents of the PE section s. func (s *Section) Data() ([]byte, error) { dat := make([]byte, s.sr.Size()) n, err := s.sr.ReadAt(dat, 0) @@ -57,5 +76,7 @@ func (s *Section) Data() ([]byte, error) { return dat[0:n], err } -// Open returns a new ReadSeeker reading the PE section. -func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) } +// Open returns a new ReadSeeker reading the PE section s. +func (s *Section) Open() io.ReadSeeker { + return io.NewSectionReader(s.sr, 0, 1<<63-1) +} -- cgit v1.3 From 11f1041022e001869de076699f297b28d25fc558 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Thu, 21 Apr 2016 10:42:25 +1000 Subject: debug/pe: update cstring documentation Updates #15345 Change-Id: If1fca1f6042571cb0ac689bbb3c294309dd6e7b4 Reviewed-on: https://go-review.googlesource.com/22331 Reviewed-by: Ian Lance Taylor Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- src/debug/pe/string.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/debug') diff --git a/src/debug/pe/string.go b/src/debug/pe/string.go index f0928d09c5..e00bd97dd4 100644 --- a/src/debug/pe/string.go +++ b/src/debug/pe/string.go @@ -10,9 +10,8 @@ import ( "io" ) -// TODO(brainman): return error from cstring and see what errors we get and what to do about it - -// cstring converts ASCII byte sequence b to string. It stops once it finds 0. +// cstring converts ASCII byte sequence b to string. +// It stops once it finds 0 or reaches end of b. func cstring(b []byte) string { var i int for i = 0; i < len(b) && b[i] != 0; i++ { -- cgit v1.3 From 45522a6a93efe0fd487f6875f2b104d772a26469 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Thu, 21 Apr 2016 11:44:05 +1000 Subject: debug/pe: introduce Section.Relocs cmd/link reads PE object files when building programs with cgo. cmd/link accesses object relocations. Add new Section.Relocs that provides similar functionality in debug/pe. Updates #15345 Change-Id: I34de91b7f18cf1c9e4cdb3aedd685486a625ac92 Reviewed-on: https://go-review.googlesource.com/22332 TryBot-Result: Gobot Gobot Reviewed-by: Alex Brainman --- src/debug/pe/file.go | 8 ++++++++ src/debug/pe/section.go | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) (limited to 'src/debug') diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index 73b7c1cba2..cfd8e08a63 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -192,6 +192,14 @@ func NewFile(r io.ReaderAt) (*File, error) { s.ReaderAt = s.sr f.Sections[i] = s } + for i := range f.Sections { + var err error + f.Sections[i].Relocs, err = readRelocs(&f.Sections[i].SectionHeader, sr) + if err != nil { + return nil, err + } + } + return f, nil } diff --git a/src/debug/pe/section.go b/src/debug/pe/section.go index ded3ec4393..69fe41fd7a 100644 --- a/src/debug/pe/section.go +++ b/src/debug/pe/section.go @@ -5,6 +5,8 @@ package pe import ( + "encoding/binary" + "fmt" "io" "strconv" ) @@ -37,6 +39,30 @@ func (sh *SectionHeader32) fullName(st StringTable) (string, error) { return st.String(uint32(i)) } +// Reloc represents a PE COFF relocation. +// Each section contains its own relocation list. +type Reloc struct { + VirtualAddress uint32 + SymbolTableIndex uint32 + Type uint16 +} + +func readRelocs(sh *SectionHeader, r io.ReadSeeker) ([]Reloc, error) { + if sh.NumberOfRelocations <= 0 { + return nil, nil + } + _, err := r.Seek(int64(sh.PointerToRelocations), io.SeekStart) + if err != nil { + return nil, fmt.Errorf("fail to seek to %q section relocations: %v", sh.Name, err) + } + relocs := make([]Reloc, sh.NumberOfRelocations) + err = binary.Read(r, binary.LittleEndian, relocs) + if err != nil { + return nil, fmt.Errorf("fail to read section relocations: %v", err) + } + return relocs, nil +} + // SectionHeader is similar to SectionHeader32 with Name // field replaced by Go string. type SectionHeader struct { @@ -55,6 +81,7 @@ type SectionHeader struct { // Section provides access to PE COFF section. type Section struct { SectionHeader + Relocs []Reloc // Embed ReaderAt for ReadAt method. // Do not embed SectionReader directly -- cgit v1.3 From 687fe991e42f15fe1f491680c615ef96568f780a Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Thu, 21 Apr 2016 16:51:36 +1000 Subject: debug/pe: introduce File.COFFSymbols and (*COFFSymbol).FullName Reloc.SymbolTableIndex is an index into symbol table. But Reloc.SymbolTableIndex cannot be used as index into File.Symbols, because File.Symbols slice has Aux lines removed as it is built. We cannot change the way File.Symbols works, so I propose we introduce new File.COFFSymbols that does not have that limitation. Also unlike File.Symbols, File.COFFSymbols will consist of COFFSymbol. COFFSymbol matches PE COFF specification exactly, and it is simpler to use. Updates #15345 Change-Id: Icbc265853a472529cd6d64a76427b27e5459e373 Reviewed-on: https://go-review.googlesource.com/22336 Reviewed-by: David Crawshaw Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- src/debug/pe/file.go | 53 +++++++----------------------------- src/debug/pe/section.go | 2 ++ src/debug/pe/symbol.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 43 deletions(-) (limited to 'src/debug') diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index cfd8e08a63..abc33dfea8 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -19,7 +19,8 @@ type File struct { FileHeader OptionalHeader interface{} // of type *OptionalHeader32 or *OptionalHeader64 Sections []*Section - Symbols []*Symbol + Symbols []*Symbol // COFF symbols with auxiliary symbol records removed + COFFSymbols []COFFSymbol // all COFF symbols (including auxiliary symbol records) StringTable StringTable closer io.Closer @@ -94,48 +95,14 @@ func NewFile(r io.ReaderAt) (*File, error) { return nil, err } - var ss []byte - if f.FileHeader.NumberOfSymbols > 0 { - // Get COFF string table, which is located at the end of the COFF symbol table. - sr.Seek(int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols), io.SeekStart) - var l uint32 - if err := binary.Read(sr, binary.LittleEndian, &l); err != nil { - return nil, err - } - ss = make([]byte, l) - if _, err := r.ReadAt(ss, int64(f.FileHeader.PointerToSymbolTable+COFFSymbolSize*f.FileHeader.NumberOfSymbols)); err != nil { - return nil, err - } - - // Process COFF symbol table. - sr.Seek(int64(f.FileHeader.PointerToSymbolTable), io.SeekStart) - aux := uint8(0) - for i := 0; i < int(f.FileHeader.NumberOfSymbols); i++ { - cs := new(COFFSymbol) - if err := binary.Read(sr, binary.LittleEndian, cs); err != nil { - return nil, err - } - if aux > 0 { - aux-- - continue - } - var name string - if cs.Name[0] == 0 && cs.Name[1] == 0 && cs.Name[2] == 0 && cs.Name[3] == 0 { - si := int(binary.LittleEndian.Uint32(cs.Name[4:])) - name, _ = getString(ss, si) - } else { - name = cstring(cs.Name[:]) - } - aux = cs.NumberOfAuxSymbols - s := &Symbol{ - Name: name, - Value: cs.Value, - SectionNumber: cs.SectionNumber, - Type: cs.Type, - StorageClass: cs.StorageClass, - } - f.Symbols = append(f.Symbols, s) - } + // Read symbol table. + f.COFFSymbols, err = readCOFFSymbols(&f.FileHeader, sr) + if err != nil { + return nil, err + } + f.Symbols, err = removeAuxSymbols(f.COFFSymbols, f.StringTable) + if err != nil { + return nil, err } // Read optional header. diff --git a/src/debug/pe/section.go b/src/debug/pe/section.go index 69fe41fd7a..5d881577d3 100644 --- a/src/debug/pe/section.go +++ b/src/debug/pe/section.go @@ -39,6 +39,8 @@ func (sh *SectionHeader32) fullName(st StringTable) (string, error) { return st.String(uint32(i)) } +// TODO(brainman): copy all IMAGE_REL_* consts from ldpe.go here + // Reloc represents a PE COFF relocation. // Each section contains its own relocation list. type Reloc struct { diff --git a/src/debug/pe/symbol.go b/src/debug/pe/symbol.go index 559174838c..3cf5a101e7 100644 --- a/src/debug/pe/symbol.go +++ b/src/debug/pe/symbol.go @@ -4,8 +4,15 @@ package pe +import ( + "encoding/binary" + "fmt" + "io" +) + const COFFSymbolSize = 18 +// COFFSymbol represents single COFF symbol table record. type COFFSymbol struct { Name [8]uint8 Value uint32 @@ -15,6 +22,70 @@ type COFFSymbol struct { NumberOfAuxSymbols uint8 } +func readCOFFSymbols(fh *FileHeader, r io.ReadSeeker) ([]COFFSymbol, error) { + if fh.NumberOfSymbols <= 0 { + return nil, nil + } + _, err := r.Seek(int64(fh.PointerToSymbolTable), io.SeekStart) + if err != nil { + return nil, fmt.Errorf("fail to seek to symbol table: %v", err) + } + syms := make([]COFFSymbol, fh.NumberOfSymbols) + err = binary.Read(r, binary.LittleEndian, syms) + if err != nil { + return nil, fmt.Errorf("fail to read symbol table: %v", err) + } + return syms, nil +} + +// isSymNameOffset checks symbol name if it is encoded as offset into string table. +func isSymNameOffset(name [8]byte) (bool, uint32) { + if name[0] == 0 && name[1] == 0 && name[2] == 0 && name[3] == 0 { + return true, binary.LittleEndian.Uint32(name[4:]) + } + return false, 0 +} + +// FullName finds real name of symbol sym. Normally name is stored +// in sym.Name, but if it is longer then 8 characters, it is stored +// in COFF string table st instead. +func (sym *COFFSymbol) FullName(st StringTable) (string, error) { + if ok, offset := isSymNameOffset(sym.Name); ok { + return st.String(offset) + } + return cstring(sym.Name[:]), nil +} + +func removeAuxSymbols(allsyms []COFFSymbol, st StringTable) ([]*Symbol, error) { + if len(allsyms) == 0 { + return nil, nil + } + syms := make([]*Symbol, 0) + aux := uint8(0) + for _, sym := range allsyms { + if aux > 0 { + aux-- + continue + } + name, err := sym.FullName(st) + if err != nil { + return nil, err + } + aux = sym.NumberOfAuxSymbols + s := &Symbol{ + Name: name, + Value: sym.Value, + SectionNumber: sym.SectionNumber, + Type: sym.Type, + StorageClass: sym.StorageClass, + } + syms = append(syms, s) + } + return syms, nil +} + +// Symbol is similar to COFFSymbol with Name field replaced +// by Go string. Symbol also does not have NumberOfAuxSymbols. type Symbol struct { Name string Value uint32 -- cgit v1.3 From 093ac15a14137b4a9454442ae9fea282e5c09180 Mon Sep 17 00:00:00 2001 From: Alex Brainman Date: Sun, 24 Apr 2016 15:09:00 +1000 Subject: debug/pe: better error messages Updates #15345 Change-Id: Iae35d3e378cbc8157ba1ff91e4971ed4515a5e5c Reviewed-on: https://go-review.googlesource.com/22394 Reviewed-by: David Crawshaw Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- src/debug/pe/file.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src/debug') diff --git a/src/debug/pe/file.go b/src/debug/pe/file.go index abc33dfea8..1cd84d5727 100644 --- a/src/debug/pe/file.go +++ b/src/debug/pe/file.go @@ -8,7 +8,6 @@ package pe import ( "debug/dwarf" "encoding/binary" - "errors" "fmt" "io" "os" @@ -58,6 +57,8 @@ var ( 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. func NewFile(r io.ReaderAt) (*File, error) { f := new(File) @@ -73,7 +74,7 @@ func NewFile(r io.ReaderAt) (*File, error) { var sign [4]byte r.ReadAt(sign[:], signoff) 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 } else { @@ -83,8 +84,10 @@ func NewFile(r io.ReaderAt) (*File, error) { if err := binary.Read(sr, binary.LittleEndian, &f.FileHeader); err != nil { 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 { - return nil, errors.New("Invalid PE File Format.") + switch f.FileHeader.Machine { + 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 -- cgit v1.3