Skip to content

Commit

Permalink
move inlinedCall into package obj
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujhd committed Feb 8, 2022
1 parent b62c3ed commit 8ff7f0c
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 59 deletions.
1 change: 0 additions & 1 deletion const.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const (
UInt64Size = int(unsafe.Sizeof(uint64(0)))
_FuncSize = int(unsafe.Offsetof(_func{}.nfuncdata)) + int(unsafe.Sizeof(_func{}.nfuncdata))
FindFuncBucketSize = int(unsafe.Sizeof(findfuncbucket{}))
InlinedCallSize = int(unsafe.Sizeof(inlinedCall{}))
InvalidHandleValue = ^uintptr(0)
InvalidOffset = int(-1)
PageSize = 1 << 12 //4096
Expand Down
4 changes: 4 additions & 0 deletions func.1.12.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ func getfuncname(f *_func, md *moduledata) string {
}
return gostringnocopy(&(md.pclntable[f.nameoff]))
}

func getfuncID(f *_func) uint8 {
return uint8(f.funcID)
}
4 changes: 4 additions & 0 deletions func.1.16.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ func getfuncname(f *_func, md *moduledata) string {
}
return gostringnocopy(&(md.funcnametab[f.nameoff]))
}

func getfuncID(f *_func) uint8 {
return uint8(f.funcID)
}
4 changes: 4 additions & 0 deletions func.1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,7 @@ func getfuncname(f *_func, md *moduledata) string {
}
return gostringnocopy(&(md.funcnametab[f.nameoff]))
}

func getfuncID(f *_func) uint8 {
return uint8(f.funcID)
}
4 changes: 4 additions & 0 deletions func.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,7 @@ func getfuncname(f *_func, md *moduledata) string {
}
return gostringnocopy(&(md.pclntable[f.nameoff]))
}

func getfuncID(f *_func) uint8 {
return 0
}
12 changes: 0 additions & 12 deletions inlinetree.1.8.go

This file was deleted.

19 changes: 3 additions & 16 deletions inlinetree.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build go1.9 && !go1.19
// +build go1.9,!go1.19

package goloader

import (
Expand All @@ -10,16 +7,6 @@ import (
"github.com/pkujhd/goloader/objabi/dataindex"
)

func findFileTab(linker *Linker, filename string) int32 {
tab := linker.namemap[filename]
for index, value := range linker.filetab {
if uint32(tab) == value {
return int32(index)
}
}
return -1
}

func (linker *Linker) addInlineTree(_func *_func, symbol *obj.ObjSymbol) (err error) {
funcname := symbol.Name
Func := symbol.Func
Expand All @@ -40,10 +27,10 @@ func (linker *Linker) addInlineTree(_func *_func, symbol *obj.ObjSymbol) (err er
}
}

bytes := make([]byte, len(Func.InlTree)*InlinedCallSize)
bytes := make([]byte, len(Func.InlTree)*obj.InlinedCallSize)
for k, inl := range Func.InlTree {
inlinedcall := linker.initInlinedCall(inl, _func)
copy2Slice(bytes[k*InlinedCallSize:], uintptr(unsafe.Pointer(&inlinedcall)), InlinedCallSize)
inlinedcall := obj.InitInlinedCall(inl, getfuncID(_func), linker.namemap, linker.filetab)
copy2Slice(bytes[k*obj.InlinedCallSize:], uintptr(unsafe.Pointer(&inlinedcall)), obj.InlinedCallSize)
}
offset := len(linker.noptrdata)
linker.noptrdata = append(linker.noptrdata, bytes...)
Expand Down
13 changes: 9 additions & 4 deletions obj/const.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package obj

import (
"unsafe"
)

const (
InvalidOffset = int(-1)
InvalidIndex = uint32(0xFFFFFFFF)
EmptyPkgPath = `""`
EmptyString = ""
InvalidOffset = int(-1)
InvalidIndex = uint32(0xFFFFFFFF)
InlinedCallSize = int(unsafe.Sizeof(InlinedCall{}))
EmptyPkgPath = `""`
EmptyString = ""
)
18 changes: 8 additions & 10 deletions inlinetree.1.12.go → obj/inlinedcall.1.12.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//go:build go1.12 && !go1.19
// +build go1.12,!go1.19

package goloader
package obj

import (
"github.com/pkujhd/goloader/obj"
)
type funcID uint8

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
type inlinedCall struct {
type InlinedCall struct {
parent int16 // index of parent in the inltree, or < 0
funcID funcID // type of the called function
_ byte
Expand All @@ -18,12 +16,12 @@ type inlinedCall struct {
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
}

func (linker *Linker) initInlinedCall(inl obj.InlTreeNode, _func *_func) inlinedCall {
return inlinedCall{
func InitInlinedCall(inl InlTreeNode, funcid uint8, namemap map[string]int, filetab []uint32) InlinedCall {
return InlinedCall{
parent: int16(inl.Parent),
funcID: _func.funcID,
file: findFileTab(linker, inl.File),
funcID: funcID(funcid),
file: findFileTab(inl.File, namemap, filetab),
line: int32(inl.Line),
func_: int32(linker.namemap[inl.Func]),
func_: int32(namemap[inl.Func]),
parentPc: int32(inl.ParentPC)}
}
12 changes: 12 additions & 0 deletions obj/inlinedcall.1.8.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build go1.8 && !go1.9
// +build go1.8,!go1.9

package obj

//golang 1.8 not support inline
type InlinedCall struct {
}

func InitInlinedCall(inl InlTreeNode, funcid uint8, namemap map[string]int, filetab []uint32) InlinedCall {
return InlinedCall{}
}
16 changes: 6 additions & 10 deletions inlinetree.1.9.go → obj/inlinedcall.1.9.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
//go:build go1.9 && !go1.12
// +build go1.9,!go1.12

package goloader

import (
"github.com/pkujhd/goloader/obj"
)
package obj

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
type inlinedCall struct {
type InlinedCall struct {
parent int32 // index of parent in the inltree, or < 0
file int32 // fileno index into filetab
line int32 // line number of the call site
func_ int32 // offset into pclntab for name of called function
}

func (linker *Linker) initInlinedCall(inl obj.InlTreeNode, _func *_func) inlinedCall {
return inlinedCall{
func InitInlinedCall(inl InlTreeNode, funcid uint8, namemap map[string]int, filetab []uint32) InlinedCall {
return InlinedCall{
parent: int32(inl.Parent),
file: findFileTab(linker, inl.File),
file: findFileTab(inl.File, namemap, filetab),
line: int32(inl.Line),
func_: int32(linker.namemap[inl.Func])}
func_: int32(namemap[inl.Func])}
}
6 changes: 0 additions & 6 deletions obj/readobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,3 @@ type Reloc struct {
Type int
Add int
}

func grow(bytes *[]byte, size int) {
if len(*bytes) < size {
*bytes = append(*bytes, make([]byte, size-len(*bytes))...)
}
}
17 changes: 17 additions & 0 deletions obj/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package obj

func findFileTab(filename string, namemap map[string]int, filetab []uint32) int32 {
tab := namemap[filename]
for index, value := range filetab {
if uint32(tab) == value {
return int32(index)
}
}
return -1
}

func grow(bytes *[]byte, size int) {
if len(*bytes) < size {
*bytes = append(*bytes, make([]byte, size-len(*bytes))...)
}
}

0 comments on commit 8ff7f0c

Please sign in to comment.