]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: reorder modules so main.main comes first
authorDavid Crawshaw <crawshaw@golang.org>
Wed, 25 Jan 2017 04:19:36 +0000 (20:19 -0800)
committerDavid Crawshaw <crawshaw@golang.org>
Wed, 25 Jan 2017 22:33:57 +0000 (22:33 +0000)
Modules appear in the moduledata linked list in the order they are
loaded by the dynamic loader, with one exception: the
firstmoduledata itself the module that contains the runtime.
This is not always the first module (when using -buildmode=shared,
it is typically libstd.so, the second module).

The order matters for typelinksinit, so we swap the first module
with whatever module contains the main function.

Updates #18729

This fixes the test case extracted with -linkshared, and now

go test -linkshared encoding/...

passes. However the original issue about a plugin failure is not
yet fixed.

Change-Id: I9f399ecc3518e22e6b0a350358e90b0baa44ac96
Reviewed-on: https://go-review.googlesource.com/35644
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Michael Hudson-Doyle <michael.hudson@canonical.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/cgo/testshared/src/depBase/dep.go
misc/cgo/testshared/src/exe/exe.go
src/runtime/symtab.go

index a518b4efe2955e785b7c46fddf9513bf89902624..9f86710db01a4438fe4ab7fb7e8f3b13829592f5 100644 (file)
@@ -5,6 +5,8 @@ import (
        "reflect"
 )
 
+var SlicePtr interface{} = &[]int{}
+
 var V int = 1
 
 var HasMask []string = []string{"hi"}
index 433727112b13166d1bdc71a5c82e73e32b7b422b..84302a811f0919b9035b8da22814581af1d122a0 100644 (file)
@@ -19,6 +19,8 @@ func F() *C {
        return nil
 }
 
+var slicePtr interface{} = &[]int{}
+
 func main() {
        defer depBase.ImplementedInAsm()
        // This code below causes various go.itab.* symbols to be generated in
@@ -32,4 +34,11 @@ func main() {
        if reflect.TypeOf(F).Out(0) != reflect.TypeOf(c) {
                panic("bad reflection results, see golang.org/issue/18252")
        }
+
+       sp := reflect.New(reflect.TypeOf(slicePtr).Elem())
+       s := sp.Interface()
+
+       if reflect.TypeOf(s) != reflect.TypeOf(slicePtr) {
+               panic("bad reflection results, see golang.org/issue/18729")
+       }
 }
index f52190661cee83ee80e69eddb2fba1d775101a45..ed82783ca969f328c2ec11ad0e1ffd35710b2203 100644 (file)
@@ -285,6 +285,25 @@ func modulesinit() {
                        md.gcbssmask = progToPointerMask((*byte)(unsafe.Pointer(md.gcbss)), md.ebss-md.bss)
                }
        }
+
+       // Modules appear in the moduledata linked list in the order they are
+       // loaded by the dynamic loader, with one exception: the
+       // firstmoduledata itself the module that contains the runtime. This
+       // is not always the first module (when using -buildmode=shared, it
+       // is typically libstd.so, the second module). The order matters for
+       // typelinksinit, so we swap the first module with whatever module
+       // contains the main function.
+       //
+       // See Issue #18729.
+       mainText := funcPC(main_main)
+       for i, md := range *modules {
+               if md.text <= mainText && mainText <= md.etext {
+                       (*modules)[0] = md
+                       (*modules)[i] = &firstmoduledata
+                       break
+               }
+       }
+
        atomicstorep(unsafe.Pointer(&modulesSlice), unsafe.Pointer(modules))
 }