xref: /llvm-project/bolt/include/bolt/Rewrite/MachORewriteInstance.h (revision 05634f7346a59f6dab89cde53f39b40d9a70b9c9)
1 //===- bolt/Rewrite/MachORewriteInstance.h - MachO rewriter -----*- C++ -*-===//
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 // Interface to control an instance of a macho binary rewriting process.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef BOLT_REWRITE_MACHO_REWRITE_INSTANCE_H
14 #define BOLT_REWRITE_MACHO_REWRITE_INSTANCE_H
15 
16 #include "bolt/Core/Linker.h"
17 #include "bolt/Utils/NameResolver.h"
18 #include "llvm/Support/Error.h"
19 #include <memory>
20 
21 namespace llvm {
22 class ToolOutputFile;
23 class raw_pwrite_stream;
24 namespace object {
25 class MachOObjectFile;
26 } // namespace object
27 
28 namespace bolt {
29 
30 class BinaryContext;
31 class ProfileReaderBase;
32 
33 class MachORewriteInstance {
34   object::MachOObjectFile *InputFile;
35   StringRef ToolPath;
36   std::unique_ptr<BinaryContext> BC;
37 
38   NameResolver NR;
39 
40   std::unique_ptr<BOLTLinker> Linker;
41 
42   std::unique_ptr<ToolOutputFile> Out;
43 
44   std::unique_ptr<ProfileReaderBase> ProfileReader;
45   void preprocessProfileData();
46   void processProfileDataPreCFG();
47   void processProfileData();
48 
getNewSecPrefix()49   static StringRef getNewSecPrefix() { return ".bolt.new"; }
getOrgSecPrefix()50   static StringRef getOrgSecPrefix() { return ".bolt.org"; }
51 
52   void mapInstrumentationSection(StringRef SectionName,
53                                  BOLTLinker::SectionMapper MapSection);
54   void mapCodeSections(BOLTLinker::SectionMapper MapSection);
55 
56   void adjustCommandLineOptions();
57   void readSpecialSections();
58   void discoverFileObjects();
59   void disassembleFunctions();
60   void buildFunctionsCFG();
61   void postProcessFunctions();
62   void runOptimizationPasses();
63   void emitAndLink();
64 
65   void writeInstrumentationSection(StringRef SectionName,
66                                    raw_pwrite_stream &OS);
67   void rewriteFile();
68 
69 public:
70   // This constructor has complex initialization that can fail during
71   // construction. Constructors can’t return errors, so clients must test \p Err
72   // after the object is constructed. Use `create` method instead.
73   MachORewriteInstance(object::MachOObjectFile *InputFile, StringRef ToolPath,
74                        Error &Err);
75 
76   static Expected<std::unique_ptr<MachORewriteInstance>>
77   create(object::MachOObjectFile *InputFile, StringRef ToolPath);
78   ~MachORewriteInstance();
79 
80   Error setProfile(StringRef FileName);
81 
82   /// Run all the necessary steps to read, optimize and rewrite the binary.
83   void run();
84 };
85 
86 } // namespace bolt
87 } // namespace llvm
88 
89 #endif
90