173
|
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
|
207
|
12 #include "MachOStructs.h"
|
|
13 #include "Target.h"
|
|
14
|
173
|
15 #include "lld/Common/LLVM.h"
|
207
|
16 #include "lld/Common/Memory.h"
|
173
|
17 #include "llvm/ADT/DenseSet.h"
|
207
|
18 #include "llvm/ADT/SetVector.h"
|
173
|
19 #include "llvm/BinaryFormat/MachO.h"
|
207
|
20 #include "llvm/DebugInfo/DWARF/DWARFUnit.h"
|
173
|
21 #include "llvm/Object/Archive.h"
|
|
22 #include "llvm/Support/MemoryBuffer.h"
|
207
|
23 #include "llvm/TextAPI/TextAPIReader.h"
|
173
|
24
|
|
25 #include <map>
|
|
26 #include <vector>
|
|
27
|
207
|
28 namespace llvm {
|
|
29 namespace lto {
|
|
30 class InputFile;
|
|
31 } // namespace lto
|
|
32 namespace MachO {
|
|
33 class InterfaceFile;
|
|
34 } // namespace MachO
|
|
35 class TarWriter;
|
|
36 } // namespace llvm
|
|
37
|
173
|
38 namespace lld {
|
|
39 namespace macho {
|
|
40
|
207
|
41 struct PlatformInfo;
|
173
|
42 class InputSection;
|
|
43 class Symbol;
|
|
44 struct Reloc;
|
207
|
45 enum class RefState : uint8_t;
|
|
46
|
|
47 // If --reproduce option is given, all input files are written
|
|
48 // to this tar archive.
|
|
49 extern std::unique_ptr<llvm::TarWriter> tar;
|
173
|
50
|
|
51 // If .subsections_via_symbols is set, each InputSection will be split along
|
207
|
52 // symbol boundaries. The field offset represents the offset of the subsection
|
|
53 // from the start of the original pre-split InputSection.
|
|
54 struct SubsectionEntry {
|
|
55 uint64_t offset;
|
|
56 InputSection *isec;
|
|
57 };
|
|
58 using SubsectionMap = std::vector<SubsectionEntry>;
|
173
|
59
|
|
60 class InputFile {
|
|
61 public:
|
|
62 enum Kind {
|
|
63 ObjKind,
|
207
|
64 OpaqueKind,
|
173
|
65 DylibKind,
|
|
66 ArchiveKind,
|
207
|
67 BitcodeKind,
|
173
|
68 };
|
|
69
|
|
70 virtual ~InputFile() = default;
|
|
71 Kind kind() const { return fileKind; }
|
207
|
72 StringRef getName() const { return name; }
|
173
|
73
|
|
74 MemoryBufferRef mb;
|
207
|
75
|
173
|
76 std::vector<Symbol *> symbols;
|
|
77 std::vector<SubsectionMap> subsections;
|
207
|
78 // Provides an easy way to sort InputFiles deterministically.
|
|
79 const int id;
|
|
80
|
|
81 // If not empty, this stores the name of the archive containing this file.
|
|
82 // We use this string for creating error messages.
|
|
83 std::string archiveName;
|
173
|
84
|
|
85 protected:
|
207
|
86 InputFile(Kind kind, MemoryBufferRef mb)
|
|
87 : mb(mb), id(idCount++), fileKind(kind), name(mb.getBufferIdentifier()) {}
|
173
|
88
|
207
|
89 InputFile(Kind, const llvm::MachO::InterfaceFile &);
|
173
|
90
|
|
91 private:
|
|
92 const Kind fileKind;
|
207
|
93 const StringRef name;
|
|
94
|
|
95 static int idCount;
|
173
|
96 };
|
|
97
|
|
98 // .o file
|
|
99 class ObjFile : public InputFile {
|
|
100 public:
|
207
|
101 ObjFile(MemoryBufferRef mb, uint32_t modTime, StringRef archiveName);
|
173
|
102 static bool classof(const InputFile *f) { return f->kind() == ObjKind; }
|
207
|
103
|
|
104 llvm::DWARFUnit *compileUnit = nullptr;
|
|
105 const uint32_t modTime;
|
|
106 std::vector<InputSection *> debugSections;
|
|
107
|
|
108 private:
|
|
109 template <class LP> void parse();
|
|
110 template <class Section> void parseSections(ArrayRef<Section>);
|
|
111 template <class LP>
|
|
112 void parseSymbols(ArrayRef<typename LP::section> sectionHeaders,
|
|
113 ArrayRef<typename LP::nlist> nList, const char *strtab,
|
|
114 bool subsectionsViaSymbols);
|
|
115 template <class NList>
|
|
116 Symbol *parseNonSectionSymbol(const NList &sym, StringRef name);
|
|
117 template <class Section>
|
|
118 void parseRelocations(ArrayRef<Section> sectionHeaders, const Section &,
|
|
119 SubsectionMap &);
|
|
120 void parseDebugInfo();
|
173
|
121 };
|
|
122
|
207
|
123 // command-line -sectcreate file
|
|
124 class OpaqueFile : public InputFile {
|
|
125 public:
|
|
126 OpaqueFile(MemoryBufferRef mb, StringRef segName, StringRef sectName);
|
|
127 static bool classof(const InputFile *f) { return f->kind() == OpaqueKind; }
|
|
128 };
|
|
129
|
|
130 // .dylib or .tbd file
|
173
|
131 class DylibFile : public InputFile {
|
|
132 public:
|
|
133 // Mach-O dylibs can re-export other dylibs as sub-libraries, meaning that the
|
|
134 // symbols in those sub-libraries will be available under the umbrella
|
|
135 // library's namespace. Those sub-libraries can also have their own
|
|
136 // re-exports. When loading a re-exported dylib, `umbrella` should be set to
|
|
137 // the root dylib to ensure symbols in the child library are correctly bound
|
|
138 // to the root. On the other hand, if a dylib is being directly loaded
|
|
139 // (through an -lfoo flag), then `umbrella` should be a nullptr.
|
207
|
140 explicit DylibFile(MemoryBufferRef mb, DylibFile *umbrella,
|
|
141 bool isBundleLoader = false);
|
|
142 explicit DylibFile(const llvm::MachO::InterfaceFile &interface,
|
|
143 DylibFile *umbrella = nullptr,
|
|
144 bool isBundleLoader = false);
|
|
145
|
|
146 void parseLoadCommands(MemoryBufferRef mb);
|
|
147 void parseReexports(const llvm::MachO::InterfaceFile &interface);
|
|
148
|
173
|
149 static bool classof(const InputFile *f) { return f->kind() == DylibKind; }
|
|
150
|
207
|
151 StringRef installName;
|
|
152 DylibFile *exportingFile = nullptr;
|
|
153 DylibFile *umbrella;
|
|
154 SmallVector<StringRef, 2> rpaths;
|
|
155 uint32_t compatibilityVersion = 0;
|
|
156 uint32_t currentVersion = 0;
|
|
157 int64_t ordinal = 0; // Ordinal numbering starts from 1, so 0 is a sentinel
|
|
158 RefState refState;
|
|
159 bool reexport = false;
|
|
160 bool forceNeeded = false;
|
|
161 bool forceWeakImport = false;
|
|
162 bool deadStrippable = false;
|
|
163 bool explicitlyLinked = false;
|
173
|
164
|
207
|
165 unsigned numReferencedSymbols = 0;
|
|
166
|
|
167 bool isReferenced() const {
|
|
168 return numReferencedSymbols > 0;
|
|
169 }
|
|
170
|
|
171 // An executable can be used as a bundle loader that will load the output
|
|
172 // file being linked, and that contains symbols referenced, but not
|
|
173 // implemented in the bundle. When used like this, it is very similar
|
|
174 // to a Dylib, so we re-used the same class to represent it.
|
|
175 bool isBundleLoader;
|
|
176
|
|
177 private:
|
|
178 bool handleLDSymbol(StringRef originalName);
|
|
179 void handleLDPreviousSymbol(StringRef name, StringRef originalName);
|
|
180 void handleLDInstallNameSymbol(StringRef name, StringRef originalName);
|
173
|
181 };
|
|
182
|
|
183 // .a file
|
|
184 class ArchiveFile : public InputFile {
|
|
185 public:
|
|
186 explicit ArchiveFile(std::unique_ptr<llvm::object::Archive> &&file);
|
|
187 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
|
|
188 void fetch(const llvm::object::Archive::Symbol &sym);
|
|
189
|
|
190 private:
|
|
191 std::unique_ptr<llvm::object::Archive> file;
|
|
192 // Keep track of children fetched from the archive by tracking
|
|
193 // which address offsets have been fetched already.
|
|
194 llvm::DenseSet<uint64_t> seen;
|
|
195 };
|
|
196
|
207
|
197 class BitcodeFile : public InputFile {
|
|
198 public:
|
|
199 explicit BitcodeFile(MemoryBufferRef mb);
|
|
200 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
|
|
201
|
|
202 std::unique_ptr<llvm::lto::InputFile> obj;
|
|
203 };
|
|
204
|
|
205 extern llvm::SetVector<InputFile *> inputFiles;
|
173
|
206
|
|
207 llvm::Optional<MemoryBufferRef> readFile(StringRef path);
|
|
208
|
207
|
209 namespace detail {
|
|
210
|
|
211 template <class CommandType, class... Types>
|
|
212 std::vector<const CommandType *>
|
|
213 findCommands(const void *anyHdr, size_t maxCommands, Types... types) {
|
|
214 std::vector<const CommandType *> cmds;
|
|
215 std::initializer_list<uint32_t> typesList{types...};
|
|
216 const auto *hdr = reinterpret_cast<const llvm::MachO::mach_header *>(anyHdr);
|
|
217 const uint8_t *p =
|
|
218 reinterpret_cast<const uint8_t *>(hdr) + target->headerSize;
|
|
219 for (uint32_t i = 0, n = hdr->ncmds; i < n; ++i) {
|
|
220 auto *cmd = reinterpret_cast<const CommandType *>(p);
|
|
221 if (llvm::is_contained(typesList, cmd->cmd)) {
|
|
222 cmds.push_back(cmd);
|
|
223 if (cmds.size() == maxCommands)
|
|
224 return cmds;
|
|
225 }
|
|
226 p += cmd->cmdsize;
|
|
227 }
|
|
228 return cmds;
|
|
229 }
|
|
230
|
|
231 } // namespace detail
|
|
232
|
|
233 // anyHdr should be a pointer to either mach_header or mach_header_64
|
|
234 template <class CommandType = llvm::MachO::load_command, class... Types>
|
|
235 const CommandType *findCommand(const void *anyHdr, Types... types) {
|
|
236 std::vector<const CommandType *> cmds =
|
|
237 detail::findCommands<CommandType>(anyHdr, 1, types...);
|
|
238 return cmds.size() ? cmds[0] : nullptr;
|
|
239 }
|
|
240
|
|
241 template <class CommandType = llvm::MachO::load_command, class... Types>
|
|
242 std::vector<const CommandType *> findCommands(const void *anyHdr,
|
|
243 Types... types) {
|
|
244 return detail::findCommands<CommandType>(anyHdr, 0, types...);
|
|
245 }
|
|
246
|
173
|
247 } // namespace macho
|
|
248
|
|
249 std::string toString(const macho::InputFile *file);
|
|
250 } // namespace lld
|
|
251
|
|
252 #endif
|