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 "Buffer.h"
11 #include "CopyConfig.h"
12 #include "Object.h"
13 #include "Reader.h"
14 #include "Writer.h"
15 #include "llvm/Support/Errc.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 CopyConfig & Config,Object & Obj)42 static Error handleArgs(const CopyConfig &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 if (!Config.AddGnuDebugLink.empty() || !Config.BuildIdLinkDir.empty() ||
76 Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
77 Config.ExtractPartition || !Config.SplitDWO.empty() ||
78 !Config.SymbolsPrefix.empty() || !Config.AllocSectionsPrefix.empty() ||
79 Config.DiscardMode != DiscardType::None || Config.NewSymbolVisibility ||
80 !Config.SymbolsToAdd.empty() || !Config.RPathToAdd.empty() ||
81 !Config.OnlySection.empty() || !Config.SymbolsToGlobalize.empty() ||
82 !Config.SymbolsToKeep.empty() || !Config.SymbolsToLocalize.empty() ||
83 !Config.SymbolsToRemove.empty() ||
84 !Config.UnneededSymbolsToRemove.empty() ||
85 !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
86 !Config.SectionsToRename.empty() || !Config.SetSectionAlignment.empty() ||
87 !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty()) {
88 return createStringError(
89 llvm::errc::invalid_argument,
90 "only add-section, dump-section, and remove-section are supported");
91 }
92 return Error::success();
93 }
94
executeObjcopyOnBinary(const CopyConfig & Config,object::WasmObjectFile & In,Buffer & Out)95 Error executeObjcopyOnBinary(const CopyConfig &Config,
96 object::WasmObjectFile &In, Buffer &Out) {
97 Reader TheReader(In);
98 Expected<std::unique_ptr<Object>> ObjOrErr = TheReader.create();
99 if (!ObjOrErr)
100 return createFileError(Config.InputFilename, ObjOrErr.takeError());
101 Object *Obj = ObjOrErr->get();
102 assert(Obj && "Unable to deserialize Wasm object");
103 if (Error E = handleArgs(Config, *Obj))
104 return E;
105 Writer TheWriter(*Obj, Out);
106 if (Error E = TheWriter.write())
107 return createFileError(Config.OutputFilename, std::move(E));
108 return Error::success();
109 }
110
111 } // end namespace wasm
112 } // end namespace objcopy
113 } // end namespace llvm
114