1 //===- lib/ReaderWriter/MachO/Atoms.h ---------------------------*- C++ -*-===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_READER_WRITER_MACHO_ATOMS_H
11 #define LLD_READER_WRITER_MACHO_ATOMS_H
13 #include "lld/Core/Atom.h"
14 #include "lld/Core/DefinedAtom.h"
15 #include "lld/Core/SharedLibraryAtom.h"
16 #include "lld/Core/Simple.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
28 class MachODefinedAtom : public SimpleDefinedAtom {
30 MachODefinedAtom(const File &f, const StringRef name, Scope scope,
31 ContentType type, Merge merge, bool thumb, bool noDeadStrip,
32 const ArrayRef<uint8_t> content, Alignment align)
33 : SimpleDefinedAtom(f), _name(name), _content(content),
34 _align(align), _contentType(type), _scope(scope), _merge(merge),
35 _thumb(thumb), _noDeadStrip(noDeadStrip) {}
37 // Constructor for zero-fill content
38 MachODefinedAtom(const File &f, const StringRef name, Scope scope,
39 ContentType type, uint64_t size, bool noDeadStrip,
41 : SimpleDefinedAtom(f), _name(name),
42 _content(ArrayRef<uint8_t>(nullptr, size)), _align(align),
43 _contentType(type), _scope(scope), _merge(mergeNo), _thumb(false),
44 _noDeadStrip(noDeadStrip) {}
46 ~MachODefinedAtom() override = default;
48 uint64_t size() const override { return _content.size(); }
50 ContentType contentType() const override { return _contentType; }
52 Alignment alignment() const override { return _align; }
54 StringRef name() const override { return _name; }
56 Scope scope() const override { return _scope; }
58 Merge merge() const override { return _merge; }
60 DeadStripKind deadStrip() const override {
61 if (_contentType == DefinedAtom::typeInitializerPtr)
62 return deadStripNever;
63 if (_contentType == DefinedAtom::typeTerminatorPtr)
64 return deadStripNever;
66 return deadStripNever;
67 return deadStripNormal;
70 ArrayRef<uint8_t> rawContent() const override {
71 // Note: Zerofill atoms have a content pointer which is null.
75 bool isThumb() const { return _thumb; }
78 const StringRef _name;
79 const ArrayRef<uint8_t> _content;
80 const DefinedAtom::Alignment _align;
81 const ContentType _contentType;
85 const bool _noDeadStrip;
88 class MachODefinedCustomSectionAtom : public MachODefinedAtom {
90 MachODefinedCustomSectionAtom(const File &f, const StringRef name,
91 Scope scope, ContentType type, Merge merge,
92 bool thumb, bool noDeadStrip,
93 const ArrayRef<uint8_t> content,
94 StringRef sectionName, Alignment align)
95 : MachODefinedAtom(f, name, scope, type, merge, thumb, noDeadStrip,
97 _sectionName(sectionName) {}
99 ~MachODefinedCustomSectionAtom() override = default;
101 SectionChoice sectionChoice() const override {
102 return DefinedAtom::sectionCustomRequired;
105 StringRef customSectionName() const override {
109 StringRef _sectionName;
112 class MachOTentativeDefAtom : public SimpleDefinedAtom {
114 MachOTentativeDefAtom(const File &f, const StringRef name, Scope scope,
115 uint64_t size, DefinedAtom::Alignment align)
116 : SimpleDefinedAtom(f), _name(name), _scope(scope), _size(size),
119 ~MachOTentativeDefAtom() override = default;
121 uint64_t size() const override { return _size; }
123 Merge merge() const override { return DefinedAtom::mergeAsTentative; }
125 ContentType contentType() const override { return DefinedAtom::typeZeroFill; }
127 Alignment alignment() const override { return _align; }
129 StringRef name() const override { return _name; }
131 Scope scope() const override { return _scope; }
133 ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }
136 const std::string _name;
138 const uint64_t _size;
139 const DefinedAtom::Alignment _align;
142 class MachOSharedLibraryAtom : public SharedLibraryAtom {
144 MachOSharedLibraryAtom(const File &file, StringRef name,
145 StringRef dylibInstallName, bool weakDef)
146 : SharedLibraryAtom(), _file(file), _name(name),
147 _dylibInstallName(dylibInstallName) {}
148 ~MachOSharedLibraryAtom() override = default;
150 StringRef loadName() const override { return _dylibInstallName; }
152 bool canBeNullAtRuntime() const override {
153 // FIXME: this may actually be changeable. For now, all symbols are strongly
158 const File &file() const override { return _file; }
160 StringRef name() const override { return _name; }
162 Type type() const override {
163 // Unused in MachO (I think).
164 return Type::Unknown;
167 uint64_t size() const override {
168 // Unused in MachO (I think)
175 StringRef _dylibInstallName;
178 } // end namespace mach_o
179 } // end namespace lld
181 #endif // LLD_READER_WRITER_MACHO_ATOMS_H