]> git.saurik.com Git - apple/ld64.git/blob - src/ld/ld.cpp
ld64-236.3.tar.gz
[apple/ld64.git] / src / ld / ld.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
2 *
3 * Copyright (c) 2005-2011 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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
12 * file.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 // start temp HACK for cross builds
26 extern "C" double log2 ( double );
27 //#define __MATH__
28 // end temp HACK for cross builds
29
30
31 #include <stdlib.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/sysctl.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <limits.h>
39 #include <unistd.h>
40 #include <execinfo.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>
45 #include <dlfcn.h>
46 #include <mach-o/dyld.h>
47 #include <dlfcn.h>
48 #include <AvailabilityMacros.h>
49
50 #include <string>
51 #include <map>
52 #include <set>
53 #include <string>
54 #include <vector>
55 #include <list>
56 #include <algorithm>
57 #include <unordered_map>
58 #include <cxxabi.h>
59
60 #include "Options.h"
61
62 #include "MachOFileAbstraction.hpp"
63 #include "Architectures.hpp"
64 #include "ld.hpp"
65
66 #include "InputFiles.h"
67 #include "Resolver.h"
68 #include "OutputFile.h"
69 #include "Snapshot.h"
70
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"
82
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"
88
89
90 struct PerformanceStatistics {
91 uint64_t startTool;
92 uint64_t startInputFileProcessing;
93 uint64_t startResolver;
94 uint64_t startDylibs;
95 uint64_t startPasses;
96 uint64_t startOutput;
97 uint64_t startDone;
98 vm_statistics_data_t vmStart;
99 vm_statistics_data_t vmEnd;
100 };
101
102
103 class InternalState : public ld::Internal
104 {
105 public:
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&);
109
110 uint64_t assignFileOffsets();
111 void setSectionSizesAndAlignments();
112 void sortSections();
113 void markAtomsOrdered() { _atomsOrderedInSections = true; }
114 virtual ~InternalState() {}
115 private:
116
117 class FinalSection : public ld::Internal::FinalSection
118 {
119 public:
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&);
124 private:
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;
130
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;
139 };
140
141 bool hasZeroForFileOffset(const ld::Section* sect);
142 uint64_t pageAlign(uint64_t addr);
143 uint64_t pageAlign(uint64_t addr, uint64_t pageSize);
144
145 struct SectionHash {
146 size_t operator()(const ld::Section*) const;
147 };
148 struct SectionEquals {
149 bool operator()(const ld::Section* left, const ld::Section* right) const;
150 };
151 typedef std::unordered_map<const ld::Section*, FinalSection*, SectionHash, SectionEquals> SectionInToOut;
152
153
154 SectionInToOut _sectionInToFinalMap;
155 const Options& _options;
156 bool _atomsOrderedInSections;
157 };
158
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;
167
168
169 size_t InternalState::SectionHash::operator()(const ld::Section* sect) const
170 {
171 size_t hash = 0;
172 ld::CStringHash temp;
173 hash += temp.operator()(sect->segmentName());
174 hash += temp.operator()(sect->sectionName());
175 return hash;
176 }
177
178 bool InternalState::SectionEquals::operator()(const ld::Section* left, const ld::Section* right) const
179 {
180 return (*left == *right);
181 }
182
183
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))
188 {
189 //fprintf(stderr, "FinalSection(%s, %s) _segmentOrder=%d, _sectionOrder=%d\n",
190 // this->segmentName(), this->sectionName(), _segmentOrder, _sectionOrder);
191 }
192
193 const ld::Section& InternalState::FinalSection::outputSection(const ld::Section& sect, bool mergeZeroFill)
194 {
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 )
204 return _s_DATA_data;
205 if ( strcmp(sect.sectionName(), "__const_coal") == 0 )
206 return _s_DATA_const;
207 }
208 else if ( strcmp(sect.segmentName(), "__TEXT") == 0 ) {
209 if ( strcmp(sect.sectionName(), "__const_coal") == 0 )
210 return _s_TEXT_const;
211 }
212 break;
213 case ld::Section::typeZeroFill:
214 if ( mergeZeroFill )
215 return _s_DATA_zerofill;
216 break;
217 case ld::Section::typeCode:
218 if ( strcmp(sect.segmentName(), "__TEXT") == 0 ) {
219 if ( strcmp(sect.sectionName(), "__textcoal_nt") == 0 )
220 return _s_TEXT_text;
221 else if ( strcmp(sect.sectionName(), "__StaticInit") == 0 )
222 return _s_TEXT_text;
223 }
224 break;
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;
229 }
230 else if ( strcmp(sect.segmentName(), "__IMPORT") == 0 ) {
231 if ( strcmp(sect.sectionName(), "__pointers") == 0 )
232 return _s_DATA_nl_symbol_ptr;
233 }
234 break;
235 case ld::Section::typeTentativeDefs:
236 if ( mergeZeroFill )
237 return _s_DATA_zerofill;
238 else
239 return _s_DATA_common;
240 break;
241 // FIX ME: more
242 default:
243 break;
244 }
245 return sect;
246 }
247
248 const ld::Section& InternalState::FinalSection::objectOutputSection(const ld::Section& sect, const Options& options)
249 {
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());
254 return *s;
255 }
256 }
257
258
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;
262 return sect;
263 }
264
265 uint32_t InternalState::FinalSection::segmentOrder(const ld::Section& sect, bool objFile)
266 {
267 if ( strcmp(sect.segmentName(), "__PAGEZERO") == 0 )
268 return 0;
269 if ( strcmp(sect.segmentName(), "__HEADER") == 0 ) // only used with -preload
270 return 0;
271 if ( strcmp(sect.segmentName(), "__TEXT") == 0 )
272 return 1;
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 )
277 return 3;
278 if ( strcmp(sect.segmentName(), "__IMPORT") == 0 )
279 return 4;
280
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 )
284 return i+10;
285 }
286 _s_segmentsSeen.push_back(sect.segmentName());
287 return _s_segmentsSeen.size()-1+10;
288 }
289
290 uint32_t InternalState::FinalSection::sectionOrder(const ld::Section& sect, uint32_t sectionsSeen)
291 {
292 if ( sect.type() == ld::Section::typeFirstSection )
293 return 0;
294 if ( sect.type() == ld::Section::typeMachHeader )
295 return 1;
296 if ( sect.type() == ld::Section::typeLastSection )
297 return INT_MAX;
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 )
303 return 10;
304 else
305 return 11;
306 case ld::Section::typeStub:
307 return 12;
308 case ld::Section::typeStubHelper:
309 return 13;
310 case ld::Section::typeLSDA:
311 return INT_MAX-3;
312 case ld::Section::typeUnwindInfo:
313 return INT_MAX-2;
314 case ld::Section::typeCFI:
315 return INT_MAX-1;
316 case ld::Section::typeStubClose:
317 return INT_MAX;
318 default:
319 return sectionsSeen+20;
320 }
321 }
322 else if ( strcmp(sect.segmentName(), "__DATA") == 0 ) {
323 switch ( sect.type() ) {
324 case ld::Section::typeLazyPointerClose:
325 return 8;
326 case ld::Section::typeDyldInfo:
327 return 9;
328 case ld::Section::typeNonLazyPointer:
329 return 10;
330 case ld::Section::typeLazyPointer:
331 return 11;
332 case ld::Section::typeInitializerPointers:
333 return 12;
334 case ld::Section::typeTerminatorPointers:
335 return 13;
336 case ld::Section::typeTLVInitialValues:
337 return INT_MAX-4; // need TLV zero-fill to follow TLV init values
338 case ld::Section::typeTLVZeroFill:
339 return INT_MAX-3;
340 case ld::Section::typeZeroFill:
341 // make sure __huge is always last zerofill section
342 if ( strcmp(sect.sectionName(), "__huge") == 0 )
343 return INT_MAX-1;
344 else
345 return INT_MAX-2;
346 default:
347 // <rdar://problem/14348664> __DATA,__const section should be near __mod_init_func not __data
348 if ( strcmp(sect.sectionName(), "__const") == 0 )
349 return 14;
350 // <rdar://problem/7435296> Reorder sections to reduce page faults in object files
351 else if ( strcmp(sect.sectionName(), "__objc_classlist") == 0 )
352 return 20;
353 else if ( strcmp(sect.sectionName(), "__objc_nlclslist") == 0 )
354 return 21;
355 else if ( strcmp(sect.sectionName(), "__objc_catlist") == 0 )
356 return 22;
357 else if ( strcmp(sect.sectionName(), "__objc_protolist") == 0 )
358 return 23;
359 else if ( strcmp(sect.sectionName(), "__objc_imageinfo") == 0 )
360 return 24;
361 else if ( strcmp(sect.sectionName(), "__objc_const") == 0 )
362 return 25;
363 else if ( strcmp(sect.sectionName(), "__objc_selrefs") == 0 )
364 return 26;
365 else if ( strcmp(sect.sectionName(), "__objc_msgrefs") == 0 )
366 return 27;
367 else if ( strcmp(sect.sectionName(), "__objc_protorefs") == 0 )
368 return 28;
369 else if ( strcmp(sect.sectionName(), "__objc_classrefs") == 0 )
370 return 29;
371 else if ( strcmp(sect.sectionName(), "__objc_superrefs") == 0 )
372 return 30;
373 else if ( strcmp(sect.sectionName(), "__objc_data") == 0 )
374 return 31;
375 else
376 return sectionsSeen+40;
377 }
378 }
379 // make sure zerofill in any other section is at end of segment
380 if ( sect.type() == ld::Section::typeZeroFill )
381 return INT_MAX-1;
382 return sectionsSeen+20;
383 }
384
385 #ifndef NDEBUG
386 static void validateFixups(const ld::Atom& atom)
387 {
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);
399 }
400 else {
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;
410 break;
411 case ld::Fixup::k2of2:
412 assert(lastClusterSize = ld::Fixup::k1of2);
413 lastWasClusterEnd = true;
414 break;
415 case ld::Fixup::k2of3:
416 assert(lastClusterSize = ld::Fixup::k1of3);
417 lastWasClusterEnd = false;
418 break;
419 case ld::Fixup::k2of4:
420 assert(lastClusterSize = ld::Fixup::k1of4);
421 lastWasClusterEnd = false;
422 break;
423 case ld::Fixup::k2of5:
424 assert(lastClusterSize = ld::Fixup::k1of5);
425 lastWasClusterEnd = false;
426 break;
427 case ld::Fixup::k3of3:
428 assert(lastClusterSize = ld::Fixup::k2of3);
429 lastWasClusterEnd = true;
430 break;
431 case ld::Fixup::k3of4:
432 assert(lastClusterSize = ld::Fixup::k2of4);
433 lastWasClusterEnd = false;
434 break;
435 case ld::Fixup::k3of5:
436 assert(lastClusterSize = ld::Fixup::k2of5);
437 lastWasClusterEnd = false;
438 break;
439 case ld::Fixup::k4of4:
440 assert(lastClusterSize = ld::Fixup::k3of4);
441 lastWasClusterEnd = true;
442 break;
443 case ld::Fixup::k4of5:
444 assert(lastClusterSize = ld::Fixup::k3of5);
445 lastWasClusterEnd = false;
446 break;
447 case ld::Fixup::k5of5:
448 assert(lastClusterSize = ld::Fixup::k4of5);
449 lastWasClusterEnd = true;
450 break;
451 }
452 }
453 lastClusterSize = fit->clusterSize;
454 if ( fit->binding == ld::Fixup::bindingDirectlyBound ) {
455 assert(fit->u.target != NULL);
456 }
457 }
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:
464 break;
465 default:
466 assert(0 && "last fixup was not end of cluster");
467 break;
468 }
469 }
470 #endif
471
472 ld::Internal::FinalSection* InternalState::addAtom(const ld::Atom& atom)
473 {
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);
476 #ifndef NDEBUG
477 validateFixups(atom);
478 #endif
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);
487 }
488 else {
489 // not end atom, just append new atom
490 fs->atoms.push_back(&atom);
491 }
492 }
493 else {
494 // normal case
495 fs->atoms.push_back(&atom);
496 }
497 return fs;
498 }
499
500 ld::Internal::FinalSection* InternalState::getFinalSection(const ld::Section& inputSection)
501 {
502 const ld::Section* baseForFinalSection = &inputSection;
503
504 // see if input section already has a FinalSection
505 SectionInToOut::iterator pos = _sectionInToFinalMap.find(&inputSection);
506 if ( pos != _sectionInToFinalMap.end() ) {
507 return pos->second;
508 }
509
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:
517 case Options::kDyld:
518 case Options::kKextBundle:
519 case Options::kPreload:
520 {
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);
527 return pos->second;
528 }
529 else if ( outSect != inputSection ) {
530 // new output section created, but not in map
531 baseForFinalSection = &outSect;
532 }
533 }
534 break;
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);
541 return pos->second;
542 }
543 objFile = true;
544 break;
545 }
546
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);
552 return result;
553 }
554
555
556 int InternalState::FinalSection::sectionComparer(const void* l, const void* r)
557 {
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);
563 }
564
565 void InternalState::sortSections()
566 {
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());
570 //}
571 qsort(&sections[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());
575 //}
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)) );
580
581 }
582
583
584 bool InternalState::hasZeroForFileOffset(const ld::Section* sect)
585 {
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:
593 return true;
594 default:
595 break;
596 }
597 return false;
598 }
599
600 uint64_t InternalState::pageAlign(uint64_t addr)
601 {
602 const uint64_t alignment = _options.segmentAlignment();
603 return ((addr+alignment-1) & (-alignment));
604 }
605
606 uint64_t InternalState::pageAlign(uint64_t addr, uint64_t pageSize)
607 {
608 return ((addr+pageSize-1) & (-pageSize));
609 }
610
611 void InternalState::setSectionSizesAndAlignments()
612 {
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());
620 }
621 }
622 else {
623 uint16_t maxAlignment = 0;
624 uint64_t offset = 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 )
642 break;
643 pagePerAtom = true;
644 if ( atomAlignmentPowerOf2 < 12 ) {
645 atomAlignmentPowerOf2 = 12;
646 atomModulus = 0;
647 }
648 break;
649 default:
650 break;
651 }
652 }
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;
662 else
663 offset += requiredModulus+alignment-currentModulus;
664 }
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();
669 if ( pagePerAtom ) {
670 offset = (offset + 4095) & (-4096); // round up to end of page
671 }
672 }
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());
681 }
682 }
683 sect->size = offset;
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 )
693 sect->alignment = 3;
694 if ( sect->type() == ld::Section::typeTLVDefs )
695 this->hasThreadLocalVariableDefinitions = true;
696 }
697 }
698 }
699
700 uint64_t InternalState::assignFileOffsets()
701 {
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);
706
707 uint64_t address = 0;
708 const char* lastSegName = "";
709 uint64_t floatingAddressStart = _options.baseAddress();
710
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()) )
716 continue;
717 if ( segmentsArePageAligned ) {
718 if ( strcmp(lastSegName, sect->segmentName()) != 0 ) {
719 address = _options.customSegmentAddress(sect->segmentName());
720 lastSegName = sect->segmentName();
721 }
722 }
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) );
727
728 // update section info
729 sect->address = address;
730 sect->alignmentPaddingBytes = (address - unalignedAddress);
731
732 // sanity check size
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);
737
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;
743
744 // if TEXT segment address is fixed, then flow other segments after it
745 if ( strcmp(sect->segmentName(), "__TEXT") == 0 ) {
746 floatingAddressStart = address;
747 }
748 }
749
750 // second pass, assign section address to sections in segments that are contiguous with previous segment
751 address = floatingAddressStart;
752 lastSegName = "";
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()) )
759 continue;
760 if ( (_options.outputKind() == Options::kPreload) && (sect->type() == ld::Section::typeMachHeader) ) {
761 sect->alignmentPaddingBytes = 0;
762 continue;
763 }
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));
769 }
770 // set segment address based on end of last segment
771 address = pageAlign(address);
772 lastSegName = sect->segmentName();
773 }
774 }
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) );
779
780 // update section info
781 sect->address = address;
782 sect->alignmentPaddingBytes = (address - unalignedAddress);
783
784 // sanity check size
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);
789
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()) )
794 continue;
795 if ( sect->address > otherSect->address ) {
796 if ( (otherSect->address+otherSect->size) > sect->address ) {
797 overlappingFixedSection = otherSect;
798 overlappingFlowSection = sect;
799 }
800 }
801 else {
802 if ( (sect->address+sect->size) > otherSect->address ) {
803 overlappingFixedSection = otherSect;
804 overlappingFlowSection = sect;
805 }
806 }
807 }
808
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;
815 }
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() )
821 continue;
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());
825
826 }
827 throwf("Section (%s/%s) overlaps fixed address section (%s/%s)",
828 overlappingFlowSection->segmentName(), overlappingFlowSection->sectionName(),
829 overlappingFixedSection->segmentName(), overlappingFixedSection->sectionName());
830 }
831
832
833 // third pass, assign section file offsets
834 uint64_t fileOffset = 0;
835 lastSegName = "";
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;
842
843 // <rdar://problem/10445047> align file offset with address layout
844 fileOffset += sect->alignmentPaddingBytes;
845 }
846 else {
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));
850 }
851 lastSegName = sect->segmentName();
852
853 // align file offset with address layout
854 fileOffset += sect->alignmentPaddingBytes;
855
856 // update section info
857 sect->fileOffset = fileOffset;
858
859 // update running total
860 fileOffset += sect->size;
861 }
862
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());
866 }
867
868 #if 0
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);
876 }
877 }
878 }
879 #endif
880
881 // return total file size
882 return fileOffset;
883 }
884
885 static char* commatize(uint64_t in, char* out)
886 {
887 char* result = out;
888 char rawNum[30];
889 sprintf(rawNum, "%llu", in);
890 const int rawNumLen = strlen(rawNum);
891 for(int i=0; i < rawNumLen-1; ++i) {
892 *out++ = rawNum[i];
893 if ( ((rawNumLen-i) % 3) == 1 )
894 *out++ = ',';
895 }
896 *out++ = rawNum[rawNumLen-1];
897 *out = '\0';
898 return result;
899 }
900
901 static void printTime(const char* msg, uint64_t partTime, uint64_t totalTime)
902 {
903 static uint64_t sUnitsPerSecond = 0;
904 if ( sUnitsPerSecond == 0 ) {
905 struct mach_timebase_info timeBaseInfo;
906 if ( mach_timebase_info(&timeBaseInfo) != KERN_SUCCESS )
907 return;
908 sUnitsPerSecond = 1000000000ULL * timeBaseInfo.denom / timeBaseInfo.numer;
909 }
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);
916 }
917 else {
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);
923 }
924 }
925
926
927 static void getVMInfo(vm_statistics_data_t& info)
928 {
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));
934 }
935 }
936
937
938
939 static const char* sOverridePathlibLTO = NULL;
940
941 //
942 // This is magic glue that overrides the default behaviour
943 // of lazydylib1.o which is used to lazily load libLTO.dylib.
944 //
945 extern "C" const char* dyld_lazy_dylib_path_fix(const char* path);
946 const char* dyld_lazy_dylib_path_fix(const char* path)
947 {
948 if ( sOverridePathlibLTO != NULL )
949 return sOverridePathlibLTO;
950 else
951 return path;
952 }
953
954
955
956 int main(int argc, const char* argv[])
957 {
958 const char* archName = NULL;
959 bool showArch = false;
960 bool archInferred = false;
961 try {
962 PerformanceStatistics statistics;
963 statistics.startTool = mach_absolute_time();
964
965 // create object to track command line arguments
966 Options options(argc, argv);
967 InternalState state(options);
968
969 // allow libLTO to be overridden by command line -lto_library
970 sOverridePathlibLTO = options.overridePathlibLTO();
971
972 // gather vm stats
973 if ( options.printStatistics() )
974 getVMInfo(statistics.vmStart);
975
976 // update strings for error messages
977 showArch = options.printArchPrefix();
978 archName = options.architectureName();
979 archInferred = (options.architecture() == 0);
980
981 // open and parse input files
982 statistics.startInputFileProcessing = mach_absolute_time();
983 ld::tool::InputFiles inputFiles(options, &archName);
984
985 // load and resolve all references
986 statistics.startResolver = mach_absolute_time();
987 ld::tool::Resolver resolver(options, inputFiles, state);
988 resolver.resolve();
989
990 // add dylibs used
991 statistics.startDylibs = mach_absolute_time();
992 inputFiles.dylibs(state);
993
994 // do initial section sorting so passes have rough idea of the layout
995 state.sortSections();
996
997 // run passes
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
1011
1012 // sort final sections
1013 state.sortSections();
1014
1015 // write output file
1016 statistics.startOutput = mach_absolute_time();
1017 ld::tool::OutputFile out(options);
1018 out.write(state);
1019 statistics.startDone = mach_absolute_time();
1020
1021 // print statistics
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);
1037 char temp[40];
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));
1042 }
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");
1046 return 1;
1047 }
1048 }
1049 catch (const char* msg) {
1050 if ( archInferred )
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);
1054 else
1055 fprintf(stderr, "ld: %s\n", msg);
1056 return 1;
1057 }
1058
1059 return 0;
1060 }
1061
1062
1063 #ifndef NDEBUG
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)
1066 {
1067 Snapshot *snapshot = Snapshot::globalSnapshot;
1068
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);
1072
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) {
1077 Dl_info info;
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;
1082 int result;
1083 char* unmangled = abi::__cxa_demangle(symboName, buffer, &bufLen, &result);
1084 if ( unmangled != NULL )
1085 symboName = unmangled;
1086 }
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);
1090 }
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);
1093 exit(1);
1094 }
1095 #endif
1096
1097