]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/internal/obj: use index for static symbols
authorCherry Zhang <cherryyz@google.com>
Fri, 18 Oct 2019 14:30:04 +0000 (10:30 -0400)
committerCherry Zhang <cherryyz@google.com>
Mon, 21 Oct 2019 21:57:32 +0000 (21:57 +0000)
In assembly we always reference symbols by name. But for static
symbols, as they are reachable only within the current file, we
can assign them local indices and use the indices to reference
them. The index is only meaningful locally, and it is fine.

Change-Id: I16e011cd41575ef703ceb6f35899e5fa58fbcf1e
Reviewed-on: https://go-review.googlesource.com/c/go/+/201997
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/internal/obj/sym.go

index 4c116d28f2b8cc6157585d6c31f3b828ba7a5c60..ab886bce36ccc85d4b50410b4b35f314e5358480 100644 (file)
@@ -184,7 +184,7 @@ func (ctxt *Link) NumberSyms(asm bool) {
 
        var idx, nonpkgidx int32 = 0, 0
        ctxt.traverseSyms(traverseDefs, func(s *LSym) {
-               if asm || s.Pkg == "_" || s.DuplicateOK() || ctxt.Flag_linkshared {
+               if isNonPkgSym(ctxt, asm, s) {
                        s.PkgIdx = goobj2.PkgIdxNone
                        s.SymIdx = nonpkgidx
                        if nonpkgidx != int32(len(ctxt.nonpkgdefs)) {
@@ -232,6 +232,31 @@ func (ctxt *Link) NumberSyms(asm bool) {
        })
 }
 
+// Returns whether s is a non-package symbol, which needs to be referenced
+// by name instead of by index.
+func isNonPkgSym(ctxt *Link, asm bool, s *LSym) bool {
+       if asm && !s.Static() {
+               // asm symbols are referenced by name only, except static symbols
+               // which are file-local and can be referenced by index.
+               return true
+       }
+       if ctxt.Flag_linkshared {
+               // The referenced symbol may be in a different shared library so
+               // the linker cannot see its index.
+               return true
+       }
+       if s.Pkg == "_" {
+               // The frontend uses package "_" to mark symbols that should not
+               // be referenced by index, e.g. linkname'd symbols.
+               return true
+       }
+       if s.DuplicateOK() {
+               // Dupok symbol needs to be dedup'd by name.
+               return true
+       }
+       return false
+}
+
 type traverseFlag uint32
 
 const (