Skip to content

Commit f2bba30

Browse files
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Than McIntosh <[email protected]> Reviewed-by: Jeremy Faller <[email protected]>
1 parent 27a0012 commit f2bba30

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

src/cmd/link/internal/ld/pe.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,25 +1470,26 @@ func setpersrc(ctxt *Link, sym loader.Sym) {
14701470
}
14711471

14721472
rsrcsym = sym
1473+
ctxt.loader.SetAttrReachable(rsrcsym, true)
14731474
}
14741475

14751476
func addpersrc(ctxt *Link) {
14761477
if rsrcsym == 0 {
14771478
return
14781479
}
14791480

1480-
data := ctxt.loader.Data(rsrcsym)
1481+
rsrc := ctxt.loader.Syms[rsrcsym]
1482+
data := rsrc.P
14811483
size := len(data)
14821484
h := pefile.addSection(".rsrc", size, size)
14831485
h.characteristics = IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | IMAGE_SCN_CNT_INITIALIZED_DATA
14841486
h.checkOffset(ctxt.Out.Offset())
14851487

14861488
// relocation
1487-
relocs := ctxt.loader.Relocs(rsrcsym)
1488-
for i := 0; i < relocs.Count(); i++ {
1489-
r := relocs.At2(i)
1490-
p := data[r.Off():]
1491-
val := uint32(int64(h.virtualAddress) + r.Add())
1489+
for ri := range rsrc.R {
1490+
r := &rsrc.R[ri]
1491+
p := data[r.Off:]
1492+
val := uint32(int64(h.virtualAddress) + r.Add)
14921493

14931494
// 32-bit little-endian
14941495
p[0] = byte(val)

src/cmd/link/link_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,37 @@ func TestIndexMismatch(t *testing.T) {
746746
t.Errorf("did not see expected error message. out:\n%s", out)
747747
}
748748
}
749+
750+
func TestPErsrc(t *testing.T) {
751+
// Test that PE rsrc section is handled correctly (issue 39658).
752+
testenv.MustHaveGoBuild(t)
753+
754+
if runtime.GOARCH != "amd64" || runtime.GOOS != "windows" {
755+
t.Skipf("this is a windows/amd64-only test")
756+
}
757+
758+
tmpdir, err := ioutil.TempDir("", "TestPErsrc")
759+
if err != nil {
760+
t.Fatal(err)
761+
}
762+
defer os.RemoveAll(tmpdir)
763+
764+
pkgdir := filepath.Join("testdata", "testPErsrc")
765+
exe := filepath.Join(tmpdir, "a.exe")
766+
cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe)
767+
cmd.Dir = pkgdir
768+
// cmd.Env = append(os.Environ(), "GOOS=windows", "GOARCH=amd64") // uncomment if debugging in a cross-compiling environment
769+
out, err := cmd.CombinedOutput()
770+
if err != nil {
771+
t.Fatalf("building failed: %v, output:\n%s", err, out)
772+
}
773+
774+
// Check that the binary contains the rsrc data
775+
b, err := ioutil.ReadFile(exe)
776+
if err != nil {
777+
t.Fatalf("reading output failed: %v", err)
778+
}
779+
if !bytes.Contains(b, []byte("Hello Gophers!")) {
780+
t.Fatalf("binary does not contain expected content")
781+
}
782+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Test that a PE rsrc section is handled correctly (issue 39658).
6+
//
7+
// rsrc.syso is created with:
8+
// windres -i a.rc -o rsrc.syso -O coff
9+
// on windows-amd64-2016 builder, where a.rc is a text file with
10+
// the following content:
11+
//
12+
// resname RCDATA {
13+
// "Hello Gophers!\0",
14+
// "This is a test.\0",
15+
// }
16+
17+
package main
18+
19+
func main() {}
228 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)