blob: e6c5dde42027d12c75c865125df9bd84197e0dbc [file] [log] [blame]
Seiya Nuta552bcb82019-08-19 21:05:31 +00001//===- MachOLayoutBuilder.cpp -----------------------------------*- 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#include "MachOLayoutBuilder.h"
Guillaume Chateletaf11cc72019-09-12 15:20:36 +000010#include "llvm/Support/Alignment.h"
Seiya Nuta552bcb82019-08-19 21:05:31 +000011#include "llvm/Support/Errc.h"
12#include "llvm/Support/ErrorHandling.h"
13
14namespace llvm {
15namespace objcopy {
16namespace macho {
17
18uint32_t MachOLayoutBuilder::computeSizeOfCmds() const {
19 uint32_t Size = 0;
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -080020 for (const LoadCommand &LC : O.LoadCommands) {
Seiya Nuta12bd4902019-08-19 21:12:02 +000021 const MachO::macho_load_command &MLC = LC.MachOLoadCommand;
Seiya Nuta552bcb82019-08-19 21:05:31 +000022 auto cmd = MLC.load_command_data.cmd;
23 switch (cmd) {
24 case MachO::LC_SEGMENT:
25 Size += sizeof(MachO::segment_command) +
26 sizeof(MachO::section) * LC.Sections.size();
27 continue;
28 case MachO::LC_SEGMENT_64:
29 Size += sizeof(MachO::segment_command_64) +
30 sizeof(MachO::section_64) * LC.Sections.size();
31 continue;
32 }
33
34 switch (cmd) {
35#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
36 case MachO::LCName: \
37 Size += sizeof(MachO::LCStruct) + LC.Payload.size(); \
38 break;
39#include "llvm/BinaryFormat/MachO.def"
40#undef HANDLE_LOAD_COMMAND
41 }
42 }
43
44 return Size;
45}
46
47void MachOLayoutBuilder::constructStringTable() {
48 for (std::unique_ptr<SymbolEntry> &Sym : O.SymTable.Symbols)
49 StrTableBuilder.add(Sym->Name);
50 StrTableBuilder.finalize();
51}
52
53void MachOLayoutBuilder::updateSymbolIndexes() {
54 uint32_t Index = 0;
55 for (auto &Symbol : O.SymTable.Symbols)
56 Symbol->Index = Index++;
57}
58
59// Updates the index and the number of local/external/undefined symbols.
60void MachOLayoutBuilder::updateDySymTab(MachO::macho_load_command &MLC) {
61 assert(MLC.load_command_data.cmd == MachO::LC_DYSYMTAB);
62 // Make sure that nlist entries in the symbol table are sorted by the those
63 // types. The order is: local < defined external < undefined external.
Georgii Rymar1647ff62020-04-13 14:46:41 +030064 assert(llvm::is_sorted(O.SymTable.Symbols,
65 [](const std::unique_ptr<SymbolEntry> &A,
66 const std::unique_ptr<SymbolEntry> &B) {
67 bool AL = A->isLocalSymbol(),
68 BL = B->isLocalSymbol();
69 if (AL != BL)
70 return AL;
71 return !AL && !A->isUndefinedSymbol() &&
72 B->isUndefinedSymbol();
73 }) &&
Seiya Nuta552bcb82019-08-19 21:05:31 +000074 "Symbols are not sorted by their types.");
75
76 uint32_t NumLocalSymbols = 0;
77 auto Iter = O.SymTable.Symbols.begin();
78 auto End = O.SymTable.Symbols.end();
79 for (; Iter != End; ++Iter) {
80 if ((*Iter)->isExternalSymbol())
81 break;
82
83 ++NumLocalSymbols;
84 }
85
86 uint32_t NumExtDefSymbols = 0;
87 for (; Iter != End; ++Iter) {
88 if ((*Iter)->isUndefinedSymbol())
89 break;
90
91 ++NumExtDefSymbols;
92 }
93
94 MLC.dysymtab_command_data.ilocalsym = 0;
95 MLC.dysymtab_command_data.nlocalsym = NumLocalSymbols;
96 MLC.dysymtab_command_data.iextdefsym = NumLocalSymbols;
97 MLC.dysymtab_command_data.nextdefsym = NumExtDefSymbols;
98 MLC.dysymtab_command_data.iundefsym = NumLocalSymbols + NumExtDefSymbols;
99 MLC.dysymtab_command_data.nundefsym =
100 O.SymTable.Symbols.size() - (NumLocalSymbols + NumExtDefSymbols);
101}
102
103// Recomputes and updates offset and size fields in load commands and sections
104// since they could be modified.
105uint64_t MachOLayoutBuilder::layoutSegments() {
106 auto HeaderSize =
107 Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
Seiya Nuta12bd4902019-08-19 21:12:02 +0000108 const bool IsObjectFile =
109 O.Header.FileType == MachO::HeaderFileType::MH_OBJECT;
110 uint64_t Offset = IsObjectFile ? (HeaderSize + O.Header.SizeOfCmds) : 0;
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800111 for (LoadCommand &LC : O.LoadCommands) {
Seiya Nuta552bcb82019-08-19 21:05:31 +0000112 auto &MLC = LC.MachOLoadCommand;
113 StringRef Segname;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000114 uint64_t SegmentVmAddr;
115 uint64_t SegmentVmSize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000116 switch (MLC.load_command_data.cmd) {
117 case MachO::LC_SEGMENT:
Seiya Nuta12bd4902019-08-19 21:12:02 +0000118 SegmentVmAddr = MLC.segment_command_data.vmaddr;
119 SegmentVmSize = MLC.segment_command_data.vmsize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000120 Segname = StringRef(MLC.segment_command_data.segname,
121 strnlen(MLC.segment_command_data.segname,
122 sizeof(MLC.segment_command_data.segname)));
123 break;
124 case MachO::LC_SEGMENT_64:
Seiya Nuta12bd4902019-08-19 21:12:02 +0000125 SegmentVmAddr = MLC.segment_command_64_data.vmaddr;
126 SegmentVmSize = MLC.segment_command_64_data.vmsize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000127 Segname = StringRef(MLC.segment_command_64_data.segname,
128 strnlen(MLC.segment_command_64_data.segname,
129 sizeof(MLC.segment_command_64_data.segname)));
130 break;
131 default:
132 continue;
133 }
134
135 if (Segname == "__LINKEDIT") {
136 // We update the __LINKEDIT segment later (in layoutTail).
137 assert(LC.Sections.empty() && "__LINKEDIT segment has sections");
138 LinkEditLoadCommand = &MLC;
139 continue;
140 }
141
142 // Update file offsets and sizes of sections.
Seiya Nuta12bd4902019-08-19 21:12:02 +0000143 uint64_t SegOffset = Offset;
144 uint64_t SegFileSize = 0;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000145 uint64_t VMSize = 0;
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800146 for (std::unique_ptr<Section> &Sec : LC.Sections) {
Seiya Nuta12bd4902019-08-19 21:12:02 +0000147 if (IsObjectFile) {
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800148 if (Sec->isVirtualSection()) {
149 Sec->Offset = 0;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000150 } else {
Simon Pilgrim5a28f0a2019-08-20 10:25:57 +0000151 uint64_t PaddingSize =
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800152 offsetToAlignment(SegFileSize, Align(1ull << Sec->Align));
153 Sec->Offset = SegOffset + SegFileSize + PaddingSize;
154 Sec->Size = Sec->Content.size();
155 SegFileSize += PaddingSize + Sec->Size;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000156 }
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800157 VMSize = std::max(VMSize, Sec->Addr + Sec->Size);
Seiya Nuta12bd4902019-08-19 21:12:02 +0000158 } else {
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800159 if (Sec->isVirtualSection()) {
160 Sec->Offset = 0;
161 VMSize += Sec->Size;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000162 } else {
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800163 uint32_t SectOffset = Sec->Addr - SegmentVmAddr;
164 Sec->Offset = SegOffset + SectOffset;
165 Sec->Size = Sec->Content.size();
166 SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);
Seiya Nuta12bd4902019-08-19 21:12:02 +0000167 VMSize = std::max(VMSize, SegFileSize);
168 }
Seiya Nuta552bcb82019-08-19 21:05:31 +0000169 }
Seiya Nuta552bcb82019-08-19 21:05:31 +0000170 }
171
Seiya Nuta12bd4902019-08-19 21:12:02 +0000172 if (IsObjectFile) {
173 Offset += SegFileSize;
174 } else {
175 Offset = alignTo(Offset + SegFileSize, PageSize);
176 SegFileSize = alignTo(SegFileSize, PageSize);
177 // Use the original vmsize if the segment is __PAGEZERO.
178 VMSize =
179 Segname == "__PAGEZERO" ? SegmentVmSize : alignTo(VMSize, PageSize);
180 }
181
Seiya Nuta552bcb82019-08-19 21:05:31 +0000182 switch (MLC.load_command_data.cmd) {
183 case MachO::LC_SEGMENT:
184 MLC.segment_command_data.cmdsize =
185 sizeof(MachO::segment_command) +
186 sizeof(MachO::section) * LC.Sections.size();
187 MLC.segment_command_data.nsects = LC.Sections.size();
Seiya Nuta12bd4902019-08-19 21:12:02 +0000188 MLC.segment_command_data.fileoff = SegOffset;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000189 MLC.segment_command_data.vmsize = VMSize;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000190 MLC.segment_command_data.filesize = SegFileSize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000191 break;
192 case MachO::LC_SEGMENT_64:
193 MLC.segment_command_64_data.cmdsize =
194 sizeof(MachO::segment_command_64) +
195 sizeof(MachO::section_64) * LC.Sections.size();
196 MLC.segment_command_64_data.nsects = LC.Sections.size();
Seiya Nuta12bd4902019-08-19 21:12:02 +0000197 MLC.segment_command_64_data.fileoff = SegOffset;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000198 MLC.segment_command_64_data.vmsize = VMSize;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000199 MLC.segment_command_64_data.filesize = SegFileSize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000200 break;
201 }
Seiya Nuta552bcb82019-08-19 21:05:31 +0000202 }
203
204 return Offset;
205}
206
207uint64_t MachOLayoutBuilder::layoutRelocations(uint64_t Offset) {
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800208 for (LoadCommand &LC : O.LoadCommands)
209 for (std::unique_ptr<Section> &Sec : LC.Sections) {
210 Sec->RelOff = Sec->Relocations.empty() ? 0 : Offset;
211 Sec->NReloc = Sec->Relocations.size();
212 Offset += sizeof(MachO::any_relocation_info) * Sec->NReloc;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000213 }
214
215 return Offset;
216}
217
218Error MachOLayoutBuilder::layoutTail(uint64_t Offset) {
219 // The order of LINKEDIT elements is as follows:
220 // rebase info, binding info, weak binding info, lazy binding info, export
221 // trie, data-in-code, symbol table, indirect symbol table, symbol table
222 // strings.
223 uint64_t NListSize = Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
224 uint64_t StartOfLinkEdit = Offset;
225 uint64_t StartOfRebaseInfo = StartOfLinkEdit;
226 uint64_t StartOfBindingInfo = StartOfRebaseInfo + O.Rebases.Opcodes.size();
227 uint64_t StartOfWeakBindingInfo = StartOfBindingInfo + O.Binds.Opcodes.size();
228 uint64_t StartOfLazyBindingInfo =
229 StartOfWeakBindingInfo + O.WeakBinds.Opcodes.size();
230 uint64_t StartOfExportTrie =
231 StartOfLazyBindingInfo + O.LazyBinds.Opcodes.size();
232 uint64_t StartOfFunctionStarts = StartOfExportTrie + O.Exports.Trie.size();
233 uint64_t StartOfDataInCode =
234 StartOfFunctionStarts + O.FunctionStarts.Data.size();
235 uint64_t StartOfSymbols = StartOfDataInCode + O.DataInCode.Data.size();
236 uint64_t StartOfIndirectSymbols =
237 StartOfSymbols + NListSize * O.SymTable.Symbols.size();
238 uint64_t StartOfSymbolStrings =
239 StartOfIndirectSymbols +
240 sizeof(uint32_t) * O.IndirectSymTable.Symbols.size();
241 uint64_t LinkEditSize =
242 (StartOfSymbolStrings + StrTableBuilder.getSize()) - StartOfLinkEdit;
243
244 // Now we have determined the layout of the contents of the __LINKEDIT
245 // segment. Update its load command.
246 if (LinkEditLoadCommand) {
247 MachO::macho_load_command *MLC = LinkEditLoadCommand;
248 switch (LinkEditLoadCommand->load_command_data.cmd) {
249 case MachO::LC_SEGMENT:
250 MLC->segment_command_data.cmdsize = sizeof(MachO::segment_command);
251 MLC->segment_command_data.fileoff = StartOfLinkEdit;
252 MLC->segment_command_data.vmsize = alignTo(LinkEditSize, PageSize);
253 MLC->segment_command_data.filesize = LinkEditSize;
254 break;
255 case MachO::LC_SEGMENT_64:
256 MLC->segment_command_64_data.cmdsize = sizeof(MachO::segment_command_64);
257 MLC->segment_command_64_data.fileoff = StartOfLinkEdit;
258 MLC->segment_command_64_data.vmsize = alignTo(LinkEditSize, PageSize);
259 MLC->segment_command_64_data.filesize = LinkEditSize;
260 break;
261 }
262 }
263
Alexander Shaposhnikovdc046c72020-02-21 13:18:36 -0800264 for (LoadCommand &LC : O.LoadCommands) {
Seiya Nuta552bcb82019-08-19 21:05:31 +0000265 auto &MLC = LC.MachOLoadCommand;
266 auto cmd = MLC.load_command_data.cmd;
267 switch (cmd) {
268 case MachO::LC_SYMTAB:
269 MLC.symtab_command_data.symoff = StartOfSymbols;
270 MLC.symtab_command_data.nsyms = O.SymTable.Symbols.size();
271 MLC.symtab_command_data.stroff = StartOfSymbolStrings;
272 MLC.symtab_command_data.strsize = StrTableBuilder.getSize();
273 break;
274 case MachO::LC_DYSYMTAB: {
275 if (MLC.dysymtab_command_data.ntoc != 0 ||
276 MLC.dysymtab_command_data.nmodtab != 0 ||
277 MLC.dysymtab_command_data.nextrefsyms != 0 ||
278 MLC.dysymtab_command_data.nlocrel != 0 ||
279 MLC.dysymtab_command_data.nextrel != 0)
280 return createStringError(llvm::errc::not_supported,
281 "shared library is not yet supported");
282
283 if (!O.IndirectSymTable.Symbols.empty()) {
284 MLC.dysymtab_command_data.indirectsymoff = StartOfIndirectSymbols;
285 MLC.dysymtab_command_data.nindirectsyms =
286 O.IndirectSymTable.Symbols.size();
287 }
288
289 updateDySymTab(MLC);
290 break;
291 }
292 case MachO::LC_DATA_IN_CODE:
293 MLC.linkedit_data_command_data.dataoff = StartOfDataInCode;
294 MLC.linkedit_data_command_data.datasize = O.DataInCode.Data.size();
295 break;
296 case MachO::LC_FUNCTION_STARTS:
297 MLC.linkedit_data_command_data.dataoff = StartOfFunctionStarts;
298 MLC.linkedit_data_command_data.datasize = O.FunctionStarts.Data.size();
299 break;
300 case MachO::LC_DYLD_INFO:
301 case MachO::LC_DYLD_INFO_ONLY:
302 MLC.dyld_info_command_data.rebase_off =
303 O.Rebases.Opcodes.empty() ? 0 : StartOfRebaseInfo;
304 MLC.dyld_info_command_data.rebase_size = O.Rebases.Opcodes.size();
305 MLC.dyld_info_command_data.bind_off =
306 O.Binds.Opcodes.empty() ? 0 : StartOfBindingInfo;
307 MLC.dyld_info_command_data.bind_size = O.Binds.Opcodes.size();
308 MLC.dyld_info_command_data.weak_bind_off =
309 O.WeakBinds.Opcodes.empty() ? 0 : StartOfWeakBindingInfo;
310 MLC.dyld_info_command_data.weak_bind_size = O.WeakBinds.Opcodes.size();
311 MLC.dyld_info_command_data.lazy_bind_off =
312 O.LazyBinds.Opcodes.empty() ? 0 : StartOfLazyBindingInfo;
313 MLC.dyld_info_command_data.lazy_bind_size = O.LazyBinds.Opcodes.size();
314 MLC.dyld_info_command_data.export_off =
315 O.Exports.Trie.empty() ? 0 : StartOfExportTrie;
316 MLC.dyld_info_command_data.export_size = O.Exports.Trie.size();
317 break;
Alexander Shaposhnikovd17d50e2020-04-20 16:33:18 -0700318 // Note that LC_ENCRYPTION_INFO.cryptoff despite its name and the comment in
319 // <mach-o/loader.h> is not an offset in the binary file, instead, it is a
320 // relative virtual address. At the moment modification of the __TEXT
321 // segment of executables isn't supported anyway (e.g. data in code entries
322 // are not recalculated). Moreover, in general
323 // LC_ENCRYPT_INFO/LC_ENCRYPTION_INFO_64 are nontrivial to update because
324 // without making additional assumptions (e.g. that the entire __TEXT
325 // segment should be encrypted) we do not know how to recalculate the
326 // boundaries of the encrypted part. For now just copy over these load
327 // commands until we encounter a real world usecase where
328 // LC_ENCRYPT_INFO/LC_ENCRYPTION_INFO_64 need to be adjusted.
329 case MachO::LC_ENCRYPTION_INFO:
330 case MachO::LC_ENCRYPTION_INFO_64:
Seiya Nuta552bcb82019-08-19 21:05:31 +0000331 case MachO::LC_LOAD_DYLINKER:
332 case MachO::LC_MAIN:
333 case MachO::LC_RPATH:
334 case MachO::LC_SEGMENT:
335 case MachO::LC_SEGMENT_64:
336 case MachO::LC_VERSION_MIN_MACOSX:
Alexander Shaposhnikov074af2d2019-10-24 17:35:10 -0700337 case MachO::LC_VERSION_MIN_IPHONEOS:
338 case MachO::LC_VERSION_MIN_TVOS:
339 case MachO::LC_VERSION_MIN_WATCHOS:
Seiya Nuta552bcb82019-08-19 21:05:31 +0000340 case MachO::LC_BUILD_VERSION:
341 case MachO::LC_ID_DYLIB:
342 case MachO::LC_LOAD_DYLIB:
Alexander Shaposhnikovd987eed2020-04-23 11:37:39 -0700343 case MachO::LC_LOAD_WEAK_DYLIB:
Seiya Nuta552bcb82019-08-19 21:05:31 +0000344 case MachO::LC_UUID:
345 case MachO::LC_SOURCE_VERSION:
346 // Nothing to update.
347 break;
348 default:
349 // Abort if it's unsupported in order to prevent corrupting the object.
350 return createStringError(llvm::errc::not_supported,
351 "unsupported load command (cmd=0x%x)", cmd);
352 }
353 }
354
355 return Error::success();
356}
357
358Error MachOLayoutBuilder::layout() {
359 O.Header.NCmds = O.LoadCommands.size();
360 O.Header.SizeOfCmds = computeSizeOfCmds();
361 constructStringTable();
362 updateSymbolIndexes();
363 uint64_t Offset = layoutSegments();
364 Offset = layoutRelocations(Offset);
365 return layoutTail(Offset);
366}
367
368} // end namespace macho
369} // end namespace objcopy
370} // end namespace llvm