WasmWriter.h 1.6 KB

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