]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/pprof: fix addr calculation for Windows
authorEgon Elbre <egonelbre@gmail.com>
Mon, 11 Jul 2022 13:12:41 +0000 (16:12 +0300)
committerAlex Brainman <alex.brainman@gmail.com>
Sat, 20 Aug 2022 00:56:30 +0000 (00:56 +0000)
This makes it possible to use `disasm` with ASLR windows binaries.

For #46639

Change-Id: I08aff38dc0b33fdfb07e0206766db066e33207d1
Reviewed-on: https://go-review.googlesource.com/c/go/+/416976
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/internal/objfile/pe.go
src/cmd/pprof/pprof.go
src/cmd/pprof/pprof_test.go

index 9088866fcf5326f9b84757bf72aa582acbc77096..4c4be1e6b7c04c95873e2d0c88efe974428bfae1 100644 (file)
@@ -31,13 +31,7 @@ func (f *peFile) symbols() ([]Sym, error) {
        // We infer the size of a symbol by looking at where the next symbol begins.
        var addrs []uint64
 
-       var imageBase uint64
-       switch oh := f.pe.OptionalHeader.(type) {
-       case *pe.OptionalHeader32:
-               imageBase = uint64(oh.ImageBase)
-       case *pe.OptionalHeader64:
-               imageBase = oh.ImageBase
-       }
+       imageBase, _ := f.imageBase()
 
        var syms []Sym
        for _, s := range f.pe.Symbols {
@@ -96,15 +90,11 @@ func (f *peFile) symbols() ([]Sym, error) {
 }
 
 func (f *peFile) pcln() (textStart uint64, symtab, pclntab []byte, err error) {
-       var imageBase uint64
-       switch oh := f.pe.OptionalHeader.(type) {
-       case *pe.OptionalHeader32:
-               imageBase = uint64(oh.ImageBase)
-       case *pe.OptionalHeader64:
-               imageBase = oh.ImageBase
-       default:
-               return 0, nil, nil, fmt.Errorf("pe file format not recognized")
+       imageBase, err := f.imageBase()
+       if err != nil {
+               return 0, nil, nil, err
        }
+
        if sect := f.pe.Section(".text"); sect != nil {
                textStart = imageBase + uint64(sect.VirtualAddress)
        }
@@ -127,15 +117,11 @@ func (f *peFile) pcln() (textStart uint64, symtab, pclntab []byte, err error) {
 }
 
 func (f *peFile) text() (textStart uint64, text []byte, err error) {
-       var imageBase uint64
-       switch oh := f.pe.OptionalHeader.(type) {
-       case *pe.OptionalHeader32:
-               imageBase = uint64(oh.ImageBase)
-       case *pe.OptionalHeader64:
-               imageBase = oh.ImageBase
-       default:
-               return 0, nil, fmt.Errorf("pe file format not recognized")
+       imageBase, err := f.imageBase()
+       if err != nil {
+               return 0, nil, err
        }
+
        sect := f.pe.Section(".text")
        if sect == nil {
                return 0, nil, fmt.Errorf("text section not found")
@@ -197,7 +183,18 @@ func (f *peFile) goarch() string {
 }
 
 func (f *peFile) loadAddress() (uint64, error) {
-       return 0, fmt.Errorf("unknown load address")
+       return f.imageBase()
+}
+
+func (f *peFile) imageBase() (uint64, error) {
+       switch oh := f.pe.OptionalHeader.(type) {
+       case *pe.OptionalHeader32:
+               return uint64(oh.ImageBase), nil
+       case *pe.OptionalHeader64:
+               return oh.ImageBase, nil
+       default:
+               return 0, fmt.Errorf("pe file format not recognized")
+       }
 }
 
 func (f *peFile) dwarf() (*dwarf.Data, error) {
index c073c964b42af0b5060e92ee74cc7204182dab9e..147b3ad41862c5ddb1117e2b51204e9b78739e2b 100644 (file)
@@ -233,8 +233,7 @@ func (f *file) Name() string {
 }
 
 func (f *file) ObjAddr(addr uint64) (uint64, error) {
-       // No support for shared libraries, so translation is a no-op.
-       return addr, nil
+       return addr - f.offset, nil
 }
 
 func (f *file) BuildID() string {
index 9a37b972864115546ea153e88d4b8f2d63a6d2ab..e001975f83196bb74b9dd537fe3c7e3170716e2d 100644 (file)
@@ -83,9 +83,6 @@ func mustHaveDisasm(t *testing.T) {
        }
 
        // Skip PIE platforms, pprof can't disassemble PIE.
-       if runtime.GOOS == "windows" {
-               t.Skipf("skipping on %s, issue 46639", runtime.GOOS)
-       }
        if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
                t.Skipf("skipping on %s/%s, issue 46639", runtime.GOOS, runtime.GOARCH)
        }