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