Commit 8da54a4e authored by Srdjan Petrovic's avatar Srdjan Petrovic Committed by Ian Lance Taylor

cmd: linker changes for shared library initialization

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: 's avatarIan Lance Taylor <iant@golang.org>
parent ff1f3a11
...@@ -936,6 +936,15 @@ func Addstring(s *LSym, str string) int64 { ...@@ -936,6 +936,15 @@ func Addstring(s *LSym, str string) int64 {
return int64(r) 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() { func dosymtype() {
for s := Ctxt.Allsym; s != nil; s = s.Allsym { for s := Ctxt.Allsym; s != nil; s = s.Allsym {
if len(s.P) > 0 { if len(s.P) > 0 {
...@@ -946,6 +955,11 @@ func dosymtype() { ...@@ -946,6 +955,11 @@ func dosymtype() {
s.Type = SNOPTRDATA 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)
}
} }
} }
......
...@@ -1362,6 +1362,7 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) { ...@@ -1362,6 +1362,7 @@ func genasmsym(put func(*LSym, string, int, int64, int64, int, *LSym)) {
SRODATA, SRODATA,
SSYMTAB, SSYMTAB,
SPCLNTAB, SPCLNTAB,
SINITARR,
SDATA, SDATA,
SNOPTRDATA, SNOPTRDATA,
SELFROSECT, SELFROSECT,
......
...@@ -10,6 +10,13 @@ TEXT _rt0_amd64_linux(SB),NOSPLIT,$-8 ...@@ -10,6 +10,13 @@ TEXT _rt0_amd64_linux(SB),NOSPLIT,$-8
MOVQ $main(SB), AX MOVQ $main(SB), AX
JMP 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 TEXT main(SB),NOSPLIT,$-8
MOVQ $runtime·rt0_go(SB), AX MOVQ $runtime·rt0_go(SB), AX
JMP AX JMP AX
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment