1
0
mirror of https://github.com/golang/go synced 2024-11-16 20:54:48 -07:00

cmd/link: remove elfsetstring out of the loader

Currently, we pass elfsetstring to the loader as a callback, for
a special case of Addstring. This is only used for ELF when adding
strings to the section header string table. Move the logic to the
caller instead, so the loader would not have this special case.

Change-Id: Icfb91f380fe4ba435985c3019681597932f58242
Reviewed-on: https://go-review.googlesource.com/c/go/+/492718
Run-TryBot: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
This commit is contained in:
Cherry Mui 2023-05-04 17:09:49 -04:00
parent 26899ef3ee
commit f90bb8a38f
7 changed files with 74 additions and 82 deletions

View File

@ -14,10 +14,9 @@ import (
func setUpContext(arch *sys.Arch, iself bool, ht objabi.HeadType, bm, lm string) *Link {
ctxt := linknew(arch)
edummy := func(str string, off int) {}
ctxt.HeadType = ht
er := loader.ErrorReporter{}
ctxt.loader = loader.NewLoader(0, edummy, &er)
ctxt.loader = loader.NewLoader(0, &er)
ctxt.BuildMode.Set(bm)
ctxt.LinkMode.Set(lm)
ctxt.IsELF = iself

View File

@ -2147,21 +2147,20 @@ func (d *dwctxt) collectUnitLocs(u *sym.CompilationUnit) []loader.Sym {
return syms
}
/*
* Elf.
*/
func dwarfaddshstrings(ctxt *Link, shstrtab *loader.SymbolBuilder) {
// Add DWARF section names to the section header string table, by calling add
// on each name. ELF only.
func dwarfaddshstrings(ctxt *Link, add func(string)) {
if *FlagW { // disable dwarf
return
}
secs := []string{"abbrev", "frame", "info", "loc", "line", "gdb_scripts", "ranges"}
for _, sec := range secs {
shstrtab.Addstring(".debug_" + sec)
add(".debug_" + sec)
if ctxt.IsExternal() {
shstrtab.Addstring(elfRelType + ".debug_" + sec)
add(elfRelType + ".debug_" + sec)
} else {
shstrtab.Addstring(".zdebug_" + sec)
add(".zdebug_" + sec)
}
}
}

View File

@ -1390,83 +1390,88 @@ func (ctxt *Link) doelf() {
shstrtab.SetType(sym.SELFROSECT)
shstrtab.Addstring("")
shstrtab.Addstring(".text")
shstrtab.Addstring(".noptrdata")
shstrtab.Addstring(".data")
shstrtab.Addstring(".bss")
shstrtab.Addstring(".noptrbss")
shstrtab.Addstring(".go.fuzzcntrs")
shstrtab.Addstring(".go.buildinfo")
shstrtabAddstring := func(s string) {
off := shstrtab.Addstring(s)
elfsetstring(ctxt, 0, s, int(off))
}
shstrtabAddstring("")
shstrtabAddstring(".text")
shstrtabAddstring(".noptrdata")
shstrtabAddstring(".data")
shstrtabAddstring(".bss")
shstrtabAddstring(".noptrbss")
shstrtabAddstring(".go.fuzzcntrs")
shstrtabAddstring(".go.buildinfo")
if ctxt.IsMIPS() {
shstrtab.Addstring(".MIPS.abiflags")
shstrtab.Addstring(".gnu.attributes")
shstrtabAddstring(".MIPS.abiflags")
shstrtabAddstring(".gnu.attributes")
}
// generate .tbss section for dynamic internal linker or external
// linking, so that various binutils could correctly calculate
// PT_TLS size. See https://golang.org/issue/5200.
if !*FlagD || ctxt.IsExternal() {
shstrtab.Addstring(".tbss")
shstrtabAddstring(".tbss")
}
if ctxt.IsNetbsd() {
shstrtab.Addstring(".note.netbsd.ident")
shstrtabAddstring(".note.netbsd.ident")
if *flagRace {
shstrtab.Addstring(".note.netbsd.pax")
shstrtabAddstring(".note.netbsd.pax")
}
}
if ctxt.IsOpenbsd() {
shstrtab.Addstring(".note.openbsd.ident")
shstrtabAddstring(".note.openbsd.ident")
}
if ctxt.IsFreebsd() {
shstrtab.Addstring(".note.tag")
shstrtabAddstring(".note.tag")
}
if len(buildinfo) > 0 {
shstrtab.Addstring(".note.gnu.build-id")
shstrtabAddstring(".note.gnu.build-id")
}
if *flagBuildid != "" {
shstrtab.Addstring(".note.go.buildid")
shstrtabAddstring(".note.go.buildid")
}
shstrtab.Addstring(".elfdata")
shstrtab.Addstring(".rodata")
shstrtabAddstring(".elfdata")
shstrtabAddstring(".rodata")
// See the comment about data.rel.ro.FOO section names in data.go.
relro_prefix := ""
if ctxt.UseRelro() {
shstrtab.Addstring(".data.rel.ro")
shstrtabAddstring(".data.rel.ro")
relro_prefix = ".data.rel.ro"
}
shstrtab.Addstring(relro_prefix + ".typelink")
shstrtab.Addstring(relro_prefix + ".itablink")
shstrtab.Addstring(relro_prefix + ".gosymtab")
shstrtab.Addstring(relro_prefix + ".gopclntab")
shstrtabAddstring(relro_prefix + ".typelink")
shstrtabAddstring(relro_prefix + ".itablink")
shstrtabAddstring(relro_prefix + ".gosymtab")
shstrtabAddstring(relro_prefix + ".gopclntab")
if ctxt.IsExternal() {
*FlagD = true
shstrtab.Addstring(elfRelType + ".text")
shstrtab.Addstring(elfRelType + ".rodata")
shstrtab.Addstring(elfRelType + relro_prefix + ".typelink")
shstrtab.Addstring(elfRelType + relro_prefix + ".itablink")
shstrtab.Addstring(elfRelType + relro_prefix + ".gosymtab")
shstrtab.Addstring(elfRelType + relro_prefix + ".gopclntab")
shstrtab.Addstring(elfRelType + ".noptrdata")
shstrtab.Addstring(elfRelType + ".data")
shstrtabAddstring(elfRelType + ".text")
shstrtabAddstring(elfRelType + ".rodata")
shstrtabAddstring(elfRelType + relro_prefix + ".typelink")
shstrtabAddstring(elfRelType + relro_prefix + ".itablink")
shstrtabAddstring(elfRelType + relro_prefix + ".gosymtab")
shstrtabAddstring(elfRelType + relro_prefix + ".gopclntab")
shstrtabAddstring(elfRelType + ".noptrdata")
shstrtabAddstring(elfRelType + ".data")
if ctxt.UseRelro() {
shstrtab.Addstring(elfRelType + ".data.rel.ro")
shstrtabAddstring(elfRelType + ".data.rel.ro")
}
shstrtab.Addstring(elfRelType + ".go.buildinfo")
shstrtabAddstring(elfRelType + ".go.buildinfo")
if ctxt.IsMIPS() {
shstrtab.Addstring(elfRelType + ".MIPS.abiflags")
shstrtab.Addstring(elfRelType + ".gnu.attributes")
shstrtabAddstring(elfRelType + ".MIPS.abiflags")
shstrtabAddstring(elfRelType + ".gnu.attributes")
}
// add a .note.GNU-stack section to mark the stack as non-executable
shstrtab.Addstring(".note.GNU-stack")
shstrtabAddstring(".note.GNU-stack")
if ctxt.IsShared() {
shstrtab.Addstring(".note.go.abihash")
shstrtab.Addstring(".note.go.pkg-list")
shstrtab.Addstring(".note.go.deps")
shstrtabAddstring(".note.go.abihash")
shstrtabAddstring(".note.go.pkg-list")
shstrtabAddstring(".note.go.deps")
}
}
@ -1479,35 +1484,35 @@ func (ctxt *Link) doelf() {
}
if hasinitarr {
shstrtab.Addstring(".init_array")
shstrtab.Addstring(elfRelType + ".init_array")
shstrtabAddstring(".init_array")
shstrtabAddstring(elfRelType + ".init_array")
}
if !*FlagS {
shstrtab.Addstring(".symtab")
shstrtab.Addstring(".strtab")
dwarfaddshstrings(ctxt, shstrtab)
shstrtabAddstring(".symtab")
shstrtabAddstring(".strtab")
dwarfaddshstrings(ctxt, shstrtabAddstring)
}
shstrtab.Addstring(".shstrtab")
shstrtabAddstring(".shstrtab")
if !*FlagD { /* -d suppresses dynamic loader format */
shstrtab.Addstring(".interp")
shstrtab.Addstring(".hash")
shstrtab.Addstring(".got")
shstrtabAddstring(".interp")
shstrtabAddstring(".hash")
shstrtabAddstring(".got")
if ctxt.IsPPC64() {
shstrtab.Addstring(".glink")
shstrtabAddstring(".glink")
}
shstrtab.Addstring(".got.plt")
shstrtab.Addstring(".dynamic")
shstrtab.Addstring(".dynsym")
shstrtab.Addstring(".dynstr")
shstrtab.Addstring(elfRelType)
shstrtab.Addstring(elfRelType + ".plt")
shstrtabAddstring(".got.plt")
shstrtabAddstring(".dynamic")
shstrtabAddstring(".dynsym")
shstrtabAddstring(".dynstr")
shstrtabAddstring(elfRelType)
shstrtabAddstring(elfRelType + ".plt")
shstrtab.Addstring(".plt")
shstrtab.Addstring(".gnu.version")
shstrtab.Addstring(".gnu.version_r")
shstrtabAddstring(".plt")
shstrtabAddstring(".gnu.version")
shstrtabAddstring(".gnu.version_r")
/* dynamic symbol table - first entry all zeros */
dynsym := ldr.CreateSymForUpdate(".dynsym", 0)

View File

@ -528,8 +528,7 @@ func (ctxt *Link) loadlib() {
default:
log.Fatalf("invalid -strictdups flag value %d", *FlagStrictDups)
}
elfsetstring1 := func(str string, off int) { elfsetstring(ctxt, 0, str, off) }
ctxt.loader = loader.NewLoader(flags, elfsetstring1, &ctxt.ErrorReporter.ErrorReporter)
ctxt.loader = loader.NewLoader(flags, &ctxt.ErrorReporter.ErrorReporter)
ctxt.ErrorReporter.SymName = func(s loader.Sym) string {
return ctxt.loader.SymName(s)
}

View File

@ -261,8 +261,6 @@ type Loader struct {
strictDupMsgs int // number of strict-dup warning/errors, when FlagStrictDups is enabled
elfsetstring elfsetstringFunc
errorReporter *ErrorReporter
npkgsyms int // number of package symbols, for accounting
@ -284,8 +282,6 @@ const (
goObjStart
)
type elfsetstringFunc func(str string, off int)
// extSymPayload holds the payload (data + relocations) for linker-synthesized
// external symbols (note that symbol value is stored in a separate slice).
type extSymPayload struct {
@ -304,7 +300,7 @@ const (
FlagStrictDups = 1 << iota
)
func NewLoader(flags uint32, elfsetstring elfsetstringFunc, reporter *ErrorReporter) *Loader {
func NewLoader(flags uint32, reporter *ErrorReporter) *Loader {
nbuiltin := goobj.NBuiltin()
extReader := &oReader{objidx: extObj}
ldr := &Loader{
@ -333,7 +329,6 @@ func NewLoader(flags uint32, elfsetstring elfsetstringFunc, reporter *ErrorRepor
extStaticSyms: make(map[nameVer]Sym),
builtinSyms: make([]Sym, nbuiltin),
flags: flags,
elfsetstring: elfsetstring,
errorReporter: reporter,
sects: []*sym.Section{nil}, // reserve index 0 for nil section
}

View File

@ -26,9 +26,8 @@ func addDummyObjSym(t *testing.T, ldr *Loader, or *oReader, name string) Sym {
}
func mkLoader() *Loader {
edummy := func(str string, off int) {}
er := ErrorReporter{}
ldr := NewLoader(0, edummy, &er)
ldr := NewLoader(0, &er)
er.ldr = ldr
return ldr
}

View File

@ -332,10 +332,6 @@ func (sb *SymbolBuilder) Addstring(str string) int64 {
sb.kind = sym.SNOPTRDATA
}
r := sb.size
if sb.name == ".shstrtab" {
// FIXME: find a better mechanism for this
sb.l.elfsetstring(str, int(r))
}
sb.data = append(sb.data, str...)
sb.data = append(sb.data, 0)
sb.size = int64(len(sb.data))