xref: /llvm-project/llvm/lib/ObjCopy/wasm/WasmReader.cpp (revision c9dd1cc6f0536c6547f322107cd27cd953913f5c)
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 
9 #include "WasmReader.h"
10 
11 namespace llvm {
12 namespace objcopy {
13 namespace wasm {
14 
15 using namespace object;
16 using namespace llvm::wasm;
17 
18 Expected<std::unique_ptr<Object>> Reader::create() const {
19   auto Obj = std::make_unique<Object>();
20   Obj->Header = WasmObj.getHeader();
21   std::vector<Section> Sections;
22   Obj->Sections.reserve(WasmObj.getNumSections());
23   for (const SectionRef &Sec : WasmObj.sections()) {
24     const WasmSection &WS = WasmObj.getWasmSection(Sec);
25     Obj->Sections.push_back(
26         {static_cast<uint8_t>(WS.Type), WS.Name, WS.Content});
27     // Give known sections standard names to allow them to be selected.
28     Section &ReaderSec = Obj->Sections.back();
29     if (ReaderSec.SectionType > WASM_SEC_CUSTOM &&
30         ReaderSec.SectionType <= WASM_SEC_TAG)
31       ReaderSec.Name = sectionTypeToString(ReaderSec.SectionType);
32     // If the section type is CUSTOM, it has a name already. If it's a new type
33     // of section that we don't explicitly handle here, it will have an empty
34     // name and objcopy won't be able to select it by name (e.g. for removal
35     // or dumping) but it will still be valid and able to be copied.
36   }
37   return std::move(Obj);
38 }
39 
40 } // end namespace wasm
41 } // end namespace objcopy
42 } // end namespace llvm
43