Commit a6b183fa authored by Daniel Martí's avatar Daniel Martí

cmd/link/internal/ld: simple cleanups

Simplify some C-style loops with range statements, and move some
declarations closer to their uses.

While at it, ensure that all the SymbolType consts are typed.

Change-Id: I04b06afb2c1fb249ef8093a0c5cca0a597d1e05c
Reviewed-on: https://go-review.googlesource.com/105217
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent ae2a2d12
...@@ -504,7 +504,7 @@ func windynrelocsym(ctxt *Link, s *sym.Symbol) { ...@@ -504,7 +504,7 @@ func windynrelocsym(ctxt *Link, s *sym.Symbol) {
if s == rel { if s == rel {
return return
} }
for ri := 0; ri < len(s.R); ri++ { for ri := range s.R {
r := &s.R[ri] r := &s.R[ri]
targ := r.Sym targ := r.Sym
if targ == nil { if targ == nil {
...@@ -550,7 +550,7 @@ func dynrelocsym(ctxt *Link, s *sym.Symbol) { ...@@ -550,7 +550,7 @@ func dynrelocsym(ctxt *Link, s *sym.Symbol) {
return return
} }
for ri := 0; ri < len(s.R); ri++ { for ri := range s.R {
r := &s.R[ri] r := &s.R[ri]
if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal { if ctxt.BuildMode == BuildModePIE && ctxt.LinkMode == LinkInternal {
// It's expected that some relocations will be done // It's expected that some relocations will be done
...@@ -620,7 +620,6 @@ func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) { ...@@ -620,7 +620,6 @@ func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) {
} }
eaddr := addr + size eaddr := addr + size
var q []byte
for _, s := range syms { for _, s := range syms {
if !s.Attr.Reachable() { if !s.Attr.Reachable() {
continue continue
...@@ -638,7 +637,7 @@ func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) { ...@@ -638,7 +637,7 @@ func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) {
} }
ctxt.Logf("%.6x\t%-20s\n", uint64(addr), s.Name) ctxt.Logf("%.6x\t%-20s\n", uint64(addr), s.Name)
q = s.P q := s.P
for len(q) >= 16 { for len(q) >= 16 {
ctxt.Logf("%.6x\t% x\n", uint64(addr), q[:16]) ctxt.Logf("%.6x\t% x\n", uint64(addr), q[:16])
...@@ -1195,7 +1194,6 @@ func (ctxt *Link) dodata() { ...@@ -1195,7 +1194,6 @@ func (ctxt *Link) dodata() {
sect.Align = dataMaxAlign[sym.SELFGOT] sect.Align = dataMaxAlign[sym.SELFGOT]
datsize = Rnd(datsize, int64(sect.Align)) datsize = Rnd(datsize, int64(sect.Align))
sect.Vaddr = uint64(datsize) sect.Vaddr = uint64(datsize)
var toc *sym.Symbol
for _, s := range data[sym.SELFGOT] { for _, s := range data[sym.SELFGOT] {
datsize = aligndatsize(datsize, s) datsize = aligndatsize(datsize, s)
s.Sect = sect s.Sect = sect
...@@ -1203,7 +1201,7 @@ func (ctxt *Link) dodata() { ...@@ -1203,7 +1201,7 @@ func (ctxt *Link) dodata() {
s.Value = int64(uint64(datsize) - sect.Vaddr) s.Value = int64(uint64(datsize) - sect.Vaddr)
// Resolve .TOC. symbol for this object file (ppc64) // Resolve .TOC. symbol for this object file (ppc64)
toc = ctxt.Syms.ROLookup(".TOC.", int(s.Version)) toc := ctxt.Syms.ROLookup(".TOC.", int(s.Version))
if toc != nil { if toc != nil {
toc.Sect = sect toc.Sect = sect
toc.Outer = s toc.Outer = s
......
...@@ -295,7 +295,7 @@ func (d *deadcodepass) flood() { ...@@ -295,7 +295,7 @@ func (d *deadcodepass) flood() {
mpos := 0 // 0-3, the R_METHODOFF relocs of runtime.uncommontype mpos := 0 // 0-3, the R_METHODOFF relocs of runtime.uncommontype
var methods []methodref var methods []methodref
for i := 0; i < len(s.R); i++ { for i := range s.R {
r := &s.R[i] r := &s.R[i]
if r.Sym == nil { if r.Sym == nil {
continue continue
......
...@@ -463,11 +463,9 @@ func newtype(ctxt *Link, gotype *sym.Symbol) *dwarf.DWDie { ...@@ -463,11 +463,9 @@ func newtype(ctxt *Link, gotype *sym.Symbol) *dwarf.DWDie {
dotypedef(ctxt, &dwtypes, name, die) dotypedef(ctxt, &dwtypes, name, die)
newrefattr(die, dwarf.DW_AT_type, mustFind(ctxt, "void")) newrefattr(die, dwarf.DW_AT_type, mustFind(ctxt, "void"))
nfields := decodetypeFuncInCount(ctxt.Arch, gotype) nfields := decodetypeFuncInCount(ctxt.Arch, gotype)
var fld *dwarf.DWDie
var s *sym.Symbol
for i := 0; i < nfields; i++ { for i := 0; i < nfields; i++ {
s = decodetypeFuncInType(ctxt.Arch, gotype, i) s := decodetypeFuncInType(ctxt.Arch, gotype, i)
fld = newdie(ctxt, die, dwarf.DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0) fld := newdie(ctxt, die, dwarf.DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0)
newrefattr(fld, dwarf.DW_AT_type, defgotype(ctxt, s)) newrefattr(fld, dwarf.DW_AT_type, defgotype(ctxt, s))
} }
...@@ -476,8 +474,8 @@ func newtype(ctxt *Link, gotype *sym.Symbol) *dwarf.DWDie { ...@@ -476,8 +474,8 @@ func newtype(ctxt *Link, gotype *sym.Symbol) *dwarf.DWDie {
} }
nfields = decodetypeFuncOutCount(ctxt.Arch, gotype) nfields = decodetypeFuncOutCount(ctxt.Arch, gotype)
for i := 0; i < nfields; i++ { for i := 0; i < nfields; i++ {
s = decodetypeFuncOutType(ctxt.Arch, gotype, i) s := decodetypeFuncOutType(ctxt.Arch, gotype, i)
fld = newdie(ctxt, die, dwarf.DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0) fld := newdie(ctxt, die, dwarf.DW_ABRV_FUNCTYPEPARAM, s.Name[5:], 0)
newrefattr(fld, dwarf.DW_AT_type, defptrto(ctxt, defgotype(ctxt, s))) newrefattr(fld, dwarf.DW_AT_type, defptrto(ctxt, defgotype(ctxt, s)))
} }
...@@ -667,15 +665,10 @@ func synthesizeslicetypes(ctxt *Link, die *dwarf.DWDie) { ...@@ -667,15 +665,10 @@ func synthesizeslicetypes(ctxt *Link, die *dwarf.DWDie) {
} }
func mkinternaltypename(base string, arg1 string, arg2 string) string { func mkinternaltypename(base string, arg1 string, arg2 string) string {
var buf string
if arg2 == "" { if arg2 == "" {
buf = fmt.Sprintf("%s<%s>", base, arg1) return fmt.Sprintf("%s<%s>", base, arg1)
} else {
buf = fmt.Sprintf("%s<%s,%s>", base, arg1, arg2)
} }
n := buf return fmt.Sprintf("%s<%s,%s>", base, arg1, arg2)
return n
} }
// synthesizemaptypes is way too closely married to runtime/hashmap.c // synthesizemaptypes is way too closely married to runtime/hashmap.c
...@@ -1208,7 +1201,7 @@ func writelines(ctxt *Link, lib *sym.Library, textp []*sym.Symbol, ls *sym.Symbo ...@@ -1208,7 +1201,7 @@ func writelines(ctxt *Link, lib *sym.Library, textp []*sym.Symbol, ls *sym.Symbo
// example, files mentioned only in an inlined subroutine). // example, files mentioned only in an inlined subroutine).
dsym := ctxt.Syms.Lookup(dwarf.InfoPrefix+s.Name, int(s.Version)) dsym := ctxt.Syms.Lookup(dwarf.InfoPrefix+s.Name, int(s.Version))
importInfoSymbol(ctxt, dsym) importInfoSymbol(ctxt, dsym)
for ri := 0; ri < len(dsym.R); ri++ { for ri := range dsym.R {
r := &dsym.R[ri] r := &dsym.R[ri]
if r.Type != objabi.R_DWARFFILEREF { if r.Type != objabi.R_DWARFFILEREF {
continue continue
...@@ -1321,9 +1314,8 @@ func writelines(ctxt *Link, lib *sym.Library, textp []*sym.Symbol, ls *sym.Symbo ...@@ -1321,9 +1314,8 @@ func writelines(ctxt *Link, lib *sym.Library, textp []*sym.Symbol, ls *sym.Symbo
// DIE flavors (ex: variables) then those DIEs would need to // DIE flavors (ex: variables) then those DIEs would need to
// be included below. // be included below.
missing := make(map[int]interface{}) missing := make(map[int]interface{})
for fidx := 0; fidx < len(funcs); fidx++ { for _, f := range funcs {
f := funcs[fidx] for ri := range f.R {
for ri := 0; ri < len(f.R); ri++ {
r := &f.R[ri] r := &f.R[ri]
if r.Type != objabi.R_DWARFFILEREF { if r.Type != objabi.R_DWARFFILEREF {
continue continue
......
...@@ -1349,7 +1349,7 @@ func elfrelocsect(ctxt *Link, sect *sym.Section, syms []*sym.Symbol) { ...@@ -1349,7 +1349,7 @@ func elfrelocsect(ctxt *Link, sect *sym.Section, syms []*sym.Symbol) {
if s.Value >= int64(eaddr) { if s.Value >= int64(eaddr) {
break break
} }
for ri := 0; ri < len(s.R); ri++ { for ri := range s.R {
r := &s.R[ri] r := &s.R[ri]
if r.Done { if r.Done {
continue continue
......
...@@ -275,9 +275,9 @@ func loadinternal(ctxt *Link, name string) *sym.Library { ...@@ -275,9 +275,9 @@ func loadinternal(ctxt *Link, name string) *sym.Library {
return nil return nil
} }
for i := 0; i < len(ctxt.Libdir); i++ { for _, libdir := range ctxt.Libdir {
if ctxt.linkShared { if ctxt.linkShared {
shlibname := filepath.Join(ctxt.Libdir[i], name+".shlibname") shlibname := filepath.Join(libdir, name+".shlibname")
if ctxt.Debugvlog != 0 { if ctxt.Debugvlog != 0 {
ctxt.Logf("searching for %s.a in %s\n", name, shlibname) ctxt.Logf("searching for %s.a in %s\n", name, shlibname)
} }
...@@ -285,7 +285,7 @@ func loadinternal(ctxt *Link, name string) *sym.Library { ...@@ -285,7 +285,7 @@ func loadinternal(ctxt *Link, name string) *sym.Library {
return addlibpath(ctxt, "internal", "internal", "", name, shlibname) return addlibpath(ctxt, "internal", "internal", "", name, shlibname)
} }
} }
pname := filepath.Join(ctxt.Libdir[i], name+".a") pname := filepath.Join(libdir, name+".a")
if ctxt.Debugvlog != 0 { if ctxt.Debugvlog != 0 {
ctxt.Logf("searching for %s.a in %s\n", name, pname) ctxt.Logf("searching for %s.a in %s\n", name, pname)
} }
...@@ -484,7 +484,7 @@ func (ctxt *Link) loadlib() { ...@@ -484,7 +484,7 @@ func (ctxt *Link) loadlib() {
x = sym.AttrCgoExportStatic x = sym.AttrCgoExportStatic
} }
w := 0 w := 0
for i := 0; i < len(dynexp); i++ { for i := range dynexp {
if dynexp[i].Attr&x != 0 { if dynexp[i].Attr&x != 0 {
dynexp[w] = dynexp[i] dynexp[w] = dynexp[i]
w++ w++
...@@ -868,8 +868,8 @@ var internalpkg = []string{ ...@@ -868,8 +868,8 @@ var internalpkg = []string{
func ldhostobj(ld func(*Link, *bio.Reader, string, int64, string), headType objabi.HeadType, f *bio.Reader, pkg string, length int64, pn string, file string) *Hostobj { func ldhostobj(ld func(*Link, *bio.Reader, string, int64, string), headType objabi.HeadType, f *bio.Reader, pkg string, length int64, pn string, file string) *Hostobj {
isinternal := false isinternal := false
for i := 0; i < len(internalpkg); i++ { for _, intpkg := range internalpkg {
if pkg == internalpkg[i] { if pkg == intpkg {
isinternal = true isinternal = true
break break
} }
...@@ -1955,22 +1955,18 @@ func usage() { ...@@ -1955,22 +1955,18 @@ func usage() {
Exit(2) Exit(2)
} }
func doversion() {
Exitf("version %s", objabi.Version)
}
type SymbolType int8 type SymbolType int8
const ( const (
// see also http://9p.io/magic/man2html/1/nm // see also http://9p.io/magic/man2html/1/nm
TextSym SymbolType = 'T' TextSym SymbolType = 'T'
DataSym = 'D' DataSym SymbolType = 'D'
BSSSym = 'B' BSSSym SymbolType = 'B'
UndefinedSym = 'U' UndefinedSym SymbolType = 'U'
TLSSym = 't' TLSSym SymbolType = 't'
FrameSym = 'm' FrameSym SymbolType = 'm'
ParamSym = 'p' ParamSym SymbolType = 'p'
AutoSym = 'a' AutoSym SymbolType = 'a'
// Deleted auto (not a real sym, just placeholder for type) // Deleted auto (not a real sym, just placeholder for type)
DeletedAutoSym = 'x' DeletedAutoSym = 'x'
...@@ -2256,7 +2252,7 @@ func bgetc(r *bio.Reader) int { ...@@ -2256,7 +2252,7 @@ func bgetc(r *bio.Reader) int {
type markKind uint8 // for postorder traversal type markKind uint8 // for postorder traversal
const ( const (
unvisited markKind = iota _ markKind = iota
visiting visiting
visited visited
) )
......
...@@ -234,7 +234,7 @@ func machowrite(arch *sys.Arch, out *OutBuf, linkmode LinkMode) int { ...@@ -234,7 +234,7 @@ func machowrite(arch *sys.Arch, out *OutBuf, linkmode LinkMode) int {
o1 := out.Offset() o1 := out.Offset()
loadsize := 4 * 4 * ndebug loadsize := 4 * 4 * ndebug
for i := 0; i < len(load); i++ { for i := range load {
loadsize += 4 * (len(load[i].data) + 2) loadsize += 4 * (len(load[i].data) + 2)
} }
if arch.PtrSize == 8 { if arch.PtrSize == 8 {
...@@ -327,7 +327,7 @@ func machowrite(arch *sys.Arch, out *OutBuf, linkmode LinkMode) int { ...@@ -327,7 +327,7 @@ func machowrite(arch *sys.Arch, out *OutBuf, linkmode LinkMode) int {
} }
} }
for i := 0; i < len(load); i++ { for i := range load {
l := &load[i] l := &load[i]
out.Write32(l.type_) out.Write32(l.type_)
out.Write32(4 * (uint32(len(l.data)) + 2)) out.Write32(4 * (uint32(len(l.data)) + 2))
...@@ -621,13 +621,13 @@ func Asmbmacho(ctxt *Link) { ...@@ -621,13 +621,13 @@ func Asmbmacho(ctxt *Link) {
ml.data[0] = 12 /* offset to string */ ml.data[0] = 12 /* offset to string */
stringtouint32(ml.data[1:], "/usr/lib/dyld") stringtouint32(ml.data[1:], "/usr/lib/dyld")
for i := 0; i < len(dylib); i++ { for _, lib := range dylib {
ml = newMachoLoad(ctxt.Arch, LC_LOAD_DYLIB, 4+(uint32(len(dylib[i]))+1+7)/8*2) ml = newMachoLoad(ctxt.Arch, LC_LOAD_DYLIB, 4+(uint32(len(lib))+1+7)/8*2)
ml.data[0] = 24 /* offset of string from beginning of load */ ml.data[0] = 24 /* offset of string from beginning of load */
ml.data[1] = 0 /* time stamp */ ml.data[1] = 0 /* time stamp */
ml.data[2] = 0 /* version */ ml.data[2] = 0 /* version */
ml.data[3] = 0 /* compatibility version */ ml.data[3] = 0 /* compatibility version */
stringtouint32(ml.data[4:], dylib[i]) stringtouint32(ml.data[4:], lib)
} }
} }
} }
...@@ -721,7 +721,7 @@ func machosymorder(ctxt *Link) { ...@@ -721,7 +721,7 @@ func machosymorder(ctxt *Link) {
// On Mac OS X Mountain Lion, we must sort exported symbols // On Mac OS X Mountain Lion, we must sort exported symbols
// So we sort them here and pre-allocate dynid for them // So we sort them here and pre-allocate dynid for them
// See https://golang.org/issue/4029 // See https://golang.org/issue/4029
for i := 0; i < len(dynexp); i++ { for i := range dynexp {
dynexp[i].Attr |= sym.AttrReachable dynexp[i].Attr |= sym.AttrReachable
} }
machogenasmsym(ctxt) machogenasmsym(ctxt)
...@@ -919,7 +919,7 @@ func machorelocsect(ctxt *Link, sect *sym.Section, syms []*sym.Symbol) { ...@@ -919,7 +919,7 @@ func machorelocsect(ctxt *Link, sect *sym.Section, syms []*sym.Symbol) {
if s.Value >= int64(eaddr) { if s.Value >= int64(eaddr) {
break break
} }
for ri := 0; ri < len(s.R); ri++ { for ri := range s.R {
r := &s.R[ri] r := &s.R[ri]
if r.Done { if r.Done {
continue continue
......
...@@ -122,11 +122,8 @@ func numberfile(ctxt *Link, file *sym.Symbol) { ...@@ -122,11 +122,8 @@ func numberfile(ctxt *Link, file *sym.Symbol) {
} }
func renumberfiles(ctxt *Link, files []*sym.Symbol, d *sym.Pcdata) { func renumberfiles(ctxt *Link, files []*sym.Symbol, d *sym.Pcdata) {
var f *sym.Symbol
// Give files numbers. // Give files numbers.
for i := 0; i < len(files); i++ { for _, f := range files {
f = files[i]
numberfile(ctxt, f) numberfile(ctxt, f)
} }
...@@ -399,7 +396,7 @@ func (ctxt *Link) pclntab() { ...@@ -399,7 +396,7 @@ func (ctxt *Link) pclntab() {
off = addpctab(ctxt, ftab, off, &pcln.Pcline) off = addpctab(ctxt, ftab, off, &pcln.Pcline)
off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(len(pcln.Pcdata)))) off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(len(pcln.Pcdata))))
off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(len(pcln.Funcdata)))) off = int32(ftab.SetUint32(ctxt.Arch, int64(off), uint32(len(pcln.Funcdata))))
for i := 0; i < len(pcln.Pcdata); i++ { for i := range pcln.Pcdata {
off = addpctab(ctxt, ftab, off, &pcln.Pcdata[i]) off = addpctab(ctxt, ftab, off, &pcln.Pcdata[i])
} }
...@@ -409,7 +406,7 @@ func (ctxt *Link) pclntab() { ...@@ -409,7 +406,7 @@ func (ctxt *Link) pclntab() {
if off&int32(ctxt.Arch.PtrSize-1) != 0 { if off&int32(ctxt.Arch.PtrSize-1) != 0 {
off += 4 off += 4
} }
for i := 0; i < len(pcln.Funcdata); i++ { for i := range pcln.Funcdata {
if pcln.Funcdata[i] == nil { if pcln.Funcdata[i] == nil {
ftab.SetUint(ctxt.Arch, int64(off)+int64(ctxt.Arch.PtrSize)*int64(i), uint64(pcln.Funcdataoff[i])) ftab.SetUint(ctxt.Arch, int64(off)+int64(ctxt.Arch.PtrSize)*int64(i), uint64(pcln.Funcdataoff[i]))
} else { } else {
......
...@@ -526,7 +526,7 @@ func (f *peFile) emitRelocations(ctxt *Link) { ...@@ -526,7 +526,7 @@ func (f *peFile) emitRelocations(ctxt *Link) {
if sym.Value >= int64(eaddr) { if sym.Value >= int64(eaddr) {
break break
} }
for ri := 0; ri < len(sym.R); ri++ { for ri := range sym.R {
r := &sym.R[ri] r := &sym.R[ri]
if r.Done { if r.Done {
continue continue
...@@ -1082,9 +1082,8 @@ func addimports(ctxt *Link, datsect *peSection) { ...@@ -1082,9 +1082,8 @@ func addimports(ctxt *Link, datsect *peSection) {
} }
// write function names // write function names
var m *Imp
for d := dr; d != nil; d = d.next { for d := dr; d != nil; d = d.next {
for m = d.ms; m != nil; m = m.next { for m := d.ms; m != nil; m = m.next {
m.off = uint64(pefile.nextSectOffset) + uint64(ctxt.Out.Offset()) - uint64(startoff) m.off = uint64(pefile.nextSectOffset) + uint64(ctxt.Out.Offset()) - uint64(startoff)
ctxt.Out.Write16(0) // hint ctxt.Out.Write16(0) // hint
strput(ctxt.Out, m.s.Extname) strput(ctxt.Out, m.s.Extname)
...@@ -1097,7 +1096,7 @@ func addimports(ctxt *Link, datsect *peSection) { ...@@ -1097,7 +1096,7 @@ func addimports(ctxt *Link, datsect *peSection) {
n = uint64(ctxt.Out.Offset()) n = uint64(ctxt.Out.Offset())
for d := dr; d != nil; d = d.next { for d := dr; d != nil; d = d.next {
d.thunkoff = uint64(ctxt.Out.Offset()) - n d.thunkoff = uint64(ctxt.Out.Offset()) - n
for m = d.ms; m != nil; m = m.next { for m := d.ms; m != nil; m = m.next {
if pe64 != 0 { if pe64 != 0 {
ctxt.Out.Write64(m.off) ctxt.Out.Write64(m.off)
} else { } else {
...@@ -1126,7 +1125,7 @@ func addimports(ctxt *Link, datsect *peSection) { ...@@ -1126,7 +1125,7 @@ func addimports(ctxt *Link, datsect *peSection) {
ctxt.Out.SeekSet(int64(uint64(datsect.pointerToRawData) + ftbase)) ctxt.Out.SeekSet(int64(uint64(datsect.pointerToRawData) + ftbase))
for d := dr; d != nil; d = d.next { for d := dr; d != nil; d = d.next {
for m = d.ms; m != nil; m = m.next { for m := d.ms; m != nil; m = m.next {
if pe64 != 0 { if pe64 != 0 {
ctxt.Out.Write64(m.off) ctxt.Out.Write64(m.off)
} else { } else {
...@@ -1287,13 +1286,10 @@ func addpersrc(ctxt *Link) { ...@@ -1287,13 +1286,10 @@ func addpersrc(ctxt *Link) {
h.checkOffset(ctxt.Out.Offset()) h.checkOffset(ctxt.Out.Offset())
// relocation // relocation
var p []byte for ri := range rsrcsym.R {
var r *sym.Reloc r := &rsrcsym.R[ri]
var val uint32 p := rsrcsym.P[r.Off:]
for ri := 0; ri < len(rsrcsym.R); ri++ { val := uint32(int64(h.virtualAddress) + r.Add)
r = &rsrcsym.R[ri]
p = rsrcsym.P[r.Off:]
val = uint32(int64(h.virtualAddress) + r.Add)
// 32-bit little-endian // 32-bit little-endian
p[0] = byte(val) p[0] = byte(val)
......
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