]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: set correct flags in .dynamic for PIE buildmode
authorThan McIntosh <thanm@google.com>
Tue, 15 Jun 2021 12:01:54 +0000 (08:01 -0400)
committerThan McIntosh <thanm@google.com>
Tue, 15 Jun 2021 18:09:44 +0000 (18:09 +0000)
For internal linking, when generating a PIE binary, set the proper
.dynamic section flags to mark the binary as position-independent.

Fixes #46747.

Change-Id: I2b899148c6d06f92c9d12257a9761278b4236dfc
Reviewed-on: https://go-review.googlesource.com/c/go/+/328089
Run-TryBot: Than McIntosh <thanm@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Trust: Than McIntosh <thanm@google.com>

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

index 6f81e74da29ec8cf6f6158b59e59da12f6a8a870..81011638bc5fae6b52cf2898d2849dced105c74d 100644 (file)
@@ -950,6 +950,11 @@ func elfdynhash(ctxt *Link) {
        }
 
        s = ldr.CreateSymForUpdate(".dynamic", 0)
+       if ctxt.BuildMode == BuildModePIE {
+               // https://github.com/bminor/glibc/blob/895ef79e04a953cac1493863bcae29ad85657ee1/elf/elf.h#L986
+               const DTFLAGS_1_PIE = 0x08000000
+               Elfwritedynent(ctxt.Arch, s, elf.DT_FLAGS_1, uint64(DTFLAGS_1_PIE))
+       }
        elfverneed = nfile
        if elfverneed != 0 {
                elfWriteDynEntSym(ctxt, s, elf.DT_VERNEED, gnuVersionR.Sym())