Commit 3ca0d34f authored by Russ Cox's avatar Russ Cox Committed by Austin Clements

[release-branch.go1.8] 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/39603
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: 's avatarRuss Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 84192f27
...@@ -179,6 +179,13 @@ if test "$output" != "PASS"; then ...@@ -179,6 +179,13 @@ if test "$output" != "PASS"; then
status=1 status=1
fi 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 if test $status = 0; then
echo "ok" echo "ok"
fi fi
......
...@@ -17,6 +17,7 @@ import ( ...@@ -17,6 +17,7 @@ import (
var realdwarf, linkseg *macho.Segment var realdwarf, linkseg *macho.Segment
var dwarfstart, linkstart int64 var dwarfstart, linkstart int64
var dwarfaddr, linkaddr int64
var linkoffset uint32 var linkoffset uint32
const ( const (
...@@ -41,7 +42,6 @@ const ( ...@@ -41,7 +42,6 @@ const (
LC_DYLIB_CODE_SIGN_DRS = 0x2B LC_DYLIB_CODE_SIGN_DRS = 0x2B
LC_ENCRYPTION_INFO_64 = 0x2C LC_ENCRYPTION_INFO_64 = 0x2C
dwarfMinAlign = 6 // 64 = 1 << 6
pageAlign = 12 // 4096 = 1 << 12 pageAlign = 12 // 4096 = 1 << 12
) )
...@@ -157,16 +157,13 @@ func machoCombineDwarf(inexe, dsym, outexe string) error { ...@@ -157,16 +157,13 @@ func machoCombineDwarf(inexe, dsym, outexe string) error {
} }
// Now copy the dwarf data into the output. // Now copy the dwarf data into the output.
maxalign := uint32(dwarfMinAlign) // // Kernel requires all loaded segments to be page-aligned in the file,
for _, sect := range dwarfm.Sections { // even though we mark this one as being 0 bytes of virtual address space.
if sect.Align > maxalign { dwarfstart = machoCalcStart(realdwarf.Offset, linkseg.Offset, pageAlign)
maxalign = sect.Align
}
}
dwarfstart = machoCalcStart(realdwarf.Offset, linkseg.Offset, maxalign)
if _, err = outf.Seek(dwarfstart, 0); err != nil { if _, err = outf.Seek(dwarfstart, 0); err != nil {
return err return err
} }
dwarfaddr = int64((linkseg.Addr + linkseg.Memsz + 1<<pageAlign - 1) &^ (1<<pageAlign - 1))
if _, err = dwarff.Seek(int64(realdwarf.Offset), 0); err != nil { if _, err = dwarff.Seek(int64(realdwarf.Offset), 0); err != nil {
return err return err
...@@ -277,10 +274,10 @@ func machoUpdateSegment(r loadCmdReader, seg, sect interface{}) error { ...@@ -277,10 +274,10 @@ func machoUpdateSegment(r loadCmdReader, seg, sect interface{}) error {
return err return err
} }
// There shouldn't be any sections, but just to make sure... // There shouldn't be any sections, but just to make sure...
return machoUpdateSections(r, segValue, reflect.ValueOf(sect), uint64(linkoffset)) return machoUpdateSections(r, segValue, reflect.ValueOf(sect), uint64(linkoffset), 0)
} }
func machoUpdateSections(r loadCmdReader, seg, sect reflect.Value, delta uint64) error { func machoUpdateSections(r loadCmdReader, seg, sect reflect.Value, deltaOffset, deltaAddr uint64) error {
iseg := reflect.Indirect(seg) iseg := reflect.Indirect(seg)
nsect := iseg.FieldByName("Nsect").Uint() nsect := iseg.FieldByName("Nsect").Uint()
if nsect == 0 { if nsect == 0 {
...@@ -291,16 +288,20 @@ func machoUpdateSections(r loadCmdReader, seg, sect reflect.Value, delta uint64) ...@@ -291,16 +288,20 @@ func machoUpdateSections(r loadCmdReader, seg, sect reflect.Value, delta uint64)
isect := reflect.Indirect(sect) isect := reflect.Indirect(sect)
offsetField := isect.FieldByName("Offset") offsetField := isect.FieldByName("Offset")
reloffField := isect.FieldByName("Reloff") reloffField := isect.FieldByName("Reloff")
addrField := isect.FieldByName("Addr")
sectSize := int64(isect.Type().Size()) sectSize := int64(isect.Type().Size())
for i := uint64(0); i < nsect; i++ { for i := uint64(0); i < nsect; i++ {
if err := r.ReadAt(sectOffset, sect.Interface()); err != nil { if err := r.ReadAt(sectOffset, sect.Interface()); err != nil {
return err return err
} }
if offsetField.Uint() != 0 { if offsetField.Uint() != 0 {
offsetField.SetUint(offsetField.Uint() + delta) offsetField.SetUint(offsetField.Uint() + deltaOffset)
} }
if reloffField.Uint() != 0 { if reloffField.Uint() != 0 {
reloffField.SetUint(reloffField.Uint() + delta) reloffField.SetUint(reloffField.Uint() + deltaOffset)
}
if addrField.Uint() != 0 {
addrField.SetUint(addrField.Uint() + deltaAddr)
} }
if err := r.WriteAt(sectOffset, sect.Interface()); err != nil { if err := r.WriteAt(sectOffset, sect.Interface()); err != nil {
return err return err
...@@ -327,15 +328,30 @@ func machoUpdateDwarfHeader(r *loadCmdReader) error { ...@@ -327,15 +328,30 @@ func machoUpdateDwarfHeader(r *loadCmdReader) error {
if err := r.ReadAt(0, seg); err != nil { if err := r.ReadAt(0, seg); err != nil {
return err return err
} }
segValue := reflect.ValueOf(seg) segv := reflect.ValueOf(seg).Elem()
offset := reflect.Indirect(segValue).FieldByName("Offset")
segv.FieldByName("Offset").SetUint(uint64(dwarfstart))
segv.FieldByName("Addr").SetUint(uint64(dwarfaddr))
deltaOffset := uint64(dwarfstart) - realdwarf.Offset
deltaAddr := uint64(dwarfaddr) - realdwarf.Addr
// If we set Memsz to 0 (and might as well set Addr too),
// then the xnu kernel will bail out halfway through load_segment
// and not apply further sanity checks that we might fail in the future.
// We don't need the DWARF information actually available in memory.
// But if we do this for buildmode=c-shared then the user-space
// dynamic loader complains about memsz < filesz. Sigh.
if Buildmode != BuildmodeCShared {
segv.FieldByName("Addr").SetUint(0)
segv.FieldByName("Memsz").SetUint(0)
deltaAddr = 0
}
delta := uint64(dwarfstart) - realdwarf.Offset
offset.SetUint(offset.Uint() + delta)
if err := r.WriteAt(0, seg); err != nil { if err := r.WriteAt(0, seg); err != nil {
return err return err
} }
return machoUpdateSections(*r, segValue, reflect.ValueOf(sect), delta) return machoUpdateSections(*r, segv, reflect.ValueOf(sect), deltaOffset, deltaAddr)
} }
func machoUpdateLoadCommand(r loadCmdReader, cmd interface{}, fields ...string) error { func machoUpdateLoadCommand(r loadCmdReader, cmd interface{}, fields ...string) error {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment