Skip to content

Commit 3c95144

Browse files
committed
rewrite inlinetree, when relocate function, generate inline pcdata and funcdata
1 parent bec3d6f commit 3c95144

14 files changed

+187
-153
lines changed

Diff for: dymcode.1.12.go

-29
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
package goloader
55

6-
import (
7-
"cmd/objfile/goobj"
8-
)
9-
106
const (
117
R_PCREL = 15
128
// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
@@ -55,31 +51,6 @@ const (
5551
// Update cmd/link/internal/sym/AbiSymKindToSymKind for new SymKind values.
5652
)
5753

58-
// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
59-
type inlinedCall struct {
60-
parent int16 // index of parent in the inltree, or < 0
61-
funcID funcID // type of the called function
62-
_ byte
63-
file int32 // fileno index into filetab
64-
line int32 // line number of the call site
65-
func_ int32 // offset into pclntab for name of called function
66-
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
67-
}
68-
69-
func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
70-
return inlinedCall{
71-
parent: int16(inl.Parent),
72-
funcID: _func.funcID,
73-
file: int32(findFileTab(codereloc, inl.File)),
74-
line: int32(inl.Line),
75-
func_: int32(findFuncNameOff(codereloc, inl.Func.Name)),
76-
parentPc: int32(inl.ParentPC)}
77-
}
78-
79-
func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
80-
return _addInlineTree(codereloc, _func, funcdata, pcdata, inlineOffset)
81-
}
82-
8354
func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
8455
return _addStackObject(codereloc, funcname, symbolMap)
8556
}

Diff for: dymcode.1.14.go

-26
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
package goloader
55

66
import (
7-
"cmd/objfile/goobj"
87
"errors"
98
"fmt"
109
)
@@ -60,31 +59,6 @@ const (
6059

6160
)
6261

63-
// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
64-
type inlinedCall struct {
65-
parent int16 // index of parent in the inltree, or < 0
66-
funcID funcID // type of the called function
67-
_ byte
68-
file int32 // fileno index into filetab
69-
line int32 // line number of the call site
70-
func_ int32 // offset into pclntab for name of called function
71-
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
72-
}
73-
74-
func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
75-
return inlinedCall{
76-
parent: int16(inl.Parent),
77-
funcID: _func.funcID,
78-
file: int32(findFileTab(codereloc, inl.File)),
79-
line: int32(inl.Line),
80-
func_: int32(findFuncNameOff(codereloc, inl.Func.Name)),
81-
parentPc: int32(inl.ParentPC)}
82-
}
83-
84-
func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
85-
return _addInlineTree(codereloc, _func, funcdata, pcdata, inlineOffset)
86-
}
87-
8862
func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
8963
return _addStackObject(codereloc, funcname, symbolMap)
9064
}

Diff for: dymcode.1.8.go

-6
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ const (
6767
SDWARFINFO
6868
)
6969

70-
type inlinedCall struct{}
71-
72-
func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
73-
return nil
74-
}
75-
7670
func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
7771
return nil
7872
}

Diff for: dymcode.1.9.go

-24
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33

44
package goloader
55

6-
import (
7-
"cmd/objfile/goobj"
8-
)
9-
106
const (
117
R_PCREL = 15
128
// R_TLS_LE, used on 386, amd64, and ARM, resolves to the offset of the
@@ -48,26 +44,6 @@ const (
4844
// Update cmd/link/internal/sym/AbiSymKindToSymKind for new SymKind values.
4945
)
5046

51-
// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
52-
type inlinedCall struct {
53-
parent int32 // index of parent in the inltree, or < 0
54-
file int32 // fileno index into filetab
55-
line int32 // line number of the call site
56-
func_ int32 // offset into pclntab for name of called function
57-
}
58-
59-
func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
60-
return inlinedCall{
61-
parent: int32(inl.Parent),
62-
file: int32(findFileTab(codereloc, inl.File)),
63-
line: int32(inl.Line),
64-
func_: int32(findFuncNameOff(codereloc, inl.Func.Name))}
65-
}
66-
67-
func addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
68-
return _addInlineTree(codereloc, _func, funcdata, pcdata, inlineOffset)
69-
}
70-
7147
func addStackObject(codereloc *CodeReloc, funcname string, symbolMap map[string]uintptr) (err error) {
7248
return nil
7349
}

