WasmObjectWriter.cpp 70 KB

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