Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/Native/ReaderNative.cpp ---------------------------===// |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +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 | |
Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 10 | #include "lld/ReaderWriter/Reader.h" |
Rui Ueyama | e5416ec | 2013-09-12 19:14:05 +0000 | [diff] [blame] | 11 | #include "lld/ReaderWriter/Simple.h" |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 12 | |
Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame] | 13 | #include "lld/Core/Atom.h" |
| 14 | #include "lld/Core/Error.h" |
| 15 | #include "lld/Core/File.h" |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 16 | |
| 17 | #include "llvm/ADT/ArrayRef.h" |
| 18 | #include "llvm/ADT/OwningPtr.h" |
| 19 | #include "llvm/ADT/StringRef.h" |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Format.h" |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | |
| 26 | #include "NativeFileFormat.h" |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 27 | |
Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame] | 28 | #include <vector> |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 29 | #include <memory> |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 30 | |
| 31 | namespace lld { |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 32 | namespace native { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 33 | |
| 34 | // forward reference |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 35 | class File; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 36 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 37 | // |
| 38 | // An object of this class is instantied for each NativeDefinedAtomIvarsV1 |
| 39 | // struct in the NCS_DefinedAtomsV1 chunk. |
| 40 | // |
| 41 | class NativeDefinedAtomV1 : public DefinedAtom { |
| 42 | public: |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 43 | NativeDefinedAtomV1(const File& f, |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 44 | const NativeDefinedAtomIvarsV1* ivarData) |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 45 | : _file(&f), _ivarData(ivarData) { } |
| 46 | |
Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 47 | virtual const lld::File& file() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 48 | |
| 49 | virtual uint64_t ordinal() const; |
| 50 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 51 | virtual StringRef name() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 52 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 53 | virtual uint64_t size() const { |
| 54 | return _ivarData->contentSize; |
| 55 | } |
| 56 | |
| 57 | virtual DefinedAtom::Scope scope() const { |
| 58 | return (DefinedAtom::Scope)(attributes().scope); |
| 59 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 60 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 61 | virtual DefinedAtom::Interposable interposable() const { |
| 62 | return (DefinedAtom::Interposable)(attributes().interposable); |
| 63 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 64 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 65 | virtual DefinedAtom::Merge merge() const { |
| 66 | return (DefinedAtom::Merge)(attributes().merge); |
| 67 | } |
| 68 | |
| 69 | virtual DefinedAtom::ContentType contentType() const { |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 70 | const NativeAtomAttributesV1& attr = attributes(); |
| 71 | return (DefinedAtom::ContentType)(attr.contentType); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 72 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 73 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 74 | virtual DefinedAtom::Alignment alignment() const { |
| 75 | return DefinedAtom::Alignment(attributes().align2, attributes().alignModulus); |
| 76 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 77 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 78 | virtual DefinedAtom::SectionChoice sectionChoice() const { |
Nick Kledzik | 36293f6 | 2013-01-23 22:32:56 +0000 | [diff] [blame] | 79 | return (DefinedAtom::SectionChoice)( |
| 80 | attributes().sectionChoiceAndPosition >> 4); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 83 | virtual StringRef customSectionName() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 84 | |
Nick Kledzik | 36293f6 | 2013-01-23 22:32:56 +0000 | [diff] [blame] | 85 | virtual SectionPosition sectionPosition() const { |
| 86 | return (DefinedAtom::SectionPosition)( |
| 87 | attributes().sectionChoiceAndPosition & 0xF); |
| 88 | } |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 89 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 90 | virtual DefinedAtom::DeadStripKind deadStrip() const { |
| 91 | return (DefinedAtom::DeadStripKind)(attributes().deadStrip); |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 92 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 93 | |
Michael J. Spencer | b8ab9f5 | 2013-11-08 21:04:20 +0000 | [diff] [blame] | 94 | virtual DynamicExport dynamicExport() const { |
| 95 | return (DynamicExport)attributes().dynamicExport; |
| 96 | } |
| 97 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 98 | virtual DefinedAtom::ContentPermissions permissions() const { |
| 99 | return (DefinedAtom::ContentPermissions)(attributes().permissions); |
| 100 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 101 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 102 | virtual bool isAlias() const { |
| 103 | return (attributes().alias != 0); |
| 104 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 105 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 106 | virtual ArrayRef<uint8_t> rawContent() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 107 | |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 108 | virtual reference_iterator begin() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 109 | |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 110 | virtual reference_iterator end() const; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 111 | |
| 112 | virtual const Reference* derefIterator(const void*) const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 113 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 114 | virtual void incrementIterator(const void*& it) const; |
| 115 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 116 | private: |
| 117 | const NativeAtomAttributesV1& attributes() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 118 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 119 | const File *_file; |
| 120 | const NativeDefinedAtomIvarsV1 *_ivarData; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | |
| 124 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 125 | // |
| 126 | // An object of this class is instantied for each NativeUndefinedAtomIvarsV1 |
| 127 | // struct in the NCS_UndefinedAtomsV1 chunk. |
| 128 | // |
| 129 | class NativeUndefinedAtomV1 : public UndefinedAtom { |
| 130 | public: |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 131 | NativeUndefinedAtomV1(const File& f, |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 132 | const NativeUndefinedAtomIvarsV1* ivarData) |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 133 | : _file(&f), _ivarData(ivarData) { } |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 134 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 135 | virtual const lld::File& file() const; |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 136 | virtual StringRef name() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 137 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 138 | virtual CanBeNull canBeNull() const { |
| 139 | return (CanBeNull)(_ivarData->flags & 0x3); |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 140 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 141 | |
Rui Ueyama | e5416ec | 2013-09-12 19:14:05 +0000 | [diff] [blame] | 142 | virtual const UndefinedAtom *fallback() const; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 143 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 144 | private: |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 145 | const File *_file; |
| 146 | const NativeUndefinedAtomIvarsV1 *_ivarData; |
Rui Ueyama | e5416ec | 2013-09-12 19:14:05 +0000 | [diff] [blame] | 147 | mutable std::unique_ptr<const SimpleUndefinedAtom> _fallback; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 151 | // |
| 152 | // An object of this class is instantied for each NativeUndefinedAtomIvarsV1 |
| 153 | // struct in the NCS_SharedLibraryAtomsV1 chunk. |
| 154 | // |
| 155 | class NativeSharedLibraryAtomV1 : public SharedLibraryAtom { |
| 156 | public: |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 157 | NativeSharedLibraryAtomV1(const File& f, |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 158 | const NativeSharedLibraryAtomIvarsV1* ivarData) |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 159 | : _file(&f), _ivarData(ivarData) { } |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 160 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 161 | virtual const lld::File& file() const; |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 162 | virtual StringRef name() const; |
| 163 | virtual StringRef loadName() const; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 164 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 165 | virtual bool canBeNullAtRuntime() const { |
| 166 | return (_ivarData->flags & 0x1); |
| 167 | } |
| 168 | |
Michael J. Spencer | 4355bb9 | 2013-09-26 22:08:43 +0000 | [diff] [blame] | 169 | virtual Type type() const { |
| 170 | return (Type)_ivarData->type; |
| 171 | } |
| 172 | |
| 173 | virtual uint64_t size() const { |
| 174 | return _ivarData->size; |
| 175 | } |
| 176 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 177 | private: |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 178 | const File *_file; |
| 179 | const NativeSharedLibraryAtomIvarsV1 *_ivarData; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | |
| 183 | // |
| 184 | // An object of this class is instantied for each NativeAbsoluteAtomIvarsV1 |
| 185 | // struct in the NCS_AbsoluteAtomsV1 chunk. |
| 186 | // |
| 187 | class NativeAbsoluteAtomV1 : public AbsoluteAtom { |
| 188 | public: |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 189 | NativeAbsoluteAtomV1(const File& f, |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 190 | const NativeAbsoluteAtomIvarsV1* ivarData) |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 191 | : _file(&f), _ivarData(ivarData) { } |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 192 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 193 | virtual const lld::File& file() const; |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 194 | virtual StringRef name() const; |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 195 | virtual Scope scope() const { |
| 196 | const NativeAtomAttributesV1& attr = absAttributes(); |
| 197 | return (Scope)(attr.scope); |
| 198 | } |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 199 | virtual uint64_t value() const { |
| 200 | return _ivarData->value; |
| 201 | } |
| 202 | |
| 203 | private: |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 204 | const NativeAtomAttributesV1& absAttributes() const; |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 205 | const File *_file; |
| 206 | const NativeAbsoluteAtomIvarsV1 *_ivarData; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 210 | // |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 211 | // An object of this class is instantied for each NativeReferenceIvarsV1 |
| 212 | // struct in the NCS_ReferencesArrayV1 chunk. |
| 213 | // |
| 214 | class NativeReferenceV1 : public Reference { |
| 215 | public: |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 216 | NativeReferenceV1(const File &f, const NativeReferenceIvarsV1 *ivarData) |
| 217 | : Reference((KindNamespace)ivarData->kindNamespace, |
| 218 | (KindArch)ivarData->kindArch, ivarData->kindValue), |
| 219 | _file(&f), _ivarData(ivarData) {} |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 220 | |
| 221 | virtual uint64_t offsetInAtom() const { |
| 222 | return _ivarData->offsetInAtom; |
| 223 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 224 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 225 | virtual const Atom* target() const; |
| 226 | virtual Addend addend() const; |
| 227 | virtual void setTarget(const Atom* newAtom); |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 228 | virtual void setAddend(Addend a); |
Nick Kledzik | f4e2c73 | 2012-03-15 23:36:24 +0000 | [diff] [blame] | 229 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 230 | private: |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 231 | const File *_file; |
| 232 | const NativeReferenceIvarsV1 *_ivarData; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 236 | // |
| 237 | // An object of this class is instantied for each NativeReferenceIvarsV1 |
| 238 | // struct in the NCS_ReferencesArrayV1 chunk. |
| 239 | // |
| 240 | class NativeReferenceV2 : public Reference { |
| 241 | public: |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 242 | NativeReferenceV2(const File &f, const NativeReferenceIvarsV2 *ivarData) |
| 243 | : Reference((KindNamespace)ivarData->kindNamespace, |
| 244 | (KindArch)ivarData->kindArch, ivarData->kindValue), |
| 245 | _file(&f), _ivarData(ivarData) {} |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 246 | |
| 247 | virtual uint64_t offsetInAtom() const { |
| 248 | return _ivarData->offsetInAtom; |
| 249 | } |
| 250 | |
| 251 | virtual const Atom* target() const; |
| 252 | virtual Addend addend() const; |
| 253 | virtual void setTarget(const Atom* newAtom); |
| 254 | virtual void setAddend(Addend a); |
| 255 | |
| 256 | private: |
| 257 | const File *_file; |
| 258 | const NativeReferenceIvarsV2 *_ivarData; |
| 259 | }; |
| 260 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 261 | |
| 262 | // |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 263 | // lld::File object for native llvm object file |
| 264 | // |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 265 | class File : public lld::File { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 266 | public: |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 267 | |
| 268 | /// Instantiates a File object from a native object file. Ownership |
Alp Toker | 32e8bef | 2013-12-01 23:51:36 +0000 | [diff] [blame] | 269 | /// of the MemoryBuffer is transferred to the resulting File object. |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 270 | static error_code make(std::unique_ptr<MemoryBuffer> mb, |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 271 | std::vector<std::unique_ptr<lld::File>> &result) { |
Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 272 | const uint8_t *const base = |
Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 273 | reinterpret_cast<const uint8_t *>(mb->getBufferStart()); |
| 274 | StringRef path(mb->getBufferIdentifier()); |
| 275 | const NativeFileHeader *const header = |
| 276 | reinterpret_cast<const NativeFileHeader *>(base); |
Michael J. Spencer | b2bd733 | 2012-01-31 21:45:53 +0000 | [diff] [blame] | 277 | const NativeChunk *const chunks = |
Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 278 | reinterpret_cast<const NativeChunk *>(base + sizeof(NativeFileHeader)); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 279 | // make sure magic matches |
Rui Ueyama | a3ada6b | 2013-11-16 01:31:24 +0000 | [diff] [blame] | 280 | if (memcmp(header->magic, NATIVE_FILE_HEADER_MAGIC, |
| 281 | sizeof(header->magic)) != 0) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 282 | return make_error_code(NativeReaderError::unknown_file_format); |
Michael J. Spencer | 7aba895 | 2012-01-31 21:47:13 +0000 | [diff] [blame] | 283 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 284 | // make sure mapped file contains all needed data |
Shankar Easwaran | a96f3a3 | 2013-10-07 02:47:09 +0000 | [diff] [blame] | 285 | const size_t fileSize = mb->getBufferSize(); |
| 286 | if (header->fileSize > fileSize) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 287 | return make_error_code(NativeReaderError::file_too_short); |
Michael J. Spencer | 7aba895 | 2012-01-31 21:47:13 +0000 | [diff] [blame] | 288 | |
Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 289 | DEBUG_WITH_TYPE("ReaderNative", |
| 290 | llvm::dbgs() << " Native File Header:" << " fileSize=" |
| 291 | << header->fileSize << " chunkCount=" |
| 292 | << header->chunkCount << "\n"); |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 293 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 294 | // instantiate NativeFile object and add values to it as found |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 295 | std::unique_ptr<File> file(new File(std::move(mb), path)); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 296 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 297 | // process each chunk |
Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 298 | for (uint32_t i = 0; i < header->chunkCount; ++i) { |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 299 | error_code ec; |
Michael J. Spencer | b2bd733 | 2012-01-31 21:45:53 +0000 | [diff] [blame] | 300 | const NativeChunk* chunk = &chunks[i]; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 301 | // sanity check chunk is within file |
Michael J. Spencer | 7aba895 | 2012-01-31 21:47:13 +0000 | [diff] [blame] | 302 | if ( chunk->fileOffset > fileSize ) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 303 | return make_error_code(NativeReaderError::file_malformed); |
Michael J. Spencer | 7aba895 | 2012-01-31 21:47:13 +0000 | [diff] [blame] | 304 | if ( (chunk->fileOffset + chunk->fileSize) > fileSize) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 305 | return make_error_code(NativeReaderError::file_malformed); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 306 | // process chunk, based on signature |
| 307 | switch ( chunk->signature ) { |
| 308 | case NCS_DefinedAtomsV1: |
| 309 | ec = file->processDefinedAtomsV1(base, chunk); |
| 310 | break; |
| 311 | case NCS_AttributesArrayV1: |
| 312 | ec = file->processAttributesV1(base, chunk); |
| 313 | break; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 314 | case NCS_UndefinedAtomsV1: |
| 315 | ec = file->processUndefinedAtomsV1(base, chunk); |
| 316 | break; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 317 | case NCS_SharedLibraryAtomsV1: |
| 318 | ec = file->processSharedLibraryAtomsV1(base, chunk); |
| 319 | break; |
| 320 | case NCS_AbsoluteAtomsV1: |
| 321 | ec = file->processAbsoluteAtomsV1(base, chunk); |
| 322 | break; |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 323 | case NCS_AbsoluteAttributesV1: |
| 324 | ec = file->processAbsoluteAttributesV1(base, chunk); |
| 325 | break; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 326 | case NCS_ReferencesArrayV1: |
| 327 | ec = file->processReferencesV1(base, chunk); |
| 328 | break; |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 329 | case NCS_ReferencesArrayV2: |
| 330 | ec = file->processReferencesV2(base, chunk); |
| 331 | break; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 332 | case NCS_TargetsTable: |
| 333 | ec = file->processTargetsTable(base, chunk); |
| 334 | break; |
| 335 | case NCS_AddendsTable: |
| 336 | ec = file->processAddendsTable(base, chunk); |
| 337 | break; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 338 | case NCS_Content: |
| 339 | ec = file->processContent(base, chunk); |
| 340 | break; |
| 341 | case NCS_Strings: |
| 342 | ec = file->processStrings(base, chunk); |
| 343 | break; |
| 344 | default: |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 345 | return make_error_code(NativeReaderError::unknown_chunk_type); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 346 | } |
| 347 | if ( ec ) { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 348 | return ec; |
| 349 | } |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 350 | } |
| 351 | // TO DO: validate enough chunks were used |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 352 | |
Rui Ueyama | b37c431 | 2013-11-16 01:41:47 +0000 | [diff] [blame] | 353 | DEBUG_WITH_TYPE("ReaderNative", { |
| 354 | llvm::dbgs() << " ReaderNative DefinedAtoms:\n"; |
| 355 | for (const DefinedAtom *a : file->defined()) { |
| 356 | llvm::dbgs() << llvm::format(" 0x%09lX", a) |
| 357 | << ", name=" << a->name() |
| 358 | << ", size=" << a->size() << "\n"; |
| 359 | for (const Reference *r : *a) { |
| 360 | llvm::dbgs() << " offset=" |
| 361 | << llvm::format("0x%03X", r->offsetInAtom()) |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 362 | << ", kind=" << r->kindValue() |
Rui Ueyama | b37c431 | 2013-11-16 01:41:47 +0000 | [diff] [blame] | 363 | << ", target=" << r->target() << "\n"; |
| 364 | } |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 365 | } |
Rui Ueyama | b37c431 | 2013-11-16 01:41:47 +0000 | [diff] [blame] | 366 | }); |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 367 | result.push_back(std::move(file)); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 368 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 369 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 370 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 371 | virtual ~File() { |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 372 | // _buffer is automatically deleted because of OwningPtr<> |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 373 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 374 | // All other ivar pointers are pointers into the MemoryBuffer, except |
| 375 | // the _definedAtoms array which was allocated to contain an array |
| 376 | // of Atom objects. The atoms have empty destructors, so it is ok |
| 377 | // to just delete the memory. |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 378 | delete _definedAtoms._arrayStart; |
| 379 | delete _undefinedAtoms._arrayStart; |
| 380 | delete _sharedLibraryAtoms._arrayStart; |
| 381 | delete _absoluteAtoms._arrayStart; |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 382 | delete _referencesV1.arrayStart; |
| 383 | delete _referencesV2.arrayStart; |
Michael J. Spencer | 20231f1 | 2013-01-26 12:26:56 +0000 | [diff] [blame] | 384 | delete [] _targetsTable; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 385 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 386 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 387 | virtual const atom_collection<DefinedAtom>& defined() const { |
| 388 | return _definedAtoms; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 389 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 390 | virtual const atom_collection<UndefinedAtom>& undefined() const { |
| 391 | return _undefinedAtoms; |
| 392 | } |
| 393 | virtual const atom_collection<SharedLibraryAtom>& sharedLibrary() const { |
| 394 | return _sharedLibraryAtoms; |
| 395 | } |
Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 396 | virtual const atom_collection<AbsoluteAtom> &absolute() const { |
| 397 | return _absoluteAtoms; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 400 | private: |
Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 401 | friend NativeDefinedAtomV1; |
| 402 | friend NativeUndefinedAtomV1; |
| 403 | friend NativeSharedLibraryAtomV1; |
| 404 | friend NativeAbsoluteAtomV1; |
| 405 | friend NativeReferenceV1; |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 406 | friend NativeReferenceV2; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 407 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 408 | // instantiate array of DefinedAtoms from v1 ivar data in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 409 | error_code processDefinedAtomsV1(const uint8_t *base, |
| 410 | const NativeChunk *chunk) { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 411 | const size_t atomSize = sizeof(NativeDefinedAtomV1); |
| 412 | size_t atomsArraySize = chunk->elementCount * atomSize; |
| 413 | uint8_t* atomsStart = reinterpret_cast<uint8_t*> |
| 414 | (operator new(atomsArraySize, std::nothrow)); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 415 | if (atomsStart == nullptr) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 416 | return make_error_code(NativeReaderError::memory_error); |
Michael J. Spencer | 7aba895 | 2012-01-31 21:47:13 +0000 | [diff] [blame] | 417 | const size_t ivarElementSize = chunk->fileSize |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 418 | / chunk->elementCount; |
| 419 | if ( ivarElementSize != sizeof(NativeDefinedAtomIvarsV1) ) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 420 | return make_error_code(NativeReaderError::file_malformed); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 421 | uint8_t* atomsEnd = atomsStart + atomsArraySize; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 422 | const NativeDefinedAtomIvarsV1* ivarData = |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 423 | reinterpret_cast<const NativeDefinedAtomIvarsV1*> |
| 424 | (base + chunk->fileOffset); |
| 425 | for(uint8_t* s = atomsStart; s != atomsEnd; s += atomSize) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 426 | NativeDefinedAtomV1* atomAllocSpace = |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 427 | reinterpret_cast<NativeDefinedAtomV1*>(s); |
| 428 | new (atomAllocSpace) NativeDefinedAtomV1(*this, ivarData); |
| 429 | ++ivarData; |
| 430 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 431 | this->_definedAtoms._arrayStart = atomsStart; |
| 432 | this->_definedAtoms._arrayEnd = atomsEnd; |
| 433 | this->_definedAtoms._elementSize = atomSize; |
| 434 | this->_definedAtoms._elementCount = chunk->elementCount; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 435 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 436 | << " chunk DefinedAtomsV1: " |
| 437 | << " count=" << chunk->elementCount |
| 438 | << " chunkSize=" << chunk->fileSize |
| 439 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 440 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 441 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 442 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 443 | |
| 444 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 445 | // set up pointers to attributes array |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 446 | error_code processAttributesV1(const uint8_t *base, |
| 447 | const NativeChunk *chunk) { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 448 | this->_attributes = base + chunk->fileOffset; |
| 449 | this->_attributesMaxOffset = chunk->fileSize; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 450 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 451 | << " chunk AttributesV1: " |
| 452 | << " count=" << chunk->elementCount |
| 453 | << " chunkSize=" << chunk->fileSize |
| 454 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 455 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 456 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 457 | |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 458 | // set up pointers to attributes array |
| 459 | error_code processAbsoluteAttributesV1(const uint8_t *base, |
| 460 | const NativeChunk *chunk) { |
| 461 | this->_absAttributes = base + chunk->fileOffset; |
| 462 | this->_absAbsoluteMaxOffset = chunk->fileSize; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 463 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 464 | << " chunk AbsoluteAttributesV1: " |
| 465 | << " count=" << chunk->elementCount |
| 466 | << " chunkSize=" << chunk->fileSize |
| 467 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 468 | return make_error_code(NativeReaderError::success); |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 471 | // instantiate array of UndefinedAtoms from v1 ivar data in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 472 | error_code processUndefinedAtomsV1(const uint8_t *base, |
| 473 | const NativeChunk *chunk) { |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 474 | const size_t atomSize = sizeof(NativeUndefinedAtomV1); |
| 475 | size_t atomsArraySize = chunk->elementCount * atomSize; |
| 476 | uint8_t* atomsStart = reinterpret_cast<uint8_t*> |
| 477 | (operator new(atomsArraySize, std::nothrow)); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 478 | if (atomsStart == nullptr) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 479 | return make_error_code(NativeReaderError::memory_error); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 480 | const size_t ivarElementSize = chunk->fileSize |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 481 | / chunk->elementCount; |
| 482 | if ( ivarElementSize != sizeof(NativeUndefinedAtomIvarsV1) ) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 483 | return make_error_code(NativeReaderError::file_malformed); |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 484 | uint8_t* atomsEnd = atomsStart + atomsArraySize; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 485 | const NativeUndefinedAtomIvarsV1* ivarData = |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 486 | reinterpret_cast<const NativeUndefinedAtomIvarsV1*> |
| 487 | (base + chunk->fileOffset); |
| 488 | for(uint8_t* s = atomsStart; s != atomsEnd; s += atomSize) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 489 | NativeUndefinedAtomV1* atomAllocSpace = |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 490 | reinterpret_cast<NativeUndefinedAtomV1*>(s); |
| 491 | new (atomAllocSpace) NativeUndefinedAtomV1(*this, ivarData); |
| 492 | ++ivarData; |
| 493 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 494 | this->_undefinedAtoms._arrayStart = atomsStart; |
| 495 | this->_undefinedAtoms._arrayEnd = atomsEnd; |
| 496 | this->_undefinedAtoms._elementSize = atomSize; |
| 497 | this->_undefinedAtoms._elementCount = chunk->elementCount; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 498 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 499 | << " chunk UndefinedAtomsV1:" |
| 500 | << " count=" << chunk->elementCount |
| 501 | << " chunkSize=" << chunk->fileSize |
| 502 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 503 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 504 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 505 | |
| 506 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 507 | // instantiate array of ShareLibraryAtoms from v1 ivar data in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 508 | error_code processSharedLibraryAtomsV1(const uint8_t *base, |
| 509 | const NativeChunk *chunk) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 510 | const size_t atomSize = sizeof(NativeSharedLibraryAtomV1); |
| 511 | size_t atomsArraySize = chunk->elementCount * atomSize; |
| 512 | uint8_t* atomsStart = reinterpret_cast<uint8_t*> |
| 513 | (operator new(atomsArraySize, std::nothrow)); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 514 | if (atomsStart == nullptr) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 515 | return make_error_code(NativeReaderError::memory_error); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 516 | const size_t ivarElementSize = chunk->fileSize |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 517 | / chunk->elementCount; |
| 518 | if ( ivarElementSize != sizeof(NativeSharedLibraryAtomIvarsV1) ) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 519 | return make_error_code(NativeReaderError::file_malformed); |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 520 | uint8_t* atomsEnd = atomsStart + atomsArraySize; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 521 | const NativeSharedLibraryAtomIvarsV1* ivarData = |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 522 | reinterpret_cast<const NativeSharedLibraryAtomIvarsV1*> |
| 523 | (base + chunk->fileOffset); |
| 524 | for(uint8_t* s = atomsStart; s != atomsEnd; s += atomSize) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 525 | NativeSharedLibraryAtomV1* atomAllocSpace = |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 526 | reinterpret_cast<NativeSharedLibraryAtomV1*>(s); |
| 527 | new (atomAllocSpace) NativeSharedLibraryAtomV1(*this, ivarData); |
| 528 | ++ivarData; |
| 529 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 530 | this->_sharedLibraryAtoms._arrayStart = atomsStart; |
| 531 | this->_sharedLibraryAtoms._arrayEnd = atomsEnd; |
| 532 | this->_sharedLibraryAtoms._elementSize = atomSize; |
| 533 | this->_sharedLibraryAtoms._elementCount = chunk->elementCount; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 534 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 535 | << " chunk SharedLibraryAtomsV1:" |
| 536 | << " count=" << chunk->elementCount |
| 537 | << " chunkSize=" << chunk->fileSize |
| 538 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 539 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 540 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 541 | |
| 542 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 543 | // instantiate array of AbsoluteAtoms from v1 ivar data in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 544 | error_code processAbsoluteAtomsV1(const uint8_t *base, |
| 545 | const NativeChunk *chunk) { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 546 | const size_t atomSize = sizeof(NativeAbsoluteAtomV1); |
| 547 | size_t atomsArraySize = chunk->elementCount * atomSize; |
| 548 | uint8_t* atomsStart = reinterpret_cast<uint8_t*> |
| 549 | (operator new(atomsArraySize, std::nothrow)); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 550 | if (atomsStart == nullptr) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 551 | return make_error_code(NativeReaderError::memory_error); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 552 | const size_t ivarElementSize = chunk->fileSize |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 553 | / chunk->elementCount; |
| 554 | if ( ivarElementSize != sizeof(NativeAbsoluteAtomIvarsV1) ) |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 555 | return make_error_code(NativeReaderError::file_malformed); |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 556 | uint8_t* atomsEnd = atomsStart + atomsArraySize; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 557 | const NativeAbsoluteAtomIvarsV1* ivarData = |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 558 | reinterpret_cast<const NativeAbsoluteAtomIvarsV1*> |
| 559 | (base + chunk->fileOffset); |
| 560 | for(uint8_t* s = atomsStart; s != atomsEnd; s += atomSize) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 561 | NativeAbsoluteAtomV1* atomAllocSpace = |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 562 | reinterpret_cast<NativeAbsoluteAtomV1*>(s); |
| 563 | new (atomAllocSpace) NativeAbsoluteAtomV1(*this, ivarData); |
| 564 | ++ivarData; |
| 565 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 566 | this->_absoluteAtoms._arrayStart = atomsStart; |
| 567 | this->_absoluteAtoms._arrayEnd = atomsEnd; |
| 568 | this->_absoluteAtoms._elementSize = atomSize; |
| 569 | this->_absoluteAtoms._elementCount = chunk->elementCount; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 570 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 571 | << " chunk AbsoluteAtomsV1: " |
| 572 | << " count=" << chunk->elementCount |
| 573 | << " chunkSize=" << chunk->fileSize |
| 574 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 575 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 576 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 577 | |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 578 | template<class T, class U> |
| 579 | error_code processReferences(const uint8_t *base, const NativeChunk *chunk, |
| 580 | uint8_t *&refsStart, uint8_t *&refsEnd) const { |
| 581 | if (chunk->elementCount == 0) |
| 582 | return make_error_code(NativeReaderError::success); |
| 583 | size_t refsArraySize = chunk->elementCount * sizeof(T); |
| 584 | refsStart = reinterpret_cast<uint8_t *>( |
| 585 | operator new(refsArraySize, std::nothrow)); |
| 586 | if (refsStart == nullptr) |
| 587 | return make_error_code(NativeReaderError::memory_error); |
| 588 | const size_t ivarElementSize = chunk->fileSize / chunk->elementCount; |
| 589 | if (ivarElementSize != sizeof(U)) |
| 590 | return make_error_code(NativeReaderError::file_malformed); |
| 591 | refsEnd = refsStart + refsArraySize; |
| 592 | const U* ivarData = reinterpret_cast<const U *>(base + chunk->fileOffset); |
| 593 | for (uint8_t *s = refsStart; s != refsEnd; s += sizeof(T), ++ivarData) { |
| 594 | T *atomAllocSpace = reinterpret_cast<T *>(s); |
| 595 | new (atomAllocSpace) T(*this, ivarData); |
| 596 | } |
| 597 | return make_error_code(NativeReaderError::success); |
| 598 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 599 | |
Alp Toker | 32e8bef | 2013-12-01 23:51:36 +0000 | [diff] [blame] | 600 | // instantiate array of References from v1 ivar data in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 601 | error_code processReferencesV1(const uint8_t *base, |
| 602 | const NativeChunk *chunk) { |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 603 | uint8_t *refsStart, *refsEnd; |
| 604 | if (error_code ec |
| 605 | = processReferences<NativeReferenceV1, NativeReferenceIvarsV1>( |
| 606 | base, chunk, refsStart, refsEnd)) |
| 607 | return ec; |
| 608 | this->_referencesV1.arrayStart = refsStart; |
| 609 | this->_referencesV1.arrayEnd = refsEnd; |
| 610 | this->_referencesV1.elementSize = sizeof(NativeReferenceV1); |
| 611 | this->_referencesV1.elementCount = chunk->elementCount; |
| 612 | DEBUG_WITH_TYPE("ReaderNative", { |
| 613 | llvm::dbgs() << " chunk ReferencesV1: " |
| 614 | << " count=" << chunk->elementCount |
| 615 | << " chunkSize=" << chunk->fileSize << "\n"; |
| 616 | }); |
| 617 | return make_error_code(NativeReaderError::success); |
| 618 | } |
| 619 | |
Alp Toker | 32e8bef | 2013-12-01 23:51:36 +0000 | [diff] [blame] | 620 | // instantiate array of References from v2 ivar data in file |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 621 | error_code processReferencesV2(const uint8_t *base, |
| 622 | const NativeChunk *chunk) { |
| 623 | uint8_t *refsStart, *refsEnd; |
| 624 | if (error_code ec |
| 625 | = processReferences<NativeReferenceV2, NativeReferenceIvarsV2>( |
| 626 | base, chunk, refsStart, refsEnd)) |
| 627 | return ec; |
| 628 | this->_referencesV2.arrayStart = refsStart; |
| 629 | this->_referencesV2.arrayEnd = refsEnd; |
| 630 | this->_referencesV2.elementSize = sizeof(NativeReferenceV2); |
| 631 | this->_referencesV2.elementCount = chunk->elementCount; |
| 632 | DEBUG_WITH_TYPE("ReaderNative", { |
| 633 | llvm::dbgs() << " chunk ReferencesV2: " |
| 634 | << " count=" << chunk->elementCount |
| 635 | << " chunkSize=" << chunk->fileSize << "\n"; |
| 636 | }); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 637 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 638 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 639 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 640 | // set up pointers to target table |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 641 | error_code processTargetsTable(const uint8_t *base, |
| 642 | const NativeChunk *chunk) { |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 643 | const uint32_t* targetIndexes = reinterpret_cast<const uint32_t*> |
| 644 | (base + chunk->fileOffset); |
| 645 | this->_targetsTableCount = chunk->elementCount; |
| 646 | this->_targetsTable = new const Atom*[chunk->elementCount]; |
| 647 | for (uint32_t i=0; i < chunk->elementCount; ++i) { |
| 648 | const uint32_t index = targetIndexes[i]; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 649 | if ( index < _definedAtoms._elementCount ) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 650 | const uint8_t* p = _definedAtoms._arrayStart |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 651 | + index * _definedAtoms._elementSize; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 652 | this->_targetsTable[i] = reinterpret_cast<const DefinedAtom*>(p); |
| 653 | continue; |
| 654 | } |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 655 | const uint32_t undefIndex = index - _definedAtoms._elementCount; |
| 656 | if ( undefIndex < _undefinedAtoms._elementCount ) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 657 | const uint8_t* p = _undefinedAtoms._arrayStart |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 658 | + undefIndex * _undefinedAtoms._elementSize; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 659 | this->_targetsTable[i] = reinterpret_cast<const UndefinedAtom*>(p); |
| 660 | continue; |
| 661 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 662 | const uint32_t slIndex = index - _definedAtoms._elementCount |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 663 | - _undefinedAtoms._elementCount; |
| 664 | if ( slIndex < _sharedLibraryAtoms._elementCount ) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 665 | const uint8_t* p = _sharedLibraryAtoms._arrayStart |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 666 | + slIndex * _sharedLibraryAtoms._elementSize; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 667 | this->_targetsTable[i] = reinterpret_cast<const SharedLibraryAtom*>(p); |
| 668 | continue; |
| 669 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 670 | const uint32_t abIndex = index - _definedAtoms._elementCount |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 671 | - _undefinedAtoms._elementCount |
| 672 | - _sharedLibraryAtoms._elementCount; |
| 673 | if ( abIndex < _absoluteAtoms._elementCount ) { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 674 | const uint8_t* p = _absoluteAtoms._arrayStart |
Simon Atanasyan | 8ef39b5 | 2013-11-04 19:40:02 +0000 | [diff] [blame] | 675 | + abIndex * _absoluteAtoms._elementSize; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 676 | this->_targetsTable[i] = reinterpret_cast<const AbsoluteAtom*>(p); |
| 677 | continue; |
| 678 | } |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 679 | return make_error_code(NativeReaderError::file_malformed); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 680 | } |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 681 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 682 | << " chunk Targets Table: " |
| 683 | << " count=" << chunk->elementCount |
| 684 | << " chunkSize=" << chunk->fileSize |
| 685 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 686 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 687 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 688 | |
| 689 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 690 | // set up pointers to addend pool in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 691 | error_code processAddendsTable(const uint8_t *base, |
| 692 | const NativeChunk *chunk) { |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 693 | this->_addends = reinterpret_cast<const Reference::Addend*> |
| 694 | (base + chunk->fileOffset); |
| 695 | this->_addendsMaxIndex = chunk->elementCount; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 696 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 697 | << " chunk Addends: " |
| 698 | << " count=" << chunk->elementCount |
| 699 | << " chunkSize=" << chunk->fileSize |
| 700 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 701 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 702 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 703 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 704 | // set up pointers to string pool in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 705 | error_code processStrings(const uint8_t *base, |
| 706 | const NativeChunk *chunk) { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 707 | this->_strings = reinterpret_cast<const char*>(base + chunk->fileOffset); |
| 708 | this->_stringsMaxOffset = chunk->fileSize; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 709 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 710 | << " chunk Strings: " |
| 711 | << " chunkSize=" << chunk->fileSize |
| 712 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 713 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 714 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 715 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 716 | // set up pointers to content area in file |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 717 | error_code processContent(const uint8_t *base, |
| 718 | const NativeChunk *chunk) { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 719 | this->_contentStart = base + chunk->fileOffset; |
| 720 | this->_contentEnd = base + chunk->fileOffset + chunk->fileSize; |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 721 | DEBUG_WITH_TYPE("ReaderNative", llvm::dbgs() |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 722 | << " chunk content: " |
| 723 | << " chunkSize=" << chunk->fileSize |
| 724 | << "\n"); |
Rui Ueyama | c6015f6 | 2013-10-09 00:57:22 +0000 | [diff] [blame] | 725 | return make_error_code(NativeReaderError::success); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 726 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 727 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 728 | StringRef string(uint32_t offset) const { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 729 | assert(offset < _stringsMaxOffset); |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 730 | return StringRef(&_strings[offset]); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 731 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 732 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 733 | Reference::Addend addend(uint32_t index) const { |
| 734 | if ( index == 0 ) |
| 735 | return 0; // addend index zero is used to mean "no addend" |
| 736 | assert(index <= _addendsMaxIndex); |
| 737 | return _addends[index-1]; // one-based indexing |
| 738 | } |
| 739 | |
| 740 | const NativeAtomAttributesV1& attribute(uint32_t off) const { |
| 741 | assert(off < _attributesMaxOffset); |
| 742 | return *reinterpret_cast<const NativeAtomAttributesV1*>(_attributes + off); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 745 | const NativeAtomAttributesV1& absAttribute(uint32_t off) const { |
| 746 | assert(off < _absAbsoluteMaxOffset); |
| 747 | return *reinterpret_cast<const NativeAtomAttributesV1*>(_absAttributes + off); |
| 748 | } |
| 749 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 750 | const uint8_t* content(uint32_t offset, uint32_t size) const { |
| 751 | const uint8_t* result = _contentStart + offset; |
| 752 | assert((result+size) <= _contentEnd); |
| 753 | return result; |
| 754 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 755 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 756 | const Reference* referenceByIndex(uintptr_t index) const { |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 757 | if (index < _referencesV1.elementCount) { |
| 758 | return reinterpret_cast<const NativeReferenceV1*>( |
| 759 | _referencesV1.arrayStart + index * _referencesV1.elementSize); |
| 760 | } |
| 761 | assert(index < _referencesV2.elementCount); |
| 762 | return reinterpret_cast<const NativeReferenceV2*>( |
| 763 | _referencesV2.arrayStart + index * _referencesV2.elementSize); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 766 | const Atom* targetV1(uint16_t index) const { |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 767 | if ( index == NativeReferenceIvarsV1::noTarget ) |
| 768 | return nullptr; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 769 | assert(index < _targetsTableCount); |
| 770 | return _targetsTable[index]; |
| 771 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 772 | |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 773 | void setTargetV1(uint16_t index, const Atom* newAtom) const { |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 774 | assert(index != NativeReferenceIvarsV1::noTarget); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 775 | assert(index > _targetsTableCount); |
| 776 | _targetsTable[index] = newAtom; |
| 777 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 778 | |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 779 | const Atom* targetV2(uint32_t index) const { |
| 780 | if (index == NativeReferenceIvarsV2::noTarget) |
| 781 | return nullptr; |
| 782 | assert(index < _targetsTableCount); |
| 783 | return _targetsTable[index]; |
| 784 | } |
| 785 | |
| 786 | void setTargetV2(uint32_t index, const Atom* newAtom) const { |
| 787 | assert(index != NativeReferenceIvarsV2::noTarget); |
| 788 | assert(index > _targetsTableCount); |
| 789 | _targetsTable[index] = newAtom; |
| 790 | } |
| 791 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 792 | // private constructor, only called by make() |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 793 | File(std::unique_ptr<MemoryBuffer> mb, StringRef path) |
Michael J. Spencer | 0f3dd61 | 2013-03-20 18:57:27 +0000 | [diff] [blame] | 794 | : lld::File(path, kindObject), |
Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 795 | _buffer(std::move(mb)), // Reader now takes ownership of buffer |
| 796 | _header(nullptr), _targetsTable(nullptr), _targetsTableCount(0), |
| 797 | _strings(nullptr), _stringsMaxOffset(0), _addends(nullptr), |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 798 | _addendsMaxIndex(0), _contentStart(nullptr), _contentEnd(nullptr) { |
Shankar Easwaran | 3256d4f | 2013-01-25 07:39:18 +0000 | [diff] [blame] | 799 | _header = |
| 800 | reinterpret_cast<const NativeFileHeader *>(_buffer->getBufferStart()); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 803 | template <typename T> |
| 804 | class AtomArray : public File::atom_collection<T> { |
| 805 | public: |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 806 | AtomArray() : _arrayStart(nullptr), _arrayEnd(nullptr), |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 807 | _elementSize(0), _elementCount(0) { } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 808 | |
| 809 | virtual atom_iterator<T> begin() const { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 810 | return atom_iterator<T>(*this, reinterpret_cast<const void*>(_arrayStart)); |
| 811 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 812 | virtual atom_iterator<T> end() const{ |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 813 | return atom_iterator<T>(*this, reinterpret_cast<const void*>(_arrayEnd)); |
| 814 | } |
| 815 | virtual const T* deref(const void* it) const { |
| 816 | return reinterpret_cast<const T*>(it); |
| 817 | } |
| 818 | virtual void next(const void*& it) const { |
| 819 | const uint8_t* p = reinterpret_cast<const uint8_t*>(it); |
| 820 | p += _elementSize; |
| 821 | it = reinterpret_cast<const void*>(p); |
| 822 | } |
Shankar Easwaran | cc06891 | 2013-04-29 04:10:42 +0000 | [diff] [blame] | 823 | virtual uint64_t size() const { return _elementCount; } |
| 824 | const uint8_t *_arrayStart; |
| 825 | const uint8_t *_arrayEnd; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 826 | uint32_t _elementSize; |
| 827 | uint32_t _elementCount; |
| 828 | }; |
| 829 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 830 | struct IvarArray { |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 831 | IvarArray() : |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 832 | arrayStart(nullptr), |
| 833 | arrayEnd(nullptr), |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 834 | elementSize(0), |
| 835 | elementCount(0) { } |
| 836 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 837 | const uint8_t* arrayStart; |
| 838 | const uint8_t* arrayEnd; |
| 839 | uint32_t elementSize; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 840 | uint32_t elementCount; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 841 | }; |
| 842 | |
Nick Kledzik | f30e848 | 2013-12-20 20:34:19 +0000 | [diff] [blame^] | 843 | std::unique_ptr<MemoryBuffer> _buffer; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 844 | const NativeFileHeader* _header; |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 845 | AtomArray<DefinedAtom> _definedAtoms; |
| 846 | AtomArray<UndefinedAtom> _undefinedAtoms; |
| 847 | AtomArray<SharedLibraryAtom> _sharedLibraryAtoms; |
| 848 | AtomArray<AbsoluteAtom> _absoluteAtoms; |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 849 | const uint8_t* _absAttributes; |
| 850 | uint32_t _absAbsoluteMaxOffset; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 851 | const uint8_t* _attributes; |
| 852 | uint32_t _attributesMaxOffset; |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 853 | IvarArray _referencesV1; |
| 854 | IvarArray _referencesV2; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 855 | const Atom** _targetsTable; |
| 856 | uint32_t _targetsTableCount; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 857 | const char* _strings; |
| 858 | uint32_t _stringsMaxOffset; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 859 | const Reference::Addend* _addends; |
Nick Kledzik | f30e848 | 2013-12-20 20:34:19 +0000 | [diff] [blame^] | 860 | uint32_t _addendsMaxIndex; |
| 861 | const uint8_t *_contentStart; |
| 862 | const uint8_t *_contentEnd; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 863 | }; |
| 864 | |
Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 865 | inline const lld::File &NativeDefinedAtomV1::file() const { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 866 | return *_file; |
| 867 | } |
| 868 | |
| 869 | inline uint64_t NativeDefinedAtomV1:: ordinal() const { |
| 870 | const uint8_t* p = reinterpret_cast<const uint8_t*>(_ivarData); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 871 | return p - _file->_definedAtoms._arrayStart; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 874 | inline StringRef NativeDefinedAtomV1::name() const { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 875 | return _file->string(_ivarData->nameOffset); |
| 876 | } |
| 877 | |
| 878 | inline const NativeAtomAttributesV1& NativeDefinedAtomV1::attributes() const { |
| 879 | return _file->attribute(_ivarData->attributesOffset); |
| 880 | } |
| 881 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 882 | inline ArrayRef<uint8_t> NativeDefinedAtomV1::rawContent() const { |
Shankar Easwaran | d17ba4b | 2013-08-23 20:03:21 +0000 | [diff] [blame] | 883 | if (!occupiesDiskSpace()) |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 884 | return ArrayRef<uint8_t>(); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 885 | const uint8_t* p = _file->content(_ivarData->contentOffset, |
| 886 | _ivarData->contentSize); |
Shankar Easwaran | d17ba4b | 2013-08-23 20:03:21 +0000 | [diff] [blame] | 887 | return ArrayRef<uint8_t>(p, _ivarData->contentSize); |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 890 | inline StringRef NativeDefinedAtomV1::customSectionName() const { |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 891 | uint32_t offset = attributes().sectionNameOffset; |
| 892 | return _file->string(offset); |
| 893 | } |
| 894 | |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 895 | DefinedAtom::reference_iterator NativeDefinedAtomV1::begin() const { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 896 | uintptr_t index = _ivarData->referencesStartIndex; |
| 897 | const void* it = reinterpret_cast<const void*>(index); |
| 898 | return reference_iterator(*this, it); |
| 899 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 900 | |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 901 | DefinedAtom::reference_iterator NativeDefinedAtomV1::end() const { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 902 | uintptr_t index = _ivarData->referencesStartIndex+_ivarData->referencesCount; |
| 903 | const void* it = reinterpret_cast<const void*>(index); |
| 904 | return reference_iterator(*this, it); |
| 905 | } |
| 906 | |
| 907 | const Reference* NativeDefinedAtomV1::derefIterator(const void* it) const { |
| 908 | uintptr_t index = reinterpret_cast<uintptr_t>(it); |
| 909 | return _file->referenceByIndex(index); |
| 910 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 911 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 912 | void NativeDefinedAtomV1::incrementIterator(const void*& it) const { |
| 913 | uintptr_t index = reinterpret_cast<uintptr_t>(it); |
| 914 | ++index; |
| 915 | it = reinterpret_cast<const void*>(index); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 916 | } |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 917 | |
Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 918 | inline const lld::File& NativeUndefinedAtomV1::file() const { |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 919 | return *_file; |
| 920 | } |
| 921 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 922 | inline StringRef NativeUndefinedAtomV1::name() const { |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 923 | return _file->string(_ivarData->nameOffset); |
| 924 | } |
| 925 | |
Rui Ueyama | e5416ec | 2013-09-12 19:14:05 +0000 | [diff] [blame] | 926 | inline const UndefinedAtom *NativeUndefinedAtomV1::fallback() const { |
| 927 | if (!_ivarData->fallbackNameOffset) |
| 928 | return nullptr; |
| 929 | if (!_fallback) |
| 930 | _fallback.reset(new SimpleUndefinedAtom( |
| 931 | *_file, _file->string(_ivarData->fallbackNameOffset))); |
| 932 | return _fallback.get(); |
| 933 | } |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 934 | |
Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 935 | inline const lld::File& NativeSharedLibraryAtomV1::file() const { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 936 | return *_file; |
| 937 | } |
| 938 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 939 | inline StringRef NativeSharedLibraryAtomV1::name() const { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 940 | return _file->string(_ivarData->nameOffset); |
| 941 | } |
| 942 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 943 | inline StringRef NativeSharedLibraryAtomV1::loadName() const { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 944 | return _file->string(_ivarData->loadNameOffset); |
| 945 | } |
| 946 | |
| 947 | |
| 948 | |
Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 949 | inline const lld::File& NativeAbsoluteAtomV1::file() const { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 950 | return *_file; |
| 951 | } |
| 952 | |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 953 | inline StringRef NativeAbsoluteAtomV1::name() const { |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 954 | return _file->string(_ivarData->nameOffset); |
| 955 | } |
| 956 | |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 957 | inline const NativeAtomAttributesV1& NativeAbsoluteAtomV1::absAttributes() const { |
| 958 | return _file->absAttribute(_ivarData->attributesOffset); |
| 959 | } |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 960 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 961 | inline const Atom* NativeReferenceV1::target() const { |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 962 | return _file->targetV1(_ivarData->targetIndex); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | inline Reference::Addend NativeReferenceV1::addend() const { |
| 966 | return _file->addend(_ivarData->addendIndex); |
| 967 | } |
| 968 | |
| 969 | inline void NativeReferenceV1::setTarget(const Atom* newAtom) { |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 970 | return _file->setTargetV1(_ivarData->targetIndex, newAtom); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 971 | } |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 972 | |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 973 | inline void NativeReferenceV1::setAddend(Addend a) { |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 974 | // Do nothing if addend value is not being changed. |
Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 975 | if (addend() == a) |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 976 | return; |
Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 977 | llvm_unreachable("setAddend() not supported"); |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 980 | inline const Atom* NativeReferenceV2::target() const { |
| 981 | return _file->targetV2(_ivarData->targetIndex); |
| 982 | } |
| 983 | |
| 984 | inline Reference::Addend NativeReferenceV2::addend() const { |
| 985 | return _ivarData->addend; |
| 986 | } |
| 987 | |
| 988 | inline void NativeReferenceV2::setTarget(const Atom* newAtom) { |
| 989 | return _file->setTargetV2(_ivarData->targetIndex, newAtom); |
| 990 | } |
| 991 | |
| 992 | inline void NativeReferenceV2::setAddend(Addend a) { |
| 993 | // Do nothing if addend value is not being changed. |
| 994 | if (addend() == a) |
| 995 | return; |
| 996 | llvm_unreachable("setAddend() not supported"); |
| 997 | } |
| 998 | |
Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 999 | } // end namespace native |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 1000 | |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 1001 | namespace { |
| 1002 | |
| 1003 | class NativeReader : public Reader { |
| 1004 | public: |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 1005 | virtual bool canParse(file_magic magic, StringRef, |
| 1006 | const MemoryBuffer &mb) const { |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 1007 | const NativeFileHeader *const header = |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 1008 | reinterpret_cast<const NativeFileHeader *>(mb.getBufferStart()); |
| 1009 | return (memcmp(header->magic, NATIVE_FILE_HEADER_MAGIC, |
| 1010 | sizeof(header->magic)) == 0); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 1011 | } |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 1012 | |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 1013 | virtual error_code |
| 1014 | parseFile(std::unique_ptr<MemoryBuffer> &mb, const class Registry &, |
| 1015 | std::vector<std::unique_ptr<File>> &result) const { |
| 1016 | return lld::native::File::make(std::move(mb), result); |
| 1017 | return error_code::success(); |
| 1018 | } |
| 1019 | }; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 1020 | } |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 1021 | |
| 1022 | void Registry::addSupportNativeObjects() { |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 1023 | add(std::unique_ptr<Reader>(new NativeReader())); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 1026 | } // end namespace lld |