Eugene Zelenko | 20a3845 | 2016-04-29 19:47:09 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/Atoms.h ---------------------------*- C++ -*-===// |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLD_READER_WRITER_MACHO_ATOMS_H |
| 11 | #define LLD_READER_WRITER_MACHO_ATOMS_H |
| 12 | |
Eugene Zelenko | 20a3845 | 2016-04-29 19:47:09 +0000 | [diff] [blame] | 13 | #include "lld/Core/Atom.h" |
| 14 | #include "lld/Core/DefinedAtom.h" |
| 15 | #include "lld/Core/SharedLibraryAtom.h" |
Rui Ueyama | e05d380 | 2014-06-11 21:47:51 +0000 | [diff] [blame] | 16 | #include "lld/Core/Simple.h" |
Eugene Zelenko | 20a3845 | 2016-04-29 19:47:09 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include <cstdint> |
| 20 | #include <string> |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 21 | |
| 22 | namespace lld { |
Eugene Zelenko | 20a3845 | 2016-04-29 19:47:09 +0000 | [diff] [blame] | 23 | |
| 24 | class File; |
| 25 | |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 26 | namespace mach_o { |
Eugene Zelenko | 20a3845 | 2016-04-29 19:47:09 +0000 | [diff] [blame] | 27 | |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 28 | class MachODefinedAtom : public SimpleDefinedAtom { |
| 29 | public: |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 30 | MachODefinedAtom(const File &f, const StringRef name, Scope scope, |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 31 | ContentType type, Merge merge, bool thumb, bool noDeadStrip, |
Nick Kledzik | 63adb08 | 2014-11-18 00:30:25 +0000 | [diff] [blame] | 32 | const ArrayRef<uint8_t> content, Alignment align) |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 33 | : SimpleDefinedAtom(f), _name(name), _content(content), |
Nick Kledzik | 63adb08 | 2014-11-18 00:30:25 +0000 | [diff] [blame] | 34 | _align(align), _contentType(type), _scope(scope), _merge(merge), |
| 35 | _thumb(thumb), _noDeadStrip(noDeadStrip) {} |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 36 | |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 37 | // Constructor for zero-fill content |
| 38 | MachODefinedAtom(const File &f, const StringRef name, Scope scope, |
Lang Hames | ac2adce | 2015-12-11 23:25:09 +0000 | [diff] [blame] | 39 | ContentType type, uint64_t size, bool noDeadStrip, |
| 40 | Alignment align) |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 41 | : SimpleDefinedAtom(f), _name(name), |
Nick Kledzik | 63adb08 | 2014-11-18 00:30:25 +0000 | [diff] [blame] | 42 | _content(ArrayRef<uint8_t>(nullptr, size)), _align(align), |
Lang Hames | ac2adce | 2015-12-11 23:25:09 +0000 | [diff] [blame] | 43 | _contentType(type), _scope(scope), _merge(mergeNo), _thumb(false), |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 44 | _noDeadStrip(noDeadStrip) {} |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 45 | |
Pete Cooper | 8ad55fb | 2016-03-22 17:15:50 +0000 | [diff] [blame] | 46 | ~MachODefinedAtom() override = default; |
| 47 | |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 48 | uint64_t size() const override { return _content.size(); } |
| 49 | |
| 50 | ContentType contentType() const override { return _contentType; } |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 51 | |
Nick Kledzik | 63adb08 | 2014-11-18 00:30:25 +0000 | [diff] [blame] | 52 | Alignment alignment() const override { return _align; } |
| 53 | |
Rui Ueyama | bc69bce | 2014-03-28 21:36:33 +0000 | [diff] [blame] | 54 | StringRef name() const override { return _name; } |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 55 | |
Rui Ueyama | bc69bce | 2014-03-28 21:36:33 +0000 | [diff] [blame] | 56 | Scope scope() const override { return _scope; } |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 57 | |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 58 | Merge merge() const override { return _merge; } |
| 59 | |
Nick Kledzik | a4a08d3 | 2014-05-27 23:20:52 +0000 | [diff] [blame] | 60 | DeadStripKind deadStrip() const override { |
| 61 | if (_contentType == DefinedAtom::typeInitializerPtr) |
| 62 | return deadStripNever; |
| 63 | if (_contentType == DefinedAtom::typeTerminatorPtr) |
| 64 | return deadStripNever; |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 65 | if (_noDeadStrip) |
| 66 | return deadStripNever; |
Nick Kledzik | a4a08d3 | 2014-05-27 23:20:52 +0000 | [diff] [blame] | 67 | return deadStripNormal; |
| 68 | } |
| 69 | |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 70 | ArrayRef<uint8_t> rawContent() const override { |
Nick Kledzik | 3f69076 | 2014-06-27 18:25:01 +0000 | [diff] [blame] | 71 | // Note: Zerofill atoms have a content pointer which is null. |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 72 | return _content; |
| 73 | } |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 74 | |
Nick Kledzik | 4576c11 | 2014-07-04 00:11:09 +0000 | [diff] [blame] | 75 | bool isThumb() const { return _thumb; } |
| 76 | |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 77 | private: |
| 78 | const StringRef _name; |
| 79 | const ArrayRef<uint8_t> _content; |
Nick Kledzik | 63adb08 | 2014-11-18 00:30:25 +0000 | [diff] [blame] | 80 | const DefinedAtom::Alignment _align; |
Nick Kledzik | e09cfc5 | 2014-05-15 23:03:50 +0000 | [diff] [blame] | 81 | const ContentType _contentType; |
Joey Gouly | d221537 | 2014-01-13 22:28:02 +0000 | [diff] [blame] | 82 | const Scope _scope; |
Nick Kledzik | 388f3d0 | 2014-05-28 01:16:35 +0000 | [diff] [blame] | 83 | const Merge _merge; |
Nick Kledzik | 4576c11 | 2014-07-04 00:11:09 +0000 | [diff] [blame] | 84 | const bool _thumb; |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 85 | const bool _noDeadStrip; |
Joey Gouly | ceb16de | 2014-01-03 23:12:02 +0000 | [diff] [blame] | 86 | }; |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 87 | |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 88 | class MachODefinedCustomSectionAtom : public MachODefinedAtom { |
| 89 | public: |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 90 | MachODefinedCustomSectionAtom(const File &f, const StringRef name, |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 91 | Scope scope, ContentType type, Merge merge, |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 92 | bool thumb, bool noDeadStrip, |
| 93 | const ArrayRef<uint8_t> content, |
Nick Kledzik | 63adb08 | 2014-11-18 00:30:25 +0000 | [diff] [blame] | 94 | StringRef sectionName, Alignment align) |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 95 | : MachODefinedAtom(f, name, scope, type, merge, thumb, noDeadStrip, |
Nick Kledzik | 63adb08 | 2014-11-18 00:30:25 +0000 | [diff] [blame] | 96 | content, align), |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 97 | _sectionName(sectionName) {} |
| 98 | |
Pete Cooper | 8ad55fb | 2016-03-22 17:15:50 +0000 | [diff] [blame] | 99 | ~MachODefinedCustomSectionAtom() override = default; |
| 100 | |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 101 | SectionChoice sectionChoice() const override { |
| 102 | return DefinedAtom::sectionCustomRequired; |
| 103 | } |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 104 | |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 105 | StringRef customSectionName() const override { |
| 106 | return _sectionName; |
| 107 | } |
Nick Kledzik | 7820c80 | 2014-08-21 22:18:30 +0000 | [diff] [blame] | 108 | private: |
Nick Kledzik | acfad80 | 2014-05-30 22:51:04 +0000 | [diff] [blame] | 109 | StringRef _sectionName; |
| 110 | }; |
| 111 | |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 112 | class MachOTentativeDefAtom : public SimpleDefinedAtom { |
| 113 | public: |
| 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), |
| 117 | _align(align) {} |
| 118 | |
Pete Cooper | 8ad55fb | 2016-03-22 17:15:50 +0000 | [diff] [blame] | 119 | ~MachOTentativeDefAtom() override = default; |
| 120 | |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 121 | uint64_t size() const override { return _size; } |
| 122 | |
| 123 | Merge merge() const override { return DefinedAtom::mergeAsTentative; } |
| 124 | |
| 125 | ContentType contentType() const override { return DefinedAtom::typeZeroFill; } |
| 126 | |
| 127 | Alignment alignment() const override { return _align; } |
| 128 | |
| 129 | StringRef name() const override { return _name; } |
| 130 | |
| 131 | Scope scope() const override { return _scope; } |
| 132 | |
| 133 | ArrayRef<uint8_t> rawContent() const override { return ArrayRef<uint8_t>(); } |
| 134 | |
| 135 | private: |
Lang Hames | ac416d42 | 2015-10-29 03:57:31 +0000 | [diff] [blame] | 136 | const std::string _name; |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 137 | const Scope _scope; |
| 138 | const uint64_t _size; |
| 139 | const DefinedAtom::Alignment _align; |
| 140 | }; |
| 141 | |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 142 | class MachOSharedLibraryAtom : public SharedLibraryAtom { |
| 143 | public: |
| 144 | MachOSharedLibraryAtom(const File &file, StringRef name, |
Nick Kledzik | 8fc67fb | 2014-08-13 23:55:41 +0000 | [diff] [blame] | 145 | StringRef dylibInstallName, bool weakDef) |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 146 | : SharedLibraryAtom(), _file(file), _name(name), |
| 147 | _dylibInstallName(dylibInstallName) {} |
Rui Ueyama | 9071e4a | 2015-09-10 18:51:36 +0000 | [diff] [blame] | 148 | ~MachOSharedLibraryAtom() override = default; |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 149 | |
David Blaikie | 71df405 | 2015-04-08 17:08:27 +0000 | [diff] [blame] | 150 | StringRef loadName() const override { return _dylibInstallName; } |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 151 | |
David Blaikie | 71df405 | 2015-04-08 17:08:27 +0000 | [diff] [blame] | 152 | bool canBeNullAtRuntime() const override { |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 153 | // FIXME: this may actually be changeable. For now, all symbols are strongly |
| 154 | // defined though. |
| 155 | return false; |
| 156 | } |
| 157 | |
David Blaikie | 71df405 | 2015-04-08 17:08:27 +0000 | [diff] [blame] | 158 | const File &file() const override { return _file; } |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 159 | |
David Blaikie | 71df405 | 2015-04-08 17:08:27 +0000 | [diff] [blame] | 160 | StringRef name() const override { return _name; } |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 161 | |
David Blaikie | 71df405 | 2015-04-08 17:08:27 +0000 | [diff] [blame] | 162 | Type type() const override { |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 163 | // Unused in MachO (I think). |
| 164 | return Type::Unknown; |
| 165 | } |
| 166 | |
David Blaikie | 71df405 | 2015-04-08 17:08:27 +0000 | [diff] [blame] | 167 | uint64_t size() const override { |
Tim Northover | f9b13d6 | 2014-06-30 09:11:38 +0000 | [diff] [blame] | 168 | // Unused in MachO (I think) |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | private: |
| 173 | const File &_file; |
| 174 | StringRef _name; |
| 175 | StringRef _dylibInstallName; |
| 176 | }; |
| 177 | |
Eugene Zelenko | 20a3845 | 2016-04-29 19:47:09 +0000 | [diff] [blame] | 178 | } // end namespace mach_o |
| 179 | } // end namespace lld |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 180 | |
Rui Ueyama | 9071e4a | 2015-09-10 18:51:36 +0000 | [diff] [blame] | 181 | #endif // LLD_READER_WRITER_MACHO_ATOMS_H |