Commit 5f94ff4c authored by Dave Cheney's avatar Dave Cheney

cmd/link/internal/ld: move ld.Cpos to coutbuf.Offset

This change moves the ld.Cpos function to a method on coutbuf. This is
part of a larger change that makes ld.outbuf look more like a bio.Buf in
an effort to eventually replace the former with the latter.

Change-Id: I506f7131935a2aa903fa302a0fab0c5be50220fd
Reviewed-on: https://go-review.googlesource.com/27578
Run-TryBot: Dave Cheney <dave@cheney.net>
Reviewed-by: 's avatarDavid Crawshaw <crawshaw@golang.org>
Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
parent 61d5daea
...@@ -746,7 +746,7 @@ func Codeblk(ctxt *Link, addr int64, size int64) { ...@@ -746,7 +746,7 @@ func Codeblk(ctxt *Link, addr int64, size int64) {
} }
func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) { func CodeblkPad(ctxt *Link, addr int64, size int64, pad []byte) {
if *flagA { if *flagA {
fmt.Fprintf(ctxt.Bso, "codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) fmt.Fprintf(ctxt.Bso, "codeblk [%#x,%#x) at offset %#x\n", addr, addr+size, coutbuf.Offset())
} }
blk(ctxt, ctxt.Textp, addr, size, pad) blk(ctxt, ctxt.Textp, addr, size, pad)
...@@ -857,7 +857,7 @@ func blk(ctxt *Link, syms []*Symbol, addr, size int64, pad []byte) { ...@@ -857,7 +857,7 @@ func blk(ctxt *Link, syms []*Symbol, addr, size int64, pad []byte) {
func Datblk(ctxt *Link, addr int64, size int64) { func Datblk(ctxt *Link, addr int64, size int64) {
if *flagA { if *flagA {
fmt.Fprintf(ctxt.Bso, "datblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) fmt.Fprintf(ctxt.Bso, "datblk [%#x,%#x) at offset %#x\n", addr, addr+size, coutbuf.Offset())
} }
blk(ctxt, datap, addr, size, zeros[:]) blk(ctxt, datap, addr, size, zeros[:])
...@@ -928,7 +928,7 @@ func Datblk(ctxt *Link, addr int64, size int64) { ...@@ -928,7 +928,7 @@ func Datblk(ctxt *Link, addr int64, size int64) {
func Dwarfblk(ctxt *Link, addr int64, size int64) { func Dwarfblk(ctxt *Link, addr int64, size int64) {
if *flagA { if *flagA {
fmt.Fprintf(ctxt.Bso, "dwarfblk [%#x,%#x) at offset %#x\n", addr, addr+size, Cpos()) fmt.Fprintf(ctxt.Bso, "dwarfblk [%#x,%#x) at offset %#x\n", addr, addr+size, coutbuf.Offset())
} }
blk(ctxt, dwarfp, addr, size, zeros[:]) blk(ctxt, dwarfp, addr, size, zeros[:])
......
...@@ -1729,7 +1729,7 @@ func elfrelocsect(ctxt *Link, sect *Section, syms []*Symbol) { ...@@ -1729,7 +1729,7 @@ func elfrelocsect(ctxt *Link, sect *Section, syms []*Symbol) {
return return
} }
sect.Reloff = uint64(Cpos()) sect.Reloff = uint64(coutbuf.Offset())
for i, s := range syms { for i, s := range syms {
if !s.Attr.Reachable() { if !s.Attr.Reachable() {
continue continue
...@@ -1768,11 +1768,11 @@ func elfrelocsect(ctxt *Link, sect *Section, syms []*Symbol) { ...@@ -1768,11 +1768,11 @@ func elfrelocsect(ctxt *Link, sect *Section, syms []*Symbol) {
} }
} }
sect.Rellen = uint64(Cpos()) - sect.Reloff sect.Rellen = uint64(coutbuf.Offset()) - sect.Reloff
} }
func Elfemitreloc(ctxt *Link) { func Elfemitreloc(ctxt *Link) {
for Cpos()&7 != 0 { for coutbuf.Offset()&7 != 0 {
Cput(0) Cput(0)
} }
......
...@@ -229,6 +229,10 @@ func (w *outBuf) WriteString(s string) (n int, err error) { ...@@ -229,6 +229,10 @@ func (w *outBuf) WriteString(s string) (n int, err error) {
return n, err return n, err
} }
func (w *outBuf) Offset() int64 {
return w.off
}
var coutbuf outBuf var coutbuf outBuf
const pkgname = "__.PKGDEF" const pkgname = "__.PKGDEF"
...@@ -1771,10 +1775,6 @@ func Cflush() { ...@@ -1771,10 +1775,6 @@ func Cflush() {
} }
} }
func Cpos() int64 {
return coutbuf.off
}
func Cseek(p int64) { func Cseek(p int64) {
if p == coutbuf.off { if p == coutbuf.off {
return return
......
...@@ -186,7 +186,7 @@ var dylib []string ...@@ -186,7 +186,7 @@ var dylib []string
var linkoff int64 var linkoff int64
func machowrite() int { func machowrite() int {
o1 := Cpos() o1 := coutbuf.Offset()
loadsize := 4 * 4 * ndebug loadsize := 4 * 4 * ndebug
for i := 0; i < len(load); i++ { for i := 0; i < len(load); i++ {
...@@ -291,7 +291,7 @@ func machowrite() int { ...@@ -291,7 +291,7 @@ func machowrite() int {
} }
} }
return int(Cpos() - o1) return int(coutbuf.Offset() - o1)
} }
func (ctxt *Link) domacho() { func (ctxt *Link) domacho() {
...@@ -815,7 +815,7 @@ func machorelocsect(ctxt *Link, sect *Section, syms []*Symbol) { ...@@ -815,7 +815,7 @@ func machorelocsect(ctxt *Link, sect *Section, syms []*Symbol) {
return return
} }
sect.Reloff = uint64(Cpos()) sect.Reloff = uint64(coutbuf.Offset())
for i, s := range syms { for i, s := range syms {
if !s.Attr.Reachable() { if !s.Attr.Reachable() {
continue continue
...@@ -847,11 +847,11 @@ func machorelocsect(ctxt *Link, sect *Section, syms []*Symbol) { ...@@ -847,11 +847,11 @@ func machorelocsect(ctxt *Link, sect *Section, syms []*Symbol) {
} }
} }
sect.Rellen = uint64(Cpos()) - sect.Reloff sect.Rellen = uint64(coutbuf.Offset()) - sect.Reloff
} }
func Machoemitreloc(ctxt *Link) { func Machoemitreloc(ctxt *Link) {
for Cpos()&7 != 0 { for coutbuf.Offset()&7 != 0 {
Cput(0) Cput(0)
} }
......
...@@ -570,7 +570,7 @@ func peimporteddlls() []string { ...@@ -570,7 +570,7 @@ func peimporteddlls() []string {
} }
func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) { func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) {
startoff := Cpos() startoff := coutbuf.Offset()
dynamic := Linklookup(ctxt, ".windynamic", 0) dynamic := Linklookup(ctxt, ".windynamic", 0)
// skip import descriptor table (will write it later) // skip import descriptor table (will write it later)
...@@ -583,7 +583,7 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) { ...@@ -583,7 +583,7 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) {
// write dll names // write dll names
for d := dr; d != nil; d = d.next { for d := dr; d != nil; d = d.next {
d.nameoff = uint64(Cpos()) - uint64(startoff) d.nameoff = uint64(coutbuf.Offset()) - uint64(startoff)
strput(d.name) strput(d.name)
} }
...@@ -591,18 +591,18 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) { ...@@ -591,18 +591,18 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) {
var m *Imp 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(nextsectoff) + uint64(Cpos()) - uint64(startoff) m.off = uint64(nextsectoff) + uint64(coutbuf.Offset()) - uint64(startoff)
Wputl(0) // hint Wputl(0) // hint
strput(m.s.Extname) strput(m.s.Extname)
} }
} }
// write OriginalFirstThunks // write OriginalFirstThunks
oftbase := uint64(Cpos()) - uint64(startoff) oftbase := uint64(coutbuf.Offset()) - uint64(startoff)
n = uint64(Cpos()) n = uint64(coutbuf.Offset())
for d := dr; d != nil; d = d.next { for d := dr; d != nil; d = d.next {
d.thunkoff = uint64(Cpos()) - n d.thunkoff = uint64(coutbuf.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 {
Vputl(m.off) Vputl(m.off)
...@@ -619,13 +619,13 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) { ...@@ -619,13 +619,13 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) {
} }
// add pe section and pad it at the end // add pe section and pad it at the end
n = uint64(Cpos()) - uint64(startoff) n = uint64(coutbuf.Offset()) - uint64(startoff)
isect := addpesection(ctxt, ".idata", int(n), int(n)) isect := addpesection(ctxt, ".idata", int(n), int(n))
isect.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE isect.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE
chksectoff(ctxt, isect, startoff) chksectoff(ctxt, isect, startoff)
strnput("", int(uint64(isect.SizeOfRawData)-n)) strnput("", int(uint64(isect.SizeOfRawData)-n))
endoff := Cpos() endoff := coutbuf.Offset()
// write FirstThunks (allocated in .data section) // write FirstThunks (allocated in .data section)
ftbase := uint64(dynamic.Value) - uint64(datsect.VirtualAddress) - PEBASE ftbase := uint64(dynamic.Value) - uint64(datsect.VirtualAddress) - PEBASE
...@@ -666,7 +666,6 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) { ...@@ -666,7 +666,6 @@ func addimports(ctxt *Link, datsect *IMAGE_SECTION_HEADER) {
// update data directory // update data directory
dd[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = isect.VirtualAddress dd[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress = isect.VirtualAddress
dd[IMAGE_DIRECTORY_ENTRY_IMPORT].Size = isect.VirtualSize dd[IMAGE_DIRECTORY_ENTRY_IMPORT].Size = isect.VirtualSize
dd[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress = uint32(dynamic.Value - PEBASE) dd[IMAGE_DIRECTORY_ENTRY_IAT].VirtualAddress = uint32(dynamic.Value - PEBASE)
dd[IMAGE_DIRECTORY_ENTRY_IAT].Size = uint32(dynamic.Size) dd[IMAGE_DIRECTORY_ENTRY_IAT].Size = uint32(dynamic.Size)
...@@ -712,7 +711,7 @@ func addexports(ctxt *Link) { ...@@ -712,7 +711,7 @@ func addexports(ctxt *Link) {
sect := addpesection(ctxt, ".edata", size, size) sect := addpesection(ctxt, ".edata", size, size)
sect.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ sect.Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ
chksectoff(ctxt, sect, Cpos()) chksectoff(ctxt, sect, coutbuf.Offset())
va := int(sect.VirtualAddress) va := int(sect.VirtualAddress)
dd[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = uint32(va) dd[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress = uint32(va)
dd[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sect.VirtualSize dd[IMAGE_DIRECTORY_ENTRY_EXPORT].Size = sect.VirtualSize
...@@ -772,7 +771,7 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int { ...@@ -772,7 +771,7 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
relocs := 0 relocs := 0
sect.Reloff = uint64(Cpos()) sect.Reloff = uint64(coutbuf.Offset())
for i, s := range syms { for i, s := range syms {
if !s.Attr.Reachable() { if !s.Attr.Reachable() {
continue continue
...@@ -814,18 +813,18 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int { ...@@ -814,18 +813,18 @@ func perelocsect(ctxt *Link, sect *Section, syms []*Symbol) int {
} }
} }
sect.Rellen = uint64(Cpos()) - sect.Reloff sect.Rellen = uint64(coutbuf.Offset()) - sect.Reloff
return relocs return relocs
} }
// peemitreloc emits relocation entries for go.o in external linking. // peemitreloc emits relocation entries for go.o in external linking.
func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) { func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
for Cpos()&7 != 0 { for coutbuf.Offset()&7 != 0 {
Cput(0) Cput(0)
} }
text.PointerToRelocations = uint32(Cpos()) text.PointerToRelocations = uint32(coutbuf.Offset())
// first entry: extended relocs // first entry: extended relocs
Lputl(0) // placeholder for number of relocation + 1 Lputl(0) // placeholder for number of relocation + 1
Lputl(0) Lputl(0)
...@@ -836,7 +835,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) { ...@@ -836,7 +835,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
n += perelocsect(ctxt, sect, datap) n += perelocsect(ctxt, sect, datap)
} }
cpos := Cpos() cpos := coutbuf.Offset()
Cseek(int64(text.PointerToRelocations)) Cseek(int64(text.PointerToRelocations))
Lputl(uint32(n)) Lputl(uint32(n))
Cseek(cpos) Cseek(cpos)
...@@ -859,7 +858,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) { ...@@ -859,7 +858,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
n += perelocsect(ctxt, sect, datap) n += perelocsect(ctxt, sect, datap)
} }
cpos = Cpos() cpos = coutbuf.Offset()
Cseek(int64(data.PointerToRelocations)) Cseek(int64(data.PointerToRelocations))
Lputl(uint32(n)) Lputl(uint32(n))
Cseek(cpos) Cseek(cpos)
...@@ -873,7 +872,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) { ...@@ -873,7 +872,7 @@ func peemitreloc(ctxt *Link, text, data, ctors *IMAGE_SECTION_HEADER) {
dottext := Linklookup(ctxt, ".text", 0) dottext := Linklookup(ctxt, ".text", 0)
ctors.NumberOfRelocations = 1 ctors.NumberOfRelocations = 1
ctors.PointerToRelocations = uint32(Cpos()) ctors.PointerToRelocations = uint32(coutbuf.Offset())
sectoff := ctors.VirtualAddress sectoff := ctors.VirtualAddress
Lputl(sectoff) Lputl(sectoff)
Lputl(uint32(dottext.Dynid)) Lputl(uint32(dottext.Dynid))
...@@ -1017,7 +1016,7 @@ func writePESymTableRecords(ctxt *Link) int { ...@@ -1017,7 +1016,7 @@ func writePESymTableRecords(ctxt *Link) int {
} }
func addpesymtable(ctxt *Link) { func addpesymtable(ctxt *Link) {
symtabStartPos := Cpos() symtabStartPos := coutbuf.Offset()
// write COFF symbol table // write COFF symbol table
var symcnt int var symcnt int
...@@ -1063,7 +1062,7 @@ func addpersrc(ctxt *Link) { ...@@ -1063,7 +1062,7 @@ func addpersrc(ctxt *Link) {
h := addpesection(ctxt, ".rsrc", int(rsrcsym.Size), int(rsrcsym.Size)) h := addpesection(ctxt, ".rsrc", int(rsrcsym.Size), int(rsrcsym.Size))
h.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_CNT_INITIALIZED_DATA h.Characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_CNT_INITIALIZED_DATA
chksectoff(ctxt, h, Cpos()) chksectoff(ctxt, h, coutbuf.Offset())
// relocation // relocation
var p []byte var p []byte
...@@ -1113,7 +1112,7 @@ func addinitarray(ctxt *Link) (c *IMAGE_SECTION_HEADER) { ...@@ -1113,7 +1112,7 @@ func addinitarray(ctxt *Link) (c *IMAGE_SECTION_HEADER) {
c.SizeOfRawData = uint32(size) c.SizeOfRawData = uint32(size)
Cseek(int64(c.PointerToRawData)) Cseek(int64(c.PointerToRawData))
chksectoff(ctxt, c, Cpos()) chksectoff(ctxt, c, coutbuf.Offset())
init_entry := Linklookup(ctxt, *flagEntrySymbol, 0) init_entry := Linklookup(ctxt, *flagEntrySymbol, 0)
addr := uint64(init_entry.Value) - init_entry.Sect.Vaddr addr := uint64(init_entry.Value) - init_entry.Sect.Vaddr
......
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