From 91cfbf39e45b130562bbc5b353aa041cfe315faa Mon Sep 17 00:00:00 2001 From: "Jason A. Donenfeld" Date: Sun, 14 Feb 2021 19:01:39 +0100 Subject: [PATCH] cmd/link: set .ctors COFF section to writable and aligned Without setting these flags, LLVM's LLD ignores the .ctors section when merging objects. Updates #44250. Updates #39326. Updates #38755. Updates #36439. Updates #43800. Change-Id: I8766104508f7acd832088a590ee7d68afa0d6065 Reviewed-on: https://go-review.googlesource.com/c/go/+/291633 Trust: Jason A. Donenfeld Run-TryBot: Jason A. Donenfeld TryBot-Result: Go Bot Reviewed-by: Than McIntosh --- src/cmd/link/internal/ld/pe.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cmd/link/internal/ld/pe.go b/src/cmd/link/internal/ld/pe.go index 46e3df5df18..a0aba866dcb 100644 --- a/src/cmd/link/internal/ld/pe.go +++ b/src/cmd/link/internal/ld/pe.go @@ -66,6 +66,8 @@ const ( IMAGE_SCN_MEM_WRITE = 0x80000000 IMAGE_SCN_MEM_DISCARDABLE = 0x2000000 IMAGE_SCN_LNK_NRELOC_OVFL = 0x1000000 + IMAGE_SCN_ALIGN_4BYTES = 0x300000 + IMAGE_SCN_ALIGN_8BYTES = 0x400000 IMAGE_SCN_ALIGN_32BYTES = 0x600000 ) @@ -478,20 +480,19 @@ func (f *peFile) addInitArray(ctxt *Link) *peSection { // However, the entire Go runtime is initialized from just one function, so it is unlikely // that this will need to grow in the future. var size int + var alignment uint32 switch objabi.GOARCH { default: Exitf("peFile.addInitArray: unsupported GOARCH=%q\n", objabi.GOARCH) - case "386": + case "386", "arm": size = 4 - case "amd64": - size = 8 - case "arm": - size = 4 - case "arm64": + alignment = IMAGE_SCN_ALIGN_4BYTES + case "amd64", "arm64": size = 8 + alignment = IMAGE_SCN_ALIGN_8BYTES } sect := f.addSection(".ctors", size, size) - sect.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ + sect.characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE | alignment sect.sizeOfRawData = uint32(size) ctxt.Out.SeekSet(int64(sect.pointerToRawData)) sect.checkOffset(ctxt.Out.Offset())