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