xref: /llvm-project/llvm/lib/ObjCopy/wasm/WasmObject.cpp (revision 54dad9e269f365d0eff2f63c5ee843564eecca7e)
125d7b4fbSAlexey Lapshin //===- WasmObject.cpp -----------------------------------------------------===//
225d7b4fbSAlexey Lapshin //
325d7b4fbSAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
425d7b4fbSAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
525d7b4fbSAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
625d7b4fbSAlexey Lapshin //
725d7b4fbSAlexey Lapshin //===----------------------------------------------------------------------===//
825d7b4fbSAlexey Lapshin 
925d7b4fbSAlexey Lapshin #include "WasmObject.h"
1025d7b4fbSAlexey Lapshin 
1125d7b4fbSAlexey Lapshin namespace llvm {
1225d7b4fbSAlexey Lapshin namespace objcopy {
1325d7b4fbSAlexey Lapshin namespace wasm {
1425d7b4fbSAlexey Lapshin 
1525d7b4fbSAlexey Lapshin using namespace object;
1625d7b4fbSAlexey Lapshin using namespace llvm::wasm;
1725d7b4fbSAlexey Lapshin 
1825d7b4fbSAlexey Lapshin void Object::addSectionWithOwnedContents(
1925d7b4fbSAlexey Lapshin     Section NewSection, std::unique_ptr<MemoryBuffer> &&Content) {
2025d7b4fbSAlexey Lapshin   Sections.push_back(NewSection);
2125d7b4fbSAlexey Lapshin   OwnedContents.emplace_back(std::move(Content));
2225d7b4fbSAlexey Lapshin }
2325d7b4fbSAlexey Lapshin 
2425d7b4fbSAlexey Lapshin void Object::removeSections(function_ref<bool(const Section &)> ToRemove) {
25*2a6136e5SSam Clegg   if (isRelocatableObject) {
26*2a6136e5SSam Clegg     // For relocatable objects, avoid actually removing any sections,
27*2a6136e5SSam Clegg     // since that can invalidate the symbol table and relocation sections.
28*2a6136e5SSam Clegg     // TODO: Allow removal of sections by re-generating symbol table and
29*2a6136e5SSam Clegg     // relocation sections here instead.
30*2a6136e5SSam Clegg     for (auto &Sec : Sections) {
31*2a6136e5SSam Clegg       if (ToRemove(Sec)) {
32*2a6136e5SSam Clegg         Sec.Name = ".objcopy.removed";
33*2a6136e5SSam Clegg         Sec.SectionType = wasm::WASM_SEC_CUSTOM;
34*2a6136e5SSam Clegg         Sec.Contents = {};
35*2a6136e5SSam Clegg         Sec.HeaderSecSizeEncodingLen = std::nullopt;
36*2a6136e5SSam Clegg       }
37*2a6136e5SSam Clegg     }
38*2a6136e5SSam Clegg   } else {
3925d7b4fbSAlexey Lapshin     llvm::erase_if(Sections, ToRemove);
4025d7b4fbSAlexey Lapshin   }
41*2a6136e5SSam Clegg }
4225d7b4fbSAlexey Lapshin 
4325d7b4fbSAlexey Lapshin } // end namespace wasm
4425d7b4fbSAlexey Lapshin } // end namespace objcopy
4525d7b4fbSAlexey Lapshin } // end namespace llvm
46