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