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