]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: align .reloc block starts by 32 bits for PE target
authorThan McIntosh <thanm@google.com>
Mon, 1 Jul 2024 16:11:33 +0000 (16:11 +0000)
committerThan McIntosh <thanm@google.com>
Mon, 1 Jul 2024 19:15:10 +0000 (19:15 +0000)
Tweak the code that emits the PE ".reloc" section on Windows to ensure
that each relocation block is 32-bit aligned, which is required by the
PE standard.

Fixes #68260.

Change-Id: I39b75a7491b00fa97871aebb90d3be0ec09f9c40
Reviewed-on: https://go-review.googlesource.com/c/go/+/595896
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

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

index 8cfecafe84ab6c576a3cdc9470f0fabfcdbdf5b7..14f04855305a401e6c6e2f1a88533d7d57de961c 100644 (file)
@@ -1548,8 +1548,21 @@ func (rt *peBaseRelocTable) write(ctxt *Link) {
        // sort the pages array
        sort.Sort(rt.pages)
 
+       // .reloc section must be 32-bit aligned
+       if out.Offset()&3 != 0 {
+               Errorf(nil, "internal error, start of .reloc not 32-bit aligned")
+       }
+
        for _, p := range rt.pages {
                b := rt.blocks[p]
+
+               // Add a dummy entry at the end of the list if we have an
+               // odd number of entries, so as to ensure that the next
+               // block starts on a 32-bit boundary (see issue 68260).
+               if len(b.entries)&1 != 0 {
+                       b.entries = append(b.entries, peBaseRelocEntry{})
+               }
+
                const sizeOfPEbaseRelocBlock = 8 // 2 * sizeof(uint32)
                blockSize := uint32(sizeOfPEbaseRelocBlock + len(b.entries)*2)
                out.Write32(p)