18MachOLayoutBuilder::getStringTableBuilderKind(
const Object &O,
bool Is64Bit) {
25uint32_t MachOLayoutBuilder::computeSizeOfCmds()
const {
27 for (
const LoadCommand &LC : O.LoadCommands) {
29 auto cmd = MLC.load_command_data.cmd;
31 case MachO::LC_SEGMENT:
32 Size +=
sizeof(MachO::segment_command) +
33 sizeof(MachO::section) * LC.
Sections.size();
35 case MachO::LC_SEGMENT_64:
36 Size +=
sizeof(MachO::segment_command_64) +
37 sizeof(MachO::section_64) * LC.
Sections.size();
42#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
44 Size += sizeof(MachO::LCStruct) + LC.Payload.size(); \
46#include "llvm/BinaryFormat/MachO.def"
47#undef HANDLE_LOAD_COMMAND
54void MachOLayoutBuilder::constructStringTable() {
55 for (std::unique_ptr<SymbolEntry> &Sym : O.SymTable.Symbols)
56 StrTableBuilder.add(Sym->Name);
57 StrTableBuilder.finalize();
60void MachOLayoutBuilder::updateSymbolIndexes() {
62 for (
auto &Symbol : O.SymTable.Symbols)
67void MachOLayoutBuilder::updateDySymTab(MachO::macho_load_command &MLC) {
68 assert(MLC.load_command_data.cmd == MachO::LC_DYSYMTAB);
72 [](
const std::unique_ptr<SymbolEntry> &
A,
73 const std::unique_ptr<SymbolEntry> &
B) {
74 bool AL = A->isLocalSymbol(),
75 BL = B->isLocalSymbol();
78 return !AL && !A->isUndefinedSymbol() &&
79 B->isUndefinedSymbol();
81 "Symbols are not sorted by their types.");
83 uint32_t NumLocalSymbols = 0;
84 auto Iter = O.SymTable.Symbols.begin();
85 auto End = O.SymTable.Symbols.end();
86 for (; Iter != End; ++Iter) {
87 if ((*Iter)->isExternalSymbol())
93 uint32_t NumExtDefSymbols = 0;
94 for (; Iter != End; ++Iter) {
95 if ((*Iter)->isUndefinedSymbol())
101 MLC.dysymtab_command_data.ilocalsym = 0;
102 MLC.dysymtab_command_data.nlocalsym = NumLocalSymbols;
103 MLC.dysymtab_command_data.iextdefsym = NumLocalSymbols;
104 MLC.dysymtab_command_data.nextdefsym = NumExtDefSymbols;
105 MLC.dysymtab_command_data.iundefsym = NumLocalSymbols + NumExtDefSymbols;
106 MLC.dysymtab_command_data.nundefsym =
107 O.SymTable.Symbols.size() - (NumLocalSymbols + NumExtDefSymbols);
112uint64_t MachOLayoutBuilder::layoutSegments() {
114 Is64Bit ?
sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
115 const bool IsObjectFile =
118 for (LoadCommand &LC : O.LoadCommands) {
119 auto &MLC = LC.MachOLoadCommand;
121 uint64_t SegmentVmAddr;
122 uint64_t SegmentVmSize;
123 switch (MLC.load_command_data.cmd) {
124 case MachO::LC_SEGMENT:
125 SegmentVmAddr = MLC.segment_command_data.vmaddr;
126 SegmentVmSize = MLC.segment_command_data.vmsize;
127 Segname = StringRef(MLC.segment_command_data.segname,
128 strnlen(MLC.segment_command_data.segname,
129 sizeof(MLC.segment_command_data.segname)));
131 case MachO::LC_SEGMENT_64:
132 SegmentVmAddr = MLC.segment_command_64_data.vmaddr;
133 SegmentVmSize = MLC.segment_command_64_data.vmsize;
134 Segname = StringRef(MLC.segment_command_64_data.segname,
135 strnlen(MLC.segment_command_64_data.segname,
136 sizeof(MLC.segment_command_64_data.segname)));
142 if (Segname ==
"__LINKEDIT") {
145 LinkEditLoadCommand = &MLC;
150 uint64_t SegOffset =
Offset;
151 uint64_t SegFileSize = 0;
153 for (std::unique_ptr<Section> &Sec : LC.
Sections) {
154 assert(SegmentVmAddr <= Sec->Addr &&
155 "Section's address cannot be smaller than Segment's one");
156 uint32_t SectOffset = Sec->Addr - SegmentVmAddr;
158 if (!Sec->hasValidOffset()) {
161 uint64_t PaddingSize =
162 offsetToAlignment(SegFileSize, Align(1ull << Sec->Align));
163 Sec->Offset = SegOffset + SegFileSize + PaddingSize;
164 Sec->Size = Sec->Content.size();
165 SegFileSize += PaddingSize + Sec->Size;
168 if (!Sec->hasValidOffset()) {
171 Sec->Offset = SegOffset + SectOffset;
172 Sec->Size = Sec->Content.size();
173 SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);
176 VMSize = std::max(VMSize, SectOffset + Sec->Size);
185 VMSize = Segname ==
"__PAGEZERO" ? SegmentVmSize
189 switch (MLC.load_command_data.cmd) {
190 case MachO::LC_SEGMENT:
191 MLC.segment_command_data.cmdsize =
192 sizeof(MachO::segment_command) +
193 sizeof(MachO::section) * LC.
Sections.size();
194 MLC.segment_command_data.nsects = LC.
Sections.size();
195 MLC.segment_command_data.fileoff = SegOffset;
196 MLC.segment_command_data.vmsize = VMSize;
197 MLC.segment_command_data.filesize = SegFileSize;
199 case MachO::LC_SEGMENT_64:
200 MLC.segment_command_64_data.cmdsize =
201 sizeof(MachO::segment_command_64) +
202 sizeof(MachO::section_64) * LC.
Sections.size();
203 MLC.segment_command_64_data.nsects = LC.
Sections.size();
204 MLC.segment_command_64_data.fileoff = SegOffset;
205 MLC.segment_command_64_data.vmsize = VMSize;
206 MLC.segment_command_64_data.filesize = SegFileSize;
214uint64_t MachOLayoutBuilder::layoutRelocations(uint64_t
Offset) {
215 for (LoadCommand &LC : O.LoadCommands)
216 for (std::unique_ptr<Section> &Sec : LC.
Sections) {
217 Sec->RelOff = Sec->Relocations.empty() ? 0 :
Offset;
218 Sec->NReloc = Sec->Relocations.size();
219 Offset +=
sizeof(MachO::any_relocation_info) * Sec->NReloc;
225Error MachOLayoutBuilder::layoutTail(uint64_t
Offset) {
232 Is64Bit ?
sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
234 Offset >= HeaderSize + O.Header.SizeOfCmds) &&
235 "Incorrect tail offset");
236 Offset = std::max(
Offset, HeaderSize + O.Header.SizeOfCmds);
238 const uint64_t LinkEditAlign = Is64Bit ? 8 : 4;
243 uint64_t DyldInfoExportsTrieRawSize = 0;
244 uint64_t DyldExportsTrieRawSize = 0;
245 for (
const auto &LC : O.LoadCommands) {
247 case MachO::LC_DYLD_INFO:
248 case MachO::LC_DYLD_INFO_ONLY:
249 DyldInfoExportsTrieRawSize = O.Exports.Trie.size();
251 case MachO::LC_DYLD_EXPORTS_TRIE:
252 DyldExportsTrieRawSize = O.Exports.Trie.size();
258 assert((DyldInfoExportsTrieRawSize == 0 || DyldExportsTrieRawSize == 0) &&
259 "Export trie in both LCs");
261 uint64_t NListSize = Is64Bit ?
sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
262 uint64_t StartOfLinkEdit =
Offset;
269 auto updateOffset = [&
Offset, LinkEditAlign](uint64_t
Size) {
270 uint64_t PreviousOffset =
Offset;
275 return std::make_pair(PreviousOffset, PaddedSize);
278 auto [StartOfRebaseInfo, RebaseInfoSize] =
279 updateOffset(O.Rebases.Opcodes.size());
280 auto [StartOfBindingInfo, BindingInfoSize] =
281 updateOffset(O.Binds.Opcodes.size());
282 auto [StartOfWeakBindingInfo, WeakBindingInfoSize] =
283 updateOffset(O.WeakBinds.Opcodes.size());
284 auto [StartOfLazyBindingInfo, LazyBindingInfoSize] =
285 updateOffset(O.LazyBinds.Opcodes.size());
286 auto [StartOfExportTrie, ExportTrieSize] =
287 updateOffset(DyldInfoExportsTrieRawSize);
288 auto [StartOfChainedFixups, ChainedFixupsSize] =
289 updateOffset(O.ChainedFixups.Data.size());
290 auto [StartOfDyldExportsTrie, DyldExportsTrieSize] =
291 updateOffset(DyldExportsTrieRawSize);
292 auto [StartOfFunctionStarts, FunctionStartsSize] =
293 updateOffset(O.FunctionStarts.Data.size());
294 auto [StartOfDataInCode, DataInCodeSize] =
295 updateOffset(O.DataInCode.Data.size());
296 auto [StartOfLinkerOptimizationHint, LinkerOptimizationHintSize] =
297 updateOffset(O.LinkerOptimizationHint.Data.size());
298 uint64_t StartOfSymbols =
299 updateOffset(NListSize * O.SymTable.Symbols.size()).first;
300 uint64_t StartOfIndirectSymbols =
301 updateOffset(
sizeof(uint32_t) * O.IndirectSymTable.Symbols.size()).first;
302 auto [StartOfSymbolStrings, SymbolStringsSize] =
303 updateOffset(StrTableBuilder.getSize());
304 auto [StartOfDylibCodeSignDRs, DylibCodeSignDRsSize] =
305 updateOffset(O.DylibCodeSignDRs.Data.size());
307 uint64_t StartOfCodeSignature =
Offset;
308 uint32_t CodeSignatureSize = 0;
309 if (O.CodeSignatureCommandIndex) {
315 CodeSignature.FixedHeadersSize + OutputFileName.size() + 1,
316 CodeSignature.Align);
317 const uint32_t BlockCount =
318 (StartOfCodeSignature + CodeSignature.BlockSize - 1) /
319 CodeSignature.BlockSize;
320 const uint32_t
Size =
322 CodeSignature.Align);
324 CodeSignature.StartOffset = StartOfCodeSignature;
325 CodeSignature.AllHeadersSize = AllHeadersSize;
326 CodeSignature.BlockCount = BlockCount;
327 CodeSignature.OutputFileName = OutputFileName;
328 CodeSignature.Size =
Size;
329 CodeSignatureSize =
Size;
331 uint64_t LinkEditSize =
332 StartOfCodeSignature + CodeSignatureSize - StartOfLinkEdit;
336 if (LinkEditLoadCommand) {
337 MachO::macho_load_command *MLC = LinkEditLoadCommand;
338 switch (LinkEditLoadCommand->load_command_data.cmd) {
339 case MachO::LC_SEGMENT:
340 MLC->segment_command_data.cmdsize =
sizeof(MachO::segment_command);
341 MLC->segment_command_data.fileoff = StartOfLinkEdit;
342 MLC->segment_command_data.vmsize =
344 MLC->segment_command_data.filesize = LinkEditSize;
346 case MachO::LC_SEGMENT_64:
347 MLC->segment_command_64_data.cmdsize =
sizeof(MachO::segment_command_64);
348 MLC->segment_command_64_data.fileoff = StartOfLinkEdit;
349 MLC->segment_command_64_data.vmsize =
351 MLC->segment_command_64_data.filesize = LinkEditSize;
356 for (LoadCommand &LC : O.LoadCommands) {
358 auto cmd = MLC.load_command_data.cmd;
360 case MachO::LC_CODE_SIGNATURE:
361 MLC.linkedit_data_command_data.dataoff = StartOfCodeSignature;
362 MLC.linkedit_data_command_data.datasize = CodeSignatureSize;
364 case MachO::LC_DYLIB_CODE_SIGN_DRS:
365 MLC.linkedit_data_command_data.dataoff = StartOfDylibCodeSignDRs;
366 MLC.linkedit_data_command_data.datasize = DylibCodeSignDRsSize;
368 case MachO::LC_SYMTAB:
369 MLC.symtab_command_data.symoff = StartOfSymbols;
370 MLC.symtab_command_data.nsyms = O.SymTable.Symbols.size();
371 MLC.symtab_command_data.stroff = StartOfSymbolStrings;
372 MLC.symtab_command_data.strsize = SymbolStringsSize;
374 case MachO::LC_DYSYMTAB: {
375 if (MLC.dysymtab_command_data.ntoc != 0 ||
376 MLC.dysymtab_command_data.nmodtab != 0 ||
377 MLC.dysymtab_command_data.nextrefsyms != 0 ||
378 MLC.dysymtab_command_data.nlocrel != 0 ||
379 MLC.dysymtab_command_data.nextrel != 0)
381 "shared library is not yet supported");
382 MLC.dysymtab_command_data.indirectsymoff =
383 O.IndirectSymTable.Symbols.size() ? StartOfIndirectSymbols : 0;
384 MLC.dysymtab_command_data.nindirectsyms =
385 O.IndirectSymTable.Symbols.size();
389 case MachO::LC_DATA_IN_CODE:
390 MLC.linkedit_data_command_data.dataoff = StartOfDataInCode;
391 MLC.linkedit_data_command_data.datasize = DataInCodeSize;
393 case MachO::LC_LINKER_OPTIMIZATION_HINT:
394 MLC.linkedit_data_command_data.dataoff = StartOfLinkerOptimizationHint;
395 MLC.linkedit_data_command_data.datasize = LinkerOptimizationHintSize;
397 case MachO::LC_FUNCTION_STARTS:
398 MLC.linkedit_data_command_data.dataoff = StartOfFunctionStarts;
399 MLC.linkedit_data_command_data.datasize = FunctionStartsSize;
401 case MachO::LC_DYLD_CHAINED_FIXUPS:
402 MLC.linkedit_data_command_data.dataoff = StartOfChainedFixups;
403 MLC.linkedit_data_command_data.datasize = ChainedFixupsSize;
405 case MachO::LC_DYLD_EXPORTS_TRIE:
406 MLC.linkedit_data_command_data.dataoff = StartOfDyldExportsTrie;
407 MLC.linkedit_data_command_data.datasize = DyldExportsTrieSize;
409 case MachO::LC_DYLD_INFO:
410 case MachO::LC_DYLD_INFO_ONLY:
411 MLC.dyld_info_command_data.rebase_off =
412 O.Rebases.Opcodes.empty() ? 0 : StartOfRebaseInfo;
413 MLC.dyld_info_command_data.rebase_size = RebaseInfoSize;
414 MLC.dyld_info_command_data.bind_off =
415 O.Binds.Opcodes.empty() ? 0 : StartOfBindingInfo;
416 MLC.dyld_info_command_data.bind_size = BindingInfoSize;
417 MLC.dyld_info_command_data.weak_bind_off =
418 O.WeakBinds.Opcodes.empty() ? 0 : StartOfWeakBindingInfo;
419 MLC.dyld_info_command_data.weak_bind_size = WeakBindingInfoSize;
420 MLC.dyld_info_command_data.lazy_bind_off =
421 O.LazyBinds.Opcodes.empty() ? 0 : StartOfLazyBindingInfo;
422 MLC.dyld_info_command_data.lazy_bind_size = LazyBindingInfoSize;
423 MLC.dyld_info_command_data.export_off =
424 O.Exports.Trie.empty() ? 0 : StartOfExportTrie;
425 MLC.dyld_info_command_data.export_size = ExportTrieSize;
438 case MachO::LC_ENCRYPTION_INFO:
439 case MachO::LC_ENCRYPTION_INFO_64:
440 case MachO::LC_LOAD_DYLINKER:
442 case MachO::LC_RPATH:
443 case MachO::LC_SEGMENT:
444 case MachO::LC_SEGMENT_64:
445 case MachO::LC_VERSION_MIN_MACOSX:
446 case MachO::LC_VERSION_MIN_IPHONEOS:
447 case MachO::LC_VERSION_MIN_TVOS:
448 case MachO::LC_VERSION_MIN_WATCHOS:
449 case MachO::LC_BUILD_VERSION:
450 case MachO::LC_ID_DYLIB:
451 case MachO::LC_LOAD_DYLIB:
452 case MachO::LC_LOAD_WEAK_DYLIB:
454 case MachO::LC_SOURCE_VERSION:
455 case MachO::LC_THREAD:
456 case MachO::LC_UNIXTHREAD:
457 case MachO::LC_SUB_FRAMEWORK:
458 case MachO::LC_SUB_UMBRELLA:
459 case MachO::LC_SUB_CLIENT:
460 case MachO::LC_SUB_LIBRARY:
461 case MachO::LC_LINKER_OPTION:
467 "unsupported load command (cmd=0x%x)", cmd);
475 O.Header.NCmds = O.LoadCommands.size();
476 O.Header.SizeOfCmds = computeSizeOfCmds();
477 constructStringTable();
478 updateSymbolIndexes();
481 return layoutTail(
Offset);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
This is an optimization pass for GlobalISel generic memory operations.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
MachO::macho_load_command MachOLoadCommand
std::vector< std::unique_ptr< Section > > Sections