blob: ffd216c97ecb77530e665d58a31e81d0c7296537 [file] [log] [blame]
Nick Kledzik55fd6be2012-01-16 22:03:44 +00001//===- 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. Spencercfd029f2012-03-28 19:04:02 +000010#include "NativeFileFormat.h"
Nick Kledzik55fd6be2012-01-16 22:03:44 +000011
Michael J. Spencercfd029f2012-03-28 19:04:02 +000012#include "lld/Core/Atom.h"
13#include "lld/Core/Error.h"
14#include "lld/Core/File.h"
Nick Kledzik55fd6be2012-01-16 22:03:44 +000015
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 Kledzik55fd6be2012-01-16 22:03:44 +000021
Michael J. Spencercfd029f2012-03-28 19:04:02 +000022#include <vector>
Nick Kledzik55fd6be2012-01-16 22:03:44 +000023
24namespace lld {
25
26// forward reference
27class NativeFile;
28
Nick Kledzik55fd6be2012-01-16 22:03:44 +000029//
30// An object of this class is instantied for each NativeDefinedAtomIvarsV1
31// struct in the NCS_DefinedAtomsV1 chunk.
32//
33class NativeDefinedAtomV1 : public DefinedAtom {
34public:
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 Kledzik55fd6be2012-01-16 22:03:44 +000045 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 Kledzik23384e82012-02-07 02:59:54 +000062 const NativeAtomAttributesV1& attr = attributes();
63 return (DefinedAtom::ContentType)(attr.contentType);
Nick Kledzik55fd6be2012-01-16 22:03:44 +000064 }
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 Kledzik6bc04c62012-02-22 21:56:59 +000078 }
Nick Kledzik55fd6be2012-01-16 22:03:44 +000079
80 virtual DefinedAtom::ContentPermissions permissions() const {
81 return (DefinedAtom::ContentPermissions)(attributes().permissions);
82 }
83
84 virtual bool isThumb() const {
Nick Kledzik49d6cc82012-02-15 00:38:09 +000085 return false; //(attributes().thumb != 0);
Nick Kledzik55fd6be2012-01-16 22:03:44 +000086 }
87
88 virtual bool isAlias() const {
89 return (attributes().alias != 0);
90 }
91
Nick Kledzik49d6cc82012-02-15 00:38:09 +000092 virtual llvm::ArrayRef<uint8_t> rawContent() const;
Nick Kledzik1a6615d2012-03-08 00:18:30 +000093
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 Kledzik55fd6be2012-01-16 22:03:44 +0000102private:
103 const NativeAtomAttributesV1& attributes() const;
104
105 const NativeFile* _file;
106 const NativeDefinedAtomIvarsV1* _ivarData;
107};
108
109
110
Nick Kledzik23384e82012-02-07 02:59:54 +0000111//
112// An object of this class is instantied for each NativeUndefinedAtomIvarsV1
113// struct in the NCS_UndefinedAtomsV1 chunk.
114//
115class NativeUndefinedAtomV1 : public UndefinedAtom {
116public:
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 Kledzik6bc04c62012-02-22 21:56:59 +0000124 virtual CanBeNull canBeNull() const {
125 return (CanBeNull)(_ivarData->flags & 0x3);
Nick Kledzik23384e82012-02-07 02:59:54 +0000126 }
127
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000128
Nick Kledzik23384e82012-02-07 02:59:54 +0000129private:
130 const NativeFile* _file;
131 const NativeUndefinedAtomIvarsV1* _ivarData;
132};
133
134
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000135//
136// An object of this class is instantied for each NativeUndefinedAtomIvarsV1
137// struct in the NCS_SharedLibraryAtomsV1 chunk.
138//
139class NativeSharedLibraryAtomV1 : public SharedLibraryAtom {
140public:
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
153private:
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//
163class NativeAbsoluteAtomV1 : public AbsoluteAtom {
164public:
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
176private:
177 const NativeFile* _file;
178 const NativeAbsoluteAtomIvarsV1* _ivarData;
179};
180
181
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000182
183//
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000184// An object of this class is instantied for each NativeReferenceIvarsV1
185// struct in the NCS_ReferencesArrayV1 chunk.
186//
187class NativeReferenceV1 : public Reference {
188public:
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 Kledzikf4e2c732012-03-15 23:36:24 +0000201 virtual void setKind(Kind);
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000202 virtual const Atom* target() const;
203 virtual Addend addend() const;
204 virtual void setTarget(const Atom* newAtom);
Nick Kledzikf4e2c732012-03-15 23:36:24 +0000205
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000206private:
Nick Kledzikf4e2c732012-03-15 23:36:24 +0000207 // 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 Kledzik49d6cc82012-02-15 00:38:09 +0000217 const NativeFile* _file;
218 const NativeReferenceIvarsV1* _ivarData;
219};
220
221
222
223//
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000224// lld::File object for native llvm object file
225//
226class NativeFile : public File {
227public:
228
229 /// Instantiates a File object from a native object file. Ownership
230 /// of the MemoryBuffer is transfered to the resulting File object.
Nick Kledzik23384e82012-02-07 02:59:54 +0000231 static llvm::error_code make(llvm::OwningPtr<llvm::MemoryBuffer>& mb,
232 llvm::StringRef path,
233 llvm::OwningPtr<File>& result) {
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000234 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. Spencerb2bd7332012-01-31 21:45:53 +0000238 const NativeChunk *const chunks =
239 reinterpret_cast<const NativeChunk*>(base + sizeof(NativeFileHeader));
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000240 // make sure magic matches
241 if ( memcmp(header->magic, NATIVE_FILE_HEADER_MAGIC, 16) != 0 )
Michael J. Spencer7aba8952012-01-31 21:47:13 +0000242 return make_error_code(native_reader_error::unknown_file_format);
243
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000244 // make sure mapped file contains all needed data
245 const size_t fileSize = mb->getBufferSize();
246 if ( header->fileSize > fileSize )
Michael J. Spencer7aba8952012-01-31 21:47:13 +0000247 return make_error_code(native_reader_error::file_too_short);
248
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000249 // 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. Spencerb2bd7332012-01-31 21:45:53 +0000255 const NativeChunk* chunk = &chunks[i];
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000256 // sanity check chunk is within file
Michael J. Spencer7aba8952012-01-31 21:47:13 +0000257 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 Kledzik55fd6be2012-01-16 22:03:44 +0000261 // 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 Kledzik23384e82012-02-07 02:59:54 +0000269 case NCS_UndefinedAtomsV1:
270 ec = file->processUndefinedAtomsV1(base, chunk);
271 break;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000272 case NCS_SharedLibraryAtomsV1:
273 ec = file->processSharedLibraryAtomsV1(base, chunk);
274 break;
275 case NCS_AbsoluteAtomsV1:
276 ec = file->processAbsoluteAtomsV1(base, chunk);
277 break;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000278 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 Kledzik55fd6be2012-01-16 22:03:44 +0000287 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. Spencer7aba8952012-01-31 21:47:13 +0000294 return make_error_code(native_reader_error::unknown_chunk_type);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000295 }
296 if ( ec ) {
297 delete file;
298 return ec;
299 }
300
301 // TO DO: validate enough chunks were used
302
Nick Kledzik23384e82012-02-07 02:59:54 +0000303 result.reset(file);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000304 }
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000305
Michael J. Spencer7aba8952012-01-31 21:47:13 +0000306
307 return make_error_code(native_reader_error::success);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000308 }
309
310 virtual ~NativeFile() {
Nick Kledzik23384e82012-02-07 02:59:54 +0000311 // _buffer is automatically deleted because of OwningPtr<>
312
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000313 // 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 Kledzik1a6615d2012-03-08 00:18:30 +0000317 delete _definedAtoms._arrayStart;
318 delete _undefinedAtoms._arrayStart;
319 delete _sharedLibraryAtoms._arrayStart;
320 delete _absoluteAtoms._arrayStart;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000321 delete _references.arrayStart;
322 delete _targetsTable;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000323 }
324
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000325 virtual const atom_collection<DefinedAtom>& defined() const {
326 return _definedAtoms;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000327 }
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000328 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 Kledzik55fd6be2012-01-16 22:03:44 +0000340 }
341
342private:
343 friend class NativeDefinedAtomV1;
Nick Kledzik23384e82012-02-07 02:59:54 +0000344 friend class NativeUndefinedAtomV1;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000345 friend class NativeSharedLibraryAtomV1;
346 friend class NativeAbsoluteAtomV1;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000347 friend class NativeReferenceV1;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000348
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. Spencer7aba8952012-01-31 21:47:13 +0000357 return make_error_code(native_reader_error::memory_error);
358 const size_t ivarElementSize = chunk->fileSize
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000359 / chunk->elementCount;
360 if ( ivarElementSize != sizeof(NativeDefinedAtomIvarsV1) )
Michael J. Spencer7aba8952012-01-31 21:47:13 +0000361 return make_error_code(native_reader_error::file_malformed);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000362 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 Kledzik1a6615d2012-03-08 00:18:30 +0000372 this->_definedAtoms._arrayStart = atomsStart;
373 this->_definedAtoms._arrayEnd = atomsEnd;
374 this->_definedAtoms._elementSize = atomSize;
375 this->_definedAtoms._elementCount = chunk->elementCount;
Michael J. Spencer7aba8952012-01-31 21:47:13 +0000376 return make_error_code(native_reader_error::success);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000377 }
378
379 // set up pointers to attributes array
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000380 llvm::error_code processAttributesV1(const uint8_t* base,
381 const NativeChunk* chunk) {
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000382 this->_attributes = base + chunk->fileOffset;
383 this->_attributesMaxOffset = chunk->fileSize;
Michael J. Spencer7aba8952012-01-31 21:47:13 +0000384 return make_error_code(native_reader_error::success);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000385 }
386
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000387 // instantiate array of UndefinedAtoms from v1 ivar data in file
Nick Kledzik23384e82012-02-07 02:59:54 +0000388 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 Kledzik1a6615d2012-03-08 00:18:30 +0000410 this->_undefinedAtoms._arrayStart = atomsStart;
411 this->_undefinedAtoms._arrayEnd = atomsEnd;
412 this->_undefinedAtoms._elementSize = atomSize;
413 this->_undefinedAtoms._elementCount = chunk->elementCount;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000414 return make_error_code(native_reader_error::success);
415 }
416
417
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000418 // 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 Kledzik1a6615d2012-03-08 00:18:30 +0000441 this->_sharedLibraryAtoms._arrayStart = atomsStart;
442 this->_sharedLibraryAtoms._arrayEnd = atomsEnd;
443 this->_sharedLibraryAtoms._elementSize = atomSize;
444 this->_sharedLibraryAtoms._elementCount = chunk->elementCount;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000445 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 Kledzik1a6615d2012-03-08 00:18:30 +0000472 this->_absoluteAtoms._arrayStart = atomsStart;
473 this->_absoluteAtoms._arrayEnd = atomsEnd;
474 this->_absoluteAtoms._elementSize = atomSize;
475 this->_absoluteAtoms._elementCount = chunk->elementCount;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000476 return make_error_code(native_reader_error::success);
477 }
478
479
480
481
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000482 // 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 Kledzik1a6615d2012-03-08 00:18:30 +0000523 if ( index < _definedAtoms._elementCount ) {
524 const uint8_t* p = _definedAtoms._arrayStart
525 + index * _definedAtoms._elementSize;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000526 this->_targetsTable[i] = reinterpret_cast<const DefinedAtom*>(p);
527 continue;
528 }
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000529 const uint32_t undefIndex = index - _definedAtoms._elementCount;
530 if ( undefIndex < _undefinedAtoms._elementCount ) {
531 const uint8_t* p = _undefinedAtoms._arrayStart
532 + undefIndex * _undefinedAtoms._elementSize;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000533 this->_targetsTable[i] = reinterpret_cast<const UndefinedAtom*>(p);
534 continue;
535 }
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000536 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 Kledzik6bc04c62012-02-22 21:56:59 +0000541 this->_targetsTable[i] = reinterpret_cast<const SharedLibraryAtom*>(p);
542 continue;
543 }
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000544 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 Kledzik6bc04c62012-02-22 21:56:59 +0000550 this->_targetsTable[i] = reinterpret_cast<const AbsoluteAtom*>(p);
551 continue;
552 }
553 return make_error_code(native_reader_error::file_malformed);
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000554 }
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 Kledzik23384e82012-02-07 02:59:54 +0000565 return make_error_code(native_reader_error::success);
566 }
567
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000568 // 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. Spencer7aba8952012-01-31 21:47:13 +0000573 return make_error_code(native_reader_error::success);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000574 }
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. Spencer7aba8952012-01-31 21:47:13 +0000581 return make_error_code(native_reader_error::success);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000582 }
583
584 llvm::StringRef string(uint32_t offset) const {
585 assert(offset < _stringsMaxOffset);
586 return llvm::StringRef(&_strings[offset]);
587 }
588
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000589 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 Kledzik55fd6be2012-01-16 22:03:44 +0000599 }
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 Kledzik49d6cc82012-02-15 00:38:09 +0000606
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000607 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 Kledzik49d6cc82012-02-15 00:38:09 +0000613 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 Kledzik55fd6be2012-01-16 22:03:44 +0000624 // private constructor, only called by make()
Nick Kledzik23384e82012-02-07 02:59:54 +0000625 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 Kledzik49d6cc82012-02-15 00:38:09 +0000629 _targetsTable(NULL),
630 _targetsTableCount(0),
Nick Kledzik23384e82012-02-07 02:59:54 +0000631 _strings(NULL),
632 _stringsMaxOffset(0),
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000633 _addends(NULL),
634 _addendsMaxIndex(0),
Nick Kledzik23384e82012-02-07 02:59:54 +0000635 _contentStart(NULL),
636 _contentEnd(NULL)
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000637 {
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000638 _header = reinterpret_cast<const NativeFileHeader*>
639 (_buffer->getBufferStart());
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000640 }
641
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000642 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 Kledzik49d6cc82012-02-15 00:38:09 +0000668 struct IvarArray {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000669 IvarArray() :
670 arrayStart(NULL),
671 arrayEnd(NULL),
672 elementSize(0),
673 elementCount(0) { }
674
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000675 const uint8_t* arrayStart;
676 const uint8_t* arrayEnd;
677 uint32_t elementSize;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000678 uint32_t elementCount;
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000679 };
680
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000681
Nick Kledzik23384e82012-02-07 02:59:54 +0000682 llvm::OwningPtr<llvm::MemoryBuffer> _buffer;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000683 const NativeFileHeader* _header;
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000684 AtomArray<DefinedAtom> _definedAtoms;
685 AtomArray<UndefinedAtom> _undefinedAtoms;
686 AtomArray<SharedLibraryAtom> _sharedLibraryAtoms;
687 AtomArray<AbsoluteAtom> _absoluteAtoms;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000688 const uint8_t* _attributes;
689 uint32_t _attributesMaxOffset;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000690 IvarArray _references;
691 const Atom** _targetsTable;
692 uint32_t _targetsTableCount;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000693 const char* _strings;
694 uint32_t _stringsMaxOffset;
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000695 const Reference::Addend* _addends;
696 uint32_t _addendsMaxIndex;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000697 const uint8_t* _contentStart;
698 const uint8_t* _contentEnd;
699};
700
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000701
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000702inline const class File& NativeDefinedAtomV1::file() const {
703 return *_file;
704}
705
706inline uint64_t NativeDefinedAtomV1:: ordinal() const {
707 const uint8_t* p = reinterpret_cast<const uint8_t*>(_ivarData);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000708 return p - _file->_definedAtoms._arrayStart;
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000709}
710
711inline llvm::StringRef NativeDefinedAtomV1::name() const {
712 return _file->string(_ivarData->nameOffset);
713}
714
715inline const NativeAtomAttributesV1& NativeDefinedAtomV1::attributes() const {
716 return _file->attribute(_ivarData->attributesOffset);
717}
718
719inline 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
727inline llvm::StringRef NativeDefinedAtomV1::customSectionName() const {
728 uint32_t offset = attributes().sectionNameOffset;
729 return _file->string(offset);
730}
731
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000732DefinedAtom::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
738DefinedAtom::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
744const Reference* NativeDefinedAtomV1::derefIterator(const void* it) const {
745 uintptr_t index = reinterpret_cast<uintptr_t>(it);
746 return _file->referenceByIndex(index);
747}
748
749void 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 Kledzik49d6cc82012-02-15 00:38:09 +0000753}
Nick Kledzik23384e82012-02-07 02:59:54 +0000754
755inline const class File& NativeUndefinedAtomV1::file() const {
756 return *_file;
757}
758
759inline llvm::StringRef NativeUndefinedAtomV1::name() const {
760 return _file->string(_ivarData->nameOffset);
761}
762
763
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000764
765
766inline const class File& NativeSharedLibraryAtomV1::file() const {
767 return *_file;
768}
769
770inline llvm::StringRef NativeSharedLibraryAtomV1::name() const {
771 return _file->string(_ivarData->nameOffset);
772}
773
774inline llvm::StringRef NativeSharedLibraryAtomV1::loadName() const {
775 return _file->string(_ivarData->loadNameOffset);
776}
777
778
779
780inline const class File& NativeAbsoluteAtomV1::file() const {
781 return *_file;
782}
783
784inline llvm::StringRef NativeAbsoluteAtomV1::name() const {
785 return _file->string(_ivarData->nameOffset);
786}
787
788
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000789inline const Atom* NativeReferenceV1::target() const {
790 return _file->target(_ivarData->targetIndex);
791}
792
793inline Reference::Addend NativeReferenceV1::addend() const {
794 return _file->addend(_ivarData->addendIndex);
795}
796
Nick Kledzikf4e2c732012-03-15 23:36:24 +0000797inline void NativeReferenceV1::setKind(Kind k) {
798 this->cloneIvarData();
799 const_cast<NativeReferenceIvarsV1*>(_ivarData)->kind = k;
800}
801
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000802inline void NativeReferenceV1::setTarget(const Atom* newAtom) {
803 return _file->setTarget(_ivarData->targetIndex, newAtom);
804}
Nick Kledzik23384e82012-02-07 02:59:54 +0000805
806
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000807//
808// Instantiate an lld::File from the given native object file buffer
809//
Nick Kledzik23384e82012-02-07 02:59:54 +0000810llvm::error_code parseNativeObjectFile(llvm::OwningPtr<llvm::MemoryBuffer>& mb,
811 llvm::StringRef path,
812 llvm::OwningPtr<File>& result) {
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000813 return NativeFile::make(mb, path, result);
814}
815
816
817
818//
819// Instantiate an lld::File from the given native object file path
820//
821llvm::error_code parseNativeObjectFileOrSTDIN(llvm::StringRef path,
Nick Kledzik23384e82012-02-07 02:59:54 +0000822 llvm::OwningPtr<File>& result) {
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000823 llvm::OwningPtr<llvm::MemoryBuffer> mb;
824 llvm::error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(path, mb);
825 if ( ec )
826 return ec;
827
Nick Kledzik23384e82012-02-07 02:59:54 +0000828 return parseNativeObjectFile(mb, path, result);
Nick Kledzik55fd6be2012-01-16 22:03:44 +0000829}
830
831
832
833} // namespace lld