From f210576887b3785ced6f09a8aba949f29c121685 Mon Sep 17 00:00:00 2001 From: Filip Navara Date: Fri, 22 Sep 2023 19:25:49 +0200 Subject: [PATCH] [ObjWriter] Emit the MH_SUBSECTIONS_VIA_SYMBOLS header flag for Mach-O (#471) The flag tells the linker that there are no functions with overlapping code and thus the code sections can be divided into subsections based on the symbols. This is similar in effect to how `-ffunction-sections` behaves on ELF. Notably, this enables optimization such as dead code stripping and identical code folding, if requested through linker switches. Old ld64 linker tends to run into deep stack loop without this switch if `-dead_strip` is used (encountered in Xamarin, not used by regular NativeAOT), and often ends up stack overflowing. New Xcode 15+ linker completely fails to produce correct unwind tables without this flag, which results in the NativeAOT executables crashing during GC or on first stack walk. --- llvm/tools/objwriter/objwriter.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/llvm/tools/objwriter/objwriter.cpp b/llvm/tools/objwriter/objwriter.cpp index cde58f14aa90bd..903d4ee813bb01 100644 --- a/llvm/tools/objwriter/objwriter.cpp +++ b/llvm/tools/objwriter/objwriter.cpp @@ -155,6 +155,10 @@ bool ObjectWriter::Init(llvm::StringRef ObjectFilePath, const char* tripleName) CFIsPerOffset.truncate(0); + if (OutContext->getObjectFileType() == MCContext::IsMachO) { + Streamer->emitAssemblerFlag(MCAF_SubsectionsViaSymbols); + } + return true; }