Commit 3877f820 authored by David Crawshaw's avatar David Crawshaw

cmd/link, etc: introduce SymKind type

Moves the grouping of symbol kinds (sections) into cmd/internal/obj
to keep it near the definition. Groundwork for CL 28538.

Change-Id: I99112981e69b028f366e1333f31cd7defd4ff82c
Reviewed-on: https://go-review.googlesource.com/28691
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent 791f71d1
...@@ -29,50 +29,48 @@ type SymKind int ...@@ -29,50 +29,48 @@ type SymKind int
// TODO(rsc): Give idiomatic Go names. // TODO(rsc): Give idiomatic Go names.
// TODO(rsc): Reduce the number of symbol types in the object files. // TODO(rsc): Reduce the number of symbol types in the object files.
const ( const (
_ SymKind = iota
// readonly, executable // readonly, executable
STEXT SymKind = obj.STEXT STEXT = SymKind(obj.STEXT)
SELFRXSECT SymKind = obj.SELFRXSECT SELFRXSECT = SymKind(obj.SELFRXSECT)
// readonly, non-executable // readonly, non-executable
STYPE SymKind = obj.STYPE STYPE = SymKind(obj.STYPE)
SSTRING SymKind = obj.SSTRING SSTRING = SymKind(obj.SSTRING)
SGOSTRING SymKind = obj.SGOSTRING SGOSTRING = SymKind(obj.SGOSTRING)
SGOFUNC SymKind = obj.SGOFUNC SGOFUNC = SymKind(obj.SGOFUNC)
SRODATA SymKind = obj.SRODATA SRODATA = SymKind(obj.SRODATA)
SFUNCTAB SymKind = obj.SFUNCTAB SFUNCTAB = SymKind(obj.SFUNCTAB)
STYPELINK SymKind = obj.STYPELINK STYPELINK = SymKind(obj.STYPELINK)
SITABLINK SymKind = obj.SITABLINK SITABLINK = SymKind(obj.SITABLINK)
SSYMTAB SymKind = obj.SSYMTAB // TODO: move to unmapped section SSYMTAB = SymKind(obj.SSYMTAB) // TODO: move to unmapped section
SPCLNTAB SymKind = obj.SPCLNTAB SPCLNTAB = SymKind(obj.SPCLNTAB)
SELFROSECT SymKind = obj.SELFROSECT SELFROSECT = SymKind(obj.SELFROSECT)
// writable, non-executable // writable, non-executable
SMACHOPLT SymKind = obj.SMACHOPLT SMACHOPLT = SymKind(obj.SMACHOPLT)
SELFSECT SymKind = obj.SELFSECT SELFSECT = SymKind(obj.SELFSECT)
SMACHO SymKind = obj.SMACHO // Mach-O __nl_symbol_ptr SMACHO = SymKind(obj.SMACHO) // Mach-O __nl_symbol_ptr
SMACHOGOT SymKind = obj.SMACHOGOT SMACHOGOT = SymKind(obj.SMACHOGOT)
SWINDOWS SymKind = obj.SWINDOWS SWINDOWS = SymKind(obj.SWINDOWS)
SELFGOT SymKind = obj.SELFGOT SELFGOT = SymKind(obj.SELFGOT)
SNOPTRDATA SymKind = obj.SNOPTRDATA SNOPTRDATA = SymKind(obj.SNOPTRDATA)
SINITARR SymKind = obj.SINITARR SINITARR = SymKind(obj.SINITARR)
SDATA SymKind = obj.SDATA SDATA = SymKind(obj.SDATA)
SBSS SymKind = obj.SBSS SBSS = SymKind(obj.SBSS)
SNOPTRBSS SymKind = obj.SNOPTRBSS SNOPTRBSS = SymKind(obj.SNOPTRBSS)
STLSBSS SymKind = obj.STLSBSS STLSBSS = SymKind(obj.STLSBSS)
// not mapped // not mapped
SXREF SymKind = obj.SXREF SXREF = SymKind(obj.SXREF)
SMACHOSYMSTR SymKind = obj.SMACHOSYMSTR SMACHOSYMSTR = SymKind(obj.SMACHOSYMSTR)
SMACHOSYMTAB SymKind = obj.SMACHOSYMTAB SMACHOSYMTAB = SymKind(obj.SMACHOSYMTAB)
SMACHOINDIRECTPLT SymKind = obj.SMACHOINDIRECTPLT SMACHOINDIRECTPLT = SymKind(obj.SMACHOINDIRECTPLT)
SMACHOINDIRECTGOT SymKind = obj.SMACHOINDIRECTGOT SMACHOINDIRECTGOT = SymKind(obj.SMACHOINDIRECTGOT)
SFILE SymKind = obj.SFILE SFILE = SymKind(obj.SFILE)
SFILEPATH SymKind = obj.SFILEPATH SFILEPATH = SymKind(obj.SFILEPATH)
SCONST SymKind = obj.SCONST SCONST = SymKind(obj.SCONST)
SDYNIMPORT SymKind = obj.SDYNIMPORT SDYNIMPORT = SymKind(obj.SDYNIMPORT)
SHOSTOBJ SymKind = obj.SHOSTOBJ SHOSTOBJ = SymKind(obj.SHOSTOBJ)
) )
var symKindStrings = []string{ var symKindStrings = []string{
......
...@@ -316,7 +316,7 @@ const ( ...@@ -316,7 +316,7 @@ const (
// An LSym is the sort of symbol that is written to an object file. // An LSym is the sort of symbol that is written to an object file.
type LSym struct { type LSym struct {
Name string Name string
Type int16 Type SymKind
Version int16 Version int16
Dupok bool Dupok bool
Cfunc bool Cfunc bool
...@@ -371,9 +371,16 @@ type Pcln struct { ...@@ -371,9 +371,16 @@ type Pcln struct {
Lastindex int Lastindex int
} }
// LSym.type // A SymKind describes the kind of memory represented by a symbol.
type SymKind int16
// Defined SymKind values.
//
// TODO(rsc): Give idiomatic Go names.
// TODO(rsc): Reduce the number of symbol types in the object files.
//go:generate stringer -type=SymKind
const ( const (
Sxxx = iota Sxxx SymKind = iota
STEXT STEXT
SELFRXSECT SELFRXSECT
...@@ -434,12 +441,39 @@ const ( ...@@ -434,12 +441,39 @@ const (
SHOSTOBJ SHOSTOBJ
SDWARFSECT SDWARFSECT
SDWARFINFO SDWARFINFO
SSUB = 1 << 8 SSUB = SymKind(1 << 8)
SMASK = SSUB - 1 SMASK = SymKind(SSUB - 1)
SHIDDEN = 1 << 9 SHIDDEN = SymKind(1 << 9)
SCONTAINER = 1 << 10 // has a sub-symbol SCONTAINER = SymKind(1 << 10) // has a sub-symbol
) )
// ReadOnly are the symbol kinds that form read-only sections. In some
// cases, if they will require relocations, they are transformed into
// rel-ro sections using RelROMap.
var ReadOnly = []SymKind{
STYPE,
SSTRING,
SGOSTRING,
SGOSTRINGHDR,
SGOFUNC,
SGCBITS,
SRODATA,
SFUNCTAB,
}
// RelROMap describes the transformation of read-only symbols to rel-ro
// symbols.
var RelROMap = map[SymKind]SymKind{
STYPE: STYPERELRO,
SSTRING: SSTRINGRELRO,
SGOSTRING: SGOSTRINGRELRO,
SGOSTRINGHDR: SGOSTRINGHDRRELRO,
SGOFUNC: SGOFUNCRELRO,
SGCBITS: SGCBITSRELRO,
SRODATA: SRODATARELRO,
SFUNCTAB: SFUNCTABRELRO,
}
type Reloc struct { type Reloc struct {
Off int32 Off int32
Siz uint8 Siz uint8
......
// Code generated by "stringer -type=SymKind"; DO NOT EDIT
package obj
import "fmt"
const _SymKind_name = "SxxxSTEXTSELFRXSECTSTYPESSTRINGSGOSTRINGSGOSTRINGHDRSGOFUNCSGCBITSSRODATASFUNCTABSTYPERELROSSTRINGRELROSGOSTRINGRELROSGOSTRINGHDRRELROSGOFUNCRELROSGCBITSRELROSRODATARELROSFUNCTABRELROSTYPELINKSITABLINKSSYMTABSPCLNTABSELFROSECTSMACHOPLTSELFSECTSMACHOSMACHOGOTSWINDOWSSELFGOTSNOPTRDATASINITARRSDATASBSSSNOPTRBSSSTLSBSSSXREFSMACHOSYMSTRSMACHOSYMTABSMACHOINDIRECTPLTSMACHOINDIRECTGOTSFILESFILEPATHSCONSTSDYNIMPORTSHOSTOBJSDWARFSECTSDWARFINFO"
var _SymKind_index = [...]uint16{0, 4, 9, 19, 24, 31, 40, 52, 59, 66, 73, 81, 91, 103, 117, 134, 146, 158, 170, 183, 192, 201, 208, 216, 226, 235, 243, 249, 258, 266, 273, 283, 291, 296, 300, 309, 316, 321, 333, 345, 362, 379, 384, 393, 399, 409, 417, 427, 437}
func (i SymKind) String() string {
if i < 0 || i >= SymKind(len(_SymKind_index)-1) {
return fmt.Sprintf("SymKind(%d)", i)
}
return _SymKind_name[_SymKind_index[i]:_SymKind_index[i+1]]
}
...@@ -1168,7 +1168,7 @@ func (d bySizeAndName) Less(i, j int) bool { ...@@ -1168,7 +1168,7 @@ func (d bySizeAndName) Less(i, j int) bool {
const cutoff int64 = 2e9 // 2 GB (or so; looks better in errors than 2^31) const cutoff int64 = 2e9 // 2 GB (or so; looks better in errors than 2^31)
func checkdatsize(ctxt *Link, datsize int64, symn int) { func checkdatsize(ctxt *Link, datsize int64, symn obj.SymKind) {
if datsize > cutoff { if datsize > cutoff {
ctxt.Diag("too much data in section %v (over %d bytes)", symn, cutoff) ctxt.Diag("too much data in section %v (over %d bytes)", symn, cutoff)
} }
...@@ -1210,8 +1210,8 @@ func (ctxt *Link) dodata() { ...@@ -1210,8 +1210,8 @@ func (ctxt *Link) dodata() {
// "read only" data with relocations needs to go in its own section // "read only" data with relocations needs to go in its own section
// when building a shared library. We do this by boosting objects of // when building a shared library. We do this by boosting objects of
// type SXXX with relocations to type SXXXRELRO. // type SXXX with relocations to type SXXXRELRO.
for symnro := int16(obj.STYPE); symnro < obj.STYPERELRO; symnro++ { for _, symnro := range obj.ReadOnly {
symnrelro := symnro + obj.STYPERELRO - obj.STYPE symnrelro := obj.RelROMap[symnro]
ro := []*Symbol{} ro := []*Symbol{}
relro := data[symnrelro] relro := data[symnrelro]
...@@ -1256,7 +1256,7 @@ func (ctxt *Link) dodata() { ...@@ -1256,7 +1256,7 @@ func (ctxt *Link) dodata() {
var dataMaxAlign [obj.SXREF]int32 var dataMaxAlign [obj.SXREF]int32
var wg sync.WaitGroup var wg sync.WaitGroup
for symn := range data { for symn := range data {
symn := symn symn := obj.SymKind(symn)
wg.Add(1) wg.Add(1)
go func() { go func() {
data[symn], dataMaxAlign[symn] = dodataSect(ctxt, symn, data[symn]) data[symn], dataMaxAlign[symn] = dodataSect(ctxt, symn, data[symn])
...@@ -1271,14 +1271,14 @@ func (ctxt *Link) dodata() { ...@@ -1271,14 +1271,14 @@ func (ctxt *Link) dodata() {
// to generate garbage collection information. // to generate garbage collection information.
datsize := int64(0) datsize := int64(0)
// Writable sections. // Writable data sections that do not need any specialized handling.
writableSects := []int{ writable := []obj.SymKind{
obj.SELFSECT, obj.SELFSECT,
obj.SMACHO, obj.SMACHO,
obj.SMACHOGOT, obj.SMACHOGOT,
obj.SWINDOWS, obj.SWINDOWS,
} }
for _, symn := range writableSects { for _, symn := range writable {
for _, s := range data[symn] { for _, s := range data[symn] {
sect := addsection(&Segdata, s.Name, 06) sect := addsection(&Segdata, s.Name, 06)
sect.Align = symalign(s) sect.Align = symalign(s)
...@@ -1489,24 +1489,14 @@ func (ctxt *Link) dodata() { ...@@ -1489,24 +1489,14 @@ func (ctxt *Link) dodata() {
Linklookup(ctxt, "runtime.types", 0).Sect = sect Linklookup(ctxt, "runtime.types", 0).Sect = sect
Linklookup(ctxt, "runtime.etypes", 0).Sect = sect Linklookup(ctxt, "runtime.etypes", 0).Sect = sect
} }
roSects := []int{ for _, symn := range obj.ReadOnly {
obj.STYPE,
obj.SSTRING,
obj.SGOSTRING,
obj.SGOSTRINGHDR,
obj.SGOFUNC,
obj.SGCBITS,
obj.SRODATA,
obj.SFUNCTAB,
}
for _, symn := range roSects {
align := dataMaxAlign[symn] align := dataMaxAlign[symn]
if sect.Align < align { if sect.Align < align {
sect.Align = align sect.Align = align
} }
} }
datsize = Rnd(datsize, int64(sect.Align)) datsize = Rnd(datsize, int64(sect.Align))
for _, symn := range roSects { for _, symn := range obj.ReadOnly {
for _, s := range data[symn] { for _, s := range data[symn] {
datsize = aligndatsize(datsize, s) datsize = aligndatsize(datsize, s)
s.Sect = sect s.Sect = sect
...@@ -1540,24 +1530,16 @@ func (ctxt *Link) dodata() { ...@@ -1540,24 +1530,16 @@ func (ctxt *Link) dodata() {
sect.Vaddr = 0 sect.Vaddr = 0
Linklookup(ctxt, "runtime.types", 0).Sect = sect Linklookup(ctxt, "runtime.types", 0).Sect = sect
Linklookup(ctxt, "runtime.etypes", 0).Sect = sect Linklookup(ctxt, "runtime.etypes", 0).Sect = sect
relroSects := []int{ for _, symnro := range obj.ReadOnly {
obj.STYPERELRO, symn := obj.RelROMap[symnro]
obj.SSTRINGRELRO,
obj.SGOSTRINGRELRO,
obj.SGOSTRINGHDRRELRO,
obj.SGOFUNCRELRO,
obj.SGCBITSRELRO,
obj.SRODATARELRO,
obj.SFUNCTABRELRO,
}
for _, symn := range relroSects {
align := dataMaxAlign[symn] align := dataMaxAlign[symn]
if sect.Align < align { if sect.Align < align {
sect.Align = align sect.Align = align
} }
} }
datsize = Rnd(datsize, int64(sect.Align)) datsize = Rnd(datsize, int64(sect.Align))
for _, symn := range relroSects { for _, symnro := range obj.ReadOnly {
symn := obj.RelROMap[symnro]
for _, s := range data[symn] { for _, s := range data[symn] {
datsize = aligndatsize(datsize, s) datsize = aligndatsize(datsize, s)
if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect { if s.Outer != nil && s.Outer.Sect != nil && s.Outer.Sect != sect {
...@@ -1739,13 +1721,13 @@ func (ctxt *Link) dodata() { ...@@ -1739,13 +1721,13 @@ func (ctxt *Link) dodata() {
} }
} }
func dodataSect(ctxt *Link, symn int, syms []*Symbol) (result []*Symbol, maxAlign int32) { func dodataSect(ctxt *Link, symn obj.SymKind, syms []*Symbol) (result []*Symbol, maxAlign int32) {
if Headtype == obj.Hdarwin { if Headtype == obj.Hdarwin {
// Some symbols may no longer belong in syms // Some symbols may no longer belong in syms
// due to movement in machosymorder. // due to movement in machosymorder.
newSyms := make([]*Symbol, 0, len(syms)) newSyms := make([]*Symbol, 0, len(syms))
for _, s := range syms { for _, s := range syms {
if int(s.Type) == symn { if s.Type == symn {
newSyms = append(newSyms, s) newSyms = append(newSyms, s)
} }
} }
......
...@@ -1949,9 +1949,9 @@ func Symaddr(ctxt *Link, s *Symbol) int64 { ...@@ -1949,9 +1949,9 @@ func Symaddr(ctxt *Link, s *Symbol) int64 {
return s.Value return s.Value
} }
func (ctxt *Link) xdefine(p string, t int, v int64) { func (ctxt *Link) xdefine(p string, t obj.SymKind, v int64) {
s := Linklookup(ctxt, p, 0) s := Linklookup(ctxt, p, 0)
s.Type = int16(t) s.Type = t
s.Value = v s.Value = v
s.Attr |= AttrReachable s.Attr |= AttrReachable
s.Attr |= AttrSpecial s.Attr |= AttrSpecial
......
...@@ -42,7 +42,7 @@ import ( ...@@ -42,7 +42,7 @@ import (
type Symbol struct { type Symbol struct {
Name string Name string
Extname string Extname string
Type int16 Type obj.SymKind
Version int16 Version int16
Attr Attribute Attr Attribute
Localentry uint8 Localentry uint8
......
...@@ -254,7 +254,7 @@ func (r *objReader) readSym() { ...@@ -254,7 +254,7 @@ func (r *objReader) readSym() {
if c, err := r.rd.ReadByte(); c != symPrefix || err != nil { if c, err := r.rd.ReadByte(); c != symPrefix || err != nil {
log.Fatalln("readSym out of sync") log.Fatalln("readSym out of sync")
} }
t := r.readInt() t := obj.SymKind(r.readInt())
s := r.readSymIndex() s := r.readSymIndex()
flags := r.readInt() flags := r.readInt()
dupok := flags&1 != 0 dupok := flags&1 != 0
...@@ -302,9 +302,9 @@ overwrite: ...@@ -302,9 +302,9 @@ overwrite:
log.Fatalf("missing type for %s in %s", s.Name, r.pn) log.Fatalf("missing type for %s in %s", s.Name, r.pn)
} }
if t == obj.SBSS && (s.Type == obj.SRODATA || s.Type == obj.SNOPTRBSS) { if t == obj.SBSS && (s.Type == obj.SRODATA || s.Type == obj.SNOPTRBSS) {
t = int(s.Type) t = s.Type
} }
s.Type = int16(t) s.Type = t
if s.Size < int64(size) { if s.Size < int64(size) {
s.Size = int64(size) s.Size = int64(size)
} }
......
...@@ -386,7 +386,7 @@ func (ctxt *Link) symtab() { ...@@ -386,7 +386,7 @@ func (ctxt *Link) symtab() {
symtyperel = s symtyperel = s
} }
groupSym := func(name string, t int16) *Symbol { groupSym := func(name string, t obj.SymKind) *Symbol {
s := Linklookup(ctxt, name, 0) s := Linklookup(ctxt, name, 0)
s.Type = t s.Type = t
s.Size = 0 s.Size = 0
......
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