summaryrefslogtreecommitdiff
path: root/lib/MC/WasmObjectWriter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/MC/WasmObjectWriter.cpp')
-rw-r--r--lib/MC/WasmObjectWriter.cpp27
1 files changed, 16 insertions, 11 deletions
diff --git a/lib/MC/WasmObjectWriter.cpp b/lib/MC/WasmObjectWriter.cpp
index 6444046a30d7..0540c4c47a3f 100644
--- a/lib/MC/WasmObjectWriter.cpp
+++ b/lib/MC/WasmObjectWriter.cpp
@@ -468,16 +468,16 @@ static void ApplyRelocations(
// Write out the portions of the relocation records that the linker will
// need to handle.
-static void WriteRelocations(
- ArrayRef<WasmRelocationEntry> Relocations,
- raw_pwrite_stream &Stream,
- DenseMap<const MCSymbolWasm *, uint32_t> &SymbolIndices)
-{
+static void
+WriteRelocations(ArrayRef<WasmRelocationEntry> Relocations,
+ raw_pwrite_stream &Stream,
+ DenseMap<const MCSymbolWasm *, uint32_t> &SymbolIndices,
+ uint64_t HeaderSize) {
for (const WasmRelocationEntry RelEntry : Relocations) {
encodeULEB128(RelEntry.Type, Stream);
uint64_t Offset = RelEntry.Offset +
- RelEntry.FixupSection->getSectionOffset();
+ RelEntry.FixupSection->getSectionOffset() + HeaderSize;
uint32_t Index = SymbolIndices[RelEntry.Symbol];
int64_t Addend = RelEntry.Addend;
@@ -913,12 +913,14 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
// For now, always emit the memory section, since loads and stores are not
// valid without it. In the future, we could perhaps be more clever and omit
// it if there are no loads or stores.
- startSection(Section, wasm::WASM_SEC_MEMORY);
+ uint32_t NumPages =
+ (DataBytes.size() + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
+ startSection(Section, wasm::WASM_SEC_MEMORY);
encodeULEB128(1, getStream()); // number of memory spaces
encodeULEB128(0, getStream()); // flags
- encodeULEB128(DataBytes.size(), getStream()); // initial
+ encodeULEB128(NumPages, getStream()); // initial
endSection(Section);
@@ -1050,6 +1052,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
}
// === Data Section ==========================================================
+ uint32_t DataSectionHeaderSize = 0;
if (!DataBytes.empty()) {
startSection(Section, wasm::WASM_SEC_DATA);
@@ -1059,11 +1062,12 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
encodeSLEB128(0, getStream()); // offset
write8(wasm::WASM_OPCODE_END);
encodeULEB128(DataBytes.size(), getStream()); // size
+ DataSectionHeaderSize = getStream().tell() - Section.ContentsOffset;
writeBytes(DataBytes); // data
// Apply fixups.
ApplyRelocations(DataRelocations, getStream(), SymbolIndices,
- Section.ContentsOffset);
+ Section.ContentsOffset + DataSectionHeaderSize);
endSection(Section);
}
@@ -1107,7 +1111,7 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
encodeULEB128(CodeRelocations.size() + TypeIndexFixups.size(), getStream());
- WriteRelocations(CodeRelocations, getStream(), SymbolIndices);
+ WriteRelocations(CodeRelocations, getStream(), SymbolIndices, 0);
WriteTypeRelocations(TypeIndexFixups, TypeIndexFixupTypes, getStream());
endSection(Section);
@@ -1121,7 +1125,8 @@ void WasmObjectWriter::writeObject(MCAssembler &Asm,
encodeULEB128(DataRelocations.size(), getStream());
- WriteRelocations(DataRelocations, getStream(), SymbolIndices);
+ WriteRelocations(DataRelocations, getStream(), SymbolIndices,
+ DataSectionHeaderSize);
endSection(Section);
}