Skip to content

Commit 4bc5f6f

Browse files
committed
cmd/link: put DYLD_INFO at beginning of LINKEDIT segment on darwin
Apparently, code signing requires DYLD_INFO tables are at the beginning of the LINKEDIT segment. Put it there. May fix #42507. Change-Id: I1836e0f495719cf75f66d0831fe1544bbe3ff1a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/269377 Trust: Cherry Zhang <[email protected]> Run-TryBot: Cherry Zhang <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Than McIntosh <[email protected]>
1 parent d7974c3 commit 4bc5f6f

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

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

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -761,12 +761,12 @@ func asmbMacho(ctxt *Link) {
761761
ldr := ctxt.loader
762762

763763
// must match domacholink below
764-
s1 := ldr.SymSize(ldr.Lookup(".machosymtab", 0))
765-
s2 := ldr.SymSize(ctxt.ArchSyms.LinkEditPLT)
766-
s3 := ldr.SymSize(ctxt.ArchSyms.LinkEditGOT)
767-
s4 := ldr.SymSize(ldr.Lookup(".machosymstr", 0))
768-
s5 := ldr.SymSize(ldr.Lookup(".machorebase", 0))
769-
s6 := ldr.SymSize(ldr.Lookup(".machobind", 0))
764+
s1 := ldr.SymSize(ldr.Lookup(".machorebase", 0))
765+
s2 := ldr.SymSize(ldr.Lookup(".machobind", 0))
766+
s3 := ldr.SymSize(ldr.Lookup(".machosymtab", 0))
767+
s4 := ldr.SymSize(ctxt.ArchSyms.LinkEditPLT)
768+
s5 := ldr.SymSize(ctxt.ArchSyms.LinkEditGOT)
769+
s6 := ldr.SymSize(ldr.Lookup(".machosymstr", 0))
770770

771771
if ctxt.LinkMode != LinkExternal {
772772
ms := newMachoSeg("__LINKEDIT", 0)
@@ -778,13 +778,27 @@ func asmbMacho(ctxt *Link) {
778778
ms.prot2 = 1
779779
}
780780

781+
if ctxt.LinkMode != LinkExternal && ctxt.IsPIE() {
782+
ml := newMachoLoad(ctxt.Arch, LC_DYLD_INFO_ONLY, 10)
783+
ml.data[0] = uint32(linkoff) // rebase off
784+
ml.data[1] = uint32(s1) // rebase size
785+
ml.data[2] = uint32(linkoff + s1) // bind off
786+
ml.data[3] = uint32(s2) // bind size
787+
ml.data[4] = 0 // weak bind off
788+
ml.data[5] = 0 // weak bind size
789+
ml.data[6] = 0 // lazy bind off
790+
ml.data[7] = 0 // lazy bind size
791+
ml.data[8] = 0 // export
792+
ml.data[9] = 0 // export size
793+
}
794+
781795
ml := newMachoLoad(ctxt.Arch, LC_SYMTAB, 4)
782-
ml.data[0] = uint32(linkoff) /* symoff */
783-
ml.data[1] = uint32(nsortsym) /* nsyms */
784-
ml.data[2] = uint32(linkoff + s1 + s2 + s3) /* stroff */
785-
ml.data[3] = uint32(s4) /* strsize */
796+
ml.data[0] = uint32(linkoff + s1 + s2) /* symoff */
797+
ml.data[1] = uint32(nsortsym) /* nsyms */
798+
ml.data[2] = uint32(linkoff + s1 + s2 + s3 + s4 + s5) /* stroff */
799+
ml.data[3] = uint32(s6) /* strsize */
786800

787-
machodysymtab(ctxt)
801+
machodysymtab(ctxt, linkoff+s1+s2)
788802

789803
if ctxt.LinkMode != LinkExternal {
790804
ml := newMachoLoad(ctxt.Arch, LC_LOAD_DYLINKER, 6)
@@ -800,20 +814,6 @@ func asmbMacho(ctxt *Link) {
800814
stringtouint32(ml.data[4:], lib)
801815
}
802816
}
803-
804-
if ctxt.LinkMode != LinkExternal && ctxt.IsPIE() {
805-
ml := newMachoLoad(ctxt.Arch, LC_DYLD_INFO_ONLY, 10)
806-
ml.data[0] = uint32(linkoff + s1 + s2 + s3 + s4) // rebase off
807-
ml.data[1] = uint32(s5) // rebase size
808-
ml.data[2] = uint32(linkoff + s1 + s2 + s3 + s4 + s5) // bind off
809-
ml.data[3] = uint32(s6) // bind size
810-
ml.data[4] = 0 // weak bind off
811-
ml.data[5] = 0 // weak bind size
812-
ml.data[6] = 0 // lazy bind off
813-
ml.data[7] = 0 // lazy bind size
814-
ml.data[8] = 0 // export
815-
ml.data[9] = 0 // export size
816-
}
817817
}
818818

819819
a := machowrite(ctxt, ctxt.Arch, ctxt.Out, ctxt.LinkMode)
@@ -1018,7 +1018,7 @@ func machosymtab(ctxt *Link) {
10181018
}
10191019
}
10201020

