From 350d17294b859a18d14788d98d65e24c4ab08350 Mon Sep 17 00:00:00 2001 From: pkujhd Date: Fri, 5 Jun 2020 18:37:30 +0800 Subject: [PATCH] change unnecessary exported function to lowercase --- debug.go | 2 +- dymcode.go | 10 +++++----- iface.1.10.go | 2 +- iface.1.8.go | 2 +- readobj.go | 4 ++-- register.go | 35 ++++++++++++++++------------------- type.go | 13 ++++++------- utils.go | 6 +++++- 8 files changed, 37 insertions(+), 37 deletions(-) diff --git a/debug.go b/debug.go index 09be6ec8..51553b35 100644 --- a/debug.go +++ b/debug.go @@ -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) diff --git a/dymcode.go b/dymcode.go index 97d75616..7eaa77ff 100644 --- a/dymcode.go +++ b/dymcode.go @@ -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]) @@ -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]) @@ -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) } @@ -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 { @@ -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: diff --git a/iface.1.10.go b/iface.1.10.go index b561ef33..0432c43f 100644 --- a/iface.1.10.go +++ b/iface.1.10.go @@ -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 } diff --git a/iface.1.8.go b/iface.1.8.go index ca10d8c1..f79389a7 100644 --- a/iface.1.8.go +++ b/iface.1.8.go @@ -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)) diff --git a/readobj.go b/readobj.go index 264d2332..52ca0efe 100644 --- a/readobj.go +++ b/readobj.go @@ -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 @@ -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 { diff --git a/register.go b/register.go index 1026d62a..28b65877 100644 --- a/register.go +++ b/register.go @@ -2,7 +2,6 @@ package goloader import ( "cmd/objfile/objfile" - "encoding/binary" "os" "reflect" "strings" @@ -10,7 +9,7 @@ import ( ) // See reflect/value.go emptyInterface -type interfaceHeader struct { +type emptyInterface struct { typ unsafe.Pointer word unsafe.Pointer } @@ -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)} @@ -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) } diff --git a/type.go b/type.go index b5673dde..ff5a7bae 100644 --- a/type.go +++ b/type.go @@ -61,19 +61,17 @@ 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() @@ -81,6 +79,7 @@ func registerTypeInfo(symPtr map[string]uintptr, v reflect.Value) { name += symname[:1] symname = symname[1:] } + header := (*emptyInterface)(unsafe.Pointer(&inter)) pkgPath := (*_type)(header.typ).PkgPath() lastSlash := strings.LastIndexByte(pkgPath, '/') if lastSlash > -1 { diff --git a/utils.go b/utils.go index 32dffcf1..07f7d041 100644 --- a/utils.go +++ b/utils.go @@ -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) @@ -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) }