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