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