blob: a72a9609454e6aadb2dd8d215ddfa071cf3f02d8 [file] [log] [blame]
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00001//===- MachOWriter.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth127252b2019-02-11 08:25:19 +00003// 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
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MachOWriter.h"
10#include "../llvm-objcopy.h"
11#include "Object.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/BinaryFormat/MachO.h"
14#include "llvm/Object/MachO.h"
15#include <memory>
16
17namespace llvm {
18namespace objcopy {
19namespace macho {
20
21size_t MachOWriter::headerSize() const {
22 return Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
23}
24
25size_t MachOWriter::loadCommandsSize() const { return O.Header.SizeOfCmds; }
26
27size_t MachOWriter::symTableSize() const {
28 return O.SymTable.NameList.size() *
29 (Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist));
30}
31
32size_t MachOWriter::strTableSize() const {
33 size_t S = 0;
34 for (const auto &Str : O.StrTable.Strings)
35 S += Str.size();
36 S += (O.StrTable.Strings.empty() ? 0 : O.StrTable.Strings.size() - 1);
37 return S;
38}
39
40size_t MachOWriter::totalSize() const {
41 // Going from tail to head and looking for an appropriate "anchor" to
42 // calculate the total size assuming that all the offsets are either valid
43 // ("true") or 0 (0 indicates that the corresponding part is missing).
44
45 SmallVector<size_t, 7> Ends;
46 if (O.SymTabCommandIndex) {
47 const MachO::symtab_command &SymTabCommand =
48 O.LoadCommands[*O.SymTabCommandIndex]
49 .MachOLoadCommand.symtab_command_data;
50 if (SymTabCommand.symoff) {
51 assert((SymTabCommand.nsyms == O.SymTable.NameList.size()) &&
52 "Incorrect number of symbols");
53 Ends.push_back(SymTabCommand.symoff + symTableSize());
54 }
55 if (SymTabCommand.stroff) {
56 assert((SymTabCommand.strsize == strTableSize()) &&
57 "Incorrect string table size");
58 Ends.push_back(SymTabCommand.stroff + SymTabCommand.strsize);
59 }
60 }
61 if (O.DyLdInfoCommandIndex) {
62 const MachO::dyld_info_command &DyLdInfoCommand =
63 O.LoadCommands[*O.DyLdInfoCommandIndex]
64 .MachOLoadCommand.dyld_info_command_data;
65 if (DyLdInfoCommand.rebase_off) {
66 assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
67 "Incorrect rebase opcodes size");
68 Ends.push_back(DyLdInfoCommand.rebase_off + DyLdInfoCommand.rebase_size);
69 }
70 if (DyLdInfoCommand.bind_off) {
71 assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
72 "Incorrect bind opcodes size");
73 Ends.push_back(DyLdInfoCommand.bind_off + DyLdInfoCommand.bind_size);
74 }
75 if (DyLdInfoCommand.weak_bind_off) {
76 assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
77 "Incorrect weak bind opcodes size");
78 Ends.push_back(DyLdInfoCommand.weak_bind_off +
79 DyLdInfoCommand.weak_bind_size);
80 }
81 if (DyLdInfoCommand.lazy_bind_off) {
82 assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
83 "Incorrect lazy bind opcodes size");
84 Ends.push_back(DyLdInfoCommand.lazy_bind_off +
85 DyLdInfoCommand.lazy_bind_size);
86 }
87 if (DyLdInfoCommand.export_off) {
88 assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
89 "Incorrect trie size");
90 Ends.push_back(DyLdInfoCommand.export_off + DyLdInfoCommand.export_size);
91 }
92 }
93
94 // Otherwise, use the last section / reloction.
95 for (const auto &LC : O.LoadCommands)
96 for (const auto &S : LC.Sections) {
97 Ends.push_back(S.Offset + S.Size);
98 if (S.RelOff)
99 Ends.push_back(S.RelOff +
100 S.NReloc * sizeof(MachO::any_relocation_info));
101 }
102
103 if (!Ends.empty())
104 return *std::max_element(Ends.begin(), Ends.end());
105
106 // Otherwise, we have only Mach header and load commands.
107 return headerSize() + loadCommandsSize();
108}
109
110void MachOWriter::writeHeader() {
111 MachO::mach_header_64 Header;
112
113 Header.magic = O.Header.Magic;
114 Header.cputype = O.Header.CPUType;
115 Header.cpusubtype = O.Header.CPUSubType;
116 Header.filetype = O.Header.FileType;
117 Header.ncmds = O.Header.NCmds;
118 Header.sizeofcmds = O.Header.SizeOfCmds;
119 Header.flags = O.Header.Flags;
120 Header.reserved = O.Header.Reserved;
121
122 if (IsLittleEndian != sys::IsLittleEndianHost)
123 MachO::swapStruct(Header);
124
125 auto HeaderSize =
126 Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
127 memcpy(B.getBufferStart(), &Header, HeaderSize);
128}
129
130void MachOWriter::writeLoadCommands() {
131 uint8_t *Begin = B.getBufferStart() + headerSize();
132 MachO::macho_load_command MLC;
133 for (const auto &LC : O.LoadCommands) {
134#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
135 case MachO::LCName: \
136 assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \
137 LC.MachOLoadCommand.load_command_data.cmdsize); \
138 MLC = LC.MachOLoadCommand; \
139 if (IsLittleEndian != sys::IsLittleEndianHost) \
140 MachO::swapStruct(MLC.LCStruct##_data); \
141 memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \
142 Begin += sizeof(MachO::LCStruct); \
143 memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \
144 Begin += LC.Payload.size(); \
145 break;
146
147 switch (LC.MachOLoadCommand.load_command_data.cmd) {
148 default:
149 assert(sizeof(MachO::load_command) + LC.Payload.size() ==
150 LC.MachOLoadCommand.load_command_data.cmdsize);
151 MLC = LC.MachOLoadCommand;
152 if (IsLittleEndian != sys::IsLittleEndianHost)
153 MachO::swapStruct(MLC.load_command_data);
154 memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command));
155 Begin += sizeof(MachO::load_command);
156 memcpy(Begin, LC.Payload.data(), LC.Payload.size());
157 Begin += LC.Payload.size();
158 break;
159#include "llvm/BinaryFormat/MachO.def"
160 }
161 }
162}
163
164void MachOWriter::writeSections() {
165 for (const auto &LC : O.LoadCommands)
166 for (const auto &Sec : LC.Sections) {
167 assert(Sec.Offset && "Section offset can not be zero");
168 assert((Sec.Size == Sec.Content.size()) && "Incorrect section size");
169 memcpy(B.getBufferStart() + Sec.Offset, Sec.Content.data(),
170 Sec.Content.size());
171 for (size_t Index = 0; Index < Sec.Relocations.size(); ++Index) {
172 MachO::any_relocation_info R = Sec.Relocations[Index];
173 if (IsLittleEndian != sys::IsLittleEndianHost)
174 MachO::swapStruct(R);
175 memcpy(B.getBufferStart() + Sec.RelOff +
176 Index * sizeof(MachO::any_relocation_info),
177 &R, sizeof(R));
178 }
179 }
180}
181
182template <typename NListType>
183void writeNListEntry(const NListEntry &NLE, bool IsLittleEndian, char *&Out) {
184 NListType ListEntry;
185 ListEntry.n_strx = NLE.n_strx;
186 ListEntry.n_type = NLE.n_type;
187 ListEntry.n_sect = NLE.n_sect;
188 ListEntry.n_desc = NLE.n_desc;
189 ListEntry.n_value = NLE.n_value;
190
191 if (IsLittleEndian != sys::IsLittleEndianHost)
192 MachO::swapStruct(ListEntry);
193 memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType));
194 Out += sizeof(NListType);
195}
196
197void MachOWriter::writeSymbolTable() {
198 if (!O.SymTabCommandIndex)
199 return;
200 const MachO::symtab_command &SymTabCommand =
201 O.LoadCommands[*O.SymTabCommandIndex]
202 .MachOLoadCommand.symtab_command_data;
203 assert((SymTabCommand.nsyms == O.SymTable.NameList.size()) &&
204 "Incorrect number of symbols");
205 char *Out = (char *)B.getBufferStart() + SymTabCommand.symoff;
206 for (auto NLE : O.SymTable.NameList) {
207 if (Is64Bit)
208 writeNListEntry<MachO::nlist_64>(NLE, IsLittleEndian, Out);
209 else
210 writeNListEntry<MachO::nlist>(NLE, IsLittleEndian, Out);
211 }
212}
213
214void MachOWriter::writeStringTable() {
215 if (!O.SymTabCommandIndex)
216 return;
217 const MachO::symtab_command &SymTabCommand =
218 O.LoadCommands[*O.SymTabCommandIndex]
219 .MachOLoadCommand.symtab_command_data;
220 char *Out = (char *)B.getBufferStart() + SymTabCommand.stroff;
221 assert((SymTabCommand.strsize == strTableSize()) &&
222 "Incorrect string table size");
223 for (size_t Index = 0; Index < O.StrTable.Strings.size(); ++Index) {
224 memcpy(Out, O.StrTable.Strings[Index].data(),
225 O.StrTable.Strings[Index].size());
226 Out += O.StrTable.Strings[Index].size();
227 if (Index + 1 != O.StrTable.Strings.size()) {
228 memcpy(Out, "\0", 1);
229 Out += 1;
230 }
231 }
232}
233
234void MachOWriter::writeRebaseInfo() {
235 if (!O.DyLdInfoCommandIndex)
236 return;
237 const MachO::dyld_info_command &DyLdInfoCommand =
238 O.LoadCommands[*O.DyLdInfoCommandIndex]
239 .MachOLoadCommand.dyld_info_command_data;
240 char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.rebase_off;
241 assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) &&
242 "Incorrect rebase opcodes size");
243 memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size());
244}
245
246void MachOWriter::writeBindInfo() {
247 if (!O.DyLdInfoCommandIndex)
248 return;
249 const MachO::dyld_info_command &DyLdInfoCommand =
250 O.LoadCommands[*O.DyLdInfoCommandIndex]
251 .MachOLoadCommand.dyld_info_command_data;
252 char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.bind_off;
253 assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) &&
254 "Incorrect bind opcodes size");
255 memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size());
256}
257
258void MachOWriter::writeWeakBindInfo() {
259 if (!O.DyLdInfoCommandIndex)
260 return;
261 const MachO::dyld_info_command &DyLdInfoCommand =
262 O.LoadCommands[*O.DyLdInfoCommandIndex]
263 .MachOLoadCommand.dyld_info_command_data;
264 char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.weak_bind_off;
265 assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) &&
266 "Incorrect weak bind opcodes size");
267 memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size());
268}
269
270void MachOWriter::writeLazyBindInfo() {
271 if (!O.DyLdInfoCommandIndex)
272 return;
273 const MachO::dyld_info_command &DyLdInfoCommand =
274 O.LoadCommands[*O.DyLdInfoCommandIndex]
275 .MachOLoadCommand.dyld_info_command_data;
276 char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.lazy_bind_off;
277 assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) &&
278 "Incorrect lazy bind opcodes size");
279 memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size());
280}
281
282void MachOWriter::writeExportInfo() {
283 if (!O.DyLdInfoCommandIndex)
284 return;
285 const MachO::dyld_info_command &DyLdInfoCommand =
286 O.LoadCommands[*O.DyLdInfoCommandIndex]
287 .MachOLoadCommand.dyld_info_command_data;
288 char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.export_off;
289 assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) &&
290 "Incorrect export trie size");
291 memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size());
292}
293
294void MachOWriter::writeTail() {
295 typedef void (MachOWriter::*WriteHandlerType)(void);
296 typedef std::pair<uint64_t, WriteHandlerType> WriteOperation;
297 SmallVector<WriteOperation, 7> Queue;
298
299 if (O.SymTabCommandIndex) {
300 const MachO::symtab_command &SymTabCommand =
301 O.LoadCommands[*O.SymTabCommandIndex]
302 .MachOLoadCommand.symtab_command_data;
303 if (SymTabCommand.symoff)
304 Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable});
305 if (SymTabCommand.stroff)
306 Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable});
307 }
308
309 if (O.DyLdInfoCommandIndex) {
310 const MachO::dyld_info_command &DyLdInfoCommand =
311 O.LoadCommands[*O.DyLdInfoCommandIndex]
312 .MachOLoadCommand.dyld_info_command_data;
313 if (DyLdInfoCommand.rebase_off)
314 Queue.push_back(
315 {DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo});
316 if (DyLdInfoCommand.bind_off)
317 Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo});
318 if (DyLdInfoCommand.weak_bind_off)
319 Queue.push_back(
320 {DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo});
321 if (DyLdInfoCommand.lazy_bind_off)
322 Queue.push_back(
323 {DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo});
324 if (DyLdInfoCommand.export_off)
325 Queue.push_back(
326 {DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo});
327 }
328
329 llvm::sort(Queue, [](const WriteOperation &LHS, const WriteOperation &RHS) {
330 return LHS.first < RHS.first;
331 });
332
333 for (auto WriteOp : Queue)
334 (this->*WriteOp.second)();
335}
336
337Error MachOWriter::write() {
338 if (Error E = B.allocate(totalSize()))
339 return E;
340 memset(B.getBufferStart(), 0, totalSize());
341 writeHeader();
342 writeLoadCommands();
343 writeSections();
344 writeTail();
Jordan Rupprecht2e862c72019-02-04 19:09:20 +0000345 return B.commit();
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000346}
347
348} // end namespace macho
349} // end namespace objcopy
350} // end namespace llvm