1021-
func machodysymtab(ctxt *Link) {
1021+
func machodysymtab(ctxt *Link, base int64) {
10221022
ml := newMachoLoad(ctxt.Arch, LC_DYSYMTAB, 18)
10231023

10241024
n := 0
@@ -1046,7 +1046,7 @@ func machodysymtab(ctxt *Link) {
10461046
s1 := ldr.SymSize(ldr.Lookup(".machosymtab", 0))
10471047
s2 := ldr.SymSize(ctxt.ArchSyms.LinkEditPLT)
10481048
s3 := ldr.SymSize(ctxt.ArchSyms.LinkEditGOT)
1049-
ml.data[12] = uint32(linkoff + s1) /* indirectsymoff */
1049+
ml.data[12] = uint32(base + s1) /* indirectsymoff */
10501050
ml.data[13] = uint32((s2 + s3) / 4) /* nindirectsyms */
10511051

10521052
ml.data[14] = 0 /* extreloff */
@@ -1063,12 +1063,12 @@ func doMachoLink(ctxt *Link) int64 {
10631063
ldr := ctxt.loader
10641064

10651065
// write data that will be linkedit section
1066-
s1 := ldr.Lookup(".machosymtab", 0)
1067-
s2 := ctxt.ArchSyms.LinkEditPLT
1068-
s3 := ctxt.ArchSyms.LinkEditGOT
1069-
s4 := ldr.Lookup(".machosymstr", 0)
1070-
s5 := ldr.Lookup(".machorebase", 0)
1071-
s6 := ldr.Lookup(".machobind", 0)
1066+
s1 := ldr.Lookup(".machorebase", 0)
1067+
s2 := ldr.Lookup(".machobind", 0)
1068+
s3 := ldr.Lookup(".machosymtab", 0)
1069+
s4 := ctxt.ArchSyms.LinkEditPLT
1070+
s5 := ctxt.ArchSyms.LinkEditGOT
1071+
s6 := ldr.Lookup(".machosymstr", 0)
10721072

10731073
// Force the linkedit section to end on a 16-byte
10741074
// boundary. This allows pure (non-cgo) Go binaries
@@ -1087,9 +1087,9 @@ func doMachoLink(ctxt *Link) int64 {
10871087
// boundary, codesign_allocate will not need to apply
10881088
// any alignment padding itself, working around the
10891089
// issue.
1090-
s4b := ldr.MakeSymbolUpdater(s4)
1091-
for s4b.Size()%16 != 0 {
1092-
s4b.AddUint8(0)
1090+
s6b := ldr.MakeSymbolUpdater(s6)
1091+
for s6b.Size()%16 != 0 {
1092+
s6b.AddUint8(0)
10931093
}
10941094

10951095
size := int(ldr.SymSize(s1) + ldr.SymSize(s2) + ldr.SymSize(s3) + ldr.SymSize(s4) + ldr.SymSize(s5) + ldr.SymSize(s6))

0 commit comments

Comments
 (0)