Skip to content

Commit 14ffbb8

Browse files
committed
[lld][WebAssembly] Rename methods/members to match ELF backend. NFC.
Specifically: - InputChunk::outputOffset -> outSecOffset - Symbol::get/setVirtualAddress -> get/setVA - add InputChunk::getOffset helper that takes an offset These are mostly in preparation for adding support for SHF_MERGE/SHF_STRINGS but its also good to align with ELF where possible. Differential Revision: https://reviews.llvm.org/D97595
1 parent 1ab2753 commit 14ffbb8

File tree

10 files changed

+53
-51
lines changed

10 files changed

+53
-51
lines changed

lld/wasm/InputChunks.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void InputChunk::verifyRelocTargets() const {
122122
// Copy this input chunk to an mmap'ed output file and apply relocations.
123123
void InputChunk::writeTo(uint8_t *buf) const {
124124
// Copy contents
125-
memcpy(buf + outputOffset, data().data(), data().size());
125+
memcpy(buf + outSecOff, data().data(), data().size());
126126

127127
// Apply relocations
128128
if (relocations.empty())
@@ -134,7 +134,7 @@ void InputChunk::writeTo(uint8_t *buf) const {
134134

135135
LLVM_DEBUG(dbgs() << "applying relocations: " << toString(this)
136136
<< " count=" << relocations.size() << "\n");
137-
int32_t off = outputOffset - getInputSectionOffset();
137+
int32_t off = outSecOff - getInputSectionOffset();
138138
auto tombstone = getTombstone();
139139

140140
for (const WasmRelocation &rel : relocations) {
@@ -196,7 +196,7 @@ void InputChunk::writeRelocations(raw_ostream &os) const {
196196
if (relocations.empty())
197197
return;
198198

199-
int32_t off = outputOffset - getInputSectionOffset();
199+
int32_t off = outSecOff - getInputSectionOffset();
200200
LLVM_DEBUG(dbgs() << "writeRelocations: " << file->getName()
201201
<< " offset=" << Twine(off) << "\n");
202202

@@ -323,7 +323,7 @@ void InputFunction::writeTo(uint8_t *buf) const {
323323
if (!file || !config->compressRelocations)
324324
return InputChunk::writeTo(buf);
325325

326-
buf += outputOffset;
326+
buf += outSecOff;
327327
uint8_t *orig = buf;
328328
(void)orig;
329329

@@ -353,8 +353,8 @@ void InputFunction::writeTo(uint8_t *buf) const {
353353
LLVM_DEBUG(dbgs() << " total: " << (buf + chunkSize - orig) << "\n");
354354
}
355355

356-
uint64_t InputSegment::getVA() const {
357-
return outputSeg->startVA + outputSegmentOffset;
356+
uint64_t InputSegment::getVA(uint64_t offset) const {
357+
return outputSeg->startVA + outputSegmentOffset + offset;
358358
}
359359

360360
// Generate code to apply relocations to the data section at runtime.
@@ -375,20 +375,19 @@ void InputSegment::generateRelocationCode(raw_ostream &os) const {
375375
// TODO(sbc): Encode the relocations in the data section and write a loop
376376
// here to apply them.
377377
for (const WasmRelocation &rel : relocations) {
378-
uint64_t offset = rel.Offset - getInputSectionOffset();
379-
uint64_t outputOffset = getVA() + offset;
378+
uint64_t offset = getVA(rel.Offset) - getInputSectionOffset();
380379

381380
LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(rel.Type)
382381
<< " addend=" << rel.Addend << " index=" << rel.Index
383-
<< " output offset=" << outputOffset << "\n");
382+
<< " output offset=" << offset << "\n");
384383

385384
// Get __memory_base
386385
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
387386
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base");
388387

389388
// Add the offset of the relocation
390389
writeU8(os, opcode_ptr_const, "CONST");
391-
writeSleb128(os, outputOffset, "offset");
390+
writeSleb128(os, offset, "offset");
392391
writeU8(os, opcode_ptr_add, "ADD");
393392

394393
bool is64 = relocIs64(rel.Type);

lld/wasm/InputChunks.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class InputChunk {
4747
ArrayRef<WasmRelocation> getRelocations() const { return relocations; }
4848
void setRelocations(ArrayRef<WasmRelocation> rs) { relocations = rs; }
4949

50+
uint64_t getOffset(uint64_t offset) const { return outSecOff + offset; }
5051
virtual StringRef getName() const = 0;
5152
virtual StringRef getDebugName() const = 0;
5253
virtual uint32_t getComdat() const = 0;
@@ -58,8 +59,10 @@ class InputChunk {
5859

5960
ObjFile *file;
6061
OutputSection *outputSec = nullptr;
61-
// Offset withing the output section
62-
int32_t outputOffset = 0;
62+
63+
// After assignAddresses is called, this represents the offset from
64+
// the beginning of the output section this chunk was assigned to.
65+
int32_t outSecOff = 0;
6366

6467
// Signals that the section is part of the output. The garbage collector,
6568
// and COMDAT handling can set a sections' Live bit.
@@ -108,7 +111,7 @@ class InputSegment : public InputChunk {
108111
uint32_t getInputSectionOffset() const override {
109112
return segment.SectionOffset;
110113
}
111-
uint64_t getVA() const;
114+
uint64_t getVA(uint64_t offset = 0) const;
112115

113116
const OutputSegment *outputSeg = nullptr;
114117
int32_t outputSegmentOffset = 0;

lld/wasm/InputFiles.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ uint64_t ObjFile::calcNewAddend(const WasmRelocation &reloc) const {
128128
case R_WASM_FUNCTION_OFFSET_I64:
129129
return reloc.Addend;
130130
case R_WASM_SECTION_OFFSET_I32:
131-
return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend;
131+
return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend);
132132
default:
133133
llvm_unreachable("unexpected relocation type");
134134
}
@@ -245,7 +245,7 @@ uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, uint64_t tombstone)
245245
// backward compat with old object files built with `-fPIC`.
246246
if (D->segment && D->segment->outputSeg->name == ".tdata")
247247
return D->getOutputSegmentOffset() + reloc.Addend;
248-
return D->getVirtualAddress() + reloc.Addend;
248+
return D->getVA(reloc.Addend);
249249
}
250250
case R_WASM_MEMORY_ADDR_TLS_SLEB:
251251
if (isa<UndefinedData>(sym) || sym->isUndefWeak())
@@ -266,11 +266,11 @@ uint64_t ObjFile::calcNewValue(const WasmRelocation &reloc, uint64_t tombstone)
266266
case R_WASM_FUNCTION_OFFSET_I32:
267267
case R_WASM_FUNCTION_OFFSET_I64: {
268268
auto *f = cast<DefinedFunction>(sym);
269-
return f->function->outputOffset +
270-
(f->function->getFunctionCodeOffset() + reloc.Addend);
269+
return f->function->getOffset(f->function->getFunctionCodeOffset() +
270+
reloc.Addend);
271271
}
272272
case R_WASM_SECTION_OFFSET_I32:
273-
return getSectionSymbol(reloc.Index)->section->outputOffset + reloc.Addend;
273+
return getSectionSymbol(reloc.Index)->section->getOffset(reloc.Addend);
274274
case R_WASM_TABLE_NUMBER_LEB:
275275
return getTableSymbol(reloc.Index)->getTableNumber();
276276
default:

lld/wasm/MapFile.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,13 @@ getSymbolStrings(ArrayRef<Symbol *> syms) {
8080
auto *chunk = syms[i]->getChunk();
8181
if (chunk == nullptr)
8282
return;
83-
uint64_t fileOffset = chunk->outputSec->getOffset() + chunk->outputOffset;
83+
uint64_t fileOffset = chunk->outputSec->getOffset() + chunk->outSecOff;
8484
uint64_t vma = -1;
8585
uint64_t size = 0;
8686
if (auto *DD = dyn_cast<DefinedData>(syms[i])) {
87-
vma = DD->getVirtualAddress();
87+
vma = DD->getVA();
8888
size = DD->getSize();
89-
fileOffset += DD->offset;
89+
fileOffset += DD->value;
9090
}
9191
if (auto *DF = dyn_cast<DefinedFunction>(syms[i])) {
9292
size = DF->function->getSize();
@@ -126,7 +126,7 @@ void lld::wasm::writeMapFile(ArrayRef<OutputSection *> outputSections) {
126126
os << toString(*osec) << '\n';
127127
if (auto *code = dyn_cast<CodeSection>(osec)) {
128128
for (auto *chunk : code->functions) {
129-
writeHeader(os, -1, chunk->outputSec->getOffset() + chunk->outputOffset,
129+
writeHeader(os, -1, chunk->outputSec->getOffset() + chunk->outSecOff,
130130
chunk->getSize());
131131
os.indent(8) << toString(chunk) << '\n';
132132
for (Symbol *sym : sectionSyms[chunk])
@@ -139,7 +139,7 @@ void lld::wasm::writeMapFile(ArrayRef<OutputSection *> outputSections) {
139139
os << oseg->name << '\n';
140140
for (auto *chunk : oseg->inputSegments) {
141141
writeHeader(os, chunk->getVA(),
142-
chunk->outputSec->getOffset() + chunk->outputOffset,
142+
chunk->outputSec->getOffset() + chunk->outSecOff,
143143
chunk->getSize());
144144
os.indent(8) << toString(chunk) << '\n';
145145
for (Symbol *sym : sectionSyms[chunk])

lld/wasm/OutputSections.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void CodeSection::finalizeContents() {
8888

8989
for (InputFunction *func : functions) {
9090
func->outputSec = this;
91-
func->outputOffset = bodySize;
91+
func->outSecOff = bodySize;
9292
func->calculateSize();
9393
// All functions should have a non-empty body at this point
9494
assert(func->getSize());
@@ -180,8 +180,8 @@ void DataSection::finalizeContents() {
180180

181181
for (InputSegment *inputSeg : segment->inputSegments) {
182182
inputSeg->outputSec = this;
183-
inputSeg->outputOffset = segment->sectionOffset + segment->header.size() +
184-
inputSeg->outputSegmentOffset;
183+
inputSeg->outSecOff = segment->sectionOffset + segment->header.size() +
184+
inputSeg->outputSegmentOffset;
185185
}
186186
}
187187

@@ -243,7 +243,7 @@ void CustomSection::finalizeContents() {
243243
for (InputSection *section : inputSections) {
244244
assert(!section->discarded);
245245
section->outputSec = this;
246-
section->outputOffset = payloadSize;
246+
section->outSecOff = payloadSize;
247247
payloadSize += section->getSize();
248248
}
249249

lld/wasm/SymbolTable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ DefinedData *SymbolTable::addOptionalDataSymbol(StringRef name,
234234
return nullptr;
235235
LLVM_DEBUG(dbgs() << "addOptionalDataSymbol: " << name << "\n");
236236
auto *rtn = replaceSymbol<DefinedData>(s, name, WASM_SYMBOL_VISIBILITY_HIDDEN);
237-
rtn->setVirtualAddress(value);
237+
rtn->setVA(value);
238238
rtn->referenced = true;
239239
return rtn;
240240
}

lld/wasm/Symbols.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -276,22 +276,22 @@ DefinedFunction::DefinedFunction(StringRef name, uint32_t flags, InputFile *f,
276276
function ? &function->signature : nullptr),
277277
function(function) {}
278278

279-
uint64_t DefinedData::getVirtualAddress() const {
280-
LLVM_DEBUG(dbgs() << "getVirtualAddress: " << getName() << "\n");
279+
uint64_t DefinedData::getVA(uint64_t addend) const {
280+
LLVM_DEBUG(dbgs() << "getVA: " << getName() << "\n");
281281
if (segment)
282-
return segment->getVA() + offset;
283-
return offset;
282+
return segment->getVA(value + addend);
283+
return value;
284284
}
285285

286-
void DefinedData::setVirtualAddress(uint64_t value) {
287-
LLVM_DEBUG(dbgs() << "setVirtualAddress " << name << " -> " << value << "\n");
286+
void DefinedData::setVA(uint64_t value_) {
287+
LLVM_DEBUG(dbgs() << "setVA " << name << " -> " << value_ << "\n");
288288
assert(!segment);
289-
offset = value;
289+
value = value_;
290290
}
291291

292292
uint64_t DefinedData::getOutputSegmentOffset() const {
293293
LLVM_DEBUG(dbgs() << "getOutputSegmentOffset: " << getName() << "\n");
294-
return segment->outputSegmentOffset + offset;
294+
return segment->outputSegmentOffset + value;
295295
}
296296

297297
uint64_t DefinedData::getOutputSegmentIndex() const {

lld/wasm/Symbols.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,9 @@ class DefinedData : public DataSymbol {
278278
public:
279279
// Constructor for regular data symbols originating from input files.
280280
DefinedData(StringRef name, uint32_t flags, InputFile *f,
281-
InputSegment *segment, uint64_t offset, uint64_t size)
281+
InputSegment *segment, uint64_t value, uint64_t size)
282282
: DataSymbol(name, DefinedDataKind, flags, f), segment(segment),
283-
offset(offset), size(size) {}
283+
value(value), size(size) {}
284284

285285
// Constructor for linker synthetic data symbols.
286286
DefinedData(StringRef name, uint32_t flags)
@@ -289,16 +289,16 @@ class DefinedData : public DataSymbol {
289289
static bool classof(const Symbol *s) { return s->kind() == DefinedDataKind; }
290290

291291
// Returns the output virtual address of a defined data symbol.
292-
uint64_t getVirtualAddress() const;
293-
void setVirtualAddress(uint64_t va);
292+
uint64_t getVA(uint64_t addend = 0) const;
293+
void setVA(uint64_t va);
294294

295295
// Returns the offset of a defined data symbol within its OutputSegment.
296296
uint64_t getOutputSegmentOffset() const;
297297
uint64_t getOutputSegmentIndex() const;
298298
uint64_t getSize() const { return size; }
299299

300300
InputSegment *segment = nullptr;
301-
uint32_t offset = 0;
301+
uint64_t value = 0;
302302

303303
protected:
304304
uint64_t size = 0;

lld/wasm/SyntheticSections.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ void GlobalSection::generateRelocationCode(raw_ostream &os) const {
324324

325325
// Add the virtual address of the data symbol
326326
writeU8(os, opcode_ptr_const, "CONST");
327-
writeSleb128(os, d->getVirtualAddress(), "offset");
327+
writeSleb128(os, d->getVA(), "offset");
328328
} else if (auto *f = dyn_cast<FunctionSymbol>(sym)) {
329329
if (f->isStub)
330330
continue;
@@ -363,7 +363,7 @@ void GlobalSection::writeBody() {
363363
WasmInitExpr initExpr;
364364
initExpr.Opcode = WASM_OPCODE_I32_CONST;
365365
if (auto *d = dyn_cast<DefinedData>(sym))
366-
initExpr.Value.Int32 = d->getVirtualAddress();
366+
initExpr.Value.Int32 = d->getVA();
367367
else if (auto *f = dyn_cast<FunctionSymbol>(sym))
368368
initExpr.Value.Int32 = f->isStub ? 0 : f->getTableIndex();
369369
else {
@@ -377,7 +377,7 @@ void GlobalSection::writeBody() {
377377
WasmGlobalType type{WASM_TYPE_I32, false};
378378
WasmInitExpr initExpr;
379379
initExpr.Opcode = WASM_OPCODE_I32_CONST;
380-
initExpr.Value.Int32 = sym->getVirtualAddress();
380+
initExpr.Value.Int32 = sym->getVA();
381381
writeGlobalType(os, type);
382382
writeInitExpr(os, initExpr);
383383
}

lld/wasm/Writer.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,14 +252,14 @@ void Writer::layoutMemory() {
252252
}
253253

254254
if (WasmSym::globalBase)
255-
WasmSym::globalBase->setVirtualAddress(memoryPtr);
255+
WasmSym::globalBase->setVA(memoryPtr);
256256

257257
uint64_t dataStart = memoryPtr;
258258

259259
// Arbitrarily set __dso_handle handle to point to the start of the data
260260
// segments.
261261
if (WasmSym::dsoHandle)
262-
WasmSym::dsoHandle->setVirtualAddress(dataStart);
262+
WasmSym::dsoHandle->setVA(dataStart);
263263

264264
out.dylinkSec->memAlign = 0;
265265
for (OutputSegment *seg : segments) {
@@ -291,14 +291,14 @@ void Writer::layoutMemory() {
291291
WasmSym::initMemoryFlag = symtab->addSyntheticDataSymbol(
292292
"__wasm_init_memory_flag", WASM_SYMBOL_VISIBILITY_HIDDEN);
293293
WasmSym::initMemoryFlag->markLive();
294-
WasmSym::initMemoryFlag->setVirtualAddress(memoryPtr);
294+
WasmSym::initMemoryFlag->setVA(memoryPtr);
295295
log(formatv("mem: {0,-15} offset={1,-8} size={2,-8} align={3}",
296296
"__wasm_init_memory_flag", memoryPtr, 4, 4));
297297
memoryPtr += 4;
298298
}
299299

300300
if (WasmSym::dataEnd)
301-
WasmSym::dataEnd->setVirtualAddress(memoryPtr);
301+
WasmSym::dataEnd->setVA(memoryPtr);
302302

303303
uint64_t staticDataSize = memoryPtr - dataStart;
304304
log("mem: static data = " + Twine(staticDataSize));
@@ -313,7 +313,7 @@ void Writer::layoutMemory() {
313313
// The fact that this comes last means that a malloc/brk implementation
314314
// can grow the heap at runtime.
315315
log("mem: heap base = " + Twine(memoryPtr));
316-
WasmSym::heapBase->setVirtualAddress(memoryPtr);
316+
WasmSym::heapBase->setVA(memoryPtr);
317317
}
318318

319319
uint64_t maxMemorySetting = 1ULL
@@ -980,7 +980,7 @@ void Writer::createInitMemoryFunction() {
980980
assert(WasmSym::initMemory);
981981
assert(WasmSym::initMemoryFlag);
982982
assert(hasPassiveInitializedSegments());
983-
uint64_t flagAddress = WasmSym::initMemoryFlag->getVirtualAddress();
983+
uint64_t flagAddress = WasmSym::initMemoryFlag->getVA();
984984
bool is64 = config->is64.getValueOr(false);
985985
std::string bodyContent;
986986
{
@@ -1369,7 +1369,7 @@ void Writer::run() {
13691369
if (!config->isPic) {
13701370
config->tableBase = 1;
13711371
if (WasmSym::definedTableBase)
1372-
WasmSym::definedTableBase->setVirtualAddress(config->tableBase);
1372+
WasmSym::definedTableBase->setVA(config->tableBase);
13731373
}
13741374

13751375
log("-- createOutputSegments");

0 commit comments

Comments
 (0)