@@ -15,6 +15,7 @@ import (
15
15
"debug/elf"
16
16
"debug/macho"
17
17
"debug/pe"
18
+ "debug/plan9obj"
18
19
"encoding/binary"
19
20
"errors"
20
21
"fmt"
@@ -130,6 +131,12 @@ func readRawBuildInfo(r io.ReaderAt) (vers, mod string, err error) {
130
131
return "" , "" , errUnrecognizedFormat
131
132
}
132
133
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 }
133
140
default :
134
141
return "" , "" , errUnrecognizedFormat
135
142
}
@@ -205,6 +212,17 @@ func readRawBuildInfo(r io.ReaderAt) (vers, mod string, err error) {
205
212
return vers , mod , nil
206
213
}
207
214
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
+
208
226
func decodeString (data []byte ) (s string , rest []byte ) {
209
227
u , n := binary .Uvarint (data )
210
228
if n <= 0 || u >= uint64 (len (data )- n ) {
@@ -389,7 +407,7 @@ func (x *xcoffExe) ReadData(addr, size uint64) ([]byte, error) {
389
407
return data , nil
390
408
}
391
409
}
392
- return nil , fmt . Errorf ("address not mapped" )
410
+ return nil , errors . New ("address not mapped" )
393
411
}
394
412
395
413
func (x * xcoffExe ) DataStart () uint64 {
@@ -398,3 +416,34 @@ func (x *xcoffExe) DataStart() uint64 {
398
416
}
399
417
return 0
400
418
}
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