Skip to content

Commit

Permalink
change unnecessary exported function to lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
pkujhd committed Jun 5, 2020
1 parent d8a1b3c commit 350d172
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func dumpPCData(b []byte, prefix string) {
}

func dumpStackMap(f interface{}) {
finfo := findfunc(getFuncPtr(f))
finfo := findfunc(getFunctionPtr(f))
fmt.Println(funcname(finfo))
stkmap := (*stackmap)(funcdata(finfo, _FUNCDATA_LocalsPointerMaps))
fmt.Printf("%v %p\n", stkmap, stkmap)
Expand Down
10 changes: 5 additions & 5 deletions dymcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func addSymAddrs(code *CodeReloc, symPtr map[string]uintptr, codeModule *CodeMod
seg.errors += fmt.Sprintf("unresolve external:%s\n", sym.Name)
}
} else if sym.Name == TLSNAME {
RegTLS(symPtr, sym.Offset)
regTLS(symPtr, sym.Offset)
} else if sym.Kind == STEXT {
seg.symAddrs[i] = uintptr(code.Syms[i].Offset + seg.codeBase)
codeModule.Syms[sym.Name] = uintptr(seg.symAddrs[i])
Expand Down Expand Up @@ -257,7 +257,7 @@ func relocateItab(code *CodeReloc, module *CodeModule, seg *segment) {
seg.offset += ItabSize
} else if seg.codeByte[iter.Offset-2] == x86amd64LEAcode {
seg.codeByte[iter.Offset-2:][0] = x86amd64MOVcode
*(*uintptr)(unsafe.Pointer(&(seg.codeByte[seg.offset:][0]))) = address
putAddress(seg.codeByte[seg.offset:], uint64(address))
seg.offset += PtrSize
} else {
seg.errors += fmt.Sprintf("relocateItab: not support code:%v!\n", seg.codeByte[iter.Offset-2:iter.Offset])
Expand All @@ -268,7 +268,7 @@ func relocateItab(code *CodeReloc, module *CodeModule, seg *segment) {
case R_ADDRARM64:
relocateADRP(seg.codeByte[iter.Offset:], iter.Reloc, seg, address, itabName)
case R_ADDR:
*(*uintptr)(unsafe.Pointer(&(seg.codeByte[iter.Offset:][0]))) = uintptr(int(address) + iter.Add)
putAddress(seg.codeByte[iter.Offset:], uint64(int(address)+iter.Add))
default:
seg.errors += fmt.Sprintf("unknown relocateItab type:%d Name:%s\n", iter.Type, itabName)
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func relocate(code *CodeReloc, symPtr map[string]uintptr, codeModule *CodeModule
copy(seg.codeByte[seg.offset:], armcode)
seg.offset += len(armcode)
}
*(*uintptr)(unsafe.Pointer(&(seg.codeByte[seg.offset:][0]))) = uintptr(int(addr) + add)
putAddress(seg.codeByte[seg.offset:], uint64(int(addr)+add))
seg.offset += PtrSize
}
} else {
Expand All @@ -407,7 +407,7 @@ func relocate(code *CodeReloc, symPtr map[string]uintptr, codeModule *CodeModule
relocByte = seg.codeByte
}
address := uintptr(int(addr) + loc.Add)
*(*uintptr)(unsafe.Pointer(&(relocByte[loc.Offset:][0]))) = uintptr(address)
putAddress(relocByte[loc.Offset:], uint64(address))
case R_CALLIND:

case R_ADDROFF, R_WEAKADDROFF, R_METHODOFF:
Expand Down
2 changes: 1 addition & 1 deletion iface.1.10.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func eraseiface(inter *interfacetype, typ *_type) bool {
// Use atomic read here so if we see m != nil, we also see
// the initializations of the fields of m.
// m := *p
m := (*itab)(Loadp(unsafe.Pointer(p)))
m := (*itab)(loadp(unsafe.Pointer(p)))
if m == nil {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion iface.1.8.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func eraseiface(inter *interfacetype, typ *_type) bool {
defer unlock(&ifaceLock)
h := itabhash(inter, typ)
var m, last *itab = nil, nil
for m = (*itab)(Loadp(unsafe.Pointer(&hash[h]))); m != nil; m = m.link {
for m = (*itab)(loadp(unsafe.Pointer(&hash[h]))); m != nil; m = m.link {
if m.inter == inter && m._type == typ {
if last == nil {
atomicstorep(unsafe.Pointer(&hash[h]), unsafe.Pointer(nil))
Expand Down
4 changes: 2 additions & 2 deletions readobj.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func readObj(f *os.File, reloc *CodeReloc, objsymmap map[string]objSym, pkgpath
func ReadObj(f *os.File) (*CodeReloc, error) {
reloc := CodeReloc{SymMap: make(map[string]int), GCObjs: make(map[string]uintptr), FileMap: make(map[string]int)}
reloc.Mod.pclntable = append(reloc.Mod.pclntable, x86moduleHead...)
var objsymmap = make(map[string]objSym)
objsymmap := make(map[string]objSym)
err := readObj(f, &reloc, objsymmap, nil)
if err != nil {
return nil, err
Expand All @@ -53,7 +53,7 @@ func ReadObj(f *os.File) (*CodeReloc, error) {
func ReadObjs(files []string, pkgPath []string) (*CodeReloc, error) {
reloc := CodeReloc{SymMap: make(map[string]int), GCObjs: make(map[string]uintptr), FileMap: make(map[string]int)}
reloc.Mod.pclntable = append(reloc.Mod.pclntable, x86moduleHead...)
var objsymmap = make(map[string]objSym)
objsymmap := make(map[string]objSym)
for i, file := range files {
f, err := os.Open(file)
if err != nil {
Expand Down
35 changes: 16 additions & 19 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package goloader

import (
"cmd/objfile/objfile"
"encoding/binary"
"os"
"reflect"
"strings"
"unsafe"
)

// See reflect/value.go emptyInterface
type interfaceHeader struct {
type emptyInterface struct {
typ unsafe.Pointer
word unsafe.Pointer
}
Expand Down Expand Up @@ -89,13 +88,13 @@ func RegSymbol(symPtr map[string]uintptr) error {
symPtr[sym.Name] = uintptr(sym.Addr)
}
if strings.HasPrefix(sym.Name, ITAB_PREFIX) {
RegItab(symPtr, sym.Name, uintptr(sym.Addr))
regItab(symPtr, sym.Name, uintptr(sym.Addr))
}
}
return nil
}

func RegItab(symPtr map[string]uintptr, name string, addr uintptr) {
func regItab(symPtr map[string]uintptr, name string, addr uintptr) {
symPtr[name] = uintptr(addr)
bss := strings.Split(strings.TrimLeft(name, ITAB_PREFIX), ",")
slice := sliceHeader{addr, len(bss), len(bss)}
Expand All @@ -104,29 +103,27 @@ func RegItab(symPtr map[string]uintptr, name string, addr uintptr) {
tname := bss[len(bss)-i-1]
if tname[0] == '*' {
obj := reflect.TypeOf(0)
(*interfaceHeader)(unsafe.Pointer(&obj)).word = ptr
(*emptyInterface)(unsafe.Pointer(&obj)).word = ptr
obj = obj.(reflect.Type).Elem()
symPtr[TYPE_PREFIX+tname[1:]] = uintptr((*interfaceHeader)(unsafe.Pointer(&obj)).word)
symPtr[TYPE_PREFIX+tname[1:]] = uintptr((*emptyInterface)(unsafe.Pointer(&obj)).word)
}
symPtr[TYPE_PREFIX+tname] = uintptr(ptr)
}
}

func RegTLS(symPtr map[string]uintptr, offset int) {
var ptr interface{} = RegSymbol
slice := sliceHeader{*(*uintptr)((*interfaceHeader)(unsafe.Pointer(&ptr)).word), offset + 4, offset + 4}
bytes := *(*[]byte)(unsafe.Pointer(&slice))
symPtr[TLSNAME] = uintptr(binary.LittleEndian.Uint32(bytes[offset:]))
func regTLS(symPtr map[string]uintptr, offset int) {
//FUNCTION HEADER
//asm: MOVQ (TLS), CX
//bytes: 0x488b0c2500000000
funcptr := getFunctionPtr(regTLS)
tlsptr := *(*uint32)(adduintptr(funcptr, offset))
symPtr[TLSNAME] = uintptr(tlsptr)
}

func RegType(symPtr map[string]uintptr, name string, typ interface{}) {
symPtr[name] = uintptr((*interfaceHeader)(unsafe.Pointer(&typ)).typ)
func regFunc(symPtr map[string]uintptr, name string, function interface{}) {
symPtr[name] = getFunctionPtr(function)
}

func RegFunc(symPtr map[string]uintptr, name string, f interface{}) {
symPtr[name] = getFuncPtr(f)
}

func getFuncPtr(f interface{}) uintptr {
return *(*uintptr)((*interfaceHeader)(unsafe.Pointer(&f)).word)
func getFunctionPtr(function interface{}) uintptr {
return *(*uintptr)((*emptyInterface)(unsafe.Pointer(&function)).word)
}
13 changes: 6 additions & 7 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,25 @@ func (t *_type) PkgPath() string {
func RegTypes(symPtr map[string]uintptr, interfaces ...interface{}) {
for _, inter := range interfaces {
v := reflect.ValueOf(inter)
registerTypeInfo(symPtr, v)
regType(symPtr, v)
if v.Kind() == reflect.Ptr {
registerTypeInfo(symPtr, v.Elem())
regType(symPtr, v.Elem())
}
}
}

func registerTypeInfo(symPtr map[string]uintptr, v reflect.Value) {
func regType(symPtr map[string]uintptr, v reflect.Value) {
inter := v.Interface()
header := (*interfaceHeader)(unsafe.Pointer(&inter))

if v.Kind() == reflect.Func && uintptr(header.word) != 0 {
symPtr[runtime.FuncForPC(v.Pointer()).Name()] = *(*uintptr)(header.word)
if v.Kind() == reflect.Func && getFunctionPtr(inter) != 0 {
symPtr[runtime.FuncForPC(v.Pointer()).Name()] = getFunctionPtr(inter)
} else {
name := TYPE_PREFIX
symname := v.Type().String()
if v.Type().Kind() == reflect.Ptr {
name += symname[:1]
symname = symname[1:]
}
header := (*emptyInterface)(unsafe.Pointer(&inter))
pkgPath := (*_type)(header.typ).PkgPath()
lastSlash := strings.LastIndexByte(pkgPath, '/')
if lastSlash > -1 {
Expand Down
6 changes: 5 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
//go:linkname add runtime.add
func add(p unsafe.Pointer, x uintptr) unsafe.Pointer

//go:linkname adduintptr runtime.add
func adduintptr(p uintptr, x int) unsafe.Pointer

func putUint24(b []byte, v uint32) {
_ = b[2] // early bounds check to guarantee safety of writes below
b[0] = byte(v)
Expand Down Expand Up @@ -53,6 +56,7 @@ func copy2Slice(dst []byte, src uintptr, size int) {

//go:nosplit
//go:noinline
func Loadp(ptr unsafe.Pointer) unsafe.Pointer {
//see runtime.internal.atomic.Loadp
func loadp(ptr unsafe.Pointer) unsafe.Pointer {
return *(*unsafe.Pointer)(ptr)
}

0 comments on commit 350d172

Please sign in to comment.