LLVM  15.0.0git
WasmWriter.cpp
Go to the documentation of this file.
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"
15 
16 namespace llvm {
17 namespace objcopy {
18 namespace wasm {
19 
20 using namespace object;
21 using namespace llvm::wasm;
22 
23 Writer::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  // Pad the LEB value out to 5 bytes to make it a predictable size, and
33  // match the behavior of clang.
34  encodeULEB128(SectionSize, OS, 5);
35  if (HasName) {
36  encodeULEB128(S.Name.size(), OS);
37  OS << S.Name;
38  }
39  // Total section size is the content size plus 1 for the section type and
40  // 5 for the LEB-encoded size.
41  SectionSize = SectionSize + 1 + 5;
42  return Header;
43 }
44 
45 size_t Writer::finalize() {
46  size_t ObjectSize = sizeof(WasmMagic) + sizeof(WasmVersion);
47  SectionHeaders.reserve(Obj.Sections.size());
48  // Finalize the headers of each section so we know the total size.
49  for (const Section &S : Obj.Sections) {
50  size_t SectionSize;
51  SectionHeaders.push_back(createSectionHeader(S, SectionSize));
52  ObjectSize += SectionSize;
53  }
54  return ObjectSize;
55 }
56 
58  size_t TotalSize = finalize();
59  Out.reserveExtraSpace(TotalSize);
60 
61  // Write the header.
62  Out.write(Obj.Header.Magic.data(), Obj.Header.Magic.size());
64  support::endian::write32le(&Version, Obj.Header.Version);
65  Out.write(reinterpret_cast<const char *>(&Version), sizeof(Version));
66 
67  // Write each section.
68  for (size_t I = 0, S = SectionHeaders.size(); I < S; ++I) {
69  Out.write(SectionHeaders[I].data(), SectionHeaders[I].size());
70  Out.write(reinterpret_cast<const char *>(Obj.Sections[I].Contents.data()),
71  Obj.Sections[I].Contents.size());
72  }
73 
74  return Error::success();
75 }
76 
77 } // end namespace wasm
78 } // end namespace objcopy
79 } // end namespace llvm
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::wasm::WasmVersion
const uint32_t WasmVersion
Definition: Wasm.h:28
llvm::support::endian::write32le
void write32le(void *P, uint32_t V)
Definition: Endian.h:416
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::pdb::PDB_ColorItem::SectionHeader
@ SectionHeader
Errc.h
llvm::getULEB128Size
unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
Definition: LEB128.cpp:19
WasmWriter.h
llvm::wasm::WASM_SEC_CUSTOM
@ WASM_SEC_CUSTOM
Definition: Wasm.h:242
finalize
arc branch finalize
Definition: ARCBranchFinalize.cpp:65
llvm::IndexedInstrProf::Version
const uint64_t Version
Definition: InstrProf.h:1027
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
LEB128.h
llvm::COFF::SectionSize
@ SectionSize
Definition: COFF.h:60
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::size
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1598
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::wasm::WasmMagic
const char WasmMagic[]
Definition: Wasm.h:26
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
Wasm.h
llvm::wasm
Definition: Wasm.h:23
llvm::objcopy::wasm::Writer::write
Error write()
Definition: WasmWriter.cpp:57
llvm::encodeULEB128
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
raw_ostream.h
Endian.h