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 uint32_t calcNewIndex(const WasmRelocation
&reloc
) const;
120 uint64_t calcNewValue(const WasmRelocation
&reloc
, uint64_t tombstone
,
121 const InputChunk
*chunk
) const;
122 int64_t calcNewAddend(const WasmRelocation
&reloc
) const;
123 Symbol
*getSymbol(const WasmRelocation
&reloc
) const {
124 return symbols
[reloc
.Index
];
127 const WasmSection
*codeSection
= nullptr;
128 const WasmSection
*dataSection
= nullptr;
130 // Maps input type indices to output type indices
131 std::vector
<uint32_t> typeMap
;
132 std::vector
<bool> typeIsUsed
;
133 // Maps function indices to table indices
134 std::vector
<uint32_t> tableEntries
;
135 std::vector
<uint32_t> tableEntriesRel
;
136 std::vector
<bool> keptComdats
;
137 std::vector
<InputChunk
*> segments
;
138 std::vector
<InputFunction
*> functions
;
139 std::vector
<InputGlobal
*> globals
;
140 std::vector
<InputTag
*> tags
;
141 std::vector
<InputTable
*> tables
;
142 std::vector
<InputChunk
*> customSections
;
143 llvm::DenseMap
<uint32_t, InputChunk
*> customSectionsByIndex
;
145 Symbol
*getSymbol(uint32_t index
) const { return symbols
[index
]; }
146 FunctionSymbol
*getFunctionSymbol(uint32_t index
) const;
147 DataSymbol
*getDataSymbol(uint32_t index
) const;
148 GlobalSymbol
*getGlobalSymbol(uint32_t index
) const;
149 SectionSymbol
*getSectionSymbol(uint32_t index
) const;
150 TagSymbol
*getTagSymbol(uint32_t index
) const;
151 TableSymbol
*getTableSymbol(uint32_t index
) const;
154 Symbol
*createDefined(const WasmSymbol
&sym
);
155 Symbol
*createUndefined(const WasmSymbol
&sym
, bool isCalledDirectly
);
157 bool isExcludedByComdat(const InputChunk
*chunk
) const;
158 void addLegacyIndirectFunctionTableIfNeeded(uint32_t tableSymbolCount
);
160 std::unique_ptr
<WasmObjectFile
> wasmObj
;
164 class SharedFile
: public InputFile
{
166 explicit SharedFile(MemoryBufferRef m
) : InputFile(SharedKind
, m
) {}
167 static bool classof(const InputFile
*f
) { return f
->kind() == SharedKind
; }
171 class BitcodeFile
: public InputFile
{
173 BitcodeFile(MemoryBufferRef m
, StringRef archiveName
,
174 uint64_t offsetInArchive
);
175 static bool classof(const InputFile
*f
) { return f
->kind() == BitcodeKind
; }
178 std::unique_ptr
<llvm::lto::InputFile
> obj
;
180 // Set to true once LTO is complete in order prevent further bitcode objects
185 inline bool isBitcode(MemoryBufferRef mb
) {
186 return identify_magic(mb
.getBuffer()) == llvm::file_magic::bitcode
;
189 // Will report a fatal() error if the input buffer is not a valid bitcode
190 // or wasm object file.
191 InputFile
*createObjectFile(MemoryBufferRef mb
, StringRef archiveName
= "",
192 uint64_t offsetInArchive
= 0);
194 // Opens a given file.
195 llvm::Optional
<MemoryBufferRef
> readFile(StringRef path
);
199 std::string
toString(const wasm::InputFile
*file
);