125d7b4fbSAlexey Lapshin //===- WasmReader.cpp -----------------------------------------------------===//
225d7b4fbSAlexey Lapshin //
325d7b4fbSAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
425d7b4fbSAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
525d7b4fbSAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
625d7b4fbSAlexey Lapshin //
725d7b4fbSAlexey Lapshin //===----------------------------------------------------------------------===//
825d7b4fbSAlexey Lapshin 
925d7b4fbSAlexey Lapshin #include "WasmReader.h"
1025d7b4fbSAlexey Lapshin 
1125d7b4fbSAlexey Lapshin namespace llvm {
1225d7b4fbSAlexey Lapshin namespace objcopy {
1325d7b4fbSAlexey Lapshin namespace wasm {
1425d7b4fbSAlexey Lapshin 
1525d7b4fbSAlexey Lapshin using namespace object;
1625d7b4fbSAlexey Lapshin using namespace llvm::wasm;
1725d7b4fbSAlexey Lapshin 
create() const1825d7b4fbSAlexey Lapshin Expected<std::unique_ptr<Object>> Reader::create() const {
1925d7b4fbSAlexey Lapshin   auto Obj = std::make_unique<Object>();
2025d7b4fbSAlexey Lapshin   Obj->Header = WasmObj.getHeader();
2125d7b4fbSAlexey Lapshin   std::vector<Section> Sections;
2225d7b4fbSAlexey Lapshin   Obj->Sections.reserve(WasmObj.getNumSections());
2325d7b4fbSAlexey Lapshin   for (const SectionRef &Sec : WasmObj.sections()) {
2425d7b4fbSAlexey Lapshin     const WasmSection &WS = WasmObj.getWasmSection(Sec);
2525d7b4fbSAlexey Lapshin     Obj->Sections.push_back(
2625d7b4fbSAlexey Lapshin         {static_cast<uint8_t>(WS.Type), WS.Name, WS.Content});
27*2ae385e5SDerek Schuff     // Give known sections standard names to allow them to be selected. (Custom
28*2ae385e5SDerek Schuff     // sections already have their names filled in by the parser).
29c9dd1cc6SDerek Schuff     Section &ReaderSec = Obj->Sections.back();
30c9dd1cc6SDerek Schuff     if (ReaderSec.SectionType > WASM_SEC_CUSTOM &&
31*2ae385e5SDerek Schuff         ReaderSec.SectionType <= WASM_SEC_LAST_KNOWN)
32c9dd1cc6SDerek Schuff       ReaderSec.Name = sectionTypeToString(ReaderSec.SectionType);
3325d7b4fbSAlexey Lapshin   }
3425d7b4fbSAlexey Lapshin   return std::move(Obj);
3525d7b4fbSAlexey Lapshin }
3625d7b4fbSAlexey Lapshin 
3725d7b4fbSAlexey Lapshin } // end namespace wasm
3825d7b4fbSAlexey Lapshin } // end namespace objcopy
3925d7b4fbSAlexey Lapshin } // end namespace llvm
40