diff options
Diffstat (limited to 'wasm/Writer.cpp')
| -rw-r--r-- | wasm/Writer.cpp | 43 | 
1 files changed, 32 insertions, 11 deletions
diff --git a/wasm/Writer.cpp b/wasm/Writer.cpp index 61ac54a3e4b3..e7dd49d52213 100644 --- a/wasm/Writer.cpp +++ b/wasm/Writer.cpp @@ -174,7 +174,7 @@ void Writer::createImportSection() {      Import.Field = Sym->getName();      Import.Kind = WASM_EXTERNAL_GLOBAL;      Import.Global.Mutable = false; -    Import.Global.Type = WASM_TYPE_I32; // Sym->getGlobalType(); +    Import.Global.Type = WASM_TYPE_I32;      writeImport(OS, Import);    }  } @@ -183,9 +183,8 @@ void Writer::createTypeSection() {    SyntheticSection *Section = createSyntheticSection(WASM_SEC_TYPE);    raw_ostream &OS = Section->getStream();    writeUleb128(OS, Types.size(), "type count"); -  for (const WasmSignature *Sig : Types) { +  for (const WasmSignature *Sig : Types)      writeSig(OS, *Sig); -  }  }  void Writer::createFunctionSection() { @@ -196,11 +195,9 @@ void Writer::createFunctionSection() {    raw_ostream &OS = Section->getStream();    writeUleb128(OS, NumFunctions, "function count"); -  for (ObjFile *File : Symtab->ObjectFiles) { -    for (uint32_t Sig : File->getWasmObj()->functionTypes()) { +  for (ObjFile *File : Symtab->ObjectFiles) +    for (uint32_t Sig : File->getWasmObj()->functionTypes())        writeUleb128(OS, File->relocateTypeIndex(Sig), "sig index"); -    } -  }  }  void Writer::createMemorySection() { @@ -358,7 +355,7 @@ void Writer::createDataSection() {    OutputSections.push_back(Section);  } -// Create reloctions sections in the final output. +// Create relocations sections in the final output.  // These are only created when relocatable output is requested.  void Writer::createRelocSections() {    log("createRelocSections"); @@ -376,7 +373,7 @@ void Writer::createRelocSections() {      else if (S->Type == WASM_SEC_CODE)        name = "reloc.CODE";      else -      llvm_unreachable("relocations only support for code and data"); +      llvm_unreachable("relocations only supported for code and data");      SyntheticSection *Section = createSyntheticSection(WASM_SEC_CUSTOM, name);      raw_ostream &OS = Section->getStream(); @@ -398,7 +395,10 @@ void Writer::createLinkingSection() {    DataSizeSubSection.finalizeContents();    DataSizeSubSection.writeToStream(OS); -  if (Segments.size() && Config->Relocatable) { +  if (!Config->Relocatable) +    return; + +  if (Segments.size()) {      SubSection SubSection(WASM_SEGMENT_INFO);      writeUleb128(SubSection.getStream(), Segments.size(), "num data segments");      for (const OutputSegment *S : Segments) { @@ -409,6 +409,27 @@ void Writer::createLinkingSection() {      SubSection.finalizeContents();      SubSection.writeToStream(OS);    } + +  std::vector<WasmInitFunc> InitFunctions; +  for (ObjFile *File : Symtab->ObjectFiles) { +    const WasmLinkingData &L = File->getWasmObj()->linkingData(); +    InitFunctions.reserve(InitFunctions.size() + L.InitFunctions.size()); +    for (const WasmInitFunc &F : L.InitFunctions) +      InitFunctions.emplace_back(WasmInitFunc{ +          F.Priority, File->relocateFunctionIndex(F.FunctionIndex)}); +  } + +  if (!InitFunctions.empty()) { +    SubSection SubSection(WASM_INIT_FUNCS); +    writeUleb128(SubSection.getStream(), InitFunctions.size(), +                 "num init functionsw"); +    for (const WasmInitFunc &F : InitFunctions) { +      writeUleb128(SubSection.getStream(), F.Priority, "priority"); +      writeUleb128(SubSection.getStream(), F.FunctionIndex, "function index"); +    } +    SubSection.finalizeContents(); +    SubSection.writeToStream(OS); +  }  }  // Create the custom "name" section containing debug symbol names. @@ -501,7 +522,7 @@ void Writer::layoutMemory() {  SyntheticSection *Writer::createSyntheticSection(uint32_t Type,                                                   std::string Name) {    auto Sec = make<SyntheticSection>(Type, Name); -  log("createSection: " + toString(Sec)); +  log("createSection: " + toString(*Sec));    OutputSections.push_back(Sec);    return Sec;  }  | 
