aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/ObjCopy/wasm/WasmObject.cpp
blob: 991191320c40b82898af2e0388f111788b0feaeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//===- WasmObject.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 "WasmObject.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) {
  if (isRelocatableObject) {
    // For relocatable objects, avoid actually removing any sections,
    // since that can invalidate the symbol table and relocation sections.
    // TODO: Allow removal of sections by re-generating symbol table and
    // relocation sections here instead.
    for (auto &Sec : Sections) {
      if (ToRemove(Sec)) {
        Sec.Name = ".objcopy.removed";
        Sec.SectionType = wasm::WASM_SEC_CUSTOM;
        Sec.Contents = {};
        Sec.HeaderSecSizeEncodingLen = std::nullopt;
      }
    }
  } else {
    llvm::erase_if(Sections, ToRemove);
  }
}

} // end namespace wasm
} // end namespace objcopy
} // end namespace llvm