16#include "llvm/Config/llvm-config.h"
35#define DEBUG_TYPE "mc"
41static const uint32_t InitialTableOffset = 1;
46struct SectionBookkeeping {
60struct WasmDataSegment {
85struct WasmComdatEntry {
91struct WasmRelocationEntry {
99 int64_t Addend,
unsigned Type,
102 FixupSection(FixupSection) {}
108 <<
", Sym=" << *Symbol <<
", Addend=" << Addend
109 <<
", FixupSection=" << FixupSection->
getName();
112#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
119struct WasmCustomSection {
128 : Name(Name), Section(Section) {}
140template <
typename T,
int W>
150template <
typename T,
int W>
160 writePatchableULEB<uint32_t, 5>(Stream,
Value,
Offset);
165 writePatchableSLEB<int32_t, 5>(Stream,
Value,
Offset);
170 writePatchableSLEB<uint64_t, 10>(Stream,
Value,
Offset);
175 writePatchableSLEB<int64_t, 10>(Stream,
Value,
Offset);
201 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
204 std::vector<WasmRelocationEntry> CodeRelocations;
206 std::vector<WasmRelocationEntry> DataRelocations;
223 std::vector<WasmCustomSection> CustomSections;
224 std::unique_ptr<WasmCustomSection> ProducersSection;
225 std::unique_ptr<WasmCustomSection> TargetFeaturesSection;
228 CustomSectionsRelocations;
236 unsigned NumFunctionImports = 0;
237 unsigned NumGlobalImports = 0;
238 unsigned NumTableImports = 0;
239 unsigned NumTagImports = 0;
247 bool IsSplitDwarf =
false;
252 bool is64Bit()
const {
return TargetObjectWriter->is64Bit(); }
253 bool isEmscripten()
const {
return TargetObjectWriter->isEmscripten(); }
255 void startSection(SectionBookkeeping &Section,
unsigned SectionId);
256 void startCustomSection(SectionBookkeeping &Section,
StringRef Name);
257 void endSection(SectionBookkeeping &Section);
260 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
262 : TargetObjectWriter(std::move(MOTW)), OS(&OS_) {}
264 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
266 : TargetObjectWriter(std::move(MOTW)), IsSplitDwarf(
true), OS(&OS_),
270 void reset()
override {
271 CodeRelocations.clear();
272 DataRelocations.clear();
276 TableIndices.
clear();
277 DataLocations.
clear();
278 CustomSections.clear();
279 ProducersSection.reset();
280 TargetFeaturesSection.reset();
281 CustomSectionsRelocations.
clear();
282 SignatureIndices.
clear();
284 DataSegments.
clear();
285 SectionFunctions.
clear();
286 NumFunctionImports = 0;
287 NumGlobalImports = 0;
297 void executePostLayoutBinding()
override;
309 void writeStringWithAlignment(
const StringRef Str,
unsigned Alignment);
311 void writeI32(int32_t val) {
314 W->OS.write(Buffer,
sizeof(Buffer));
317 void writeI64(int64_t val) {
320 W->OS.write(Buffer,
sizeof(Buffer));
323 void writeValueType(
wasm::ValType Ty) { W->OS <<
static_cast<char>(Ty); }
330 void writeElemSection(
const MCSymbolWasm *IndirectFunctionTable,
332 void writeDataCountSection();
340 std::vector<WasmRelocationEntry> &Relocations);
341 void writeLinkingMetaDataSection(
343 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
344 const std::map<
StringRef, std::vector<WasmComdatEntry>> &Comdats);
345 void writeCustomSection(WasmCustomSection &CustomSection,
347 void writeCustomRelocSections();
350 const WasmRelocationEntry &RelEntry);
354 uint32_t getRelocationIndexValue(
const WasmRelocationEntry &RelEntry);
364void WasmObjectWriter::startSection(SectionBookkeeping &Section,
365 unsigned SectionId) {
367 W->
OS << char(SectionId);
378 Section.Index = SectionCount++;
383void WasmObjectWriter::writeStringWithAlignment(
const StringRef Str,
384 unsigned Alignment) {
388 raw_null_ostream NullOS;
390 uint64_t
Offset = W->
OS.
tell() + StrSizeLength + Str.size();
395 assert((StrSizeLength + Paddings) <= 5 &&
"too long string to align");
403void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
412 if (Name !=
"__clangast") {
416 writeStringWithAlignment(Name, 4);
425void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
440 writePatchableU32(
static_cast<raw_pwrite_stream &
>(W->
OS),
Size,
445void WasmObjectWriter::writeHeader(
const MCAssembler &Asm) {
450void WasmObjectWriter::executePostLayoutBinding() {
457 if (
auto *Sym =
Asm->getContext().lookupSymbol(
"__indirect_function_table")) {
458 const auto *WasmSym =
static_cast<const MCSymbolWasm *
>(Sym);
459 if (WasmSym->isNoStrip())
460 Asm->registerSymbol(*Sym);
465 for (
const MCSymbol &S :
Asm->symbols()) {
466 const auto &WS =
static_cast<const MCSymbolWasm &
>(S);
467 if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
468 const auto &Sec =
static_cast<const MCSectionWasm &
>(S.getSection());
469 auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S));
477void WasmObjectWriter::recordRelocation(
const MCFragment &
F,
478 const MCFixup &
Fixup, MCValue Target,
479 uint64_t &FixedValue) {
483 const auto &FixupSection =
static_cast<MCSectionWasm &
>(*
F.getParent());
484 uint64_t
C =
Target.getConstant();
485 uint64_t FixupOffset =
Asm->getFragmentOffset(
F) +
Fixup.getOffset();
487 bool IsLocRel =
false;
489 if (
const auto *RefB =
Target.getSubSym()) {
490 auto &SymB =
static_cast<const MCSymbolWasm &
>(*RefB);
492 if (FixupSection.isText()) {
494 Twine(
"symbol '") + SymB.getName() +
495 "' unsupported subtraction expression used in "
496 "relocation in code section.");
500 if (SymB.isUndefined()) {
502 Twine(
"symbol '") + SymB.getName() +
503 "' can not be undefined in a subtraction expression");
506 const MCSection &SecB = SymB.getSection();
507 if (&SecB != &FixupSection) {
509 Twine(
"symbol '") + SymB.getName() +
510 "' can not be placed in a different section");
514 C += FixupOffset -
Asm->getSymbolOffset(SymB);
518 auto *SymA =
static_cast<const MCSymbolWasm *
>(
Target.getAddSym());
521 if (FixupSection.getName().starts_with(
".init_array")) {
522 SymA->setUsedInInitArray();
536 TargetObjectWriter->getRelocType(Target,
Fixup, FixupSection, IsLocRel);
541 if ((
Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
542 Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
543 Type == wasm::R_WASM_SECTION_OFFSET_I32) &&
550 if (!FixupSection.isMetadata())
552 "only supported in metadata sections");
554 const MCSymbol *SectionSymbol =
nullptr;
555 const MCSection &SecA = SymA->getSection();
557 auto SecSymIt = SectionFunctions.find(&SecA);
558 if (SecSymIt == SectionFunctions.end())
560 SectionSymbol = SecSymIt->second;
567 C +=
Asm->getSymbolOffset(*SymA);
568 SymA =
static_cast<const MCSymbolWasm *
>(SectionSymbol);
571 if (
Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB ||
572 Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB64 ||
573 Type == wasm::R_WASM_TABLE_INDEX_SLEB ||
574 Type == wasm::R_WASM_TABLE_INDEX_SLEB64 ||
575 Type == wasm::R_WASM_TABLE_INDEX_I32 ||
576 Type == wasm::R_WASM_TABLE_INDEX_I64) {
579 auto TableName =
"__indirect_function_table";
580 auto *Sym =
static_cast<MCSymbolWasm *
>(Ctx.
lookupSymbol(TableName));
584 if (!Sym->isFunctionTable())
588 Asm->registerSymbol(*Sym);
594 if (
Type != wasm::R_WASM_TYPE_INDEX_LEB) {
595 if (SymA->getName().empty())
597 "supported by wasm");
599 SymA->setUsedInReloc();
602 WasmRelocationEntry Rec(FixupOffset, SymA,
C,
Type, &FixupSection);
605 if (FixupSection.isWasmData()) {
606 DataRelocations.push_back(Rec);
607 }
else if (FixupSection.isText()) {
608 CodeRelocations.push_back(Rec);
609 }
else if (FixupSection.isMetadata()) {
610 CustomSectionsRelocations[&FixupSection].push_back(Rec);
621WasmObjectWriter::getProvisionalValue(
const MCAssembler &Asm,
622 const WasmRelocationEntry &RelEntry) {
623 if ((RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_LEB ||
624 RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_I32) &&
626 assert(GOTIndices.contains(RelEntry.Symbol) &&
"symbol not found in GOT");
627 return GOTIndices[RelEntry.Symbol];
630 switch (RelEntry.Type) {
631 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
632 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
633 case wasm::R_WASM_TABLE_INDEX_SLEB:
634 case wasm::R_WASM_TABLE_INDEX_SLEB64:
635 case wasm::R_WASM_TABLE_INDEX_I32:
636 case wasm::R_WASM_TABLE_INDEX_I64: {
639 static_cast<const MCSymbolWasm *
>(
Asm.getBaseSymbol(*RelEntry.Symbol));
641 if (RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB ||
642 RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB64)
643 return TableIndices[
Base] - InitialTableOffset;
645 return TableIndices[
Base];
647 case wasm::R_WASM_TYPE_INDEX_LEB:
649 return getRelocationIndexValue(RelEntry);
650 case wasm::R_WASM_FUNCTION_INDEX_LEB:
651 case wasm::R_WASM_FUNCTION_INDEX_I32:
652 case wasm::R_WASM_GLOBAL_INDEX_LEB:
653 case wasm::R_WASM_GLOBAL_INDEX_I32:
654 case wasm::R_WASM_TAG_INDEX_LEB:
655 case wasm::R_WASM_TABLE_NUMBER_LEB:
657 assert(WasmIndices.contains(RelEntry.Symbol) &&
658 "symbol not found in wasm index space");
659 return WasmIndices[RelEntry.Symbol];
660 case wasm::R_WASM_FUNCTION_OFFSET_I32:
661 case wasm::R_WASM_FUNCTION_OFFSET_I64:
662 case wasm::R_WASM_SECTION_OFFSET_I32: {
666 static_cast<const MCSectionWasm &
>(RelEntry.Symbol->
getSection());
667 return Section.getSectionOffset() + RelEntry.Addend;
669 case wasm::R_WASM_MEMORY_ADDR_LEB:
670 case wasm::R_WASM_MEMORY_ADDR_LEB64:
671 case wasm::R_WASM_MEMORY_ADDR_SLEB:
672 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
673 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
674 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
675 case wasm::R_WASM_MEMORY_ADDR_I32:
676 case wasm::R_WASM_MEMORY_ADDR_I64:
677 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
678 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
679 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
680 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64: {
685 const wasm::WasmDataReference &SymRef = DataLocations[RelEntry.Symbol];
686 const WasmDataSegment &Segment = DataSegments[SymRef.
Segment];
688 return Segment.Offset + SymRef.
Offset + RelEntry.Addend;
702 if (Frag.hasInstructions())
707 if (Frag.getAlignFillLen() != 1)
710 uint8_t Value = Frag.hasAlignEmitNops() ? 0 : Frag.getAlignFill();
712 std::min<uint64_t>(
alignTo(DataBytes.
size(), Frag.getAlignment()),
713 DataBytes.
size() + Frag.getAlignMaxBytesToEmit());
717 if (!Fill->getNumValues().evaluateAsAbsolute(NumValues))
719 DataBytes.
insert(DataBytes.
end(), Fill->getValueSize() * NumValues,
732WasmObjectWriter::getRelocationIndexValue(
const WasmRelocationEntry &RelEntry) {
733 if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) {
734 auto It = TypeIndices.find(RelEntry.Symbol);
735 if (It == TypeIndices.end())
746void WasmObjectWriter::applyRelocations(
748 const MCAssembler &Asm) {
749 auto &Stream =
static_cast<raw_pwrite_stream &
>(W->
OS);
750 for (
const WasmRelocationEntry &RelEntry : Relocations) {
751 uint64_t
Offset = ContentsOffset +
756 uint64_t
Value = getProvisionalValue(Asm, RelEntry);
758 switch (RelEntry.Type) {
759 case wasm::R_WASM_FUNCTION_INDEX_LEB:
760 case wasm::R_WASM_TYPE_INDEX_LEB:
761 case wasm::R_WASM_GLOBAL_INDEX_LEB:
762 case wasm::R_WASM_MEMORY_ADDR_LEB:
763 case wasm::R_WASM_TAG_INDEX_LEB:
764 case wasm::R_WASM_TABLE_NUMBER_LEB:
767 case wasm::R_WASM_MEMORY_ADDR_LEB64:
770 case wasm::R_WASM_TABLE_INDEX_I32:
771 case wasm::R_WASM_MEMORY_ADDR_I32:
772 case wasm::R_WASM_FUNCTION_OFFSET_I32:
773 case wasm::R_WASM_FUNCTION_INDEX_I32:
774 case wasm::R_WASM_SECTION_OFFSET_I32:
775 case wasm::R_WASM_GLOBAL_INDEX_I32:
776 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I32:
779 case wasm::R_WASM_TABLE_INDEX_I64:
780 case wasm::R_WASM_MEMORY_ADDR_I64:
781 case wasm::R_WASM_FUNCTION_OFFSET_I64:
782 case wasm::R_WASM_MEMORY_ADDR_LOCREL_I64:
785 case wasm::R_WASM_TABLE_INDEX_SLEB:
786 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
787 case wasm::R_WASM_MEMORY_ADDR_SLEB:
788 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
789 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
792 case wasm::R_WASM_TABLE_INDEX_SLEB64:
793 case wasm::R_WASM_TABLE_INDEX_REL_SLEB64:
794 case wasm::R_WASM_MEMORY_ADDR_SLEB64:
795 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
796 case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB64:
805void WasmObjectWriter::writeTypeSection(
807 if (Signatures.
empty())
815 for (
const wasm::WasmSignature &Sig : Signatures) {
830 uint32_t NumElements) {
841 for (
const wasm::WasmImport &
Import : Imports) {
852 W->
OS << char(
Import.Global.Mutable ? 1 : 0);
859 W->
OS << char(
Import.Table.ElemType);
876 if (Functions.
empty())
883 for (
const WasmFunction &Func : Functions)
889void WasmObjectWriter::writeTagSection(ArrayRef<uint32_t> TagTypes) {
890 if (TagTypes.
empty())
897 for (uint32_t Index : TagTypes) {
913 for (
const wasm::WasmGlobal &
Global : Globals) {
916 if (
Global.InitExpr.Extended) {
919 W->
OS << char(
Global.InitExpr.Inst.Opcode);
920 switch (
Global.Type.Type) {
934 writeValueType(wasm::ValType::EXTERNREF);
954 for (
const wasm::WasmTable &Table : Tables) {
955 assert(Table.Type.ElemType != wasm::ValType::OTHERREF &&
956 "Cannot encode general ref-typed tables");
974 for (
const wasm::WasmExport &
Export : Exports) {
983void WasmObjectWriter::writeElemSection(
984 const MCSymbolWasm *IndirectFunctionTable, ArrayRef<uint32_t> TableElems) {
985 if (TableElems.
empty())
988 assert(IndirectFunctionTable);
995 assert(WasmIndices.contains(IndirectFunctionTable));
996 uint32_t TableNumber = WasmIndices.find(IndirectFunctionTable)->second;
1013 const uint8_t ElemKind = 0;
1018 for (uint32_t Elem : TableElems)
1021 endSection(Section);
1024void WasmObjectWriter::writeDataCountSection() {
1025 if (DataSegments.empty())
1031 endSection(Section);
1034uint32_t WasmObjectWriter::writeCodeSection(
const MCAssembler &Asm,
1036 if (Functions.
empty())
1044 for (
const WasmFunction &Func : Functions) {
1045 auto *FuncSection =
static_cast<MCSectionWasm *
>(
Func.Section);
1047 int64_t
Size =
Asm.getSectionAddressSize(*FuncSection);
1049 FuncSection->setSectionOffset(W->
OS.
tell() -
Section.ContentsOffset);
1050 Asm.writeSectionData(W->
OS, FuncSection);
1054 applyRelocations(CodeRelocations,
Section.ContentsOffset, Asm);
1056 endSection(Section);
1060uint32_t WasmObjectWriter::writeDataSection(
const MCAssembler &Asm) {
1061 if (DataSegments.empty())
1069 for (
const WasmDataSegment &Segment : DataSegments) {
1080 Segment.Section->setSectionOffset(W->
OS.
tell() -
Section.ContentsOffset);
1081 W->
OS << Segment.Data;
1085 applyRelocations(DataRelocations,
Section.ContentsOffset, Asm);
1087 endSection(Section);
1091void WasmObjectWriter::writeRelocSection(
1092 uint32_t SectionIndex, StringRef Name,
1093 std::vector<WasmRelocationEntry> &Relocs) {
1106 Relocs, [](
const WasmRelocationEntry &
A,
const WasmRelocationEntry &
B) {
1107 return (
A.Offset +
A.FixupSection->getSectionOffset()) <
1108 (
B.Offset +
B.FixupSection->getSectionOffset());
1112 startCustomSection(Section, std::string(
"reloc.") +
Name.str());
1116 for (
const WasmRelocationEntry &RelEntry : Relocs) {
1119 uint32_t
Index = getRelocationIndexValue(RelEntry);
1121 W->
OS << char(RelEntry.Type);
1124 if (RelEntry.hasAddend())
1128 endSection(Section);
1131void WasmObjectWriter::writeCustomRelocSections() {
1132 for (
const auto &Sec : CustomSections) {
1133 auto &Relocations = CustomSectionsRelocations[Sec.Section];
1134 writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
1138void WasmObjectWriter::writeLinkingMetaDataSection(
1140 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
1141 const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
1143 startCustomSection(Section,
"linking");
1146 SectionBookkeeping SubSection;
1147 if (SymbolInfos.
size() != 0) {
1150 for (
const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
1172 const uint32_t SectionIndex =
1173 CustomSections[Sym.ElementIndex].OutputIndex;
1181 endSection(SubSection);
1184 if (DataSegments.size()) {
1187 for (
const WasmDataSegment &Segment : DataSegments) {
1192 endSection(SubSection);
1195 if (!InitFuncs.empty()) {
1198 for (
auto &StartFunc : InitFuncs) {
1202 endSection(SubSection);
1205 if (Comdats.size()) {
1208 for (
const auto &
C : Comdats) {
1212 for (
const WasmComdatEntry &Entry :
C.second) {
1217 endSection(SubSection);
1220 endSection(Section);
1223void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
1224 const MCAssembler &Asm) {
1226 auto *Sec = CustomSection.Section;
1227 startCustomSection(Section, CustomSection.Name);
1229 Sec->setSectionOffset(W->
OS.
tell() -
Section.ContentsOffset);
1230 Asm.writeSectionData(W->
OS, Sec);
1232 CustomSection.OutputContentsOffset =
Section.ContentsOffset;
1233 CustomSection.OutputIndex =
Section.Index;
1235 endSection(Section);
1238 auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
1239 applyRelocations(Relocations, CustomSection.OutputContentsOffset, Asm);
1242uint32_t WasmObjectWriter::getFunctionType(
const MCSymbolWasm &Symbol) {
1244 assert(TypeIndices.contains(&Symbol));
1245 return TypeIndices[&
Symbol];
1248uint32_t WasmObjectWriter::getTagType(
const MCSymbolWasm &Symbol) {
1250 assert(TypeIndices.contains(&Symbol));
1251 return TypeIndices[&
Symbol];
1254void WasmObjectWriter::registerFunctionType(
const MCSymbolWasm &Symbol) {
1257 wasm::WasmSignature S;
1259 if (
auto *Sig =
Symbol.getSignature()) {
1264 auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
1266 Signatures.push_back(S);
1267 TypeIndices[&
Symbol] = Pair.first->second;
1270 <<
" new:" << Pair.second <<
"\n");
1271 LLVM_DEBUG(
dbgs() <<
" -> type index: " << Pair.first->second <<
"\n");
1274void WasmObjectWriter::registerTagType(
const MCSymbolWasm &Symbol) {
1279 wasm::WasmSignature S;
1280 if (
auto *Sig =
Symbol.getSignature()) {
1285 auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
1287 Signatures.push_back(S);
1288 TypeIndices[&
Symbol] = Pair.first->second;
1290 LLVM_DEBUG(
dbgs() <<
"registerTagType: " << Symbol <<
" new:" << Pair.second
1292 LLVM_DEBUG(
dbgs() <<
" -> type index: " << Pair.first->second <<
"\n");
1317 for (
const MCSymbol &S : Asm.symbols()) {
1319 if (WS.isData() && WS.isInSection()) {
1320 auto &RefSection =
static_cast<MCSectionWasm &
>(WS.getSection());
1330void WasmObjectWriter::prepareImports(
1331 SmallVectorImpl<wasm::WasmImport> &Imports, MCAssembler &Asm) {
1335 wasm::WasmImport MemImport;
1336 MemImport.
Module =
"env";
1337 MemImport.
Field =
"__linear_memory";
1346 for (
const MCSymbol &S :
Asm.symbols()) {
1347 const auto &WS =
static_cast<const MCSymbolWasm &
>(S);
1351 if (WS.isFunction()) {
1352 auto *BS =
static_cast<const MCSymbolWasm *
>(
Asm.getBaseSymbol(S));
1355 ": absolute addressing not supported!");
1356 registerFunctionType(*BS);
1360 registerTagType(WS);
1362 if (WS.isTemporary())
1366 if (!WS.isDefined() && !WS.isComdat()) {
1367 if (WS.isFunction()) {
1369 Import.Module = WS.getImportModule();
1370 Import.Field = WS.getImportName();
1372 Import.SigIndex = getFunctionType(WS);
1374 assert(!WasmIndices.contains(&WS));
1375 WasmIndices[&WS] = NumFunctionImports++;
1376 }
else if (WS.isGlobal()) {
1381 Import.Field = WS.getImportName();
1383 Import.Module = WS.getImportModule();
1384 Import.Global = WS.getGlobalType();
1386 assert(!WasmIndices.contains(&WS));
1387 WasmIndices[&WS] = NumGlobalImports++;
1388 }
else if (WS.isTag()) {
1393 Import.Module = WS.getImportModule();
1394 Import.Field = WS.getImportName();
1396 Import.SigIndex = getTagType(WS);
1398 assert(!WasmIndices.contains(&WS));
1399 WasmIndices[&WS] = NumTagImports++;
1400 }
else if (WS.isTable()) {
1405 Import.Module = WS.getImportModule();
1406 Import.Field = WS.getImportName();
1408 Import.Table = WS.getTableType();
1410 assert(!WasmIndices.contains(&WS));
1411 WasmIndices[&WS] = NumTableImports++;
1417 for (
const MCSymbol &S :
Asm.symbols()) {
1418 const auto &WS =
static_cast<const MCSymbolWasm &
>(S);
1419 if (WS.isUsedInGOT()) {
1421 if (WS.isFunction())
1422 Import.Module =
"GOT.func";
1424 Import.Module =
"GOT.mem";
1425 Import.Field = WS.getName();
1429 assert(!GOTIndices.contains(&WS));
1430 GOTIndices[&WS] = NumGlobalImports++;
1435uint64_t WasmObjectWriter::writeObject() {
1439 uint64_t TotalSize = writeOneObject(*Asm, DwoMode::NonDwoOnly);
1443 return TotalSize + writeOneObject(*Asm, DwoMode::DwoOnly);
1445 return writeOneObject(*Asm, DwoMode::AllSections);
1449uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
1451 uint64_t StartOffset = W->
OS.
tell();
1453 CustomSections.clear();
1459 SmallVector<uint32_t, 4> TableElems;
1467 std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
1469 if (
Mode != DwoMode::DwoOnly)
1470 prepareImports(Imports, Asm);
1474 for (MCSection &Sec : Asm) {
1475 auto &
Section =
static_cast<MCSectionWasm &
>(Sec);
1483 LLVM_DEBUG(
dbgs() <<
"Processing Section " << SectionName <<
" group "
1484 <<
Section.getGroup() <<
"\n";);
1497 uint32_t SegmentIndex = DataSegments.size();
1499 DataSegments.emplace_back();
1500 WasmDataSegment &Segment = DataSegments.back();
1502 Segment.InitFlags =
Section.getPassive()
1507 addData(Segment.Data, Section);
1509 Segment.LinkingFlags =
Section.getSegmentFlags();
1511 Section.setSegmentIndex(SegmentIndex);
1513 if (
const MCSymbolWasm *
C =
Section.getGroup()) {
1514 Comdats[
C->getName()].emplace_back(
1524 Name.consume_front(
".custom_section.");
1526 auto *Begin =
static_cast<MCSymbolWasm *
>(Sec.getBeginSymbol());
1528 assert(!WasmIndices.contains(Begin));
1529 WasmIndices[Begin] = CustomSections.size();
1533 if (Name ==
"producers") {
1534 ProducersSection = std::make_unique<WasmCustomSection>(Name, &Section);
1537 if (Name ==
"target_features") {
1538 TargetFeaturesSection =
1539 std::make_unique<WasmCustomSection>(Name, &Section);
1547 if (
const MCSymbolWasm *
C =
Section.getGroup()) {
1548 Comdats[
C->getName()].emplace_back(
1550 static_cast<uint32_t
>(CustomSections.size())});
1553 CustomSections.emplace_back(Name, &Section);
1557 if (
Mode != DwoMode::DwoOnly) {
1559 for (
const MCSymbol &S :
Asm.symbols()) {
1562 if (S.isTemporary() && S.getName().empty())
1565 const auto &WS =
static_cast<const MCSymbolWasm &
>(S);
1567 dbgs() <<
"MCSymbol: "
1570 <<
" isDefined=" << S.isDefined() <<
" isExternal="
1571 << WS.isExternal() <<
" isTemporary=" << S.isTemporary()
1572 <<
" isWeak=" << WS.isWeak() <<
" isHidden=" << WS.isHidden()
1573 <<
" isVariable=" << WS.isVariable() <<
"\n");
1575 if (WS.isVariable())
1577 if (WS.isComdat() && !WS.isDefined())
1580 if (WS.isFunction()) {
1582 if (WS.isDefined()) {
1583 if (WS.getOffset() != 0)
1585 "function sections must contain one function each");
1588 Index = NumFunctionImports + Functions.
size();
1590 Func.SigIndex = getFunctionType(WS);
1591 Func.Section = &WS.getSection();
1592 assert(!WasmIndices.contains(&WS));
1593 WasmIndices[&WS] =
Index;
1596 auto &
Section =
static_cast<MCSectionWasm &
>(WS.getSection());
1597 if (
const MCSymbolWasm *
C =
Section.getGroup()) {
1598 Comdats[
C->getName()].emplace_back(
1602 if (WS.hasExportName()) {
1604 Export.Name = WS.getExportName();
1611 Index = WasmIndices.find(&WS)->second;
1616 }
else if (WS.isData()) {
1620 if (!WS.isDefined()) {
1631 if (!WS.getSize()->evaluateAsAbsolute(
Size, Asm))
1634 auto &DataSection =
static_cast<MCSectionWasm &
>(WS.getSection());
1635 if (!DataSection.isWasmData())
1641 wasm::WasmDataReference
Ref = wasm::WasmDataReference{
1642 DataSection.getSegmentIndex(),
Asm.getSymbolOffset(WS),
1643 static_cast<uint64_t
>(
Size)};
1644 assert(!DataLocations.contains(&WS));
1645 DataLocations[&WS] =
Ref;
1648 }
else if (WS.isGlobal()) {
1650 if (WS.isDefined()) {
1652 Global.Type = WS.getGlobalType();
1653 Global.Index = NumGlobalImports + Globals.
size();
1654 Global.InitExpr.Extended =
false;
1655 switch (
Global.Type.Type) {
1674 assert(!WasmIndices.contains(&WS));
1675 WasmIndices[&WS] =
Global.Index;
1680 << WasmIndices.find(&WS)->second <<
"\n");
1682 }
else if (WS.isTable()) {
1683 if (WS.isDefined()) {
1684 wasm::WasmTable Table;
1685 Table.
Index = NumTableImports + Tables.
size();
1686 Table.
Type = WS.getTableType();
1687 assert(!WasmIndices.contains(&WS));
1688 WasmIndices[&WS] = Table.
Index;
1692 << WasmIndices.find(&WS)->second <<
"\n");
1693 }
else if (WS.isTag()) {
1697 if (WS.isDefined()) {
1698 Index = NumTagImports + TagTypes.
size();
1699 uint32_t SigIndex = getTagType(WS);
1700 assert(!WasmIndices.contains(&WS));
1701 WasmIndices[&WS] =
Index;
1705 assert(WasmIndices.contains(&WS));
1707 LLVM_DEBUG(
dbgs() <<
" -> tag index: " << WasmIndices.find(&WS)->second
1719 for (
const MCSymbol &S :
Asm.symbols()) {
1720 if (!S.isVariable())
1725 const auto *BS =
Asm.getBaseSymbol(S);
1728 ": absolute addressing not supported!");
1729 const MCSymbolWasm *
Base =
static_cast<const MCSymbolWasm *
>(BS);
1732 const auto &WS =
static_cast<const MCSymbolWasm &
>(S);
1736 if (
Base->isFunction()) {
1738 uint32_t WasmIndex = WasmIndices.find(
Base)->second;
1739 assert(!WasmIndices.contains(&WS));
1740 WasmIndices[&WS] = WasmIndex;
1742 }
else if (
Base->isData()) {
1743 auto &DataSection =
static_cast<MCSectionWasm &
>(WS.getSection());
1744 uint64_t
Offset =
Asm.getSymbolOffset(S);
1750 if (!
Base->getSize()->evaluateAsAbsolute(
Size, Asm))
1752 const WasmDataSegment &Segment =
1753 DataSegments[DataSection.getSegmentIndex()];
1755 std::min(
static_cast<uint64_t
>(
Size), Segment.Data.size() -
Offset);
1756 wasm::WasmDataReference
Ref = wasm::WasmDataReference{
1757 DataSection.getSegmentIndex(),
1758 static_cast<uint32_t
>(
Asm.getSymbolOffset(S)),
1759 static_cast<uint32_t
>(
Size)};
1760 DataLocations[&WS] =
Ref;
1769 for (
const MCSymbol &S :
Asm.symbols()) {
1770 const auto &WS =
static_cast<const MCSymbolWasm &
>(S);
1785 if (WS.isData() && WS.isDefined() && !DataLocations.contains(&WS))
1794 if (!WS.isExternal() && WS.isDefined())
1796 if (WS.isUndefined())
1798 if (WS.isNoStrip()) {
1800 if (isEmscripten()) {
1804 if (WS.hasImportName())
1806 if (WS.hasExportName())
1811 wasm::WasmSymbolInfo
Info;
1812 Info.Name = WS.getName();
1816 assert(WasmIndices.contains(&WS));
1817 Info.ElementIndex = WasmIndices.find(&WS)->second;
1818 }
else if (WS.isDefined()) {
1819 assert(DataLocations.contains(&WS));
1820 Info.DataRef = DataLocations.find(&WS)->second;
1822 WS.setIndex(SymbolInfos.
size());
1827 auto HandleReloc = [&](
const WasmRelocationEntry &Rel) {
1831 if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 &&
1832 Rel.Type != wasm::R_WASM_TABLE_INDEX_I64 &&
1833 Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB &&
1834 Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB64 &&
1835 Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB &&
1836 Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB64)
1838 assert(Rel.Symbol->isFunction());
1840 static_cast<const MCSymbolWasm *
>(
Asm.getBaseSymbol(*Rel.Symbol));
1841 uint32_t FunctionIndex = WasmIndices.find(
Base)->second;
1842 uint32_t TableIndex = TableElems.
size() + InitialTableOffset;
1843 if (TableIndices.try_emplace(
Base, TableIndex).second) {
1845 <<
" to table: " << TableIndex <<
"\n");
1847 registerFunctionType(*
Base);
1851 for (
const WasmRelocationEntry &RelEntry : CodeRelocations)
1852 HandleReloc(RelEntry);
1853 for (
const WasmRelocationEntry &RelEntry : DataRelocations)
1854 HandleReloc(RelEntry);
1858 for (
const MCSection &S : Asm) {
1859 const auto &WS =
static_cast<const MCSectionWasm &
>(S);
1860 if (WS.getName().starts_with(
".fini_array"))
1862 if (!WS.getName().starts_with(
".init_array"))
1864 auto IT = WS.begin();
1867 for (
auto *Frag = &*
IT; Frag; Frag = Frag->getNext()) {
1872 uint16_t Priority = UINT16_MAX;
1873 unsigned PrefixLength = strlen(
".init_array");
1874 if (WS.getName().size() > PrefixLength) {
1875 if (WS.getName()[PrefixLength] !=
'.')
1877 ".init_array section priority should start with '.'");
1878 if (WS.getName().substr(PrefixLength + 1).getAsInteger(10, Priority))
1882 for (
const MCFixup &
Fixup : Frag->getFixups()) {
1885 const MCExpr *Expr =
Fixup.getValue();
1889 "fixups in .init_array should be symbol references");
1891 static_cast<const MCSymbolWasm &
>(SymRef->getSymbol());
1894 if (!TargetSym.isFunction())
1896 InitFuncs.
push_back(std::make_pair(Priority, TargetSym.getIndex()));
1904 uint32_t CodeSectionIndex, DataSectionIndex;
1905 if (
Mode != DwoMode::DwoOnly) {
1906 writeTypeSection(Signatures);
1907 writeImportSection(Imports,
DataSize, TableElems.
size());
1908 writeFunctionSection(Functions);
1909 writeTableSection(Tables);
1911 writeTagSection(TagTypes);
1912 writeGlobalSection(Globals);
1913 writeExportSection(Exports);
1914 const MCSymbol *IndirectFunctionTable =
1915 getContext().lookupSymbol(
"__indirect_function_table");
1916 writeElemSection(
static_cast<const MCSymbolWasm *
>(IndirectFunctionTable),
1918 writeDataCountSection();
1920 CodeSectionIndex = writeCodeSection(Asm, Functions);
1921 DataSectionIndex = writeDataSection(Asm);
1926 for (
auto &Group : Comdats) {
1927 for (
auto &Entry : Group.second) {
1929 Entry.Index += SectionCount;
1933 for (
auto &CustomSection : CustomSections)
1934 writeCustomSection(CustomSection, Asm);
1936 if (
Mode != DwoMode::DwoOnly) {
1937 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
1939 writeRelocSection(CodeSectionIndex,
"CODE", CodeRelocations);
1940 writeRelocSection(DataSectionIndex,
"DATA", DataRelocations);
1942 writeCustomRelocSections();
1943 if (ProducersSection)
1944 writeCustomSection(*ProducersSection, Asm);
1945 if (TargetFeaturesSection)
1946 writeCustomSection(*TargetFeaturesSection, Asm);
1949 return W->
OS.
tell() - StartOffset;
1952std::unique_ptr<MCObjectWriter>
1955 return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
1958std::unique_ptr<MCObjectWriter>
1962 return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS, DwoOS);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate any type of IT block"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow complex IT blocks")))
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
static void writeString(raw_ostream &OS, StringRef S)
PowerPC TLS Dynamic Call Fixup
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static const unsigned InvalidIndex
static bool isSectionReferenced(MCAssembler &Asm, MCSectionWasm &Section)
static void addData(SmallVectorImpl< char > &DataBytes, MCSectionWasm &DataSection)
static bool isInSymtab(const MCSymbolWasm &Sym)
static bool isDwoSection(const MCSection &Sec)
static bool is64Bit(const char *name)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
bool empty() const
Check if the array is empty.
LLVM_ABI MCSymbol * lookupSymbol(const Twine &Name) const
Get the symbol for Name, or null.
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
static MCFixupKind getDataKindForSize(unsigned Size)
Return the generic fixup kind for a value with the given size.
Defines the object file and target independent interfaces used by the assembler backend to write nati...
virtual void reset()
lifetime management
This represents a section on wasm.
uint64_t getSectionOffset() const
Instances of this class represent a uniqued identifier for a section in the current translation unit.
StringRef getName() const
MCSymbol * getBeginSymbol()
bool omitFromLinkingSection() const
bool isUsedInInitArray() const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
StringRef getName() const
getName - Get the symbol name.
uint32_t getIndex() const
Get the (implementation defined) index.
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
bool isUsedInReloc() const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Target - Wrapper for Target specific information.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream.
uint64_t tell() const
tell - Return the current offset with the file.
raw_ostream & write(unsigned char C)
An abstract base class for streams implementations that also support a pwrite operation.
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI void writeHeader(support::endian::Writer &W, bool Is64Bit, uint8_t OSABI, uint8_t ABIVersion, uint16_t EMachine, uint32_t EFlags, uint64_t SHOff, uint16_t SHNum, uint16_t SHStrNdx)
Write an ELF file header (Elf32_Ehdr or Elf64_Ehdr) for an ET_REL object.
bool isRelocRelocation(MCFixupKind FixupKind)
NodeAddr< FuncNode * > Func
Context & getContext() const
void write64le(void *P, uint64_t V)
void write32le(void *P, uint32_t V)
const unsigned WASM_SYMBOL_UNDEFINED
const unsigned WASM_SYMBOL_NO_STRIP
const unsigned WASM_SYMBOL_TLS
const uint32_t WasmMetadataVersion
const unsigned WASM_SYMBOL_BINDING_WEAK
const unsigned WASM_SYMBOL_BINDING_LOCAL
@ WASM_LIMITS_FLAG_HAS_MAX
@ WASM_SYMBOL_TYPE_GLOBAL
@ WASM_SYMBOL_TYPE_SECTION
@ WASM_SYMBOL_TYPE_FUNCTION
const uint32_t WasmVersion
const unsigned WASM_SYMBOL_EXPORTED
@ WASM_ELEM_SEGMENT_HAS_TABLE_NUMBER
@ WASM_DATA_SEGMENT_IS_PASSIVE
@ WASM_DATA_SEGMENT_HAS_MEMINDEX
LLVM_ABI bool relocTypeHasAddend(uint32_t type)
const uint32_t WasmDefaultPageSize
const unsigned WASM_SYMBOL_EXPLICIT_NAME
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
const unsigned WASM_ELEM_SEGMENT_MASK_HAS_ELEM_DESC
LLVM_ABI llvm::StringRef relocTypetoString(uint32_t type)
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
FunctionAddr VTableAddr Value
void stable_sort(R &&Range)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
@ Export
Export information to summary.
@ Import
Import information from summary.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
FunctionAddr VTableAddr uintptr_t uintptr_t DataSize
LLVM_ABI std::unique_ptr< MCObjectWriter > createWasmObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Wasm writer instance.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Global
Append to llvm.global_dtors.
@ Ref
The access may reference the value stored in memory.
@ FirstLiteralRelocationKind
FunctionAddr VTableAddr uintptr_t uintptr_t Data
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
unsigned Log2(Align A)
Returns the log2 of the alignment.
LLVM_ABI std::unique_ptr< MCObjectWriter > createWasmDwoObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS, raw_pwrite_stream &DwoOS)
Adapter to write values to a stream in a particular byte order.
void write(ArrayRef< value_type > Val)
SmallVector< ValType, 1 > Returns
SmallVector< ValType, 4 > Params