2017-11-18 02:14:09 +08:00
|
|
|
//===- InputFiles.h ---------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
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>
|
|
|
|
|
|
|
|
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-04-11 00:12:49 +08:00
|
|
|
class InputSection;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
class InputFile {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
ObjectKind,
|
|
|
|
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(); }
|
|
|
|
|
|
|
|
// Reads a file (the constructor doesn't do that).
|
|
|
|
virtual void parse() = 0;
|
|
|
|
|
|
|
|
Kind kind() const { return FileKind; }
|
|
|
|
|
|
|
|
// An archive file name if this file is created from an archive.
|
2018-07-19 05:46:09 +08:00
|
|
|
StringRef ArchiveName;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
ArrayRef<Symbol *> getSymbols() const { return Symbols; }
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
protected:
|
|
|
|
InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// .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
|
|
|
|
|
|
|
void parse() override;
|
|
|
|
|
|
|
|
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:
|
|
|
|
explicit ObjFile(MemoryBufferRef M) : InputFile(ObjectKind, M) {}
|
|
|
|
static bool classof(const InputFile *F) { return F->kind() == ObjectKind; }
|
|
|
|
|
|
|
|
void parse() override;
|
|
|
|
|
|
|
|
// 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;
|
2018-01-23 09:25:56 +08:00
|
|
|
uint32_t calcNewValue(const WasmRelocation &Reloc) const;
|
2018-05-05 07:14:42 +08:00
|
|
|
uint32_t calcNewAddend(const WasmRelocation &Reloc) const;
|
2018-03-13 03:54:26 +08:00
|
|
|
uint32_t calcExpectedValue(const WasmRelocation &Reloc) const;
|
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;
|
2018-03-14 23:45:11 +08:00
|
|
|
std::vector<bool> UsedComdats;
|
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-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;
|
2017-11-18 02:14:09 +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-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);
|
2018-02-23 13:08:53 +08:00
|
|
|
Symbol *createUndefined(const WasmSymbol &Sym);
|
|
|
|
|
2018-01-13 06:25:17 +08:00
|
|
|
bool isExcludedByComdat(InputChunk *Chunk) const;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
std::unique_ptr<WasmObjectFile> WasmObj;
|
|
|
|
};
|
|
|
|
|
2018-05-31 02:07:52 +08:00
|
|
|
class BitcodeFile : public InputFile {
|
|
|
|
public:
|
|
|
|
explicit BitcodeFile(MemoryBufferRef M) : InputFile(BitcodeKind, M) {}
|
|
|
|
static bool classof(const InputFile *F) { return F->kind() == BitcodeKind; }
|
|
|
|
|
|
|
|
void parse() override;
|
|
|
|
std::unique_ptr<llvm::lto::InputFile> Obj;
|
|
|
|
};
|
|
|
|
|
2018-07-24 07:51:19 +08:00
|
|
|
// Will report a fatal() error if the input buffer is not a valid bitcode
|
|
|
|
// or was object file.
|
|
|
|
InputFile *createObjectFile(MemoryBufferRef MB);
|
|
|
|
|
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
|