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/LTO/LTO.h"
17 #include "llvm/Object/Wasm.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/TargetParser/Triple.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
; }
70 // True if this is a relocatable object file/bitcode file in an ar archive
71 // or between --start-lib and --end-lib.
75 InputFile(Kind k
, MemoryBufferRef m
)
76 : mb(m
), fileKind(k
), live(!config
->gcSections
) {}
78 void checkArch(llvm::Triple::ArchType arch
) const;
82 // List of all symbols referenced or defined by this file.
83 std::vector
<Symbol
*> symbols
;
90 class WasmFileBase
: public InputFile
{
92 explicit WasmFileBase(Kind k
, MemoryBufferRef m
);
94 // Returns the underlying wasm file.
95 const WasmObjectFile
*getWasmObj() const { return wasmObj
.get(); }
98 std::unique_ptr
<WasmObjectFile
> wasmObj
;
101 // .o file (wasm object file)
102 class ObjFile
: public WasmFileBase
{
104 ObjFile(MemoryBufferRef m
, StringRef archiveName
, bool lazy
= false);
105 static bool classof(const InputFile
*f
) { return f
->kind() == ObjectKind
; }
107 void parse(bool ignoreComdats
= false);
110 uint32_t calcNewIndex(const WasmRelocation
&reloc
) const;
111 uint64_t calcNewValue(const WasmRelocation
&reloc
, uint64_t tombstone
,
112 const InputChunk
*chunk
) const;
113 int64_t calcNewAddend(const WasmRelocation
&reloc
) const;
114 Symbol
*getSymbol(const WasmRelocation
&reloc
) const {
115 return symbols
[reloc
.Index
];
118 const WasmSection
*codeSection
= nullptr;
119 const WasmSection
*dataSection
= nullptr;
121 // Maps input type indices to output type indices
122 std::vector
<uint32_t> typeMap
;
123 std::vector
<bool> typeIsUsed
;
124 // Maps function indices to table indices
125 std::vector
<uint32_t> tableEntries
;
126 std::vector
<uint32_t> tableEntriesRel
;
127 std::vector
<bool> keptComdats
;
128 std::vector
<InputChunk
*> segments
;
129 std::vector
<InputFunction
*> functions
;
130 std::vector
<InputGlobal
*> globals
;
131 std::vector
<InputTag
*> tags
;
132 std::vector
<InputTable
*> tables
;
133 std::vector
<InputChunk
*> customSections
;
134 llvm::DenseMap
<uint32_t, InputChunk
*> customSectionsByIndex
;
136 Symbol
*getSymbol(uint32_t index
) const { return symbols
[index
]; }
137 FunctionSymbol
*getFunctionSymbol(uint32_t index
) const;
138 DataSymbol
*getDataSymbol(uint32_t index
) const;
139 GlobalSymbol
*getGlobalSymbol(uint32_t index
) const;
140 SectionSymbol
*getSectionSymbol(uint32_t index
) const;
141 TagSymbol
*getTagSymbol(uint32_t index
) const;
142 TableSymbol
*getTableSymbol(uint32_t index
) const;
145 Symbol
*createDefined(const WasmSymbol
&sym
);
146 Symbol
*createUndefined(const WasmSymbol
&sym
, bool isCalledDirectly
);
148 bool isExcludedByComdat(const InputChunk
*chunk
) const;
149 void addLegacyIndirectFunctionTableIfNeeded(uint32_t tableSymbolCount
);
153 class SharedFile
: public WasmFileBase
{
155 explicit SharedFile(MemoryBufferRef m
) : WasmFileBase(SharedKind
, m
) {}
159 static bool classof(const InputFile
*f
) { return f
->kind() == SharedKind
; }
163 class BitcodeFile
: public InputFile
{
165 BitcodeFile(MemoryBufferRef m
, StringRef archiveName
,
166 uint64_t offsetInArchive
, bool lazy
);
167 static bool classof(const InputFile
*f
) { return f
->kind() == BitcodeKind
; }
169 void parse(StringRef symName
);
171 std::unique_ptr
<llvm::lto::InputFile
> obj
;
173 // Set to true once LTO is complete in order prevent further bitcode objects
178 // Stub library (See docs/WebAssembly.rst)
179 class StubFile
: public InputFile
{
181 explicit StubFile(MemoryBufferRef m
) : InputFile(StubKind
, m
) {}
183 static bool classof(const InputFile
*f
) { return f
->kind() == StubKind
; }
187 llvm::DenseMap
<StringRef
, std::vector
<StringRef
>> symbolDependencies
;
190 // Will report a fatal() error if the input buffer is not a valid bitcode
191 // or wasm object file.
192 InputFile
*createObjectFile(MemoryBufferRef mb
, StringRef archiveName
= "",
193 uint64_t offsetInArchive
= 0, bool lazy
= false);
195 // Opens a given file.
196 std::optional
<MemoryBufferRef
> readFile(StringRef path
);
198 std::string
replaceThinLTOSuffix(StringRef path
);
202 std::string
toString(const wasm::InputFile
*file
);