[ELF][ARM] Increase default max-page-size from 4096 to 6536
[llvm-project.git] / lld / wasm / InputFiles.h
blobbf4a4ec99abf55d4e84961c58b38957e5e6f6326
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 //===----------------------------------------------------------------------===//
9 #ifndef LLD_WASM_INPUT_FILES_H
10 #define LLD_WASM_INPUT_FILES_H
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/Archive.h"
18 #include "llvm/Object/Wasm.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include <vector>
22 namespace llvm {
23 class TarWriter;
26 namespace lld {
27 namespace wasm {
29 class InputChunk;
30 class InputFunction;
31 class InputSegment;
32 class InputGlobal;
33 class InputEvent;
34 class InputSection;
36 // If --reproduce option is given, all input files are written
37 // to this tar archive.
38 extern std::unique_ptr<llvm::TarWriter> tar;
40 class InputFile {
41 public:
42 enum Kind {
43 ObjectKind,
44 SharedKind,
45 ArchiveKind,
46 BitcodeKind,
49 virtual ~InputFile() {}
51 // Returns the filename.
52 StringRef getName() const { return mb.getBufferIdentifier(); }
54 Kind kind() const { return fileKind; }
56 // An archive file name if this file is created from an archive.
57 std::string archiveName;
59 ArrayRef<Symbol *> getSymbols() const { return symbols; }
61 MutableArrayRef<Symbol *> getMutableSymbols() { return symbols; }
63 protected:
64 InputFile(Kind k, MemoryBufferRef m) : mb(m), fileKind(k) {}
65 MemoryBufferRef mb;
67 // List of all symbols referenced or defined by this file.
68 std::vector<Symbol *> symbols;
70 private:
71 const Kind fileKind;
74 // .a file (ar archive)
75 class ArchiveFile : public InputFile {
76 public:
77 explicit ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
78 static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
80 void addMember(const llvm::object::Archive::Symbol *sym);
82 void parse();
84 private:
85 std::unique_ptr<llvm::object::Archive> file;
86 llvm::DenseSet<uint64_t> seen;
89 // .o file (wasm object file)
90 class ObjFile : public InputFile {
91 public:
92 explicit ObjFile(MemoryBufferRef m, StringRef archiveName)
93 : InputFile(ObjectKind, m) {
94 this->archiveName = std::string(archiveName);
96 static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
98 void parse(bool ignoreComdats = false);
100 // Returns the underlying wasm file.
101 const WasmObjectFile *getWasmObj() const { return wasmObj.get(); }
103 void dumpInfo() const;
105 uint32_t calcNewIndex(const WasmRelocation &reloc) const;
106 uint32_t calcNewValue(const WasmRelocation &reloc) const;
107 uint32_t calcNewAddend(const WasmRelocation &reloc) const;
108 uint32_t calcExpectedValue(const WasmRelocation &reloc) const;
109 Symbol *getSymbol(const WasmRelocation &reloc) const {
110 return symbols[reloc.Index];
113 const WasmSection *codeSection = nullptr;
114 const WasmSection *dataSection = nullptr;
116 // Maps input type indices to output type indices
117 std::vector<uint32_t> typeMap;
118 std::vector<bool> typeIsUsed;
119 // Maps function indices to table indices
120 std::vector<uint32_t> tableEntries;
121 std::vector<bool> keptComdats;
122 std::vector<InputSegment *> segments;
123 std::vector<InputFunction *> functions;
124 std::vector<InputGlobal *> globals;
125 std::vector<InputEvent *> events;
126 std::vector<InputSection *> customSections;
127 llvm::DenseMap<uint32_t, InputSection *> customSectionsByIndex;
129 Symbol *getSymbol(uint32_t index) const { return symbols[index]; }
130 FunctionSymbol *getFunctionSymbol(uint32_t index) const;
131 DataSymbol *getDataSymbol(uint32_t index) const;
132 GlobalSymbol *getGlobalSymbol(uint32_t index) const;
133 SectionSymbol *getSectionSymbol(uint32_t index) const;
134 EventSymbol *getEventSymbol(uint32_t index) const;
136 private:
137 Symbol *createDefined(const WasmSymbol &sym);
138 Symbol *createUndefined(const WasmSymbol &sym, bool isCalledDirectly);
140 bool isExcludedByComdat(InputChunk *chunk) const;
142 std::unique_ptr<WasmObjectFile> wasmObj;
145 // .so file.
146 class SharedFile : public InputFile {
147 public:
148 explicit SharedFile(MemoryBufferRef m) : InputFile(SharedKind, m) {}
149 static bool classof(const InputFile *f) { return f->kind() == SharedKind; }
152 // .bc file
153 class BitcodeFile : public InputFile {
154 public:
155 explicit BitcodeFile(MemoryBufferRef m, StringRef archiveName)
156 : InputFile(BitcodeKind, m) {
157 this->archiveName = std::string(archiveName);
159 static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
161 void parse();
162 std::unique_ptr<llvm::lto::InputFile> obj;
164 // Set to true once LTO is complete in order prevent further bitcode objects
165 // being added.
166 static bool doneLTO;
169 inline bool isBitcode(MemoryBufferRef mb) {
170 return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode;
173 // Will report a fatal() error if the input buffer is not a valid bitcode
174 // or wasm object file.
175 InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName = "");
177 // Opens a given file.
178 llvm::Optional<MemoryBufferRef> readFile(StringRef path);
180 } // namespace wasm
182 std::string toString(const wasm::InputFile *file);
184 } // namespace lld
186 #endif