Skip to content

Commit

Permalink
Merge pull request #29 from goretk/fix/text-section-correction-factor
Browse files Browse the repository at this point in the history
Add text section correction factor
  • Loading branch information
TcM1911 authored Apr 23, 2023
2 parents 058070b + e65dc82 commit cd6a158
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions r2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"encoding/base64"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -228,6 +229,15 @@ func initAnal() {
return
}

var correction uint64
if file.FileInfo.OS == "windows" {
textStart := getFileSectionAddress(r2, ".text")
correction = findAddressCorrection(file, textStart)
if correction != 0 {
fmt.Printf("PE .text section and Go runtime mismatch. Using address correction 0x%x.\n", correction)
}
}

// Vendors, stdlib and unknown have now been populated so we can ignore the err check.
vendors, _ := file.GetVendors()
std, _ := file.GetSTDLib()
Expand All @@ -240,7 +250,7 @@ func initAnal() {
pkgs = append(pkgs, generated...)

fmt.Printf("%d packages found.\n", len(pkgs))
applyFuncSymbols(pkgs, r2)
applyFuncSymbols(pkgs, r2, correction)

// Analyze init and main
fmt.Println("Analyzing all init functions.")
Expand All @@ -265,7 +275,19 @@ func initAnal() {
fmt.Printf("%d type symbols found\n", count)
}

func applyFuncSymbols(pkgs []*gore.Package, r2 *r2g2.Client) {
func findAddressCorrection(file *gore.GoFile, headerTextAddress uint64) uint64 {
modData, err := file.Moduledata()
if err != nil {
return 0
}
mtxt := modData.Text().Address
if headerTextAddress >= mtxt {
return 0
}
return mtxt - headerTextAddress
}

func applyFuncSymbols(pkgs []*gore.Package, r2 *r2g2.Client, correction uint64) {
count := 0
for _, p := range pkgs {
for _, f := range p.Functions {
Expand All @@ -274,7 +296,7 @@ func applyFuncSymbols(pkgs []*gore.Package, r2 *r2g2.Client) {
}
r2.NewFlagWithLength(
"fcn."+cleanupName(p.Name)+"."+cleanupName(f.Name),
f.Offset,
f.Offset+correction,
f.End-f.Offset)
count++
}
Expand All @@ -284,7 +306,7 @@ func applyFuncSymbols(pkgs []*gore.Package, r2 *r2g2.Client) {
}
r2.NewFlagWithLength(
"fcn."+cleanupName(p.Name)+"#"+cleanupName(m.Receiver)+"."+cleanupName(m.Name),
m.Offset,
m.Offset+correction,
m.End-m.Offset)
count++
}
Expand Down Expand Up @@ -402,3 +424,32 @@ func srcLineInfo(r2 *r2g2.Client, file *gore.GoFile, useComment bool) {
r2.Run("fs *")
}
}

func getFileSectionAddress(r2 *r2g2.Client, name string) uint64 {
data, err := r2.Run("iSj")
if err != nil {
return 0
}

var sections []struct {
Name string `json:"name"`
Size uint64 `json:"size"`
VSize uint64 `json:"vsize"`
Permissions string `json:"perm"`
PAddr uint64 `json:"paddr"`
VAddr uint64 `json:"vaddr"`
}

err = json.Unmarshal(data, &sections)
if err != nil {
return 0
}

for _, s := range sections {
if s.Name == name {
return s.VAddr
}
}

return 0
}

0 comments on commit cd6a158

Please sign in to comment.