cmd/link: mmap output file

Use mmap for writing most of the output file content,
specifically, the sections and segments. After layout, we
already know the sizes and file offsets for the sections and
segments. So we can just write the bytes by copying to a mmap'd
backing store.

The writing of the output file is split into two parts. The first
part writes the sections and segments to the mmap'd region. The
second part writes some extra content, for which we don't know
the size, so we use direct file IO.

This is in preparation for mmap'ing input files read-only.

Change-Id: I9f3b4616a9f96bfd5c940d74c50aacd6d330f7d2
Reviewed-on: https://go-review.googlesource.com/c/go/+/170738
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
Cherry Zhang 2019-04-03 22:41:48 -04:00
parent 47150aafbf
commit b2f94d3e88
24 changed files with 165 additions and 14 deletions

View file

@ -241,8 +241,29 @@ func Main(arch *sys.Arch, theArch Arch) {
order := ctxt.address()
ctxt.reloc()
dwarfcompress(ctxt)
ctxt.layout(order)
thearch.Asmb(ctxt)
filesize := ctxt.layout(order)
// Write out the output file.
// It is split into two parts (Asmb and Asmb2). The first
// part writes most of the content (sections and segments),
// for which we have computed the size and offset, in a
// mmap'd region. The second part writes more content, for
// which we don't know the size.
var outputMmapped bool
if ctxt.Arch.Family != sys.Wasm {
// Don't mmap if we're building for Wasm. Wasm file
// layout is very different so filesize is meaningless.
err := ctxt.Out.Mmap(filesize)
outputMmapped = err == nil
}
if outputMmapped {
thearch.Asmb(ctxt)
ctxt.Out.Munmap()
} else {
thearch.Asmb(ctxt)
}
thearch.Asmb2(ctxt)
ctxt.undef()
ctxt.hostlink()
ctxt.archive()