Skip to content

Commit 0816d38

Browse files
oridbianlancetaylor
authored andcommitted
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 <[email protected]> Reviewed-by: Benny Siegert <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 5b1658d commit 0816d38

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

src/debug/buildinfo/buildinfo.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"debug/elf"
1616
"debug/macho"
1717
"debug/pe"
18+
"debug/plan9obj"
1819
"encoding/binary"
1920
"errors"
2021
"fmt"
@@ -130,6 +131,12 @@ func readRawBuildInfo(r io.ReaderAt) (vers, mod string, err error) {
130131
return "", "", errUnrecognizedFormat
131132
}
132133
x = &xcoffExe{f}
134+
case hasPlan9Magic(ident):
135+
f, err := plan9obj.NewFile(r)
136+
if err != nil {
137+
return "", "", errUnrecognizedFormat
138+
}
139+
x = &plan9objExe{f}
133140
default:
134141
return "", "", errUnrecognizedFormat
135142
}
@@ -205,6 +212,17 @@ func readRawBuildInfo(r io.ReaderAt) (vers, mod string, err error) {
205212
return vers, mod, nil
206213
}
207214

215+
func hasPlan9Magic(magic []byte) bool {
216+
if len(magic) >= 4 {
217+
m := binary.BigEndian.Uint32(magic)
218+
switch m {
219+
case plan9obj.Magic386, plan9obj.MagicAMD64, plan9obj.MagicARM:
220+
return true
221+
}
222+
}
223+
return false
224+
}
225+
208226
func decodeString(data []byte) (s string, rest []byte) {
209227
u, n := binary.Uvarint(data)
210228
if n <= 0 || u >= uint64(len(data)-n) {
@@ -389,7 +407,7 @@ func (x *xcoffExe) ReadData(addr, size uint64) ([]byte, error) {
389407
return data, nil
390408
}
391409
}
392-
return nil, fmt.Errorf("address not mapped")
410+
return nil, errors.New("address not mapped")
393411
}
394412

395413
func (x *xcoffExe) DataStart() uint64 {
@@ -398,3 +416,34 @@ func (x *xcoffExe) DataStart() uint64 {
398416
}
399417
return 0
400418
}
419+
420+
// plan9objExe is the Plan 9 a.out implementation of the exe interface.
421+
type plan9objExe struct {
422+
f *plan9obj.File
423+
}
424+
425+
func (x *plan9objExe) DataStart() uint64 {
426+
if s := x.f.Section("data"); s != nil {
427+
return uint64(s.Offset)
428+
}
429+
return 0
430+
}
431+
432+
func (x *plan9objExe) ReadData(addr, size uint64) ([]byte, error) {
433+
for _, sect := range x.f.Sections {
434+
if uint64(sect.Offset) <= addr && addr <= uint64(sect.Offset+sect.Size-1) {
435+
n := uint64(sect.Offset+sect.Size) - addr
436+
if n > size {
437+
n = size
438+
}
439+
data := make([]byte, n)
440+
_, err := sect.ReadAt(data, int64(addr-uint64(sect.Offset)))
441+
if err != nil {
442+
return nil, err
443+
}
444+
return data, nil
445+
}
446+
}
447+
return nil, errors.New("address not mapped")
448+
449+
}

0 commit comments

Comments
 (0)