cmd/link: apply relocations later

Move the phase of applying relocations later, after the sections
and segments are written to the mmap'd output region. Then apply
relocations directly in the output region, instead of the input.
So the input slices we read in don't need to be modified.

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

Change-Id: If9c80657b4469da36aec5a9ab6acf664f5af8fa0
Reviewed-on: https://go-review.googlesource.com/c/go/+/170739
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 23:35:44 -04:00
parent 248444d5eb
commit f957a7e357
3 changed files with 22 additions and 2 deletions

View file

@ -240,7 +240,6 @@ func Main(arch *sys.Arch, theArch Arch) {
ctxt.dodata()
order := ctxt.address()
dwarfcompress(ctxt)
ctxt.reloc()
filesize := ctxt.layout(order)
// Write out the output file.
@ -257,9 +256,15 @@ func Main(arch *sys.Arch, theArch Arch) {
outputMmapped = err == nil
}
if outputMmapped {
// Asmb will redirect symbols to the output file mmap, and relocations
// will be applied directly there.
thearch.Asmb(ctxt)
ctxt.reloc()
ctxt.Out.Munmap()
} else {
// If we don't mmap, we need to apply relocations before
// writing out.
ctxt.reloc()
thearch.Asmb(ctxt)
}
thearch.Asmb2(ctxt)