Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujhd committed Nov 24, 2023
1 parent 183c3d7 commit 328a5ad
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 87 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2019-2022 pkujhd
Copyright 2019-2023 pkujhd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
7 changes: 4 additions & 3 deletions func.1.12.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package goloader

import (
"cmd/objfile/objabi"
"github.com/pkujhd/goloader/obj"
"strings"

"github.com/pkujhd/goloader/obj"
)

// A funcID identifies particular functions that need to be treated
Expand Down Expand Up @@ -36,13 +37,13 @@ type _func struct {
nfuncdata uint8 // must be last
}

func initfunc(symbol *obj.ObjSymbol, nameOff, spOff, pcfileOff, pclnOff int, cuOff int) _func {
func initfunc(symbol *obj.ObjSymbol, nameOff, pcspOff, pcfileOff, pclnOff int, cuOff int) _func {
fdata := _func{
entry: uintptr(0),
nameoff: int32(nameOff),
args: int32(symbol.Func.Args),
deferreturn: uint32(0),
pcsp: int32(spOff),
pcsp: int32(pcspOff),
pcfile: int32(pcfileOff),
pcln: int32(pclnOff),
npcdata: int32(len(symbol.Func.PCData)),
Expand Down
4 changes: 2 additions & 2 deletions func.1.16.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ type _func struct {
nfuncdata uint8 // must be last
}

func initfunc(symbol *obj.ObjSymbol, nameOff, spOff, pcfileOff, pclnOff, cuOff int) _func {
func initfunc(symbol *obj.ObjSymbol, nameOff, pcspOff, pcfileOff, pclnOff, cuOff int) _func {
fdata := _func{
entry: uintptr(0),
nameoff: int32(nameOff),
args: int32(symbol.Func.Args),
deferreturn: uint32(0),
pcsp: uint32(spOff),
pcsp: uint32(pcspOff),
pcfile: uint32(pcfileOff),
pcln: uint32(pclnOff),
npcdata: uint32(len(symbol.Func.PCData)),
Expand Down
4 changes: 2 additions & 2 deletions func.1.18.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ type _func struct {
nfuncdata uint8 // must be last, must end on a uint32-aligned boundary
}

func initfunc(symbol *obj.ObjSymbol, nameOff, spOff, pcfileOff, pclnOff, cuOff int) _func {
func initfunc(symbol *obj.ObjSymbol, nameOff, pcspOff, pcfileOff, pclnOff, cuOff int) _func {
fdata := _func{
entryoff: uint32(0),
nameoff: int32(nameOff),
args: int32(symbol.Func.Args),
deferreturn: uint32(0),
pcsp: uint32(spOff),
pcsp: uint32(pcspOff),
pcfile: uint32(pcfileOff),
pcln: uint32(pclnOff),
npcdata: uint32(len(symbol.Func.PCData)),
Expand Down
4 changes: 2 additions & 2 deletions func.1.20.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ type _func struct {
// funcdata [nfuncdata]uint32
}

func initfunc(symbol *obj.ObjSymbol, nameOff, spOff, pcfileOff, pclnOff, cuOff int) _func {
func initfunc(symbol *obj.ObjSymbol, nameOff, pcspOff, pcfileOff, pclnOff, cuOff int) _func {
fdata := _func{
entryoff: uint32(0),
nameoff: int32(nameOff),
args: int32(symbol.Func.Args),
deferreturn: uint32(0),
pcsp: uint32(spOff),
pcsp: uint32(pcspOff),
pcfile: uint32(pcfileOff),
pcln: uint32(pclnOff),
npcdata: uint32(len(symbol.Func.PCData)),
Expand Down
4 changes: 2 additions & 2 deletions func.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ type _func struct {
nfuncdata int32
}

func initfunc(symbol *obj.ObjSymbol, nameOff, spOff, pcfileOff, pclnOff, cuOff int) _func {
func initfunc(symbol *obj.ObjSymbol, nameOff, pcspOff, pcfileOff, pclnOff, cuOff int) _func {
fdata := _func{
entry: uintptr(0),
nameoff: int32(nameOff),
args: int32(symbol.Func.Args),
pcsp: int32(spOff),
pcsp: int32(pcspOff),
pcfile: int32(pcfileOff),
pcln: int32(pclnOff),
npcdata: int32(len(symbol.Func.PCData)),
Expand Down
4 changes: 2 additions & 2 deletions gcdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const (
func generategcdata(linker *Linker, codeModule *CodeModule, symbolMap map[string]uintptr, w *gcprog.Writer, sym *obj.Sym) error {
segment := &codeModule.segment
//if symbol is in loader, ignore generate gc data
if symbolMap[sym.Name] < uintptr(segment.dataBase) || symbolMap[sym.Name] > uintptr(segment.dataBase+segment.sumDataLen) {
if symbolMap[sym.Name] < uintptr(segment.dataBase) || symbolMap[sym.Name] > uintptr(segment.dataBase+segment.dataSeg.length) {
return nil
}
objsym := linker.objsymbolMap[sym.Name]
objsym := linker.objSymbolMap[sym.Name]
typeName := objsym.Type
if len(typeName) == 0 && objsym.Size > 0 {
// This is likely a global var with no type information encoded, so can't be GC'd (ignore it)
Expand Down
4 changes: 2 additions & 2 deletions inlinetree.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func (linker *Linker) addInlineTree(_func *_func, symbol *obj.ObjSymbol) (err er
bytes := make([]byte, len(Func.InlTree)*obj.InlinedCallSize)
for k, inl := range Func.InlTree {
funcID := uint8(0)
if _, ok := linker.objsymbolMap[inl.Func]; ok {
funcID = linker.objsymbolMap[inl.Func].Func.FuncID
if _, ok := linker.objSymbolMap[inl.Func]; ok {
funcID = linker.objSymbolMap[inl.Func].Func.FuncID
}
inlinedcall := obj.InitInlinedCall(inl, funcID, linker.nameMap, linker.filetab)
copy2Slice(bytes[k*obj.InlinedCallSize:], uintptr(unsafe.Pointer(&inlinedcall)), obj.InlinedCallSize)
Expand Down
115 changes: 65 additions & 50 deletions ld.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,30 @@ import (

// ourself defined struct
// code segment
type codeSeg struct {
codeByte []byte
codeBase int
length int
maxLen int
codeOff int
}

// data segment
type dataSeg struct {
dataByte []byte
dataBase int
length int
maxLen int
dataLen int
noptrdataLen int
bssLen int
noptrbssLen int
dataOff int
}

type segment struct {
codeByte []byte
dataByte []byte
codeBase int
dataBase int
sumDataLen int
dataLen int
noptrdataLen int
bssLen int
noptrbssLen int
codeLen int
maxCodeLength int
maxDataLength int
codeOff int
dataOff int
codeSeg
dataSeg
}

type Linker struct {
Expand All @@ -44,7 +53,7 @@ type Linker struct {
bss []byte
noptrbss []byte
symMap map[string]*obj.Sym
objsymbolMap map[string]*obj.ObjSymbol
objSymbolMap map[string]*obj.ObjSymbol
nameMap map[string]int
stringMap map[string]*string
filetab []uint32
Expand Down Expand Up @@ -72,7 +81,7 @@ var (
func initLinker() *Linker {
linker := &Linker{
symMap: make(map[string]*obj.Sym),
objsymbolMap: make(map[string]*obj.ObjSymbol),
objSymbolMap: make(map[string]*obj.ObjSymbol),
nameMap: make(map[string]int),
stringMap: make(map[string]*string),
}
Expand Down Expand Up @@ -104,7 +113,7 @@ func (linker *Linker) addFiles(files []string) {
func (linker *Linker) addSymbols() error {
//static_tmp is 0, golang compile not allocate memory.
linker.noptrdata = append(linker.noptrdata, make([]byte, IntSize)...)
for _, objSym := range linker.objsymbolMap {
for _, objSym := range linker.objSymbolMap {
if objSym.Kind == symkind.STEXT && objSym.DupOK == false {
if _, err := linker.addSymbol(objSym.Name); err != nil {
return err
Expand Down Expand Up @@ -142,7 +151,7 @@ func (linker *Linker) addSymbol(name string) (symbol *obj.Sym, err error) {
if symbol, ok := linker.symMap[name]; ok {
return symbol, nil
}
objsym := linker.objsymbolMap[name]
objsym := linker.objSymbolMap[name]
symbol = &obj.Sym{Name: objsym.Name, Kind: objsym.Kind}
linker.symMap[symbol.Name] = symbol

Expand All @@ -156,7 +165,7 @@ func (linker *Linker) addSymbol(name string) (symbol *obj.Sym, err error) {
}
bytearrayAlignNops(linker.Arch, &linker.code, funcalign.GetFuncAlign(linker.Arch))
symbol.Func = &obj.Func{}
if err := linker.readFuncData(linker.objsymbolMap[name], symbol.Offset); err != nil {
if err := linker.readFuncData(linker.objSymbolMap[name], symbol.Offset); err != nil {
return nil, err
}
case symkind.SDATA:
Expand Down Expand Up @@ -193,12 +202,12 @@ func (linker *Linker) addSymbol(name string) (symbol *obj.Sym, err error) {
if reloc.Epilogue.Offset != 0 {
reloc.Epilogue.Offset += symbol.Offset
}
if _, ok := linker.objsymbolMap[reloc.Sym.Name]; ok {
if _, ok := linker.objSymbolMap[reloc.Sym.Name]; ok {
reloc.Sym, err = linker.addSymbol(reloc.Sym.Name)
if err != nil {
return nil, err
}
if len(linker.objsymbolMap[reloc.Sym.Name].Data) == 0 && reloc.Size > 0 {
if len(linker.objSymbolMap[reloc.Sym.Name].Data) == 0 && reloc.Size > 0 {
//static_tmp is 0, golang compile not allocate memory.
//goloader add IntSize bytes on linker.noptrdata[0]
if reloc.Size <= IntSize {
Expand Down Expand Up @@ -252,7 +261,7 @@ func (linker *Linker) addSymbol(name string) (symbol *obj.Sym, err error) {

if objsym.Type != EmptyString {
if _, ok := linker.symMap[objsym.Type]; !ok {
if _, ok := linker.objsymbolMap[objsym.Type]; !ok {
if _, ok := linker.objSymbolMap[objsym.Type]; !ok {
linker.symMap[objsym.Type] = &obj.Sym{Name: objsym.Type, Offset: InvalidOffset}
}
}
Expand Down Expand Up @@ -307,7 +316,7 @@ func (linker *Linker) readFuncData(symbol *obj.ObjSymbol, codeLen int) (err erro
Func.FuncData = append(Func.FuncData, (uintptr)(0))
} else {
if _, ok := linker.symMap[name]; !ok {
if _, ok := linker.objsymbolMap[name]; ok {
if _, ok := linker.objSymbolMap[name]; ok {
if _, err = linker.addSymbol(name); err != nil {
return err
}
Expand Down Expand Up @@ -478,46 +487,52 @@ func Load(linker *Linker, symPtr map[string]uintptr) (codeModule *CodeModule, er
Syms: make(map[string]uintptr),
module: &moduledata{typemap: nil},
}
codeModule.codeLen = len(linker.code)
codeModule.dataLen = len(linker.data)
codeModule.noptrdataLen = len(linker.noptrdata)
codeModule.bssLen = len(linker.bss)
codeModule.noptrbssLen = len(linker.noptrbss)
codeModule.sumDataLen = codeModule.dataLen + codeModule.noptrdataLen + codeModule.bssLen + codeModule.noptrbssLen
codeModule.maxCodeLength = alignof((codeModule.codeLen)*2, PageSize)
codeModule.maxDataLength = alignof((codeModule.sumDataLen)*2, PageSize)
codeByte, err := Mmap(codeModule.maxCodeLength)

//init code segment
codeSeg := &codeModule.segment.codeSeg
codeSeg.length = len(linker.code)
codeSeg.maxLen = alignof((codeSeg.length)*2, PageSize)
codeByte, err := Mmap(codeSeg.maxLen)
if err != nil {
return nil, err
}
dataByte, err := MmapData(codeModule.maxDataLength)
codeSeg.codeByte = codeByte
codeSeg.codeBase = int((*sliceHeader)(unsafe.Pointer(&codeByte)).Data)
copy(codeSeg.codeByte, linker.code)
codeSeg.codeOff = codeSeg.length

//init data segment
dataSeg := &codeModule.segment.dataSeg
dataSeg.dataLen = len(linker.data)
dataSeg.noptrdataLen = len(linker.noptrdata)
dataSeg.bssLen = len(linker.bss)
dataSeg.noptrbssLen = len(linker.noptrbss)
dataSeg.length = dataSeg.dataLen + dataSeg.noptrdataLen + dataSeg.bssLen + dataSeg.noptrbssLen
dataSeg.maxLen = alignof((dataSeg.length)*2, PageSize)
dataSeg.dataOff = 0
dataByte, err := MmapData(dataSeg.maxLen)
if err != nil {
Munmap(dataSeg.dataByte)
return nil, err
}

codeModule.codeByte = codeByte
codeModule.codeBase = int((*sliceHeader)(unsafe.Pointer(&codeByte)).Data)
copy(codeModule.codeByte, linker.code)
codeModule.codeOff = codeModule.codeLen

codeModule.dataByte = dataByte
codeModule.dataBase = int((*sliceHeader)(unsafe.Pointer(&dataByte)).Data)
copy(codeModule.dataByte[codeModule.dataOff:], linker.data)
codeModule.dataOff = codeModule.dataLen
copy(codeModule.dataByte[codeModule.dataOff:], linker.noptrdata)
codeModule.dataOff += codeModule.noptrdataLen
copy(codeModule.dataByte[codeModule.dataOff:], linker.bss)
codeModule.dataOff += codeModule.bssLen
copy(codeModule.dataByte[codeModule.dataOff:], linker.noptrbss)
codeModule.dataOff += codeModule.noptrbssLen
dataSeg.dataByte = dataByte
dataSeg.dataBase = int((*sliceHeader)(unsafe.Pointer(&dataByte)).Data)
copy(dataSeg.dataByte[dataSeg.dataOff:], linker.data)
dataSeg.dataOff = dataSeg.dataLen
copy(dataSeg.dataByte[dataSeg.dataOff:], linker.noptrdata)
dataSeg.dataOff += dataSeg.noptrdataLen
copy(dataSeg.dataByte[dataSeg.dataOff:], linker.bss)
dataSeg.dataOff += dataSeg.bssLen
copy(dataSeg.dataByte[dataSeg.dataOff:], linker.noptrbss)
dataSeg.dataOff += dataSeg.noptrbssLen

codeModule.stringMap = linker.stringMap

var symbolMap map[string]uintptr
if symbolMap, err = linker.addSymbolMap(symPtr, codeModule); err == nil {
if err = linker.relocate(codeModule, symbolMap, symPtr); err == nil {
if err = linker.buildModule(codeModule, symbolMap); err == nil {
MakeThreadJITCodeExecutable(uintptr(codeModule.codeBase), codeModule.maxCodeLength)
MakeThreadJITCodeExecutable(uintptr(codeModule.codeBase), codeSeg.maxLen)
if err = linker.doInitialize(symPtr, symbolMap); err == nil {
return codeModule, err
}
Expand Down
6 changes: 3 additions & 3 deletions obj/readobj.1.16.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func (pkg *Pkg) Symbols() error {
a, err := archive.Parse(pkg.F, false)
a, err := archive.Parse(pkg.File, false)
if err != nil {
return err
}
Expand All @@ -24,7 +24,7 @@ func (pkg *Pkg) Symbols() error {
//nothing todo
case archive.EntryGoObj:
b := make([]byte, e.Obj.Size)
_, err := pkg.F.ReadAt(b, e.Obj.Offset)
_, err := pkg.File.ReadAt(b, e.Obj.Offset)
if err != nil {
return err
}
Expand All @@ -45,7 +45,7 @@ func (pkg *Pkg) Symbols() error {
pkg.addSym(r, uint32(i), &refNames)
}
default:
return fmt.Errorf("Parse open %s: unrecognized archive member %s\n", pkg.F.Name(), e.Name)
return fmt.Errorf("Parse open %s: unrecognized archive member %s\n", pkg.File.Name(), e.Name)
}
}
for _, sym := range pkg.Syms {
Expand Down
4 changes: 2 additions & 2 deletions obj/readobj.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ func (r *readAtSeeker) BytesAt(offset, size int64) (bytes []byte, err error) {
}

func (pkg *Pkg) Symbols() error {
obj, err := goobj.Parse(pkg.F, pkg.PkgPath)
obj, err := goobj.Parse(pkg.File, pkg.PkgPath)
if err != nil {
return fmt.Errorf("read error: %v", err)
}
pkg.Arch = obj.Arch
fd := readAtSeeker{ReadSeeker: pkg.F}
fd := readAtSeeker{ReadSeeker: pkg.File}
for _, sym := range obj.Syms {
symbol := &ObjSymbol{}
symbol.Name = sym.Name
Expand Down
2 changes: 1 addition & 1 deletion obj/readobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Pkg struct {
Syms map[string]*ObjSymbol
Arch string
PkgPath string
F *os.File
File *os.File
CUFiles []string
}

Expand Down
Loading

0 comments on commit 328a5ad

Please sign in to comment.