diff --git a/const.go b/const.go index 1c0719b0..3ff8a260 100644 --- a/const.go +++ b/const.go @@ -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 diff --git a/func.1.12.go b/func.1.12.go index c0c8fbee..7ad72be1 100644 --- a/func.1.12.go +++ b/func.1.12.go @@ -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) +} diff --git a/func.1.16.go b/func.1.16.go index 517c371a..e8e5d9a1 100644 --- a/func.1.16.go +++ b/func.1.16.go @@ -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) +} diff --git a/func.1.18.go b/func.1.18.go index 7fb4ead3..84210958 100644 --- a/func.1.18.go +++ b/func.1.18.go @@ -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) +} diff --git a/func.1.8.go b/func.1.8.go index 5244ab38..0df90cc6 100644 --- a/func.1.8.go +++ b/func.1.8.go @@ -47,3 +47,7 @@ func getfuncname(f *_func, md *moduledata) string { } return gostringnocopy(&(md.pclntable[f.nameoff])) } + +func getfuncID(f *_func) uint8 { + return 0 +} diff --git a/inlinetree.1.8.go b/inlinetree.1.8.go deleted file mode 100644 index bbf488d0..00000000 --- a/inlinetree.1.8.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build go1.8 && !go1.9 -// +build go1.8,!go1.9 - -package goloader - -import "github.com/pkujhd/goloader/obj" - -type inlinedCall struct{} - -func (linker *Linker) addInlineTree(_func *_func, objsym *obj.ObjSymbol) (err error) { - return nil -} diff --git a/inlinetree.go b/inlinetree.go index 8231ee00..462b9d00 100644 --- a/inlinetree.go +++ b/inlinetree.go @@ -1,6 +1,3 @@ -//go:build go1.9 && !go1.19 -// +build go1.9,!go1.19 - package goloader import ( @@ -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 @@ -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...) diff --git a/obj/const.go b/obj/const.go index d9dd8161..cfa16ebb 100644 --- a/obj/const.go +++ b/obj/const.go @@ -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 = "" ) diff --git a/inlinetree.1.12.go b/obj/inlinedcall.1.12.go similarity index 66% rename from inlinetree.1.12.go rename to obj/inlinedcall.1.12.go index 60259c73..99fe989b 100644 --- a/inlinetree.1.12.go +++ b/obj/inlinedcall.1.12.go @@ -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 @@ -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)} } diff --git a/obj/inlinedcall.1.8.go b/obj/inlinedcall.1.8.go new file mode 100644 index 00000000..177bc6dc --- /dev/null +++ b/obj/inlinedcall.1.8.go @@ -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{} +} diff --git a/inlinetree.1.9.go b/obj/inlinedcall.1.9.go similarity index 58% rename from inlinetree.1.9.go rename to obj/inlinedcall.1.9.go index f995f104..cd145462 100644 --- a/inlinetree.1.9.go +++ b/obj/inlinedcall.1.9.go @@ -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])} } diff --git a/obj/readobj.go b/obj/readobj.go index 4db35568..a492919d 100644 --- a/obj/readobj.go +++ b/obj/readobj.go @@ -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))...) - } -} diff --git a/obj/utils.go b/obj/utils.go new file mode 100644 index 00000000..f3bc97bf --- /dev/null +++ b/obj/utils.go @@ -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))...) + } +}