1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
3 * Copyright (c) 2005-2011 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 // start temp HACK for cross builds
26 extern "C" double log2 ( double );
28 // end temp HACK for cross builds
32 #include <sys/types.h>
35 #include <sys/sysctl.h>
41 #include <mach/mach_time.h>
42 #include <mach/vm_statistics.h>
43 #include <mach/mach_init.h>
44 #include <mach/mach_host.h>
46 #include <mach-o/dyld.h>
48 #include <AvailabilityMacros.h>
57 #include <unordered_map>
62 #include "MachOFileAbstraction.hpp"
63 #include "Architectures.hpp"
66 #include "InputFiles.h"
68 #include "OutputFile.h"
71 #include "passes/stubs/make_stubs.h"
72 #include "passes/dtrace_dof.h"
73 #include "passes/got.h"
74 #include "passes/tlvp.h"
75 #include "passes/huge.h"
76 #include "passes/compact_unwind.h"
77 #include "passes/order.h"
78 #include "passes/branch_island.h"
79 #include "passes/branch_shim.h"
80 #include "passes/objc.h"
81 #include "passes/dylibs.h"
83 #include "parsers/archive_file.h"
84 #include "parsers/macho_relocatable_file.h"
85 #include "parsers/macho_dylib_file.h"
86 #include "parsers/lto_file.h"
87 #include "parsers/opaque_section_file.h"
90 struct PerformanceStatistics
{
92 uint64_t startInputFileProcessing
;
93 uint64_t startResolver
;
98 vm_statistics_data_t vmStart
;
99 vm_statistics_data_t vmEnd
;
103 class InternalState
: public ld::Internal
106 InternalState(const Options
& opts
) : _options(opts
), _atomsOrderedInSections(false) { }
107 virtual ld::Internal::FinalSection
* addAtom(const ld::Atom
& atom
);
108 virtual ld::Internal::FinalSection
* getFinalSection(const ld::Section
&);
110 uint64_t assignFileOffsets();
111 void setSectionSizesAndAlignments();
113 void markAtomsOrdered() { _atomsOrderedInSections
= true; }
114 virtual ~InternalState() {}
117 class FinalSection
: public ld::Internal::FinalSection
120 FinalSection(const ld::Section
& sect
, uint32_t sectionsSeen
, bool objFile
);
121 static int sectionComparer(const void* l
, const void* r
);
122 static const ld::Section
& outputSection(const ld::Section
& sect
, bool mergeZeroFill
);
123 static const ld::Section
& objectOutputSection(const ld::Section
& sect
, const Options
&);
125 friend class InternalState
;
126 static uint32_t sectionOrder(const ld::Section
& sect
, uint32_t sectionsSeen
);
127 static uint32_t segmentOrder(const ld::Section
& sect
, bool objFile
);
128 uint32_t _segmentOrder
;
129 uint32_t _sectionOrder
;
131 static std::vector
<const char*> _s_segmentsSeen
;
132 static ld::Section _s_DATA_data
;
133 static ld::Section _s_DATA_const
;
134 static ld::Section _s_TEXT_text
;
135 static ld::Section _s_TEXT_const
;
136 static ld::Section _s_DATA_nl_symbol_ptr
;
137 static ld::Section _s_DATA_common
;
138 static ld::Section _s_DATA_zerofill
;
141 bool hasZeroForFileOffset(const ld::Section
* sect
);
142 uint64_t pageAlign(uint64_t addr
);
143 uint64_t pageAlign(uint64_t addr
, uint64_t pageSize
);
146 size_t operator()(const ld::Section
*) const;
148 struct SectionEquals
{
149 bool operator()(const ld::Section
* left
, const ld::Section
* right
) const;
151 typedef std::unordered_map
<const ld::Section
*, FinalSection
*, SectionHash
, SectionEquals
> SectionInToOut
;
154 SectionInToOut _sectionInToFinalMap
;
155 const Options
& _options
;
156 bool _atomsOrderedInSections
;
159 ld::Section
InternalState::FinalSection::_s_DATA_data( "__DATA", "__data", ld::Section::typeUnclassified
);
160 ld::Section
InternalState::FinalSection::_s_DATA_const("__DATA", "__const", ld::Section::typeUnclassified
);
161 ld::Section
InternalState::FinalSection::_s_TEXT_text( "__TEXT", "__text", ld::Section::typeCode
);
162 ld::Section
InternalState::FinalSection::_s_TEXT_const("__TEXT", "__const", ld::Section::typeUnclassified
);
163 ld::Section
InternalState::FinalSection::_s_DATA_nl_symbol_ptr("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer
);
164 ld::Section
InternalState::FinalSection::_s_DATA_common("__DATA", "__common", ld::Section::typeZeroFill
);
165 ld::Section
InternalState::FinalSection::_s_DATA_zerofill("__DATA", "__zerofill", ld::Section::typeZeroFill
);
166 std::vector
<const char*> InternalState::FinalSection::_s_segmentsSeen
;
169 size_t InternalState::SectionHash::operator()(const ld::Section
* sect
) const
172 ld::CStringHash temp
;
173 hash
+= temp
.operator()(sect
->segmentName());
174 hash
+= temp
.operator()(sect
->sectionName());
178 bool InternalState::SectionEquals::operator()(const ld::Section
* left
, const ld::Section
* right
) const
180 return (*left
== *right
);
184 InternalState::FinalSection::FinalSection(const ld::Section
& sect
, uint32_t sectionsSeen
, bool objFile
)
185 : ld::Internal::FinalSection(sect
),
186 _segmentOrder(segmentOrder(sect
, objFile
)),
187 _sectionOrder(sectionOrder(sect
, sectionsSeen
))
189 //fprintf(stderr, "FinalSection(%s, %s) _segmentOrder=%d, _sectionOrder=%d\n",
190 // this->segmentName(), this->sectionName(), _segmentOrder, _sectionOrder);
193 const ld::Section
& InternalState::FinalSection::outputSection(const ld::Section
& sect
, bool mergeZeroFill
)
195 // merge sections in final linked image
196 switch ( sect
.type() ) {
197 case ld::Section::typeLiteral4
:
198 case ld::Section::typeLiteral8
:
199 case ld::Section::typeLiteral16
:
200 return _s_TEXT_const
;
201 case ld::Section::typeUnclassified
:
202 if ( strcmp(sect
.segmentName(), "__DATA") == 0 ) {
203 if ( strcmp(sect
.sectionName(), "__datacoal_nt") == 0 )
205 if ( strcmp(sect
.sectionName(), "__const_coal") == 0 )
206 return _s_DATA_const
;
208 else if ( strcmp(sect
.segmentName(), "__TEXT") == 0 ) {
209 if ( strcmp(sect
.sectionName(), "__const_coal") == 0 )
210 return _s_TEXT_const
;
213 case ld::Section::typeZeroFill
:
215 return _s_DATA_zerofill
;
217 case ld::Section::typeCode
:
218 if ( strcmp(sect
.segmentName(), "__TEXT") == 0 ) {
219 if ( strcmp(sect
.sectionName(), "__textcoal_nt") == 0 )
221 else if ( strcmp(sect
.sectionName(), "__StaticInit") == 0 )
225 case ld::Section::typeNonLazyPointer
:
226 if ( strcmp(sect
.segmentName(), "__DATA") == 0 ) {
227 if ( strcmp(sect
.sectionName(), "__nl_symbol_ptr") == 0 )
228 return _s_DATA_nl_symbol_ptr
;
230 else if ( strcmp(sect
.segmentName(), "__IMPORT") == 0 ) {
231 if ( strcmp(sect
.sectionName(), "__pointers") == 0 )
232 return _s_DATA_nl_symbol_ptr
;
235 case ld::Section::typeTentativeDefs
:
237 return _s_DATA_zerofill
;
239 return _s_DATA_common
;
248 const ld::Section
& InternalState::FinalSection::objectOutputSection(const ld::Section
& sect
, const Options
& options
)
250 const std::vector
<Options::SectionRename
>& renames
= options
.sectionRenames();
251 for ( std::vector
<Options::SectionRename
>::const_iterator it
=renames
.begin(); it
!= renames
.end(); ++it
) {
252 if ( (strcmp(sect
.sectionName(), it
->fromSection
) == 0) && (strcmp(sect
.segmentName(), it
->fromSegment
) == 0) ) {
253 ld::Section
* s
= new ld::Section(it
->toSegment
, it
->toSection
, sect
.type());
259 // in -r mode the only section that ever changes is __tenative -> __common with -d option
260 if ( (sect
.type() == ld::Section::typeTentativeDefs
) && options
.makeTentativeDefinitionsReal())
261 return _s_DATA_common
;
265 uint32_t InternalState::FinalSection::segmentOrder(const ld::Section
& sect
, bool objFile
)
267 if ( strcmp(sect
.segmentName(), "__PAGEZERO") == 0 )
269 if ( strcmp(sect
.segmentName(), "__HEADER") == 0 ) // only used with -preload
271 if ( strcmp(sect
.segmentName(), "__TEXT") == 0 )
273 // in -r mode, want __DATA last so zerofill sections are at end
274 if ( strcmp(sect
.segmentName(), "__DATA") == 0 )
275 return (objFile
? 5 : 2);
276 if ( strcmp(sect
.segmentName(), "__OBJC") == 0 )
278 if ( strcmp(sect
.segmentName(), "__IMPORT") == 0 )
281 // layout non-standard segments in order seen (+10 to shift beyond standard segments)
282 for (uint32_t i
=0; i
< _s_segmentsSeen
.size(); ++i
) {
283 if ( strcmp(_s_segmentsSeen
[i
], sect
.segmentName()) == 0 )
286 _s_segmentsSeen
.push_back(sect
.segmentName());
287 return _s_segmentsSeen
.size()-1+10;
290 uint32_t InternalState::FinalSection::sectionOrder(const ld::Section
& sect
, uint32_t sectionsSeen
)
292 if ( sect
.type() == ld::Section::typeFirstSection
)
294 if ( sect
.type() == ld::Section::typeMachHeader
)
296 if ( sect
.type() == ld::Section::typeLastSection
)
298 if ( strcmp(sect
.segmentName(), "__TEXT") == 0 ) {
299 switch ( sect
.type() ) {
300 case ld::Section::typeCode
:
301 // <rdar://problem/8346444> make __text always be first "code" section
302 if ( strcmp(sect
.sectionName(), "__text") == 0 )
306 case ld::Section::typeStub
:
308 case ld::Section::typeStubHelper
:
310 case ld::Section::typeLSDA
:
312 case ld::Section::typeUnwindInfo
:
314 case ld::Section::typeCFI
:
316 case ld::Section::typeStubClose
:
319 return sectionsSeen
+20;
322 else if ( strcmp(sect
.segmentName(), "__DATA") == 0 ) {
323 switch ( sect
.type() ) {
324 case ld::Section::typeLazyPointerClose
:
326 case ld::Section::typeDyldInfo
:
328 case ld::Section::typeNonLazyPointer
:
330 case ld::Section::typeLazyPointer
:
332 case ld::Section::typeInitializerPointers
:
334 case ld::Section::typeTerminatorPointers
:
336 case ld::Section::typeTLVInitialValues
:
337 return INT_MAX
-4; // need TLV zero-fill to follow TLV init values
338 case ld::Section::typeTLVZeroFill
:
340 case ld::Section::typeZeroFill
:
341 // make sure __huge is always last zerofill section
342 if ( strcmp(sect
.sectionName(), "__huge") == 0 )
347 // <rdar://problem/14348664> __DATA,__const section should be near __mod_init_func not __data
348 if ( strcmp(sect
.sectionName(), "__const") == 0 )
350 // <rdar://problem/7435296> Reorder sections to reduce page faults in object files
351 else if ( strcmp(sect
.sectionName(), "__objc_classlist") == 0 )
353 else if ( strcmp(sect
.sectionName(), "__objc_nlclslist") == 0 )
355 else if ( strcmp(sect
.sectionName(), "__objc_catlist") == 0 )
357 else if ( strcmp(sect
.sectionName(), "__objc_protolist") == 0 )
359 else if ( strcmp(sect
.sectionName(), "__objc_imageinfo") == 0 )
361 else if ( strcmp(sect
.sectionName(), "__objc_const") == 0 )
363 else if ( strcmp(sect
.sectionName(), "__objc_selrefs") == 0 )
365 else if ( strcmp(sect
.sectionName(), "__objc_msgrefs") == 0 )
367 else if ( strcmp(sect
.sectionName(), "__objc_protorefs") == 0 )
369 else if ( strcmp(sect
.sectionName(), "__objc_classrefs") == 0 )
371 else if ( strcmp(sect
.sectionName(), "__objc_superrefs") == 0 )
373 else if ( strcmp(sect
.sectionName(), "__objc_data") == 0 )
376 return sectionsSeen
+40;
379 // make sure zerofill in any other section is at end of segment
380 if ( sect
.type() == ld::Section::typeZeroFill
)
382 return sectionsSeen
+20;
386 static void validateFixups(const ld::Atom
& atom
)
388 //fprintf(stderr, "validateFixups %s\n", atom.name());
389 bool lastWasClusterEnd
= true;
390 ld::Fixup::Cluster lastClusterSize
= ld::Fixup::k1of1
;
391 uint32_t curClusterOffsetInAtom
= 0;
392 for (ld::Fixup::iterator fit
=atom
.fixupsBegin(); fit
!= atom
.fixupsEnd(); ++fit
) {
393 //fprintf(stderr, " fixup offset=%d, cluster=%d\n", fit->offsetInAtom, fit->clusterSize);
394 assert((fit
->offsetInAtom
<= atom
.size()) || (fit
->offsetInAtom
== 0));
395 if ( fit
->firstInCluster() ) {
396 assert(lastWasClusterEnd
);
397 curClusterOffsetInAtom
= fit
->offsetInAtom
;
398 lastWasClusterEnd
= (fit
->clusterSize
== ld::Fixup::k1of1
);
401 assert(!lastWasClusterEnd
);
402 assert(fit
->offsetInAtom
== curClusterOffsetInAtom
);
403 switch ((ld::Fixup::Cluster
)fit
->clusterSize
) {
404 case ld::Fixup::k1of1
:
405 case ld::Fixup::k1of2
:
406 case ld::Fixup::k1of3
:
407 case ld::Fixup::k1of4
:
408 case ld::Fixup::k1of5
:
409 lastWasClusterEnd
= false;
411 case ld::Fixup::k2of2
:
412 assert(lastClusterSize
= ld::Fixup::k1of2
);
413 lastWasClusterEnd
= true;
415 case ld::Fixup::k2of3
:
416 assert(lastClusterSize
= ld::Fixup::k1of3
);
417 lastWasClusterEnd
= false;
419 case ld::Fixup::k2of4
:
420 assert(lastClusterSize
= ld::Fixup::k1of4
);
421 lastWasClusterEnd
= false;
423 case ld::Fixup::k2of5
:
424 assert(lastClusterSize
= ld::Fixup::k1of5
);
425 lastWasClusterEnd
= false;
427 case ld::Fixup::k3of3
:
428 assert(lastClusterSize
= ld::Fixup::k2of3
);
429 lastWasClusterEnd
= true;
431 case ld::Fixup::k3of4
:
432 assert(lastClusterSize
= ld::Fixup::k2of4
);
433 lastWasClusterEnd
= false;
435 case ld::Fixup::k3of5
:
436 assert(lastClusterSize
= ld::Fixup::k2of5
);
437 lastWasClusterEnd
= false;
439 case ld::Fixup::k4of4
:
440 assert(lastClusterSize
= ld::Fixup::k3of4
);
441 lastWasClusterEnd
= true;
443 case ld::Fixup::k4of5
:
444 assert(lastClusterSize
= ld::Fixup::k3of5
);
445 lastWasClusterEnd
= false;
447 case ld::Fixup::k5of5
:
448 assert(lastClusterSize
= ld::Fixup::k4of5
);
449 lastWasClusterEnd
= true;
453 lastClusterSize
= fit
->clusterSize
;
454 if ( fit
->binding
== ld::Fixup::bindingDirectlyBound
) {
455 assert(fit
->u
.target
!= NULL
);
458 switch (lastClusterSize
) {
459 case ld::Fixup::k1of1
:
460 case ld::Fixup::k2of2
:
461 case ld::Fixup::k3of3
:
462 case ld::Fixup::k4of4
:
463 case ld::Fixup::k5of5
:
466 assert(0 && "last fixup was not end of cluster");
472 ld::Internal::FinalSection
* InternalState::addAtom(const ld::Atom
& atom
)
474 ld::Internal::FinalSection
* fs
= this->getFinalSection(atom
.section());
475 //fprintf(stderr, "InternalState::doAtom(%p), name=%s, sect=%s, finalsect=%p\n", &atom, atom.name(), atom.section().sectionName(), fs);
477 validateFixups(atom
);
479 if ( _atomsOrderedInSections
) {
480 // make sure this atom is placed before any trailing section$end$ atom
481 if ( (fs
->atoms
.size() > 1) && (fs
->atoms
.back()->contentType() == ld::Atom::typeSectionEnd
) ) {
482 // last atom in section$end$ atom, insert before it
483 const ld::Atom
* endAtom
= fs
->atoms
.back();
484 fs
->atoms
.pop_back();
485 fs
->atoms
.push_back(&atom
);
486 fs
->atoms
.push_back(endAtom
);
489 // not end atom, just append new atom
490 fs
->atoms
.push_back(&atom
);
495 fs
->atoms
.push_back(&atom
);
500 ld::Internal::FinalSection
* InternalState::getFinalSection(const ld::Section
& inputSection
)
502 const ld::Section
* baseForFinalSection
= &inputSection
;
504 // see if input section already has a FinalSection
505 SectionInToOut::iterator pos
= _sectionInToFinalMap
.find(&inputSection
);
506 if ( pos
!= _sectionInToFinalMap
.end() ) {
510 // otherwise, create a new final section
511 bool objFile
= false;
512 switch ( _options
.outputKind() ) {
513 case Options::kStaticExecutable
:
514 case Options::kDynamicExecutable
:
515 case Options::kDynamicLibrary
:
516 case Options::kDynamicBundle
:
518 case Options::kKextBundle
:
519 case Options::kPreload
:
521 // coalesce some sections
522 const ld::Section
& outSect
= FinalSection::outputSection(inputSection
, _options
.mergeZeroFill());
523 pos
= _sectionInToFinalMap
.find(&outSect
);
524 if ( pos
!= _sectionInToFinalMap
.end() ) {
525 _sectionInToFinalMap
[&inputSection
] = pos
->second
;
526 //fprintf(stderr, "_sectionInToFinalMap[%p] = %p\n", &inputSection, pos->second);
529 else if ( outSect
!= inputSection
) {
530 // new output section created, but not in map
531 baseForFinalSection
= &outSect
;
535 case Options::kObjectFile
:
536 baseForFinalSection
= &FinalSection::objectOutputSection(inputSection
, _options
);
537 pos
= _sectionInToFinalMap
.find(baseForFinalSection
);
538 if ( pos
!= _sectionInToFinalMap
.end() ) {
539 _sectionInToFinalMap
[&inputSection
] = pos
->second
;
540 //fprintf(stderr, "_sectionInToFinalMap[%p] = %p\n", &inputSection, pos->second);
547 InternalState::FinalSection
* result
= new InternalState::FinalSection(*baseForFinalSection
,
548 _sectionInToFinalMap
.size(), objFile
);
549 _sectionInToFinalMap
[baseForFinalSection
] = result
;
550 //fprintf(stderr, "_sectionInToFinalMap[%p] = %p\n", baseForFinalSection, result);
551 sections
.push_back(result
);
556 int InternalState::FinalSection::sectionComparer(const void* l
, const void* r
)
558 const FinalSection
* left
= *(FinalSection
**)l
;
559 const FinalSection
* right
= *(FinalSection
**)r
;
560 if ( left
->_segmentOrder
!= right
->_segmentOrder
)
561 return (left
->_segmentOrder
- right
->_segmentOrder
);
562 return (left
->_sectionOrder
- right
->_sectionOrder
);
565 void InternalState::sortSections()
567 //fprintf(stderr, "UNSORTED final sections:\n");
568 //for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
569 // fprintf(stderr, "final section %p %s/%s\n", (*it), (*it)->segmentName(), (*it)->sectionName());
571 qsort(§ions
[0], sections
.size(), sizeof(FinalSection
*), &InternalState::FinalSection::sectionComparer
);
572 //fprintf(stderr, "SORTED final sections:\n");
573 //for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
574 // fprintf(stderr, "final section %p %s/%s\n", (*it), (*it)->segmentName(), (*it)->sectionName());
576 assert((sections
[0]->type() == ld::Section::typeMachHeader
)
577 || ((sections
[0]->type() == ld::Section::typeFirstSection
) && (sections
[1]->type() == ld::Section::typeMachHeader
))
578 || ((sections
[0]->type() == ld::Section::typePageZero
) && (sections
[1]->type() == ld::Section::typeMachHeader
))
579 || ((sections
[0]->type() == ld::Section::typePageZero
) && (sections
[1]->type() == ld::Section::typeFirstSection
) && (sections
[2]->type() == ld::Section::typeMachHeader
)) );
584 bool InternalState::hasZeroForFileOffset(const ld::Section
* sect
)
586 switch ( sect
->type() ) {
587 case ld::Section::typeZeroFill
:
588 case ld::Section::typeTLVZeroFill
:
589 return _options
.optimizeZeroFill();
590 case ld::Section::typePageZero
:
591 case ld::Section::typeStack
:
592 case ld::Section::typeTentativeDefs
:
600 uint64_t InternalState::pageAlign(uint64_t addr
)
602 const uint64_t alignment
= _options
.segmentAlignment();
603 return ((addr
+alignment
-1) & (-alignment
));
606 uint64_t InternalState::pageAlign(uint64_t addr
, uint64_t pageSize
)
608 return ((addr
+pageSize
-1) & (-pageSize
));
611 void InternalState::setSectionSizesAndAlignments()
613 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
= sections
.begin(); sit
!= sections
.end(); ++sit
) {
614 ld::Internal::FinalSection
* sect
= *sit
;
615 if ( sect
->type() == ld::Section::typeAbsoluteSymbols
) {
616 // absolute symbols need their finalAddress() to their value
617 for (std::vector
<const ld::Atom
*>::iterator ait
= sect
->atoms
.begin(); ait
!= sect
->atoms
.end(); ++ait
) {
618 const ld::Atom
* atom
= *ait
;
619 (const_cast<ld::Atom
*>(atom
))->setSectionOffset(atom
->objectAddress());
623 uint16_t maxAlignment
= 0;
625 for (std::vector
<const ld::Atom
*>::iterator ait
= sect
->atoms
.begin(); ait
!= sect
->atoms
.end(); ++ait
) {
626 const ld::Atom
* atom
= *ait
;
627 bool pagePerAtom
= false;
628 uint32_t atomAlignmentPowerOf2
= atom
->alignment().powerOf2
;
629 uint32_t atomModulus
= atom
->alignment().modulus
;
630 if ( _options
.pageAlignDataAtoms() && ( strcmp(atom
->section().segmentName(), "__DATA") == 0) ) {
631 // most objc sections cannot be padded
632 bool contiguousObjCSection
= ( strncmp(atom
->section().sectionName(), "__objc_", 7) == 0 );
633 if ( strcmp(atom
->section().sectionName(), "__objc_const") == 0 )
634 contiguousObjCSection
= false;
635 if ( strcmp(atom
->section().sectionName(), "__objc_data") == 0 )
636 contiguousObjCSection
= false;
637 switch ( atom
->section().type() ) {
638 case ld::Section::typeUnclassified
:
639 case ld::Section::typeTentativeDefs
:
640 case ld::Section::typeZeroFill
:
641 if ( contiguousObjCSection
)
644 if ( atomAlignmentPowerOf2
< 12 ) {
645 atomAlignmentPowerOf2
= 12;
653 if ( atomAlignmentPowerOf2
> maxAlignment
)
654 maxAlignment
= atomAlignmentPowerOf2
;
655 // calculate section offset for this atom
656 uint64_t alignment
= 1 << atomAlignmentPowerOf2
;
657 uint64_t currentModulus
= (offset
% alignment
);
658 uint64_t requiredModulus
= atomModulus
;
659 if ( currentModulus
!= requiredModulus
) {
660 if ( requiredModulus
> currentModulus
)
661 offset
+= requiredModulus
-currentModulus
;
663 offset
+= requiredModulus
+alignment
-currentModulus
;
665 // LINKEDIT atoms are laid out later
666 if ( sect
->type() != ld::Section::typeLinkEdit
) {
667 (const_cast<ld::Atom
*>(atom
))->setSectionOffset(offset
);
668 offset
+= atom
->size();
670 offset
= (offset
+ 4095) & (-4096); // round up to end of page
673 if ( (atom
->scope() == ld::Atom::scopeGlobal
)
674 && (atom
->definition() == ld::Atom::definitionRegular
)
675 && (atom
->combine() == ld::Atom::combineByName
)
676 && ((atom
->symbolTableInclusion() == ld::Atom::symbolTableIn
)
677 || (atom
->symbolTableInclusion() == ld::Atom::symbolTableInAndNeverStrip
)) ) {
678 this->hasWeakExternalSymbols
= true;
679 if ( _options
.warnWeakExports() )
680 warning("weak external symbol: %s", atom
->name());
684 // section alignment is that of a contained atom with the greatest alignment
685 sect
->alignment
= maxAlignment
;
686 // unless -sectalign command line option overrides
687 if ( _options
.hasCustomSectionAlignment(sect
->segmentName(), sect
->sectionName()) )
688 sect
->alignment
= _options
.customSectionAlignment(sect
->segmentName(), sect
->sectionName());
689 // each atom in __eh_frame has zero alignment to assure they pack together,
690 // but compilers usually make the CFIs pointer sized, so we want whole section
691 // to start on pointer sized boundary.
692 if ( sect
->type() == ld::Section::typeCFI
)
694 if ( sect
->type() == ld::Section::typeTLVDefs
)
695 this->hasThreadLocalVariableDefinitions
= true;
700 uint64_t InternalState::assignFileOffsets()
702 const bool log
= false;
703 const bool hiddenSectionsOccupyAddressSpace
= ((_options
.outputKind() != Options::kObjectFile
)
704 && (_options
.outputKind() != Options::kPreload
));
705 const bool segmentsArePageAligned
= (_options
.outputKind() != Options::kObjectFile
);
707 uint64_t address
= 0;
708 const char* lastSegName
= "";
709 uint64_t floatingAddressStart
= _options
.baseAddress();
711 // first pass, assign addresses to sections in segments with fixed start addresses
712 if ( log
) fprintf(stderr
, "Fixed address segments:\n");
713 for (std::vector
<ld::Internal::FinalSection
*>::iterator it
= sections
.begin(); it
!= sections
.end(); ++it
) {
714 ld::Internal::FinalSection
* sect
= *it
;
715 if ( ! _options
.hasCustomSegmentAddress(sect
->segmentName()) )
717 if ( segmentsArePageAligned
) {
718 if ( strcmp(lastSegName
, sect
->segmentName()) != 0 ) {
719 address
= _options
.customSegmentAddress(sect
->segmentName());
720 lastSegName
= sect
->segmentName();
723 // adjust section address based on alignment
724 uint64_t unalignedAddress
= address
;
725 uint64_t alignment
= (1 << sect
->alignment
);
726 address
= ( (unalignedAddress
+alignment
-1) & (-alignment
) );
728 // update section info
729 sect
->address
= address
;
730 sect
->alignmentPaddingBytes
= (address
- unalignedAddress
);
733 if ( ((address
+ sect
->size
) > _options
.maxAddress()) && (_options
.outputKind() != Options::kObjectFile
)
734 && (_options
.outputKind() != Options::kStaticExecutable
) )
735 throwf("section %s (address=0x%08llX, size=%llu) would make the output executable exceed available address range",
736 sect
->sectionName(), address
, sect
->size
);
738 if ( log
) fprintf(stderr
, " address=0x%08llX, hidden=%d, alignment=%02d, section=%s,%s\n",
739 sect
->address
, sect
->isSectionHidden(), sect
->alignment
, sect
->segmentName(), sect
->sectionName());
740 // update running totals
741 if ( !sect
->isSectionHidden() || hiddenSectionsOccupyAddressSpace
)
742 address
+= sect
->size
;
744 // if TEXT segment address is fixed, then flow other segments after it
745 if ( strcmp(sect
->segmentName(), "__TEXT") == 0 ) {
746 floatingAddressStart
= address
;
750 // second pass, assign section address to sections in segments that are contiguous with previous segment
751 address
= floatingAddressStart
;
753 ld::Internal::FinalSection
* overlappingFixedSection
= NULL
;
754 ld::Internal::FinalSection
* overlappingFlowSection
= NULL
;
755 if ( log
) fprintf(stderr
, "Regular layout segments:\n");
756 for (std::vector
<ld::Internal::FinalSection
*>::iterator it
= sections
.begin(); it
!= sections
.end(); ++it
) {
757 ld::Internal::FinalSection
* sect
= *it
;
758 if ( _options
.hasCustomSegmentAddress(sect
->segmentName()) )
760 if ( (_options
.outputKind() == Options::kPreload
) && (sect
->type() == ld::Section::typeMachHeader
) ) {
761 sect
->alignmentPaddingBytes
= 0;
764 if ( segmentsArePageAligned
) {
765 if ( strcmp(lastSegName
, sect
->segmentName()) != 0 ) {
766 // round up size of last segment if needed
767 if ( *lastSegName
!= '\0' ) {
768 address
= pageAlign(address
, _options
.segPageSize(lastSegName
));
770 // set segment address based on end of last segment
771 address
= pageAlign(address
);
772 lastSegName
= sect
->segmentName();
775 // adjust section address based on alignment
776 uint64_t unalignedAddress
= address
;
777 uint64_t alignment
= (1 << sect
->alignment
);
778 address
= ( (unalignedAddress
+alignment
-1) & (-alignment
) );
780 // update section info
781 sect
->address
= address
;
782 sect
->alignmentPaddingBytes
= (address
- unalignedAddress
);
785 if ( ((address
+ sect
->size
) > _options
.maxAddress()) && (_options
.outputKind() != Options::kObjectFile
)
786 && (_options
.outputKind() != Options::kStaticExecutable
) )
787 throwf("section %s (address=0x%08llX, size=%llu) would make the output executable exceed available address range",
788 sect
->sectionName(), address
, sect
->size
);
790 // sanity check it does not overlap a fixed address segment
791 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
= sections
.begin(); sit
!= sections
.end(); ++sit
) {
792 ld::Internal::FinalSection
* otherSect
= *sit
;
793 if ( ! _options
.hasCustomSegmentAddress(otherSect
->segmentName()) )
795 if ( sect
->address
> otherSect
->address
) {
796 if ( (otherSect
->address
+otherSect
->size
) > sect
->address
) {
797 overlappingFixedSection
= otherSect
;
798 overlappingFlowSection
= sect
;
802 if ( (sect
->address
+sect
->size
) > otherSect
->address
) {
803 overlappingFixedSection
= otherSect
;
804 overlappingFlowSection
= sect
;
809 if ( log
) fprintf(stderr
, " address=0x%08llX, size=0x%08llX, hidden=%d, alignment=%02d, padBytes=%d, section=%s,%s\n",
810 sect
->address
, sect
->size
, sect
->isSectionHidden(), sect
->alignment
, sect
->alignmentPaddingBytes
,
811 sect
->segmentName(), sect
->sectionName());
812 // update running totals
813 if ( !sect
->isSectionHidden() || hiddenSectionsOccupyAddressSpace
)
814 address
+= sect
->size
;
816 if ( overlappingFixedSection
!= NULL
) {
817 fprintf(stderr
, "Section layout:\n");
818 for (std::vector
<ld::Internal::FinalSection
*>::iterator it
= sections
.begin(); it
!= sections
.end(); ++it
) {
819 ld::Internal::FinalSection
* sect
= *it
;
820 if ( sect
->isSectionHidden() )
822 fprintf(stderr
, " address:0x%08llX, alignment:2^%d, size:0x%08llX, padBytes:%d, section:%s/%s\n",
823 sect
->address
, sect
->alignment
, sect
->size
, sect
->alignmentPaddingBytes
,
824 sect
->segmentName(), sect
->sectionName());
827 throwf("Section (%s/%s) overlaps fixed address section (%s/%s)",
828 overlappingFlowSection
->segmentName(), overlappingFlowSection
->sectionName(),
829 overlappingFixedSection
->segmentName(), overlappingFixedSection
->sectionName());
833 // third pass, assign section file offsets
834 uint64_t fileOffset
= 0;
836 if ( log
) fprintf(stderr
, "All segments with file offsets:\n");
837 for (std::vector
<ld::Internal::FinalSection
*>::iterator it
= sections
.begin(); it
!= sections
.end(); ++it
) {
838 ld::Internal::FinalSection
* sect
= *it
;
839 if ( hasZeroForFileOffset(sect
) ) {
840 // fileoff of zerofill sections is moot, but historically it is set to zero
841 sect
->fileOffset
= 0;
843 // <rdar://problem/10445047> align file offset with address layout
844 fileOffset
+= sect
->alignmentPaddingBytes
;
847 // page align file offset at start of each segment
848 if ( segmentsArePageAligned
&& (*lastSegName
!= '\0') && (strcmp(lastSegName
, sect
->segmentName()) != 0) ) {
849 fileOffset
= pageAlign(fileOffset
, _options
.segPageSize(lastSegName
));
851 lastSegName
= sect
->segmentName();
853 // align file offset with address layout
854 fileOffset
+= sect
->alignmentPaddingBytes
;
856 // update section info
857 sect
->fileOffset
= fileOffset
;
859 // update running total
860 fileOffset
+= sect
->size
;
863 if ( log
) fprintf(stderr
, " fileoffset=0x%08llX, address=0x%08llX, hidden=%d, size=%lld, alignment=%02d, section=%s,%s\n",
864 sect
->fileOffset
, sect
->address
, sect
->isSectionHidden(), sect
->size
, sect
->alignment
,
865 sect
->segmentName(), sect
->sectionName());
869 // for encrypted iPhoneOS apps
870 if ( _options
.makeEncryptable() ) {
871 // remember end of __TEXT for later use by load command
872 for (std::vector
<ld::Internal::FinalSection
*>::iterator it
= state
.sections
.begin(); it
!= state
.sections
.end(); ++it
) {
873 ld::Internal::FinalSection
* sect
= *it
;
874 if ( strcmp(sect
->segmentName(), "__TEXT") == 0 ) {
875 _encryptedTEXTendOffset
= pageAlign(sect
->fileOffset
+ sect
->size
);
881 // return total file size
885 static char* commatize(uint64_t in
, char* out
)
889 sprintf(rawNum
, "%llu", in
);
890 const int rawNumLen
= strlen(rawNum
);
891 for(int i
=0; i
< rawNumLen
-1; ++i
) {
893 if ( ((rawNumLen
-i
) % 3) == 1 )
896 *out
++ = rawNum
[rawNumLen
-1];
901 static void printTime(const char* msg
, uint64_t partTime
, uint64_t totalTime
)
903 static uint64_t sUnitsPerSecond
= 0;
904 if ( sUnitsPerSecond
== 0 ) {
905 struct mach_timebase_info timeBaseInfo
;
906 if ( mach_timebase_info(&timeBaseInfo
) != KERN_SUCCESS
)
908 sUnitsPerSecond
= 1000000000ULL * timeBaseInfo
.denom
/ timeBaseInfo
.numer
;
910 if ( partTime
< sUnitsPerSecond
) {
911 uint32_t milliSecondsTimeTen
= (partTime
*10000)/sUnitsPerSecond
;
912 uint32_t milliSeconds
= milliSecondsTimeTen
/10;
913 uint32_t percentTimesTen
= (partTime
*1000)/totalTime
;
914 uint32_t percent
= percentTimesTen
/10;
915 fprintf(stderr
, "%24s: % 4d.%d milliseconds (% 4d.%d%%)\n", msg
, milliSeconds
, milliSecondsTimeTen
-milliSeconds
*10, percent
, percentTimesTen
-percent
*10);
918 uint32_t secondsTimeTen
= (partTime
*10)/sUnitsPerSecond
;
919 uint32_t seconds
= secondsTimeTen
/10;
920 uint32_t percentTimesTen
= (partTime
*1000)/totalTime
;
921 uint32_t percent
= percentTimesTen
/10;
922 fprintf(stderr
, "%24s: % 4d.%d seconds (% 4d.%d%%)\n", msg
, seconds
, secondsTimeTen
-seconds
*10, percent
, percentTimesTen
-percent
*10);
927 static void getVMInfo(vm_statistics_data_t
& info
)
929 mach_msg_type_number_t count
= sizeof(vm_statistics_data_t
) / sizeof(natural_t
);
930 kern_return_t error
= host_statistics(mach_host_self(), HOST_VM_INFO
,
931 (host_info_t
)&info
, &count
);
932 if (error
!= KERN_SUCCESS
) {
933 bzero(&info
, sizeof(vm_statistics_data_t
));
939 static const char* sOverridePathlibLTO
= NULL
;
942 // This is magic glue that overrides the default behaviour
943 // of lazydylib1.o which is used to lazily load libLTO.dylib.
945 extern "C" const char* dyld_lazy_dylib_path_fix(const char* path
);
946 const char* dyld_lazy_dylib_path_fix(const char* path
)
948 if ( sOverridePathlibLTO
!= NULL
)
949 return sOverridePathlibLTO
;
956 int main(int argc
, const char* argv
[])
958 const char* archName
= NULL
;
959 bool showArch
= false;
960 bool archInferred
= false;
962 PerformanceStatistics statistics
;
963 statistics
.startTool
= mach_absolute_time();
965 // create object to track command line arguments
966 Options
options(argc
, argv
);
967 InternalState
state(options
);
969 // allow libLTO to be overridden by command line -lto_library
970 sOverridePathlibLTO
= options
.overridePathlibLTO();
973 if ( options
.printStatistics() )
974 getVMInfo(statistics
.vmStart
);
976 // update strings for error messages
977 showArch
= options
.printArchPrefix();
978 archName
= options
.architectureName();
979 archInferred
= (options
.architecture() == 0);
981 // open and parse input files
982 statistics
.startInputFileProcessing
= mach_absolute_time();
983 ld::tool::InputFiles
inputFiles(options
, &archName
);
985 // load and resolve all references
986 statistics
.startResolver
= mach_absolute_time();
987 ld::tool::Resolver
resolver(options
, inputFiles
, state
);
991 statistics
.startDylibs
= mach_absolute_time();
992 inputFiles
.dylibs(state
);
994 // do initial section sorting so passes have rough idea of the layout
995 state
.sortSections();
998 statistics
.startPasses
= mach_absolute_time();
999 ld::passes::objc::doPass(options
, state
);
1000 ld::passes::stubs::doPass(options
, state
);
1001 ld::passes::huge::doPass(options
, state
);
1002 ld::passes::got::doPass(options
, state
);
1003 ld::passes::tlvp::doPass(options
, state
);
1004 ld::passes::dylibs::doPass(options
, state
); // must be after stubs and GOT passes
1005 ld::passes::order::doPass(options
, state
);
1006 state
.markAtomsOrdered();
1007 ld::passes::branch_shim::doPass(options
, state
); // must be after stubs
1008 ld::passes::branch_island::doPass(options
, state
); // must be after stubs and order pass
1009 ld::passes::dtrace::doPass(options
, state
);
1010 ld::passes::compact_unwind::doPass(options
, state
); // must be after order pass
1012 // sort final sections
1013 state
.sortSections();
1015 // write output file
1016 statistics
.startOutput
= mach_absolute_time();
1017 ld::tool::OutputFile
out(options
);
1019 statistics
.startDone
= mach_absolute_time();
1022 //mach_o::relocatable::printCounts();
1023 if ( options
.printStatistics() ) {
1024 getVMInfo(statistics
.vmEnd
);
1025 uint64_t totalTime
= statistics
.startDone
- statistics
.startTool
;
1026 printTime("ld total time", totalTime
, totalTime
);
1027 printTime(" option parsing time", statistics
.startInputFileProcessing
- statistics
.startTool
, totalTime
);
1028 printTime(" object file processing", statistics
.startResolver
- statistics
.startInputFileProcessing
,totalTime
);
1029 printTime(" resolve symbols", statistics
.startDylibs
- statistics
.startResolver
, totalTime
);
1030 printTime(" build atom list", statistics
.startPasses
- statistics
.startDylibs
, totalTime
);
1031 printTime(" passess", statistics
.startOutput
- statistics
.startPasses
, totalTime
);
1032 printTime(" write output", statistics
.startDone
- statistics
.startOutput
, totalTime
);
1033 fprintf(stderr
, "pageins=%u, pageouts=%u, faults=%u\n",
1034 statistics
.vmEnd
.pageins
-statistics
.vmStart
.pageins
,
1035 statistics
.vmEnd
.pageouts
-statistics
.vmStart
.pageouts
,
1036 statistics
.vmEnd
.faults
-statistics
.vmStart
.faults
);
1038 fprintf(stderr
, "processed %3u object files, totaling %15s bytes\n", inputFiles
._totalObjectLoaded
, commatize(inputFiles
._totalObjectSize
, temp
));
1039 fprintf(stderr
, "processed %3u archive files, totaling %15s bytes\n", inputFiles
._totalArchivesLoaded
, commatize(inputFiles
._totalArchiveSize
, temp
));
1040 fprintf(stderr
, "processed %3u dylib files\n", inputFiles
._totalDylibsLoaded
);
1041 fprintf(stderr
, "wrote output file totaling %15s bytes\n", commatize(out
.fileSize(), temp
));
1043 // <rdar://problem/6780050> Would like linker warning to be build error.
1044 if ( options
.errorBecauseOfWarnings() ) {
1045 fprintf(stderr
, "ld: fatal warning(s) induced error (-fatal_warnings)\n");
1049 catch (const char* msg
) {
1051 fprintf(stderr
, "ld: %s for inferred architecture %s\n", msg
, archName
);
1052 else if ( showArch
)
1053 fprintf(stderr
, "ld: %s for architecture %s\n", msg
, archName
);
1055 fprintf(stderr
, "ld: %s\n", msg
);
1064 // implement assert() function to print out a backtrace before aborting
1065 void __assert_rtn(const char* func
, const char* file
, int line
, const char* failedexpr
)
1067 Snapshot
*snapshot
= Snapshot::globalSnapshot
;
1069 snapshot
->setSnapshotMode(Snapshot::SNAPSHOT_DEBUG
);
1070 snapshot
->createSnapshot();
1071 snapshot
->recordAssertionMessage("Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr
, func
, file
, line
);
1073 void* callStack
[128];
1074 int depth
= ::backtrace(callStack
, 128);
1075 char* buffer
= (char*)malloc(1024);
1076 for(int i
=0; i
< depth
-1; ++i
) {
1078 dladdr(callStack
[i
], &info
);
1079 const char* symboName
= info
.dli_sname
;
1080 if ( (symboName
!= NULL
) && (strncmp(symboName
, "_Z", 2) == 0) ) {
1081 size_t bufLen
= 1024;
1083 char* unmangled
= abi::__cxa_demangle(symboName
, buffer
, &bufLen
, &result
);
1084 if ( unmangled
!= NULL
)
1085 symboName
= unmangled
;
1087 long offset
= (uintptr_t)callStack
[i
] - (uintptr_t)info
.dli_saddr
;
1088 fprintf(stderr
, "%d %p %s + %ld\n", i
, callStack
[i
], symboName
, offset
);
1089 snapshot
->recordAssertionMessage("%d %p %s + %ld\n", i
, callStack
[i
], symboName
, offset
);
1091 fprintf(stderr
, "A linker snapshot was created at:\n\t%s\n", snapshot
->rootDir());
1092 fprintf(stderr
, "ld: Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr
, func
, file
, line
);