From 1b8987f1a708539dbadedb67a69987b2b61705fe Mon Sep 17 00:00:00 2001 From: Michael Hudson-Doyle Date: Fri, 28 Apr 2017 13:01:03 +1200 Subject: [PATCH] cmd/link: replace SSUB with an attribute bit There is some stuff I don't understand very well involved in SSUB, better words for the documentation gratefully accepted. As this is the last use of a bit in SMASK, kill that off too. Change-Id: Iddff1c9b2af02c9dfb12ac8e668d004e4642f997 Reviewed-on: https://go-review.googlesource.com/42026 Run-TryBot: Michael Hudson-Doyle Run-TryBot: Ian Lance Taylor Reviewed-by: Ian Lance Taylor TryBot-Result: Gobot Gobot --- src/cmd/link/internal/amd64/asm.go | 3 ++- src/cmd/link/internal/ld/ar.go | 2 +- src/cmd/link/internal/ld/data.go | 12 ++++++------ src/cmd/link/internal/ld/elf.go | 6 +++--- src/cmd/link/internal/ld/lib.go | 4 ++-- src/cmd/link/internal/ld/pe.go | 3 ++- src/cmd/link/internal/loadelf/ldelf.go | 3 ++- src/cmd/link/internal/loadmacho/ldmacho.go | 3 ++- src/cmd/link/internal/loadpe/ldpe.go | 3 ++- src/cmd/link/internal/sym/attribute.go | 20 ++++++++++++++++++-- src/cmd/link/internal/sym/symkind.go | 2 -- src/cmd/link/internal/x86/asm.go | 3 ++- 12 files changed, 42 insertions(+), 22 deletions(-) diff --git a/src/cmd/link/internal/amd64/asm.go b/src/cmd/link/internal/amd64/asm.go index ca652f8353..4f426d7bb6 100644 --- a/src/cmd/link/internal/amd64/asm.go +++ b/src/cmd/link/internal/amd64/asm.go @@ -348,7 +348,8 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool { ld.Adddynsym(ctxt, targ) got := ctxt.Syms.Lookup(".got", 0) - s.Type = got.Type | sym.SSUB + s.Type = got.Type + s.Attr |= sym.AttrSubSymbol s.Outer = got s.Sub = got.Sub got.Sub = s diff --git a/src/cmd/link/internal/ld/ar.go b/src/cmd/link/internal/ld/ar.go index 13e29aa1a3..4b513041a3 100644 --- a/src/cmd/link/internal/ld/ar.go +++ b/src/cmd/link/internal/ld/ar.go @@ -106,7 +106,7 @@ func hostArchive(ctxt *Link, name string) { var load []uint64 for _, s := range ctxt.Syms.Allsym { for _, r := range s.R { - if r.Sym != nil && r.Sym.Type&sym.SMASK == sym.SXREF { + if r.Sym != nil && r.Sym.Type == sym.SXREF { if off := armap[r.Sym.Name]; off != 0 && !loaded[off] { load = append(load, off) loaded[off] = true diff --git a/src/cmd/link/internal/ld/data.go b/src/cmd/link/internal/ld/data.go index cb824811aa..3572d7309d 100644 --- a/src/cmd/link/internal/ld/data.go +++ b/src/cmd/link/internal/ld/data.go @@ -122,7 +122,7 @@ func relocsym(ctxt *Link, s *sym.Symbol) { continue } - if r.Sym != nil && ((r.Sym.Type == 0 && !r.Sym.Attr.VisibilityHidden()) || r.Sym.Type&sym.SMASK == sym.SXREF) { + if r.Sym != nil && ((r.Sym.Type == 0 && !r.Sym.Attr.VisibilityHidden()) || r.Sym.Type == sym.SXREF) { // When putting the runtime but not main into a shared library // these symbols are undefined and that's OK. if ctxt.BuildMode == BuildModeShared { @@ -148,7 +148,7 @@ func relocsym(ctxt *Link, s *sym.Symbol) { // We need to be able to reference dynimport symbols when linking against // shared libraries, and Solaris needs it always - if ctxt.HeadType != objabi.Hsolaris && r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT && !ctxt.DynlinkingGo() { + if ctxt.HeadType != objabi.Hsolaris && r.Sym != nil && r.Sym.Type == sym.SDYNIMPORT && !ctxt.DynlinkingGo() && !r.Sym.Attr.SubSymbol() { if !(ctxt.Arch.Family == sys.PPC64 && ctxt.LinkMode == LinkExternal && r.Sym.Name == ".TOC.") { Errorf(s, "unhandled relocation for %s (type %d (%s) rtype %d (%s))", r.Sym.Name, r.Sym.Type, r.Sym.Type, r.Type, sym.RelocName(ctxt.Arch, r.Type)) } @@ -650,7 +650,7 @@ func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) { func blk(ctxt *Link, syms []*sym.Symbol, addr, size int64, pad []byte) { for i, s := range syms { - if s.Type&sym.SSUB == 0 && s.Value >= addr { + if !s.Attr.SubSymbol() && s.Value >= addr { syms = syms[i:] break } @@ -658,7 +658,7 @@ func blk(ctxt *Link, syms []*sym.Symbol, addr, size int64, pad []byte) { eaddr := addr + size for _, s := range syms { - if s.Type&sym.SSUB != 0 { + if s.Attr.SubSymbol() { continue } if s.Value >= eaddr { @@ -1052,7 +1052,7 @@ func (ctxt *Link) dodata() { // Collect data symbols by type into data. var data [sym.SXREF][]*sym.Symbol for _, s := range ctxt.Syms.Allsym { - if !s.Attr.Reachable() || s.Attr.Special() { + if !s.Attr.Reachable() || s.Attr.Special() || s.Attr.SubSymbol() { continue } if s.Type <= sym.STEXT || s.Type >= sym.SXREF { @@ -1813,7 +1813,7 @@ func (ctxt *Link) textaddress() { // will not need to create new text sections, and so no need to return sect and n. func assignAddress(ctxt *Link, sect *sym.Section, n int, s *sym.Symbol, va uint64, isTramp bool) (*sym.Section, int, uint64) { s.Sect = sect - if s.Type&sym.SSUB != 0 { + if s.Attr.SubSymbol() { return sect, n, va } if s.Align != 0 { diff --git a/src/cmd/link/internal/ld/elf.go b/src/cmd/link/internal/ld/elf.go index 230b0aa5df..d56a2359d3 100644 --- a/src/cmd/link/internal/ld/elf.go +++ b/src/cmd/link/internal/ld/elf.go @@ -2262,7 +2262,7 @@ func elfadddynsym(ctxt *Link, s *sym.Symbol) { /* type */ t := STB_GLOBAL << 4 - if s.Attr.CgoExport() && s.Type&sym.SMASK == sym.STEXT { + if s.Attr.CgoExport() && s.Type == sym.STEXT { t |= STT_FUNC } else { t |= STT_OBJECT @@ -2317,9 +2317,9 @@ func elfadddynsym(ctxt *Link, s *sym.Symbol) { t := STB_GLOBAL << 4 // TODO(mwhudson): presumably the behavior should actually be the same on both arm and 386. - if ctxt.Arch.Family == sys.I386 && s.Attr.CgoExport() && s.Type&sym.SMASK == sym.STEXT { + if ctxt.Arch.Family == sys.I386 && s.Attr.CgoExport() && s.Type == sym.STEXT { t |= STT_FUNC - } else if ctxt.Arch.Family == sys.ARM && s.Attr.CgoExportDynamic() && s.Type&sym.SMASK == sym.STEXT { + } else if ctxt.Arch.Family == sys.ARM && s.Attr.CgoExportDynamic() && s.Type == sym.STEXT { t |= STT_FUNC } else { t |= STT_OBJECT diff --git a/src/cmd/link/internal/ld/lib.go b/src/cmd/link/internal/ld/lib.go index 083c56c131..26ce209f1c 100644 --- a/src/cmd/link/internal/ld/lib.go +++ b/src/cmd/link/internal/ld/lib.go @@ -508,7 +508,7 @@ func (ctxt *Link) loadlib() { any := false for _, s := range ctxt.Syms.Allsym { for _, r := range s.R { - if r.Sym != nil && r.Sym.Type&sym.SMASK == sym.SXREF && r.Sym.Name != ".got" { + if r.Sym != nil && r.Sym.Type == sym.SXREF && r.Sym.Name != ".got" { any = true break } @@ -2006,7 +2006,7 @@ func genasmsym(ctxt *Link, put func(*Link, *sym.Symbol, string, SymbolType, int6 if (s.Name == "" || s.Name[0] == '.') && s.Version == 0 && s.Name != ".rathole" && s.Name != ".TOC." { continue } - switch s.Type & sym.SMASK { + switch s.Type { case sym.SCONST, sym.SRODATA, sym.SSYMTAB, diff --git a/src/cmd/link/internal/ld/pe.go b/src/cmd/link/internal/ld/pe.go index f870d5d917..550ffc1659 100644 --- a/src/cmd/link/internal/ld/pe.go +++ b/src/cmd/link/internal/ld/pe.go @@ -1033,7 +1033,8 @@ func initdynimport(ctxt *Link) *Dll { dynamic.Type = sym.SWINDOWS for d := dr; d != nil; d = d.next { for m = d.ms; m != nil; m = m.next { - m.s.Type = sym.SWINDOWS | sym.SSUB + m.s.Type = sym.SWINDOWS + m.s.Attr |= sym.AttrSubSymbol m.s.Sub = dynamic.Sub dynamic.Sub = m.s m.s.Value = dynamic.Size diff --git a/src/cmd/link/internal/loadelf/ldelf.go b/src/cmd/link/internal/loadelf/ldelf.go index 5cae1070ce..793fd961d1 100644 --- a/src/cmd/link/internal/loadelf/ldelf.go +++ b/src/cmd/link/internal/loadelf/ldelf.go @@ -795,7 +795,8 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i s.Sub = sect.sym.Sub sect.sym.Sub = s - s.Type = sect.sym.Type | s.Type&^sym.SMASK | sym.SSUB + s.Type = sect.sym.Type + s.Attr |= sym.AttrSubSymbol if !s.Attr.CgoExportDynamic() { s.Dynimplib = "" // satisfy dynimport } diff --git a/src/cmd/link/internal/loadmacho/ldmacho.go b/src/cmd/link/internal/loadmacho/ldmacho.go index bcea6c731a..e6b0f70e38 100644 --- a/src/cmd/link/internal/loadmacho/ldmacho.go +++ b/src/cmd/link/internal/loadmacho/ldmacho.go @@ -637,7 +637,8 @@ func Load(arch *sys.Arch, syms *sym.Symbols, f *bio.Reader, pkg string, length i return errorf("duplicate symbol reference: %s in both %s and %s", s.Name, s.Outer.Name, sect.sym.Name) } - s.Type = outer.Type | sym.SSUB + s.Type = outer.Type + s.Attr |= sym.AttrSubSymbol s.Sub = outer.Sub outer.Sub = s s.Outer = outer diff --git a/src/cmd/link/internal/loadpe/ldpe.go b/src/cmd/link/internal/loadpe/ldpe.go index 5f62760c20..09c9e31c2e 100644 --- a/src/cmd/link/internal/loadpe/ldpe.go +++ b/src/cmd/link/internal/loadpe/ldpe.go @@ -350,7 +350,8 @@ func Load(arch *sys.Arch, syms *sym.Symbols, input *bio.Reader, pkg string, leng sectsym := sectsyms[sect] s.Sub = sectsym.Sub sectsym.Sub = s - s.Type = sectsym.Type | sym.SSUB + s.Type = sectsym.Type + s.Attr |= sym.AttrSubSymbol s.Value = int64(pesym.Value) s.Size = 4 s.Outer = sectsym diff --git a/src/cmd/link/internal/sym/attribute.go b/src/cmd/link/internal/sym/attribute.go index 1a14c932d5..45a7939d67 100644 --- a/src/cmd/link/internal/sym/attribute.go +++ b/src/cmd/link/internal/sym/attribute.go @@ -5,7 +5,7 @@ package sym // Attribute is a set of common symbol attributes. -type Attribute uint16 +type Attribute int32 const ( // AttrDuplicateOK marks a symbol that can be present in multiple object @@ -57,10 +57,25 @@ const ( // the final executable. Only relevant when internally linking // on an ELF platform. AttrVisibilityHidden + // AttrSubSymbol mostly means that the symbol appears on the Sub list of some + // other symbol. Unfortunately, it's not 100% reliable; at least, it's not set + // correctly for the .TOC. symbol in Link.dodata. Usually the Outer field of the + // symbol points to the symbol whose list it is on, but that it is not set for the + // symbols added to .windynamic in initdynimport in pe.go. + // + // TODO(mwhudson): fix the inconsistencies noticed above. + // + // Sub lists are used when loading host objects (sections from the host object + // become regular linker symbols and symbols go on the Sub list of their section) + // and for constructing the global offset table when internally linking a dynamic + // executable. + // + // TOOD(mwhudson): perhaps a better name for this is AttrNonGoSymbol. + AttrSubSymbol // AttrContainer is set on text symbols that are present as the .Outer for some // other symbol. AttrContainer - // 16 attributes defined so far. + // 17 attributes defined so far. ) func (a Attribute) DuplicateOK() bool { return a&AttrDuplicateOK != 0 } @@ -78,6 +93,7 @@ func (a Attribute) ReflectMethod() bool { return a&AttrReflectMethod != 0 } func (a Attribute) MakeTypelink() bool { return a&AttrMakeTypelink != 0 } func (a Attribute) Shared() bool { return a&AttrShared != 0 } func (a Attribute) VisibilityHidden() bool { return a&AttrVisibilityHidden != 0 } +func (a Attribute) SubSymbol() bool { return a&AttrSubSymbol != 0 } func (a Attribute) Container() bool { return a&AttrContainer != 0 } func (a Attribute) CgoExport() bool { diff --git a/src/cmd/link/internal/sym/symkind.go b/src/cmd/link/internal/sym/symkind.go index 4b92917846..68522a39d2 100644 --- a/src/cmd/link/internal/sym/symkind.go +++ b/src/cmd/link/internal/sym/symkind.go @@ -105,8 +105,6 @@ const ( SDWARFINFO SDWARFRANGE SDWARFLOC - SSUB = SymKind(1 << 8) - SMASK = SymKind(SSUB - 1) ) // AbiSymKindToSymKind maps values read from object files (which are diff --git a/src/cmd/link/internal/x86/asm.go b/src/cmd/link/internal/x86/asm.go index 2230384303..56d502c2dd 100644 --- a/src/cmd/link/internal/x86/asm.go +++ b/src/cmd/link/internal/x86/asm.go @@ -330,7 +330,8 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool { ld.Adddynsym(ctxt, targ) got := ctxt.Syms.Lookup(".got", 0) - s.Type = got.Type | sym.SSUB + s.Type = got.Type + s.Attr |= sym.AttrSubSymbol s.Outer = got s.Sub = got.Sub got.Sub = s -- 2.48.1