RetroLinker
Linker for several 8-bit, 16-bit and 32-bit formats
Loading...
Searching...
No Matches
macho.h
1#ifndef MACHO_H
2#define MACHO_H
3
4#include <sstream>
5#include <vector>
6#include "../common.h"
7#include "../dumper/dumper.h"
8#include "../linker/module.h"
9#include "../linker/segment_manager.h"
10#include "../linker/reader.h"
11
12// TODO: incomplete, untested
13
14namespace MachO
15{
21 class MachOFormat : public virtual Linker::SegmentManager
22 {
23 public:
24 ::EndianType endiantype = ::UndefinedEndian;
25 size_t wordsize;
26 enum cpu_type
27 {
28 CPU_VAX = 1,
29 CPU_ROMP = 2,
30 CPU_NS32032 = 4,
31 CPU_NS32332 = 5,
32 CPU_MC680x0 = 6,
33 CPU_X86 = 7,
34 CPU_MIPS = 8,
35 CPU_NS32352 = 9,
36 CPU_MC98000 = 10,
37 CPU_HPPA = 11,
38 CPU_ARM = 12,
39 CPU_MC88000 = 13,
40 CPU_SPARC = 14,
41 CPU_I860 = 15,
42 CPU_I860_LE = 16,
43 CPU_ALPHA = 16,
44 CPU_RS6000 = 17,
45 CPU_POWERPC = 18,
46 };
47 cpu_type cpu = cpu_type(0);
48 uint32_t cpu_subtype = 0;
49 uint32_t file_type = 0;
50 uint32_t commands_size = 0;
51 uint32_t flags = 0;
52
54 {
55 public:
56 enum command_type : uint32_t
57 {
58 REQ_DYLD = 0x80000000,
59
60 SEGMENT = 1,
61 SYMTAB = 2,
62 SYMSEG = 3, // obsolete
63 THREAD = 4,
64 UNIXTHREAD = 5,
65 LOADFVMLIB = 6,
66 IDFVMLIB = 7,
67 IDENT = 8, // obsolete
68 FVMFILE = 9, // internal
69 PREPAGE = 10, // internal
70 DYNSYMTAB = 11,
71 LOAD_DYLIB = 12,
72 ID_DYLIB = 13,
73 LOAD_DYLINKER = 14,
74 ID_DYLINKER = 15,
75 PREBOUND_DYLIB = 16,
76 ROUTINES = 17,
77 SUB_FRAMEWORK = 18,
78 SUB_UMBRELLA = 19,
79 SUB_CLIENT = 20,
80 SUB_LIBRARY = 21,
81 TWOLEVEL_HINTS = 22,
82 PREBIND_CKSUM = 23,
83 _LOAD_WEAK_DYLIB = 24,
84 SEGMENT_64 = 25,
85 ROUTINES_64 = 26,
86 UUID = 27,
87 _RPATH = 28,
88 CODE_SIGNATURE = 29,
89 SEGMENT_SPLIT_INFO = 30,
90 _REEXPORT_DYLIB = 31,
91 LAZY_LOAD_DYLIB = 32,
92 ENCRYPTION_INFO = 33,
93 DYLD_INFO = 34,
94 _LOAD_UPWARD_DYLIB = 35,
95 VERSION_MIN_MACOSX = 36,
96 VERSION_MIN_IPHONEOS = 37,
97 FUNCTION_STARTS = 38,
98 DYLD_ENVIRONMENT = 39,
99 _MAIN = 40,
100 DATA_IN_CODE = 41,
101 SOURCE_VERSION = 42,
102 DYLIB_CODE_SIGN_DRS = 43,
103 ENCRYPTION_INFO_64 = 44,
104 LINKER_OPTION = 45,
105 LINKER_OPTIMIZATION_HINT = 46,
106 VERSION_MIN_TVOS = 47,
107 VERSION_MIN_WATCHOS = 48,
108 NOTE = 49,
109 BUILD_VERSION = 50,
110 _DYLD_EXPORTS_TRIE = 51,
111 _DYLD_CHAINED_FIXUPS = 52,
112 _FILESET_ENTRY = 53,
113
114 LOAD_WEAK_DYLIB = _LOAD_WEAK_DYLIB | REQ_DYLD,
115 RPATH = _RPATH | REQ_DYLD,
116 REEXPORT_DYLIB = _REEXPORT_DYLIB | REQ_DYLD,
117 DYLD_INFO_ONLY = DYLD_INFO | REQ_DYLD,
118 LOAD_UPWARD_DYLIB = _LOAD_UPWARD_DYLIB | REQ_DYLD,
119 MAIN = _MAIN | REQ_DYLD,
120 DYLD_EXPORTS_TRIE = _DYLD_EXPORTS_TRIE | REQ_DYLD,
121 DYLD_CHAINED_FIXUPS = _DYLD_CHAINED_FIXUPS | REQ_DYLD,
122 FILESET_ENTRY = _FILESET_ENTRY | REQ_DYLD,
123 };
124 command_type command;
125
126 LoadCommand(command_type command)
127 : command(command)
128 {
129 }
130
131 virtual ~LoadCommand() = default;
132
133 static std::unique_ptr<LoadCommand> Read(Linker::Reader& rd);
134
135 virtual void ReadFile(Linker::Reader& rd);
136 virtual void WriteFile(Linker::Writer& wr) const;
137
138 virtual void Read(Linker::Reader& rd, offset_t size) = 0;
139 virtual void Write(Linker::Writer& wr) const;
140 virtual offset_t GetSize() const = 0;
141 };
142 std::vector<std::unique_ptr<LoadCommand>> load_commands;
143
145 {
146 public:
147 GenericDataCommand(command_type command)
148 : LoadCommand(command)
149 {
150 }
151
152 std::shared_ptr<Linker::Image> command_image;
153
154 void Read(Linker::Reader& rd, offset_t size) override;
155 void Write(Linker::Writer& wr) const override;
156 offset_t GetSize() const override;
157 };
158
160 {
161 public:
162 std::string name;
163 std::string segment_name;
164 uint64_t address = 0;
165 uint64_t memory_size = 0;
166 uint32_t offset = 0;
167 uint32_t align_shift = 0;
168 uint32_t relocation_offset = 0;
169 uint32_t relocation_count = 0; // TODO
170 uint32_t flags = 0;
171 uint32_t reserved1 = 0;
172 uint32_t reserved2 = 0;
173
174 static Section Read(Linker::Reader& rd, int wordsize);
175 void Write(Linker::Writer& rd, int wordsize) const;
176 };
177
179 {
180 public:
181 std::string name;
182 uint64_t address = 0;
183 uint64_t memory_size = 0;
184 uint64_t offset = 0;
185 uint64_t file_size = 0;
186 uint32_t max_protection = 0;
187 uint32_t init_protection = 0;
188 uint32_t flags;
189 std::vector<Section> sections;
190
191 void ReadFile(Linker::Reader& rd) override;
192 void WriteFile(Linker::Writer& wr) const override;
193
194 void Read(Linker::Reader& rd, offset_t size) override;
195 void Write(Linker::Writer& wr) const override;
196 offset_t GetSize() const override;
197 };
198
199 void ReadFile(Linker::Reader& rd) override;
201 offset_t WriteFile(Linker::Writer& wr) const override;
202 void Dump(Dumper::Dumper& dump) const override;
203 /* TODO */
204 };
205
207 {
208 public:
209 struct Entry
210 {
211 MachOFormat::cpu_type cpu = MachOFormat::cpu_type(0);
212 uint32_t cpu_subtype = 0;
213 uint32_t offset = 0;
214 uint32_t size = 0;
215 uint32_t align = 0;
216 std::shared_ptr<Linker::Image> image;
217
218 static Entry Read(Linker::Reader& rd);
219 void Write(Linker::Writer& wr) const;
220 };
221 std::vector<Entry> entries;
222
223 offset_t ImageSize() const override;
224 void ReadFile(Linker::Reader& rd) override;
225
227 offset_t WriteFile(Linker::Writer& wr) const override;
228
229 void Dump(Dumper::Dumper& dump) const override;
230 void CalculateValues() override;
231 };
232}
233
234#endif /* MACHO_H */
An abstract interface that separates structure and presentation of the data inside a file.
Definition dumper.h:586
offset_t WriteFile(Writer &wr) const override=0
Stores data in memory to file.
A class that provides a general interface to setting up generation for a format.
Definition format.h:61
A helper class, encapsulating functionality needed to import binary data.
Definition reader.h:16
A helper class to collect sections into segments.
Definition segment_manager.h:32
A helper class, encapsulating functionality needed to export binary data.
Definition writer.h:15
Definition macho.h:207
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macho.cc:344
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macho.cc:309
void CalculateValues() override
Intermediate step between processing module and generating output file to set up headers and manageme...
Definition macho.cc:353
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macho.cc:327
offset_t ImageSize() const override
Retrieves size of stored data.
Definition macho.cc:296
Definition macho.h:54
Definition macho.h:160
Mach/NeXTSTEP/Mac OS X (macOS) file format.
Definition macho.h:22
void Dump(Dumper::Dumper &dump) const override
Display file contents in a nice manner.
Definition macho.cc:265
offset_t WriteFile(Linker::Writer &wr) const override
Stores data in memory to file.
Definition macho.cc:200
void ReadFile(Linker::Reader &rd) override
Loads file into memory.
Definition macho.cc:149
Definition macho.h:210