From 0816d3871376f8fe029db1a6d00b1f6882106043 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sun, 17 Jul 2022 12:02:30 -0400 Subject: [PATCH] debug/buildinfo: implement for Plan 9 a.out Plan 9 a.out was not implemented for debug/buildinfo, which was causing test failures on Plan 9. This adds an implementation, and causes the tests to pass. Fixes #53949 Change-Id: I90a307ef9babf8cf381f8746d731cac2206b234a Reviewed-on: https://go-review.googlesource.com/c/go/+/418014 Reviewed-by: Ian Lance Taylor Reviewed-by: Benny Siegert Run-TryBot: Ian Lance Taylor Auto-Submit: Ian Lance Taylor --- src/debug/buildinfo/buildinfo.go | 51 +++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/debug/buildinfo/buildinfo.go b/src/debug/buildinfo/buildinfo.go index d1f4892751..3dbe3fe41d 100644 --- a/src/debug/buildinfo/buildinfo.go +++ b/src/debug/buildinfo/buildinfo.go @@ -15,6 +15,7 @@ import ( "debug/elf" "debug/macho" "debug/pe" + "debug/plan9obj" "encoding/binary" "errors" "fmt" @@ -130,6 +131,12 @@ func readRawBuildInfo(r io.ReaderAt) (vers, mod string, err error) { return "", "", errUnrecognizedFormat } x = &xcoffExe{f} + case hasPlan9Magic(ident): + f, err := plan9obj.NewFile(r) + if err != nil { + return "", "", errUnrecognizedFormat + } + x = &plan9objExe{f} default: return "", "", errUnrecognizedFormat } @@ -205,6 +212,17 @@ func readRawBuildInfo(r io.ReaderAt) (vers, mod string, err error) { return vers, mod, nil } +func hasPlan9Magic(magic []byte) bool { + if len(magic) >= 4 { + m := binary.BigEndian.Uint32(magic) + switch m { + case plan9obj.Magic386, plan9obj.MagicAMD64, plan9obj.MagicARM: + return true + } + } + return false +} + func decodeString(data []byte) (s string, rest []byte) { u, n := binary.Uvarint(data) if n <= 0 || u >= uint64(len(data)-n) { @@ -389,7 +407,7 @@ func (x *xcoffExe) ReadData(addr, size uint64) ([]byte, error) { return data, nil } } - return nil, fmt.Errorf("address not mapped") + return nil, errors.New("address not mapped") } func (x *xcoffExe) DataStart() uint64 { @@ -398,3 +416,34 @@ func (x *xcoffExe) DataStart() uint64 { } return 0 } + +// plan9objExe is the Plan 9 a.out implementation of the exe interface. +type plan9objExe struct { + f *plan9obj.File +} + +func (x *plan9objExe) DataStart() uint64 { + if s := x.f.Section("data"); s != nil { + return uint64(s.Offset) + } + return 0 +} + +func (x *plan9objExe) ReadData(addr, size uint64) ([]byte, error) { + for _, sect := range x.f.Sections { + if uint64(sect.Offset) <= addr && addr <= uint64(sect.Offset+sect.Size-1) { + n := uint64(sect.Offset+sect.Size) - addr + if n > size { + n = size + } + data := make([]byte, n) + _, err := sect.ReadAt(data, int64(addr-uint64(sect.Offset))) + if err != nil { + return nil, err + } + return data, nil + } + } + return nil, errors.New("address not mapped") + +} -- 2.50.0