1 //===- MachOWriter.h --------------------------------------------*- 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 #ifndef LLVM_LIB_OBJCOPY_MACHO_MACHOWRITER_H
10 #define LLVM_LIB_OBJCOPY_MACHO_MACHOWRITER_H
11 
12 #include "MachOLayoutBuilder.h"
13 #include "MachOObject.h"
14 #include "llvm/BinaryFormat/MachO.h"
15 #include "llvm/ObjCopy/MachO/MachOObjcopy.h"
16 #include "llvm/Object/MachO.h"
17 
18 namespace llvm {
19 class Error;
20 
21 namespace objcopy {
22 namespace macho {
23 
24 class MachOWriter {
25   Object &O;
26   bool Is64Bit;
27   bool IsLittleEndian;
28   uint64_t PageSize;
29   std::unique_ptr<WritableMemoryBuffer> Buf;
30   raw_ostream &Out;
31   MachOLayoutBuilder LayoutBuilder;
32 
33   size_t headerSize() const;
34   size_t loadCommandsSize() const;
35   size_t symTableSize() const;
36   size_t strTableSize() const;
37 
38   void writeHeader();
39   void writeLoadCommands();
40   template <typename StructType>
41   void writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out);
42   void writeSections();
43   void writeSymbolTable();
44   void writeStringTable();
45   void writeRebaseInfo();
46   void writeBindInfo();
47   void writeWeakBindInfo();
48   void writeLazyBindInfo();
49   void writeExportInfo();
50   void writeIndirectSymbolTable();
51   void writeLinkData(std::optional<size_t> LCIndex, const LinkData &LD);
52   void writeCodeSignatureData();
53   void writeDataInCodeData();
54   void writeLinkerOptimizationHint();
55   void writeFunctionStartsData();
56   void writeDylibCodeSignDRsData();
57   void writeChainedFixupsData();
58   void writeExportsTrieData();
59   void writeTail();
60 
61 public:
62   MachOWriter(Object &O, bool Is64Bit, bool IsLittleEndian,
63               StringRef OutputFileName, uint64_t PageSize, raw_ostream &Out)
64       : O(O), Is64Bit(Is64Bit), IsLittleEndian(IsLittleEndian),
65         PageSize(PageSize), Out(Out),
66         LayoutBuilder(O, Is64Bit, OutputFileName, PageSize) {}
67 
68   size_t totalSize() const;
69   Error finalize();
70   Error write();
71 };
72 
73 } // end namespace macho
74 } // end namespace objcopy
75 } // end namespace llvm
76 
77 #endif // LLVM_LIB_OBJCOPY_MACHO_MACHOWRITER_H
78