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