llvm-project
2360 строк · 94.1 Кб
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 "EhFrame.h"
49#include "ExportTrie.h"
50#include "InputSection.h"
51#include "MachOStructs.h"
52#include "ObjC.h"
53#include "OutputSection.h"
54#include "OutputSegment.h"
55#include "SymbolTable.h"
56#include "Symbols.h"
57#include "SyntheticSections.h"
58#include "Target.h"
59
60#include "lld/Common/CommonLinkerContext.h"
61#include "lld/Common/DWARF.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/BinaryStreamReader.h"
67#include "llvm/Support/Endian.h"
68#include "llvm/Support/LEB128.h"
69#include "llvm/Support/MemoryBuffer.h"
70#include "llvm/Support/Path.h"
71#include "llvm/Support/TarWriter.h"
72#include "llvm/Support/TimeProfiler.h"
73#include "llvm/TextAPI/Architecture.h"
74#include "llvm/TextAPI/InterfaceFile.h"
75
76#include <optional>
77#include <type_traits>
78
79using namespace llvm;
80using namespace llvm::MachO;
81using namespace llvm::support::endian;
82using namespace llvm::sys;
83using namespace lld;
84using namespace lld::macho;
85
86// Returns "<internal>", "foo.a(bar.o)", or "baz.o".
87std::string lld::toString(const InputFile *f) {
88if (!f)
89return "<internal>";
90
91// Multiple dylibs can be defined in one .tbd file.
92if (const auto *dylibFile = dyn_cast<DylibFile>(f))
93if (f->getName().ends_with(".tbd"))
94return (f->getName() + "(" + dylibFile->installName + ")").str();
95
96if (f->archiveName.empty())
97return std::string(f->getName());
98return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
99}
100
101std::string lld::toString(const Section &sec) {
102return (toString(sec.file) + ":(" + sec.name + ")").str();
103}
104
105SetVector<InputFile *> macho::inputFiles;
106std::unique_ptr<TarWriter> macho::tar;
107int InputFile::idCount = 0;
108
109static VersionTuple decodeVersion(uint32_t version) {
110unsigned major = version >> 16;
111unsigned minor = (version >> 8) & 0xffu;
112unsigned subMinor = version & 0xffu;
113return VersionTuple(major, minor, subMinor);
114}
115
116static std::vector<PlatformInfo> getPlatformInfos(const InputFile *input) {
117if (!isa<ObjFile>(input) && !isa<DylibFile>(input))
118return {};
119
120const char *hdr = input->mb.getBufferStart();
121
122// "Zippered" object files can have multiple LC_BUILD_VERSION load commands.
123std::vector<PlatformInfo> platformInfos;
124for (auto *cmd : findCommands<build_version_command>(hdr, LC_BUILD_VERSION)) {
125PlatformInfo info;
126info.target.Platform = static_cast<PlatformType>(cmd->platform);
127info.target.MinDeployment = decodeVersion(cmd->minos);
128platformInfos.emplace_back(std::move(info));
129}
130for (auto *cmd : findCommands<version_min_command>(
131hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
132LC_VERSION_MIN_TVOS, LC_VERSION_MIN_WATCHOS)) {
133PlatformInfo info;
134switch (cmd->cmd) {
135case LC_VERSION_MIN_MACOSX:
136info.target.Platform = PLATFORM_MACOS;
137break;
138case LC_VERSION_MIN_IPHONEOS:
139info.target.Platform = PLATFORM_IOS;
140break;
141case LC_VERSION_MIN_TVOS:
142info.target.Platform = PLATFORM_TVOS;
143break;
144case LC_VERSION_MIN_WATCHOS:
145info.target.Platform = PLATFORM_WATCHOS;
146break;
147}
148info.target.MinDeployment = decodeVersion(cmd->version);
149platformInfos.emplace_back(std::move(info));
150}
151
152return platformInfos;
153}
154
155static bool checkCompatibility(const InputFile *input) {
156std::vector<PlatformInfo> platformInfos = getPlatformInfos(input);
157if (platformInfos.empty())
158return true;
159
160auto it = find_if(platformInfos, [&](const PlatformInfo &info) {
161return removeSimulator(info.target.Platform) ==
162removeSimulator(config->platform());
163});
164if (it == platformInfos.end()) {
165std::string platformNames;
166raw_string_ostream os(platformNames);
167interleave(
168platformInfos, os,
169[&](const PlatformInfo &info) {
170os << getPlatformName(info.target.Platform);
171},
172"/");
173error(toString(input) + " has platform " + platformNames +
174Twine(", which is different from target platform ") +
175getPlatformName(config->platform()));
176return false;
177}
178
179if (it->target.MinDeployment > config->platformInfo.target.MinDeployment)
180warn(toString(input) + " has version " +
181it->target.MinDeployment.getAsString() +
182", which is newer than target minimum of " +
183config->platformInfo.target.MinDeployment.getAsString());
184
185return true;
186}
187
188template <class Header>
189static bool compatWithTargetArch(const InputFile *file, const Header *hdr) {
190uint32_t cpuType;
191std::tie(cpuType, std::ignore) = getCPUTypeFromArchitecture(config->arch());
192
193if (hdr->cputype != cpuType) {
194Architecture arch =
195getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype);
196auto msg = config->errorForArchMismatch
197? static_cast<void (*)(const Twine &)>(error)
198: warn;
199
200msg(toString(file) + " has architecture " + getArchitectureName(arch) +
201" which is incompatible with target architecture " +
202getArchitectureName(config->arch()));
203return false;
204}
205
206return checkCompatibility(file);
207}
208
209// This cache mostly exists to store system libraries (and .tbds) as they're
210// loaded, rather than the input archives, which are already cached at a higher
211// level, and other files like the filelist that are only read once.
212// Theoretically this caching could be more efficient by hoisting it, but that
213// would require altering many callers to track the state.
214DenseMap<CachedHashStringRef, MemoryBufferRef> macho::cachedReads;
215// Open a given file path and return it as a memory-mapped file.
216std::optional<MemoryBufferRef> macho::readFile(StringRef path) {
217CachedHashStringRef key(path);
218auto entry = cachedReads.find(key);
219if (entry != cachedReads.end())
220return entry->second;
221
222ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = MemoryBuffer::getFile(path);
223if (std::error_code ec = mbOrErr.getError()) {
224error("cannot open " + path + ": " + ec.message());
225return std::nullopt;
226}
227
228std::unique_ptr<MemoryBuffer> &mb = *mbOrErr;
229MemoryBufferRef mbref = mb->getMemBufferRef();
230make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take mb ownership
231
232// If this is a regular non-fat file, return it.
233const char *buf = mbref.getBufferStart();
234const auto *hdr = reinterpret_cast<const fat_header *>(buf);
235if (mbref.getBufferSize() < sizeof(uint32_t) ||
236read32be(&hdr->magic) != FAT_MAGIC) {
237if (tar)
238tar->append(relativeToRoot(path), mbref.getBuffer());
239return cachedReads[key] = mbref;
240}
241
242llvm::BumpPtrAllocator &bAlloc = lld::bAlloc();
243
244// Object files and archive files may be fat files, which contain multiple
245// real files for different CPU ISAs. Here, we search for a file that matches
246// with the current link target and returns it as a MemoryBufferRef.
247const auto *arch = reinterpret_cast<const fat_arch *>(buf + sizeof(*hdr));
248auto getArchName = [](uint32_t cpuType, uint32_t cpuSubtype) {
249return getArchitectureName(getArchitectureFromCpuType(cpuType, cpuSubtype));
250};
251
252std::vector<StringRef> archs;
253for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
254if (reinterpret_cast<const char *>(arch + i + 1) >
255buf + mbref.getBufferSize()) {
256error(path + ": fat_arch struct extends beyond end of file");
257return std::nullopt;
258}
259
260uint32_t cpuType = read32be(&arch[i].cputype);
261uint32_t cpuSubtype =
262read32be(&arch[i].cpusubtype) & ~MachO::CPU_SUBTYPE_MASK;
263
264// FIXME: LD64 has a more complex fallback logic here.
265// Consider implementing that as well?
266if (cpuType != static_cast<uint32_t>(target->cpuType) ||
267cpuSubtype != target->cpuSubtype) {
268archs.emplace_back(getArchName(cpuType, cpuSubtype));
269continue;
270}
271
272uint32_t offset = read32be(&arch[i].offset);
273uint32_t size = read32be(&arch[i].size);
274if (offset + size > mbref.getBufferSize())
275error(path + ": slice extends beyond end of file");
276if (tar)
277tar->append(relativeToRoot(path), mbref.getBuffer());
278return cachedReads[key] = MemoryBufferRef(StringRef(buf + offset, size),
279path.copy(bAlloc));
280}
281
282auto targetArchName = getArchName(target->cpuType, target->cpuSubtype);
283warn(path + ": ignoring file because it is universal (" + join(archs, ",") +
284") but does not contain the " + targetArchName + " architecture");
285return std::nullopt;
286}
287
288InputFile::InputFile(Kind kind, const InterfaceFile &interface)
289: id(idCount++), fileKind(kind), name(saver().save(interface.getPath())) {}
290
291// Some sections comprise of fixed-size records, so instead of splitting them at
292// symbol boundaries, we split them based on size. Records are distinct from
293// literals in that they may contain references to other sections, instead of
294// being leaf nodes in the InputSection graph.
295//
296// Note that "record" is a term I came up with. In contrast, "literal" is a term
297// used by the Mach-O format.
298static std::optional<size_t> getRecordSize(StringRef segname, StringRef name) {
299if (name == section_names::compactUnwind) {
300if (segname == segment_names::ld)
301return target->wordSize == 8 ? 32 : 20;
302}
303if (!config->dedupStrings)
304return {};
305
306if (name == section_names::cfString && segname == segment_names::data)
307return target->wordSize == 8 ? 32 : 16;
308
309if (config->icfLevel == ICFLevel::none)
310return {};
311
312if (name == section_names::objcClassRefs && segname == segment_names::data)
313return target->wordSize;
314
315if (name == section_names::objcSelrefs && segname == segment_names::data)
316return target->wordSize;
317return {};
318}
319
320static Error parseCallGraph(ArrayRef<uint8_t> data,
321std::vector<CallGraphEntry> &callGraph) {
322TimeTraceScope timeScope("Parsing call graph section");
323BinaryStreamReader reader(data, llvm::endianness::little);
324while (!reader.empty()) {
325uint32_t fromIndex, toIndex;
326uint64_t count;
327if (Error err = reader.readInteger(fromIndex))
328return err;
329if (Error err = reader.readInteger(toIndex))
330return err;
331if (Error err = reader.readInteger(count))
332return err;
333callGraph.emplace_back(fromIndex, toIndex, count);
334}
335return Error::success();
336}
337
338// Parse the sequence of sections within a single LC_SEGMENT(_64).
339// Split each section into subsections.
340template <class SectionHeader>
341void ObjFile::parseSections(ArrayRef<SectionHeader> sectionHeaders) {
342sections.reserve(sectionHeaders.size());
343auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
344
345for (const SectionHeader &sec : sectionHeaders) {
346StringRef name =
347StringRef(sec.sectname, strnlen(sec.sectname, sizeof(sec.sectname)));
348StringRef segname =
349StringRef(sec.segname, strnlen(sec.segname, sizeof(sec.segname)));
350sections.push_back(make<Section>(this, segname, name, sec.flags, sec.addr));
351if (sec.align >= 32) {
352error("alignment " + std::to_string(sec.align) + " of section " + name +
353" is too large");
354continue;
355}
356Section §ion = *sections.back();
357uint32_t align = 1 << sec.align;
358ArrayRef<uint8_t> data = {isZeroFill(sec.flags) ? nullptr
359: buf + sec.offset,
360static_cast<size_t>(sec.size)};
361
362auto splitRecords = [&](size_t recordSize) -> void {
363if (data.empty())
364return;
365Subsections &subsections = section.subsections;
366subsections.reserve(data.size() / recordSize);
367for (uint64_t off = 0; off < data.size(); off += recordSize) {
368auto *isec = make<ConcatInputSection>(
369section, data.slice(off, std::min(data.size(), recordSize)), align);
370subsections.push_back({off, isec});
371}
372section.doneSplitting = true;
373};
374
375if (sectionType(sec.flags) == S_CSTRING_LITERALS) {
376if (sec.nreloc)
377fatal(toString(this) + ": " + sec.segname + "," + sec.sectname +
378" contains relocations, which is unsupported");
379bool dedupLiterals =
380name == section_names::objcMethname || config->dedupStrings;
381InputSection *isec =
382make<CStringInputSection>(section, data, align, dedupLiterals);
383// FIXME: parallelize this?
384cast<CStringInputSection>(isec)->splitIntoPieces();
385section.subsections.push_back({0, isec});
386} else if (isWordLiteralSection(sec.flags)) {
387if (sec.nreloc)
388fatal(toString(this) + ": " + sec.segname + "," + sec.sectname +
389" contains relocations, which is unsupported");
390InputSection *isec = make<WordLiteralInputSection>(section, data, align);
391section.subsections.push_back({0, isec});
392} else if (auto recordSize = getRecordSize(segname, name)) {
393splitRecords(*recordSize);
394} else if (name == section_names::ehFrame &&
395segname == segment_names::text) {
396splitEhFrames(data, *sections.back());
397} else if (segname == segment_names::llvm) {
398if (config->callGraphProfileSort && name == section_names::cgProfile)
399checkError(parseCallGraph(data, callGraph));
400// ld64 does not appear to emit contents from sections within the __LLVM
401// segment. Symbols within those sections point to bitcode metadata
402// instead of actual symbols. Global symbols within those sections could
403// have the same name without causing duplicate symbol errors. To avoid
404// spurious duplicate symbol errors, we do not parse these sections.
405// TODO: Evaluate whether the bitcode metadata is needed.
406} else if (name == section_names::objCImageInfo &&
407segname == segment_names::data) {
408objCImageInfo = data;
409} else {
410if (name == section_names::addrSig)
411addrSigSection = sections.back();
412
413auto *isec = make<ConcatInputSection>(section, data, align);
414if (isDebugSection(isec->getFlags()) &&
415isec->getSegName() == segment_names::dwarf) {
416// Instead of emitting DWARF sections, we emit STABS symbols to the
417// object files that contain them. We filter them out early to avoid
418// parsing their relocations unnecessarily.
419debugSections.push_back(isec);
420} else {
421section.subsections.push_back({0, isec});
422}
423}
424}
425}
426
427void ObjFile::splitEhFrames(ArrayRef<uint8_t> data, Section &ehFrameSection) {
428EhReader reader(this, data, /*dataOff=*/0);
429size_t off = 0;
430while (off < reader.size()) {
431uint64_t frameOff = off;
432uint64_t length = reader.readLength(&off);
433if (length == 0)
434break;
435uint64_t fullLength = length + (off - frameOff);
436off += length;
437// We hard-code an alignment of 1 here because we don't actually want our
438// EH frames to be aligned to the section alignment. EH frame decoders don't
439// expect this alignment. Moreover, each EH frame must start where the
440// previous one ends, and where it ends is indicated by the length field.
441// Unless we update the length field (troublesome), we should keep the
442// alignment to 1.
443// Note that we still want to preserve the alignment of the overall section,
444// just not of the individual EH frames.
445ehFrameSection.subsections.push_back(
446{frameOff, make<ConcatInputSection>(ehFrameSection,
447data.slice(frameOff, fullLength),
448/*align=*/1)});
449}
450ehFrameSection.doneSplitting = true;
451}
452
453template <class T>
454static Section *findContainingSection(const std::vector<Section *> §ions,
455T *offset) {
456static_assert(std::is_same<uint64_t, T>::value ||
457std::is_same<uint32_t, T>::value,
458"unexpected type for offset");
459auto it = std::prev(llvm::upper_bound(
460sections, *offset,
461[](uint64_t value, const Section *sec) { return value < sec->addr; }));
462*offset -= (*it)->addr;
463return *it;
464}
465
466// Find the subsection corresponding to the greatest section offset that is <=
467// that of the given offset.
468//
469// offset: an offset relative to the start of the original InputSection (before
470// any subsection splitting has occurred). It will be updated to represent the
471// same location as an offset relative to the start of the containing
472// subsection.
473template <class T>
474static InputSection *findContainingSubsection(const Section §ion,
475T *offset) {
476static_assert(std::is_same<uint64_t, T>::value ||
477std::is_same<uint32_t, T>::value,
478"unexpected type for offset");
479auto it = std::prev(llvm::upper_bound(
480section.subsections, *offset,
481[](uint64_t value, Subsection subsec) { return value < subsec.offset; }));
482*offset -= it->offset;
483return it->isec;
484}
485
486// Find a symbol at offset `off` within `isec`.
487static Defined *findSymbolAtOffset(const ConcatInputSection *isec,
488uint64_t off) {
489auto it = llvm::lower_bound(isec->symbols, off, [](Defined *d, uint64_t off) {
490return d->value < off;
491});
492// The offset should point at the exact address of a symbol (with no addend.)
493if (it == isec->symbols.end() || (*it)->value != off) {
494assert(isec->wasCoalesced);
495return nullptr;
496}
497return *it;
498}
499
500template <class SectionHeader>
501static bool validateRelocationInfo(InputFile *file, const SectionHeader &sec,
502relocation_info rel) {
503const RelocAttrs &relocAttrs = target->getRelocAttrs(rel.r_type);
504bool valid = true;
505auto message = [relocAttrs, file, sec, rel, &valid](const Twine &diagnostic) {
506valid = false;
507return (relocAttrs.name + " relocation " + diagnostic + " at offset " +
508std::to_string(rel.r_address) + " of " + sec.segname + "," +
509sec.sectname + " in " + toString(file))
510.str();
511};
512
513if (!relocAttrs.hasAttr(RelocAttrBits::LOCAL) && !rel.r_extern)
514error(message("must be extern"));
515if (relocAttrs.hasAttr(RelocAttrBits::PCREL) != rel.r_pcrel)
516error(message(Twine("must ") + (rel.r_pcrel ? "not " : "") +
517"be PC-relative"));
518if (isThreadLocalVariables(sec.flags) &&
519!relocAttrs.hasAttr(RelocAttrBits::UNSIGNED))
520error(message("not allowed in thread-local section, must be UNSIGNED"));
521if (rel.r_length < 2 || rel.r_length > 3 ||
522!relocAttrs.hasAttr(static_cast<RelocAttrBits>(1 << rel.r_length))) {
523static SmallVector<StringRef, 4> widths{"0", "4", "8", "4 or 8"};
524error(message("has width " + std::to_string(1 << rel.r_length) +
525" bytes, but must be " +
526widths[(static_cast<int>(relocAttrs.bits) >> 2) & 3] +
527" bytes"));
528}
529return valid;
530}
531
532template <class SectionHeader>
533void ObjFile::parseRelocations(ArrayRef<SectionHeader> sectionHeaders,
534const SectionHeader &sec, Section §ion) {
535auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
536ArrayRef<relocation_info> relInfos(
537reinterpret_cast<const relocation_info *>(buf + sec.reloff), sec.nreloc);
538
539Subsections &subsections = section.subsections;
540auto subsecIt = subsections.rbegin();
541for (size_t i = 0; i < relInfos.size(); i++) {
542// Paired relocations serve as Mach-O's method for attaching a
543// supplemental datum to a primary relocation record. ELF does not
544// need them because the *_RELOC_RELA records contain the extra
545// addend field, vs. *_RELOC_REL which omit the addend.
546//
547// The {X86_64,ARM64}_RELOC_SUBTRACTOR record holds the subtrahend,
548// and the paired *_RELOC_UNSIGNED record holds the minuend. The
549// datum for each is a symbolic address. The result is the offset
550// between two addresses.
551//
552// The ARM64_RELOC_ADDEND record holds the addend, and the paired
553// ARM64_RELOC_BRANCH26 or ARM64_RELOC_PAGE21/PAGEOFF12 holds the
554// base symbolic address.
555//
556// Note: X86 does not use *_RELOC_ADDEND because it can embed an addend into
557// the instruction stream. On X86, a relocatable address field always
558// occupies an entire contiguous sequence of byte(s), so there is no need to
559// merge opcode bits with address bits. Therefore, it's easy and convenient
560// to store addends in the instruction-stream bytes that would otherwise
561// contain zeroes. By contrast, RISC ISAs such as ARM64 mix opcode bits with
562// address bits so that bitwise arithmetic is necessary to extract and
563// insert them. Storing addends in the instruction stream is possible, but
564// inconvenient and more costly at link time.
565
566relocation_info relInfo = relInfos[i];
567bool isSubtrahend =
568target->hasAttr(relInfo.r_type, RelocAttrBits::SUBTRAHEND);
569int64_t pairedAddend = 0;
570if (target->hasAttr(relInfo.r_type, RelocAttrBits::ADDEND)) {
571pairedAddend = SignExtend64<24>(relInfo.r_symbolnum);
572relInfo = relInfos[++i];
573}
574assert(i < relInfos.size());
575if (!validateRelocationInfo(this, sec, relInfo))
576continue;
577if (relInfo.r_address & R_SCATTERED)
578fatal("TODO: Scattered relocations not supported");
579
580int64_t embeddedAddend = target->getEmbeddedAddend(mb, sec.offset, relInfo);
581assert(!(embeddedAddend && pairedAddend));
582int64_t totalAddend = pairedAddend + embeddedAddend;
583Reloc r;
584r.type = relInfo.r_type;
585r.pcrel = relInfo.r_pcrel;
586r.length = relInfo.r_length;
587r.offset = relInfo.r_address;
588if (relInfo.r_extern) {
589r.referent = symbols[relInfo.r_symbolnum];
590r.addend = isSubtrahend ? 0 : totalAddend;
591} else {
592assert(!isSubtrahend);
593const SectionHeader &referentSecHead =
594sectionHeaders[relInfo.r_symbolnum - 1];
595uint64_t referentOffset;
596if (relInfo.r_pcrel) {
597// The implicit addend for pcrel section relocations is the pcrel offset
598// in terms of the addresses in the input file. Here we adjust it so
599// that it describes the offset from the start of the referent section.
600// FIXME This logic was written around x86_64 behavior -- ARM64 doesn't
601// have pcrel section relocations. We may want to factor this out into
602// the arch-specific .cpp file.
603assert(target->hasAttr(r.type, RelocAttrBits::BYTE4));
604referentOffset = sec.addr + relInfo.r_address + 4 + totalAddend -
605referentSecHead.addr;
606} else {
607// The addend for a non-pcrel relocation is its absolute address.
608referentOffset = totalAddend - referentSecHead.addr;
609}
610r.referent = findContainingSubsection(*sections[relInfo.r_symbolnum - 1],
611&referentOffset);
612r.addend = referentOffset;
613}
614
615// Find the subsection that this relocation belongs to.
616// Though not required by the Mach-O format, clang and gcc seem to emit
617// relocations in order, so let's take advantage of it. However, ld64 emits
618// unsorted relocations (in `-r` mode), so we have a fallback for that
619// uncommon case.
620InputSection *subsec;
621while (subsecIt != subsections.rend() && subsecIt->offset > r.offset)
622++subsecIt;
623if (subsecIt == subsections.rend() ||
624subsecIt->offset + subsecIt->isec->getSize() <= r.offset) {
625subsec = findContainingSubsection(section, &r.offset);
626// Now that we know the relocs are unsorted, avoid trying the 'fast path'
627// for the other relocations.
628subsecIt = subsections.rend();
629} else {
630subsec = subsecIt->isec;
631r.offset -= subsecIt->offset;
632}
633subsec->relocs.push_back(r);
634
635if (isSubtrahend) {
636relocation_info minuendInfo = relInfos[++i];
637// SUBTRACTOR relocations should always be followed by an UNSIGNED one
638// attached to the same address.
639assert(target->hasAttr(minuendInfo.r_type, RelocAttrBits::UNSIGNED) &&
640relInfo.r_address == minuendInfo.r_address);
641Reloc p;
642p.type = minuendInfo.r_type;
643if (minuendInfo.r_extern) {
644p.referent = symbols[minuendInfo.r_symbolnum];
645p.addend = totalAddend;
646} else {
647uint64_t referentOffset =
648totalAddend - sectionHeaders[minuendInfo.r_symbolnum - 1].addr;
649p.referent = findContainingSubsection(
650*sections[minuendInfo.r_symbolnum - 1], &referentOffset);
651p.addend = referentOffset;
652}
653subsec->relocs.push_back(p);
654}
655}
656}
657
658template <class NList>
659static macho::Symbol *createDefined(const NList &sym, StringRef name,
660InputSection *isec, uint64_t value,
661uint64_t size, bool forceHidden) {
662// Symbol scope is determined by sym.n_type & (N_EXT | N_PEXT):
663// N_EXT: Global symbols. These go in the symbol table during the link,
664// and also in the export table of the output so that the dynamic
665// linker sees them.
666// N_EXT | N_PEXT: Linkage unit (think: dylib) scoped. These go in the
667// symbol table during the link so that duplicates are
668// either reported (for non-weak symbols) or merged
669// (for weak symbols), but they do not go in the export
670// table of the output.
671// N_PEXT: llvm-mc does not emit these, but `ld -r` (wherein ld64 emits
672// object files) may produce them. LLD does not yet support -r.
673// These are translation-unit scoped, identical to the `0` case.
674// 0: Translation-unit scoped. These are not in the symbol table during
675// link, and not in the export table of the output either.
676bool isWeakDefCanBeHidden =
677(sym.n_desc & (N_WEAK_DEF | N_WEAK_REF)) == (N_WEAK_DEF | N_WEAK_REF);
678
679assert(!(sym.n_desc & N_ARM_THUMB_DEF) && "ARM32 arch is not supported");
680
681if (sym.n_type & N_EXT) {
682// -load_hidden makes us treat global symbols as linkage unit scoped.
683// Duplicates are reported but the symbol does not go in the export trie.
684bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;
685
686// lld's behavior for merging symbols is slightly different from ld64:
687// ld64 picks the winning symbol based on several criteria (see
688// pickBetweenRegularAtoms() in ld64's SymbolTable.cpp), while lld
689// just merges metadata and keeps the contents of the first symbol
690// with that name (see SymbolTable::addDefined). For:
691// * inline function F in a TU built with -fvisibility-inlines-hidden
692// * and inline function F in another TU built without that flag
693// ld64 will pick the one from the file built without
694// -fvisibility-inlines-hidden.
695// lld will instead pick the one listed first on the link command line and
696// give it visibility as if the function was built without
697// -fvisibility-inlines-hidden.
698// If both functions have the same contents, this will have the same
699// behavior. If not, it won't, but the input had an ODR violation in
700// that case.
701//
702// Similarly, merging a symbol
703// that's isPrivateExtern and not isWeakDefCanBeHidden with one
704// that's not isPrivateExtern but isWeakDefCanBeHidden technically
705// should produce one
706// that's not isPrivateExtern but isWeakDefCanBeHidden. That matters
707// with ld64's semantics, because it means the non-private-extern
708// definition will continue to take priority if more private extern
709// definitions are encountered. With lld's semantics there's no observable
710// difference between a symbol that's isWeakDefCanBeHidden(autohide) or one
711// that's privateExtern -- neither makes it into the dynamic symbol table,
712// unless the autohide symbol is explicitly exported.
713// But if a symbol is both privateExtern and autohide then it can't
714// be exported.
715// So we nullify the autohide flag when privateExtern is present
716// and promote the symbol to privateExtern when it is not already.
717if (isWeakDefCanBeHidden && isPrivateExtern)
718isWeakDefCanBeHidden = false;
719else if (isWeakDefCanBeHidden)
720isPrivateExtern = true;
721return symtab->addDefined(
722name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
723isPrivateExtern, sym.n_desc & REFERENCED_DYNAMICALLY,
724sym.n_desc & N_NO_DEAD_STRIP, isWeakDefCanBeHidden);
725}
726bool includeInSymtab = !isPrivateLabel(name) && !isEhFrameSection(isec);
727return make<Defined>(
728name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
729/*isExternal=*/false, /*isPrivateExtern=*/false, includeInSymtab,
730sym.n_desc & REFERENCED_DYNAMICALLY, sym.n_desc & N_NO_DEAD_STRIP);
731}
732
733// Absolute symbols are defined symbols that do not have an associated
734// InputSection. They cannot be weak.
735template <class NList>
736static macho::Symbol *createAbsolute(const NList &sym, InputFile *file,
737StringRef name, bool forceHidden) {
738assert(!(sym.n_desc & N_ARM_THUMB_DEF) && "ARM32 arch is not supported");
739
740if (sym.n_type & N_EXT) {
741bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;
742return symtab->addDefined(name, file, nullptr, sym.n_value, /*size=*/0,
743/*isWeakDef=*/false, isPrivateExtern,
744/*isReferencedDynamically=*/false,
745sym.n_desc & N_NO_DEAD_STRIP,
746/*isWeakDefCanBeHidden=*/false);
747}
748return make<Defined>(name, file, nullptr, sym.n_value, /*size=*/0,
749/*isWeakDef=*/false,
750/*isExternal=*/false, /*isPrivateExtern=*/false,
751/*includeInSymtab=*/true,
752/*isReferencedDynamically=*/false,
753sym.n_desc & N_NO_DEAD_STRIP);
754}
755
756template <class NList>
757macho::Symbol *ObjFile::parseNonSectionSymbol(const NList &sym,
758const char *strtab) {
759StringRef name = StringRef(strtab + sym.n_strx);
760uint8_t type = sym.n_type & N_TYPE;
761bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;
762switch (type) {
763case N_UNDF:
764return sym.n_value == 0
765? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)
766: symtab->addCommon(name, this, sym.n_value,
7671 << GET_COMM_ALIGN(sym.n_desc),
768isPrivateExtern);
769case N_ABS:
770return createAbsolute(sym, this, name, forceHidden);
771case N_INDR: {
772// Not much point in making local aliases -- relocs in the current file can
773// just refer to the actual symbol itself. ld64 ignores these symbols too.
774if (!(sym.n_type & N_EXT))
775return nullptr;
776StringRef aliasedName = StringRef(strtab + sym.n_value);
777// isPrivateExtern is the only symbol flag that has an impact on the final
778// aliased symbol.
779auto *alias = make<AliasSymbol>(this, name, aliasedName, isPrivateExtern);
780aliases.push_back(alias);
781return alias;
782}
783case N_PBUD:
784error("TODO: support symbols of type N_PBUD");
785return nullptr;
786case N_SECT:
787llvm_unreachable(
788"N_SECT symbols should not be passed to parseNonSectionSymbol");
789default:
790llvm_unreachable("invalid symbol type");
791}
792}
793
794template <class NList> static bool isUndef(const NList &sym) {
795return (sym.n_type & N_TYPE) == N_UNDF && sym.n_value == 0;
796}
797
798template <class LP>
799void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
800ArrayRef<typename LP::nlist> nList,
801const char *strtab, bool subsectionsViaSymbols) {
802using NList = typename LP::nlist;
803
804// Groups indices of the symbols by the sections that contain them.
805std::vector<std::vector<uint32_t>> symbolsBySection(sections.size());
806symbols.resize(nList.size());
807SmallVector<unsigned, 32> undefineds;
808for (uint32_t i = 0; i < nList.size(); ++i) {
809const NList &sym = nList[i];
810
811// Ignore debug symbols for now.
812// FIXME: may need special handling.
813if (sym.n_type & N_STAB)
814continue;
815
816if ((sym.n_type & N_TYPE) == N_SECT) {
817Subsections &subsections = sections[sym.n_sect - 1]->subsections;
818// parseSections() may have chosen not to parse this section.
819if (subsections.empty())
820continue;
821symbolsBySection[sym.n_sect - 1].push_back(i);
822} else if (isUndef(sym)) {
823undefineds.push_back(i);
824} else {
825symbols[i] = parseNonSectionSymbol(sym, strtab);
826}
827}
828
829for (size_t i = 0; i < sections.size(); ++i) {
830Subsections &subsections = sections[i]->subsections;
831if (subsections.empty())
832continue;
833std::vector<uint32_t> &symbolIndices = symbolsBySection[i];
834uint64_t sectionAddr = sectionHeaders[i].addr;
835uint32_t sectionAlign = 1u << sectionHeaders[i].align;
836
837// Some sections have already been split into subsections during
838// parseSections(), so we simply need to match Symbols to the corresponding
839// subsection here.
840if (sections[i]->doneSplitting) {
841for (size_t j = 0; j < symbolIndices.size(); ++j) {
842const uint32_t symIndex = symbolIndices[j];
843const NList &sym = nList[symIndex];
844StringRef name = strtab + sym.n_strx;
845uint64_t symbolOffset = sym.n_value - sectionAddr;
846InputSection *isec =
847findContainingSubsection(*sections[i], &symbolOffset);
848if (symbolOffset != 0) {
849error(toString(*sections[i]) + ": symbol " + name +
850" at misaligned offset");
851continue;
852}
853symbols[symIndex] =
854createDefined(sym, name, isec, 0, isec->getSize(), forceHidden);
855}
856continue;
857}
858sections[i]->doneSplitting = true;
859
860auto getSymName = [strtab](const NList& sym) -> StringRef {
861return StringRef(strtab + sym.n_strx);
862};
863
864// Calculate symbol sizes and create subsections by splitting the sections
865// along symbol boundaries.
866// We populate subsections by repeatedly splitting the last (highest
867// address) subsection.
868llvm::stable_sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {
869// Put extern weak symbols after other symbols at the same address so
870// that weak symbol coalescing works correctly. See
871// SymbolTable::addDefined() for details.
872if (nList[lhs].n_value == nList[rhs].n_value &&
873nList[lhs].n_type & N_EXT && nList[rhs].n_type & N_EXT)
874return !(nList[lhs].n_desc & N_WEAK_DEF) && (nList[rhs].n_desc & N_WEAK_DEF);
875return nList[lhs].n_value < nList[rhs].n_value;
876});
877for (size_t j = 0; j < symbolIndices.size(); ++j) {
878const uint32_t symIndex = symbolIndices[j];
879const NList &sym = nList[symIndex];
880StringRef name = getSymName(sym);
881Subsection &subsec = subsections.back();
882InputSection *isec = subsec.isec;
883
884uint64_t subsecAddr = sectionAddr + subsec.offset;
885size_t symbolOffset = sym.n_value - subsecAddr;
886uint64_t symbolSize =
887j + 1 < symbolIndices.size()
888? nList[symbolIndices[j + 1]].n_value - sym.n_value
889: isec->data.size() - symbolOffset;
890// There are 4 cases where we do not need to create a new subsection:
891// 1. If the input file does not use subsections-via-symbols.
892// 2. Multiple symbols at the same address only induce one subsection.
893// (The symbolOffset == 0 check covers both this case as well as
894// the first loop iteration.)
895// 3. Alternative entry points do not induce new subsections.
896// 4. If we have a literal section (e.g. __cstring and __literal4).
897if (!subsectionsViaSymbols || symbolOffset == 0 ||
898sym.n_desc & N_ALT_ENTRY || !isa<ConcatInputSection>(isec)) {
899isec->hasAltEntry = symbolOffset != 0;
900symbols[symIndex] = createDefined(sym, name, isec, symbolOffset,
901symbolSize, forceHidden);
902continue;
903}
904auto *concatIsec = cast<ConcatInputSection>(isec);
905
906auto *nextIsec = make<ConcatInputSection>(*concatIsec);
907nextIsec->wasCoalesced = false;
908if (isZeroFill(isec->getFlags())) {
909// Zero-fill sections have NULL data.data() non-zero data.size()
910nextIsec->data = {nullptr, isec->data.size() - symbolOffset};
911isec->data = {nullptr, symbolOffset};
912} else {
913nextIsec->data = isec->data.slice(symbolOffset);
914isec->data = isec->data.slice(0, symbolOffset);
915}
916
917// By construction, the symbol will be at offset zero in the new
918// subsection.
919symbols[symIndex] = createDefined(sym, name, nextIsec, /*value=*/0,
920symbolSize, forceHidden);
921// TODO: ld64 appears to preserve the original alignment as well as each
922// subsection's offset from the last aligned address. We should consider
923// emulating that behavior.
924nextIsec->align = MinAlign(sectionAlign, sym.n_value);
925subsections.push_back({sym.n_value - sectionAddr, nextIsec});
926}
927}
928
929// Undefined symbols can trigger recursive fetch from Archives due to
930// LazySymbols. Process defined symbols first so that the relative order
931// between a defined symbol and an undefined symbol does not change the
932// symbol resolution behavior. In addition, a set of interconnected symbols
933// will all be resolved to the same file, instead of being resolved to
934// different files.
935for (unsigned i : undefineds)
936symbols[i] = parseNonSectionSymbol(nList[i], strtab);
937}
938
939OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,
940StringRef sectName)
941: InputFile(OpaqueKind, mb) {
942const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
943ArrayRef<uint8_t> data = {buf, mb.getBufferSize()};
944sections.push_back(make<Section>(/*file=*/this, segName.take_front(16),
945sectName.take_front(16),
946/*flags=*/0, /*addr=*/0));
947Section §ion = *sections.back();
948ConcatInputSection *isec = make<ConcatInputSection>(section, data);
949isec->live = true;
950section.subsections.push_back({0, isec});
951}
952
953template <class LP>
954void ObjFile::parseLinkerOptions(SmallVectorImpl<StringRef> &LCLinkerOptions) {
955using Header = typename LP::mach_header;
956auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());
957
958for (auto *cmd : findCommands<linker_option_command>(hdr, LC_LINKER_OPTION)) {
959StringRef data{reinterpret_cast<const char *>(cmd + 1),
960cmd->cmdsize - sizeof(linker_option_command)};
961parseLCLinkerOption(LCLinkerOptions, this, cmd->count, data);
962}
963}
964
965SmallVector<StringRef> macho::unprocessedLCLinkerOptions;
966ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,
967bool lazy, bool forceHidden, bool compatArch,
968bool builtFromBitcode)
969: InputFile(ObjKind, mb, lazy), modTime(modTime), forceHidden(forceHidden),
970builtFromBitcode(builtFromBitcode) {
971this->archiveName = std::string(archiveName);
972this->compatArch = compatArch;
973if (lazy) {
974if (target->wordSize == 8)
975parseLazy<LP64>();
976else
977parseLazy<ILP32>();
978} else {
979if (target->wordSize == 8)
980parse<LP64>();
981else
982parse<ILP32>();
983}
984}
985
986template <class LP> void ObjFile::parse() {
987using Header = typename LP::mach_header;
988using SegmentCommand = typename LP::segment_command;
989using SectionHeader = typename LP::section;
990using NList = typename LP::nlist;
991
992auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
993auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());
994
995// If we've already checked the arch, then don't need to check again.
996if (!compatArch)
997return;
998if (!(compatArch = compatWithTargetArch(this, hdr)))
999return;
1000
1001// We will resolve LC linker options once all native objects are loaded after
1002// LTO is finished.
1003SmallVector<StringRef, 4> LCLinkerOptions;
1004parseLinkerOptions<LP>(LCLinkerOptions);
1005unprocessedLCLinkerOptions.append(LCLinkerOptions);
1006
1007ArrayRef<SectionHeader> sectionHeaders;
1008if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {
1009auto *c = reinterpret_cast<const SegmentCommand *>(cmd);
1010sectionHeaders = ArrayRef<SectionHeader>{
1011reinterpret_cast<const SectionHeader *>(c + 1), c->nsects};
1012parseSections(sectionHeaders);
1013}
1014
1015// TODO: Error on missing LC_SYMTAB?
1016if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {
1017auto *c = reinterpret_cast<const symtab_command *>(cmd);
1018ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
1019c->nsyms);
1020const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
1021bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
1022parseSymbols<LP>(sectionHeaders, nList, strtab, subsectionsViaSymbols);
1023}
1024
1025// The relocations may refer to the symbols, so we parse them after we have
1026// parsed all the symbols.
1027for (size_t i = 0, n = sections.size(); i < n; ++i)
1028if (!sections[i]->subsections.empty())
1029parseRelocations(sectionHeaders, sectionHeaders[i], *sections[i]);
1030
1031parseDebugInfo();
1032
1033Section *ehFrameSection = nullptr;
1034Section *compactUnwindSection = nullptr;
1035for (Section *sec : sections) {
1036Section **s = StringSwitch<Section **>(sec->name)
1037.Case(section_names::compactUnwind, &compactUnwindSection)
1038.Case(section_names::ehFrame, &ehFrameSection)
1039.Default(nullptr);
1040if (s)
1041*s = sec;
1042}
1043if (compactUnwindSection)
1044registerCompactUnwind(*compactUnwindSection);
1045if (ehFrameSection)
1046registerEhFrames(*ehFrameSection);
1047}
1048
1049template <class LP> void ObjFile::parseLazy() {
1050using Header = typename LP::mach_header;
1051using NList = typename LP::nlist;
1052
1053auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
1054auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());
1055
1056if (!compatArch)
1057return;
1058if (!(compatArch = compatWithTargetArch(this, hdr)))
1059return;
1060
1061const load_command *cmd = findCommand(hdr, LC_SYMTAB);
1062if (!cmd)
1063return;
1064auto *c = reinterpret_cast<const symtab_command *>(cmd);
1065ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
1066c->nsyms);
1067const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
1068symbols.resize(nList.size());
1069for (const auto &[i, sym] : llvm::enumerate(nList)) {
1070if ((sym.n_type & N_EXT) && !isUndef(sym)) {
1071// TODO: Bound checking
1072StringRef name = strtab + sym.n_strx;
1073symbols[i] = symtab->addLazyObject(name, *this);
1074if (!lazy)
1075break;
1076}
1077}
1078}
1079
1080void ObjFile::parseDebugInfo() {
1081std::unique_ptr<DwarfObject> dObj = DwarfObject::create(this);
1082if (!dObj)
1083return;
1084
1085// We do not re-use the context from getDwarf() here as that function
1086// constructs an expensive DWARFCache object.
1087auto *ctx = make<DWARFContext>(
1088std::move(dObj), "",
1089[&](Error err) {
1090warn(toString(this) + ": " + toString(std::move(err)));
1091},
1092[&](Error warning) {
1093warn(toString(this) + ": " + toString(std::move(warning)));
1094});
1095
1096// TODO: Since object files can contain a lot of DWARF info, we should verify
1097// that we are parsing just the info we need
1098const DWARFContext::compile_unit_range &units = ctx->compile_units();
1099// FIXME: There can be more than one compile unit per object file. See
1100// PR48637.
1101auto it = units.begin();
1102compileUnit = it != units.end() ? it->get() : nullptr;
1103}
1104
1105ArrayRef<data_in_code_entry> ObjFile::getDataInCode() const {
1106const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
1107const load_command *cmd = findCommand(buf, LC_DATA_IN_CODE);
1108if (!cmd)
1109return {};
1110const auto *c = reinterpret_cast<const linkedit_data_command *>(cmd);
1111return {reinterpret_cast<const data_in_code_entry *>(buf + c->dataoff),
1112c->datasize / sizeof(data_in_code_entry)};
1113}
1114
1115ArrayRef<uint8_t> ObjFile::getOptimizationHints() const {
1116const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
1117if (auto *cmd =
1118findCommand<linkedit_data_command>(buf, LC_LINKER_OPTIMIZATION_HINT))
1119return {buf + cmd->dataoff, cmd->datasize};
1120return {};
1121}
1122
1123// Create pointers from symbols to their associated compact unwind entries.
1124void ObjFile::registerCompactUnwind(Section &compactUnwindSection) {
1125for (const Subsection &subsection : compactUnwindSection.subsections) {
1126ConcatInputSection *isec = cast<ConcatInputSection>(subsection.isec);
1127// Hack!! Each compact unwind entry (CUE) has its UNSIGNED relocations embed
1128// their addends in its data. Thus if ICF operated naively and compared the
1129// entire contents of each CUE, entries with identical unwind info but e.g.
1130// belonging to different functions would never be considered equivalent. To
1131// work around this problem, we remove some parts of the data containing the
1132// embedded addends. In particular, we remove the function address and LSDA
1133// pointers. Since these locations are at the start and end of the entry,
1134// we can do this using a simple, efficient slice rather than performing a
1135// copy. We are not losing any information here because the embedded
1136// addends have already been parsed in the corresponding Reloc structs.
1137//
1138// Removing these pointers would not be safe if they were pointers to
1139// absolute symbols. In that case, there would be no corresponding
1140// relocation. However, (AFAIK) MC cannot emit references to absolute
1141// symbols for either the function address or the LSDA. However, it *can* do
1142// so for the personality pointer, so we are not slicing that field away.
1143//
1144// Note that we do not adjust the offsets of the corresponding relocations;
1145// instead, we rely on `relocateCompactUnwind()` to correctly handle these
1146// truncated input sections.
1147isec->data = isec->data.slice(target->wordSize, 8 + target->wordSize);
1148uint32_t encoding = read32le(isec->data.data() + sizeof(uint32_t));
1149// llvm-mc omits CU entries for functions that need DWARF encoding, but
1150// `ld -r` doesn't. We can ignore them because we will re-synthesize these
1151// CU entries from the DWARF info during the output phase.
1152if ((encoding & static_cast<uint32_t>(UNWIND_MODE_MASK)) ==
1153target->modeDwarfEncoding)
1154continue;
1155
1156ConcatInputSection *referentIsec;
1157for (auto it = isec->relocs.begin(); it != isec->relocs.end();) {
1158Reloc &r = *it;
1159// CUE::functionAddress is at offset 0. Skip personality & LSDA relocs.
1160if (r.offset != 0) {
1161++it;
1162continue;
1163}
1164uint64_t add = r.addend;
1165if (auto *sym = cast_or_null<Defined>(r.referent.dyn_cast<Symbol *>())) {
1166// Check whether the symbol defined in this file is the prevailing one.
1167// Skip if it is e.g. a weak def that didn't prevail.
1168if (sym->getFile() != this) {
1169++it;
1170continue;
1171}
1172add += sym->value;
1173referentIsec = cast<ConcatInputSection>(sym->isec());
1174} else {
1175referentIsec =
1176cast<ConcatInputSection>(r.referent.dyn_cast<InputSection *>());
1177}
1178// Unwind info lives in __DATA, and finalization of __TEXT will occur
1179// before finalization of __DATA. Moreover, the finalization of unwind
1180// info depends on the exact addresses that it references. So it is safe
1181// for compact unwind to reference addresses in __TEXT, but not addresses
1182// in any other segment.
1183if (referentIsec->getSegName() != segment_names::text)
1184error(isec->getLocation(r.offset) + " references section " +
1185referentIsec->getName() + " which is not in segment __TEXT");
1186// The functionAddress relocations are typically section relocations.
1187// However, unwind info operates on a per-symbol basis, so we search for
1188// the function symbol here.
1189Defined *d = findSymbolAtOffset(referentIsec, add);
1190if (!d) {
1191++it;
1192continue;
1193}
1194d->originalUnwindEntry = isec;
1195// Now that the symbol points to the unwind entry, we can remove the reloc
1196// that points from the unwind entry back to the symbol.
1197//
1198// First, the symbol keeps the unwind entry alive (and not vice versa), so
1199// this keeps dead-stripping simple.
1200//
1201// Moreover, it reduces the work that ICF needs to do to figure out if
1202// functions with unwind info are foldable.
1203//
1204// However, this does make it possible for ICF to fold CUEs that point to
1205// distinct functions (if the CUEs are otherwise identical).
1206// UnwindInfoSection takes care of this by re-duplicating the CUEs so that
1207// each one can hold a distinct functionAddress value.
1208//
1209// Given that clang emits relocations in reverse order of address, this
1210// relocation should be at the end of the vector for most of our input
1211// object files, so this erase() is typically an O(1) operation.
1212it = isec->relocs.erase(it);
1213}
1214}
1215}
1216
1217struct CIE {
1218macho::Symbol *personalitySymbol = nullptr;
1219bool fdesHaveAug = false;
1220uint8_t lsdaPtrSize = 0; // 0 => no LSDA
1221uint8_t funcPtrSize = 0;
1222};
1223
1224static uint8_t pointerEncodingToSize(uint8_t enc) {
1225switch (enc & 0xf) {
1226case dwarf::DW_EH_PE_absptr:
1227return target->wordSize;
1228case dwarf::DW_EH_PE_sdata4:
1229return 4;
1230case dwarf::DW_EH_PE_sdata8:
1231// ld64 doesn't actually support sdata8, but this seems simple enough...
1232return 8;
1233default:
1234return 0;
1235};
1236}
1237
1238static CIE parseCIE(const InputSection *isec, const EhReader &reader,
1239size_t off) {
1240// Handling the full generality of possible DWARF encodings would be a major
1241// pain. We instead take advantage of our knowledge of how llvm-mc encodes
1242// DWARF and handle just that.
1243constexpr uint8_t expectedPersonalityEnc =
1244dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_sdata4;
1245
1246CIE cie;
1247uint8_t version = reader.readByte(&off);
1248if (version != 1 && version != 3)
1249fatal("Expected CIE version of 1 or 3, got " + Twine(version));
1250StringRef aug = reader.readString(&off);
1251reader.skipLeb128(&off); // skip code alignment
1252reader.skipLeb128(&off); // skip data alignment
1253reader.skipLeb128(&off); // skip return address register
1254reader.skipLeb128(&off); // skip aug data length
1255uint64_t personalityAddrOff = 0;
1256for (char c : aug) {
1257switch (c) {
1258case 'z':
1259cie.fdesHaveAug = true;
1260break;
1261case 'P': {
1262uint8_t personalityEnc = reader.readByte(&off);
1263if (personalityEnc != expectedPersonalityEnc)
1264reader.failOn(off, "unexpected personality encoding 0x" +
1265Twine::utohexstr(personalityEnc));
1266personalityAddrOff = off;
1267off += 4;
1268break;
1269}
1270case 'L': {
1271uint8_t lsdaEnc = reader.readByte(&off);
1272cie.lsdaPtrSize = pointerEncodingToSize(lsdaEnc);
1273if (cie.lsdaPtrSize == 0)
1274reader.failOn(off, "unexpected LSDA encoding 0x" +
1275Twine::utohexstr(lsdaEnc));
1276break;
1277}
1278case 'R': {
1279uint8_t pointerEnc = reader.readByte(&off);
1280cie.funcPtrSize = pointerEncodingToSize(pointerEnc);
1281if (cie.funcPtrSize == 0 || !(pointerEnc & dwarf::DW_EH_PE_pcrel))
1282reader.failOn(off, "unexpected pointer encoding 0x" +
1283Twine::utohexstr(pointerEnc));
1284break;
1285}
1286default:
1287break;
1288}
1289}
1290if (personalityAddrOff != 0) {
1291const auto *personalityReloc = isec->getRelocAt(personalityAddrOff);
1292if (!personalityReloc)
1293reader.failOn(off, "Failed to locate relocation for personality symbol");
1294cie.personalitySymbol = personalityReloc->referent.get<macho::Symbol *>();
1295}
1296return cie;
1297}
1298
1299// EH frame target addresses may be encoded as pcrel offsets. However, instead
1300// of using an actual pcrel reloc, ld64 emits subtractor relocations instead.
1301// This function recovers the target address from the subtractors, essentially
1302// performing the inverse operation of EhRelocator.
1303//
1304// Concretely, we expect our relocations to write the value of `PC -
1305// target_addr` to `PC`. `PC` itself is denoted by a minuend relocation that
1306// points to a symbol plus an addend.
1307//
1308// It is important that the minuend relocation point to a symbol within the
1309// same section as the fixup value, since sections may get moved around.
1310//
1311// For example, for arm64, llvm-mc emits relocations for the target function
1312// address like so:
1313//
1314// ltmp:
1315// <CIE start>
1316// ...
1317// <CIE end>
1318// ... multiple FDEs ...
1319// <FDE start>
1320// <target function address - (ltmp + pcrel offset)>
1321// ...
1322//
1323// If any of the FDEs in `multiple FDEs` get dead-stripped, then `FDE start`
1324// will move to an earlier address, and `ltmp + pcrel offset` will no longer
1325// reflect an accurate pcrel value. To avoid this problem, we "canonicalize"
1326// our relocation by adding an `EH_Frame` symbol at `FDE start`, and updating
1327// the reloc to be `target function address - (EH_Frame + new pcrel offset)`.
1328//
1329// If `Invert` is set, then we instead expect `target_addr - PC` to be written
1330// to `PC`.
1331template <bool Invert = false>
1332Defined *
1333targetSymFromCanonicalSubtractor(const InputSection *isec,
1334std::vector<macho::Reloc>::iterator relocIt) {
1335macho::Reloc &subtrahend = *relocIt;
1336macho::Reloc &minuend = *std::next(relocIt);
1337assert(target->hasAttr(subtrahend.type, RelocAttrBits::SUBTRAHEND));
1338assert(target->hasAttr(minuend.type, RelocAttrBits::UNSIGNED));
1339// Note: pcSym may *not* be exactly at the PC; there's usually a non-zero
1340// addend.
1341auto *pcSym = cast<Defined>(subtrahend.referent.get<macho::Symbol *>());
1342Defined *target =
1343cast_or_null<Defined>(minuend.referent.dyn_cast<macho::Symbol *>());
1344if (!pcSym) {
1345auto *targetIsec =
1346cast<ConcatInputSection>(minuend.referent.get<InputSection *>());
1347target = findSymbolAtOffset(targetIsec, minuend.addend);
1348}
1349if (Invert)
1350std::swap(pcSym, target);
1351if (pcSym->isec() == isec) {
1352if (pcSym->value - (Invert ? -1 : 1) * minuend.addend != subtrahend.offset)
1353fatal("invalid FDE relocation in __eh_frame");
1354} else {
1355// Ensure the pcReloc points to a symbol within the current EH frame.
1356// HACK: we should really verify that the original relocation's semantics
1357// are preserved. In particular, we should have
1358// `oldSym->value + oldOffset == newSym + newOffset`. However, we don't
1359// have an easy way to access the offsets from this point in the code; some
1360// refactoring is needed for that.
1361macho::Reloc &pcReloc = Invert ? minuend : subtrahend;
1362pcReloc.referent = isec->symbols[0];
1363assert(isec->symbols[0]->value == 0);
1364minuend.addend = pcReloc.offset * (Invert ? 1LL : -1LL);
1365}
1366return target;
1367}
1368
1369Defined *findSymbolAtAddress(const std::vector<Section *> §ions,
1370uint64_t addr) {
1371Section *sec = findContainingSection(sections, &addr);
1372auto *isec = cast<ConcatInputSection>(findContainingSubsection(*sec, &addr));
1373return findSymbolAtOffset(isec, addr);
1374}
1375
1376// For symbols that don't have compact unwind info, associate them with the more
1377// general-purpose (and verbose) DWARF unwind info found in __eh_frame.
1378//
1379// This requires us to parse the contents of __eh_frame. See EhFrame.h for a
1380// description of its format.
1381//
1382// While parsing, we also look for what MC calls "abs-ified" relocations -- they
1383// are relocations which are implicitly encoded as offsets in the section data.
1384// We convert them into explicit Reloc structs so that the EH frames can be
1385// handled just like a regular ConcatInputSection later in our output phase.
1386//
1387// We also need to handle the case where our input object file has explicit
1388// relocations. This is the case when e.g. it's the output of `ld -r`. We only
1389// look for the "abs-ified" relocation if an explicit relocation is absent.
1390void ObjFile::registerEhFrames(Section &ehFrameSection) {
1391DenseMap<const InputSection *, CIE> cieMap;
1392for (const Subsection &subsec : ehFrameSection.subsections) {
1393auto *isec = cast<ConcatInputSection>(subsec.isec);
1394uint64_t isecOff = subsec.offset;
1395
1396// Subtractor relocs require the subtrahend to be a symbol reloc. Ensure
1397// that all EH frames have an associated symbol so that we can generate
1398// subtractor relocs that reference them.
1399if (isec->symbols.size() == 0)
1400make<Defined>("EH_Frame", isec->getFile(), isec, /*value=*/0,
1401isec->getSize(), /*isWeakDef=*/false, /*isExternal=*/false,
1402/*isPrivateExtern=*/false, /*includeInSymtab=*/false,
1403/*isReferencedDynamically=*/false,
1404/*noDeadStrip=*/false);
1405else if (isec->symbols[0]->value != 0)
1406fatal("found symbol at unexpected offset in __eh_frame");
1407
1408EhReader reader(this, isec->data, subsec.offset);
1409size_t dataOff = 0; // Offset from the start of the EH frame.
1410reader.skipValidLength(&dataOff); // readLength() already validated this.
1411// cieOffOff is the offset from the start of the EH frame to the cieOff
1412// value, which is itself an offset from the current PC to a CIE.
1413const size_t cieOffOff = dataOff;
1414
1415EhRelocator ehRelocator(isec);
1416auto cieOffRelocIt = llvm::find_if(
1417isec->relocs, [=](const Reloc &r) { return r.offset == cieOffOff; });
1418InputSection *cieIsec = nullptr;
1419if (cieOffRelocIt != isec->relocs.end()) {
1420// We already have an explicit relocation for the CIE offset.
1421cieIsec =
1422targetSymFromCanonicalSubtractor</*Invert=*/true>(isec, cieOffRelocIt)
1423->isec();
1424dataOff += sizeof(uint32_t);
1425} else {
1426// If we haven't found a relocation, then the CIE offset is most likely
1427// embedded in the section data (AKA an "abs-ified" reloc.). Parse that
1428// and generate a Reloc struct.
1429uint32_t cieMinuend = reader.readU32(&dataOff);
1430if (cieMinuend == 0) {
1431cieIsec = isec;
1432} else {
1433uint32_t cieOff = isecOff + dataOff - cieMinuend;
1434cieIsec = findContainingSubsection(ehFrameSection, &cieOff);
1435if (cieIsec == nullptr)
1436fatal("failed to find CIE");
1437}
1438if (cieIsec != isec)
1439ehRelocator.makeNegativePcRel(cieOffOff, cieIsec->symbols[0],
1440/*length=*/2);
1441}
1442if (cieIsec == isec) {
1443cieMap[cieIsec] = parseCIE(isec, reader, dataOff);
1444continue;
1445}
1446
1447assert(cieMap.count(cieIsec));
1448const CIE &cie = cieMap[cieIsec];
1449// Offset of the function address within the EH frame.
1450const size_t funcAddrOff = dataOff;
1451uint64_t funcAddr = reader.readPointer(&dataOff, cie.funcPtrSize) +
1452ehFrameSection.addr + isecOff + funcAddrOff;
1453uint32_t funcLength = reader.readPointer(&dataOff, cie.funcPtrSize);
1454size_t lsdaAddrOff = 0; // Offset of the LSDA address within the EH frame.
1455std::optional<uint64_t> lsdaAddrOpt;
1456if (cie.fdesHaveAug) {
1457reader.skipLeb128(&dataOff);
1458lsdaAddrOff = dataOff;
1459if (cie.lsdaPtrSize != 0) {
1460uint64_t lsdaOff = reader.readPointer(&dataOff, cie.lsdaPtrSize);
1461if (lsdaOff != 0) // FIXME possible to test this?
1462lsdaAddrOpt = ehFrameSection.addr + isecOff + lsdaAddrOff + lsdaOff;
1463}
1464}
1465
1466auto funcAddrRelocIt = isec->relocs.end();
1467auto lsdaAddrRelocIt = isec->relocs.end();
1468for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {
1469if (it->offset == funcAddrOff)
1470funcAddrRelocIt = it++; // Found subtrahend; skip over minuend reloc
1471else if (lsdaAddrOpt && it->offset == lsdaAddrOff)
1472lsdaAddrRelocIt = it++; // Found subtrahend; skip over minuend reloc
1473}
1474
1475Defined *funcSym;
1476if (funcAddrRelocIt != isec->relocs.end()) {
1477funcSym = targetSymFromCanonicalSubtractor(isec, funcAddrRelocIt);
1478// Canonicalize the symbol. If there are multiple symbols at the same
1479// address, we want both `registerEhFrame` and `registerCompactUnwind`
1480// to register the unwind entry under same symbol.
1481// This is not particularly efficient, but we should run into this case
1482// infrequently (only when handling the output of `ld -r`).
1483if (funcSym->isec())
1484funcSym = findSymbolAtOffset(cast<ConcatInputSection>(funcSym->isec()),
1485funcSym->value);
1486} else {
1487funcSym = findSymbolAtAddress(sections, funcAddr);
1488ehRelocator.makePcRel(funcAddrOff, funcSym, target->p2WordSize);
1489}
1490// The symbol has been coalesced, or already has a compact unwind entry.
1491if (!funcSym || funcSym->getFile() != this || funcSym->unwindEntry()) {
1492// We must prune unused FDEs for correctness, so we cannot rely on
1493// -dead_strip being enabled.
1494isec->live = false;
1495continue;
1496}
1497
1498InputSection *lsdaIsec = nullptr;
1499if (lsdaAddrRelocIt != isec->relocs.end()) {
1500lsdaIsec =
1501targetSymFromCanonicalSubtractor(isec, lsdaAddrRelocIt)->isec();
1502} else if (lsdaAddrOpt) {
1503uint64_t lsdaAddr = *lsdaAddrOpt;
1504Section *sec = findContainingSection(sections, &lsdaAddr);
1505lsdaIsec =
1506cast<ConcatInputSection>(findContainingSubsection(*sec, &lsdaAddr));
1507ehRelocator.makePcRel(lsdaAddrOff, lsdaIsec, target->p2WordSize);
1508}
1509
1510fdes[isec] = {funcLength, cie.personalitySymbol, lsdaIsec};
1511funcSym->originalUnwindEntry = isec;
1512ehRelocator.commit();
1513}
1514
1515// __eh_frame is marked as S_ATTR_LIVE_SUPPORT in input files, because FDEs
1516// are normally required to be kept alive if they reference a live symbol.
1517// However, we've explicitly created a dependency from a symbol to its FDE, so
1518// dead-stripping will just work as usual, and S_ATTR_LIVE_SUPPORT will only
1519// serve to incorrectly prevent us from dead-stripping duplicate FDEs for a
1520// live symbol (e.g. if there were multiple weak copies). Remove this flag to
1521// let dead-stripping proceed correctly.
1522ehFrameSection.flags &= ~S_ATTR_LIVE_SUPPORT;
1523}
1524
1525std::string ObjFile::sourceFile() const {
1526const char *unitName = compileUnit->getUnitDIE().getShortName();
1527// DWARF allows DW_AT_name to be absolute, in which case nothing should be
1528// prepended. As for the styles, debug info can contain paths from any OS, not
1529// necessarily an OS we're currently running on. Moreover different
1530// compilation units can be compiled on different operating systems and linked
1531// together later.
1532if (sys::path::is_absolute(unitName, llvm::sys::path::Style::posix) ||
1533sys::path::is_absolute(unitName, llvm::sys::path::Style::windows))
1534return unitName;
1535SmallString<261> dir(compileUnit->getCompilationDir());
1536StringRef sep = sys::path::get_separator();
1537// We don't use `path::append` here because we want an empty `dir` to result
1538// in an absolute path. `append` would give us a relative path for that case.
1539if (!dir.ends_with(sep))
1540dir += sep;
1541return (dir + unitName).str();
1542}
1543
1544lld::DWARFCache *ObjFile::getDwarf() {
1545llvm::call_once(initDwarf, [this]() {
1546auto dwObj = DwarfObject::create(this);
1547if (!dwObj)
1548return;
1549dwarfCache = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(
1550std::move(dwObj), "",
1551[&](Error err) { warn(getName() + ": " + toString(std::move(err))); },
1552[&](Error warning) {
1553warn(getName() + ": " + toString(std::move(warning)));
1554}));
1555});
1556
1557return dwarfCache.get();
1558}
1559// The path can point to either a dylib or a .tbd file.
1560static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) {
1561std::optional<MemoryBufferRef> mbref = readFile(path);
1562if (!mbref) {
1563error("could not read dylib file at " + path);
1564return nullptr;
1565}
1566return loadDylib(*mbref, umbrella);
1567}
1568
1569// TBD files are parsed into a series of TAPI documents (InterfaceFiles), with
1570// the first document storing child pointers to the rest of them. When we are
1571// processing a given TBD file, we store that top-level document in
1572// currentTopLevelTapi. When processing re-exports, we search its children for
1573// potentially matching documents in the same TBD file. Note that the children
1574// themselves don't point to further documents, i.e. this is a two-level tree.
1575//
1576// Re-exports can either refer to on-disk files, or to documents within .tbd
1577// files.
1578static DylibFile *findDylib(StringRef path, DylibFile *umbrella,
1579const InterfaceFile *currentTopLevelTapi) {
1580// Search order:
1581// 1. Install name basename in -F / -L directories.
1582{
1583StringRef stem = path::stem(path);
1584SmallString<128> frameworkName;
1585path::append(frameworkName, path::Style::posix, stem + ".framework", stem);
1586bool isFramework = path.ends_with(frameworkName);
1587if (isFramework) {
1588for (StringRef dir : config->frameworkSearchPaths) {
1589SmallString<128> candidate = dir;
1590path::append(candidate, frameworkName);
1591if (std::optional<StringRef> dylibPath =
1592resolveDylibPath(candidate.str()))
1593return loadDylib(*dylibPath, umbrella);
1594}
1595} else if (std::optional<StringRef> dylibPath = findPathCombination(
1596stem, config->librarySearchPaths, {".tbd", ".dylib", ".so"}))
1597return loadDylib(*dylibPath, umbrella);
1598}
1599
1600// 2. As absolute path.
1601if (path::is_absolute(path, path::Style::posix))
1602for (StringRef root : config->systemLibraryRoots)
1603if (std::optional<StringRef> dylibPath =
1604resolveDylibPath((root + path).str()))
1605return loadDylib(*dylibPath, umbrella);
1606
1607// 3. As relative path.
1608
1609// TODO: Handle -dylib_file
1610
1611// Replace @executable_path, @loader_path, @rpath prefixes in install name.
1612SmallString<128> newPath;
1613if (config->outputType == MH_EXECUTE &&
1614path.consume_front("@executable_path/")) {
1615// ld64 allows overriding this with the undocumented flag -executable_path.
1616// lld doesn't currently implement that flag.
1617// FIXME: Consider using finalOutput instead of outputFile.
1618path::append(newPath, path::parent_path(config->outputFile), path);
1619path = newPath;
1620} else if (path.consume_front("@loader_path/")) {
1621fs::real_path(umbrella->getName(), newPath);
1622path::remove_filename(newPath);
1623path::append(newPath, path);
1624path = newPath;
1625} else if (path.starts_with("@rpath/")) {
1626for (StringRef rpath : umbrella->rpaths) {
1627newPath.clear();
1628if (rpath.consume_front("@loader_path/")) {
1629fs::real_path(umbrella->getName(), newPath);
1630path::remove_filename(newPath);
1631}
1632path::append(newPath, rpath, path.drop_front(strlen("@rpath/")));
1633if (std::optional<StringRef> dylibPath = resolveDylibPath(newPath.str()))
1634return loadDylib(*dylibPath, umbrella);
1635}
1636}
1637
1638// FIXME: Should this be further up?
1639if (currentTopLevelTapi) {
1640for (InterfaceFile &child :
1641make_pointee_range(currentTopLevelTapi->documents())) {
1642assert(child.documents().empty());
1643if (path == child.getInstallName()) {
1644auto *file = make<DylibFile>(child, umbrella, /*isBundleLoader=*/false,
1645/*explicitlyLinked=*/false);
1646file->parseReexports(child);
1647return file;
1648}
1649}
1650}
1651
1652if (std::optional<StringRef> dylibPath = resolveDylibPath(path))
1653return loadDylib(*dylibPath, umbrella);
1654
1655return nullptr;
1656}
1657
1658// If a re-exported dylib is public (lives in /usr/lib or
1659// /System/Library/Frameworks), then it is considered implicitly linked: we
1660// should bind to its symbols directly instead of via the re-exporting umbrella
1661// library.
1662static bool isImplicitlyLinked(StringRef path) {
1663if (!config->implicitDylibs)
1664return false;
1665
1666if (path::parent_path(path) == "/usr/lib")
1667return true;
1668
1669// Match /System/Library/Frameworks/$FOO.framework/**/$FOO
1670if (path.consume_front("/System/Library/Frameworks/")) {
1671StringRef frameworkName = path.take_until([](char c) { return c == '.'; });
1672return path::filename(path) == frameworkName;
1673}
1674
1675return false;
1676}
1677
1678void DylibFile::loadReexport(StringRef path, DylibFile *umbrella,
1679const InterfaceFile *currentTopLevelTapi) {
1680DylibFile *reexport = findDylib(path, umbrella, currentTopLevelTapi);
1681if (!reexport)
1682error(toString(this) + ": unable to locate re-export with install name " +
1683path);
1684}
1685
1686DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
1687bool isBundleLoader, bool explicitlyLinked)
1688: InputFile(DylibKind, mb), refState(RefState::Unreferenced),
1689explicitlyLinked(explicitlyLinked), isBundleLoader(isBundleLoader) {
1690assert(!isBundleLoader || !umbrella);
1691if (umbrella == nullptr)
1692umbrella = this;
1693this->umbrella = umbrella;
1694
1695auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
1696
1697// Initialize installName.
1698if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) {
1699auto *c = reinterpret_cast<const dylib_command *>(cmd);
1700currentVersion = read32le(&c->dylib.current_version);
1701compatibilityVersion = read32le(&c->dylib.compatibility_version);
1702installName =
1703reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);
1704} else if (!isBundleLoader) {
1705// macho_executable and macho_bundle don't have LC_ID_DYLIB,
1706// so it's OK.
1707error(toString(this) + ": dylib missing LC_ID_DYLIB load command");
1708return;
1709}
1710
1711if (config->printEachFile)
1712message(toString(this));
1713inputFiles.insert(this);
1714
1715deadStrippable = hdr->flags & MH_DEAD_STRIPPABLE_DYLIB;
1716
1717if (!checkCompatibility(this))
1718return;
1719
1720checkAppExtensionSafety(hdr->flags & MH_APP_EXTENSION_SAFE);
1721
1722for (auto *cmd : findCommands<rpath_command>(hdr, LC_RPATH)) {
1723StringRef rpath{reinterpret_cast<const char *>(cmd) + cmd->path};
1724rpaths.push_back(rpath);
1725}
1726
1727// Initialize symbols.
1728bool canBeImplicitlyLinked = findCommand(hdr, LC_SUB_CLIENT) == nullptr;
1729exportingFile = (canBeImplicitlyLinked && isImplicitlyLinked(installName))
1730? this
1731: this->umbrella;
1732
1733const auto *dyldInfo = findCommand<dyld_info_command>(hdr, LC_DYLD_INFO_ONLY);
1734const auto *exportsTrie =
1735findCommand<linkedit_data_command>(hdr, LC_DYLD_EXPORTS_TRIE);
1736if (dyldInfo && exportsTrie) {
1737// It's unclear what should happen in this case. Maybe we should only error
1738// out if the two load commands refer to different data?
1739error(toString(this) +
1740": dylib has both LC_DYLD_INFO_ONLY and LC_DYLD_EXPORTS_TRIE");
1741return;
1742}
1743
1744if (dyldInfo) {
1745parseExportedSymbols(dyldInfo->export_off, dyldInfo->export_size);
1746} else if (exportsTrie) {
1747parseExportedSymbols(exportsTrie->dataoff, exportsTrie->datasize);
1748} else {
1749error("No LC_DYLD_INFO_ONLY or LC_DYLD_EXPORTS_TRIE found in " +
1750toString(this));
1751}
1752}
1753
1754void DylibFile::parseExportedSymbols(uint32_t offset, uint32_t size) {
1755struct TrieEntry {
1756StringRef name;
1757uint64_t flags;
1758};
1759
1760auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());
1761std::vector<TrieEntry> entries;
1762// Find all the $ld$* symbols to process first.
1763parseTrie(buf + offset, size, [&](const Twine &name, uint64_t flags) {
1764StringRef savedName = saver().save(name);
1765if (handleLDSymbol(savedName))
1766return;
1767entries.push_back({savedName, flags});
1768});
1769
1770// Process the "normal" symbols.
1771for (TrieEntry &entry : entries) {
1772if (exportingFile->hiddenSymbols.contains(CachedHashStringRef(entry.name)))
1773continue;
1774
1775bool isWeakDef = entry.flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;
1776bool isTlv = entry.flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;
1777
1778symbols.push_back(
1779symtab->addDylib(entry.name, exportingFile, isWeakDef, isTlv));
1780}
1781}
1782
1783void DylibFile::parseLoadCommands(MemoryBufferRef mb) {
1784auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());
1785const uint8_t *p = reinterpret_cast<const uint8_t *>(mb.getBufferStart()) +
1786target->headerSize;
1787for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
1788auto *cmd = reinterpret_cast<const load_command *>(p);
1789p += cmd->cmdsize;
1790
1791if (!(hdr->flags & MH_NO_REEXPORTED_DYLIBS) &&
1792cmd->cmd == LC_REEXPORT_DYLIB) {
1793const auto *c = reinterpret_cast<const dylib_command *>(cmd);
1794StringRef reexportPath =
1795reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
1796loadReexport(reexportPath, exportingFile, nullptr);
1797}
1798
1799// FIXME: What about LC_LOAD_UPWARD_DYLIB, LC_LAZY_LOAD_DYLIB,
1800// LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB (..are reexports from dylibs with
1801// MH_NO_REEXPORTED_DYLIBS loaded for -flat_namespace)?
1802if (config->namespaceKind == NamespaceKind::flat &&
1803cmd->cmd == LC_LOAD_DYLIB) {
1804const auto *c = reinterpret_cast<const dylib_command *>(cmd);
1805StringRef dylibPath =
1806reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
1807DylibFile *dylib = findDylib(dylibPath, umbrella, nullptr);
1808if (!dylib)
1809error(Twine("unable to locate library '") + dylibPath +
1810"' loaded from '" + toString(this) + "' for -flat_namespace");
1811}
1812}
1813}
1814
1815// Some versions of Xcode ship with .tbd files that don't have the right
1816// platform settings.
1817constexpr std::array<StringRef, 3> skipPlatformChecks{
1818"/usr/lib/system/libsystem_kernel.dylib",
1819"/usr/lib/system/libsystem_platform.dylib",
1820"/usr/lib/system/libsystem_pthread.dylib"};
1821
1822static bool skipPlatformCheckForCatalyst(const InterfaceFile &interface,
1823bool explicitlyLinked) {
1824// Catalyst outputs can link against implicitly linked macOS-only libraries.
1825if (config->platform() != PLATFORM_MACCATALYST || explicitlyLinked)
1826return false;
1827return is_contained(interface.targets(),
1828MachO::Target(config->arch(), PLATFORM_MACOS));
1829}
1830
1831static bool isArchABICompatible(ArchitectureSet archSet,
1832Architecture targetArch) {
1833uint32_t cpuType;
1834uint32_t targetCpuType;
1835std::tie(targetCpuType, std::ignore) = getCPUTypeFromArchitecture(targetArch);
1836
1837return llvm::any_of(archSet, [&](const auto &p) {
1838std::tie(cpuType, std::ignore) = getCPUTypeFromArchitecture(p);
1839return cpuType == targetCpuType;
1840});
1841}
1842
1843static bool isTargetPlatformArchCompatible(
1844InterfaceFile::const_target_range interfaceTargets, Target target) {
1845if (is_contained(interfaceTargets, target))
1846return true;
1847
1848if (config->forceExactCpuSubtypeMatch)
1849return false;
1850
1851ArchitectureSet archSet;
1852for (const auto &p : interfaceTargets)
1853if (p.Platform == target.Platform)
1854archSet.set(p.Arch);
1855if (archSet.empty())
1856return false;
1857
1858return isArchABICompatible(archSet, target.Arch);
1859}
1860
1861DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,
1862bool isBundleLoader, bool explicitlyLinked)
1863: InputFile(DylibKind, interface), refState(RefState::Unreferenced),
1864explicitlyLinked(explicitlyLinked), isBundleLoader(isBundleLoader) {
1865// FIXME: Add test for the missing TBD code path.
1866
1867if (umbrella == nullptr)
1868umbrella = this;
1869this->umbrella = umbrella;
1870
1871installName = saver().save(interface.getInstallName());
1872compatibilityVersion = interface.getCompatibilityVersion().rawValue();
1873currentVersion = interface.getCurrentVersion().rawValue();
1874
1875if (config->printEachFile)
1876message(toString(this));
1877inputFiles.insert(this);
1878
1879if (!is_contained(skipPlatformChecks, installName) &&
1880!isTargetPlatformArchCompatible(interface.targets(),
1881config->platformInfo.target) &&
1882!skipPlatformCheckForCatalyst(interface, explicitlyLinked)) {
1883error(toString(this) + " is incompatible with " +
1884std::string(config->platformInfo.target));
1885return;
1886}
1887
1888checkAppExtensionSafety(interface.isApplicationExtensionSafe());
1889
1890bool canBeImplicitlyLinked = interface.allowableClients().size() == 0;
1891exportingFile = (canBeImplicitlyLinked && isImplicitlyLinked(installName))
1892? this
1893: umbrella;
1894auto addSymbol = [&](const llvm::MachO::Symbol &symbol,
1895const Twine &name) -> void {
1896StringRef savedName = saver().save(name);
1897if (exportingFile->hiddenSymbols.contains(CachedHashStringRef(savedName)))
1898return;
1899
1900symbols.push_back(symtab->addDylib(savedName, exportingFile,
1901symbol.isWeakDefined(),
1902symbol.isThreadLocalValue()));
1903};
1904
1905std::vector<const llvm::MachO::Symbol *> normalSymbols;
1906normalSymbols.reserve(interface.symbolsCount());
1907for (const auto *symbol : interface.symbols()) {
1908if (!isArchABICompatible(symbol->getArchitectures(), config->arch()))
1909continue;
1910if (handleLDSymbol(symbol->getName()))
1911continue;
1912
1913switch (symbol->getKind()) {
1914case EncodeKind::GlobalSymbol:
1915case EncodeKind::ObjectiveCClass:
1916case EncodeKind::ObjectiveCClassEHType:
1917case EncodeKind::ObjectiveCInstanceVariable:
1918normalSymbols.push_back(symbol);
1919}
1920}
1921// interface.symbols() order is non-deterministic.
1922llvm::sort(normalSymbols,
1923[](auto *l, auto *r) { return l->getName() < r->getName(); });
1924
1925// TODO(compnerd) filter out symbols based on the target platform
1926for (const auto *symbol : normalSymbols) {
1927switch (symbol->getKind()) {
1928case EncodeKind::GlobalSymbol:
1929addSymbol(*symbol, symbol->getName());
1930break;
1931case EncodeKind::ObjectiveCClass:
1932// XXX ld64 only creates these symbols when -ObjC is passed in. We may
1933// want to emulate that.
1934addSymbol(*symbol, objc::symbol_names::klass + symbol->getName());
1935addSymbol(*symbol, objc::symbol_names::metaclass + symbol->getName());
1936break;
1937case EncodeKind::ObjectiveCClassEHType:
1938addSymbol(*symbol, objc::symbol_names::ehtype + symbol->getName());
1939break;
1940case EncodeKind::ObjectiveCInstanceVariable:
1941addSymbol(*symbol, objc::symbol_names::ivar + symbol->getName());
1942break;
1943}
1944}
1945}
1946
1947DylibFile::DylibFile(DylibFile *umbrella)
1948: InputFile(DylibKind, MemoryBufferRef{}), refState(RefState::Unreferenced),
1949explicitlyLinked(false), isBundleLoader(false) {
1950if (umbrella == nullptr)
1951umbrella = this;
1952this->umbrella = umbrella;
1953}
1954
1955void DylibFile::parseReexports(const InterfaceFile &interface) {
1956const InterfaceFile *topLevel =
1957interface.getParent() == nullptr ? &interface : interface.getParent();
1958for (const InterfaceFileRef &intfRef : interface.reexportedLibraries()) {
1959InterfaceFile::const_target_range targets = intfRef.targets();
1960if (is_contained(skipPlatformChecks, intfRef.getInstallName()) ||
1961isTargetPlatformArchCompatible(targets, config->platformInfo.target))
1962loadReexport(intfRef.getInstallName(), exportingFile, topLevel);
1963}
1964}
1965
1966bool DylibFile::isExplicitlyLinked() const {
1967if (!explicitlyLinked)
1968return false;
1969
1970// If this dylib was explicitly linked, but at least one of the symbols
1971// of the synthetic dylibs it created via $ld$previous symbols is
1972// referenced, then that synthetic dylib fulfils the explicit linkedness
1973// and we can deadstrip this dylib if it's unreferenced.
1974for (const auto *dylib : extraDylibs)
1975if (dylib->isReferenced())
1976return false;
1977
1978return true;
1979}
1980
1981DylibFile *DylibFile::getSyntheticDylib(StringRef installName,
1982uint32_t currentVersion,
1983uint32_t compatVersion) {
1984for (DylibFile *dylib : extraDylibs)
1985if (dylib->installName == installName) {
1986// FIXME: Check what to do if different $ld$previous symbols
1987// request the same dylib, but with different versions.
1988return dylib;
1989}
1990
1991auto *dylib = make<DylibFile>(umbrella == this ? nullptr : umbrella);
1992dylib->installName = saver().save(installName);
1993dylib->currentVersion = currentVersion;
1994dylib->compatibilityVersion = compatVersion;
1995extraDylibs.push_back(dylib);
1996return dylib;
1997}
1998
1999// $ld$ symbols modify the properties/behavior of the library (e.g. its install
2000// name, compatibility version or hide/add symbols) for specific target
2001// versions.
2002bool DylibFile::handleLDSymbol(StringRef originalName) {
2003if (!originalName.starts_with("$ld$"))
2004return false;
2005
2006StringRef action;
2007StringRef name;
2008std::tie(action, name) = originalName.drop_front(strlen("$ld$")).split('$');
2009if (action == "previous")
2010handleLDPreviousSymbol(name, originalName);
2011else if (action == "install_name")
2012handleLDInstallNameSymbol(name, originalName);
2013else if (action == "hide")
2014handleLDHideSymbol(name, originalName);
2015return true;
2016}
2017
2018void DylibFile::handleLDPreviousSymbol(StringRef name, StringRef originalName) {
2019// originalName: $ld$ previous $ <installname> $ <compatversion> $
2020// <platformstr> $ <startversion> $ <endversion> $ <symbol-name> $
2021StringRef installName;
2022StringRef compatVersion;
2023StringRef platformStr;
2024StringRef startVersion;
2025StringRef endVersion;
2026StringRef symbolName;
2027StringRef rest;
2028
2029std::tie(installName, name) = name.split('$');
2030std::tie(compatVersion, name) = name.split('$');
2031std::tie(platformStr, name) = name.split('$');
2032std::tie(startVersion, name) = name.split('$');
2033std::tie(endVersion, name) = name.split('$');
2034std::tie(symbolName, rest) = name.rsplit('$');
2035
2036// FIXME: Does this do the right thing for zippered files?
2037unsigned platform;
2038if (platformStr.getAsInteger(10, platform) ||
2039platform != static_cast<unsigned>(config->platform()))
2040return;
2041
2042VersionTuple start;
2043if (start.tryParse(startVersion)) {
2044warn(toString(this) + ": failed to parse start version, symbol '" +
2045originalName + "' ignored");
2046return;
2047}
2048VersionTuple end;
2049if (end.tryParse(endVersion)) {
2050warn(toString(this) + ": failed to parse end version, symbol '" +
2051originalName + "' ignored");
2052return;
2053}
2054if (config->platformInfo.target.MinDeployment < start ||
2055config->platformInfo.target.MinDeployment >= end)
2056return;
2057
2058// Initialized to compatibilityVersion for the symbolName branch below.
2059uint32_t newCompatibilityVersion = compatibilityVersion;
2060uint32_t newCurrentVersionForSymbol = currentVersion;
2061if (!compatVersion.empty()) {
2062VersionTuple cVersion;
2063if (cVersion.tryParse(compatVersion)) {
2064warn(toString(this) +
2065": failed to parse compatibility version, symbol '" + originalName +
2066"' ignored");
2067return;
2068}
2069newCompatibilityVersion = encodeVersion(cVersion);
2070newCurrentVersionForSymbol = newCompatibilityVersion;
2071}
2072
2073if (!symbolName.empty()) {
2074// A $ld$previous$ symbol with symbol name adds a symbol with that name to
2075// a dylib with given name and version.
2076auto *dylib = getSyntheticDylib(installName, newCurrentVersionForSymbol,
2077newCompatibilityVersion);
2078
2079// The tbd file usually contains the $ld$previous symbol for an old version,
2080// and then the symbol itself later, for newer deployment targets, like so:
2081// symbols: [
2082// '$ld$previous$/Another$$1$3.0$14.0$_zzz$',
2083// _zzz,
2084// ]
2085// Since the symbols are sorted, adding them to the symtab in the given
2086// order means the $ld$previous version of _zzz will prevail, as desired.
2087dylib->symbols.push_back(symtab->addDylib(
2088saver().save(symbolName), dylib, /*isWeakDef=*/false, /*isTlv=*/false));
2089return;
2090}
2091
2092// A $ld$previous$ symbol without symbol name modifies the dylib it's in.
2093this->installName = saver().save(installName);
2094this->compatibilityVersion = newCompatibilityVersion;
2095}
2096
2097void DylibFile::handleLDInstallNameSymbol(StringRef name,
2098StringRef originalName) {
2099// originalName: $ld$ install_name $ os<version> $ install_name
2100StringRef condition, installName;
2101std::tie(condition, installName) = name.split('$');
2102VersionTuple version;
2103if (!condition.consume_front("os") || version.tryParse(condition))
2104warn(toString(this) + ": failed to parse os version, symbol '" +
2105originalName + "' ignored");
2106else if (version == config->platformInfo.target.MinDeployment)
2107this->installName = saver().save(installName);
2108}
2109
2110void DylibFile::handleLDHideSymbol(StringRef name, StringRef originalName) {
2111StringRef symbolName;
2112bool shouldHide = true;
2113if (name.starts_with("os")) {
2114// If it's hidden based on versions.
2115name = name.drop_front(2);
2116StringRef minVersion;
2117std::tie(minVersion, symbolName) = name.split('$');
2118VersionTuple versionTup;
2119if (versionTup.tryParse(minVersion)) {
2120warn(toString(this) + ": failed to parse hidden version, symbol `" + originalName +
2121"` ignored.");
2122return;
2123}
2124shouldHide = versionTup == config->platformInfo.target.MinDeployment;
2125} else {
2126symbolName = name;
2127}
2128
2129if (shouldHide)
2130exportingFile->hiddenSymbols.insert(CachedHashStringRef(symbolName));
2131}
2132
2133void DylibFile::checkAppExtensionSafety(bool dylibIsAppExtensionSafe) const {
2134if (config->applicationExtension && !dylibIsAppExtensionSafe)
2135warn("using '-application_extension' with unsafe dylib: " + toString(this));
2136}
2137
2138ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f, bool forceHidden)
2139: InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)),
2140forceHidden(forceHidden) {}
2141
2142void ArchiveFile::addLazySymbols() {
2143// Avoid calling getMemoryBufferRef() on zero-symbol archive
2144// since that crashes.
2145if (file->isEmpty() || file->getNumberOfSymbols() == 0)
2146return;
2147
2148Error err = Error::success();
2149auto child = file->child_begin(err);
2150// Ignore the I/O error here - will be reported later.
2151if (!err) {
2152Expected<MemoryBufferRef> mbOrErr = child->getMemoryBufferRef();
2153if (!mbOrErr) {
2154llvm::consumeError(mbOrErr.takeError());
2155} else {
2156if (identify_magic(mbOrErr->getBuffer()) == file_magic::macho_object) {
2157if (target->wordSize == 8)
2158compatArch = compatWithTargetArch(
2159this, reinterpret_cast<const LP64::mach_header *>(
2160mbOrErr->getBufferStart()));
2161else
2162compatArch = compatWithTargetArch(
2163this, reinterpret_cast<const ILP32::mach_header *>(
2164mbOrErr->getBufferStart()));
2165if (!compatArch)
2166return;
2167}
2168}
2169}
2170
2171for (const object::Archive::Symbol &sym : file->symbols())
2172symtab->addLazyArchive(sym.getName(), this, sym);
2173}
2174
2175static Expected<InputFile *>
2176loadArchiveMember(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,
2177uint64_t offsetInArchive, bool forceHidden, bool compatArch) {
2178if (config->zeroModTime)
2179modTime = 0;
2180
2181switch (identify_magic(mb.getBuffer())) {
2182case file_magic::macho_object:
2183return make<ObjFile>(mb, modTime, archiveName, /*lazy=*/false, forceHidden,
2184compatArch);
2185case file_magic::bitcode:
2186return make<BitcodeFile>(mb, archiveName, offsetInArchive, /*lazy=*/false,
2187forceHidden, compatArch);
2188default:
2189return createStringError(inconvertibleErrorCode(),
2190mb.getBufferIdentifier() +
2191" has unhandled file type");
2192}
2193}
2194
2195Error ArchiveFile::fetch(const object::Archive::Child &c, StringRef reason) {
2196if (!seen.insert(c.getChildOffset()).second)
2197return Error::success();
2198
2199Expected<MemoryBufferRef> mb = c.getMemoryBufferRef();
2200if (!mb)
2201return mb.takeError();
2202
2203// Thin archives refer to .o files, so --reproduce needs the .o files too.
2204if (tar && c.getParent()->isThin())
2205tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb->getBuffer());
2206
2207Expected<TimePoint<std::chrono::seconds>> modTime = c.getLastModified();
2208if (!modTime)
2209return modTime.takeError();
2210
2211Expected<InputFile *> file =
2212loadArchiveMember(*mb, toTimeT(*modTime), getName(), c.getChildOffset(),
2213forceHidden, compatArch);
2214
2215if (!file)
2216return file.takeError();
2217
2218inputFiles.insert(*file);
2219printArchiveMemberLoad(reason, *file);
2220return Error::success();
2221}
2222
2223void ArchiveFile::fetch(const object::Archive::Symbol &sym) {
2224object::Archive::Child c =
2225CHECK(sym.getMember(), toString(this) +
2226": could not get the member defining symbol " +
2227toMachOString(sym));
2228
2229// `sym` is owned by a LazySym, which will be replace<>()d by make<ObjFile>
2230// and become invalid after that call. Copy it to the stack so we can refer
2231// to it later.
2232const object::Archive::Symbol symCopy = sym;
2233
2234// ld64 doesn't demangle sym here even with -demangle.
2235// Match that: intentionally don't call toMachOString().
2236if (Error e = fetch(c, symCopy.getName()))
2237error(toString(this) + ": could not get the member defining symbol " +
2238toMachOString(symCopy) + ": " + toString(std::move(e)));
2239}
2240
2241static macho::Symbol *createBitcodeSymbol(const lto::InputFile::Symbol &objSym,
2242BitcodeFile &file) {
2243StringRef name = saver().save(objSym.getName());
2244
2245if (objSym.isUndefined())
2246return symtab->addUndefined(name, &file, /*isWeakRef=*/objSym.isWeak());
2247
2248// TODO: Write a test demonstrating why computing isPrivateExtern before
2249// LTO compilation is important.
2250bool isPrivateExtern = false;
2251switch (objSym.getVisibility()) {
2252case GlobalValue::HiddenVisibility:
2253isPrivateExtern = true;
2254break;
2255case GlobalValue::ProtectedVisibility:
2256error(name + " has protected visibility, which is not supported by Mach-O");
2257break;
2258case GlobalValue::DefaultVisibility:
2259break;
2260}
2261isPrivateExtern = isPrivateExtern || objSym.canBeOmittedFromSymbolTable() ||
2262file.forceHidden;
2263
2264if (objSym.isCommon())
2265return symtab->addCommon(name, &file, objSym.getCommonSize(),
2266objSym.getCommonAlignment(), isPrivateExtern);
2267
2268return symtab->addDefined(name, &file, /*isec=*/nullptr, /*value=*/0,
2269/*size=*/0, objSym.isWeak(), isPrivateExtern,
2270/*isReferencedDynamically=*/false,
2271/*noDeadStrip=*/false,
2272/*isWeakDefCanBeHidden=*/false);
2273}
2274
2275BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,
2276uint64_t offsetInArchive, bool lazy, bool forceHidden,
2277bool compatArch)
2278: InputFile(BitcodeKind, mb, lazy), forceHidden(forceHidden) {
2279this->archiveName = std::string(archiveName);
2280this->compatArch = compatArch;
2281std::string path = mb.getBufferIdentifier().str();
2282if (config->thinLTOIndexOnly)
2283path = replaceThinLTOSuffix(mb.getBufferIdentifier());
2284
2285// If the parent archive already determines that the arch is not compat with
2286// target, then just return.
2287if (!compatArch)
2288return;
2289
2290// ThinLTO assumes that all MemoryBufferRefs given to it have a unique
2291// name. If two members with the same name are provided, this causes a
2292// collision and ThinLTO can't proceed.
2293// So, we append the archive name to disambiguate two members with the same
2294// name from multiple different archives, and offset within the archive to
2295// disambiguate two members of the same name from a single archive.
2296MemoryBufferRef mbref(mb.getBuffer(),
2297saver().save(archiveName.empty()
2298? path
2299: archiveName + "(" +
2300sys::path::filename(path) + ")" +
2301utostr(offsetInArchive)));
2302obj = check(lto::InputFile::create(mbref));
2303if (lazy)
2304parseLazy();
2305else
2306parse();
2307}
2308
2309void BitcodeFile::parse() {
2310// Convert LTO Symbols to LLD Symbols in order to perform resolution. The
2311// "winning" symbol will then be marked as Prevailing at LTO compilation
2312// time.
2313symbols.resize(obj->symbols().size());
2314
2315// Process defined symbols first. See the comment at the end of
2316// ObjFile<>::parseSymbols.
2317for (auto it : llvm::enumerate(obj->symbols()))
2318if (!it.value().isUndefined())
2319symbols[it.index()] = createBitcodeSymbol(it.value(), *this);
2320for (auto it : llvm::enumerate(obj->symbols()))
2321if (it.value().isUndefined())
2322symbols[it.index()] = createBitcodeSymbol(it.value(), *this);
2323}
2324
2325void BitcodeFile::parseLazy() {
2326symbols.resize(obj->symbols().size());
2327for (const auto &[i, objSym] : llvm::enumerate(obj->symbols())) {
2328if (!objSym.isUndefined()) {
2329symbols[i] = symtab->addLazyObject(saver().save(objSym.getName()), *this);
2330if (!lazy)
2331break;
2332}
2333}
2334}
2335
2336std::string macho::replaceThinLTOSuffix(StringRef path) {
2337auto [suffix, repl] = config->thinLTOObjectSuffixReplace;
2338if (path.consume_back(suffix))
2339return (path + repl).str();
2340return std::string(path);
2341}
2342
2343void macho::extract(InputFile &file, StringRef reason) {
2344if (!file.lazy)
2345return;
2346file.lazy = false;
2347
2348printArchiveMemberLoad(reason, &file);
2349if (auto *bitcode = dyn_cast<BitcodeFile>(&file)) {
2350bitcode->parse();
2351} else {
2352auto &f = cast<ObjFile>(file);
2353if (target->wordSize == 8)
2354f.parse<LP64>();
2355else
2356f.parse<ILP32>();
2357}
2358}
2359
2360template void ObjFile::parse<LP64>();
2361