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/Archive.h"
18 #include "llvm/Object/Wasm.h"
19 #include "llvm/Support/MemoryBuffer.h"
36 // If --reproduce option is given, all input files are written
37 // to this tar archive.
38 extern std::unique_ptr
<llvm::TarWriter
> tar
;
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
; }
64 InputFile(Kind k
, MemoryBufferRef m
) : mb(m
), fileKind(k
) {}
67 // List of all symbols referenced or defined by this file.
68 std::vector
<Symbol
*> symbols
;
74 // .a file (ar archive)
75 class ArchiveFile
: public InputFile
{
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
);
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
{
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;
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
;
146 class SharedFile
: public InputFile
{
148 explicit SharedFile(MemoryBufferRef m
) : InputFile(SharedKind
, m
) {}
149 static bool classof(const InputFile
*f
) { return f
->kind() == SharedKind
; }
153 class BitcodeFile
: public InputFile
{
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
; }
162 std::unique_ptr
<llvm::lto::InputFile
> obj
;
164 // Set to true once LTO is complete in order prevent further bitcode objects
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
);
182 std::string
toString(const wasm::InputFile
*file
);