Lines Matching +full:abs +full:- +full:flat

1 //===- InputFiles.cpp -----------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
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.
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.
17 // output files. When we merge or garbage-collect sections, we treat each
18 // section as an atomic unit. In Mach-O, that's not the case. Sections can
20 // garbage-collecting. Therefore, Mach-O's subsections are more similar to
21 // ELF/COFF's sections than Mach-O's sections are.
30 // Mach-O. All references within a section need to be explicitly represented as
33 // than they were in object files. To represent that, Mach-O relocations can
36 // Non-scattered relocations refer to an unnamed location if r_extern is not set
40 // and COFF for Mach-O.
42 //===----------------------------------------------------------------------===//
93 if (f->getName().ends_with(".tbd"))
94 return (f->getName() + "(" + dylibFile->installName + ")").str();
96 if (f->archiveName.empty())
97 return std::string(f->getName());
98 return (f->archiveName + "(" + path::filename(f->getName()) + ")").str();
120 const char *hdr = input->mb.getBufferStart();
126 info.target.Platform = static_cast<PlatformType>(cmd->platform);
127 info.target.MinDeployment = decodeVersion(cmd->minos);
134 switch (cmd->cmd) {
148 info.target.MinDeployment = decodeVersion(cmd->version);
162 removeSimulator(config->platform());
175 getPlatformName(config->platform()));
179 if (it->target.MinDeployment > config->platformInfo.target.MinDeployment)
181 it->target.MinDeployment.getAsString() +
183 config->platformInfo.target.MinDeployment.getAsString());
191 std::tie(cpuType, std::ignore) = getCPUTypeFromArchitecture(config->arch());
193 if (hdr->cputype != cpuType) {
195 getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype);
196 auto msg = config->errorForArchMismatch
202 getArchitectureName(config->arch()));
215 // Open a given file path and return it as a memory-mapped file.
220 return entry->second;
229 MemoryBufferRef mbref = mb->getMemBufferRef();
232 // If this is a regular non-fat file, return it.
236 read32be(&hdr->magic) != FAT_MAGIC) {
238 tar->append(relativeToRoot(path), mbref.getBuffer());
253 for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) {
266 if (cpuType != static_cast<uint32_t>(target->cpuType) ||
267 cpuSubtype != target->cpuSubtype) {
277 tar->append(relativeToRoot(path), mbref.getBuffer());
282 auto targetArchName = getArchName(target->cpuType, target->cpuSubtype);
291 // Some sections comprise of fixed-size records, so instead of splitting them at
297 // used by the Mach-O format.
301 return target->wordSize == 8 ? 32 : 20;
303 if (!config->dedupStrings)
307 return target->wordSize == 8 ? 32 : 16;
309 if (config->icfLevel == ICFLevel::none)
313 return target->wordSize;
316 return target->wordSize;
362 auto splitRecords = [&](size_t recordSize) -> void {
380 name == section_names::objcMethname || config->dedupStrings;
384 cast<CStringInputSection>(isec)->splitIntoPieces();
398 if (config->callGraphProfileSort && name == section_names::cgProfile)
414 if (isDebugSection(isec->getFlags()) &&
415 isec->getSegName() == segment_names::dwarf) {
435 uint64_t fullLength = length + (off - frameOff);
437 // We hard-code an alignment of 1 here because we don't actually want our
461 [](uint64_t value, const Section *sec) { return value < sec->addr; }));
462 *offset -= (*it)->addr;
482 *offset -= it->offset;
483 return it->isec;
489 auto it = llvm::lower_bound(isec->symbols, off, [](Defined *d, uint64_t off) {
490 return d->value < off;
493 if (it == isec->symbols.end() || (*it)->value != off) {
494 assert(isec->wasCoalesced);
503 const RelocAttrs &relocAttrs = target->getRelocAttrs(rel.r_type);
517 "be PC-relative"));
520 error(message("not allowed in thread-local section, must be UNSIGNED"));
542 // Paired relocations serve as Mach-O's method for attaching a
560 // to store addends in the instruction-stream bytes that would otherwise
568 target->hasAttr(relInfo.r_type, RelocAttrBits::SUBTRAHEND);
570 if (target->hasAttr(relInfo.r_type, RelocAttrBits::ADDEND)) {
580 int64_t embeddedAddend = target->getEmbeddedAddend(mb, sec.offset, relInfo);
594 sectionHeaders[relInfo.r_symbolnum - 1];
600 // FIXME This logic was written around x86_64 behavior -- ARM64 doesn't
602 // the arch-specific .cpp file.
603 assert(target->hasAttr(r.type, RelocAttrBits::BYTE4));
604 referentOffset = sec.addr + relInfo.r_address + 4 + totalAddend -
607 // The addend for a non-pcrel relocation is its absolute address.
608 referentOffset = totalAddend - referentSecHead.addr;
610 r.referent = findContainingSubsection(*sections[relInfo.r_symbolnum - 1],
616 // Though not required by the Mach-O format, clang and gcc seem to emit
618 // unsorted relocations (in `-r` mode), so we have a fallback for that
621 while (subsecIt != subsections.rend() && subsecIt->offset > r.offset)
624 subsecIt->offset + subsecIt->isec->getSize() <= r.offset) {
630 subsec = subsecIt->isec;
631 r.offset -= subsecIt->offset;
633 subsec->relocs.push_back(r);
639 assert(target->hasAttr(minuendInfo.r_type, RelocAttrBits::UNSIGNED) &&
648 totalAddend - sectionHeaders[minuendInfo.r_symbolnum - 1].addr;
650 *sections[minuendInfo.r_symbolnum - 1], &referentOffset);
653 subsec->relocs.push_back(p);
668 // either reported (for non-weak symbols) or merged
671 // N_PEXT: llvm-mc does not emit these, but `ld -r` (wherein ld64 emits
672 // object files) may produce them. LLD does not yet support -r.
673 // These are translation-unit scoped, identical to the `0` case.
674 // 0: Translation-unit scoped. These are not in the symbol table during
682 // -load_hidden makes us treat global symbols as linkage unit scoped.
691 // * inline function F in a TU built with -fvisibility-inlines-hidden
694 // -fvisibility-inlines-hidden.
697 // -fvisibility-inlines-hidden.
707 // with ld64's semantics, because it means the non-private-extern
711 // that's privateExtern -- neither makes it into the dynamic symbol table,
721 return symtab->addDefined(
722 name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
728 name, isec->getFile(), isec, value, size, sym.n_desc & N_WEAK_DEF,
742 return symtab->addDefined(name, file, nullptr, sym.n_value, /*size=*/0,
765 ? symtab->addUndefined(name, this, sym.n_desc & N_WEAK_REF)
766 : symtab->addCommon(name, this, sym.n_value,
772 // Not much point in making local aliases -- relocs in the current file can
817 Subsections &subsections = sections[sym.n_sect - 1]->subsections;
821 symbolsBySection[sym.n_sect - 1].push_back(i);
830 Subsections &subsections = sections[i]->subsections;
840 if (sections[i]->doneSplitting) {
845 uint64_t symbolOffset = sym.n_value - sectionAddr;
854 createDefined(sym, name, isec, 0, isec->getSize(), forceHidden);
858 sections[i]->doneSplitting = true;
860 auto getSymName = [strtab](const NList& sym) -> StringRef {
885 size_t symbolOffset = sym.n_value - subsecAddr;
888 ? nList[symbolIndices[j + 1]].n_value - sym.n_value
889 : isec->data.size() - symbolOffset;
891 // 1. If the input file does not use subsections-via-symbols.
899 isec->hasAltEntry = symbolOffset != 0;
907 nextIsec->wasCoalesced = false;
908 if (isZeroFill(isec->getFlags())) {
909 // Zero-fill sections have NULL data.data() non-zero data.size()
910 nextIsec->data = {nullptr, isec->data.size() - symbolOffset};
911 isec->data = {nullptr, symbolOffset};
913 nextIsec->data = isec->data.slice(symbolOffset);
914 isec->data = isec->data.slice(0, symbolOffset);
924 nextIsec->align = MinAlign(sectionAlign, sym.n_value);
925 subsections.push_back({sym.n_value - sectionAddr, nextIsec});
949 isec->live = true;
960 cmd->cmdsize - sizeof(linker_option_command)};
961 parseLCLinkerOption(LCLinkerOptions, this, cmd->count, data);
971 this->archiveName = std::string(archiveName);
972 this->compatArch = compatArch;
974 if (target->wordSize == 8)
979 if (target->wordSize == 8)
1011 reinterpret_cast<const SectionHeader *>(c + 1), c->nsects};
1018 ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
1019 c->nsyms);
1020 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
1021 bool subsectionsViaSymbols = hdr->flags & MH_SUBSECTIONS_VIA_SYMBOLS;
1028 if (!sections[i]->subsections.empty())
1036 Section **s = StringSwitch<Section **>(sec->name)
1065 ArrayRef<NList> nList(reinterpret_cast<const NList *>(buf + c->symoff),
1066 c->nsyms);
1067 const char *strtab = reinterpret_cast<const char *>(buf) + c->stroff;
1073 symbols[i] = symtab->addLazyObject(name, *this);
1085 // We do not re-use the context from getDwarf() here as that function
1098 const DWARFContext::compile_unit_range &units = ctx->compile_units();
1102 compileUnit = it != units.end() ? it->get() : nullptr;
1111 return {reinterpret_cast<const data_in_code_entry *>(buf + c->dataoff),
1112 c->datasize / sizeof(data_in_code_entry)};
1119 return {buf + cmd->dataoff, cmd->datasize};
1147 isec->data = isec->data.slice(target->wordSize, 8 + target->wordSize);
1148 uint32_t encoding = read32le(isec->data.data() + sizeof(uint32_t));
1149 // llvm-mc omits CU entries for functions that need DWARF encoding, but
1150 // `ld -r` doesn't. We can ignore them because we will re-synthesize these
1153 target->modeDwarfEncoding)
1157 for (auto it = isec->relocs.begin(); it != isec->relocs.end();) {
1168 if (sym->getFile() != this) {
1172 add += sym->value;
1173 referentIsec = cast<ConcatInputSection>(sym->isec());
1183 if (referentIsec->getSegName() != segment_names::text)
1184 error(isec->getLocation(r.offset) + " references section " +
1185 referentIsec->getName() + " which is not in segment __TEXT");
1187 // However, unwind info operates on a per-symbol basis, so we search for
1194 d->originalUnwindEntry = isec;
1199 // this keeps dead-stripping simple.
1206 // UnwindInfoSection takes care of this by re-duplicating the CUEs so that
1212 it = isec->relocs.erase(it);
1227 return target->wordSize;
1241 // pain. We instead take advantage of our knowledge of how llvm-mc encodes
1291 const auto *personalityReloc = isec->getRelocAt(personalityAddrOff);
1294 cie.personalitySymbol = personalityReloc->referent.get<macho::Symbol *>();
1304 // Concretely, we expect our relocations to write the value of `PC -
1311 // For example, for arm64, llvm-mc emits relocations for the target function
1320 // <target function address - (ltmp + pcrel offset)>
1323 // If any of the FDEs in `multiple FDEs` get dead-stripped, then `FDE start`
1327 // the reloc to be `target function address - (EH_Frame + new pcrel offset)`.
1329 // If `Invert` is set, then we instead expect `target_addr - PC` to be written
1337 assert(target->hasAttr(subtrahend.type, RelocAttrBits::SUBTRAHEND));
1338 assert(target->hasAttr(minuend.type, RelocAttrBits::UNSIGNED));
1339 // Note: pcSym may *not* be exactly at the PC; there's usually a non-zero
1351 if (pcSym->isec() == isec) {
1352 if (pcSym->value - (Invert ? -1 : 1) * minuend.addend != subtrahend.offset)
1358 // `oldSym->value + oldOffset == newSym + newOffset`. However, we don't
1362 pcReloc.referent = isec->symbols[0];
1363 assert(isec->symbols[0]->value == 0);
1364 minuend.addend = pcReloc.offset * (Invert ? 1LL : -1LL);
1377 // general-purpose (and verbose) DWARF unwind info found in __eh_frame.
1382 // While parsing, we also look for what MC calls "abs-ified" relocations -- they
1388 // relocations. This is the case when e.g. it's the output of `ld -r`. We only
1389 // look for the "abs-ified" relocation if an explicit relocation is absent.
1399 if (isec->symbols.size() == 0)
1400 make<Defined>("EH_Frame", isec->getFile(), isec, /*value=*/0,
1401 isec->getSize(), /*isWeakDef=*/false, /*isExternal=*/false,
1405 else if (isec->symbols[0]->value != 0)
1408 EhReader reader(this, isec->data, subsec.offset);
1417 isec->relocs, [=](const Reloc &r) { return r.offset == cieOffOff; });
1419 if (cieOffRelocIt != isec->relocs.end()) {
1423 ->isec();
1427 // embedded in the section data (AKA an "abs-ified" reloc.). Parse that
1433 uint32_t cieOff = isecOff + dataOff - cieMinuend;
1439 ehRelocator.makeNegativePcRel(cieOffOff, cieIsec->symbols[0],
1466 auto funcAddrRelocIt = isec->relocs.end();
1467 auto lsdaAddrRelocIt = isec->relocs.end();
1468 for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {
1469 if (it->offset == funcAddrOff)
1471 else if (lsdaAddrOpt && it->offset == lsdaAddrOff)
1476 if (funcAddrRelocIt != isec->relocs.end()) {
1482 // infrequently (only when handling the output of `ld -r`).
1483 if (funcSym->isec())
1484 funcSym = findSymbolAtOffset(cast<ConcatInputSection>(funcSym->isec()),
1485 funcSym->value);
1488 ehRelocator.makePcRel(funcAddrOff, funcSym, target->p2WordSize);
1491 if (!funcSym || funcSym->getFile() != this || funcSym->unwindEntry()) {
1493 // -dead_strip being enabled.
1494 isec->live = false;
1499 if (lsdaAddrRelocIt != isec->relocs.end()) {
1501 targetSymFromCanonicalSubtractor(isec, lsdaAddrRelocIt)->isec();
1507 ehRelocator.makePcRel(lsdaAddrOff, lsdaIsec, target->p2WordSize);
1511 funcSym->originalUnwindEntry = isec;
1518 // dead-stripping will just work as usual, and S_ATTR_LIVE_SUPPORT will only
1519 // serve to incorrectly prevent us from dead-stripping duplicate FDEs for a
1521 // let dead-stripping proceed correctly.
1526 const char *unitName = compileUnit->getUnitDIE().getShortName();
1535 SmallString<261> dir(compileUnit->getCompilationDir());
1571 // processing a given TBD file, we store that top-level document in
1572 // currentTopLevelTapi. When processing re-exports, we search its children for
1574 // themselves don't point to further documents, i.e. this is a two-level tree.
1576 // Re-exports can either refer to on-disk files, or to documents within .tbd
1581 // 1. Install name basename in -F / -L directories.
1588 for (StringRef dir : config->frameworkSearchPaths) {
1596 stem, config->librarySearchPaths, {".tbd", ".dylib", ".so"}))
1602 for (StringRef root : config->systemLibraryRoots)
1609 // TODO: Handle -dylib_file
1613 if (config->outputType == MH_EXECUTE &&
1615 // ld64 allows overriding this with the undocumented flag -executable_path.
1618 path::append(newPath, path::parent_path(config->outputFile), path);
1621 fs::real_path(umbrella->getName(), newPath);
1626 for (StringRef rpath : umbrella->rpaths) {
1629 fs::real_path(umbrella->getName(), newPath);
1641 make_pointee_range(currentTopLevelTapi->documents())) {
1646 file->parseReexports(child);
1658 // If a re-exported dylib is public (lives in /usr/lib or
1660 // should bind to its symbols directly instead of via the re-exporting umbrella
1663 if (!config->implicitDylibs)
1682 error(toString(this) + ": unable to locate re-export with install name " +
1693 this->umbrella = umbrella;
1700 currentVersion = read32le(&c->dylib.current_version);
1701 compatibilityVersion = read32le(&c->dylib.compatibility_version);
1703 reinterpret_cast<const char *>(cmd) + read32le(&c->dylib.name);
1711 if (config->printEachFile)
1715 deadStrippable = hdr->flags & MH_DEAD_STRIPPABLE_DYLIB;
1720 checkAppExtensionSafety(hdr->flags & MH_APP_EXTENSION_SAFE);
1723 StringRef rpath{reinterpret_cast<const char *>(cmd) + cmd->path};
1731 : this->umbrella;
1745 parseExportedSymbols(dyldInfo->export_off, dyldInfo->export_size);
1747 parseExportedSymbols(exportsTrie->dataoff, exportsTrie->datasize);
1772 if (exportingFile->hiddenSymbols.contains(CachedHashStringRef(entry.name)))
1779 symtab->addDylib(entry.name, exportingFile, isWeakDef, isTlv));
1786 target->headerSize;
1787 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
1789 p += cmd->cmdsize;
1791 if (!(hdr->flags & MH_NO_REEXPORTED_DYLIBS) &&
1792 cmd->cmd == LC_REEXPORT_DYLIB) {
1795 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
1801 // MH_NO_REEXPORTED_DYLIBS loaded for -flat_namespace)?
1802 if (config->namespaceKind == NamespaceKind::flat &&
1803 cmd->cmd == LC_LOAD_DYLIB) {
1806 reinterpret_cast<const char *>(c) + read32le(&c->dylib.name);
1810 "' loaded from '" + toString(this) + "' for -flat_namespace");
1824 // Catalyst outputs can link against implicitly linked macOS-only libraries.
1825 if (config->platform() != PLATFORM_MACCATALYST || explicitlyLinked)
1828 MachO::Target(config->arch(), PLATFORM_MACOS));
1848 if (config->forceExactCpuSubtypeMatch)
1869 this->umbrella = umbrella;
1875 if (config->printEachFile)
1881 config->platformInfo.target) &&
1884 std::string(config->platformInfo.target));
1895 const Twine &name) -> void {
1897 if (exportingFile->hiddenSymbols.contains(CachedHashStringRef(savedName)))
1900 symbols.push_back(symtab->addDylib(savedName, exportingFile,
1908 if (!isArchABICompatible(symbol->getArchitectures(), config->arch()))
1910 if (handleLDSymbol(symbol->getName()))
1913 switch (symbol->getKind()) {
1921 // interface.symbols() order is non-deterministic.
1923 [](auto *l, auto *r) { return l->getName() < r->getName(); });
1927 switch (symbol->getKind()) {
1929 addSymbol(*symbol, symbol->getName());
1932 // XXX ld64 only creates these symbols when -ObjC is passed in. We may
1934 addSymbol(*symbol, objc::symbol_names::klass + symbol->getName());
1935 addSymbol(*symbol, objc::symbol_names::metaclass + symbol->getName());
1938 addSymbol(*symbol, objc::symbol_names::ehtype + symbol->getName());
1941 addSymbol(*symbol, objc::symbol_names::ivar + symbol->getName());
1952 this->umbrella = umbrella;
1961 isTargetPlatformArchCompatible(targets, config->platformInfo.target))
1975 if (dylib->isReferenced())
1985 if (dylib->installName == installName) {
1992 dylib->installName = saver().save(installName);
1993 dylib->currentVersion = currentVersion;
1994 dylib->compatibilityVersion = compatVersion;
2020 // <platformstr> $ <startversion> $ <endversion> $ <symbol-name> $
2039 platform != static_cast<unsigned>(config->platform()))
2054 if (config->platformInfo.target.MinDeployment < start ||
2055 config->platformInfo.target.MinDeployment >= end)
2087 dylib->symbols.push_back(symtab->addDylib(
2093 this->installName = saver().save(installName);
2094 this->compatibilityVersion = newCompatibilityVersion;
2106 else if (version == config->platformInfo.target.MinDeployment)
2107 this->installName = saver().save(installName);
2124 shouldHide = versionTup == config->platformInfo.target.MinDeployment;
2130 exportingFile->hiddenSymbols.insert(CachedHashStringRef(symbolName));
2134 if (config->applicationExtension && !dylibIsAppExtensionSafe)
2135 warn("using '-application_extension' with unsafe dylib: " + toString(this));
2139 : InputFile(ArchiveKind, f->getMemoryBufferRef()), file(std::move(f)),
2143 // Avoid calling getMemoryBufferRef() on zero-symbol archive
2145 if (file->isEmpty() || file->getNumberOfSymbols() == 0)
2149 auto child = file->child_begin(err);
2150 // Ignore the I/O error here - will be reported later.
2152 Expected<MemoryBufferRef> mbOrErr = child->getMemoryBufferRef();
2156 if (identify_magic(mbOrErr->getBuffer()) == file_magic::macho_object) {
2157 if (target->wordSize == 8)
2160 mbOrErr->getBufferStart()));
2164 mbOrErr->getBufferStart()));
2171 for (const object::Archive::Symbol &sym : file->symbols())
2172 symtab->addLazyArchive(sym.getName(), this, sym);
2178 if (config->zeroModTime)
2230 // ld64 doesn't demangle sym here even with -demangle.
2242 return symtab->addUndefined(name, &file, /*isWeakRef=*/objSym.isWeak());
2252 error(name + " has protected visibility, which is not supported by Mach-O");
2261 return symtab->addCommon(name, &file, objSym.getCommonSize(),
2264 return symtab->addDefined(name, &file, /*isec=*/nullptr, /*value=*/0,
2275 this->archiveName = std::string(archiveName);
2276 this->compatArch = compatArch;
2278 if (config->thinLTOIndexOnly)
2309 symbols.resize(obj->symbols().size());
2313 for (auto it : llvm::enumerate(obj->symbols()))
2316 for (auto it : llvm::enumerate(obj->symbols()))
2322 symbols.resize(obj->symbols().size());
2323 for (const auto &[i, objSym] : llvm::enumerate(obj->symbols())) {
2325 symbols[i] = symtab->addLazyObject(saver().save(objSym.getName()), *this);
2333 auto [suffix, repl] = config->thinLTOObjectSuffixReplace;
2346 bitcode->parse();
2349 if (target->wordSize == 8)