From b28f2f73991c849657e5898dfa2f8cae54e885a0 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 29 Mar 2017 20:53:32 -0400 Subject: [PATCH] cmd/link: make mach-o dwarf segment properly aligned Without this, the load fails during kernel exec, which results in the mysterious and completely uninformative "Killed: 9" error. It appears that the stars (or at least the inputs) were properly aligned with earlier versions of Xcode so that this happened accidentally. Make it happen on purpose. Gregory Man bisected the breakage to this change in LLVM, which fits the theory nicely: https://github.com/llvm-mirror/llvm/commit/9a41e59c Fixes #19734. Change-Id: Ice67a09af2de29d3c0d5e3fcde6a769580897c95 Reviewed-on: https://go-review.googlesource.com/38854 Run-TryBot: Russ Cox TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- misc/cgo/testcshared/test.bash | 7 +++ .../link/internal/ld/macho_combine_dwarf.go | 52 ++++++++++++------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/misc/cgo/testcshared/test.bash b/misc/cgo/testcshared/test.bash index 4ff4db446c..315a0d4036 100755 --- a/misc/cgo/testcshared/test.bash +++ b/misc/cgo/testcshared/test.bash @@ -179,6 +179,13 @@ if test "$output" != "PASS"; then status=1 fi +if test "$libext" = "dylib"; then + # make sure dylibs are well-formed + if ! otool -l libgo*.dylib >/dev/null; then + status=1 + fi +fi + if test $status = 0; then echo "ok" fi diff --git a/src/cmd/link/internal/ld/macho_combine_dwarf.go b/src/cmd/link/internal/ld/macho_combine_dwarf.go index dcc371ec05..8c6c4a86ac 100644 --- a/src/cmd/link/internal/ld/macho_combine_dwarf.go +++ b/src/cmd/link/internal/ld/macho_combine_dwarf.go @@ -17,6 +17,7 @@ import ( var realdwarf, linkseg *macho.Segment var dwarfstart, linkstart int64 +var dwarfaddr, linkaddr int64 var linkoffset uint32 const ( @@ -41,8 +42,7 @@ const ( LC_DYLIB_CODE_SIGN_DRS = 0x2B LC_ENCRYPTION_INFO_64 = 0x2C - dwarfMinAlign = 6 // 64 = 1 << 6 - pageAlign = 12 // 4096 = 1 << 12 + pageAlign = 12 // 4096 = 1 << 12 ) type loadCmd struct { @@ -157,16 +157,13 @@ func machoCombineDwarf(inexe, dsym, outexe string) error { } // Now copy the dwarf data into the output. - maxalign := uint32(dwarfMinAlign) // - for _, sect := range dwarfm.Sections { - if sect.Align > maxalign { - maxalign = sect.Align - } - } - dwarfstart = machoCalcStart(realdwarf.Offset, linkseg.Offset, maxalign) + // Kernel requires all loaded segments to be page-aligned in the file, + // even though we mark this one as being 0 bytes of virtual address space. + dwarfstart = machoCalcStart(realdwarf.Offset, linkseg.Offset, pageAlign) if _, err = outf.Seek(dwarfstart, 0); err != nil { return err } + dwarfaddr = int64((linkseg.Addr + linkseg.Memsz + 1<