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 if ( atom->weakImported() )
490 entry.set_n_desc(desc);
492 // set n_value, zero for import proxy and size for tentative definition
493 if ( atom->definition() == ld::Atom::definitionTentative )
494 entry.set_n_value(atom->size());
496 entry.set_n_value(0);
499 _imports.push_back(entry);
502 template <typename A>
503 uint8_t SymbolTableAtom<A>::sectionIndexForStab(const ld::relocatable::File::Stab& stab)
505 // in FUN stabs, n_sect field is 0 for start FUN and 1 for end FUN
506 if ( stab.type == N_FUN )
508 else if ( stab.type == N_GSYM )
510 else if ( stab.atom != NULL )
511 return stab.atom->machoSection();
517 template <typename A>
518 uint64_t SymbolTableAtom<A>::valueForStab(const ld::relocatable::File::Stab& stab)
520 switch ( stab.type ) {
522 if ( stab.atom == NULL ) {
523 // <rdar://problem/5591394> Add support to ld64 for N_FUN stabs when used for symbolic constants
526 if ( (stab.string == NULL) || (strlen(stab.string) == 0) ) {
527 // end of function N_FUN has size
528 return stab.atom->size();
531 // start of function N_FUN has address
532 return stab.atom->finalAddress();
537 if ( stab.atom == NULL )
538 // some weird assembly files have slines not associated with a function
541 // all these stab types need their value changed from an offset in the atom to an address
542 return stab.atom->finalAddress() + stab.value;
546 // all these need address of atom
547 if ( stab.atom != NULL )
548 return stab.atom->finalAddress();
550 return 0; // <rdar://problem/7811357> work around for mismatch N_BNSYM
552 return stab.atom->size();
554 if ( stab.atom == NULL ) {
558 if ( (stab.string == NULL) || (strlen(stab.string) == 0) ) {
559 // end of translation unit N_SO has address of end of last atom
560 return stab.atom->finalAddress() + stab.atom->size();
563 // start of translation unit N_SO has address of end of first atom
564 return stab.atom->finalAddress();
573 template <typename A>
574 uint32_t SymbolTableAtom<A>::stringOffsetForStab(const ld::relocatable::File::Stab& stab, StringPoolAtom* pool)
578 if ( (stab.string == NULL) || stab.string[0] == '\0' ) {
579 return pool->emptyString();
582 // fall into uniquing case
586 return pool->addUnique(stab.string);
589 if ( stab.string == NULL )
591 else if ( stab.string[0] == '\0' )
592 return pool->emptyString();
594 return pool->add(stab.string);
601 template <typename A>
602 bool SymbolTableAtom<A>::hasStabs(uint32_t& ssos, uint32_t& ssoe, uint32_t& sos, uint32_t& soe)
604 ssos = _stabsStringsOffsetStart;
605 ssoe = _stabsStringsOffsetEnd;
606 sos = _stabsIndexStart * sizeof(macho_nlist<P>);
607 soe = _stabsIndexEnd * sizeof(macho_nlist<P>);
608 return ( (_stabsIndexStart != _stabsIndexEnd) || (_stabsStringsOffsetStart != _stabsStringsOffsetEnd) );
611 template <typename A>
612 void SymbolTableAtom<A>::encode()
614 uint32_t symbolIndex = 0;
616 // make nlist entries for all local symbols
617 std::vector<const ld::Atom*>& localAtoms = this->_writer._localAtoms;
618 _locals.reserve(localAtoms.size()+this->_state.stabs.size());
619 this->_writer._localSymbolsStartIndex = 0;
620 // make nlist entries for all debug notes
621 _stabsIndexStart = symbolIndex;
622 _stabsStringsOffsetStart = this->_writer._stringPoolAtom->currentOffset();
623 for (std::vector<ld::relocatable::File::Stab>::const_iterator sit=this->_state.stabs.begin(); sit != this->_state.stabs.end(); ++sit) {
624 macho_nlist<P> entry;
625 entry.set_n_type(sit->type);
626 entry.set_n_sect(sectionIndexForStab(*sit));
627 entry.set_n_desc(sit->desc);
628 entry.set_n_value(valueForStab(*sit));
629 entry.set_n_strx(stringOffsetForStab(*sit, this->_writer._stringPoolAtom));
630 _locals.push_back(entry);
633 _stabsIndexEnd = symbolIndex;
634 _stabsStringsOffsetEnd = this->_writer._stringPoolAtom->currentOffset();
635 for (std::vector<const ld::Atom*>::const_iterator it=localAtoms.begin(); it != localAtoms.end(); ++it) {
636 const ld::Atom* atom = *it;
637 if ( this->addLocal(atom, this->_writer._stringPoolAtom) )
638 this->_writer._atomToSymbolIndex[atom] = symbolIndex++;
640 this->_writer._localSymbolsCount = symbolIndex;
643 // make nlist entries for all global symbols
644 std::vector<const ld::Atom*>& globalAtoms = this->_writer._exportedAtoms;
645 _globals.reserve(globalAtoms.size());
646 this->_writer._globalSymbolsStartIndex = symbolIndex;
647 for (std::vector<const ld::Atom*>::const_iterator it=globalAtoms.begin(); it != globalAtoms.end(); ++it) {
648 const ld::Atom* atom = *it;
649 this->addGlobal(atom, this->_writer._stringPoolAtom);
650 this->_writer._atomToSymbolIndex[atom] = symbolIndex++;
652 this->_writer._globalSymbolsCount = symbolIndex - this->_writer._globalSymbolsStartIndex;
654 // make nlist entries for all undefined (imported) symbols
655 std::vector<const ld::Atom*>& importAtoms = this->_writer._importedAtoms;
656 _imports.reserve(importAtoms.size());
657 this->_writer._importSymbolsStartIndex = symbolIndex;
658 for (std::vector<const ld::Atom*>::const_iterator it=importAtoms.begin(); it != importAtoms.end(); ++it) {
659 this->addImport(*it, this->_writer._stringPoolAtom);
660 this->_writer._atomToSymbolIndex[*it] = symbolIndex++;
662 this->_writer._importSymbolsCount = symbolIndex - this->_writer._importSymbolsStartIndex;
665 template <typename A>
666 uint64_t SymbolTableAtom<A>::size() const
668 return sizeof(macho_nlist<P>) * (_locals.size() + _globals.size() + _imports.size());
671 template <typename A>
672 void SymbolTableAtom<A>::copyRawContent(uint8_t buffer[]) const
674 memcpy(&buffer[this->_writer._localSymbolsStartIndex*sizeof(macho_nlist<P>)], &_locals[0],
675 this->_writer._localSymbolsCount*sizeof(macho_nlist<P>));
676 memcpy(&buffer[this->_writer._globalSymbolsStartIndex*sizeof(macho_nlist<P>)], &_globals[0],
677 this->_writer._globalSymbolsCount*sizeof(macho_nlist<P>));
678 memcpy(&buffer[this->_writer._importSymbolsStartIndex *sizeof(macho_nlist<P>)], &_imports[0],
679 this->_writer._importSymbolsCount*sizeof(macho_nlist<P>));
685 class RelocationsAtomAbstract : public ClassicLinkEditAtom
688 RelocationsAtomAbstract(const Options& opts, ld::Internal& state,
689 OutputFile& writer, const ld::Section& sect,
690 unsigned int pointerSize)
691 : ClassicLinkEditAtom(opts, state, writer, sect, pointerSize) { }
693 virtual void addPointerReloc(uint64_t addr, uint32_t symNum) = 0;
694 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum) = 0;
695 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*) = 0;
696 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*) = 0;
697 virtual uint64_t relocBaseAddress(ld::Internal& state) = 0;
698 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
699 const ld::Atom* inAtom, uint32_t offsetInAtom,
700 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
701 const ld::Atom* toTarget, uint64_t toAddend,
702 const ld::Atom* fromTarget, uint64_t fromAddend) = 0;
704 uint32_t symbolIndex(const ld::Atom* atom) const;
710 uint32_t RelocationsAtomAbstract::symbolIndex(const ld::Atom* atom) const
712 std::map<const ld::Atom*, uint32_t>::iterator pos = this->_writer._atomToSymbolIndex.find(atom);
713 if ( pos != this->_writer._atomToSymbolIndex.end() )
715 fprintf(stderr, "_atomToSymbolIndex content:\n");
716 for(std::map<const ld::Atom*, uint32_t>::iterator it = this->_writer._atomToSymbolIndex.begin(); it != this->_writer._atomToSymbolIndex.end(); ++it) {
717 fprintf(stderr, "%p(%s) => %d\n", it->first, it->first->name(), it->second);
719 throwf("internal error: atom not found in symbolIndex(%s)", atom->name());
723 template <typename A>
724 class LocalRelocationsAtom : public RelocationsAtomAbstract
727 LocalRelocationsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
728 : RelocationsAtomAbstract(opts, state, writer, _s_section, sizeof(pint_t)) { }
730 // overrides of ld::Atom
731 virtual const char* name() const { return "local relocations"; }
732 virtual uint64_t size() const;
733 virtual void copyRawContent(uint8_t buffer[]) const;
734 // overrides of ClassicLinkEditAtom
735 virtual void encode() {}
736 // overrides of RelocationsAtomAbstract
737 virtual void addPointerReloc(uint64_t addr, uint32_t symNum);
738 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*) {}
739 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*) {}
740 virtual uint64_t relocBaseAddress(ld::Internal& state);
741 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum);
742 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
743 const ld::Atom* inAtom, uint32_t offsetInAtom,
744 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
745 const ld::Atom* toTarget, uint64_t toAddend,
746 const ld::Atom* fromTarget, uint64_t fromAddend) { }
749 typedef typename A::P P;
750 typedef typename A::P::E E;
751 typedef typename A::P::uint_t pint_t;
753 std::vector<macho_relocation_info<P> > _relocs;
755 static ld::Section _s_section;
758 template <typename A>
759 ld::Section LocalRelocationsAtom<A>::_s_section("__LINKEDIT", "__local_relocs", ld::Section::typeLinkEdit, true);
763 uint64_t LocalRelocationsAtom<x86_64>::relocBaseAddress(ld::Internal& state)
765 if ( _options.outputKind() == Options::kKextBundle ) {
766 // for kext bundles the reloc base address starts at __TEXT segment
767 return _options.baseAddress();
769 // for all other kinds, the x86_64 reloc base address starts at __DATA segment
770 for (std::vector<ld::Internal::FinalSection*>::iterator sit = state.sections.begin(); sit != state.sections.end(); ++sit) {
771 ld::Internal::FinalSection* sect = *sit;
772 if ( strcmp(sect->segmentName(), "__DATA") == 0 )
773 return sect->address;
775 throw "__DATA segment not found";
778 template <typename A>
779 uint64_t LocalRelocationsAtom<A>::relocBaseAddress(ld::Internal& state)
781 return _options.baseAddress();
784 template <typename A>
785 void LocalRelocationsAtom<A>::addPointerReloc(uint64_t addr, uint32_t symNum)
787 macho_relocation_info<P> reloc;
788 reloc.set_r_address(addr);
789 reloc.set_r_symbolnum(symNum);
790 reloc.set_r_pcrel(false);
791 reloc.set_r_length();
792 reloc.set_r_extern(false);
793 reloc.set_r_type(GENERIC_RELOC_VANILLA);
794 _relocs.push_back(reloc);
797 template <typename A>
798 void LocalRelocationsAtom<A>::addTextReloc(uint64_t addr, ld::Fixup::Kind kind, uint64_t targetAddr, uint32_t symNum)
800 macho_relocation_info<P> reloc1;
801 macho_relocation_info<P> reloc2;
803 case ld::Fixup::kindStorePPCAbsLow14:
804 case ld::Fixup::kindStorePPCAbsLow16:
805 // a reference to the absolute address of something in this same linkage unit can be
806 // encoded as a local text reloc in a dylib or bundle
807 if ( _options.outputSlidable() ) {
808 reloc1.set_r_address(addr);
809 reloc1.set_r_symbolnum(symNum);
810 reloc1.set_r_pcrel(false);
811 reloc1.set_r_length(2);
812 reloc1.set_r_extern(false);
813 reloc1.set_r_type(kind==ld::Fixup::kindStorePPCAbsLow16 ? PPC_RELOC_LO16 : PPC_RELOC_LO14);
814 reloc2.set_r_address(targetAddr >> 16);
815 reloc2.set_r_symbolnum(0);
816 reloc2.set_r_pcrel(false);
817 reloc2.set_r_length(2);
818 reloc2.set_r_extern(false);
819 reloc2.set_r_type(PPC_RELOC_PAIR);
820 _relocs.push_back(reloc1);
821 _relocs.push_back(reloc2);
824 case ld::Fixup::kindStorePPCAbsHigh16AddLow:
825 case ld::Fixup::kindStorePPCAbsHigh16:
826 if ( _options.outputSlidable() ) {
827 reloc1.set_r_address(addr);
828 reloc1.set_r_symbolnum(symNum);
829 reloc1.set_r_pcrel(false);
830 reloc1.set_r_length(2);
831 reloc1.set_r_extern(false);
832 reloc1.set_r_type(kind==ld::Fixup::kindStorePPCAbsHigh16AddLow ? PPC_RELOC_HA16 : PPC_RELOC_HI16);
833 reloc2.set_r_address(targetAddr & 0xFFFF);
834 reloc2.set_r_symbolnum(0);
835 reloc2.set_r_pcrel(false);
836 reloc2.set_r_length(2);
837 reloc2.set_r_extern(false);
838 reloc2.set_r_type(PPC_RELOC_PAIR);
839 _relocs.push_back(reloc1);
840 _relocs.push_back(reloc2);
849 template <typename A>
850 uint64_t LocalRelocationsAtom<A>::size() const
852 return _relocs.size() * sizeof(macho_relocation_info<P>);
855 template <typename A>
856 void LocalRelocationsAtom<A>::copyRawContent(uint8_t buffer[]) const
858 memcpy(buffer, &_relocs[0], _relocs.size()*sizeof(macho_relocation_info<P>));
866 template <typename A>
867 class ExternalRelocationsAtom : public RelocationsAtomAbstract
870 ExternalRelocationsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
871 : RelocationsAtomAbstract(opts, state, writer, _s_section, sizeof(pint_t)) { }
873 // overrides of ld::Atom
874 virtual const char* name() const { return "external relocations"; }
875 virtual uint64_t size() const;
876 virtual void copyRawContent(uint8_t buffer[]) const;
877 // overrides of ClassicLinkEditAtom
878 virtual void encode() {}
879 // overrides of RelocationsAtomAbstract
880 virtual void addPointerReloc(uint64_t addr, uint32_t symNum) {}
881 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum) {}
882 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*);
883 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*);
884 virtual uint64_t relocBaseAddress(ld::Internal& state);
885 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
886 const ld::Atom* inAtom, uint32_t offsetInAtom,
887 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
888 const ld::Atom* toTarget, uint64_t toAddend,
889 const ld::Atom* fromTarget, uint64_t fromAddend) { }
893 typedef typename A::P P;
894 typedef typename A::P::E E;
895 typedef typename A::P::uint_t pint_t;
898 LocAndAtom(uint64_t l, const ld::Atom* a) : loc(l), atom(a), symbolIndex(0) {}
901 const ld::Atom* atom;
902 uint32_t symbolIndex;
904 bool operator<(const LocAndAtom& rhs) const {
905 // sort first by symbol number
906 if ( this->symbolIndex != rhs.symbolIndex )
907 return (this->symbolIndex < rhs.symbolIndex);
908 // then sort all uses of the same symbol by address
909 return (this->loc < rhs.loc);
914 static uint32_t pointerReloc();
915 static uint32_t callReloc();
917 mutable std::vector<LocAndAtom> _pointerLocations;
918 mutable std::vector<LocAndAtom> _callSiteLocations;
920 static ld::Section _s_section;
923 template <typename A>
924 ld::Section ExternalRelocationsAtom<A>::_s_section("__LINKEDIT", "__extrn_relocs", ld::Section::typeLinkEdit, true);
927 uint64_t ExternalRelocationsAtom<x86_64>::relocBaseAddress(ld::Internal& state)
929 // for x86_64 the reloc base address starts at __DATA segment
930 for (std::vector<ld::Internal::FinalSection*>::iterator sit = state.sections.begin(); sit != state.sections.end(); ++sit) {
931 ld::Internal::FinalSection* sect = *sit;
932 if ( strcmp(sect->segmentName(), "__DATA") == 0 )
933 return sect->address;
935 throw "__DATA segment not found";
938 template <typename A>
939 uint64_t ExternalRelocationsAtom<A>::relocBaseAddress(ld::Internal& state)
944 template <typename A>
945 void ExternalRelocationsAtom<A>::addExternalPointerReloc(uint64_t addr, const ld::Atom* target)
947 _pointerLocations.push_back(LocAndAtom(addr, target));
950 template <typename A>
951 void ExternalRelocationsAtom<A>::addExternalCallSiteReloc(uint64_t addr, const ld::Atom* target)
953 _callSiteLocations.push_back(LocAndAtom(addr, target));
957 template <typename A>
958 uint64_t ExternalRelocationsAtom<A>::size() const
960 if ( _options.outputKind() == Options::kStaticExecutable ) {
961 assert(_pointerLocations.size() == 0);
962 assert(_callSiteLocations.size() == 0);
964 return (_pointerLocations.size() + _callSiteLocations.size()) * sizeof(macho_relocation_info<P>);
967 template <> uint32_t ExternalRelocationsAtom<arm>::pointerReloc() { return ARM_RELOC_VANILLA; }
968 template <> uint32_t ExternalRelocationsAtom<x86>::pointerReloc() { return GENERIC_RELOC_VANILLA; }
969 template <> uint32_t ExternalRelocationsAtom<ppc>::pointerReloc() { return PPC_RELOC_VANILLA; }
970 template <> uint32_t ExternalRelocationsAtom<x86_64>::pointerReloc() { return X86_64_RELOC_UNSIGNED; }
971 template <> uint32_t ExternalRelocationsAtom<ppc64>::pointerReloc() { return PPC_RELOC_VANILLA; }
974 template <> uint32_t ExternalRelocationsAtom<x86_64>::callReloc() { return X86_64_RELOC_BRANCH; }
975 template <> uint32_t ExternalRelocationsAtom<x86>::callReloc() { return GENERIC_RELOC_VANILLA; }
976 template <typename A>
977 uint32_t ExternalRelocationsAtom<A>::callReloc()
979 assert(0 && "external call relocs not implemented");
984 template <typename A>
985 void ExternalRelocationsAtom<A>::copyRawContent(uint8_t buffer[]) const
987 macho_relocation_info<P>* r = (macho_relocation_info<P>*)buffer;
989 // assign symbol index, now that symbol table is built
990 for (typename std::vector<LocAndAtom>::iterator it = _pointerLocations.begin(); it != _pointerLocations.end(); ++it) {
991 it->symbolIndex = symbolIndex(it->atom);
993 std::sort(_pointerLocations.begin(), _pointerLocations.end());
994 for (typename std::vector<LocAndAtom>::const_iterator it = _pointerLocations.begin(); it != _pointerLocations.end(); ++it, ++r) {
995 r->set_r_address(it->loc);
996 r->set_r_symbolnum(it->symbolIndex);
997 r->set_r_pcrel(false);
999 r->set_r_extern(true);
1000 r->set_r_type(this->pointerReloc());
1003 for (typename std::vector<LocAndAtom>::iterator it = _callSiteLocations.begin(); it != _callSiteLocations.end(); ++it) {
1004 it->symbolIndex = symbolIndex(it->atom);
1006 std::sort(_callSiteLocations.begin(), _callSiteLocations.end());
1007 for (typename std::vector<LocAndAtom>::const_iterator it = _callSiteLocations.begin(); it != _callSiteLocations.end(); ++it, ++r) {
1008 r->set_r_address(it->loc);
1009 r->set_r_symbolnum(it->symbolIndex);
1010 r->set_r_pcrel(true);
1012 r->set_r_extern(true);
1013 r->set_r_type(this->callReloc());
1018 template <typename A>
1019 class SectionRelocationsAtom : public RelocationsAtomAbstract
1022 SectionRelocationsAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
1023 : RelocationsAtomAbstract(opts, state, writer, _s_section, sizeof(pint_t)) { }
1025 // overrides of ld::Atom
1026 virtual const char* name() const { return "section relocations"; }
1027 virtual uint64_t size() const;
1028 virtual void copyRawContent(uint8_t buffer[]) const;
1029 // overrides of ClassicLinkEditAtom
1030 virtual void encode();
1031 // overrides of RelocationsAtomAbstract
1032 virtual void addPointerReloc(uint64_t addr, uint32_t symNum) {}
1033 virtual void addTextReloc(uint64_t addr, ld::Fixup::Kind k, uint64_t targetAddr, uint32_t symNum) {}
1034 virtual void addExternalPointerReloc(uint64_t addr, const ld::Atom*) {}
1035 virtual void addExternalCallSiteReloc(uint64_t addr, const ld::Atom*) {}
1036 virtual uint64_t relocBaseAddress(ld::Internal& state) { return 0; }
1037 virtual void addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind,
1038 const ld::Atom* inAtom, uint32_t offsetInAtom,
1039 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
1040 const ld::Atom* toTarget, uint64_t toAddend,
1041 const ld::Atom* fromTarget, uint64_t fromAddend);
1044 typedef typename A::P P;
1045 typedef typename A::P::E E;
1046 typedef typename A::P::uint_t pint_t;
1050 ld::Fixup::Kind kind;
1051 bool toTargetUsesExternalReloc;
1052 bool fromTargetUsesExternalReloc;
1053 const ld::Atom* inAtom;
1054 uint32_t offsetInAtom;
1055 const ld::Atom* toTarget;
1057 const ld::Atom* fromTarget;
1058 uint64_t fromAddend;
1060 uint32_t sectSymNum(bool external, const ld::Atom* target);
1061 void encodeSectionReloc(ld::Internal::FinalSection* sect,
1062 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs);
1064 struct SectionAndEntries {
1065 ld::Internal::FinalSection* sect;
1066 std::vector<Entry> entries;
1067 std::vector<macho_relocation_info<P> > relocs;
1070 std::vector<SectionAndEntries> _entriesBySection;
1072 static ld::Section _s_section;
1075 template <typename A>
1076 ld::Section SectionRelocationsAtom<A>::_s_section("__LINKEDIT", "__sect_relocs", ld::Section::typeLinkEdit, true);
1081 template <typename A>
1082 uint64_t SectionRelocationsAtom<A>::size() const
1085 for(typename std::vector<SectionAndEntries>::const_iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
1086 const SectionAndEntries& se = *it;
1087 count += se.relocs.size();
1089 return count * sizeof(macho_relocation_info<P>);
1092 template <typename A>
1093 void SectionRelocationsAtom<A>::copyRawContent(uint8_t buffer[]) const
1095 uint32_t offset = 0;
1096 for(typename std::vector<SectionAndEntries>::const_iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
1097 const SectionAndEntries& se = *it;
1098 memcpy(&buffer[offset], &se.relocs[0], se.relocs.size()*sizeof(macho_relocation_info<P>));
1099 offset += (se.relocs.size() * sizeof(macho_relocation_info<P>));
1105 void SectionRelocationsAtom<x86_64>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1106 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1108 macho_relocation_info<P> reloc1;
1109 macho_relocation_info<P> reloc2;
1110 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1111 bool external = entry.toTargetUsesExternalReloc;
1112 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1113 bool fromExternal = false;
1114 uint32_t fromSymbolNum = 0;
1115 if ( entry.fromTarget != NULL ) {
1116 fromExternal = entry.fromTargetUsesExternalReloc;
1117 fromSymbolNum = sectSymNum(fromExternal, entry.fromTarget);
1121 switch ( entry.kind ) {
1122 case ld::Fixup::kindStoreX86BranchPCRel32:
1123 case ld::Fixup::kindStoreTargetAddressX86BranchPCRel32:
1124 case ld::Fixup::kindStoreX86DtraceCallSiteNop:
1125 case ld::Fixup::kindStoreX86DtraceIsEnableSiteClear:
1126 reloc1.set_r_address(address);
1127 reloc1.set_r_symbolnum(symbolNum);
1128 reloc1.set_r_pcrel(true);
1129 reloc1.set_r_length(2);
1130 reloc1.set_r_extern(external);
1131 reloc1.set_r_type(X86_64_RELOC_BRANCH);
1132 relocs.push_back(reloc1);
1135 case ld::Fixup::kindStoreX86BranchPCRel8:
1136 reloc1.set_r_address(address);
1137 reloc1.set_r_symbolnum(symbolNum);
1138 reloc1.set_r_pcrel(true);
1139 reloc1.set_r_length(0);
1140 reloc1.set_r_extern(external);
1141 reloc1.set_r_type(X86_64_RELOC_BRANCH);
1142 relocs.push_back(reloc1);
1145 case ld::Fixup::kindStoreX86PCRel32:
1146 case ld::Fixup::kindStoreTargetAddressX86PCRel32:
1147 reloc1.set_r_address(address);
1148 reloc1.set_r_symbolnum(symbolNum);
1149 reloc1.set_r_pcrel(true);
1150 reloc1.set_r_length(2);
1151 reloc1.set_r_extern(external);
1152 reloc1.set_r_type(X86_64_RELOC_SIGNED);
1153 relocs.push_back(reloc1);
1156 case ld::Fixup::kindStoreX86PCRel32_1:
1157 reloc1.set_r_address(address);
1158 reloc1.set_r_symbolnum(symbolNum);
1159 reloc1.set_r_pcrel(true);
1160 reloc1.set_r_length(2);
1161 reloc1.set_r_extern(external);
1162 reloc1.set_r_type(X86_64_RELOC_SIGNED_1);
1163 relocs.push_back(reloc1);
1166 case ld::Fixup::kindStoreX86PCRel32_2:
1167 reloc1.set_r_address(address);
1168 reloc1.set_r_symbolnum(symbolNum);
1169 reloc1.set_r_pcrel(true);
1170 reloc1.set_r_length(2);
1171 reloc1.set_r_extern(external);
1172 reloc1.set_r_type(X86_64_RELOC_SIGNED_2);
1173 relocs.push_back(reloc1);
1176 case ld::Fixup::kindStoreX86PCRel32_4:
1177 reloc1.set_r_address(address);
1178 reloc1.set_r_symbolnum(symbolNum);
1179 reloc1.set_r_pcrel(true);
1180 reloc1.set_r_length(2);
1181 reloc1.set_r_extern(external);
1182 reloc1.set_r_type(X86_64_RELOC_SIGNED_4);
1183 relocs.push_back(reloc1);
1186 case ld::Fixup::kindStoreX86PCRel32GOTLoad:
1187 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad:
1188 reloc1.set_r_address(address);
1189 reloc1.set_r_symbolnum(symbolNum);
1190 reloc1.set_r_pcrel(true);
1191 reloc1.set_r_length(2);
1192 reloc1.set_r_extern(external);
1193 reloc1.set_r_type(X86_64_RELOC_GOT_LOAD);
1194 relocs.push_back(reloc1);
1197 case ld::Fixup::kindStoreX86PCRel32GOT:
1198 reloc1.set_r_address(address);
1199 reloc1.set_r_symbolnum(symbolNum);
1200 reloc1.set_r_pcrel(true);
1201 reloc1.set_r_length(2);
1202 reloc1.set_r_extern(external);
1203 reloc1.set_r_type(X86_64_RELOC_GOT);
1204 relocs.push_back(reloc1);
1207 case ld::Fixup::kindStoreLittleEndian64:
1208 case ld::Fixup::kindStoreTargetAddressLittleEndian64:
1209 if ( entry.fromTarget != NULL ) {
1210 // this is a pointer-diff
1211 reloc1.set_r_address(address);
1212 reloc1.set_r_symbolnum(symbolNum);
1213 reloc1.set_r_pcrel(false);
1214 reloc1.set_r_length(3);
1215 reloc1.set_r_extern(external);
1216 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1217 reloc2.set_r_address(address);
1218 reloc2.set_r_symbolnum(fromSymbolNum);
1219 reloc2.set_r_pcrel(false);
1220 reloc2.set_r_length(3);
1221 reloc2.set_r_extern(fromExternal);
1222 reloc2.set_r_type(X86_64_RELOC_SUBTRACTOR);
1223 relocs.push_back(reloc2);
1224 relocs.push_back(reloc1);
1228 reloc1.set_r_address(address);
1229 reloc1.set_r_symbolnum(symbolNum);
1230 reloc1.set_r_pcrel(false);
1231 reloc1.set_r_length(3);
1232 reloc1.set_r_extern(external);
1233 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1234 relocs.push_back(reloc1);
1238 case ld::Fixup::kindStoreLittleEndian32:
1239 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1240 if ( entry.fromTarget != NULL ) {
1241 // this is a pointer-diff
1242 reloc1.set_r_address(address);
1243 reloc1.set_r_symbolnum(symbolNum);
1244 reloc1.set_r_pcrel(false);
1245 reloc1.set_r_length(2);
1246 reloc1.set_r_extern(external);
1247 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1248 reloc2.set_r_address(address);
1249 reloc2.set_r_symbolnum(fromSymbolNum);
1250 reloc2.set_r_pcrel(false);
1251 reloc2.set_r_length(2);
1252 reloc2.set_r_extern(fromExternal);
1253 reloc2.set_r_type(X86_64_RELOC_SUBTRACTOR);
1254 relocs.push_back(reloc2);
1255 relocs.push_back(reloc1);
1259 reloc1.set_r_address(address);
1260 reloc1.set_r_symbolnum(symbolNum);
1261 reloc1.set_r_pcrel(false);
1262 reloc1.set_r_length(2);
1263 reloc1.set_r_extern(external);
1264 reloc1.set_r_type(X86_64_RELOC_UNSIGNED);
1265 relocs.push_back(reloc1);
1269 assert(0 && "need to handle -r reloc");
1277 template <typename A>
1278 uint32_t SectionRelocationsAtom<A>::sectSymNum(bool external, const ld::Atom* target)
1280 if ( target->definition() == ld::Atom::definitionAbsolute )
1283 return this->symbolIndex(target); // in external relocations, r_symbolnum field is symbol index
1285 return target->machoSection(); // in non-extern relocations, r_symbolnum is mach-o section index of target
1289 void SectionRelocationsAtom<x86>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1290 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1292 macho_relocation_info<P> reloc1;
1293 macho_relocation_info<P> reloc2;
1294 macho_scattered_relocation_info<P>* sreloc1 = (macho_scattered_relocation_info<P>*)&reloc1;
1295 macho_scattered_relocation_info<P>* sreloc2 = (macho_scattered_relocation_info<P>*)&reloc2;
1296 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1297 bool external = entry.toTargetUsesExternalReloc;
1298 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1299 bool fromExternal = false;
1300 uint32_t fromSymbolNum = 0;
1301 if ( entry.fromTarget != NULL ) {
1302 fromExternal = entry.fromTargetUsesExternalReloc;
1303 fromSymbolNum = sectSymNum(fromExternal, entry.fromTarget);
1307 switch ( entry.kind ) {
1308 case ld::Fixup::kindStoreX86PCRel32:
1309 case ld::Fixup::kindStoreX86BranchPCRel32:
1310 case ld::Fixup::kindStoreTargetAddressX86BranchPCRel32:
1311 case ld::Fixup::kindStoreX86DtraceCallSiteNop:
1312 case ld::Fixup::kindStoreX86DtraceIsEnableSiteClear:
1313 if ( !external && (entry.toAddend != 0) ) {
1314 // use scattered reloc is target offset is non-zero
1315 sreloc1->set_r_scattered(true);
1316 sreloc1->set_r_pcrel(true);
1317 sreloc1->set_r_length(2);
1318 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1319 sreloc1->set_r_address(address);
1320 sreloc1->set_r_value(entry.toTarget->finalAddress());
1323 reloc1.set_r_address(address);
1324 reloc1.set_r_symbolnum(symbolNum);
1325 reloc1.set_r_pcrel(true);
1326 reloc1.set_r_length(2);
1327 reloc1.set_r_extern(external);
1328 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1330 relocs.push_back(reloc1);
1333 case ld::Fixup::kindStoreX86BranchPCRel8:
1334 if ( !external && (entry.toAddend != 0) ) {
1335 // use scattered reloc is target offset is non-zero
1336 sreloc1->set_r_scattered(true);
1337 sreloc1->set_r_pcrel(true);
1338 sreloc1->set_r_length(0);
1339 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1340 sreloc1->set_r_address(address);
1341 sreloc1->set_r_value(entry.toTarget->finalAddress());
1344 reloc1.set_r_address(address);
1345 reloc1.set_r_symbolnum(symbolNum);
1346 reloc1.set_r_pcrel(true);
1347 reloc1.set_r_length(0);
1348 reloc1.set_r_extern(external);
1349 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1351 relocs.push_back(reloc1);
1354 case ld::Fixup::kindStoreX86PCRel16:
1355 if ( !external && (entry.toAddend != 0) ) {
1356 // use scattered reloc is target offset is non-zero
1357 sreloc1->set_r_scattered(true);
1358 sreloc1->set_r_pcrel(true);
1359 sreloc1->set_r_length(1);
1360 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1361 sreloc1->set_r_address(address);
1362 sreloc1->set_r_value(entry.toTarget->finalAddress());
1365 reloc1.set_r_address(address);
1366 reloc1.set_r_symbolnum(symbolNum);
1367 reloc1.set_r_pcrel(true);
1368 reloc1.set_r_length(1);
1369 reloc1.set_r_extern(external);
1370 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1372 relocs.push_back(reloc1);
1375 case ld::Fixup::kindStoreLittleEndian32:
1376 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1377 if ( entry.fromTarget != NULL ) {
1378 // this is a pointer-diff
1379 sreloc1->set_r_scattered(true);
1380 sreloc1->set_r_pcrel(false);
1381 sreloc1->set_r_length(2);
1382 if ( entry.toTarget->scope() == ld::Atom::scopeTranslationUnit )
1383 sreloc1->set_r_type(GENERIC_RELOC_LOCAL_SECTDIFF);
1385 sreloc1->set_r_type(GENERIC_RELOC_SECTDIFF);
1386 sreloc1->set_r_address(address);
1387 if ( entry.toTarget == entry.inAtom )
1388 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.toAddend);
1390 sreloc1->set_r_value(entry.toTarget->finalAddress());
1391 sreloc2->set_r_scattered(true);
1392 sreloc2->set_r_pcrel(false);
1393 sreloc2->set_r_length(2);
1394 sreloc2->set_r_type(GENERIC_RELOC_PAIR);
1395 sreloc2->set_r_address(0);
1396 if ( entry.fromTarget == entry.inAtom ) {
1397 if ( entry.fromAddend > entry.fromTarget->size() )
1398 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.offsetInAtom);
1400 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
1403 sreloc2->set_r_value(entry.fromTarget->finalAddress());
1404 relocs.push_back(reloc1);
1405 relocs.push_back(reloc2);
1409 if ( !external && (entry.toAddend != 0) ) {
1410 // use scattered reloc is target offset is non-zero
1411 sreloc1->set_r_scattered(true);
1412 sreloc1->set_r_pcrel(false);
1413 sreloc1->set_r_length(2);
1414 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1415 sreloc1->set_r_address(address);
1416 sreloc1->set_r_value(entry.toTarget->finalAddress());
1419 reloc1.set_r_address(address);
1420 reloc1.set_r_symbolnum(symbolNum);
1421 reloc1.set_r_pcrel(false);
1422 reloc1.set_r_length(2);
1423 reloc1.set_r_extern(external);
1424 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1426 relocs.push_back(reloc1);
1430 assert(0 && "need to handle -r reloc");
1437 void SectionRelocationsAtom<arm>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1438 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1440 macho_relocation_info<P> reloc1;
1441 macho_relocation_info<P> reloc2;
1442 macho_scattered_relocation_info<P>* sreloc1 = (macho_scattered_relocation_info<P>*)&reloc1;
1443 macho_scattered_relocation_info<P>* sreloc2 = (macho_scattered_relocation_info<P>*)&reloc2;
1444 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1445 bool external = entry.toTargetUsesExternalReloc;
1446 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1447 bool fromExternal = false;
1448 uint32_t fromSymbolNum = 0;
1449 if ( entry.fromTarget != NULL ) {
1450 fromExternal = entry.fromTargetUsesExternalReloc;
1451 fromSymbolNum = sectSymNum(fromExternal, entry.fromTarget);
1455 switch ( entry.kind ) {
1456 case ld::Fixup::kindStoreTargetAddressARMBranch24:
1457 case ld::Fixup::kindStoreARMBranch24:
1458 case ld::Fixup::kindStoreARMDtraceCallSiteNop:
1459 case ld::Fixup::kindStoreARMDtraceIsEnableSiteClear:
1460 if ( !external && (entry.toAddend != 0) ) {
1461 // use scattered reloc is target offset is non-zero
1462 sreloc1->set_r_scattered(true);
1463 sreloc1->set_r_pcrel(true);
1464 sreloc1->set_r_length(2);
1465 sreloc1->set_r_type(ARM_RELOC_BR24);
1466 sreloc1->set_r_address(address);
1467 sreloc1->set_r_value(entry.toTarget->finalAddress());
1470 reloc1.set_r_address(address);
1471 reloc1.set_r_symbolnum(symbolNum);
1472 reloc1.set_r_pcrel(true);
1473 reloc1.set_r_length(2);
1474 reloc1.set_r_extern(external);
1475 reloc1.set_r_type(ARM_RELOC_BR24);
1477 relocs.push_back(reloc1);
1480 case ld::Fixup::kindStoreTargetAddressThumbBranch22:
1481 case ld::Fixup::kindStoreThumbBranch22:
1482 case ld::Fixup::kindStoreThumbDtraceCallSiteNop:
1483 case ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear:
1484 if ( !external && (entry.toAddend != 0) ) {
1485 // use scattered reloc is target offset is non-zero
1486 sreloc1->set_r_scattered(true);
1487 sreloc1->set_r_pcrel(true);
1488 sreloc1->set_r_length(2);
1489 sreloc1->set_r_type(ARM_THUMB_RELOC_BR22);
1490 sreloc1->set_r_address(address);
1491 sreloc1->set_r_value(entry.toTarget->finalAddress());
1494 reloc1.set_r_address(address);
1495 reloc1.set_r_symbolnum(symbolNum);
1496 reloc1.set_r_pcrel(true);
1497 reloc1.set_r_length(2);
1498 reloc1.set_r_extern(external);
1499 reloc1.set_r_type(ARM_THUMB_RELOC_BR22);
1501 relocs.push_back(reloc1);
1504 case ld::Fixup::kindStoreLittleEndian32:
1505 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
1506 if ( entry.fromTarget != NULL ) {
1507 // this is a pointer-diff
1508 sreloc1->set_r_scattered(true);
1509 sreloc1->set_r_pcrel(false);
1510 sreloc1->set_r_length(2);
1511 if ( entry.toTarget->scope() == ld::Atom::scopeTranslationUnit )
1512 sreloc1->set_r_type(ARM_RELOC_LOCAL_SECTDIFF);
1514 sreloc1->set_r_type(ARM_RELOC_SECTDIFF);
1515 sreloc1->set_r_address(address);
1516 if ( entry.toTarget == entry.inAtom )
1517 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.toAddend);
1519 sreloc1->set_r_value(entry.toTarget->finalAddress());
1520 sreloc2->set_r_scattered(true);
1521 sreloc2->set_r_pcrel(false);
1522 sreloc2->set_r_length(2);
1523 sreloc2->set_r_type(ARM_RELOC_PAIR);
1524 sreloc2->set_r_address(0);
1525 if ( entry.fromTarget == entry.inAtom ) {
1526 //unsigned int pcBaseOffset = entry.inAtom->isThumb() ? 4 : 8;
1527 //if ( entry.fromAddend > pcBaseOffset )
1528 // sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend-pcBaseOffset);
1530 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
1533 sreloc2->set_r_value(entry.fromTarget->finalAddress());
1535 relocs.push_back(reloc1);
1536 relocs.push_back(reloc2);
1540 if ( !external && (entry.toAddend != 0) ) {
1541 // use scattered reloc is target offset is non-zero
1542 sreloc1->set_r_scattered(true);
1543 sreloc1->set_r_pcrel(false);
1544 sreloc1->set_r_length(2);
1545 sreloc1->set_r_type(ARM_RELOC_VANILLA);
1546 sreloc1->set_r_address(address);
1547 sreloc1->set_r_value(entry.toTarget->finalAddress());
1550 reloc1.set_r_address(address);
1551 reloc1.set_r_symbolnum(symbolNum);
1552 reloc1.set_r_pcrel(false);
1553 reloc1.set_r_length(2);
1554 reloc1.set_r_extern(external);
1555 reloc1.set_r_type(ARM_RELOC_VANILLA);
1557 relocs.push_back(reloc1);
1561 case ld::Fixup::kindStoreARMLow16:
1562 case ld::Fixup::kindStoreARMHigh16:
1563 case ld::Fixup::kindStoreThumbLow16:
1564 case ld::Fixup::kindStoreThumbHigh16:
1567 uint32_t otherHalf = 0;
1568 uint32_t value = entry.toTarget->finalAddress()+entry.toAddend;
1569 if ( entry.fromTarget != NULL )
1570 value -= (entry.fromTarget->finalAddress()+entry.fromAddend);
1571 switch ( entry.kind ) {
1572 case ld::Fixup::kindStoreARMLow16:
1574 otherHalf = value >> 16;
1576 case ld::Fixup::kindStoreARMHigh16:
1578 otherHalf = value & 0xFFFF;
1580 case ld::Fixup::kindStoreThumbLow16:
1582 otherHalf = value >> 16;
1584 case ld::Fixup::kindStoreThumbHigh16:
1586 otherHalf = value & 0xFFFF;
1591 if ( entry.fromTarget != NULL ) {
1592 // this is a sect-diff
1593 sreloc1->set_r_scattered(true);
1594 sreloc1->set_r_pcrel(false);
1595 sreloc1->set_r_length(len);
1596 sreloc1->set_r_type(ARM_RELOC_HALF_SECTDIFF);
1597 sreloc1->set_r_address(address);
1598 sreloc1->set_r_value(entry.toTarget->finalAddress());
1599 sreloc2->set_r_scattered(true);
1600 sreloc2->set_r_pcrel(false);
1601 sreloc2->set_r_length(len);
1602 sreloc2->set_r_type(ARM_RELOC_PAIR);
1603 sreloc2->set_r_address(otherHalf);
1604 if ( entry.fromTarget == entry.inAtom )
1605 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
1607 sreloc2->set_r_value(entry.fromTarget->finalAddress());
1608 relocs.push_back(reloc1);
1609 relocs.push_back(reloc2);
1612 // this is absolute address
1613 if ( !external && (entry.toAddend != 0) ) {
1614 // use scattered reloc is target offset is non-zero
1615 sreloc1->set_r_scattered(true);
1616 sreloc1->set_r_pcrel(false);
1617 sreloc1->set_r_length(len);
1618 sreloc1->set_r_type(ARM_RELOC_HALF);
1619 sreloc1->set_r_address(address);
1620 sreloc1->set_r_value(entry.toTarget->finalAddress());
1621 reloc2.set_r_address(otherHalf);
1622 reloc2.set_r_symbolnum(0);
1623 reloc2.set_r_pcrel(false);
1624 reloc2.set_r_length(len);
1625 reloc2.set_r_extern(false);
1626 reloc2.set_r_type(ARM_RELOC_PAIR);
1627 relocs.push_back(reloc1);
1628 relocs.push_back(reloc2);
1631 reloc1.set_r_address(address);
1632 reloc1.set_r_symbolnum(symbolNum);
1633 reloc1.set_r_pcrel(false);
1634 reloc1.set_r_length(len);
1635 reloc1.set_r_extern(false);
1636 reloc1.set_r_type(ARM_RELOC_HALF);
1637 reloc2.set_r_address(otherHalf); // other half
1638 reloc2.set_r_symbolnum(0);
1639 reloc2.set_r_pcrel(false);
1640 reloc2.set_r_length(len);
1641 reloc2.set_r_extern(false);
1642 reloc2.set_r_type(ARM_RELOC_PAIR);
1643 relocs.push_back(reloc1);
1644 relocs.push_back(reloc2);
1651 assert(0 && "need to handle -r reloc");
1658 void SectionRelocationsAtom<ppc>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1659 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1661 macho_relocation_info<P> reloc1;
1662 macho_relocation_info<P> reloc2;
1663 macho_scattered_relocation_info<P>* sreloc1 = (macho_scattered_relocation_info<P>*)&reloc1;
1664 macho_scattered_relocation_info<P>* sreloc2 = (macho_scattered_relocation_info<P>*)&reloc2;
1665 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1666 bool external = entry.toTargetUsesExternalReloc;
1667 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1668 bool fromExternal = false;
1669 uint32_t fromSymbolNum = 0;
1670 if ( entry.fromTarget != NULL ) {
1671 fromExternal = entry.fromTargetUsesExternalReloc;
1672 fromSymbolNum= sectSymNum(fromExternal, entry.fromTarget);
1677 switch ( entry.kind ) {
1679 case ld::Fixup::kindStorePPCBranch24:
1680 case ld::Fixup::kindStoreTargetAddressPPCBranch24:
1681 case ld::Fixup::kindStorePPCDtraceCallSiteNop:
1682 case ld::Fixup::kindStorePPCDtraceIsEnableSiteClear:
1683 if ( !external && (entry.toAddend != 0) ) {
1684 // use scattered reloc if target offset is non-zero
1685 sreloc1->set_r_scattered(true);
1686 sreloc1->set_r_pcrel(true);
1687 sreloc1->set_r_length(2);
1688 sreloc1->set_r_type(PPC_RELOC_BR24);
1689 sreloc1->set_r_address(address);
1690 sreloc1->set_r_value(entry.toTarget->finalAddress());
1693 reloc1.set_r_address(address);
1694 reloc1.set_r_symbolnum(symbolNum);
1695 reloc1.set_r_pcrel(true);
1696 reloc1.set_r_length(2);
1697 reloc1.set_r_extern(external);
1698 reloc1.set_r_type(PPC_RELOC_BR24);
1700 relocs.push_back(reloc1);
1703 case ld::Fixup::kindStorePPCBranch14:
1704 if ( !external && (entry.toAddend != 0) ) {
1705 // use scattered reloc if target offset is non-zero
1706 sreloc1->set_r_scattered(true);
1707 sreloc1->set_r_pcrel(true);
1708 sreloc1->set_r_length(2);
1709 sreloc1->set_r_type(PPC_RELOC_BR14);
1710 sreloc1->set_r_address(address);
1711 sreloc1->set_r_value(entry.toTarget->finalAddress());
1714 reloc1.set_r_address(address);
1715 reloc1.set_r_symbolnum(symbolNum);
1716 reloc1.set_r_pcrel(true);
1717 reloc1.set_r_length(2);
1718 reloc1.set_r_extern(external);
1719 reloc1.set_r_type(PPC_RELOC_BR14);
1721 relocs.push_back(reloc1);
1724 case ld::Fixup::kindStoreBigEndian32:
1725 case ld::Fixup::kindStoreTargetAddressBigEndian32:
1726 if ( entry.fromTarget != NULL ) {
1727 // this is a pointer-diff
1728 sreloc1->set_r_scattered(true);
1729 sreloc1->set_r_pcrel(false);
1730 sreloc1->set_r_length(2);
1731 if ( entry.toTarget->scope() == ld::Atom::scopeTranslationUnit )
1732 sreloc1->set_r_type(PPC_RELOC_LOCAL_SECTDIFF);
1734 sreloc1->set_r_type(PPC_RELOC_SECTDIFF);
1735 sreloc1->set_r_address(address);
1736 if ( entry.toTarget == entry.inAtom )
1737 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.toAddend);
1739 sreloc1->set_r_value(entry.toTarget->finalAddress());
1740 sreloc2->set_r_scattered(true);
1741 sreloc2->set_r_pcrel(false);
1742 sreloc2->set_r_length(2);
1743 sreloc2->set_r_type(PPC_RELOC_PAIR);
1744 sreloc2->set_r_address(0);
1745 if ( entry.fromTarget == entry.inAtom ) {
1746 if ( entry.fromAddend > entry.fromTarget->size() )
1747 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.offsetInAtom);
1749 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
1752 sreloc2->set_r_value(entry.fromTarget->finalAddress());
1753 relocs.push_back(reloc1);
1754 relocs.push_back(reloc2);
1758 if ( !external && (entry.toAddend != 0) ) {
1759 // use scattered reloc is target offset is non-zero
1760 sreloc1->set_r_scattered(true);
1761 sreloc1->set_r_pcrel(false);
1762 sreloc1->set_r_length(2);
1763 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
1764 sreloc1->set_r_address(address);
1765 sreloc1->set_r_value(entry.toTarget->finalAddress());
1768 reloc1.set_r_address(address);
1769 reloc1.set_r_symbolnum(symbolNum);
1770 reloc1.set_r_pcrel(false);
1771 reloc1.set_r_length(2);
1772 reloc1.set_r_extern(external);
1773 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
1775 relocs.push_back(reloc1);
1779 case ld::Fixup::kindStorePPCAbsLow14:
1780 case ld::Fixup::kindStorePPCAbsLow16:
1781 if ( !external && (entry.toAddend != 0) ) {
1782 // use scattered reloc if target offset is non-zero
1783 sreloc1->set_r_scattered(true);
1784 sreloc1->set_r_pcrel(false);
1785 sreloc1->set_r_length(2);
1786 sreloc1->set_r_type(entry.kind==ld::Fixup::kindStorePPCAbsLow16 ? PPC_RELOC_LO16 : PPC_RELOC_LO14);
1787 sreloc1->set_r_address(address);
1788 sreloc1->set_r_value(entry.toTarget->finalAddress());
1791 reloc1.set_r_address(address);
1792 reloc1.set_r_symbolnum(symbolNum);
1793 reloc1.set_r_pcrel(false);
1794 reloc1.set_r_length(2);
1795 reloc1.set_r_extern(external);
1796 reloc1.set_r_type(entry.kind==ld::Fixup::kindStorePPCAbsLow16 ? PPC_RELOC_LO16 : PPC_RELOC_LO14);
1799 reloc2.set_r_address(entry.toAddend >> 16);
1801 reloc2.set_r_address((entry.toTarget->finalAddress()+entry.toAddend) >> 16);
1802 reloc2.set_r_symbolnum(0);
1803 reloc2.set_r_pcrel(false);
1804 reloc2.set_r_length(2);
1805 reloc2.set_r_extern(false);
1806 reloc2.set_r_type(PPC_RELOC_PAIR);
1807 relocs.push_back(reloc1);
1808 relocs.push_back(reloc2);
1811 case ld::Fixup::kindStorePPCAbsHigh16:
1812 if ( !external && (entry.toAddend != 0) ) {
1813 // use scattered reloc if target offset is non-zero
1814 sreloc1->set_r_scattered(true);
1815 sreloc1->set_r_pcrel(false);
1816 sreloc1->set_r_length(2);
1817 sreloc1->set_r_type(PPC_RELOC_HI16);
1818 sreloc1->set_r_address(address);
1819 sreloc1->set_r_value(entry.toTarget->finalAddress());
1822 reloc1.set_r_address(address);
1823 reloc1.set_r_symbolnum(symbolNum);
1824 reloc1.set_r_pcrel(false);
1825 reloc1.set_r_length(2);
1826 reloc1.set_r_extern(external);
1827 reloc1.set_r_type(PPC_RELOC_HI16);
1830 reloc2.set_r_address(entry.toAddend & 0x0000FFFF);
1832 reloc2.set_r_address((entry.toTarget->finalAddress()+entry.toAddend) & 0x0000FFFF);
1833 reloc2.set_r_symbolnum(0);
1834 reloc2.set_r_pcrel(false);
1835 reloc2.set_r_length(2);
1836 reloc2.set_r_extern(false);
1837 reloc2.set_r_type(PPC_RELOC_PAIR);
1838 relocs.push_back(reloc1);
1839 relocs.push_back(reloc2);
1842 case ld::Fixup::kindStorePPCAbsHigh16AddLow:
1843 if ( !external && (entry.toAddend != 0) ) {
1844 // use scattered reloc if target offset is non-zero
1845 sreloc1->set_r_scattered(true);
1846 sreloc1->set_r_pcrel(false);
1847 sreloc1->set_r_length(2);
1848 sreloc1->set_r_type(PPC_RELOC_HA16);
1849 sreloc1->set_r_address(address);
1850 sreloc1->set_r_value(entry.toTarget->finalAddress());
1853 reloc1.set_r_address(address);
1854 reloc1.set_r_symbolnum(symbolNum);
1855 reloc1.set_r_pcrel(false);
1856 reloc1.set_r_length(2);
1857 reloc1.set_r_extern(external);
1858 reloc1.set_r_type(PPC_RELOC_HA16);
1861 reloc2.set_r_address(entry.toAddend & 0x0000FFFF);
1863 reloc2.set_r_address((entry.toTarget->finalAddress()+entry.toAddend) & 0x0000FFFF);
1864 reloc2.set_r_symbolnum(0);
1865 reloc2.set_r_pcrel(false);
1866 reloc2.set_r_length(2);
1867 reloc2.set_r_extern(false);
1868 reloc2.set_r_type(PPC_RELOC_PAIR);
1869 relocs.push_back(reloc1);
1870 relocs.push_back(reloc2);
1873 case ld::Fixup::kindStorePPCPicLow14:
1874 case ld::Fixup::kindStorePPCPicLow16:
1875 fromAddr = entry.fromTarget->finalAddress() + entry.fromAddend;
1876 toAddr = entry.toTarget->finalAddress() + entry.toAddend;
1877 sreloc1->set_r_scattered(true);
1878 sreloc1->set_r_pcrel(false);
1879 sreloc1->set_r_length(2);
1880 sreloc1->set_r_type(entry.kind == ld::Fixup::kindStorePPCPicLow16 ? PPC_RELOC_LO16_SECTDIFF : PPC_RELOC_LO14_SECTDIFF);
1881 sreloc1->set_r_address(address);
1882 sreloc1->set_r_value(entry.toTarget->finalAddress());
1883 sreloc2->set_r_scattered(true);
1884 sreloc2->set_r_pcrel(false);
1885 sreloc2->set_r_length(2);
1886 sreloc2->set_r_type(PPC_RELOC_PAIR);
1887 sreloc2->set_r_address(((toAddr-fromAddr) >> 16) & 0xFFFF);
1888 sreloc2->set_r_value(fromAddr);
1889 relocs.push_back(reloc1);
1890 relocs.push_back(reloc2);
1893 case ld::Fixup::kindStorePPCPicHigh16AddLow:
1894 fromAddr = entry.fromTarget->finalAddress() + entry.fromAddend;
1895 toAddr = entry.toTarget->finalAddress() + entry.toAddend;
1896 sreloc1->set_r_scattered(true);
1897 sreloc1->set_r_pcrel(false);
1898 sreloc1->set_r_length(2);
1899 sreloc1->set_r_type(PPC_RELOC_HA16_SECTDIFF);
1900 sreloc1->set_r_address(address);
1901 sreloc1->set_r_value(entry.toTarget->finalAddress());
1902 sreloc2->set_r_scattered(true);
1903 sreloc2->set_r_pcrel(false);
1904 sreloc2->set_r_length(2);
1905 sreloc2->set_r_type(PPC_RELOC_PAIR);
1906 sreloc2->set_r_address((toAddr-fromAddr) & 0xFFFF);
1907 sreloc2->set_r_value(fromAddr);
1908 relocs.push_back(reloc1);
1909 relocs.push_back(reloc2);
1913 assert(0 && "need to handle -r reloc");
1919 void SectionRelocationsAtom<ppc64>::encodeSectionReloc(ld::Internal::FinalSection* sect,
1920 const Entry& entry, std::vector<macho_relocation_info<P> >& relocs)
1922 macho_relocation_info<P> reloc1;
1923 macho_relocation_info<P> reloc2;
1924 macho_scattered_relocation_info<P>* sreloc1 = (macho_scattered_relocation_info<P>*)&reloc1;
1925 macho_scattered_relocation_info<P>* sreloc2 = (macho_scattered_relocation_info<P>*)&reloc2;
1926 uint64_t address = entry.inAtom->finalAddress()+entry.offsetInAtom - sect->address;
1927 bool external = entry.toTargetUsesExternalReloc;
1928 uint32_t symbolNum = sectSymNum(external, entry.toTarget);
1929 bool fromExternal = false;
1930 uint32_t fromSymbolNum = 0;
1931 if ( entry.fromTarget != NULL ) {
1932 fromExternal = entry.fromTargetUsesExternalReloc;
1933 fromSymbolNum= sectSymNum(fromExternal, entry.fromTarget);
1938 switch ( entry.kind ) {
1940 case ld::Fixup::kindStorePPCBranch24:
1941 case ld::Fixup::kindStoreTargetAddressPPCBranch24:
1942 case ld::Fixup::kindStorePPCDtraceCallSiteNop:
1943 case ld::Fixup::kindStorePPCDtraceIsEnableSiteClear:
1944 if ( !external && (entry.toAddend != 0) ) {
1945 // use scattered reloc if target offset is non-zero
1946 sreloc1->set_r_scattered(true);
1947 sreloc1->set_r_pcrel(true);
1948 sreloc1->set_r_length(2);
1949 sreloc1->set_r_type(PPC_RELOC_BR24);
1950 sreloc1->set_r_address(address);
1951 sreloc1->set_r_value(entry.toTarget->finalAddress());
1954 reloc1.set_r_address(address);
1955 reloc1.set_r_symbolnum(symbolNum);
1956 reloc1.set_r_pcrel(true);
1957 reloc1.set_r_length(2);
1958 reloc1.set_r_extern(external);
1959 reloc1.set_r_type(PPC_RELOC_BR24);
1961 relocs.push_back(reloc1);
1964 case ld::Fixup::kindStorePPCBranch14:
1965 if ( !external && (entry.toAddend != 0) ) {
1966 // use scattered reloc if target offset is non-zero
1967 sreloc1->set_r_scattered(true);
1968 sreloc1->set_r_pcrel(true);
1969 sreloc1->set_r_length(2);
1970 sreloc1->set_r_type(PPC_RELOC_BR14);
1971 sreloc1->set_r_address(address);
1972 sreloc1->set_r_value(entry.toTarget->finalAddress());
1975 reloc1.set_r_address(address);
1976 reloc1.set_r_symbolnum(symbolNum);
1977 reloc1.set_r_pcrel(true);
1978 reloc1.set_r_length(2);
1979 reloc1.set_r_extern(external);
1980 reloc1.set_r_type(PPC_RELOC_BR14);
1982 relocs.push_back(reloc1);
1985 case ld::Fixup::kindStoreBigEndian32:
1986 case ld::Fixup::kindStoreTargetAddressBigEndian32:
1987 if ( entry.fromTarget != NULL ) {
1988 // this is a pointer-diff
1989 sreloc1->set_r_scattered(true);
1990 sreloc1->set_r_pcrel(false);
1991 sreloc1->set_r_length(2);
1992 if ( entry.toTarget->scope() == ld::Atom::scopeTranslationUnit )
1993 sreloc1->set_r_type(PPC_RELOC_LOCAL_SECTDIFF);
1995 sreloc1->set_r_type(PPC_RELOC_SECTDIFF);
1996 sreloc1->set_r_address(address);
1997 if ( entry.toTarget == entry.inAtom )
1998 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.toAddend);
2000 sreloc1->set_r_value(entry.toTarget->finalAddress());
2001 sreloc2->set_r_scattered(true);
2002 sreloc2->set_r_pcrel(false);
2003 sreloc2->set_r_length(2);
2004 sreloc2->set_r_type(PPC_RELOC_PAIR);
2005 sreloc2->set_r_address(0);
2006 if ( entry.fromTarget == entry.inAtom ) {
2007 if ( entry.fromAddend > entry.fromTarget->size() )
2008 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.offsetInAtom);
2010 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
2013 sreloc2->set_r_value(entry.fromTarget->finalAddress());
2014 relocs.push_back(reloc1);
2015 relocs.push_back(reloc2);
2019 if ( !external && (entry.toAddend != 0) ) {
2020 // use scattered reloc is target offset is non-zero
2021 sreloc1->set_r_scattered(true);
2022 sreloc1->set_r_pcrel(false);
2023 sreloc1->set_r_length(2);
2024 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
2025 sreloc1->set_r_address(address);
2026 sreloc1->set_r_value(entry.toTarget->finalAddress());
2029 reloc1.set_r_address(address);
2030 reloc1.set_r_symbolnum(symbolNum);
2031 reloc1.set_r_pcrel(false);
2032 reloc1.set_r_length(2);
2033 reloc1.set_r_extern(external);
2034 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
2036 relocs.push_back(reloc1);
2040 case ld::Fixup::kindStoreBigEndian64:
2041 case ld::Fixup::kindStoreTargetAddressBigEndian64:
2042 if ( entry.fromTarget != NULL ) {
2043 // this is a pointer-diff
2044 sreloc1->set_r_scattered(true);
2045 sreloc1->set_r_pcrel(false);
2046 sreloc1->set_r_length(3);
2047 if ( entry.toTarget->scope() == ld::Atom::scopeTranslationUnit )
2048 sreloc1->set_r_type(PPC_RELOC_LOCAL_SECTDIFF);
2050 sreloc1->set_r_type(PPC_RELOC_SECTDIFF);
2051 sreloc1->set_r_address(address);
2052 if ( entry.toTarget == entry.inAtom )
2053 sreloc1->set_r_value(entry.toTarget->finalAddress()+entry.toAddend);
2055 sreloc1->set_r_value(entry.toTarget->finalAddress());
2056 sreloc2->set_r_scattered(true);
2057 sreloc2->set_r_pcrel(false);
2058 sreloc2->set_r_length(3);
2059 sreloc2->set_r_type(PPC_RELOC_PAIR);
2060 sreloc2->set_r_address(0);
2061 if ( entry.fromTarget == entry.inAtom ) {
2062 if ( entry.fromAddend > entry.fromTarget->size() )
2063 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.offsetInAtom);
2065 sreloc2->set_r_value(entry.fromTarget->finalAddress()+entry.fromAddend);
2068 sreloc2->set_r_value(entry.fromTarget->finalAddress());
2069 relocs.push_back(reloc1);
2070 relocs.push_back(reloc2);
2074 if ( !external && (entry.toAddend != 0) ) {
2075 // use scattered reloc is target offset is non-zero
2076 sreloc1->set_r_scattered(true);
2077 sreloc1->set_r_pcrel(false);
2078 sreloc1->set_r_length(3);
2079 sreloc1->set_r_type(GENERIC_RELOC_VANILLA);
2080 sreloc1->set_r_address(address);
2081 sreloc1->set_r_value(entry.toTarget->finalAddress());
2084 reloc1.set_r_address(address);
2085 reloc1.set_r_symbolnum(symbolNum);
2086 reloc1.set_r_pcrel(false);
2087 reloc1.set_r_length(3);
2088 reloc1.set_r_extern(external);
2089 reloc1.set_r_type(GENERIC_RELOC_VANILLA);
2091 relocs.push_back(reloc1);
2095 case ld::Fixup::kindStorePPCAbsLow14:
2096 case ld::Fixup::kindStorePPCAbsLow16:
2097 if ( !external && (entry.toAddend != 0) ) {
2098 // use scattered reloc if target offset is non-zero
2099 sreloc1->set_r_scattered(true);
2100 sreloc1->set_r_pcrel(false);
2101 sreloc1->set_r_length(2);
2102 sreloc1->set_r_type(entry.kind==ld::Fixup::kindStorePPCAbsLow16 ? PPC_RELOC_LO16 : PPC_RELOC_LO14);
2103 sreloc1->set_r_address(address);
2104 sreloc1->set_r_value(entry.toTarget->finalAddress());
2107 reloc1.set_r_address(address);
2108 reloc1.set_r_symbolnum(symbolNum);
2109 reloc1.set_r_pcrel(false);
2110 reloc1.set_r_length(2);
2111 reloc1.set_r_extern(external);
2112 reloc1.set_r_type(entry.kind==ld::Fixup::kindStorePPCAbsLow16 ? PPC_RELOC_LO16 : PPC_RELOC_LO14);
2115 reloc2.set_r_address(entry.toAddend >> 16);
2117 reloc2.set_r_address((entry.toTarget->finalAddress()+entry.toAddend) >> 16);
2118 reloc2.set_r_symbolnum(0);
2119 reloc2.set_r_pcrel(false);
2120 reloc2.set_r_length(2);
2121 reloc2.set_r_extern(false);
2122 reloc2.set_r_type(PPC_RELOC_PAIR);
2123 relocs.push_back(reloc1);
2124 relocs.push_back(reloc2);
2127 case ld::Fixup::kindStorePPCAbsHigh16:
2128 if ( !external && (entry.toAddend != 0) ) {
2129 // use scattered reloc if target offset is non-zero
2130 sreloc1->set_r_scattered(true);
2131 sreloc1->set_r_pcrel(false);
2132 sreloc1->set_r_length(2);
2133 sreloc1->set_r_type(PPC_RELOC_HI16);
2134 sreloc1->set_r_address(address);
2135 sreloc1->set_r_value(entry.toTarget->finalAddress());
2138 reloc1.set_r_address(address);
2139 reloc1.set_r_symbolnum(symbolNum);
2140 reloc1.set_r_pcrel(false);
2141 reloc1.set_r_length(2);
2142 reloc1.set_r_extern(external);
2143 reloc1.set_r_type(PPC_RELOC_HI16);
2146 reloc2.set_r_address(entry.toAddend & 0x0000FFFF);
2148 reloc2.set_r_address((entry.toTarget->finalAddress()+entry.toAddend) & 0x0000FFFF);
2149 reloc2.set_r_symbolnum(0);
2150 reloc2.set_r_pcrel(false);
2151 reloc2.set_r_length(2);
2152 reloc2.set_r_extern(false);
2153 reloc2.set_r_type(PPC_RELOC_PAIR);
2154 relocs.push_back(reloc1);
2155 relocs.push_back(reloc2);
2158 case ld::Fixup::kindStorePPCAbsHigh16AddLow:
2159 if ( !external && (entry.toAddend != 0) ) {
2160 // use scattered reloc if target offset is non-zero
2161 sreloc1->set_r_scattered(true);
2162 sreloc1->set_r_pcrel(false);
2163 sreloc1->set_r_length(2);
2164 sreloc1->set_r_type(PPC_RELOC_HA16);
2165 sreloc1->set_r_address(address);
2166 sreloc1->set_r_value(entry.toTarget->finalAddress());
2169 reloc1.set_r_address(address);
2170 reloc1.set_r_symbolnum(symbolNum);
2171 reloc1.set_r_pcrel(false);
2172 reloc1.set_r_length(2);
2173 reloc1.set_r_extern(external);
2174 reloc1.set_r_type(PPC_RELOC_HA16);
2177 reloc2.set_r_address(entry.toAddend & 0x0000FFFF);
2179 reloc2.set_r_address((entry.toTarget->finalAddress()+entry.toAddend) & 0x0000FFFF);
2180 reloc2.set_r_symbolnum(0);
2181 reloc2.set_r_pcrel(false);
2182 reloc2.set_r_length(2);
2183 reloc2.set_r_extern(false);
2184 reloc2.set_r_type(PPC_RELOC_PAIR);
2185 relocs.push_back(reloc1);
2186 relocs.push_back(reloc2);
2189 case ld::Fixup::kindStorePPCPicLow14:
2190 case ld::Fixup::kindStorePPCPicLow16:
2191 fromAddr = entry.fromTarget->finalAddress() + entry.fromAddend;
2192 toAddr = entry.toTarget->finalAddress() + entry.toAddend;
2193 sreloc1->set_r_scattered(true);
2194 sreloc1->set_r_pcrel(false);
2195 sreloc1->set_r_length(2);
2196 sreloc1->set_r_type(entry.kind == ld::Fixup::kindStorePPCPicLow16 ? PPC_RELOC_LO16_SECTDIFF : PPC_RELOC_LO14_SECTDIFF);
2197 sreloc1->set_r_address(address);
2198 sreloc1->set_r_value(entry.toTarget->finalAddress());
2199 sreloc2->set_r_scattered(true);
2200 sreloc2->set_r_pcrel(false);
2201 sreloc2->set_r_length(2);
2202 sreloc2->set_r_type(PPC_RELOC_PAIR);
2203 sreloc2->set_r_address(((toAddr-fromAddr) >> 16) & 0xFFFF);
2204 sreloc2->set_r_value(fromAddr);
2205 relocs.push_back(reloc1);
2206 relocs.push_back(reloc2);
2209 case ld::Fixup::kindStorePPCPicHigh16AddLow:
2210 fromAddr = entry.fromTarget->finalAddress() + entry.fromAddend;
2211 toAddr = entry.toTarget->finalAddress() + entry.toAddend;
2212 sreloc1->set_r_scattered(true);
2213 sreloc1->set_r_pcrel(false);
2214 sreloc1->set_r_length(2);
2215 sreloc1->set_r_type(PPC_RELOC_HA16_SECTDIFF);
2216 sreloc1->set_r_address(address);
2217 sreloc1->set_r_value(entry.toTarget->finalAddress());
2218 sreloc2->set_r_scattered(true);
2219 sreloc2->set_r_pcrel(false);
2220 sreloc2->set_r_length(2);
2221 sreloc2->set_r_type(PPC_RELOC_PAIR);
2222 sreloc2->set_r_address((toAddr-fromAddr) & 0xFFFF);
2223 sreloc2->set_r_value(fromAddr);
2224 relocs.push_back(reloc1);
2225 relocs.push_back(reloc2);
2229 assert(0 && "need to handle -r reloc");
2234 template <typename A>
2235 void SectionRelocationsAtom<A>::addSectionReloc(ld::Internal::FinalSection* sect, ld::Fixup::Kind kind,
2236 const ld::Atom* inAtom, uint32_t offsetInAtom,
2237 bool toTargetUsesExternalReloc ,bool fromTargetExternalReloc,
2238 const ld::Atom* toTarget, uint64_t toAddend,
2239 const ld::Atom* fromTarget, uint64_t fromAddend)
2243 entry.toTargetUsesExternalReloc = toTargetUsesExternalReloc;
2244 entry.fromTargetUsesExternalReloc = fromTargetExternalReloc;
2245 entry.inAtom = inAtom;
2246 entry.offsetInAtom = offsetInAtom;
2247 entry.toTarget = toTarget;
2248 entry.toAddend = toAddend;
2249 entry.fromTarget = fromTarget;
2250 entry.fromAddend = fromAddend;
2252 static ld::Internal::FinalSection* lastSection = NULL;
2253 static SectionAndEntries* lastSectionAndEntries = NULL;
2255 if ( sect != lastSection ) {
2256 for(typename std::vector<SectionAndEntries>::iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
2257 if ( sect == it->sect ) {
2259 lastSectionAndEntries = &*it;
2263 if ( sect != lastSection ) {
2264 SectionAndEntries tmp;
2266 _entriesBySection.push_back(tmp);
2268 lastSectionAndEntries = &_entriesBySection.back();
2271 lastSectionAndEntries->entries.push_back(entry);
2274 template <typename A>
2275 void SectionRelocationsAtom<A>::encode()
2277 // convert each Entry record to one or two reloc records
2278 for(typename std::vector<SectionAndEntries>::iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
2279 SectionAndEntries& se = *it;
2280 for(typename std::vector<Entry>::iterator eit=se.entries.begin(); eit != se.entries.end(); ++eit) {
2281 encodeSectionReloc(se.sect, *eit, se.relocs);
2285 // update sections with start and count or relocs
2287 for(typename std::vector<SectionAndEntries>::iterator it=_entriesBySection.begin(); it != _entriesBySection.end(); ++it) {
2288 SectionAndEntries& se = *it;
2289 se.sect->relocStart = index;
2290 se.sect->relocCount = se.relocs.size();
2291 index += se.sect->relocCount;
2298 template <typename A>
2299 class IndirectSymbolTableAtom : public ClassicLinkEditAtom
2302 IndirectSymbolTableAtom(const Options& opts, ld::Internal& state, OutputFile& writer)
2303 : ClassicLinkEditAtom(opts, state, writer, _s_section, sizeof(pint_t)) { }
2305 // overrides of ld::Atom
2306 virtual const char* name() const { return "indirect symbol table"; }
2307 virtual uint64_t size() const;
2308 virtual void copyRawContent(uint8_t buffer[]) const;
2309 // overrides of ClassicLinkEditAtom
2310 virtual void encode();
2313 typedef typename A::P P;
2314 typedef typename A::P::E E;
2315 typedef typename A::P::uint_t pint_t;
2317 void encodeStubSection(ld::Internal::FinalSection* sect);
2318 void encodeLazyPointerSection(ld::Internal::FinalSection* sect);
2319 void encodeNonLazyPointerSection(ld::Internal::FinalSection* sect);
2320 uint32_t symIndexOfStubAtom(const ld::Atom*);
2321 uint32_t symIndexOfLazyPointerAtom(const ld::Atom*);
2322 uint32_t symIndexOfNonLazyPointerAtom(const ld::Atom*);
2323 uint32_t symbolIndex(const ld::Atom*);
2324 bool kextBundlesDontHaveIndirectSymbolTable();
2327 std::vector<uint32_t> _entries;
2329 static ld::Section _s_section;
2332 template <typename A>
2333 ld::Section IndirectSymbolTableAtom<A>::_s_section("__LINKEDIT", "__ind_sym_tab", ld::Section::typeLinkEdit, true);
2338 template <typename A>
2339 uint32_t IndirectSymbolTableAtom<A>::symbolIndex(const ld::Atom* atom)
2341 std::map<const ld::Atom*, uint32_t>::iterator pos = this->_writer._atomToSymbolIndex.find(atom);
2342 if ( pos != this->_writer._atomToSymbolIndex.end() )
2344 //fprintf(stderr, "_atomToSymbolIndex content:\n");
2345 //for(std::map<const ld::Atom*, uint32_t>::iterator it = this->_writer._atomToSymbolIndex.begin(); it != this->_writer._atomToSymbolIndex.end(); ++it) {
2346 // fprintf(stderr, "%p(%s) => %d\n", it->first, it->first->name(), it->second);
2348 throwf("internal error: atom not found in symbolIndex(%s)", atom->name());
2351 template <typename A>
2352 uint32_t IndirectSymbolTableAtom<A>::symIndexOfStubAtom(const ld::Atom* stubAtom)
2354 for (ld::Fixup::iterator fit = stubAtom->fixupsBegin(); fit != stubAtom->fixupsEnd(); ++fit) {
2355 if ( fit->binding == ld::Fixup::bindingDirectlyBound ) {
2356 assert((fit->u.target->contentType() == ld::Atom::typeLazyPointer)
2357 || (fit->u.target->contentType() == ld::Atom::typeLazyDylibPointer));
2358 return symIndexOfLazyPointerAtom(fit->u.target);
2361 throw "internal error: stub missing fixup to lazy pointer";
2365 template <typename A>
2366 uint32_t IndirectSymbolTableAtom<A>::symIndexOfLazyPointerAtom(const ld::Atom* lpAtom)
2368 for (ld::Fixup::iterator fit = lpAtom->fixupsBegin(); fit != lpAtom->fixupsEnd(); ++fit) {
2369 if ( fit->kind == ld::Fixup::kindLazyTarget ) {
2370 assert(fit->binding == ld::Fixup::bindingDirectlyBound);
2371 return symbolIndex(fit->u.target);
2374 throw "internal error: lazy pointer missing fixupLazyTarget fixup";
2377 template <typename A>
2378 uint32_t IndirectSymbolTableAtom<A>::symIndexOfNonLazyPointerAtom(const ld::Atom* nlpAtom)
2380 //fprintf(stderr, "symIndexOfNonLazyPointerAtom(%p) %s\n", nlpAtom, nlpAtom->name());
2381 for (ld::Fixup::iterator fit = nlpAtom->fixupsBegin(); fit != nlpAtom->fixupsEnd(); ++fit) {
2382 // non-lazy-pointer to a stripped symbol => no symbol index
2383 if ( fit->clusterSize != ld::Fixup::k1of1 )
2384 return INDIRECT_SYMBOL_LOCAL;
2385 const ld::Atom* target;
2386 switch ( fit->binding ) {
2387 case ld::Fixup::bindingDirectlyBound:
2388 target = fit->u.target;
2390 case ld::Fixup::bindingsIndirectlyBound:
2391 target = _state.indirectBindingTable[fit->u.bindingIndex];
2394 throw "internal error: unexpected non-lazy pointer binding";
2396 // Special case non-lazy-pointer slot used to point to "dyld_stub_binder"
2397 // That slot is never bound using indirect symbol table
2398 if ( target == _state.compressedFastBinderProxy )
2399 return INDIRECT_SYMBOL_ABS;
2400 bool targetIsGlobal = (target->scope() == ld::Atom::scopeGlobal);
2401 switch ( target->definition() ) {
2402 case ld::Atom::definitionRegular:
2403 if ( targetIsGlobal ) {
2404 if ( _options.outputKind() == Options::kObjectFile ) {
2405 // nlpointer to global symbol uses indirect symbol table in .o files
2406 return symbolIndex(target);
2408 else if ( target->combine() == ld::Atom::combineByName ) {
2409 // dyld needs to bind nlpointer to global weak def
2410 return symbolIndex(target);
2412 else if ( _options.nameSpace() != Options::kTwoLevelNameSpace ) {
2413 // dyld needs to bind nlpointer to global def linked for flat namespace
2414 return symbolIndex(target);
2418 case ld::Atom::definitionTentative:
2419 case ld::Atom::definitionAbsolute:
2420 if ( _options.outputKind() == Options::kObjectFile ) {
2421 // tentative def in .o file always uses symbol index
2422 return symbolIndex(target);
2424 // dyld needs to bind nlpointer to global def linked for flat namespace
2425 if ( targetIsGlobal && _options.nameSpace() != Options::kTwoLevelNameSpace )
2426 return symbolIndex(target);
2428 case ld::Atom::definitionProxy:
2429 // dyld needs to bind nlpointer to something in another dylib
2431 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(target->file());
2432 if ( (dylib != NULL) && dylib->willBeLazyLoadedDylib() )
2433 throwf("illegal data reference to %s in lazy loaded dylib %s", target->name(), dylib->path());
2435 return symbolIndex(target);
2438 if ( nlpAtom->fixupsBegin() == nlpAtom->fixupsEnd() ) {
2439 // no fixups means this is the ImageLoader cache slot
2440 return INDIRECT_SYMBOL_ABS;
2443 // The magic index INDIRECT_SYMBOL_LOCAL tells dyld it should does not need to bind
2444 // this non-lazy pointer.
2445 return INDIRECT_SYMBOL_LOCAL;
2450 template <typename A>
2451 void IndirectSymbolTableAtom<A>::encodeStubSection(ld::Internal::FinalSection* sect)
2453 sect->indirectSymTabStartIndex = _entries.size();
2454 sect->indirectSymTabElementSize = sect->atoms[0]->size();
2455 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
2456 _entries.push_back(symIndexOfStubAtom(*ait));
2460 template <typename A>
2461 void IndirectSymbolTableAtom<A>::encodeLazyPointerSection(ld::Internal::FinalSection* sect)
2463 sect->indirectSymTabStartIndex = _entries.size();
2464 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
2465 _entries.push_back(symIndexOfLazyPointerAtom(*ait));
2469 template <typename A>
2470 void IndirectSymbolTableAtom<A>::encodeNonLazyPointerSection(ld::Internal::FinalSection* sect)
2472 sect->indirectSymTabStartIndex = _entries.size();
2473 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
2474 _entries.push_back(symIndexOfNonLazyPointerAtom(*ait));
2478 template <typename A>
2479 bool IndirectSymbolTableAtom<A>::kextBundlesDontHaveIndirectSymbolTable()
2484 template <typename A>
2485 void IndirectSymbolTableAtom<A>::encode()
2487 // static executables should not have an indirect symbol table
2488 if ( this->_options.outputKind() == Options::kStaticExecutable )
2491 // x86_64 kext bundles should not have an indirect symbol table
2492 if ( (this->_options.outputKind() == Options::kKextBundle) && kextBundlesDontHaveIndirectSymbolTable() )
2495 // find all special sections that need a range of the indirect symbol table section
2496 for (std::vector<ld::Internal::FinalSection*>::iterator sit = this->_state.sections.begin(); sit != this->_state.sections.end(); ++sit) {
2497 ld::Internal::FinalSection* sect = *sit;
2498 switch ( sect->type() ) {
2499 case ld::Section::typeStub:
2500 case ld::Section::typeStubClose:
2501 this->encodeStubSection(sect);
2503 case ld::Section::typeLazyPointerClose:
2504 case ld::Section::typeLazyPointer:
2505 case ld::Section::typeLazyDylibPointer:
2506 this->encodeLazyPointerSection(sect);
2508 case ld::Section::typeNonLazyPointer:
2509 this->encodeNonLazyPointerSection(sect);
2517 template <typename A>
2518 uint64_t IndirectSymbolTableAtom<A>::size() const
2520 return _entries.size() * sizeof(uint32_t);
2523 template <typename A>
2524 void IndirectSymbolTableAtom<A>::copyRawContent(uint8_t buffer[]) const
2526 uint32_t* array = (uint32_t*)buffer;
2527 for(unsigned long i=0; i < _entries.size(); ++i) {
2528 E::set32(array[i], _entries[i]);
2542 #endif // __LINKEDIT_CLASSIC_HPP__