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