19MachOLayoutBuilder::getStringTableBuilderKind(
const Object &O,
bool Is64Bit) {
26uint32_t MachOLayoutBuilder::computeSizeOfCmds()
const {
30 auto cmd = MLC.load_command_data.cmd;
32 case MachO::LC_SEGMENT:
36 case MachO::LC_SEGMENT_64:
43#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
45 Size += sizeof(MachO::LCStruct) + LC.Payload.size(); \
47#include "llvm/BinaryFormat/MachO.def"
48#undef HANDLE_LOAD_COMMAND
55void MachOLayoutBuilder::constructStringTable() {
56 for (std::unique_ptr<SymbolEntry> &
Sym :
O.SymTable.Symbols)
57 StrTableBuilder.
add(
Sym->Name);
61void MachOLayoutBuilder::updateSymbolIndexes() {
63 for (
auto &Symbol :
O.SymTable.Symbols)
69 assert(MLC.load_command_data.cmd == MachO::LC_DYSYMTAB);
73 [](
const std::unique_ptr<SymbolEntry> &
A,
74 const std::unique_ptr<SymbolEntry> &
B) {
75 bool AL = A->isLocalSymbol(),
76 BL = B->isLocalSymbol();
79 return !AL && !A->isUndefinedSymbol() &&
80 B->isUndefinedSymbol();
82 "Symbols are not sorted by their types.");
85 auto Iter =
O.SymTable.Symbols.begin();
86 auto End =
O.SymTable.Symbols.end();
87 for (; Iter !=
End; ++Iter) {
88 if ((*Iter)->isExternalSymbol())
95 for (; Iter !=
End; ++Iter) {
96 if ((*Iter)->isUndefinedSymbol())
102 MLC.dysymtab_command_data.ilocalsym = 0;
103 MLC.dysymtab_command_data.nlocalsym = NumLocalSymbols;
104 MLC.dysymtab_command_data.iextdefsym = NumLocalSymbols;
105 MLC.dysymtab_command_data.nextdefsym = NumExtDefSymbols;
106 MLC.dysymtab_command_data.iundefsym = NumLocalSymbols + NumExtDefSymbols;
107 MLC.dysymtab_command_data.nundefsym =
108 O.SymTable.Symbols.size() - (NumLocalSymbols + NumExtDefSymbols);
113uint64_t MachOLayoutBuilder::layoutSegments() {
116 const bool IsObjectFile =
124 switch (MLC.load_command_data.cmd) {
125 case MachO::LC_SEGMENT:
126 SegmentVmAddr = MLC.segment_command_data.vmaddr;
127 SegmentVmSize = MLC.segment_command_data.vmsize;
128 Segname =
StringRef(MLC.segment_command_data.segname,
129 strnlen(MLC.segment_command_data.segname,
130 sizeof(MLC.segment_command_data.segname)));
132 case MachO::LC_SEGMENT_64:
133 SegmentVmAddr = MLC.segment_command_64_data.vmaddr;
134 SegmentVmSize = MLC.segment_command_64_data.vmsize;
135 Segname =
StringRef(MLC.segment_command_64_data.segname,
136 strnlen(MLC.segment_command_64_data.segname,
137 sizeof(MLC.segment_command_64_data.segname)));
143 if (Segname ==
"__LINKEDIT") {
146 LinkEditLoadCommand = &MLC;
154 for (std::unique_ptr<Section> &Sec : LC.
Sections) {
156 "Section's address cannot be smaller than Segment's one");
157 uint32_t SectOffset = Sec->Addr - SegmentVmAddr;
159 if (!Sec->hasValidOffset()) {
164 Sec->Offset = SegOffset + SegFileSize + PaddingSize;
165 Sec->Size = Sec->Content.size();
166 SegFileSize += PaddingSize + Sec->Size;
169 if (!Sec->hasValidOffset()) {
172 Sec->Offset = SegOffset + SectOffset;
173 Sec->Size = Sec->Content.size();
174 SegFileSize = std::max(SegFileSize, SectOffset + Sec->Size);
177 VMSize = std::max(VMSize, SectOffset + Sec->Size);
184 SegFileSize =
alignTo(SegFileSize, PageSize);
187 Segname ==
"__PAGEZERO" ? SegmentVmSize :
alignTo(VMSize, PageSize);
190 switch (MLC.load_command_data.cmd) {
191 case MachO::LC_SEGMENT:
192 MLC.segment_command_data.cmdsize =
195 MLC.segment_command_data.nsects = LC.
Sections.size();
196 MLC.segment_command_data.fileoff = SegOffset;
197 MLC.segment_command_data.vmsize = VMSize;
198 MLC.segment_command_data.filesize = SegFileSize;
200 case MachO::LC_SEGMENT_64:
201 MLC.segment_command_64_data.cmdsize =
204 MLC.segment_command_64_data.nsects = LC.
Sections.size();
205 MLC.segment_command_64_data.fileoff = SegOffset;
206 MLC.segment_command_64_data.vmsize = VMSize;
207 MLC.segment_command_64_data.filesize = SegFileSize;
217 for (std::unique_ptr<Section> &Sec : LC.
Sections) {
218 Sec->RelOff = Sec->Relocations.empty() ? 0 :
Offset;
219 Sec->NReloc = Sec->Relocations.size();
235 Offset >= HeaderSize +
O.Header.SizeOfCmds) &&
236 "Incorrect tail offset");
241 size_t DyldInfoExportsTrieSize = 0;
242 size_t DyldExportsTrieSize = 0;
243 for (
const auto &LC :
O.LoadCommands) {
245 case MachO::LC_DYLD_INFO:
246 case MachO::LC_DYLD_INFO_ONLY:
247 DyldInfoExportsTrieSize =
O.Exports.Trie.size();
249 case MachO::LC_DYLD_EXPORTS_TRIE:
250 DyldExportsTrieSize =
O.Exports.Trie.size();
256 assert((DyldInfoExportsTrieSize == 0 || DyldExportsTrieSize == 0) &&
257 "Export trie in both LCs");
270 return PreviousOffset;
273 uint64_t StartOfRebaseInfo = updateOffset(
O.Rebases.Opcodes.size());
274 uint64_t StartOfBindingInfo = updateOffset(
O.Binds.Opcodes.size());
275 uint64_t StartOfWeakBindingInfo = updateOffset(
O.WeakBinds.Opcodes.size());
276 uint64_t StartOfLazyBindingInfo = updateOffset(
O.LazyBinds.Opcodes.size());
277 uint64_t StartOfExportTrie = updateOffset(DyldInfoExportsTrieSize);
278 uint64_t StartOfChainedFixups = updateOffset(
O.ChainedFixups.Data.size());
279 uint64_t StartOfDyldExportsTrie = updateOffset(DyldExportsTrieSize);
280 uint64_t StartOfFunctionStarts = updateOffset(
O.FunctionStarts.Data.size());
281 uint64_t StartOfDataInCode = updateOffset(
O.DataInCode.Data.size());
282 uint64_t StartOfLinkerOptimizationHint =
283 updateOffset(
O.LinkerOptimizationHint.Data.size());
284 uint64_t StartOfSymbols = updateOffset(NListSize *
O.SymTable.Symbols.size());
286 updateOffset(
sizeof(
uint32_t) *
O.IndirectSymTable.Symbols.size());
287 uint64_t StartOfSymbolStrings = updateOffset(StrTableBuilder.
getSize());
288 uint64_t StartOfDylibCodeSignDRs = updateOffset(
O.DylibCodeSignDRs.Data.size());
292 if (
O.CodeSignatureCommandIndex) {
293 StartOfCodeSignature =
alignTo(StartOfCodeSignature, 16);
299 CodeSignature.
Align);
301 (StartOfCodeSignature + CodeSignature.
BlockSize - 1) /
305 CodeSignature.
Align);
312 CodeSignatureSize =
Size;
315 StartOfCodeSignature + CodeSignatureSize - StartOfLinkEdit;
319 if (LinkEditLoadCommand) {
321 switch (LinkEditLoadCommand->load_command_data.cmd) {
322 case MachO::LC_SEGMENT:
324 MLC->segment_command_data.fileoff = StartOfLinkEdit;
325 MLC->segment_command_data.vmsize =
alignTo(LinkEditSize, PageSize);
326 MLC->segment_command_data.filesize = LinkEditSize;
328 case MachO::LC_SEGMENT_64:
330 MLC->segment_command_64_data.fileoff = StartOfLinkEdit;
331 MLC->segment_command_64_data.vmsize =
alignTo(LinkEditSize, PageSize);
332 MLC->segment_command_64_data.filesize = LinkEditSize;
339 auto cmd = MLC.load_command_data.cmd;
341 case MachO::LC_CODE_SIGNATURE:
342 MLC.linkedit_data_command_data.dataoff = StartOfCodeSignature;
343 MLC.linkedit_data_command_data.datasize = CodeSignatureSize;
345 case MachO::LC_DYLIB_CODE_SIGN_DRS:
346 MLC.linkedit_data_command_data.dataoff = StartOfDylibCodeSignDRs;
347 MLC.linkedit_data_command_data.datasize =
O.DylibCodeSignDRs.Data.size();
349 case MachO::LC_SYMTAB:
350 MLC.symtab_command_data.symoff = StartOfSymbols;
351 MLC.symtab_command_data.nsyms =
O.SymTable.Symbols.size();
352 MLC.symtab_command_data.stroff = StartOfSymbolStrings;
353 MLC.symtab_command_data.strsize = StrTableBuilder.
getSize();
355 case MachO::LC_DYSYMTAB: {
356 if (MLC.dysymtab_command_data.ntoc != 0 ||
357 MLC.dysymtab_command_data.nmodtab != 0 ||
358 MLC.dysymtab_command_data.nextrefsyms != 0 ||
359 MLC.dysymtab_command_data.nlocrel != 0 ||
360 MLC.dysymtab_command_data.nextrel != 0)
362 "shared library is not yet supported");
364 if (!
O.IndirectSymTable.Symbols.empty()) {
365 MLC.dysymtab_command_data.indirectsymoff = StartOfIndirectSymbols;
366 MLC.dysymtab_command_data.nindirectsyms =
367 O.IndirectSymTable.Symbols.size();
373 case MachO::LC_DATA_IN_CODE:
374 MLC.linkedit_data_command_data.dataoff = StartOfDataInCode;
375 MLC.linkedit_data_command_data.datasize =
O.DataInCode.Data.size();
377 case MachO::LC_LINKER_OPTIMIZATION_HINT:
378 MLC.linkedit_data_command_data.dataoff = StartOfLinkerOptimizationHint;
379 MLC.linkedit_data_command_data.datasize =
380 O.LinkerOptimizationHint.Data.size();
382 case MachO::LC_FUNCTION_STARTS:
383 MLC.linkedit_data_command_data.dataoff = StartOfFunctionStarts;
384 MLC.linkedit_data_command_data.datasize =
O.FunctionStarts.Data.size();
386 case MachO::LC_DYLD_CHAINED_FIXUPS:
387 MLC.linkedit_data_command_data.dataoff = StartOfChainedFixups;
388 MLC.linkedit_data_command_data.datasize =
O.ChainedFixups.Data.size();
390 case MachO::LC_DYLD_EXPORTS_TRIE:
391 MLC.linkedit_data_command_data.dataoff = StartOfDyldExportsTrie;
392 MLC.linkedit_data_command_data.datasize = DyldExportsTrieSize;
394 case MachO::LC_DYLD_INFO:
395 case MachO::LC_DYLD_INFO_ONLY:
396 MLC.dyld_info_command_data.rebase_off =
397 O.Rebases.Opcodes.empty() ? 0 : StartOfRebaseInfo;
398 MLC.dyld_info_command_data.rebase_size =
O.Rebases.Opcodes.size();
399 MLC.dyld_info_command_data.bind_off =
400 O.Binds.Opcodes.empty() ? 0 : StartOfBindingInfo;
401 MLC.dyld_info_command_data.bind_size =
O.Binds.Opcodes.size();
402 MLC.dyld_info_command_data.weak_bind_off =
403 O.WeakBinds.Opcodes.empty() ? 0 : StartOfWeakBindingInfo;
404 MLC.dyld_info_command_data.weak_bind_size =
O.WeakBinds.Opcodes.size();
405 MLC.dyld_info_command_data.lazy_bind_off =
406 O.LazyBinds.Opcodes.empty() ? 0 : StartOfLazyBindingInfo;
407 MLC.dyld_info_command_data.lazy_bind_size =
O.LazyBinds.Opcodes.size();
408 MLC.dyld_info_command_data.export_off =
409 O.Exports.Trie.empty() ? 0 : StartOfExportTrie;
410 MLC.dyld_info_command_data.export_size = DyldInfoExportsTrieSize;
423 case MachO::LC_ENCRYPTION_INFO:
424 case MachO::LC_ENCRYPTION_INFO_64:
425 case MachO::LC_LOAD_DYLINKER:
427 case MachO::LC_RPATH:
428 case MachO::LC_SEGMENT:
429 case MachO::LC_SEGMENT_64:
430 case MachO::LC_VERSION_MIN_MACOSX:
431 case MachO::LC_VERSION_MIN_IPHONEOS:
432 case MachO::LC_VERSION_MIN_TVOS:
433 case MachO::LC_VERSION_MIN_WATCHOS:
434 case MachO::LC_BUILD_VERSION:
435 case MachO::LC_ID_DYLIB:
436 case MachO::LC_LOAD_DYLIB:
437 case MachO::LC_LOAD_WEAK_DYLIB:
439 case MachO::LC_SOURCE_VERSION:
440 case MachO::LC_THREAD:
441 case MachO::LC_UNIXTHREAD:
442 case MachO::LC_SUB_FRAMEWORK:
443 case MachO::LC_SUB_UMBRELLA:
444 case MachO::LC_SUB_CLIENT:
445 case MachO::LC_SUB_LIBRARY:
446 case MachO::LC_LINKER_OPTION:
452 "unsupported load command (cmd=0x%x)", cmd);
460 O.Header.NCmds = O.LoadCommands.size();
461 O.Header.SizeOfCmds = computeSizeOfCmds();
462 constructStringTable();
463 updateSymbolIndexes();
466 return layoutTail(
Offset);
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
StringRef - Represent a constant reference to a string, i.e.
constexpr size_t size() const
size - Get the string size.
size_t add(CachedHashStringRef S)
Add a string to the builder.
void finalize()
Analyze the strings and build the final table.
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.
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...
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
This struct is a compact representation of a valid (non-zero power of two) alignment.
static constexpr size_t HashSize
static constexpr uint32_t FixedHeadersSize
static constexpr size_t BlockSize
static constexpr uint32_t Align
MachO::macho_load_command MachOLoadCommand
std::vector< std::unique_ptr< Section > > Sections