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