xref: /freebsd-src/contrib/llvm-project/llvm/lib/ObjCopy/wasm/WasmWriter.cpp (revision 8a4dda33d67586ca2624f2a38417baa03a533a7f)
181ad6265SDimitry Andric //===- WasmWriter.cpp -----------------------------------------------------===//
281ad6265SDimitry Andric //
381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
681ad6265SDimitry Andric //
781ad6265SDimitry Andric //===----------------------------------------------------------------------===//
881ad6265SDimitry Andric 
981ad6265SDimitry Andric #include "WasmWriter.h"
1081ad6265SDimitry Andric #include "llvm/BinaryFormat/Wasm.h"
1181ad6265SDimitry Andric #include "llvm/Support/Endian.h"
1281ad6265SDimitry Andric #include "llvm/Support/Errc.h"
1381ad6265SDimitry Andric #include "llvm/Support/LEB128.h"
1481ad6265SDimitry Andric #include "llvm/Support/raw_ostream.h"
1581ad6265SDimitry Andric 
1681ad6265SDimitry Andric namespace llvm {
1781ad6265SDimitry Andric namespace objcopy {
1881ad6265SDimitry Andric namespace wasm {
1981ad6265SDimitry Andric 
2081ad6265SDimitry Andric using namespace object;
2181ad6265SDimitry Andric using namespace llvm::wasm;
2281ad6265SDimitry Andric 
createSectionHeader(const Section & S,size_t & SectionSize)2381ad6265SDimitry Andric Writer::SectionHeader Writer::createSectionHeader(const Section &S,
2481ad6265SDimitry Andric                                                   size_t &SectionSize) {
2581ad6265SDimitry Andric   SectionHeader Header;
2681ad6265SDimitry Andric   raw_svector_ostream OS(Header);
2781ad6265SDimitry Andric   OS << S.SectionType;
2881ad6265SDimitry Andric   bool HasName = S.SectionType == WASM_SEC_CUSTOM;
2981ad6265SDimitry Andric   SectionSize = S.Contents.size();
3081ad6265SDimitry Andric   if (HasName)
3181ad6265SDimitry Andric     SectionSize += getULEB128Size(S.Name.size()) + S.Name.size();
32*8a4dda33SDimitry Andric   // If we read this section from an object file, use its original size for the
33*8a4dda33SDimitry Andric   // padding of the LEB value to avoid changing the file size. Otherwise, pad
34*8a4dda33SDimitry Andric   // out to 5 bytes to make it predictable, and match the behavior of clang.
35*8a4dda33SDimitry Andric   unsigned HeaderSecSizeEncodingLen =
36*8a4dda33SDimitry Andric       S.HeaderSecSizeEncodingLen ? *S.HeaderSecSizeEncodingLen : 5;
37*8a4dda33SDimitry Andric   encodeULEB128(SectionSize, OS, HeaderSecSizeEncodingLen);
3881ad6265SDimitry Andric   if (HasName) {
3981ad6265SDimitry Andric     encodeULEB128(S.Name.size(), OS);
4081ad6265SDimitry Andric     OS << S.Name;
4181ad6265SDimitry Andric   }
4281ad6265SDimitry Andric   // Total section size is the content size plus 1 for the section type and
43*8a4dda33SDimitry Andric   // the LEB-encoded size.
44*8a4dda33SDimitry Andric   SectionSize = SectionSize + 1 + HeaderSecSizeEncodingLen;
4581ad6265SDimitry Andric   return Header;
4681ad6265SDimitry Andric }
4781ad6265SDimitry Andric 
finalize()4881ad6265SDimitry Andric size_t Writer::finalize() {
4981ad6265SDimitry Andric   size_t ObjectSize = sizeof(WasmMagic) + sizeof(WasmVersion);
5081ad6265SDimitry Andric   SectionHeaders.reserve(Obj.Sections.size());
5181ad6265SDimitry Andric   // Finalize the headers of each section so we know the total size.
5281ad6265SDimitry Andric   for (const Section &S : Obj.Sections) {
5381ad6265SDimitry Andric     size_t SectionSize;
5481ad6265SDimitry Andric     SectionHeaders.push_back(createSectionHeader(S, SectionSize));
5581ad6265SDimitry Andric     ObjectSize += SectionSize;
5681ad6265SDimitry Andric   }
5781ad6265SDimitry Andric   return ObjectSize;
5881ad6265SDimitry Andric }
5981ad6265SDimitry Andric 
write()6081ad6265SDimitry Andric Error Writer::write() {
6181ad6265SDimitry Andric   size_t TotalSize = finalize();
6281ad6265SDimitry Andric   Out.reserveExtraSpace(TotalSize);
6381ad6265SDimitry Andric 
6481ad6265SDimitry Andric   // Write the header.
6581ad6265SDimitry Andric   Out.write(Obj.Header.Magic.data(), Obj.Header.Magic.size());
6681ad6265SDimitry Andric   uint32_t Version;
6781ad6265SDimitry Andric   support::endian::write32le(&Version, Obj.Header.Version);
6881ad6265SDimitry Andric   Out.write(reinterpret_cast<const char *>(&Version), sizeof(Version));
6981ad6265SDimitry Andric 
7081ad6265SDimitry Andric   // Write each section.
7181ad6265SDimitry Andric   for (size_t I = 0, S = SectionHeaders.size(); I < S; ++I) {
7281ad6265SDimitry Andric     Out.write(SectionHeaders[I].data(), SectionHeaders[I].size());
7381ad6265SDimitry Andric     Out.write(reinterpret_cast<const char *>(Obj.Sections[I].Contents.data()),
7481ad6265SDimitry Andric               Obj.Sections[I].Contents.size());
7581ad6265SDimitry Andric   }
7681ad6265SDimitry Andric 
7781ad6265SDimitry Andric   return Error::success();
7881ad6265SDimitry Andric }
7981ad6265SDimitry Andric 
8081ad6265SDimitry Andric } // end namespace wasm
8181ad6265SDimitry Andric } // end namespace objcopy
8281ad6265SDimitry Andric } // end namespace llvm
83