1 //===- Writer.h -------------------------------------------------*- C++ -*-===//
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 #ifndef LLVM_TOOLS_LLVM_OBJCOPY_WASM_WRITER_H
10 #define LLVM_TOOLS_LLVM_OBJCOPY_WASM_WRITER_H
11 
12 #include "Buffer.h"
13 #include "Object.h"
14 #include <cstdint>
15 #include <vector>
16 
17 namespace llvm {
18 namespace objcopy {
19 namespace wasm {
20 
21 class Writer {
22 public:
Writer(Object & Obj,Buffer & Buf)23   Writer(Object &Obj, Buffer &Buf) : Obj(Obj), Buf(Buf) {}
24   Error write();
25 
26 private:
27   using SectionHeader = SmallVector<char, 8>;
28   Object &Obj;
29   Buffer &Buf;
30   std::vector<SectionHeader> SectionHeaders;
31 
32   /// Generate a wasm section section header for S.
33   /// The header consists of
34   /// * A one-byte section ID (aka the section type).
35   /// * The size of the section contents, encoded as ULEB128.
36   /// * If the section is a custom section (type 0) it also has a name, which is
37   ///   encoded as a length-prefixed string. The encoded section size *includes*
38   ///   this string.
39   /// See https://webassembly.github.io/spec/core/binary/modules.html#sections
40   /// Return the header and store the total size in SectionSize.
41   static SectionHeader createSectionHeader(const Section &S,
42                                            size_t &SectionSize);
43   size_t finalize();
44 };
45 
46 } // end namespace wasm
47 } // end namespace objcopy
48 } // end namespace llvm
49 
50 #endif // LLVM_TOOLS_LLVM_OBJCOPY_WASM_WRITER_H
51