Skip to content

Commit

Permalink
Avoids 'go.info.runtime.itabTable: relocation target go.info.*github.…
Browse files Browse the repository at this point in the history
…com/pkujhd/goloader.itabTableType not defined' error
  • Loading branch information
pkujhd committed Mar 8, 2023
1 parent 2b7ad33 commit 0b5d8fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
23 changes: 15 additions & 8 deletions iface.1.10.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,33 @@ type itabTableType struct {
entries [itabInitSize]*itab // really [size] large
}

//go:linkname itabTable runtime.itabTable
var itabTable *itabTableType // pointer to current table
//go:linkname __itabTable runtime.itabTable
var __itabTable unsafe.Pointer // pointer to current table

//go:linkname itabLock runtime.itabLock
var itabLock mutex
// Avoids "go.info.runtime.itabTable: relocation target go.info.*github.com/pkujhd/goloader.itabTableType not defined"
var itabTable = *(**itabTableType)(unsafe.Pointer(&__itabTable))

//go:linkname __itabLock runtime.itabLock
var __itabLock uintptr

// Avoids "go.info.runtime.itabLock: relocation target go.info.github.com/pkujhd/goloader.mutex not defined"
var itabLock = (*mutex)(unsafe.Pointer(&__itabLock))

//go:linkname itabAdd runtime.itabAdd
func itabAdd(m *itab)

func additabs(module *moduledata) {
lock(&itabLock)
lock(itabLock)
for _, itab := range module.itablinks {
itabAdd(itab)
}
unlock(&itabLock)
unlock(itabLock)
}

func removeitabs(module *moduledata) bool {
lock(&itabLock)
defer unlock(&itabLock)
lock(itabLock)
defer unlock(itabLock)

for i := uintptr(0); i < itabTable.size; i++ {
p := (**itab)(add(unsafe.Pointer(&itabTable.entries), i*PtrSize))
m := (*itab)(loadp(unsafe.Pointer(p)))
Expand Down
26 changes: 15 additions & 11 deletions iface.1.8.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// +build go1.8
// +build !go1.10
//go:build go1.8 && !go1.10
// +build go1.8,!go1.10

package goloader

Expand All @@ -23,17 +23,21 @@ type itab struct {
// See: src/runtime/iface.go
const hashSize = 1009

//go:linkname hash runtime.hash
var hash [hashSize]*itab
//go:linkname __hash runtime.hash
var __hash uintptr

//go:linkname ifaceLock runtime.ifaceLock
var ifaceLock mutex
var hash = (*[hashSize]*itab)(unsafe.Pointer(&__hash))

//go:linkname __ifaceLock runtime.ifaceLock
var __ifaceLock uintptr

var ifaceLock = (*mutex)(unsafe.Pointer(&__ifaceLock))

//go:linkname additab runtime.additab
func additab(m *itab, locked, canfail bool)

func additabs(module *moduledata) {
lock(&ifaceLock)
lock(ifaceLock)
for _, itab := range module.itablinks {
//golang1.8-1.9 not relocate itab.fun, in additab function, itab.fun is setted. but additab check type match,
//interface _type is in first moduledata, but type in loaded moduledata, set module.typemap[typeOff](trick)
Expand All @@ -52,15 +56,15 @@ func additabs(module *moduledata) {
additab(itab, true, false)
}
}
unlock(&ifaceLock)
unlock(ifaceLock)
}

func removeitabs(module *moduledata) bool {
lock(&ifaceLock)
defer unlock(&ifaceLock)
lock(ifaceLock)
defer unlock(ifaceLock)

//the itab alloc by runtime.persistentalloc, can't free
for index, h := range &hash {
for index, h := range hash {
last := h
for m := h; m != nil; m = m.link {
uintptrm := uintptr(unsafe.Pointer(m))
Expand Down

0 comments on commit 0b5d8fa

Please sign in to comment.