1 //===- lib/ReaderWriter/MachO/Atoms.h ---------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLD_READER_WRITER_MACHO_ATOMS_H
10 #define LLD_READER_WRITER_MACHO_ATOMS_H
12 #include "lld/Core/Atom.h"
13 #include "lld/Core/DefinedAtom.h"
14 #include "lld/Core/SharedLibraryAtom.h"
15 #include "lld/Core/Simple.h"
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringRef.h"
27 class MachODefinedAtom : public SimpleDefinedAtom {
29 MachODefinedAtom(const File &f, const StringRef name, Scope scope,
30 ContentType type, Merge merge, bool thumb, bool noDeadStrip,
31 const ArrayRef<uint8_t> content, Alignment align)
32 : SimpleDefinedAtom(f), _name(name), _content(content),
33 _align(align), _contentType(type), _scope(scope), _merge(merge),
34 _thumb(thumb), _noDeadStrip(noDeadStrip) {}
36 // Constructor for zero-fill content
37 MachODefinedAtom(const File &f, const StringRef name, Scope scope,
38 ContentType type, uint64_t size, bool noDeadStrip,
40 : SimpleDefinedAtom(f), _name(name),
41 _content(ArrayRef<uint8_t>(nullptr, size)), _align(align),
42 _contentType(type), _scope(scope), _merge(mergeNo), _thumb(false),
43 _noDeadStrip(noDeadStrip) {}
45 ~MachODefinedAtom() override = default;
47 uint64_t size() const override { return _content.size(); }
49 ContentType contentType() const override { return _contentType; }
51 Alignment alignment() const override { return _align; }
53 StringRef name() const override { return _name; }
55 Scope scope() const override { return _scope; }
57 Merge merge() const override { return _merge; }
59 DeadStripKind deadStrip() const override {
60 if (_contentType == DefinedAtom::typeInitializerPtr)
61 return deadStripNever;
62 if (_contentType == DefinedAtom::typeTerminatorPtr)
63 return deadStripNever;
65 return deadStripNever;
66 return deadStripNormal;
69 ArrayRef<uint8_t> rawContent() const override {
70 // Note: Zerofill atoms have a content pointer which is null.
74 bool isThumb() const { return _thumb; }
77 const StringRef _name;
78 const ArrayRef<uint8_t> _content;
79 const DefinedAtom::Alignment _align;
80 const ContentType _contentType;
84 const bool _noDeadStrip;
87 class MachODefinedCustomSectionAtom : public MachODefinedAtom {
89 MachODefinedCustomSectionAtom(const File &f, const StringRef name,
90 Scope scope, ContentType type, Merge merge,
91 bool thumb, bool noDeadStrip,
92 const ArrayRef<uint8_t> content,
93 StringRef sectionName, Alignment align)
94 : MachODefinedAtom(f, name, scope, type, merge, thumb, noDeadStrip,
96 _sectionName(sectionName) {}
98 ~MachODefinedCustomSectionAtom() override = default;
100 SectionChoice sectionChoice() const override {
101 return DefinedAtom::sectionCustomRequired;
104 StringRef customSectionName() const override {
108 StringRef _sectionName;
111 class MachOTentativeDefAtom : public SimpleDefinedAtom {
113 MachOTentativeDefAtom(const File &f, const StringRef name, Scope scope,
114 uint64_t size, DefinedAtom::Alignment align)
115 : SimpleDefinedAtom(f), _name(name), _scope(scope), _size(size),
118 ~MachOTentativeDefAtom() override = default;
120 uint64_t size() const override { return _size; }
122 Merge merge() const override { return DefinedAtom::mergeAsTentative; }
124 ContentType contentType() const override { return DefinedAtom::typeZeroFill; }
126 Alignment alignment() const override { return _align; }
128 StringRef name() const override { return _name; }
130 Scope scope() const override { return _scope; }
132 ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); }
135 const std::string _name;
137 const uint64_t _size;
138 const DefinedAtom::Alignment _align;
141 class MachOSharedLibraryAtom : public SharedLibraryAtom {
143 MachOSharedLibraryAtom(const File &file, StringRef name,
144 StringRef dylibInstallName, bool weakDef)
145 : SharedLibraryAtom(), _file(file), _name(name),
146 _dylibInstallName(dylibInstallName) {}
147 ~MachOSharedLibraryAtom() override = default;
149 StringRef loadName() const override { return _dylibInstallName; }
151 bool canBeNullAtRuntime() const override {
152 // FIXME: this may actually be changeable. For now, all symbols are strongly
157 const File &file() const override { return _file; }
159 StringRef name() const override { return _name; }
161 Type type() const override {
162 // Unused in MachO (I think).
163 return Type::Unknown;
166 uint64_t size() const override {
167 // Unused in MachO (I think)
174 StringRef _dylibInstallName;
177 } // end namespace mach_o
178 } // end namespace lld
180 #endif // LLD_READER_WRITER_MACHO_ATOMS_H