Skip to content

Commit

Permalink
rewrite inlinetree, when relocate function, generate inline pcdata an…
Browse files Browse the repository at this point in the history
…d funcdata
  • Loading branch information
pkujhd committed Jun 22, 2020
1 parent bec3d6f commit 3c95144
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 153 deletions.
29 changes: 0 additions & 29 deletions dymcode.1.12.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

package goloader

import (
"cmd/objfile/goobj"
)

const (
R_PCREL = 15
// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
Expand Down Expand Up @@ -55,31 +51,6 @@ const (
// Update cmd/link/internal/sym/AbiSymKindToSymKind for new SymKind values.
)

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
type inlinedCall struct {
parent int16 // index of parent in the inltree, or < 0
funcID funcID // type of the called function
_ byte
file int32 // fileno index into filetab
line int32 // line number of the call site
func_ int32 // offset into pclntab for name of called function
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
}

func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
return inlinedCall{
parent: int16(inl.Parent),
funcID: _func.funcID,
file: int32(findFileTab(codereloc, inl.File)),
line: int32(inl.Line),
func_: int32(findFuncNameOff(codereloc, inl.Func.Name)),
parentPc: int32(inl.ParentPC)}
}

func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
return _addInlineTree(codereloc, _func, funcdata, pcdata, inlineOffset)
}

func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
return _addStackObject(codereloc, funcname, symbolMap)
}
Expand Down
26 changes: 0 additions & 26 deletions dymcode.1.14.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package goloader

import (
"cmd/objfile/goobj"
"errors"
"fmt"
)
Expand Down Expand Up @@ -60,31 +59,6 @@ const (

)

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
type inlinedCall struct {
parent int16 // index of parent in the inltree, or < 0
funcID funcID // type of the called function
_ byte
file int32 // fileno index into filetab
line int32 // line number of the call site
func_ int32 // offset into pclntab for name of called function
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
}

func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
return inlinedCall{
parent: int16(inl.Parent),
funcID: _func.funcID,
file: int32(findFileTab(codereloc, inl.File)),
line: int32(inl.Line),
func_: int32(findFuncNameOff(codereloc, inl.Func.Name)),
parentPc: int32(inl.ParentPC)}
}

func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
return _addInlineTree(codereloc, _func, funcdata, pcdata, inlineOffset)
}

func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
return _addStackObject(codereloc, funcname, symbolMap)
}
Expand Down
6 changes: 0 additions & 6 deletions dymcode.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ const (
SDWARFINFO
)

type inlinedCall struct{}

func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
return nil
}

func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
return nil
}
Expand Down
24 changes: 0 additions & 24 deletions dymcode.1.9.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

package goloader

import (
"cmd/objfile/goobj"
)

const (
R_PCREL = 15
// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
Expand Down Expand Up @@ -48,26 +44,6 @@ const (
// Update cmd/link/internal/sym/AbiSymKindToSymKind for new SymKind values.
)

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
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 initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
return inlinedCall{
parent: int32(inl.Parent),
file: int32(findFileTab(codereloc, inl.File)),
line: int32(inl.Line),
func_: int32(findFuncNameOff(codereloc, inl.Func.Name))}
}

func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
return _addInlineTree(codereloc, _func, funcdata, pcdata, inlineOffset)
}

func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
return nil
}
Expand Down
33 changes: 12 additions & 21 deletions dymcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ const (
R_CALLIND = 11
)

type Func struct {
PCData []uint32
FuncData []uintptr
Var *[]goobj.Var
}

// copy from $GOROOT/src/cmd/internal/goobj/read.go type Sym struct
type Sym struct {
Name string
Kind int
Offset int
Func *goobj.Func
Func *Func
Reloc []Reloc
}

Expand Down Expand Up @@ -99,7 +105,7 @@ func relocSym(codereloc *CodeReloc, name string, objSymMap map[string]objSym) (*
return symbol, nil
}
objsym := objSymMap[name].sym
symbol := &Sym{Name: objsym.Name, Kind: int(objsym.Kind), Func: objsym.Func}
symbol := &Sym{Name: objsym.Name, Kind: int(objsym.Kind)}
codereloc.symMap[symbol.Name] = symbol

