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_WASM_INPUT_FILES_H
10#define LLD_WASM_INPUT_FILES_H
11
12#include "Symbols.h"
13#include "lld/Common/LLVM.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/DenseSet.h"
16#include "llvm/LTO/LTO.h"
17#include "llvm/Object/Wasm.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/TargetParser/Triple.h"
20#include <optional>
21#include <vector>
22
23namespace llvm {
24class TarWriter;
25}
26
27namespace lld {
28namespace wasm {
29
30class InputChunk;
31class InputFunction;
32class InputSegment;
33class InputGlobal;
34class InputTag;
35class InputTable;
36class InputSection;
37
38// If --reproduce option is given, all input files are written
39// to this tar archive.
40extern std::unique_ptr<llvm::TarWriter> tar;
41
42class InputFile {
43public:
44 enum Kind {
45 ObjectKind,
46 SharedKind,
47 BitcodeKind,
48 StubKind,
49 };
50
51 virtual ~InputFile() {}
52
53 // Returns the filename.
54 StringRef getName() const { return mb.getBufferIdentifier(); }
55
56 Kind kind() const { return fileKind; }
57
58 // An archive file name if this file is created from an archive.
59 std::string archiveName;
60
61 ArrayRef<Symbol *> getSymbols() const { return symbols; }
62
63 MutableArrayRef<Symbol *> getMutableSymbols() { return symbols; }
64
65 // An InputFile is considered live if any of the symbols defined by it
66 // are live.
67 void markLive() { live = true; }
68 bool isLive() const { return live; }
69
70 // True if this is a relocatable object file/bitcode file in an ar archive
71 // or between --start-lib and --end-lib.
72 bool lazy = false;
73
74protected:
75 InputFile(Kind k, MemoryBufferRef m)
76 : mb(m), fileKind(k), live(!config->gcSections) {}
77
78 void checkArch(llvm::Triple::ArchType arch) const;
79
80 MemoryBufferRef mb;
81
82 // List of all symbols referenced or defined by this file.
83 std::vector<Symbol *> symbols;
84
85private:
86 const Kind fileKind;
87 bool live;
88};
89
90// .o file (wasm object file)
91class ObjFile : public InputFile {
92public:
93 ObjFile(MemoryBufferRef m, StringRef archiveName, bool lazy = false);
94 static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
95
96 void parse(bool ignoreComdats = false);
97 void parseLazy();
98
99 // Returns the underlying wasm file.
100 const WasmObjectFile *getWasmObj() const { return wasmObj.get(); }
101
102 uint32_t calcNewIndex(const WasmRelocation &reloc) const;
103 uint64_t calcNewValue(const WasmRelocation &reloc, uint64_t tombstone,
104 const InputChunk *chunk) const;
105 int64_t calcNewAddend(const WasmRelocation &reloc) const;
106 Symbol *getSymbol(const WasmRelocation &reloc) const {
107 return symbols[reloc.Index];
108 };
109
110 const WasmSection *codeSection = nullptr;
111 const WasmSection *dataSection = nullptr;
112
113 // Maps input type indices to output type indices
114 std::vector<uint32_t> typeMap;
115 std::vector<bool> typeIsUsed;
116 // Maps function indices to table indices
117 std::vector<uint32_t> tableEntries;
118 std::vector<uint32_t> tableEntriesRel;
119 std::vector<bool> keptComdats;
120 std::vector<InputChunk *> segments;
121 std::vector<InputFunction *> functions;
122 std::vector<InputGlobal *> globals;
123 std::vector<InputTag *> tags;
124 std::vector<InputTable *> tables;
125 std::vector<InputChunk *> customSections;
126 llvm::DenseMap<uint32_t, InputChunk *> customSectionsByIndex;
127
128 Symbol *getSymbol(uint32_t index) const { return symbols[index]; }
129 FunctionSymbol *getFunctionSymbol(uint32_t index) const;
130 DataSymbol *getDataSymbol(uint32_t index) const;
131 GlobalSymbol *getGlobalSymbol(uint32_t index) const;
132 SectionSymbol *getSectionSymbol(uint32_t index) const;
133 TagSymbol *getTagSymbol(uint32_t index) const;
134 TableSymbol *getTableSymbol(uint32_t index) const;
135
136private:
137 Symbol *createDefined(const WasmSymbol &sym);
138 Symbol *createUndefined(const WasmSymbol &sym, bool isCalledDirectly);
139
140 bool isExcludedByComdat(const InputChunk *chunk) const;
141 void addLegacyIndirectFunctionTableIfNeeded(uint32_t tableSymbolCount);
142
143 std::unique_ptr<WasmObjectFile> wasmObj;
144};
145
146// .so file.
147class SharedFile : public InputFile {
148public:
149 explicit SharedFile(MemoryBufferRef m) : InputFile(SharedKind, m) {}
150 static bool classof(const InputFile *f) { return f->kind() == SharedKind; }
151};
152
153// .bc file
154class BitcodeFile : public InputFile {
155public:
156 BitcodeFile(MemoryBufferRef m, StringRef archiveName,
157 uint64_t offsetInArchive, bool lazy);
158 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
159
160 void parse(StringRef symName);
161 void parseLazy();
162 std::unique_ptr<llvm::lto::InputFile> obj;
163
164 // Set to true once LTO is complete in order prevent further bitcode objects
165 // being added.
166 static bool doneLTO;
167};
168
169// Stub library (See docs/WebAssembly.rst)
170class StubFile : public InputFile {
171public:
172 explicit StubFile(MemoryBufferRef m) : InputFile(StubKind, m) {}
173
174 static bool classof(const InputFile *f) { return f->kind() == StubKind; }
175
176 void parse();
177
178 llvm::DenseMap<StringRef, std::vector<StringRef>> symbolDependencies;
179};
180
181// Will report a fatal() error if the input buffer is not a valid bitcode
182// or wasm object file.
183InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName = "",
184 uint64_t offsetInArchive = 0, bool lazy = false);
185
186// Opens a given file.
187std::optional<MemoryBufferRef> readFile(StringRef path);
188
189} // namespace wasm
190
191std::string toString(const wasm::InputFile *file);
192
193} // namespace lld
194
195#endif
196

source code of lld/wasm/InputFiles.h