From d33a5136e1a4513a3b1deacf1cc0677e070c7e8f Mon Sep 17 00:00:00 2001
From: Cherry Mui <cherryyz@google.com>
Date: Fri, 21 Apr 2023 12:02:30 -0400
Subject: [PATCH] cmd/link: use uint32 as symbol index
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Currently, a symbol's global index, the Sym type, is defined as an
int, which is 64-bit on 64-bit machines. We're unlikely to have
more than 4 billion symbols in the near future. Even if we will,
we will probably hit some other limit (e.g. section size) before
the symbol number limit. Use a 32-bit type to reduce memory usage.

E,g, linking cmd/compile in external linking mode (on macOS/amd64)

Munmap_GC    43.2M ± 0%     35.5M ± 1%   -17.74%  (p=0.000 n=16+20)

This brings the memory usage back before the previous CL, and even
lower.

Change-Id: Ie185f1586638fe70d8121312bfa9410942d518c7
Reviewed-on: https://go-review.googlesource.com/c/go/+/487416
Reviewed-by: Austin Clements <austin@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
---
 src/cmd/link/internal/ld/stackcheck.go | 2 +-
 src/cmd/link/internal/loader/loader.go | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/cmd/link/internal/ld/stackcheck.go b/src/cmd/link/internal/ld/stackcheck.go
index 24a96fb9968..98e7edaeb14 100644
--- a/src/cmd/link/internal/ld/stackcheck.go
+++ b/src/cmd/link/internal/ld/stackcheck.go
@@ -42,7 +42,7 @@ const stackCheckCycle int16 = 1<<15 - 1
 
 // stackCheckIndirect is a sentinel Sym value used to represent the
 // target of an indirect/closure call.
-const stackCheckIndirect loader.Sym = -1
+const stackCheckIndirect loader.Sym = ^loader.Sym(0)
 
 // doStackCheck walks the call tree to check that there is always
 // enough stack space for call frames, especially for a chain of
diff --git a/src/cmd/link/internal/loader/loader.go b/src/cmd/link/internal/loader/loader.go
index 1dea66393db..455ef587d1c 100644
--- a/src/cmd/link/internal/loader/loader.go
+++ b/src/cmd/link/internal/loader/loader.go
@@ -27,7 +27,7 @@ var _ = fmt.Print
 
 // Sym encapsulates a global symbol index, used to identify a specific
 // Go symbol. The 0-valued Sym is corresponds to an invalid symbol.
-type Sym int
+type Sym uint32
 
 // Relocs encapsulates the set of relocations on a given symbol; an
 // instance of this type is returned by the Loader Relocs() method.
@@ -364,6 +364,9 @@ func (st *loadState) addSym(name string, ver int, r *oReader, li uint32, kind in
 		panic("addSym called after external symbol is created")
 	}
 	i := Sym(len(l.objSyms))
+	if int(i) != len(l.objSyms) { // overflow
+		panic("too many symbols")
+	}
 	addToGlobal := func() {
 		l.objSyms = append(l.objSyms, objSym{r.objidx, li})
 	}
@@ -485,6 +488,9 @@ func (st *loadState) addSym(name string, ver int, r *oReader, li uint32, kind in
 // name/version.
 func (l *Loader) newExtSym(name string, ver int) Sym {
 	i := Sym(len(l.objSyms))
+	if int(i) != len(l.objSyms) { // overflow
+		panic("too many symbols")
+	}
 	if l.extStart == 0 {
 		l.extStart = i
 	}
-- 
GitLab