From 9d673bf1db091db5660e941c1c7d2fd40e268d44 Mon Sep 17 00:00:00 2001 From: Jeremy Faller Date: Thu, 13 Feb 2020 10:38:33 -0500 Subject: [PATCH] [dev.link] cmd/link: use RO memory (when avail) in elf loader Recreation of CL 206139 in the new symbol hierarchy. Change-Id: Ic20c5c1b5db8f7eadf4c6ee4638e3c1a4e10ef3a Reviewed-on: https://go-review.googlesource.com/c/go/+/219317 Run-TryBot: Jeremy Faller TryBot-Result: Gobot Gobot Reviewed-by: Cherry Zhang --- src/cmd/link/internal/amd64/asm.go | 1 + src/cmd/link/internal/loadelf/ldelf.go | 33 +++++++++++++------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/cmd/link/internal/amd64/asm.go b/src/cmd/link/internal/amd64/asm.go index 0f0dcc4f42..f6b13c4abc 100644 --- a/src/cmd/link/internal/amd64/asm.go +++ b/src/cmd/link/internal/amd64/asm.go @@ -158,6 +158,7 @@ func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool { if targ.Type != sym.SDYNIMPORT { // have symbol if r.Off >= 2 && s.P[r.Off-2] == 0x8b { + makeWritable(s) // turn MOVQ of GOT entry into LEAQ of symbol itself s.P[r.Off-2] = 0x8d diff --git a/src/cmd/link/internal/loadelf/ldelf.go b/src/cmd/link/internal/loadelf/ldelf.go index d3cf4aed7b..2eabefd4fb 100644 --- a/src/cmd/link/internal/loadelf/ldelf.go +++ b/src/cmd/link/internal/loadelf/ldelf.go @@ -270,19 +270,20 @@ type ElfSymBytes64 struct { } type ElfSect struct { - name string - nameoff uint32 - type_ uint32 - flags uint64 - addr uint64 - off uint64 - size uint64 - link uint32 - info uint32 - align uint64 - entsize uint64 - base []byte - sym loader.Sym + name string + nameoff uint32 + type_ uint32 + flags uint64 + addr uint64 + off uint64 + size uint64 + link uint32 + info uint32 + align uint64 + entsize uint64 + base []byte + readOnlyMem bool // Is this section in readonly memory? + sym loader.Sym } type ElfObj struct { @@ -600,7 +601,6 @@ func Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, f *bio.Reader, sect := &elfobj.sect[i] if is64 != 0 { var b ElfSectBytes64 - if err := binary.Read(f, e, &b); err != nil { return errorf("malformed elf file: %v", err) } @@ -750,6 +750,7 @@ func Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, f *bio.Reader, sb.SetSize(int64(sect.size)) sb.SetAlign(int32(sect.align)) + sb.SetReadOnly(sect.readOnlyMem) sect.sym = sb.Sym() } @@ -1016,9 +1017,9 @@ func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) { return err } - sect.base = make([]byte, sect.size) elfobj.f.MustSeek(int64(uint64(elfobj.base)+sect.off), 0) - if _, err := io.ReadFull(elfobj.f, sect.base); err != nil { + sect.base, sect.readOnlyMem, err = elfobj.f.Slice(uint64(sect.size)) + if err != nil { return fmt.Errorf("short read: %v", err) }