]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: apply relocations later
authorCherry Zhang <cherryyz@google.com>
Thu, 4 Apr 2019 03:35:44 +0000 (23:35 -0400)
committerCherry Zhang <cherryyz@google.com>
Fri, 19 Apr 2019 18:25:33 +0000 (18:25 +0000)
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>
src/cmd/link/internal/ld/data.go
src/cmd/link/internal/ld/main.go
src/cmd/link/internal/ld/outbuf.go

index 7f4fe71cb42cf73c521e44b8c10a45eaa7d32ae5..5d31de99ee538eccbd4df79a5c11cbce2ec00dd7 100644 (file)
@@ -765,7 +765,7 @@ func blk(out *OutBuf, syms []*sym.Symbol, addr, size int64, pad []byte) {
                        out.WriteStringPad("", int(s.Value-addr), pad)
                        addr = s.Value
                }
-               out.Write(s.P)
+               out.WriteSym(s)
                addr += int64(len(s.P))
                if addr < s.Value+s.Size {
                        out.WriteStringPad("", int(s.Value+s.Size-addr), pad)
index aac37883e1bb718173c04165fb451165e2e90d0f..e0725a13841079aa5e5e44a90bc4546b190364f5 100644 (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)
index f1b5d7495c546ca4bff64269a28a8355a4fb3f8a..3efd43d6ae0575a5c8addd2a4b22c504dd44dc6a 100644 (file)
@@ -7,6 +7,7 @@ package ld
 import (
        "bufio"
        "cmd/internal/sys"
+       "cmd/link/internal/sym"
        "encoding/binary"
        "log"
        "os"
@@ -148,6 +149,20 @@ func (out *OutBuf) WriteStringPad(s string, n int, pad []byte) {
        }
 }
 
+// WriteSym writes the content of a Symbol, then changes the Symbol's content
+// to point to the output buffer that we just wrote, so we can apply further
+// edit to the symbol content.
+// If the output file is not Mmap'd, just writes the content.
+func (out *OutBuf) WriteSym(s *sym.Symbol) {
+       if out.buf != nil {
+               start := out.off
+               out.Write(s.P)
+               s.P = out.buf[start:out.off]
+       } else {
+               out.Write(s.P)
+       }
+}
+
 func (out *OutBuf) Flush() {
        var err error
        if out.buf != nil {