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 if ( usingCompressedLINKEDIT() && !forLazyDylib
) {
175 if ( _internal
->compressedFastBinderProxy
== NULL
)
176 throwf("symbol dyld_stub_binder not found (normally in libSystem.dylib). Needed to perform lazy binding to function %s", target
.name());
179 switch ( _architecture
) {
180 case CPU_TYPE_POWERPC
:
182 return new ld::passes::stubs::ppc::classic::StubPICAtom(*this, target
, forLazyDylib
, false, weakImport
);
184 return new ld::passes::stubs::ppc::classic::StubNoPICAtom(*this, target
, forLazyDylib
, false, weakImport
);
186 case CPU_TYPE_POWERPC64
:
187 return new ld::passes::stubs::ppc::classic::StubPICAtom(*this, target
, forLazyDylib
, true, weakImport
);
190 if ( usingCompressedLINKEDIT() && !forLazyDylib
)
191 return new ld::passes::stubs::x86::StubAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
193 return new ld::passes::stubs::x86::classic::StubAtom(*this, target
, forLazyDylib
, weakImport
);
195 case CPU_TYPE_X86_64
:
196 if ( usingCompressedLINKEDIT() && !forLazyDylib
)
197 return new ld::passes::stubs::x86_64::StubAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
199 return new ld::passes::stubs::x86_64::classic::StubAtom(*this, target
, forLazyDylib
, weakImport
);
202 if ( usingCompressedLINKEDIT() && !forLazyDylib
) {
203 if ( (_stubCount
< 900) && !_mightBeInSharedRegion
&& !_largeText
)
204 return new ld::passes::stubs::arm::StubCloseAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
206 return new ld::passes::stubs::arm::StubPICAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
208 return new ld::passes::stubs::arm::StubNoPICAtom(*this, target
, stubToGlobalWeakDef
, stubToResolver
, weakImport
);
212 return new ld::passes::stubs::arm::classic::StubPICAtom(*this, target
, forLazyDylib
, weakImport
);
214 return new ld::passes::stubs::arm::classic::StubNoPICAtom(*this, target
, forLazyDylib
, weakImport
);
218 throw "unsupported arch for stub";
222 void Pass::verifyNoResolverFunctions(ld::Internal
& state
)
224 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
=state
.sections
.begin(); sit
!= state
.sections
.end(); ++sit
) {
225 ld::Internal::FinalSection
* sect
= *sit
;
226 for (std::vector
<const ld::Atom
*>::iterator ait
=sect
->atoms
.begin(); ait
!= sect
->atoms
.end(); ++ait
) {
227 const ld::Atom
* atom
= *ait
;
228 if ( atom
->contentType() == ld::Atom::typeResolver
)
229 throwf("resolver function '%s' not supported in type of output", atom
->name());
234 void Pass::process(ld::Internal
& state
)
236 switch ( _options
.outputKind() ) {
237 case Options::kObjectFile
:
238 // these kinds don't use stubs and can have resolver functions
240 case Options::kKextBundle
:
241 case Options::kStaticExecutable
:
242 case Options::kPreload
:
244 // these kinds don't use stubs and cannot have resolver functions
245 verifyNoResolverFunctions(state
);
247 case Options::kDynamicLibrary
:
248 // uses stubs and can have resolver functions
250 case Options::kDynamicExecutable
:
251 case Options::kDynamicBundle
:
252 // these kinds do use stubs and cannot have resolver functions
253 verifyNoResolverFunctions(state
);
257 // walk all atoms and fixups looking for stubable references
258 // don't create stubs inline because that could invalidate the sections iterator
259 std::vector
<const ld::Atom
*> atomsCallingStubs
;
260 std::map
<const ld::Atom
*,ld::Atom
*> stubFor
;
261 std::map
<const ld::Atom
*,bool> weakImportMap
;
262 atomsCallingStubs
.reserve(128);
263 uint64_t codeSize
= 0;
264 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
=state
.sections
.begin(); sit
!= state
.sections
.end(); ++sit
) {
265 ld::Internal::FinalSection
* sect
= *sit
;
266 for (std::vector
<const ld::Atom
*>::iterator ait
=sect
->atoms
.begin(); ait
!= sect
->atoms
.end(); ++ait
) {
267 const ld::Atom
* atom
= *ait
;
268 codeSize
+= atom
->size();
269 bool atomNeedsStub
= false;
270 for (ld::Fixup::iterator fit
= atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
271 const ld::Atom
* stubableTargetOfFixup
= stubableFixup(fit
, state
);
272 if ( stubableTargetOfFixup
!= NULL
) {
273 if ( !atomNeedsStub
) {
274 atomsCallingStubs
.push_back(atom
);
275 atomNeedsStub
= true;
277 stubFor
[stubableTargetOfFixup
] = NULL
;
278 // record weak_import attribute
279 std::map
<const ld::Atom
*,bool>::iterator pos
= weakImportMap
.find(stubableTargetOfFixup
);
280 if ( pos
== weakImportMap
.end() ) {
281 // target not in weakImportMap, so add
282 weakImportMap
[stubableTargetOfFixup
] = fit
->weakImport
;
285 // target in weakImportMap, check for weakness mismatch
286 if ( pos
->second
!= fit
->weakImport
) {
288 switch ( _options
.weakReferenceMismatchTreatment() ) {
289 case Options::kWeakReferenceMismatchError
:
290 throwf("mismatching weak references for symbol: %s", stubableTargetOfFixup
->name());
291 case Options::kWeakReferenceMismatchWeak
:
294 case Options::kWeakReferenceMismatchNonWeak
:
302 // all resolver functions must have a corresponding stub
303 if ( atom
->contentType() == ld::Atom::typeResolver
) {
304 if ( _options
.outputKind() != Options::kDynamicLibrary
)
305 throwf("resolver functions (%s) can only be used in dylibs", atom
->name());
306 if ( !_options
.makeCompressedDyldInfo() ) {
307 if ( _options
.architecture() == CPU_TYPE_POWERPC
)
308 throwf("resolver functions (%s) not supported for PowerPC", atom
->name());
309 else if ( _options
.architecture() == CPU_TYPE_ARM
)
310 throwf("resolver functions (%s) can only be used when targeting iOS 4.2 or later", atom
->name());
312 throwf("resolver functions (%s) can only be used when targeting Mac OS X 10.6 or later", atom
->name());
314 stubFor
[atom
] = NULL
;
319 // short circuit if no stubs needed
321 _stubCount
= stubFor
.size();
322 if ( _stubCount
== 0 )
325 // <rdar://problem/8553283> lazily check for helper
326 if ( !_options
.makeCompressedDyldInfo() && (state
.classicBindingHelper
== NULL
) )
327 throw "symbol dyld_stub_binding_helper not found, normally in crt1.o/dylib1.o/bundle1.o";
329 // disable arm close stubs in some cases
330 if ( _architecture
== CPU_TYPE_ARM
) {
331 if ( codeSize
> 4*1024*1024 )
334 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
=state
.sections
.begin(); sit
!= state
.sections
.end(); ++sit
) {
335 ld::Internal::FinalSection
* sect
= *sit
;
336 if ( sect
->type() == ld::Section::typeMachHeader
)
338 if ( strcmp(sect
->segmentName(), "__TEXT") == 0) {
339 for (std::vector
<const ld::Atom
*>::iterator ait
=sect
->atoms
.begin(); ait
!= sect
->atoms
.end(); ++ait
) {
340 const ld::Atom
* atom
= *ait
;
341 if ( atom
->alignment().powerOf2
> 10 ) {
342 // overaligned section means might not be able to keep closestubs sect pushed to end of __TEXT
343 //warning("alignment 1<<%d in atom %s in section %s disables close stubs optimization",
344 // atom->alignment().powerOf2, atom->name(), sect->segmentName());
355 for (std::map
<const ld::Atom
*,ld::Atom
*>::iterator it
= stubFor
.begin(); it
!= stubFor
.end(); ++it
) {
356 it
->second
= makeStub(*it
->first
, weakImportMap
[it
->first
]);
359 // updated atoms to use stubs
360 for (std::vector
<const ld::Atom
*>::iterator it
=atomsCallingStubs
.begin(); it
!= atomsCallingStubs
.end(); ++it
) {
361 const ld::Atom
* atom
= *it
;
362 for (ld::Fixup::iterator fit
= atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
363 const ld::Atom
* stubableTargetOfFixup
= stubableFixup(fit
, state
);
364 if ( stubableTargetOfFixup
!= NULL
) {
365 ld::Atom
* stub
= stubFor
[stubableTargetOfFixup
];
366 assert(stub
!= NULL
&& "stub not created");
367 fit
->binding
= ld::Fixup::bindingDirectlyBound
;
368 fit
->u
.target
= stub
;
373 // sort new atoms so links are consistent
374 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
=state
.sections
.begin(); sit
!= state
.sections
.end(); ++sit
) {
375 ld::Internal::FinalSection
* sect
= *sit
;
376 switch ( sect
->type() ) {
377 case ld::Section::typeStubHelper
:
378 case ld::Section::typeStub
:
379 case ld::Section::typeStubClose
:
380 case ld::Section::typeLazyPointer
:
381 case ld::Section::typeLazyPointerClose
:
382 std::sort(sect
->atoms
.begin(), sect
->atoms
.end(), AtomByNameSorter());
392 void doPass(const Options
& opts
, ld::Internal
& internal
)
395 pass
.process(internal
);
401 } // namespace passes