]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.link] cmd/link: use mmapped data for macho host objs
authorJeremy Faller <jeremy@golang.org>
Thu, 7 Nov 2019 20:17:42 +0000 (15:17 -0500)
committerJeremy Faller <jeremy@golang.org>
Fri, 8 Nov 2019 16:32:35 +0000 (16:32 +0000)
Move all the reads from macho host objects to mmapped data.

Change-Id: I9904f148feab6ef972d814a93964bcad04207b13
Reviewed-on: https://go-review.googlesource.com/c/go/+/205841
Run-TryBot: Jeremy Faller <jeremy@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/link/internal/amd64/asm.go
src/cmd/link/internal/loadmacho/ldmacho.go
src/go/build/deps_test.go

index 991f5523edc94746078c3056912ef936c4684f44..0f0dcc4f423e2f92bbf237b6319de3c4d606d334 100644 (file)
@@ -98,6 +98,14 @@ func gentext(ctxt *ld.Link) {
        initarray_entry.AddAddr(ctxt.Arch, initfunc)
 }
 
+// makeWritable makes a readonly symbol writable if we do opcode rewriting.
+func makeWritable(s *sym.Symbol) {
+       if s.Attr.ReadOnly() {
+               s.Attr.Set(sym.AttrReadOnly, false)
+               s.P = append([]byte(nil), s.P...)
+       }
+}
+
 func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
        targ := r.Sym
 
@@ -219,6 +227,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
                                return false
                        }
 
+                       makeWritable(s)
                        s.P[r.Off-2] = 0x8d
                        r.Type = objabi.R_PCREL
                        return true
index 85a1ebc6318689552901e697618db48b35a19dce..deea27bebb6e1f80540034b644e60d0a17cd4ca6 100644 (file)
@@ -14,7 +14,6 @@ import (
        "cmd/link/internal/sym"
        "encoding/binary"
        "fmt"
-       "io"
        "sort"
 )
 
@@ -320,10 +319,9 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int {
                return 0
        }
        rel := make([]ldMachoRel, sect.nreloc)
-       n := int(sect.nreloc * 8)
-       buf := make([]byte, n)
        m.f.MustSeek(m.base+int64(sect.reloff), 0)
