diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -690,10 +690,6 @@ make(symtabSection, indirectSymtabSection)); if (!config->umbrella.empty()) in.header->addLoadCommand(make(config->umbrella)); - if (functionStartsSection) - in.header->addLoadCommand(make(functionStartsSection)); - if (dataInCodeSection) - in.header->addLoadCommand(make(dataInCodeSection)); if (config->emitEncryptionInfo) in.header->addLoadCommand(make>()); for (StringRef path : config->runtimePaths) @@ -702,7 +698,6 @@ switch (config->outputType) { case MH_EXECUTE: in.header->addLoadCommand(make()); - in.header->addLoadCommand(make()); break; case MH_DYLIB: in.header->addLoadCommand(make(LC_ID_DYLIB, config->installName, @@ -723,6 +718,10 @@ else in.header->addLoadCommand(make(config->platformInfo)); + // This is down here to match ld64's load command order. + if (config->outputType == MH_EXECUTE) + in.header->addLoadCommand(make()); + int64_t dylibOrdinal = 1; DenseMap ordinalForInstallName; for (InputFile *file : inputFiles) { @@ -789,6 +788,10 @@ } } + if (functionStartsSection) + in.header->addLoadCommand(make(functionStartsSection)); + if (dataInCodeSection) + in.header->addLoadCommand(make(dataInCodeSection)); if (codeSignatureSection) in.header->addLoadCommand(make(codeSignatureSection)); diff --git a/lld/test/MachO/load-command-sequence.s b/lld/test/MachO/load-command-sequence.s --- a/lld/test/MachO/load-command-sequence.s +++ b/lld/test/MachO/load-command-sequence.s @@ -43,11 +43,13 @@ # COMMON: cmd LC_DYSYMTAB # EXEC: cmd LC_LOAD_DYLINKER -# EXEC: cmd LC_MAIN # DYLIB: cmd LC_ID_DYLIB # COMMON: cmd LC_UUID # COMMON: cmd LC_BUILD_VERSION + +# EXEC: cmd LC_MAIN + # COMMON: cmd LC_LOAD_DYLIB .section __TEXT,__cstring