2017-11-18 02:14:09 +08:00
|
|
|
//===- Writer.cpp ---------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Writer.h"
|
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "OutputSections.h"
|
|
|
|
#include "OutputSegment.h"
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "WriterUtils.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-29 04:39:17 +08:00
|
|
|
#include "lld/Common/Memory.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/Threads.h"
|
2018-02-21 05:53:18 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/Support/LEB128.h"
|
|
|
|
|
|
|
|
#include <cstdarg>
|
2018-01-13 06:25:17 +08:00
|
|
|
#include <map>
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::wasm;
|
|
|
|
|
|
|
|
static constexpr int kStackAlignment = 16;
|
2018-01-25 05:37:30 +08:00
|
|
|
static constexpr int kInitialTableOffset = 1;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Traits for using WasmSignature in a DenseMap.
|
|
|
|
struct WasmSignatureDenseMapInfo {
|
|
|
|
static WasmSignature getEmptyKey() {
|
|
|
|
WasmSignature Sig;
|
|
|
|
Sig.ReturnType = 1;
|
|
|
|
return Sig;
|
|
|
|
}
|
|
|
|
static WasmSignature getTombstoneKey() {
|
|
|
|
WasmSignature Sig;
|
|
|
|
Sig.ReturnType = 2;
|
|
|
|
return Sig;
|
|
|
|
}
|
|
|
|
static unsigned getHashValue(const WasmSignature &Sig) {
|
|
|
|
uintptr_t Value = 0;
|
|
|
|
Value += DenseMapInfo<int32_t>::getHashValue(Sig.ReturnType);
|
|
|
|
for (int32_t Param : Sig.ParamTypes)
|
|
|
|
Value += DenseMapInfo<int32_t>::getHashValue(Param);
|
|
|
|
return Value;
|
|
|
|
}
|
|
|
|
static bool isEqual(const WasmSignature &LHS, const WasmSignature &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-19 07:40:49 +08:00
|
|
|
// A Wasm export to be written into the export section.
|
|
|
|
struct WasmExportEntry {
|
2018-01-19 11:31:07 +08:00
|
|
|
const Symbol *Sym;
|
2018-01-19 07:40:49 +08:00
|
|
|
StringRef FieldName; // may not match the Symbol name
|
|
|
|
};
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
// The writer writes a SymbolTable result to a file.
|
|
|
|
class Writer {
|
|
|
|
public:
|
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void openFile();
|
|
|
|
|
2018-01-11 03:18:22 +08:00
|
|
|
uint32_t lookupType(const WasmSignature &Sig);
|
|
|
|
uint32_t registerType(const WasmSignature &Sig);
|
2018-01-13 02:35:13 +08:00
|
|
|
void createCtorFunction();
|
|
|
|
void calculateInitFunctions();
|
2018-01-10 07:56:44 +08:00
|
|
|
void assignIndexes();
|
2017-11-18 02:14:09 +08:00
|
|
|
void calculateImports();
|
2018-01-19 07:40:49 +08:00
|
|
|
void calculateExports();
|
2017-11-18 02:14:09 +08:00
|
|
|
void calculateTypes();
|
|
|
|
void createOutputSegments();
|
|
|
|
void layoutMemory();
|
|
|
|
void createHeader();
|
|
|
|
void createSections();
|
|
|
|
SyntheticSection *createSyntheticSection(uint32_t Type,
|
2018-01-11 03:18:22 +08:00
|
|
|
StringRef Name = "");
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Builtin sections
|
|
|
|
void createTypeSection();
|
|
|
|
void createFunctionSection();
|
|
|
|
void createTableSection();
|
|
|
|
void createGlobalSection();
|
|
|
|
void createExportSection();
|
|
|
|
void createImportSection();
|
|
|
|
void createMemorySection();
|
|
|
|
void createElemSection();
|
|
|
|
void createStartSection();
|
|
|
|
void createCodeSection();
|
|
|
|
void createDataSection();
|
|
|
|
|
|
|
|
// Custom sections
|
|
|
|
void createRelocSections();
|
|
|
|
void createLinkingSection();
|
|
|
|
void createNameSection();
|
|
|
|
|
|
|
|
void writeHeader();
|
|
|
|
void writeSections();
|
|
|
|
|
|
|
|
uint64_t FileSize = 0;
|
|
|
|
uint32_t DataSize = 0;
|
|
|
|
uint32_t NumMemoryPages = 0;
|
|
|
|
|
|
|
|
std::vector<const WasmSignature *> Types;
|
|
|
|
DenseMap<WasmSignature, int32_t, WasmSignatureDenseMapInfo> TypeIndices;
|
2018-02-15 02:27:59 +08:00
|
|
|
std::vector<const FunctionSymbol *> ImportedFunctions;
|
2018-02-21 07:38:27 +08:00
|
|
|
std::vector<const DataSymbol *> ImportedGlobals;
|
2018-01-19 07:40:49 +08:00
|
|
|
std::vector<WasmExportEntry> ExportedSymbols;
|
2018-02-21 07:38:27 +08:00
|
|
|
std::vector<const DefinedData *> DefinedDataSymbols;
|
2018-02-22 02:29:23 +08:00
|
|
|
std::vector<InputFunction *> InputFunctions;
|
2018-02-15 02:27:59 +08:00
|
|
|
std::vector<const FunctionSymbol *> IndirectFunctions;
|
2018-01-13 02:35:13 +08:00
|
|
|
std::vector<WasmInitFunc> InitFunctions;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
// Elements that are used to construct the final output
|
|
|
|
std::string Header;
|
|
|
|
std::vector<OutputSection *> OutputSections;
|
|
|
|
|
|
|
|
std::unique_ptr<FileOutputBuffer> Buffer;
|
2018-01-13 02:35:13 +08:00
|
|
|
std::string CtorFunctionBody;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
std::vector<OutputSegment *> Segments;
|
|
|
|
llvm::SmallDenseMap<StringRef, OutputSegment *> SegmentMap;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
static void debugPrint(const char *fmt, ...) {
|
|
|
|
if (!errorHandler().Verbose)
|
|
|
|
return;
|
|
|
|
fprintf(stderr, "lld: ");
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createImportSection() {
|
2018-01-10 07:56:44 +08:00
|
|
|
uint32_t NumImports = ImportedFunctions.size() + ImportedGlobals.size();
|
2017-11-18 02:14:09 +08:00
|
|
|
if (Config->ImportMemory)
|
|
|
|
++NumImports;
|
|
|
|
|
|
|
|
if (NumImports == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_IMPORT);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
|
|
|
writeUleb128(OS, NumImports, "import count");
|
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
for (const FunctionSymbol *Sym : ImportedFunctions) {
|
2017-11-18 02:14:09 +08:00
|
|
|
WasmImport Import;
|
|
|
|
Import.Module = "env";
|
|
|
|
Import.Field = Sym->getName();
|
|
|
|
Import.Kind = WASM_EXTERNAL_FUNCTION;
|
2018-02-17 07:50:23 +08:00
|
|
|
Import.SigIndex = lookupType(*Sym->getFunctionType());
|
2017-11-18 02:14:09 +08:00
|
|
|
writeImport(OS, Import);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Config->ImportMemory) {
|
|
|
|
WasmImport Import;
|
|
|
|
Import.Module = "env";
|
|
|
|
Import.Field = "memory";
|
|
|
|
Import.Kind = WASM_EXTERNAL_MEMORY;
|
|
|
|
Import.Memory.Flags = 0;
|
|
|
|
Import.Memory.Initial = NumMemoryPages;
|
|
|
|
writeImport(OS, Import);
|
|
|
|
}
|
|
|
|
|
2018-01-10 07:56:44 +08:00
|
|
|
for (const Symbol *Sym : ImportedGlobals) {
|
2017-11-18 02:14:09 +08:00
|
|
|
WasmImport Import;
|
|
|
|
Import.Module = "env";
|
|
|
|
Import.Field = Sym->getName();
|
|
|
|
Import.Kind = WASM_EXTERNAL_GLOBAL;
|
|
|
|
Import.Global.Mutable = false;
|
2017-12-20 03:56:27 +08:00
|
|
|
Import.Global.Type = WASM_TYPE_I32;
|
2017-11-18 02:14:09 +08:00
|
|
|
writeImport(OS, Import);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createTypeSection() {
|
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
writeUleb128(OS, Types.size(), "type count");
|
2017-12-20 03:56:27 +08:00
|
|
|
for (const WasmSignature *Sig : Types)
|
2017-11-18 02:14:09 +08:00
|
|
|
writeSig(OS, *Sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createFunctionSection() {
|
2018-02-22 02:29:23 +08:00
|
|
|
if (InputFunctions.empty())
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_FUNCTION);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
2018-02-22 02:29:23 +08:00
|
|
|
writeUleb128(OS, InputFunctions.size(), "function count");
|
|
|
|
for (const InputFunction *Func : InputFunctions)
|
2018-01-11 03:18:22 +08:00
|
|
|
writeUleb128(OS, lookupType(Func->Signature), "sig index");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createMemorySection() {
|
|
|
|
if (Config->ImportMemory)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_MEMORY);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
|
|
|
writeUleb128(OS, 1, "memory count");
|
|
|
|
writeUleb128(OS, 0, "memory limits flags");
|
|
|
|
writeUleb128(OS, NumMemoryPages, "initial pages");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createGlobalSection() {
|
2018-02-21 07:38:27 +08:00
|
|
|
if (DefinedDataSymbols.empty())
|
2017-12-07 09:51:24 +08:00
|
|
|
return;
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_GLOBAL);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
writeUleb128(OS, DefinedDataSymbols.size(), "global count");
|
|
|
|
for (const DefinedData *Sym : DefinedDataSymbols) {
|
2017-12-06 03:05:45 +08:00
|
|
|
WasmGlobal Global;
|
2018-02-01 03:54:34 +08:00
|
|
|
Global.Type.Type = WASM_TYPE_I32;
|
2018-02-03 06:59:56 +08:00
|
|
|
Global.Type.Mutable = Sym == WasmSym::StackPointer;
|
2017-12-06 03:05:45 +08:00
|
|
|
Global.InitExpr.Opcode = WASM_OPCODE_I32_CONST;
|
|
|
|
Global.InitExpr.Value.Int32 = Sym->getVirtualAddress();
|
2017-11-18 02:14:09 +08:00
|
|
|
writeGlobal(OS, Global);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createTableSection() {
|
2017-12-12 06:00:56 +08:00
|
|
|
// Always output a table section, even if there are no indirect calls.
|
|
|
|
// There are two reasons for this:
|
|
|
|
// 1. For executables it is useful to have an empty table slot at 0
|
|
|
|
// which can be filled with a null function call handler.
|
|
|
|
// 2. If we don't do this, any program that contains a call_indirect but
|
|
|
|
// no address-taken function will fail at validation time since it is
|
|
|
|
// a validation error to include a call_indirect instruction if there
|
|
|
|
// is not table.
|
2018-01-25 05:37:30 +08:00
|
|
|
uint32_t TableSize = kInitialTableOffset + IndirectFunctions.size();
|
2017-12-12 06:00:56 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_TABLE);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
|
|
|
writeUleb128(OS, 1, "table count");
|
|
|
|
writeSleb128(OS, WASM_TYPE_ANYFUNC, "table type");
|
|
|
|
writeUleb128(OS, WASM_LIMITS_FLAG_HAS_MAX, "table flags");
|
2017-12-12 06:00:56 +08:00
|
|
|
writeUleb128(OS, TableSize, "table initial size");
|
|
|
|
writeUleb128(OS, TableSize, "table max size");
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createExportSection() {
|
|
|
|
bool ExportMemory = !Config->Relocatable && !Config->ImportMemory;
|
|
|
|
|
2018-01-19 07:40:49 +08:00
|
|
|
uint32_t NumExports = (ExportMemory ? 1 : 0) + ExportedSymbols.size();
|
2017-11-18 02:14:09 +08:00
|
|
|
if (!NumExports)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_EXPORT);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
|
|
|
writeUleb128(OS, NumExports, "export count");
|
|
|
|
|
|
|
|
if (ExportMemory) {
|
|
|
|
WasmExport MemoryExport;
|
|
|
|
MemoryExport.Name = "memory";
|
|
|
|
MemoryExport.Kind = WASM_EXTERNAL_MEMORY;
|
|
|
|
MemoryExport.Index = 0;
|
|
|
|
writeExport(OS, MemoryExport);
|
|
|
|
}
|
|
|
|
|
2018-01-19 07:40:49 +08:00
|
|
|
for (const WasmExportEntry &E : ExportedSymbols) {
|
2018-01-19 11:31:07 +08:00
|
|
|
DEBUG(dbgs() << "Export: " << E.Sym->getName() << "\n");
|
2017-12-07 09:51:24 +08:00
|
|
|
WasmExport Export;
|
2018-01-19 07:40:49 +08:00
|
|
|
Export.Name = E.FieldName;
|
2018-01-19 11:31:07 +08:00
|
|
|
Export.Index = E.Sym->getOutputIndex();
|
2018-02-17 08:44:21 +08:00
|
|
|
if (isa<FunctionSymbol>(E.Sym))
|
2017-12-07 09:51:24 +08:00
|
|
|
Export.Kind = WASM_EXTERNAL_FUNCTION;
|
|
|
|
else
|
|
|
|
Export.Kind = WASM_EXTERNAL_GLOBAL;
|
|
|
|
writeExport(OS, Export);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createStartSection() {}
|
|
|
|
|
|
|
|
void Writer::createElemSection() {
|
2017-12-12 06:00:56 +08:00
|
|
|
if (IndirectFunctions.empty())
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_ELEM);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
|
|
|
writeUleb128(OS, 1, "segment count");
|
|
|
|
writeUleb128(OS, 0, "table index");
|
|
|
|
WasmInitExpr InitExpr;
|
|
|
|
InitExpr.Opcode = WASM_OPCODE_I32_CONST;
|
2018-01-25 05:37:30 +08:00
|
|
|
InitExpr.Value.Int32 = kInitialTableOffset;
|
2017-11-18 02:14:09 +08:00
|
|
|
writeInitExpr(OS, InitExpr);
|
2017-12-12 06:00:56 +08:00
|
|
|
writeUleb128(OS, IndirectFunctions.size(), "elem count");
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-25 05:37:30 +08:00
|
|
|
uint32_t TableIndex = kInitialTableOffset;
|
2018-02-15 02:27:59 +08:00
|
|
|
for (const FunctionSymbol *Sym : IndirectFunctions) {
|
2017-12-12 06:00:56 +08:00
|
|
|
assert(Sym->getTableIndex() == TableIndex);
|
|
|
|
writeUleb128(OS, Sym->getOutputIndex(), "function index");
|
|
|
|
++TableIndex;
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createCodeSection() {
|
2018-02-22 02:29:23 +08:00
|
|
|
if (InputFunctions.empty())
|
2017-11-18 02:14:09 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
log("createCodeSection");
|
|
|
|
|
2018-02-22 02:29:23 +08:00
|
|
|
auto Section = make<CodeSection>(InputFunctions);
|
2017-11-18 02:14:09 +08:00
|
|
|
OutputSections.push_back(Section);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createDataSection() {
|
|
|
|
if (!Segments.size())
|
|
|
|
return;
|
|
|
|
|
|
|
|
log("createDataSection");
|
|
|
|
auto Section = make<DataSection>(Segments);
|
|
|
|
OutputSections.push_back(Section);
|
|
|
|
}
|
|
|
|
|
2017-12-20 03:56:27 +08:00
|
|
|
// Create relocations sections in the final output.
|
2017-11-18 02:14:09 +08:00
|
|
|
// These are only created when relocatable output is requested.
|
|
|
|
void Writer::createRelocSections() {
|
|
|
|
log("createRelocSections");
|
|
|
|
// Don't use iterator here since we are adding to OutputSection
|
|
|
|
size_t OrigSize = OutputSections.size();
|
|
|
|
for (size_t i = 0; i < OrigSize; i++) {
|
|
|
|
OutputSection *S = OutputSections[i];
|
|
|
|
const char *name;
|
|
|
|
uint32_t Count = S->numRelocations();
|
|
|
|
if (!Count)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (S->Type == WASM_SEC_DATA)
|
|
|
|
name = "reloc.DATA";
|
|
|
|
else if (S->Type == WASM_SEC_CODE)
|
|
|
|
name = "reloc.CODE";
|
|
|
|
else
|
2017-12-20 03:56:27 +08:00
|
|
|
llvm_unreachable("relocations only supported for code and data");
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name);
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
writeUleb128(OS, S->Type, "reloc section");
|
|
|
|
writeUleb128(OS, Count, "reloc count");
|
|
|
|
S->writeRelocations(OS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 08:53:21 +08:00
|
|
|
// Create the custom "linking" section containing linker metadata.
|
2017-11-18 02:14:09 +08:00
|
|
|
// This is only created when relocatable output is requested.
|
|
|
|
void Writer::createLinkingSection() {
|
|
|
|
SyntheticSection *Section =
|
|
|
|
createSyntheticSection(WASM_SEC_CUSTOM, "linking");
|
|
|
|
raw_ostream &OS = Section->getStream();
|
|
|
|
|
|
|
|
SubSection DataSizeSubSection(WASM_DATA_SIZE);
|
|
|
|
writeUleb128(DataSizeSubSection.getStream(), DataSize, "data size");
|
|
|
|
DataSizeSubSection.finalizeContents();
|
|
|
|
DataSizeSubSection.writeToStream(OS);
|
|
|
|
|
2017-12-20 01:09:45 +08:00
|
|
|
if (!Config->Relocatable)
|
|
|
|
return;
|
|
|
|
|
2018-01-19 07:40:49 +08:00
|
|
|
std::vector<std::pair<StringRef, uint32_t>> SymbolInfo;
|
2018-01-20 05:49:41 +08:00
|
|
|
auto addSymInfo = [&](const Symbol *Sym, StringRef ExternalName) {
|
2018-01-19 07:40:49 +08:00
|
|
|
uint32_t Flags =
|
2018-01-20 05:49:41 +08:00
|
|
|
(Sym->isLocal() ? WASM_SYMBOL_BINDING_LOCAL :
|
|
|
|
Sym->isWeak() ? WASM_SYMBOL_BINDING_WEAK : 0) |
|
|
|
|
(Sym->isHidden() ? WASM_SYMBOL_VISIBILITY_HIDDEN : 0);
|
2018-01-19 07:40:49 +08:00
|
|
|
if (Flags)
|
2018-01-20 05:49:41 +08:00
|
|
|
SymbolInfo.emplace_back(ExternalName, Flags);
|
|
|
|
};
|
|
|
|
// (Imports can't have internal linkage, their names don't need to be budged.)
|
|
|
|
for (const Symbol *Sym : ImportedFunctions)
|
|
|
|
addSymInfo(Sym, Sym->getName());
|
|
|
|
for (const Symbol *Sym : ImportedGlobals)
|
|
|
|
addSymInfo(Sym, Sym->getName());
|
|
|
|
for (const WasmExportEntry &E : ExportedSymbols)
|
|
|
|
addSymInfo(E.Sym, E.FieldName);
|
2018-01-19 07:40:49 +08:00
|
|
|
if (!SymbolInfo.empty()) {
|
|
|
|
SubSection SubSection(WASM_SYMBOL_INFO);
|
|
|
|
writeUleb128(SubSection.getStream(), SymbolInfo.size(), "num sym info");
|
|
|
|
for (auto Pair: SymbolInfo) {
|
|
|
|
writeStr(SubSection.getStream(), Pair.first, "sym name");
|
|
|
|
writeUleb128(SubSection.getStream(), Pair.second, "sym flags");
|
|
|
|
}
|
|
|
|
SubSection.finalizeContents();
|
|
|
|
SubSection.writeToStream(OS);
|
|
|
|
}
|
|
|
|
|
2017-12-20 01:09:45 +08:00
|
|
|
if (Segments.size()) {
|
2017-11-18 02:14:09 +08:00
|
|
|
SubSection SubSection(WASM_SEGMENT_INFO);
|
|
|
|
writeUleb128(SubSection.getStream(), Segments.size(), "num data segments");
|
|
|
|
for (const OutputSegment *S : Segments) {
|
|
|
|
writeStr(SubSection.getStream(), S->Name, "segment name");
|
|
|
|
writeUleb128(SubSection.getStream(), S->Alignment, "alignment");
|
|
|
|
writeUleb128(SubSection.getStream(), 0, "flags");
|
|
|
|
}
|
|
|
|
SubSection.finalizeContents();
|
|
|
|
SubSection.writeToStream(OS);
|
|
|
|
}
|
2017-12-20 01:09:45 +08:00
|
|
|
|
|
|
|
if (!InitFunctions.empty()) {
|
|
|
|
SubSection SubSection(WASM_INIT_FUNCS);
|
|
|
|
writeUleb128(SubSection.getStream(), InitFunctions.size(),
|
2018-01-18 02:50:30 +08:00
|
|
|
"num init functions");
|
2017-12-20 01:09:45 +08:00
|
|
|
for (const WasmInitFunc &F : InitFunctions) {
|
|
|
|
writeUleb128(SubSection.getStream(), F.Priority, "priority");
|
|
|
|
writeUleb128(SubSection.getStream(), F.FunctionIndex, "function index");
|
|
|
|
}
|
|
|
|
SubSection.finalizeContents();
|
|
|
|
SubSection.writeToStream(OS);
|
|
|
|
}
|
2018-01-13 06:25:17 +08:00
|
|
|
|
|
|
|
struct ComdatEntry { unsigned Kind; uint32_t Index; };
|
|
|
|
std::map<StringRef,std::vector<ComdatEntry>> Comdats;
|
|
|
|
|
2018-02-22 02:29:23 +08:00
|
|
|
for (const InputFunction *F : InputFunctions) {
|
2018-01-13 06:25:17 +08:00
|
|
|
StringRef Comdat = F->getComdat();
|
|
|
|
if (!Comdat.empty())
|
|
|
|
Comdats[Comdat].emplace_back(
|
|
|
|
ComdatEntry{WASM_COMDAT_FUNCTION, F->getOutputIndex()});
|
|
|
|
}
|
|
|
|
for (uint32_t I = 0; I < Segments.size(); ++I) {
|
2018-01-13 23:57:48 +08:00
|
|
|
const auto &InputSegments = Segments[I]->InputSegments;
|
|
|
|
if (InputSegments.empty())
|
|
|
|
continue;
|
|
|
|
StringRef Comdat = InputSegments[0]->getComdat();
|
2018-01-13 23:59:53 +08:00
|
|
|
#ifndef NDEBUG
|
2018-01-13 23:57:48 +08:00
|
|
|
for (const InputSegment *IS : InputSegments)
|
|
|
|
assert(IS->getComdat() == Comdat);
|
2018-01-13 23:59:53 +08:00
|
|
|
#endif
|
2018-01-13 06:25:17 +08:00
|
|
|
if (!Comdat.empty())
|
|
|
|
Comdats[Comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, I});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Comdats.empty()) {
|
|
|
|
SubSection SubSection(WASM_COMDAT_INFO);
|
|
|
|
writeUleb128(SubSection.getStream(), Comdats.size(), "num comdats");
|
|
|
|
for (const auto &C : Comdats) {
|
|
|
|
writeStr(SubSection.getStream(), C.first, "comdat name");
|
|
|
|
writeUleb128(SubSection.getStream(), 0, "comdat flags"); // flags for future use
|
|
|
|
writeUleb128(SubSection.getStream(), C.second.size(), "num entries");
|
|
|
|
for (const ComdatEntry &Entry : C.second) {
|
|
|
|
writeUleb128(SubSection.getStream(), Entry.Kind, "entry kind");
|
|
|
|
writeUleb128(SubSection.getStream(), Entry.Index, "entry index");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SubSection.finalizeContents();
|
|
|
|
SubSection.writeToStream(OS);
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the custom "name" section containing debug symbol names.
|
|
|
|
void Writer::createNameSection() {
|
2018-01-18 04:19:04 +08:00
|
|
|
unsigned NumNames = ImportedFunctions.size();
|
2018-02-22 02:29:23 +08:00
|
|
|
for (const InputFunction *F : InputFunctions)
|
2018-01-18 04:19:04 +08:00
|
|
|
if (!F->getName().empty())
|
|
|
|
++NumNames;
|
2018-01-13 02:35:13 +08:00
|
|
|
|
2018-01-18 04:19:04 +08:00
|
|
|
if (NumNames == 0)
|
|
|
|
return;
|
2018-01-13 02:35:13 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, "name");
|
|
|
|
|
|
|
|
SubSection FunctionSubsection(WASM_NAMES_FUNCTION);
|
|
|
|
raw_ostream &OS = FunctionSubsection.getStream();
|
2018-01-18 04:19:04 +08:00
|
|
|
writeUleb128(OS, NumNames, "name count");
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-01-18 04:19:04 +08:00
|
|
|
// Names must appear in function index order. As it happens ImportedFunctions
|
2018-02-22 02:29:23 +08:00
|
|
|
// and InputFunctions are numbers in order with imported functions coming
|
2018-01-18 04:19:04 +08:00
|
|
|
// first.
|
|
|
|
for (const Symbol *S : ImportedFunctions) {
|
|
|
|
writeUleb128(OS, S->getOutputIndex(), "import index");
|
2017-11-18 02:14:09 +08:00
|
|
|
writeStr(OS, S->getName(), "symbol name");
|
|
|
|
}
|
2018-02-22 02:29:23 +08:00
|
|
|
for (const InputFunction *F : InputFunctions) {
|
2018-01-18 04:19:04 +08:00
|
|
|
if (!F->getName().empty()) {
|
|
|
|
writeUleb128(OS, F->getOutputIndex(), "func index");
|
|
|
|
writeStr(OS, F->getName(), "symbol name");
|
|
|
|
}
|
|
|
|
}
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
FunctionSubsection.finalizeContents();
|
|
|
|
FunctionSubsection.writeToStream(Section->getStream());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::writeHeader() {
|
|
|
|
memcpy(Buffer->getBufferStart(), Header.data(), Header.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::writeSections() {
|
|
|
|
uint8_t *Buf = Buffer->getBufferStart();
|
|
|
|
parallelForEach(OutputSections, [Buf](OutputSection *S) { S->writeTo(Buf); });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fix the memory layout of the output binary. This assigns memory offsets
|
2017-12-01 08:53:21 +08:00
|
|
|
// to each of the input data sections as well as the explicit stack region.
|
2018-02-03 06:59:56 +08:00
|
|
|
// The memory layout is as follows, from low to high.
|
|
|
|
// - initialized data (starting at Config->GlobalBase)
|
|
|
|
// - BSS data (not currently implemented in llvm)
|
|
|
|
// - explicit stack (Config->ZStackSize)
|
|
|
|
// - heap start / unallocated
|
2017-11-18 02:14:09 +08:00
|
|
|
void Writer::layoutMemory() {
|
|
|
|
uint32_t MemoryPtr = 0;
|
|
|
|
if (!Config->Relocatable) {
|
|
|
|
MemoryPtr = Config->GlobalBase;
|
|
|
|
debugPrint("mem: global base = %d\n", Config->GlobalBase);
|
|
|
|
}
|
|
|
|
|
|
|
|
createOutputSegments();
|
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
// Arbitrarily set __dso_handle handle to point to the start of the data
|
|
|
|
// segments.
|
|
|
|
if (WasmSym::DsoHandle)
|
|
|
|
WasmSym::DsoHandle->setVirtualAddress(MemoryPtr);
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
for (OutputSegment *Seg : Segments) {
|
|
|
|
MemoryPtr = alignTo(MemoryPtr, Seg->Alignment);
|
|
|
|
Seg->StartVA = MemoryPtr;
|
2018-01-12 08:34:04 +08:00
|
|
|
debugPrint("mem: %-15s offset=%-8d size=%-8d align=%d\n",
|
2017-11-18 02:14:09 +08:00
|
|
|
Seg->Name.str().c_str(), MemoryPtr, Seg->Size, Seg->Alignment);
|
|
|
|
MemoryPtr += Seg->Size;
|
|
|
|
}
|
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
// TODO: Add .bss space here.
|
2018-02-07 11:04:53 +08:00
|
|
|
if (WasmSym::DataEnd)
|
|
|
|
WasmSym::DataEnd->setVirtualAddress(MemoryPtr);
|
2018-02-03 06:59:56 +08:00
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
DataSize = MemoryPtr;
|
|
|
|
if (!Config->Relocatable)
|
|
|
|
DataSize -= Config->GlobalBase;
|
|
|
|
debugPrint("mem: static data = %d\n", DataSize);
|
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
// Stack comes after static data and bss
|
2017-11-18 02:14:09 +08:00
|
|
|
if (!Config->Relocatable) {
|
|
|
|
MemoryPtr = alignTo(MemoryPtr, kStackAlignment);
|
|
|
|
if (Config->ZStackSize != alignTo(Config->ZStackSize, kStackAlignment))
|
|
|
|
error("stack size must be " + Twine(kStackAlignment) + "-byte aligned");
|
|
|
|
debugPrint("mem: stack size = %d\n", Config->ZStackSize);
|
|
|
|
debugPrint("mem: stack base = %d\n", MemoryPtr);
|
|
|
|
MemoryPtr += Config->ZStackSize;
|
2018-02-03 06:59:56 +08:00
|
|
|
WasmSym::StackPointer->setVirtualAddress(MemoryPtr);
|
2017-11-18 02:14:09 +08:00
|
|
|
debugPrint("mem: stack top = %d\n", MemoryPtr);
|
2018-01-17 09:34:31 +08:00
|
|
|
// Set `__heap_base` to directly follow the end of the stack. We don't
|
|
|
|
// allocate any heap memory up front, but instead really on the malloc/brk
|
|
|
|
// implementation growing the memory at runtime.
|
2018-02-03 06:59:56 +08:00
|
|
|
WasmSym::HeapBase->setVirtualAddress(MemoryPtr);
|
2018-01-17 09:34:31 +08:00
|
|
|
debugPrint("mem: heap base = %d\n", MemoryPtr);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MemSize = alignTo(MemoryPtr, WasmPageSize);
|
|
|
|
NumMemoryPages = MemSize / WasmPageSize;
|
|
|
|
debugPrint("mem: total pages = %d\n", NumMemoryPages);
|
|
|
|
}
|
|
|
|
|
|
|
|
SyntheticSection *Writer::createSyntheticSection(uint32_t Type,
|
2018-01-11 03:18:22 +08:00
|
|
|
StringRef Name) {
|
2017-11-18 02:14:09 +08:00
|
|
|
auto Sec = make<SyntheticSection>(Type, Name);
|
2017-12-20 13:14:48 +08:00
|
|
|
log("createSection: " + toString(*Sec));
|
2017-11-18 02:14:09 +08:00
|
|
|
OutputSections.push_back(Sec);
|
|
|
|
return Sec;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createSections() {
|
|
|
|
// Known sections
|
|
|
|
createTypeSection();
|
|
|
|
createImportSection();
|
|
|
|
createFunctionSection();
|
|
|
|
createTableSection();
|
|
|
|
createMemorySection();
|
|
|
|
createGlobalSection();
|
|
|
|
createExportSection();
|
|
|
|
createStartSection();
|
|
|
|
createElemSection();
|
|
|
|
createCodeSection();
|
|
|
|
createDataSection();
|
|
|
|
|
|
|
|
// Custom sections
|
2018-01-23 05:55:43 +08:00
|
|
|
if (Config->Relocatable)
|
2017-11-18 02:14:09 +08:00
|
|
|
createRelocSections();
|
|
|
|
createLinkingSection();
|
|
|
|
if (!Config->StripDebug && !Config->StripAll)
|
|
|
|
createNameSection();
|
|
|
|
|
|
|
|
for (OutputSection *S : OutputSections) {
|
|
|
|
S->setOffset(FileSize);
|
|
|
|
S->finalizeContents();
|
|
|
|
FileSize += S->getSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::calculateImports() {
|
2017-12-16 03:23:49 +08:00
|
|
|
for (Symbol *Sym : Symtab->getSymbols()) {
|
2018-01-23 05:55:43 +08:00
|
|
|
if (!Sym->isUndefined() || (Sym->isWeak() && !Config->Relocatable))
|
2017-12-16 03:23:49 +08:00
|
|
|
continue;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
if (auto *F = dyn_cast<FunctionSymbol>(Sym)) {
|
|
|
|
F->setOutputIndex(ImportedFunctions.size());
|
|
|
|
ImportedFunctions.push_back(F);
|
2018-02-21 07:38:27 +08:00
|
|
|
} else if (auto *G = dyn_cast<DataSymbol>(Sym)) {
|
2018-02-15 02:27:59 +08:00
|
|
|
G->setOutputIndex(ImportedGlobals.size());
|
|
|
|
ImportedGlobals.push_back(G);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-19 07:40:49 +08:00
|
|
|
void Writer::calculateExports() {
|
2018-01-23 05:55:43 +08:00
|
|
|
bool ExportHidden = Config->Relocatable;
|
2018-01-19 07:40:49 +08:00
|
|
|
StringSet<> UsedNames;
|
2018-02-03 06:59:56 +08:00
|
|
|
|
2018-01-19 07:40:49 +08:00
|
|
|
auto BudgeLocalName = [&](const Symbol *Sym) {
|
|
|
|
StringRef SymName = Sym->getName();
|
|
|
|
// We can't budge non-local names.
|
|
|
|
if (!Sym->isLocal())
|
|
|
|
return SymName;
|
|
|
|
// We must budge local names that have a collision with a symbol that we
|
|
|
|
// haven't yet processed.
|
|
|
|
if (!Symtab->find(SymName) && UsedNames.insert(SymName).second)
|
|
|
|
return SymName;
|
|
|
|
for (unsigned I = 1; ; ++I) {
|
|
|
|
std::string NameBuf = (SymName + "." + Twine(I)).str();
|
|
|
|
if (!UsedNames.count(NameBuf)) {
|
|
|
|
StringRef Name = Saver.save(NameBuf);
|
|
|
|
UsedNames.insert(Name); // Insert must use safe StringRef from save()
|
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
if (WasmSym::CallCtors && (!WasmSym::CallCtors->isHidden() || ExportHidden))
|
2018-01-19 07:40:49 +08:00
|
|
|
ExportedSymbols.emplace_back(
|
2018-02-03 06:59:56 +08:00
|
|
|
WasmExportEntry{WasmSym::CallCtors, WasmSym::CallCtors->getName()});
|
2018-01-19 07:40:49 +08:00
|
|
|
|
|
|
|
for (ObjFile *File : Symtab->ObjectFiles) {
|
|
|
|
for (Symbol *Sym : File->getSymbols()) {
|
|
|
|
if (!Sym->isDefined() || File != Sym->getFile())
|
|
|
|
continue;
|
2018-02-21 07:38:27 +08:00
|
|
|
if (!isa<FunctionSymbol>(Sym))
|
2018-01-19 07:40:49 +08:00
|
|
|
continue;
|
2018-02-14 04:29:38 +08:00
|
|
|
if (!Sym->getChunk()->Live)
|
2018-01-19 07:40:49 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if ((Sym->isHidden() || Sym->isLocal()) && !ExportHidden)
|
|
|
|
continue;
|
|
|
|
ExportedSymbols.emplace_back(WasmExportEntry{Sym, BudgeLocalName(Sym)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
for (const Symbol *Sym : DefinedDataSymbols) {
|
2018-01-19 07:57:55 +08:00
|
|
|
// Can't export the SP right now because its mutable, and mutuable globals
|
2018-01-23 05:55:43 +08:00
|
|
|
// are yet supported in the official binary format.
|
2018-01-19 07:57:55 +08:00
|
|
|
// TODO(sbc): Remove this if/when the "mutable global" proposal is accepted.
|
2018-02-03 06:59:56 +08:00
|
|
|
if (Sym == WasmSym::StackPointer)
|
2018-01-19 07:40:49 +08:00
|
|
|
continue;
|
|
|
|
ExportedSymbols.emplace_back(WasmExportEntry{Sym, BudgeLocalName(Sym)});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-11 03:18:22 +08:00
|
|
|
uint32_t Writer::lookupType(const WasmSignature &Sig) {
|
2018-01-11 04:12:26 +08:00
|
|
|
auto It = TypeIndices.find(Sig);
|
|
|
|
if (It == TypeIndices.end()) {
|
2018-01-11 03:18:22 +08:00
|
|
|
error("type not found: " + toString(Sig));
|
2018-01-11 04:12:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return It->second;
|
2018-01-11 03:18:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t Writer::registerType(const WasmSignature &Sig) {
|
2017-11-30 09:40:08 +08:00
|
|
|
auto Pair = TypeIndices.insert(std::make_pair(Sig, Types.size()));
|
2018-01-11 03:18:22 +08:00
|
|
|
if (Pair.second) {
|
|
|
|
DEBUG(dbgs() << "type " << toString(Sig) << "\n");
|
2017-11-30 09:40:08 +08:00
|
|
|
Types.push_back(&Sig);
|
2018-01-11 03:18:22 +08:00
|
|
|
}
|
2017-11-30 09:40:08 +08:00
|
|
|
return Pair.first->second;
|
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void Writer::calculateTypes() {
|
2018-02-01 07:48:14 +08:00
|
|
|
// The output type section is the union of the following sets:
|
|
|
|
// 1. Any signature used in the TYPE relocation
|
|
|
|
// 2. The signatures of all imported functions
|
|
|
|
// 3. The signatures of all defined functions
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
for (ObjFile *File : Symtab->ObjectFiles) {
|
2018-02-01 07:48:14 +08:00
|
|
|
ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
|
|
|
|
for (uint32_t I = 0; I < Types.size(); I++)
|
|
|
|
if (File->TypeIsUsed[I])
|
|
|
|
File->TypeMap[I] = registerType(Types[I]);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
2018-01-13 02:35:13 +08:00
|
|
|
|
2018-02-15 02:27:59 +08:00
|
|
|
for (const FunctionSymbol *Sym : ImportedFunctions)
|
2018-02-17 07:50:23 +08:00
|
|
|
registerType(*Sym->getFunctionType());
|
2018-02-01 07:48:14 +08:00
|
|
|
|
2018-02-22 02:29:23 +08:00
|
|
|
for (const InputFunction *F : InputFunctions)
|
2018-02-01 07:48:14 +08:00
|
|
|
registerType(F->Signature);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
|
2018-01-10 07:56:44 +08:00
|
|
|
void Writer::assignIndexes() {
|
2018-02-21 07:38:27 +08:00
|
|
|
uint32_t GlobalIndex = ImportedGlobals.size() + DefinedDataSymbols.size();
|
2018-02-22 02:29:23 +08:00
|
|
|
uint32_t FunctionIndex = ImportedFunctions.size() + InputFunctions.size();
|
2017-12-07 09:51:24 +08:00
|
|
|
|
2018-02-21 07:38:27 +08:00
|
|
|
auto AddDefinedData = [&](DefinedData *Sym) {
|
2018-02-03 06:59:56 +08:00
|
|
|
if (Sym) {
|
2018-02-21 07:38:27 +08:00
|
|
|
DefinedDataSymbols.emplace_back(Sym);
|
2018-02-03 06:59:56 +08:00
|
|
|
Sym->setOutputIndex(GlobalIndex++);
|
|
|
|
}
|
|
|
|
};
|
2018-02-21 07:38:27 +08:00
|
|
|
AddDefinedData(WasmSym::StackPointer);
|
|
|
|
AddDefinedData(WasmSym::HeapBase);
|
|
|
|
AddDefinedData(WasmSym::DataEnd);
|
2018-01-17 09:34:31 +08:00
|
|
|
|
2018-01-23 05:55:43 +08:00
|
|
|
if (Config->Relocatable)
|
2018-02-21 07:38:27 +08:00
|
|
|
DefinedDataSymbols.reserve(Symtab->getSymbols().size());
|
2017-12-07 09:51:24 +08:00
|
|
|
|
2018-01-25 05:37:30 +08:00
|
|
|
uint32_t TableIndex = kInitialTableOffset;
|
2017-12-12 06:00:56 +08:00
|
|
|
|
2018-02-03 06:59:56 +08:00
|
|
|
if (Config->Relocatable) {
|
|
|
|
for (ObjFile *File : Symtab->ObjectFiles) {
|
2018-01-10 07:56:44 +08:00
|
|
|
DEBUG(dbgs() << "Globals: " << File->getName() << "\n");
|
|
|
|
for (Symbol *Sym : File->getSymbols()) {
|
|
|
|
// Create wasm globals for data symbols defined in this file
|
2018-02-15 02:27:59 +08:00
|
|
|
if (File != Sym->getFile())
|
2018-01-10 07:56:44 +08:00
|
|
|
continue;
|
2018-02-21 07:38:27 +08:00
|
|
|
if (auto *G = dyn_cast<DefinedData>(Sym))
|
|
|
|
AddDefinedData(G);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 07:39:11 +08:00
|
|
|
}
|
2017-12-12 06:00:56 +08:00
|
|
|
|
2018-01-09 07:39:11 +08:00
|
|
|
for (ObjFile *File : Symtab->ObjectFiles) {
|
2018-01-10 07:56:44 +08:00
|
|
|
DEBUG(dbgs() << "Functions: " << File->getName() << "\n");
|
|
|
|
for (InputFunction *Func : File->Functions) {
|
2018-02-14 04:29:38 +08:00
|
|
|
if (!Func->Live)
|
2018-01-13 06:25:17 +08:00
|
|
|
continue;
|
2018-02-22 02:29:23 +08:00
|
|
|
InputFunctions.emplace_back(Func);
|
2018-01-10 07:56:44 +08:00
|
|
|
Func->setOutputIndex(FunctionIndex++);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ObjFile *File : Symtab->ObjectFiles) {
|
2018-02-01 07:48:14 +08:00
|
|
|
DEBUG(dbgs() << "Handle relocs: " << File->getName() << "\n");
|
|
|
|
auto HandleRelocs = [&](InputChunk *Chunk) {
|
2018-02-14 04:29:38 +08:00
|
|
|
if (!Chunk->Live)
|
2018-01-23 09:25:56 +08:00
|
|
|
return;
|
2018-02-01 07:48:14 +08:00
|
|
|
ArrayRef<WasmSignature> Types = File->getWasmObj()->types();
|
2018-01-23 09:25:56 +08:00
|
|
|
for (const WasmRelocation& Reloc : Chunk->getRelocations()) {
|
2018-02-01 07:48:14 +08:00
|
|
|
if (Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_I32 ||
|
|
|
|
Reloc.Type == R_WEBASSEMBLY_TABLE_INDEX_SLEB) {
|
2018-02-15 02:27:59 +08:00
|
|
|
FunctionSymbol *Sym = File->getFunctionSymbol(Reloc.Index);
|
2018-02-01 07:48:14 +08:00
|
|
|
if (Sym->hasTableIndex() || !Sym->hasOutputIndex())
|
|
|
|
continue;
|
|
|
|
Sym->setTableIndex(TableIndex++);
|
|
|
|
IndirectFunctions.emplace_back(Sym);
|
|
|
|
} else if (Reloc.Type == R_WEBASSEMBLY_TYPE_INDEX_LEB) {
|
|
|
|
Chunk->File->TypeMap[Reloc.Index] = registerType(Types[Reloc.Index]);
|
|
|
|
Chunk->File->TypeIsUsed[Reloc.Index] = true;
|
|
|
|
}
|
2018-01-23 09:25:56 +08:00
|
|
|
}
|
|
|
|
};
|
2018-02-01 07:48:14 +08:00
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
for (InputFunction* Function : File->Functions)
|
2018-02-01 07:48:14 +08:00
|
|
|
HandleRelocs(Function);
|
2018-01-23 09:25:56 +08:00
|
|
|
for (InputSegment* Segment : File->Segments)
|
2018-02-01 07:48:14 +08:00
|
|
|
HandleRelocs(Segment);
|
2017-11-18 02:14:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef getOutputDataSegmentName(StringRef Name) {
|
|
|
|
if (Config->Relocatable)
|
|
|
|
return Name;
|
|
|
|
|
|
|
|
for (StringRef V :
|
|
|
|
{".text.", ".rodata.", ".data.rel.ro.", ".data.", ".bss.rel.ro.",
|
|
|
|
".bss.", ".init_array.", ".fini_array.", ".ctors.", ".dtors.", ".tbss.",
|
|
|
|
".gcc_except_table.", ".tdata.", ".ARM.exidx.", ".ARM.extab."}) {
|
|
|
|
StringRef Prefix = V.drop_back();
|
|
|
|
if (Name.startswith(V) || Name == Prefix)
|
|
|
|
return Prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createOutputSegments() {
|
|
|
|
for (ObjFile *File : Symtab->ObjectFiles) {
|
|
|
|
for (InputSegment *Segment : File->Segments) {
|
2018-02-14 04:29:38 +08:00
|
|
|
if (!Segment->Live)
|
2018-01-13 06:25:17 +08:00
|
|
|
continue;
|
2017-11-18 02:14:09 +08:00
|
|
|
StringRef Name = getOutputDataSegmentName(Segment->getName());
|
|
|
|
OutputSegment *&S = SegmentMap[Name];
|
|
|
|
if (S == nullptr) {
|
|
|
|
DEBUG(dbgs() << "new segment: " << Name << "\n");
|
|
|
|
S = make<OutputSegment>(Name);
|
|
|
|
Segments.push_back(S);
|
|
|
|
}
|
|
|
|
S->addInputSegment(Segment);
|
|
|
|
DEBUG(dbgs() << "added data: " << Name << ": " << S->Size << "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 02:35:13 +08:00
|
|
|
static const int OPCODE_CALL = 0x10;
|
|
|
|
static const int OPCODE_END = 0xb;
|
|
|
|
|
|
|
|
// Create synthetic "__wasm_call_ctors" function based on ctor functions
|
|
|
|
// in input object.
|
|
|
|
void Writer::createCtorFunction() {
|
2018-02-22 02:29:23 +08:00
|
|
|
uint32_t FunctionIndex = ImportedFunctions.size() + InputFunctions.size();
|
2018-02-03 06:59:56 +08:00
|
|
|
WasmSym::CallCtors->setOutputIndex(FunctionIndex);
|
2018-01-13 02:35:13 +08:00
|
|
|
|
|
|
|
// First write the body bytes to a string.
|
|
|
|
std::string FunctionBody;
|
|
|
|
static WasmSignature Signature = {{}, WASM_TYPE_NORESULT};
|
|
|
|
{
|
|
|
|
raw_string_ostream OS(FunctionBody);
|
|
|
|
writeUleb128(OS, 0, "num locals");
|
|
|
|
for (const WasmInitFunc &F : InitFunctions) {
|
|
|
|
writeU8(OS, OPCODE_CALL, "CALL");
|
|
|
|
writeUleb128(OS, F.FunctionIndex, "function index");
|
|
|
|
}
|
|
|
|
writeU8(OS, OPCODE_END, "END");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Once we know the size of the body we can create the final function body
|
|
|
|
raw_string_ostream OS(CtorFunctionBody);
|
|
|
|
writeUleb128(OS, FunctionBody.size(), "function size");
|
|
|
|
OS.flush();
|
|
|
|
CtorFunctionBody += FunctionBody;
|
2018-01-13 08:22:00 +08:00
|
|
|
ArrayRef<uint8_t> BodyArray(
|
|
|
|
reinterpret_cast<const uint8_t *>(CtorFunctionBody.data()),
|
|
|
|
CtorFunctionBody.size());
|
2018-02-22 02:37:44 +08:00
|
|
|
SyntheticFunction *F = make<SyntheticFunction>(Signature, BodyArray,
|
|
|
|
WasmSym::CallCtors->getName());
|
|
|
|
F->setOutputIndex(FunctionIndex);
|
|
|
|
InputFunctions.emplace_back(F);
|
2018-01-13 02:35:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate InitFunctions vector with init functions from all input objects.
|
|
|
|
// This is then used either when creating the output linking section or to
|
|
|
|
// synthesize the "__wasm_call_ctors" function.
|
|
|
|
void Writer::calculateInitFunctions() {
|
|
|
|
for (ObjFile *File : Symtab->ObjectFiles) {
|
|
|
|
const WasmLinkingData &L = File->getWasmObj()->linkingData();
|
|
|
|
InitFunctions.reserve(InitFunctions.size() + L.InitFunctions.size());
|
|
|
|
for (const WasmInitFunc &F : L.InitFunctions)
|
|
|
|
InitFunctions.emplace_back(WasmInitFunc{
|
|
|
|
F.Priority, File->relocateFunctionIndex(F.FunctionIndex)});
|
|
|
|
}
|
|
|
|
// Sort in order of priority (lowest first) so that they are called
|
|
|
|
// in the correct order.
|
2018-02-21 08:34:34 +08:00
|
|
|
std::stable_sort(InitFunctions.begin(), InitFunctions.end(),
|
|
|
|
[](const WasmInitFunc &L, const WasmInitFunc &R) {
|
|
|
|
return L.Priority < R.Priority;
|
|
|
|
});
|
2018-01-13 02:35:13 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 02:14:09 +08:00
|
|
|
void Writer::run() {
|
|
|
|
log("-- calculateImports");
|
|
|
|
calculateImports();
|
2018-01-10 07:56:44 +08:00
|
|
|
log("-- assignIndexes");
|
|
|
|
assignIndexes();
|
2018-01-19 07:40:49 +08:00
|
|
|
log("-- calculateExports");
|
|
|
|
calculateExports();
|
2018-01-13 02:35:13 +08:00
|
|
|
log("-- calculateInitFunctions");
|
|
|
|
calculateInitFunctions();
|
|
|
|
if (!Config->Relocatable)
|
|
|
|
createCtorFunction();
|
2018-02-01 07:48:14 +08:00
|
|
|
log("-- calculateTypes");
|
|
|
|
calculateTypes();
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
if (errorHandler().Verbose) {
|
2018-02-22 02:29:23 +08:00
|
|
|
log("Defined Functions: " + Twine(InputFunctions.size()));
|
2018-02-21 07:38:27 +08:00
|
|
|
log("Defined Data Syms: " + Twine(DefinedDataSymbols.size()));
|
2018-01-10 07:56:44 +08:00
|
|
|
log("Function Imports : " + Twine(ImportedFunctions.size()));
|
|
|
|
log("Global Imports : " + Twine(ImportedGlobals.size()));
|
2017-12-12 06:00:56 +08:00
|
|
|
log("Total Imports : " +
|
2018-01-10 07:56:44 +08:00
|
|
|
Twine(ImportedFunctions.size() + ImportedGlobals.size()));
|
2017-11-18 02:14:09 +08:00
|
|
|
for (ObjFile *File : Symtab->ObjectFiles)
|
|
|
|
File->dumpInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
log("-- layoutMemory");
|
|
|
|
layoutMemory();
|
|
|
|
|
|
|
|
createHeader();
|
|
|
|
log("-- createSections");
|
|
|
|
createSections();
|
|
|
|
|
|
|
|
log("-- openFile");
|
|
|
|
openFile();
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
writeHeader();
|
|
|
|
|
|
|
|
log("-- writeSections");
|
|
|
|
writeSections();
|
|
|
|
if (errorCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Error E = Buffer->commit())
|
|
|
|
fatal("failed to write the output file: " + toString(std::move(E)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open a result file.
|
|
|
|
void Writer::openFile() {
|
|
|
|
log("writing: " + Config->OutputFile);
|
|
|
|
::remove(Config->OutputFile.str().c_str());
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
|
|
|
|
FileOutputBuffer::create(Config->OutputFile, FileSize,
|
|
|
|
FileOutputBuffer::F_executable);
|
|
|
|
|
|
|
|
if (!BufferOrErr)
|
|
|
|
error("failed to open " + Config->OutputFile + ": " +
|
|
|
|
toString(BufferOrErr.takeError()));
|
|
|
|
else
|
|
|
|
Buffer = std::move(*BufferOrErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Writer::createHeader() {
|
|
|
|
raw_string_ostream OS(Header);
|
|
|
|
writeBytes(OS, WasmMagic, sizeof(WasmMagic), "wasm magic");
|
|
|
|
writeU32(OS, WasmVersion, "wasm version");
|
|
|
|
OS.flush();
|
|
|
|
FileSize += Header.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void lld::wasm::writeResult() { Writer().run(); }
|