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 "SyntheticSections.h"
57 #include "Target.h"
58 
59 #include "lld/Common/DWARF.h"
60 #include "lld/Common/ErrorHandler.h"
61 #include "lld/Common/Memory.h"
62 #include "lld/Common/Reproduce.h"
63 #include "llvm/ADT/iterator.h"
64 #include "llvm/BinaryFormat/MachO.h"
65 #include "llvm/LTO/LTO.h"
66 #include "llvm/Support/Endian.h"
67 #include "llvm/Support/MemoryBuffer.h"
68 #include "llvm/Support/Path.h"
69 #include "llvm/Support/TarWriter.h"
70 #include "llvm/TextAPI/Architecture.h"
71 #include "llvm/TextAPI/InterfaceFile.h"
72 
73 using namespace llvm;
74 using namespace llvm::MachO;
75 using namespace llvm::support::endian;
76 using namespace llvm::sys;
77 using namespace lld;
78 using namespace lld::macho;
79 
80 // Returns "<internal>", "foo.a(bar.o)", or "baz.o".
81 std::string lld::toString(const InputFile *f) {
82   if (!f)
83     return "<internal>";
84 
85   // Multiple dylibs can be defined in one .tbd file.
86   if (auto dylibFile = dyn_cast<DylibFile>(f))
87     if (f->getName().endswith(".tbd"))
88       return (f->getName() + "(" + dylibFile->installName + ")").str();
89 
90   if (f->archiveName.empty())
91     return std::string(f->getName());
92   return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
93 }
94 
95 SetVector<InputFile *> macho::inputFiles;
96 std::unique_ptr<TarWriter> macho::tar;
97 int InputFile::idCount = 0;
98 
99 static VersionTuple decodeVersion(uint32_t version) {
100   unsigned major = version >> 16;
101   unsigned minor = (version >> 8) & 0xffu;
102   unsigned subMinor = version & 0xffu;
103   return VersionTuple(major, minor, subMinor);
104 }
105 
106 static std::vector<PlatformInfo> getPlatformInfos(const InputFile *input) {
107   if (!isa<ObjFile>(input) && !isa<DylibFile>(input))
108     return {};
109 
110   const char *hdr = input->mb.getBufferStart();
111 
112   std::vector<PlatformInfo> platformInfos;
113   for (auto *cmd : findCommands<build_version_command>(hdr, LC_BUILD_VERSION)) {
114     PlatformInfo info;
115     info.target.Platform = static_cast<PlatformKind>(cmd->platform);
116     info.minimum = decodeVersion(cmd->minos);
117     platformInfos.emplace_back(std::move(info));
118   }
119   for (auto *cmd : findCommands<version_min_command>(
120            hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
121            LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS)) {
122     PlatformInfo info;
123     switch (cmd->cmd) {
124     case LC_VERSION_MIN_MACOSX:
125       info.target.Platform = PlatformKind::macOS;
126       break;
127     case LC_VERSION_MIN_IPHONEOS:
128       info.target.Platform = PlatformKind::iOS;
129       break;
130     case LC_VERSION_MIN_TVOS:
131       info.target.Platform = PlatformKind::tvOS;
132       break;
133     case LC_VERSION_MIN_WATCHOS:
134       info.target.Platform = PlatformKind::watchOS;
135       break;
136     }
137     info.minimum = decodeVersion(cmd->version);
138     platformInfos.emplace_back(std::move(info));
139   }
140 
141   return platformInfos;
142 }
143 
144 static bool checkCompatibility(const InputFile *input) {
145   std::vector<PlatformInfo> platformInfos = getPlatformInfos(input);
146   if (platformInfos.empty())
147     return true;
148 
149   auto it = find_if(platformInfos, [&](const PlatformInfo &info) {
150     return removeSimulator(info.target.Platform) ==
151            removeSimulator(config->platform());
152   });
153   if (it == platformInfos.end()) {
154     std::string platformNames;
155     raw_string_ostream os(platformNames);
156     interleave(
157         platformInfos, os,
158         [&](const PlatformInfo &info) {
159           os << getPlatformName(info.target.Platform);
160         },
161         "/");
162     error(toString(input) + " has platform " + platformNames +
163           Twine(", which is different from target platform ") +
164           getPlatformName(config->platform()));
165     return false;
166   }
167 
168   if (it->minimum > config->platformInfo.minimum)
169     warn(toString(input) + " has version " + it->minimum.getAsString() +
170          ", which is newer than target minimum of " +
171          config->platformInfo.minimum.getAsString());
172 
173   return true;
174 }
175 
176 // Open a given file path and return it as a memory-mapped file.
177 Optional<MemoryBufferRef> macho::readFile(StringRef path) {
178   ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = MemoryBuffer::getFile(path);
179   if (std::error_code ec = mbOrErr.getError()) {
180     error("cannot open " + path + ": " + ec.message());
181     return None;
182   }
183 
184   std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
185   MemoryBufferRef mbref = mb->getMemBufferRef();
186   make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
187 
188   // If this is a regular non-fat file, return it.
189   const char *buf = mbref.getBufferStart();
190   const auto *hdr = reinterpret_cast<const fat_header *>(buf);
191   if (mbref.getBufferSize() < sizeof(uint32_t) ||
192       read32be(&hdr->magic) != FAT_MAGIC) {
193     if (tar)
194       tar->append(relativeToRoot(path), mbref.getBuffer());
195     return mbref;
196   }
197 
198   // Object files and archive files may be fat files, which contain multiple
199   // real files for different CPU ISAs. Here, we search for a file that matches
200   // with the current link target and returns it as a MemoryBufferRef.
201   const auto *arch = reinterpret_cast<const fat_arch *>(buf + sizeof(*hdr));
202 
203   for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
204     if (reinterpret_cast<const char *>(arch + i + 1) >
205         buf + mbref.getBufferSize()) {
206       error(path + ": fat_arch struct extends beyond end of file");
207       return None;
208     }
209 
210     if (read32be(&arch[i].cputype) != static_cast<uint32_t>(target->cpuType) ||
211         read32be(&arch[i].cpusubtype) != target->cpuSubtype)
212       continue;
213 
214     uint32_t offset = read32be(&arch[i].offset);
215     uint32_t size = read32be(&arch[i].size);
216     if (offset + size > mbref.getBufferSize())
217       error(path + ": slice extends beyond end of file");
218     if (tar)
219       tar->append(relativeToRoot(path), mbref.getBuffer());
220     return MemoryBufferRef(StringRef(buf + offset, size), path.copy(bAlloc));
221   }
222 
223   error("unable to find matching architecture in " + path);
224   return None;
225 }
226 
227 InputFile::InputFile(Kind kind, const InterfaceFile &interface)
228     : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {}
229 
230 // Some sections comprise of fixed-size records, so instead of splitting them at
231 // symbol boundaries, we split them based on size. Records are distinct from
232 // literals in that they may contain references to other sections, instead of
233 // being leaf nodes in the InputSection graph.
234 //
235 // Note that "record" is a term I came up with. In contrast, "literal" is a term
236 // used by the Mach-O format.
237 static Optional<size_t> getRecordSize(StringRef segname, StringRef name) {
238   if (name == section_names::cfString) {
239     if (config->icfLevel != ICFLevel::none && segname == segment_names::data)
240       return target->wordSize == 8 ? 32 : 16;
241   } else if (name == section_names::compactUnwind) {
242     if (segname == segment_names::ld)
243       return target->wordSize == 8 ? 32 : 20;
244   }
245   return {};
246 }
247 
248 template <class Section>
249 void ObjFile::parseSections(ArrayRef<Section> sections) {
250   subsections.reserve(sections.size());
251   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
252 
253   for (const Section &sec : sections) {
254     StringRef name =
255         StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
256     StringRef segname =
257         StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
258     ArrayRef<uint8_t> data = {isZeroFill(sec.flags) ? nullptr
259                                                     : buf + sec.offset,
260                               static_cast<size_t>(sec.size)};
261     if (sec.align >= 32) {
262       error("alignment " + std::to_string(sec.align) + " of section " + name +
263             " is too large");
264       subsections.push_back({});
265       continue;
266     }
267     uint32_t align = 1 << sec.align;
268     uint32_t flags = sec.flags;
269 
270     auto splitRecords = [&](int recordSize) -> void {
271       subsections.push_back({});
272       if (data.size() == 0)
273         return;
274 
275       SubsectionMap &subsecMap = subsections.back();
276       subsecMap.reserve(data.size() / recordSize);
277       auto *isec = make<ConcatInputSection>(
278           segname, name, this, data.slice(0, recordSize), align, flags);
279       subsecMap.push_back({0, isec});
280       for (uint64_t off = recordSize; off < data.size(); off += recordSize) {
281         // Copying requires less memory than constructing a fresh InputSection.
282         auto *copy = make<ConcatInputSection>(*isec);
283         copy->data = data.slice(off, recordSize);
284         subsecMap.push_back({off, copy});
285       }
286     };
287 
288     if (sectionType(sec.flags) == S_CSTRING_LITERALS ||
289         (config->dedupLiterals && isWordLiteralSection(sec.flags))) {
290       if (sec.nreloc && config->dedupLiterals)
291         fatal(toString(this) + " contains relocations in " + sec.segname + "," +
292               sec.sectname +
293               ", so LLD cannot deduplicate literals. Try re-running without "
294               "--deduplicate-literals.");
295 
296       InputSection *isec;
297       if (sectionType(sec.flags) == S_CSTRING_LITERALS) {
298         isec =
299             make<CStringInputSection>(segname, name, this, data, align, flags);
300         // FIXME: parallelize this?
301         cast<CStringInputSection>(isec)->splitIntoPieces();
302       } else {
303         isec = make<WordLiteralInputSection>(segname, name, this, data, align,
304                                              flags);
305       }
306       subsections.push_back({{0, isec}});
307     } else if (auto recordSize = getRecordSize(segname, name)) {
308       splitRecords(*recordSize);
309     } else if (segname == segment_names::llvm) {
310       // ld64 does not appear to emit contents from sections within the __LLVM
311       // segment. Symbols within those sections point to bitcode metadata
312       // instead of actual symbols. Global symbols within those sections could
313       // have the same name without causing duplicate symbol errors. Push an
314       // empty map to ensure indices line up for the remaining sections.
315       // TODO: Evaluate whether the bitcode metadata is needed.
316       subsections.push_back({});
317     } else {
318       auto *isec =
319           make<ConcatInputSection>(segname, name, this, data, align, flags);
320       if (isDebugSection(isec->getFlags()) &&
321           isec->getSegName() == segment_names::dwarf) {
322         // Instead of emitting DWARF sections, we emit STABS symbols to the
323         // object files that contain them. We filter them out early to avoid
324         // parsing their relocations unnecessarily. But we must still push an
325         // empty map to ensure the indices line up for the remaining sections.
326         subsections.push_back({});
327         debugSections.push_back(isec);
328       } else {
329         subsections.push_back({{0, isec}});
330       }
331     }
332   }
333 }
334 
335 // Find the subsection corresponding to the greatest section offset that is <=
336 // that of the given offset.
337 //
338 // offset: an offset relative to the start of the original InputSection (before
339 // any subsection splitting has occurred). It will be updated to represent the
340 // same location as an offset relative to the start of the containing
341 // subsection.
342 static InputSection *findContainingSubsection(SubsectionMap &map,
343                                               uint64_t *offset) {
344   auto it = std::prev(llvm::upper_bound(
345       map, *offset, [](uint64_t value, SubsectionEntry subsecEntry) {
346         return value < subsecEntry.offset;
347       }));
348   *offset -= it->offset;
349   return it->isec;
350 }
351 
352 template <class Section>
353 static bool validateRelocationInfo(InputFile *file, const Section &sec,
354                                    relocation_info rel) {
355   const RelocAttrs &relocAttrs = target->getRelocAttrs(rel.r_type);
356   bool valid = true;
357   auto message = [relocAttrs, file, sec, rel, &valid](const Twine &diagnostic) {
358     valid = false;
359     return (relocAttrs.name + " relocation " + diagnostic + " at offset " +
360             std::to_string(rel.r_address) + " of " + sec.segname + "," +
361             sec.sectname + " in " + toString(file))
362         .str();
363   };
364 
365   if (!relocAttrs.hasAttr(RelocAttrBits::LOCAL) && !rel.r_extern)
366     error(message("must be extern"));
367   if (relocAttrs.hasAttr(RelocAttrBits::PCREL) != rel.r_pcrel)
368     error(message(Twine("must ") + (rel.r_pcrel ? "not " : "") +
369                   "be PC-relative"));
370   if (isThreadLocalVariables(sec.flags) &&
371       !relocAttrs.hasAttr(RelocAttrBits::UNSIGNED))
372     error(message("not allowed in thread-local section, must be UNSIGNED"));
373   if (rel.r_length < 2 || rel.r_length > 3 ||
374       !relocAttrs.hasAttr(static_cast<RelocAttrBits>(1 << rel.r_length))) {
375     static SmallVector<StringRef, 4> widths{"0", "4", "8", "4 or 8"};
376     error(message("has width " + std::to_string(1 << rel.r_length) +
377                   " bytes, but must be " +
378                   widths[(static_cast<int>(relocAttrs.bits) >> 2) & 3] +
379                   " bytes"));
380   }
381   return valid;
382 }
383 
384 template <class Section>
385 void ObjFile::parseRelocations(ArrayRef<Section> sectionHeaders,
386                                const Section &sec, SubsectionMap &subsecMap) {
387   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
388   ArrayRef<relocation_info> relInfos(
389       reinterpret_cast<const relocation_info *>(buf + sec.reloff), sec.nreloc);
390 
391   auto subsecIt = subsecMap.rbegin();
392   for (size_t i = 0; i < relInfos.size(); i++) {
393     // Paired relocations serve as Mach-O's method for attaching a
394     // supplemental datum to a primary relocation record. ELF does not
395     // need them because the *_RELOC_RELA records contain the extra
396     // addend field, vs. *_RELOC_REL which omit the addend.
397     //
398     // The {X86_64,ARM64}_RELOC_SUBTRACTOR record holds the subtrahend,
399     // and the paired *_RELOC_UNSIGNED record holds the minuend. The
400     // datum for each is a symbolic address. The result is the offset
401     // between two addresses.
402     //
403     // The ARM64_RELOC_ADDEND record holds the addend, and the paired
404     // ARM64_RELOC_BRANCH26 or ARM64_RELOC_PAGE21/PAGEOFF12 holds the
405     // base symbolic address.
406     //
407     // Note: X86 does not use *_RELOC_ADDEND because it can embed an
408     // addend into the instruction stream. On X86, a relocatable address
409     // field always occupies an entire contiguous sequence of byte(s),
410     // so there is no need to merge opcode bits with address
411     // bits. Therefore, it's easy and convenient to store addends in the
412     // instruction-stream bytes that would otherwise contain zeroes. By
413     // contrast, RISC ISAs such as ARM64 mix opcode bits with with
414     // address bits so that bitwise arithmetic is necessary to extract
415     // and insert them. Storing addends in the instruction stream is
416     // possible, but inconvenient and more costly at link time.
417 
418     int64_t pairedAddend = 0;
419     relocation_info relInfo = relInfos[i];
420     if (target->hasAttr(relInfo.r_type, RelocAttrBits::ADDEND)) {
421       pairedAddend = SignExtend64<24>(relInfo.r_symbolnum);
422       relInfo = relInfos[++i];
423     }
424     assert(i < relInfos.size());
425     if (!validateRelocationInfo(this, sec, relInfo))
426       continue;
427     if (relInfo.r_address & R_SCATTERED)
428       fatal("TODO: Scattered relocations not supported");
429 
430     bool isSubtrahend =
431         target->hasAttr(relInfo.r_type, RelocAttrBits::SUBTRAHEND);
432     int64_t embeddedAddend = target->getEmbeddedAddend(mb, sec.offset, relInfo);
433     assert(!(embeddedAddend && pairedAddend));
434     int64_t totalAddend = pairedAddend + embeddedAddend;
435     Reloc r;
436     r.type = relInfo.r_type;
437     r.pcrel = relInfo.r_pcrel;
438     r.length = relInfo.r_length;
439     r.offset = relInfo.r_address;
440     if (relInfo.r_extern) {
441       r.referent = symbols[relInfo.r_symbolnum];
442       r.addend = isSubtrahend ? 0 : totalAddend;
443     } else {
444       assert(!isSubtrahend);
445       const Section &referentSec = sectionHeaders[relInfo.r_symbolnum - 1];
446       uint64_t referentOffset;
447       if (relInfo.r_pcrel) {
448         // The implicit addend for pcrel section relocations is the pcrel offset
449         // in terms of the addresses in the input file. Here we adjust it so
450         // that it describes the offset from the start of the referent section.
451         // FIXME This logic was written around x86_64 behavior -- ARM64 doesn't
452         // have pcrel section relocations. We may want to factor this out into
453         // the arch-specific .cpp file.
454         assert(target->hasAttr(r.type, RelocAttrBits::BYTE4));
455         referentOffset =
456             sec.addr + relInfo.r_address + 4 + totalAddend - referentSec.addr;
457       } else {
458         // The addend for a non-pcrel relocation is its absolute address.
459         referentOffset = totalAddend - referentSec.addr;
460       }
461       SubsectionMap &referentSubsecMap = subsections[relInfo.r_symbolnum - 1];
462       r.referent = findContainingSubsection(referentSubsecMap, &referentOffset);
463       r.addend = referentOffset;
464     }
465 
466     // Find the subsection that this relocation belongs to.
467     // Though not required by the Mach-O format, clang and gcc seem to emit
468     // relocations in order, so let's take advantage of it. However, ld64 emits
469     // unsorted relocations (in `-r` mode), so we have a fallback for that
470     // uncommon case.
471     InputSection *subsec;
472     while (subsecIt != subsecMap.rend() && subsecIt->offset > r.offset)
473       ++subsecIt;
474     if (subsecIt == subsecMap.rend() ||
475         subsecIt->offset + subsecIt->isec->getSize() <= r.offset) {
476       subsec = findContainingSubsection(subsecMap, &r.offset);
477       // Now that we know the relocs are unsorted, avoid trying the 'fast path'
478       // for the other relocations.
479       subsecIt = subsecMap.rend();
480     } else {
481       subsec = subsecIt->isec;
482       r.offset -= subsecIt->offset;
483     }
484     subsec->relocs.push_back(r);
485 
486     if (isSubtrahend) {
487       relocation_info minuendInfo = relInfos[++i];
488       // SUBTRACTOR relocations should always be followed by an UNSIGNED one
489       // attached to the same address.
490       assert(target->hasAttr(minuendInfo.r_type, RelocAttrBits::UNSIGNED) &&
491              relInfo.r_address == minuendInfo.r_address);
492       Reloc p;
493       p.type = minuendInfo.r_type;
494       if (minuendInfo.r_extern) {
495         p.referent = symbols[minuendInfo.r_symbolnum];
496         p.addend = totalAddend;
497       } else {
498         uint64_t referentOffset =
499             totalAddend - sectionHeaders[minuendInfo.r_symbolnum - 1].addr;
500         SubsectionMap &referentSubsecMap =
501             subsections[minuendInfo.r_symbolnum - 1];
502         p.referent =
503             findContainingSubsection(referentSubsecMap, &referentOffset);
504         p.addend = referentOffset;
505       }
506       subsec->relocs.push_back(p);
507     }
508   }
509 }
510 
511 template <class NList>
512 static macho::Symbol *createDefined(const NList &sym, StringRef name,
513                                     InputSection *isec, uint64_t value,
514                                     uint64_t size) {
515   // Symbol scope is determined by sym.n_type & (N_EXT | N_PEXT):
516   // N_EXT: Global symbols. These go in the symbol table during the link,
517   //        and also in the export table of the output so that the dynamic
518   //        linker sees them.
519   // N_EXT | N_PEXT: Linkage unit (think: dylib) scoped. These go in the
520   //                 symbol table during the link so that duplicates are
521   //                 either reported (for non-weak symbols) or merged
522   //                 (for weak symbols), but they do not go in the export
523   //                 table of the output.
524   // N_PEXT: llvm-mc does not emit these, but `ld -r` (wherein ld64 emits
525   //         object files) may produce them. LLD does not yet support -r.
526   //         These are translation-unit scoped, identical to the `0` case.
527   // 0: Translation-unit scoped. These are not in the symbol table during
528   //    link, and not in the export table of the output either.
529   bool isWeakDefCanBeHidden =
530       (sym.n_desc & (N_WEAK_DEF | N_WEAK_REF)) == (N_WEAK_DEF | N_WEAK_REF);
531 
532   if (sym.n_type & N_EXT) {
533     bool isPrivateExtern = sym.n_type & N_PEXT;
534     // lld's behavior for merging symbols is slightly different from ld64:
535     // ld64 picks the winning symbol based on several criteria (see
536     // pickBetweenRegularAtoms() in ld64's SymbolTable.cpp), while lld
537     // just merges metadata and keeps the contents of the first symbol
538     // with that name (see SymbolTable::addDefined). For:
539     // * inline function F in a TU built with -fvisibility-inlines-hidden
540     // * and inline function F in another TU built without that flag
541     // ld64 will pick the one from the file built without
542     // -fvisibility-inlines-hidden.
543     // lld will instead pick the one listed first on the link command line and
544     // give it visibility as if the function was built without
545     // -fvisibility-inlines-hidden.
546     // If both functions have the same contents, this will have the same
547     // behavior. If not, it won't, but the input had an ODR violation in
548     // that case.
549     //
550     // Similarly, merging a symbol
551     // that's isPrivateExtern and not isWeakDefCanBeHidden with one
552     // that's not isPrivateExtern but isWeakDefCanBeHidden technically
553     // should produce one
554     // that's not isPrivateExtern but isWeakDefCanBeHidden. That matters
555     // with ld64's semantics, because it means the non-private-extern
556     // definition will continue to take priority if more private extern
557     // definitions are encountered. With lld's semantics there's no observable
558     // difference between a symbol that's isWeakDefCanBeHidden or one that's
559     // privateExtern -- neither makes it into the dynamic symbol table. So just
560     // promote isWeakDefCanBeHidden to isPrivateExtern here.
561     if (isWeakDefCanBeHidden)
562       isPrivateExtern = true;
563 
564     return symtab->addDefined(
565         name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
566         isPrivateExtern, sym.n_desc & N_ARM_THUMB_DEF,
567         sym.n_desc & REFERENCED_DYNAMICALLY, sym.n_desc & N_NO_DEAD_STRIP);
568   }
569   assert(!isWeakDefCanBeHidden &&
570          "weak_def_can_be_hidden on already-hidden symbol?");
571   return make<Defined>(
572       name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
573       /*isExternal=*/false, /*isPrivateExtern=*/false,
574       sym.n_desc & N_ARM_THUMB_DEF, sym.n_desc & REFERENCED_DYNAMICALLY,
575       sym.n_desc & N_NO_DEAD_STRIP);
576 }
577 
578 // Absolute symbols are defined symbols that do not have an associated
579 // InputSection. They cannot be weak.
580 template <class NList>
581 static macho::Symbol *createAbsolute(const NList &sym, InputFile *file,
582                                      StringRef name) {
583   if (sym.n_type & N_EXT) {
584     return symtab->addDefined(
585         name, file, nullptr, sym.n_value, /*size=*/0,
586         /*isWeakDef=*/false, sym.n_type & N_PEXT, sym.n_desc & N_ARM_THUMB_DEF,
587         /*isReferencedDynamically=*/false, sym.n_desc & N_NO_DEAD_STRIP);
588   }
589   return make<Defined>(name, file, nullptr, sym.n_value, /*size=*/0,
590                        /*isWeakDef=*/false,
591                        /*isExternal=*/false, /*isPrivateExtern=*/false,
592                        sym.n_desc & N_ARM_THUMB_DEF,
593                        /*isReferencedDynamically=*/false,
594                        sym.n_desc & N_NO_DEAD_STRIP);
595 }
596 
597 template <class NList>
598 macho::Symbol *ObjFile::parseNonSectionSymbol(const NList &sym,
599                                               StringRef name) {
600   uint8_t type = sym.n_type & N_TYPE;
601   switch (type) {
602   case N_UNDF:
603     return sym.n_value == 0
604                ? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)
605                : symtab->addCommon(name, this, sym.n_value,
606                                    1 << GET_COMM_ALIGN(sym.n_desc),
607                                    sym.n_type & N_PEXT);
608   case N_ABS:
609     return createAbsolute(sym, this, name);
610   case N_PBUD:
611   case N_INDR:
612     error("TODO: support symbols of type " + std::to_string(type));
613     return nullptr;
614   case N_SECT:
615     llvm_unreachable(
616         "N_SECT symbols should not be passed to parseNonSectionSymbol");
617   default:
618     llvm_unreachable("invalid symbol type");
619   }
620 }
621 
622 template <class NList>
623 static bool isUndef(const NList &sym) {
624   return (sym.n_type & N_TYPE) == N_UNDF && sym.n_value == 0;
625 }
626 
627 template <class LP>
628 void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
629                            ArrayRef<typename LP::nlist> nList,
630                            const char *strtab, bool subsectionsViaSymbols) {
631   using NList = typename LP::nlist;
632 
633   // Groups indices of the symbols by the sections that contain them.
634   std::vector<std::vector<uint32_t>> symbolsBySection(subsections.size());
635   symbols.resize(nList.size());
636   SmallVector<unsigned, 32> undefineds;
637   for (uint32_t i = 0; i < nList.size(); ++i) {
638     const NList &sym = nList[i];
639 
640     // Ignore debug symbols for now.
641     // FIXME: may need special handling.
642     if (sym.n_type & N_STAB)
643       continue;
644 
645     StringRef name = strtab + sym.n_strx;
646     if ((sym.n_type & N_TYPE) == N_SECT) {
647       SubsectionMap &subsecMap = subsections[sym.n_sect - 1];
648       // parseSections() may have chosen not to parse this section.
649       if (subsecMap.empty())
650         continue;
651       symbolsBySection[sym.n_sect - 1].push_back(i);
652     } else if (isUndef(sym)) {
653       undefineds.push_back(i);
654     } else {
655       symbols[i] = parseNonSectionSymbol(sym, name);
656     }
657   }
658 
659   for (size_t i = 0; i < subsections.size(); ++i) {
660     SubsectionMap &subsecMap = subsections[i];
661     if (subsecMap.empty())
662       continue;
663 
664     std::vector<uint32_t> &symbolIndices = symbolsBySection[i];
665     uint64_t sectionAddr = sectionHeaders[i].addr;
666     uint32_t sectionAlign = 1u << sectionHeaders[i].align;
667 
668     InputSection *lastIsec = subsecMap.back().isec;
669     // Record-based sections have already been split into subsections during
670     // parseSections(), so we simply need to match Symbols to the corresponding
671     // subsection here.
672     if (getRecordSize(lastIsec->getSegName(), lastIsec->getName())) {
673       for (size_t j = 0; j < symbolIndices.size(); ++j) {
674         uint32_t symIndex = symbolIndices[j];
675         const NList &sym = nList[symIndex];
676         StringRef name = strtab + sym.n_strx;
677         uint64_t symbolOffset = sym.n_value - sectionAddr;
678         InputSection *isec = findContainingSubsection(subsecMap, &symbolOffset);
679         if (symbolOffset != 0) {
680           error(toString(lastIsec) + ":  symbol " + name +
681                 " at misaligned offset");
682           continue;
683         }
684         symbols[symIndex] = createDefined(sym, name, isec, 0, isec->getSize());
685       }
686       continue;
687     }
688 
689     // Calculate symbol sizes and create subsections by splitting the sections
690     // along symbol boundaries.
691     // We populate subsecMap by repeatedly splitting the last (highest address)
692     // subsection.
693     llvm::stable_sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {
694       return nList[lhs].n_value < nList[rhs].n_value;
695     });
696     SubsectionEntry subsecEntry = subsecMap.back();
697     for (size_t j = 0; j < symbolIndices.size(); ++j) {
698       uint32_t symIndex = symbolIndices[j];
699       const NList &sym = nList[symIndex];
700       StringRef name = strtab + sym.n_strx;
701       InputSection *isec = subsecEntry.isec;
702 
703       uint64_t subsecAddr = sectionAddr + subsecEntry.offset;
704       size_t symbolOffset = sym.n_value - subsecAddr;
705       uint64_t symbolSize =
706           j + 1 < symbolIndices.size()
707               ? nList[symbolIndices[j + 1]].n_value - sym.n_value
708               : isec->data.size() - symbolOffset;
709       // There are 4 cases where we do not need to create a new subsection:
710       //   1. If the input file does not use subsections-via-symbols.
711       //   2. Multiple symbols at the same address only induce one subsection.
712       //      (The symbolOffset == 0 check covers both this case as well as
713       //      the first loop iteration.)
714       //   3. Alternative entry points do not induce new subsections.
715       //   4. If we have a literal section (e.g. __cstring and __literal4).
716       if (!subsectionsViaSymbols || symbolOffset == 0 ||
717           sym.n_desc & N_ALT_ENTRY || !isa<ConcatInputSection>(isec)) {
718         symbols[symIndex] =
719             createDefined(sym, name, isec, symbolOffset, symbolSize);
720         continue;
721       }
722       auto *concatIsec = cast<ConcatInputSection>(isec);
723 
724       auto *nextIsec = make<ConcatInputSection>(*concatIsec);
725       nextIsec->wasCoalesced = false;
726       if (isZeroFill(isec->getFlags())) {
727         // Zero-fill sections have NULL data.data() non-zero data.size()
728         nextIsec->data = {nullptr, isec->data.size() - symbolOffset};
729         isec->data = {nullptr, symbolOffset};
730       } else {
731         nextIsec->data = isec->data.slice(symbolOffset);
732         isec->data = isec->data.slice(0, symbolOffset);
733       }
734 
735       // By construction, the symbol will be at offset zero in the new
736       // subsection.
737       symbols[symIndex] =
738           createDefined(sym, name, nextIsec, /*value=*/0, symbolSize);
739       // TODO: ld64 appears to preserve the original alignment as well as each
740       // subsection's offset from the last aligned address. We should consider
741       // emulating that behavior.
742       nextIsec->align = MinAlign(sectionAlign, sym.n_value);
743       subsecMap.push_back({sym.n_value - sectionAddr, nextIsec});
744       subsecEntry = subsecMap.back();
745     }
746   }
747 
748   // Undefined symbols can trigger recursive fetch from Archives due to
749   // LazySymbols. Process defined symbols first so that the relative order
750   // between a defined symbol and an undefined symbol does not change the
751   // symbol resolution behavior. In addition, a set of interconnected symbols
752   // will all be resolved to the same file, instead of being resolved to
753   // different files.
754   for (unsigned i : undefineds) {
755     const NList &sym = nList[i];
756     StringRef name = strtab + sym.n_strx;
757     symbols[i] = parseNonSectionSymbol(sym, name);
758   }
759 }
760 
761 OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
762                        StringRef sectName)
763     : InputFile(OpaqueKind, mb) {
764   const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
765   ArrayRef<uint8_t> data = {buf, mb.getBufferSize()};
766   ConcatInputSection *isec =
767       make<ConcatInputSection>(segName.take_front(16), sectName.take_front(16),
768                                /*file=*/this, data);
769   isec->live = true;
770   subsections.push_back({{0, isec}});
771 }
772 
773 ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName)
774     : InputFile(ObjKind, mb), modTime(modTime) {
775   this->archiveName = std::string(archiveName);
776   if (target->wordSize == 8)
777     parse<LP64>();
778   else
779     parse<ILP32>();
780 }
781 
782 template <class LP> void ObjFile::parse() {
783   using Header = typename LP::mach_header;
784   using SegmentCommand = typename LP::segment_command;
785   using Section = typename LP::section;
786   using NList = typename LP::nlist;
787 
788   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
789   auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());
790 
791   Architecture arch = getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype);
792   if (arch != config->arch()) {
793     error(toString(this) + " has architecture " + getArchitectureName(arch) +
794           " which is incompatible with target architecture " +
795           getArchitectureName(config->arch()));
796     return;
797   }
798 
799   if (!checkCompatibility(this))
800     return;
801 
802   for (auto *cmd : findCommands<linker_option_command>(hdr, LC_LINKER_OPTION)) {
803     StringRef data{reinterpret_cast<const char *>(cmd + 1),
804                    cmd->cmdsize - sizeof(linker_option_command)};
805     parseLCLinkerOption(this, cmd->count, data);
806   }
807 
808   ArrayRef<Section> sectionHeaders;
809   if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
810     auto *c = reinterpret_cast<const SegmentCommand *>(cmd);
811     sectionHeaders =
812         ArrayRef<Section>{reinterpret_cast<const Section *>(c + 1), c->nsects};
813     parseSections(sectionHeaders);
814   }
815 
816   // TODO: Error on missing LC_SYMTAB?
817   if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
818     auto *c = reinterpret_cast<const symtab_command *>(cmd);
819     ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
820                           c->nsyms);
821     const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
822     bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
823     parseSymbols<LP>(sectionHeaders, nList, strtab, subsectionsViaSymbols);
824   }
825 
826   // The relocations may refer to the symbols, so we parse them after we have
827   // parsed all the symbols.
828   for (size_t i = 0, n = subsections.size(); i < n; ++i)
829     if (!subsections[i].empty())
830       parseRelocations(sectionHeaders, sectionHeaders[i], subsections[i]);
831 
832   parseDebugInfo();
833   if (config->emitDataInCodeInfo)
834     parseDataInCode();
835   registerCompactUnwind();
836 }
837 
838 void ObjFile::parseDebugInfo() {
839   std::unique_ptr<DwarfObject> dObj = DwarfObject::create(this);
840   if (!dObj)
841     return;
842 
843   auto *ctx = make<DWARFContext>(
844       std::move(dObj), "",
845       [&](Error err) {
846         warn(toString(this) + ": " + toString(std::move(err)));
847       },
848       [&](Error warning) {
849         warn(toString(this) + ": " + toString(std::move(warning)));
850       });
851 
852   // TODO: Since object files can contain a lot of DWARF info, we should verify
853   // that we are parsing just the info we need
854   const DWARFContext::compile_unit_range &units = ctx->compile_units();
855   // FIXME: There can be more than one compile unit per object file. See
856   // PR48637.
857   auto it = units.begin();
858   compileUnit = it->get();
859 }
860 
861 void ObjFile::parseDataInCode() {
862   const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
863   const load_command *cmd = findCommand(buf, LC_DATA_IN_CODE);
864   if (!cmd)
865     return;
866   const auto *c = reinterpret_cast<const linkedit_data_command *>(cmd);
867   dataInCodeEntries = {
868       reinterpret_cast<const data_in_code_entry *>(buf + c->dataoff),
869       c->datasize / sizeof(data_in_code_entry)};
870   assert(is_sorted(dataInCodeEntries, [](const data_in_code_entry &lhs,
871                                          const data_in_code_entry &rhs) {
872     return lhs.offset < rhs.offset;
873   }));
874 }
875 
876 // Create pointers from symbols to their associated compact unwind entries.
877 void ObjFile::registerCompactUnwind() {
878   // First, locate the __compact_unwind section.
879   SubsectionMap *cuSubsecMap = nullptr;
880   for (SubsectionMap &map : subsections) {
881     if (map.empty())
882       continue;
883     if (map[0].isec->getSegName() != segment_names::ld)
884       continue;
885     cuSubsecMap = &map;
886     break;
887   }
888   if (!cuSubsecMap)
889     return;
890 
891   for (SubsectionEntry &entry : *cuSubsecMap) {
892     ConcatInputSection *isec = cast<ConcatInputSection>(entry.isec);
893     ConcatInputSection *referentIsec;
894     for (const Reloc &r : isec->relocs) {
895       if (r.offset != 0)
896         continue;
897       uint64_t add = r.addend;
898       if (auto *sym = cast_or_null<Defined>(r.referent.dyn_cast<Symbol *>())) {
899         add += sym->value;
900         referentIsec = cast<ConcatInputSection>(sym->isec);
901       } else {
902         referentIsec =
903             cast<ConcatInputSection>(r.referent.dyn_cast<InputSection *>());
904       }
905       if (referentIsec->getSegName() != segment_names::text)
906         error("compact unwind references address in " + toString(referentIsec) +
907               " which is not in segment __TEXT");
908       // The functionAddress relocations are typically section relocations.
909       // However, unwind info operates on a per-symbol basis, so we search for
910       // the function symbol here.
911       auto it = llvm::lower_bound(
912           referentIsec->symbols, add,
913           [](Defined *d, uint64_t add) { return d->value < add; });
914       // The relocation should point at the exact address of a symbol (with no
915       // addend).
916       if (it == referentIsec->symbols.end() || (*it)->value != add) {
917         assert(referentIsec->wasCoalesced);
918         continue;
919       }
920       (*it)->compactUnwind = isec;
921     }
922   }
923 }
924 
925 // The path can point to either a dylib or a .tbd file.
926 static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) {
927   Optional<MemoryBufferRef> mbref = readFile(path);
928   if (!mbref) {
929     error("could not read dylib file at " + path);
930     return nullptr;
931   }
932   return loadDylib(*mbref, umbrella);
933 }
934 
935 // TBD files are parsed into a series of TAPI documents (InterfaceFiles), with
936 // the first document storing child pointers to the rest of them. When we are
937 // processing a given TBD file, we store that top-level document in
938 // currentTopLevelTapi. When processing re-exports, we search its children for
939 // potentially matching documents in the same TBD file. Note that the children
940 // themselves don't point to further documents, i.e. this is a two-level tree.
941 //
942 // Re-exports can either refer to on-disk files, or to documents within .tbd
943 // files.
944 static DylibFile *findDylib(StringRef path, DylibFile *umbrella,
945                             const InterfaceFile *currentTopLevelTapi) {
946   // Search order:
947   // 1. Install name basename in -F / -L directories.
948   {
949     StringRef stem = path::stem(path);
950     SmallString<128> frameworkName;
951     path::append(frameworkName, path::Style::posix, stem + ".framework", stem);
952     bool isFramework = path.endswith(frameworkName);
953     if (isFramework) {
954       for (StringRef dir : config->frameworkSearchPaths) {
955         SmallString<128> candidate = dir;
956         path::append(candidate, frameworkName);
957         if (Optional<StringRef> dylibPath = resolveDylibPath(candidate.str()))
958           return loadDylib(*dylibPath, umbrella);
959       }
960     } else if (Optional<StringRef> dylibPath = findPathCombination(
961                    stem, config->librarySearchPaths, {".tbd", ".dylib"}))
962       return loadDylib(*dylibPath, umbrella);
963   }
964 
965   // 2. As absolute path.
966   if (path::is_absolute(path, path::Style::posix))
967     for (StringRef root : config->systemLibraryRoots)
968       if (Optional<StringRef> dylibPath = resolveDylibPath((root + path).str()))
969         return loadDylib(*dylibPath, umbrella);
970 
971   // 3. As relative path.
972 
973   // TODO: Handle -dylib_file
974 
975   // Replace @executable_path, @loader_path, @rpath prefixes in install name.
976   SmallString<128> newPath;
977   if (config->outputType == MH_EXECUTE &&
978       path.consume_front("@executable_path/")) {
979     // ld64 allows overriding this with the undocumented flag -executable_path.
980     // lld doesn't currently implement that flag.
981     // FIXME: Consider using finalOutput instead of outputFile.
982     path::append(newPath, path::parent_path(config->outputFile), path);
983     path = newPath;
984   } else if (path.consume_front("@loader_path/")) {
985     fs::real_path(umbrella->getName(), newPath);
986     path::remove_filename(newPath);
987     path::append(newPath, path);
988     path = newPath;
989   } else if (path.startswith("@rpath/")) {
990     for (StringRef rpath : umbrella->rpaths) {
991       newPath.clear();
992       if (rpath.consume_front("@loader_path/")) {
993         fs::real_path(umbrella->getName(), newPath);
994         path::remove_filename(newPath);
995       }
996       path::append(newPath, rpath, path.drop_front(strlen("@rpath/")));
997       if (Optional<StringRef> dylibPath = resolveDylibPath(newPath.str()))
998         return loadDylib(*dylibPath, umbrella);
999     }
1000   }
1001 
1002   // FIXME: Should this be further up?
1003   if (currentTopLevelTapi) {
1004     for (InterfaceFile &child :
1005          make_pointee_range(currentTopLevelTapi->documents())) {
1006       assert(child.documents().empty());
1007       if (path == child.getInstallName()) {
1008         auto file = make<DylibFile>(child, umbrella);
1009         file->parseReexports(child);
1010         return file;
1011       }
1012     }
1013   }
1014 
1015   if (Optional<StringRef> dylibPath = resolveDylibPath(path))
1016     return loadDylib(*dylibPath, umbrella);
1017 
1018   return nullptr;
1019 }
1020 
1021 // If a re-exported dylib is public (lives in /usr/lib or
1022 // /System/Library/Frameworks), then it is considered implicitly linked: we
1023 // should bind to its symbols directly instead of via the re-exporting umbrella
1024 // library.
1025 static bool isImplicitlyLinked(StringRef path) {
1026   if (!config->implicitDylibs)
1027     return false;
1028 
1029   if (path::parent_path(path) == "/usr/lib")
1030     return true;
1031 
1032   // Match /System/Library/Frameworks/$FOO.framework/**/$FOO
1033   if (path.consume_front("/System/Library/Frameworks/")) {
1034     StringRef frameworkName = path.take_until([](char c) { return c == '.'; });
1035     return path::filename(path) == frameworkName;
1036   }
1037 
1038   return false;
1039 }
1040 
1041 static void loadReexport(StringRef path, DylibFile *umbrella,
1042                          const InterfaceFile *currentTopLevelTapi) {
1043   DylibFile *reexport = findDylib(path, umbrella, currentTopLevelTapi);
1044   if (!reexport)
1045     error("unable to locate re-export with install name " + path);
1046 }
1047 
1048 DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
1049                      bool isBundleLoader)
1050     : InputFile(DylibKind, mb), refState(RefState::Unreferenced),
1051       isBundleLoader(isBundleLoader) {
1052   assert(!isBundleLoader || !umbrella);
1053   if (umbrella == nullptr)
1054     umbrella = this;
1055   this->umbrella = umbrella;
1056 
1057   auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
1058   auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
1059 
1060   // Initialize installName.
1061   if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) {
1062     auto *c = reinterpret_cast<const dylib_command *>(cmd);
1063     currentVersion = read32le(&c->dylib.current_version);
1064     compatibilityVersion = read32le(&c->dylib.compatibility_version);
1065     installName =
1066         reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);
1067   } else if (!isBundleLoader) {
1068     // macho_executable and macho_bundle don't have LC_ID_DYLIB,
1069     // so it's OK.
1070     error("dylib " + toString(this) + " missing LC_ID_DYLIB load command");
1071     return;
1072   }
1073 
1074   if (config->printEachFile)
1075     message(toString(this));
1076   inputFiles.insert(this);
1077 
1078   deadStrippable = hdr->flags & MH_DEAD_STRIPPABLE_DYLIB;
1079 
1080   if (!checkCompatibility(this))
1081     return;
1082 
1083   checkAppExtensionSafety(hdr->flags & MH_APP_EXTENSION_SAFE);
1084 
1085   for (auto *cmd : findCommands<rpath_command>(hdr, LC_RPATH)) {
1086     StringRef rpath{reinterpret_cast<const char *>(cmd) + cmd->path};
1087     rpaths.push_back(rpath);
1088   }
1089 
1090   // Initialize symbols.
1091   exportingFile = isImplicitlyLinked(installName) ? this : this->umbrella;
1092   if (const load_command *cmd = findCommand(hdr, LC_DYLD_INFO_ONLY)) {
1093     auto *c = reinterpret_cast<const dyld_info_command *>(cmd);
1094     parseTrie(buf + c->export_off, c->export_size,
1095               [&](const Twine &name, uint64_t flags) {
1096                 StringRef savedName = saver.save(name);
1097                 if (handleLDSymbol(savedName))
1098                   return;
1099                 bool isWeakDef = flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
1100                 bool isTlv = flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;
1101                 symbols.push_back(symtab->addDylib(savedName, exportingFile,
1102                                                    isWeakDef, isTlv));
1103               });
1104   } else {
1105     error("LC_DYLD_INFO_ONLY not found in " + toString(this));
1106     return;
1107   }
1108 }
1109 
1110 void DylibFile::parseLoadCommands(MemoryBufferRef mb) {
1111   auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
1112   const uint8_t *p = reinterpret_cast<const uint8_t *>(mb.getBufferStart()) +
1113                      target->headerSize;
1114   for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
1115     auto *cmd = reinterpret_cast<const load_command *>(p);
1116     p += cmd->cmdsize;
1117 
1118     if (!(hdr->flags & MH_NO_REEXPORTED_DYLIBS) &&
1119         cmd->cmd == LC_REEXPORT_DYLIB) {
1120       const auto *c = reinterpret_cast<const dylib_command *>(cmd);
1121       StringRef reexportPath =
1122           reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
1123       loadReexport(reexportPath, exportingFile, nullptr);
1124     }
1125 
1126     // FIXME: What about LC_LOAD_UPWARD_DYLIB, LC_LAZY_LOAD_DYLIB,
1127     // LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB (..are reexports from dylibs with
1128     // MH_NO_REEXPORTED_DYLIBS loaded for -flat_namespace)?
1129     if (config->namespaceKind == NamespaceKind::flat &&
1130         cmd->cmd == LC_LOAD_DYLIB) {
1131       const auto *c = reinterpret_cast<const dylib_command *>(cmd);
1132       StringRef dylibPath =
1133           reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
1134       DylibFile *dylib = findDylib(dylibPath, umbrella, nullptr);
1135       if (!dylib)
1136         error(Twine("unable to locate library '") + dylibPath +
1137               "' loaded from '" + toString(this) + "' for -flat_namespace");
1138     }
1139   }
1140 }
1141 
1142 // Some versions of XCode ship with .tbd files that don't have the right
1143 // platform settings.
1144 static constexpr std::array<StringRef, 3> skipPlatformChecks{
1145     "/usr/lib/system/libsystem_kernel.dylib",
1146     "/usr/lib/system/libsystem_platform.dylib",
1147     "/usr/lib/system/libsystem_pthread.dylib"};
1148 
1149 DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
1150                      bool isBundleLoader)
1151     : InputFile(DylibKind, interface), refState(RefState::Unreferenced),
1152       isBundleLoader(isBundleLoader) {
1153   // FIXME: Add test for the missing TBD code path.
1154 
1155   if (umbrella == nullptr)
1156     umbrella = this;
1157   this->umbrella = umbrella;
1158 
1159   installName = saver.save(interface.getInstallName());
1160   compatibilityVersion = interface.getCompatibilityVersion().rawValue();
1161   currentVersion = interface.getCurrentVersion().rawValue();
1162 
1163   if (config->printEachFile)
1164     message(toString(this));
1165   inputFiles.insert(this);
1166 
1167   if (!is_contained(skipPlatformChecks, installName) &&
1168       !is_contained(interface.targets(), config->platformInfo.target)) {
1169     error(toString(this) + " is incompatible with " +
1170           std::string(config->platformInfo.target));
1171     return;
1172   }
1173 
1174   checkAppExtensionSafety(interface.isApplicationExtensionSafe());
1175 
1176   exportingFile = isImplicitlyLinked(installName) ? this : umbrella;
1177   auto addSymbol = [&](const Twine &name) -> void {
1178     symbols.push_back(symtab->addDylib(saver.save(name), exportingFile,
1179                                        /*isWeakDef=*/false,
1180                                        /*isTlv=*/false));
1181   };
1182   // TODO(compnerd) filter out symbols based on the target platform
1183   // TODO: handle weak defs, thread locals
1184   for (const auto *symbol : interface.symbols()) {
1185     if (!symbol->getArchitectures().has(config->arch()))
1186       continue;
1187 
1188     if (handleLDSymbol(symbol->getName()))
1189       continue;
1190 
1191     switch (symbol->getKind()) {
1192     case SymbolKind::GlobalSymbol:
1193       addSymbol(symbol->getName());
1194       break;
1195     case SymbolKind::ObjectiveCClass:
1196       // XXX ld64 only creates these symbols when -ObjC is passed in. We may
1197       // want to emulate that.
1198       addSymbol(objc::klass + symbol->getName());
1199       addSymbol(objc::metaclass + symbol->getName());
1200       break;
1201     case SymbolKind::ObjectiveCClassEHType:
1202       addSymbol(objc::ehtype + symbol->getName());
1203       break;
1204     case SymbolKind::ObjectiveCInstanceVariable:
1205       addSymbol(objc::ivar + symbol->getName());
1206       break;
1207     }
1208   }
1209 }
1210 
1211 void DylibFile::parseReexports(const InterfaceFile &interface) {
1212   const InterfaceFile *topLevel =
1213       interface.getParent() == nullptr ? &interface : interface.getParent();
1214   for (InterfaceFileRef intfRef : interface.reexportedLibraries()) {
1215     InterfaceFile::const_target_range targets = intfRef.targets();
1216     if (is_contained(skipPlatformChecks, intfRef.getInstallName()) ||
1217         is_contained(targets, config->platformInfo.target))
1218       loadReexport(intfRef.getInstallName(), exportingFile, topLevel);
1219   }
1220 }
1221 
1222 // $ld$ symbols modify the properties/behavior of the library (e.g. its install
1223 // name, compatibility version or hide/add symbols) for specific target
1224 // versions.
1225 bool DylibFile::handleLDSymbol(StringRef originalName) {
1226   if (!originalName.startswith("$ld$"))
1227     return false;
1228 
1229   StringRef action;
1230   StringRef name;
1231   std::tie(action, name) = originalName.drop_front(strlen("$ld$")).split('$');
1232   if (action == "previous")
1233     handleLDPreviousSymbol(name, originalName);
1234   else if (action == "install_name")
1235     handleLDInstallNameSymbol(name, originalName);
1236   return true;
1237 }
1238 
1239 void DylibFile::handleLDPreviousSymbol(StringRef name, StringRef originalName) {
1240   // originalName: $ld$ previous $ <installname> $ <compatversion> $
1241   // <platformstr> $ <startversion> $ <endversion> $ <symbol-name> $
1242   StringRef installName;
1243   StringRef compatVersion;
1244   StringRef platformStr;
1245   StringRef startVersion;
1246   StringRef endVersion;
1247   StringRef symbolName;
1248   StringRef rest;
1249 
1250   std::tie(installName, name) = name.split('$');
1251   std::tie(compatVersion, name) = name.split('$');
1252   std::tie(platformStr, name) = name.split('$');
1253   std::tie(startVersion, name) = name.split('$');
1254   std::tie(endVersion, name) = name.split('$');
1255   std::tie(symbolName, rest) = name.split('$');
1256   // TODO: ld64 contains some logic for non-empty symbolName as well.
1257   if (!symbolName.empty())
1258     return;
1259   unsigned platform;
1260   if (platformStr.getAsInteger(10, platform) ||
1261       platform != static_cast<unsigned>(config->platform()))
1262     return;
1263 
1264   VersionTuple start;
1265   if (start.tryParse(startVersion)) {
1266     warn("failed to parse start version, symbol '" + originalName +
1267          "' ignored");
1268     return;
1269   }
1270   VersionTuple end;
1271   if (end.tryParse(endVersion)) {
1272     warn("failed to parse end version, symbol '" + originalName + "' ignored");
1273     return;
1274   }
1275   if (config->platformInfo.minimum < start ||
1276       config->platformInfo.minimum >= end)
1277     return;
1278 
1279   this->installName = saver.save(installName);
1280 
1281   if (!compatVersion.empty()) {
1282     VersionTuple cVersion;
1283     if (cVersion.tryParse(compatVersion)) {
1284       warn("failed to parse compatibility version, symbol '" + originalName +
1285            "' ignored");
1286       return;
1287     }
1288     compatibilityVersion = encodeVersion(cVersion);
1289   }
1290 }
1291 
1292 void DylibFile::handleLDInstallNameSymbol(StringRef name,
1293                                           StringRef originalName) {
1294   // originalName: $ld$ install_name $ os<version> $ install_name
1295   StringRef condition, installName;
1296   std::tie(condition, installName) = name.split('$');
1297   VersionTuple version;
1298   if (!condition.consume_front("os") || version.tryParse(condition))
1299     warn("failed to parse os version, symbol '" + originalName + "' ignored");
1300   else if (version == config->platformInfo.minimum)
1301     this->installName = saver.save(installName);
1302 }
1303 
1304 void DylibFile::checkAppExtensionSafety(bool dylibIsAppExtensionSafe) const {
1305   if (config->applicationExtension && !dylibIsAppExtensionSafe)
1306     warn("using '-application_extension' with unsafe dylib: " + toString(this));
1307 }
1308 
1309 ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f)
1310     : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)) {}
1311 
1312 void ArchiveFile::addLazySymbols() {
1313   for (const object::Archive::Symbol &sym : file->symbols())
1314     symtab->addLazy(sym.getName(), this, sym);
1315 }
1316 
1317 static Expected<InputFile *> loadArchiveMember(MemoryBufferRef mb,
1318                                                uint32_t modTime,
1319                                                StringRef archiveName,
1320                                                uint64_t offsetInArchive) {
1321   if (config->zeroModTime)
1322     modTime = 0;
1323 
1324   switch (identify_magic(mb.getBuffer())) {
1325   case file_magic::macho_object:
1326     return make<ObjFile>(mb, modTime, archiveName);
1327   case file_magic::bitcode:
1328     return make<BitcodeFile>(mb, archiveName, offsetInArchive);
1329   default:
1330     return createStringError(inconvertibleErrorCode(),
1331                              mb.getBufferIdentifier() +
1332                                  " has unhandled file type");
1333   }
1334 }
1335 
1336 Error ArchiveFile::fetch(const object::Archive::Child &c, StringRef reason) {
1337   if (!seen.insert(c.getChildOffset()).second)
1338     return Error::success();
1339 
1340   Expected<MemoryBufferRef> mb = c.getMemoryBufferRef();
1341   if (!mb)
1342     return mb.takeError();
1343 
1344   // Thin archives refer to .o files, so --reproduce needs the .o files too.
1345   if (tar && c.getParent()->isThin())
1346     tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb->getBuffer());
1347 
1348   Expected<TimePoint<std::chrono::seconds>> modTime = c.getLastModified();
1349   if (!modTime)
1350     return modTime.takeError();
1351 
1352   Expected<InputFile *> file =
1353       loadArchiveMember(*mb, toTimeT(*modTime), getName(), c.getChildOffset());
1354 
1355   if (!file)
1356     return file.takeError();
1357 
1358   inputFiles.insert(*file);
1359   printArchiveMemberLoad(reason, *file);
1360   return Error::success();
1361 }
1362 
1363 void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
1364   object::Archive::Child c =
1365       CHECK(sym.getMember(), toString(this) +
1366                                  ": could not get the member defining symbol " +
1367                                  toMachOString(sym));
1368 
1369   // `sym` is owned by a LazySym, which will be replace<>()d by make<ObjFile>
1370   // and become invalid after that call. Copy it to the stack so we can refer
1371   // to it later.
1372   const object::Archive::Symbol symCopy = sym;
1373 
1374   // ld64 doesn't demangle sym here even with -demangle.
1375   // Match that: intentionally don't call toMachOString().
1376   if (Error e = fetch(c, symCopy.getName()))
1377     error(toString(this) + ": could not get the member defining symbol " +
1378           toMachOString(symCopy) + ": " + toString(std::move(e)));
1379 }
1380 
1381 static macho::Symbol *createBitcodeSymbol(const lto::InputFile::Symbol &objSym,
1382                                           BitcodeFile &file) {
1383   StringRef name = saver.save(objSym.getName());
1384 
1385   // TODO: support weak references
1386   if (objSym.isUndefined())
1387     return symtab->addUndefined(name, &file, /*isWeakRef=*/false);
1388 
1389   // TODO: Write a test demonstrating why computing isPrivateExtern before
1390   // LTO compilation is important.
1391   bool isPrivateExtern = false;
1392   switch (objSym.getVisibility()) {
1393   case GlobalValue::HiddenVisibility:
1394     isPrivateExtern = true;
1395     break;
1396   case GlobalValue::ProtectedVisibility:
1397     error(name + " has protected visibility, which is not supported by Mach-O");
1398     break;
1399   case GlobalValue::DefaultVisibility:
1400     break;
1401   }
1402 
1403   if (objSym.isCommon())
1404     return symtab->addCommon(name, &file, objSym.getCommonSize(),
1405                              objSym.getCommonAlignment(), isPrivateExtern);
1406 
1407   return symtab->addDefined(name, &file, /*isec=*/nullptr, /*value=*/0,
1408                             /*size=*/0, objSym.isWeak(), isPrivateExtern,
1409                             /*isThumb=*/false,
1410                             /*isReferencedDynamically=*/false,
1411                             /*noDeadStrip=*/false);
1412 }
1413 
1414 BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
1415                          uint64_t offsetInArchive)
1416     : InputFile(BitcodeKind, mb) {
1417   std::string path = mb.getBufferIdentifier().str();
1418   // ThinLTO assumes that all MemoryBufferRefs given to it have a unique
1419   // name. If two members with the same name are provided, this causes a
1420   // collision and ThinLTO can't proceed.
1421   // So, we append the archive name to disambiguate two members with the same
1422   // name from multiple different archives, and offset within the archive to
1423   // disambiguate two members of the same name from a single archive.
1424   MemoryBufferRef mbref(
1425       mb.getBuffer(),
1426       saver.save(archiveName.empty() ? path
1427                                      : archiveName + sys::path::filename(path) +
1428                                            utostr(offsetInArchive)));
1429 
1430   obj = check(lto::InputFile::create(mbref));
1431 
1432   // Convert LTO Symbols to LLD Symbols in order to perform resolution. The
1433   // "winning" symbol will then be marked as Prevailing at LTO compilation
1434   // time.
1435   for (const lto::InputFile::Symbol &objSym : obj->symbols())
1436     symbols.push_back(createBitcodeSymbol(objSym, *this));
1437 }
1438 
1439 template void ObjFile::parse<LP64>();
1440