diff options
author | Tianon Gravi <admwiggin@gmail.com> | 2015-01-15 11:54:00 -0700 |
---|---|---|
committer | Tianon Gravi <admwiggin@gmail.com> | 2015-01-15 11:54:00 -0700 |
commit | f154da9e12608589e8d5f0508f908a0c3e88a1bb (patch) | |
tree | f8255d51e10c6f1e0ed69702200b966c9556a431 /src/pkg/debug/plan9obj | |
parent | 8d8329ed5dfb9622c82a9fbec6fd99a580f9c9f6 (diff) | |
download | golang-upstream/1.4.tar.gz |
Imported Upstream version 1.4upstream/1.4
Diffstat (limited to 'src/pkg/debug/plan9obj')
-rw-r--r-- | src/pkg/debug/plan9obj/file.go | 325 | ||||
-rw-r--r-- | src/pkg/debug/plan9obj/file_test.go | 81 | ||||
-rw-r--r-- | src/pkg/debug/plan9obj/plan9obj.go | 36 | ||||
-rwxr-xr-x | src/pkg/debug/plan9obj/testdata/386-plan9-exec | bin | 37232 -> 0 bytes | |||
-rwxr-xr-x | src/pkg/debug/plan9obj/testdata/amd64-plan9-exec | bin | 34279 -> 0 bytes | |||
-rw-r--r-- | src/pkg/debug/plan9obj/testdata/hello.c | 8 |
6 files changed, 0 insertions, 450 deletions
diff --git a/src/pkg/debug/plan9obj/file.go b/src/pkg/debug/plan9obj/file.go deleted file mode 100644 index 60a585719..000000000 --- a/src/pkg/debug/plan9obj/file.go +++ /dev/null @@ -1,325 +0,0 @@ -// Copyright 2014 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 plan9obj implements access to Plan 9 a.out object files. -package plan9obj - -import ( - "encoding/binary" - "errors" - "fmt" - "io" - "os" -) - -// A FileHeader represents a Plan 9 a.out file header. -type FileHeader struct { - Magic uint32 - Bss uint32 - Entry uint64 - PtrSize int -} - -// A File represents an open Plan 9 a.out file. -type File struct { - FileHeader - Sections []*Section - closer io.Closer -} - -// A SectionHeader represents a single Plan 9 a.out section header. -// This structure doesn't exist on-disk, but eases navigation -// through the object file. -type SectionHeader struct { - Name string - Size uint32 - Offset uint32 -} - -// A Section represents a single section in a Plan 9 a.out file. -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 Plan 9 a.out 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 Plan 9 a.out section. -func (s *Section) Open() io.ReadSeeker { return io.NewSectionReader(s.sr, 0, 1<<63-1) } - -// A Symbol represents an entry in a Plan 9 a.out symbol table section. -type Sym struct { - Value uint64 - Type rune - Name string -} - -/* - * Plan 9 a.out reader - */ - -// formatError is returned by some operations if the data does -// not have the correct format for an object file. -type formatError struct { - off int - 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 Plan 9 a.out binary. -func Open(name string) (*File, error) { - f, err := os.Open(name) - if err != nil { - return nil, err - } - ff, err := NewFile(f) - if err != nil { - f.Close() - return nil, err - } - ff.closer = f - return ff, nil -} - -// Close closes the File. -// If the File was created using NewFile directly instead of Open, -// Close has no effect. -func (f *File) Close() error { - var err error - if f.closer != nil { - err = f.closer.Close() - f.closer = nil - } - return err -} - -func parseMagic(magic []byte) (uint32, error) { - m := binary.BigEndian.Uint32(magic) - switch m { - case Magic386, MagicAMD64, MagicARM: - return m, nil - } - return 0, &formatError{0, "bad magic number", magic} -} - -// NewFile creates a new File for accessing a Plan 9 binary in an underlying reader. -// The Plan 9 binary is expected to start at position 0 in the ReaderAt. -func NewFile(r io.ReaderAt) (*File, error) { - sr := io.NewSectionReader(r, 0, 1<<63-1) - // Read and decode Plan 9 magic - var magic [4]byte - if _, err := r.ReadAt(magic[:], 0); err != nil { - return nil, err - } - _, err := parseMagic(magic[:]) - if err != nil { - return nil, err - } - - ph := new(prog) - if err := binary.Read(sr, binary.BigEndian, ph); err != nil { - return nil, err - } - - f := &File{FileHeader: FileHeader{ - Magic: ph.Magic, - Bss: ph.Bss, - Entry: uint64(ph.Entry), - PtrSize: 4, - }} - - hdrSize := 4 * 8 - - if ph.Magic&Magic64 != 0 { - if err := binary.Read(sr, binary.BigEndian, &f.Entry); err != nil { - return nil, err - } - f.PtrSize = 8 - hdrSize += 8 - } - - var sects = []struct { - name string - size uint32 - }{ - {"text", ph.Text}, - {"data", ph.Data}, - {"syms", ph.Syms}, - {"spsz", ph.Spsz}, - {"pcsz", ph.Pcsz}, - } - - f.Sections = make([]*Section, 5) - - off := uint32(hdrSize) - - for i, sect := range sects { - s := new(Section) - s.SectionHeader = SectionHeader{ - Name: sect.name, - Size: sect.size, - Offset: off, - } - off += sect.size - s.sr = io.NewSectionReader(r, int64(s.Offset), int64(s.Size)) - s.ReaderAt = s.sr - f.Sections[i] = s - } - - return f, nil -} - -func walksymtab(data []byte, ptrsz int, fn func(sym) error) error { - var order binary.ByteOrder = binary.BigEndian - var s sym - p := data - for len(p) >= 4 { - // Symbol type, value. - if len(p) < ptrsz { - return &formatError{len(data), "unexpected EOF", nil} - } - // fixed-width value - if ptrsz == 8 { - s.value = order.Uint64(p[0:8]) - p = p[8:] - } else { - s.value = uint64(order.Uint32(p[0:4])) - p = p[4:] - } - - var typ byte - typ = p[0] & 0x7F - s.typ = typ - p = p[1:] - - // Name. - var i int - var nnul int - for i = 0; i < len(p); i++ { - if p[i] == 0 { - nnul = 1 - break - } - } - switch typ { - case 'z', 'Z': - p = p[i+nnul:] - for i = 0; i+2 <= len(p); i += 2 { - if p[i] == 0 && p[i+1] == 0 { - nnul = 2 - break - } - } - } - if len(p) < i+nnul { - return &formatError{len(data), "unexpected EOF", nil} - } - s.name = p[0:i] - i += nnul - p = p[i:] - - fn(s) - } - return nil -} - -// NewTable decodes the Go symbol table in data, -// returning an in-memory representation. -func newTable(symtab []byte, ptrsz int) ([]Sym, error) { - var n int - err := walksymtab(symtab, ptrsz, func(s sym) error { - n++ - return nil - }) - if err != nil { - return nil, err - } - - fname := make(map[uint16]string) - syms := make([]Sym, 0, n) - err = walksymtab(symtab, ptrsz, func(s sym) error { - n := len(syms) - syms = syms[0 : n+1] - ts := &syms[n] - ts.Type = rune(s.typ) - ts.Value = s.value - switch s.typ { - default: - ts.Name = string(s.name[:]) - case 'z', 'Z': - for i := 0; i < len(s.name); i += 2 { - eltIdx := binary.BigEndian.Uint16(s.name[i : i+2]) - elt, ok := fname[eltIdx] - if !ok { - return &formatError{-1, "bad filename code", eltIdx} - } - if n := len(ts.Name); n > 0 && ts.Name[n-1] != '/' { - ts.Name += "/" - } - ts.Name += elt - } - } - switch s.typ { - case 'f': - fname[uint16(s.value)] = ts.Name - } - return nil - }) - if err != nil { - return nil, err - } - - return syms, nil -} - -// Symbols returns the symbol table for f. -func (f *File) Symbols() ([]Sym, error) { - symtabSection := f.Section("syms") - if symtabSection == nil { - return nil, errors.New("no symbol section") - } - - symtab, err := symtabSection.Data() - if err != nil { - return nil, errors.New("cannot load symbol section") - } - - return newTable(symtab, f.PtrSize) -} - -// Section returns a section with the given name, or nil if no such -// section exists. -func (f *File) Section(name string) *Section { - for _, s := range f.Sections { - if s.Name == name { - return s - } - } - return nil -} diff --git a/src/pkg/debug/plan9obj/file_test.go b/src/pkg/debug/plan9obj/file_test.go deleted file mode 100644 index 96186d815..000000000 --- a/src/pkg/debug/plan9obj/file_test.go +++ /dev/null @@ -1,81 +0,0 @@ -// Copyright 2014 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 plan9obj - -import ( - "reflect" - "testing" -) - -type fileTest struct { - file string - hdr FileHeader - sections []*SectionHeader -} - -var fileTests = []fileTest{ - { - "testdata/386-plan9-exec", - FileHeader{Magic386, 0x324, 0x14, 4}, - []*SectionHeader{ - {"text", 0x4c5f, 0x20}, - {"data", 0x94c, 0x4c7f}, - {"syms", 0x2c2b, 0x55cb}, - {"spsz", 0x0, 0x81f6}, - {"pcsz", 0xf7a, 0x81f6}, - }, - }, - { - "testdata/amd64-plan9-exec", - FileHeader{MagicAMD64, 0x618, 0x13, 8}, - []*SectionHeader{ - {"text", 0x4213, 0x28}, - {"data", 0xa80, 0x423b}, - {"syms", 0x2c8c, 0x4cbb}, - {"spsz", 0x0, 0x7947}, - {"pcsz", 0xca0, 0x7947}, - }, - }, -} - -func TestOpen(t *testing.T) { - for i := range fileTests { - tt := &fileTests[i] - - f, err := Open(tt.file) - if err != nil { - t.Error(err) - continue - } - if !reflect.DeepEqual(f.FileHeader, tt.hdr) { - t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr) - continue - } - - for i, sh := range f.Sections { - if i >= len(tt.sections) { - break - } - have := &sh.SectionHeader - want := tt.sections[i] - if !reflect.DeepEqual(have, want) { - t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want) - } - } - tn := len(tt.sections) - fn := len(f.Sections) - if tn != fn { - t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn) - } - } -} - -func TestOpenFailure(t *testing.T) { - filename := "file.go" // not a Plan 9 a.out file - _, err := Open(filename) // don't crash - if err == nil { - t.Errorf("open %s: succeeded unexpectedly", filename) - } -} diff --git a/src/pkg/debug/plan9obj/plan9obj.go b/src/pkg/debug/plan9obj/plan9obj.go deleted file mode 100644 index af9858562..000000000 --- a/src/pkg/debug/plan9obj/plan9obj.go +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2014 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. - -/* - * Plan 9 a.out constants and data structures - */ - -package plan9obj - -// Plan 9 Program header. -type prog struct { - Magic uint32 /* magic number */ - Text uint32 /* size of text segment */ - Data uint32 /* size of initialized data */ - Bss uint32 /* size of uninitialized data */ - Syms uint32 /* size of symbol table */ - Entry uint32 /* entry point */ - Spsz uint32 /* size of pc/sp offset table */ - Pcsz uint32 /* size of pc/line number table */ -} - -// Plan 9 symbol table entries. -type sym struct { - value uint64 - typ byte - name []byte -} - -const ( - Magic64 = 0x8000 // 64-bit expanded header - - Magic386 = (4*11+0)*11 + 7 - MagicAMD64 = (4*26+0)*26 + 7 + Magic64 - MagicARM = (4*20+0)*20 + 7 -) diff --git a/src/pkg/debug/plan9obj/testdata/386-plan9-exec b/src/pkg/debug/plan9obj/testdata/386-plan9-exec Binary files differdeleted file mode 100755 index 748e83f8e..000000000 --- a/src/pkg/debug/plan9obj/testdata/386-plan9-exec +++ /dev/null diff --git a/src/pkg/debug/plan9obj/testdata/amd64-plan9-exec b/src/pkg/debug/plan9obj/testdata/amd64-plan9-exec Binary files differdeleted file mode 100755 index 3e257dd8f..000000000 --- a/src/pkg/debug/plan9obj/testdata/amd64-plan9-exec +++ /dev/null diff --git a/src/pkg/debug/plan9obj/testdata/hello.c b/src/pkg/debug/plan9obj/testdata/hello.c deleted file mode 100644 index c0d633e29..000000000 --- a/src/pkg/debug/plan9obj/testdata/hello.c +++ /dev/null @@ -1,8 +0,0 @@ -#include <u.h> -#include <libc.h> - -void -main(void) -{ - print("hello, world\n"); -} |