16 #include "llvm/Config/llvm-config.h" 37 #define DEBUG_TYPE "mc" 43 static const uint32_t InitialTableOffset = 1;
48 struct SectionBookkeeping {
52 uint64_t PayloadOffset;
54 uint64_t ContentsOffset;
61 struct WasmSignature {
63 enum { Plain,
Empty, Tombstone } State = Plain;
72 return State == Other.State && Returns == Other.Returns &&
73 Params == Other.Params;
78 struct WasmSignatureDenseMapInfo {
79 static WasmSignature getEmptyKey() {
84 static WasmSignature getTombstoneKey() {
86 Sig.State = WasmSignature::Tombstone;
89 static unsigned getHashValue(
const WasmSignature &Sig) {
90 uintptr_t
Value = Sig.State;
97 static bool isEqual(
const WasmSignature &LHS,
const WasmSignature &RHS) {
106 struct WasmDataSegment {
117 struct WasmFunction {
125 uint64_t InitialValue;
131 struct WasmComdatEntry {
137 struct WasmRelocationEntry {
144 WasmRelocationEntry(uint64_t Offset,
const MCSymbolWasm *Symbol,
145 int64_t Addend,
unsigned Type,
147 :
Offset(Offset),
Symbol(Symbol), Addend(Addend), Type(Type),
148 FixupSection(FixupSection) {}
154 <<
", Sym=" << *Symbol <<
", Addend=" << Addend
158 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 163 static const uint32_t InvalidIndex = -1;
165 struct WasmCustomSection {
174 :
Name(Name),
Section(Section), OutputContentsOffset(0),
175 OutputIndex(InvalidIndex) {}
192 Stream.
pwrite((
char *)Buffer, SizeLen, Offset);
202 Stream.
pwrite((
char *)Buffer, SizeLen, Offset);
209 Stream.
pwrite((
char *)Buffer,
sizeof(Buffer), Offset);
216 std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
219 std::vector<WasmRelocationEntry> CodeRelocations;
223 std::vector<WasmRelocationEntry> DataRelocations;
241 std::vector<WasmCustomSection> CustomSections;
242 std::unique_ptr<WasmCustomSection> ProducersSection;
243 std::unique_ptr<WasmCustomSection> TargetFeaturesSection;
246 CustomSectionsRelocations;
254 unsigned NumFunctionImports = 0;
255 unsigned NumGlobalImports = 0;
256 unsigned NumEventImports = 0;
260 bool is64Bit()
const {
return TargetObjectWriter->is64Bit(); }
261 bool isEmscripten()
const {
return TargetObjectWriter->isEmscripten(); }
263 void startSection(SectionBookkeeping &
Section,
unsigned SectionId);
264 void startCustomSection(SectionBookkeeping &Section,
StringRef Name);
265 void endSection(SectionBookkeeping &Section);
268 WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
273 void reset()
override {
274 CodeRelocations.clear();
275 DataRelocations.clear();
279 TableIndices.
clear();
280 DataLocations.
clear();
281 CustomSections.clear();
282 ProducersSection.reset();
283 TargetFeaturesSection.reset();
284 CustomSectionsRelocations.
clear();
285 SignatureIndices.
clear();
287 DataSegments.
clear();
288 SectionFunctions.
clear();
289 NumFunctionImports = 0;
290 NumGlobalImports = 0;
310 void writeValueType(
wasm::ValType Ty) { W.
OS <<
static_cast<char>(Ty); }
318 void writeDataCountSection();
321 void writeDataSection();
324 std::vector<WasmRelocationEntry> &Relocations);
325 void writeLinkingMetaDataSection(
327 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
328 const std::map<
StringRef, std::vector<WasmComdatEntry>> &Comdats);
329 void writeCustomSection(WasmCustomSection &CustomSection,
331 void writeCustomRelocSections();
336 uint32_t getProvisionalValue(
const WasmRelocationEntry &RelEntry);
338 uint64_t ContentsOffset);
340 uint32_t getRelocationIndexValue(
const WasmRelocationEntry &RelEntry);
350 void WasmObjectWriter::startSection(SectionBookkeeping &
Section,
351 unsigned SectionId) {
353 W.OS <<
char(SectionId);
355 Section.SizeOffset =
W.OS.tell();
362 Section.ContentsOffset =
W.OS.tell();
363 Section.PayloadOffset =
W.OS.tell();
364 Section.Index = SectionCount++;
367 void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
373 Section.PayloadOffset =
W.OS.tell();
379 Section.ContentsOffset =
W.OS.tell();
384 void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
385 uint64_t
Size =
W.OS.tell();
391 Size -= Section.PayloadOffset;
399 writePatchableLEB(static_cast<raw_pwrite_stream &>(
W.OS), Size,
409 void WasmObjectWriter::executePostLayoutBinding(
MCAssembler &Asm,
415 if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
416 const auto &Sec =
static_cast<const MCSectionWasm &
>(S.getSection());
417 auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S));
420 Sec.getSectionName());
425 void WasmObjectWriter::recordRelocation(
MCAssembler &Asm,
429 uint64_t &FixedValue) {
434 const auto &FixupSection = cast<MCSectionWasm>(*Fragment->
getParent());
440 if (FixupSection.getSectionName().startswith(
".init_array"))
447 const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
450 Twine(
"symbol '") + SymB.getName() +
451 "': unsupported subtraction expression used in relocation.");
457 const auto *SymA = cast<MCSymbolWasm>(&RefA->
getSymbol());
459 if (SymA->isVariable()) {
460 const MCExpr *Expr = SymA->getVariableValue();
461 const auto *Inner = cast<MCSymbolRefExpr>(Expr);
471 unsigned Type = TargetObjectWriter->getRelocType(Target, Fixup);
476 if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
477 Type == wasm::R_WASM_SECTION_OFFSET_I32) {
478 if (!FixupSection.getKind().isMetadata())
480 "only supported in metadata sections");
482 const MCSymbol *SectionSymbol =
nullptr;
483 const MCSection &SecA = SymA->getSection();
485 SectionSymbol = SectionFunctions.find(&SecA)->second;
492 SymA = cast<MCSymbolWasm>(SectionSymbol);
497 if (Type != wasm::R_WASM_TYPE_INDEX_LEB) {
498 if (SymA->getName().empty())
500 "supported by wasm");
502 SymA->setUsedInReloc();
506 SymA->setUsedInGOT();
508 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
511 if (FixupSection.isWasmData()) {
512 DataRelocations.push_back(Rec);
513 }
else if (FixupSection.getKind().isText()) {
514 CodeRelocations.push_back(Rec);
515 }
else if (FixupSection.getKind().isMetadata()) {
516 CustomSectionsRelocations[&FixupSection].push_back(Rec);
526 auto *Inner = cast<MCSymbolRefExpr>(Expr);
527 Ret = cast<MCSymbolWasm>(&Inner->getSymbol());
537 WasmObjectWriter::getProvisionalValue(
const WasmRelocationEntry &RelEntry) {
538 if (RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_LEB && !RelEntry.Symbol->isGlobal()) {
539 assert(GOTIndices.count(RelEntry.Symbol) > 0 &&
"symbol not found in GOT index space");
540 return GOTIndices[RelEntry.Symbol];
543 switch (RelEntry.Type) {
544 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
545 case wasm::R_WASM_TABLE_INDEX_SLEB:
546 case wasm::R_WASM_TABLE_INDEX_I32: {
550 return TableIndices[Sym];
552 case wasm::R_WASM_TYPE_INDEX_LEB:
554 return getRelocationIndexValue(RelEntry);
555 case wasm::R_WASM_FUNCTION_INDEX_LEB:
556 case wasm::R_WASM_GLOBAL_INDEX_LEB:
557 case wasm::R_WASM_EVENT_INDEX_LEB:
559 assert(WasmIndices.count(RelEntry.Symbol) > 0 &&
"symbol not found in wasm index space");
560 return WasmIndices[RelEntry.Symbol];
561 case wasm::R_WASM_FUNCTION_OFFSET_I32:
562 case wasm::R_WASM_SECTION_OFFSET_I32: {
563 const auto &Section =
564 static_cast<const MCSectionWasm &
>(RelEntry.Symbol->getSection());
565 return Section.getSectionOffset() + RelEntry.Addend;
567 case wasm::R_WASM_MEMORY_ADDR_LEB:
568 case wasm::R_WASM_MEMORY_ADDR_I32:
569 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
570 case wasm::R_WASM_MEMORY_ADDR_SLEB: {
577 const WasmDataSegment &Segment = DataSegments[Ref.
Segment];
579 return Segment.Offset + Ref.
Offset + RelEntry.Addend;
593 if (Frag.hasInstructions())
596 if (
auto *
Align = dyn_cast<MCAlignFragment>(&Frag)) {
597 if (
Align->getValueSize() != 1)
603 DataBytes.
size() +
Align->getMaxBytesToEmit());
604 DataBytes.
resize(Size, Value);
605 }
else if (
auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
607 if (!Fill->getNumValues().evaluateAsAbsolute(NumValues))
609 DataBytes.
insert(DataBytes.
end(), Fill->getValueSize() * NumValues,
611 }
else if (
auto *LEB = dyn_cast<MCLEBFragment>(&Frag)) {
615 const auto &DataFrag = cast<MCDataFragment>(Frag);
617 DataBytes.
insert(DataBytes.
end(), Contents.begin(), Contents.end());
625 WasmObjectWriter::getRelocationIndexValue(
const WasmRelocationEntry &RelEntry) {
626 if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) {
627 if (!TypeIndices.count(RelEntry.Symbol))
629 RelEntry.Symbol->getName());
630 return TypeIndices[RelEntry.Symbol];
633 return RelEntry.Symbol->getIndex();
638 void WasmObjectWriter::applyRelocations(
641 for (
const WasmRelocationEntry &RelEntry : Relocations) {
642 uint64_t
Offset = ContentsOffset +
643 RelEntry.FixupSection->getSectionOffset() +
649 switch (RelEntry.Type) {
650 case wasm::R_WASM_FUNCTION_INDEX_LEB:
651 case wasm::R_WASM_TYPE_INDEX_LEB:
652 case wasm::R_WASM_GLOBAL_INDEX_LEB:
653 case wasm::R_WASM_MEMORY_ADDR_LEB:
654 case wasm::R_WASM_EVENT_INDEX_LEB:
655 writePatchableLEB(Stream, Value, Offset);
657 case wasm::R_WASM_TABLE_INDEX_I32:
658 case wasm::R_WASM_MEMORY_ADDR_I32:
659 case wasm::R_WASM_FUNCTION_OFFSET_I32:
660 case wasm::R_WASM_SECTION_OFFSET_I32:
661 writeI32(Stream, Value, Offset);
663 case wasm::R_WASM_TABLE_INDEX_SLEB:
664 case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
665 case wasm::R_WASM_MEMORY_ADDR_SLEB:
666 case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
667 writePatchableSLEB(Stream, Value, Offset);
676 if (Signatures.
empty())
684 for (
const WasmSignature &Sig : Signatures) {
710 writeString(
Import.Module);
711 writeString(
Import.Field);
744 if (Functions.
empty())
751 for (
const WasmFunction &Func : Functions)
791 if (TableElems.
empty())
812 void WasmObjectWriter::writeDataCountSection() {
813 if (DataSegments.empty())
822 void WasmObjectWriter::writeCodeSection(
const MCAssembler &Asm,
825 if (Functions.
empty())
830 CodeSectionIndex = Section.Index;
834 for (
const WasmFunction &Func : Functions) {
835 auto &FuncSection =
static_cast<MCSectionWasm &
>(Func.Sym->getSection());
838 if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
842 FuncSection.setSectionOffset(
W.OS.tell() - Section.ContentsOffset);
847 applyRelocations(CodeRelocations, Section.ContentsOffset);
852 void WasmObjectWriter::writeDataSection() {
853 if (DataSegments.empty())
858 DataSectionIndex = Section.Index;
862 for (
const WasmDataSegment &Segment : DataSegments) {
872 Segment.Section->setSectionOffset(
W.OS.tell() - Section.ContentsOffset);
873 W.OS << Segment.Data;
877 applyRelocations(DataRelocations, Section.ContentsOffset);
882 void WasmObjectWriter::writeRelocSection(
884 std::vector<WasmRelocationEntry> &Relocs) {
897 Relocs, [](
const WasmRelocationEntry &A,
const WasmRelocationEntry &
B) {
898 return (A.Offset + A.FixupSection->getSectionOffset()) <
899 (B.Offset + B.FixupSection->getSectionOffset());
903 startCustomSection(Section, std::string(
"reloc.") + Name.
str());
907 for (
const WasmRelocationEntry &RelEntry : Relocs) {
909 RelEntry.Offset + RelEntry.FixupSection->getSectionOffset();
912 W.OS <<
char(RelEntry.Type);
915 if (RelEntry.hasAddend())
922 void WasmObjectWriter::writeCustomRelocSections() {
923 for (
const auto &Sec : CustomSections) {
924 auto &Relocations = CustomSectionsRelocations[Sec.Section];
925 writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
929 void WasmObjectWriter::writeLinkingMetaDataSection(
931 ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
932 const std::map<
StringRef, std::vector<WasmComdatEntry>> &Comdats) {
934 startCustomSection(Section,
"linking");
937 SectionBookkeeping SubSection;
938 if (SymbolInfos.
size() != 0) {
951 writeString(Sym.Name);
954 writeString(Sym.Name);
963 CustomSections[Sym.ElementIndex].OutputIndex;
971 endSection(SubSection);
974 if (DataSegments.size()) {
977 for (
const WasmDataSegment &Segment : DataSegments) {
978 writeString(Segment.Name);
982 endSection(SubSection);
985 if (!InitFuncs.empty()) {
988 for (
auto &StartFunc : InitFuncs) {
992 endSection(SubSection);
995 if (Comdats.size()) {
998 for (
const auto &
C : Comdats) {
999 writeString(
C.first);
1002 for (
const WasmComdatEntry &
Entry :
C.second) {
1007 endSection(SubSection);
1010 endSection(Section);
1013 void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
1017 auto *Sec = CustomSection.Section;
1018 startCustomSection(Section, CustomSection.Name);
1020 Sec->setSectionOffset(
W.OS.tell() - Section.ContentsOffset);
1023 CustomSection.OutputContentsOffset = Section.ContentsOffset;
1024 CustomSection.OutputIndex = Section.Index;
1026 endSection(Section);
1029 auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
1030 applyRelocations(Relocations, CustomSection.OutputContentsOffset);
1035 assert(TypeIndices.count(&Symbol));
1036 return TypeIndices[&
Symbol];
1041 assert(TypeIndices.count(&Symbol));
1042 return TypeIndices[&
Symbol];
1045 void WasmObjectWriter::registerFunctionType(
const MCSymbolWasm &Symbol) {
1051 S.Returns = Sig->Returns;
1052 S.Params = Sig->Params;
1055 auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.
size()));
1057 Signatures.push_back(S);
1058 TypeIndices[&
Symbol] = Pair.first->second;
1061 <<
" new:" << Pair.second <<
"\n");
1062 LLVM_DEBUG(
dbgs() <<
" -> type index: " << Pair.first->second <<
"\n");
1065 void WasmObjectWriter::registerEventType(
const MCSymbolWasm &Symbol) {
1072 S.Returns = Sig->Returns;
1073 S.Params = Sig->Params;
1076 auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.
size()));
1078 Signatures.push_back(S);
1079 TypeIndices[&
Symbol] = Pair.first->second;
1081 LLVM_DEBUG(
dbgs() <<
"registerEventType: " << Symbol <<
" new:" << Pair.second
1083 LLVM_DEBUG(
dbgs() <<
" -> type index: " << Pair.first->second <<
"\n");
1105 uint64_t WasmObjectWriter::writeObject(
MCAssembler &Asm,
1107 uint64_t StartOffset =
W.OS.tell();
1119 std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
1126 MemImport.
Module =
"env";
1127 MemImport.
Field =
"__linear_memory";
1135 TableImport.
Module =
"env";
1136 TableImport.
Field =
"__indirect_function_table";
1149 if (WS.isFunction())
1150 registerFunctionType(WS);
1153 registerEventType(WS);
1155 if (WS.isTemporary())
1159 if (!WS.isDefined() && !WS.isComdat()) {
1160 if (WS.isFunction()) {
1162 Import.
Module = WS.getImportModule();
1163 Import.
Field = WS.getImportName();
1165 Import.
SigIndex = getFunctionType(WS);
1167 assert(WasmIndices.count(&WS) == 0);
1168 WasmIndices[&WS] = NumFunctionImports++;
1169 }
else if (WS.isGlobal()) {
1174 Import.
Field = WS.getImportName();
1176 Import.
Module = WS.getImportModule();
1177 Import.
Global = WS.getGlobalType();
1179 assert(WasmIndices.count(&WS) == 0);
1180 WasmIndices[&WS] = NumGlobalImports++;
1181 }
else if (WS.isEvent()) {
1186 Import.
Module = WS.getImportModule();
1187 Import.
Field = WS.getImportName();
1192 assert(WasmIndices.count(&WS) == 0);
1193 WasmIndices[&WS] = NumEventImports++;
1201 if (WS.isUsedInGOT()) {
1203 if (WS.isFunction())
1204 Import.
Module =
"GOT.func";
1206 Import.
Module =
"GOT.mem";
1207 Import.
Field = WS.getName();
1211 assert(GOTIndices.count(&WS) == 0);
1212 GOTIndices[&WS] = NumGlobalImports++;
1227 if (Section.getKind().isText())
1230 if (Section.isWasmData()) {
1231 uint32_t SegmentIndex = DataSegments.size();
1232 DataSize =
alignTo(DataSize, Section.getAlignment());
1233 DataSegments.emplace_back();
1234 WasmDataSegment &Segment = DataSegments.back();
1238 Segment.Offset = DataSize;
1240 addData(Segment.Data, Section);
1241 Segment.Alignment =
Log2_32(Section.getAlignment());
1242 Segment.LinkerFlags = 0;
1243 DataSize += Segment.Data.size();
1244 Section.setSegmentIndex(SegmentIndex);
1247 Comdats[
C->
getName()].emplace_back(
1252 assert(Sec.getKind().isMetadata());
1257 if (Name.startswith(
".custom_section."))
1258 Name = Name.substr(strlen(
".custom_section."));
1260 MCSymbol *Begin = Sec.getBeginSymbol();
1262 WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size();
1263 if (SectionName != Begin->
getName())
1265 Twine(SectionName));
1269 if (Name ==
"producers") {
1270 ProducersSection = std::make_unique<WasmCustomSection>(
Name, &
Section);
1273 if (Name ==
"target_features") {
1274 TargetFeaturesSection =
1275 std::make_unique<WasmCustomSection>(
Name, &
Section);
1279 CustomSections.emplace_back(Name, &Section);
1284 for (
const MCSymbol &S : Asm.symbols()) {
1287 if (S.isTemporary() && S.getName().empty())
1292 dbgs() <<
"MCSymbol: " <<
toString(WS.getType()) <<
" '" << S <<
"'" 1293 <<
" isDefined=" << S.isDefined() <<
" isExternal=" 1294 << S.isExternal() <<
" isTemporary=" << S.isTemporary()
1295 <<
" isWeak=" << WS.isWeak() <<
" isHidden=" << WS.isHidden()
1296 <<
" isVariable=" << WS.isVariable() <<
"\n");
1298 if (WS.isVariable())
1300 if (WS.isComdat() && !WS.isDefined())
1303 if (WS.isFunction()) {
1305 if (WS.isDefined()) {
1306 if (WS.getOffset() != 0)
1308 "function sections must contain one function each");
1310 if (WS.getSize() ==
nullptr)
1312 "function symbols must have a size set with .size");
1315 Index = NumFunctionImports + Functions.
size();
1317 Func.SigIndex = getFunctionType(WS);
1319 WasmIndices[&WS] =
Index;
1322 auto &Section =
static_cast<MCSectionWasm &
>(WS.getSection());
1324 Comdats[
C->
getName()].emplace_back(
1328 if (WS.hasExportName()) {
1330 Export.
Name = WS.getExportName();
1337 Index = WasmIndices.find(&WS)->second;
1342 }
else if (WS.isData()) {
1346 if (!WS.isDefined()) {
1357 if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
1360 auto &DataSection =
static_cast<MCSectionWasm &
>(WS.getSection());
1361 if (!DataSection.isWasmData())
1368 DataSection.getSegmentIndex(),
1370 static_cast<uint32_t>(Size)};
1371 DataLocations[&WS] =
Ref;
1374 }
else if (WS.isGlobal()) {
1381 << WasmIndices.find(&WS)->second <<
"\n");
1383 }
else if (WS.isEvent()) {
1386 if (WS.isDefined()) {
1387 Index = NumEventImports + Events.
size();
1391 assert(WasmIndices.count(&WS) == 0);
1392 WasmIndices[&WS] =
Index;
1396 assert(WasmIndices.count(&WS) > 0);
1398 LLVM_DEBUG(
dbgs() <<
" -> event index: " << WasmIndices.find(&WS)->second
1410 for (
const MCSymbol &S : Asm.symbols()) {
1411 if (!S.isVariable())
1419 LLVM_DEBUG(
dbgs() << WS.getName() <<
": weak alias of '" << *ResolvedSym
1423 assert(WasmIndices.count(ResolvedSym) > 0);
1424 uint32_t WasmIndex = WasmIndices.find(ResolvedSym)->second;
1425 assert(WasmIndices.count(&WS) == 0);
1426 WasmIndices[&WS] = WasmIndex;
1428 }
else if (ResolvedSym->
isData()) {
1429 assert(DataLocations.count(ResolvedSym) > 0);
1431 DataLocations.find(ResolvedSym)->second;
1432 DataLocations[&WS] =
Ref;
1440 for (
const MCSymbol &S : Asm.symbols()) {
1453 if (!WS.isExternal() && WS.isDefined())
1455 if (WS.isUndefined())
1457 if (WS.isNoStrip()) {
1459 if (isEmscripten()) {
1463 if (WS.hasImportName())
1465 if (WS.hasExportName())
1469 Info.
Name = WS.getName();
1470 Info.
Kind = WS.getType();
1473 assert(WasmIndices.count(&WS) > 0);
1475 }
else if (WS.isDefined()) {
1476 assert(DataLocations.count(&WS) > 0);
1477 Info.
DataRef = DataLocations.find(&WS)->second;
1479 WS.setIndex(SymbolInfos.
size());
1484 auto HandleReloc = [&](
const WasmRelocationEntry &Rel) {
1488 if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 &&
1489 Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB)
1491 assert(Rel.Symbol->isFunction());
1493 uint32_t FunctionIndex = WasmIndices.find(&WS)->second;
1494 uint32_t TableIndex = TableElems.
size() + InitialTableOffset;
1495 if (TableIndices.try_emplace(&WS, TableIndex).second) {
1497 <<
" to table: " << TableIndex <<
"\n");
1499 registerFunctionType(WS);
1503 for (
const WasmRelocationEntry &RelEntry : CodeRelocations)
1504 HandleReloc(RelEntry);
1505 for (
const WasmRelocationEntry &RelEntry : DataRelocations)
1506 HandleReloc(RelEntry);
1512 if (WS.getSectionName().startswith(
".fini_array"))
1514 if (!WS.getSectionName().startswith(
".init_array"))
1516 if (WS.getFragmentList().empty())
1520 if (WS.getFragmentList().size() != 3)
1523 auto IT = WS.begin();
1532 if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (
is64Bit() ? 8 : 4))
1540 unsigned PrefixLength = strlen(
".init_array");
1541 if (WS.getSectionName().size() > PrefixLength) {
1542 if (WS.getSectionName()[PrefixLength] !=
'.')
1544 ".init_array section priority should start with '.'");
1545 if (WS.getSectionName()
1546 .substr(PrefixLength + 1)
1547 .getAsInteger(10, Priority))
1550 const auto &DataFrag = cast<MCDataFragment>(Frag);
1552 for (
const uint8_t *
1553 P = (
const uint8_t *)Contents.data(),
1554 *End = (
const uint8_t *)Contents.data() + Contents.size();
1559 for (
const MCFixup &Fixup : DataFrag.getFixups()) {
1566 const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol());
1567 if (TargetSym.getIndex() == InvalidIndex)
1569 if (!TargetSym.isFunction())
1572 std::make_pair(Priority, TargetSym.getIndex()));
1579 writeTypeSection(Signatures);
1580 writeImportSection(Imports, DataSize, TableElems.
size());
1581 writeFunctionSection(Functions);
1584 writeEventSection(Events);
1585 writeExportSection(Exports);
1586 writeElemSection(TableElems);
1587 writeDataCountSection();
1588 writeCodeSection(Asm, Layout, Functions);
1590 for (
auto &CustomSection : CustomSections)
1591 writeCustomSection(CustomSection, Asm, Layout);
1592 writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
1593 writeRelocSection(CodeSectionIndex,
"CODE", CodeRelocations);
1594 writeRelocSection(DataSectionIndex,
"DATA", DataRelocations);
1595 writeCustomRelocSections();
1596 if (ProducersSection)
1597 writeCustomSection(*ProducersSection, Asm, Layout);
1598 if (TargetFeaturesSection)
1599 writeCustomSection(*TargetFeaturesSection, Asm, Layout);
1602 return W.OS.tell() - StartOffset;
1605 std::unique_ptr<MCObjectWriter>
1608 return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
Instances of this class represent a uniqued identifier for a section in the current translation unit...
reference emplace_back(ArgTypes &&... Args)
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
SectionKind getKind() const
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
bool isVariable() const
isVariable - Check if this is a variable symbol.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
This represents an "assembler immediate".
bool relocTypeHasAddend(uint32_t type)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
VariantKind getKind() const
const unsigned WASM_SYMBOL_BINDING_LOCAL
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
void push_back(const T &Elt)
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
const uint32_t WasmMetadataVersion
StringRef getSectionName() const
FragmentType getKind() const
void write32le(void *P, uint32_t V)
Defines the object file and target independent interfaces used by the assembler backend to write nati...
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
unsigned getAlignment() const
Export information to summary.
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
MCContext & getContext() const
const unsigned WASM_SYMBOL_UNDEFINED
int64_t getConstant() const
const MCSymbolRefExpr * getSymB() const
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Encapsulates the layout of an assembly file at a particular point in time.
Base class for the full range of assembler expressions which are needed for parsing.
The access may reference the value stored in memory.
const unsigned WASM_SYMBOL_NO_STRIP
Represent a reference to a symbol from inside an expression.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
const uint32_t WasmVersion
Context object for machine code objects.
const wasm::WasmSignature * getSignature() const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
std::string relocTypetoString(uint32_t type)
LLVM_NODISCARD size_t size() const
size - Get the string size.
Analysis containing CSE Info
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
Import information from summary.
The instances of the Type class are immutable: once they are created, they are never changed...
const unsigned WASM_SYMBOL_VISIBILITY_HIDDEN
size_t size() const
size - Get the array size.
This represents a section on wasm.
virtual void reset()
lifetime management
const unsigned WASM_SYMBOL_BINDING_WEAK
bool getSymbolOffset(const MCSymbol &S, uint64_t &Val) const
Get the offset of the given symbol, as computed in the current layout.
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
void pwrite(const char *Ptr, size_t Size, uint64_t Offset)
std::unique_ptr< MCObjectWriter > createWasmObjectWriter(std::unique_ptr< MCWasmObjectTargetWriter > MOTW, raw_pwrite_stream &OS)
Construct a new Wasm writer instance.
const MCSymbolRefExpr * getSymA() const
void reportError(SMLoc L, const Twine &Msg)
static void addData(SmallVectorImpl< char > &DataBytes, MCSectionWasm &DataSection)
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
uint32_t getOffset() const
void writeSectionData(raw_ostream &OS, const MCSection *Section, const MCAsmLayout &Layout) const
Emit the section contents to OS.
const MCExpr * getSize() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This struct is a compact representation of a valid (non-zero power of two) alignment.
uint64_t getFragmentOffset(const MCFragment *F) const
Get the offset of the given fragment inside its containing section.
PowerPC TLS Dynamic Call Fixup
const unsigned WASM_SYMBOL_EXPLICIT_NAME
MCAsmBackend & getBackend() const
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
const MCSymbol & getSymbol() const
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
MCSymbol * getBeginSymbol()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
bool isDefined() const
isDefined - Check if this symbol is defined (i.e., it has an address).
Target - Wrapper for Target specific information.
MCSection * getParent() const
bool hasInstructions() const
Does this fragment have instructions emitted into it? By default this is false, but specific fragment...
iterator insert(iterator I, T &&Elt)
bool isUsedInReloc() const
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate IT block based on arch"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT, "arm-no-restrict-it", "Allow IT blocks based on ARMv7")))
Adapter to write values to a stream in a particular byte order.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
StringRef getName() const
Return a constant reference to the value's name.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
StringRef getName() const
getName - Get the symbol name.
An abstract base class for streams implementations that also support a pwrite operation.
static MCFixupKind getKindForSize(unsigned Size, bool IsPCRel)
Return the generic fixup kind for a value with the given size.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const MCExpr * getVariableValue(bool SetUsed=true) const
getVariableValue - Get the value for variable symbols.
void stable_sort(R &&Range)
WasmDataReference DataRef
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream...
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
const MCExpr * getValue() const
const uint32_t WasmPageSize
StringRef - Represent a constant reference to a string, i.e.
bool operator==(uint64_t V1, const APInt &V2)
static const MCSymbolWasm * resolveSymbol(const MCSymbolWasm &Symbol)
const unsigned WASM_SYMBOL_EXPORTED
static bool isInSymtab(const MCSymbolWasm &Sym)
void setIndex(uint32_t Value) const
Set the (implementation defined) index.
static bool is64Bit(const char *name)
MCFixupKind getKind() const
bool empty() const
empty - Check if the array is empty.