1 //===---- lib/ReaderWriter/MachO/SectCreateFile.h ---------------*- c++ -*-===// 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 #ifndef LLD_READER_WRITER_MACHO_SECTCREATE_FILE_H 10 #define LLD_READER_WRITER_MACHO_SECTCREATE_FILE_H 11 12 #include "lld/Core/DefinedAtom.h" 13 #include "lld/Core/Simple.h" 14 #include "lld/ReaderWriter/MachOLinkingContext.h" 15 16 namespace lld { 17 namespace mach_o { 18 19 // 20 // A FlateNamespaceFile instance may be added as a resolution source of last 21 // resort, depending on how -flat_namespace and -undefined are set. 22 // 23 class SectCreateFile : public File { 24 public: 25 class SectCreateAtom : public SimpleDefinedAtom { 26 public: SectCreateAtom(const File & file,StringRef segName,StringRef sectName,std::unique_ptr<MemoryBuffer> content)27 SectCreateAtom(const File &file, StringRef segName, StringRef sectName, 28 std::unique_ptr<MemoryBuffer> content) 29 : SimpleDefinedAtom(file), 30 _combinedName((segName + "/" + sectName).str()), 31 _content(std::move(content)) {} 32 33 ~SectCreateAtom() override = default; 34 size()35 uint64_t size() const override { return _content->getBufferSize(); } 36 scope()37 Scope scope() const override { return scopeGlobal; } 38 contentType()39 ContentType contentType() const override { return typeSectCreate; } 40 sectionChoice()41 SectionChoice sectionChoice() const override { return sectionCustomRequired; } 42 customSectionName()43 StringRef customSectionName() const override { return _combinedName; } 44 deadStrip()45 DeadStripKind deadStrip() const override { return deadStripNever; } 46 rawContent()47 ArrayRef<uint8_t> rawContent() const override { 48 const uint8_t *data = 49 reinterpret_cast<const uint8_t*>(_content->getBufferStart()); 50 return ArrayRef<uint8_t>(data, _content->getBufferSize()); 51 } 52 segmentName()53 StringRef segmentName() const { return _segName; } sectionName()54 StringRef sectionName() const { return _sectName; } 55 56 private: 57 std::string _combinedName; 58 StringRef _segName; 59 StringRef _sectName; 60 std::unique_ptr<MemoryBuffer> _content; 61 }; 62 SectCreateFile()63 SectCreateFile() : File("sectcreate", kindSectCreateObject) {} 64 addSection(StringRef seg,StringRef sect,std::unique_ptr<MemoryBuffer> content)65 void addSection(StringRef seg, StringRef sect, 66 std::unique_ptr<MemoryBuffer> content) { 67 _definedAtoms.push_back( 68 new (allocator()) SectCreateAtom(*this, seg, sect, std::move(content))); 69 } 70 defined()71 const AtomRange<DefinedAtom> defined() const override { 72 return _definedAtoms; 73 } 74 undefined()75 const AtomRange<UndefinedAtom> undefined() const override { 76 return _noUndefinedAtoms; 77 } 78 sharedLibrary()79 const AtomRange<SharedLibraryAtom> sharedLibrary() const override { 80 return _noSharedLibraryAtoms; 81 } 82 absolute()83 const AtomRange<AbsoluteAtom> absolute() const override { 84 return _noAbsoluteAtoms; 85 } 86 clearAtoms()87 void clearAtoms() override { 88 _definedAtoms.clear(); 89 _noUndefinedAtoms.clear(); 90 _noSharedLibraryAtoms.clear(); 91 _noAbsoluteAtoms.clear(); 92 } 93 94 private: 95 AtomVector<DefinedAtom> _definedAtoms; 96 }; 97 98 } // namespace mach_o 99 } // namespace lld 100 101 #endif // LLD_READER_WRITER_MACHO_SECTCREATE_FILE_H 102