1//===- WasmWriter.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 "WasmWriter.h"
10#include "llvm/BinaryFormat/Wasm.h"
11#include "llvm/Support/Endian.h"
12#include "llvm/Support/Errc.h"
13#include "llvm/Support/LEB128.h"
14#include "llvm/Support/raw_ostream.h"
15
16namespace llvm {
17namespace objcopy {
18namespace wasm {
19
20using namespace object;
21using namespace llvm::wasm;
22
23Writer::SectionHeader Writer::createSectionHeader(const Section &S,
24                                                  size_t &SectionSize) {
25  SectionHeader Header;
26  raw_svector_ostream OS(Header);
27  OS << S.SectionType;
28  bool HasName = S.SectionType == WASM_SEC_CUSTOM;
29  SectionSize = S.Contents.size();
30  if (HasName)
31    SectionSize += getULEB128Size(S.Name.size()) + S.Name.size();
32  // If we read this section from an object file, use its original size for the
33  // padding of the LEB value to avoid changing the file size. Otherwise, pad
34  // out to 5 bytes to make it predictable, and match the behavior of clang.
35  unsigned HeaderSecSizeEncodingLen =
36      S.HeaderSecSizeEncodingLen ? *S.HeaderSecSizeEncodingLen : 5;
37  encodeULEB128(SectionSize, OS, HeaderSecSizeEncodingLen);
38  if (HasName) {
39    encodeULEB128(S.Name.size(), OS);
40    OS << S.Name;
41  }
42  // Total section size is the content size plus 1 for the section type and
43  // the LEB-encoded size.
44  SectionSize = SectionSize + 1 + HeaderSecSizeEncodingLen;
45  return Header;
46}
47
48size_t Writer::finalize() {
49  size_t ObjectSize = sizeof(WasmMagic) + sizeof(WasmVersion);
50  SectionHeaders.reserve(Obj.Sections.size());
51  // Finalize the headers of each section so we know the total size.
52  for (const Section &S : Obj.Sections) {
53    size_t SectionSize;
54    SectionHeaders.push_back(createSectionHeader(S, SectionSize));
55    ObjectSize += SectionSize;
56  }
57  return ObjectSize;
58}
59
60Error Writer::write() {
61  size_t TotalSize = finalize();
62  Out.reserveExtraSpace(TotalSize);
63
64  // Write the header.
65  Out.write(Obj.Header.Magic.data(), Obj.Header.Magic.size());
66  uint32_t Version;
67  support::endian::write32le(&Version, Obj.Header.Version);
68  Out.write(reinterpret_cast<const char *>(&Version), sizeof(Version));
69
70  // Write each section.
71  for (size_t I = 0, S = SectionHeaders.size(); I < S; ++I) {
72    Out.write(SectionHeaders[I].data(), SectionHeaders[I].size());
73    Out.write(reinterpret_cast<const char *>(Obj.Sections[I].Contents.data()),
74              Obj.Sections[I].Contents.size());
75  }
76
77  return Error::success();
78}
79
80} // end namespace wasm
81} // end namespace objcopy
82} // end namespace llvm
83