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