[lld][WebAssembly] Store table base in config rather than passing it around. NFC.

I've got another change that makes more use of this value in other
places.

Differential Revision: https://reviews.llvm.org/D66777

llvm-svn: 370010
This commit is contained in:
Sam Clegg 2019-08-27 04:19:34 +00:00
parent 4797f68b15
commit 1a1df72a43
4 changed files with 16 additions and 12 deletions

View File

@ -70,6 +70,12 @@ struct Configuration {
// True if we are creating position-independent code.
bool isPic;
// The table offset at which to place function addresses. We reserve zero
// for the null function pointer. This gets set to 1 for exectuables and 0
// for shared libraries (since they always added to a dynamic offset at
// runtime).
uint32_t tableBase = 0;
};
// The only instance of Configuration struct.

View File

@ -143,7 +143,7 @@ void ImportSection::writeBody() {
}
if (config->importTable) {
uint32_t tableSize = out.elemSec->elemOffset + out.elemSec->numEntries();
uint32_t tableSize = config->tableBase + out.elemSec->numEntries();
WasmImport import;
import.Module = defaultModule;
import.Field = functionTableName;
@ -212,7 +212,7 @@ void FunctionSection::addFunction(InputFunction *func) {
}
void TableSection::writeBody() {
uint32_t tableSize = out.elemSec->elemOffset + out.elemSec->numEntries();
uint32_t tableSize = config->tableBase + out.elemSec->numEntries();
raw_ostream &os = bodyOutputStream;
writeUleb128(os, 1, "table count");
@ -313,7 +313,7 @@ void ExportSection::writeBody() {
void ElemSection::addEntry(FunctionSymbol *sym) {
if (sym->hasTableIndex())
return;
sym->setTableIndex(elemOffset + indirectFunctions.size());
sym->setTableIndex(config->tableBase + indirectFunctions.size());
indirectFunctions.emplace_back(sym);
}
@ -328,12 +328,12 @@ void ElemSection::writeBody() {
initExpr.Value.Global = WasmSym::tableBase->getGlobalIndex();
} else {
initExpr.Opcode = WASM_OPCODE_I32_CONST;
initExpr.Value.Int32 = elemOffset;
initExpr.Value.Int32 = config->tableBase;
}
writeInitExpr(os, initExpr);
writeUleb128(os, indirectFunctions.size(), "elem count");
uint32_t tableIndex = elemOffset;
uint32_t tableIndex = config->tableBase;
for (const FunctionSymbol *sym : indirectFunctions) {
assert(sym->getTableIndex() == tableIndex);
writeUleb128(os, sym->getFunctionIndex(), "function index");

View File

@ -219,13 +219,12 @@ public:
class ElemSection : public SyntheticSection {
public:
ElemSection(uint32_t offset)
: SyntheticSection(llvm::wasm::WASM_SEC_ELEM), elemOffset(offset) {}
ElemSection()
: SyntheticSection(llvm::wasm::WASM_SEC_ELEM) {}
bool isNeeded() const override { return indirectFunctions.size() > 0; };
void writeBody() override;
void addEntry(FunctionSymbol *sym);
uint32_t numEntries() const { return indirectFunctions.size(); }
uint32_t elemOffset;
protected:
std::vector<const FunctionSymbol *> indirectFunctions;

View File

@ -87,7 +87,6 @@ private:
void writeSections();
uint64_t fileSize = 0;
uint32_t tableBase = 0;
std::vector<WasmInitEntry> initFunctions;
llvm::StringMap<std::vector<InputSection *>> customSectionMapping;
@ -852,7 +851,7 @@ void Writer::createSyntheticSections() {
out.globalSec = make<GlobalSection>();
out.eventSec = make<EventSection>();
out.exportSec = make<ExportSection>();
out.elemSec = make<ElemSection>(tableBase);
out.elemSec = make<ElemSection>();
out.dataCountSec = make<DataCountSection>(segments.size());
out.linkingSec = make<LinkingSection>(initFunctions, segments);
out.nameSec = make<NameSection>();
@ -867,9 +866,9 @@ void Writer::run() {
// For PIC code the table base is assigned dynamically by the loader.
// For non-PIC, we start at 1 so that accessing table index 0 always traps.
if (!config->isPic) {
tableBase = 1;
config->tableBase = 1;
if (WasmSym::definedTableBase)
WasmSym::definedTableBase->setVirtualAddress(tableBase);
WasmSym::definedTableBase->setVirtualAddress(config->tableBase);
}
log("-- createOutputSegments");