LLVM: lib/ObjCopy/wasm/WasmWriter.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
14
15namespace llvm {
17namespace wasm {
18
19using namespace object;
20using namespace llvm::wasm;
21
22Writer::SectionHeader Writer::createSectionHeader(const Section &S,
23 size_t &SectionSize) {
24 SectionHeader Header;
25 raw_svector_ostream OS(Header);
26 OS << S.SectionType;
29 if (HasName)
31
32
33
34 unsigned HeaderSecSizeEncodingLen = S.HeaderSecSizeEncodingLen.value_or(5);
35 encodeULEB128(SectionSize, OS, HeaderSecSizeEncodingLen);
36 if (HasName) {
38 OS << S.Name;
39 }
40
41
43 return Header;
44}
45
46size_t Writer::finalize() {
48 SectionHeaders.reserve(Obj.Sections.size());
49
50 for (const Section &S : Obj.Sections) {
52 SectionHeaders.push_back(createSectionHeader(S, SectionSize));
54 }
55 return ObjectSize;
56}
57
59 size_t TotalSize = finalize();
60 Out.reserveExtraSpace(TotalSize);
61
62
63 Out.write(Obj.Header.Magic.data(), Obj.Header.Magic.size());
66 Out.write(reinterpret_cast<const char *>(&Version), sizeof(Version));
67
68
69 for (size_t I = 0, S = SectionHeaders.size(); I < S; ++I) {
70 Out.write(SectionHeaders[I].data(), SectionHeaders[I].size());
71 Out.write(reinterpret_cast<const char *>(Obj.Sections[I].Contents.data()),
72 Obj.Sections[I].Contents.size());
73 }
74
76}
77
78}
79}
80}
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Error write()
Definition WasmWriter.cpp:58
void write32le(void *P, uint32_t V)
const uint32_t WasmVersion
This is an optimization pass for GlobalISel generic memory operations.
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.
FunctionAddr VTableAddr uintptr_t uintptr_t Version
LLVM_ABI unsigned getULEB128Size(uint64_t Value)
Utility function to get the size of the ULEB128-encoded value.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.