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