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