]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/stubs/stubs.cpp
ld64-123.2.tar.gz
[apple/ld64.git] / src / ld / passes / stubs / stubs.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009-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
27 #include <stdint.h>
28 #include <math.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <libkern/OSByteOrder.h>
32
33 #include <vector>
34 #include <set>
35 #include <map>
36
37 #include "Options.h"
38 #include "ld.hpp"
39
40 #include "make_stubs.h"
41
42
43 namespace ld {
44 namespace passes {
45 namespace stubs {
46
47 class Pass {
48 public:
49 Pass(const Options& opts);
50 void process(ld::Internal& internal);
51 void addAtom(const ld::Atom& atom) { _internal->addAtom(atom); }
52 bool usingCompressedLINKEDIT() const { return _compressedLINKEDIT; }
53 ld::Internal* internal() { return _internal; }
54
55 Atom* compressedHelperHelper;
56 Atom* compressedImageCache;
57 Atom* compressedFastBinderPointer;
58
59 private:
60
61 struct AtomByNameSorter
62 {
63 bool operator()(const ld::Atom* left, const ld::Atom* right)
64 {
65 return (strcmp(left->name(), right->name()) < 0);
66 }
67 };
68
69 const ld::Atom* stubableFixup(const ld::Fixup* fixup, ld::Internal&);
70 ld::Atom* makeStub(const ld::Atom& target, bool weakImport);
71 void verifyNoResolverFunctions(ld::Internal& state);
72
73 const Options& _options;
74 const cpu_type_t _architecture;
75 const bool _lazyDylibsInUuse;
76 const bool _compressedLINKEDIT;
77 const bool _prebind;
78 const bool _mightBeInSharedRegion;
79 const bool _pic;
80 const bool _flatNamespace;
81 ld::Internal* _internal;
82 uint32_t _stubCount;
83 bool _largeText;
84 };
85
86 #include "stub_x86_64.hpp"
87 #include "stub_x86_64_classic.hpp"
88 #include "stub_x86.hpp"
89 #include "stub_x86_classic.hpp"
90 #include "stub_arm.hpp"
91 #include "stub_arm_classic.hpp"
92 #include "stub_ppc_classic.hpp"
93
94
95
96 Pass::Pass(const Options& opts)
97 : compressedHelperHelper(NULL),
98 compressedImageCache(NULL),
99 compressedFastBinderPointer(NULL),
100 _options(opts),
101 _architecture(opts.architecture()),
102 _lazyDylibsInUuse(opts.usingLazyDylibLinking()),
103 _compressedLINKEDIT(opts.makeCompressedDyldInfo()),
104 _prebind(opts.prebind()),
105 _mightBeInSharedRegion(opts.sharedRegionEligible()),
106 _pic(opts.outputSlidable()),
107 _flatNamespace(opts.nameSpace() != Options::kTwoLevelNameSpace),
108 _internal(NULL), _stubCount(0), _largeText(false)
109 {
110
111 }
112
113
114 const ld::Atom* Pass::stubableFixup(const ld::Fixup* fixup, ld::Internal& state)
115 {
116 if ( fixup->binding == ld::Fixup::bindingsIndirectlyBound ) {
117 const ld::Atom* target = state.indirectBindingTable[fixup->u.bindingIndex];
118 switch ( fixup->kind ) {
119 case ld::Fixup::kindStoreTargetAddressPPCBranch24:
120 case ld::Fixup::kindStoreTargetAddressX86BranchPCRel32:
121 case ld::Fixup::kindStoreTargetAddressARMBranch24:
122 case ld::Fixup::kindStoreTargetAddressThumbBranch22:
123 // create stub if target is in a dylib
124 if ( target->definition() == ld::Atom::definitionProxy )
125 return target;
126 // use stub if target is a resolver function in same linkage unit
127 if ( target->contentType() == ld::Atom::typeResolver )
128 return target;
129 if ( target->scope() == ld::Atom::scopeGlobal ) {
130 // create stub if target is global weak definition in symbol table
131 if ( (target->definition() == ld::Atom::definitionRegular)
132 && (target->combine() == ld::Atom::combineByName)
133 && ((target->symbolTableInclusion() == ld::Atom::symbolTableIn)
134 || (target->symbolTableInclusion() == ld::Atom::symbolTableInAndNeverStrip)) )
135 return target;
136 // create stub if target is interposable
137 if ( _options.interposable(target->name()) )
138 return target;
139 if ( _flatNamespace ) {
140 // flat namespace does not indirect calls within main exectuables
141 if ( _options.outputKind() == Options::kDynamicExecutable )
142 return NULL;
143 // create stub if target is global and building -flat dylib or bundle
144 return target;
145 }
146 }
147 break;
148 default:
149 if ( target->contentType() == ld::Atom::typeResolver ) {
150 // any pointer to a resolver needs to change to pointer to stub
151 return target;
152 }
153 break;
154 }
155 }
156 return NULL;
157 }
158
159
160
161 ld::Atom* Pass::makeStub(const ld::Atom& target, bool weakImport)
162 {
163 //fprintf(stderr, "makeStub(target=%p %s in sect %s)\n", &target, target.name(), target.section().sectionName());
164 bool stubToGlobalWeakDef = ( (target.scope() == ld::Atom::scopeGlobal)
165 && (target.definition() == ld::Atom::definitionRegular)
166 && (target.combine() == ld::Atom::combineByName) );
167
168 bool forLazyDylib = false;
169 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(target.file());
170 if ( (dylib != NULL) && dylib->willBeLazyLoadedDylib() )
171 forLazyDylib = true;
172 bool stubToResolver = (target.contentType() == ld::Atom::typeResolver);
173
174 switch ( _architecture ) {
175 case CPU_TYPE_POWERPC:
176 if ( _pic )
177 return new ld::passes::stubs::ppc::classic::StubPICAtom(*this, target, forLazyDylib, false, weakImport);
178 else
179 return new ld::passes::stubs::ppc::classic::StubNoPICAtom(*this, target, forLazyDylib, false, weakImport);
180 break;
181 case CPU_TYPE_POWERPC64:
182 return new ld::passes::stubs::ppc::classic::StubPICAtom(*this, target, forLazyDylib, true, weakImport);
183 break;
184 case CPU_TYPE_I386:
185 if ( usingCompressedLINKEDIT() && !forLazyDylib )
186 return new ld::passes::stubs::x86::StubAtom(*this, target, stubToGlobalWeakDef, stubToResolver, weakImport);
187 else
188 return new ld::passes::stubs::x86::classic::StubAtom(*this, target, forLazyDylib, weakImport);
189 break;
190 case CPU_TYPE_X86_64:
191 if ( usingCompressedLINKEDIT() && !forLazyDylib )
192 return new ld::passes::stubs::x86_64::StubAtom(*this, target, stubToGlobalWeakDef, stubToResolver, weakImport);
193 else
194 return new ld::passes::stubs::x86_64::classic::StubAtom(*this, target, forLazyDylib, weakImport);
195 break;
196 case CPU_TYPE_ARM:
197 if ( usingCompressedLINKEDIT() && !forLazyDylib ) {
198 if ( (_stubCount < 900) && !_mightBeInSharedRegion && !_largeText )
199 return new ld::passes::stubs::arm::StubCloseAtom(*this, target, stubToGlobalWeakDef, stubToResolver, weakImport);
200 else if ( _pic )
201 return new ld::passes::stubs::arm::StubPICAtom(*this, target, stubToGlobalWeakDef, stubToResolver, weakImport);
202 else
203 return new ld::passes::stubs::arm::StubNoPICAtom(*this, target, stubToGlobalWeakDef, stubToResolver, weakImport);
204 }
205 else {
206 if ( _pic )
207 return new ld::passes::stubs::arm::classic::StubPICAtom(*this, target, forLazyDylib, weakImport);
208 else
209 return new ld::passes::stubs::arm::classic::StubNoPICAtom(*this, target, forLazyDylib, weakImport);
210 }
211 break;
212 }
213 throw "unsupported arch for stub";
214 }
215
216
217 void Pass::verifyNoResolverFunctions(ld::Internal& state)
218 {
219 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
220 ld::Internal::FinalSection* sect = *sit;
221 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
222 const ld::Atom* atom = *ait;
223 if ( atom->contentType() == ld::Atom::typeResolver )
224 throwf("resolver function '%s' not supported in type of output", atom->name());
225 }
226 }
227 }
228
229 void Pass::process(ld::Internal& state)
230 {
231 switch ( _options.outputKind() ) {
232 case Options::kObjectFile:
233 // these kinds don't use stubs and can have resolver functions
234 return;
235 case Options::kKextBundle:
236 case Options::kStaticExecutable:
237 case Options::kPreload:
238 case Options::kDyld:
239 // these kinds don't use stubs and cannot have resolver functions
240 verifyNoResolverFunctions(state);
241 return;
242 case Options::kDynamicLibrary:
243 // uses stubs and can have resolver functions
244 break;
245 case Options::kDynamicExecutable:
246 case Options::kDynamicBundle:
247 // these kinds do use stubs and cannot have resolver functions
248 verifyNoResolverFunctions(state);
249 break;
250 }
251
252 // walk all atoms and fixups looking for stubable references
253 // don't create stubs inline because that could invalidate the sections iterator
254 std::vector<const ld::Atom*> atomsCallingStubs;
255 std::map<const ld::Atom*,ld::Atom*> stubFor;
256 std::map<const ld::Atom*,bool> weakImportMap;
257 atomsCallingStubs.reserve(128);
258 uint64_t codeSize = 0;
259 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
260 ld::Internal::FinalSection* sect = *sit;
261 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
262 const ld::Atom* atom = *ait;
263 codeSize += atom->size();
264 bool atomNeedsStub = false;
265 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
266 const ld::Atom* stubableTargetOfFixup = stubableFixup(fit, state);
267 if ( stubableTargetOfFixup != NULL ) {
268 if ( !atomNeedsStub ) {
269 atomsCallingStubs.push_back(atom);
270 atomNeedsStub = true;
271 }
272 stubFor[stubableTargetOfFixup] = NULL;
273 // record weak_import attribute
274 std::map<const ld::Atom*,bool>::iterator pos = weakImportMap.find(stubableTargetOfFixup);
275 if ( pos == weakImportMap.end() ) {
276 // target not in weakImportMap, so add
277 weakImportMap[stubableTargetOfFixup] = fit->weakImport;
278 // <rdar://problem/5529626> If only weak_import symbols are used, linker should use LD_LOAD_WEAK_DYLIB
279 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(stubableTargetOfFixup->file());
280 if ( dylib != NULL ) {
281 if ( fit->weakImport )
282 (const_cast<ld::dylib::File*>(dylib))->setUsingWeakImportedSymbols();
283 else
284 (const_cast<ld::dylib::File*>(dylib))->setUsingNonWeakImportedSymbols();
285 }
286 }
287 else {
288 // target in weakImportMap, check for weakness mismatch
289 if ( pos->second != fit->weakImport ) {
290 // found mismatch
291 switch ( _options.weakReferenceMismatchTreatment() ) {
292 case Options::kWeakReferenceMismatchError:
293 throwf("mismatching weak references for symbol: %s", stubableTargetOfFixup->name());
294 case Options::kWeakReferenceMismatchWeak:
295 pos->second = true;
296 break;
297 case Options::kWeakReferenceMismatchNonWeak:
298 pos->second = false;
299 break;
300 }
301 }
302 }
303 }
304 }
305 // all resolver functions must have a corresponding stub
306 if ( atom->contentType() == ld::Atom::typeResolver ) {
307 if ( _options.outputKind() != Options::kDynamicLibrary )
308 throwf("resolver functions (%s) can only be used in dylibs", atom->name());
309 if ( !_options.makeCompressedDyldInfo() ) {
310 if ( _options.architecture() == CPU_TYPE_POWERPC )
311 throwf("resolver functions (%s) not supported for PowerPC", atom->name());
312 else if ( _options.architecture() == CPU_TYPE_ARM )
313 throwf("resolver functions (%s) can only be used when targeting iOS 4.2 or later", atom->name());
314 else
315 throwf("resolver functions (%s) can only be used when targeting Mac OS X 10.6 or later", atom->name());
316 }
317 stubFor[atom] = NULL;
318 }
319 }
320 }
321
322 // short circuit if no stubs needed
323 _internal = &state;
324 _stubCount = stubFor.size();
325 if ( _stubCount == 0 )
326 return;
327
328 // <rdar://problem/8553283> lazily check for helper
329 if ( !_options.makeCompressedDyldInfo() && (state.classicBindingHelper == NULL) )
330 throw "symbol dyld_stub_binding_helper not found, normally in crt1.o/dylib1.o/bundle1.o";
331
332 // disable close stubs when branch islands might be needed
333 if ( (_architecture == CPU_TYPE_ARM) && (codeSize > 4*1024*1024) )
334 _largeText = true;
335
336 // make stub atoms
337 for (std::map<const ld::Atom*,ld::Atom*>::iterator it = stubFor.begin(); it != stubFor.end(); ++it) {
338 it->second = makeStub(*it->first, weakImportMap[it->first]);
339 }
340
341 // updated atoms to use stubs
342 for (std::vector<const ld::Atom*>::iterator it=atomsCallingStubs.begin(); it != atomsCallingStubs.end(); ++it) {
343 const ld::Atom* atom = *it;
344 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
345 const ld::Atom* stubableTargetOfFixup = stubableFixup(fit, state);
346 if ( stubableTargetOfFixup != NULL ) {
347 ld::Atom* stub = stubFor[stubableTargetOfFixup];
348 assert(stub != NULL && "stub not created");
349 fit->binding = ld::Fixup::bindingDirectlyBound;
350 fit->u.target = stub;
351 }
352 }
353 }
354
355 // sort new atoms so links are consistent
356 for (std::vector<ld::Internal::FinalSection*>::iterator sit=state.sections.begin(); sit != state.sections.end(); ++sit) {
357 ld::Internal::FinalSection* sect = *sit;
358 switch ( sect->type() ) {
359 case ld::Section::typeStubHelper:
360 case ld::Section::typeStub:
361 case ld::Section::typeStubClose:
362 case ld::Section::typeLazyPointer:
363 case ld::Section::typeLazyPointerClose:
364 std::sort(sect->atoms.begin(), sect->atoms.end(), AtomByNameSorter());
365 break;
366 default:
367 break;
368 }
369 }
370
371 }
372
373
374 void doPass(const Options& opts, ld::Internal& internal)
375 {
376 Pass pass(opts);
377 pass.process(internal);
378 }
379
380
381
382 } // namespace stubs
383 } // namespace passes
384 } // namespace ld
385