xref: /freebsd/contrib/llvm-project/lld/MachO/InputFiles.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLD_MACHO_INPUT_FILES_H
10 #define LLD_MACHO_INPUT_FILES_H
11 
12 #include "MachOStructs.h"
13 
14 #include "lld/Common/LLVM.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/BinaryFormat/MachO.h"
17 #include "llvm/Object/Archive.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/TextAPI/MachO/InterfaceFile.h"
20 #include "llvm/TextAPI/MachO/TextAPIReader.h"
21 
22 #include <map>
23 #include <vector>
24 
25 namespace lld {
26 namespace macho {
27 
28 class InputSection;
29 class Symbol;
30 struct Reloc;
31 
32 // If .subsections_via_symbols is set, each InputSection will be split along
33 // symbol boundaries. The keys of a SubsectionMap represent the offsets of
34 // each subsection from the start of the original pre-split InputSection.
35 using SubsectionMap = std::map<uint32_t, InputSection *>;
36 
37 class InputFile {
38 public:
39   enum Kind {
40     ObjKind,
41     DylibKind,
42     ArchiveKind,
43   };
44 
45   virtual ~InputFile() = default;
46   Kind kind() const { return fileKind; }
47   StringRef getName() const { return mb.getBufferIdentifier(); }
48 
49   MemoryBufferRef mb;
50   std::vector<Symbol *> symbols;
51   ArrayRef<llvm::MachO::section_64> sectionHeaders;
52   std::vector<SubsectionMap> subsections;
53 
54 protected:
55   InputFile(Kind kind, MemoryBufferRef mb) : mb(mb), fileKind(kind) {}
56 
57   void parseSections(ArrayRef<llvm::MachO::section_64>);
58 
59   void parseSymbols(ArrayRef<lld::structs::nlist_64> nList, const char *strtab,
60                     bool subsectionsViaSymbols);
61 
62   void parseRelocations(const llvm::MachO::section_64 &, SubsectionMap &);
63 
64 private:
65   const Kind fileKind;
66 };
67 
68 // .o file
69 class ObjFile : public InputFile {
70 public:
71   explicit ObjFile(MemoryBufferRef mb);
72   static bool classof(const InputFile *f) { return f->kind() == ObjKind; }
73 };
74 
75 // .dylib file
76 class DylibFile : public InputFile {
77 public:
78   explicit DylibFile(std::shared_ptr<llvm::MachO::InterfaceFile> interface,
79                      DylibFile *umbrella = nullptr);
80 
81   // Mach-O dylibs can re-export other dylibs as sub-libraries, meaning that the
82   // symbols in those sub-libraries will be available under the umbrella
83   // library's namespace. Those sub-libraries can also have their own
84   // re-exports. When loading a re-exported dylib, `umbrella` should be set to
85   // the root dylib to ensure symbols in the child library are correctly bound
86   // to the root. On the other hand, if a dylib is being directly loaded
87   // (through an -lfoo flag), then `umbrella` should be a nullptr.
88   explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella = nullptr);
89 
90   static bool classof(const InputFile *f) { return f->kind() == DylibKind; }
91 
92   StringRef dylibName;
93   uint64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel
94   bool reexport = false;
95   std::vector<DylibFile *> reexported;
96 };
97 
98 // .a file
99 class ArchiveFile : public InputFile {
100 public:
101   explicit ArchiveFile(std::unique_ptr<llvm::object::Archive> &&file);
102   static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
103   void fetch(const llvm::object::Archive::Symbol &sym);
104 
105 private:
106   std::unique_ptr<llvm::object::Archive> file;
107   // Keep track of children fetched from the archive by tracking
108   // which address offsets have been fetched already.
109   llvm::DenseSet<uint64_t> seen;
110 };
111 
112 extern std::vector<InputFile *> inputFiles;
113 
114 llvm::Optional<MemoryBufferRef> readFile(StringRef path);
115 
116 } // namespace macho
117 
118 std::string toString(const macho::InputFile *file);
119 } // namespace lld
120 
121 #endif
122