Revert "[dev.link] cmd/link: remove buffered file I/O from OutBuf"

This reverts commit b2def42d9e.

Reason for revert: trybots failing

Change-Id: I920be6d8de158b1e513154ac0eb0c8fa0cffa9f4
Reviewed-on: https://go-review.googlesource.com/c/go/+/228657
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Jeremy Faller 2020-04-16 23:33:11 +00:00
parent b2def42d9e
commit 7fe3f30bbb
15 changed files with 135 additions and 41 deletions

View file

@ -756,6 +756,7 @@ func asmb2(ctxt *ld.Link) {
if ctxt.IsELF { if ctxt.IsELF {
ctxt.Out.SeekSet(symo) ctxt.Out.SeekSet(symo)
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -765,11 +766,13 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hplan9: case objabi.Hplan9:
ld.Asmplan9sym(ctxt) ld.Asmplan9sym(ctxt)
ctxt.Out.Flush()
sym := ctxt.Syms.Lookup("pclntab", 0) sym := ctxt.Syms.Lookup("pclntab", 0)
if sym != nil { if sym != nil {
ld.Lcsize = int32(len(sym.P)) ld.Lcsize = int32(len(sym.P))
ctxt.Out.Write(sym.P) ctxt.Out.Write(sym.P)
ctxt.Out.Flush()
} }
case objabi.Hwindows: case objabi.Hwindows:
@ -814,6 +817,8 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hwindows: case objabi.Hwindows:
ld.Asmbpe(ctxt) ld.Asmbpe(ctxt)
} }
ctxt.Out.Flush()
} }
func tlsIEtoLE(s *sym.Symbol, off, size int) { func tlsIEtoLE(s *sym.Symbol, off, size int) {

View file

@ -816,6 +816,7 @@ func asmb2(ctxt *ld.Link) {
default: default:
if ctxt.IsELF { if ctxt.IsELF {
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -825,11 +826,13 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hplan9: case objabi.Hplan9:
ld.Asmplan9sym(ctxt) ld.Asmplan9sym(ctxt)
ctxt.Out.Flush()
sym := ctxt.Syms.Lookup("pclntab", 0) sym := ctxt.Syms.Lookup("pclntab", 0)
if sym != nil { if sym != nil {
ld.Lcsize = int32(len(sym.P)) ld.Lcsize = int32(len(sym.P))
ctxt.Out.Write(sym.P) ctxt.Out.Write(sym.P)
ctxt.Out.Flush()
} }
case objabi.Hwindows: case objabi.Hwindows:
@ -860,6 +863,7 @@ func asmb2(ctxt *ld.Link) {
ld.Asmbpe(ctxt) ld.Asmbpe(ctxt)
} }
ctxt.Out.Flush()
if *ld.FlagC { if *ld.FlagC {
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)

View file

@ -866,6 +866,7 @@ func asmb2(ctxt *ld.Link) {
default: default:
if ctxt.IsELF { if ctxt.IsELF {
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -875,11 +876,13 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hplan9: case objabi.Hplan9:
ld.Asmplan9sym(ctxt) ld.Asmplan9sym(ctxt)
ctxt.Out.Flush()
sym := ctxt.Syms.Lookup("pclntab", 0) sym := ctxt.Syms.Lookup("pclntab", 0)
if sym != nil { if sym != nil {
ld.Lcsize = int32(len(sym.P)) ld.Lcsize = int32(len(sym.P))
ctxt.Out.Write(sym.P) ctxt.Out.Write(sym.P)
ctxt.Out.Flush()
} }
case objabi.Hdarwin: case objabi.Hdarwin:
@ -912,6 +915,7 @@ func asmb2(ctxt *ld.Link) {
ld.Asmbmacho(ctxt) ld.Asmbmacho(ctxt)
} }
ctxt.Out.Flush()
if *ld.FlagC { if *ld.FlagC {
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)

View file

@ -32,6 +32,7 @@
package ld package ld
import ( import (
"bufio"
"bytes" "bytes"
"cmd/internal/gcprog" "cmd/internal/gcprog"
"cmd/internal/objabi" "cmd/internal/objabi"
@ -957,10 +958,11 @@ func Datblk(ctxt *Link, out *OutBuf, addr, size int64) {
// Used only on Wasm for now. // Used only on Wasm for now.
func DatblkBytes(ctxt *Link, addr int64, size int64) []byte { func DatblkBytes(ctxt *Link, addr int64, size int64) []byte {
buf := make([]byte, size) buf := bytes.NewBuffer(make([]byte, 0, size))
out := &OutBuf{heap: buf} out := &OutBuf{w: bufio.NewWriter(buf)}
writeDatblkToOutBuf(ctxt, out, addr, size) writeDatblkToOutBuf(ctxt, out, addr, size)
return buf out.Flush()
return buf.Bytes()
} }
func writeDatblkToOutBuf(ctxt *Link, out *OutBuf, addr int64, size int64) { func writeDatblkToOutBuf(ctxt *Link, out *OutBuf, addr int64, size int64) {

View file

@ -319,25 +319,35 @@ func Main(arch *sys.Arch, theArch Arch) {
// for which we have computed the size and offset, in a // for which we have computed the size and offset, in a
// mmap'd region. The second part writes more content, for // mmap'd region. The second part writes more content, for
// which we don't know the size. // which we don't know the size.
var outputMmapped bool
if ctxt.Arch.Family != sys.Wasm { if ctxt.Arch.Family != sys.Wasm {
// Don't mmap if we're building for Wasm. Wasm file // Don't mmap if we're building for Wasm. Wasm file
// layout is very different so filesize is meaningless. // layout is very different so filesize is meaningless.
if err := ctxt.Out.Mmap(filesize); err != nil { err := ctxt.Out.Mmap(filesize)
ctxt.Errorf(0, "error mapping file: %v", err) outputMmapped = err == nil
} }
if outputMmapped {
// Asmb will redirect symbols to the output file mmap, and relocations
// will be applied directly there.
bench.Start("Asmb")
thearch.Asmb(ctxt)
bench.Start("reloc")
ctxt.reloc()
} else {
// If we don't mmap, we need to apply relocations before
// writing out.
bench.Start("reloc")
ctxt.reloc()
bench.Start("Asmb")
thearch.Asmb(ctxt)
} }
// Asmb will redirect symbols to the output file mmap, and relocations
// will be applied directly there.
bench.Start("Asmb")
thearch.Asmb(ctxt)
bench.Start("reloc")
ctxt.reloc()
bench.Start("Asmb2") bench.Start("Asmb2")
thearch.Asmb2(ctxt) thearch.Asmb2(ctxt)
bench.Start("Munmap") if outputMmapped {
ctxt.Out.Close() // Close handles Munmapping if necessary. bench.Start("Munmap")
ctxt.Out.Munmap()
}
bench.Start("undef") bench.Start("undef")
ctxt.undef() ctxt.undef()

View file

@ -5,6 +5,7 @@
package ld package ld
import ( import (
"bufio"
"cmd/internal/sys" "cmd/internal/sys"
"cmd/link/internal/sym" "cmd/link/internal/sym"
"encoding/binary" "encoding/binary"
@ -60,6 +61,7 @@ type OutBuf struct {
buf []byte // backing store of mmap'd output file buf []byte // backing store of mmap'd output file
heap []byte // backing store for non-mmapped data heap []byte // backing store for non-mmapped data
w *bufio.Writer
name string name string
f *os.File f *os.File
encbuf [8]byte // temp buffer used by WriteN methods encbuf [8]byte // temp buffer used by WriteN methods
@ -76,6 +78,7 @@ func (out *OutBuf) Open(name string) error {
} }
out.off = 0 out.off = 0
out.name = name out.name = name
out.w = bufio.NewWriter(f)
out.f = f out.f = f
return nil return nil
} }
@ -107,17 +110,10 @@ func (out *OutBuf) Close() error {
if out.isView { if out.isView {
return viewCloseError return viewCloseError
} }
if out.isMmapped() { out.Flush()
return out.Munmap()
}
if out.f == nil { if out.f == nil {
return nil return nil
} }
if len(out.heap) != 0 {
if _, err := out.f.Write(out.heap); err != nil {
return err
}
}
if err := out.f.Close(); err != nil { if err := out.f.Close(); err != nil {
return err return err
} }
@ -155,6 +151,10 @@ func (out *OutBuf) Munmap() error {
// writing. When the mmapped section is full, we switch over the heap memory // writing. When the mmapped section is full, we switch over the heap memory
// for writing. // for writing.
func (out *OutBuf) writeLoc(lenToWrite int64) (int64, []byte) { func (out *OutBuf) writeLoc(lenToWrite int64) (int64, []byte) {
if !out.isMmapped() {
panic("shouldn't happen")
}
// See if we have enough space in the mmaped area. // See if we have enough space in the mmaped area.
bufLen := int64(len(out.buf)) bufLen := int64(len(out.buf))
if out.off+lenToWrite <= bufLen { if out.off+lenToWrite <= bufLen {
@ -178,6 +178,15 @@ func (out *OutBuf) writeLoc(lenToWrite int64) (int64, []byte) {
} }
func (out *OutBuf) SeekSet(p int64) { func (out *OutBuf) SeekSet(p int64) {
if p == out.off {
return
}
if !out.isMmapped() {
out.Flush()
if _, err := out.f.Seek(p, 0); err != nil {
Exitf("seeking to %d in %s: %v", p, out.name, err)
}
}
out.off = p out.off = p
} }
@ -186,18 +195,33 @@ func (out *OutBuf) Offset() int64 {
} }
// Write writes the contents of v to the buffer. // Write writes the contents of v to the buffer.
//
// As Write is backed by a bufio.Writer, callers do not have
// to explicitly handle the returned error as long as Flush is
// eventually called.
func (out *OutBuf) Write(v []byte) (int, error) { func (out *OutBuf) Write(v []byte) (int, error) {
n := len(v) if out.isMmapped() {
pos, buf := out.writeLoc(int64(n)) n := len(v)
copy(buf[pos:], v) pos, buf := out.writeLoc(int64(n))
copy(buf[pos:], v)
out.off += int64(n)
return n, nil
}
n, err := out.w.Write(v)
out.off += int64(n) out.off += int64(n)
return n, nil return n, err
} }
func (out *OutBuf) Write8(v uint8) { func (out *OutBuf) Write8(v uint8) {
pos, buf := out.writeLoc(1) if out.isMmapped() {
buf[pos] = v pos, buf := out.writeLoc(1)
out.off++ buf[pos] = v
out.off++
return
}
if err := out.w.WriteByte(v); err == nil {
out.off++
}
} }
// WriteByte is an alias for Write8 to fulfill the io.ByteWriter interface. // WriteByte is an alias for Write8 to fulfill the io.ByteWriter interface.
@ -232,11 +256,16 @@ func (out *OutBuf) Write64b(v uint64) {
} }
func (out *OutBuf) WriteString(s string) { func (out *OutBuf) WriteString(s string) {
pos, buf := out.writeLoc(int64(len(s))) if out.isMmapped() {
n := copy(buf[pos:], s) pos, buf := out.writeLoc(int64(len(s)))
if n != len(s) { n := copy(buf[pos:], s)
log.Fatalf("WriteString truncated. buffer size: %d, offset: %d, len(s)=%d", len(out.buf), out.off, len(s)) if n != len(s) {
log.Fatalf("WriteString truncated. buffer size: %d, offset: %d, len(s)=%d", len(out.buf), out.off, len(s))
}
out.off += int64(n)
return
} }
n, _ := out.w.WriteString(s)
out.off += int64(n) out.off += int64(n)
} }
@ -268,10 +297,26 @@ func (out *OutBuf) WriteStringPad(s string, n int, pad []byte) {
// edit to the symbol content. // edit to the symbol content.
// If the output file is not Mmap'd, just writes the content. // If the output file is not Mmap'd, just writes the content.
func (out *OutBuf) WriteSym(s *sym.Symbol) { func (out *OutBuf) WriteSym(s *sym.Symbol) {
n := int64(len(s.P)) // NB: We inline the Write call for speediness.
pos, buf := out.writeLoc(n) if out.isMmapped() {
copy(buf[pos:], s.P) n := int64(len(s.P))
out.off += n pos, buf := out.writeLoc(n)
s.P = buf[pos : pos+n] copy(buf[pos:], s.P)
s.Attr.Set(sym.AttrReadOnly, false) out.off += n
s.P = buf[pos : pos+n]
s.Attr.Set(sym.AttrReadOnly, false)
} else {
n, _ := out.w.Write(s.P)
out.off += int64(n)
}
}
func (out *OutBuf) Flush() {
var err error
if out.w != nil {
err = out.w.Flush()
}
if err != nil {
Exitf("flushing %s: %v", out.name, err)
}
} }

View file

@ -50,7 +50,6 @@ func TestWriteLoc(t *testing.T) {
{100, 100, 0, 100, 100, 0, true}, {100, 100, 0, 100, 100, 0, true},
{10, 10, 0, 100, 100, 0, true}, {10, 10, 0, 100, 100, 0, true},
{10, 20, 10, 100, 110, 10, true}, {10, 20, 10, 100, 110, 10, true},
{0, 0, 0, 100, 100, 0, true},
} }
for i, test := range tests { for i, test := range tests {

View file

@ -1389,6 +1389,7 @@ func (f *xcoffFile) writeLdrScn(ctxt *Link, globalOff uint64) {
} }
f.loaderSize = off + uint64(stlen) f.loaderSize = off + uint64(stlen)
ctxt.Out.Flush()
/* again for printing */ /* again for printing */
if !*flagA { if !*flagA {
@ -1558,6 +1559,8 @@ func Asmbxcoff(ctxt *Link, fileoff int64) {
// write string table // write string table
xfile.stringTable.write(ctxt.Out) xfile.stringTable.write(ctxt.Out)
ctxt.Out.Flush()
// write headers // write headers
xcoffwrite(ctxt) xcoffwrite(ctxt)
} }

View file

@ -204,6 +204,7 @@ func asmb2(ctxt *ld.Link) {
ctxt.Out.SeekSet(int64(symo)) ctxt.Out.SeekSet(int64(symo))
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -219,6 +220,7 @@ func asmb2(ctxt *ld.Link) {
ld.Asmbelf(ctxt, int64(symo)) ld.Asmbelf(ctxt, int64(symo))
} }
ctxt.Out.Flush()
if *ld.FlagC { if *ld.FlagC {
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)

View file

@ -223,6 +223,7 @@ func asmb2(ctxt *ld.Link) {
default: default:
if ctxt.IsELF { if ctxt.IsELF {
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -232,11 +233,13 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hplan9: case objabi.Hplan9:
ld.Asmplan9sym(ctxt) ld.Asmplan9sym(ctxt)
ctxt.Out.Flush()
sym := ctxt.Syms.Lookup("pclntab", 0) sym := ctxt.Syms.Lookup("pclntab", 0)
if sym != nil { if sym != nil {
ld.Lcsize = int32(len(sym.P)) ld.Lcsize = int32(len(sym.P))
ctxt.Out.Write(sym.P) ctxt.Out.Write(sym.P)
ctxt.Out.Flush()
} }
} }
} }
@ -265,6 +268,7 @@ func asmb2(ctxt *ld.Link) {
ld.Asmbelf(ctxt, int64(symo)) ld.Asmbelf(ctxt, int64(symo))
} }
ctxt.Out.Flush()
if *ld.FlagC { if *ld.FlagC {
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)

View file

@ -1130,6 +1130,7 @@ func asmb2(ctxt *ld.Link) {
default: default:
if ctxt.IsELF { if ctxt.IsELF {
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -1139,15 +1140,18 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hplan9: case objabi.Hplan9:
ld.Asmplan9sym(ctxt) ld.Asmplan9sym(ctxt)
ctxt.Out.Flush()
sym := ctxt.Syms.Lookup("pclntab", 0) sym := ctxt.Syms.Lookup("pclntab", 0)
if sym != nil { if sym != nil {
ld.Lcsize = int32(len(sym.P)) ld.Lcsize = int32(len(sym.P))
ctxt.Out.Write(sym.P) ctxt.Out.Write(sym.P)
ctxt.Out.Flush()
} }
case objabi.Haix: case objabi.Haix:
// symtab must be added once sections have been created in ld.Asmbxcoff // symtab must be added once sections have been created in ld.Asmbxcoff
ctxt.Out.Flush()
} }
} }
@ -1176,6 +1180,7 @@ func asmb2(ctxt *ld.Link) {
ld.Asmbxcoff(ctxt, int64(fileoff)) ld.Asmbxcoff(ctxt, int64(fileoff))
} }
ctxt.Out.Flush()
if *ld.FlagC { if *ld.FlagC {
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)

View file

@ -142,6 +142,7 @@ func asmb2(ctxt *ld.Link) {
ctxt.Out.SeekSet(int64(symo)) ctxt.Out.SeekSet(int64(symo))
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -156,6 +157,7 @@ func asmb2(ctxt *ld.Link) {
default: default:
ld.Errorf(nil, "unsupported operating system") ld.Errorf(nil, "unsupported operating system")
} }
ctxt.Out.Flush()
if *ld.FlagC { if *ld.FlagC {
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)

View file

@ -515,6 +515,7 @@ func asmb2(ctxt *ld.Link) {
ctxt.Out.SeekSet(int64(symo)) ctxt.Out.SeekSet(int64(symo))
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -530,6 +531,7 @@ func asmb2(ctxt *ld.Link) {
ld.Asmbelf(ctxt, int64(symo)) ld.Asmbelf(ctxt, int64(symo))
} }
ctxt.Out.Flush()
if *ld.FlagC { if *ld.FlagC {
fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)

View file

@ -187,6 +187,8 @@ func asmb2(ctxt *ld.Link) {
if !*ld.FlagS { if !*ld.FlagS {
writeNameSec(ctxt, len(hostImports), fns) writeNameSec(ctxt, len(hostImports), fns)
} }
ctxt.Out.Flush()
} }
func lookupType(sig *wasmFuncType, types *[]*wasmFuncType) uint32 { func lookupType(sig *wasmFuncType, types *[]*wasmFuncType) uint32 {

View file

@ -646,6 +646,7 @@ func asmb2(ctxt *ld.Link) {
default: default:
if ctxt.IsELF { if ctxt.IsELF {
ld.Asmelfsym(ctxt) ld.Asmelfsym(ctxt)
ctxt.Out.Flush()
ctxt.Out.Write(ld.Elfstrdat) ctxt.Out.Write(ld.Elfstrdat)
if ctxt.LinkMode == ld.LinkExternal { if ctxt.LinkMode == ld.LinkExternal {
@ -655,11 +656,13 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hplan9: case objabi.Hplan9:
ld.Asmplan9sym(ctxt) ld.Asmplan9sym(ctxt)
ctxt.Out.Flush()
sym := ctxt.Syms.Lookup("pclntab", 0) sym := ctxt.Syms.Lookup("pclntab", 0)
if sym != nil { if sym != nil {
ld.Lcsize = int32(len(sym.P)) ld.Lcsize = int32(len(sym.P))
ctxt.Out.Write(sym.P) ctxt.Out.Write(sym.P)
ctxt.Out.Flush()
} }
case objabi.Hwindows: case objabi.Hwindows:
@ -699,4 +702,6 @@ func asmb2(ctxt *ld.Link) {
case objabi.Hwindows: case objabi.Hwindows:
ld.Asmbpe(ctxt) ld.Asmbpe(ctxt)
} }
ctxt.Out.Flush()
} }