1292934Sdim//===---- lib/ReaderWriter/MachO/SectCreateFile.h ---------------*- c++ -*-===//
2292934Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6292934Sdim//
7292934Sdim//===----------------------------------------------------------------------===//
8292934Sdim
9292934Sdim#ifndef LLD_READER_WRITER_MACHO_SECTCREATE_FILE_H
10292934Sdim#define LLD_READER_WRITER_MACHO_SECTCREATE_FILE_H
11292934Sdim
12292934Sdim#include "lld/Core/DefinedAtom.h"
13292934Sdim#include "lld/Core/Simple.h"
14292934Sdim#include "lld/ReaderWriter/MachOLinkingContext.h"
15292934Sdim
16292934Sdimnamespace lld {
17292934Sdimnamespace mach_o {
18292934Sdim
19292934Sdim//
20292934Sdim// A FlateNamespaceFile instance may be added as a resolution source of last
21292934Sdim// resort, depending on how -flat_namespace and -undefined are set.
22292934Sdim//
23292934Sdimclass SectCreateFile : public File {
24292934Sdimpublic:
25292934Sdim  class SectCreateAtom : public SimpleDefinedAtom {
26292934Sdim  public:
27292934Sdim    SectCreateAtom(const File &file, StringRef segName, StringRef sectName,
28292934Sdim                   std::unique_ptr<MemoryBuffer> content)
29292934Sdim      : SimpleDefinedAtom(file),
30292934Sdim        _combinedName((segName + "/" + sectName).str()),
31292934Sdim        _content(std::move(content)) {}
32292934Sdim
33303239Sdim    ~SectCreateAtom() override = default;
34303239Sdim
35292934Sdim    uint64_t size() const override { return _content->getBufferSize(); }
36292934Sdim
37292934Sdim    Scope scope() const override { return scopeGlobal; }
38292934Sdim
39292934Sdim    ContentType contentType() const override { return typeSectCreate; }
40292934Sdim
41292934Sdim    SectionChoice sectionChoice() const override { return sectionCustomRequired; }
42292934Sdim
43292934Sdim    StringRef customSectionName() const override { return _combinedName; }
44292934Sdim
45292934Sdim    DeadStripKind deadStrip() const override { return deadStripNever; }
46292934Sdim
47292934Sdim    ArrayRef<uint8_t> rawContent() const override {
48292934Sdim      const uint8_t *data =
49292934Sdim        reinterpret_cast<const uint8_t*>(_content->getBufferStart());
50292934Sdim      return ArrayRef<uint8_t>(data, _content->getBufferSize());
51292934Sdim    }
52292934Sdim
53292934Sdim    StringRef segmentName() const { return _segName; }
54292934Sdim    StringRef sectionName() const { return _sectName; }
55292934Sdim
56292934Sdim  private:
57292934Sdim    std::string _combinedName;
58292934Sdim    StringRef _segName;
59292934Sdim    StringRef _sectName;
60292934Sdim    std::unique_ptr<MemoryBuffer> _content;
61292934Sdim  };
62292934Sdim
63303239Sdim  SectCreateFile() : File("sectcreate", kindSectCreateObject) {}
64292934Sdim
65292934Sdim  void addSection(StringRef seg, StringRef sect,
66292934Sdim                  std::unique_ptr<MemoryBuffer> content) {
67292934Sdim    _definedAtoms.push_back(
68292934Sdim      new (allocator()) SectCreateAtom(*this, seg, sect, std::move(content)));
69292934Sdim  }
70292934Sdim
71303239Sdim  const AtomRange<DefinedAtom> defined() const override {
72292934Sdim    return _definedAtoms;
73292934Sdim  }
74292934Sdim
75303239Sdim  const AtomRange<UndefinedAtom> undefined() const override {
76292934Sdim    return _noUndefinedAtoms;
77292934Sdim  }
78292934Sdim
79303239Sdim  const AtomRange<SharedLibraryAtom> sharedLibrary() const override {
80292934Sdim    return _noSharedLibraryAtoms;
81292934Sdim  }
82292934Sdim
83303239Sdim  const AtomRange<AbsoluteAtom> absolute() const override {
84292934Sdim    return _noAbsoluteAtoms;
85292934Sdim  }
86292934Sdim
87303239Sdim  void clearAtoms() override {
88303239Sdim    _definedAtoms.clear();
89303239Sdim    _noUndefinedAtoms.clear();
90303239Sdim    _noSharedLibraryAtoms.clear();
91303239Sdim    _noAbsoluteAtoms.clear();
92303239Sdim  }
93303239Sdim
94292934Sdimprivate:
95292934Sdim  AtomVector<DefinedAtom> _definedAtoms;
96292934Sdim};
97292934Sdim
98292934Sdim} // namespace mach_o
99292934Sdim} // namespace lld
100292934Sdim
101292934Sdim#endif // LLD_READER_WRITER_MACHO_SECTCREATE_FILE_H
102