]> Cypherpunks repositories - gostls13.git/commitdiff
cmd: linker changes for shared library initialization
authorSrdjan Petrovic <spetrovic@google.com>
Tue, 17 Mar 2015 16:47:01 +0000 (09:47 -0700)
committerIan Lance Taylor <iant@golang.org>
Fri, 27 Mar 2015 22:52:10 +0000 (22:52 +0000)
Suggested by iant@, this change:
  - looks for a symbol _rt0_<GOARCH>_<GOOS>_lib,
  - if the symbol is present, adds a new entry into the .init_array ELF
    section that points to the symbol.

The end-effect is that the symbol _rt0_<GOARCH>_<GOOS>_lib will be
invoked as soon as the (ELF) shared library is loaded, which will in turn
initialize the runtime. (To be implemented.)

Change-Id: I99911a180215a6df18f8a18483d12b9b497b48f4
Reviewed-on: https://go-review.googlesource.com/7692
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/internal/ld/data.go
src/cmd/internal/ld/lib.go
src/runtime/rt0_linux_amd64.s

index b7de5af9fb378adad08a40e5f4b2265ff58d44b3..ea44ca95c1d87f82f4d6c7694d78f8d299df0f6d 100644 (file)
@@ -936,6 +936,15 @@ func Addstring(s *LSym, str string) int64 {
        return int64(r)
 }
 
+func addinitarrdata(s *LSym) {
+       p := s.Name + ".ptr"
+       sp := Linklookup(Ctxt, p, 0)
+       sp.Type = SINITARR
+       sp.Size = 0
+       sp.Dupok = 1
+       Addaddr(Ctxt, sp, s)
+}
+
 func dosymtype() {
        for s := Ctxt.Allsym; s != nil; s = s.Allsym {
                if len(s.P) > 0 {
@@ -946,6 +955,11 @@ func dosymtype() {
                                s.Type = SNOPTRDATA
                        }
                }
+               // Create a new entry in the .init_array section that points to the
+               // library initializer function.
+               if Flag_shared != 0 && s.Name == INITENTRY {
+                       addinitarrdata(s)
+               }
        }
 }
 
index 148ada714b4d6af8db7c4771cdb6a9d86fcb1d78..caca87c180c5e5e6a3b0aed6def586cb73e8bfc8 100644 (file)
@@ -1362,6 +1362,7 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) {
                        SRODATA,
                        SSYMTAB,
                        SPCLNTAB,
+                       SINITARR,
                        SDATA,
                        SNOPTRDATA,
                        SELFROSECT,
index 985426acc468b377b834e7c9d99202c109464de1..9d9cb3412892f59a91b5193756837a7b69972ac4 100644 (file)
@@ -10,6 +10,13 @@ TEXT _rt0_amd64_linux(SB),NOSPLIT,$-8
        MOVQ    $main(SB), AX
        JMP     AX
 
+// When linking with -shared, this symbol is called when the shared library
+// is loaded.
+TEXT _rt0_amd64_linux_lib(SB),NOSPLIT,$0
+       // TODO(spetrovic): Do something useful, like calling $main.  (Note that
+       // this has to be done in a separate thread, as main is expected to block.)
+       RET
+
 TEXT main(SB),NOSPLIT,$-8
        MOVQ    $runtime·rt0_go(SB), AX
        JMP     AX