2019-05-21 17:13:09 +08:00
|
|
|
//===- SyntheticSections.cpp ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains linker-synthesized sections.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "SyntheticSections.h"
|
|
|
|
|
|
|
|
#include "InputChunks.h"
|
2021-02-11 19:15:24 +08:00
|
|
|
#include "InputElement.h"
|
2019-05-21 17:13:09 +08:00
|
|
|
#include "OutputSegment.h"
|
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "llvm/Support/Path.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
2019-05-21 17:13:09 +08:00
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
OutStruct out;
|
2019-05-21 17:13:09 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Some synthetic sections (e.g. "name" and "linking") have subsections.
|
|
|
|
// Just like the synthetic sections themselves these need to be created before
|
|
|
|
// they can be written out (since they are preceded by their length). This
|
|
|
|
// class is used to create subsections and then write them into the stream
|
|
|
|
// of the parent section.
|
|
|
|
class SubSection {
|
|
|
|
public:
|
|
|
|
explicit SubSection(uint32_t type) : type(type) {}
|
|
|
|
|
|
|
|
void writeTo(raw_ostream &to) {
|
|
|
|
os.flush();
|
|
|
|
writeUleb128(to, type, "subsection type");
|
|
|
|
writeUleb128(to, body.size(), "subsection size");
|
|
|
|
to.write(body.data(), body.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t type;
|
|
|
|
std::string body;
|
|
|
|
|
|
|
|
public:
|
|
|
|
raw_string_ostream os{body};
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void DylinkSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-09-10 19:21:28 +08:00
|
|
|
{
|
|
|
|
SubSection sub(WASM_DYLINK_MEM_INFO);
|
|
|
|
writeUleb128(sub.os, memSize, "MemSize");
|
|
|
|
writeUleb128(sub.os, memAlign, "MemAlign");
|
|
|
|
writeUleb128(sub.os, out.elemSec->numEntries(), "TableSize");
|
|
|
|
writeUleb128(sub.os, 0, "TableAlign");
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (symtab->sharedFiles.size()) {
|
|
|
|
SubSection sub(WASM_DYLINK_NEEDED);
|
|
|
|
writeUleb128(sub.os, symtab->sharedFiles.size(), "Needed");
|
|
|
|
for (auto *so : symtab->sharedFiles)
|
|
|
|
writeStr(sub.os, llvm::sys::path::filename(so->getName()), "so name");
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
2021-08-27 03:29:32 +08:00
|
|
|
|
2021-10-08 03:17:15 +08:00
|
|
|
// Under certain circumstances we need to include extra information about our
|
|
|
|
// exports and/or imports to the dynamic linker.
|
|
|
|
// For exports we need to notify the linker when an export is TLS since the
|
|
|
|
// exported value is relative to __tls_base rather than __memory_base.
|
|
|
|
// For imports we need to notify the dynamic linker when an import is weak
|
|
|
|
// so that knows not to report an error for such symbols.
|
|
|
|
std::vector<const Symbol *> importInfo;
|
2021-08-27 03:29:32 +08:00
|
|
|
std::vector<const Symbol *> exportInfo;
|
|
|
|
for (const Symbol *sym : symtab->getSymbols()) {
|
2021-10-08 03:17:15 +08:00
|
|
|
if (sym->isLive()) {
|
|
|
|
if (sym->isExported() && sym->isTLS() && isa<DefinedData>(sym)) {
|
|
|
|
exportInfo.push_back(sym);
|
|
|
|
}
|
|
|
|
if (sym->isUndefWeak()) {
|
|
|
|
importInfo.push_back(sym);
|
|
|
|
}
|
2021-08-27 03:29:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!exportInfo.empty()) {
|
|
|
|
SubSection sub(WASM_DYLINK_EXPORT_INFO);
|
|
|
|
writeUleb128(sub.os, exportInfo.size(), "num exports");
|
|
|
|
|
|
|
|
for (const Symbol *sym : exportInfo) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "export info: " << toString(*sym) << "\n");
|
|
|
|
StringRef name = sym->getName();
|
|
|
|
if (auto *f = dyn_cast<DefinedFunction>(sym)) {
|
|
|
|
if (Optional<StringRef> exportName = f->function->getExportName()) {
|
|
|
|
name = *exportName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writeStr(sub.os, name, "sym name");
|
|
|
|
writeUleb128(sub.os, sym->flags, "sym flags");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
2021-10-08 03:17:15 +08:00
|
|
|
|
|
|
|
if (!importInfo.empty()) {
|
|
|
|
SubSection sub(WASM_DYLINK_IMPORT_INFO);
|
|
|
|
writeUleb128(sub.os, importInfo.size(), "num imports");
|
|
|
|
|
|
|
|
for (const Symbol *sym : importInfo) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "imports info: " << toString(*sym) << "\n");
|
|
|
|
StringRef module = sym->importModule.getValueOr(defaultModule);
|
|
|
|
StringRef name = sym->importName.getValueOr(sym->getName());
|
|
|
|
writeStr(sub.os, module, "import module");
|
|
|
|
writeStr(sub.os, name, "import name");
|
|
|
|
writeUleb128(sub.os, sym->flags, "sym flags");
|
|
|
|
}
|
|
|
|
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TypeSection::registerType(const WasmSignature &sig) {
|
|
|
|
auto pair = typeIndices.insert(std::make_pair(sig, types.size()));
|
|
|
|
if (pair.second) {
|
|
|
|
LLVM_DEBUG(llvm::dbgs() << "type " << toString(sig) << "\n");
|
|
|
|
types.push_back(&sig);
|
|
|
|
}
|
|
|
|
return pair.first->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t TypeSection::lookupType(const WasmSignature &sig) {
|
|
|
|
auto it = typeIndices.find(sig);
|
|
|
|
if (it == typeIndices.end()) {
|
|
|
|
error("type not found: " + toString(sig));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TypeSection::writeBody() {
|
|
|
|
writeUleb128(bodyOutputStream, types.size(), "type count");
|
|
|
|
for (const WasmSignature *sig : types)
|
|
|
|
writeSig(bodyOutputStream, *sig);
|
|
|
|
}
|
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
uint32_t ImportSection::getNumImports() const {
|
2019-05-23 17:41:03 +08:00
|
|
|
assert(isSealed);
|
2019-05-21 17:13:09 +08:00
|
|
|
uint32_t numImports = importedSymbols.size() + gotSymbols.size();
|
|
|
|
if (config->importMemory)
|
|
|
|
++numImports;
|
|
|
|
return numImports;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportSection::addGOTEntry(Symbol *sym) {
|
2019-05-23 17:41:03 +08:00
|
|
|
assert(!isSealed);
|
2019-05-21 17:13:09 +08:00
|
|
|
if (sym->hasGOTIndex())
|
|
|
|
return;
|
2019-09-25 04:52:12 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addGOTEntry: " << toString(*sym) << "\n");
|
2019-05-21 17:13:09 +08:00
|
|
|
sym->setGOTIndex(numImportedGlobals++);
|
|
|
|
gotSymbols.push_back(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImportSection::addImport(Symbol *sym) {
|
2019-05-23 17:41:03 +08:00
|
|
|
assert(!isSealed);
|
2021-07-23 05:12:50 +08:00
|
|
|
StringRef module = sym->importModule.getValueOr(defaultModule);
|
|
|
|
StringRef name = sym->importName.getValueOr(sym->getName());
|
|
|
|
if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
|
|
|
|
ImportKey<WasmSignature> key(*(f->getSignature()), module, name);
|
|
|
|
auto entry = importedFunctions.try_emplace(key, numImportedFunctions);
|
|
|
|
if (entry.second) {
|
|
|
|
importedSymbols.emplace_back(sym);
|
|
|
|
f->setFunctionIndex(numImportedFunctions++);
|
|
|
|
} else {
|
|
|
|
f->setFunctionIndex(entry.first->second);
|
|
|
|
}
|
|
|
|
} else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
|
|
|
|
ImportKey<WasmGlobalType> key(*(g->getGlobalType()), module, name);
|
|
|
|
auto entry = importedGlobals.try_emplace(key, numImportedGlobals);
|
|
|
|
if (entry.second) {
|
|
|
|
importedSymbols.emplace_back(sym);
|
|
|
|
g->setGlobalIndex(numImportedGlobals++);
|
|
|
|
} else {
|
|
|
|
g->setGlobalIndex(entry.first->second);
|
|
|
|
}
|
|
|
|
} else if (auto *t = dyn_cast<TagSymbol>(sym)) {
|
2021-09-30 05:17:14 +08:00
|
|
|
ImportKey<WasmSignature> key(*(t->getSignature()), module, name);
|
|
|
|
auto entry = importedTags.try_emplace(key, numImportedTags);
|
|
|
|
if (entry.second) {
|
|
|
|
importedSymbols.emplace_back(sym);
|
|
|
|
t->setTagIndex(numImportedTags++);
|
|
|
|
} else {
|
|
|
|
t->setTagIndex(entry.first->second);
|
|
|
|
}
|
2021-07-23 05:12:50 +08:00
|
|
|
} else {
|
|
|
|
assert(TableSymbol::classof(sym));
|
|
|
|
auto *table = cast<TableSymbol>(sym);
|
|
|
|
ImportKey<WasmTableType> key(*(table->getTableType()), module, name);
|
|
|
|
auto entry = importedTables.try_emplace(key, numImportedTables);
|
|
|
|
if (entry.second) {
|
|
|
|
importedSymbols.emplace_back(sym);
|
|
|
|
table->setTableNumber(numImportedTables++);
|
|
|
|
} else {
|
|
|
|
table->setTableNumber(entry.first->second);
|
|
|
|
}
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ImportSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
writeUleb128(os, getNumImports(), "import count");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-07-13 08:18:39 +08:00
|
|
|
bool is64 = config->is64.getValueOr(false);
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
if (config->importMemory) {
|
|
|
|
WasmImport import;
|
|
|
|
import.Module = defaultModule;
|
|
|
|
import.Field = "memory";
|
|
|
|
import.Kind = WASM_EXTERNAL_MEMORY;
|
|
|
|
import.Memory.Flags = 0;
|
2021-03-23 21:46:32 +08:00
|
|
|
import.Memory.Minimum = out.memorySec->numMemoryPages;
|
2019-05-21 17:13:09 +08:00
|
|
|
if (out.memorySec->maxMemoryPages != 0 || config->sharedMemory) {
|
|
|
|
import.Memory.Flags |= WASM_LIMITS_FLAG_HAS_MAX;
|
|
|
|
import.Memory.Maximum = out.memorySec->maxMemoryPages;
|
|
|
|
}
|
|
|
|
if (config->sharedMemory)
|
|
|
|
import.Memory.Flags |= WASM_LIMITS_FLAG_IS_SHARED;
|
2021-07-13 08:18:39 +08:00
|
|
|
if (is64)
|
2020-06-30 08:53:09 +08:00
|
|
|
import.Memory.Flags |= WASM_LIMITS_FLAG_IS_64;
|
2019-05-21 17:13:09 +08:00
|
|
|
writeImport(os, import);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Symbol *sym : importedSymbols) {
|
|
|
|
WasmImport import;
|
2021-07-15 08:16:15 +08:00
|
|
|
import.Field = sym->importName.getValueOr(sym->getName());
|
|
|
|
import.Module = sym->importModule.getValueOr(defaultModule);
|
2019-05-21 17:13:09 +08:00
|
|
|
|
|
|
|
if (auto *functionSym = dyn_cast<FunctionSymbol>(sym)) {
|
|
|
|
import.Kind = WASM_EXTERNAL_FUNCTION;
|
|
|
|
import.SigIndex = out.typeSec->lookupType(*functionSym->signature);
|
|
|
|
} else if (auto *globalSym = dyn_cast<GlobalSymbol>(sym)) {
|
|
|
|
import.Kind = WASM_EXTERNAL_GLOBAL;
|
|
|
|
import.Global = *globalSym->getGlobalType();
|
2021-06-15 16:49:43 +08:00
|
|
|
} else if (auto *tagSym = dyn_cast<TagSymbol>(sym)) {
|
|
|
|
import.Kind = WASM_EXTERNAL_TAG;
|
[WebAssembly] Remove WasmTagType
This removes `WasmTagType`. `WasmTagType` contained an attribute and a
signature index:
```
struct WasmTagType {
uint8_t Attribute;
uint32_t SigIndex;
};
```
Currently the attribute field is not used and reserved for future use,
and always 0. And that this class contains `SigIndex` as its property is
a little weird in the place, because the tag type's signature index is
not an inherent property of a tag but rather a reference to another
section that changes after linking. This makes tag handling in the
linker also weird that tag-related methods are taking both `WasmTagType`
and `WasmSignature` even though `WasmTagType` contains a signature
index. This is because the signature index changes in linking so it
doesn't have any info at this point. This instead moves `SigIndex` to
`struct WasmTag` itself, as we did for `struct WasmFunction` in D111104.
In this CL, in lib/MC and lib/Object, this now treats tag types in the
same way as function types. Also in YAML, this removes `struct Tag`,
because now it only contains the tag index. Also tags set `SigIndex` in
`WasmImport` union, as functions do.
I think this makes things simpler and makes tag handling more in line
with function handling. These two shares similar properties in that both
of them have signatures, but they are kind of nominal so having the same
signature doesn't mean they are the same element.
Also a drive-by fix: the reserved 'attirubute' part's encoding changed
from uleb32 to uint8 a while ago. This was fixed in lib/MC and
lib/Object but not in YAML. This doesn't change object files because the
field's value is always 0 and its encoding is the same for the both
encoding.
This is effectively NFC; I didn't mark it as such just because it
changed YAML test results.
Reviewed By: sbc100, tlively
Differential Revision: https://reviews.llvm.org/D111086
2021-10-02 10:07:41 +08:00
|
|
|
import.SigIndex = out.typeSec->lookupType(*tagSym->signature);
|
2021-01-05 19:08:58 +08:00
|
|
|
} else {
|
|
|
|
auto *tableSym = cast<TableSymbol>(sym);
|
|
|
|
import.Kind = WASM_EXTERNAL_TABLE;
|
|
|
|
import.Table = *tableSym->getTableType();
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
writeImport(os, import);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Symbol *sym : gotSymbols) {
|
|
|
|
WasmImport import;
|
|
|
|
import.Kind = WASM_EXTERNAL_GLOBAL;
|
2021-07-13 08:18:39 +08:00
|
|
|
auto ptrType = is64 ? WASM_TYPE_I64 : WASM_TYPE_I32;
|
|
|
|
import.Global = {static_cast<uint8_t>(ptrType), true};
|
2019-05-21 17:13:09 +08:00
|
|
|
if (isa<DataSymbol>(sym))
|
|
|
|
import.Module = "GOT.mem";
|
|
|
|
else
|
|
|
|
import.Module = "GOT.func";
|
|
|
|
import.Field = sym->getName();
|
|
|
|
writeImport(os, import);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
|
|
|
|
|
|
|
writeUleb128(os, inputFunctions.size(), "function count");
|
|
|
|
for (const InputFunction *func : inputFunctions)
|
|
|
|
writeUleb128(os, out.typeSec->lookupType(func->signature), "sig index");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionSection::addFunction(InputFunction *func) {
|
|
|
|
if (!func->live)
|
|
|
|
return;
|
|
|
|
uint32_t functionIndex =
|
2019-07-10 17:10:01 +08:00
|
|
|
out.importSec->getNumImportedFunctions() + inputFunctions.size();
|
2019-05-21 17:13:09 +08:00
|
|
|
inputFunctions.emplace_back(func);
|
|
|
|
func->setFunctionIndex(functionIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2021-01-05 19:08:58 +08:00
|
|
|
|
2021-01-14 17:15:56 +08:00
|
|
|
writeUleb128(os, inputTables.size(), "table count");
|
2021-01-05 19:08:58 +08:00
|
|
|
for (const InputTable *table : inputTables)
|
|
|
|
writeTableType(os, table->getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableSection::addTable(InputTable *table) {
|
|
|
|
if (!table->live)
|
|
|
|
return;
|
2021-02-13 03:01:41 +08:00
|
|
|
// Some inputs require that the indirect function table be assigned to table
|
|
|
|
// number 0.
|
|
|
|
if (config->legacyFunctionTable &&
|
|
|
|
isa<DefinedTable>(WasmSym::indirectFunctionTable) &&
|
|
|
|
cast<DefinedTable>(WasmSym::indirectFunctionTable)->table == table) {
|
|
|
|
if (out.importSec->getNumImportedTables()) {
|
|
|
|
// Alack! Some other input imported a table, meaning that we are unable
|
|
|
|
// to assign table number 0 to the indirect function table.
|
|
|
|
for (const auto *culprit : out.importSec->importedSymbols) {
|
|
|
|
if (isa<UndefinedTable>(culprit)) {
|
|
|
|
error("object file not built with 'reference-types' feature "
|
|
|
|
"conflicts with import of table " +
|
2021-03-04 17:33:11 +08:00
|
|
|
culprit->getName() + " by file " +
|
|
|
|
toString(culprit->getFile()));
|
2021-02-13 03:01:41 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
llvm_unreachable("failed to find conflicting table import");
|
|
|
|
}
|
|
|
|
inputTables.insert(inputTables.begin(), table);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-05 19:08:58 +08:00
|
|
|
inputTables.push_back(table);
|
2021-02-13 03:01:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TableSection::assignIndexes() {
|
|
|
|
uint32_t tableNumber = out.importSec->getNumImportedTables();
|
|
|
|
for (InputTable *t : inputTables)
|
|
|
|
t->assignIndex(tableNumber++);
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemorySection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
bool hasMax = maxMemoryPages != 0 || config->sharedMemory;
|
|
|
|
writeUleb128(os, 1, "memory count");
|
|
|
|
unsigned flags = 0;
|
|
|
|
if (hasMax)
|
|
|
|
flags |= WASM_LIMITS_FLAG_HAS_MAX;
|
|
|
|
if (config->sharedMemory)
|
|
|
|
flags |= WASM_LIMITS_FLAG_IS_SHARED;
|
2020-07-07 04:34:16 +08:00
|
|
|
if (config->is64.getValueOr(false))
|
2020-06-30 08:53:09 +08:00
|
|
|
flags |= WASM_LIMITS_FLAG_IS_64;
|
2019-05-21 17:13:09 +08:00
|
|
|
writeUleb128(os, flags, "memory limits flags");
|
|
|
|
writeUleb128(os, numMemoryPages, "initial pages");
|
|
|
|
if (hasMax)
|
|
|
|
writeUleb128(os, maxMemoryPages, "max pages");
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
void TagSection::writeBody() {
|
2020-03-25 10:36:13 +08:00
|
|
|
raw_ostream &os = bodyOutputStream;
|
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
writeUleb128(os, inputTags.size(), "tag count");
|
|
|
|
for (InputTag *t : inputTags) {
|
[WebAssembly] Remove WasmTagType
This removes `WasmTagType`. `WasmTagType` contained an attribute and a
signature index:
```
struct WasmTagType {
uint8_t Attribute;
uint32_t SigIndex;
};
```
Currently the attribute field is not used and reserved for future use,
and always 0. And that this class contains `SigIndex` as its property is
a little weird in the place, because the tag type's signature index is
not an inherent property of a tag but rather a reference to another
section that changes after linking. This makes tag handling in the
linker also weird that tag-related methods are taking both `WasmTagType`
and `WasmSignature` even though `WasmTagType` contains a signature
index. This is because the signature index changes in linking so it
doesn't have any info at this point. This instead moves `SigIndex` to
`struct WasmTag` itself, as we did for `struct WasmFunction` in D111104.
In this CL, in lib/MC and lib/Object, this now treats tag types in the
same way as function types. Also in YAML, this removes `struct Tag`,
because now it only contains the tag index. Also tags set `SigIndex` in
`WasmImport` union, as functions do.
I think this makes things simpler and makes tag handling more in line
with function handling. These two shares similar properties in that both
of them have signatures, but they are kind of nominal so having the same
signature doesn't mean they are the same element.
Also a drive-by fix: the reserved 'attirubute' part's encoding changed
from uleb32 to uint8 a while ago. This was fixed in lib/MC and
lib/Object but not in YAML. This doesn't change object files because the
field's value is always 0 and its encoding is the same for the both
encoding.
This is effectively NFC; I didn't mark it as such just because it
changed YAML test results.
Reviewed By: sbc100, tlively
Differential Revision: https://reviews.llvm.org/D111086
2021-10-02 10:07:41 +08:00
|
|
|
writeUleb128(os, 0, "tag attribute"); // Reserved "attribute" field
|
|
|
|
writeUleb128(os, out.typeSec->lookupType(t->signature), "sig index");
|
2020-03-25 10:36:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 16:49:43 +08:00
|
|
|
void TagSection::addTag(InputTag *tag) {
|
|
|
|
if (!tag->live)
|
2020-03-25 10:36:13 +08:00
|
|
|
return;
|
2021-06-15 16:49:43 +08:00
|
|
|
uint32_t tagIndex = out.importSec->getNumImportedTags() + inputTags.size();
|
|
|
|
LLVM_DEBUG(dbgs() << "addTag: " << tagIndex << "\n");
|
|
|
|
tag->assignIndex(tagIndex);
|
|
|
|
inputTags.push_back(tag);
|
2020-03-25 10:36:13 +08:00
|
|
|
}
|
|
|
|
|
2019-08-14 01:02:02 +08:00
|
|
|
void GlobalSection::assignIndexes() {
|
|
|
|
uint32_t globalIndex = out.importSec->getNumImportedGlobals();
|
|
|
|
for (InputGlobal *g : inputGlobals)
|
2021-02-11 19:15:24 +08:00
|
|
|
g->assignIndex(globalIndex++);
|
2020-10-08 05:48:37 +08:00
|
|
|
for (Symbol *sym : internalGotSymbols)
|
2019-08-14 01:02:02 +08:00
|
|
|
sym->setGOTIndex(globalIndex++);
|
2019-09-25 04:52:12 +08:00
|
|
|
isSealed = true;
|
2019-08-14 01:02:02 +08:00
|
|
|
}
|
|
|
|
|
2021-03-03 18:13:25 +08:00
|
|
|
static void ensureIndirectFunctionTable() {
|
|
|
|
if (!WasmSym::indirectFunctionTable)
|
|
|
|
WasmSym::indirectFunctionTable =
|
|
|
|
symtab->resolveIndirectFunctionTable(/*required =*/true);
|
|
|
|
}
|
|
|
|
|
2020-10-08 05:48:37 +08:00
|
|
|
void GlobalSection::addInternalGOTEntry(Symbol *sym) {
|
2019-09-25 04:52:12 +08:00
|
|
|
assert(!isSealed);
|
|
|
|
if (sym->requiresGOT)
|
2019-08-14 01:02:02 +08:00
|
|
|
return;
|
2020-10-08 05:48:37 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "addInternalGOTEntry: " << sym->getName() << " "
|
2019-09-25 04:52:12 +08:00
|
|
|
<< toString(sym->kind()) << "\n");
|
|
|
|
sym->requiresGOT = true;
|
2021-03-03 18:13:25 +08:00
|
|
|
if (auto *F = dyn_cast<FunctionSymbol>(sym)) {
|
|
|
|
ensureIndirectFunctionTable();
|
2019-09-25 04:52:12 +08:00
|
|
|
out.elemSec->addEntry(F);
|
2021-03-03 18:13:25 +08:00
|
|
|
}
|
2020-10-08 05:48:37 +08:00
|
|
|
internalGotSymbols.push_back(sym);
|
|
|
|
}
|
|
|
|
|
2021-08-27 03:29:32 +08:00
|
|
|
void GlobalSection::generateRelocationCode(raw_ostream &os, bool TLS) const {
|
2021-07-13 08:18:39 +08:00
|
|
|
bool is64 = config->is64.getValueOr(false);
|
|
|
|
unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST
|
|
|
|
: WASM_OPCODE_I32_CONST;
|
|
|
|
unsigned opcode_ptr_add = is64 ? WASM_OPCODE_I64_ADD
|
|
|
|
: WASM_OPCODE_I32_ADD;
|
2020-10-08 05:48:37 +08:00
|
|
|
|
|
|
|
for (const Symbol *sym : internalGotSymbols) {
|
2021-08-27 03:29:32 +08:00
|
|
|
if (TLS != sym->isTLS())
|
|
|
|
continue;
|
|
|
|
|
2020-10-08 05:48:37 +08:00
|
|
|
if (auto *d = dyn_cast<DefinedData>(sym)) {
|
|
|
|
// Get __memory_base
|
|
|
|
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
|
2021-08-27 03:29:32 +08:00
|
|
|
if (sym->isTLS())
|
|
|
|
writeUleb128(os, WasmSym::tlsBase->getGlobalIndex(), "__tls_base");
|
|
|
|
else
|
|
|
|
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(),
|
|
|
|
"__memory_base");
|
2020-10-08 05:48:37 +08:00
|
|
|
|
|
|
|
// Add the virtual address of the data symbol
|
|
|
|
writeU8(os, opcode_ptr_const, "CONST");
|
2021-02-27 07:22:23 +08:00
|
|
|
writeSleb128(os, d->getVA(), "offset");
|
2020-10-08 05:48:37 +08:00
|
|
|
} else if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
|
2020-11-24 07:41:07 +08:00
|
|
|
if (f->isStub)
|
|
|
|
continue;
|
2020-10-08 05:48:37 +08:00
|
|
|
// Get __table_base
|
|
|
|
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
|
|
|
|
writeUleb128(os, WasmSym::tableBase->getGlobalIndex(), "__table_base");
|
|
|
|
|
|
|
|
// Add the table index to __table_base
|
|
|
|
writeU8(os, opcode_ptr_const, "CONST");
|
|
|
|
writeSleb128(os, f->getTableIndex(), "offset");
|
|
|
|
} else {
|
|
|
|
assert(isa<UndefinedData>(sym));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
writeU8(os, opcode_ptr_add, "ADD");
|
|
|
|
writeU8(os, WASM_OPCODE_GLOBAL_SET, "GLOBAL_SET");
|
|
|
|
writeUleb128(os, sym->getGOTIndex(), "got_entry");
|
|
|
|
}
|
2019-08-14 01:02:02 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
void GlobalSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
writeUleb128(os, numGlobals(), "global count");
|
2021-02-11 19:15:24 +08:00
|
|
|
for (InputGlobal *g : inputGlobals) {
|
|
|
|
writeGlobalType(os, g->getType());
|
|
|
|
writeInitExpr(os, g->getInitExpr());
|
|
|
|
}
|
2021-04-23 07:54:58 +08:00
|
|
|
bool is64 = config->is64.getValueOr(false);
|
|
|
|
uint8_t itype = is64 ? WASM_TYPE_I64 : WASM_TYPE_I32;
|
2020-10-08 05:48:37 +08:00
|
|
|
for (const Symbol *sym : internalGotSymbols) {
|
2021-10-30 00:58:56 +08:00
|
|
|
bool mutable_ = false;
|
|
|
|
if (!sym->isStub) {
|
|
|
|
// In the case of dynamic linking, these global must to be mutable since
|
|
|
|
// they get updated to the correct runtime value during
|
|
|
|
// `__wasm_apply_global_relocs`.
|
|
|
|
if (config->isPic && !sym->isTLS())
|
|
|
|
mutable_ = true;
|
|
|
|
// With multi-theadeding any TLS globals must be mutable since they get
|
|
|
|
// set during `__wasm_apply_global_tls_relocs`
|
|
|
|
if (config->sharedMemory && sym->isTLS())
|
|
|
|
mutable_ = true;
|
|
|
|
}
|
2021-04-23 07:54:58 +08:00
|
|
|
WasmGlobalType type{itype, mutable_};
|
2021-02-11 19:15:24 +08:00
|
|
|
WasmInitExpr initExpr;
|
2019-09-25 04:52:12 +08:00
|
|
|
if (auto *d = dyn_cast<DefinedData>(sym))
|
2021-04-23 07:54:58 +08:00
|
|
|
initExpr = intConst(d->getVA(), is64);
|
2019-10-16 01:05:42 +08:00
|
|
|
else if (auto *f = dyn_cast<FunctionSymbol>(sym))
|
2021-04-23 07:54:58 +08:00
|
|
|
initExpr = intConst(f->isStub ? 0 : f->getTableIndex(), is64);
|
2019-10-16 01:05:42 +08:00
|
|
|
else {
|
|
|
|
assert(isa<UndefinedData>(sym));
|
2021-04-23 07:54:58 +08:00
|
|
|
initExpr = intConst(0, is64);
|
2019-10-16 01:05:42 +08:00
|
|
|
}
|
2021-02-11 19:15:24 +08:00
|
|
|
writeGlobalType(os, type);
|
|
|
|
writeInitExpr(os, initExpr);
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
2019-09-25 04:52:12 +08:00
|
|
|
for (const DefinedData *sym : dataAddressGlobals) {
|
2021-04-23 07:54:58 +08:00
|
|
|
WasmGlobalType type{itype, false};
|
2021-02-11 19:15:24 +08:00
|
|
|
writeGlobalType(os, type);
|
2021-04-23 07:54:58 +08:00
|
|
|
writeInitExpr(os, intConst(sym->getVA(), is64));
|
2019-08-14 01:02:02 +08:00
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSection::addGlobal(InputGlobal *global) {
|
2019-09-25 04:52:12 +08:00
|
|
|
assert(!isSealed);
|
2019-05-21 17:13:09 +08:00
|
|
|
if (!global->live)
|
|
|
|
return;
|
2019-08-14 01:02:02 +08:00
|
|
|
inputGlobals.push_back(global);
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExportSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
|
|
|
|
|
|
|
writeUleb128(os, exports.size(), "export count");
|
|
|
|
for (const WasmExport &export_ : exports)
|
|
|
|
writeExport(os, export_);
|
|
|
|
}
|
|
|
|
|
2019-09-05 03:50:39 +08:00
|
|
|
bool StartSection::isNeeded() const {
|
2020-12-10 10:14:31 +08:00
|
|
|
return WasmSym::startFunction != nullptr;
|
2019-09-05 03:50:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StartSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2020-12-10 10:14:31 +08:00
|
|
|
writeUleb128(os, WasmSym::startFunction->getFunctionIndex(),
|
|
|
|
"function index");
|
2019-09-05 03:50:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
void ElemSection::addEntry(FunctionSymbol *sym) {
|
2020-11-24 07:41:07 +08:00
|
|
|
// Don't add stub functions to the wasm table. The address of all stub
|
|
|
|
// functions should be zero and they should they don't appear in the table.
|
|
|
|
// They only exist so that the calls to missing functions can validate.
|
|
|
|
if (sym->hasTableIndex() || sym->isStub)
|
2019-05-21 17:13:09 +08:00
|
|
|
return;
|
2019-08-27 12:19:34 +08:00
|
|
|
sym->setTableIndex(config->tableBase + indirectFunctions.size());
|
2019-05-21 17:13:09 +08:00
|
|
|
indirectFunctions.emplace_back(sym);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElemSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-03-04 17:33:11 +08:00
|
|
|
assert(WasmSym::indirectFunctionTable);
|
2019-05-21 17:13:09 +08:00
|
|
|
writeUleb128(os, 1, "segment count");
|
2021-03-04 17:33:11 +08:00
|
|
|
uint32_t tableNumber = WasmSym::indirectFunctionTable->getTableNumber();
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (tableNumber)
|
|
|
|
flags |= WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER;
|
|
|
|
writeUleb128(os, flags, "elem segment flags");
|
|
|
|
if (flags & WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER)
|
|
|
|
writeUleb128(os, tableNumber, "table number");
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
WasmInitExpr initExpr;
|
|
|
|
if (config->isPic) {
|
|
|
|
initExpr.Opcode = WASM_OPCODE_GLOBAL_GET;
|
2021-04-23 07:54:58 +08:00
|
|
|
initExpr.Value.Global =
|
|
|
|
(config->is64.getValueOr(false) ? WasmSym::tableBase32
|
|
|
|
: WasmSym::tableBase)
|
|
|
|
->getGlobalIndex();
|
2019-05-21 17:13:09 +08:00
|
|
|
} else {
|
|
|
|
initExpr.Opcode = WASM_OPCODE_I32_CONST;
|
2019-08-27 12:19:34 +08:00
|
|
|
initExpr.Value.Int32 = config->tableBase;
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
writeInitExpr(os, initExpr);
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2021-03-04 17:33:11 +08:00
|
|
|
if (flags & WASM_ELEM_SEGMENT_MASK_HAS_ELEM_KIND) {
|
|
|
|
// We only write active function table initializers, for which the elem kind
|
|
|
|
// is specified to be written as 0x00 and interpreted to mean "funcref".
|
|
|
|
const uint8_t elemKind = 0;
|
|
|
|
writeU8(os, elemKind, "elem kind");
|
|
|
|
}
|
|
|
|
|
|
|
|
writeUleb128(os, indirectFunctions.size(), "elem count");
|
2019-08-27 12:19:34 +08:00
|
|
|
uint32_t tableIndex = config->tableBase;
|
2019-05-21 17:13:09 +08:00
|
|
|
for (const FunctionSymbol *sym : indirectFunctions) {
|
|
|
|
assert(sym->getTableIndex() == tableIndex);
|
|
|
|
writeUleb128(os, sym->getFunctionIndex(), "function index");
|
|
|
|
++tableIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-16 03:05:11 +08:00
|
|
|
DataCountSection::DataCountSection(ArrayRef<OutputSegment *> segments)
|
|
|
|
: SyntheticSection(llvm::wasm::WASM_SEC_DATACOUNT),
|
2021-10-27 09:08:07 +08:00
|
|
|
numSegments(std::count_if(segments.begin(), segments.end(),
|
|
|
|
[](OutputSegment *const segment) {
|
|
|
|
return segment->requiredInBinary();
|
|
|
|
})) {}
|
2019-10-16 03:05:11 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
void DataCountSection::writeBody() {
|
|
|
|
writeUleb128(bodyOutputStream, numSegments, "data count");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataCountSection::isNeeded() const {
|
2019-09-05 03:50:39 +08:00
|
|
|
return numSegments && config->sharedMemory;
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LinkingSection::writeBody() {
|
|
|
|
raw_ostream &os = bodyOutputStream;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
writeUleb128(os, WasmMetadataVersion, "Version");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
if (!symtabEntries.empty()) {
|
|
|
|
SubSection sub(WASM_SYMBOL_TABLE);
|
|
|
|
writeUleb128(sub.os, symtabEntries.size(), "num symbols");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
for (const Symbol *sym : symtabEntries) {
|
|
|
|
assert(sym->isDefined() || sym->isUndefined());
|
|
|
|
WasmSymbolType kind = sym->getWasmType();
|
2020-02-28 09:32:22 +08:00
|
|
|
uint32_t flags = sym->flags;
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
writeU8(sub.os, kind, "sym kind");
|
|
|
|
writeUleb128(sub.os, flags, "sym flags");
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
|
2021-08-19 00:57:34 +08:00
|
|
|
if (auto *d = dyn_cast<DefinedFunction>(sym)) {
|
|
|
|
writeUleb128(sub.os, d->getExportedFunctionIndex(), "index");
|
|
|
|
} else {
|
|
|
|
writeUleb128(sub.os, f->getFunctionIndex(), "index");
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
|
|
|
|
writeStr(sub.os, sym->getName(), "sym name");
|
|
|
|
} else if (auto *g = dyn_cast<GlobalSymbol>(sym)) {
|
|
|
|
writeUleb128(sub.os, g->getGlobalIndex(), "index");
|
|
|
|
if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
|
|
|
|
writeStr(sub.os, sym->getName(), "sym name");
|
2021-06-15 16:49:43 +08:00
|
|
|
} else if (auto *t = dyn_cast<TagSymbol>(sym)) {
|
|
|
|
writeUleb128(sub.os, t->getTagIndex(), "index");
|
2019-05-21 17:13:09 +08:00
|
|
|
if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
|
2021-01-05 19:08:58 +08:00
|
|
|
writeStr(sub.os, sym->getName(), "sym name");
|
|
|
|
} else if (auto *t = dyn_cast<TableSymbol>(sym)) {
|
|
|
|
writeUleb128(sub.os, t->getTableNumber(), "table number");
|
|
|
|
if (sym->isDefined() || (flags & WASM_SYMBOL_EXPLICIT_NAME) != 0)
|
2019-05-21 17:13:09 +08:00
|
|
|
writeStr(sub.os, sym->getName(), "sym name");
|
|
|
|
} else if (isa<DataSymbol>(sym)) {
|
|
|
|
writeStr(sub.os, sym->getName(), "sym name");
|
|
|
|
if (auto *dataSym = dyn_cast<DefinedData>(sym)) {
|
|
|
|
writeUleb128(sub.os, dataSym->getOutputSegmentIndex(), "index");
|
|
|
|
writeUleb128(sub.os, dataSym->getOutputSegmentOffset(),
|
|
|
|
"data offset");
|
|
|
|
writeUleb128(sub.os, dataSym->getSize(), "data size");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
auto *s = cast<OutputSectionSymbol>(sym);
|
|
|
|
writeUleb128(sub.os, s->section->sectionIndex, "sym section index");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dataSegments.size()) {
|
|
|
|
SubSection sub(WASM_SEGMENT_INFO);
|
|
|
|
writeUleb128(sub.os, dataSegments.size(), "num data segments");
|
|
|
|
for (const OutputSegment *s : dataSegments) {
|
|
|
|
writeStr(sub.os, s->name, "segment name");
|
|
|
|
writeUleb128(sub.os, s->alignment, "alignment");
|
2021-02-27 08:09:32 +08:00
|
|
|
writeUleb128(sub.os, s->linkingFlags, "flags");
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!initFunctions.empty()) {
|
|
|
|
SubSection sub(WASM_INIT_FUNCS);
|
|
|
|
writeUleb128(sub.os, initFunctions.size(), "num init functions");
|
|
|
|
for (const WasmInitEntry &f : initFunctions) {
|
|
|
|
writeUleb128(sub.os, f.priority, "priority");
|
|
|
|
writeUleb128(sub.os, f.sym->getOutputSymbolIndex(), "function index");
|
|
|
|
}
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ComdatEntry {
|
|
|
|
unsigned kind;
|
|
|
|
uint32_t index;
|
|
|
|
};
|
|
|
|
std::map<StringRef, std::vector<ComdatEntry>> comdats;
|
|
|
|
|
|
|
|
for (const InputFunction *f : out.functionSec->inputFunctions) {
|
|
|
|
StringRef comdat = f->getComdatName();
|
|
|
|
if (!comdat.empty())
|
|
|
|
comdats[comdat].emplace_back(
|
|
|
|
ComdatEntry{WASM_COMDAT_FUNCTION, f->getFunctionIndex()});
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < dataSegments.size(); ++i) {
|
|
|
|
const auto &inputSegments = dataSegments[i]->inputSegments;
|
|
|
|
if (inputSegments.empty())
|
|
|
|
continue;
|
|
|
|
StringRef comdat = inputSegments[0]->getComdatName();
|
|
|
|
#ifndef NDEBUG
|
2021-05-15 07:25:04 +08:00
|
|
|
for (const InputChunk *isec : inputSegments)
|
2019-05-21 17:13:09 +08:00
|
|
|
assert(isec->getComdatName() == comdat);
|
|
|
|
#endif
|
|
|
|
if (!comdat.empty())
|
|
|
|
comdats[comdat].emplace_back(ComdatEntry{WASM_COMDAT_DATA, i});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!comdats.empty()) {
|
|
|
|
SubSection sub(WASM_COMDAT_INFO);
|
|
|
|
writeUleb128(sub.os, comdats.size(), "num comdats");
|
|
|
|
for (const auto &c : comdats) {
|
|
|
|
writeStr(sub.os, c.first, "comdat name");
|
|
|
|
writeUleb128(sub.os, 0, "comdat flags"); // flags for future use
|
|
|
|
writeUleb128(sub.os, c.second.size(), "num entries");
|
|
|
|
for (const ComdatEntry &entry : c.second) {
|
|
|
|
writeU8(sub.os, entry.kind, "entry kind");
|
|
|
|
writeUleb128(sub.os, entry.index, "entry index");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sub.writeTo(os);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LinkingSection::addToSymtab(Symbol *sym) {
|
|
|
|
sym->setOutputSymbolIndex(symtabEntries.size());
|
|
|
|
symtabEntries.emplace_back(sym);
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:38:23 +08:00
|
|
|
unsigned NameSection::numNamedFunctions() const {
|
2019-07-10 17:10:01 +08:00
|
|
|
unsigned numNames = out.importSec->getNumImportedFunctions();
|
2020-11-19 13:38:23 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
for (const InputFunction *f : out.functionSec->inputFunctions)
|
|
|
|
if (!f->getName().empty() || !f->getDebugName().empty())
|
|
|
|
++numNames;
|
|
|
|
|
|
|
|
return numNames;
|
|
|
|
}
|
|
|
|
|
2020-11-19 13:38:23 +08:00
|
|
|
unsigned NameSection::numNamedGlobals() const {
|
|
|
|
unsigned numNames = out.importSec->getNumImportedGlobals();
|
|
|
|
|
|
|
|
for (const InputGlobal *g : out.globalSec->inputGlobals)
|
|
|
|
if (!g->getName().empty())
|
|
|
|
++numNames;
|
|
|
|
|
|
|
|
numNames += out.globalSec->internalGotSymbols.size();
|
|
|
|
return numNames;
|
|
|
|
}
|
|
|
|
|
2020-12-09 13:47:19 +08:00
|
|
|
unsigned NameSection::numNamedDataSegments() const {
|
|
|
|
unsigned numNames = 0;
|
|
|
|
|
|
|
|
for (const OutputSegment *s : segments)
|
2021-10-27 09:08:07 +08:00
|
|
|
if (!s->name.empty() && s->requiredInBinary())
|
2020-12-09 13:47:19 +08:00
|
|
|
++numNames;
|
|
|
|
|
|
|
|
return numNames;
|
|
|
|
}
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
// Create the custom "name" section containing debug symbol names.
|
|
|
|
void NameSection::writeBody() {
|
2020-11-19 13:38:23 +08:00
|
|
|
unsigned count = numNamedFunctions();
|
|
|
|
if (count) {
|
|
|
|
SubSection sub(WASM_NAMES_FUNCTION);
|
|
|
|
writeUleb128(sub.os, count, "name count");
|
|
|
|
|
|
|
|
// Function names appear in function index order. As it happens
|
|
|
|
// importedSymbols and inputFunctions are numbered in order with imported
|
|
|
|
// functions coming first.
|
|
|
|
for (const Symbol *s : out.importSec->importedSymbols) {
|
|
|
|
if (auto *f = dyn_cast<FunctionSymbol>(s)) {
|
|
|
|
writeUleb128(sub.os, f->getFunctionIndex(), "func index");
|
|
|
|
writeStr(sub.os, toString(*s), "symbol name");
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
2020-11-19 13:38:23 +08:00
|
|
|
for (const InputFunction *f : out.functionSec->inputFunctions) {
|
|
|
|
if (!f->getName().empty()) {
|
|
|
|
writeUleb128(sub.os, f->getFunctionIndex(), "func index");
|
|
|
|
if (!f->getDebugName().empty()) {
|
|
|
|
writeStr(sub.os, f->getDebugName(), "symbol name");
|
|
|
|
} else {
|
|
|
|
writeStr(sub.os, maybeDemangleSymbol(f->getName()), "symbol name");
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
2020-11-19 13:38:23 +08:00
|
|
|
sub.writeTo(bodyOutputStream);
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 13:38:23 +08:00
|
|
|
count = numNamedGlobals();
|
|
|
|
if (count) {
|
|
|
|
SubSection sub(WASM_NAMES_GLOBAL);
|
|
|
|
writeUleb128(sub.os, count, "name count");
|
|
|
|
|
|
|
|
for (const Symbol *s : out.importSec->importedSymbols) {
|
|
|
|
if (auto *g = dyn_cast<GlobalSymbol>(s)) {
|
|
|
|
writeUleb128(sub.os, g->getGlobalIndex(), "global index");
|
|
|
|
writeStr(sub.os, toString(*s), "symbol name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const Symbol *s : out.importSec->gotSymbols) {
|
|
|
|
writeUleb128(sub.os, s->getGOTIndex(), "global index");
|
|
|
|
writeStr(sub.os, toString(*s), "symbol name");
|
|
|
|
}
|
|
|
|
for (const InputGlobal *g : out.globalSec->inputGlobals) {
|
|
|
|
if (!g->getName().empty()) {
|
2021-02-11 19:15:24 +08:00
|
|
|
writeUleb128(sub.os, g->getAssignedIndex(), "global index");
|
2020-11-19 13:38:23 +08:00
|
|
|
writeStr(sub.os, maybeDemangleSymbol(g->getName()), "symbol name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Symbol *s : out.globalSec->internalGotSymbols) {
|
|
|
|
writeUleb128(sub.os, s->getGOTIndex(), "global index");
|
2020-12-10 10:14:31 +08:00
|
|
|
if (isa<FunctionSymbol>(s))
|
|
|
|
writeStr(sub.os, "GOT.func.internal." + toString(*s), "symbol name");
|
|
|
|
else
|
|
|
|
writeStr(sub.os, "GOT.data.internal." + toString(*s), "symbol name");
|
2020-11-19 13:38:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub.writeTo(bodyOutputStream);
|
|
|
|
}
|
2020-12-09 13:47:19 +08:00
|
|
|
|
|
|
|
count = numNamedDataSegments();
|
|
|
|
if (count) {
|
|
|
|
SubSection sub(WASM_NAMES_DATA_SEGMENT);
|
|
|
|
writeUleb128(sub.os, count, "name count");
|
|
|
|
|
|
|
|
for (OutputSegment *s : segments) {
|
2021-10-27 09:08:07 +08:00
|
|
|
if (!s->name.empty() && s->requiredInBinary()) {
|
2020-12-10 12:46:37 +08:00
|
|
|
writeUleb128(sub.os, s->index, "global index");
|
|
|
|
writeStr(sub.os, s->name, "segment name");
|
|
|
|
}
|
2020-12-09 13:47:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub.writeTo(bodyOutputStream);
|
|
|
|
}
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProducersSection::addInfo(const WasmProducerInfo &info) {
|
|
|
|
for (auto &producers :
|
|
|
|
{std::make_pair(&info.Languages, &languages),
|
|
|
|
std::make_pair(&info.Tools, &tools), std::make_pair(&info.SDKs, &sDKs)})
|
|
|
|
for (auto &producer : *producers.first)
|
|
|
|
if (producers.second->end() ==
|
|
|
|
llvm::find_if(*producers.second,
|
|
|
|
[&](std::pair<std::string, std::string> seen) {
|
|
|
|
return seen.first == producer.first;
|
|
|
|
}))
|
|
|
|
producers.second->push_back(producer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProducersSection::writeBody() {
|
|
|
|
auto &os = bodyOutputStream;
|
|
|
|
writeUleb128(os, fieldCount(), "field count");
|
|
|
|
for (auto &field :
|
|
|
|
{std::make_pair("language", languages),
|
|
|
|
std::make_pair("processed-by", tools), std::make_pair("sdk", sDKs)}) {
|
|
|
|
if (field.second.empty())
|
|
|
|
continue;
|
|
|
|
writeStr(os, field.first, "field name");
|
|
|
|
writeUleb128(os, field.second.size(), "number of entries");
|
|
|
|
for (auto &entry : field.second) {
|
|
|
|
writeStr(os, entry.first, "producer name");
|
|
|
|
writeStr(os, entry.second, "producer version");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TargetFeaturesSection::writeBody() {
|
|
|
|
SmallVector<std::string, 8> emitted(features.begin(), features.end());
|
|
|
|
llvm::sort(emitted);
|
|
|
|
auto &os = bodyOutputStream;
|
|
|
|
writeUleb128(os, emitted.size(), "feature count");
|
|
|
|
for (auto &feature : emitted) {
|
|
|
|
writeU8(os, WASM_FEATURE_PREFIX_USED, "feature used prefix");
|
|
|
|
writeStr(os, feature, "feature name");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RelocSection::writeBody() {
|
2019-07-10 17:10:01 +08:00
|
|
|
uint32_t count = sec->getNumRelocations();
|
2019-05-21 17:13:09 +08:00
|
|
|
assert(sec->sectionIndex != UINT32_MAX);
|
|
|
|
writeUleb128(bodyOutputStream, sec->sectionIndex, "reloc section");
|
|
|
|
writeUleb128(bodyOutputStream, count, "reloc count");
|
|
|
|
sec->writeRelocations(bodyOutputStream);
|
|
|
|
}
|
2019-10-10 13:25:39 +08:00
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|