WasmObjectWriter.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869
  1. //===- lib/MC/WasmObjectWriter.cpp - Wasm File Writer ---------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements Wasm object file writer information.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/ADT/STLExtras.h"
  13. #include "llvm/ADT/SmallPtrSet.h"
  14. #include "llvm/BinaryFormat/Wasm.h"
  15. #include "llvm/BinaryFormat/WasmTraits.h"
  16. #include "llvm/Config/llvm-config.h"
  17. #include "llvm/MC/MCAsmBackend.h"
  18. #include "llvm/MC/MCAsmLayout.h"
  19. #include "llvm/MC/MCAssembler.h"
  20. #include "llvm/MC/MCContext.h"
  21. #include "llvm/MC/MCExpr.h"
  22. #include "llvm/MC/MCFixupKindInfo.h"
  23. #include "llvm/MC/MCObjectWriter.h"
  24. #include "llvm/MC/MCSectionWasm.h"
  25. #include "llvm/MC/MCSymbolWasm.h"
  26. #include "llvm/MC/MCValue.h"
  27. #include "llvm/MC/MCWasmObjectWriter.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/Debug.h"
  30. #include "llvm/Support/EndianStream.h"
  31. #include "llvm/Support/ErrorHandling.h"
  32. #include "llvm/Support/LEB128.h"
  33. #include "llvm/Support/StringSaver.h"
  34. #include <vector>
  35. using namespace llvm;
  36. #define DEBUG_TYPE "mc"
  37. namespace {
  38. // When we create the indirect function table we start at 1, so that there is
  39. // and empty slot at 0 and therefore calling a null function pointer will trap.
  40. static const uint32_t InitialTableOffset = 1;
  41. // For patching purposes, we need to remember where each section starts, both
  42. // for patching up the section size field, and for patching up references to
  43. // locations within the section.
  44. struct SectionBookkeeping {
  45. // Where the size of the section is written.
  46. uint64_t SizeOffset;
  47. // Where the section header ends (without custom section name).
  48. uint64_t PayloadOffset;
  49. // Where the contents of the section starts.
  50. uint64_t ContentsOffset;
  51. uint32_t Index;
  52. };
  53. // A wasm data segment. A wasm binary contains only a single data section
  54. // but that can contain many segments, each with their own virtual location
  55. // in memory. Each MCSection data created by llvm is modeled as its own
  56. // wasm data segment.
  57. struct WasmDataSegment {
  58. MCSectionWasm *Section;
  59. StringRef Name;
  60. uint32_t InitFlags;
  61. uint64_t Offset;
  62. uint32_t Alignment;
  63. uint32_t LinkerFlags;
  64. SmallVector<char, 4> Data;
  65. };
  66. // A wasm function to be written into the function section.
  67. struct WasmFunction {
  68. uint32_t SigIndex;
  69. const MCSymbolWasm *Sym;
  70. };
  71. // A wasm global to be written into the global section.
  72. struct WasmGlobal {
  73. wasm::WasmGlobalType Type;
  74. uint64_t InitialValue;
  75. };
  76. // Information about a single item which is part of a COMDAT. For each data
  77. // segment or function which is in the COMDAT, there is a corresponding
  78. // WasmComdatEntry.
  79. struct WasmComdatEntry {
  80. unsigned Kind;
  81. uint32_t Index;
  82. };
  83. // Information about a single relocation.
  84. struct WasmRelocationEntry {
  85. uint64_t Offset; // Where is the relocation.
  86. const MCSymbolWasm *Symbol; // The symbol to relocate with.
  87. int64_t Addend; // A value to add to the symbol.
  88. unsigned Type; // The type of the relocation.
  89. const MCSectionWasm *FixupSection; // The section the relocation is targeting.
  90. WasmRelocationEntry(uint64_t Offset, const MCSymbolWasm *Symbol,
  91. int64_t Addend, unsigned Type,
  92. const MCSectionWasm *FixupSection)
  93. : Offset(Offset), Symbol(Symbol), Addend(Addend), Type(Type),
  94. FixupSection(FixupSection) {}
  95. bool hasAddend() const { return wasm::relocTypeHasAddend(Type); }
  96. void print(raw_ostream &Out) const {
  97. Out << wasm::relocTypetoString(Type) << " Off=" << Offset
  98. << ", Sym=" << *Symbol << ", Addend=" << Addend
  99. << ", FixupSection=" << FixupSection->getName();
  100. }
  101. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  102. LLVM_DUMP_METHOD void dump() const { print(dbgs()); }
  103. #endif
  104. };
  105. static const uint32_t InvalidIndex = -1;
  106. struct WasmCustomSection {
  107. StringRef Name;
  108. MCSectionWasm *Section;
  109. uint32_t OutputContentsOffset;
  110. uint32_t OutputIndex;
  111. WasmCustomSection(StringRef Name, MCSectionWasm *Section)
  112. : Name(Name), Section(Section), OutputContentsOffset(0),
  113. OutputIndex(InvalidIndex) {}
  114. };
  115. #if !defined(NDEBUG)
  116. raw_ostream &operator<<(raw_ostream &OS, const WasmRelocationEntry &Rel) {
  117. Rel.print(OS);
  118. return OS;
  119. }
  120. #endif
  121. // Write X as an (unsigned) LEB value at offset Offset in Stream, padded
  122. // to allow patching.
  123. template <int W>
  124. void writePatchableLEB(raw_pwrite_stream &Stream, uint64_t X, uint64_t Offset) {
  125. uint8_t Buffer[W];
  126. unsigned SizeLen = encodeULEB128(X, Buffer, W);
  127. assert(SizeLen == W);
  128. Stream.pwrite((char *)Buffer, SizeLen, Offset);
  129. }
  130. // Write X as an signed LEB value at offset Offset in Stream, padded
  131. // to allow patching.
  132. template <int W>
  133. void writePatchableSLEB(raw_pwrite_stream &Stream, int64_t X, uint64_t Offset) {
  134. uint8_t Buffer[W];
  135. unsigned SizeLen = encodeSLEB128(X, Buffer, W);
  136. assert(SizeLen == W);
  137. Stream.pwrite((char *)Buffer, SizeLen, Offset);
  138. }
  139. // Write X as a plain integer value at offset Offset in Stream.
  140. static void patchI32(raw_pwrite_stream &Stream, uint32_t X, uint64_t Offset) {
  141. uint8_t Buffer[4];
  142. support::endian::write32le(Buffer, X);
  143. Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
  144. }
  145. static void patchI64(raw_pwrite_stream &Stream, uint64_t X, uint64_t Offset) {
  146. uint8_t Buffer[8];
  147. support::endian::write64le(Buffer, X);
  148. Stream.pwrite((char *)Buffer, sizeof(Buffer), Offset);
  149. }
  150. bool isDwoSection(const MCSection &Sec) {
  151. return Sec.getName().endswith(".dwo");
  152. }
  153. class WasmObjectWriter : public MCObjectWriter {
  154. support::endian::Writer *W;
  155. /// The target specific Wasm writer instance.
  156. std::unique_ptr<MCWasmObjectTargetWriter> TargetObjectWriter;
  157. // Relocations for fixing up references in the code section.
  158. std::vector<WasmRelocationEntry> CodeRelocations;
  159. // Relocations for fixing up references in the data section.
  160. std::vector<WasmRelocationEntry> DataRelocations;
  161. // Index values to use for fixing up call_indirect type indices.
  162. // Maps function symbols to the index of the type of the function
  163. DenseMap<const MCSymbolWasm *, uint32_t> TypeIndices;
  164. // Maps function symbols to the table element index space. Used
  165. // for TABLE_INDEX relocation types (i.e. address taken functions).
  166. DenseMap<const MCSymbolWasm *, uint32_t> TableIndices;
  167. // Maps function/global/table symbols to the
  168. // function/global/table/event/section index space.
  169. DenseMap<const MCSymbolWasm *, uint32_t> WasmIndices;
  170. DenseMap<const MCSymbolWasm *, uint32_t> GOTIndices;
  171. // Maps data symbols to the Wasm segment and offset/size with the segment.
  172. DenseMap<const MCSymbolWasm *, wasm::WasmDataReference> DataLocations;
  173. // Stores output data (index, relocations, content offset) for custom
  174. // section.
  175. std::vector<WasmCustomSection> CustomSections;
  176. std::unique_ptr<WasmCustomSection> ProducersSection;
  177. std::unique_ptr<WasmCustomSection> TargetFeaturesSection;
  178. // Relocations for fixing up references in the custom sections.
  179. DenseMap<const MCSectionWasm *, std::vector<WasmRelocationEntry>>
  180. CustomSectionsRelocations;
  181. // Map from section to defining function symbol.
  182. DenseMap<const MCSection *, const MCSymbol *> SectionFunctions;
  183. DenseMap<wasm::WasmSignature, uint32_t> SignatureIndices;
  184. SmallVector<wasm::WasmSignature, 4> Signatures;
  185. SmallVector<WasmDataSegment, 4> DataSegments;
  186. unsigned NumFunctionImports = 0;
  187. unsigned NumGlobalImports = 0;
  188. unsigned NumTableImports = 0;
  189. unsigned NumEventImports = 0;
  190. uint32_t SectionCount = 0;
  191. enum class DwoMode {
  192. AllSections,
  193. NonDwoOnly,
  194. DwoOnly,
  195. };
  196. bool IsSplitDwarf = false;
  197. raw_pwrite_stream *OS = nullptr;
  198. raw_pwrite_stream *DwoOS = nullptr;
  199. // TargetObjectWriter wranppers.
  200. bool is64Bit() const { return TargetObjectWriter->is64Bit(); }
  201. bool isEmscripten() const { return TargetObjectWriter->isEmscripten(); }
  202. void startSection(SectionBookkeeping &Section, unsigned SectionId);
  203. void startCustomSection(SectionBookkeeping &Section, StringRef Name);
  204. void endSection(SectionBookkeeping &Section);
  205. public:
  206. WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
  207. raw_pwrite_stream &OS_)
  208. : TargetObjectWriter(std::move(MOTW)), OS(&OS_) {}
  209. WasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
  210. raw_pwrite_stream &OS_, raw_pwrite_stream &DwoOS_)
  211. : TargetObjectWriter(std::move(MOTW)), IsSplitDwarf(true), OS(&OS_),
  212. DwoOS(&DwoOS_) {}
  213. private:
  214. void reset() override {
  215. CodeRelocations.clear();
  216. DataRelocations.clear();
  217. TypeIndices.clear();
  218. WasmIndices.clear();
  219. GOTIndices.clear();
  220. TableIndices.clear();
  221. DataLocations.clear();
  222. CustomSections.clear();
  223. ProducersSection.reset();
  224. TargetFeaturesSection.reset();
  225. CustomSectionsRelocations.clear();
  226. SignatureIndices.clear();
  227. Signatures.clear();
  228. DataSegments.clear();
  229. SectionFunctions.clear();
  230. NumFunctionImports = 0;
  231. NumGlobalImports = 0;
  232. NumTableImports = 0;
  233. MCObjectWriter::reset();
  234. }
  235. void writeHeader(const MCAssembler &Asm);
  236. void recordRelocation(MCAssembler &Asm, const MCAsmLayout &Layout,
  237. const MCFragment *Fragment, const MCFixup &Fixup,
  238. MCValue Target, uint64_t &FixedValue) override;
  239. void executePostLayoutBinding(MCAssembler &Asm,
  240. const MCAsmLayout &Layout) override;
  241. void prepareImports(SmallVectorImpl<wasm::WasmImport> &Imports,
  242. MCAssembler &Asm, const MCAsmLayout &Layout);
  243. uint64_t writeObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
  244. uint64_t writeOneObject(MCAssembler &Asm, const MCAsmLayout &Layout,
  245. DwoMode Mode);
  246. void writeString(const StringRef Str) {
  247. encodeULEB128(Str.size(), W->OS);
  248. W->OS << Str;
  249. }
  250. void writeI32(int32_t val) {
  251. char Buffer[4];
  252. support::endian::write32le(Buffer, val);
  253. W->OS.write(Buffer, sizeof(Buffer));
  254. }
  255. void writeI64(int64_t val) {
  256. char Buffer[8];
  257. support::endian::write64le(Buffer, val);
  258. W->OS.write(Buffer, sizeof(Buffer));
  259. }
  260. void writeValueType(wasm::ValType Ty) { W->OS << static_cast<char>(Ty); }
  261. void writeTypeSection(ArrayRef<wasm::WasmSignature> Signatures);
  262. void writeImportSection(ArrayRef<wasm::WasmImport> Imports, uint64_t DataSize,
  263. uint32_t NumElements);
  264. void writeFunctionSection(ArrayRef<WasmFunction> Functions);
  265. void writeExportSection(ArrayRef<wasm::WasmExport> Exports);
  266. void writeElemSection(ArrayRef<uint32_t> TableElems);
  267. void writeDataCountSection();
  268. uint32_t writeCodeSection(const MCAssembler &Asm, const MCAsmLayout &Layout,
  269. ArrayRef<WasmFunction> Functions);
  270. uint32_t writeDataSection(const MCAsmLayout &Layout);
  271. void writeEventSection(ArrayRef<wasm::WasmEventType> Events);
  272. void writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals);
  273. void writeTableSection(ArrayRef<wasm::WasmTable> Tables);
  274. void writeRelocSection(uint32_t SectionIndex, StringRef Name,
  275. std::vector<WasmRelocationEntry> &Relocations);
  276. void writeLinkingMetaDataSection(
  277. ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
  278. ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
  279. const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats);
  280. void writeCustomSection(WasmCustomSection &CustomSection,
  281. const MCAssembler &Asm, const MCAsmLayout &Layout);
  282. void writeCustomRelocSections();
  283. uint64_t getProvisionalValue(const WasmRelocationEntry &RelEntry,
  284. const MCAsmLayout &Layout);
  285. void applyRelocations(ArrayRef<WasmRelocationEntry> Relocations,
  286. uint64_t ContentsOffset, const MCAsmLayout &Layout);
  287. uint32_t getRelocationIndexValue(const WasmRelocationEntry &RelEntry);
  288. uint32_t getFunctionType(const MCSymbolWasm &Symbol);
  289. uint32_t getEventType(const MCSymbolWasm &Symbol);
  290. void registerFunctionType(const MCSymbolWasm &Symbol);
  291. void registerEventType(const MCSymbolWasm &Symbol);
  292. };
  293. } // end anonymous namespace
  294. // Write out a section header and a patchable section size field.
  295. void WasmObjectWriter::startSection(SectionBookkeeping &Section,
  296. unsigned SectionId) {
  297. LLVM_DEBUG(dbgs() << "startSection " << SectionId << "\n");
  298. W->OS << char(SectionId);
  299. Section.SizeOffset = W->OS.tell();
  300. // The section size. We don't know the size yet, so reserve enough space
  301. // for any 32-bit value; we'll patch it later.
  302. encodeULEB128(0, W->OS, 5);
  303. // The position where the section starts, for measuring its size.
  304. Section.ContentsOffset = W->OS.tell();
  305. Section.PayloadOffset = W->OS.tell();
  306. Section.Index = SectionCount++;
  307. }
  308. void WasmObjectWriter::startCustomSection(SectionBookkeeping &Section,
  309. StringRef Name) {
  310. LLVM_DEBUG(dbgs() << "startCustomSection " << Name << "\n");
  311. startSection(Section, wasm::WASM_SEC_CUSTOM);
  312. // The position where the section header ends, for measuring its size.
  313. Section.PayloadOffset = W->OS.tell();
  314. // Custom sections in wasm also have a string identifier.
  315. writeString(Name);
  316. // The position where the custom section starts.
  317. Section.ContentsOffset = W->OS.tell();
  318. }
  319. // Now that the section is complete and we know how big it is, patch up the
  320. // section size field at the start of the section.
  321. void WasmObjectWriter::endSection(SectionBookkeeping &Section) {
  322. uint64_t Size = W->OS.tell();
  323. // /dev/null doesn't support seek/tell and can report offset of 0.
  324. // Simply skip this patching in that case.
  325. if (!Size)
  326. return;
  327. Size -= Section.PayloadOffset;
  328. if (uint32_t(Size) != Size)
  329. report_fatal_error("section size does not fit in a uint32_t");
  330. LLVM_DEBUG(dbgs() << "endSection size=" << Size << "\n");
  331. // Write the final section size to the payload_len field, which follows
  332. // the section id byte.
  333. writePatchableLEB<5>(static_cast<raw_pwrite_stream &>(W->OS), Size,
  334. Section.SizeOffset);
  335. }
  336. // Emit the Wasm header.
  337. void WasmObjectWriter::writeHeader(const MCAssembler &Asm) {
  338. W->OS.write(wasm::WasmMagic, sizeof(wasm::WasmMagic));
  339. W->write<uint32_t>(wasm::WasmVersion);
  340. }
  341. void WasmObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
  342. const MCAsmLayout &Layout) {
  343. // As a stopgap measure until call_indirect instructions start explicitly
  344. // referencing the indirect function table via TABLE_NUMBER relocs, ensure
  345. // that the indirect function table import makes it to the output if anything
  346. // in the compilation unit has caused it to be present.
  347. if (auto *Sym = Asm.getContext().lookupSymbol("__indirect_function_table"))
  348. Asm.registerSymbol(*Sym);
  349. // Build a map of sections to the function that defines them, for use
  350. // in recordRelocation.
  351. for (const MCSymbol &S : Asm.symbols()) {
  352. const auto &WS = static_cast<const MCSymbolWasm &>(S);
  353. if (WS.isDefined() && WS.isFunction() && !WS.isVariable()) {
  354. const auto &Sec = static_cast<const MCSectionWasm &>(S.getSection());
  355. auto Pair = SectionFunctions.insert(std::make_pair(&Sec, &S));
  356. if (!Pair.second)
  357. report_fatal_error("section already has a defining function: " +
  358. Sec.getName());
  359. }
  360. }
  361. }
  362. void WasmObjectWriter::recordRelocation(MCAssembler &Asm,
  363. const MCAsmLayout &Layout,
  364. const MCFragment *Fragment,
  365. const MCFixup &Fixup, MCValue Target,
  366. uint64_t &FixedValue) {
  367. // The WebAssembly backend should never generate FKF_IsPCRel fixups
  368. assert(!(Asm.getBackend().getFixupKindInfo(Fixup.getKind()).Flags &
  369. MCFixupKindInfo::FKF_IsPCRel));
  370. const auto &FixupSection = cast<MCSectionWasm>(*Fragment->getParent());
  371. uint64_t C = Target.getConstant();
  372. uint64_t FixupOffset = Layout.getFragmentOffset(Fragment) + Fixup.getOffset();
  373. MCContext &Ctx = Asm.getContext();
  374. if (const MCSymbolRefExpr *RefB = Target.getSymB()) {
  375. // To get here the A - B expression must have failed evaluateAsRelocatable.
  376. // This means either A or B must be undefined and in WebAssembly we can't
  377. // support either of those cases.
  378. const auto &SymB = cast<MCSymbolWasm>(RefB->getSymbol());
  379. Ctx.reportError(
  380. Fixup.getLoc(),
  381. Twine("symbol '") + SymB.getName() +
  382. "': unsupported subtraction expression used in relocation.");
  383. return;
  384. }
  385. // We either rejected the fixup or folded B into C at this point.
  386. const MCSymbolRefExpr *RefA = Target.getSymA();
  387. const auto *SymA = cast<MCSymbolWasm>(&RefA->getSymbol());
  388. // The .init_array isn't translated as data, so don't do relocations in it.
  389. if (FixupSection.getName().startswith(".init_array")) {
  390. SymA->setUsedInInitArray();
  391. return;
  392. }
  393. if (SymA->isVariable()) {
  394. const MCExpr *Expr = SymA->getVariableValue();
  395. if (const auto *Inner = dyn_cast<MCSymbolRefExpr>(Expr))
  396. if (Inner->getKind() == MCSymbolRefExpr::VK_WEAKREF)
  397. llvm_unreachable("weakref used in reloc not yet implemented");
  398. }
  399. // Put any constant offset in an addend. Offsets can be negative, and
  400. // LLVM expects wrapping, in contrast to wasm's immediates which can't
  401. // be negative and don't wrap.
  402. FixedValue = 0;
  403. unsigned Type = TargetObjectWriter->getRelocType(Target, Fixup);
  404. // Absolute offset within a section or a function.
  405. // Currently only supported for for metadata sections.
  406. // See: test/MC/WebAssembly/blockaddress.ll
  407. if (Type == wasm::R_WASM_FUNCTION_OFFSET_I32 ||
  408. Type == wasm::R_WASM_FUNCTION_OFFSET_I64 ||
  409. Type == wasm::R_WASM_SECTION_OFFSET_I32) {
  410. if (!FixupSection.getKind().isMetadata())
  411. report_fatal_error("relocations for function or section offsets are "
  412. "only supported in metadata sections");
  413. const MCSymbol *SectionSymbol = nullptr;
  414. const MCSection &SecA = SymA->getSection();
  415. if (SecA.getKind().isText())
  416. SectionSymbol = SectionFunctions.find(&SecA)->second;
  417. else
  418. SectionSymbol = SecA.getBeginSymbol();
  419. if (!SectionSymbol)
  420. report_fatal_error("section symbol is required for relocation");
  421. C += Layout.getSymbolOffset(*SymA);
  422. SymA = cast<MCSymbolWasm>(SectionSymbol);
  423. }
  424. if (Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB ||
  425. Type == wasm::R_WASM_TABLE_INDEX_SLEB ||
  426. Type == wasm::R_WASM_TABLE_INDEX_SLEB64 ||
  427. Type == wasm::R_WASM_TABLE_INDEX_I32 ||
  428. Type == wasm::R_WASM_TABLE_INDEX_I64) {
  429. // TABLE_INDEX relocs implicitly use the default indirect function table.
  430. auto TableName = "__indirect_function_table";
  431. MCSymbolWasm *Sym = cast_or_null<MCSymbolWasm>(Ctx.lookupSymbol(TableName));
  432. if (Sym) {
  433. if (!Sym->isFunctionTable())
  434. Ctx.reportError(
  435. Fixup.getLoc(),
  436. "symbol '__indirect_function_table' is not a function table");
  437. } else {
  438. Sym = cast<MCSymbolWasm>(Ctx.getOrCreateSymbol(TableName));
  439. Sym->setFunctionTable();
  440. // The default function table is synthesized by the linker.
  441. Sym->setUndefined();
  442. }
  443. Sym->setUsedInReloc();
  444. Asm.registerSymbol(*Sym);
  445. }
  446. // Relocation other than R_WASM_TYPE_INDEX_LEB are required to be
  447. // against a named symbol.
  448. if (Type != wasm::R_WASM_TYPE_INDEX_LEB) {
  449. if (SymA->getName().empty())
  450. report_fatal_error("relocations against un-named temporaries are not yet "
  451. "supported by wasm");
  452. SymA->setUsedInReloc();
  453. }
  454. if (RefA->getKind() == MCSymbolRefExpr::VK_GOT)
  455. SymA->setUsedInGOT();
  456. WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
  457. LLVM_DEBUG(dbgs() << "WasmReloc: " << Rec << "\n");
  458. if (FixupSection.isWasmData()) {
  459. DataRelocations.push_back(Rec);
  460. } else if (FixupSection.getKind().isText()) {
  461. CodeRelocations.push_back(Rec);
  462. } else if (FixupSection.getKind().isMetadata()) {
  463. CustomSectionsRelocations[&FixupSection].push_back(Rec);
  464. } else {
  465. llvm_unreachable("unexpected section type");
  466. }
  467. }
  468. // Compute a value to write into the code at the location covered
  469. // by RelEntry. This value isn't used by the static linker; it just serves
  470. // to make the object format more readable and more likely to be directly
  471. // useable.
  472. uint64_t
  473. WasmObjectWriter::getProvisionalValue(const WasmRelocationEntry &RelEntry,
  474. const MCAsmLayout &Layout) {
  475. if ((RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_LEB ||
  476. RelEntry.Type == wasm::R_WASM_GLOBAL_INDEX_I32) &&
  477. !RelEntry.Symbol->isGlobal()) {
  478. assert(GOTIndices.count(RelEntry.Symbol) > 0 && "symbol not found in GOT index space");
  479. return GOTIndices[RelEntry.Symbol];
  480. }
  481. switch (RelEntry.Type) {
  482. case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
  483. case wasm::R_WASM_TABLE_INDEX_SLEB:
  484. case wasm::R_WASM_TABLE_INDEX_SLEB64:
  485. case wasm::R_WASM_TABLE_INDEX_I32:
  486. case wasm::R_WASM_TABLE_INDEX_I64: {
  487. // Provisional value is table address of the resolved symbol itself
  488. const MCSymbolWasm *Base =
  489. cast<MCSymbolWasm>(Layout.getBaseSymbol(*RelEntry.Symbol));
  490. assert(Base->isFunction());
  491. if (RelEntry.Type == wasm::R_WASM_TABLE_INDEX_REL_SLEB)
  492. return TableIndices[Base] - InitialTableOffset;
  493. else
  494. return TableIndices[Base];
  495. }
  496. case wasm::R_WASM_TYPE_INDEX_LEB:
  497. // Provisional value is same as the index
  498. return getRelocationIndexValue(RelEntry);
  499. case wasm::R_WASM_FUNCTION_INDEX_LEB:
  500. case wasm::R_WASM_GLOBAL_INDEX_LEB:
  501. case wasm::R_WASM_GLOBAL_INDEX_I32:
  502. case wasm::R_WASM_EVENT_INDEX_LEB:
  503. case wasm::R_WASM_TABLE_NUMBER_LEB:
  504. // Provisional value is function/global/event Wasm index
  505. assert(WasmIndices.count(RelEntry.Symbol) > 0 && "symbol not found in wasm index space");
  506. return WasmIndices[RelEntry.Symbol];
  507. case wasm::R_WASM_FUNCTION_OFFSET_I32:
  508. case wasm::R_WASM_FUNCTION_OFFSET_I64:
  509. case wasm::R_WASM_SECTION_OFFSET_I32: {
  510. const auto &Section =
  511. static_cast<const MCSectionWasm &>(RelEntry.Symbol->getSection());
  512. return Section.getSectionOffset() + RelEntry.Addend;
  513. }
  514. case wasm::R_WASM_MEMORY_ADDR_LEB:
  515. case wasm::R_WASM_MEMORY_ADDR_LEB64:
  516. case wasm::R_WASM_MEMORY_ADDR_SLEB:
  517. case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  518. case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
  519. case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
  520. case wasm::R_WASM_MEMORY_ADDR_I32:
  521. case wasm::R_WASM_MEMORY_ADDR_I64:
  522. case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB: {
  523. // Provisional value is address of the global plus the offset
  524. const MCSymbolWasm *Base =
  525. cast<MCSymbolWasm>(Layout.getBaseSymbol(*RelEntry.Symbol));
  526. // For undefined symbols, use zero
  527. if (!Base->isDefined())
  528. return 0;
  529. const wasm::WasmDataReference &BaseRef = DataLocations[Base],
  530. &SymRef = DataLocations[RelEntry.Symbol];
  531. const WasmDataSegment &Segment = DataSegments[BaseRef.Segment];
  532. // Ignore overflow. LLVM allows address arithmetic to silently wrap.
  533. return Segment.Offset + BaseRef.Offset + SymRef.Offset + RelEntry.Addend;
  534. }
  535. default:
  536. llvm_unreachable("invalid relocation type");
  537. }
  538. }
  539. static void addData(SmallVectorImpl<char> &DataBytes,
  540. MCSectionWasm &DataSection) {
  541. LLVM_DEBUG(errs() << "addData: " << DataSection.getName() << "\n");
  542. DataBytes.resize(alignTo(DataBytes.size(), DataSection.getAlignment()));
  543. for (const MCFragment &Frag : DataSection) {
  544. if (Frag.hasInstructions())
  545. report_fatal_error("only data supported in data sections");
  546. if (auto *Align = dyn_cast<MCAlignFragment>(&Frag)) {
  547. if (Align->getValueSize() != 1)
  548. report_fatal_error("only byte values supported for alignment");
  549. // If nops are requested, use zeros, as this is the data section.
  550. uint8_t Value = Align->hasEmitNops() ? 0 : Align->getValue();
  551. uint64_t Size =
  552. std::min<uint64_t>(alignTo(DataBytes.size(), Align->getAlignment()),
  553. DataBytes.size() + Align->getMaxBytesToEmit());
  554. DataBytes.resize(Size, Value);
  555. } else if (auto *Fill = dyn_cast<MCFillFragment>(&Frag)) {
  556. int64_t NumValues;
  557. if (!Fill->getNumValues().evaluateAsAbsolute(NumValues))
  558. llvm_unreachable("The fill should be an assembler constant");
  559. DataBytes.insert(DataBytes.end(), Fill->getValueSize() * NumValues,
  560. Fill->getValue());
  561. } else if (auto *LEB = dyn_cast<MCLEBFragment>(&Frag)) {
  562. const SmallVectorImpl<char> &Contents = LEB->getContents();
  563. llvm::append_range(DataBytes, Contents);
  564. } else {
  565. const auto &DataFrag = cast<MCDataFragment>(Frag);
  566. const SmallVectorImpl<char> &Contents = DataFrag.getContents();
  567. llvm::append_range(DataBytes, Contents);
  568. }
  569. }
  570. LLVM_DEBUG(dbgs() << "addData -> " << DataBytes.size() << "\n");
  571. }
  572. uint32_t
  573. WasmObjectWriter::getRelocationIndexValue(const WasmRelocationEntry &RelEntry) {
  574. if (RelEntry.Type == wasm::R_WASM_TYPE_INDEX_LEB) {
  575. if (!TypeIndices.count(RelEntry.Symbol))
  576. report_fatal_error("symbol not found in type index space: " +
  577. RelEntry.Symbol->getName());
  578. return TypeIndices[RelEntry.Symbol];
  579. }
  580. return RelEntry.Symbol->getIndex();
  581. }
  582. // Apply the portions of the relocation records that we can handle ourselves
  583. // directly.
  584. void WasmObjectWriter::applyRelocations(
  585. ArrayRef<WasmRelocationEntry> Relocations, uint64_t ContentsOffset,
  586. const MCAsmLayout &Layout) {
  587. auto &Stream = static_cast<raw_pwrite_stream &>(W->OS);
  588. for (const WasmRelocationEntry &RelEntry : Relocations) {
  589. uint64_t Offset = ContentsOffset +
  590. RelEntry.FixupSection->getSectionOffset() +
  591. RelEntry.Offset;
  592. LLVM_DEBUG(dbgs() << "applyRelocation: " << RelEntry << "\n");
  593. auto Value = getProvisionalValue(RelEntry, Layout);
  594. switch (RelEntry.Type) {
  595. case wasm::R_WASM_FUNCTION_INDEX_LEB:
  596. case wasm::R_WASM_TYPE_INDEX_LEB:
  597. case wasm::R_WASM_GLOBAL_INDEX_LEB:
  598. case wasm::R_WASM_MEMORY_ADDR_LEB:
  599. case wasm::R_WASM_EVENT_INDEX_LEB:
  600. case wasm::R_WASM_TABLE_NUMBER_LEB:
  601. writePatchableLEB<5>(Stream, Value, Offset);
  602. break;
  603. case wasm::R_WASM_MEMORY_ADDR_LEB64:
  604. writePatchableLEB<10>(Stream, Value, Offset);
  605. break;
  606. case wasm::R_WASM_TABLE_INDEX_I32:
  607. case wasm::R_WASM_MEMORY_ADDR_I32:
  608. case wasm::R_WASM_FUNCTION_OFFSET_I32:
  609. case wasm::R_WASM_SECTION_OFFSET_I32:
  610. case wasm::R_WASM_GLOBAL_INDEX_I32:
  611. patchI32(Stream, Value, Offset);
  612. break;
  613. case wasm::R_WASM_TABLE_INDEX_I64:
  614. case wasm::R_WASM_MEMORY_ADDR_I64:
  615. case wasm::R_WASM_FUNCTION_OFFSET_I64:
  616. patchI64(Stream, Value, Offset);
  617. break;
  618. case wasm::R_WASM_TABLE_INDEX_SLEB:
  619. case wasm::R_WASM_TABLE_INDEX_REL_SLEB:
  620. case wasm::R_WASM_MEMORY_ADDR_SLEB:
  621. case wasm::R_WASM_MEMORY_ADDR_REL_SLEB:
  622. case wasm::R_WASM_MEMORY_ADDR_TLS_SLEB:
  623. writePatchableSLEB<5>(Stream, Value, Offset);
  624. break;
  625. case wasm::R_WASM_TABLE_INDEX_SLEB64:
  626. case wasm::R_WASM_MEMORY_ADDR_SLEB64:
  627. case wasm::R_WASM_MEMORY_ADDR_REL_SLEB64:
  628. writePatchableSLEB<10>(Stream, Value, Offset);
  629. break;
  630. default:
  631. llvm_unreachable("invalid relocation type");
  632. }
  633. }
  634. }
  635. void WasmObjectWriter::writeTypeSection(
  636. ArrayRef<wasm::WasmSignature> Signatures) {
  637. if (Signatures.empty())
  638. return;
  639. SectionBookkeeping Section;
  640. startSection(Section, wasm::WASM_SEC_TYPE);
  641. encodeULEB128(Signatures.size(), W->OS);
  642. for (const wasm::WasmSignature &Sig : Signatures) {
  643. W->OS << char(wasm::WASM_TYPE_FUNC);
  644. encodeULEB128(Sig.Params.size(), W->OS);
  645. for (wasm::ValType Ty : Sig.Params)
  646. writeValueType(Ty);
  647. encodeULEB128(Sig.Returns.size(), W->OS);
  648. for (wasm::ValType Ty : Sig.Returns)
  649. writeValueType(Ty);
  650. }
  651. endSection(Section);
  652. }
  653. void WasmObjectWriter::writeImportSection(ArrayRef<wasm::WasmImport> Imports,
  654. uint64_t DataSize,
  655. uint32_t NumElements) {
  656. if (Imports.empty())
  657. return;
  658. uint64_t NumPages = (DataSize + wasm::WasmPageSize - 1) / wasm::WasmPageSize;
  659. SectionBookkeeping Section;
  660. startSection(Section, wasm::WASM_SEC_IMPORT);
  661. encodeULEB128(Imports.size(), W->OS);
  662. for (const wasm::WasmImport &Import : Imports) {
  663. writeString(Import.Module);
  664. writeString(Import.Field);
  665. W->OS << char(Import.Kind);
  666. switch (Import.Kind) {
  667. case wasm::WASM_EXTERNAL_FUNCTION:
  668. encodeULEB128(Import.SigIndex, W->OS);
  669. break;
  670. case wasm::WASM_EXTERNAL_GLOBAL:
  671. W->OS << char(Import.Global.Type);
  672. W->OS << char(Import.Global.Mutable ? 1 : 0);
  673. break;
  674. case wasm::WASM_EXTERNAL_MEMORY:
  675. encodeULEB128(Import.Memory.Flags, W->OS);
  676. encodeULEB128(NumPages, W->OS); // initial
  677. break;
  678. case wasm::WASM_EXTERNAL_TABLE:
  679. W->OS << char(Import.Table.ElemType);
  680. encodeULEB128(0, W->OS); // flags
  681. encodeULEB128(NumElements, W->OS); // initial
  682. break;
  683. case wasm::WASM_EXTERNAL_EVENT:
  684. encodeULEB128(Import.Event.Attribute, W->OS);
  685. encodeULEB128(Import.Event.SigIndex, W->OS);
  686. break;
  687. default:
  688. llvm_unreachable("unsupported import kind");
  689. }
  690. }
  691. endSection(Section);
  692. }
  693. void WasmObjectWriter::writeFunctionSection(ArrayRef<WasmFunction> Functions) {
  694. if (Functions.empty())
  695. return;
  696. SectionBookkeeping Section;
  697. startSection(Section, wasm::WASM_SEC_FUNCTION);
  698. encodeULEB128(Functions.size(), W->OS);
  699. for (const WasmFunction &Func : Functions)
  700. encodeULEB128(Func.SigIndex, W->OS);
  701. endSection(Section);
  702. }
  703. void WasmObjectWriter::writeEventSection(ArrayRef<wasm::WasmEventType> Events) {
  704. if (Events.empty())
  705. return;
  706. SectionBookkeeping Section;
  707. startSection(Section, wasm::WASM_SEC_EVENT);
  708. encodeULEB128(Events.size(), W->OS);
  709. for (const wasm::WasmEventType &Event : Events) {
  710. encodeULEB128(Event.Attribute, W->OS);
  711. encodeULEB128(Event.SigIndex, W->OS);
  712. }
  713. endSection(Section);
  714. }
  715. void WasmObjectWriter::writeGlobalSection(ArrayRef<wasm::WasmGlobal> Globals) {
  716. if (Globals.empty())
  717. return;
  718. SectionBookkeeping Section;
  719. startSection(Section, wasm::WASM_SEC_GLOBAL);
  720. encodeULEB128(Globals.size(), W->OS);
  721. for (const wasm::WasmGlobal &Global : Globals) {
  722. encodeULEB128(Global.Type.Type, W->OS);
  723. W->OS << char(Global.Type.Mutable);
  724. W->OS << char(Global.InitExpr.Opcode);
  725. switch (Global.Type.Type) {
  726. case wasm::WASM_TYPE_I32:
  727. encodeSLEB128(0, W->OS);
  728. break;
  729. case wasm::WASM_TYPE_I64:
  730. encodeSLEB128(0, W->OS);
  731. break;
  732. case wasm::WASM_TYPE_F32:
  733. writeI32(0);
  734. break;
  735. case wasm::WASM_TYPE_F64:
  736. writeI64(0);
  737. break;
  738. case wasm::WASM_TYPE_EXTERNREF:
  739. writeValueType(wasm::ValType::EXTERNREF);
  740. break;
  741. default:
  742. llvm_unreachable("unexpected type");
  743. }
  744. W->OS << char(wasm::WASM_OPCODE_END);
  745. }
  746. endSection(Section);
  747. }
  748. void WasmObjectWriter::writeTableSection(ArrayRef<wasm::WasmTable> Tables) {
  749. if (Tables.empty())
  750. return;
  751. SectionBookkeeping Section;
  752. startSection(Section, wasm::WASM_SEC_TABLE);
  753. encodeULEB128(Tables.size(), W->OS);
  754. for (const wasm::WasmTable &Table : Tables) {
  755. encodeULEB128(Table.Type.ElemType, W->OS);
  756. encodeULEB128(Table.Type.Limits.Flags, W->OS);
  757. encodeULEB128(Table.Type.Limits.Initial, W->OS);
  758. if (Table.Type.Limits.Flags & wasm::WASM_LIMITS_FLAG_HAS_MAX)
  759. encodeULEB128(Table.Type.Limits.Maximum, W->OS);
  760. }
  761. endSection(Section);
  762. }
  763. void WasmObjectWriter::writeExportSection(ArrayRef<wasm::WasmExport> Exports) {
  764. if (Exports.empty())
  765. return;
  766. SectionBookkeeping Section;
  767. startSection(Section, wasm::WASM_SEC_EXPORT);
  768. encodeULEB128(Exports.size(), W->OS);
  769. for (const wasm::WasmExport &Export : Exports) {
  770. writeString(Export.Name);
  771. W->OS << char(Export.Kind);
  772. encodeULEB128(Export.Index, W->OS);
  773. }
  774. endSection(Section);
  775. }
  776. void WasmObjectWriter::writeElemSection(ArrayRef<uint32_t> TableElems) {
  777. if (TableElems.empty())
  778. return;
  779. SectionBookkeeping Section;
  780. startSection(Section, wasm::WASM_SEC_ELEM);
  781. encodeULEB128(1, W->OS); // number of "segments"
  782. encodeULEB128(0, W->OS); // the table index
  783. // init expr for starting offset
  784. W->OS << char(wasm::WASM_OPCODE_I32_CONST);
  785. encodeSLEB128(InitialTableOffset, W->OS);
  786. W->OS << char(wasm::WASM_OPCODE_END);
  787. encodeULEB128(TableElems.size(), W->OS);
  788. for (uint32_t Elem : TableElems)
  789. encodeULEB128(Elem, W->OS);
  790. endSection(Section);
  791. }
  792. void WasmObjectWriter::writeDataCountSection() {
  793. if (DataSegments.empty())
  794. return;
  795. SectionBookkeeping Section;
  796. startSection(Section, wasm::WASM_SEC_DATACOUNT);
  797. encodeULEB128(DataSegments.size(), W->OS);
  798. endSection(Section);
  799. }
  800. uint32_t WasmObjectWriter::writeCodeSection(const MCAssembler &Asm,
  801. const MCAsmLayout &Layout,
  802. ArrayRef<WasmFunction> Functions) {
  803. if (Functions.empty())
  804. return 0;
  805. SectionBookkeeping Section;
  806. startSection(Section, wasm::WASM_SEC_CODE);
  807. encodeULEB128(Functions.size(), W->OS);
  808. for (const WasmFunction &Func : Functions) {
  809. auto &FuncSection = static_cast<MCSectionWasm &>(Func.Sym->getSection());
  810. int64_t Size = 0;
  811. if (!Func.Sym->getSize()->evaluateAsAbsolute(Size, Layout))
  812. report_fatal_error(".size expression must be evaluatable");
  813. encodeULEB128(Size, W->OS);
  814. FuncSection.setSectionOffset(W->OS.tell() - Section.ContentsOffset);
  815. Asm.writeSectionData(W->OS, &FuncSection, Layout);
  816. }
  817. // Apply fixups.
  818. applyRelocations(CodeRelocations, Section.ContentsOffset, Layout);
  819. endSection(Section);
  820. return Section.Index;
  821. }
  822. uint32_t WasmObjectWriter::writeDataSection(const MCAsmLayout &Layout) {
  823. if (DataSegments.empty())
  824. return 0;
  825. SectionBookkeeping Section;
  826. startSection(Section, wasm::WASM_SEC_DATA);
  827. encodeULEB128(DataSegments.size(), W->OS); // count
  828. for (const WasmDataSegment &Segment : DataSegments) {
  829. encodeULEB128(Segment.InitFlags, W->OS); // flags
  830. if (Segment.InitFlags & wasm::WASM_DATA_SEGMENT_HAS_MEMINDEX)
  831. encodeULEB128(0, W->OS); // memory index
  832. if ((Segment.InitFlags & wasm::WASM_DATA_SEGMENT_IS_PASSIVE) == 0) {
  833. W->OS << char(Segment.Offset > INT32_MAX ? wasm::WASM_OPCODE_I64_CONST
  834. : wasm::WASM_OPCODE_I32_CONST);
  835. encodeSLEB128(Segment.Offset, W->OS); // offset
  836. W->OS << char(wasm::WASM_OPCODE_END);
  837. }
  838. encodeULEB128(Segment.Data.size(), W->OS); // size
  839. Segment.Section->setSectionOffset(W->OS.tell() - Section.ContentsOffset);
  840. W->OS << Segment.Data; // data
  841. }
  842. // Apply fixups.
  843. applyRelocations(DataRelocations, Section.ContentsOffset, Layout);
  844. endSection(Section);
  845. return Section.Index;
  846. }
  847. void WasmObjectWriter::writeRelocSection(
  848. uint32_t SectionIndex, StringRef Name,
  849. std::vector<WasmRelocationEntry> &Relocs) {
  850. // See: https://github.com/WebAssembly/tool-conventions/blob/master/Linking.md
  851. // for descriptions of the reloc sections.
  852. if (Relocs.empty())
  853. return;
  854. // First, ensure the relocations are sorted in offset order. In general they
  855. // should already be sorted since `recordRelocation` is called in offset
  856. // order, but for the code section we combine many MC sections into single
  857. // wasm section, and this order is determined by the order of Asm.Symbols()
  858. // not the sections order.
  859. llvm::stable_sort(
  860. Relocs, [](const WasmRelocationEntry &A, const WasmRelocationEntry &B) {
  861. return (A.Offset + A.FixupSection->getSectionOffset()) <
  862. (B.Offset + B.FixupSection->getSectionOffset());
  863. });
  864. SectionBookkeeping Section;
  865. startCustomSection(Section, std::string("reloc.") + Name.str());
  866. encodeULEB128(SectionIndex, W->OS);
  867. encodeULEB128(Relocs.size(), W->OS);
  868. for (const WasmRelocationEntry &RelEntry : Relocs) {
  869. uint64_t Offset =
  870. RelEntry.Offset + RelEntry.FixupSection->getSectionOffset();
  871. uint32_t Index = getRelocationIndexValue(RelEntry);
  872. W->OS << char(RelEntry.Type);
  873. encodeULEB128(Offset, W->OS);
  874. encodeULEB128(Index, W->OS);
  875. if (RelEntry.hasAddend())
  876. encodeSLEB128(RelEntry.Addend, W->OS);
  877. }
  878. endSection(Section);
  879. }
  880. void WasmObjectWriter::writeCustomRelocSections() {
  881. for (const auto &Sec : CustomSections) {
  882. auto &Relocations = CustomSectionsRelocations[Sec.Section];
  883. writeRelocSection(Sec.OutputIndex, Sec.Name, Relocations);
  884. }
  885. }
  886. void WasmObjectWriter::writeLinkingMetaDataSection(
  887. ArrayRef<wasm::WasmSymbolInfo> SymbolInfos,
  888. ArrayRef<std::pair<uint16_t, uint32_t>> InitFuncs,
  889. const std::map<StringRef, std::vector<WasmComdatEntry>> &Comdats) {
  890. SectionBookkeeping Section;
  891. startCustomSection(Section, "linking");
  892. encodeULEB128(wasm::WasmMetadataVersion, W->OS);
  893. SectionBookkeeping SubSection;
  894. if (SymbolInfos.size() != 0) {
  895. startSection(SubSection, wasm::WASM_SYMBOL_TABLE);
  896. encodeULEB128(SymbolInfos.size(), W->OS);
  897. for (const wasm::WasmSymbolInfo &Sym : SymbolInfos) {
  898. encodeULEB128(Sym.Kind, W->OS);
  899. encodeULEB128(Sym.Flags, W->OS);
  900. switch (Sym.Kind) {
  901. case wasm::WASM_SYMBOL_TYPE_FUNCTION:
  902. case wasm::WASM_SYMBOL_TYPE_GLOBAL:
  903. case wasm::WASM_SYMBOL_TYPE_EVENT:
  904. case wasm::WASM_SYMBOL_TYPE_TABLE:
  905. encodeULEB128(Sym.ElementIndex, W->OS);
  906. if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0 ||
  907. (Sym.Flags & wasm::WASM_SYMBOL_EXPLICIT_NAME) != 0)
  908. writeString(Sym.Name);
  909. break;
  910. case wasm::WASM_SYMBOL_TYPE_DATA:
  911. writeString(Sym.Name);
  912. if ((Sym.Flags & wasm::WASM_SYMBOL_UNDEFINED) == 0) {
  913. encodeULEB128(Sym.DataRef.Segment, W->OS);
  914. encodeULEB128(Sym.DataRef.Offset, W->OS);
  915. encodeULEB128(Sym.DataRef.Size, W->OS);
  916. }
  917. break;
  918. case wasm::WASM_SYMBOL_TYPE_SECTION: {
  919. const uint32_t SectionIndex =
  920. CustomSections[Sym.ElementIndex].OutputIndex;
  921. encodeULEB128(SectionIndex, W->OS);
  922. break;
  923. }
  924. default:
  925. llvm_unreachable("unexpected kind");
  926. }
  927. }
  928. endSection(SubSection);
  929. }
  930. if (DataSegments.size()) {
  931. startSection(SubSection, wasm::WASM_SEGMENT_INFO);
  932. encodeULEB128(DataSegments.size(), W->OS);
  933. for (const WasmDataSegment &Segment : DataSegments) {
  934. writeString(Segment.Name);
  935. encodeULEB128(Segment.Alignment, W->OS);
  936. encodeULEB128(Segment.LinkerFlags, W->OS);
  937. }
  938. endSection(SubSection);
  939. }
  940. if (!InitFuncs.empty()) {
  941. startSection(SubSection, wasm::WASM_INIT_FUNCS);
  942. encodeULEB128(InitFuncs.size(), W->OS);
  943. for (auto &StartFunc : InitFuncs) {
  944. encodeULEB128(StartFunc.first, W->OS); // priority
  945. encodeULEB128(StartFunc.second, W->OS); // function index
  946. }
  947. endSection(SubSection);
  948. }
  949. if (Comdats.size()) {
  950. startSection(SubSection, wasm::WASM_COMDAT_INFO);
  951. encodeULEB128(Comdats.size(), W->OS);
  952. for (const auto &C : Comdats) {
  953. writeString(C.first);
  954. encodeULEB128(0, W->OS); // flags for future use
  955. encodeULEB128(C.second.size(), W->OS);
  956. for (const WasmComdatEntry &Entry : C.second) {
  957. encodeULEB128(Entry.Kind, W->OS);
  958. encodeULEB128(Entry.Index, W->OS);
  959. }
  960. }
  961. endSection(SubSection);
  962. }
  963. endSection(Section);
  964. }
  965. void WasmObjectWriter::writeCustomSection(WasmCustomSection &CustomSection,
  966. const MCAssembler &Asm,
  967. const MCAsmLayout &Layout) {
  968. SectionBookkeeping Section;
  969. auto *Sec = CustomSection.Section;
  970. startCustomSection(Section, CustomSection.Name);
  971. Sec->setSectionOffset(W->OS.tell() - Section.ContentsOffset);
  972. Asm.writeSectionData(W->OS, Sec, Layout);
  973. CustomSection.OutputContentsOffset = Section.ContentsOffset;
  974. CustomSection.OutputIndex = Section.Index;
  975. endSection(Section);
  976. // Apply fixups.
  977. auto &Relocations = CustomSectionsRelocations[CustomSection.Section];
  978. applyRelocations(Relocations, CustomSection.OutputContentsOffset, Layout);
  979. }
  980. uint32_t WasmObjectWriter::getFunctionType(const MCSymbolWasm &Symbol) {
  981. assert(Symbol.isFunction());
  982. assert(TypeIndices.count(&Symbol));
  983. return TypeIndices[&Symbol];
  984. }
  985. uint32_t WasmObjectWriter::getEventType(const MCSymbolWasm &Symbol) {
  986. assert(Symbol.isEvent());
  987. assert(TypeIndices.count(&Symbol));
  988. return TypeIndices[&Symbol];
  989. }
  990. void WasmObjectWriter::registerFunctionType(const MCSymbolWasm &Symbol) {
  991. assert(Symbol.isFunction());
  992. wasm::WasmSignature S;
  993. if (auto *Sig = Symbol.getSignature()) {
  994. S.Returns = Sig->Returns;
  995. S.Params = Sig->Params;
  996. }
  997. auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
  998. if (Pair.second)
  999. Signatures.push_back(S);
  1000. TypeIndices[&Symbol] = Pair.first->second;
  1001. LLVM_DEBUG(dbgs() << "registerFunctionType: " << Symbol
  1002. << " new:" << Pair.second << "\n");
  1003. LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
  1004. }
  1005. void WasmObjectWriter::registerEventType(const MCSymbolWasm &Symbol) {
  1006. assert(Symbol.isEvent());
  1007. // TODO Currently we don't generate imported exceptions, but if we do, we
  1008. // should have a way of infering types of imported exceptions.
  1009. wasm::WasmSignature S;
  1010. if (auto *Sig = Symbol.getSignature()) {
  1011. S.Returns = Sig->Returns;
  1012. S.Params = Sig->Params;
  1013. }
  1014. auto Pair = SignatureIndices.insert(std::make_pair(S, Signatures.size()));
  1015. if (Pair.second)
  1016. Signatures.push_back(S);
  1017. TypeIndices[&Symbol] = Pair.first->second;
  1018. LLVM_DEBUG(dbgs() << "registerEventType: " << Symbol << " new:" << Pair.second
  1019. << "\n");
  1020. LLVM_DEBUG(dbgs() << " -> type index: " << Pair.first->second << "\n");
  1021. }
  1022. static bool isInSymtab(const MCSymbolWasm &Sym) {
  1023. if (Sym.isUsedInReloc() || Sym.isUsedInInitArray())
  1024. return true;
  1025. if (Sym.isComdat() && !Sym.isDefined())
  1026. return false;
  1027. if (Sym.isTemporary())
  1028. return false;
  1029. if (Sym.isSection())
  1030. return false;
  1031. return true;
  1032. }
  1033. void WasmObjectWriter::prepareImports(
  1034. SmallVectorImpl<wasm::WasmImport> &Imports, MCAssembler &Asm,
  1035. const MCAsmLayout &Layout) {
  1036. // For now, always emit the memory import, since loads and stores are not
  1037. // valid without it. In the future, we could perhaps be more clever and omit
  1038. // it if there are no loads or stores.
  1039. wasm::WasmImport MemImport;
  1040. MemImport.Module = "env";
  1041. MemImport.Field = "__linear_memory";
  1042. MemImport.Kind = wasm::WASM_EXTERNAL_MEMORY;
  1043. MemImport.Memory.Flags = is64Bit() ? wasm::WASM_LIMITS_FLAG_IS_64
  1044. : wasm::WASM_LIMITS_FLAG_NONE;
  1045. Imports.push_back(MemImport);
  1046. // Populate SignatureIndices, and Imports and WasmIndices for undefined
  1047. // symbols. This must be done before populating WasmIndices for defined
  1048. // symbols.
  1049. for (const MCSymbol &S : Asm.symbols()) {
  1050. const auto &WS = static_cast<const MCSymbolWasm &>(S);
  1051. // Register types for all functions, including those with private linkage
  1052. // (because wasm always needs a type signature).
  1053. if (WS.isFunction()) {
  1054. const auto *BS = Layout.getBaseSymbol(S);
  1055. if (!BS)
  1056. report_fatal_error(Twine(S.getName()) +
  1057. ": absolute addressing not supported!");
  1058. registerFunctionType(*cast<MCSymbolWasm>(BS));
  1059. }
  1060. if (WS.isEvent())
  1061. registerEventType(WS);
  1062. if (WS.isTemporary())
  1063. continue;
  1064. // If the symbol is not defined in this translation unit, import it.
  1065. if (!WS.isDefined() && !WS.isComdat()) {
  1066. if (WS.isFunction()) {
  1067. wasm::WasmImport Import;
  1068. Import.Module = WS.getImportModule();
  1069. Import.Field = WS.getImportName();
  1070. Import.Kind = wasm::WASM_EXTERNAL_FUNCTION;
  1071. Import.SigIndex = getFunctionType(WS);
  1072. Imports.push_back(Import);
  1073. assert(WasmIndices.count(&WS) == 0);
  1074. WasmIndices[&WS] = NumFunctionImports++;
  1075. } else if (WS.isGlobal()) {
  1076. if (WS.isWeak())
  1077. report_fatal_error("undefined global symbol cannot be weak");
  1078. wasm::WasmImport Import;
  1079. Import.Field = WS.getImportName();
  1080. Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
  1081. Import.Module = WS.getImportModule();
  1082. Import.Global = WS.getGlobalType();
  1083. Imports.push_back(Import);
  1084. assert(WasmIndices.count(&WS) == 0);
  1085. WasmIndices[&WS] = NumGlobalImports++;
  1086. } else if (WS.isEvent()) {
  1087. if (WS.isWeak())
  1088. report_fatal_error("undefined event symbol cannot be weak");
  1089. wasm::WasmImport Import;
  1090. Import.Module = WS.getImportModule();
  1091. Import.Field = WS.getImportName();
  1092. Import.Kind = wasm::WASM_EXTERNAL_EVENT;
  1093. Import.Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION;
  1094. Import.Event.SigIndex = getEventType(WS);
  1095. Imports.push_back(Import);
  1096. assert(WasmIndices.count(&WS) == 0);
  1097. WasmIndices[&WS] = NumEventImports++;
  1098. } else if (WS.isTable()) {
  1099. if (WS.isWeak())
  1100. report_fatal_error("undefined table symbol cannot be weak");
  1101. wasm::WasmImport Import;
  1102. Import.Module = WS.getImportModule();
  1103. Import.Field = WS.getImportName();
  1104. Import.Kind = wasm::WASM_EXTERNAL_TABLE;
  1105. wasm::ValType ElemType = WS.getTableType();
  1106. Import.Table.ElemType = uint8_t(ElemType);
  1107. // FIXME: Extend table type to include limits? For now we don't specify
  1108. // a min or max which does not place any restrictions on the size of the
  1109. // imported table.
  1110. Import.Table.Limits = {wasm::WASM_LIMITS_FLAG_NONE, 0, 0};
  1111. Imports.push_back(Import);
  1112. assert(WasmIndices.count(&WS) == 0);
  1113. WasmIndices[&WS] = NumTableImports++;
  1114. }
  1115. }
  1116. }
  1117. // Add imports for GOT globals
  1118. for (const MCSymbol &S : Asm.symbols()) {
  1119. const auto &WS = static_cast<const MCSymbolWasm &>(S);
  1120. if (WS.isUsedInGOT()) {
  1121. wasm::WasmImport Import;
  1122. if (WS.isFunction())
  1123. Import.Module = "GOT.func";
  1124. else
  1125. Import.Module = "GOT.mem";
  1126. Import.Field = WS.getName();
  1127. Import.Kind = wasm::WASM_EXTERNAL_GLOBAL;
  1128. Import.Global = {wasm::WASM_TYPE_I32, true};
  1129. Imports.push_back(Import);
  1130. assert(GOTIndices.count(&WS) == 0);
  1131. GOTIndices[&WS] = NumGlobalImports++;
  1132. }
  1133. }
  1134. }
  1135. uint64_t WasmObjectWriter::writeObject(MCAssembler &Asm,
  1136. const MCAsmLayout &Layout) {
  1137. support::endian::Writer MainWriter(*OS, support::little);
  1138. W = &MainWriter;
  1139. if (IsSplitDwarf) {
  1140. uint64_t TotalSize = writeOneObject(Asm, Layout, DwoMode::NonDwoOnly);
  1141. assert(DwoOS);
  1142. support::endian::Writer DwoWriter(*DwoOS, support::little);
  1143. W = &DwoWriter;
  1144. return TotalSize + writeOneObject(Asm, Layout, DwoMode::DwoOnly);
  1145. } else {
  1146. return writeOneObject(Asm, Layout, DwoMode::AllSections);
  1147. }
  1148. }
  1149. uint64_t WasmObjectWriter::writeOneObject(MCAssembler &Asm,
  1150. const MCAsmLayout &Layout,
  1151. DwoMode Mode) {
  1152. uint64_t StartOffset = W->OS.tell();
  1153. SectionCount = 0;
  1154. CustomSections.clear();
  1155. LLVM_DEBUG(dbgs() << "WasmObjectWriter::writeObject\n");
  1156. // Collect information from the available symbols.
  1157. SmallVector<WasmFunction, 4> Functions;
  1158. SmallVector<uint32_t, 4> TableElems;
  1159. SmallVector<wasm::WasmImport, 4> Imports;
  1160. SmallVector<wasm::WasmExport, 4> Exports;
  1161. SmallVector<wasm::WasmEventType, 1> Events;
  1162. SmallVector<wasm::WasmGlobal, 1> Globals;
  1163. SmallVector<wasm::WasmTable, 1> Tables;
  1164. SmallVector<wasm::WasmSymbolInfo, 4> SymbolInfos;
  1165. SmallVector<std::pair<uint16_t, uint32_t>, 2> InitFuncs;
  1166. std::map<StringRef, std::vector<WasmComdatEntry>> Comdats;
  1167. uint64_t DataSize = 0;
  1168. if (Mode != DwoMode::DwoOnly) {
  1169. prepareImports(Imports, Asm, Layout);
  1170. }
  1171. // Populate DataSegments and CustomSections, which must be done before
  1172. // populating DataLocations.
  1173. for (MCSection &Sec : Asm) {
  1174. auto &Section = static_cast<MCSectionWasm &>(Sec);
  1175. StringRef SectionName = Section.getName();
  1176. if (Mode == DwoMode::NonDwoOnly && isDwoSection(Sec))
  1177. continue;
  1178. if (Mode == DwoMode::DwoOnly && !isDwoSection(Sec))
  1179. continue;
  1180. LLVM_DEBUG(dbgs() << "Processing Section " << SectionName << " group "
  1181. << Section.getGroup() << "\n";);
  1182. // .init_array sections are handled specially elsewhere.
  1183. if (SectionName.startswith(".init_array"))
  1184. continue;
  1185. // Code is handled separately
  1186. if (Section.getKind().isText())
  1187. continue;
  1188. if (Section.isWasmData()) {
  1189. uint32_t SegmentIndex = DataSegments.size();
  1190. DataSize = alignTo(DataSize, Section.getAlignment());
  1191. DataSegments.emplace_back();
  1192. WasmDataSegment &Segment = DataSegments.back();
  1193. Segment.Name = SectionName;
  1194. Segment.InitFlags = Section.getPassive()
  1195. ? (uint32_t)wasm::WASM_DATA_SEGMENT_IS_PASSIVE
  1196. : 0;
  1197. Segment.Offset = DataSize;
  1198. Segment.Section = &Section;
  1199. addData(Segment.Data, Section);
  1200. Segment.Alignment = Log2_32(Section.getAlignment());
  1201. Segment.LinkerFlags = 0;
  1202. DataSize += Segment.Data.size();
  1203. Section.setSegmentIndex(SegmentIndex);
  1204. if (const MCSymbolWasm *C = Section.getGroup()) {
  1205. Comdats[C->getName()].emplace_back(
  1206. WasmComdatEntry{wasm::WASM_COMDAT_DATA, SegmentIndex});
  1207. }
  1208. } else {
  1209. // Create custom sections
  1210. assert(Sec.getKind().isMetadata());
  1211. StringRef Name = SectionName;
  1212. // For user-defined custom sections, strip the prefix
  1213. if (Name.startswith(".custom_section."))
  1214. Name = Name.substr(strlen(".custom_section."));
  1215. MCSymbol *Begin = Sec.getBeginSymbol();
  1216. if (Begin) {
  1217. assert(WasmIndices.count(cast<MCSymbolWasm>(Begin)) == 0);
  1218. WasmIndices[cast<MCSymbolWasm>(Begin)] = CustomSections.size();
  1219. }
  1220. // Separate out the producers and target features sections
  1221. if (Name == "producers") {
  1222. ProducersSection = std::make_unique<WasmCustomSection>(Name, &Section);
  1223. continue;
  1224. }
  1225. if (Name == "target_features") {
  1226. TargetFeaturesSection =
  1227. std::make_unique<WasmCustomSection>(Name, &Section);
  1228. continue;
  1229. }
  1230. // Custom sections can also belong to COMDAT groups. In this case the
  1231. // decriptor's "index" field is the section index (in the final object
  1232. // file), but that is not known until after layout, so it must be fixed up
  1233. // later
  1234. if (const MCSymbolWasm *C = Section.getGroup()) {
  1235. Comdats[C->getName()].emplace_back(
  1236. WasmComdatEntry{wasm::WASM_COMDAT_SECTION,
  1237. static_cast<uint32_t>(CustomSections.size())});
  1238. }
  1239. CustomSections.emplace_back(Name, &Section);
  1240. }
  1241. }
  1242. if (Mode != DwoMode::DwoOnly) {
  1243. // Populate WasmIndices and DataLocations for defined symbols.
  1244. for (const MCSymbol &S : Asm.symbols()) {
  1245. // Ignore unnamed temporary symbols, which aren't ever exported, imported,
  1246. // or used in relocations.
  1247. if (S.isTemporary() && S.getName().empty())
  1248. continue;
  1249. const auto &WS = static_cast<const MCSymbolWasm &>(S);
  1250. LLVM_DEBUG(
  1251. dbgs() << "MCSymbol: " << toString(WS.getType()) << " '" << S << "'"
  1252. << " isDefined=" << S.isDefined() << " isExternal="
  1253. << S.isExternal() << " isTemporary=" << S.isTemporary()
  1254. << " isWeak=" << WS.isWeak() << " isHidden=" << WS.isHidden()
  1255. << " isVariable=" << WS.isVariable() << "\n");
  1256. if (WS.isVariable())
  1257. continue;
  1258. if (WS.isComdat() && !WS.isDefined())
  1259. continue;
  1260. if (WS.isFunction()) {
  1261. unsigned Index;
  1262. if (WS.isDefined()) {
  1263. if (WS.getOffset() != 0)
  1264. report_fatal_error(
  1265. "function sections must contain one function each");
  1266. if (WS.getSize() == nullptr)
  1267. report_fatal_error(
  1268. "function symbols must have a size set with .size");
  1269. // A definition. Write out the function body.
  1270. Index = NumFunctionImports + Functions.size();
  1271. WasmFunction Func;
  1272. Func.SigIndex = getFunctionType(WS);
  1273. Func.Sym = &WS;
  1274. assert(WasmIndices.count(&WS) == 0);
  1275. WasmIndices[&WS] = Index;
  1276. Functions.push_back(Func);
  1277. auto &Section = static_cast<MCSectionWasm &>(WS.getSection());
  1278. if (const MCSymbolWasm *C = Section.getGroup()) {
  1279. Comdats[C->getName()].emplace_back(
  1280. WasmComdatEntry{wasm::WASM_COMDAT_FUNCTION, Index});
  1281. }
  1282. if (WS.hasExportName()) {
  1283. wasm::WasmExport Export;
  1284. Export.Name = WS.getExportName();
  1285. Export.Kind = wasm::WASM_EXTERNAL_FUNCTION;
  1286. Export.Index = Index;
  1287. Exports.push_back(Export);
  1288. }
  1289. } else {
  1290. // An import; the index was assigned above.
  1291. Index = WasmIndices.find(&WS)->second;
  1292. }
  1293. LLVM_DEBUG(dbgs() << " -> function index: " << Index << "\n");
  1294. } else if (WS.isData()) {
  1295. if (!isInSymtab(WS))
  1296. continue;
  1297. if (!WS.isDefined()) {
  1298. LLVM_DEBUG(dbgs() << " -> segment index: -1"
  1299. << "\n");
  1300. continue;
  1301. }
  1302. if (!WS.getSize())
  1303. report_fatal_error("data symbols must have a size set with .size: " +
  1304. WS.getName());
  1305. int64_t Size = 0;
  1306. if (!WS.getSize()->evaluateAsAbsolute(Size, Layout))
  1307. report_fatal_error(".size expression must be evaluatable");
  1308. auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
  1309. if (!DataSection.isWasmData())
  1310. report_fatal_error("data symbols must live in a data section: " +
  1311. WS.getName());
  1312. // For each data symbol, export it in the symtab as a reference to the
  1313. // corresponding Wasm data segment.
  1314. wasm::WasmDataReference Ref = wasm::WasmDataReference{
  1315. DataSection.getSegmentIndex(), Layout.getSymbolOffset(WS),
  1316. static_cast<uint64_t>(Size)};
  1317. assert(DataLocations.count(&WS) == 0);
  1318. DataLocations[&WS] = Ref;
  1319. LLVM_DEBUG(dbgs() << " -> segment index: " << Ref.Segment << "\n");
  1320. } else if (WS.isGlobal()) {
  1321. // A "true" Wasm global (currently just __stack_pointer)
  1322. if (WS.isDefined()) {
  1323. wasm::WasmGlobal Global;
  1324. Global.Type = WS.getGlobalType();
  1325. Global.Index = NumGlobalImports + Globals.size();
  1326. switch (Global.Type.Type) {
  1327. case wasm::WASM_TYPE_I32:
  1328. Global.InitExpr.Opcode = wasm::WASM_OPCODE_I32_CONST;
  1329. break;
  1330. case wasm::WASM_TYPE_I64:
  1331. Global.InitExpr.Opcode = wasm::WASM_OPCODE_I64_CONST;
  1332. break;
  1333. case wasm::WASM_TYPE_F32:
  1334. Global.InitExpr.Opcode = wasm::WASM_OPCODE_F32_CONST;
  1335. break;
  1336. case wasm::WASM_TYPE_F64:
  1337. Global.InitExpr.Opcode = wasm::WASM_OPCODE_F64_CONST;
  1338. break;
  1339. case wasm::WASM_TYPE_EXTERNREF:
  1340. Global.InitExpr.Opcode = wasm::WASM_OPCODE_REF_NULL;
  1341. break;
  1342. default:
  1343. llvm_unreachable("unexpected type");
  1344. }
  1345. assert(WasmIndices.count(&WS) == 0);
  1346. WasmIndices[&WS] = Global.Index;
  1347. Globals.push_back(Global);
  1348. } else {
  1349. // An import; the index was assigned above
  1350. LLVM_DEBUG(dbgs() << " -> global index: "
  1351. << WasmIndices.find(&WS)->second << "\n");
  1352. }
  1353. } else if (WS.isTable()) {
  1354. if (WS.isDefined()) {
  1355. wasm::WasmTable Table;
  1356. Table.Index = NumTableImports + Tables.size();
  1357. Table.Type.ElemType = static_cast<uint8_t>(WS.getTableType());
  1358. // FIXME: Work on custom limits is ongoing
  1359. Table.Type.Limits = {wasm::WASM_LIMITS_FLAG_NONE, 0, 0};
  1360. assert(WasmIndices.count(&WS) == 0);
  1361. WasmIndices[&WS] = Table.Index;
  1362. Tables.push_back(Table);
  1363. }
  1364. LLVM_DEBUG(dbgs() << " -> table index: "
  1365. << WasmIndices.find(&WS)->second << "\n");
  1366. } else if (WS.isEvent()) {
  1367. // C++ exception symbol (__cpp_exception)
  1368. unsigned Index;
  1369. if (WS.isDefined()) {
  1370. Index = NumEventImports + Events.size();
  1371. wasm::WasmEventType Event;
  1372. Event.SigIndex = getEventType(WS);
  1373. Event.Attribute = wasm::WASM_EVENT_ATTRIBUTE_EXCEPTION;
  1374. assert(WasmIndices.count(&WS) == 0);
  1375. WasmIndices[&WS] = Index;
  1376. Events.push_back(Event);
  1377. } else {
  1378. // An import; the index was assigned above.
  1379. assert(WasmIndices.count(&WS) > 0);
  1380. }
  1381. LLVM_DEBUG(dbgs() << " -> event index: "
  1382. << WasmIndices.find(&WS)->second << "\n");
  1383. } else {
  1384. assert(WS.isSection());
  1385. }
  1386. }
  1387. // Populate WasmIndices and DataLocations for aliased symbols. We need to
  1388. // process these in a separate pass because we need to have processed the
  1389. // target of the alias before the alias itself and the symbols are not
  1390. // necessarily ordered in this way.
  1391. for (const MCSymbol &S : Asm.symbols()) {
  1392. if (!S.isVariable())
  1393. continue;
  1394. assert(S.isDefined());
  1395. const auto *BS = Layout.getBaseSymbol(S);
  1396. if (!BS)
  1397. report_fatal_error(Twine(S.getName()) +
  1398. ": absolute addressing not supported!");
  1399. const MCSymbolWasm *Base = cast<MCSymbolWasm>(BS);
  1400. // Find the target symbol of this weak alias and export that index
  1401. const auto &WS = static_cast<const MCSymbolWasm &>(S);
  1402. LLVM_DEBUG(dbgs() << WS.getName() << ": weak alias of '" << *Base
  1403. << "'\n");
  1404. if (Base->isFunction()) {
  1405. assert(WasmIndices.count(Base) > 0);
  1406. uint32_t WasmIndex = WasmIndices.find(Base)->second;
  1407. assert(WasmIndices.count(&WS) == 0);
  1408. WasmIndices[&WS] = WasmIndex;
  1409. LLVM_DEBUG(dbgs() << " -> index:" << WasmIndex << "\n");
  1410. } else if (Base->isData()) {
  1411. auto &DataSection = static_cast<MCSectionWasm &>(WS.getSection());
  1412. uint64_t Offset = Layout.getSymbolOffset(S);
  1413. int64_t Size = 0;
  1414. // For data symbol alias we use the size of the base symbol as the
  1415. // size of the alias. When an offset from the base is involved this
  1416. // can result in a offset + size goes past the end of the data section
  1417. // which out object format doesn't support. So we must clamp it.
  1418. if (!Base->getSize()->evaluateAsAbsolute(Size, Layout))
  1419. report_fatal_error(".size expression must be evaluatable");
  1420. const WasmDataSegment &Segment =
  1421. DataSegments[DataSection.getSegmentIndex()];
  1422. Size =
  1423. std::min(static_cast<uint64_t>(Size), Segment.Data.size() - Offset);
  1424. wasm::WasmDataReference Ref = wasm::WasmDataReference{
  1425. DataSection.getSegmentIndex(),
  1426. static_cast<uint32_t>(Layout.getSymbolOffset(S)),
  1427. static_cast<uint32_t>(Size)};
  1428. DataLocations[&WS] = Ref;
  1429. LLVM_DEBUG(dbgs() << " -> index:" << Ref.Segment << "\n");
  1430. } else {
  1431. report_fatal_error("don't yet support global/event aliases");
  1432. }
  1433. }
  1434. }
  1435. // Finally, populate the symbol table itself, in its "natural" order.
  1436. for (const MCSymbol &S : Asm.symbols()) {
  1437. const auto &WS = static_cast<const MCSymbolWasm &>(S);
  1438. if (!isInSymtab(WS)) {
  1439. WS.setIndex(InvalidIndex);
  1440. continue;
  1441. }
  1442. if (WS.isTable() && WS.getName() == "__indirect_function_table") {
  1443. // For the moment, don't emit table symbols -- wasm-ld can't handle them.
  1444. continue;
  1445. }
  1446. LLVM_DEBUG(dbgs() << "adding to symtab: " << WS << "\n");
  1447. uint32_t Flags = 0;
  1448. if (WS.isWeak())
  1449. Flags |= wasm::WASM_SYMBOL_BINDING_WEAK;
  1450. if (WS.isHidden())
  1451. Flags |= wasm::WASM_SYMBOL_VISIBILITY_HIDDEN;
  1452. if (!WS.isExternal() && WS.isDefined())
  1453. Flags |= wasm::WASM_SYMBOL_BINDING_LOCAL;
  1454. if (WS.isUndefined())
  1455. Flags |= wasm::WASM_SYMBOL_UNDEFINED;
  1456. if (WS.isNoStrip()) {
  1457. Flags |= wasm::WASM_SYMBOL_NO_STRIP;
  1458. if (isEmscripten()) {
  1459. Flags |= wasm::WASM_SYMBOL_EXPORTED;
  1460. }
  1461. }
  1462. if (WS.hasImportName())
  1463. Flags |= wasm::WASM_SYMBOL_EXPLICIT_NAME;
  1464. if (WS.hasExportName())
  1465. Flags |= wasm::WASM_SYMBOL_EXPORTED;
  1466. wasm::WasmSymbolInfo Info;
  1467. Info.Name = WS.getName();
  1468. Info.Kind = WS.getType();
  1469. Info.Flags = Flags;
  1470. if (!WS.isData()) {
  1471. assert(WasmIndices.count(&WS) > 0);
  1472. Info.ElementIndex = WasmIndices.find(&WS)->second;
  1473. } else if (WS.isDefined()) {
  1474. assert(DataLocations.count(&WS) > 0);
  1475. Info.DataRef = DataLocations.find(&WS)->second;
  1476. }
  1477. WS.setIndex(SymbolInfos.size());
  1478. SymbolInfos.emplace_back(Info);
  1479. }
  1480. {
  1481. auto HandleReloc = [&](const WasmRelocationEntry &Rel) {
  1482. // Functions referenced by a relocation need to put in the table. This is
  1483. // purely to make the object file's provisional values readable, and is
  1484. // ignored by the linker, which re-calculates the relocations itself.
  1485. if (Rel.Type != wasm::R_WASM_TABLE_INDEX_I32 &&
  1486. Rel.Type != wasm::R_WASM_TABLE_INDEX_I64 &&
  1487. Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB &&
  1488. Rel.Type != wasm::R_WASM_TABLE_INDEX_SLEB64 &&
  1489. Rel.Type != wasm::R_WASM_TABLE_INDEX_REL_SLEB)
  1490. return;
  1491. assert(Rel.Symbol->isFunction());
  1492. const MCSymbolWasm *Base =
  1493. cast<MCSymbolWasm>(Layout.getBaseSymbol(*Rel.Symbol));
  1494. uint32_t FunctionIndex = WasmIndices.find(Base)->second;
  1495. uint32_t TableIndex = TableElems.size() + InitialTableOffset;
  1496. if (TableIndices.try_emplace(Base, TableIndex).second) {
  1497. LLVM_DEBUG(dbgs() << " -> adding " << Base->getName()
  1498. << " to table: " << TableIndex << "\n");
  1499. TableElems.push_back(FunctionIndex);
  1500. registerFunctionType(*Base);
  1501. }
  1502. };
  1503. for (const WasmRelocationEntry &RelEntry : CodeRelocations)
  1504. HandleReloc(RelEntry);
  1505. for (const WasmRelocationEntry &RelEntry : DataRelocations)
  1506. HandleReloc(RelEntry);
  1507. }
  1508. // Translate .init_array section contents into start functions.
  1509. for (const MCSection &S : Asm) {
  1510. const auto &WS = static_cast<const MCSectionWasm &>(S);
  1511. if (WS.getName().startswith(".fini_array"))
  1512. report_fatal_error(".fini_array sections are unsupported");
  1513. if (!WS.getName().startswith(".init_array"))
  1514. continue;
  1515. if (WS.getFragmentList().empty())
  1516. continue;
  1517. // init_array is expected to contain a single non-empty data fragment
  1518. if (WS.getFragmentList().size() != 3)
  1519. report_fatal_error("only one .init_array section fragment supported");
  1520. auto IT = WS.begin();
  1521. const MCFragment &EmptyFrag = *IT;
  1522. if (EmptyFrag.getKind() != MCFragment::FT_Data)
  1523. report_fatal_error(".init_array section should be aligned");
  1524. IT = std::next(IT);
  1525. const MCFragment &AlignFrag = *IT;
  1526. if (AlignFrag.getKind() != MCFragment::FT_Align)
  1527. report_fatal_error(".init_array section should be aligned");
  1528. if (cast<MCAlignFragment>(AlignFrag).getAlignment() != (is64Bit() ? 8 : 4))
  1529. report_fatal_error(".init_array section should be aligned for pointers");
  1530. const MCFragment &Frag = *std::next(IT);
  1531. if (Frag.hasInstructions() || Frag.getKind() != MCFragment::FT_Data)
  1532. report_fatal_error("only data supported in .init_array section");
  1533. uint16_t Priority = UINT16_MAX;
  1534. unsigned PrefixLength = strlen(".init_array");
  1535. if (WS.getName().size() > PrefixLength) {
  1536. if (WS.getName()[PrefixLength] != '.')
  1537. report_fatal_error(
  1538. ".init_array section priority should start with '.'");
  1539. if (WS.getName().substr(PrefixLength + 1).getAsInteger(10, Priority))
  1540. report_fatal_error("invalid .init_array section priority");
  1541. }
  1542. const auto &DataFrag = cast<MCDataFragment>(Frag);
  1543. const SmallVectorImpl<char> &Contents = DataFrag.getContents();
  1544. for (const uint8_t *
  1545. P = (const uint8_t *)Contents.data(),
  1546. *End = (const uint8_t *)Contents.data() + Contents.size();
  1547. P != End; ++P) {
  1548. if (*P != 0)
  1549. report_fatal_error("non-symbolic data in .init_array section");
  1550. }
  1551. for (const MCFixup &Fixup : DataFrag.getFixups()) {
  1552. assert(Fixup.getKind() ==
  1553. MCFixup::getKindForSize(is64Bit() ? 8 : 4, false));
  1554. const MCExpr *Expr = Fixup.getValue();
  1555. auto *SymRef = dyn_cast<MCSymbolRefExpr>(Expr);
  1556. if (!SymRef)
  1557. report_fatal_error("fixups in .init_array should be symbol references");
  1558. const auto &TargetSym = cast<const MCSymbolWasm>(SymRef->getSymbol());
  1559. if (TargetSym.getIndex() == InvalidIndex)
  1560. report_fatal_error("symbols in .init_array should exist in symtab");
  1561. if (!TargetSym.isFunction())
  1562. report_fatal_error("symbols in .init_array should be for functions");
  1563. InitFuncs.push_back(
  1564. std::make_pair(Priority, TargetSym.getIndex()));
  1565. }
  1566. }
  1567. // Write out the Wasm header.
  1568. writeHeader(Asm);
  1569. uint32_t CodeSectionIndex, DataSectionIndex;
  1570. if (Mode != DwoMode::DwoOnly) {
  1571. writeTypeSection(Signatures);
  1572. writeImportSection(Imports, DataSize, TableElems.size());
  1573. writeFunctionSection(Functions);
  1574. writeTableSection(Tables);
  1575. // Skip the "memory" section; we import the memory instead.
  1576. writeEventSection(Events);
  1577. writeGlobalSection(Globals);
  1578. writeExportSection(Exports);
  1579. writeElemSection(TableElems);
  1580. writeDataCountSection();
  1581. CodeSectionIndex = writeCodeSection(Asm, Layout, Functions);
  1582. DataSectionIndex = writeDataSection(Layout);
  1583. }
  1584. // The Sections in the COMDAT list have placeholder indices (their index among
  1585. // custom sections, rather than among all sections). Fix them up here.
  1586. for (auto &Group : Comdats) {
  1587. for (auto &Entry : Group.second) {
  1588. if (Entry.Kind == wasm::WASM_COMDAT_SECTION) {
  1589. Entry.Index += SectionCount;
  1590. }
  1591. }
  1592. }
  1593. for (auto &CustomSection : CustomSections)
  1594. writeCustomSection(CustomSection, Asm, Layout);
  1595. if (Mode != DwoMode::DwoOnly) {
  1596. writeLinkingMetaDataSection(SymbolInfos, InitFuncs, Comdats);
  1597. writeRelocSection(CodeSectionIndex, "CODE", CodeRelocations);
  1598. writeRelocSection(DataSectionIndex, "DATA", DataRelocations);
  1599. }
  1600. writeCustomRelocSections();
  1601. if (ProducersSection)
  1602. writeCustomSection(*ProducersSection, Asm, Layout);
  1603. if (TargetFeaturesSection)
  1604. writeCustomSection(*TargetFeaturesSection, Asm, Layout);
  1605. // TODO: Translate the .comment section to the output.
  1606. return W->OS.tell() - StartOffset;
  1607. }
  1608. std::unique_ptr<MCObjectWriter>
  1609. llvm::createWasmObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
  1610. raw_pwrite_stream &OS) {
  1611. return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS);
  1612. }
  1613. std::unique_ptr<MCObjectWriter>
  1614. llvm::createWasmDwoObjectWriter(std::unique_ptr<MCWasmObjectTargetWriter> MOTW,
  1615. raw_pwrite_stream &OS,
  1616. raw_pwrite_stream &DwoOS) {
  1617. return std::make_unique<WasmObjectWriter>(std::move(MOTW), OS, DwoOS);
  1618. }