1 //===- InputFiles.cpp -----------------------------------------------------===//
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 // This file contains functions to parse Mach-O object files. In this comment,
10 // we describe the Mach-O file structure and how we parse it.
11 //
12 // Mach-O is not very different from ELF or COFF. The notion of symbols,
13 // sections and relocations exists in Mach-O as it does in ELF and COFF.
14 //
15 // Perhaps the notion that is new to those who know ELF/COFF is "subsections".
16 // In ELF/COFF, sections are an atomic unit of data copied from input files to
17 // output files. When we merge or garbage-collect sections, we treat each
18 // section as an atomic unit. In Mach-O, that's not the case. Sections can
19 // consist of multiple subsections, and subsections are a unit of merging and
20 // garbage-collecting. Therefore, Mach-O's subsections are more similar to
21 // ELF/COFF's sections than Mach-O's sections are.
22 //
23 // A section can have multiple symbols. A symbol that does not have the
24 // N_ALT_ENTRY attribute indicates a beginning of a subsection. Therefore, by
25 // definition, a symbol is always present at the beginning of each subsection. A
26 // symbol with N_ALT_ENTRY attribute does not start a new subsection and can
27 // point to a middle of a subsection.
28 //
29 // The notion of subsections also affects how relocations are represented in
30 // Mach-O. All references within a section need to be explicitly represented as
31 // relocations if they refer to different subsections, because we obviously need
32 // to fix up addresses if subsections are laid out in an output file differently
33 // than they were in object files. To represent that, Mach-O relocations can
34 // refer to an unnamed location via its address. Scattered relocations (those
35 // with the R_SCATTERED bit set) always refer to unnamed locations.
36 // Non-scattered relocations refer to an unnamed location if r_extern is not set
37 // and r_symbolnum is zero.
38 //
39 // Without the above differences, I think you can use your knowledge about ELF
40 // and COFF for Mach-O.
41 //
42 //===----------------------------------------------------------------------===//
43 
44 #include "InputFiles.h"
45 #include "Config.h"
46 #include "Driver.h"
47 #include "Dwarf.h"
48 #include "ExportTrie.h"
49 #include "InputSection.h"
50 #include "MachOStructs.h"
51 #include "ObjC.h"
52 #include "OutputSection.h"
53 #include "OutputSegment.h"
54 #include "SymbolTable.h"
55 #include "Symbols.h"
56 #include "Target.h"
57 
58 #include "lld/Common/DWARF.h"
59 #include "lld/Common/ErrorHandler.h"
60 #include "lld/Common/Memory.h"
61 #include "lld/Common/Reproduce.h"
62 #include "llvm/ADT/iterator.h"
63 #include "llvm/BinaryFormat/MachO.h"
64 #include "llvm/LTO/LTO.h"
65 #include "llvm/Support/Endian.h"
66 #include "llvm/Support/MemoryBuffer.h"
67 #include "llvm/Support/Path.h"
68 #include "llvm/Support/TarWriter.h"
69 
70 using namespace llvm;
71 using namespace llvm::MachO;
72 using namespace llvm::support::endian;
73 using namespace llvm::sys;
74 using namespace lld;
75 using namespace lld::macho;
76 
77 // Returns "<internal>", "foo.a(bar.o)", or "baz.o".
78 std::string lld::toString(const InputFile *f) {
79   if (!f)
80     return "<internal>";
81   if (f->archiveName.empty())
82     return std::string(f->getName());
83   return (path::filename(f->archiveName) + "(" + path::filename(f->getName()) +
84           ")")
85       .str();
86 }
87 
88 SetVector<InputFile *> macho::inputFiles;
89 std::unique_ptr<TarWriter> macho::tar;
90 int InputFile::idCount = 0;
91 
92 // Open a given file path and return it as a memory-mapped file.
93 Optional<MemoryBufferRef> macho::readFile(StringRef path) {
94   // Open a file.
95   auto mbOrErr = MemoryBuffer::getFile(path);
96   if (auto ec = mbOrErr.getError()) {
97     error("cannot open " + path + ": " + ec.message());
98     return None;
99   }
100 
101   std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
102   MemoryBufferRef mbref = mb->getMemBufferRef();
103   make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
104 
105   // If this is a regular non-fat file, return it.
106   const char *buf = mbref.getBufferStart();
107   auto *hdr = reinterpret_cast<const MachO::fat_header *>(buf);
108   if (read32be(&hdr->magic) != MachO::FAT_MAGIC) {
109     if (tar)
110       tar->append(relativeToRoot(path), mbref.getBuffer());
111     return mbref;
112   }
113 
114   // Object files and archive files may be fat files, which contains
115   // multiple real files for different CPU ISAs. Here, we search for a
116   // file that matches with the current link target and returns it as
117   // a MemoryBufferRef.
118   auto *arch = reinterpret_cast<const MachO::fat_arch *>(buf + sizeof(*hdr));
119 
120   for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
121     if (reinterpret_cast<const char *>(arch + i + 1) >
122         buf + mbref.getBufferSize()) {
123       error(path + ": fat_arch struct extends beyond end of file");
124       return None;
125     }
126 
127     if (read32be(&arch[i].cputype) != target->cpuType ||
128         read32be(&arch[i].cpusubtype) != target->cpuSubtype)
129       continue;
130 
131     uint32_t offset = read32be(&arch[i].offset);
132     uint32_t size = read32be(&arch[i].size);
133     if (offset + size > mbref.getBufferSize())
134       error(path + ": slice extends beyond end of file");
135     if (tar)
136       tar->append(relativeToRoot(path), mbref.getBuffer());
137     return MemoryBufferRef(StringRef(buf + offset, size), path.copy(bAlloc));
138   }
139 
140   error("unable to find matching architecture in " + path);
141   return None;
142 }
143 
144 const load_command *macho::findCommand(const mach_header_64 *hdr,
145                                        uint32_t type) {
146   const uint8_t *p =
147       reinterpret_cast<const uint8_t *>(hdr) + sizeof(mach_header_64);
148 
149   for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
150     auto *cmd = reinterpret_cast<const load_command *>(p);
151     if (cmd->cmd == type)
152       return cmd;
153     p += cmd->cmdsize;
154   }
155   return nullptr;
156 }
157 
158 void ObjFile::parseSections(ArrayRef<section_64> sections) {
159   subsections.reserve(sections.size());
160   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
161 
162   for (const section_64 &sec : sections) {
163     InputSection *isec = make<InputSection>();
164     isec->file = this;
165     isec->name =
166         StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
167     isec->segname =
168         StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
169     isec->data = {isZeroFill(sec.flags) ? nullptr : buf + sec.offset,
170                   static_cast<size_t>(sec.size)};
171     if (sec.align >= 32)
172       error("alignment " + std::to_string(sec.align) + " of section " +
173             isec->name + " is too large");
174     else
175       isec->align = 1 << sec.align;
176     isec->flags = sec.flags;
177 
178     if (!(isDebugSection(isec->flags) &&
179           isec->segname == segment_names::dwarf)) {
180       subsections.push_back({{0, isec}});
181     } else {
182       // Instead of emitting DWARF sections, we emit STABS symbols to the
183       // object files that contain them. We filter them out early to avoid
184       // parsing their relocations unnecessarily. But we must still push an
185       // empty map to ensure the indices line up for the remaining sections.
186       subsections.push_back({});
187       debugSections.push_back(isec);
188     }
189   }
190 }
191 
192 // Find the subsection corresponding to the greatest section offset that is <=
193 // that of the given offset.
194 //
195 // offset: an offset relative to the start of the original InputSection (before
196 // any subsection splitting has occurred). It will be updated to represent the
197 // same location as an offset relative to the start of the containing
198 // subsection.
199 static InputSection *findContainingSubsection(SubsectionMap &map,
200                                               uint32_t *offset) {
201   auto it = std::prev(map.upper_bound(*offset));
202   *offset -= it->first;
203   return it->second;
204 }
205 
206 static bool validateRelocationInfo(MemoryBufferRef mb, const section_64 &sec,
207                                    relocation_info rel) {
208   const TargetInfo::RelocAttrs &relocAttrs = target->getRelocAttrs(rel.r_type);
209   bool valid = true;
210   auto message = [relocAttrs, mb, sec, rel, &valid](const Twine &diagnostic) {
211     valid = false;
212     return (relocAttrs.name + " relocation " + diagnostic + " at offset " +
213             std::to_string(rel.r_address) + " of " + sec.segname + "," +
214             sec.sectname + " in " + mb.getBufferIdentifier())
215         .str();
216   };
217 
218   if (!relocAttrs.hasAttr(RelocAttrBits::LOCAL) && !rel.r_extern)
219     error(message("must be extern"));
220   if (relocAttrs.hasAttr(RelocAttrBits::PCREL) != rel.r_pcrel)
221     error(message(Twine("must ") + (rel.r_pcrel ? "not " : "") +
222                   "be PC-relative"));
223   if (isThreadLocalVariables(sec.flags) &&
224       (!relocAttrs.hasAttr(RelocAttrBits::TLV) ||
225        relocAttrs.hasAttr(RelocAttrBits::LOAD)))
226     error(message("not allowed in thread-local section, must be UNSIGNED"));
227   if (rel.r_length < 2 || rel.r_length > 3 ||
228       !relocAttrs.hasAttr(static_cast<RelocAttrBits>(1 << rel.r_length))) {
229     static SmallVector<StringRef, 4> widths{"INVALID", "4", "8", "4 or 8"};
230     error(message("has width " + std::to_string(1 << rel.r_length) +
231                   " bytes, but must be " +
232                   widths[(static_cast<int>(relocAttrs.bits) >> 2) & 3] +
233                   " bytes"));
234   }
235   return valid;
236 }
237 
238 void ObjFile::parseRelocations(const section_64 &sec,
239                                SubsectionMap &subsecMap) {
240   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
241   ArrayRef<relocation_info> relInfos(
242       reinterpret_cast<const relocation_info *>(buf + sec.reloff), sec.nreloc);
243 
244   for (size_t i = 0; i < relInfos.size(); i++) {
245     // Paired relocations serve as Mach-O's method for attaching a
246     // supplemental datum to a primary relocation record. ELF does not
247     // need them because the *_RELOC_RELA records contain the extra
248     // addend field, vs. *_RELOC_REL which omit the addend.
249     //
250     // The {X86_64,ARM64}_RELOC_SUBTRACTOR record holds the subtrahend,
251     // and the paired *_RELOC_UNSIGNED record holds the minuend. The
252     // datum for each is a symbolic address. The result is the offset
253     // between two addresses.
254     //
255     // The ARM64_RELOC_ADDEND record holds the addend, and the paired
256     // ARM64_RELOC_BRANCH26 or ARM64_RELOC_PAGE21/PAGEOFF12 holds the
257     // base symbolic address.
258     //
259     // Note: X86 does not use *_RELOC_ADDEND because it can embed an
260     // addend into the instruction stream. On X86, a relocatable address
261     // field always occupies an entire contiguous sequence of byte(s),
262     // so there is no need to merge opcode bits with address
263     // bits. Therefore, it's easy and convenient to store addends in the
264     // instruction-stream bytes that would otherwise contain zeroes. By
265     // contrast, RISC ISAs such as ARM64 mix opcode bits with with
266     // address bits so that bitwise arithmetic is necessary to extract
267     // and insert them. Storing addends in the instruction stream is
268     // possible, but inconvenient and more costly at link time.
269 
270     uint64_t pairedAddend = 0;
271     relocation_info relInfo = relInfos[i];
272     if (target->hasAttr(relInfo.r_type, RelocAttrBits::ADDEND)) {
273       pairedAddend = SignExtend64<24>(relInfo.r_symbolnum);
274       relInfo = relInfos[++i];
275     }
276     assert(i < relInfos.size());
277     if (!validateRelocationInfo(mb, sec, relInfo))
278       continue;
279     if (relInfo.r_address & R_SCATTERED)
280       fatal("TODO: Scattered relocations not supported");
281     uint64_t embeddedAddend = target->getEmbeddedAddend(mb, sec, relInfo);
282     assert(!(embeddedAddend && pairedAddend));
283     uint64_t totalAddend = pairedAddend + embeddedAddend;
284 
285     Reloc p;
286     if (target->hasAttr(relInfo.r_type, RelocAttrBits::SUBTRAHEND)) {
287       p.type = relInfo.r_type;
288       p.referent = symbols[relInfo.r_symbolnum];
289       relInfo = relInfos[++i];
290     }
291     Reloc r;
292     r.type = relInfo.r_type;
293     r.pcrel = relInfo.r_pcrel;
294     r.length = relInfo.r_length;
295     r.offset = relInfo.r_address;
296     if (relInfo.r_extern) {
297       r.referent = symbols[relInfo.r_symbolnum];
298       r.addend = totalAddend;
299     } else {
300       SubsectionMap &referentSubsecMap = subsections[relInfo.r_symbolnum - 1];
301       const section_64 &referentSec = sectionHeaders[relInfo.r_symbolnum - 1];
302       uint32_t referentOffset;
303       if (relInfo.r_pcrel) {
304         // The implicit addend for pcrel section relocations is the pcrel offset
305         // in terms of the addresses in the input file. Here we adjust it so
306         // that it describes the offset from the start of the referent section.
307         // TODO: The offset of 4 is probably not right for ARM64, nor for
308         //       relocations with r_length != 2.
309         referentOffset =
310             sec.addr + relInfo.r_address + 4 + totalAddend - referentSec.addr;
311       } else {
312         // The addend for a non-pcrel relocation is its absolute address.
313         referentOffset = totalAddend - referentSec.addr;
314       }
315       r.referent = findContainingSubsection(referentSubsecMap, &referentOffset);
316       r.addend = referentOffset;
317     }
318 
319     InputSection *subsec = findContainingSubsection(subsecMap, &r.offset);
320     if (p.type != GENERIC_RELOC_INVALID &&
321         target->hasAttr(p.type, RelocAttrBits::SUBTRAHEND))
322       subsec->relocs.push_back(p);
323     subsec->relocs.push_back(r);
324   }
325 }
326 
327 static macho::Symbol *createDefined(const structs::nlist_64 &sym,
328                                     StringRef name, InputSection *isec,
329                                     uint32_t value) {
330   // Symbol scope is determined by sym.n_type & (N_EXT | N_PEXT):
331   // N_EXT: Global symbols
332   // N_EXT | N_PEXT: Linkage unit (think: dylib) scoped
333   // N_PEXT: Does not occur in input files in practice,
334   //         a private extern must be external.
335   // 0: Translation-unit scoped. These are not in the symbol table.
336 
337   if (sym.n_type & (N_EXT | N_PEXT)) {
338     assert((sym.n_type & N_EXT) && "invalid input");
339     return symtab->addDefined(name, isec->file, isec, value,
340                               sym.n_desc & N_WEAK_DEF, sym.n_type & N_PEXT);
341   }
342   return make<Defined>(name, isec->file, isec, value, sym.n_desc & N_WEAK_DEF,
343                        /*isExternal=*/false, /*isPrivateExtern=*/false);
344 }
345 
346 // Absolute symbols are defined symbols that do not have an associated
347 // InputSection. They cannot be weak.
348 static macho::Symbol *createAbsolute(const structs::nlist_64 &sym,
349                                      InputFile *file, StringRef name) {
350   if (sym.n_type & (N_EXT | N_PEXT)) {
351     assert((sym.n_type & N_EXT) && "invalid input");
352     return symtab->addDefined(name, file, nullptr, sym.n_value,
353                               /*isWeakDef=*/false, sym.n_type & N_PEXT);
354   }
355   return make<Defined>(name, file, nullptr, sym.n_value, /*isWeakDef=*/false,
356                        /*isExternal=*/false, /*isPrivateExtern=*/false);
357 }
358 
359 macho::Symbol *ObjFile::parseNonSectionSymbol(const structs::nlist_64 &sym,
360                                               StringRef name) {
361   uint8_t type = sym.n_type & N_TYPE;
362   switch (type) {
363   case N_UNDF:
364     return sym.n_value == 0
365                ? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)
366                : symtab->addCommon(name, this, sym.n_value,
367                                    1 << GET_COMM_ALIGN(sym.n_desc),
368                                    sym.n_type & N_PEXT);
369   case N_ABS:
370     return createAbsolute(sym, this, name);
371   case N_PBUD:
372   case N_INDR:
373     error("TODO: support symbols of type " + std::to_string(type));
374     return nullptr;
375   case N_SECT:
376     llvm_unreachable(
377         "N_SECT symbols should not be passed to parseNonSectionSymbol");
378   default:
379     llvm_unreachable("invalid symbol type");
380   }
381 }
382 
383 void ObjFile::parseSymbols(ArrayRef<structs::nlist_64> nList,
384                            const char *strtab, bool subsectionsViaSymbols) {
385   // resize(), not reserve(), because we are going to create N_ALT_ENTRY symbols
386   // out-of-sequence.
387   symbols.resize(nList.size());
388   std::vector<size_t> altEntrySymIdxs;
389 
390   for (size_t i = 0, n = nList.size(); i < n; ++i) {
391     const structs::nlist_64 &sym = nList[i];
392     StringRef name = strtab + sym.n_strx;
393 
394     if ((sym.n_type & N_TYPE) != N_SECT) {
395       symbols[i] = parseNonSectionSymbol(sym, name);
396       continue;
397     }
398 
399     const section_64 &sec = sectionHeaders[sym.n_sect - 1];
400     SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
401     assert(!subsecMap.empty());
402     uint64_t offset = sym.n_value - sec.addr;
403 
404     // If the input file does not use subsections-via-symbols, all symbols can
405     // use the same subsection. Otherwise, we must split the sections along
406     // symbol boundaries.
407     if (!subsectionsViaSymbols) {
408       symbols[i] = createDefined(sym, name, subsecMap[0], offset);
409       continue;
410     }
411 
412     // nList entries aren't necessarily arranged in address order. Therefore,
413     // we can't create alt-entry symbols at this point because a later symbol
414     // may split its section, which may affect which subsection the alt-entry
415     // symbol is assigned to. So we need to handle them in a second pass below.
416     if (sym.n_desc & N_ALT_ENTRY) {
417       altEntrySymIdxs.push_back(i);
418       continue;
419     }
420 
421     // Find the subsection corresponding to the greatest section offset that is
422     // <= that of the current symbol. The subsection that we find either needs
423     // to be used directly or split in two.
424     uint32_t firstSize = offset;
425     InputSection *firstIsec = findContainingSubsection(subsecMap, &firstSize);
426 
427     if (firstSize == 0) {
428       // Alias of an existing symbol, or the first symbol in the section. These
429       // are handled by reusing the existing section.
430       symbols[i] = createDefined(sym, name, firstIsec, 0);
431       continue;
432     }
433 
434     // We saw a symbol definition at a new offset. Split the section into two
435     // subsections. The new symbol uses the second subsection.
436     auto *secondIsec = make<InputSection>(*firstIsec);
437     secondIsec->data = firstIsec->data.slice(firstSize);
438     firstIsec->data = firstIsec->data.slice(0, firstSize);
439     // TODO: ld64 appears to preserve the original alignment as well as each
440     // subsection's offset from the last aligned address. We should consider
441     // emulating that behavior.
442     secondIsec->align = MinAlign(firstIsec->align, offset);
443 
444     subsecMap[offset] = secondIsec;
445     // By construction, the symbol will be at offset zero in the new section.
446     symbols[i] = createDefined(sym, name, secondIsec, 0);
447   }
448 
449   for (size_t idx : altEntrySymIdxs) {
450     const structs::nlist_64 &sym = nList[idx];
451     StringRef name = strtab + sym.n_strx;
452     SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
453     uint32_t off = sym.n_value - sectionHeaders[sym.n_sect - 1].addr;
454     InputSection *subsec = findContainingSubsection(subsecMap, &off);
455     symbols[idx] = createDefined(sym, name, subsec, off);
456   }
457 }
458 
459 OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
460                        StringRef sectName)
461     : InputFile(OpaqueKind, mb) {
462   InputSection *isec = make<InputSection>();
463   isec->file = this;
464   isec->name = sectName.take_front(16);
465   isec->segname = segName.take_front(16);
466   const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
467   isec->data = {buf, mb.getBufferSize()};
468   subsections.push_back({{0, isec}});
469 }
470 
471 ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName)
472     : InputFile(ObjKind, mb), modTime(modTime) {
473   this->archiveName = std::string(archiveName);
474 
475   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
476   auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
477 
478   if (const load_command *cmd = findCommand(hdr, LC_LINKER_OPTION)) {
479     auto *c = reinterpret_cast<const linker_option_command *>(cmd);
480     StringRef data{reinterpret_cast<const char *>(c + 1),
481                    c->cmdsize - sizeof(linker_option_command)};
482     parseLCLinkerOption(this, c->count, data);
483   }
484 
485   if (const load_command *cmd = findCommand(hdr, LC_SEGMENT_64)) {
486     auto *c = reinterpret_cast<const segment_command_64 *>(cmd);
487     sectionHeaders = ArrayRef<section_64>{
488         reinterpret_cast<const section_64 *>(c + 1), c->nsects};
489     parseSections(sectionHeaders);
490   }
491 
492   // TODO: Error on missing LC_SYMTAB?
493   if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
494     auto *c = reinterpret_cast<const symtab_command *>(cmd);
495     ArrayRef<structs::nlist_64> nList(
496         reinterpret_cast<const structs::nlist_64 *>(buf + c->symoff), c->nsyms);
497     const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
498     bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
499     parseSymbols(nList, strtab, subsectionsViaSymbols);
500   }
501 
502   // The relocations may refer to the symbols, so we parse them after we have
503   // parsed all the symbols.
504   for (size_t i = 0, n = subsections.size(); i < n; ++i)
505     if (!subsections[i].empty())
506       parseRelocations(sectionHeaders[i], subsections[i]);
507 
508   parseDebugInfo();
509 }
510 
511 void ObjFile::parseDebugInfo() {
512   std::unique_ptr<DwarfObject> dObj = DwarfObject::create(this);
513   if (!dObj)
514     return;
515 
516   auto *ctx = make<DWARFContext>(
517       std::move(dObj), "",
518       [&](Error err) {
519         warn(toString(this) + ": " + toString(std::move(err)));
520       },
521       [&](Error warning) {
522         warn(toString(this) + ": " + toString(std::move(warning)));
523       });
524 
525   // TODO: Since object files can contain a lot of DWARF info, we should verify
526   // that we are parsing just the info we need
527   const DWARFContext::compile_unit_range &units = ctx->compile_units();
528   auto it = units.begin();
529   compileUnit = it->get();
530   assert(std::next(it) == units.end());
531 }
532 
533 // The path can point to either a dylib or a .tbd file.
534 static Optional<DylibFile *> loadDylib(StringRef path, DylibFile *umbrella) {
535   Optional<MemoryBufferRef> mbref = readFile(path);
536   if (!mbref) {
537     error("could not read dylib file at " + path);
538     return {};
539   }
540   return loadDylib(*mbref, umbrella);
541 }
542 
543 // TBD files are parsed into a series of TAPI documents (InterfaceFiles), with
544 // the first document storing child pointers to the rest of them. When we are
545 // processing a given TBD file, we store that top-level document here. When
546 // processing re-exports, we search its children for potentially matching
547 // documents in the same TBD file. Note that the children themselves don't
548 // point to further documents, i.e. this is a two-level tree.
549 //
550 // ld64 allows a TAPI re-export to reference documents nested within other TBD
551 // files, but that seems like a strange design, so this is an intentional
552 // deviation.
553 const InterfaceFile *currentTopLevelTapi = nullptr;
554 
555 // Re-exports can either refer to on-disk files, or to documents within .tbd
556 // files.
557 static Optional<DylibFile *> loadReexportHelper(StringRef path,
558                                                 DylibFile *umbrella) {
559   if (path::is_absolute(path, path::Style::posix))
560     for (StringRef root : config->systemLibraryRoots)
561       if (Optional<std::string> dylibPath =
562               resolveDylibPath((root + path).str()))
563         return loadDylib(*dylibPath, umbrella);
564 
565   // TODO: Expand @loader_path, @executable_path etc
566 
567   if (currentTopLevelTapi) {
568     for (InterfaceFile &child :
569          make_pointee_range(currentTopLevelTapi->documents())) {
570       if (path == child.getInstallName())
571         return make<DylibFile>(child, umbrella);
572       assert(child.documents().empty());
573     }
574   }
575 
576   if (Optional<std::string> dylibPath = resolveDylibPath(path))
577     return loadDylib(*dylibPath, umbrella);
578 
579   error("unable to locate re-export with install name " + path);
580   return {};
581 }
582 
583 // If a re-exported dylib is public (lives in /usr/lib or
584 // /System/Library/Frameworks), then it is considered implicitly linked: we
585 // should bind to its symbols directly instead of via the re-exporting umbrella
586 // library.
587 static bool isImplicitlyLinked(StringRef path) {
588   if (!config->implicitDylibs)
589     return false;
590 
591   if (path::parent_path(path) == "/usr/lib")
592     return true;
593 
594   // Match /System/Library/Frameworks/$FOO.framework/**/$FOO
595   if (path.consume_front("/System/Library/Frameworks/")) {
596     StringRef frameworkName = path.take_until([](char c) { return c == '.'; });
597     return path::filename(path) == frameworkName;
598   }
599 
600   return false;
601 }
602 
603 void loadReexport(StringRef path, DylibFile *umbrella) {
604   Optional<DylibFile *> reexport = loadReexportHelper(path, umbrella);
605   if (reexport && isImplicitlyLinked(path))
606     inputFiles.insert(*reexport);
607 }
608 
609 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella)
610     : InputFile(DylibKind, mb), refState(RefState::Unreferenced) {
611   if (umbrella == nullptr)
612     umbrella = this;
613 
614   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
615   auto *hdr = reinterpret_cast<const mach_header_64 *>(mb.getBufferStart());
616 
617   // Initialize dylibName.
618   if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) {
619     auto *c = reinterpret_cast<const dylib_command *>(cmd);
620     currentVersion = read32le(&c->dylib.current_version);
621     compatibilityVersion = read32le(&c->dylib.compatibility_version);
622     dylibName = reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);
623   } else {
624     error("dylib " + toString(this) + " missing LC_ID_DYLIB load command");
625     return;
626   }
627 
628   // Initialize symbols.
629   DylibFile *exportingFile = isImplicitlyLinked(dylibName) ? this : umbrella;
630   if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) {
631     auto *c = reinterpret_cast<const dyld_info_command *>(cmd);
632     parseTrie(buf + c->export_off, c->export_size,
633               [&](const Twine &name, uint64_t flags) {
634                 bool isWeakDef = flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
635                 bool isTlv = flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;
636                 symbols.push_back(symtab->addDylib(
637                     saver.save(name), exportingFile, isWeakDef, isTlv));
638               });
639   } else {
640     error("LC_DYLD_INFO_ONLY not found in " + toString(this));
641     return;
642   }
643 
644   if (hdr->flags & MH_NO_REEXPORTED_DYLIBS)
645     return;
646 
647   const uint8_t *p =
648       reinterpret_cast<const uint8_t *>(hdr) + sizeof(mach_header_64);
649   for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
650     auto *cmd = reinterpret_cast<const load_command *>(p);
651     p += cmd->cmdsize;
652     if (cmd->cmd != LC_REEXPORT_DYLIB)
653       continue;
654 
655     auto *c = reinterpret_cast<const dylib_command *>(cmd);
656     StringRef reexportPath =
657         reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
658     loadReexport(reexportPath, umbrella);
659   }
660 }
661 
662 DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella)
663     : InputFile(DylibKind, interface), refState(RefState::Unreferenced) {
664   if (umbrella == nullptr)
665     umbrella = this;
666 
667   dylibName = saver.save(interface.getInstallName());
668   compatibilityVersion = interface.getCompatibilityVersion().rawValue();
669   currentVersion = interface.getCurrentVersion().rawValue();
670   DylibFile *exportingFile = isImplicitlyLinked(dylibName) ? this : umbrella;
671   auto addSymbol = [&](const Twine &name) -> void {
672     symbols.push_back(symtab->addDylib(saver.save(name), exportingFile,
673                                        /*isWeakDef=*/false,
674                                        /*isTlv=*/false));
675   };
676   // TODO(compnerd) filter out symbols based on the target platform
677   // TODO: handle weak defs, thread locals
678   for (const auto symbol : interface.symbols()) {
679     if (!symbol->getArchitectures().has(config->arch))
680       continue;
681 
682     switch (symbol->getKind()) {
683     case SymbolKind::GlobalSymbol:
684       addSymbol(symbol->getName());
685       break;
686     case SymbolKind::ObjectiveCClass:
687       // XXX ld64 only creates these symbols when -ObjC is passed in. We may
688       // want to emulate that.
689       addSymbol(objc::klass + symbol->getName());
690       addSymbol(objc::metaclass + symbol->getName());
691       break;
692     case SymbolKind::ObjectiveCClassEHType:
693       addSymbol(objc::ehtype + symbol->getName());
694       break;
695     case SymbolKind::ObjectiveCInstanceVariable:
696       addSymbol(objc::ivar + symbol->getName());
697       break;
698     }
699   }
700 
701   bool isTopLevelTapi = false;
702   if (currentTopLevelTapi == nullptr) {
703     currentTopLevelTapi = &interface;
704     isTopLevelTapi = true;
705   }
706 
707   for (InterfaceFileRef intfRef : interface.reexportedLibraries())
708     loadReexport(intfRef.getInstallName(), umbrella);
709 
710   if (isTopLevelTapi)
711     currentTopLevelTapi = nullptr;
712 }
713 
714 ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f)
715     : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)) {
716   for (const object::Archive::Symbol &sym : file->symbols())
717     symtab->addLazy(sym.getName(), this, sym);
718 }
719 
720 void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
721   object::Archive::Child c =
722       CHECK(sym.getMember(), toString(this) +
723                                  ": could not get the member for symbol " +
724                                  toMachOString(sym));
725 
726   if (!seen.insert(c.getChildOffset()).second)
727     return;
728 
729   MemoryBufferRef mb =
730       CHECK(c.getMemoryBufferRef(),
731             toString(this) +
732                 ": could not get the buffer for the member defining symbol " +
733                 toMachOString(sym));
734 
735   if (tar && c.getParent()->isThin())
736     tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb.getBuffer());
737 
738   uint32_t modTime = toTimeT(
739       CHECK(c.getLastModified(), toString(this) +
740                                      ": could not get the modification time "
741                                      "for the member defining symbol " +
742                                      toMachOString(sym)));
743 
744   // `sym` is owned by a LazySym, which will be replace<>() by make<ObjFile>
745   // and become invalid after that call. Copy it to the stack so we can refer
746   // to it later.
747   const object::Archive::Symbol sym_copy = sym;
748 
749   if (Optional<InputFile *> file =
750           loadArchiveMember(mb, modTime, getName(), /*objCOnly=*/false)) {
751     inputFiles.insert(*file);
752     // ld64 doesn't demangle sym here even with -demangle. Match that, so
753     // intentionally no call to toMachOString() here.
754     printArchiveMemberLoad(sym_copy.getName(), *file);
755   }
756 }
757 
758 BitcodeFile::BitcodeFile(MemoryBufferRef mbref)
759     : InputFile(BitcodeKind, mbref) {
760   obj = check(lto::InputFile::create(mbref));
761 }
762