Commit ed815d00 authored by Dave Cheney's avatar Dave Cheney

cmd/link/internal/ld: reduce the scope of some local declarations

Move some local declarations closer to their use, reducing their
respective lifetimes. Spotted while reviewing CL 67318.

Change-Id: I68db67fe8530344d95e50efb6587bc724e1171f5
Reviewed-on: https://go-review.googlesource.com/67370
Run-TryBot: Dave Cheney <dave@cheney.net>
Reviewed-by: default avatarMarvin Stenger <marvin.stenger94@gmail.com>
Reviewed-by: default avatarDavid Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 3487c4e0
...@@ -381,20 +381,11 @@ func trampoline(ctxt *Link, s *Symbol) { ...@@ -381,20 +381,11 @@ func trampoline(ctxt *Link, s *Symbol) {
// resolve relocations in s. // resolve relocations in s.
func relocsym(ctxt *Link, s *Symbol) { func relocsym(ctxt *Link, s *Symbol) {
var r *Reloc
var rs *Symbol
var i16 int16
var off int32
var siz int32
var fl int32
var o int64
for ri := int32(0); ri < int32(len(s.R)); ri++ { for ri := int32(0); ri < int32(len(s.R)); ri++ {
r = &s.R[ri] r := &s.R[ri]
r.Done = true r.Done = true
off = r.Off off := r.Off
siz = int32(r.Siz) siz := int32(r.Siz)
if off < 0 || off+siz > int32(len(s.P)) { if off < 0 || off+siz > int32(len(s.P)) {
rname := "" rname := ""
if r.Sym != nil { if r.Sym != nil {
...@@ -450,6 +441,7 @@ func relocsym(ctxt *Link, s *Symbol) { ...@@ -450,6 +441,7 @@ func relocsym(ctxt *Link, s *Symbol) {
} }
} }
var o int64
switch r.Type { switch r.Type {
default: default:
switch siz { switch siz {
...@@ -535,7 +527,7 @@ func relocsym(ctxt *Link, s *Symbol) { ...@@ -535,7 +527,7 @@ func relocsym(ctxt *Link, s *Symbol) {
r.Done = false r.Done = false
// set up addend for eventual relocation via outer symbol. // set up addend for eventual relocation via outer symbol.
rs = r.Sym rs := r.Sym
r.Xadd = r.Add r.Xadd = r.Add
for rs.Outer != nil { for rs.Outer != nil {
...@@ -607,7 +599,6 @@ func relocsym(ctxt *Link, s *Symbol) { ...@@ -607,7 +599,6 @@ func relocsym(ctxt *Link, s *Symbol) {
r.Xadd = r.Add + Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr) r.Xadd = r.Add + Symaddr(r.Sym) - int64(r.Sym.Sect.Vaddr)
o = r.Xadd o = r.Xadd
rs = r.Xsym
if Iself && SysArch.Family == sys.AMD64 { if Iself && SysArch.Family == sys.AMD64 {
o = 0 o = 0
} }
...@@ -647,7 +638,7 @@ func relocsym(ctxt *Link, s *Symbol) { ...@@ -647,7 +638,7 @@ func relocsym(ctxt *Link, s *Symbol) {
r.Done = false r.Done = false
// set up addend for eventual relocation via outer symbol. // set up addend for eventual relocation via outer symbol.
rs = r.Sym rs := r.Sym
r.Xadd = r.Add r.Xadd = r.Add
for rs.Outer != nil { for rs.Outer != nil {
...@@ -722,7 +713,7 @@ func relocsym(ctxt *Link, s *Symbol) { ...@@ -722,7 +713,7 @@ func relocsym(ctxt *Link, s *Symbol) {
if o != int64(int16(o)) { if o != int64(int16(o)) {
Errorf(s, "relocation address for %s is too big: %#x", r.Sym.Name, o) Errorf(s, "relocation address for %s is too big: %#x", r.Sym.Name, o)
} }
i16 = int16(o) i16 := int16(o)
ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i16)) ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i16))
case 4: case 4:
if r.Type == objabi.R_PCREL || r.Type == objabi.R_CALL { if r.Type == objabi.R_PCREL || r.Type == objabi.R_CALL {
...@@ -735,7 +726,7 @@ func relocsym(ctxt *Link, s *Symbol) { ...@@ -735,7 +726,7 @@ func relocsym(ctxt *Link, s *Symbol) {
} }
} }
fl = int32(o) fl := int32(o)
ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(fl)) ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(fl))
case 8: case 8:
ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o)) ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o))
...@@ -2233,12 +2224,11 @@ func (ctxt *Link) address() { ...@@ -2233,12 +2224,11 @@ func (ctxt *Link) address() {
var noptr *Section var noptr *Section
var bss *Section var bss *Section
var noptrbss *Section var noptrbss *Section
var vlen int64
for i, s := range Segdata.Sections { for i, s := range Segdata.Sections {
if Iself && s.Name == ".tbss" { if Iself && s.Name == ".tbss" {
continue continue
} }
vlen = int64(s.Length) vlen := int64(s.Length)
if i+1 < len(Segdata.Sections) && !(Iself && Segdata.Sections[i+1].Name == ".tbss") { if i+1 < len(Segdata.Sections) && !(Iself && Segdata.Sections[i+1].Name == ".tbss") {
vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr) vlen = int64(Segdata.Sections[i+1].Vaddr - s.Vaddr)
} }
...@@ -2270,7 +2260,7 @@ func (ctxt *Link) address() { ...@@ -2270,7 +2260,7 @@ func (ctxt *Link) address() {
Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(PEFILEALIGN))) Segdwarf.Fileoff = Segdata.Fileoff + uint64(Rnd(int64(Segdata.Filelen), int64(PEFILEALIGN)))
} }
for i, s := range Segdwarf.Sections { for i, s := range Segdwarf.Sections {
vlen = int64(s.Length) vlen := int64(s.Length)
if i+1 < len(Segdwarf.Sections) { if i+1 < len(Segdwarf.Sections) {
vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr) vlen = int64(Segdwarf.Sections[i+1].Vaddr - s.Vaddr)
} }
......
...@@ -1462,7 +1462,6 @@ func elfdynhash(ctxt *Link) { ...@@ -1462,7 +1462,6 @@ func elfdynhash(ctxt *Link) {
chain := make([]uint32, nsym) chain := make([]uint32, nsym)
buckets := make([]uint32, nbucket) buckets := make([]uint32, nbucket)
var b int
for _, sy := range ctxt.Syms.Allsym { for _, sy := range ctxt.Syms.Allsym {
if sy.Dynid <= 0 { if sy.Dynid <= 0 {
continue continue
...@@ -1475,7 +1474,7 @@ func elfdynhash(ctxt *Link) { ...@@ -1475,7 +1474,7 @@ func elfdynhash(ctxt *Link) {
name := sy.Extname name := sy.Extname
hc := elfhash(name) hc := elfhash(name)
b = int(hc % uint32(nbucket)) b := hc % uint32(nbucket)
chain[sy.Dynid] = buckets[b] chain[sy.Dynid] = buckets[b]
buckets[b] = uint32(sy.Dynid) buckets[b] = uint32(sy.Dynid)
} }
...@@ -1507,15 +1506,13 @@ func elfdynhash(ctxt *Link) { ...@@ -1507,15 +1506,13 @@ func elfdynhash(ctxt *Link) {
s = ctxt.Syms.Lookup(".gnu.version_r", 0) s = ctxt.Syms.Lookup(".gnu.version_r", 0)
i = 2 i = 2
nfile := 0 nfile := 0
var j int
var x *Elfaux
for l := needlib; l != nil; l = l.next { for l := needlib; l != nil; l = l.next {
nfile++ nfile++
// header // header
Adduint16(ctxt, s, 1) // table version Adduint16(ctxt, s, 1) // table version
j = 0 j := 0
for x = l.aux; x != nil; x = x.next { for x := l.aux; x != nil; x = x.next {
j++ j++
} }
Adduint16(ctxt, s, uint16(j)) // aux count Adduint16(ctxt, s, uint16(j)) // aux count
...@@ -1527,7 +1524,7 @@ func elfdynhash(ctxt *Link) { ...@@ -1527,7 +1524,7 @@ func elfdynhash(ctxt *Link) {
Adduint32(ctxt, s, 0) Adduint32(ctxt, s, 0)
} }
for x = l.aux; x != nil; x = x.next { for x := l.aux; x != nil; x = x.next {
x.num = i x.num = i
i++ i++
...@@ -1613,24 +1610,18 @@ func elfphrelro(seg *Segment) { ...@@ -1613,24 +1610,18 @@ func elfphrelro(seg *Segment) {
} }
func elfshname(name string) *ElfShdr { func elfshname(name string) *ElfShdr {
var off int
var sh *ElfShdr
for i := 0; i < nelfstr; i++ { for i := 0; i < nelfstr; i++ {
if name == elfstr[i].s { if name == elfstr[i].s {
off = elfstr[i].off off := elfstr[i].off
for i = 0; i < int(ehdr.shnum); i++ { for i = 0; i < int(ehdr.shnum); i++ {
sh = shdr[i] sh := shdr[i]
if sh.name == uint32(off) { if sh.name == uint32(off) {
return sh return sh
} }
} }
return newElfShdr(int64(off))
sh = newElfShdr(int64(off))
return sh
} }
} }
Exitf("cannot find elf name %s", name) Exitf("cannot find elf name %s", name)
return nil return nil
} }
...@@ -1638,14 +1629,10 @@ func elfshname(name string) *ElfShdr { ...@@ -1638,14 +1629,10 @@ func elfshname(name string) *ElfShdr {
// Create an ElfShdr for the section with name. // Create an ElfShdr for the section with name.
// Create a duplicate if one already exists with that name // Create a duplicate if one already exists with that name
func elfshnamedup(name string) *ElfShdr { func elfshnamedup(name string) *ElfShdr {
var off int
var sh *ElfShdr
for i := 0; i < nelfstr; i++ { for i := 0; i < nelfstr; i++ {
if name == elfstr[i].s { if name == elfstr[i].s {
off = elfstr[i].off off := elfstr[i].off
sh = newElfShdr(int64(off)) return newElfShdr(int64(off))
return sh
} }
} }
......
...@@ -29,8 +29,6 @@ func expandpkg(t0 string, pkg string) string { ...@@ -29,8 +29,6 @@ func expandpkg(t0 string, pkg string) string {
// libmach, so that other linkers and ar can share. // libmach, so that other linkers and ar can share.
func ldpkg(ctxt *Link, f *bio.Reader, pkg string, length int64, filename string, whence int) { func ldpkg(ctxt *Link, f *bio.Reader, pkg string, length int64, filename string, whence int) {
var p0, p1 int
if *flagG { if *flagG {
return return
} }
...@@ -95,7 +93,8 @@ func ldpkg(ctxt *Link, f *bio.Reader, pkg string, length int64, filename string, ...@@ -95,7 +93,8 @@ func ldpkg(ctxt *Link, f *bio.Reader, pkg string, length int64, filename string,
} }
// look for cgo section // look for cgo section
p0 = strings.Index(data, "\n$$ // cgo") p0 := strings.Index(data, "\n$$ // cgo")
var p1 int
if p0 >= 0 { if p0 >= 0 {
p0 += p1 p0 += p1
i := strings.IndexByte(data[p0+1:], '\n') i := strings.IndexByte(data[p0+1:], '\n')
...@@ -128,9 +127,6 @@ func ldpkg(ctxt *Link, f *bio.Reader, pkg string, length int64, filename string, ...@@ -128,9 +127,6 @@ func ldpkg(ctxt *Link, f *bio.Reader, pkg string, length int64, filename string,
func loadcgo(ctxt *Link, file string, pkg string, p string) { func loadcgo(ctxt *Link, file string, pkg string, p string) {
var next string var next string
var q string var q string
var f []string
var local string
var remote string
var lib string var lib string
var s *Symbol var s *Symbol
...@@ -143,7 +139,7 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) { ...@@ -143,7 +139,7 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) {
} }
p0 = p // save for error message p0 = p // save for error message
f = tokenize(p) f := tokenize(p)
if len(f) == 0 { if len(f) == 0 {
continue continue
} }
...@@ -153,8 +149,8 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) { ...@@ -153,8 +149,8 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) {
goto err goto err
} }
local = f[1] local := f[1]
remote = local remote := local
if len(f) > 2 { if len(f) > 2 {
remote = f[2] remote = f[2]
} }
...@@ -205,7 +201,7 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) { ...@@ -205,7 +201,7 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) {
if len(f) != 2 { if len(f) != 2 {
goto err goto err
} }
local = f[1] local := f[1]
s = ctxt.Syms.Lookup(local, 0) s = ctxt.Syms.Lookup(local, 0)
s.Type = SHOSTOBJ s.Type = SHOSTOBJ
s.Size = 0 s.Size = 0
...@@ -216,7 +212,8 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) { ...@@ -216,7 +212,8 @@ func loadcgo(ctxt *Link, file string, pkg string, p string) {
if len(f) < 2 || len(f) > 3 { if len(f) < 2 || len(f) > 3 {
goto err goto err
} }
local = f[1] local := f[1]
var remote string
if len(f) > 2 { if len(f) > 2 {
remote = f[2] remote = f[2]
} else { } else {
......
...@@ -450,36 +450,18 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -450,36 +450,18 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
localSymVersion := ctxt.Syms.IncVersion() localSymVersion := ctxt.Syms.IncVersion()
base := f.Offset() base := f.Offset()
var add uint64
var e binary.ByteOrder
var elfobj *ElfObj
var flag int
var hdr *ElfHdrBytes
var hdrbuf [64]uint8 var hdrbuf [64]uint8
var info uint64
var is64 int
var j int
var n int
var name string
var p []byte
var r []Reloc
var rela int
var rp *Reloc
var rsect *ElfSect
var s *Symbol
var sect *ElfSect
var sym ElfSym
var symbols []*Symbol
if _, err := io.ReadFull(f, hdrbuf[:]); err != nil { if _, err := io.ReadFull(f, hdrbuf[:]); err != nil {
Errorf(nil, "%s: malformed elf file: %v", pn, err) Errorf(nil, "%s: malformed elf file: %v", pn, err)
return return
} }
hdr = new(ElfHdrBytes) hdr := new(ElfHdrBytes)
binary.Read(bytes.NewReader(hdrbuf[:]), binary.BigEndian, hdr) // only byte arrays; byte order doesn't matter binary.Read(bytes.NewReader(hdrbuf[:]), binary.BigEndian, hdr) // only byte arrays; byte order doesn't matter
if string(hdr.Ident[:4]) != "\x7FELF" { if string(hdr.Ident[:4]) != "\x7FELF" {
Errorf(nil, "%s: malformed elf file", pn) Errorf(nil, "%s: malformed elf file", pn)
return return
} }
var e binary.ByteOrder
switch hdr.Ident[5] { switch hdr.Ident[5] {
case ElfDataLsb: case ElfDataLsb:
e = binary.LittleEndian e = binary.LittleEndian
...@@ -493,7 +475,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -493,7 +475,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
} }
// read header // read header
elfobj = new(ElfObj) elfobj := new(ElfObj)
elfobj.e = e elfobj.e = e
elfobj.f = f elfobj.f = f
...@@ -501,7 +483,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -501,7 +483,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
elfobj.length = length elfobj.length = length
elfobj.name = pn elfobj.name = pn
is64 = 0 is64 := 0
if hdr.Ident[4] == ElfClass64 { if hdr.Ident[4] == ElfClass64 {
is64 = 1 is64 = 1
hdr := new(ElfHdrBytes64) hdr := new(ElfHdrBytes64)
...@@ -609,7 +591,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -609,7 +591,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
Errorf(nil, "%s: malformed elf file", pn) Errorf(nil, "%s: malformed elf file", pn)
return return
} }
sect = &elfobj.sect[i] sect := &elfobj.sect[i]
if is64 != 0 { if is64 != 0 {
var b ElfSectBytes64 var b ElfSectBytes64
...@@ -655,7 +637,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -655,7 +637,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
return return
} }
sect = &elfobj.sect[elfobj.shstrndx] sect := &elfobj.sect[elfobj.shstrndx]
if err := elfmap(elfobj, sect); err != nil { if err := elfmap(elfobj, sect); err != nil {
Errorf(nil, "%s: malformed elf file: %v", pn, err) Errorf(nil, "%s: malformed elf file: %v", pn, err)
return return
...@@ -720,8 +702,8 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -720,8 +702,8 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
} }
} }
name = fmt.Sprintf("%s(%s)", pkg, sect.name) name := fmt.Sprintf("%s(%s)", pkg, sect.name)
s = ctxt.Syms.Lookup(name, localSymVersion) s := ctxt.Syms.Lookup(name, localSymVersion)
switch int(sect.flags) & (ElfSectFlagAlloc | ElfSectFlagWrite | ElfSectFlagExec) { switch int(sect.flags) & (ElfSectFlagAlloc | ElfSectFlagWrite | ElfSectFlagExec) {
default: default:
...@@ -757,9 +739,10 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -757,9 +739,10 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
// enter sub-symbols into symbol table. // enter sub-symbols into symbol table.
// symbol 0 is the null symbol. // symbol 0 is the null symbol.
symbols = make([]*Symbol, elfobj.nsymtab) symbols := make([]*Symbol, elfobj.nsymtab)
for i := 1; i < elfobj.nsymtab; i++ { for i := 1; i < elfobj.nsymtab; i++ {
var sym ElfSym
if err := readelfsym(ctxt, elfobj, i, &sym, 1, localSymVersion); err != nil { if err := readelfsym(ctxt, elfobj, i, &sym, 1, localSymVersion); err != nil {
Errorf(nil, "%s: malformed elf file: %v", pn, err) Errorf(nil, "%s: malformed elf file: %v", pn, err)
return return
...@@ -769,7 +752,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -769,7 +752,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
continue continue
} }
if sym.shndx == ElfSymShnCommon || sym.type_ == ElfSymTypeCommon { if sym.shndx == ElfSymShnCommon || sym.type_ == ElfSymTypeCommon {
s = sym.sym s := sym.sym
if uint64(s.Size) < sym.size { if uint64(s.Size) < sym.size {
s.Size = int64(sym.size) s.Size = int64(sym.size)
} }
...@@ -806,7 +789,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -806,7 +789,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
continue continue
} }
s = sym.sym s := sym.sym
if s.Outer != nil { if s.Outer != nil {
if s.Attr.DuplicateOK() { if s.Attr.DuplicateOK() {
continue continue
...@@ -831,7 +814,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -831,7 +814,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
} }
if elfobj.machine == ElfMachPower64 { if elfobj.machine == ElfMachPower64 {
flag = int(sym.other) >> 5 flag := int(sym.other) >> 5
if 2 <= flag && flag <= 6 { if 2 <= flag && flag <= 6 {
s.Localentry = 1 << uint(flag-2) s.Localentry = 1 << uint(flag-2)
} else if flag == 7 { } else if flag == 7 {
...@@ -842,8 +825,8 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -842,8 +825,8 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
// Sort outer lists by address, adding to textp. // Sort outer lists by address, adding to textp.
// This keeps textp in increasing address order. // This keeps textp in increasing address order.
for i := 0; uint(i) < elfobj.nsect; i++ { for i := uint(0); i < elfobj.nsect; i++ {
s = elfobj.sect[i].sym s := elfobj.sect[i].sym
if s == nil { if s == nil {
continue continue
} }
...@@ -867,8 +850,8 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -867,8 +850,8 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
} }
// load relocations // load relocations
for i := 0; uint(i) < elfobj.nsect; i++ { for i := uint(0); i < elfobj.nsect; i++ {
rsect = &elfobj.sect[i] rsect := &elfobj.sect[i]
if rsect.type_ != ElfSectRela && rsect.type_ != ElfSectRel { if rsect.type_ != ElfSectRela && rsect.type_ != ElfSectRel {
continue continue
} }
...@@ -880,16 +863,17 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -880,16 +863,17 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
Errorf(nil, "%s: malformed elf file: %v", pn, err) Errorf(nil, "%s: malformed elf file: %v", pn, err)
return return
} }
rela = 0 rela := 0
if rsect.type_ == ElfSectRela { if rsect.type_ == ElfSectRela {
rela = 1 rela = 1
} }
n = int(rsect.size / uint64(4+4*is64) / uint64(2+rela)) n := int(rsect.size / uint64(4+4*is64) / uint64(2+rela))
r = make([]Reloc, n) r := make([]Reloc, n)
p = rsect.base p := rsect.base
for j = 0; j < n; j++ { for j := 0; j < n; j++ {
add = 0 var add uint64
rp = &r[j] rp := &r[j]
var info uint64
if is64 != 0 { if is64 != 0 {
// 64-bit rel/rela // 64-bit rel/rela
rp.Off = int32(e.Uint64(p)) rp.Off = int32(e.Uint64(p))
...@@ -924,6 +908,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -924,6 +908,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
if info>>32 == 0 { // absolute relocation, don't bother reading the null symbol if info>>32 == 0 { // absolute relocation, don't bother reading the null symbol
rp.Sym = nil rp.Sym = nil
} else { } else {
var sym ElfSym
if err := readelfsym(ctxt, elfobj, int(info>>32), &sym, 0, 0); err != nil { if err := readelfsym(ctxt, elfobj, int(info>>32), &sym, 0, 0); err != nil {
Errorf(nil, "%s: malformed elf file: %v", pn, err) Errorf(nil, "%s: malformed elf file: %v", pn, err)
return return
...@@ -964,7 +949,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) { ...@@ -964,7 +949,7 @@ func ldelf(ctxt *Link, f *bio.Reader, pkg string, length int64, pn string) {
sort.Sort(rbyoff(r[:n])) sort.Sort(rbyoff(r[:n]))
// just in case // just in case
s = sect.sym s := sect.sym
s.R = r s.R = r
s.R = s.R[:n] s.R = s.R[:n]
} }
......
...@@ -306,12 +306,9 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int { ...@@ -306,12 +306,9 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int {
if _, err := io.ReadFull(m.f, buf); err != nil { if _, err := io.ReadFull(m.f, buf); err != nil {
return -1 return -1
} }
var p []byte for i := uint32(0); i < sect.nreloc; i++ {
var r *ldMachoRel r := &rel[i]
var v uint32 p := buf[i*8:]
for i := 0; uint32(i) < sect.nreloc; i++ {
r = &rel[i]
p = buf[i*8:]
r.addr = m.e.Uint32(p) r.addr = m.e.Uint32(p)
// TODO(rsc): Wrong interpretation for big-endian bitfields? // TODO(rsc): Wrong interpretation for big-endian bitfields?
...@@ -319,7 +316,7 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int { ...@@ -319,7 +316,7 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int {
// scatterbrained relocation // scatterbrained relocation
r.scattered = 1 r.scattered = 1
v = r.addr >> 24 v := r.addr >> 24
r.addr &= 0xFFFFFF r.addr &= 0xFFFFFF
r.type_ = uint8(v & 0xF) r.type_ = uint8(v & 0xF)
v >>= 4 v >>= 4
...@@ -328,7 +325,7 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int { ...@@ -328,7 +325,7 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int {
r.pcrel = uint8(v & 1) r.pcrel = uint8(v & 1)
r.value = m.e.Uint32(p[4:]) r.value = m.e.Uint32(p[4:])
} else { } else {
v = m.e.Uint32(p[4:]) v := m.e.Uint32(p[4:])
r.symnum = v & 0xFFFFFF r.symnum = v & 0xFFFFFF
v >>= 24 v >>= 24
r.pcrel = uint8(v & 1) r.pcrel = uint8(v & 1)
...@@ -390,11 +387,9 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int { ...@@ -390,11 +387,9 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
} }
sym := make([]ldMachoSym, symtab.nsym) sym := make([]ldMachoSym, symtab.nsym)
p := symbuf p := symbuf
var s *ldMachoSym for i := uint32(0); i < symtab.nsym; i++ {
var v uint32 s := &sym[i]
for i := 0; uint32(i) < symtab.nsym; i++ { v := m.e.Uint32(p)
s = &sym[i]
v = m.e.Uint32(p)
if v >= symtab.strsize { if v >= symtab.strsize {
return -1 return -1
} }
......
...@@ -273,11 +273,8 @@ func machowrite() int { ...@@ -273,11 +273,8 @@ func machowrite() int {
Thearch.Lput(0) /* reserved */ Thearch.Lput(0) /* reserved */
} }
var j int
var s *MachoSeg
var t *MachoSect
for i := 0; i < nseg; i++ { for i := 0; i < nseg; i++ {
s = &seg[i] s := &seg[i]
if macho64 { if macho64 {
Thearch.Lput(LC_SEGMENT_64) Thearch.Lput(LC_SEGMENT_64)
Thearch.Lput(72 + 80*s.nsect) Thearch.Lput(72 + 80*s.nsect)
...@@ -304,8 +301,8 @@ func machowrite() int { ...@@ -304,8 +301,8 @@ func machowrite() int {
Thearch.Lput(s.flag) Thearch.Lput(s.flag)
} }
for j = 0; uint32(j) < s.nsect; j++ { for j := uint32(0); j < s.nsect; j++ {
t = &s.sect[j] t := &s.sect[j]
if macho64 { if macho64 {
strnput(t.name, 16) strnput(t.name, 16)
strnput(t.segname, 16) strnput(t.segname, 16)
...@@ -335,12 +332,11 @@ func machowrite() int { ...@@ -335,12 +332,11 @@ func machowrite() int {
} }
} }
var l *MachoLoad
for i := 0; i < len(load); i++ { for i := 0; i < len(load); i++ {
l = &load[i] l := &load[i]
Thearch.Lput(l.type_) Thearch.Lput(l.type_)
Thearch.Lput(4 * (uint32(len(l.data)) + 2)) Thearch.Lput(4 * (uint32(len(l.data)) + 2))
for j = 0; j < len(l.data); j++ { for j := 0; j < len(l.data); j++ {
Thearch.Lput(l.data[j]) Thearch.Lput(l.data[j])
} }
} }
......
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