-       if _, err := io.ReadFull(m.f, buf); err != nil {
+       buf, _, err := m.f.Slice(uint64(sect.nreloc * 8))
+       if err != nil {
                return -1
        }
        for i := uint32(0); i < sect.nreloc; i++ {
@@ -364,10 +362,9 @@ func macholoadrel(m *ldMachoObj, sect *ldMachoSect) int {
 
 func macholoaddsym(m *ldMachoObj, d *ldMachoDysymtab) int {
        n := int(d.nindirectsyms)
-
-       p := make([]byte, n*4)
        m.f.MustSeek(m.base+int64(d.indirectsymoff), 0)
-       if _, err := io.ReadFull(m.f, p); err != nil {
+       p, _, err := m.f.Slice(uint64(n * 4))
+       if err != nil {
                return -1
        }
 
@@ -383,9 +380,9 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
                return 0
        }
 
-       strbuf := make([]byte, symtab.strsize)
        m.f.MustSeek(m.base+int64(symtab.stroff), 0)
-       if _, err := io.ReadFull(m.f, strbuf); err != nil {
+       strbuf, _, err := m.f.Slice(uint64(symtab.strsize))
+       if err != nil {
                return -1
        }
 
@@ -394,9 +391,9 @@ func macholoadsym(m *ldMachoObj, symtab *ldMachoSymtab) int {
                symsize = 16
        }
        n := int(symtab.nsym * uint32(symsize))
-       symbuf := make([]byte, n)
        m.f.MustSeek(m.base+int64(symtab.symoff), 0)
-       if _, err := io.ReadFull(m.f, symbuf); err != nil {
+       symbuf, _, err := m.f.Slice(uint64(n))
+       if err != nil {
                return -1
        }
        sym := make([]ldMachoSym, symtab.nsym)
@@ -444,8 +441,8 @@ func load(arch *sys.Arch, localSymVersion int, lookup func(string, int) *sym.Sym
 
        base := f.Offset()
 
-       var hdr [7 * 4]uint8
-       if _, err := io.ReadFull(f, hdr[:]); err != nil {
+       hdr, _, err := f.Slice(7 * 4)
+       if err != nil {
                return errorf("reading hdr: %v", err)
        }
 
@@ -499,8 +496,8 @@ func load(arch *sys.Arch, localSymVersion int, lookup func(string, int) *sym.Sym
        }
 
        m.cmd = make([]ldMachoCmd, ncmd)
-       cmdp := make([]byte, cmdsz)
-       if _, err := io.ReadFull(f, cmdp); err != nil {
+       cmdp, _, err := f.Slice(uint64(cmdsz))
+       if err != nil {
                return errorf("reading cmds: %v", err)
        }
 
@@ -559,8 +556,8 @@ func load(arch *sys.Arch, localSymVersion int, lookup func(string, int) *sym.Sym
        }
 
        f.MustSeek(m.base+int64(c.seg.fileoff), 0)
-       dat := make([]byte, c.seg.filesz)
-       if _, err := io.ReadFull(f, dat); err != nil {
+       dat, readOnly, err := f.Slice(uint64(c.seg.filesz))
+       if err != nil {
                return errorf("cannot load object data: %v", err)
        }
 
@@ -581,6 +578,7 @@ func load(arch *sys.Arch, localSymVersion int, lookup func(string, int) *sym.Sym
                if sect.flags&0xff == 1 { // S_ZEROFILL
                        s.P = make([]byte, sect.size)
                } else {
+                       s.Attr.Set(sym.AttrReadOnly, readOnly)
                        s.P = dat[sect.addr-c.seg.vmaddr:][:sect.size]
                }
                s.Size = int64(len(s.P))
index 2ef90a977e1c2627a400ab45fc49639f8e4e53f8..62b1698797de9d1e0d135262b4be18630b01f34d 100644 (file)
@@ -243,51 +243,51 @@ var pkgDeps = map[string][]string{
        "go/types":                  {"L4", "GOPARSER", "container/heap", "go/constant"},
 
        // One of a kind.
-       "archive/tar":              {"L4", "OS", "syscall", "os/user"},
-       "archive/zip":              {"L4", "OS", "compress/flate"},
-       "container/heap":           {"sort"},
-       "compress/bzip2":           {"L4"},
-       "compress/flate":           {"L4"},
-       "compress/gzip":            {"L4", "compress/flate"},
-       "compress/lzw":             {"L4"},
-       "compress/zlib":            {"L4", "compress/flate"},
-       "context":                  {"errors", "internal/reflectlite", "sync", "sync/atomic", "time"},
-       "database/sql":             {"L4", "container/list", "context", "database/sql/driver", "database/sql/internal"},
-       "database/sql/driver":      {"L4", "context", "time", "database/sql/internal"},
-       "debug/dwarf":              {"L4"},
-       "debug/elf":                {"L4", "OS", "debug/dwarf", "compress/zlib"},
-       "debug/gosym":              {"L4"},
-       "debug/macho":              {"L4", "OS", "debug/dwarf", "compress/zlib"},
-       "debug/pe":                 {"L4", "OS", "debug/dwarf", "compress/zlib"},
-       "debug/plan9obj":           {"L4", "OS"},
-       "encoding":                 {"L4"},
-       "encoding/ascii85":         {"L4"},
-       "encoding/asn1":            {"L4", "math/big"},
-       "encoding/csv":             {"L4"},
-       "encoding/gob":             {"L4", "OS", "encoding"},
-       "encoding/hex":             {"L4"},
-       "encoding/json":            {"L4", "encoding"},
-       "encoding/pem":             {"L4"},
-       "encoding/xml":             {"L4", "encoding"},
-       "flag":                     {"L4", "OS"},
-       "go/build":                 {"L4", "OS", "GOPARSER", "internal/goroot", "internal/goversion"},
-       "html":                     {"L4"},
-       "image/draw":               {"L4", "image/internal/imageutil"},
-       "image/gif":                {"L4", "compress/lzw", "image/color/palette", "image/draw"},
-       "image/internal/imageutil": {"L4"},
-       "image/jpeg":               {"L4", "image/internal/imageutil"},
-       "image/png":                {"L4", "compress/zlib"},
-       "index/suffixarray":        {"L4", "regexp"},
-       "internal/goroot":          {"L4", "OS"},
-       "internal/singleflight":    {"sync"},
-       "internal/trace":           {"L4", "OS", "container/heap"},
-       "internal/xcoff":           {"L4", "OS", "debug/dwarf"},
-       "math/big":                 {"L4"},
-       "mime":                     {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
-       "mime/quotedprintable":     {"L4"},
-       "net/internal/socktest":    {"L4", "OS", "syscall", "internal/syscall/windows"},
-       "net/url":                  {"L4"},
-       "plugin":                   {"L0", "OS", "CGO"},
+       "archive/tar":                    {"L4", "OS", "syscall", "os/user"},
+       "archive/zip":                    {"L4", "OS", "compress/flate"},
+       "container/heap":                 {"sort"},
+       "compress/bzip2":                 {"L4"},
+       "compress/flate":                 {"L4"},
+       "compress/gzip":                  {"L4", "compress/flate"},
+       "compress/lzw":                   {"L4"},
+       "compress/zlib":                  {"L4", "compress/flate"},
+       "context":                        {"errors", "internal/reflectlite", "sync", "sync/atomic", "time"},
+       "database/sql":                   {"L4", "container/list", "context", "database/sql/driver", "database/sql/internal"},
+       "database/sql/driver":            {"L4", "context", "time", "database/sql/internal"},
+       "debug/dwarf":                    {"L4"},
+       "debug/elf":                      {"L4", "OS", "debug/dwarf", "compress/zlib"},
+       "debug/gosym":                    {"L4"},
+       "debug/macho":                    {"L4", "OS", "debug/dwarf", "compress/zlib"},
+       "debug/pe":                       {"L4", "OS", "debug/dwarf", "compress/zlib"},
+       "debug/plan9obj":                 {"L4", "OS"},
+       "encoding":                       {"L4"},
+       "encoding/ascii85":               {"L4"},
+       "encoding/asn1":                  {"L4", "math/big"},
+       "encoding/csv":                   {"L4"},
+       "encoding/gob":                   {"L4", "OS", "encoding"},
+       "encoding/hex":                   {"L4"},
+       "encoding/json":                  {"L4", "encoding"},
+       "encoding/pem":                   {"L4"},
+       "encoding/xml":                   {"L4", "encoding"},
+       "flag":                           {"L4", "OS"},
+       "go/build":                       {"L4", "OS", "GOPARSER", "internal/goroot", "internal/goversion"},
+       "html":                           {"L4"},
+       "image/draw":                     {"L4", "image/internal/imageutil"},
+       "image/gif":                      {"L4", "compress/lzw", "image/color/palette", "image/draw"},
+       "image/internal/imageutil":       {"L4"},
+       "image/jpeg":                     {"L4", "image/internal/imageutil"},
+       "image/png":                      {"L4", "compress/zlib"},
+       "index/suffixarray":              {"L4", "regexp"},
+       "internal/goroot":                {"L4", "OS"},
+       "internal/singleflight":          {"sync"},
+       "internal/trace":                 {"L4", "OS", "container/heap"},
+       "internal/xcoff":                 {"L4", "OS", "debug/dwarf"},
+       "math/big":                       {"L4"},
+       "mime":                           {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
+       "mime/quotedprintable":           {"L4"},
+       "net/internal/socktest":          {"L4", "OS", "syscall", "internal/syscall/windows"},
+       "net/url":                        {"L4"},
+       "plugin":                         {"L0", "OS", "CGO"},
        "runtime/pprof/internal/profile": {"L4", "OS", "compress/gzip", "regexp"},
        "testing/internal/testdeps":      {"L4", "internal/testlog", "runtime/pprof", "regexp"},
        "text/scanner":                   {"L4", "OS"},