Diff for: dymcode.go

+12-21
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ const (
3232
R_CALLIND = 11
3333
)
3434

35+
type Func struct {
36+
PCData []uint32
37+
FuncData []uintptr
38+
Var *[]goobj.Var
39+
}
40+
3541
// copy from $GOROOT/src/cmd/internal/goobj/read.go type Sym struct
3642
type Sym struct {
3743
Name string
3844
Kind int
3945
Offset int
40-
Func *goobj.Func
46+
Func *Func
4147
Reloc []Reloc
4248
}
4349

@@ -99,7 +105,7 @@ func relocSym(codereloc *CodeReloc, name string, objSymMap map[string]objSym) (*
99105
return symbol, nil
100106
}
101107
objsym := objSymMap[name].sym
102-
symbol := &Sym{Name: objsym.Name, Kind: int(objsym.Kind), Func: objsym.Func}
108+
symbol := &Sym{Name: objsym.Name, Kind: int(objsym.Kind)}
103109
codereloc.symMap[symbol.Name] = symbol
104110

105111
code := make([]byte, objsym.Data.Size)
@@ -113,6 +119,7 @@ func relocSym(codereloc *CodeReloc, name string, objSymMap map[string]objSym) (*
113119
symbol.Offset = len(codereloc.code)
114120
codereloc.code = append(codereloc.code, code...)
115121
bytearrayAlign(&codereloc.code, PtrSize)
122+
symbol.Func = &Func{Var: &(objsym.Func.Var)}
116123
if err := readFuncData(codereloc, objSymMap[name], objSymMap, symbol.Offset); err != nil {
117124
return nil, err
118125
}
@@ -374,23 +381,7 @@ func addFuncTab(module *moduledata, _func *_func, codereloc *CodeReloc, symbolMa
374381
funcname := gostringnocopy(&codereloc.pclntable[_func.nameoff])
375382
_func.entry = uintptr(symbolMap[funcname])
376383
Func := codereloc.symMap[funcname].Func
377-
funcdata := make([]uintptr, len(Func.FuncData))
378-
for k, symbol := range Func.FuncData {
379-
if codereloc.stkmaps[symbol.Sym.Name] != nil {
380-
funcdata[k] = (uintptr)(unsafe.Pointer(&(codereloc.stkmaps[symbol.Sym.Name][0])))
381-
} else {
382-
funcdata[k] = (uintptr)(0)
383-
}
384-
}
385-
pcdata := []uint32{}
386-
pcln := uint32(_func.pcln) + uint32(Func.PCLine.Size)
387-
for k := 0; k < len(Func.PCData); k++ {
388-
pcdata = append(pcdata, pcln)
389-
pcln += uint32(Func.PCData[k].Size)
390-
}
391-
if err = addInlineTree(codereloc, _func, &funcdata, &pcdata, pcln); err != nil {
392-
return err
393-
}
384+
394385
if err = addStackObject(codereloc, funcname, symbolMap); err != nil {
395386
return err
396387
}
@@ -401,12 +392,12 @@ func addFuncTab(module *moduledata, _func *_func, codereloc *CodeReloc, symbolMa
401392
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(_func)), _FuncSize)
402393

403394
if _func.npcdata > 0 {
404-
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&(pcdata[0]))), Uint32Size*int(_func.npcdata))
395+
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&(Func.PCData[0]))), Uint32Size*int(_func.npcdata))
405396
}
406397

407398
grow(&module.pclntable, alignof(len(module.pclntable), PtrSize))
408399
if _func.nfuncdata > 0 {
409-
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&funcdata[0])), int(PtrSize*_func.nfuncdata))
400+
append2Slice(&module.pclntable, uintptr(unsafe.Pointer(&Func.FuncData[0])), int(PtrSize*_func.nfuncdata))
410401
}
411402

412403
return err

Diff for: inlinetree.go

+22-25
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,6 @@ import (
88
"unsafe"
99
)
1010

