]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/dylibs.cpp
ld64-128.2.tar.gz
[apple/ld64.git] / src / ld / passes / dylibs.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2010 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
26 #include <stdint.h>
27 #include <math.h>
28 #include <unistd.h>
29 #include <dlfcn.h>
30 #include <mach/machine.h>
31
32 #include <vector>
33
34 #include "ld.hpp"
35 #include "dylibs.h"
36
37 namespace ld {
38 namespace passes {
39 namespace dylibs {
40
41
42 class WillBeUsed
43 {
44 public:
45 bool operator()(ld::dylib::File* dylib) const {
46 return dylib->willRemoved();
47 }
48 };
49
50
51 void doPass(const Options& opts, ld::Internal& state)
52 {
53 // const bool log = false;
54
55 // only optimize dylibs in final linked images
56 if ( opts.outputKind() == Options::kObjectFile )
57 return;
58
59 // clear "willRemoved" bit on all dylibs
60 for (std::vector<ld::dylib::File*>::iterator it = state.dylibs.begin(); it != state.dylibs.end(); ++it) {
61 ld::dylib::File* aDylib = *it;
62 aDylib->setWillBeRemoved(false);
63 }
64 for (std::vector<ld::dylib::File*>::iterator it = state.dylibs.begin(); it != state.dylibs.end(); ++it) {
65 ld::dylib::File* aDylib = *it;
66 // set "willRemoved" bit on implicit dylibs that did not provide any exports
67 if ( aDylib->implicitlyLinked() && !aDylib->explicitlyLinked() && !aDylib->providedExportAtom() )
68 aDylib->setWillBeRemoved(true);
69 // set "willRemoved" bit on dead strippable explicit dylibs that did not provide any exports
70 if ( aDylib->explicitlyLinked() && aDylib->deadStrippable() && !aDylib->providedExportAtom() )
71 aDylib->setWillBeRemoved(true);
72 // set "willRemoved" bit on any unused explicit when -dead_strip_dylibs is used
73 if ( opts.deadStripDylibs() && !aDylib->providedExportAtom() )
74 aDylib->setWillBeRemoved(true);
75 }
76 // remove unused dylibs
77 state.dylibs.erase(std::remove_if(state.dylibs.begin(), state.dylibs.end(), WillBeUsed()), state.dylibs.end());
78
79
80 // <rdar://problem/9441273> automatically weak-import dylibs when all symbols from it are weak-imported
81 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
82 ld::Internal::FinalSection* sect = *sit;
83 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
84 const ld::Atom* atom = *ait;
85 const ld::Atom* target = NULL;
86 bool targetIsWeakImport = false;
87 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
88 if ( fit->firstInCluster() )
89 target = NULL;
90 switch ( fit->binding ) {
91 case ld::Fixup::bindingsIndirectlyBound:
92 target = state.indirectBindingTable[fit->u.bindingIndex];
93 targetIsWeakImport = fit->weakImport;
94 break;
95 case ld::Fixup::bindingDirectlyBound:
96 target = fit->u.target;
97 targetIsWeakImport = fit->weakImport;
98 break;
99 default:
100 break;
101 }
102 if ( (target != NULL) && (target->definition() == ld::Atom::definitionProxy) ) {
103 ld::Atom::WeakImportState curWI = target->weakImportState();
104 if ( curWI == ld::Atom::weakImportUnset ) {
105 // first use of this proxy, set weak-import based on this usage
106 (const_cast<ld::Atom*>(target))->setWeakImportState(targetIsWeakImport);
107 }
108 else {
109 // proxy already has weak-importness set, check for weakness mismatch
110 bool curIsWeakImport = (curWI == ld::Atom::weakImportTrue);
111 if ( curIsWeakImport != targetIsWeakImport ) {
112 // found mismatch
113 switch ( opts.weakReferenceMismatchTreatment() ) {
114 case Options::kWeakReferenceMismatchError:
115 throwf("mismatching weak references for symbol: %s", target->name());
116 case Options::kWeakReferenceMismatchWeak:
117 (const_cast<ld::Atom*>(target))->setWeakImportState(true);
118 break;
119 case Options::kWeakReferenceMismatchNonWeak:
120 (const_cast<ld::Atom*>(target))->setWeakImportState(false);
121 break;
122 }
123 }
124 }
125 }
126 }
127 }
128 }
129
130 }
131
132
133 } // namespace dylibs
134 } // namespace passes
135 } // namespace ld