code := make([]byte, objsym.Data.Size)
Expand All @@ -113,6 +119,7 @@ func relocSym(codereloc *CodeReloc, name string, objSymMap map[string]objSym) (*
symbol.Offset = len(codereloc.code)
codereloc.code = append(codereloc.code, code...)
bytearrayAlign(&codereloc.code, PtrSize)
symbol.Func = &Func{Var: &(objsym.Func.Var)}
if err := readFuncData(codereloc, objSymMap[name], objSymMap, symbol.Offset); err != nil {
return nil, err
}
Expand Down Expand Up @@ -374,23 +381,7 @@ func addFuncTab(module *moduledata, _func *_func, codereloc *CodeReloc, symbolMa
funcname := gostringnocopy(&codereloc.pclntable[_func.nameoff])
_func.entry = uintptr(symbolMap[funcname])
Func := codereloc.symMap[funcname].Func
funcdata := make([]uintptr, len(Func.FuncData))
for k, symbol := range Func.FuncData {
if codereloc.stkmaps[symbol.Sym.Name] != nil {
funcdata[k] = (uintptr)(unsafe.Pointer(&(codereloc.stkmaps[symbol.Sym.Name][0])))
} else {
funcdata[k] = (uintptr)(0)
}
}
pcdata := []uint32{}
pcln := uint32(_func.pcln) + uint32(Func.PCLine.Size)
for k := 0; k < len(Func.PCData); k++ {
pcdata = append(pcdata, pcln)
pcln += uint32(Func.PCData[k].Size)
}
if err = addInlineTree(codereloc, _func, &funcdata, &pcdata, pcln); err != nil {
return err
}

if err = addStackObject(codereloc, funcname, symbolMap); err != nil {
return err
}
Expand All @@ -401,12 +392,12 @@ func addFuncTab(module *moduledata, _func *_func, codereloc *CodeReloc, symbolMa
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(_func)), _FuncSize)

if _func.npcdata > 0 {
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&(pcdata[0]))), Uint32Size*int(_func.npcdata))
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&(Func.PCData[0]))), Uint32Size*int(_func.npcdata))
}

grow(&module.pclntable, alignof(len(module.pclntable), PtrSize))
if _func.nfuncdata > 0 {
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&funcdata[0])), int(PtrSize*_func.nfuncdata))
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&Func.FuncData[0])), int(PtrSize*_func.nfuncdata))
}

return err
Expand Down
47 changes: 22 additions & 25 deletions inlinetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ import (
"unsafe"
)

func readPCInline(codeReloc *CodeReloc, symbol *goobj.Sym, fd *readAtSeeker) {
fd.ReadAtWithSize(&(codeReloc.pclntable), symbol.Func.PCInline.Size, symbol.Func.PCInline.Offset)
for _, inl := range symbol.Func.InlTree {
if _, ok := codeReloc.namemap[inl.Func.Name]; !ok {
codeReloc.namemap[inl.Func.Name] = len(codeReloc.pclntable)
codeReloc.pclntable = append(codeReloc.pclntable, []byte(inl.Func.Name)...)
codeReloc.pclntable = append(codeReloc.pclntable, ZERO_BYTE)
}
}
}

func findFuncNameOff(codereloc *CodeReloc, funcname string) int32 {
return int32(codereloc.namemap[funcname])
}

