2430 lines · cpp
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-exception6//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 to17// output files. When we merge or garbage-collect sections, we treat each18// section as an atomic unit. In Mach-O, that's not the case. Sections can19// consist of multiple subsections, and subsections are a unit of merging and20// garbage-collecting. Therefore, Mach-O's subsections are more similar to21// ELF/COFF's sections than Mach-O's sections are.22//23// A section can have multiple symbols. A symbol that does not have the24// N_ALT_ENTRY attribute indicates a beginning of a subsection. Therefore, by25// definition, a symbol is always present at the beginning of each subsection. A26// symbol with N_ALT_ENTRY attribute does not start a new subsection and can27// point to a middle of a subsection.28//29// The notion of subsections also affects how relocations are represented in30// Mach-O. All references within a section need to be explicitly represented as31// relocations if they refer to different subsections, because we obviously need32// to fix up addresses if subsections are laid out in an output file differently33// than they were in object files. To represent that, Mach-O relocations can34// refer to an unnamed location via its address. Scattered relocations (those35// 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 set37// and r_symbolnum is zero.38//39// Without the above differences, I think you can use your knowledge about ELF40// 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>(f))91 if (f->getName().ends_with(".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(f->getName()) + ")").str();97}98 99std::string lld::toString(const Section &sec) {100 return (toString(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>(input) && !isa<DylibFile>(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>(hdr, LC_BUILD_VERSION)) {123 PlatformInfo info;124 info.target.Platform = static_cast<PlatformType>(cmd->platform);125 info.target.MinDeployment = decodeVersion(cmd->minos);126 platformInfos.emplace_back(std::move(info));127 }128 for (auto *cmd : findCommands<version_min_command>(129 hdr, LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,130 LC_VERSION_MIN_TVOS, 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(cmd->version);147 platformInfos.emplace_back(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(platformInfos, [&](const PlatformInfo &info) {159 return removeSimulator(info.target.Platform) ==160 removeSimulator(config->platform());161 });162 if (it == platformInfos.end()) {163 std::string platformNames;164 raw_string_ostream os(platformNames);165 interleave(166 platformInfos, os,167 [&](const PlatformInfo &info) {168 os << getPlatformName(info.target.Platform);169 },170 "/");171 error(toString(input) + " has platform " + platformNames +172 Twine(", which is different from target platform ") +173 getPlatformName(config->platform()));174 return false;175 }176 177 if (it->target.MinDeployment > config->platformInfo.target.MinDeployment)178 warn(toString(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(cpuType, std::ignore) = getCPUTypeFromArchitecture(config->arch());190 191 if (hdr->cputype != cpuType) {192 Architecture arch =193 getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype);194 auto msg = config->errorForArchMismatch195 ? static_cast<void (*)(const Twine &)>(error)196 : warn;197 198 msg(toString(file) + " has architecture " + getArchitectureName(arch) +199 " which is incompatible with target architecture " +200 getArchitectureName(config->arch()));201 return false;202 }203 204 return checkCompatibility(file);205}206 207// This cache mostly exists to store system libraries (and .tbds) as they're208// loaded, rather than the input archives, which are already cached at a higher209// level, and other files like the filelist that are only read once.210// Theoretically this caching could be more efficient by hoisting it, but that211// 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(key);217 if (entry != cachedReads.end())218 return entry->second;219 220 ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr =221 MemoryBuffer::getFile(path, false, /*RequiresNullTerminator=*/false);222 if (std::error_code ec = mbOrErr.getError()) {223 error("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>>(std::move(mb)); // take mb ownership230 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(&hdr->magic) != FAT_MAGIC) {236 if (tar)237 tar->append(relativeToRoot(path), 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 multiple244 // real files for different CPU ISAs. Here, we search for a file that matches245 // 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(getArchitectureFromCpuType(cpuType, cpuSubtype));249 };250 251 std::vector<StringRef> archs;252 for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {253 if (reinterpret_cast<const char *>(arch + i + 1) >254 buf + mbref.getBufferSize()) {255 error(path + ": fat_arch struct extends beyond end of file");256 return std::nullopt;257 }258 259 uint32_t cpuType = read32be(&arch[i].cputype);260 uint32_t cpuSubtype =261 read32be(&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(getArchName(cpuType, cpuSubtype));268 continue;269 }270 271 uint32_t offset = read32be(&arch[i].offset);272 uint32_t size = read32be(&arch[i].size);273 if (offset + size > mbref.getBufferSize())274 error(path + ": slice extends beyond end of file");275 if (tar)276 tar->append(relativeToRoot(path), mbref.getBuffer());277 return cachedReads[key] = MemoryBufferRef(StringRef(buf + offset, size),278 path.copy(bAlloc));279 }280 281 auto targetArchName = getArchName(target->cpuType, target->cpuSubtype);282 warn(path + ": ignoring file because it is universal (" + join(archs, ",") +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(interface.getPath())) {}289 290// Some sections comprise of fixed-size records, so instead of splitting them at291// symbol boundaries, we split them based on size. Records are distinct from292// literals in that they may contain references to other sections, instead of293// being leaf nodes in the InputSection graph.294//295// Note that "record" is a term I came up with. In contrast, "literal" is a term296// 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(fromIndex))327 return err;328 if (Error err = reader.readInteger(toIndex))329 return err;330 if (Error err = reader.readInteger(count))331 return err;332 callGraph.emplace_back(fromIndex, toIndex, 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(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 §ion = *sections.back();356 uint32_t align = 1 << sec.align;357 ArrayRef<uint8_t> data = {isZeroFill(sec.flags) ? nullptr358 : 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(data.size() / recordSize);366 for (uint64_t off = 0; off < data.size(); off += recordSize) {367 auto *isec = make<ConcatInputSection>(368 section, data.slice(off, std::min(data.size(), recordSize)), align);369 subsections.push_back({off, isec});370 }371 section.doneSplitting = true;372 };373 374 if (sectionType(sec.flags) == S_CSTRING_LITERALS) {375 if (sec.nreloc)376 fatal(toString(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>(section, data, align, dedupLiterals);382 // FIXME: parallelize this?383 cast<CStringInputSection>(isec)->splitIntoPieces();384 section.subsections.push_back({0, isec});385 } else if (isWordLiteralSection(sec.flags)) {386 if (sec.nreloc)387 fatal(toString(this) + ": " + sec.segname + "," + sec.sectname +388 " contains relocations, which is unsupported");389 InputSection *isec = make<WordLiteralInputSection>(section, data, align);390 section.subsections.push_back({0, 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(data, *sections.back());396 } else if (segname == segment_names::llvm) {397 if (config->callGraphProfileSort && name == section_names::cgProfile)398 checkError(parseCallGraph(data, callGraph));399 // ld64 does not appear to emit contents from sections within the __LLVM400 // segment. Symbols within those sections point to bitcode metadata401 // instead of actual symbols. Global symbols within those sections could402 // have the same name without causing duplicate symbol errors. To avoid403 // 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>(section, data, align);413 if (isDebugSection(isec->getFlags()) &&414 isec->getSegName() == segment_names::dwarf) {415 // Instead of emitting DWARF sections, we emit STABS symbols to the416 // object files that contain them. We filter them out early to avoid417 // parsing their relocations unnecessarily.418 debugSections.push_back(isec);419 } else {420 section.subsections.push_back({0, 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);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 our437 // EH frames to be aligned to the section alignment. EH frame decoders don't438 // expect this alignment. Moreover, each EH frame must start where the439 // previous one ends, and where it ends is indicated by the length field.440 // Unless we update the length field (troublesome), we should keep the441 // 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 {frameOff, make<ConcatInputSection>(ehFrameSection,446 data.slice(frameOff, fullLength),447 /*align=*/1)});448 }449 ehFrameSection.doneSplitting = true;450}451 452template <class T>453static Section *findContainingSection(const std::vector<Section *> §ions,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 (before469// any subsection splitting has occurred). It will be updated to represent the470// same location as an offset relative to the start of the containing471// subsection.472template <class T>473static InputSection *findContainingSubsection(const Section §ion,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// Find a symbol at offset `off` within `isec`.486static Defined *findSymbolAtOffset(const ConcatInputSection *isec,487 uint64_t off) {488 auto it = llvm::lower_bound(isec->symbols, off, [](Defined *d, uint64_t off) {489 return d->value < off;490 });491 // The offset should point at the exact address of a symbol (with no addend.)492 if (it == isec->symbols.end() || (*it)->value != off) {493 assert(isec->wasCoalesced);494 return nullptr;495 }496 return *it;497}498 499template <class SectionHeader>500static bool validateRelocationInfo(InputFile *file, const SectionHeader &sec,501 relocation_info rel) {502 const RelocAttrs &relocAttrs = target->getRelocAttrs(rel.r_type);503 bool valid = true;504 auto message = [relocAttrs, file, sec, rel, &valid](const Twine &diagnostic) {505 valid = false;506 return (relocAttrs.name + " relocation " + diagnostic + " at offset " +507 std::to_string(rel.r_address) + " of " + sec.segname + "," +508 sec.sectname + " in " + toString(file))509 .str();510 };511 512 if (!relocAttrs.hasAttr(RelocAttrBits::LOCAL) && !rel.r_extern)513 error(message("must be extern"));514 if (relocAttrs.hasAttr(RelocAttrBits::PCREL) != rel.r_pcrel)515 error(message(Twine("must ") + (rel.r_pcrel ? "not " : "") +516 "be PC-relative"));517 if (isThreadLocalVariables(sec.flags) &&518 !relocAttrs.hasAttr(RelocAttrBits::UNSIGNED))519 error(message("not allowed in thread-local section, must be UNSIGNED"));520 if (!relocAttrs.hasAttr(static_cast<RelocAttrBits>(1 << rel.r_length))) {521 error(message("has invalid width of " + std::to_string(1 << rel.r_length) +522 " bytes"));523 }524 return valid;525}526 527template <class SectionHeader>528void ObjFile::parseRelocations(ArrayRef<SectionHeader> sectionHeaders,529 const SectionHeader &sec, Section §ion) {530 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());531 ArrayRef<relocation_info> relInfos(532 reinterpret_cast<const relocation_info *>(buf + sec.reloff), sec.nreloc);533 534 Subsections &subsections = section.subsections;535 auto subsecIt = subsections.rbegin();536 for (size_t i = 0; i < relInfos.size(); i++) {537 // Paired relocations serve as Mach-O's method for attaching a538 // supplemental datum to a primary relocation record. ELF does not539 // need them because the *_RELOC_RELA records contain the extra540 // addend field, vs. *_RELOC_REL which omit the addend.541 //542 // The {X86_64,ARM64}_RELOC_SUBTRACTOR record holds the subtrahend,543 // and the paired *_RELOC_UNSIGNED record holds the minuend. The544 // datum for each is a symbolic address. The result is the offset545 // between two addresses.546 //547 // The ARM64_RELOC_ADDEND record holds the addend, and the paired548 // ARM64_RELOC_BRANCH26 or ARM64_RELOC_PAGE21/PAGEOFF12 holds the549 // base symbolic address.550 //551 // Note: X86 does not use *_RELOC_ADDEND because it can embed an addend into552 // the instruction stream. On X86, a relocatable address field always553 // occupies an entire contiguous sequence of byte(s), so there is no need to554 // merge opcode bits with address bits. Therefore, it's easy and convenient555 // to store addends in the instruction-stream bytes that would otherwise556 // contain zeroes. By contrast, RISC ISAs such as ARM64 mix opcode bits with557 // address bits so that bitwise arithmetic is necessary to extract and558 // insert them. Storing addends in the instruction stream is possible, but559 // inconvenient and more costly at link time.560 561 relocation_info relInfo = relInfos[i];562 bool isSubtrahend =563 target->hasAttr(relInfo.r_type, RelocAttrBits::SUBTRAHEND);564 int64_t pairedAddend = 0;565 if (target->hasAttr(relInfo.r_type, RelocAttrBits::ADDEND)) {566 pairedAddend = SignExtend64<24>(relInfo.r_symbolnum);567 relInfo = relInfos[++i];568 }569 assert(i < relInfos.size());570 if (!validateRelocationInfo(this, sec, relInfo))571 continue;572 if (relInfo.r_address & R_SCATTERED)573 fatal("TODO: Scattered relocations not supported");574 575 int64_t embeddedAddend = target->getEmbeddedAddend(mb, sec.offset, relInfo);576 assert(!(embeddedAddend && pairedAddend));577 int64_t totalAddend = pairedAddend + embeddedAddend;578 Reloc r;579 r.type = relInfo.r_type;580 r.pcrel = relInfo.r_pcrel;581 r.length = relInfo.r_length;582 r.offset = relInfo.r_address;583 if (relInfo.r_extern) {584 r.referent = symbols[relInfo.r_symbolnum];585 r.addend = isSubtrahend ? 0 : totalAddend;586 } else {587 assert(!isSubtrahend);588 const SectionHeader &referentSecHead =589 sectionHeaders[relInfo.r_symbolnum - 1];590 uint64_t referentOffset;591 if (relInfo.r_pcrel) {592 // The implicit addend for pcrel section relocations is the pcrel offset593 // in terms of the addresses in the input file. Here we adjust it so594 // that it describes the offset from the start of the referent section.595 // FIXME This logic was written around x86_64 behavior -- ARM64 doesn't596 // have pcrel section relocations. We may want to factor this out into597 // the arch-specific .cpp file.598 referentOffset = sec.addr + relInfo.r_address +599 (1ull << relInfo.r_length) + totalAddend -600 referentSecHead.addr;601 } else {602 // The addend for a non-pcrel relocation is its absolute address.603 referentOffset = totalAddend - referentSecHead.addr;604 }605 r.referent = findContainingSubsection(*sections[relInfo.r_symbolnum - 1],606 &referentOffset);607 r.addend = referentOffset;608 }609 610 // Find the subsection that this relocation belongs to.611 // Though not required by the Mach-O format, clang and gcc seem to emit612 // relocations in order, so let's take advantage of it. However, ld64 emits613 // unsorted relocations (in `-r` mode), so we have a fallback for that614 // uncommon case.615 InputSection *subsec;616 while (subsecIt != subsections.rend() && subsecIt->offset > r.offset)617 ++subsecIt;618 if (subsecIt == subsections.rend() ||619 subsecIt->offset + subsecIt->isec->getSize() <= r.offset) {620 subsec = findContainingSubsection(section, &r.offset);621 // Now that we know the relocs are unsorted, avoid trying the 'fast path'622 // for the other relocations.623 subsecIt = subsections.rend();624 } else {625 subsec = subsecIt->isec;626 r.offset -= subsecIt->offset;627 }628 subsec->relocs.push_back(r);629 630 if (isSubtrahend) {631 relocation_info minuendInfo = relInfos[++i];632 // SUBTRACTOR relocations should always be followed by an UNSIGNED one633 // attached to the same address.634 assert(target->hasAttr(minuendInfo.r_type, RelocAttrBits::UNSIGNED) &&635 relInfo.r_address == minuendInfo.r_address);636 Reloc p;637 p.type = minuendInfo.r_type;638 if (minuendInfo.r_extern) {639 p.referent = symbols[minuendInfo.r_symbolnum];640 p.addend = totalAddend;641 } else {642 uint64_t referentOffset =643 totalAddend - sectionHeaders[minuendInfo.r_symbolnum - 1].addr;644 p.referent = findContainingSubsection(645 *sections[minuendInfo.r_symbolnum - 1], &referentOffset);646 p.addend = referentOffset;647 }648 subsec->relocs.push_back(p);649 }650 }651}652 653template <class NList>654static macho::Symbol *createDefined(const NList &sym, StringRef name,655 InputSection *isec, uint64_t value,656 uint64_t size, bool forceHidden) {657 // Symbol scope is determined by sym.n_type & (N_EXT | N_PEXT):658 // N_EXT: Global symbols. These go in the symbol table during the link,659 // and also in the export table of the output so that the dynamic660 // linker sees them.661 // N_EXT | N_PEXT: Linkage unit (think: dylib) scoped. These go in the662 // symbol table during the link so that duplicates are663 // either reported (for non-weak symbols) or merged664 // (for weak symbols), but they do not go in the export665 // table of the output.666 // N_PEXT: llvm-mc does not emit these, but `ld -r` (wherein ld64 emits667 // object files) may produce them. LLD does not yet support -r.668 // These are translation-unit scoped, identical to the `0` case.669 // 0: Translation-unit scoped. These are not in the symbol table during670 // link, and not in the export table of the output either.671 bool isWeakDefCanBeHidden =672 (sym.n_desc & (N_WEAK_DEF | N_WEAK_REF)) == (N_WEAK_DEF | N_WEAK_REF);673 674 assert(!(sym.n_desc & N_ARM_THUMB_DEF) && "ARM32 arch is not supported");675 676 if (sym.n_type & N_EXT) {677 // -load_hidden makes us treat global symbols as linkage unit scoped.678 // Duplicates are reported but the symbol does not go in the export trie.679 bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;680 681 // lld's behavior for merging symbols is slightly different from ld64:682 // ld64 picks the winning symbol based on several criteria (see683 // pickBetweenRegularAtoms() in ld64's SymbolTable.cpp), while lld684 // just merges metadata and keeps the contents of the first symbol685 // with that name (see SymbolTable::addDefined). For:686 // * inline function F in a TU built with -fvisibility-inlines-hidden687 // * and inline function F in another TU built without that flag688 // ld64 will pick the one from the file built without689 // -fvisibility-inlines-hidden.690 // lld will instead pick the one listed first on the link command line and691 // give it visibility as if the function was built without692 // -fvisibility-inlines-hidden.693 // If both functions have the same contents, this will have the same694 // behavior. If not, it won't, but the input had an ODR violation in695 // that case.696 //697 // Similarly, merging a symbol698 // that's isPrivateExtern and not isWeakDefCanBeHidden with one699 // that's not isPrivateExtern but isWeakDefCanBeHidden technically700 // should produce one701 // that's not isPrivateExtern but isWeakDefCanBeHidden. That matters702 // with ld64's semantics, because it means the non-private-extern703 // definition will continue to take priority if more private extern704 // definitions are encountered. With lld's semantics there's no observable705 // difference between a symbol that's isWeakDefCanBeHidden(autohide) or one706 // that's privateExtern -- neither makes it into the dynamic symbol table,707 // unless the autohide symbol is explicitly exported.708 // But if a symbol is both privateExtern and autohide then it can't709 // be exported.710 // So we nullify the autohide flag when privateExtern is present711 // and promote the symbol to privateExtern when it is not already.712 if (isWeakDefCanBeHidden && isPrivateExtern)713 isWeakDefCanBeHidden = false;714 else if (isWeakDefCanBeHidden)715 isPrivateExtern = true;716 return symtab->addDefined(717 name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,718 isPrivateExtern, sym.n_desc & REFERENCED_DYNAMICALLY,719 sym.n_desc & N_NO_DEAD_STRIP, isWeakDefCanBeHidden);720 }721 bool includeInSymtab = !isPrivateLabel(name) && !isEhFrameSection(isec);722 return make<Defined>(723 name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,724 /*isExternal=*/false, /*isPrivateExtern=*/false, includeInSymtab,725 sym.n_desc & REFERENCED_DYNAMICALLY, sym.n_desc & N_NO_DEAD_STRIP);726}727 728// Absolute symbols are defined symbols that do not have an associated729// InputSection. They cannot be weak.730template <class NList>731static macho::Symbol *createAbsolute(const NList &sym, InputFile *file,732 StringRef name, bool forceHidden) {733 assert(!(sym.n_desc & N_ARM_THUMB_DEF) && "ARM32 arch is not supported");734 735 if (sym.n_type & N_EXT) {736 bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;737 return symtab->addDefined(name, file, nullptr, sym.n_value, /*size=*/0,738 /*isWeakDef=*/false, isPrivateExtern,739 /*isReferencedDynamically=*/false,740 sym.n_desc & N_NO_DEAD_STRIP,741 /*isWeakDefCanBeHidden=*/false);742 }743 return make<Defined>(name, file, nullptr, sym.n_value, /*size=*/0,744 /*isWeakDef=*/false,745 /*isExternal=*/false, /*isPrivateExtern=*/false,746 /*includeInSymtab=*/true,747 /*isReferencedDynamically=*/false,748 sym.n_desc & N_NO_DEAD_STRIP);749}750 751template <class NList>752macho::Symbol *ObjFile::parseNonSectionSymbol(const NList &sym,753 const char *strtab) {754 StringRef name = StringRef(strtab + sym.n_strx);755 uint8_t type = sym.n_type & N_TYPE;756 bool isPrivateExtern = sym.n_type & N_PEXT || forceHidden;757 switch (type) {758 case N_UNDF:759 return sym.n_value == 0760 ? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)761 : symtab->addCommon(name, this, sym.n_value,762 1 << GET_COMM_ALIGN(sym.n_desc),763 isPrivateExtern);764 case N_ABS:765 return createAbsolute(sym, this, name, forceHidden);766 case N_INDR: {767 // Not much point in making local aliases -- relocs in the current file can768 // just refer to the actual symbol itself. ld64 ignores these symbols too.769 if (!(sym.n_type & N_EXT))770 return nullptr;771 StringRef aliasedName = StringRef(strtab + sym.n_value);772 // isPrivateExtern is the only symbol flag that has an impact on the final773 // aliased symbol.774 auto *alias = make<AliasSymbol>(this, name, aliasedName, isPrivateExtern);775 aliases.push_back(alias);776 return alias;777 }778 case N_PBUD:779 error("TODO: support symbols of type N_PBUD");780 return nullptr;781 case N_SECT:782 llvm_unreachable(783 "N_SECT symbols should not be passed to parseNonSectionSymbol");784 default:785 llvm_unreachable("invalid symbol type");786 }787}788 789template <class NList> static bool isUndef(const NList &sym) {790 return (sym.n_type & N_TYPE) == N_UNDF && sym.n_value == 0;791}792 793template <class LP>794void ObjFile::parseSymbols(ArrayRef<typename LP::section> sectionHeaders,795 ArrayRef<typename LP::nlist> nList,796 const char *strtab, bool subsectionsViaSymbols) {797 using NList = typename LP::nlist;798 799 // Groups indices of the symbols by the sections that contain them.800 std::vector<std::vector<uint32_t>> symbolsBySection(sections.size());801 symbols.resize(nList.size());802 SmallVector<unsigned, 32> undefineds;803 for (uint32_t i = 0; i < nList.size(); ++i) {804 const NList &sym = nList[i];805 806 // Ignore debug symbols for now.807 // FIXME: may need special handling.808 if (sym.n_type & N_STAB)809 continue;810 811 if ((sym.n_type & N_TYPE) == N_SECT) {812 if (sym.n_sect == 0) {813 fatal("section symbol " + StringRef(strtab + sym.n_strx) + " in " +814 toString(this) + " has an invalid section index [0]");815 }816 if (sym.n_sect > sections.size()) {817 fatal("section symbol " + StringRef(strtab + sym.n_strx) + " in " +818 toString(this) + " has an invalid section index [" +819 Twine(static_cast<unsigned>(sym.n_sect)) +820 "] greater than the total number of sections [" +821 Twine(sections.size()) + "]");822 }823 Subsections &subsections = sections[sym.n_sect - 1]->subsections;824 // parseSections() may have chosen not to parse this section.825 if (subsections.empty())826 continue;827 symbolsBySection[sym.n_sect - 1].push_back(i);828 } else if (isUndef(sym)) {829 undefineds.push_back(i);830 } else {831 symbols[i] = parseNonSectionSymbol(sym, strtab);832 }833 }834 835 for (size_t i = 0; i < sections.size(); ++i) {836 Subsections &subsections = sections[i]->subsections;837 if (subsections.empty())838 continue;839 std::vector<uint32_t> &symbolIndices = symbolsBySection[i];840 uint64_t sectionAddr = sectionHeaders[i].addr;841 uint32_t sectionAlign = 1u << sectionHeaders[i].align;842 843 // Some sections have already been split into subsections during844 // parseSections(), so we simply need to match Symbols to the corresponding845 // subsection here.846 if (sections[i]->doneSplitting) {847 for (size_t j = 0; j < symbolIndices.size(); ++j) {848 const uint32_t symIndex = symbolIndices[j];849 const NList &sym = nList[symIndex];850 StringRef name = strtab + sym.n_strx;851 uint64_t symbolOffset = sym.n_value - sectionAddr;852 InputSection *isec =853 findContainingSubsection(*sections[i], &symbolOffset);854 if (symbolOffset != 0) {855 error(toString(*sections[i]) + ": symbol " + name +856 " at misaligned offset");857 continue;858 }859 symbols[symIndex] =860 createDefined(sym, name, isec, 0, isec->getSize(), forceHidden);861 }862 continue;863 }864 sections[i]->doneSplitting = true;865 866 auto getSymName = [strtab](const NList& sym) -> StringRef {867 return StringRef(strtab + sym.n_strx);868 };869 870 // Calculate symbol sizes and create subsections by splitting the sections871 // along symbol boundaries.872 // We populate subsections by repeatedly splitting the last (highest873 // address) subsection.874 llvm::stable_sort(symbolIndices, [&](uint32_t lhs, uint32_t rhs) {875 // Put extern weak symbols after other symbols at the same address so876 // that weak symbol coalescing works correctly. See877 // SymbolTable::addDefined() for details.878 if (nList[lhs].n_value == nList[rhs].n_value &&879 nList[lhs].n_type & N_EXT && nList[rhs].n_type & N_EXT)880 return !(nList[lhs].n_desc & N_WEAK_DEF) && (nList[rhs].n_desc & N_WEAK_DEF);881 return nList[lhs].n_value < nList[rhs].n_value;882 });883 for (size_t j = 0; j < symbolIndices.size(); ++j) {884 const uint32_t symIndex = symbolIndices[j];885 const NList &sym = nList[symIndex];886 StringRef name = getSymName(sym);887 Subsection &subsec = subsections.back();888 InputSection *isec = subsec.isec;889 890 uint64_t subsecAddr = sectionAddr + subsec.offset;891 size_t symbolOffset = sym.n_value - subsecAddr;892 uint64_t symbolSize =893 j + 1 < symbolIndices.size()894 ? nList[symbolIndices[j + 1]].n_value - sym.n_value895 : isec->data.size() - symbolOffset;896 // There are 4 cases where we do not need to create a new subsection:897 // 1. If the input file does not use subsections-via-symbols.898 // 2. Multiple symbols at the same address only induce one subsection.899 // (The symbolOffset == 0 check covers both this case as well as900 // the first loop iteration.)901 // 3. Alternative entry points do not induce new subsections.902 // 4. If we have a literal section (e.g. __cstring and __literal4).903 if (!subsectionsViaSymbols || symbolOffset == 0 ||904 sym.n_desc & N_ALT_ENTRY || !isa<ConcatInputSection>(isec)) {905 isec->hasAltEntry = symbolOffset != 0;906 symbols[symIndex] = createDefined(sym, name, isec, symbolOffset,907 symbolSize, forceHidden);908 continue;909 }910 auto *concatIsec = cast<ConcatInputSection>(isec);911 912 auto *nextIsec = make<ConcatInputSection>(*concatIsec);913 nextIsec->wasCoalesced = false;914 if (isZeroFill(isec->getFlags())) {915 // Zero-fill sections have NULL data.data() non-zero data.size()916 nextIsec->data = {nullptr, isec->data.size() - symbolOffset};917 isec->data = {nullptr, symbolOffset};918 } else {919 nextIsec->data = isec->data.slice(symbolOffset);920 isec->data = isec->data.slice(0, symbolOffset);921 }922 923 // By construction, the symbol will be at offset zero in the new924 // subsection.925 symbols[symIndex] = createDefined(sym, name, nextIsec, /*value=*/0,926 symbolSize, forceHidden);927 // TODO: ld64 appears to preserve the original alignment as well as each928 // subsection's offset from the last aligned address. We should consider929 // emulating that behavior.930 nextIsec->align = MinAlign(sectionAlign, sym.n_value);931 subsections.push_back({sym.n_value - sectionAddr, nextIsec});932 }933 }934 935 // Undefined symbols can trigger recursive fetch from Archives due to936 // LazySymbols. Process defined symbols first so that the relative order937 // between a defined symbol and an undefined symbol does not change the938 // symbol resolution behavior. In addition, a set of interconnected symbols939 // will all be resolved to the same file, instead of being resolved to940 // different files.941 for (unsigned i : undefineds)942 symbols[i] = parseNonSectionSymbol(nList[i], strtab);943}944 945OpaqueFile::OpaqueFile(MemoryBufferRef mb, StringRef segName,946 StringRef sectName)947 : InputFile(OpaqueKind, mb) {948 const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());949 ArrayRef<uint8_t> data = {buf, mb.getBufferSize()};950 sections.push_back(make<Section>(/*file=*/this, segName.take_front(16),951 sectName.take_front(16),952 /*flags=*/0, /*addr=*/0));953 Section §ion = *sections.back();954 ConcatInputSection *isec = make<ConcatInputSection>(section, data);955 isec->live = true;956 section.subsections.push_back({0, isec});957}958 959template <class LP>960void ObjFile::parseLinkerOptions(SmallVectorImpl<StringRef> &LCLinkerOptions) {961 using Header = typename LP::mach_header;962 auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());963 964 for (auto *cmd : findCommands<linker_option_command>(hdr, LC_LINKER_OPTION)) {965 StringRef data{reinterpret_cast<const char *>(cmd + 1),966 cmd->cmdsize - sizeof(linker_option_command)};967 parseLCLinkerOption(LCLinkerOptions, this, cmd->count, data);968 }969}970 971SmallVector<StringRef> macho::unprocessedLCLinkerOptions;972ObjFile::ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,973 bool lazy, bool forceHidden, bool compatArch,974 bool builtFromBitcode)975 : InputFile(ObjKind, mb, lazy), modTime(modTime), forceHidden(forceHidden),976 builtFromBitcode(builtFromBitcode) {977 this->archiveName = std::string(archiveName);978 this->compatArch = compatArch;979 if (lazy) {980 if (target->wordSize == 8)981 parseLazy<LP64>();982 else983 parseLazy<ILP32>();984 } else {985 if (target->wordSize == 8)986 parse<LP64>();987 else988 parse<ILP32>();989 }990}991 992template <class LP> void ObjFile::parse() {993 using Header = typename LP::mach_header;994 using SegmentCommand = typename LP::segment_command;995 using SectionHeader = typename LP::section;996 using NList = typename LP::nlist;997 998 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());999 auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());1000 1001 // If we've already checked the arch, then don't need to check again.1002 if (!compatArch)1003 return;1004 if (!(compatArch = compatWithTargetArch(this, hdr)))1005 return;1006 1007 // We will resolve LC linker options once all native objects are loaded after1008 // LTO is finished.1009 SmallVector<StringRef, 4> LCLinkerOptions;1010 parseLinkerOptions<LP>(LCLinkerOptions);1011 unprocessedLCLinkerOptions.append(LCLinkerOptions);1012 1013 ArrayRef<SectionHeader> sectionHeaders;1014 if (const load_command *cmd = findCommand(hdr, LP::segmentLCType)) {1015 auto *c = reinterpret_cast<const SegmentCommand *>(cmd);1016 sectionHeaders = ArrayRef<SectionHeader>{1017 reinterpret_cast<const SectionHeader *>(c + 1), c->nsects};1018 parseSections(sectionHeaders);1019 }1020 1021 // TODO: Error on missing LC_SYMTAB?1022 if (const load_command *cmd = findCommand(hdr, LC_SYMTAB)) {1023 auto *c = reinterpret_cast<const symtab_command *>(cmd);1024 ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),1025 c->nsyms);1026 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;1027 bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;1028 parseSymbols<LP>(sectionHeaders, nList, strtab, subsectionsViaSymbols);1029 }1030 1031 // The relocations may refer to the symbols, so we parse them after we have1032 // parsed all the symbols.1033 for (size_t i = 0, n = sections.size(); i < n; ++i)1034 if (!sections[i]->subsections.empty())1035 parseRelocations(sectionHeaders, sectionHeaders[i], *sections[i]);1036 1037 parseDebugInfo();1038 1039 Section *ehFrameSection = nullptr;1040 Section *compactUnwindSection = nullptr;1041 for (Section *sec : sections) {1042 Section **s = StringSwitch<Section **>(sec->name)1043 .Case(section_names::compactUnwind, &compactUnwindSection)1044 .Case(section_names::ehFrame, &ehFrameSection)1045 .Default(nullptr);1046 if (s)1047 *s = sec;1048 }1049 if (compactUnwindSection)1050 registerCompactUnwind(*compactUnwindSection);1051 if (ehFrameSection)1052 registerEhFrames(*ehFrameSection);1053}1054 1055template <class LP> void ObjFile::parseLazy() {1056 using Header = typename LP::mach_header;1057 using NList = typename LP::nlist;1058 1059 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());1060 auto *hdr = reinterpret_cast<const Header *>(mb.getBufferStart());1061 1062 if (!compatArch)1063 return;1064 if (!(compatArch = compatWithTargetArch(this, hdr)))1065 return;1066 1067 const load_command *cmd = findCommand(hdr, LC_SYMTAB);1068 if (!cmd)1069 return;1070 auto *c = reinterpret_cast<const symtab_command *>(cmd);1071 ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),1072 c->nsyms);1073 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;1074 symbols.resize(nList.size());1075 for (const auto &[i, sym] : llvm::enumerate(nList)) {1076 if ((sym.n_type & N_EXT) && !isUndef(sym)) {1077 // TODO: Bound checking1078 StringRef name = strtab + sym.n_strx;1079 symbols[i] = symtab->addLazyObject(name, *this);1080 if (!lazy)1081 break;1082 }1083 }1084}1085 1086void ObjFile::parseDebugInfo() {1087 std::unique_ptr<DwarfObject> dObj = DwarfObject::create(this);1088 if (!dObj)1089 return;1090 1091 // We do not re-use the context from getDwarf() here as that function1092 // constructs an expensive DWARFCache object.1093 auto *ctx = make<DWARFContext>(1094 std::move(dObj), "",1095 [&](Error err) {1096 warn(toString(this) + ": " + toString(std::move(err)));1097 },1098 [&](Error warning) {1099 warn(toString(this) + ": " + toString(std::move(warning)));1100 });1101 1102 // TODO: Since object files can contain a lot of DWARF info, we should verify1103 // that we are parsing just the info we need1104 const DWARFContext::compile_unit_range &units = ctx->compile_units();1105 // FIXME: There can be more than one compile unit per object file. See1106 // PR48637.1107 auto it = units.begin();1108 compileUnit = it != units.end() ? it->get() : nullptr;1109}1110 1111ArrayRef<data_in_code_entry> ObjFile::getDataInCode() const {1112 const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());1113 const load_command *cmd = findCommand(buf, LC_DATA_IN_CODE);1114 if (!cmd)1115 return {};1116 const auto *c = reinterpret_cast<const linkedit_data_command *>(cmd);1117 return {reinterpret_cast<const data_in_code_entry *>(buf + c->dataoff),1118 c->datasize / sizeof(data_in_code_entry)};1119}1120 1121ArrayRef<uint8_t> ObjFile::getOptimizationHints() const {1122 const auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());1123 if (auto *cmd =1124 findCommand<linkedit_data_command>(buf, LC_LINKER_OPTIMIZATION_HINT))1125 return {buf + cmd->dataoff, cmd->datasize};1126 return {};1127}1128 1129// Create pointers from symbols to their associated compact unwind entries.1130void ObjFile::registerCompactUnwind(Section &compactUnwindSection) {1131 for (const Subsection &subsection : compactUnwindSection.subsections) {1132 ConcatInputSection *isec = cast<ConcatInputSection>(subsection.isec);1133 // Hack!! Each compact unwind entry (CUE) has its UNSIGNED relocations embed1134 // their addends in its data. Thus if ICF operated naively and compared the1135 // entire contents of each CUE, entries with identical unwind info but e.g.1136 // belonging to different functions would never be considered equivalent. To1137 // work around this problem, we remove some parts of the data containing the1138 // embedded addends. In particular, we remove the function address and LSDA1139 // pointers. Since these locations are at the start and end of the entry,1140 // we can do this using a simple, efficient slice rather than performing a1141 // copy. We are not losing any information here because the embedded1142 // addends have already been parsed in the corresponding Reloc structs.1143 //1144 // Removing these pointers would not be safe if they were pointers to1145 // absolute symbols. In that case, there would be no corresponding1146 // relocation. However, (AFAIK) MC cannot emit references to absolute1147 // symbols for either the function address or the LSDA. However, it *can* do1148 // so for the personality pointer, so we are not slicing that field away.1149 //1150 // Note that we do not adjust the offsets of the corresponding relocations;1151 // instead, we rely on `relocateCompactUnwind()` to correctly handle these1152 // truncated input sections.1153 isec->data = isec->data.slice(target->wordSize, 8 + target->wordSize);1154 uint32_t encoding = read32le(isec->data.data() + sizeof(uint32_t));1155 // llvm-mc omits CU entries for functions that need DWARF encoding, but1156 // `ld -r` doesn't. We can ignore them because we will re-synthesize these1157 // CU entries from the DWARF info during the output phase.1158 if ((encoding & static_cast<uint32_t>(UNWIND_MODE_MASK)) ==1159 target->modeDwarfEncoding)1160 continue;1161 1162 ConcatInputSection *referentIsec;1163 for (auto it = isec->relocs.begin(); it != isec->relocs.end();) {1164 Reloc &r = *it;1165 // CUE::functionAddress is at offset 0. Skip personality & LSDA relocs.1166 if (r.offset != 0) {1167 ++it;1168 continue;1169 }1170 uint64_t add = r.addend;1171 if (auto *sym = cast_or_null<Defined>(r.referent.dyn_cast<Symbol *>())) {1172 // Check whether the symbol defined in this file is the prevailing one.1173 // Skip if it is e.g. a weak def that didn't prevail.1174 if (sym->getFile() != this) {1175 ++it;1176 continue;1177 }1178 add += sym->value;1179 referentIsec = cast<ConcatInputSection>(sym->isec());1180 } else {1181 referentIsec =1182 cast<ConcatInputSection>(r.referent.dyn_cast<InputSection *>());1183 }1184 // Unwind info lives in __DATA, and finalization of __TEXT will occur1185 // before finalization of __DATA. Moreover, the finalization of unwind1186 // info depends on the exact addresses that it references. So it is safe1187 // for compact unwind to reference addresses in __TEXT, but not addresses1188 // in any other segment.1189 if (referentIsec->getSegName() != segment_names::text)1190 error(isec->getLocation(r.offset) + " references section " +1191 referentIsec->getName() + " which is not in segment __TEXT");1192 // The functionAddress relocations are typically section relocations.1193 // However, unwind info operates on a per-symbol basis, so we search for1194 // the function symbol here.1195 Defined *d = findSymbolAtOffset(referentIsec, add);1196 if (!d) {1197 ++it;1198 continue;1199 }1200 d->originalUnwindEntry = isec;1201 // Now that the symbol points to the unwind entry, we can remove the reloc1202 // that points from the unwind entry back to the symbol.1203 //1204 // First, the symbol keeps the unwind entry alive (and not vice versa), so1205 // this keeps dead-stripping simple.1206 //1207 // Moreover, it reduces the work that ICF needs to do to figure out if1208 // functions with unwind info are foldable.1209 //1210 // However, this does make it possible for ICF to fold CUEs that point to1211 // distinct functions (if the CUEs are otherwise identical).1212 // UnwindInfoSection takes care of this by re-duplicating the CUEs so that1213 // each one can hold a distinct functionAddress value.1214 //1215 // Given that clang emits relocations in reverse order of address, this1216 // relocation should be at the end of the vector for most of our input1217 // object files, so this erase() is typically an O(1) operation.1218 it = isec->relocs.erase(it);1219 }1220 }1221}1222 1223struct CIE {1224 macho::Symbol *personalitySymbol = nullptr;1225 bool fdesHaveAug = false;1226 uint8_t lsdaPtrSize = 0; // 0 => no LSDA1227 uint8_t funcPtrSize = 0;1228};1229 1230static uint8_t pointerEncodingToSize(uint8_t enc) {1231 switch (enc & 0xf) {1232 case dwarf::DW_EH_PE_absptr:1233 return target->wordSize;1234 case dwarf::DW_EH_PE_sdata4:1235 return 4;1236 case dwarf::DW_EH_PE_sdata8:1237 // ld64 doesn't actually support sdata8, but this seems simple enough...1238 return 8;1239 default:1240 return 0;1241 };1242}1243 1244static CIE parseCIE(const InputSection *isec, const EhReader &reader,1245 size_t off) {1246 // Handling the full generality of possible DWARF encodings would be a major1247 // pain. We instead take advantage of our knowledge of how llvm-mc encodes1248 // DWARF and handle just that.1249 constexpr uint8_t expectedPersonalityEnc =1250 dwarf::DW_EH_PE_pcrel | dwarf::DW_EH_PE_indirect | dwarf::DW_EH_PE_sdata4;1251 1252 CIE cie;1253 uint8_t version = reader.readByte(&off);1254 if (version != 1 && version != 3)1255 fatal("Expected CIE version of 1 or 3, got " + Twine(version));1256 StringRef aug = reader.readString(&off);1257 reader.skipLeb128(&off); // skip code alignment1258 reader.skipLeb128(&off); // skip data alignment1259 reader.skipLeb128(&off); // skip return address register1260 reader.skipLeb128(&off); // skip aug data length1261 uint64_t personalityAddrOff = 0;1262 for (char c : aug) {1263 switch (c) {1264 case 'z':1265 cie.fdesHaveAug = true;1266 break;1267 case 'P': {1268 uint8_t personalityEnc = reader.readByte(&off);1269 if (personalityEnc != expectedPersonalityEnc)1270 reader.failOn(off, "unexpected personality encoding 0x" +1271 Twine::utohexstr(personalityEnc));1272 personalityAddrOff = off;1273 off += 4;1274 break;1275 }1276 case 'L': {1277 uint8_t lsdaEnc = reader.readByte(&off);1278 cie.lsdaPtrSize = pointerEncodingToSize(lsdaEnc);1279 if (cie.lsdaPtrSize == 0)1280 reader.failOn(off, "unexpected LSDA encoding 0x" +1281 Twine::utohexstr(lsdaEnc));1282 break;1283 }1284 case 'R': {1285 uint8_t pointerEnc = reader.readByte(&off);1286 cie.funcPtrSize = pointerEncodingToSize(pointerEnc);1287 if (cie.funcPtrSize == 0 || !(pointerEnc & dwarf::DW_EH_PE_pcrel))1288 reader.failOn(off, "unexpected pointer encoding 0x" +1289 Twine::utohexstr(pointerEnc));1290 break;1291 }1292 default:1293 break;1294 }1295 }1296 if (personalityAddrOff != 0) {1297 const auto *personalityReloc = isec->getRelocAt(personalityAddrOff);1298 if (!personalityReloc)1299 reader.failOn(off, "Failed to locate relocation for personality symbol");1300 cie.personalitySymbol = cast<macho::Symbol *>(personalityReloc->referent);1301 }1302 return cie;1303}1304 1305// EH frame target addresses may be encoded as pcrel offsets. However, instead1306// of using an actual pcrel reloc, ld64 emits subtractor relocations instead.1307// This function recovers the target address from the subtractors, essentially1308// performing the inverse operation of EhRelocator.1309//1310// Concretely, we expect our relocations to write the value of `PC -1311// target_addr` to `PC`. `PC` itself is denoted by a minuend relocation that1312// points to a symbol plus an addend.1313//1314// It is important that the minuend relocation point to a symbol within the1315// same section as the fixup value, since sections may get moved around.1316//1317// For example, for arm64, llvm-mc emits relocations for the target function1318// address like so:1319//1320// ltmp:1321// <CIE start>1322// ...1323// <CIE end>1324// ... multiple FDEs ...1325// <FDE start>1326// <target function address - (ltmp + pcrel offset)>1327// ...1328//1329// If any of the FDEs in `multiple FDEs` get dead-stripped, then `FDE start`1330// will move to an earlier address, and `ltmp + pcrel offset` will no longer1331// reflect an accurate pcrel value. To avoid this problem, we "canonicalize"1332// our relocation by adding an `EH_Frame` symbol at `FDE start`, and updating1333// the reloc to be `target function address - (EH_Frame + new pcrel offset)`.1334//1335// If `Invert` is set, then we instead expect `target_addr - PC` to be written1336// to `PC`.1337template <bool Invert = false>1338Defined *1339targetSymFromCanonicalSubtractor(const InputSection *isec,1340 std::vector<macho::Reloc>::iterator relocIt) {1341 macho::Reloc &subtrahend = *relocIt;1342 macho::Reloc &minuend = *std::next(relocIt);1343 assert(target->hasAttr(subtrahend.type, RelocAttrBits::SUBTRAHEND));1344 assert(target->hasAttr(minuend.type, RelocAttrBits::UNSIGNED));1345 // Note: pcSym may *not* be exactly at the PC; there's usually a non-zero1346 // addend.1347 auto *pcSym = cast<Defined>(cast<macho::Symbol *>(subtrahend.referent));1348 Defined *target =1349 cast_or_null<Defined>(minuend.referent.dyn_cast<macho::Symbol *>());1350 if (!pcSym) {1351 auto *targetIsec =1352 cast<ConcatInputSection>(cast<InputSection *>(minuend.referent));1353 target = findSymbolAtOffset(targetIsec, minuend.addend);1354 }1355 if (Invert)1356 std::swap(pcSym, target);1357 if (pcSym->isec() == isec) {1358 if (pcSym->value - (Invert ? -1 : 1) * minuend.addend != subtrahend.offset)1359 fatal("invalid FDE relocation in __eh_frame");1360 } else {1361 // Ensure the pcReloc points to a symbol within the current EH frame.1362 // HACK: we should really verify that the original relocation's semantics1363 // are preserved. In particular, we should have1364 // `oldSym->value + oldOffset == newSym + newOffset`. However, we don't1365 // have an easy way to access the offsets from this point in the code; some1366 // refactoring is needed for that.1367 macho::Reloc &pcReloc = Invert ? minuend : subtrahend;1368 pcReloc.referent = isec->symbols[0];1369 assert(isec->symbols[0]->value == 0);1370 minuend.addend = pcReloc.offset * (Invert ? 1LL : -1LL);1371 }1372 return target;1373}1374 1375Defined *findSymbolAtAddress(const std::vector<Section *> §ions,1376 uint64_t addr) {1377 Section *sec = findContainingSection(sections, &addr);1378 auto *isec = cast<ConcatInputSection>(findContainingSubsection(*sec, &addr));1379 return findSymbolAtOffset(isec, addr);1380}1381 1382// For symbols that don't have compact unwind info, associate them with the more1383// general-purpose (and verbose) DWARF unwind info found in __eh_frame.1384//1385// This requires us to parse the contents of __eh_frame. See EhFrame.h for a1386// description of its format.1387//1388// While parsing, we also look for what MC calls "abs-ified" relocations -- they1389// are relocations which are implicitly encoded as offsets in the section data.1390// We convert them into explicit Reloc structs so that the EH frames can be1391// handled just like a regular ConcatInputSection later in our output phase.1392//1393// We also need to handle the case where our input object file has explicit1394// relocations. This is the case when e.g. it's the output of `ld -r`. We only1395// look for the "abs-ified" relocation if an explicit relocation is absent.1396void ObjFile::registerEhFrames(Section &ehFrameSection) {1397 DenseMap<const InputSection *, CIE> cieMap;1398 for (const Subsection &subsec : ehFrameSection.subsections) {1399 auto *isec = cast<ConcatInputSection>(subsec.isec);1400 uint64_t isecOff = subsec.offset;1401 1402 // Subtractor relocs require the subtrahend to be a symbol reloc. Ensure1403 // that all EH frames have an associated symbol so that we can generate1404 // subtractor relocs that reference them.1405 if (isec->symbols.size() == 0)1406 make<Defined>("EH_Frame", isec->getFile(), isec, /*value=*/0,1407 isec->getSize(), /*isWeakDef=*/false, /*isExternal=*/false,1408 /*isPrivateExtern=*/false, /*includeInSymtab=*/false,1409 /*isReferencedDynamically=*/false,1410 /*noDeadStrip=*/false);1411 else if (isec->symbols[0]->value != 0)1412 fatal("found symbol at unexpected offset in __eh_frame");1413 1414 EhReader reader(this, isec->data, subsec.offset);1415 size_t dataOff = 0; // Offset from the start of the EH frame.1416 reader.skipValidLength(&dataOff); // readLength() already validated this.1417 // cieOffOff is the offset from the start of the EH frame to the cieOff1418 // value, which is itself an offset from the current PC to a CIE.1419 const size_t cieOffOff = dataOff;1420 1421 EhRelocator ehRelocator(isec);1422 auto cieOffRelocIt = llvm::find_if(1423 isec->relocs, [=](const Reloc &r) { return r.offset == cieOffOff; });1424 InputSection *cieIsec = nullptr;1425 if (cieOffRelocIt != isec->relocs.end()) {1426 // We already have an explicit relocation for the CIE offset.1427 cieIsec =1428 targetSymFromCanonicalSubtractor</*Invert=*/true>(isec, cieOffRelocIt)1429 ->isec();1430 dataOff += sizeof(uint32_t);1431 } else {1432 // If we haven't found a relocation, then the CIE offset is most likely1433 // embedded in the section data (AKA an "abs-ified" reloc.). Parse that1434 // and generate a Reloc struct.1435 uint32_t cieMinuend = reader.readU32(&dataOff);1436 if (cieMinuend == 0) {1437 cieIsec = isec;1438 } else {1439 uint32_t cieOff = isecOff + dataOff - cieMinuend;1440 cieIsec = findContainingSubsection(ehFrameSection, &cieOff);1441 if (cieIsec == nullptr)1442 fatal("failed to find CIE");1443 }1444 if (cieIsec != isec)1445 ehRelocator.makeNegativePcRel(cieOffOff, cieIsec->symbols[0],1446 /*length=*/2);1447 }1448 if (cieIsec == isec) {1449 cieMap[cieIsec] = parseCIE(isec, reader, dataOff);1450 continue;1451 }1452 1453 assert(cieMap.count(cieIsec));1454 const CIE &cie = cieMap[cieIsec];1455 // Offset of the function address within the EH frame.1456 const size_t funcAddrOff = dataOff;1457 uint64_t funcAddr = reader.readPointer(&dataOff, cie.funcPtrSize) +1458 ehFrameSection.addr + isecOff + funcAddrOff;1459 uint32_t funcLength = reader.readPointer(&dataOff, cie.funcPtrSize);1460 size_t lsdaAddrOff = 0; // Offset of the LSDA address within the EH frame.1461 std::optional<uint64_t> lsdaAddrOpt;1462 if (cie.fdesHaveAug) {1463 reader.skipLeb128(&dataOff);1464 lsdaAddrOff = dataOff;1465 if (cie.lsdaPtrSize != 0) {1466 uint64_t lsdaOff = reader.readPointer(&dataOff, cie.lsdaPtrSize);1467 if (lsdaOff != 0) // FIXME possible to test this?1468 lsdaAddrOpt = ehFrameSection.addr + isecOff + lsdaAddrOff + lsdaOff;1469 }1470 }1471 1472 auto funcAddrRelocIt = isec->relocs.end();1473 auto lsdaAddrRelocIt = isec->relocs.end();1474 for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {1475 if (it->offset == funcAddrOff)1476 funcAddrRelocIt = it++; // Found subtrahend; skip over minuend reloc1477 else if (lsdaAddrOpt && it->offset == lsdaAddrOff)1478 lsdaAddrRelocIt = it++; // Found subtrahend; skip over minuend reloc1479 }1480 1481 Defined *funcSym;1482 if (funcAddrRelocIt != isec->relocs.end()) {1483 funcSym = targetSymFromCanonicalSubtractor(isec, funcAddrRelocIt);1484 // Canonicalize the symbol. If there are multiple symbols at the same1485 // address, we want both `registerEhFrame` and `registerCompactUnwind`1486 // to register the unwind entry under same symbol.1487 // This is not particularly efficient, but we should run into this case1488 // infrequently (only when handling the output of `ld -r`).1489 if (funcSym->isec())1490 funcSym = findSymbolAtOffset(cast<ConcatInputSection>(funcSym->isec()),1491 funcSym->value);1492 } else {1493 funcSym = findSymbolAtAddress(sections, funcAddr);1494 ehRelocator.makePcRel(funcAddrOff, funcSym, target->p2WordSize);1495 }1496 // The symbol has been coalesced, or already has a compact unwind entry.1497 if (!funcSym || funcSym->getFile() != this || funcSym->unwindEntry()) {1498 // We must prune unused FDEs for correctness, so we cannot rely on1499 // -dead_strip being enabled.1500 isec->live = false;1501 continue;1502 }1503 1504 InputSection *lsdaIsec = nullptr;1505 if (lsdaAddrRelocIt != isec->relocs.end()) {1506 lsdaIsec =1507 targetSymFromCanonicalSubtractor(isec, lsdaAddrRelocIt)->isec();1508 } else if (lsdaAddrOpt) {1509 uint64_t lsdaAddr = *lsdaAddrOpt;1510 Section *sec = findContainingSection(sections, &lsdaAddr);1511 lsdaIsec =1512 cast<ConcatInputSection>(findContainingSubsection(*sec, &lsdaAddr));1513 ehRelocator.makePcRel(lsdaAddrOff, lsdaIsec, target->p2WordSize);1514 }1515 1516 fdes[isec] = {funcLength, cie.personalitySymbol, lsdaIsec};1517 funcSym->originalUnwindEntry = isec;1518 ehRelocator.commit();1519 }1520 1521 // __eh_frame is marked as S_ATTR_LIVE_SUPPORT in input files, because FDEs1522 // are normally required to be kept alive if they reference a live symbol.1523 // However, we've explicitly created a dependency from a symbol to its FDE, so1524 // dead-stripping will just work as usual, and S_ATTR_LIVE_SUPPORT will only1525 // serve to incorrectly prevent us from dead-stripping duplicate FDEs for a1526 // live symbol (e.g. if there were multiple weak copies). Remove this flag to1527 // let dead-stripping proceed correctly.1528 ehFrameSection.flags &= ~S_ATTR_LIVE_SUPPORT;1529}1530 1531std::string ObjFile::sourceFile() const {1532 const char *unitName = compileUnit->getUnitDIE().getShortName();1533 // DWARF allows DW_AT_name to be absolute, in which case nothing should be1534 // prepended. As for the styles, debug info can contain paths from any OS, not1535 // necessarily an OS we're currently running on. Moreover different1536 // compilation units can be compiled on different operating systems and linked1537 // together later.1538 if (sys::path::is_absolute(unitName, llvm::sys::path::Style::posix) ||1539 sys::path::is_absolute(unitName, llvm::sys::path::Style::windows))1540 return unitName;1541 SmallString<261> dir(compileUnit->getCompilationDir());1542 StringRef sep = sys::path::get_separator();1543 // We don't use `path::append` here because we want an empty `dir` to result1544 // in an absolute path. `append` would give us a relative path for that case.1545 if (!dir.ends_with(sep))1546 dir += sep;1547 return (dir + unitName).str();1548}1549 1550lld::DWARFCache *ObjFile::getDwarf() {1551 llvm::call_once(initDwarf, [this]() {1552 auto dwObj = DwarfObject::create(this);1553 if (!dwObj)1554 return;1555 dwarfCache = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(1556 std::move(dwObj), "",1557 [&](Error err) { warn(getName() + ": " + toString(std::move(err))); },1558 [&](Error warning) {1559 warn(getName() + ": " + toString(std::move(warning)));1560 }));1561 });1562 1563 return dwarfCache.get();1564}1565// The path can point to either a dylib or a .tbd file.1566static DylibFile *loadDylib(StringRef path, DylibFile *umbrella) {1567 std::optional<MemoryBufferRef> mbref = readFile(path);1568 if (!mbref) {1569 error("could not read dylib file at " + path);1570 return nullptr;1571 }1572 return loadDylib(*mbref, umbrella);1573}1574 1575// TBD files are parsed into a series of TAPI documents (InterfaceFiles), with1576// the first document storing child pointers to the rest of them. When we are1577// processing a given TBD file, we store that top-level document in1578// currentTopLevelTapi. When processing re-exports, we search its children for1579// potentially matching documents in the same TBD file. Note that the children1580// themselves don't point to further documents, i.e. this is a two-level tree.1581//1582// Re-exports can either refer to on-disk files, or to documents within .tbd1583// files.1584static DylibFile *findDylib(StringRef path, DylibFile *umbrella,1585 const InterfaceFile *currentTopLevelTapi) {1586 // Search order:1587 // 1. Install name basename in -F / -L directories.1588 {1589 // Framework names can be in multiple formats:1590 // - Foo.framework/Foo1591 // - Foo.framework/Versions/A/Foo1592 StringRef stem = path::stem(path);1593 SmallString<128> frameworkName("/");1594 frameworkName += stem;1595 frameworkName += ".framework/";1596 size_t i = path.rfind(frameworkName);1597 if (i != StringRef::npos) {1598 StringRef frameworkPath = path.substr(i + 1);1599 for (StringRef dir : config->frameworkSearchPaths) {1600 SmallString<128> candidate = dir;1601 path::append(candidate, frameworkPath);1602 if (std::optional<StringRef> dylibPath =1603 resolveDylibPath(candidate.str()))1604 return loadDylib(*dylibPath, umbrella);1605 }1606 } else if (std::optional<StringRef> dylibPath = findPathCombination(1607 stem, config->librarySearchPaths, {".tbd", ".dylib", ".so"}))1608 return loadDylib(*dylibPath, umbrella);1609 }1610 1611 // 2. As absolute path.1612 if (path::is_absolute(path, path::Style::posix))1613 for (StringRef root : config->systemLibraryRoots)1614 if (std::optional<StringRef> dylibPath =1615 resolveDylibPath((root + path).str()))1616 return loadDylib(*dylibPath, umbrella);1617 1618 // 3. As relative path.1619 1620 // TODO: Handle -dylib_file1621 1622 // Replace @executable_path, @loader_path, @rpath prefixes in install name.1623 SmallString<128> newPath;1624 if (config->outputType == MH_EXECUTE &&1625 path.consume_front("@executable_path/")) {1626 // ld64 allows overriding this with the undocumented flag -executable_path.1627 // lld doesn't currently implement that flag.1628 // FIXME: Consider using finalOutput instead of outputFile.1629 path::append(newPath, path::parent_path(config->outputFile), path);1630 path = newPath;1631 } else if (path.consume_front("@loader_path/")) {1632 fs::real_path(umbrella->getName(), newPath);1633 path::remove_filename(newPath);1634 path::append(newPath, path);1635 path = newPath;1636 } else if (path.starts_with("@rpath/")) {1637 for (StringRef rpath : umbrella->rpaths) {1638 newPath.clear();1639 if (rpath.consume_front("@loader_path/")) {1640 fs::real_path(umbrella->getName(), newPath);1641 path::remove_filename(newPath);1642 }1643 path::append(newPath, rpath, path.drop_front(strlen("@rpath/")));1644 if (std::optional<StringRef> dylibPath = resolveDylibPath(newPath.str()))1645 return loadDylib(*dylibPath, umbrella);1646 }1647 // If not found in umbrella, try the rpaths specified via -rpath too.1648 for (StringRef rpath : config->runtimePaths) {1649 newPath.clear();1650 if (rpath.consume_front("@loader_path/")) {1651 fs::real_path(umbrella->getName(), newPath);1652 path::remove_filename(newPath);1653 }1654 path::append(newPath, rpath, path.drop_front(strlen("@rpath/")));1655 if (std::optional<StringRef> dylibPath = resolveDylibPath(newPath.str()))1656 return loadDylib(*dylibPath, umbrella);1657 }1658 }1659 1660 // FIXME: Should this be further up?1661 if (currentTopLevelTapi) {1662 for (InterfaceFile &child :1663 make_pointee_range(currentTopLevelTapi->documents())) {1664 assert(child.documents().empty());1665 if (path == child.getInstallName()) {1666 auto *file = make<DylibFile>(child, umbrella, /*isBundleLoader=*/false,1667 /*explicitlyLinked=*/false);1668 file->parseReexports(child);1669 return file;1670 }1671 }1672 }1673 1674 if (std::optional<StringRef> dylibPath = resolveDylibPath(path))1675 return loadDylib(*dylibPath, umbrella);1676 1677 return nullptr;1678}1679 1680// If a re-exported dylib is public (lives in /usr/lib or1681// /System/Library/Frameworks), then it is considered implicitly linked: we1682// should bind to its symbols directly instead of via the re-exporting umbrella1683// library.1684static bool isImplicitlyLinked(StringRef path) {1685 if (!config->implicitDylibs)1686 return false;1687 1688 if (path::parent_path(path) == "/usr/lib")1689 return true;1690 1691 // Match /System/Library/Frameworks/$FOO.framework/**/$FOO1692 if (path.consume_front("/System/Library/Frameworks/")) {1693 StringRef frameworkName = path.take_until([](char c) { return c == '.'; });1694 return path::filename(path) == frameworkName;1695 }1696 1697 return false;1698}1699 1700void DylibFile::loadReexport(StringRef path, DylibFile *umbrella,1701 const InterfaceFile *currentTopLevelTapi) {1702 DylibFile *reexport = findDylib(path, umbrella, currentTopLevelTapi);1703 if (!reexport) {1704 // If not found in umbrella, retry since some rpaths might have been1705 // defined in "this" dylib (which contains the LC_REEXPORT_DYLIB cmd) and1706 // not in the umbrella.1707 DylibFile *reexport2 = findDylib(path, this, currentTopLevelTapi);1708 if (!reexport2) {1709 error(toString(this) + ": unable to locate re-export with install name " +1710 path);1711 }1712 }1713}1714 1715DylibFile::DylibFile(MemoryBufferRef mb, DylibFile *umbrella,1716 bool isBundleLoader, bool explicitlyLinked)1717 : InputFile(DylibKind, mb), refState(RefState::Unreferenced),1718 explicitlyLinked(explicitlyLinked), isBundleLoader(isBundleLoader) {1719 assert(!isBundleLoader || !umbrella);1720 if (umbrella == nullptr)1721 umbrella = this;1722 this->umbrella = umbrella;1723 1724 auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());1725 1726 // Initialize installName.1727 if (const load_command *cmd = findCommand(hdr, LC_ID_DYLIB)) {1728 auto *c = reinterpret_cast<const dylib_command *>(cmd);1729 currentVersion = read32le(&c->dylib.current_version);1730 compatibilityVersion = read32le(&c->dylib.compatibility_version);1731 installName =1732 reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);1733 } else if (!isBundleLoader) {1734 // macho_executable and macho_bundle don't have LC_ID_DYLIB,1735 // so it's OK.1736 error(toString(this) + ": dylib missing LC_ID_DYLIB load command");1737 return;1738 }1739 1740 if (config->printEachFile)1741 message(toString(this));1742 inputFiles.insert(this);1743 1744 deadStrippable = hdr->flags & MH_DEAD_STRIPPABLE_DYLIB;1745 1746 if (!checkCompatibility(this))1747 return;1748 1749 checkAppExtensionSafety(hdr->flags & MH_APP_EXTENSION_SAFE);1750 1751 for (auto *cmd : findCommands<rpath_command>(hdr, LC_RPATH)) {1752 StringRef rpath{reinterpret_cast<const char *>(cmd) + cmd->path};1753 rpaths.push_back(rpath);1754 }1755 1756 // Initialize symbols.1757 bool canBeImplicitlyLinked = findCommand(hdr, LC_SUB_CLIENT) == nullptr;1758 exportingFile = (canBeImplicitlyLinked && isImplicitlyLinked(installName))1759 ? this1760 : this->umbrella;1761 1762 if (!canBeImplicitlyLinked) {1763 for (auto *cmd : findCommands<sub_client_command>(hdr, LC_SUB_CLIENT)) {1764 StringRef allowableClient{reinterpret_cast<const char *>(cmd) +1765 cmd->client};1766 allowableClients.push_back(allowableClient);1767 }1768 }1769 1770 const auto *dyldInfo = findCommand<dyld_info_command>(hdr, LC_DYLD_INFO_ONLY);1771 const auto *exportsTrie =1772 findCommand<linkedit_data_command>(hdr, LC_DYLD_EXPORTS_TRIE);1773 if (dyldInfo && exportsTrie) {1774 // It's unclear what should happen in this case. Maybe we should only error1775 // out if the two load commands refer to different data?1776 error(toString(this) +1777 ": dylib has both LC_DYLD_INFO_ONLY and LC_DYLD_EXPORTS_TRIE");1778 return;1779 }1780 1781 if (dyldInfo) {1782 parseExportedSymbols(dyldInfo->export_off, dyldInfo->export_size);1783 } else if (exportsTrie) {1784 parseExportedSymbols(exportsTrie->dataoff, exportsTrie->datasize);1785 } else {1786 error("No LC_DYLD_INFO_ONLY or LC_DYLD_EXPORTS_TRIE found in " +1787 toString(this));1788 }1789}1790 1791void DylibFile::parseExportedSymbols(uint32_t offset, uint32_t size) {1792 struct TrieEntry {1793 StringRef name;1794 uint64_t flags;1795 };1796 1797 auto *buf = reinterpret_cast<const uint8_t *>(mb.getBufferStart());1798 std::vector<TrieEntry> entries;1799 // Find all the $ld$* symbols to process first.1800 parseTrie(toString(this), buf + offset, size,1801 [&](const Twine &name, uint64_t flags) {1802 StringRef savedName = saver().save(name);1803 if (handleLDSymbol(savedName))1804 return;1805 entries.push_back({savedName, flags});1806 });1807 1808 // Process the "normal" symbols.1809 for (TrieEntry &entry : entries) {1810 if (exportingFile->hiddenSymbols.contains(CachedHashStringRef(entry.name)))1811 continue;1812 1813 bool isWeakDef = entry.flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION;1814 bool isTlv = entry.flags & EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL;1815 1816 symbols.push_back(1817 symtab->addDylib(entry.name, exportingFile, isWeakDef, isTlv));1818 }1819}1820 1821void DylibFile::parseLoadCommands(MemoryBufferRef mb) {1822 auto *hdr = reinterpret_cast<const mach_header *>(mb.getBufferStart());1823 const uint8_t *p = reinterpret_cast<const uint8_t *>(mb.getBufferStart()) +1824 target->headerSize;1825 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {1826 auto *cmd = reinterpret_cast<const load_command *>(p);1827 p += cmd->cmdsize;1828 1829 if (!(hdr->flags & MH_NO_REEXPORTED_DYLIBS) &&1830 cmd->cmd == LC_REEXPORT_DYLIB) {1831 const auto *c = reinterpret_cast<const dylib_command *>(cmd);1832 StringRef reexportPath =1833 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);1834 loadReexport(reexportPath, exportingFile, nullptr);1835 }1836 1837 // FIXME: What about LC_LOAD_UPWARD_DYLIB, LC_LAZY_LOAD_DYLIB,1838 // LC_LOAD_WEAK_DYLIB, LC_REEXPORT_DYLIB (..are reexports from dylibs with1839 // MH_NO_REEXPORTED_DYLIBS loaded for -flat_namespace)?1840 if (config->namespaceKind == NamespaceKind::flat &&1841 cmd->cmd == LC_LOAD_DYLIB) {1842 const auto *c = reinterpret_cast<const dylib_command *>(cmd);1843 StringRef dylibPath =1844 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);1845 DylibFile *dylib = findDylib(dylibPath, umbrella, nullptr);1846 if (!dylib)1847 error(Twine("unable to locate library '") + dylibPath +1848 "' loaded from '" + toString(this) + "' for -flat_namespace");1849 }1850 }1851}1852 1853// Some versions of Xcode ship with .tbd files that don't have the right1854// platform settings.1855constexpr std::array<StringRef, 3> skipPlatformChecks{1856 "/usr/lib/system/libsystem_kernel.dylib",1857 "/usr/lib/system/libsystem_platform.dylib",1858 "/usr/lib/system/libsystem_pthread.dylib"};1859 1860static bool skipPlatformCheckForCatalyst(const InterfaceFile &interface,1861 bool explicitlyLinked) {1862 // Catalyst outputs can link against implicitly linked macOS-only libraries.1863 if (config->platform() != PLATFORM_MACCATALYST || explicitlyLinked)1864 return false;1865 return is_contained(interface.targets(),1866 MachO::Target(config->arch(), PLATFORM_MACOS));1867}1868 1869static bool isArchABICompatible(ArchitectureSet archSet,1870 Architecture targetArch) {1871 uint32_t cpuType;1872 uint32_t targetCpuType;1873 std::tie(targetCpuType, std::ignore) = getCPUTypeFromArchitecture(targetArch);1874 1875 return llvm::any_of(archSet, [&](const auto &p) {1876 std::tie(cpuType, std::ignore) = getCPUTypeFromArchitecture(p);1877 return cpuType == targetCpuType;1878 });1879}1880 1881static bool isTargetPlatformArchCompatible(1882 InterfaceFile::const_target_range interfaceTargets, Target target) {1883 if (is_contained(interfaceTargets, target))1884 return true;1885 1886 if (config->forceExactCpuSubtypeMatch)1887 return false;1888 1889 ArchitectureSet archSet;1890 for (const auto &p : interfaceTargets)1891 if (p.Platform == target.Platform)1892 archSet.set(p.Arch);1893 if (archSet.empty())1894 return false;1895 1896 return isArchABICompatible(archSet, target.Arch);1897}1898 1899DylibFile::DylibFile(const InterfaceFile &interface, DylibFile *umbrella,1900 bool isBundleLoader, bool explicitlyLinked)1901 : InputFile(DylibKind, interface), refState(RefState::Unreferenced),1902 explicitlyLinked(explicitlyLinked), isBundleLoader(isBundleLoader) {1903 // FIXME: Add test for the missing TBD code path.1904 1905 if (umbrella == nullptr)1906 umbrella = this;1907 this->umbrella = umbrella;1908 1909 installName = saver().save(interface.getInstallName());1910 compatibilityVersion = interface.getCompatibilityVersion().rawValue();1911 currentVersion = interface.getCurrentVersion().rawValue();1912 for (const auto &rpath : interface.rpaths())1913 if (rpath.first == config->platformInfo.target)1914 rpaths.push_back(saver().save(rpath.second));1915 1916 if (config->printEachFile)1917 message(toString(this));1918 inputFiles.insert(this);1919 1920 if (!is_contained(skipPlatformChecks, installName) &&1921 !isTargetPlatformArchCompatible(interface.targets(),1922 config->platformInfo.target) &&1923 !skipPlatformCheckForCatalyst(interface, explicitlyLinked)) {1924 error(toString(this) + " is incompatible with " +1925 std::string(config->platformInfo.target));1926 return;1927 }1928 1929 checkAppExtensionSafety(interface.isApplicationExtensionSafe());1930 1931 bool canBeImplicitlyLinked = interface.allowableClients().size() == 0;1932 exportingFile = (canBeImplicitlyLinked && isImplicitlyLinked(installName))1933 ? this1934 : umbrella;1935 1936 if (!canBeImplicitlyLinked)1937 for (const auto &allowableClient : interface.allowableClients())1938 allowableClients.push_back(1939 *make<std::string>(allowableClient.getInstallName().data()));1940 1941 auto addSymbol = [&](const llvm::MachO::Symbol &symbol,1942 const Twine &name) -> void {1943 StringRef savedName = saver().save(name);1944 if (exportingFile->hiddenSymbols.contains(CachedHashStringRef(savedName)))1945 return;1946 1947 symbols.push_back(symtab->addDylib(savedName, exportingFile,1948 symbol.isWeakDefined(),1949 symbol.isThreadLocalValue()));1950 };1951 1952 std::vector<const llvm::MachO::Symbol *> normalSymbols;1953 normalSymbols.reserve(interface.symbolsCount());1954 for (const auto *symbol : interface.symbols()) {1955 if (!isArchABICompatible(symbol->getArchitectures(), config->arch()))1956 continue;1957 if (handleLDSymbol(symbol->getName()))1958 continue;1959 1960 switch (symbol->getKind()) {1961 case EncodeKind::GlobalSymbol:1962 case EncodeKind::ObjectiveCClass:1963 case EncodeKind::ObjectiveCClassEHType:1964 case EncodeKind::ObjectiveCInstanceVariable:1965 normalSymbols.push_back(symbol);1966 }1967 }1968 // interface.symbols() order is non-deterministic.1969 llvm::sort(normalSymbols,1970 [](auto *l, auto *r) { return l->getName() < r->getName(); });1971 1972 // TODO(compnerd) filter out symbols based on the target platform1973 for (const auto *symbol : normalSymbols) {1974 switch (symbol->getKind()) {1975 case EncodeKind::GlobalSymbol:1976 addSymbol(*symbol, symbol->getName());1977 break;1978 case EncodeKind::ObjectiveCClass:1979 // XXX ld64 only creates these symbols when -ObjC is passed in. We may1980 // want to emulate that.1981 addSymbol(*symbol, objc::symbol_names::klass + symbol->getName());1982 addSymbol(*symbol, objc::symbol_names::metaclass + symbol->getName());1983 break;1984 case EncodeKind::ObjectiveCClassEHType:1985 addSymbol(*symbol, objc::symbol_names::ehtype + symbol->getName());1986 break;1987 case EncodeKind::ObjectiveCInstanceVariable:1988 addSymbol(*symbol, objc::symbol_names::ivar + symbol->getName());1989 break;1990 }1991 }1992}1993 1994DylibFile::DylibFile(DylibFile *umbrella)1995 : InputFile(DylibKind, MemoryBufferRef{}), refState(RefState::Unreferenced),1996 explicitlyLinked(false), isBundleLoader(false) {1997 if (umbrella == nullptr)1998 umbrella = this;1999 this->umbrella = umbrella;2000}2001 2002void DylibFile::parseReexports(const InterfaceFile &interface) {2003 const InterfaceFile *topLevel =2004 interface.getParent() == nullptr ? &interface : interface.getParent();2005 for (const InterfaceFileRef &intfRef : interface.reexportedLibraries()) {2006 InterfaceFile::const_target_range targets = intfRef.targets();2007 if (is_contained(skipPlatformChecks, intfRef.getInstallName()) ||2008 isTargetPlatformArchCompatible(targets, config->platformInfo.target))2009 loadReexport(intfRef.getInstallName(), exportingFile, topLevel);2010 }2011}2012 2013bool DylibFile::isExplicitlyLinked() const {2014 if (!explicitlyLinked)2015 return false;2016 2017 // If this dylib was explicitly linked, but at least one of the symbols2018 // of the synthetic dylibs it created via $ld$previous symbols is2019 // referenced, then that synthetic dylib fulfils the explicit linkedness2020 // and we can deadstrip this dylib if it's unreferenced.2021 for (const auto *dylib : extraDylibs)2022 if (dylib->isReferenced())2023 return false;2024 2025 return true;2026}2027 2028DylibFile *DylibFile::getSyntheticDylib(StringRef installName,2029 uint32_t currentVersion,2030 uint32_t compatVersion) {2031 for (DylibFile *dylib : extraDylibs)2032 if (dylib->installName == installName) {2033 // FIXME: Check what to do if different $ld$previous symbols2034 // request the same dylib, but with different versions.2035 return dylib;2036 }2037 2038 auto *dylib = make<DylibFile>(umbrella == this ? nullptr : umbrella);2039 dylib->installName = saver().save(installName);2040 dylib->currentVersion = currentVersion;2041 dylib->compatibilityVersion = compatVersion;2042 extraDylibs.push_back(dylib);2043 return dylib;2044}2045 2046// $ld$ symbols modify the properties/behavior of the library (e.g. its install2047// name, compatibility version or hide/add symbols) for specific target2048// versions.2049bool DylibFile::handleLDSymbol(StringRef originalName) {2050 if (!originalName.starts_with("$ld$"))2051 return false;2052 2053 StringRef action;2054 StringRef name;2055 std::tie(action, name) = originalName.drop_front(strlen("$ld$")).split('$');2056 if (action == "previous")2057 handleLDPreviousSymbol(name, originalName);2058 else if (action == "install_name")2059 handleLDInstallNameSymbol(name, originalName);2060 else if (action == "hide")2061 handleLDHideSymbol(name, originalName);2062 return true;2063}2064 2065void DylibFile::handleLDPreviousSymbol(StringRef name, StringRef originalName) {2066 // originalName: $ld$ previous $ <installname> $ <compatversion> $2067 // <platformstr> $ <startversion> $ <endversion> $ <symbol-name> $2068 StringRef installName;2069 StringRef compatVersion;2070 StringRef platformStr;2071 StringRef startVersion;2072 StringRef endVersion;2073 StringRef symbolName;2074 StringRef rest;2075 2076 std::tie(installName, name) = name.split('$');2077 std::tie(compatVersion, name) = name.split('$');2078 std::tie(platformStr, name) = name.split('$');2079 std::tie(startVersion, name) = name.split('$');2080 std::tie(endVersion, name) = name.split('$');2081 std::tie(symbolName, rest) = name.rsplit('$');2082 2083 // FIXME: Does this do the right thing for zippered files?2084 unsigned platform;2085 if (platformStr.getAsInteger(10, platform) ||2086 platform != static_cast<unsigned>(config->platform()))2087 return;2088 2089 VersionTuple start;2090 if (start.tryParse(startVersion)) {2091 warn(toString(this) + ": failed to parse start version, symbol '" +2092 originalName + "' ignored");2093 return;2094 }2095 VersionTuple end;2096 if (end.tryParse(endVersion)) {2097 warn(toString(this) + ": failed to parse end version, symbol '" +2098 originalName + "' ignored");2099 return;2100 }2101 if (config->platformInfo.target.MinDeployment < start ||2102 config->platformInfo.target.MinDeployment >= end)2103 return;2104 2105 // Initialized to compatibilityVersion for the symbolName branch below.2106 uint32_t newCompatibilityVersion = compatibilityVersion;2107 uint32_t newCurrentVersionForSymbol = currentVersion;2108 if (!compatVersion.empty()) {2109 VersionTuple cVersion;2110 if (cVersion.tryParse(compatVersion)) {2111 warn(toString(this) +2112 ": failed to parse compatibility version, symbol '" + originalName +2113 "' ignored");2114 return;2115 }2116 newCompatibilityVersion = encodeVersion(cVersion);2117 newCurrentVersionForSymbol = newCompatibilityVersion;2118 }2119 2120 if (!symbolName.empty()) {2121 // A $ld$previous$ symbol with symbol name adds a symbol with that name to2122 // a dylib with given name and version.2123 auto *dylib = getSyntheticDylib(installName, newCurrentVersionForSymbol,2124 newCompatibilityVersion);2125 2126 // The tbd file usually contains the $ld$previous symbol for an old version,2127 // and then the symbol itself later, for newer deployment targets, like so:2128 // symbols: [2129 // '$ld$previous$/Another$$1$3.0$14.0$_zzz$',2130 // _zzz,2131 // ]2132 // Since the symbols are sorted, adding them to the symtab in the given2133 // order means the $ld$previous version of _zzz will prevail, as desired.2134 dylib->symbols.push_back(symtab->addDylib(2135 saver().save(symbolName), dylib, /*isWeakDef=*/false, /*isTlv=*/false));2136 return;2137 }2138 2139 // A $ld$previous$ symbol without symbol name modifies the dylib it's in.2140 this->installName = saver().save(installName);2141 this->compatibilityVersion = newCompatibilityVersion;2142}2143 2144void DylibFile::handleLDInstallNameSymbol(StringRef name,2145 StringRef originalName) {2146 // originalName: $ld$ install_name $ os<version> $ install_name2147 StringRef condition, installName;2148 std::tie(condition, installName) = name.split('$');2149 VersionTuple version;2150 if (!condition.consume_front("os") || version.tryParse(condition))2151 warn(toString(this) + ": failed to parse os version, symbol '" +2152 originalName + "' ignored");2153 else if (version == config->platformInfo.target.MinDeployment)2154 this->installName = saver().save(installName);2155}2156 2157void DylibFile::handleLDHideSymbol(StringRef name, StringRef originalName) {2158 StringRef symbolName;2159 bool shouldHide = true;2160 if (name.starts_with("os")) {2161 // If it's hidden based on versions.2162 name = name.drop_front(2);2163 StringRef minVersion;2164 std::tie(minVersion, symbolName) = name.split('$');2165 VersionTuple versionTup;2166 if (versionTup.tryParse(minVersion)) {2167 warn(toString(this) + ": failed to parse hidden version, symbol `" + originalName +2168 "` ignored.");2169 return;2170 }2171 shouldHide = versionTup == config->platformInfo.target.MinDeployment;2172 } else {2173 symbolName = name;2174 }2175 2176 if (shouldHide)2177 exportingFile->hiddenSymbols.insert(CachedHashStringRef(symbolName));2178}2179 2180void DylibFile::checkAppExtensionSafety(bool dylibIsAppExtensionSafe) const {2181 if (config->applicationExtension && !dylibIsAppExtensionSafe)2182 warn("using '-application_extension' with unsafe dylib: " + toString(this));2183}2184 2185ArchiveFile::ArchiveFile(std::unique_ptr<object::Archive> &&f, bool forceHidden)2186 : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)),2187 forceHidden(forceHidden) {}2188 2189void ArchiveFile::addLazySymbols() {2190 // Avoid calling getMemoryBufferRef() on zero-symbol archive2191 // since that crashes.2192 if (file->isEmpty() ||2193 (file->hasSymbolTable() && file->getNumberOfSymbols() == 0))2194 return;2195 2196 if (!file->hasSymbolTable()) {2197 // No index, treat each child as a lazy object file.2198 Error e = Error::success();2199 for (const object::Archive::Child &c : file->children(e)) {2200 // Check `seen` but don't insert so a future eager load can still happen.2201 if (seen.contains(c.getChildOffset()))2202 continue;2203 if (!seenLazy.insert(c.getChildOffset()).second)2204 continue;2205 auto file = childToObjectFile(c, /*lazy=*/true);2206 if (!file)2207 error(toString(this) +2208 ": couldn't process child: " + toString(file.takeError()));2209 inputFiles.insert(*file);2210 }2211 if (e)2212 error(toString(this) +2213 ": Archive::children failed: " + toString(std::move(e)));2214 return;2215 }2216 2217 Error err = Error::success();2218 auto child = file->child_begin(err);2219 // Ignore the I/O error here - will be reported later.2220 if (!err) {2221 Expected<MemoryBufferRef> mbOrErr = child->getMemoryBufferRef();2222 if (!mbOrErr) {2223 llvm::consumeError(mbOrErr.takeError());2224 } else {2225 if (identify_magic(mbOrErr->getBuffer()) == file_magic::macho_object) {2226 if (target->wordSize == 8)2227 compatArch = compatWithTargetArch(2228 this, reinterpret_cast<const LP64::mach_header *>(2229 mbOrErr->getBufferStart()));2230 else2231 compatArch = compatWithTargetArch(2232 this, reinterpret_cast<const ILP32::mach_header *>(2233 mbOrErr->getBufferStart()));2234 if (!compatArch)2235 return;2236 }2237 }2238 }2239 2240 for (const object::Archive::Symbol &sym : file->symbols())2241 symtab->addLazyArchive(sym.getName(), this, sym);2242}2243 2244static Expected<InputFile *>2245loadArchiveMember(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,2246 uint64_t offsetInArchive, bool forceHidden, bool compatArch,2247 bool lazy) {2248 if (config->zeroModTime)2249 modTime = 0;2250 2251 switch (identify_magic(mb.getBuffer())) {2252 case file_magic::macho_object:2253 return make<ObjFile>(mb, modTime, archiveName, lazy, forceHidden,2254 compatArch);2255 case file_magic::bitcode:2256 return make<BitcodeFile>(mb, archiveName, offsetInArchive, lazy,2257 forceHidden, compatArch);2258 default:2259 return createStringError(inconvertibleErrorCode(),2260 mb.getBufferIdentifier() +2261 " has unhandled file type");2262 }2263}2264 2265Error ArchiveFile::fetch(const object::Archive::Child &c, StringRef reason) {2266 if (!seen.insert(c.getChildOffset()).second)2267 return Error::success();2268 auto file = childToObjectFile(c, /*lazy=*/false);2269 if (!file)2270 return file.takeError();2271 2272 inputFiles.insert(*file);2273 printArchiveMemberLoad(reason, *file);2274 return Error::success();2275}2276 2277void ArchiveFile::fetch(const object::Archive::Symbol &sym) {2278 object::Archive::Child c =2279 CHECK(sym.getMember(), toString(this) +2280 ": could not get the member defining symbol " +2281 toMachOString(sym));2282 2283 // `sym` is owned by a LazySym, which will be replace<>()d by make<ObjFile>2284 // and become invalid after that call. Copy it to the stack so we can refer2285 // to it later.2286 const object::Archive::Symbol symCopy = sym;2287 2288 // ld64 doesn't demangle sym here even with -demangle.2289 // Match that: intentionally don't call toMachOString().2290 if (Error e = fetch(c, symCopy.getName()))2291 error(toString(this) + ": could not get the member defining symbol " +2292 toMachOString(symCopy) + ": " + toString(std::move(e)));2293}2294 2295Expected<InputFile *>2296ArchiveFile::childToObjectFile(const llvm::object::Archive::Child &c,2297 bool lazy) {2298 Expected<MemoryBufferRef> mb = c.getMemoryBufferRef();2299 if (!mb)2300 return mb.takeError();2301 2302 Expected<TimePoint<std::chrono::seconds>> modTime = c.getLastModified();2303 if (!modTime)2304 return modTime.takeError();2305 2306 return loadArchiveMember(*mb, toTimeT(*modTime), getName(),2307 c.getChildOffset(), forceHidden, compatArch, lazy);2308}2309 2310static macho::Symbol *createBitcodeSymbol(const lto::InputFile::Symbol &objSym,2311 BitcodeFile &file) {2312 StringRef name = saver().save(objSym.getName());2313 2314 if (objSym.isUndefined())2315 return symtab->addUndefined(name, &file, /*isWeakRef=*/objSym.isWeak());2316 2317 // TODO: Write a test demonstrating why computing isPrivateExtern before2318 // LTO compilation is important.2319 bool isPrivateExtern = false;2320 switch (objSym.getVisibility()) {2321 case GlobalValue::HiddenVisibility:2322 isPrivateExtern = true;2323 break;2324 case GlobalValue::ProtectedVisibility:2325 error(name + " has protected visibility, which is not supported by Mach-O");2326 break;2327 case GlobalValue::DefaultVisibility:2328 break;2329 }2330 isPrivateExtern = isPrivateExtern || objSym.canBeOmittedFromSymbolTable() ||2331 file.forceHidden;2332 2333 if (objSym.isCommon())2334 return symtab->addCommon(name, &file, objSym.getCommonSize(),2335 objSym.getCommonAlignment(), isPrivateExtern);2336 2337 return symtab->addDefined(name, &file, /*isec=*/nullptr, /*value=*/0,2338 /*size=*/0, objSym.isWeak(), isPrivateExtern,2339 /*isReferencedDynamically=*/false,2340 /*noDeadStrip=*/false,2341 /*isWeakDefCanBeHidden=*/false);2342}2343 2344BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName,2345 uint64_t offsetInArchive, bool lazy, bool forceHidden,2346 bool compatArch)2347 : InputFile(BitcodeKind, mb, lazy), forceHidden(forceHidden) {2348 this->archiveName = std::string(archiveName);2349 this->compatArch = compatArch;2350 std::string path = mb.getBufferIdentifier().str();2351 if (config->thinLTOIndexOnly)2352 path = replaceThinLTOSuffix(mb.getBufferIdentifier());2353 2354 // If the parent archive already determines that the arch is not compat with2355 // target, then just return.2356 if (!compatArch)2357 return;2358 2359 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique2360 // name. If two members with the same name are provided, this causes a2361 // collision and ThinLTO can't proceed.2362 // So, we append the archive name to disambiguate two members with the same2363 // name from multiple different archives, and offset within the archive to2364 // disambiguate two members of the same name from a single archive.2365 MemoryBufferRef mbref(mb.getBuffer(),2366 saver().save(archiveName.empty()2367 ? path2368 : archiveName + "(" +2369 sys::path::filename(path) + ")" +2370 utostr(offsetInArchive)));2371 obj = check(lto::InputFile::create(mbref));2372 if (lazy)2373 parseLazy();2374 else2375 parse();2376}2377 2378void BitcodeFile::parse() {2379 // Convert LTO Symbols to LLD Symbols in order to perform resolution. The2380 // "winning" symbol will then be marked as Prevailing at LTO compilation2381 // time.2382 symbols.resize(obj->symbols().size());2383 2384 // Process defined symbols first. See the comment at the end of2385 // ObjFile<>::parseSymbols.2386 for (auto it : llvm::enumerate(obj->symbols()))2387 if (!it.value().isUndefined())2388 symbols[it.index()] = createBitcodeSymbol(it.value(), *this);2389 for (auto it : llvm::enumerate(obj->symbols()))2390 if (it.value().isUndefined())2391 symbols[it.index()] = createBitcodeSymbol(it.value(), *this);2392}2393 2394void BitcodeFile::parseLazy() {2395 symbols.resize(obj->symbols().size());2396 for (const auto &[i, objSym] : llvm::enumerate(obj->symbols())) {2397 if (!objSym.isUndefined()) {2398 symbols[i] = symtab->addLazyObject(saver().save(objSym.getName()), *this);2399 if (!lazy)2400 break;2401 }2402 }2403}2404 2405std::string macho::replaceThinLTOSuffix(StringRef path) {2406 auto [suffix, repl] = config->thinLTOObjectSuffixReplace;2407 if (path.consume_back(suffix))2408 return (path + repl).str();2409 return std::string(path);2410}2411 2412void macho::extract(InputFile &file, StringRef reason) {2413 if (!file.lazy)2414 return;2415 file.lazy = false;2416 2417 printArchiveMemberLoad(reason, &file);2418 if (auto *bitcode = dyn_cast<BitcodeFile>(&file)) {2419 bitcode->parse();2420 } else {2421 auto &f = cast<ObjFile>(file);2422 if (target->wordSize == 8)2423 f.parse<LP64>();2424 else2425 f.parse<ILP32>();2426 }2427}2428 2429template void ObjFile::parse<LP64>();2430