cmd/link: use sym.Symbol in addpersrc

addpersrc is called very late, after we have converted to
sym.Symbols and various fields in loader representation have been
dropped. Use the Symbol representation there.

Fixes #39658.

Change-Id: I616e838655b6f01554644171317e2cc5cefabf39
Reviewed-on: https://go-review.googlesource.com/c/go/+/238779
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
This commit is contained in:
Cherry Zhang 2020-06-18 18:05:10 -04:00
parent 27a0012bd6
commit f2bba30e40
4 changed files with 60 additions and 6 deletions

View file

@ -1470,6 +1470,7 @@ func setpersrc(ctxt *Link, sym loader.Sym) {
} }
rsrcsym = sym rsrcsym = sym
ctxt.loader.SetAttrReachable(rsrcsym, true)
} }
func addpersrc(ctxt *Link) { func addpersrc(ctxt *Link) {
@ -1477,18 +1478,18 @@ func addpersrc(ctxt *Link) {
return return
} }
data := ctxt.loader.Data(rsrcsym) rsrc := ctxt.loader.Syms[rsrcsym]
data := rsrc.P
size := len(data) size := len(data)
h := pefile.addSection(".rsrc", size, size) h := pefile.addSection(".rsrc", size, size)
h.characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_CNT_INITIALIZED_DATA h.characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_CNT_INITIALIZED_DATA
h.checkOffset(ctxt.Out.Offset()) h.checkOffset(ctxt.Out.Offset())
// relocation // relocation
relocs := ctxt.loader.Relocs(rsrcsym) for ri := range rsrc.R {
for i := 0; i < relocs.Count(); i++ { r := &rsrc.R[ri]
r := relocs.At2(i) p := data[r.Off:]
p := data[r.Off():] val := uint32(int64(h.virtualAddress) + r.Add)
val := uint32(int64(h.virtualAddress) + r.Add())
// 32-bit little-endian // 32-bit little-endian
p[0] = byte(val) p[0] = byte(val)

View file

@ -746,3 +746,37 @@ func TestIndexMismatch(t *testing.T) {
t.Errorf("did not see expected error message. out:\n%s", out) t.Errorf("did not see expected error message. out:\n%s", out)
} }
} }
func TestPErsrc(t *testing.T) {
// Test that PE rsrc section is handled correctly (issue 39658).
testenv.MustHaveGoBuild(t)
if runtime.GOARCH != "amd64" || runtime.GOOS != "windows" {
t.Skipf("this is a windows/amd64-only test")
}
tmpdir, err := ioutil.TempDir("", "TestPErsrc")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpdir)
pkgdir := filepath.Join("testdata", "testPErsrc")
exe := filepath.Join(tmpdir, "a.exe")
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe)
cmd.Dir = pkgdir
// cmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64") // uncomment if debugging in a cross-compiling environment
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("building failed: %v, output:\n%s", err, out)
}
// Check that the binary contains the rsrc data
b, err := ioutil.ReadFile(exe)
if err != nil {
t.Fatalf("reading output failed: %v", err)
}
if !bytes.Contains(b, []byte("Hello Gophers!")) {
t.Fatalf("binary does not contain expected content")
}
}

View file

@ -0,0 +1,19 @@
// Copyright 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test that a PE rsrc section is handled correctly (issue 39658).
//
// rsrc.syso is created with:
// windres -i a.rc -o rsrc.syso -O coff
// on windows-amd64-2016 builder, where a.rc is a text file with
// the following content:
//
// resname RCDATA {
// "Hello Gophers!\0",
// "This is a test.\0",
// }
package main
func main() {}

Binary file not shown.