1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2010 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@
31 #include <libkern/OSByteOrder.h>
40 #include "make_stubs.h"
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
; }
55 Atom
* compressedHelperHelper
;
56 Atom
* compressedImageCache
;
57 Atom
* compressedFastBinderPointer
;
61 struct AtomByNameSorter
63 bool operator()(const ld::Atom
* left
, const ld::Atom
* right
)
65 return (strcmp(left
->name(), right
->name()) < 0);
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
);
73 const Options
& _options
;
74 const cpu_type_t _architecture
;
75 const bool _lazyDylibsInUuse
;
76 const bool _compressedLINKEDIT
;
78 const bool _mightBeInSharedRegion
;
80 const bool _flatNamespace
;
81 ld::Internal
* _internal
;
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"
96 Pass::Pass(const Options
& opts
)
97 : compressedHelperHelper(NULL
),
98 compressedImageCache(NULL
),
99 compressedFastBinderPointer(NULL
),
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)
114 const ld::Atom
* Pass::stubableFixup(const ld::Fixup
* fixup
, ld::Internal
& state
)
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
)
126 // use stub if target is a resolver function in same linkage unit
127 if ( target
->contentType() == ld::Atom::typeResolver
)
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
)) )
136 // create stub if target is interposable
137 if ( _options
.interposable(target
->name()) )
139 if ( _flatNamespace
) {
140 // flat namespace does not indirect calls within main exectuables
141 if ( _options
.outputKind() == Options::kDynamicExecutable
)
143 // create stub if target is global and building -flat dylib or bundle
149 if ( target
->contentType() == ld::Atom::typeResolver
) {
150 // any pointer to a resolver needs to change to pointer to stub
161 ld::Atom
* Pass::makeStub(const ld::Atom
& target
, bool weakImport
)
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
) );
168 bool forLazyDylib
= false;
169 const ld::dylib::File
* dylib
= dynamic_cast<const ld::dylib::File
*>(target
.file());
170 if ( (dylib
!= NULL
) && dylib
->willBeLazyLoadedDylib() )
172 bool stubToResolver
= (target
.contentType() == ld::Atom::typeResolver
);
174 switch ( _architecture
) {
175 case CPU_TYPE_POWERPC
:
177 return new ld::passes::stubs::ppc::classic::StubPICAtom(*this, target
, forLazyDylib
, false, weakImport
);
179 return new ld::passes::stubs::ppc::classic::StubNoPICAtom(*this, target
, forLazyDylib
, false, weakImport
);
181 case CPU_TYPE_POWERPC64
:
182 return new ld::passes::stubs::ppc::classic::StubPICAtom(*this, target
, forLazyDylib
, true, weakImport
);
185 if ( usingCompressedLINKEDIT() && !forLazyDylib
)
186 return new ld::passes::stubs::x86::StubAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
188 return new ld::passes::stubs::x86::classic::StubAtom(*this, target
, forLazyDylib
, weakImport
);
190 case CPU_TYPE_X86_64
:
191 if ( usingCompressedLINKEDIT() && !forLazyDylib
)
192 return new ld::passes::stubs::x86_64::StubAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
194 return new ld::passes::stubs::x86_64::classic::StubAtom(*this, target
, forLazyDylib
, weakImport
);
197 if ( usingCompressedLINKEDIT() && !forLazyDylib
) {
198 if ( (_stubCount
< 900) && !_mightBeInSharedRegion
&& !_largeText
)
199 return new ld::passes::stubs::arm::StubCloseAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
201 return new ld::passes::stubs::arm::StubPICAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
203 return new ld::passes::stubs::arm::StubNoPICAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
207 return new ld::passes::stubs::arm::classic::StubPICAtom(*this, target
, forLazyDylib
, weakImport
);
209 return new ld::passes::stubs::arm::classic::StubNoPICAtom(*this, target
, forLazyDylib
, weakImport
);
213 throw "unsupported arch for stub";
217 void Pass::verifyNoResolverFunctions(ld::Internal
& state
)
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());
229 void Pass::process(ld::Internal
& state
)
231 switch ( _options
.outputKind() ) {
232 case Options::kObjectFile
:
233 // these kinds don't use stubs and can have resolver functions
235 case Options::kKextBundle
:
236 case Options::kStaticExecutable
:
237 case Options::kPreload
:
239 // these kinds don't use stubs and cannot have resolver functions
240 verifyNoResolverFunctions(state
);
242 case Options::kDynamicLibrary
:
243 // uses stubs and can have resolver functions
245 case Options::kDynamicExecutable
:
246 case Options::kDynamicBundle
:
247 // these kinds do use stubs and cannot have resolver functions
248 verifyNoResolverFunctions(state
);
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;
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();
284 (const_cast<ld::dylib::File
*>(dylib
))->setUsingNonWeakImportedSymbols();
288 // target in weakImportMap, check for weakness mismatch
289 if ( pos
->second
!= fit
->weakImport
) {
291 switch ( _options
.weakReferenceMismatchTreatment() ) {
292 case Options::kWeakReferenceMismatchError
:
293 throwf("mismatching weak references for symbol: %s", stubableTargetOfFixup
->name());
294 case Options::kWeakReferenceMismatchWeak
:
297 case Options::kWeakReferenceMismatchNonWeak
:
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());
315 throwf("resolver functions (%s) can only be used when targeting Mac OS X 10.6 or later", atom
->name());
317 stubFor
[atom
] = NULL
;
322 // short circuit if no stubs needed
324 _stubCount
= stubFor
.size();
325 if ( _stubCount
== 0 )
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";
332 // disable close stubs when branch islands might be needed
333 if ( (_architecture
== CPU_TYPE_ARM
) && (codeSize
> 4*1024*1024) )
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
]);
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
;
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());
374 void doPass(const Options
& opts
, ld::Internal
& internal
)
377 pass
.process(internal
);
383 } // namespace passes