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