2017-11-18 02:14:09 +08:00
|
|
|
//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_WASM_INPUT_FILES_H
|
|
|
|
#define LLD_WASM_INPUT_FILES_H
|
|
|
|
|
2018-02-21 05:53:18 +08:00
|
|
|
#include "Symbols.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2020-10-27 05:38:09 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2018-05-31 02:07:52 +08:00
|
|
|
#include "llvm/LTO/LTO.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include <vector>
|
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
namespace llvm {
|
|
|
|
class TarWriter;
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
2018-01-13 06:25:17 +08:00
|
|
|
class InputChunk;
|
2018-01-10 07:56:44 +08:00
|
|
|
class InputFunction;
|
2017-11-18 02:14:09 +08:00
|
|
|
class InputSegment;
|
2018-02-23 13:08:53 +08:00
|
|
|
class InputGlobal;
|
2018-12-08 14:17:43 +08:00
|
|
|
class InputEvent;
|
2021-01-05 19:08:58 +08:00
|
|
|
class InputTable;
|
2018-04-11 00:12:49 +08:00
|
|
|
class InputSection;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-05-21 19:52:14 +08:00
|
|
|
// If --reproduce option is given, all input files are written
|
|
|
|
// to this tar archive.
|
|
|
|
extern std::unique_ptr<llvm::TarWriter> tar;
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
class InputFile {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
ObjectKind,
|
2019-03-14 05:29:20 +08:00
|
|
|
SharedKind,
|
2017-11-18 02:14:09 +08:00
|
|
|
ArchiveKind,
|
2018-05-31 02:07:52 +08:00
|
|
|
BitcodeKind,
|
2017-11-18 02:14:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~InputFile() {}
|
|
|
|
|
|
|
|
// Returns the filename.
|
|
|
|
StringRef getName() const { return mb.getBufferIdentifier(); }
|
|
|
|
|
|
|
|
Kind kind() const { return fileKind; }
|
|
|
|
|
|
|
|
// An archive file name if this file is created from an archive.
|
2019-09-19 05:51:03 +08:00
|
|
|
std::string archiveName;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
ArrayRef<Symbol *> getSymbols() const { return symbols; }
|
|
|
|
|
2019-05-24 22:14:25 +08:00
|
|
|
MutableArrayRef<Symbol *> getMutableSymbols() { return symbols; }
|
|
|
|
|
2020-10-01 11:00:04 +08:00
|
|
|
// An InputFile is considered live if any of the symbols defined by it
|
|
|
|
// are live.
|
|
|
|
void markLive() { live = true; }
|
|
|
|
bool isLive() const { return live; }
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
protected:
|
2020-10-01 11:00:04 +08:00
|
|
|
InputFile(Kind k, MemoryBufferRef m)
|
|
|
|
: mb(m), fileKind(k), live(!config->gcSections) {}
|
2020-10-27 05:38:09 +08:00
|
|
|
|
|
|
|
void checkArch(llvm::Triple::ArchType arch) const;
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
MemoryBufferRef mb;
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
// List of all symbols referenced or defined by this file.
|
|
|
|
std::vector<Symbol *> symbols;
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
private:
|
|
|
|
const Kind fileKind;
|
2020-10-01 11:00:04 +08:00
|
|
|
bool live;
|
2017-11-18 02:14:09 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// .a file (ar archive)
|
|
|
|
class ArchiveFile : public InputFile {
|
|
|
|
public:
|
|
|
|
explicit ArchiveFile(MemoryBufferRef m) : InputFile(ArchiveKind, m) {}
|
|
|
|
static bool classof(const InputFile *f) { return f->kind() == ArchiveKind; }
|
|
|
|
|
2018-11-27 09:08:16 +08:00
|
|
|
void addMember(const llvm::object::Archive::Symbol *sym);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-06-06 01:50:45 +08:00
|
|
|
void parse();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
private:
|
2018-11-27 09:08:16 +08:00
|
|
|
std::unique_ptr<llvm::object::Archive> file;
|
2017-11-18 02:14:09 +08:00
|
|
|
llvm::DenseSet<uint64_t> seen;
|
|
|
|
};
|
|
|
|
|
|
|
|
// .o file (wasm object file)
|
|
|
|
class ObjFile : public InputFile {
|
|
|
|
public:
|
2019-04-09 13:41:52 +08:00
|
|
|
explicit ObjFile(MemoryBufferRef m, StringRef archiveName)
|
|
|
|
: InputFile(ObjectKind, m) {
|
2020-01-29 03:23:46 +08:00
|
|
|
this->archiveName = std::string(archiveName);
|
2020-10-01 11:00:04 +08:00
|
|
|
|
|
|
|
// If this isn't part of an archive, it's eagerly linked, so mark it live.
|
|
|
|
if (archiveName.empty())
|
|
|
|
markLive();
|
2019-04-09 13:41:52 +08:00
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
static bool classof(const InputFile *f) { return f->kind() == ObjectKind; }
|
|
|
|
|
2019-06-06 01:50:45 +08:00
|
|
|
void parse(bool ignoreComdats = false);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Returns the underlying wasm file.
|
|
|
|
const WasmObjectFile *getWasmObj() const { return wasmObj.get(); }
|
|
|
|
|
|
|
|
void dumpInfo() const;
|
|
|
|
|
2018-01-11 03:22:42 +08:00
|
|
|
uint32_t calcNewIndex(const WasmRelocation &reloc) const;
|
2020-12-02 08:01:33 +08:00
|
|
|
uint64_t calcNewValue(const WasmRelocation &reloc, uint64_t tombstone) const;
|
2020-06-06 00:03:12 +08:00
|
|
|
uint64_t calcNewAddend(const WasmRelocation &reloc) const;
|
|
|
|
uint64_t calcExpectedValue(const WasmRelocation &reloc) const;
|
2019-04-05 02:40:51 +08:00
|
|
|
Symbol *getSymbol(const WasmRelocation &reloc) const {
|
|
|
|
return symbols[reloc.Index];
|
|
|
|
};
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
const WasmSection *codeSection = nullptr;
|
2018-01-10 09:13:34 +08:00
|
|
|
const WasmSection *dataSection = nullptr;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-03-13 03:54:26 +08:00
|
|
|
// Maps input type indices to output type indices
|
2017-11-18 02:14:09 +08:00
|
|
|
std::vector<uint32_t> typeMap;
|
2018-02-01 07:48:14 +08:00
|
|
|
std::vector<bool> typeIsUsed;
|
2018-03-13 03:54:26 +08:00
|
|
|
// Maps function indices to table indices
|
|
|
|
std::vector<uint32_t> tableEntries;
|
2020-05-29 09:39:27 +08:00
|
|
|
std::vector<uint32_t> tableEntriesRel;
|
2019-05-16 00:03:28 +08:00
|
|
|
std::vector<bool> keptComdats;
|
2017-11-18 02:14:09 +08:00
|
|
|
std::vector<InputSegment *> segments;
|
2018-01-10 07:56:44 +08:00
|
|
|
std::vector<InputFunction *> functions;
|
2018-02-23 13:08:53 +08:00
|
|
|
std::vector<InputGlobal *> globals;
|
2018-12-08 14:17:43 +08:00
|
|
|
std::vector<InputEvent *> events;
|
2021-01-05 19:08:58 +08:00
|
|
|
std::vector<InputTable *> tables;
|
2018-04-11 00:12:49 +08:00
|
|
|
std::vector<InputSection *> customSections;
|
2018-05-05 07:14:42 +08:00
|
|
|
llvm::DenseMap<uint32_t, InputSection *> customSectionsByIndex;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-02-23 13:08:53 +08:00
|
|
|
Symbol *getSymbol(uint32_t index) const { return symbols[index]; }
|
|
|
|
FunctionSymbol *getFunctionSymbol(uint32_t index) const;
|
|
|
|
DataSymbol *getDataSymbol(uint32_t index) const;
|
|
|
|
GlobalSymbol *getGlobalSymbol(uint32_t index) const;
|
2018-05-05 07:14:42 +08:00
|
|
|
SectionSymbol *getSectionSymbol(uint32_t index) const;
|
2018-12-08 14:17:43 +08:00
|
|
|
EventSymbol *getEventSymbol(uint32_t index) const;
|
2021-01-05 19:08:58 +08:00
|
|
|
TableSymbol *getTableSymbol(uint32_t index) const;
|
2018-01-31 09:45:47 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
private:
|
2018-02-28 08:50:54 +08:00
|
|
|
Symbol *createDefined(const WasmSymbol &sym);
|
2019-05-25 06:45:08 +08:00
|
|
|
Symbol *createUndefined(const WasmSymbol &sym, bool isCalledDirectly);
|
2018-02-23 13:08:53 +08:00
|
|
|
|
2018-01-13 06:25:17 +08:00
|
|
|
bool isExcludedByComdat(InputChunk *chunk) const;
|
2021-01-14 17:15:56 +08:00
|
|
|
void synthesizeTableSymbols();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
std::unique_ptr<WasmObjectFile> wasmObj;
|
|
|
|
};
|
|
|
|
|
2019-03-14 05:29:20 +08:00
|
|
|
// .so file.
|
|
|
|
class SharedFile : public InputFile {
|
|
|
|
public:
|
|
|
|
explicit SharedFile(MemoryBufferRef m) : InputFile(SharedKind, m) {}
|
|
|
|
static bool classof(const InputFile *f) { return f->kind() == SharedKind; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// .bc file
|
2018-05-31 02:07:52 +08:00
|
|
|
class BitcodeFile : public InputFile {
|
|
|
|
public:
|
2019-04-09 13:41:52 +08:00
|
|
|
explicit BitcodeFile(MemoryBufferRef m, StringRef archiveName)
|
|
|
|
: InputFile(BitcodeKind, m) {
|
2020-01-29 03:23:46 +08:00
|
|
|
this->archiveName = std::string(archiveName);
|
2020-10-01 11:00:04 +08:00
|
|
|
|
|
|
|
// If this isn't part of an archive, it's eagerly linked, so mark it live.
|
|
|
|
if (archiveName.empty())
|
|
|
|
markLive();
|
2019-04-09 13:41:52 +08:00
|
|
|
}
|
2018-05-31 02:07:52 +08:00
|
|
|
static bool classof(const InputFile *f) { return f->kind() == BitcodeKind; }
|
|
|
|
|
2019-06-06 01:50:45 +08:00
|
|
|
void parse();
|
2018-05-31 02:07:52 +08:00
|
|
|
std::unique_ptr<llvm::lto::InputFile> obj;
|
2019-12-20 09:30:24 +08:00
|
|
|
|
|
|
|
// Set to true once LTO is complete in order prevent further bitcode objects
|
|
|
|
// being added.
|
|
|
|
static bool doneLTO;
|
2018-05-31 02:07:52 +08:00
|
|
|
};
|
|
|
|
|
2019-12-20 09:23:59 +08:00
|
|
|
inline bool isBitcode(MemoryBufferRef mb) {
|
|
|
|
return identify_magic(mb.getBuffer()) == llvm::file_magic::bitcode;
|
|
|
|
}
|
|
|
|
|
2018-07-24 07:51:19 +08:00
|
|
|
// Will report a fatal() error if the input buffer is not a valid bitcode
|
2019-01-17 10:29:41 +08:00
|
|
|
// or wasm object file.
|
2019-04-09 13:41:52 +08:00
|
|
|
InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName = "");
|
2018-07-24 07:51:19 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
// Opens a given file.
|
|
|
|
llvm::Optional<MemoryBufferRef> readFile(StringRef path);
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
|
2017-12-06 00:50:46 +08:00
|
|
|
std::string toString(const wasm::InputFile *file);
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|