func findFileTab(codereloc *CodeReloc, filename string) int32 {
tab := codereloc.namemap[filename]
for index, value := range codereloc.filetab {
Expand All @@ -33,27 +18,39 @@ func findFileTab(codereloc *CodeReloc, filename string) int32 {
return -1
}

func _addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
funcname := gostringnocopy(&codereloc.pclntable[_func.nameoff])
Func := codereloc.symMap[funcname].Func
func _addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
funcname := symbol.Name
Func := symbol.Func
sym := codereloc.symMap[funcname]
if Func != nil && len(Func.InlTree) != 0 {
name := funcname + INLINETREE_SUFFIX

for _func.npcdata <= _PCDATA_InlTreeIndex {
sym.Func.PCData = append(sym.Func.PCData, uint32(0))
_func.npcdata++
}
sym.Func.PCData[_PCDATA_InlTreeIndex] = uint32(len(codereloc.pclntable))

fd.ReadAtWithSize(&(codereloc.pclntable), symbol.Func.PCInline.Size, symbol.Func.PCInline.Offset)
for _, inl := range symbol.Func.InlTree {
if _, ok := codereloc.namemap[inl.Func.Name]; !ok {
codereloc.namemap[inl.Func.Name] = len(codereloc.pclntable)
codereloc.pclntable = append(codereloc.pclntable, []byte(inl.Func.Name)...)
codereloc.pclntable = append(codereloc.pclntable, ZERO_BYTE)
}
}

bytes := make([]byte, len(Func.InlTree)*InlinedCallSize)
for k, inl := range Func.InlTree {
inlinedcall := initInlinedCall(codereloc, inl, _func)
copy2Slice(bytes[k*InlinedCallSize:], uintptr(unsafe.Pointer(&inlinedcall)), InlinedCallSize)
}
codereloc.stkmaps[name] = bytes
for _func.nfuncdata <= _FUNCDATA_InlTree {
*funcdata = append(*funcdata, uintptr(0))
sym.Func.FuncData = append(sym.Func.FuncData, uintptr(0))
_func.nfuncdata++
}
(*funcdata)[_FUNCDATA_InlTree] = (uintptr)(unsafe.Pointer(&(codereloc.stkmaps[name][0])))
for _func.npcdata <= _PCDATA_InlTreeIndex {
*pcdata = append(*pcdata, uint32(0))
_func.npcdata++
}
(*pcdata)[_PCDATA_InlTreeIndex] = inlineOffset
sym.Func.FuncData[_FUNCDATA_InlTree] = (uintptr)(unsafe.Pointer(&(codereloc.stkmaps[name][0])))
}
return err
}
20 changes: 20 additions & 0 deletions module.1.10.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,23 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
}
return fdata
}

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
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 initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
return inlinedCall{
parent: int32(inl.Parent),
file: int32(findFileTab(codereloc, inl.File)),
line: int32(inl.Line),
func_: int32(codereloc.namemap[inl.Func.Name])}
}

func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
return _addInlineTree(codereloc, _func, symbol, fd)
}
25 changes: 25 additions & 0 deletions module.1.12.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,28 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
}
return fdata
}

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
type inlinedCall struct {
parent int16 // index of parent in the inltree, or < 0
funcID funcID // type of the called function
_ byte
file int32 // fileno index into filetab
line int32 // line number of the call site
func_ int32 // offset into pclntab for name of called function
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
}

func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
return inlinedCall{
parent: int16(inl.Parent),
funcID: _func.funcID,
file: int32(findFileTab(codereloc, inl.File)),
line: int32(inl.Line),
func_: int32(codereloc.namemap[inl.Func.Name]),
parentPc: int32(inl.ParentPC)}
}

func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
return _addInlineTree(codereloc, _func, symbol, fd)
}
25 changes: 25 additions & 0 deletions module.1.13.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,28 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
}
return fdata
}

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
type inlinedCall struct {
parent int16 // index of parent in the inltree, or < 0
funcID funcID // type of the called function
_ byte
file int32 // fileno index into filetab
line int32 // line number of the call site
func_ int32 // offset into pclntab for name of called function
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
}

func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
return inlinedCall{
parent: int16(inl.Parent),
funcID: _func.funcID,
file: int32(findFileTab(codereloc, inl.File)),
line: int32(inl.Line),
func_: int32(codereloc.namemap[inl.Func.Name]),
parentPc: int32(inl.ParentPC)}
}

func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
return _addInlineTree(codereloc, _func, symbol, fd)
}
25 changes: 25 additions & 0 deletions module.1.14.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,28 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
}
return fdata
}

// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
type inlinedCall struct {
parent int16 // index of parent in the inltree, or < 0
funcID funcID // type of the called function
_ byte
file int32 // fileno index into filetab
line int32 // line number of the call site
func_ int32 // offset into pclntab for name of called function
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
}

func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
return inlinedCall{
parent: int16(inl.Parent),
funcID: _func.funcID,
file: int32(findFileTab(codereloc, inl.File)),
line: int32(inl.Line),
func_: int32(codereloc.namemap[inl.Func.Name]),
parentPc: int32(inl.ParentPC)}
}

func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
return _addInlineTree(codereloc, _func, symbol, fd)
}
Loading

0 comments on commit 3c95144

Please sign in to comment.