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