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
|
|
|
|
|
|
|
|
#include "lld/Common/LLVM.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/Object/Archive.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
#include "Symbols.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "WriterUtils.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2017-12-01 10:11:29 +08:00
|
|
|
using llvm::object::Archive;
|
2017-11-18 02:14:09 +08:00
|
|
|
using llvm::object::WasmObjectFile;
|
|
|
|
using llvm::object::WasmSection;
|
|
|
|
using llvm::object::WasmSymbol;
|
|
|
|
using llvm::wasm::WasmImport;
|
2018-01-10 07:56:44 +08:00
|
|
|
using llvm::wasm::WasmSignature;
|
2018-01-11 03:22:42 +08:00
|
|
|
using llvm::wasm::WasmRelocation;
|
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;
|
|
|
|
|
|
|
|
class InputFile {
|
|
|
|
public:
|
|
|
|
enum Kind {
|
|
|
|
ObjectKind,
|
|
|
|
ArchiveKind,
|
|
|
|
};
|
|
|
|
|
|
|
|
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.
|
|
|
|
StringRef ParentName;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {}
|
|
|
|
MemoryBufferRef MB;
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
|
|
void addMember(const Archive::Symbol *Sym);
|
|
|
|
|
|
|
|
void parse() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<Archive> File;
|
|
|
|
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;
|
|
|
|
|
|
|
|
uint32_t relocateFunctionIndex(uint32_t Original) 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;
|
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
|
|
|
|
|
|
|
std::vector<uint32_t> TypeMap;
|
|
|
|
std::vector<InputSegment *> Segments;
|
2018-01-10 07:56:44 +08:00
|
|
|
std::vector<InputFunction *> Functions;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
ArrayRef<Symbol *> getSymbols() const { return Symbols; }
|
|
|
|
|
|
|
|
Symbol *getFunctionSymbol(uint32_t Index) const {
|
|
|
|
return FunctionSymbols[Index];
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-31 09:45:47 +08:00
|
|
|
Symbol *getGlobalSymbol(uint32_t Index) const { return GlobalSymbols[Index]; }
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
private:
|
2018-01-23 09:25:56 +08:00
|
|
|
uint32_t relocateVirtualAddress(uint32_t Index) const;
|
2018-01-11 03:22:42 +08:00
|
|
|
uint32_t relocateTypeIndex(uint32_t Original) const;
|
|
|
|
uint32_t relocateGlobalIndex(uint32_t Original) const;
|
|
|
|
uint32_t relocateTableIndex(uint32_t Original) const;
|
|
|
|
|
2018-01-10 08:52:20 +08:00
|
|
|
Symbol *createDefined(const WasmSymbol &Sym, Symbol::Kind Kind,
|
2018-01-29 03:57:01 +08:00
|
|
|
InputChunk *Chunk = nullptr,
|
2018-01-10 08:52:20 +08:00
|
|
|
uint32_t Address = UINT32_MAX);
|
|
|
|
Symbol *createUndefined(const WasmSymbol &Sym, Symbol::Kind Kind,
|
2018-01-10 07:56:44 +08:00
|
|
|
const WasmSignature *Signature = nullptr);
|
2017-11-18 02:14:09 +08:00
|
|
|
void initializeSymbols();
|
2018-01-10 07:56:44 +08:00
|
|
|
InputSegment *getSegment(const WasmSymbol &WasmSym) const;
|
|
|
|
const WasmSignature *getFunctionSig(const WasmSymbol &Sym) const;
|
2018-01-10 08:52:20 +08:00
|
|
|
uint32_t getGlobalValue(const WasmSymbol &Sym) const;
|
2018-01-10 07:56:44 +08:00
|
|
|
InputFunction *getFunction(const WasmSymbol &Sym) const;
|
2018-01-13 06:25:17 +08:00
|
|
|
bool isExcludedByComdat(InputChunk *Chunk) const;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// List of all symbols referenced or defined by this file.
|
|
|
|
std::vector<Symbol *> Symbols;
|
|
|
|
|
|
|
|
// List of all function symbols indexed by the function index space
|
2017-12-12 06:00:56 +08:00
|
|
|
std::vector<Symbol *> FunctionSymbols;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// List of all global symbols indexed by the global index space
|
2017-12-12 06:00:56 +08:00
|
|
|
std::vector<Symbol *> GlobalSymbols;
|
|
|
|
|
2018-01-10 07:56:44 +08:00
|
|
|
uint32_t NumGlobalImports = 0;
|
|
|
|
uint32_t NumFunctionImports = 0;
|
2017-11-18 02:14:09 +08:00
|
|
|
std::unique_ptr<WasmObjectFile> WasmObj;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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
|