xref: /netbsd-src/external/apache2/llvm/dist/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 //===- WasmObjcopy.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 "WasmObjcopy.h"
10 #include "CommonConfig.h"
11 #include "Object.h"
12 #include "Reader.h"
13 #include "Writer.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/FileOutputBuffer.h"
16 
17 namespace llvm {
18 namespace objcopy {
19 namespace wasm {
20 
21 using namespace object;
22 
dumpSectionToFile(StringRef SecName,StringRef Filename,Object & Obj)23 static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
24                                Object &Obj) {
25   for (const Section &Sec : Obj.Sections) {
26     if (Sec.Name == SecName) {
27       ArrayRef<uint8_t> Contents = Sec.Contents;
28       Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
29           FileOutputBuffer::create(Filename, Contents.size());
30       if (!BufferOrErr)
31         return BufferOrErr.takeError();
32       std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
33       std::copy(Contents.begin(), Contents.end(), Buf->getBufferStart());
34       if (Error E = Buf->commit())
35         return E;
36       return Error::success();
37     }
38   }
39   return createStringError(errc::invalid_argument, "section '%s' not found",
40                            SecName.str().c_str());
41 }
handleArgs(const CommonConfig & Config,Object & Obj)42 static Error handleArgs(const CommonConfig &Config, Object &Obj) {
43   // Only support AddSection, DumpSection, RemoveSection for now.
44   for (StringRef Flag : Config.DumpSection) {
45     StringRef SecName;
46     StringRef FileName;
47     std::tie(SecName, FileName) = Flag.split("=");
48     if (Error E = dumpSectionToFile(SecName, FileName, Obj))
49       return createFileError(FileName, std::move(E));
50   }
51 
52   Obj.removeSections([&Config](const Section &Sec) {
53     if (Config.ToRemove.matches(Sec.Name))
54       return true;
55     return false;
56   });
57 
58   for (StringRef Flag : Config.AddSection) {
59     StringRef SecName, FileName;
60     std::tie(SecName, FileName) = Flag.split("=");
61     ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
62         MemoryBuffer::getFile(FileName);
63     if (!BufOrErr)
64       return createFileError(FileName, errorCodeToError(BufOrErr.getError()));
65     Section Sec;
66     Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM;
67     Sec.Name = SecName;
68     std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
69     Sec.Contents = makeArrayRef<uint8_t>(
70         reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
71         Buf->getBufferSize());
72     Obj.addSectionWithOwnedContents(Sec, std::move(Buf));
73   }
74 
75   return Error::success();
76 }
77 
executeObjcopyOnBinary(const CommonConfig & Config,const WasmConfig &,object::WasmObjectFile & In,raw_ostream & Out)78 Error executeObjcopyOnBinary(const CommonConfig &Config, const WasmConfig &,
79                              object::WasmObjectFile &In, raw_ostream &Out) {
80   Reader TheReader(In);
81   Expected<std::unique_ptr<Object>> ObjOrErr = TheReader.create();
82   if (!ObjOrErr)
83     return createFileError(Config.InputFilename, ObjOrErr.takeError());
84   Object *Obj = ObjOrErr->get();
85   assert(Obj && "Unable to deserialize Wasm object");
86   if (Error E = handleArgs(Config, *Obj))
87     return E;
88   Writer TheWriter(*Obj, Out);
89   if (Error E = TheWriter.write())
90     return createFileError(Config.OutputFilename, std::move(E));
91   return Error::success();
92 }
93 
94 } // end namespace wasm
95 } // end namespace objcopy
96 } // end namespace llvm
97