18MachOLayoutBuilder::getStringTableBuilderKind(
const Object &O,
bool Is64Bit) {
25uint32_t MachOLayoutBuilder::computeSizeOfCmds()
const {
29 auto cmd = MLC.load_command_data.cmd;
31 case MachO::LC_SEGMENT:
35 case MachO::LC_SEGMENT_64:
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);
60void MachOLayoutBuilder::updateSymbolIndexes() {
62 for (
auto &Symbol :
O.SymTable.Symbols)
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.");
84 auto Iter =
O.SymTable.Symbols.begin();
85 auto End =
O.SymTable.Symbols.end();
86 for (; Iter !=
End; ++Iter) {
87 if ((*Iter)->isExternalSymbol())
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() {
115 const bool IsObjectFile =
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;
153 for (std::unique_ptr<Section> &Sec : LC.
Sections) {
155 "Section's address cannot be smaller than Segment's one");
156 uint32_t SectOffset = Sec->Addr - SegmentVmAddr;
158 if (!Sec->hasValidOffset()) {
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);
183 SegFileSize =
alignTo(SegFileSize, PageSize);
186 Segname ==
"__PAGEZERO" ? SegmentVmSize :
alignTo(VMSize, PageSize);
189 switch (MLC.load_command_data.cmd) {
190 case MachO::LC_SEGMENT:
191 MLC.segment_command_data.cmdsize =
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 =
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;
216 for (std::unique_ptr<Section> &Sec : LC.
Sections) {
217 Sec->RelOff = Sec->Relocations.empty() ? 0 :
Offset;
218 Sec->NReloc = Sec->Relocations.size();
234 Offset >= HeaderSize +
O.Header.SizeOfCmds) &&
235 "Incorrect tail offset");
240 size_t DyldInfoExportsTrieSize = 0;
241 size_t DyldExportsTrieSize = 0;
242 for (
const auto &LC :
O.LoadCommands) {
244 case MachO::LC_DYLD_INFO:
245 case MachO::LC_DYLD_INFO_ONLY:
246 DyldInfoExportsTrieSize =
O.Exports.Trie.size();
248 case MachO::LC_DYLD_EXPORTS_TRIE:
249 DyldExportsTrieSize =
O.Exports.Trie.size();
255 assert((DyldInfoExportsTrieSize == 0 || DyldExportsTrieSize == 0) &&
256 "Export trie in both LCs");
269 return PreviousOffset;
272 uint64_t StartOfRebaseInfo = updateOffset(
O.Rebases.Opcodes.size());
273 uint64_t StartOfBindingInfo = updateOffset(
O.Binds.Opcodes.size());
274 uint64_t StartOfWeakBindingInfo = updateOffset(
O.WeakBinds.Opcodes.size());
275 uint64_t StartOfLazyBindingInfo = updateOffset(
O.LazyBinds.Opcodes.size());
276 uint64_t StartOfExportTrie = updateOffset(DyldInfoExportsTrieSize);
277 uint64_t StartOfChainedFixups = updateOffset(
O.ChainedFixups.Data.size());
278 uint64_t StartOfDyldExportsTrie = updateOffset(DyldExportsTrieSize);
279 uint64_t StartOfFunctionStarts = updateOffset(
O.FunctionStarts.Data.size());
280 uint64_t StartOfDataInCode = updateOffset(
O.DataInCode.Data.size());
281 uint64_t StartOfLinkerOptimizationHint =
282 updateOffset(
O.LinkerOptimizationHint.Data.size());
283 uint64_t StartOfSymbols = updateOffset(NListSize *
O.SymTable.Symbols.size());
285 updateOffset(
sizeof(
uint32_t) *
O.IndirectSymTable.Symbols.size());
286 uint64_t StartOfSymbolStrings = updateOffset(StrTableBuilder.
getSize());
287 uint64_t StartOfDylibCodeSignDRs = updateOffset(
O.DylibCodeSignDRs.Data.size());
291 if (
O.CodeSignatureCommandIndex) {
292 StartOfCodeSignature =
alignTo(StartOfCodeSignature, 16);
298 CodeSignature.
Align);
300 (StartOfCodeSignature + CodeSignature.
BlockSize - 1) /
304 CodeSignature.
Align);
311 CodeSignatureSize =
Size;
314 StartOfCodeSignature + CodeSignatureSize - StartOfLinkEdit;
318 if (LinkEditLoadCommand) {
320 switch (LinkEditLoadCommand->load_command_data.cmd) {
321 case MachO::LC_SEGMENT:
323 MLC->segment_command_data.fileoff = StartOfLinkEdit;
324 MLC->segment_command_data.vmsize =
alignTo(LinkEditSize, PageSize);
325 MLC->segment_command_data.filesize = LinkEditSize;
327 case MachO::LC_SEGMENT_64:
329 MLC->segment_command_64_data.fileoff = StartOfLinkEdit;
330 MLC->segment_command_64_data.vmsize =
alignTo(LinkEditSize, PageSize);
331 MLC->segment_command_64_data.filesize = LinkEditSize;
338 auto cmd = MLC.load_command_data.cmd;
340 case MachO::LC_CODE_SIGNATURE:
341 MLC.linkedit_data_command_data.dataoff = StartOfCodeSignature;
342 MLC.linkedit_data_command_data.datasize = CodeSignatureSize;
344 case MachO::LC_DYLIB_CODE_SIGN_DRS:
345 MLC.linkedit_data_command_data.dataoff = StartOfDylibCodeSignDRs;
346 MLC.linkedit_data_command_data.datasize =
O.DylibCodeSignDRs.Data.size();
348 case MachO::LC_SYMTAB:
349 MLC.symtab_command_data.symoff = StartOfSymbols;
350 MLC.symtab_command_data.nsyms =
O.SymTable.Symbols.size();
351 MLC.symtab_command_data.stroff = StartOfSymbolStrings;
352 MLC.symtab_command_data.strsize = StrTableBuilder.
getSize();
354 case MachO::LC_DYSYMTAB: {
355 if (MLC.dysymtab_command_data.ntoc != 0 ||
356 MLC.dysymtab_command_data.nmodtab != 0 ||
357 MLC.dysymtab_command_data.nextrefsyms != 0 ||
358 MLC.dysymtab_command_data.nlocrel != 0 ||
359 MLC.dysymtab_command_data.nextrel != 0)
361 "shared library is not yet supported");
363 if (!
O.IndirectSymTable.Symbols.empty()) {
364 MLC.dysymtab_command_data.indirectsymoff = StartOfIndirectSymbols;
365 MLC.dysymtab_command_data.nindirectsyms =
366 O.IndirectSymTable.Symbols.size();
372 case MachO::LC_DATA_IN_CODE:
373 MLC.linkedit_data_command_data.dataoff = StartOfDataInCode;
374 MLC.linkedit_data_command_data.datasize =
O.DataInCode.Data.size();
376 case MachO::LC_LINKER_OPTIMIZATION_HINT:
377 MLC.linkedit_data_command_data.dataoff = StartOfLinkerOptimizationHint;
378 MLC.linkedit_data_command_data.datasize =
379 O.LinkerOptimizationHint.Data.size();
381 case MachO::LC_FUNCTION_STARTS:
382 MLC.linkedit_data_command_data.dataoff = StartOfFunctionStarts;
383 MLC.linkedit_data_command_data.datasize =
O.FunctionStarts.Data.size();
385 case MachO::LC_DYLD_CHAINED_FIXUPS:
386 MLC.linkedit_data_command_data.dataoff = StartOfChainedFixups;
387 MLC.linkedit_data_command_data.datasize =
O.ChainedFixups.Data.size();
389 case MachO::LC_DYLD_EXPORTS_TRIE:
390 MLC.linkedit_data_command_data.dataoff = StartOfDyldExportsTrie;
391 MLC.linkedit_data_command_data.datasize = DyldExportsTrieSize;
393 case MachO::LC_DYLD_INFO:
394 case MachO::LC_DYLD_INFO_ONLY:
395 MLC.dyld_info_command_data.rebase_off =
396 O.Rebases.Opcodes.empty() ? 0 : StartOfRebaseInfo;
397 MLC.dyld_info_command_data.rebase_size =
O.Rebases.Opcodes.size();
398 MLC.dyld_info_command_data.bind_off =
399 O.Binds.Opcodes.empty() ? 0 : StartOfBindingInfo;
400 MLC.dyld_info_command_data.bind_size =
O.Binds.Opcodes.size();
401 MLC.dyld_info_command_data.weak_bind_off =
402 O.WeakBinds.Opcodes.empty() ? 0 : StartOfWeakBindingInfo;
403 MLC.dyld_info_command_data.weak_bind_size =
O.WeakBinds.Opcodes.size();
404 MLC.dyld_info_command_data.lazy_bind_off =
405 O.LazyBinds.Opcodes.empty() ? 0 : StartOfLazyBindingInfo;
406 MLC.dyld_info_command_data.lazy_bind_size =
O.LazyBinds.Opcodes.size();
407 MLC.dyld_info_command_data.export_off =
408 O.Exports.Trie.empty() ? 0 : StartOfExportTrie;
409 MLC.dyld_info_command_data.export_size = DyldInfoExportsTrieSize;
422 case MachO::LC_ENCRYPTION_INFO:
423 case MachO::LC_ENCRYPTION_INFO_64:
424 case MachO::LC_LOAD_DYLINKER:
426 case MachO::LC_RPATH:
427 case MachO::LC_SEGMENT:
428 case MachO::LC_SEGMENT_64:
429 case MachO::LC_VERSION_MIN_MACOSX:
430 case MachO::LC_VERSION_MIN_IPHONEOS:
431 case MachO::LC_VERSION_MIN_TVOS:
432 case MachO::LC_VERSION_MIN_WATCHOS:
433 case MachO::LC_BUILD_VERSION:
434 case MachO::LC_ID_DYLIB:
435 case MachO::LC_LOAD_DYLIB:
436 case MachO::LC_LOAD_WEAK_DYLIB:
438 case MachO::LC_SOURCE_VERSION:
439 case MachO::LC_THREAD:
440 case MachO::LC_UNIXTHREAD:
441 case MachO::LC_SUB_FRAMEWORK:
442 case MachO::LC_SUB_UMBRELLA:
443 case MachO::LC_SUB_CLIENT:
444 case MachO::LC_SUB_LIBRARY:
445 case MachO::LC_LINKER_OPTION:
451 "unsupported load command (cmd=0x%x)", cmd);
459 O.Header.NCmds = O.LoadCommands.size();
460 O.Header.SizeOfCmds = computeSizeOfCmds();
461 constructStringTable();
462 updateSymbolIndexes();
465 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