11-
func readPCInline(codeReloc *CodeReloc, symbol *goobj.Sym, fd *readAtSeeker) {
12-
fd.ReadAtWithSize(&(codeReloc.pclntable), symbol.Func.PCInline.Size, symbol.Func.PCInline.Offset)
13-
for _, inl := range symbol.Func.InlTree {
14-
if _, ok := codeReloc.namemap[inl.Func.Name]; !ok {
15-
codeReloc.namemap[inl.Func.Name] = len(codeReloc.pclntable)
16-
codeReloc.pclntable = append(codeReloc.pclntable, []byte(inl.Func.Name)...)
17-
codeReloc.pclntable = append(codeReloc.pclntable, ZERO_BYTE)
18-
}
19-
}
20-
}
21-
22-
func findFuncNameOff(codereloc *CodeReloc, funcname string) int32 {
23-
return int32(codereloc.namemap[funcname])
24-
}
25-
2611
func findFileTab(codereloc *CodeReloc, filename string) int32 {
2712
tab := codereloc.namemap[filename]
2813
for index, value := range codereloc.filetab {
@@ -33,27 +18,39 @@ func findFileTab(codereloc *CodeReloc, filename string) int32 {
3318
return -1
3419
}
3520

36-
func _addInlineTree(codereloc *CodeReloc, _func *_func, funcdata *[]uintptr, pcdata *[]uint32, inlineOffset uint32) (err error) {
37-
funcname := gostringnocopy(&codereloc.pclntable[_func.nameoff])
38-
Func := codereloc.symMap[funcname].Func
21+
func _addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
22+
funcname := symbol.Name
23+
Func := symbol.Func
24+
sym := codereloc.symMap[funcname]
3925
if Func != nil && len(Func.InlTree) != 0 {
4026
name := funcname + INLINETREE_SUFFIX
27+
28+
for _func.npcdata <= _PCDATA_InlTreeIndex {
29+
sym.Func.PCData = append(sym.Func.PCData, uint32(0))
30+
_func.npcdata++
31+
}
32+
sym.Func.PCData[_PCDATA_InlTreeIndex] = uint32(len(codereloc.pclntable))
33+
34+
fd.ReadAtWithSize(&(codereloc.pclntable), symbol.Func.PCInline.Size, symbol.Func.PCInline.Offset)
35+
for _, inl := range symbol.Func.InlTree {
36+
if _, ok := codereloc.namemap[inl.Func.Name]; !ok {
37+
codereloc.namemap[inl.Func.Name] = len(codereloc.pclntable)
38+
codereloc.pclntable = append(codereloc.pclntable, []byte(inl.Func.Name)...)
39+
codereloc.pclntable = append(codereloc.pclntable, ZERO_BYTE)
40+
}
41+
}
42+
4143
bytes := make([]byte, len(Func.InlTree)*InlinedCallSize)
4244
for k, inl := range Func.InlTree {
4345
inlinedcall := initInlinedCall(codereloc, inl, _func)
4446
copy2Slice(bytes[k*InlinedCallSize:], uintptr(unsafe.Pointer(&inlinedcall)), InlinedCallSize)
4547
}
4648
codereloc.stkmaps[name] = bytes
4749
for _func.nfuncdata <= _FUNCDATA_InlTree {
48-
*funcdata = append(*funcdata, uintptr(0))
50+
sym.Func.FuncData = append(sym.Func.FuncData, uintptr(0))
4951
_func.nfuncdata++
5052
}
51-
(*funcdata)[_FUNCDATA_InlTree] = (uintptr)(unsafe.Pointer(&(codereloc.stkmaps[name][0])))
52-
for _func.npcdata <= _PCDATA_InlTreeIndex {
53-
*pcdata = append(*pcdata, uint32(0))
54-
_func.npcdata++
55-
}
56-
(*pcdata)[_PCDATA_InlTreeIndex] = inlineOffset
53+
sym.Func.FuncData[_FUNCDATA_InlTree] = (uintptr)(unsafe.Pointer(&(codereloc.stkmaps[name][0])))
5754
}
5855
return err
5956
}

Diff for: module.1.10.go

+20
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,23 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
8787
}
8888
return fdata
8989
}
90+
91+
// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
92+
type inlinedCall struct {
93+
parent int32 // index of parent in the inltree, or < 0
94+
file int32 // fileno index into filetab
95+
line int32 // line number of the call site
96+
func_ int32 // offset into pclntab for name of called function
97+
}
98+
99+
func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
100+
return inlinedCall{
101+
parent: int32(inl.Parent),
102+
file: int32(findFileTab(codereloc, inl.File)),
103+
line: int32(inl.Line),
104+
func_: int32(codereloc.namemap[inl.Func.Name])}
105+
}
106+
107+
func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
108+
return _addInlineTree(codereloc, _func, symbol, fd)
109+
}

