1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
25 #ifndef __LINKEDIT_CLASSIC_HPP__
26 #define __LINKEDIT_CLASSIC_HPP__
29 #include <sys/types.h>
38 #include "Architectures.hpp"
39 #include "MachOFileAbstraction.hpp"
46 class ClassicLinkEditAtom : public ld::Atom
50 // overrides of ld::Atom
51 virtual ld::File* file() const { return NULL; }
52 virtual bool translationUnitSource(const char** dir, const char** nm) const
54 virtual uint64_t objectAddress() const { return 0; }
56 virtual void encode() = 0;
57 virtual bool hasStabs(uint32_t& ssos, uint32_t& ssoe, uint32_t& sos, uint32_t& soe) { return false; }
59 ClassicLinkEditAtom(const Options& opts, ld::Internal& state,
60 OutputFile& writer, const ld::Section& sect,
61 unsigned int pointerSize)
62 : ld::Atom(sect, ld::Atom::definitionRegular,
63 ld::Atom::combineNever, ld::Atom::scopeTranslationUnit,
64 ld::Atom::typeUnclassified, ld::Atom::symbolTableNotIn,
65 false, false, false, ld::Atom::Alignment(log2(pointerSize))),
66 _options(opts), _state(state), _writer(writer) { }
68 const Options& _options;
75 class StringPoolAtom : public ClassicLinkEditAtom
78 StringPoolAtom(const Options& opts, ld::Internal& state,
79 OutputFile& writer, int pointerSize);
81 // overrides of ld::Atom
82 virtual const char* name() const { return "string pool"; }
83 virtual uint64_t size() const;
84 virtual void copyRawContent(uint8_t buffer[]) const;
85 // overrides of ClassicLinkEditAtom
86 virtual void encode() { }
88 int32_t add(const char* name);
89 int32_t addUnique(const char* name);
90 int32_t emptyString() { return 1; }
91 const char* stringForIndex(int32_t) const;
92 uint32_t currentOffset();
98 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
100 enum { kBufferSize = 0x01000000 };
101 typedef __gnu_cxx::hash_map<const char*, int32_t, __gnu_cxx::hash<const char*>, CStringEquals> StringToOffset;
103 const uint32_t _pointerSize;
104 std::vector<char*> _fullBuffers;
105 char* _currentBuffer;
106 uint32_t _currentBufferUsed;
107 StringToOffset _uniqueStrings;
109 static ld::Section _s_section;
112 ld::Section StringPoolAtom::_s_section("__LINKEDIT", "__string_pool", ld::Section::typeLinkEdit, true);
115 StringPoolAtom::StringPoolAtom(const Options& opts, ld::Internal& state, OutputFile& writer, int pointerSize)
116 : ClassicLinkEditAtom(opts, state, writer, _s_section, pointerSize),
117 _pointerSize(pointerSize), _currentBuffer(NULL), _currentBufferUsed(0)
119 _currentBuffer = new char[kBufferSize];
120 // burn first byte of string pool (so zero is never a valid string offset)
121 _currentBuffer[_currentBufferUsed++] = ' ';
122 // make offset 1 always point to an empty string
123 _currentBuffer[_currentBufferUsed++] = '\0';
126 uint64_t StringPoolAtom::size() const
128 // pointer size align size
129 return (kBufferSize * _fullBuffers.size() + _currentBufferUsed + _pointerSize-1) & (-_pointerSize);
132 void StringPoolAtom::copyRawContent(uint8_t buffer[]) const
135 for (unsigned int i=0; i < _fullBuffers.size(); ++i) {
136 memcpy(&buffer[offset], _fullBuffers[i], kBufferSize);
137 offset += kBufferSize;
139 memcpy(&buffer[offset], _currentBuffer, _currentBufferUsed);
140 // zero fill end to align
141 offset += _currentBufferUsed;
142 while ( (offset % _pointerSize) != 0 )
143 buffer[offset++] = 0;
146 int32_t StringPoolAtom::add(const char* str)
148 int32_t offset = kBufferSize * _fullBuffers.size() + _currentBufferUsed;
149 int lenNeeded = strlcpy(&_currentBuffer[_currentBufferUsed], str, kBufferSize-_currentBufferUsed)+1;
150 if ( (_currentBufferUsed+lenNeeded) < kBufferSize ) {
151 _currentBufferUsed += lenNeeded;
154 int copied = kBufferSize-_currentBufferUsed-1;
155 // change trailing '\0' that strlcpy added to real char
156 _currentBuffer[kBufferSize-1] = str[copied];
158 _fullBuffers.push_back(_currentBuffer);
159 _currentBuffer = new char[kBufferSize];
160 _currentBufferUsed = 0;
161 // append rest of string
162 this->add(&str[copied+1]);
167 uint32_t StringPoolAtom::currentOffset()
169 return kBufferSize * _fullBuffers.size() + _currentBufferUsed;
173 int32_t StringPoolAtom::addUnique(const char* str)
175 StringToOffset::iterator pos = _uniqueStrings.find(str);
176 if ( pos != _uniqueStrings.end() ) {
180 int32_t offset = this->add(str);
181 _uniqueStrings[str] = offset;
187 const char* StringPoolAtom::stringForIndex(int32_t index) const
189 int32_t currentBufferStartIndex = kBufferSize * _fullBuffers.size();
190 int32_t maxIndex = currentBufferStartIndex + _currentBufferUsed;
191 // check for out of bounds
192 if ( index > maxIndex )
194 // check for index in _currentBuffer
195 if ( index > currentBufferStartIndex )
196 return &_currentBuffer[index-currentBufferStartIndex];
197 // otherwise index is in a full buffer
198 uint32_t fullBufferIndex = index/kBufferSize;
199 return &_fullBuffers[fullBufferIndex][index-(kBufferSize*fullBufferIndex)];
204 template <typename A>
205 class SymbolTableAtom : public ClassicLinkEditAtom
208 SymbolTableAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
209 : ClassicLinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)),
210 _stabsStringsOffsetStart(0), _stabsStringsOffsetEnd(0),
211 _stabsIndexStart(0), _stabsIndexEnd(0) { }
213 // overrides of ld::Atom
214 virtual const char* name() const { return "symbol table"; }
215 virtual uint64_t size() const;
216 virtual void copyRawContent(uint8_t buffer[]) const;
217 // overrides of ClassicLinkEditAtom
218 virtual void encode();
219 virtual bool hasStabs(uint32_t& ssos, uint32_t& ssoe, uint32_t& sos, uint32_t& soe);
222 typedef typename A::P P;
223 typedef typename A::P::E E;
224 typedef typename A::P::uint_t pint_t;
226 bool addLocal(const ld::Atom* atom, StringPoolAtom* pool);
227 void addGlobal(const ld::Atom* atom, StringPoolAtom* pool);
228 void addImport(const ld::Atom* atom, StringPoolAtom* pool);
229 uint8_t classicOrdinalForProxy(const ld::Atom* atom);
230 uint32_t stringOffsetForStab(const ld::relocatable::File::Stab& stab, StringPoolAtom* pool);
231 uint64_t valueForStab(const ld::relocatable::File::Stab& stab);
232 uint8_t sectionIndexForStab(const ld::relocatable::File::Stab& stab);
235 mutable std::vector<macho_nlist<P> > _globals;
236 mutable std::vector<macho_nlist<P> > _locals;
237 mutable std::vector<macho_nlist<P> > _imports;
239 uint32_t _stabsStringsOffsetStart;
240 uint32_t _stabsStringsOffsetEnd;
241 uint32_t _stabsIndexStart;
242 uint32_t _stabsIndexEnd;
244 static ld::Section _s_section;
247 template <typename A>
248 ld::Section SymbolTableAtom<A>::_s_section("__LINKEDIT", "__symbol_table", ld::Section::typeLinkEdit, true);
252 template <typename A>
253 bool SymbolTableAtom<A>::addLocal(const ld::Atom* atom, StringPoolAtom* pool)
255 macho_nlist<P> entry;
256 static int s_anonNameIndex = 1;
257 assert(atom->symbolTableInclusion() != ld::Atom::symbolTableNotIn);
260 const char* symbolName = atom->name();
262 if ( this->_options.outputKind() == Options::kObjectFile ) {
263 if ( atom->contentType() == ld::Atom::typeCString ) {
264 if ( atom->combine() == ld::Atom::combineByNameAndContent ) {
265 // don't use 'l' labels for x86_64 strings
266 // <rdar://problem/6605499> x86_64 obj-c runtime confused when static lib is stripped
267 sprintf(anonName, "LC%u", s_anonNameIndex++);
268 symbolName = anonName;
271 else if ( atom->contentType() == ld::Atom::typeCFI ) {
272 if ( _options.removeEHLabels() )
274 // synthesize .eh name
275 if ( strcmp(atom->name(), "CIE") == 0 )
276 symbolName = "EH_Frame1";
278 symbolName = "func.eh";
280 else if ( atom->symbolTableInclusion() == ld::Atom::symbolTableInWithRandomAutoStripLabel ) {
281 // make auto-strip anonymous name for symbol
282 sprintf(anonName, "l%03u", s_anonNameIndex++);
283 symbolName = anonName;
286 entry.set_n_strx(pool->add(symbolName));
289 uint8_t type = N_SECT;
290 if ( atom->definition() == ld::Atom::definitionAbsolute ) {
293 else if ( (atom->section().type() == ld::Section::typeObjC1Classes)
294 && (this->_options.outputKind() == Options::kObjectFile) ) {
295 // __OBJC __class has floating abs symbols for each class data structure
298 if ( atom->scope() == ld::Atom::scopeLinkageUnit )
300 entry.set_n_type(type);
302 // set n_sect (section number of implementation )
303 if ( atom->definition() == ld::Atom::definitionAbsolute )
306 entry.set_n_sect(atom->machoSection());
310 if ( atom->symbolTableInclusion() == ld::Atom::symbolTableInAndNeverStrip )
311 desc |= REFERENCED_DYNAMICALLY;
312 if ( atom->dontDeadStrip() && (this->_options.outputKind() == Options::kObjectFile) )
313 desc |= N_NO_DEAD_STRIP;
314 if ( (atom->definition() == ld::Atom::definitionRegular) && (atom->combine() == ld::Atom::combineByName) )
316 if ( atom->isThumb() )
317 desc |= N_ARM_THUMB_DEF;
318 entry.set_n_desc(desc);
320 // set n_value ( address this symbol will be at if this executable is loaded at it preferred address )
321 if ( atom->definition() == ld::Atom::definitionAbsolute )
322 entry.set_n_value(atom->objectAddress());
324 entry.set_n_value(atom->finalAddress());
327 _locals.push_back(entry);
332 template <typename A>
333 void SymbolTableAtom<A>::addGlobal(const ld::Atom* atom, StringPoolAtom* pool)
335 macho_nlist<P> entry;
338 entry.set_n_strx(pool->add(atom->name()));
341 if ( atom->definition() == ld::Atom::definitionAbsolute ) {
342 entry.set_n_type(N_EXT | N_ABS);
344 else if ( (atom->section().type() == ld::Section::typeObjC1Classes)
345 && (this->_options.outputKind() == Options::kObjectFile) ) {
346 // __OBJC __class has floating abs symbols for each class data structure
347 entry.set_n_type(N_EXT | N_ABS);
349 else if ( (atom->definition() == ld::Atom::definitionProxy) && (atom->scope() == ld::Atom::scopeGlobal) ) {
350 entry.set_n_type(N_EXT | N_INDR);
353 entry.set_n_type(N_EXT | N_SECT);
354 if ( (atom->scope() == ld::Atom::scopeLinkageUnit) && (this->_options.outputKind() == Options::kObjectFile) ) {
355 if ( this->_options.keepPrivateExterns() )
356 entry.set_n_type(N_EXT | N_SECT | N_PEXT);
358 else if ( (atom->symbolTableInclusion() == ld::Atom::symbolTableInAndNeverStrip)
359 && (atom->section().type() == ld::Section::typeMachHeader) ) {
360 // the __mh_execute_header is historical magic and must be an absolute symbol
361 entry.set_n_type(N_EXT | N_ABS);
365 // set n_sect (section number of implementation)
366 if ( atom->definition() == ld::Atom::definitionAbsolute )
368 else if ( (atom->definition() == ld::Atom::definitionProxy) && (atom->scope() == ld::Atom::scopeGlobal) )
371 entry.set_n_sect(atom->machoSection());
375 if ( atom->isThumb() )
376 desc |= N_ARM_THUMB_DEF;
377 if ( atom->symbolTableInclusion() == ld::Atom::symbolTableInAndNeverStrip )
378 desc |= REFERENCED_DYNAMICALLY;
379 if ( (atom->contentType() == ld::Atom::typeResolver) && (this->_options.outputKind() == Options::kObjectFile) )
380 desc |= N_SYMBOL_RESOLVER;
381 if ( atom->dontDeadStrip() && (this->_options.outputKind() == Options::kObjectFile) )
382 desc |= N_NO_DEAD_STRIP;
383 if ( (atom->definition() == ld::Atom::definitionRegular) && (atom->combine() == ld::Atom::combineByName) ) {
385 // <rdar://problem/6783167> support auto hidden weak symbols: .weak_def_can_be_hidden
386 if ( (atom->scope() == ld::Atom::scopeGlobal) && atom->autoHide() && (this->_options.outputKind() == Options::kObjectFile) )
389 entry.set_n_desc(desc);
391 // set n_value ( address this symbol will be at if this executable is loaded at it preferred address )
392 if ( atom->definition() == ld::Atom::definitionAbsolute )
393 entry.set_n_value(atom->objectAddress());
394 else if ( (atom->definition() == ld::Atom::definitionProxy) && (atom->scope() == ld::Atom::scopeGlobal) ) {
395 if ( atom->isAlias() ) {
396 // this re-export also renames
397 for (ld::Fixup::iterator fit = atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
398 if ( fit->kind == ld::Fixup::kindNoneFollowOn ) {
399 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
400 entry.set_n_value(pool->add(fit->u.target->name()));
405 entry.set_n_value(entry.n_strx());
408 entry.set_n_value(atom->finalAddress());
411 _globals.push_back(entry);
414 template <typename A>
415 uint8_t SymbolTableAtom<A>::classicOrdinalForProxy(const ld::Atom* atom)
417 assert(atom->definition() == ld::Atom::definitionProxy);
418 // when linking for flat-namespace ordinals are always zero
419 if ( _options.nameSpace() != Options::kTwoLevelNameSpace )
421 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(atom->file());
422 // when linking -undefined dynamic_lookup, unbound symbols use DYNAMIC_LOOKUP_ORDINAL
423 if ( dylib == NULL ) {
424 if (_options.undefinedTreatment() == Options::kUndefinedDynamicLookup )
425 return DYNAMIC_LOOKUP_ORDINAL;
426 if (_options.allowedUndefined(atom->name()) )
427 return DYNAMIC_LOOKUP_ORDINAL;
429 assert(dylib != NULL);
430 int ord = this->_writer.dylibToOrdinal(dylib);
431 if ( ord == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE )
432 return EXECUTABLE_ORDINAL;
437 template <typename A>
438 void SymbolTableAtom<A>::addImport(const ld::Atom* atom, StringPoolAtom* pool)
440 macho_nlist<P> entry;
443 entry.set_n_strx(pool->add(atom->name()));
446 if ( this->_options.outputKind() == Options::kObjectFile ) {
447 if ( (atom->scope() == ld::Atom::scopeLinkageUnit)
448 && (atom->definition() == ld::Atom::definitionTentative) )
449 entry.set_n_type(N_UNDF | N_EXT | N_PEXT);
451 entry.set_n_type(N_UNDF | N_EXT);
454 if ( this->_options.prebind() )
455 entry.set_n_type(N_PBUD | N_EXT);
457 entry.set_n_type(N_UNDF | N_EXT);
464 if ( this->_options.outputKind() != Options::kObjectFile ) {
465 uint8_t ordinal = this->classicOrdinalForProxy(atom);
466 //fprintf(stderr, "ordinal=%u from reader=%p for symbol=%s\n", ordinal, atom->getFile(), atom->getName());
467 SET_LIBRARY_ORDINAL(desc, ordinal);
470 // set n_desc ( high byte is library ordinal, low byte is reference type )
471 std::map<const ObjectFile::Atom*,ObjectFile::Atom*>::iterator pos = fStubsMap.find(atom);
472 if ( pos != fStubsMap.end() || ( strncmp(atom->getName(), ".objc_class_name_", 17) == 0) )
473 desc |= REFERENCE_FLAG_UNDEFINED_LAZY;
475 desc |= REFERENCE_FLAG_UNDEFINED_NON_LAZY;
478 else if ( atom->definition() == ld::Atom::definitionTentative ) {
479 uint8_t align = atom->alignment().powerOf2;
480 // always record custom alignment of common symbols to match what compiler does
481 SET_COMM_ALIGN(desc, align);
483 if ( (this->_options.outputKind() != Options::kObjectFile)
484 && (atom->definition() == ld::Atom::definitionProxy)
485 && (atom->combine() == ld::Atom::combineByName) ) {
486 desc |= N_REF_TO_WEAK;
488 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(atom->file());
489 if ( atom->weakImported() || ((dylib != NULL) && dylib->forcedWeakLinked()) )
491 entry.set_n_desc(desc);
493 // set n_value, zero for import proxy and size for tentative definition
494 if ( atom->definition() == ld::Atom::definitionTentative )
495 entry.set_n_value(atom->size());
497 entry.set_n_value(0);
500 _imports.push_back(entry);
503 template <typename A>
504 uint8_t SymbolTableAtom<A>::sectionIndexForStab(const ld::relocatable::File::Stab& stab)
506 // in FUN stabs, n_sect field is 0 for start FUN and 1 for end FUN
507 if ( stab.type == N_FUN )
509 else if ( stab.type == N_GSYM )
511 else if ( stab.atom != NULL )
512 return stab.atom->machoSection();
518 template <typename A>
519 uint64_t SymbolTableAtom<A>::valueForStab(const ld::relocatable::File::Stab& stab)
521 switch ( stab.type ) {
523 if ( stab.atom == NULL ) {
524 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
527 if ( (stab.string == NULL) || (strlen(stab.string) == 0) ) {
528 // end of function N_FUN has size
529 return stab.atom->size();
532 // start of function N_FUN has address
533 return stab.atom->finalAddress();
538 if ( stab.atom == NULL )
539 // some weird assembly files have slines not associated with a function
542 // all these stab types need their value changed from an offset in the atom to an address
543 return stab.atom->finalAddress() + stab.value;
547 // all these need address of atom
548 if ( stab.atom != NULL )
549 return stab.atom->finalAddress();
551 return 0; // <rdar://problem/7811357> work around for mismatch N_BNSYM
553 return stab.atom->size();
555 if ( stab.atom == NULL ) {
559 if ( (stab.string == NULL) || (strlen(stab.string) == 0) ) {
560 // end of translation unit N_SO has address of end of last atom
561 return stab.atom->finalAddress() + stab.atom->size();
564 // start of translation unit N_SO has address of end of first atom
565 return stab.atom->finalAddress();
574 template <typename A>
575 uint32_t SymbolTableAtom<A>::stringOffsetForStab(const ld::relocatable::File::Stab& stab, StringPoolAtom* pool)
579 if ( (stab.string == NULL) || stab.string[0] == '\0' ) {
580 return pool->emptyString();
583 // fall into uniquing case
587 return pool->addUnique(stab.string);
590 if ( stab.string == NULL )
592 else if ( stab.string[0] == '\0' )
593 return pool->emptyString();
595 return pool->add(stab.string);
602 template <typename A>
603 bool SymbolTableAtom<A>::hasStabs(uint32_t& ssos, uint32_t& ssoe, uint32_t& sos, uint32_t& soe)
605 ssos = _stabsStringsOffsetStart;
606 ssoe = _stabsStringsOffsetEnd;
607 sos = _stabsIndexStart * sizeof(macho_nlist<P>);
608 soe = _stabsIndexEnd * sizeof(macho_nlist<P>);
609 return ( (_stabsIndexStart != _stabsIndexEnd) || (_stabsStringsOffsetStart != _stabsStringsOffsetEnd) );
612 template <typename A>
613 void SymbolTableAtom<A>::encode()
615 uint32_t symbolIndex = 0;
617 // make nlist entries for all local symbols
618 std::vector<const ld::Atom*>& localAtoms = this->_writer._localAtoms;
619 _locals.reserve(localAtoms.size()+this->_state.stabs.size());
620 this->_writer._localSymbolsStartIndex = 0;
621 // make nlist entries for all debug notes
622 _stabsIndexStart = symbolIndex;
623 _stabsStringsOffsetStart = this->_writer._stringPoolAtom->currentOffset();
624 for (std::vector<ld::relocatable::File::Stab>::const_iterator sit=this->_state.stabs.begin(); sit != this->_state.stabs.end(); ++sit) {
625 macho_nlist<P> entry;
626 entry.set_n_type(sit->type);
627 entry.set_n_sect(sectionIndexForStab(*sit));
628 entry.set_n_desc(sit->desc);
629 entry.set_n_value(valueForStab(*sit));
630 entry.set_n_strx(stringOffsetForStab(*sit, this->_writer._stringPoolAtom));
631 _locals.push_back(entry);
634 _stabsIndexEnd = symbolIndex;
635 _stabsStringsOffsetEnd = this->_writer._stringPoolAtom->currentOffset();
636 for (std::vector<const ld::Atom*>::const_iterator it=localAtoms.begin(); it != localAtoms.end(); ++it) {
637 const ld::Atom* atom = *it;
638 if ( this->addLocal(atom, this->_writer._stringPoolAtom) )
639 this->_writer._atomToSymbolIndex[atom] = symbolIndex++;
641 this->_writer._localSymbolsCount = symbolIndex;
644 // make nlist entries for all global symbols
645 std::vector<const ld::Atom*>& globalAtoms = this->_writer._exportedAtoms;
646 _globals.reserve(globalAtoms.size());
647 this->_writer._globalSymbolsStartIndex = symbolIndex;
648 for (std::vector<const ld::Atom*>::const_iterator it=globalAtoms.begin(); it != globalAtoms.end(); ++it) {
649 const ld::Atom* atom = *it;
650 this->addGlobal(atom, this->_writer._stringPoolAtom);
651 this->_writer._atomToSymbolIndex[atom] = symbolIndex++;
653 this->_writer._globalSymbolsCount = symbolIndex - this->_writer._globalSymbolsStartIndex;
655 // make nlist entries for all undefined (imported) symbols
656 std::vector<const ld::Atom*>& importAtoms = this->_writer._importedAtoms;
657 _imports.reserve(importAtoms.size());
658 this->_writer._importSymbolsStartIndex = symbolIndex;
659 for (std::vector<const ld::Atom*>::const_iterator it=importAtoms.begin(); it != importAtoms.end(); ++it) {
660 this->addImport(*it, this->_writer._stringPoolAtom);
661 this->_writer._atomToSymbolIndex[*it] = symbolIndex++;
663 this->_writer._importSymbolsCount = symbolIndex - this->_writer._importSymbolsStartIndex;
666 template <typename A>
667 uint64_t SymbolTableAtom<A>::size() const
669 return sizeof(macho_nlist<P>) * (_locals.size() + _globals.size() + _imports.size());
672 template <typename A>
673 void SymbolTableAtom<A>::copyRawContent(uint8_t buffer[]) const
675 memcpy(&buffer[this->_writer._localSymbolsStartIndex*sizeof(macho_nlist<P>)], &_locals[0],
676 this->_writer._localSymbolsCount*sizeof(macho_nlist<P>));
677 memcpy(&buffer[this->_writer._globalSymbolsStartIndex*sizeof(macho_nlist<P>)], &_globals[0],
678 this->_writer._globalSymbolsCount*sizeof(macho_nlist<P>));
679 memcpy(&buffer[this->_writer._importSymbolsStartIndex *sizeof(macho_nlist<P>)], &_imports[0],
680 this->_writer._importSymbolsCount*sizeof(macho_nlist<P>));
686 class RelocationsAtomAbstract : public ClassicLinkEditAtom
689 RelocationsAtomAbstract(const Options& opts, ld::Internal& state,
690 OutputFile& writer, const ld::Section& sect,
691 unsigned int pointerSize)
692 : ClassicLinkEditAtom(opts, state, writer, sect, pointerSize) { }
694 virtual void addPointerReloc(uint64_t addr, uint32_t symNum) = 0;
695 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum) = 0;
696 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*) = 0;
697 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*) = 0;
698 virtual uint64_t relocBaseAddress(ld::Internal& state) = 0;
699 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
700 const ld::Atom* inAtom, uint32_t offsetInAtom,
701 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
702 const ld::Atom* toTarget, uint64_t toAddend,
703 const ld::Atom* fromTarget, uint64_t fromAddend) = 0;
705 uint32_t symbolIndex(const ld::Atom* atom) const;
711 uint32_t RelocationsAtomAbstract::symbolIndex(const ld::Atom* atom) const
713 std::map<const ld::Atom*, uint32_t>::iterator pos = this->_writer._atomToSymbolIndex.find(atom);
714 if ( pos != this->_writer._atomToSymbolIndex.end() )
716 fprintf(stderr, "_atomToSymbolIndex content:\n");
717 for(std::map<const ld::Atom*, uint32_t>::iterator it = this->_writer._atomToSymbolIndex.begin(); it != this->_writer._atomToSymbolIndex.end(); ++it) {
718 fprintf(stderr, "%p(%s) => %d\n", it->first, it->first->name(), it->second);
720 throwf("internal error: atom not found in symbolIndex(%s)", atom->name());
724 template <typename A>
725 class LocalRelocationsAtom : public RelocationsAtomAbstract
728 LocalRelocationsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
729 : RelocationsAtomAbstract(opts, state, writer, _s_section, sizeof(pint_t)) { }
731 // overrides of ld::Atom
732 virtual const char* name() const { return "local relocations"; }
733 virtual uint64_t size() const;
734 virtual void copyRawContent(uint8_t buffer[]) const;
735 // overrides of ClassicLinkEditAtom
736 virtual void encode() {}
737 // overrides of RelocationsAtomAbstract
738 virtual void addPointerReloc(uint64_t addr, uint32_t symNum);
739 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*) {}
740 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*) {}
741 virtual uint64_t relocBaseAddress(ld::Internal& state);
742 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum);
743 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
744 const ld::Atom* inAtom, uint32_t offsetInAtom,
745 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
746 const ld::Atom* toTarget, uint64_t toAddend,
747 const ld::Atom* fromTarget, uint64_t fromAddend) { }
750 typedef typename A::P P;
751 typedef typename A::P::E E;
752 typedef typename A::P::uint_t pint_t;
754 std::vector<macho_relocation_info<P> > _relocs;
756 static ld::Section _s_section;
759 template <typename A>
760 ld::Section LocalRelocationsAtom<A>::_s_section("__LINKEDIT", "__local_relocs", ld::Section::typeLinkEdit, true);
764 uint64_t LocalRelocationsAtom<x86_64>::relocBaseAddress(ld::Internal& state)
766 if ( _options.outputKind() == Options::kKextBundle ) {
767 // for kext bundles the reloc base address starts at __TEXT segment
768 return _options.baseAddress();
770 // for all other kinds, the x86_64 reloc base address starts at __DATA segment
771 for (std::vector<ld::Internal::FinalSection*>::iterator sit = state.sections.begin(); sit != state.sections.end(); ++sit) {
772 ld::Internal::FinalSection* sect = *sit;
773 if ( strcmp(sect->segmentName(), "__DATA") == 0 )
774 return sect->address;
776 throw "__DATA segment not found";
779 template <typename A>
780 uint64_t LocalRelocationsAtom<A>::relocBaseAddress(ld::Internal& state)
782 return _options.baseAddress();
785 template <typename A>
786 void LocalRelocationsAtom<A>::addPointerReloc(uint64_t addr, uint32_t symNum)
788 macho_relocation_info<P> reloc;
789 reloc.set_r_address(addr);
790 reloc.set_r_symbolnum(symNum);
791 reloc.set_r_pcrel(false);
792 reloc.set_r_length();
793 reloc.set_r_extern(false);
794 reloc.set_r_type(GENERIC_RELOC_VANILLA);
795 _relocs.push_back(reloc);
798 template <typename A>
799 void LocalRelocationsAtom<A>::addTextReloc(uint64_t addr, ld::Fixup::Kind kind, uint64_t targetAddr, uint32_t symNum)
804 template <typename A>
805 uint64_t LocalRelocationsAtom<A>::size() const
807 return _relocs.size() * sizeof(macho_relocation_info<P>);
810 template <typename A>
811 void LocalRelocationsAtom<A>::copyRawContent(uint8_t buffer[]) const
813 memcpy(buffer, &_relocs[0], _relocs.size()*sizeof(macho_relocation_info<P>));
821 template <typename A>
822 class ExternalRelocationsAtom : public RelocationsAtomAbstract
825 ExternalRelocationsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
826 : RelocationsAtomAbstract(opts, state, writer, _s_section, sizeof(pint_t)) { }
828 // overrides of ld::Atom
829 virtual const char* name() const { return "external relocations"; }
830 virtual uint64_t size() const;
831 virtual void copyRawContent(uint8_t buffer[]) const;
832 // overrides of ClassicLinkEditAtom
833 virtual void encode() {}
834 // overrides of RelocationsAtomAbstract
835 virtual void addPointerReloc(uint64_t addr, uint32_t symNum) {}
836 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum) {}
837 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*);
838 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*);
839 virtual uint64_t relocBaseAddress(ld::Internal& state);
840 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
841 const ld::Atom* inAtom, uint32_t offsetInAtom,
842 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
843 const ld::Atom* toTarget, uint64_t toAddend,
844 const ld::Atom* fromTarget, uint64_t fromAddend) { }
848 typedef typename A::P P;
849 typedef typename A::P::E E;
850 typedef typename A::P::uint_t pint_t;
853 LocAndAtom(uint64_t l, const ld::Atom* a) : loc(l), atom(a), symbolIndex(0) {}
856 const ld::Atom* atom;
857 uint32_t symbolIndex;
859 bool operator<(const LocAndAtom& rhs) const {
860 // sort first by symbol number
861 if ( this->symbolIndex != rhs.symbolIndex )
862 return (this->symbolIndex < rhs.symbolIndex);
863 // then sort all uses of the same symbol by address
864 return (this->loc < rhs.loc);
869 static uint32_t pointerReloc();
870 static uint32_t callReloc();
872 mutable std::vector<LocAndAtom> _pointerLocations;
873 mutable std::vector<LocAndAtom> _callSiteLocations;
875 static ld::Section _s_section;
878 template <typename A>
879 ld::Section ExternalRelocationsAtom<A>::_s_section("__LINKEDIT", "__extrn_relocs", ld::Section::typeLinkEdit, true);
882 uint64_t ExternalRelocationsAtom<x86_64>::relocBaseAddress(ld::Internal& state)
884 // for x86_64 the reloc base address starts at __DATA segment
885 for (std::vector<ld::Internal::FinalSection*>::iterator sit = state.sections.begin(); sit != state.sections.end(); ++sit) {
886 ld::Internal::FinalSection* sect = *sit;
887 if ( strcmp(sect->segmentName(), "__DATA") == 0 )
888 return sect->address;
890 throw "__DATA segment not found";
893 template <typename A>
894 uint64_t ExternalRelocationsAtom<A>::relocBaseAddress(ld::Internal& state)
899 template <typename A>
900 void ExternalRelocationsAtom<A>::addExternalPointerReloc(uint64_t addr, const ld::Atom* target)
902 _pointerLocations.push_back(LocAndAtom(addr, target));
905 template <typename A>
906 void ExternalRelocationsAtom<A>::addExternalCallSiteReloc(uint64_t addr, const ld::Atom* target)
908 _callSiteLocations.push_back(LocAndAtom(addr, target));
912 template <typename A>
913 uint64_t ExternalRelocationsAtom<A>::size() const
915 if ( _options.outputKind() == Options::kStaticExecutable ) {
916 assert(_pointerLocations.size() == 0);
917 assert(_callSiteLocations.size() == 0);
919 return (_pointerLocations.size() + _callSiteLocations.size()) * sizeof(macho_relocation_info<P>);
922 template <> uint32_t ExternalRelocationsAtom<arm>::pointerReloc() { return ARM_RELOC_VANILLA; }
923 template <> uint32_t ExternalRelocationsAtom<x86>::pointerReloc() { return GENERIC_RELOC_VANILLA; }
924 template <> uint32_t ExternalRelocationsAtom<x86_64>::pointerReloc() { return X86_64_RELOC_UNSIGNED; }
927 template <> uint32_t ExternalRelocationsAtom<x86_64>::callReloc() { return X86_64_RELOC_BRANCH; }
928 template <> uint32_t ExternalRelocationsAtom<x86>::callReloc() { return GENERIC_RELOC_VANILLA; }
929 template <typename A>
930 uint32_t ExternalRelocationsAtom<A>::callReloc()
932 assert(0 && "external call relocs not implemented");
937 template <typename A>
938 void ExternalRelocationsAtom<A>::copyRawContent(uint8_t buffer[]) const
940 macho_relocation_info<P>* r = (macho_relocation_info<P>*)buffer;
942 // assign symbol index, now that symbol table is built
943 for (typename std::vector<LocAndAtom>::iterator it = _pointerLocations.begin(); it != _pointerLocations.end(); ++it) {
944 it->symbolIndex = symbolIndex(it->atom);
946 std::sort(_pointerLocations.begin(), _pointerLocations.end());
947 for (typename std::vector<LocAndAtom>::const_iterator it = _pointerLocations.begin(); it != _pointerLocations.end(); ++it, ++r) {
948 r->set_r_address(it->loc);
949 r->set_r_symbolnum(it->symbolIndex);
950 r->set_r_pcrel(false);
952 r->set_r_extern(true);
953 r->set_r_type(this->pointerReloc());
956 for (typename std::vector<LocAndAtom>::iterator it = _callSiteLocations.begin(); it != _callSiteLocations.end(); ++it) {
957 it->symbolIndex = symbolIndex(it->atom);
959 std::sort(_callSiteLocations.begin(), _callSiteLocations.end());
960 for (typename std::vector<LocAndAtom>::const_iterator it = _callSiteLocations.begin(); it != _callSiteLocations.end(); ++it, ++r) {
961 r->set_r_address(it->loc);
962 r->set_r_symbolnum(it->symbolIndex);
963 r->set_r_pcrel(true);
965 r->set_r_extern(true);
966 r->set_r_type(this->callReloc());
971 template <typename A>
972 class SectionRelocationsAtom : public RelocationsAtomAbstract
975 SectionRelocationsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
976 : RelocationsAtomAbstract(opts, state, writer, _s_section, sizeof(pint_t)) { }
978 // overrides of ld::Atom
979 virtual const char* name() const { return "section relocations"; }
980 virtual uint64_t size() const;
981 virtual void copyRawContent(uint8_t buffer[]) const;
982 // overrides of ClassicLinkEditAtom
983 virtual void encode();
984 // overrides of RelocationsAtomAbstract
985 virtual void addPointerReloc(uint64_t addr, uint32_t symNum) {}
986 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum) {}
987 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*) {}
988 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*) {}
989 virtual uint64_t relocBaseAddress(ld::Internal& state) { return 0; }
990 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
991 const ld::Atom* inAtom, uint32_t offsetInAtom,
992 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
993 const ld::Atom* toTarget, uint64_t toAddend,
994 const ld::Atom* fromTarget, uint64_t fromAddend);
997 typedef typename A::P P;
998 typedef typename A::P::E E;
999 typedef typename A::P::uint_t pint_t;
1003 ld::Fixup::Kind kind;
1004 bool toTargetUsesExternalReloc;
1005 bool fromTargetUsesExternalReloc;
1006 const ld::Atom* inAtom;
1007 uint32_t offsetInAtom;
1008 const ld::Atom* toTarget;
1010 const ld::Atom* fromTarget;
1011 uint64_t fromAddend;
1013 uint32_t sectSymNum(bool external, const ld::Atom* target);
1014 void encodeSectionReloc(ld::Internal::FinalSection* sect,
1015 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs);
1017 struct SectionAndEntries {
1018 ld::Internal::FinalSection* sect;
1019 std::vector<Entry> entries;
1020 std::vector<macho_relocation_info<P> > relocs;
1023 std::vector<SectionAndEntries> _entriesBySection;
1025 static ld::Section _s_section;
1028 template <typename A>
1029 ld::Section SectionRelocationsAtom<A>::_s_section("__LINKEDIT", "__sect_relocs", ld::Section::typeLinkEdit, true);
1034 template <typename A>
1035 uint64_t SectionRelocationsAtom<A>::size() const
1038 for(typename std::vector<SectionAndEntries>::const_iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
1039 const SectionAndEntries& se = *it;
1040 count += se.relocs.size();
1042 return count * sizeof(macho_relocation_info<P>);
1045 template <typename A>
1046 void SectionRelocationsAtom<A>::copyRawContent(uint8_t buffer[]) const
1048 uint32_t offset = 0;
1049 for(typename std::vector<SectionAndEntries>::const_iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
1050 const SectionAndEntries& se = *it;
1051 memcpy(&buffer[offset], &se.relocs[0], se.relocs.size()*sizeof(macho_relocation_info<P>));
1052 offset += (se.relocs.size() * sizeof(macho_relocation_info<P>));
1058 void SectionRelocationsAtom<x86_64>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1059 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1061 macho_relocation_info<P> reloc1;
1062 macho_relocation_info<P> reloc2;
1063 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1064 bool external = entry.toTargetUsesExternalReloc;
1065 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1066 bool fromExternal = false;
1067 uint32_t fromSymbolNum = 0;
1068 if ( entry.fromTarget != NULL ) {
1069 fromExternal = entry.fromTargetUsesExternalReloc;
1070 fromSymbolNum = sectSymNum(fromExternal, entry.fromTarget);
1074 switch ( entry.kind ) {
1075 case ld::Fixup::kindStoreX86BranchPCRel32:
1076 case ld::Fixup::kindStoreTargetAddressX86BranchPCRel32:
1077 case ld::Fixup::kindStoreX86DtraceCallSiteNop:
1078 case ld::Fixup::kindStoreX86DtraceIsEnableSiteClear:
1079 reloc1.set_r_address(address);
1080 reloc1.set_r_symbolnum(symbolNum);
1081 reloc1.set_r_pcrel(true);
1082 reloc1.set_r_length(2);
1083 reloc1.set_r_extern(external);
1084 reloc1.set_r_type(X86_64_RELOC_BRANCH);
1085 relocs.push_back(reloc1);
1088 case ld::Fixup::kindStoreX86BranchPCRel8:
1089 reloc1.set_r_address(address);
1090 reloc1.set_r_symbolnum(symbolNum);
1091 reloc1.set_r_pcrel(true);
1092 reloc1.set_r_length(0);
1093 reloc1.set_r_extern(external);
1094 reloc1.set_r_type(X86_64_RELOC_BRANCH);
1095 relocs.push_back(reloc1);
1098 case ld::Fixup::kindStoreX86PCRel32:
1099 case ld::Fixup::kindStoreTargetAddressX86PCRel32:
1100 reloc1.set_r_address(address);
1101 reloc1.set_r_symbolnum(symbolNum);
1102 reloc1.set_r_pcrel(true);
1103 reloc1.set_r_length(2);
1104 reloc1.set_r_extern(external);
1105 reloc1.set_r_type(X86_64_RELOC_SIGNED);
1106 relocs.push_back(reloc1);
1109 case ld::Fixup::kindStoreX86PCRel32_1:
1110 reloc1.set_r_address(address);
1111 reloc1.set_r_symbolnum(symbolNum);
1112 reloc1.set_r_pcrel(true);
1113 reloc1.set_r_length(2);
1114 reloc1.set_r_extern(external);
1115 reloc1.set_r_type(X86_64_RELOC_SIGNED_1);
1116 relocs.push_back(reloc1);
1119 case ld::Fixup::kindStoreX86PCRel32_2:
1120 reloc1.set_r_address(address);
1121 reloc1.set_r_symbolnum(symbolNum);
1122 reloc1.set_r_pcrel(true);
1123 reloc1.set_r_length(2);
1124 reloc1.set_r_extern(external);
1125 reloc1.set_r_type(X86_64_RELOC_SIGNED_2);
1126 relocs.push_back(reloc1);
1129 case ld::Fixup::kindStoreX86PCRel32_4:
1130 reloc1.set_r_address(address);
1131 reloc1.set_r_symbolnum(symbolNum);
1132 reloc1.set_r_pcrel(true);
1133 reloc1.set_r_length(2);
1134 reloc1.set_r_extern(external);
1135 reloc1.set_r_type(X86_64_RELOC_SIGNED_4);
1136 relocs.push_back(reloc1);
1139 case ld::Fixup::kindStoreX86PCRel32GOTLoad:
1140 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad:
1141 reloc1.set_r_address(address);
1142 reloc1.set_r_symbolnum(symbolNum);
1143 reloc1.set_r_pcrel(true);
1144 reloc1.set_r_length(2);
1145 reloc1.set_r_extern(external);
1146 reloc1.set_r_type(X86_64_RELOC_GOT_LOAD);
1147 relocs.push_back(reloc1);
1150 case ld::Fixup::kindStoreX86PCRel32GOT:
1151 reloc1.set_r_address(address);
1152 reloc1.set_r_symbolnum(symbolNum);
1153 reloc1.set_r_pcrel(true);
1154 reloc1.set_r_length(2);
1155 reloc1.set_r_extern(external);
1156 reloc1.set_r_type(X86_64_RELOC_GOT);
1157 relocs.push_back(reloc1);
1160 case ld::Fixup::kindStoreLittleEndian64:
1161 case ld::Fixup::kindStoreTargetAddressLittleEndian64:
1162 if ( entry.fromTarget != NULL ) {
1163 // this is a pointer-diff
1164 reloc1.set_r_address(address);
1165 reloc1.set_r_symbolnum(symbolNum);
1166 reloc1.set_r_pcrel(false);
1167 reloc1.set_r_length(3);
1168 reloc1.set_r_extern(external);
1169 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1170 reloc2.set_r_address(address);
1171 reloc2.set_r_symbolnum(fromSymbolNum);
1172 reloc2.set_r_pcrel(false);
1173 reloc2.set_r_length(3);
1174 reloc2.set_r_extern(fromExternal);
1175 reloc2.set_r_type(X86_64_RELOC_SUBTRACTOR);
1176 relocs.push_back(reloc2);
1177 relocs.push_back(reloc1);
1181 reloc1.set_r_address(address);
1182 reloc1.set_r_symbolnum(symbolNum);
1183 reloc1.set_r_pcrel(false);
1184 reloc1.set_r_length(3);
1185 reloc1.set_r_extern(external);
1186 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1187 relocs.push_back(reloc1);
1191 case ld::Fixup::kindStoreLittleEndian32:
1192 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1193 if ( entry.fromTarget != NULL ) {
1194 // this is a pointer-diff
1195 reloc1.set_r_address(address);
1196 reloc1.set_r_symbolnum(symbolNum);
1197 reloc1.set_r_pcrel(false);
1198 reloc1.set_r_length(2);
1199 reloc1.set_r_extern(external);
1200 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1201 reloc2.set_r_address(address);
1202 reloc2.set_r_symbolnum(fromSymbolNum);
1203 reloc2.set_r_pcrel(false);
1204 reloc2.set_r_length(2);
1205 reloc2.set_r_extern(fromExternal);
1206 reloc2.set_r_type(X86_64_RELOC_SUBTRACTOR);
1207 relocs.push_back(reloc2);
1208 relocs.push_back(reloc1);
1212 reloc1.set_r_address(address);
1213 reloc1.set_r_symbolnum(symbolNum);
1214 reloc1.set_r_pcrel(false);
1215 reloc1.set_r_length(2);
1216 reloc1.set_r_extern(external);
1217 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1218 relocs.push_back(reloc1);
1222 assert(0 && "need to handle -r reloc");
1230 template <typename A>
1231 uint32_t SectionRelocationsAtom<A>::sectSymNum(bool external, const ld::Atom* target)
1233 if ( target->definition() == ld::Atom::definitionAbsolute )
1236 return this->symbolIndex(target); // in external relocations, r_symbolnum field is symbol index
1238 return target->machoSection(); // in non-extern relocations, r_symbolnum is mach-o section index of target
1242 void SectionRelocationsAtom<x86>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1243 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1245 macho_relocation_info<P> reloc1;
1246 macho_relocation_info<P> reloc2;
1247 macho_scattered_relocation_info<P>* sreloc1 = (macho_scattered_relocation_info<P>*)&reloc1;
1248 macho_scattered_relocation_info<P>* sreloc2 = (macho_scattered_relocation_info<P>*)&reloc2;
1249 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1250 bool external = entry.toTargetUsesExternalReloc;
1251 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1252 bool fromExternal = false;
1253 uint32_t fromSymbolNum = 0;
1254 if ( entry.fromTarget != NULL ) {
1255 fromExternal = entry.fromTargetUsesExternalReloc;
1256 fromSymbolNum = sectSymNum(fromExternal, entry.fromTarget);
1260 switch ( entry.kind ) {
1261 case ld::Fixup::kindStoreX86PCRel32:
1262 case ld::Fixup::kindStoreX86BranchPCRel32:
1263 case ld::Fixup::kindStoreTargetAddressX86BranchPCRel32:
1264 case ld::Fixup::kindStoreX86DtraceCallSiteNop:
1265 case ld::Fixup::kindStoreX86DtraceIsEnableSiteClear:
1266 if ( !external && (entry.toAddend != 0) ) {
1267 // use scattered reloc is target offset is non-zero
1268 sreloc1->set_r_scattered(true);
1269 sreloc1->set_r_pcrel(true);
1270 sreloc1->set_r_length(2);
1271 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1272 sreloc1->set_r_address(address);
1273 sreloc1->set_r_value(entry.toTarget->finalAddress());
1276 reloc1.set_r_address(address);
1277 reloc1.set_r_symbolnum(symbolNum);
1278 reloc1.set_r_pcrel(true);
1279 reloc1.set_r_length(2);
1280 reloc1.set_r_extern(external);
1281 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1283 relocs.push_back(reloc1);
1286 case ld::Fixup::kindStoreX86BranchPCRel8:
1287 if ( !external && (entry.toAddend != 0) ) {
1288 // use scattered reloc is target offset is non-zero
1289 sreloc1->set_r_scattered(true);
1290 sreloc1->set_r_pcrel(true);
1291 sreloc1->set_r_length(0);
1292 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1293 sreloc1->set_r_address(address);
1294 sreloc1->set_r_value(entry.toTarget->finalAddress());
1297 reloc1.set_r_address(address);
1298 reloc1.set_r_symbolnum(symbolNum);
1299 reloc1.set_r_pcrel(true);
1300 reloc1.set_r_length(0);
1301 reloc1.set_r_extern(external);
1302 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1304 relocs.push_back(reloc1);
1307 case ld::Fixup::kindStoreX86PCRel16:
1308 if ( !external && (entry.toAddend != 0) ) {
1309 // use scattered reloc is target offset is non-zero
1310 sreloc1->set_r_scattered(true);
1311 sreloc1->set_r_pcrel(true);
1312 sreloc1->set_r_length(1);
1313 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1314 sreloc1->set_r_address(address);
1315 sreloc1->set_r_value(entry.toTarget->finalAddress());
1318 reloc1.set_r_address(address);
1319 reloc1.set_r_symbolnum(symbolNum);
1320 reloc1.set_r_pcrel(true);
1321 reloc1.set_r_length(1);
1322 reloc1.set_r_extern(external);
1323 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1325 relocs.push_back(reloc1);
1328 case ld::Fixup::kindStoreLittleEndian32:
1329 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1330 if ( entry.fromTarget != NULL ) {
1331 // this is a pointer-diff
1332 sreloc1->set_r_scattered(true);
1333 sreloc1->set_r_pcrel(false);
1334 sreloc1->set_r_length(2);
1335 if ( entry.toTarget->scope() == ld::Atom::scopeTranslationUnit )
1336 sreloc1->set_r_type(GENERIC_RELOC_LOCAL_SECTDIFF);
1338 sreloc1->set_r_type(GENERIC_RELOC_SECTDIFF);
1339 sreloc1->set_r_address(address);
1340 if ( entry.toTarget == entry.inAtom ) {
1341 if ( entry.toAddend > entry.toTarget->size() )
1342 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.offsetInAtom);
1344 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.toAddend);
1347 sreloc1->set_r_value(entry.toTarget->finalAddress());
1348 sreloc2->set_r_scattered(true);
1349 sreloc2->set_r_pcrel(false);
1350 sreloc2->set_r_length(2);
1351 sreloc2->set_r_type(GENERIC_RELOC_PAIR);
1352 sreloc2->set_r_address(0);
1353 if ( entry.fromTarget == entry.inAtom ) {
1354 if ( entry.fromAddend > entry.fromTarget->size() )
1355 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.offsetInAtom);
1357 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
1360 sreloc2->set_r_value(entry.fromTarget->finalAddress());
1361 relocs.push_back(reloc1);
1362 relocs.push_back(reloc2);
1366 if ( !external && (entry.toAddend != 0) ) {
1367 // use scattered reloc is target offset is non-zero
1368 sreloc1->set_r_scattered(true);
1369 sreloc1->set_r_pcrel(false);
1370 sreloc1->set_r_length(2);
1371 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1372 sreloc1->set_r_address(address);
1373 sreloc1->set_r_value(entry.toTarget->finalAddress());
1376 reloc1.set_r_address(address);
1377 reloc1.set_r_symbolnum(symbolNum);
1378 reloc1.set_r_pcrel(false);
1379 reloc1.set_r_length(2);
1380 reloc1.set_r_extern(external);
1381 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1383 relocs.push_back(reloc1);
1387 assert(0 && "need to handle -r reloc");
1394 void SectionRelocationsAtom<arm>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1395 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1397 macho_relocation_info<P> reloc1;
1398 macho_relocation_info<P> reloc2;
1399 macho_scattered_relocation_info<P>* sreloc1 = (macho_scattered_relocation_info<P>*)&reloc1;
1400 macho_scattered_relocation_info<P>* sreloc2 = (macho_scattered_relocation_info<P>*)&reloc2;
1401 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1402 bool external = entry.toTargetUsesExternalReloc;
1403 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1404 bool fromExternal = false;
1405 uint32_t fromSymbolNum = 0;
1406 if ( entry.fromTarget != NULL ) {
1407 fromExternal = entry.fromTargetUsesExternalReloc;
1408 fromSymbolNum = sectSymNum(fromExternal, entry.fromTarget);
1412 switch ( entry.kind ) {
1413 case ld::Fixup::kindStoreTargetAddressARMBranch24:
1414 case ld::Fixup::kindStoreARMBranch24:
1415 case ld::Fixup::kindStoreARMDtraceCallSiteNop:
1416 case ld::Fixup::kindStoreARMDtraceIsEnableSiteClear:
1417 if ( !external && (entry.toAddend != 0) ) {
1418 // use scattered reloc is target offset is non-zero
1419 sreloc1->set_r_scattered(true);
1420 sreloc1->set_r_pcrel(true);
1421 sreloc1->set_r_length(2);
1422 sreloc1->set_r_type(ARM_RELOC_BR24);
1423 sreloc1->set_r_address(address);
1424 sreloc1->set_r_value(entry.toTarget->finalAddress());
1427 reloc1.set_r_address(address);
1428 reloc1.set_r_symbolnum(symbolNum);
1429 reloc1.set_r_pcrel(true);
1430 reloc1.set_r_length(2);
1431 reloc1.set_r_extern(external);
1432 reloc1.set_r_type(ARM_RELOC_BR24);
1434 relocs.push_back(reloc1);
1437 case ld::Fixup::kindStoreTargetAddressThumbBranch22:
1438 case ld::Fixup::kindStoreThumbBranch22:
1439 case ld::Fixup::kindStoreThumbDtraceCallSiteNop:
1440 case ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear:
1441 if ( !external && (entry.toAddend != 0) ) {
1442 // use scattered reloc is target offset is non-zero
1443 sreloc1->set_r_scattered(true);
1444 sreloc1->set_r_pcrel(true);
1445 sreloc1->set_r_length(2);
1446 sreloc1->set_r_type(ARM_THUMB_RELOC_BR22);
1447 sreloc1->set_r_address(address);
1448 sreloc1->set_r_value(entry.toTarget->finalAddress());
1451 reloc1.set_r_address(address);
1452 reloc1.set_r_symbolnum(symbolNum);
1453 reloc1.set_r_pcrel(true);
1454 reloc1.set_r_length(2);
1455 reloc1.set_r_extern(external);
1456 reloc1.set_r_type(ARM_THUMB_RELOC_BR22);
1458 relocs.push_back(reloc1);
1461 case ld::Fixup::kindStoreLittleEndian32:
1462 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1463 if ( entry.fromTarget != NULL ) {
1464 // this is a pointer-diff
1465 sreloc1->set_r_scattered(true);
1466 sreloc1->set_r_pcrel(false);
1467 sreloc1->set_r_length(2);
1468 if ( entry.toTarget->scope() == ld::Atom::scopeTranslationUnit )
1469 sreloc1->set_r_type(ARM_RELOC_LOCAL_SECTDIFF);
1471 sreloc1->set_r_type(ARM_RELOC_SECTDIFF);
1472 sreloc1->set_r_address(address);
1473 if ( entry.toTarget == entry.inAtom ) {
1474 if ( entry.toAddend > entry.toTarget->size() )
1475 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.offsetInAtom);
1477 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.toAddend);
1480 sreloc1->set_r_value(entry.toTarget->finalAddress());
1482 sreloc2->set_r_scattered(true);
1483 sreloc2->set_r_pcrel(false);
1484 sreloc2->set_r_length(2);
1485 sreloc2->set_r_type(ARM_RELOC_PAIR);
1486 sreloc2->set_r_address(0);
1487 if ( entry.fromTarget == entry.inAtom ) {
1488 //unsigned int pcBaseOffset = entry.inAtom->isThumb() ? 4 : 8;
1489 //if ( entry.fromAddend > pcBaseOffset )
1490 // sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend-pcBaseOffset);
1492 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
1495 sreloc2->set_r_value(entry.fromTarget->finalAddress());
1497 relocs.push_back(reloc1);
1498 relocs.push_back(reloc2);
1502 if ( !external && (entry.toAddend != 0) ) {
1503 // use scattered reloc is target offset is non-zero
1504 sreloc1->set_r_scattered(true);
1505 sreloc1->set_r_pcrel(false);
1506 sreloc1->set_r_length(2);
1507 sreloc1->set_r_type(ARM_RELOC_VANILLA);
1508 sreloc1->set_r_address(address);
1509 sreloc1->set_r_value(entry.toTarget->finalAddress());
1512 reloc1.set_r_address(address);
1513 reloc1.set_r_symbolnum(symbolNum);
1514 reloc1.set_r_pcrel(false);
1515 reloc1.set_r_length(2);
1516 reloc1.set_r_extern(external);
1517 reloc1.set_r_type(ARM_RELOC_VANILLA);
1519 relocs.push_back(reloc1);
1523 case ld::Fixup::kindStoreARMLow16:
1524 case ld::Fixup::kindStoreARMHigh16:
1525 case ld::Fixup::kindStoreThumbLow16:
1526 case ld::Fixup::kindStoreThumbHigh16:
1529 uint32_t otherHalf = 0;
1530 uint32_t value = entry.toTarget->finalAddress()+entry.toAddend;
1531 if ( entry.fromTarget != NULL )
1532 value -= (entry.fromTarget->finalAddress()+entry.fromAddend);
1533 switch ( entry.kind ) {
1534 case ld::Fixup::kindStoreARMLow16:
1536 otherHalf = value >> 16;
1538 case ld::Fixup::kindStoreARMHigh16:
1540 otherHalf = value & 0xFFFF;
1542 case ld::Fixup::kindStoreThumbLow16:
1544 otherHalf = value >> 16;
1546 case ld::Fixup::kindStoreThumbHigh16:
1548 otherHalf = value & 0xFFFF;
1553 if ( entry.fromTarget != NULL ) {
1554 // this is a sect-diff
1555 sreloc1->set_r_scattered(true);
1556 sreloc1->set_r_pcrel(false);
1557 sreloc1->set_r_length(len);
1558 sreloc1->set_r_type(ARM_RELOC_HALF_SECTDIFF);
1559 sreloc1->set_r_address(address);
1560 sreloc1->set_r_value(entry.toTarget->finalAddress());
1561 sreloc2->set_r_scattered(true);
1562 sreloc2->set_r_pcrel(false);
1563 sreloc2->set_r_length(len);
1564 sreloc2->set_r_type(ARM_RELOC_PAIR);
1565 sreloc2->set_r_address(otherHalf);
1566 if ( entry.fromTarget == entry.inAtom )
1567 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
1569 sreloc2->set_r_value(entry.fromTarget->finalAddress());
1570 relocs.push_back(reloc1);
1571 relocs.push_back(reloc2);
1574 // this is absolute address
1575 if ( !external && (entry.toAddend != 0) ) {
1576 // use scattered reloc is target offset is non-zero
1577 sreloc1->set_r_scattered(true);
1578 sreloc1->set_r_pcrel(false);
1579 sreloc1->set_r_length(len);
1580 sreloc1->set_r_type(ARM_RELOC_HALF);
1581 sreloc1->set_r_address(address);
1582 sreloc1->set_r_value(entry.toTarget->finalAddress());
1583 reloc2.set_r_address(otherHalf);
1584 reloc2.set_r_symbolnum(0);
1585 reloc2.set_r_pcrel(false);
1586 reloc2.set_r_length(len);
1587 reloc2.set_r_extern(false);
1588 reloc2.set_r_type(ARM_RELOC_PAIR);
1589 relocs.push_back(reloc1);
1590 relocs.push_back(reloc2);
1593 reloc1.set_r_address(address);
1594 reloc1.set_r_symbolnum(symbolNum);
1595 reloc1.set_r_pcrel(false);
1596 reloc1.set_r_length(len);
1597 reloc1.set_r_extern(external);
1598 reloc1.set_r_type(ARM_RELOC_HALF);
1599 reloc2.set_r_address(otherHalf); // other half
1600 reloc2.set_r_symbolnum(0);
1601 reloc2.set_r_pcrel(false);
1602 reloc2.set_r_length(len);
1603 reloc2.set_r_extern(false);
1604 reloc2.set_r_type(ARM_RELOC_PAIR);
1605 relocs.push_back(reloc1);
1606 relocs.push_back(reloc2);
1613 assert(0 && "need to handle -r reloc");
1620 template <typename A>
1621 void SectionRelocationsAtom<A>::addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind kind,
1622 const ld::Atom* inAtom, uint32_t offsetInAtom,
1623 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
1624 const ld::Atom* toTarget, uint64_t toAddend,
1625 const ld::Atom* fromTarget, uint64_t fromAddend)
1629 entry.toTargetUsesExternalReloc = toTargetUsesExternalReloc;
1630 entry.fromTargetUsesExternalReloc = fromTargetExternalReloc;
1631 entry.inAtom = inAtom;
1632 entry.offsetInAtom = offsetInAtom;
1633 entry.toTarget = toTarget;
1634 entry.toAddend = toAddend;
1635 entry.fromTarget = fromTarget;
1636 entry.fromAddend = fromAddend;
1638 static ld::Internal::FinalSection* lastSection = NULL;
1639 static SectionAndEntries* lastSectionAndEntries = NULL;
1641 if ( sect != lastSection ) {
1642 for(typename std::vector<SectionAndEntries>::iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
1643 if ( sect == it->sect ) {
1645 lastSectionAndEntries = &*it;
1649 if ( sect != lastSection ) {
1650 SectionAndEntries tmp;
1652 _entriesBySection.push_back(tmp);
1654 lastSectionAndEntries = &_entriesBySection.back();
1657 lastSectionAndEntries->entries.push_back(entry);
1660 template <typename A>
1661 void SectionRelocationsAtom<A>::encode()
1663 // convert each Entry record to one or two reloc records
1664 for(typename std::vector<SectionAndEntries>::iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
1665 SectionAndEntries& se = *it;
1666 for(typename std::vector<Entry>::iterator eit=se.entries.begin(); eit != se.entries.end(); ++eit) {
1667 encodeSectionReloc(se.sect, *eit, se.relocs);
1671 // update sections with start and count or relocs
1673 for(typename std::vector<SectionAndEntries>::iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
1674 SectionAndEntries& se = *it;
1675 se.sect->relocStart = index;
1676 se.sect->relocCount = se.relocs.size();
1677 index += se.sect->relocCount;
1684 template <typename A>
1685 class IndirectSymbolTableAtom : public ClassicLinkEditAtom
1688 IndirectSymbolTableAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
1689 : ClassicLinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { }
1691 // overrides of ld::Atom
1692 virtual const char* name() const { return "indirect symbol table"; }
1693 virtual uint64_t size() const;
1694 virtual void copyRawContent(uint8_t buffer[]) const;
1695 // overrides of ClassicLinkEditAtom
1696 virtual void encode();
1699 typedef typename A::P P;
1700 typedef typename A::P::E E;
1701 typedef typename A::P::uint_t pint_t;
1703 void encodeStubSection(ld::Internal::FinalSection* sect);
1704 void encodeLazyPointerSection(ld::Internal::FinalSection* sect);
1705 void encodeNonLazyPointerSection(ld::Internal::FinalSection* sect);
1706 uint32_t symIndexOfStubAtom(const ld::Atom*);
1707 uint32_t symIndexOfLazyPointerAtom(const ld::Atom*);
1708 uint32_t symIndexOfNonLazyPointerAtom(const ld::Atom*);
1709 uint32_t symbolIndex(const ld::Atom*);
1710 bool kextBundlesDontHaveIndirectSymbolTable();
1713 std::vector<uint32_t> _entries;
1715 static ld::Section _s_section;
1718 template <typename A>
1719 ld::Section IndirectSymbolTableAtom<A>::_s_section("__LINKEDIT", "__ind_sym_tab", ld::Section::typeLinkEdit, true);
1724 template <typename A>
1725 uint32_t IndirectSymbolTableAtom<A>::symbolIndex(const ld::Atom* atom)
1727 std::map<const ld::Atom*, uint32_t>::iterator pos = this->_writer._atomToSymbolIndex.find(atom);
1728 if ( pos != this->_writer._atomToSymbolIndex.end() )
1730 //fprintf(stderr, "_atomToSymbolIndex content:\n");
1731 //for(std::map<const ld::Atom*, uint32_t>::iterator it = this->_writer._atomToSymbolIndex.begin(); it != this->_writer._atomToSymbolIndex.end(); ++it) {
1732 // fprintf(stderr, "%p(%s) => %d\n", it->first, it->first->name(), it->second);
1734 throwf("internal error: atom not found in symbolIndex(%s)", atom->name());
1737 template <typename A>
1738 uint32_t IndirectSymbolTableAtom<A>::symIndexOfStubAtom(const ld::Atom* stubAtom)
1740 for (ld::Fixup::iterator fit = stubAtom->fixupsBegin(); fit != stubAtom->fixupsEnd(); ++fit) {
1741 if ( fit->binding == ld::Fixup::bindingDirectlyBound ) {
1742 assert((fit->u.target->contentType() == ld::Atom::typeLazyPointer)
1743 || (fit->u.target->contentType() == ld::Atom::typeLazyDylibPointer));
1744 return symIndexOfLazyPointerAtom(fit->u.target);
1747 throw "internal error: stub missing fixup to lazy pointer";
1751 template <typename A>
1752 uint32_t IndirectSymbolTableAtom<A>::symIndexOfLazyPointerAtom(const ld::Atom* lpAtom)
1754 for (ld::Fixup::iterator fit = lpAtom->fixupsBegin(); fit != lpAtom->fixupsEnd(); ++fit) {
1755 if ( fit->kind == ld::Fixup::kindLazyTarget ) {
1756 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
1757 return symbolIndex(fit->u.target);
1760 throw "internal error: lazy pointer missing fixupLazyTarget fixup";
1763 template <typename A>
1764 uint32_t IndirectSymbolTableAtom<A>::symIndexOfNonLazyPointerAtom(const ld::Atom* nlpAtom)
1766 //fprintf(stderr, "symIndexOfNonLazyPointerAtom(%p) %s\n", nlpAtom, nlpAtom->name());
1767 for (ld::Fixup::iterator fit = nlpAtom->fixupsBegin(); fit != nlpAtom->fixupsEnd(); ++fit) {
1768 // non-lazy-pointer to a stripped symbol => no symbol index
1769 if ( fit->clusterSize != ld::Fixup::k1of1 )
1770 return INDIRECT_SYMBOL_LOCAL;
1771 const ld::Atom* target;
1772 switch ( fit->binding ) {
1773 case ld::Fixup::bindingDirectlyBound:
1774 target = fit->u.target;
1776 case ld::Fixup::bindingsIndirectlyBound:
1777 target = _state.indirectBindingTable[fit->u.bindingIndex];
1780 throw "internal error: unexpected non-lazy pointer binding";
1782 bool targetIsGlobal = (target->scope() == ld::Atom::scopeGlobal);
1783 switch ( target->definition() ) {
1784 case ld::Atom::definitionRegular:
1785 if ( targetIsGlobal ) {
1786 if ( _options.outputKind() == Options::kObjectFile ) {
1787 // nlpointer to global symbol uses indirect symbol table in .o files
1788 return symbolIndex(target);
1790 else if ( target->combine() == ld::Atom::combineByName ) {
1791 // dyld needs to bind nlpointer to global weak def
1792 return symbolIndex(target);
1794 else if ( _options.nameSpace() != Options::kTwoLevelNameSpace ) {
1795 // dyld needs to bind nlpointer to global def linked for flat namespace
1796 return symbolIndex(target);
1800 case ld::Atom::definitionTentative:
1801 case ld::Atom::definitionAbsolute:
1802 if ( _options.outputKind() == Options::kObjectFile ) {
1803 // tentative def in .o file always uses symbol index
1804 return symbolIndex(target);
1806 // dyld needs to bind nlpointer to global def linked for flat namespace
1807 if ( targetIsGlobal && _options.nameSpace() != Options::kTwoLevelNameSpace )
1808 return symbolIndex(target);
1810 case ld::Atom::definitionProxy:
1811 // dyld needs to bind nlpointer to something in another dylib
1813 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(target->file());
1814 if ( (dylib != NULL) && dylib->willBeLazyLoadedDylib() )
1815 throwf("illegal data reference to %s in lazy loaded dylib %s", target->name(), dylib->path());
1817 return symbolIndex(target);
1820 if ( nlpAtom->fixupsBegin() == nlpAtom->fixupsEnd() ) {
1821 // no fixups means this is the ImageLoader cache slot
1822 return INDIRECT_SYMBOL_ABS;
1825 // The magic index INDIRECT_SYMBOL_LOCAL tells dyld it should does not need to bind
1826 // this non-lazy pointer.
1827 return INDIRECT_SYMBOL_LOCAL;
1832 template <typename A>
1833 void IndirectSymbolTableAtom<A>::encodeStubSection(ld::Internal::FinalSection* sect)
1835 sect->indirectSymTabStartIndex = _entries.size();
1836 sect->indirectSymTabElementSize = sect->atoms[0]->size();
1837 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
1838 _entries.push_back(symIndexOfStubAtom(*ait));
1842 template <typename A>
1843 void IndirectSymbolTableAtom<A>::encodeLazyPointerSection(ld::Internal::FinalSection* sect)
1845 sect->indirectSymTabStartIndex = _entries.size();
1846 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
1847 _entries.push_back(symIndexOfLazyPointerAtom(*ait));
1851 template <typename A>
1852 void IndirectSymbolTableAtom<A>::encodeNonLazyPointerSection(ld::Internal::FinalSection* sect)
1854 sect->indirectSymTabStartIndex = _entries.size();
1855 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
1856 _entries.push_back(symIndexOfNonLazyPointerAtom(*ait));
1860 template <typename A>
1861 bool IndirectSymbolTableAtom<A>::kextBundlesDontHaveIndirectSymbolTable()
1866 template <typename A>
1867 void IndirectSymbolTableAtom<A>::encode()
1869 // static executables should not have an indirect symbol table
1870 if ( this->_options.outputKind() == Options::kStaticExecutable )
1873 // x86_64 kext bundles should not have an indirect symbol table
1874 if ( (this->_options.outputKind() == Options::kKextBundle) && kextBundlesDontHaveIndirectSymbolTable() )
1877 // find all special sections that need a range of the indirect symbol table section
1878 for (std::vector<ld::Internal::FinalSection*>::iterator sit = this->_state.sections.begin(); sit != this->_state.sections.end(); ++sit) {
1879 ld::Internal::FinalSection* sect = *sit;
1880 switch ( sect->type() ) {
1881 case ld::Section::typeStub:
1882 case ld::Section::typeStubClose:
1883 this->encodeStubSection(sect);
1885 case ld::Section::typeLazyPointerClose:
1886 case ld::Section::typeLazyPointer:
1887 case ld::Section::typeLazyDylibPointer:
1888 this->encodeLazyPointerSection(sect);
1890 case ld::Section::typeNonLazyPointer:
1891 this->encodeNonLazyPointerSection(sect);
1899 template <typename A>
1900 uint64_t IndirectSymbolTableAtom<A>::size() const
1902 return _entries.size() * sizeof(uint32_t);
1905 template <typename A>
1906 void IndirectSymbolTableAtom<A>::copyRawContent(uint8_t buffer[]) const
1908 uint32_t* array = (uint32_t*)buffer;
1909 for(unsigned long i=0; i < _entries.size(); ++i) {
1910 E::set32(array[i], _entries[i]);
1924 #endif // __LINKEDIT_CLASSIC_HPP__