1 //===- InputFiles.h ---------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_WASM_INPUT_FILES_H
10 #define LLD_WASM_INPUT_FILES_H
13 #include "lld/Common/LLVM.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/DenseSet.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/LTO/LTO.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/Wasm.h"
20 #include "llvm/Support/MemoryBuffer.h"
38 // If --reproduce option is given, all input files are written
39 // to this tar archive.
40 extern std::unique_ptr
<llvm::TarWriter
> tar
;
51 virtual ~InputFile() {}
53 // Returns the filename.
54 StringRef
getName() const { return mb
.getBufferIdentifier(); }
56 Kind
kind() const { return fileKind
; }
58 // An archive file name if this file is created from an archive.
59 std::string archiveName
;
61 ArrayRef
<Symbol
*> getSymbols() const { return symbols
; }
63 MutableArrayRef
<Symbol
*> getMutableSymbols() { return symbols
; }
65 // An InputFile is considered live if any of the symbols defined by it
67 void markLive() { live
= true; }
68 bool isLive() const { return live
; }
71 InputFile(Kind k
, MemoryBufferRef m
)
72 : mb(m
), fileKind(k
), live(!config
->gcSections
) {}
74 void checkArch(llvm::Triple::ArchType arch
) const;
78 // List of all symbols referenced or defined by this file.
79 std::vector
<Symbol
*> symbols
;
86 // .a file (ar archive)
87 class ArchiveFile
: public InputFile
{
89 explicit ArchiveFile(MemoryBufferRef m
) : InputFile(ArchiveKind
, m
) {}
90 static bool classof(const InputFile
*f
) { return f
->kind() == ArchiveKind
; }
92 void addMember(const llvm::object::Archive::Symbol
*sym
);
97 std::unique_ptr
<llvm::object::Archive
> file
;
98 llvm::DenseSet
<uint64_t> seen
;
101 // .o file (wasm object file)
102 class ObjFile
: public InputFile
{
104 explicit ObjFile(MemoryBufferRef m
, StringRef archiveName
)
105 : InputFile(ObjectKind
, m
) {
106 this->archiveName
= std::string(archiveName
);
108 // If this isn't part of an archive, it's eagerly linked, so mark it live.
109 if (archiveName
.empty())
112 static bool classof(const InputFile
*f
) { return f
->kind() == ObjectKind
; }
114 void parse(bool ignoreComdats
= false);
116 // Returns the underlying wasm file.
117 const WasmObjectFile
*getWasmObj() const { return wasmObj
.get(); }
119 void dumpInfo() const;
121 uint32_t calcNewIndex(const WasmRelocation
&reloc
) const;
122 uint64_t calcNewValue(const WasmRelocation
&reloc
, uint64_t tombstone
,
123 const InputChunk
*chunk
) const;
124 uint64_t calcNewAddend(const WasmRelocation
&reloc
) const;
125 Symbol
*getSymbol(const WasmRelocation
&reloc
) const {
126 return symbols
[reloc
.Index
];
129 const WasmSection
*codeSection
= nullptr;
130 const WasmSection
*dataSection
= nullptr;
132 // Maps input type indices to output type indices
133 std::vector
<uint32_t> typeMap
;
134 std::vector
<bool> typeIsUsed
;
135 // Maps function indices to table indices
136 std::vector
<uint32_t> tableEntries
;
137 std::vector
<uint32_t> tableEntriesRel
;
138 std::vector
<bool> keptComdats
;
139 std::vector
<InputChunk
*> segments
;
140 std::vector
<InputFunction
*> functions
;
141 std::vector
<InputGlobal
*> globals
;
142 std::vector
<InputTag
*> tags
;
143 std::vector
<InputTable
*> tables
;
144 std::vector
<InputChunk
*> customSections
;
145 llvm::DenseMap
<uint32_t, InputChunk
*> customSectionsByIndex
;
147 Symbol
*getSymbol(uint32_t index
) const { return symbols
[index
]; }
148 FunctionSymbol
*getFunctionSymbol(uint32_t index
) const;
149 DataSymbol
*getDataSymbol(uint32_t index
) const;
150 GlobalSymbol
*getGlobalSymbol(uint32_t index
) const;
151 SectionSymbol
*getSectionSymbol(uint32_t index
) const;
152 TagSymbol
*getTagSymbol(uint32_t index
) const;
153 TableSymbol
*getTableSymbol(uint32_t index
) const;
156 Symbol
*createDefined(const WasmSymbol
&sym
);
157 Symbol
*createUndefined(const WasmSymbol
&sym
, bool isCalledDirectly
);
159 bool isExcludedByComdat(InputChunk
*chunk
) const;
160 void addLegacyIndirectFunctionTableIfNeeded(uint32_t tableSymbolCount
);
162 std::unique_ptr
<WasmObjectFile
> wasmObj
;
166 class SharedFile
: public InputFile
{
168 explicit SharedFile(MemoryBufferRef m
) : InputFile(SharedKind
, m
) {}
169 static bool classof(const InputFile
*f
) { return f
->kind() == SharedKind
; }
173 class BitcodeFile
: public InputFile
{
175 explicit BitcodeFile(MemoryBufferRef m
, StringRef archiveName
)
176 : InputFile(BitcodeKind
, m
) {
177 this->archiveName
= std::string(archiveName
);
179 // If this isn't part of an archive, it's eagerly linked, so mark it live.
180 if (archiveName
.empty())
183 static bool classof(const InputFile
*f
) { return f
->kind() == BitcodeKind
; }
186 std::unique_ptr
<llvm::lto::InputFile
> obj
;
188 // Set to true once LTO is complete in order prevent further bitcode objects
193 inline bool isBitcode(MemoryBufferRef mb
) {
194 return identify_magic(mb
.getBuffer()) == llvm::file_magic::bitcode
;
197 // Will report a fatal() error if the input buffer is not a valid bitcode
198 // or wasm object file.
199 InputFile
*createObjectFile(MemoryBufferRef mb
, StringRef archiveName
= "");
201 // Opens a given file.
202 llvm::Optional
<MemoryBufferRef
> readFile(StringRef path
);
206 std::string
toString(const wasm::InputFile
*file
);