[llvm-objcopy][WebAssembly] Add dump/add/remove-section support

Add support for adding, removing, and dumping wasm sections to objcopy

Differential Revision: https://reviews.llvm.org/D70970
diff --git a/llvm/tools/llvm-objcopy/CMakeLists.txt b/llvm/tools/llvm-objcopy/CMakeLists.txt
index b3706bc..6aa5197 100644
--- a/llvm/tools/llvm-objcopy/CMakeLists.txt
+++ b/llvm/tools/llvm-objcopy/CMakeLists.txt
@@ -33,6 +33,7 @@
   MachO/MachOWriter.cpp
   MachO/MachOLayoutBuilder.cpp
   MachO/Object.cpp
+  wasm/Object.cpp
   wasm/Reader.cpp
   wasm/Writer.cpp
   wasm/WasmObjcopy.cpp
diff --git a/llvm/tools/llvm-objcopy/wasm/Object.cpp b/llvm/tools/llvm-objcopy/wasm/Object.cpp
new file mode 100644
index 0000000..0c41648
--- /dev/null
+++ b/llvm/tools/llvm-objcopy/wasm/Object.cpp
@@ -0,0 +1,36 @@
+//===- Object.cpp ---------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Object.h"
+
+#include "llvm/Support/LEB128.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace llvm {
+namespace objcopy {
+namespace wasm {
+
+using namespace object;
+using namespace llvm::wasm;
+
+void Object::addSectionWithOwnedContents(
+    Section NewSection, std::unique_ptr<MemoryBuffer> &&Content) {
+  Sections.push_back(NewSection);
+  OwnedContents.emplace_back(std::move(Content));
+}
+
+void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
+  // TODO: remove reloc sections for the removed section, handle symbols, etc.
+  Sections.erase(
+      std::remove_if(std::begin(Sections), std::end(Sections), ToRemove),
+      std::end(Sections));
+}
+
+} // end namespace wasm
+} // end namespace objcopy
+} // end namespace llvm
diff --git a/llvm/tools/llvm-objcopy/wasm/Object.h b/llvm/tools/llvm-objcopy/wasm/Object.h
index 78a43be..9db91c4 100644
--- a/llvm/tools/llvm-objcopy/wasm/Object.h
+++ b/llvm/tools/llvm-objcopy/wasm/Object.h
@@ -12,6 +12,7 @@
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Object/Wasm.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include <vector>
 
 namespace llvm {
@@ -30,6 +31,13 @@
   llvm::wasm::WasmObjectHeader Header;
   // For now don't discriminate between kinds of sections.
   std::vector<Section> Sections;
+
+  void addSectionWithOwnedContents(Section NewSection,
+                                   std::unique_ptr<MemoryBuffer> &&Content);
+  void removeSections(function_ref<bool(const Section &)> ToRemove);
+
+private:
+  std::vector<std::unique_ptr<MemoryBuffer>> OwnedContents;
 };
 
 } // end namespace wasm
diff --git a/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp b/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp
index 41816a0..20781ce 100644
--- a/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp
+++ b/llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp
@@ -21,7 +21,58 @@
 
 using namespace object;
 
+static Error dumpSectionToFile(StringRef SecName, StringRef Filename,
+                               Object &Obj) {
+  for (const Section &Sec : Obj.Sections) {
+    if (Sec.Name == SecName) {
+      ArrayRef<uint8_t> Contents = Sec.Contents;
+      Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
+          FileOutputBuffer::create(Filename, Contents.size());
+      if (!BufferOrErr)
+        return BufferOrErr.takeError();
+      std::unique_ptr<FileOutputBuffer> Buf = std::move(*BufferOrErr);
+      std::copy(Contents.begin(), Contents.end(), Buf->getBufferStart());
+      if (Error E = Buf->commit())
+        return E;
+      return Error::success();
+    }
+  }
+  return createStringError(errc::invalid_argument, "section '%s' not found",
+                           SecName.str().c_str());
+}
 static Error handleArgs(const CopyConfig &Config, Object &Obj) {
+  // Only support AddSection, DumpSection, RemoveSection for now.
+  for (StringRef Flag : Config.DumpSection) {
+    StringRef SecName;
+    StringRef FileName;
+    std::tie(SecName, FileName) = Flag.split("=");
+    if (Error E = dumpSectionToFile(SecName, FileName, Obj))
+      return createFileError(FileName, std::move(E));
+  }
+
+  Obj.removeSections([&Config](const Section &Sec) {
+    if (Config.ToRemove.matches(Sec.Name))
+      return true;
+    return false;
+  });
+
+  for (StringRef Flag : Config.AddSection) {
+    StringRef SecName, FileName;
+    std::tie(SecName, FileName) = Flag.split("=");
+    ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
+        MemoryBuffer::getFile(FileName);
+    if (!BufOrErr)
+      return createFileError(FileName, errorCodeToError(BufOrErr.getError()));
+    Section Sec;
+    Sec.SectionType = llvm::wasm::WASM_SEC_CUSTOM;
+    Sec.Name = SecName;
+    std::unique_ptr<MemoryBuffer> Buf = std::move(*BufOrErr);
+    Sec.Contents = makeArrayRef<uint8_t>(
+        reinterpret_cast<const uint8_t *>(Buf->getBufferStart()),
+        Buf->getBufferSize());
+    Obj.addSectionWithOwnedContents(Sec, std::move(Buf));
+  }
+
   if (!Config.AddGnuDebugLink.empty() || !Config.BuildIdLinkDir.empty() ||
       Config.BuildIdLinkInput || Config.BuildIdLinkOutput ||
       Config.ExtractPartition || !Config.SplitDWO.empty() ||
@@ -34,12 +85,10 @@
       !Config.UnneededSymbolsToRemove.empty() ||
       !Config.SymbolsToWeaken.empty() || !Config.SymbolsToKeepGlobal.empty() ||
       !Config.SectionsToRename.empty() || !Config.SetSectionAlignment.empty() ||
-      !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty() ||
-      !Config.ToRemove.empty() || !Config.DumpSection.empty() ||
-      !Config.AddSection.empty()) {
+      !Config.SetSectionFlags.empty() || !Config.SymbolsToRename.empty()) {
     return createStringError(
         llvm::errc::invalid_argument,
-        "no flags are supported yet, only basic copying is allowed");
+        "only add-section, dump-section, and remove-section are supported");
   }
   return Error::success();
 }
@@ -53,7 +102,7 @@
   Object *Obj = ObjOrErr->get();
   assert(Obj && "Unable to deserialize Wasm object");
   if (Error E = handleArgs(Config, *Obj))
-    return createFileError(Config.InputFilename, std::move(E));
+    return E;
   Writer TheWriter(*Obj, Out);
   if (Error E = TheWriter.write())
     return createFileError(Config.OutputFilename, std::move(E));