Skip to content

Commit

Permalink
add ImportPkgs into struct Pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujhd committed Dec 2, 2023
1 parent 328a5ad commit e246461
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 69 deletions.
4 changes: 2 additions & 2 deletions deferreturn.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ func (linker *Linker) addDeferReturn(_func *_func, module *moduledata) (err erro
for _, r := range sym.Reloc {
if r.Sym.Name == RuntimeDeferReturn {
//../cmd/link/internal/ld/pcln.go:pclntab
switch linker.Arch.Name {
switch linker.arch.Name {
case sys.Arch386.Name, sys.ArchAMD64.Name:
_func.deferreturn = uint32(r.Offset) - uint32(sym.Offset) - 1
case sys.ArchARM.Name, sys.ArchARM64.Name:
_func.deferreturn = uint32(r.Offset) - uint32(sym.Offset)
default:
err = fmt.Errorf("not support arch:%s", linker.Arch.Name)
err = fmt.Errorf("not support arch:%s", linker.arch.Name)
}
break
}
Expand Down
10 changes: 5 additions & 5 deletions gcdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ func generategcdata(linker *Linker, codeModule *CodeModule, symbolMap map[string
}
if ptr, ok := symbolMap[typeName]; ok {
typ := (*_type)(adduintptr(ptr, 0))
nptr := int64(typ.ptrdata) / int64(linker.Arch.PtrSize)
nptr := int64(typ.ptrdata) / int64(linker.arch.PtrSize)
if typ.kind&KindGCProg == 0 {
var mask []byte
append2Slice(&mask, uintptr(unsafe.Pointer(typ.gcdata)), int(nptr+7)/8)
for i := int64(0); i < nptr; i++ {
if (mask[i/8]>>uint(i%8))&1 != 0 {
w.Ptr(sval/int64(linker.Arch.PtrSize) + i)
w.Ptr(sval/int64(linker.arch.PtrSize) + i)
}
}

} else {
var prog []byte
append2Slice(&prog, uintptr(unsafe.Pointer(typ.gcdata)), Uint32Size+int((*(*uint32)(unsafe.Pointer(typ.gcdata)))))
w.ZeroUntil(sval / int64(linker.Arch.PtrSize))
w.ZeroUntil(sval / int64(linker.arch.PtrSize))
w.Append(prog[4:], nptr)
}
} else {
Expand Down Expand Up @@ -83,7 +83,7 @@ func (linker *Linker) addgcdata(codeModule *CodeModule, symbolMap map[string]uin
return err
}
}
w.ZeroUntil(int64(module.edata-module.data) / int64(linker.Arch.PtrSize))
w.ZeroUntil(int64(module.edata-module.data) / int64(linker.arch.PtrSize))
w.End()
module.gcdata = (*sliceHeader)(unsafe.Pointer(&codeModule.gcdata)).Data
module.gcdatamask = progToPointerMask((*byte)(adduintptr(module.gcdata, 0)), module.edata-module.data)
Expand All @@ -99,7 +99,7 @@ func (linker *Linker) addgcdata(codeModule *CodeModule, symbolMap map[string]uin
return err
}
}
w.ZeroUntil(int64(module.ebss-module.bss) / int64(linker.Arch.PtrSize))
w.ZeroUntil(int64(module.ebss-module.bss) / int64(linker.arch.PtrSize))
w.End()
module.gcbss = (*sliceHeader)(unsafe.Pointer(&codeModule.gcbss)).Data
module.gcbssmask = progToPointerMask((*byte)(adduintptr(module.gcbss, 0)), module.ebss-module.bss)
Expand Down
3 changes: 2 additions & 1 deletion init.1.13.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func getInitFuncName(packagename string) string {
func doInit(t unsafe.Pointer) // t should be a *runtime.initTask

func (linker *Linker) doInitialize(symPtr, symbolMap map[string]uintptr) error {
for _, name := range linker.initFuncs {
for _, pkg := range linker.pkgs {
name := getInitFuncName(pkg.PkgPath)
if funcPtr, ok := symbolMap[name]; ok {
doInit(adduintptr(funcPtr, 0))
}
Expand Down
3 changes: 2 additions & 1 deletion init.1.21.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func getInitFuncName(packagename string) string {
func doInit1(t unsafe.Pointer) // t should be a *runtime.initTask

func (linker *Linker) doInitialize(symPtr, symbolMap map[string]uintptr) error {
for _, name := range linker.initFuncs {
for _, pkg := range linker.pkgs {
name := getInitFuncName(pkg.PkgPath)
if ptr, ok := symbolMap[name]; ok {
for _, loc := range linker.symMap[name].Reloc {
if loc.Type == reloctype.R_INITORDER {
Expand Down
3 changes: 2 additions & 1 deletion init.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func getInitFuncName(packagename string) string {
}

func (linker *Linker) doInitialize(symPtr, symbolMap map[string]uintptr) error {
for _, name := range linker.initFuncs {
for _, pkg := range linker.pkgs {
name := getInitFuncName(pkg.PkgPath)
if funcPtr, ok := symbolMap[name]; ok {
funcPtrContainer := (uintptr)(unsafe.Pointer(&funcPtr))
runFunc := *(*func())(unsafe.Pointer(&funcPtrContainer))
Expand Down
62 changes: 35 additions & 27 deletions ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,38 @@ type segment struct {
dataSeg
}

type Linker struct {
code []byte
data []byte
noptrdata []byte
bss []byte
noptrbss []byte
symMap map[string]*obj.Sym
objSymbolMap map[string]*obj.ObjSymbol
nameMap map[string]int
stringMap map[string]*string
filetab []uint32
pclntable []byte
_func []*_func
initFuncs []string
Arch *sys.Arch
type gcData struct {
gcdata []byte
gcbss []byte
}

type CodeModule struct {
segment
gcData
Syms map[string]uintptr
stringMap map[string]*string
module *moduledata
gcdata []byte
gcbss []byte
}

type linkerData struct {
code []byte
data []byte
noptrdata []byte
bss []byte
noptrbss []byte
}

type Linker struct {
linkerData
symMap map[string]*obj.Sym
objSymbolMap map[string]*obj.ObjSymbol
nameMap map[string]int
stringMap map[string]*string
filetab []uint32
pclntable []byte
_funcs []*_func
pkgs []*obj.Pkg
arch *sys.Arch
}

var (
Expand All @@ -92,8 +100,8 @@ func initLinker() *Linker {
func (linker *Linker) initPcHeader() {
pcheader := (*pcHeader)(unsafe.Pointer(&linker.pclntable[0]))
pcheader.magic = magic
pcheader.minLC = uint8(linker.Arch.MinLC)
pcheader.ptrSize = uint8(linker.Arch.PtrSize)
pcheader.minLC = uint8(linker.arch.MinLC)
pcheader.ptrSize = uint8(linker.arch.PtrSize)
}

func (linker *Linker) addFiles(files []string) {
Expand Down Expand Up @@ -161,9 +169,9 @@ func (linker *Linker) addSymbol(name string) (symbol *obj.Sym, err error) {
linker.code = append(linker.code, objsym.Data...)
expandFunc(linker, objsym, symbol)
if len(linker.code)-symbol.Offset < minfunc {
linker.code = append(linker.code, createArchNops(linker.Arch, minfunc-(len(linker.code)-symbol.Offset))...)
linker.code = append(linker.code, createArchNops(linker.arch, minfunc-(len(linker.code)-symbol.Offset))...)
}
bytearrayAlignNops(linker.Arch, &linker.code, funcalign.GetFuncAlign(linker.Arch))
bytearrayAlignNops(linker.arch, &linker.code, funcalign.GetFuncAlign(linker.arch))
symbol.Func = &obj.Func{}
if err := linker.readFuncData(linker.objSymbolMap[name], symbol.Offset); err != nil {
return nil, err
Expand Down Expand Up @@ -242,7 +250,7 @@ func (linker *Linker) addSymbol(name string) (symbol *obj.Sym, err error) {
}
if ispreprocesssymbol(reloc.Sym.Name) {
bytes := make([]byte, UInt64Size)
if err := preprocesssymbol(linker.Arch.ByteOrder, reloc.Sym.Name, bytes); err != nil {
if err := preprocesssymbol(linker.arch.ByteOrder, reloc.Sym.Name, bytes); err != nil {
return nil, err
} else {
reloc.Sym.Kind = symkind.SNOPTRDATA
Expand Down Expand Up @@ -300,7 +308,7 @@ func (linker *Linker) readFuncData(symbol *obj.ObjSymbol, codeLen int) (err erro
linker.pclntable = append(linker.pclntable, symbol.Func.PCLine...)

_func := initfunc(symbol, nameOff, pcspOff, pcfileOff, pclnOff, int(symbol.Func.CUOffset))
linker._func = append(linker._func, &_func)
linker._funcs = append(linker._funcs, &_func)
Func := linker.symMap[symbol.Name].Func
for _, pcdata := range symbol.Func.PCData {
if len(pcdata) == 0 {
Expand Down Expand Up @@ -349,7 +357,7 @@ func (linker *Linker) addSymbolMap(symPtr map[string]uintptr, codeModule *CodeMo
symbolMap[name] = ptr
} else if addr, ok := symPtr[strings.TrimSuffix(name, GOTPCRELSuffix)]; ok && strings.HasSuffix(name, GOTPCRELSuffix) {
symbolMap[name] = uintptr(segment.dataBase) + uintptr(segment.dataOff)
putAddressAddOffset(linker.Arch.ByteOrder, segment.dataByte, &segment.dataOff, uint64(addr))
putAddressAddOffset(linker.arch.ByteOrder, segment.dataByte, &segment.dataOff, uint64(addr))
} else {
symbolMap[name] = InvalidHandleValue
return nil, fmt.Errorf("unresolve external:%s", sym.Name)
Expand Down Expand Up @@ -429,18 +437,18 @@ func (linker *Linker) buildModule(codeModule *CodeModule, symbolMap map[string]u
initmodule(codeModule.module, linker)

module.ftab = append(module.ftab, initfunctab(module.minpc, uintptr(len(module.pclntable)), module.text))
for index, _func := range linker._func {
for index, _func := range linker._funcs {
funcname := getfuncname(_func, module)
module.ftab = append(module.ftab, initfunctab(symbolMap[funcname], uintptr(len(module.pclntable)), module.text))
if err = linker.addFuncTab(module, linker._func[index], symbolMap); err != nil {
if err = linker.addFuncTab(module, linker._funcs[index], symbolMap); err != nil {
return err
}
}
module.ftab = append(module.ftab, initfunctab(module.maxpc, uintptr(len(module.pclntable)), module.text))

//see:^src/cmd/link/internal/ld/pcln.go findfunctab
funcbucket := []findfuncbucket{}
for k, _func := range linker._func {
for k, _func := range linker._funcs {
funcname := getfuncname(_func, module)
x := linker.symMap[funcname].Offset
b := x / pcbucketsize
Expand Down
2 changes: 1 addition & 1 deletion obj/inlinedcall.1.12.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func InitInlinedCall(inl InlTreeNode, funcid uint8, namemap map[string]int, file
return InlinedCall{
parent: int16(inl.Parent),
funcID: funcID(funcid),
file: findFileTab(inl.File, namemap, filetab),
file: FindFileTab(inl.File, namemap, filetab),
line: int32(inl.Line),
func_: int32(namemap[inl.Func]),
parentPc: int32(inl.ParentPC)}
Expand Down
2 changes: 1 addition & 1 deletion obj/inlinedcall.1.9.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type InlinedCall struct {
func InitInlinedCall(inl InlTreeNode, funcid uint8, namemap map[string]int, filetab []uint32) InlinedCall {
return InlinedCall{
parent: int32(inl.Parent),
file: findFileTab(inl.File, namemap, filetab),
file: FindFileTab(inl.File, namemap, filetab),
line: int32(inl.Line),
func_: int32(namemap[inl.Func])}
}
6 changes: 6 additions & 0 deletions obj/readobj.1.16.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"cmd/objfile/goobj"
"cmd/objfile/objabi"
"fmt"
"path/filepath"
"strings"

"github.com/pkujhd/goloader/constants"
Expand Down Expand Up @@ -44,6 +45,11 @@ func (pkg *Pkg) Symbols() error {
for i := 0; i < nsym; i++ {
pkg.addSym(r, uint32(i), &refNames)
}
for _, importPkg := range r.Autolib() {
path := importPkg.Pkg
path = path[:len(path)-len(filepath.Ext(path))]
pkg.ImportPkgs = append(pkg.ImportPkgs, path)
}
default:
return fmt.Errorf("Parse open %s: unrecognized archive member %s\n", pkg.File.Name(), e.Name)
}
Expand Down
5 changes: 5 additions & 0 deletions obj/readobj.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"cmd/objfile/goobj"
"fmt"
"io"
"path/filepath"
)

type readAtSeeker struct {
Expand Down Expand Up @@ -86,5 +87,9 @@ func (pkg *Pkg) Symbols() error {
}
pkg.Syms[sym.Name] = symbol
}
for _, path := range obj.Imports {
path = path[:len(path)-len(filepath.Ext(path))]
pkg.ImportPkgs = append(pkg.ImportPkgs, path)
}
return nil
}
11 changes: 6 additions & 5 deletions obj/readobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
)

type Pkg struct {
Syms map[string]*ObjSymbol
Arch string
PkgPath string
File *os.File
CUFiles []string
Syms map[string]*ObjSymbol
Arch string
PkgPath string
File *os.File
ImportPkgs []string
CUFiles []string
}

type FuncInfo struct {
Expand Down
2 changes: 1 addition & 1 deletion obj/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package obj

func findFileTab(filename string, namemap map[string]int, filetab []uint32) int32 {
func FindFileTab(filename string, namemap map[string]int, filetab []uint32) int32 {
tab := namemap[filename]
for index, value := range filetab {
if uint32(tab) == value {
Expand Down
14 changes: 2 additions & 12 deletions pcvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ import (
"github.com/pkujhd/goloader/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 dumpPCValue(b []byte, prefix string) {
fmt.Println(prefix, b)
var pc uintptr
Expand Down Expand Up @@ -57,7 +47,7 @@ func rewritePCFile(symbol *obj.ObjSymbol, linker *Linker) {
if !ok || len(p) <= 0 {
break
}
nval := findFileTab(symbol.Func.File[val], linker.nameMap, linker.filetab)
nval := obj.FindFileTab(symbol.Func.File[val], linker.nameMap, linker.filetab)
pcfile = writePCValue(pcfile, int64(nval-lastval), uint64(pc-lastpc))
lastpc = pc
lastval = nval
Expand Down Expand Up @@ -119,7 +109,7 @@ func patchPCValues(linker *Linker, pcVals *[]byte, reloc obj.Reloc) {
return
}
var pcQuantum uintptr = 1
if linker.Arch.Family == sys.ARM64 {
if linker.arch.Family == sys.ARM64 {
pcQuantum = 4
}
val, startPC := pcValue(*pcVals, uintptr(reloc.Offset))
Expand Down
8 changes: 4 additions & 4 deletions readobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func readObj(pkg *obj.Pkg, linker *Linker, cuOffset int) error {
if err := pkg.Symbols(); err != nil {
return fmt.Errorf("read error: %v", err)
}
if linker.Arch != nil && linker.Arch.Name != pkg.Arch {
return fmt.Errorf("read obj error: Arch %s != Arch %s", linker.Arch.Name, pkg.Arch)
if linker.arch != nil && linker.arch.Name != pkg.Arch {
return fmt.Errorf("read obj error: Arch %s != Arch %s", linker.arch.Name, pkg.Arch)
} else {
linker.Arch = getArch(pkg.Arch)
linker.arch = getArch(pkg.Arch)
}

for _, sym := range pkg.Syms {
Expand All @@ -53,7 +53,7 @@ func readObj(pkg *obj.Pkg, linker *Linker, cuOffset int) error {
for _, sym := range pkg.Syms {
linker.objSymbolMap[sym.Name] = sym
}
linker.initFuncs = append(linker.initFuncs, getInitFuncName(pkg.PkgPath))
linker.pkgs = append(linker.pkgs, pkg)
return nil
}

Expand Down
Loading

0 comments on commit e246461

Please sign in to comment.