Skip to content

Commit bee4206

Browse files
committed
runtime: have typelinksinit work forwards
For reasons I have forgotten typelinksinit processed modules backwards. (I suspect this was an attempt to process types in the executing binary first.) It does not appear to be necessary, and it is not the order we want when a module can be loaded at an arbitrary point during a program's execution as a plugin. So reverse the order. While here, make it safe to call typelinksinit multiple times. Change-Id: Ie10587c55c8e5efa0542981efb6eb3c12dd59e8c Reviewed-on: https://go-review.googlesource.com/27822 Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 8f3c8a3 commit bee4206

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/runtime/type.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,11 @@ func typelinksinit() {
446446
if firstmoduledata.next == nil {
447447
return
448448
}
449-
typehash := make(map[uint32][]*_type)
449+
typehash := make(map[uint32][]*_type, len(firstmoduledata.typelinks))
450450

451-
modules := []*moduledata{}
452-
for md := &firstmoduledata; md != nil; md = md.next {
453-
modules = append(modules, md)
454-
}
455-
prev, modules := modules[len(modules)-1], modules[:len(modules)-1]
456-
for len(modules) > 0 {
451+
prev := &firstmoduledata
452+
md := firstmoduledata.next
453+
for md != nil {
457454
// Collect types from the previous module into typehash.
458455
collect:
459456
for _, tl := range prev.typelinks {
@@ -473,23 +470,25 @@ func typelinksinit() {
473470
typehash[t.hash] = append(tlist, t)
474471
}
475472

476-
// If any of this module's typelinks match a type from a
477-
// prior module, prefer that prior type by adding the offset
478-
// to this module's typemap.
479-
md := modules[len(modules)-1]
480-
md.typemap = make(map[typeOff]*_type, len(md.typelinks))
481-
for _, tl := range md.typelinks {
482-
t := (*_type)(unsafe.Pointer(md.types + uintptr(tl)))
483-
for _, candidate := range typehash[t.hash] {
484-
if typesEqual(t, candidate) {
485-
t = candidate
486-
break
473+
if md.typemap == nil {
474+
// If any of this module's typelinks match a type from a
475+
// prior module, prefer that prior type by adding the offset
476+
// to this module's typemap.
477+
md.typemap = make(map[typeOff]*_type, len(md.typelinks))
478+
for _, tl := range md.typelinks {
479+
t := (*_type)(unsafe.Pointer(md.types + uintptr(tl)))
480+
for _, candidate := range typehash[t.hash] {
481+
if typesEqual(t, candidate) {
482+
t = candidate
483+
break
484+
}
487485
}
486+
md.typemap[typeOff(tl)] = t
488487
}
489-
md.typemap[typeOff(tl)] = t
490488
}
491489

492-
prev, modules = md, modules[:len(modules)-1]
490+
prev = md
491+
md = md.next
493492
}
494493
}
495494

0 commit comments

Comments
 (0)