xref: /llvm-project/llvm/lib/ObjCopy/wasm/WasmReader.cpp (revision 2a6136e552d24b6bf665c42a6e32efc0b2d88fbf)
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 
1825d7b4fbSAlexey Lapshin Expected<std::unique_ptr<Object>> Reader::create() const {
1925d7b4fbSAlexey Lapshin   auto Obj = std::make_unique<Object>();
2025d7b4fbSAlexey Lapshin   Obj->Header = WasmObj.getHeader();
21*2a6136e5SSam Clegg   Obj->isRelocatableObject = WasmObj.isRelocatableObject();
2225d7b4fbSAlexey Lapshin   std::vector<Section> Sections;
2325d7b4fbSAlexey Lapshin   Obj->Sections.reserve(WasmObj.getNumSections());
2425d7b4fbSAlexey Lapshin   for (const SectionRef &Sec : WasmObj.sections()) {
2525d7b4fbSAlexey Lapshin     const WasmSection &WS = WasmObj.getWasmSection(Sec);
261b21067cSDerek Schuff     Obj->Sections.push_back({static_cast<uint8_t>(WS.Type),
271b21067cSDerek Schuff                              WS.HeaderSecSizeEncodingLen, WS.Name, WS.Content});
282ae385e5SDerek Schuff     // Give known sections standard names to allow them to be selected. (Custom
292ae385e5SDerek Schuff     // sections already have their names filled in by the parser).
30c9dd1cc6SDerek Schuff     Section &ReaderSec = Obj->Sections.back();
31c9dd1cc6SDerek Schuff     if (ReaderSec.SectionType > WASM_SEC_CUSTOM &&
322ae385e5SDerek Schuff         ReaderSec.SectionType <= WASM_SEC_LAST_KNOWN)
33c9dd1cc6SDerek Schuff       ReaderSec.Name = sectionTypeToString(ReaderSec.SectionType);
3425d7b4fbSAlexey Lapshin   }
3525d7b4fbSAlexey Lapshin   return std::move(Obj);
3625d7b4fbSAlexey Lapshin }
3725d7b4fbSAlexey Lapshin 
3825d7b4fbSAlexey Lapshin } // end namespace wasm
3925d7b4fbSAlexey Lapshin } // end namespace objcopy
4025d7b4fbSAlexey Lapshin } // end namespace llvm
41