xref: /llvm-project/llvm/lib/ObjCopy/wasm/WasmObject.cpp (revision 2a6136e552d24b6bf665c42a6e32efc0b2d88fbf)
1 //===- WasmObject.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 "WasmObject.h"
10 
11 #include "llvm/Support/LEB128.h"
12 #include "llvm/Support/raw_ostream.h"
13 
14 namespace llvm {
15 namespace objcopy {
16 namespace wasm {
17 
18 using namespace object;
19 using namespace llvm::wasm;
20 
21 void Object::addSectionWithOwnedContents(
22     Section NewSection, std::unique_ptr<MemoryBuffer> &&Content) {
23   Sections.push_back(NewSection);
24   OwnedContents.emplace_back(std::move(Content));
25 }
26 
27 void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
28   if (isRelocatableObject) {
29     // For relocatable objects, avoid actually removing any sections,
30     // since that can invalidate the symbol table and relocation sections.
31     // TODO: Allow removal of sections by re-generating symbol table and
32     // relocation sections here instead.
33     for (auto &Sec : Sections) {
34       if (ToRemove(Sec)) {
35         Sec.Name = ".objcopy.removed";
36         Sec.SectionType = wasm::WASM_SEC_CUSTOM;
37         Sec.Contents = {};
38         Sec.HeaderSecSizeEncodingLen = std::nullopt;
39       }
40     }
41   } else {
42     llvm::erase_if(Sections, ToRemove);
43   }
44 }
45 
46 } // end namespace wasm
47 } // end namespace objcopy
48 } // end namespace llvm
49