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