#include "passes/branch_shim.h"
#include "passes/objc.h"
#include "passes/dylibs.h"
+#include "passes/bitcode_bundle.h"
#include "parsers/archive_file.h"
#include "parsers/macho_relocatable_file.h"
void setSectionSizesAndAlignments();
void sortSections();
void markAtomsOrdered() { _atomsOrderedInSections = true; }
+ bool hasReferenceToWeakExternal(const ld::Atom& atom);
+
virtual ~InternalState() {}
private:
static ld::Section _s_DATA_nl_symbol_ptr;
static ld::Section _s_DATA_common;
static ld::Section _s_DATA_zerofill;
+ static ld::Section _s_DATA_DIRTY_data;
+ static ld::Section _s_DATA_CONST_const;
};
bool hasZeroForFileOffset(const ld::Section* sect);
ld::Section InternalState::FinalSection::_s_DATA_nl_symbol_ptr("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer);
ld::Section InternalState::FinalSection::_s_DATA_common("__DATA", "__common", ld::Section::typeZeroFill);
ld::Section InternalState::FinalSection::_s_DATA_zerofill("__DATA", "__zerofill", ld::Section::typeZeroFill);
+ld::Section InternalState::FinalSection::_s_DATA_DIRTY_data( "__DATA_DIRTY", "__data", ld::Section::typeUnclassified);
+ld::Section InternalState::FinalSection::_s_DATA_CONST_const( "__DATA_CONST", "__const", ld::Section::typeUnclassified);
+
std::vector<const char*> InternalState::FinalSection::_s_segmentsSeen;
if ( strcmp(sect.sectionName(), "__const_coal") == 0 )
return _s_TEXT_const;
}
+ else if ( strcmp(sect.segmentName(), "__DATA_DIRTY") == 0 ) {
+ if ( strcmp(sect.sectionName(), "__datacoal_nt") == 0 )
+ return _s_DATA_DIRTY_data;
+ }
+ else if ( strcmp(sect.segmentName(), "__DATA_CONST") == 0 ) {
+ if ( strcmp(sect.sectionName(), "__const_coal") == 0 )
+ return _s_DATA_CONST_const;
+ }
break;
case ld::Section::typeZeroFill:
if ( mergeZeroFill )
if ( sect.type() == ld::Section::typeLastSection )
return INT_MAX;
const std::vector<const char*>* sectionList = options.sectionOrder(sect.segmentName());
- if ( (options.outputKind() == Options::kPreload) && (sectionList != NULL) ) {
+ if ( ((options.outputKind() == Options::kPreload) || (options.outputKind() == Options::kDyld)) && (sectionList != NULL) ) {
uint32_t count = 10;
for (std::vector<const char*>::const_iterator it=sectionList->begin(); it != sectionList->end(); ++it, ++count) {
if ( strcmp(*it, sect.sectionName()) == 0 )
return sectionsSeen+20;
}
}
- else if ( strcmp(sect.segmentName(), "__DATA") == 0 ) {
+ else if ( strncmp(sect.segmentName(), "__DATA", 6) == 0 ) {
switch ( sect.type() ) {
case ld::Section::typeLazyPointerClose:
return 8;
}
#endif
+bool InternalState::hasReferenceToWeakExternal(const ld::Atom& atom)
+{
+ // if __DATA,__const atom has pointer to weak external symbol, don't move to __DATA_CONST
+ const ld::Atom* target = NULL;
+ for (ld::Fixup::iterator fit=atom.fixupsBegin(); fit != atom.fixupsEnd(); ++fit) {
+ if ( fit->firstInCluster() ) {
+ target = NULL;
+ }
+ switch ( fit->binding ) {
+ case ld::Fixup::bindingNone:
+ case ld::Fixup::bindingByNameUnbound:
+ break;
+ case ld::Fixup::bindingByContentBound:
+ case ld::Fixup::bindingDirectlyBound:
+ target = fit->u.target;
+ break;
+ case ld::Fixup::bindingsIndirectlyBound:
+ target = indirectBindingTable[fit->u.bindingIndex];
+ break;
+ }
+ if ( (target != NULL) && (target->definition() == ld::Atom::definitionRegular)
+ && (target->combine() == ld::Atom::combineByName) && (target->scope() == ld::Atom::scopeGlobal) ) {
+ return true;
+ }
+ }
+ return false;
+}
+
ld::Internal::FinalSection* InternalState::addAtom(const ld::Atom& atom)
{
ld::Internal::FinalSection* fs = NULL;
if ( _options.moveRwSymbol(atom.name(), path, dstSeg, wildCardMatch) ) {
if ( (sectType != ld::Section::typeZeroFill)
&& (sectType != ld::Section::typeUnclassified)
- && (sectType != ld::Section::typeTentativeDefs) ) {
+ && (sectType != ld::Section::typeTentativeDefs)
+ && (sectType != ld::Section::typeDyldInfo) ) {
if ( !wildCardMatch )
warning("cannot move symbol '%s' from file %s to segment '%s' because symbol is not data (is %d)", atom.name(), path, dstSeg, sectType);
}
}
}
if ( (fs == NULL) && _options.moveRoSymbol(atom.name(), path, dstSeg, wildCardMatch) ) {
- if ( atom.section().type() != ld::Section::typeCode ) {
+ if ( (sectType != ld::Section::typeCode)
+ && (sectType != ld::Section::typeUnclassified) ) {
if ( !wildCardMatch )
warning("cannot move symbol '%s' from file %s to segment '%s' because symbol is not code (is %d)", atom.name(), path, dstSeg, sectType);
}
const std::vector<Options::SegmentRename>& segRenames = _options.segmentRenames();
for ( std::vector<Options::SectionRename>::const_iterator it=sectRenames.begin(); it != sectRenames.end(); ++it) {
if ( (strcmp(sectName, it->fromSection) == 0) && (strcmp(atom.section().segmentName(), it->fromSegment) == 0) ) {
- if ( _options.traceSymbolLayout() )
- printf("symbol '%s', -rename_section mapped it to %s/%s\n", atom.name(), it->toSegment, it->toSection);
- fs = this->getFinalSection(it->toSegment, it->toSection, sectType);
+ if ( _options.useDataConstSegment() && (strcmp(sectName, "__const") == 0)
+ && (strcmp(atom.section().segmentName(), "__DATA") == 0) && hasReferenceToWeakExternal(atom) ) {
+ // if __DATA,__const atom has pointer to weak external symbol, don't move to __DATA_CONST
+ fs = this->getFinalSection("__DATA", "__const_weak", sectType);
+ if ( _options.traceSymbolLayout() )
+ printf("symbol '%s', contains pointers to weak symbols, so mapped it to __DATA/_const_weak\n", atom.name());
+ }
+ else if ( _options.useDataConstSegment() && (sectType == ld::Section::typeNonLazyPointer) && hasReferenceToWeakExternal(atom) ) {
+ // if __DATA,__nl_symbol_ptr atom has pointer to weak external symbol, don't move to __DATA_CONST
+ fs = this->getFinalSection("__DATA", "__got_weak", sectType);
+ if ( _options.traceSymbolLayout() )
+ printf("symbol '%s', contains pointers to weak symbols, so mapped it to __DATA/__got_weak\n", atom.name());
+ }
+ else {
+ fs = this->getFinalSection(it->toSegment, it->toSection, sectType);
+ if ( _options.traceSymbolLayout() )
+ printf("symbol '%s', -rename_section mapped it to %s/%s\n", atom.name(), fs->segmentName(), fs->sectionName());
+ }
}
}
if ( fs == NULL ) {
}
}
}
-
-
+
// if no override, use default location
if ( fs == NULL ) {
fs = this->getFinalSection(atom.section());
// normal case
fs->atoms.push_back(&atom);
}
+ this->atomToSection[&atom] = fs;
return fs;
}
bool pagePerAtom = false;
uint32_t atomAlignmentPowerOf2 = atom->alignment().powerOf2;
uint32_t atomModulus = atom->alignment().modulus;
- if ( _options.pageAlignDataAtoms() && ( strcmp(atom->section().segmentName(), "__DATA") == 0) ) {
+ if ( _options.pageAlignDataAtoms() && ( strncmp(atom->section().segmentName(), "__DATA", 6) == 0) ) {
// most objc sections cannot be padded
bool contiguousObjCSection = ( strncmp(atom->section().sectionName(), "__objc_", 7) == 0 );
if ( strcmp(atom->section().sectionName(), "__objc_const") == 0 )
uint64_t address = 0;
const char* lastSegName = "";
uint64_t floatingAddressStart = _options.baseAddress();
+ bool haveFixedSegments = false;
+ // mark all sections as not having an address yet
+ for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
+ ld::Internal::FinalSection* sect = *it;
+ sect->alignmentPaddingBytes = 0;
+ sect->address = ULLONG_MAX;
+ }
+
// first pass, assign addresses to sections in segments with fixed start addresses
if ( log ) fprintf(stderr, "Fixed address segments:\n");
for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
ld::Internal::FinalSection* sect = *it;
if ( ! _options.hasCustomSegmentAddress(sect->segmentName()) )
continue;
+ haveFixedSegments = true;
if ( segmentsArePageAligned ) {
if ( strcmp(lastSegName, sect->segmentName()) != 0 ) {
address = _options.customSegmentAddress(sect->segmentName());
floatingAddressStart = address;
}
}
-
- // second pass, assign section address to sections in segments that are contiguous with previous segment
+
+ // second pass, assign section addresses to sections in segments that are ordered after a segment with a fixed address
+ if ( haveFixedSegments && !_options.segmentOrder().empty() ) {
+ if ( log ) fprintf(stderr, "After Fixed address segments:\n");
+ lastSegName = "";
+ ld::Internal::FinalSection* lastSect = NULL;
+ for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
+ ld::Internal::FinalSection* sect = *it;
+ if ( (sect->address == ULLONG_MAX) && _options.segmentOrderAfterFixedAddressSegment(sect->segmentName()) ) {
+ address = lastSect->address + lastSect->size;
+ if ( (strcmp(lastSegName, sect->segmentName()) != 0) && segmentsArePageAligned ) {
+ // round up size of last segment
+ address = pageAlign(address, _options.segPageSize(lastSegName));
+ }
+ // adjust section address based on alignment
+ uint64_t unalignedAddress = address;
+ uint64_t alignment = (1 << sect->alignment);
+ address = ( (unalignedAddress+alignment-1) & (-alignment) );
+ sect->alignmentPaddingBytes = (address - unalignedAddress);
+ sect->address = address;
+ if ( log ) fprintf(stderr, " address=0x%08llX, hidden=%d, alignment=%02d, section=%s,%s\n",
+ sect->address, sect->isSectionHidden(), sect->alignment, sect->segmentName(), sect->sectionName());
+ // update running totals
+ if ( !sect->isSectionHidden() || hiddenSectionsOccupyAddressSpace )
+ address += sect->size;
+ }
+ lastSegName = sect->segmentName();
+ lastSect = sect;
+ }
+ }
+
+ // last pass, assign addresses to remaining sections
address = floatingAddressStart;
lastSegName = "";
ld::Internal::FinalSection* overlappingFixedSection = NULL;
if ( log ) fprintf(stderr, "Regular layout segments:\n");
for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
ld::Internal::FinalSection* sect = *it;
- if ( _options.hasCustomSegmentAddress(sect->segmentName()) )
+ if ( sect->address != ULLONG_MAX )
continue;
if ( (_options.outputKind() == Options::kPreload) && (sect->type() == ld::Section::typeMachHeader) ) {
sect->alignmentPaddingBytes = 0;
lastSegName = sect->segmentName();
}
}
+
// adjust section address based on alignment
uint64_t unalignedAddress = address;
uint64_t alignment = (1 << sect->alignment);
ld::passes::branch_island::doPass(options, state); // must be after stubs and order pass
ld::passes::dtrace::doPass(options, state);
ld::passes::compact_unwind::doPass(options, state); // must be after order pass
-
+ ld::passes::bitcode_bundle::doPass(options, state); // must be after dylib
+
// sort final sections
state.sortSections();