Diff for: module.1.12.go

+25
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,28 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
9999
}
100100
return fdata
101101
}
102+
103+
// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
104+
type inlinedCall struct {
105+
parent int16 // index of parent in the inltree, or < 0
106+
funcID funcID // type of the called function
107+
_ byte
108+
file int32 // fileno index into filetab
109+
line int32 // line number of the call site
110+
func_ int32 // offset into pclntab for name of called function
111+
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
112+
}
113+
114+
func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
115+
return inlinedCall{
116+
parent: int16(inl.Parent),
117+
funcID: _func.funcID,
118+
file: int32(findFileTab(codereloc, inl.File)),
119+
line: int32(inl.Line),
120+
func_: int32(codereloc.namemap[inl.Func.Name]),
121+
parentPc: int32(inl.ParentPC)}
122+
}
123+
124+
func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
125+
return _addInlineTree(codereloc, _func, symbol, fd)
126+
}

Diff for: module.1.13.go

+25
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,28 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
100100
}
101101
return fdata
102102
}
103+
104+
// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
105+
type inlinedCall struct {
106+
parent int16 // index of parent in the inltree, or < 0
107+
funcID funcID // type of the called function
108+
_ byte
109+
file int32 // fileno index into filetab
110+
line int32 // line number of the call site
111+
func_ int32 // offset into pclntab for name of called function
112+
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
113+
}
114+
115+
func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
116+
return inlinedCall{
117+
parent: int16(inl.Parent),
118+
funcID: _func.funcID,
119+
file: int32(findFileTab(codereloc, inl.File)),
120+
line: int32(inl.Line),
121+
func_: int32(codereloc.namemap[inl.Func.Name]),
122+
parentPc: int32(inl.ParentPC)}
123+
}
124+
125+
func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
126+
return _addInlineTree(codereloc, _func, symbol, fd)
127+
}

Diff for: module.1.14.go

+25
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,28 @@ func init_func(symbol *goobj.Sym, nameOff, spOff, pcfileOff, pclnOff int) _func
106106
}
107107
return fdata
108108
}
109+
110+
// inlinedCall is the encoding of entries in the FUNCDATA_InlTree table.
111+
type inlinedCall struct {
112+
parent int16 // index of parent in the inltree, or < 0
113+
funcID funcID // type of the called function
114+
_ byte
115+
file int32 // fileno index into filetab
116+
line int32 // line number of the call site
117+
func_ int32 // offset into pclntab for name of called function
118+
parentPc int32 // position of an instruction whose source position is the call site (offset from entry)
119+
}
120+
121+
func initInlinedCall(codereloc *CodeReloc, inl goobj.InlinedCall, _func *_func) inlinedCall {
122+
return inlinedCall{
123+
parent: int16(inl.Parent),
124+
funcID: _func.funcID,
125+
file: int32(findFileTab(codereloc, inl.File)),
126+
line: int32(inl.Line),
127+
func_: int32(codereloc.namemap[inl.Func.Name]),
128+
parentPc: int32(inl.ParentPC)}
129+
}
130+
131+
func addInlineTree(codereloc *CodeReloc, _func *_func, symbol *goobj.Sym, fd *readAtSeeker) (err error) {
132+
return _addInlineTree(codereloc, _func, symbol, fd)
133+
}

0 commit comments

Comments
 (0)