xref: /llvm-project/bolt/lib/Rewrite/MachORewriteInstance.cpp (revision 473b9dd442e452fbcfac352c0ae0a586f019a8f2)
1 //===- bolt/Rewrite/MachORewriteInstance.cpp - MachO rewriter -------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "bolt/Rewrite/MachORewriteInstance.h"
10 #include "bolt/Core/BinaryContext.h"
11 #include "bolt/Core/BinaryEmitter.h"
12 #include "bolt/Core/BinaryFunction.h"
13 #include "bolt/Core/JumpTable.h"
14 #include "bolt/Core/MCPlusBuilder.h"
15 #include "bolt/Passes/Instrumentation.h"
16 #include "bolt/Passes/PatchEntries.h"
17 #include "bolt/Profile/DataReader.h"
18 #include "bolt/Rewrite/BinaryPassManager.h"
19 #include "bolt/Rewrite/ExecutableFileMemoryManager.h"
20 #include "bolt/Rewrite/JITLinkLinker.h"
21 #include "bolt/RuntimeLibs/InstrumentationRuntimeLibrary.h"
22 #include "bolt/Utils/Utils.h"
23 #include "llvm/MC/MCObjectStreamer.h"
24 #include "llvm/Support/Errc.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/ToolOutputFile.h"
27 #include <memory>
28 #include <optional>
29 
30 namespace opts {
31 
32 using namespace llvm;
33 extern cl::opt<unsigned> AlignText;
34 //FIXME! Upstream change
35 //extern cl::opt<bool> CheckOverlappingElements;
36 extern cl::opt<bool> ForcePatch;
37 extern cl::opt<bool> Instrument;
38 extern cl::opt<bool> InstrumentCalls;
39 extern cl::opt<bolt::JumpTableSupportLevel> JumpTables;
40 extern cl::opt<bool> KeepTmp;
41 extern cl::opt<bool> NeverPrint;
42 extern cl::opt<std::string> OutputFilename;
43 extern cl::opt<bool> PrintAfterBranchFixup;
44 extern cl::opt<bool> PrintFinalized;
45 extern cl::opt<bool> PrintNormalized;
46 extern cl::opt<bool> PrintReordered;
47 extern cl::opt<bool> PrintSections;
48 extern cl::opt<bool> PrintDisasm;
49 extern cl::opt<bool> PrintCFG;
50 extern cl::opt<std::string> RuntimeInstrumentationLib;
51 extern cl::opt<unsigned> Verbosity;
52 } // namespace opts
53 
54 namespace llvm {
55 namespace bolt {
56 
57 extern MCPlusBuilder *createX86MCPlusBuilder(const MCInstrAnalysis *,
58                                              const MCInstrInfo *,
59                                              const MCRegisterInfo *);
60 extern MCPlusBuilder *createAArch64MCPlusBuilder(const MCInstrAnalysis *,
61                                                  const MCInstrInfo *,
62                                                  const MCRegisterInfo *);
63 
64 namespace {
65 
66 MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
67                                    const MCInstrAnalysis *Analysis,
68                                    const MCInstrInfo *Info,
69                                    const MCRegisterInfo *RegInfo) {
70 #ifdef X86_AVAILABLE
71   if (Arch == Triple::x86_64)
72     return createX86MCPlusBuilder(Analysis, Info, RegInfo);
73 #endif
74 
75 #ifdef AARCH64_AVAILABLE
76   if (Arch == Triple::aarch64)
77     return createAArch64MCPlusBuilder(Analysis, Info, RegInfo);
78 #endif
79 
80   llvm_unreachable("architecture unsupported by MCPlusBuilder");
81 }
82 
83 } // anonymous namespace
84 
85 #define DEBUG_TYPE "bolt"
86 
87 Expected<std::unique_ptr<MachORewriteInstance>>
88 MachORewriteInstance::create(object::MachOObjectFile *InputFile,
89                              StringRef ToolPath) {
90   Error Err = Error::success();
91   auto MachORI =
92       std::make_unique<MachORewriteInstance>(InputFile, ToolPath, Err);
93   if (Err)
94     return std::move(Err);
95   return std::move(MachORI);
96 }
97 
98 MachORewriteInstance::MachORewriteInstance(object::MachOObjectFile *InputFile,
99                                            StringRef ToolPath, Error &Err)
100     : InputFile(InputFile), ToolPath(ToolPath) {
101   ErrorAsOutParameter EAO(&Err);
102   auto BCOrErr = BinaryContext::createBinaryContext(
103       InputFile, /* IsPIC */ true, DWARFContext::create(*InputFile));
104   if (Error E = BCOrErr.takeError()) {
105     Err = std::move(E);
106     return;
107   }
108   BC = std::move(BCOrErr.get());
109   BC->initializeTarget(std::unique_ptr<MCPlusBuilder>(createMCPlusBuilder(
110       BC->TheTriple->getArch(), BC->MIA.get(), BC->MII.get(), BC->MRI.get())));
111   if (opts::Instrument)
112     BC->setRuntimeLibrary(std::make_unique<InstrumentationRuntimeLibrary>());
113 }
114 
115 Error MachORewriteInstance::setProfile(StringRef Filename) {
116   if (!sys::fs::exists(Filename))
117     return errorCodeToError(make_error_code(errc::no_such_file_or_directory));
118 
119   if (ProfileReader) {
120     // Already exists
121     return make_error<StringError>(
122         Twine("multiple profiles specified: ") + ProfileReader->getFilename() +
123         " and " + Filename, inconvertibleErrorCode());
124   }
125 
126   ProfileReader = std::make_unique<DataReader>(Filename);
127   return Error::success();
128 }
129 
130 void MachORewriteInstance::preprocessProfileData() {
131   if (!ProfileReader)
132     return;
133   if (Error E = ProfileReader->preprocessProfile(*BC.get()))
134     report_error("cannot pre-process profile", std::move(E));
135 }
136 
137 void MachORewriteInstance::processProfileDataPreCFG() {
138   if (!ProfileReader)
139     return;
140   if (Error E = ProfileReader->readProfilePreCFG(*BC.get()))
141     report_error("cannot read profile pre-CFG", std::move(E));
142 }
143 
144 void MachORewriteInstance::processProfileData() {
145   if (!ProfileReader)
146     return;
147   if (Error E = ProfileReader->readProfile(*BC.get()))
148     report_error("cannot read profile", std::move(E));
149 }
150 
151 void MachORewriteInstance::readSpecialSections() {
152   for (const object::SectionRef &Section : InputFile->sections()) {
153     Expected<StringRef> SectionName = Section.getName();;
154     check_error(SectionName.takeError(), "cannot get section name");
155     // Only register sections with names.
156     if (!SectionName->empty()) {
157       BC->registerSection(Section);
158       LLVM_DEBUG(
159           dbgs() << "BOLT-DEBUG: registering section " << *SectionName
160                  << " @ 0x" << Twine::utohexstr(Section.getAddress()) << ":0x"
161                  << Twine::utohexstr(Section.getAddress() + Section.getSize())
162                  << "\n");
163     }
164   }
165 
166   if (opts::PrintSections) {
167     outs() << "BOLT-INFO: Sections from original binary:\n";
168     BC->printSections(outs());
169   }
170 }
171 
172 namespace {
173 
174 struct DataInCodeRegion {
175   explicit DataInCodeRegion(DiceRef D) {
176     D.getOffset(Offset);
177     D.getLength(Length);
178     D.getKind(Kind);
179   }
180 
181   uint32_t Offset;
182   uint16_t Length;
183   uint16_t Kind;
184 };
185 
186 std::vector<DataInCodeRegion> readDataInCode(const MachOObjectFile &O) {
187   const MachO::linkedit_data_command DataInCodeLC =
188       O.getDataInCodeLoadCommand();
189   const uint32_t NumberOfEntries =
190       DataInCodeLC.datasize / sizeof(MachO::data_in_code_entry);
191   std::vector<DataInCodeRegion> DataInCode;
192   DataInCode.reserve(NumberOfEntries);
193   for (auto I = O.begin_dices(), E = O.end_dices(); I != E; ++I)
194     DataInCode.emplace_back(*I);
195   llvm::stable_sort(DataInCode, [](DataInCodeRegion LHS, DataInCodeRegion RHS) {
196     return LHS.Offset < RHS.Offset;
197   });
198   return DataInCode;
199 }
200 
201 std::optional<uint64_t> readStartAddress(const MachOObjectFile &O) {
202   std::optional<uint64_t> StartOffset;
203   std::optional<uint64_t> TextVMAddr;
204   for (const object::MachOObjectFile::LoadCommandInfo &LC : O.load_commands()) {
205     switch (LC.C.cmd) {
206     case MachO::LC_MAIN: {
207       MachO::entry_point_command LCMain = O.getEntryPointCommand(LC);
208       StartOffset = LCMain.entryoff;
209       break;
210     }
211     case MachO::LC_SEGMENT: {
212       MachO::segment_command LCSeg = O.getSegmentLoadCommand(LC);
213       StringRef SegmentName(LCSeg.segname,
214                             strnlen(LCSeg.segname, sizeof(LCSeg.segname)));
215       if (SegmentName == "__TEXT")
216         TextVMAddr = LCSeg.vmaddr;
217       break;
218     }
219     case MachO::LC_SEGMENT_64: {
220       MachO::segment_command_64 LCSeg = O.getSegment64LoadCommand(LC);
221       StringRef SegmentName(LCSeg.segname,
222                             strnlen(LCSeg.segname, sizeof(LCSeg.segname)));
223       if (SegmentName == "__TEXT")
224         TextVMAddr = LCSeg.vmaddr;
225       break;
226     }
227     default:
228       continue;
229     }
230   }
231   return (TextVMAddr && StartOffset)
232              ? std::optional<uint64_t>(*TextVMAddr + *StartOffset)
233              : std::nullopt;
234 }
235 
236 } // anonymous namespace
237 
238 void MachORewriteInstance::discoverFileObjects() {
239   std::vector<SymbolRef> FunctionSymbols;
240   for (const SymbolRef &S : InputFile->symbols()) {
241     SymbolRef::Type Type = cantFail(S.getType(), "cannot get symbol type");
242     if (Type == SymbolRef::ST_Function)
243       FunctionSymbols.push_back(S);
244   }
245   if (FunctionSymbols.empty())
246     return;
247   llvm::stable_sort(
248       FunctionSymbols, [](const SymbolRef &LHS, const SymbolRef &RHS) {
249         return cantFail(LHS.getValue()) < cantFail(RHS.getValue());
250       });
251   for (size_t Index = 0; Index < FunctionSymbols.size(); ++Index) {
252     const uint64_t Address = cantFail(FunctionSymbols[Index].getValue());
253     ErrorOr<BinarySection &> Section = BC->getSectionForAddress(Address);
254     // TODO: It happens for some symbols (e.g. __mh_execute_header).
255     // Add proper logic to handle them correctly.
256     if (!Section) {
257       errs() << "BOLT-WARNING: no section found for address " << Address
258              << "\n";
259       continue;
260     }
261 
262     std::string SymbolName =
263         cantFail(FunctionSymbols[Index].getName(), "cannot get symbol name")
264             .str();
265     // Uniquify names of local symbols.
266     if (!(cantFail(FunctionSymbols[Index].getFlags()) & SymbolRef::SF_Global))
267       SymbolName = NR.uniquify(SymbolName);
268 
269     section_iterator S = cantFail(FunctionSymbols[Index].getSection());
270     uint64_t EndAddress = S->getAddress() + S->getSize();
271 
272     size_t NFIndex = Index + 1;
273     // Skip aliases.
274     while (NFIndex < FunctionSymbols.size() &&
275            cantFail(FunctionSymbols[NFIndex].getValue()) == Address)
276       ++NFIndex;
277     if (NFIndex < FunctionSymbols.size() &&
278         S == cantFail(FunctionSymbols[NFIndex].getSection()))
279       EndAddress = cantFail(FunctionSymbols[NFIndex].getValue());
280 
281     const uint64_t SymbolSize = EndAddress - Address;
282     const auto It = BC->getBinaryFunctions().find(Address);
283     if (It == BC->getBinaryFunctions().end()) {
284       BinaryFunction *Function = BC->createBinaryFunction(
285           std::move(SymbolName), *Section, Address, SymbolSize);
286       if (!opts::Instrument)
287         Function->setOutputAddress(Function->getAddress());
288 
289     } else {
290       It->second.addAlternativeName(std::move(SymbolName));
291     }
292   }
293 
294   const std::vector<DataInCodeRegion> DataInCode = readDataInCode(*InputFile);
295 
296   for (auto &BFI : BC->getBinaryFunctions()) {
297     BinaryFunction &Function = BFI.second;
298     Function.setMaxSize(Function.getSize());
299 
300     ErrorOr<ArrayRef<uint8_t>> FunctionData = Function.getData();
301     if (!FunctionData) {
302       errs() << "BOLT-ERROR: corresponding section is non-executable or "
303              << "empty for function " << Function << '\n';
304       continue;
305     }
306 
307     // Treat zero-sized functions as non-simple ones.
308     if (Function.getSize() == 0) {
309       Function.setSimple(false);
310       continue;
311     }
312 
313     // Offset of the function in the file.
314     const auto *FileBegin =
315         reinterpret_cast<const uint8_t *>(InputFile->getData().data());
316     Function.setFileOffset(FunctionData->begin() - FileBegin);
317 
318     // Treat functions which contain data in code as non-simple ones.
319     const auto It = std::lower_bound(
320         DataInCode.cbegin(), DataInCode.cend(), Function.getFileOffset(),
321         [](DataInCodeRegion D, uint64_t Offset) { return D.Offset < Offset; });
322     if (It != DataInCode.cend() &&
323         It->Offset + It->Length <=
324             Function.getFileOffset() + Function.getMaxSize())
325       Function.setSimple(false);
326   }
327 
328   BC->StartFunctionAddress = readStartAddress(*InputFile);
329 }
330 
331 void MachORewriteInstance::disassembleFunctions() {
332   for (auto &BFI : BC->getBinaryFunctions()) {
333     BinaryFunction &Function = BFI.second;
334     if (!Function.isSimple())
335       continue;
336     Function.disassemble();
337     if (opts::PrintDisasm)
338       Function.print(outs(), "after disassembly");
339   }
340 }
341 
342 void MachORewriteInstance::buildFunctionsCFG() {
343   for (auto &BFI : BC->getBinaryFunctions()) {
344     BinaryFunction &Function = BFI.second;
345     if (!Function.isSimple())
346       continue;
347     if (!Function.buildCFG(/*AllocId*/ 0)) {
348       errs() << "BOLT-WARNING: failed to build CFG for the function "
349              << Function << "\n";
350     }
351   }
352 }
353 
354 void MachORewriteInstance::postProcessFunctions() {
355   for (auto &BFI : BC->getBinaryFunctions()) {
356     BinaryFunction &Function = BFI.second;
357     if (Function.empty())
358       continue;
359     Function.postProcessCFG();
360     if (opts::PrintCFG)
361       Function.print(outs(), "after building cfg");
362   }
363 }
364 
365 void MachORewriteInstance::runOptimizationPasses() {
366   BinaryFunctionPassManager Manager(*BC);
367   if (opts::Instrument) {
368     Manager.registerPass(std::make_unique<PatchEntries>());
369     Manager.registerPass(std::make_unique<Instrumentation>(opts::NeverPrint));
370   }
371 
372   Manager.registerPass(std::make_unique<ShortenInstructions>(opts::NeverPrint));
373 
374   Manager.registerPass(std::make_unique<RemoveNops>(opts::NeverPrint));
375 
376   Manager.registerPass(std::make_unique<NormalizeCFG>(opts::PrintNormalized));
377 
378   Manager.registerPass(
379       std::make_unique<ReorderBasicBlocks>(opts::PrintReordered));
380   Manager.registerPass(
381       std::make_unique<FixupBranches>(opts::PrintAfterBranchFixup));
382   // This pass should always run last.*
383   Manager.registerPass(
384       std::make_unique<FinalizeFunctions>(opts::PrintFinalized));
385 
386   Manager.runPasses();
387 }
388 
389 void MachORewriteInstance::mapInstrumentationSection(
390     StringRef SectionName, BOLTLinker::SectionMapper MapSection) {
391   if (!opts::Instrument)
392     return;
393   ErrorOr<BinarySection &> Section = BC->getUniqueSectionByName(SectionName);
394   if (!Section) {
395     llvm::errs() << "Cannot find " + SectionName + " section\n";
396     exit(1);
397   }
398   if (!Section->hasValidSectionID())
399     return;
400   MapSection(*Section, Section->getAddress());
401 }
402 
403 void MachORewriteInstance::mapCodeSections(
404     BOLTLinker::SectionMapper MapSection) {
405   for (BinaryFunction *Function : BC->getAllBinaryFunctions()) {
406     if (!Function->isEmitted())
407       continue;
408     if (Function->getOutputAddress() == 0)
409       continue;
410     ErrorOr<BinarySection &> FuncSection = Function->getCodeSection();
411     if (!FuncSection)
412       report_error(
413           (Twine("Cannot find section for function ") + Function->getOneName())
414               .str(),
415           FuncSection.getError());
416 
417     FuncSection->setOutputAddress(Function->getOutputAddress());
418     LLVM_DEBUG(dbgs() << "BOLT: mapping 0x"
419                  << Twine::utohexstr(FuncSection->getAllocAddress()) << " to 0x"
420                  << Twine::utohexstr(Function->getOutputAddress()) << '\n');
421     MapSection(*FuncSection, Function->getOutputAddress());
422     Function->setImageAddress(FuncSection->getAllocAddress());
423     Function->setImageSize(FuncSection->getOutputSize());
424   }
425 
426   if (opts::Instrument) {
427     ErrorOr<BinarySection &> BOLT = BC->getUniqueSectionByName("__bolt");
428     if (!BOLT) {
429       llvm::errs() << "Cannot find __bolt section\n";
430       exit(1);
431     }
432     uint64_t Addr = BOLT->getAddress();
433     for (BinaryFunction *Function : BC->getAllBinaryFunctions()) {
434       if (!Function->isEmitted())
435         continue;
436       if (Function->getOutputAddress() != 0)
437         continue;
438       ErrorOr<BinarySection &> FuncSection = Function->getCodeSection();
439       assert(FuncSection && "cannot find section for function");
440       Addr = llvm::alignTo(Addr, 4);
441       FuncSection->setOutputAddress(Addr);
442       MapSection(*FuncSection, Addr);
443       Function->setFileOffset(Addr - BOLT->getAddress() +
444                               BOLT->getInputFileOffset());
445       Function->setImageAddress(FuncSection->getAllocAddress());
446       Function->setImageSize(FuncSection->getOutputSize());
447       BC->registerNameAtAddress(Function->getOneName(), Addr, 0, 0);
448       Addr += FuncSection->getOutputSize();
449     }
450   }
451 }
452 
453 void MachORewriteInstance::emitAndLink() {
454   std::error_code EC;
455   std::unique_ptr<::llvm::ToolOutputFile> TempOut =
456       std::make_unique<::llvm::ToolOutputFile>(
457           opts::OutputFilename + ".bolt.o", EC, sys::fs::OF_None);
458   check_error(EC, "cannot create output object file");
459 
460   if (opts::KeepTmp)
461     TempOut->keep();
462 
463   std::unique_ptr<buffer_ostream> BOS =
464       std::make_unique<buffer_ostream>(TempOut->os());
465   raw_pwrite_stream *OS = BOS.get();
466   auto Streamer = BC->createStreamer(*OS);
467 
468   emitBinaryContext(*Streamer, *BC, getOrgSecPrefix());
469   Streamer->finish();
470 
471   std::unique_ptr<MemoryBuffer> ObjectMemBuffer =
472       MemoryBuffer::getMemBuffer(BOS->str(), "in-memory object file", false);
473   std::unique_ptr<object::ObjectFile> Obj = cantFail(
474       object::ObjectFile::createObjectFile(ObjectMemBuffer->getMemBufferRef()),
475       "error creating in-memory object");
476   assert(Obj && "createObjectFile cannot return nullptr");
477 
478   auto EFMM = std::make_unique<ExecutableFileMemoryManager>(*BC);
479   EFMM->setNewSecPrefix(getNewSecPrefix());
480   EFMM->setOrgSecPrefix(getOrgSecPrefix());
481 
482   Linker = std::make_unique<JITLinkLinker>(*BC, std::move(EFMM));
483   Linker->loadObject(ObjectMemBuffer->getMemBufferRef(),
484                      [this](auto MapSection) {
485                        // Assign addresses to all sections. If key corresponds
486                        // to the object created by ourselves, call our regular
487                        // mapping function. If we are loading additional objects
488                        // as part of runtime libraries for instrumentation,
489                        // treat them as extra sections.
490                        mapCodeSections(MapSection);
491                        mapInstrumentationSection("__counters", MapSection);
492                        mapInstrumentationSection("__tables", MapSection);
493                      });
494 
495   // TODO: Refactor addRuntimeLibSections to work properly on Mach-O
496   // and use it here.
497   // if (auto *RtLibrary = BC->getRuntimeLibrary()) {
498   //   RtLibrary->link(*BC, ToolPath, *Linker, [this](auto MapSection) {
499   //     mapInstrumentationSection("I__setup", MapSection);
500   //     mapInstrumentationSection("I__fini", MapSection);
501   //     mapInstrumentationSection("I__data", MapSection);
502   //     mapInstrumentationSection("I__text", MapSection);
503   //     mapInstrumentationSection("I__cstring", MapSection);
504   //     mapInstrumentationSection("I__literal16", MapSection);
505   //   });
506   // }
507 }
508 
509 void MachORewriteInstance::writeInstrumentationSection(StringRef SectionName,
510                                                        raw_pwrite_stream &OS) {
511   if (!opts::Instrument)
512     return;
513   ErrorOr<BinarySection &> Section = BC->getUniqueSectionByName(SectionName);
514   if (!Section) {
515     llvm::errs() << "Cannot find " + SectionName + " section\n";
516     exit(1);
517   }
518   if (!Section->hasValidSectionID())
519     return;
520   assert(Section->getInputFileOffset() &&
521          "Section input offset cannot be zero");
522   assert(Section->getAllocAddress() && "Section alloc address cannot be zero");
523   assert(Section->getOutputSize() && "Section output size cannot be zero");
524   OS.pwrite(reinterpret_cast<char *>(Section->getAllocAddress()),
525             Section->getOutputSize(), Section->getInputFileOffset());
526 }
527 
528 void MachORewriteInstance::rewriteFile() {
529   std::error_code EC;
530   Out = std::make_unique<ToolOutputFile>(opts::OutputFilename, EC,
531                                          sys::fs::OF_None);
532   check_error(EC, "cannot create output executable file");
533   raw_fd_ostream &OS = Out->os();
534   OS << InputFile->getData();
535 
536   for (auto &BFI : BC->getBinaryFunctions()) {
537     BinaryFunction &Function = BFI.second;
538     if (!Function.isSimple())
539       continue;
540     assert(Function.isEmitted() && "Simple function has not been emitted");
541     if (!opts::Instrument && (Function.getImageSize() > Function.getMaxSize()))
542       continue;
543     if (opts::Verbosity >= 2)
544       outs() << "BOLT: rewriting function \"" << Function << "\"\n";
545     OS.pwrite(reinterpret_cast<char *>(Function.getImageAddress()),
546               Function.getImageSize(), Function.getFileOffset());
547   }
548 
549   for (const BinaryFunction *Function : BC->getInjectedBinaryFunctions()) {
550     OS.pwrite(reinterpret_cast<char *>(Function->getImageAddress()),
551               Function->getImageSize(), Function->getFileOffset());
552   }
553 
554   writeInstrumentationSection("__counters", OS);
555   writeInstrumentationSection("__tables", OS);
556 
557   // TODO: Refactor addRuntimeLibSections to work properly on Mach-O and
558   // use it here.
559   writeInstrumentationSection("I__setup", OS);
560   writeInstrumentationSection("I__fini", OS);
561   writeInstrumentationSection("I__data", OS);
562   writeInstrumentationSection("I__text", OS);
563   writeInstrumentationSection("I__cstring", OS);
564   writeInstrumentationSection("I__literal16", OS);
565 
566   Out->keep();
567   EC = sys::fs::setPermissions(
568       opts::OutputFilename,
569       static_cast<sys::fs::perms>(sys::fs::perms::all_all &
570                                   ~sys::fs::getUmask()));
571   check_error(EC, "cannot set permissions of output file");
572 }
573 
574 void MachORewriteInstance::adjustCommandLineOptions() {
575 //FIXME! Upstream change
576 //  opts::CheckOverlappingElements = false;
577   if (!opts::AlignText.getNumOccurrences())
578     opts::AlignText = BC->PageAlign;
579   if (opts::Instrument.getNumOccurrences())
580     opts::ForcePatch = true;
581   opts::JumpTables = JTS_MOVE;
582   opts::InstrumentCalls = false;
583   opts::RuntimeInstrumentationLib = "libbolt_rt_instr_osx.a";
584 }
585 
586 void MachORewriteInstance::run() {
587   adjustCommandLineOptions();
588 
589   readSpecialSections();
590 
591   discoverFileObjects();
592 
593   preprocessProfileData();
594 
595   disassembleFunctions();
596 
597   processProfileDataPreCFG();
598 
599   buildFunctionsCFG();
600 
601   processProfileData();
602 
603   postProcessFunctions();
604 
605   runOptimizationPasses();
606 
607   emitAndLink();
608 
609   rewriteFile();
610 }
611 
612 MachORewriteInstance::~MachORewriteInstance() {}
613 
614 } // namespace bolt
615 } // namespace llvm
616