brintos

brintos / llvm-project-archived public Read only

0
0
Text · 12.6 KiB · 2d5bceb Raw
380 lines · c
1//===- InputFiles.h ---------------------------------------------*- C++ -*-===//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#ifndef LLD_MACHO_INPUT_FILES_H10#define LLD_MACHO_INPUT_FILES_H11 12#include "MachOStructs.h"13#include "Target.h"14 15#include "lld/Common/DWARF.h"16#include "lld/Common/LLVM.h"17#include "lld/Common/Memory.h"18#include "llvm/ADT/CachedHashString.h"19#include "llvm/ADT/DenseSet.h"20#include "llvm/ADT/SetVector.h"21#include "llvm/BinaryFormat/MachO.h"22#include "llvm/DebugInfo/DWARF/DWARFUnit.h"23#include "llvm/Object/Archive.h"24#include "llvm/Support/MemoryBuffer.h"25#include "llvm/Support/Threading.h"26#include "llvm/TextAPI/TextAPIReader.h"27 28#include <vector>29 30namespace llvm {31namespace lto {32class InputFile;33} // namespace lto34namespace MachO {35class InterfaceFile;36} // namespace MachO37class TarWriter;38} // namespace llvm39 40namespace lld {41namespace macho {42 43struct PlatformInfo;44class ConcatInputSection;45class Symbol;46class Defined;47class AliasSymbol;48struct Reloc;49enum class RefState : uint8_t;50 51// If --reproduce option is given, all input files are written52// to this tar archive.53extern std::unique_ptr<llvm::TarWriter> tar;54 55// If .subsections_via_symbols is set, each InputSection will be split along56// symbol boundaries. The field offset represents the offset of the subsection57// from the start of the original pre-split InputSection.58struct Subsection {59  uint64_t offset = 0;60  InputSection *isec = nullptr;61};62 63using Subsections = std::vector<Subsection>;64class InputFile;65 66class Section {67public:68  InputFile *file;69  StringRef segname;70  StringRef name;71  uint32_t flags;72  uint64_t addr;73  Subsections subsections;74 75  Section(InputFile *file, StringRef segname, StringRef name, uint32_t flags,76          uint64_t addr)77      : file(file), segname(segname), name(name), flags(flags), addr(addr) {}78  // Ensure pointers to Sections are never invalidated.79  Section(const Section &) = delete;80  Section &operator=(const Section &) = delete;81  Section(Section &&) = delete;82  Section &operator=(Section &&) = delete;83 84private:85  // Whether we have already split this section into individual subsections.86  // For sections that cannot be split (e.g. literal sections), this is always87  // false.88  bool doneSplitting = false;89  friend class ObjFile;90};91 92// Represents a call graph profile edge.93struct CallGraphEntry {94  // The index of the caller in the symbol table.95  uint32_t fromIndex;96  // The index of the callee in the symbol table.97  uint32_t toIndex;98  // Number of calls from callee to caller in the profile.99  uint64_t count;100 101  CallGraphEntry(uint32_t fromIndex, uint32_t toIndex, uint64_t count)102      : fromIndex(fromIndex), toIndex(toIndex), count(count) {}103};104 105class InputFile {106public:107  enum Kind {108    ObjKind,109    OpaqueKind,110    DylibKind,111    ArchiveKind,112    BitcodeKind,113  };114 115  virtual ~InputFile() = default;116  Kind kind() const { return fileKind; }117  StringRef getName() const { return name; }118  static void resetIdCount() { idCount = 0; }119 120  MemoryBufferRef mb;121 122  std::vector<Symbol *> symbols;123  std::vector<Section *> sections;124  ArrayRef<uint8_t> objCImageInfo;125 126  // If not empty, this stores the name of the archive containing this file.127  // We use this string for creating error messages.128  std::string archiveName;129 130  // Provides an easy way to sort InputFiles deterministically.131  const int id;132 133  // True if this is a lazy ObjFile or BitcodeFile.134  bool lazy = false;135 136protected:137  InputFile(Kind kind, MemoryBufferRef mb, bool lazy = false)138      : mb(mb), id(idCount++), lazy(lazy), fileKind(kind),139        name(mb.getBufferIdentifier()) {}140 141  InputFile(Kind, const llvm::MachO::InterfaceFile &);142 143  // If true, this input's arch is compatible with target.144  bool compatArch = true;145 146private:147  const Kind fileKind;148  const StringRef name;149 150  static int idCount;151};152 153struct FDE {154  uint32_t funcLength;155  Symbol *personality;156  InputSection *lsda;157};158 159// .o file160class ObjFile final : public InputFile {161public:162  ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName,163          bool lazy = false, bool forceHidden = false, bool compatArch = true,164          bool builtFromBitcode = false);165  ArrayRef<llvm::MachO::data_in_code_entry> getDataInCode() const;166  ArrayRef<uint8_t> getOptimizationHints() const;167  template <class LP> void parse();168  template <class LP>169  void parseLinkerOptions(llvm::SmallVectorImpl<StringRef> &LinkerOptions);170 171  static bool classof(const InputFile *f) { return f->kind() == ObjKind; }172 173  std::string sourceFile() const;174  // Parses line table information for diagnostics. compileUnit should be used175  // for other purposes.176  lld::DWARFCache *getDwarf();177 178  llvm::DWARFUnit *compileUnit = nullptr;179  std::unique_ptr<lld::DWARFCache> dwarfCache;180  Section *addrSigSection = nullptr;181  const uint32_t modTime;182  bool forceHidden;183  bool builtFromBitcode;184  std::vector<ConcatInputSection *> debugSections;185  std::vector<CallGraphEntry> callGraph;186  llvm::DenseMap<ConcatInputSection *, FDE> fdes;187  std::vector<AliasSymbol *> aliases;188 189private:190  llvm::once_flag initDwarf;191  template <class LP> void parseLazy();192  template <class SectionHeader> void parseSections(ArrayRef<SectionHeader>);193  template <class LP>194  void parseSymbols(ArrayRef<typename LP::section> sectionHeaders,195                    ArrayRef<typename LP::nlist> nList, const char *strtab,196                    bool subsectionsViaSymbols);197  template <class NList>198  Symbol *parseNonSectionSymbol(const NList &sym, const char *strtab);199  template <class SectionHeader>200  void parseRelocations(ArrayRef<SectionHeader> sectionHeaders,201                        const SectionHeader &, Section &);202  void parseDebugInfo();203  void splitEhFrames(ArrayRef<uint8_t> dataArr, Section &ehFrameSection);204  void registerCompactUnwind(Section &compactUnwindSection);205  void registerEhFrames(Section &ehFrameSection);206};207 208// command-line -sectcreate file209class OpaqueFile final : public InputFile {210public:211  OpaqueFile(MemoryBufferRef mb, StringRef segName, StringRef sectName);212  static bool classof(const InputFile *f) { return f->kind() == OpaqueKind; }213};214 215// .dylib or .tbd file216class DylibFile final : public InputFile {217public:218  // Mach-O dylibs can re-export other dylibs as sub-libraries, meaning that the219  // symbols in those sub-libraries will be available under the umbrella220  // library's namespace. Those sub-libraries can also have their own221  // re-exports. When loading a re-exported dylib, `umbrella` should be set to222  // the root dylib to ensure symbols in the child library are correctly bound223  // to the root. On the other hand, if a dylib is being directly loaded224  // (through an -lfoo flag), then `umbrella` should be a nullptr.225  explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella,226                     bool isBundleLoader, bool explicitlyLinked);227  explicit DylibFile(const llvm::MachO::InterfaceFile &interface,228                     DylibFile *umbrella, bool isBundleLoader,229                     bool explicitlyLinked);230  explicit DylibFile(DylibFile *umbrella);231 232  void parseLoadCommands(MemoryBufferRef mb);233  void parseReexports(const llvm::MachO::InterfaceFile &interface);234  bool isReferenced() const { return numReferencedSymbols > 0; }235  bool isExplicitlyLinked() const;236  void setExplicitlyLinked() { explicitlyLinked = true; }237 238  static bool classof(const InputFile *f) { return f->kind() == DylibKind; }239 240  StringRef installName;241  DylibFile *exportingFile = nullptr;242  DylibFile *umbrella;243  SmallVector<StringRef, 2> rpaths;244  SmallVector<StringRef> allowableClients;245  uint32_t compatibilityVersion = 0;246  uint32_t currentVersion = 0;247  int64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel248  unsigned numReferencedSymbols = 0;249  RefState refState;250  bool reexport = false;251  bool forceNeeded = false;252  bool forceWeakImport = false;253  bool deadStrippable = false;254 255private:256  bool explicitlyLinked = false; // Access via isExplicitlyLinked().257 258public:259  // An executable can be used as a bundle loader that will load the output260  // file being linked, and that contains symbols referenced, but not261  // implemented in the bundle. When used like this, it is very similar262  // to a dylib, so we've used the same class to represent it.263  bool isBundleLoader;264 265  // Synthetic Dylib objects created by $ld$previous symbols in this dylib.266  // Usually empty. These synthetic dylibs won't have synthetic dylibs267  // themselves.268  SmallVector<DylibFile *, 2> extraDylibs;269 270private:271  DylibFile *getSyntheticDylib(StringRef installName, uint32_t currentVersion,272                               uint32_t compatVersion);273 274  bool handleLDSymbol(StringRef originalName);275  void handleLDPreviousSymbol(StringRef name, StringRef originalName);276  void handleLDInstallNameSymbol(StringRef name, StringRef originalName);277  void handleLDHideSymbol(StringRef name, StringRef originalName);278  void checkAppExtensionSafety(bool dylibIsAppExtensionSafe) const;279  void parseExportedSymbols(uint32_t offset, uint32_t size);280  void loadReexport(StringRef path, DylibFile *umbrella,281                    const llvm::MachO::InterfaceFile *currentTopLevelTapi);282 283  llvm::DenseSet<llvm::CachedHashStringRef> hiddenSymbols;284};285 286// .a file287class ArchiveFile final : public InputFile {288public:289  explicit ArchiveFile(std::unique_ptr<llvm::object::Archive> &&file,290                       bool forceHidden);291  void addLazySymbols();292  void fetch(const llvm::object::Archive::Symbol &);293  // LLD normally doesn't use Error for error-handling, but the underlying294  // Archive library does, so this is the cleanest way to wrap it.295  Error fetch(const llvm::object::Archive::Child &, StringRef reason);296  const llvm::object::Archive &getArchive() const { return *file; };297  static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }298 299private:300  Expected<InputFile *> childToObjectFile(const llvm::object::Archive::Child &c,301                                          bool lazy);302  std::unique_ptr<llvm::object::Archive> file;303  // Keep track of children fetched from the archive by tracking304  // which address offsets have been fetched already.305  llvm::DenseSet<uint64_t> seen;306  llvm::DenseSet<uint64_t> seenLazy;307  // Load all symbols with hidden visibility (-load_hidden).308  bool forceHidden;309};310 311class BitcodeFile final : public InputFile {312public:313  explicit BitcodeFile(MemoryBufferRef mb, StringRef archiveName,314                       uint64_t offsetInArchive, bool lazy = false,315                       bool forceHidden = false, bool compatArch = true);316  static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }317  void parse();318 319  std::unique_ptr<llvm::lto::InputFile> obj;320  bool forceHidden;321 322private:323  void parseLazy();324};325 326extern llvm::SetVector<InputFile *> inputFiles;327extern llvm::DenseMap<llvm::CachedHashStringRef, MemoryBufferRef> cachedReads;328extern llvm::SmallVector<StringRef> unprocessedLCLinkerOptions;329 330std::optional<MemoryBufferRef> readFile(StringRef path);331 332void extract(InputFile &file, StringRef reason);333 334namespace detail {335 336template <class CommandType, class... Types>337std::vector<const CommandType *>338findCommands(const void *anyHdr, size_t maxCommands, Types... types) {339  std::vector<const CommandType *> cmds;340  std::initializer_list<uint32_t> typesList{types...};341  const auto *hdr = reinterpret_cast<const llvm::MachO::mach_header *>(anyHdr);342  const uint8_t *p =343      reinterpret_cast<const uint8_t *>(hdr) + target->headerSize;344  for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {345    auto *cmd = reinterpret_cast<const CommandType *>(p);346    if (llvm::is_contained(typesList, cmd->cmd)) {347      cmds.push_back(cmd);348      if (cmds.size() == maxCommands)349        return cmds;350    }351    p += cmd->cmdsize;352  }353  return cmds;354}355 356} // namespace detail357 358// anyHdr should be a pointer to either mach_header or mach_header_64359template <class CommandType = llvm::MachO::load_command, class... Types>360const CommandType *findCommand(const void *anyHdr, Types... types) {361  std::vector<const CommandType *> cmds =362      detail::findCommands<CommandType>(anyHdr, 1, types...);363  return cmds.size() ? cmds[0] : nullptr;364}365 366template <class CommandType = llvm::MachO::load_command, class... Types>367std::vector<const CommandType *> findCommands(const void *anyHdr,368                                              Types... types) {369  return detail::findCommands<CommandType>(anyHdr, 0, types...);370}371 372std::string replaceThinLTOSuffix(StringRef path);373} // namespace macho374 375std::string toString(const macho::InputFile *file);376std::string toString(const macho::Section &);377} // namespace lld378 379#endif380