proc: fix PIE support on macOS (#3467)

Go1.22 switched to emitting PIE by default and also changed some
details of the PIE implementation. This breaks delve entirely on macOS.

Fix how relocations are handled on macOS.
This commit is contained in:
Alessandro Arzilli
2023-08-15 00:31:05 +02:00
committed by GitHub
parent 894ba63400
commit ec07c27fc7
2 changed files with 12 additions and 6 deletions

View File

@ -1805,11 +1805,17 @@ func loadBinaryInfoMacho(bi *BinaryInfo, image *Image, path string, entryPoint u
}
if entryPoint != 0 {
// This is a little bit hacky. We use the entryPoint variable, but it
// actually holds the address of the mach-o header. We can use this
// to calculate the offset to the non-aslr location of the mach-o header
// (which is 0x100000000)
image.StaticBase = entryPoint - 0x100000000
machoOff := uint64(0x100000000)
for _, ld := range exe.Loads {
if seg, _ := ld.(*macho.Segment); seg != nil {
if seg.Name == "__TEXT" {
machoOff = seg.Addr
break
}
}
}
logflags.DebuggerLogger().Debugf("entryPoint %#x machoOff %#x", entryPoint, machoOff)
image.StaticBase = entryPoint - machoOff
}
image.closer = exe

View File

@ -662,7 +662,7 @@ func LLDBAttach(pid int, path string, waitFor *proc.WaitFor, debugInfoDirs []str
// debugging PIEs.
func (p *gdbProcess) EntryPoint() (uint64, error) {
var entryPoint uint64
if p.bi.GOOS == "darwin" && p.bi.Arch.Name == "arm64" {
if p.bi.GOOS == "darwin" {
// There is no auxv on darwin, however, we can get the location of the mach-o
// header from the debugserver by going through the loaded libraries, which includes
// the exe itself