Skip to content

Commit 1e6ad65

Browse files
committed
cmd/link: enable DWARF combining on macOS ARM64
It appears the machoCalcStart function is meant to align the segment, but it doesn't. Replace it with an actual alignment calculation. Also, use the alignment from the configuration, instead of hardcode. With this fix we could enable DWARF combining on macOS ARM64. Change-Id: I19ec771b77d752b83a54c53b6ee65af78a31b8ae Reviewed-on: https://go-review.googlesource.com/c/go/+/253558 Reviewed-by: Than McIntosh <[email protected]>
1 parent a52a5d8 commit 1e6ad65

File tree

3 files changed

+5
-18
lines changed

3 files changed

+5
-18
lines changed

src/cmd/link/internal/arm64/obj.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func archinit(ctxt *ld.Link) {
105105
*ld.FlagTextAddr = 4096 + int64(ld.HEADR)
106106
}
107107
if *ld.FlagRound == -1 {
108-
*ld.FlagRound = 4096
108+
*ld.FlagRound = 16384 // 16K page alignment
109109
}
110110
}
111111
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,11 +1240,11 @@ func (ctxt *Link) hostlink() {
12401240

12411241
// On darwin, whether to combine DWARF into executable.
12421242
// Only macOS supports unmapped segments such as our __DWARF segment.
1243-
combineDwarf := ctxt.IsDarwin() && !*FlagS && !*FlagW && !debug_s && machoPlatform == PLATFORM_MACOS && ctxt.IsAMD64()
1243+
combineDwarf := ctxt.IsDarwin() && !*FlagS && !*FlagW && !debug_s && machoPlatform == PLATFORM_MACOS
12441244

12451245
switch ctxt.HeadType {
12461246
case objabi.Hdarwin:
1247-
if machoPlatform == PLATFORM_MACOS && ctxt.IsAMD64() {
1247+
if combineDwarf {
12481248
// Leave room for DWARF combining.
12491249
// -headerpad is incompatible with -fembed-bitcode.
12501250
argv = append(argv, "-Wl,-headerpad,1144")

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@ import (
1616
"unsafe"
1717
)
1818

19-
const (
20-
pageAlign = 12 // 4096 = 1 << 12
21-
)
22-
2319
type loadCmd struct {
2420
Cmd macho.LoadCmd
2521
Len uint32
@@ -138,7 +134,7 @@ func machoCombineDwarf(ctxt *Link, exef *os.File, exem *macho.File, dsym, outexe
138134
// Now copy the dwarf data into the output.
139135
// Kernel requires all loaded segments to be page-aligned in the file,
140136
// even though we mark this one as being 0 bytes of virtual address space.
141-
dwarfstart := machoCalcStart(realdwarf.Offset, linkseg.Offset, pageAlign)
137+
dwarfstart := Rnd(int64(linkseg.Offset), int64(*FlagRound))
142138
if _, err := outf.Seek(dwarfstart, 0); err != nil {
143139
return err
144140
}
@@ -166,7 +162,7 @@ func machoCombineDwarf(ctxt *Link, exef *os.File, exem *macho.File, dsym, outexe
166162
if _, err := exef.Seek(int64(linkseg.Offset), 0); err != nil {
167163
return err
168164
}
169-
linkstart := machoCalcStart(linkseg.Offset, uint64(dwarfstart)+dwarfsize, pageAlign)
165+
linkstart := Rnd(dwarfstart+int64(dwarfsize), int64(*FlagRound))
170166
if _, err := outf.Seek(linkstart, 0); err != nil {
171167
return err
172168
}
@@ -432,12 +428,3 @@ func machoUpdateLoadCommand(r loadCmdReader, linkseg *macho.Segment, linkoffset
432428
}
433429
return nil
434430
}
435-
436-
func machoCalcStart(origAddr, newAddr uint64, alignExp uint32) int64 {
437-
align := uint64(1 << alignExp)
438-
origMod, newMod := origAddr%align, newAddr%align
439-
if origMod == newMod {
440-
return int64(newAddr)
441-
}
442-
return int64(newAddr + align + origMod - newMod)
443-
}

0 commit comments

Comments
 (0)