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 <ext/hash_map>
58 #include <ext/hash_set>
63 #include "MachOFileAbstraction.hpp"
64 #include "Architectures.hpp"
67 #include "InputFiles.h"
69 #include "OutputFile.h"
72 #include "passes/stubs/make_stubs.h"
73 #include "passes/dtrace_dof.h"
74 #include "passes/got.h"
75 #include "passes/tlvp.h"
76 #include "passes/huge.h"
77 #include "passes/compact_unwind.h"
78 #include "passes/order.h"
79 #include "passes/branch_island.h"
80 #include "passes/branch_shim.h"
81 #include "passes/objc.h"
82 #include "passes/dylibs.h"
84 #include "parsers/archive_file.h"
85 #include "parsers/macho_relocatable_file.h"
86 #include "parsers/macho_dylib_file.h"
87 #include "parsers/lto_file.h"
88 #include "parsers/opaque_section_file.h"
91 struct PerformanceStatistics
{
93 uint64_t startInputFileProcessing
;
94 uint64_t startResolver
;
99 vm_statistics_data_t vmStart
;
100 vm_statistics_data_t vmEnd
;
107 class InternalState
: public ld::Internal
110 InternalState(const Options
& opts
) : _options(opts
), _atomsOrderedInSections(false) { }
111 virtual ld::Internal::FinalSection
* addAtom(const ld::Atom
& atom
);
112 virtual ld::Internal::FinalSection
* getFinalSection(const ld::Section
&);
115 void markAtomsOrdered() { _atomsOrderedInSections
= true; }
116 virtual ~InternalState() {}
119 class FinalSection
: public ld::Internal::FinalSection
122 FinalSection(const ld::Section
& sect
, uint32_t sectionsSeen
, bool objFile
);
123 static int sectionComparer(const void* l
, const void* r
);
124 static const ld::Section
& outputSection(const ld::Section
& sect
, bool mergeZeroFill
);
125 static const ld::Section
& objectOutputSection(const ld::Section
& sect
, bool makeTentativeDefsReal
);
127 friend class InternalState
;
128 static uint32_t sectionOrder(const ld::Section
& sect
, uint32_t sectionsSeen
);
129 static uint32_t segmentOrder(const ld::Section
& sect
, bool objFile
);
130 uint32_t _segmentOrder
;
131 uint32_t _sectionOrder
;
133 static std::vector
<const char*> _s_segmentsSeen
;
134 static ld::Section _s_DATA_data
;
135 static ld::Section _s_DATA_const
;
136 static ld::Section _s_TEXT_text
;
137 static ld::Section _s_TEXT_const
;
138 static ld::Section _s_DATA_nl_symbol_ptr
;
139 static ld::Section _s_DATA_common
;
140 static ld::Section _s_DATA_zerofill
;
145 size_t operator()(const ld::Section
*) const;
147 struct SectionEquals
{
148 bool operator()(const ld::Section
* left
, const ld::Section
* right
) const;
150 typedef __gnu_cxx::hash_map
<const ld::Section
*, FinalSection
*, SectionHash
, SectionEquals
> SectionInToOut
;
153 SectionInToOut _sectionInToFinalMap
;
154 const Options
& _options
;
155 bool _atomsOrderedInSections
;
158 ld::Section
InternalState::FinalSection::_s_DATA_data( "__DATA", "__data", ld::Section::typeUnclassified
);
159 ld::Section
InternalState::FinalSection::_s_DATA_const("__DATA", "__const", ld::Section::typeUnclassified
);
160 ld::Section
InternalState::FinalSection::_s_TEXT_text( "__TEXT", "__text", ld::Section::typeCode
);
161 ld::Section
InternalState::FinalSection::_s_TEXT_const("__TEXT", "__const", ld::Section::typeUnclassified
);
162 ld::Section
InternalState::FinalSection::_s_DATA_nl_symbol_ptr("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer
);
163 ld::Section
InternalState::FinalSection::_s_DATA_common("__DATA", "__common", ld::Section::typeZeroFill
);
164 ld::Section
InternalState::FinalSection::_s_DATA_zerofill("__DATA", "__zerofill", ld::Section::typeZeroFill
);
165 std::vector
<const char*> InternalState::FinalSection::_s_segmentsSeen
;
168 size_t InternalState::SectionHash::operator()(const ld::Section
* sect
) const
171 __gnu_cxx::hash
<const char*> temp
;
172 hash
+= temp
.operator()(sect
->segmentName());
173 hash
+= temp
.operator()(sect
->sectionName());
177 bool InternalState::SectionEquals::operator()(const ld::Section
* left
, const ld::Section
* right
) const
179 return (*left
== *right
);
183 InternalState::FinalSection::FinalSection(const ld::Section
& sect
, uint32_t sectionsSeen
, bool objFile
)
184 : ld::Internal::FinalSection(sect
),
185 _segmentOrder(segmentOrder(sect
, objFile
)),
186 _sectionOrder(sectionOrder(sect
, sectionsSeen
))
188 //fprintf(stderr, "FinalSection(%s, %s) _segmentOrder=%d, _sectionOrder=%d\n",
189 // this->segmentName(), this->sectionName(), _segmentOrder, _sectionOrder);
192 const ld::Section
& InternalState::FinalSection::outputSection(const ld::Section
& sect
, bool mergeZeroFill
)
194 // merge sections in final linked image
195 switch ( sect
.type() ) {
196 case ld::Section::typeLiteral4
:
197 case ld::Section::typeLiteral8
:
198 case ld::Section::typeLiteral16
:
199 return _s_TEXT_const
;
200 case ld::Section::typeUnclassified
:
201 if ( strcmp(sect
.segmentName(), "__DATA") == 0 ) {
202 if ( strcmp(sect
.sectionName(), "__datacoal_nt") == 0 )
204 if ( strcmp(sect
.sectionName(), "__const_coal") == 0 )
205 return _s_DATA_const
;
207 else if ( strcmp(sect
.segmentName(), "__TEXT") == 0 ) {
208 if ( strcmp(sect
.sectionName(), "__const_coal") == 0 )
209 return _s_TEXT_const
;
212 case ld::Section::typeZeroFill
:
214 return _s_DATA_zerofill
;
216 case ld::Section::typeCode
:
217 if ( strcmp(sect
.segmentName(), "__TEXT") == 0 ) {
218 if ( strcmp(sect
.sectionName(), "__textcoal_nt") == 0 )
220 else if ( strcmp(sect
.sectionName(), "__StaticInit") == 0 )
224 case ld::Section::typeNonLazyPointer
:
225 if ( strcmp(sect
.segmentName(), "__DATA") == 0 ) {
226 if ( strcmp(sect
.sectionName(), "__nl_symbol_ptr") == 0 )
227 return _s_DATA_nl_symbol_ptr
;
229 else if ( strcmp(sect
.segmentName(), "__IMPORT") == 0 ) {
230 if ( strcmp(sect
.sectionName(), "__pointers") == 0 )
231 return _s_DATA_nl_symbol_ptr
;
234 case ld::Section::typeTentativeDefs
:
236 return _s_DATA_zerofill
;
238 return _s_DATA_common
;
247 const ld::Section
& InternalState::FinalSection::objectOutputSection(const ld::Section
& sect
, bool makeTentativeDefsReal
)
249 // in -r mode the only section that ever changes is __tenative -> __common with -d option
250 if ( (sect
.type() == ld::Section::typeTentativeDefs
) && makeTentativeDefsReal
)
251 return _s_DATA_common
;
255 uint32_t InternalState::FinalSection::segmentOrder(const ld::Section
& sect
, bool objFile
)
257 if ( strcmp(sect
.segmentName(), "__PAGEZERO") == 0 )
259 if ( strcmp(sect
.segmentName(), "__HEADER") == 0 ) // only used with -preload
261 if ( strcmp(sect
.segmentName(), "__TEXT") == 0 )
263 // in -r mode, want __DATA last so zerofill sections are at end
264 if ( strcmp(sect
.segmentName(), "__DATA") == 0 )
265 return (objFile
? 5 : 2);
266 if ( strcmp(sect
.segmentName(), "__OBJC") == 0 )
268 if ( strcmp(sect
.segmentName(), "__IMPORT") == 0 )
271 // layout non-standard segments in order seen (+10 to shift beyond standard segments)
272 for (uint32_t i
=0; i
< _s_segmentsSeen
.size(); ++i
) {
273 if ( strcmp(_s_segmentsSeen
[i
], sect
.segmentName()) == 0 )
276 _s_segmentsSeen
.push_back(sect
.segmentName());
277 return _s_segmentsSeen
.size()-1+10;
280 uint32_t InternalState::FinalSection::sectionOrder(const ld::Section
& sect
, uint32_t sectionsSeen
)
282 if ( sect
.type() == ld::Section::typeFirstSection
)
284 if ( sect
.type() == ld::Section::typeMachHeader
)
286 if ( sect
.type() == ld::Section::typeLastSection
)
288 if ( strcmp(sect
.segmentName(), "__TEXT") == 0 ) {
289 switch ( sect
.type() ) {
290 case ld::Section::typeCode
:
291 // <rdar://problem/8346444> make __text always be first "code" section
292 if ( strcmp(sect
.sectionName(), "__text") == 0 )
296 case ld::Section::typeStub
:
298 case ld::Section::typeStubHelper
:
300 case ld::Section::typeLSDA
:
302 case ld::Section::typeUnwindInfo
:
304 case ld::Section::typeCFI
:
306 case ld::Section::typeStubClose
:
309 return sectionsSeen
+20;
312 else if ( strcmp(sect
.segmentName(), "__DATA") == 0 ) {
313 switch ( sect
.type() ) {
314 case ld::Section::typeLazyPointerClose
:
316 case ld::Section::typeDyldInfo
:
318 case ld::Section::typeNonLazyPointer
:
320 case ld::Section::typeLazyPointer
:
322 case ld::Section::typeInitializerPointers
:
324 case ld::Section::typeTerminatorPointers
:
326 case ld::Section::typeTLVInitialValues
:
327 return INT_MAX
-4; // need TLV zero-fill to follow TLV init values
328 case ld::Section::typeTLVZeroFill
:
330 case ld::Section::typeZeroFill
:
331 // make sure __huge is always last zerofill section
332 if ( strcmp(sect
.sectionName(), "__huge") == 0 )
337 // <rdar://problem/7435296> Reorder sections to reduce page faults in object files
338 if ( strcmp(sect
.sectionName(), "__objc_classlist") == 0 )
340 else if ( strcmp(sect
.sectionName(), "__objc_nlclslist") == 0 )
342 else if ( strcmp(sect
.sectionName(), "__objc_catlist") == 0 )
344 else if ( strcmp(sect
.sectionName(), "__objc_protolist") == 0 )
346 else if ( strcmp(sect
.sectionName(), "__objc_imageinfo") == 0 )
348 else if ( strcmp(sect
.sectionName(), "__objc_const") == 0 )
350 else if ( strcmp(sect
.sectionName(), "__objc_selrefs") == 0 )
352 else if ( strcmp(sect
.sectionName(), "__objc_msgrefs") == 0 )
354 else if ( strcmp(sect
.sectionName(), "__objc_protorefs") == 0 )
356 else if ( strcmp(sect
.sectionName(), "__objc_classrefs") == 0 )
358 else if ( strcmp(sect
.sectionName(), "__objc_superrefs") == 0 )
360 else if ( strcmp(sect
.sectionName(), "__objc_data") == 0 )
363 return sectionsSeen
+40;
366 // make sure zerofill in any other section is at end of segment
367 if ( sect
.type() == ld::Section::typeZeroFill
)
369 return sectionsSeen
+20;
373 static void validateFixups(const ld::Atom
& atom
)
375 //fprintf(stderr, "validateFixups %s\n", atom.name());
376 bool lastWasClusterEnd
= true;
377 ld::Fixup::Cluster lastClusterSize
= ld::Fixup::k1of1
;
378 uint32_t curClusterOffsetInAtom
= 0;
379 for (ld::Fixup::iterator fit
=atom
.fixupsBegin(); fit
!= atom
.fixupsEnd(); ++fit
) {
380 //fprintf(stderr, " fixup offset=%d, cluster=%d\n", fit->offsetInAtom, fit->clusterSize);
381 assert((fit
->offsetInAtom
<= atom
.size()) || (fit
->offsetInAtom
== 0));
382 if ( fit
->firstInCluster() ) {
383 assert(lastWasClusterEnd
);
384 curClusterOffsetInAtom
= fit
->offsetInAtom
;
385 lastWasClusterEnd
= (fit
->clusterSize
== ld::Fixup::k1of1
);
388 assert(!lastWasClusterEnd
);
389 assert(fit
->offsetInAtom
== curClusterOffsetInAtom
);
390 switch ((ld::Fixup::Cluster
)fit
->clusterSize
) {
391 case ld::Fixup::k1of1
:
392 case ld::Fixup::k1of2
:
393 case ld::Fixup::k1of3
:
394 case ld::Fixup::k1of4
:
395 case ld::Fixup::k1of5
:
396 lastWasClusterEnd
= false;
398 case ld::Fixup::k2of2
:
399 assert(lastClusterSize
= ld::Fixup::k1of2
);
400 lastWasClusterEnd
= true;
402 case ld::Fixup::k2of3
:
403 assert(lastClusterSize
= ld::Fixup::k1of3
);
404 lastWasClusterEnd
= false;
406 case ld::Fixup::k2of4
:
407 assert(lastClusterSize
= ld::Fixup::k1of4
);
408 lastWasClusterEnd
= false;
410 case ld::Fixup::k2of5
:
411 assert(lastClusterSize
= ld::Fixup::k1of5
);
412 lastWasClusterEnd
= false;
414 case ld::Fixup::k3of3
:
415 assert(lastClusterSize
= ld::Fixup::k2of3
);
416 lastWasClusterEnd
= true;
418 case ld::Fixup::k3of4
:
419 assert(lastClusterSize
= ld::Fixup::k2of4
);
420 lastWasClusterEnd
= false;
422 case ld::Fixup::k3of5
:
423 assert(lastClusterSize
= ld::Fixup::k2of5
);
424 lastWasClusterEnd
= false;
426 case ld::Fixup::k4of4
:
427 assert(lastClusterSize
= ld::Fixup::k3of4
);
428 lastWasClusterEnd
= true;
430 case ld::Fixup::k4of5
:
431 assert(lastClusterSize
= ld::Fixup::k3of5
);
432 lastWasClusterEnd
= false;
434 case ld::Fixup::k5of5
:
435 assert(lastClusterSize
= ld::Fixup::k4of5
);
436 lastWasClusterEnd
= true;
440 lastClusterSize
= fit
->clusterSize
;
441 if ( fit
->binding
== ld::Fixup::bindingDirectlyBound
) {
442 assert(fit
->u
.target
!= NULL
);
445 switch (lastClusterSize
) {
446 case ld::Fixup::k1of1
:
447 case ld::Fixup::k2of2
:
448 case ld::Fixup::k3of3
:
449 case ld::Fixup::k4of4
:
450 case ld::Fixup::k5of5
:
453 assert(0 && "last fixup was not end of cluster");
459 ld::Internal::FinalSection
* InternalState::addAtom(const ld::Atom
& atom
)
461 ld::Internal::FinalSection
* fs
= this->getFinalSection(atom
.section());
462 //fprintf(stderr, "InternalState::doAtom(%p), name=%s, sect=%s, finalsect=%p\n", &atom, atom.name(), atom.section().sectionName(), fs);
464 validateFixups(atom
);
466 if ( _atomsOrderedInSections
) {
467 // make sure this atom is placed before any trailing section$end$ atom
468 if ( (fs
->atoms
.size() > 1) && (fs
->atoms
.back()->contentType() == ld::Atom::typeSectionEnd
) ) {
469 // last atom in section$end$ atom, insert before it
470 const ld::Atom
* endAtom
= fs
->atoms
.back();
471 fs
->atoms
.pop_back();
472 fs
->atoms
.push_back(&atom
);
473 fs
->atoms
.push_back(endAtom
);
476 // not end atom, just append new atom
477 fs
->atoms
.push_back(&atom
);
482 fs
->atoms
.push_back(&atom
);
487 ld::Internal::FinalSection
* InternalState::getFinalSection(const ld::Section
& inputSection
)
489 const ld::Section
* baseForFinalSection
= &inputSection
;
491 // see if input section already has a FinalSection
492 SectionInToOut::iterator pos
= _sectionInToFinalMap
.find(&inputSection
);
493 if ( pos
!= _sectionInToFinalMap
.end() ) {
497 // otherwise, create a new final section
498 bool objFile
= false;
499 switch ( _options
.outputKind() ) {
500 case Options::kStaticExecutable
:
501 case Options::kDynamicExecutable
:
502 case Options::kDynamicLibrary
:
503 case Options::kDynamicBundle
:
505 case Options::kKextBundle
:
506 case Options::kPreload
:
508 // coalesce some sections
509 const ld::Section
& outSect
= FinalSection::outputSection(inputSection
, _options
.mergeZeroFill());
510 pos
= _sectionInToFinalMap
.find(&outSect
);
511 if ( pos
!= _sectionInToFinalMap
.end() ) {
512 _sectionInToFinalMap
[&inputSection
] = pos
->second
;
513 //fprintf(stderr, "_sectionInToFinalMap[%p] = %p\n", &inputSection, pos->second);
516 else if ( outSect
!= inputSection
) {
517 // new output section created, but not in map
518 baseForFinalSection
= &outSect
;
522 case Options::kObjectFile
:
523 baseForFinalSection
= &FinalSection::objectOutputSection(inputSection
, _options
.makeTentativeDefinitionsReal());
524 pos
= _sectionInToFinalMap
.find(baseForFinalSection
);
525 if ( pos
!= _sectionInToFinalMap
.end() ) {
526 _sectionInToFinalMap
[&inputSection
] = pos
->second
;
527 //fprintf(stderr, "_sectionInToFinalMap[%p] = %p\n", &inputSection, pos->second);
534 InternalState::FinalSection
* result
= new InternalState::FinalSection(*baseForFinalSection
,
535 _sectionInToFinalMap
.size(), objFile
);
536 _sectionInToFinalMap
[baseForFinalSection
] = result
;
537 //fprintf(stderr, "_sectionInToFinalMap[%p] = %p\n", baseForFinalSection, result);
538 sections
.push_back(result
);
543 int InternalState::FinalSection::sectionComparer(const void* l
, const void* r
)
545 const FinalSection
* left
= *(FinalSection
**)l
;
546 const FinalSection
* right
= *(FinalSection
**)r
;
547 if ( left
->_segmentOrder
!= right
->_segmentOrder
)
548 return (left
->_segmentOrder
- right
->_segmentOrder
);
549 return (left
->_sectionOrder
- right
->_sectionOrder
);
552 void InternalState::sortSections()
554 //fprintf(stderr, "UNSORTED final sections:\n");
555 //for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
556 // fprintf(stderr, "final section %p %s/%s\n", (*it), (*it)->segmentName(), (*it)->sectionName());
558 qsort(§ions
[0], sections
.size(), sizeof(FinalSection
*), &InternalState::FinalSection::sectionComparer
);
559 //fprintf(stderr, "SORTED final sections:\n");
560 //for (std::vector<ld::Internal::FinalSection*>::iterator it = sections.begin(); it != sections.end(); ++it) {
561 // fprintf(stderr, "final section %p %s/%s\n", (*it), (*it)->segmentName(), (*it)->sectionName());
563 assert((sections
[0]->type() == ld::Section::typeMachHeader
)
564 || ((sections
[0]->type() == ld::Section::typeFirstSection
) && (sections
[1]->type() == ld::Section::typeMachHeader
))
565 || ((sections
[0]->type() == ld::Section::typePageZero
) && (sections
[1]->type() == ld::Section::typeMachHeader
))
566 || ((sections
[0]->type() == ld::Section::typePageZero
) && (sections
[1]->type() == ld::Section::typeFirstSection
) && (sections
[2]->type() == ld::Section::typeMachHeader
)) );
570 static char* commatize(uint64_t in
, char* out
)
574 sprintf(rawNum
, "%llu", in
);
575 const int rawNumLen
= strlen(rawNum
);
576 for(int i
=0; i
< rawNumLen
-1; ++i
) {
578 if ( ((rawNumLen
-i
) % 3) == 1 )
581 *out
++ = rawNum
[rawNumLen
-1];
586 static void printTime(const char* msg
, uint64_t partTime
, uint64_t totalTime
)
588 static uint64_t sUnitsPerSecond
= 0;
589 if ( sUnitsPerSecond
== 0 ) {
590 struct mach_timebase_info timeBaseInfo
;
591 if ( mach_timebase_info(&timeBaseInfo
) == KERN_SUCCESS
) {
592 sUnitsPerSecond
= 1000000000ULL * timeBaseInfo
.denom
/ timeBaseInfo
.numer
;
593 //fprintf(stderr, "sUnitsPerSecond=%llu\n", sUnitsPerSecond);
596 if ( partTime
< sUnitsPerSecond
) {
597 uint32_t milliSecondsTimeTen
= (partTime
*10000)/sUnitsPerSecond
;
598 uint32_t milliSeconds
= milliSecondsTimeTen
/10;
599 uint32_t percentTimesTen
= (partTime
*1000)/totalTime
;
600 uint32_t percent
= percentTimesTen
/10;
601 fprintf(stderr
, "%24s: % 4d.%d milliseconds (% 4d.%d%%)\n", msg
, milliSeconds
, milliSecondsTimeTen
-milliSeconds
*10, percent
, percentTimesTen
-percent
*10);
604 uint32_t secondsTimeTen
= (partTime
*10)/sUnitsPerSecond
;
605 uint32_t seconds
= secondsTimeTen
/10;
606 uint32_t percentTimesTen
= (partTime
*1000)/totalTime
;
607 uint32_t percent
= percentTimesTen
/10;
608 fprintf(stderr
, "%24s: % 4d.%d seconds (% 4d.%d%%)\n", msg
, seconds
, secondsTimeTen
-seconds
*10, percent
, percentTimesTen
-percent
*10);
613 static void getVMInfo(vm_statistics_data_t
& info
)
615 mach_msg_type_number_t count
= sizeof(vm_statistics_data_t
) / sizeof(natural_t
);
616 kern_return_t error
= host_statistics(mach_host_self(), HOST_VM_INFO
,
617 (host_info_t
)&info
, &count
);
618 if (error
!= KERN_SUCCESS
) {
619 bzero(&info
, sizeof(vm_statistics_data_t
));
625 static const char* sOverridePathlibLTO
= NULL
;
628 // This is magic glue that overrides the default behaviour
629 // of lazydylib1.o which is used to lazily load libLTO.dylib.
631 extern "C" const char* dyld_lazy_dylib_path_fix(const char* path
);
632 const char* dyld_lazy_dylib_path_fix(const char* path
)
634 if ( sOverridePathlibLTO
!= NULL
)
635 return sOverridePathlibLTO
;
642 int main(int argc
, const char* argv
[])
644 const char* archName
= NULL
;
645 bool showArch
= false;
646 bool archInferred
= false;
648 PerformanceStatistics statistics
;
649 statistics
.startTool
= mach_absolute_time();
651 // create object to track command line arguments
652 Options
options(argc
, argv
);
653 InternalState
state(options
);
655 // allow libLTO to be overridden by command line -lto_library
656 sOverridePathlibLTO
= options
.overridePathlibLTO();
659 if ( options
.printStatistics() )
660 getVMInfo(statistics
.vmStart
);
662 // update strings for error messages
663 showArch
= options
.printArchPrefix();
664 archName
= options
.architectureName();
665 archInferred
= (options
.architecture() == 0);
667 // open and parse input files
668 statistics
.startInputFileProcessing
= mach_absolute_time();
669 ld::tool::InputFiles
inputFiles(options
, &archName
);
671 // load and resolve all references
672 statistics
.startResolver
= mach_absolute_time();
673 ld::tool::Resolver
resolver(options
, inputFiles
, state
);
677 statistics
.startDylibs
= mach_absolute_time();
678 inputFiles
.dylibs(state
);
680 // do initial section sorting so passes have rough idea of the layout
681 state
.sortSections();
684 statistics
.startPasses
= mach_absolute_time();
685 ld::passes::objc::doPass(options
, state
);
686 ld::passes::stubs::doPass(options
, state
);
687 ld::passes::huge::doPass(options
, state
);
688 ld::passes::got::doPass(options
, state
);
689 ld::passes::tlvp::doPass(options
, state
);
690 ld::passes::dylibs::doPass(options
, state
); // must be after stubs and GOT passes
691 ld::passes::order::doPass(options
, state
);
692 state
.markAtomsOrdered();
693 ld::passes::branch_shim::doPass(options
, state
); // must be after stubs
694 ld::passes::branch_island::doPass(options
, state
); // must be after stubs and order pass
695 ld::passes::dtrace::doPass(options
, state
);
696 ld::passes::compact_unwind::doPass(options
, state
); // must be after order pass
698 // sort final sections
699 state
.sortSections();
702 statistics
.startOutput
= mach_absolute_time();
703 ld::tool::OutputFile
out(options
);
705 statistics
.startDone
= mach_absolute_time();
708 //mach_o::relocatable::printCounts();
709 if ( options
.printStatistics() ) {
710 getVMInfo(statistics
.vmEnd
);
711 uint64_t totalTime
= statistics
.startDone
- statistics
.startTool
;
712 printTime("ld total time", totalTime
, totalTime
);
713 printTime(" option parsing time", statistics
.startInputFileProcessing
- statistics
.startTool
, totalTime
);
714 printTime(" object file processing", statistics
.startResolver
- statistics
.startInputFileProcessing
,totalTime
);
715 printTime(" resolve symbols", statistics
.startDylibs
- statistics
.startResolver
, totalTime
);
716 printTime(" build atom list", statistics
.startPasses
- statistics
.startDylibs
, totalTime
);
717 printTime(" passess", statistics
.startOutput
- statistics
.startPasses
, totalTime
);
718 printTime(" write output", statistics
.startDone
- statistics
.startOutput
, totalTime
);
719 fprintf(stderr
, "pageins=%u, pageouts=%u, faults=%u\n",
720 statistics
.vmEnd
.pageins
-statistics
.vmStart
.pageins
,
721 statistics
.vmEnd
.pageouts
-statistics
.vmStart
.pageouts
,
722 statistics
.vmEnd
.faults
-statistics
.vmStart
.faults
);
724 fprintf(stderr
, "processed %3u object files, totaling %15s bytes\n", inputFiles
._totalObjectLoaded
, commatize(inputFiles
._totalObjectSize
, temp
));
725 fprintf(stderr
, "processed %3u archive files, totaling %15s bytes\n", inputFiles
._totalArchivesLoaded
, commatize(inputFiles
._totalArchiveSize
, temp
));
726 fprintf(stderr
, "processed %3u dylib files\n", inputFiles
._totalDylibsLoaded
);
727 fprintf(stderr
, "wrote output file totaling %15s bytes\n", commatize(out
.fileSize(), temp
));
729 // <rdar://problem/6780050> Would like linker warning to be build error.
730 if ( options
.errorBecauseOfWarnings() ) {
731 fprintf(stderr
, "ld: fatal warning(s) induced error (-fatal_warnings)\n");
735 catch (const char* msg
) {
737 fprintf(stderr
, "ld: %s for inferred architecture %s\n", msg
, archName
);
739 fprintf(stderr
, "ld: %s for architecture %s\n", msg
, archName
);
741 fprintf(stderr
, "ld: %s\n", msg
);
750 // implement assert() function to print out a backtrace before aborting
751 void __assert_rtn(const char* func
, const char* file
, int line
, const char* failedexpr
)
753 Snapshot
*snapshot
= Snapshot::globalSnapshot
;
755 snapshot
->setSnapshotMode(Snapshot::SNAPSHOT_DEBUG
);
756 snapshot
->createSnapshot();
757 snapshot
->recordAssertionMessage("Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr
, func
, file
, line
);
759 void* callStack
[128];
760 int depth
= ::backtrace(callStack
, 128);
761 char* buffer
= (char*)malloc(1024);
762 for(int i
=0; i
< depth
-1; ++i
) {
764 dladdr(callStack
[i
], &info
);
765 const char* symboName
= info
.dli_sname
;
766 if ( (symboName
!= NULL
) && (strncmp(symboName
, "_Z", 2) == 0) ) {
767 size_t bufLen
= 1024;
769 char* unmangled
= abi::__cxa_demangle(symboName
, buffer
, &bufLen
, &result
);
770 if ( unmangled
!= NULL
)
771 symboName
= unmangled
;
773 long offset
= (uintptr_t)callStack
[i
] - (uintptr_t)info
.dli_saddr
;
774 fprintf(stderr
, "%d %p %s + %ld\n", i
, callStack
[i
], symboName
, offset
);
775 snapshot
->recordAssertionMessage("%d %p %s + %ld\n", i
, callStack
[i
], symboName
, offset
);
777 fprintf(stderr
, "A linker snapshot was created at:\n\t%s\n", snapshot
->rootDir());
778 fprintf(stderr
, "ld: Assertion failed: (%s), function %s, file %s, line %d.\n", failedexpr
, func
, file
, line
);