WasmReader.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. //===- WasmReader.cpp -----------------------------------------------------===//
  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. #include "WasmReader.h"
  9. namespace llvm {
  10. namespace objcopy {
  11. namespace wasm {
  12. using namespace object;
  13. using namespace llvm::wasm;
  14. Expected<std::unique_ptr<Object>> Reader::create() const {
  15. auto Obj = std::make_unique<Object>();
  16. Obj->Header = WasmObj.getHeader();
  17. std::vector<Section> Sections;
  18. Obj->Sections.reserve(WasmObj.getNumSections());
  19. for (const SectionRef &Sec : WasmObj.sections()) {
  20. const WasmSection &WS = WasmObj.getWasmSection(Sec);
  21. Obj->Sections.push_back(
  22. {static_cast<uint8_t>(WS.Type), WS.Name, WS.Content});
  23. // Give known sections standard names to allow them to be selected. (Custom
  24. // sections already have their names filled in by the parser).
  25. Section &ReaderSec = Obj->Sections.back();
  26. if (ReaderSec.SectionType > WASM_SEC_CUSTOM &&
  27. ReaderSec.SectionType <= WASM_SEC_LAST_KNOWN)
  28. ReaderSec.Name = sectionTypeToString(ReaderSec.SectionType);
  29. }
  30. return std::move(Obj);
  31. }
  32. } // end namespace wasm
  33. } // end namespace objcopy
  34. } // end namespace llvm