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