1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009 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@
33 #include <ext/hash_map>
42 class File
; // forward reference
44 class GOTEntryAtom
: public ld::Atom
{
46 GOTEntryAtom(ld::Internal
& internal
, const ld::Atom
* target
, bool weakImport
)
47 : ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
48 ld::Atom::scopeLinkageUnit
, ld::Atom::typeNonLazyPointer
,
49 symbolTableNotIn
, false, false, false, ld::Atom::Alignment(3)),
50 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindStoreTargetAddressLittleEndian64
, target
),
52 { _fixup
.weakImport
= weakImport
; internal
.addAtom(*this); }
54 virtual const ld::File
* file() const { return NULL
; }
55 virtual bool translationUnitSource(const char** dir
, const char**) const
57 virtual const char* name() const { return _target
->name(); }
58 virtual uint64_t size() const { return 8; }
59 virtual uint64_t objectAddress() const { return 0; }
60 virtual void copyRawContent(uint8_t buffer
[]) const { }
61 virtual void setScope(Scope
) { }
62 virtual ld::Fixup::iterator
fixupsBegin() const { return &_fixup
; }
63 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
66 mutable ld::Fixup _fixup
;
67 const ld::Atom
* _target
;
69 static ld::Section _s_section
;
72 ld::Section
GOTEntryAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer
);
75 static bool gotFixup(const Options
& opts
, ld::Internal
& internal
, const ld::Atom
* targetOfGOT
, const ld::Fixup
* fixup
, bool* optimizable
)
77 switch (fixup
->kind
) {
78 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
:
79 // start by assuming this can be optimized
81 // cannot do LEA optimization if target is in another dylib
82 if ( targetOfGOT
->definition() == ld::Atom::definitionProxy
)
84 // cannot do LEA optimization if target in __huge section
85 if ( internal
.usingHugeSections
&& (targetOfGOT
->size() > 1024*1024)
86 && ( (targetOfGOT
->section().type() == ld::Section::typeZeroFill
)
87 || (targetOfGOT
->section().type() == ld::Section::typeTentativeDefs
)) ) {
90 if ( targetOfGOT
->scope() == ld::Atom::scopeGlobal
) {
91 // cannot do LEA optimization if target is weak exported symbol
92 if ( (targetOfGOT
->definition() == ld::Atom::definitionRegular
) && (targetOfGOT
->combine() == ld::Atom::combineByName
) ) {
93 switch ( opts
.outputKind() ) {
94 case Options::kDynamicExecutable
:
95 case Options::kDynamicLibrary
:
96 case Options::kDynamicBundle
:
97 case Options::kKextBundle
:
100 case Options::kStaticExecutable
:
102 case Options::kPreload
:
103 case Options::kObjectFile
:
107 // cannot do LEA optimization if target is interposable
108 if ( opts
.interposable(targetOfGOT
->name()) )
109 *optimizable
= false;
110 // cannot do LEA optimization if target is resolver function
111 if ( targetOfGOT
->contentType() == ld::Atom::typeResolver
)
112 *optimizable
= false;
113 // cannot do LEA optimization for flat-namespace
114 if ( opts
.nameSpace() != Options::kTwoLevelNameSpace
)
115 *optimizable
= false;
118 case ld::Fixup::kindStoreX86PCRel32GOT
:
119 *optimizable
= false;
121 case ld::Fixup::kindNoneGroupSubordinatePersonality
:
122 *optimizable
= false;
131 struct AtomByNameSorter
133 bool operator()(const ld::Atom
* left
, const ld::Atom
* right
)
135 return (strcmp(left
->name(), right
->name()) < 0);
139 void doPass(const Options
& opts
, ld::Internal
& internal
)
141 const bool log
= false;
143 // only make got section in final linked images
144 if ( opts
.outputKind() == Options::kObjectFile
)
147 // walk all atoms and fixups looking for GOT-able references
148 // don't create GOT atoms during this loop because that could invalidate the sections iterator
149 std::vector
<const ld::Atom
*> atomsReferencingGOT
;
150 std::map
<const ld::Atom
*,ld::Atom
*> gotMap
;
151 std::map
<const ld::Atom
*,bool> weakImportMap
;
152 atomsReferencingGOT
.reserve(128);
153 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
=internal
.sections
.begin(); sit
!= internal
.sections
.end(); ++sit
) {
154 ld::Internal::FinalSection
* sect
= *sit
;
155 for (std::vector
<const ld::Atom
*>::iterator ait
=sect
->atoms
.begin(); ait
!= sect
->atoms
.end(); ++ait
) {
156 const ld::Atom
* atom
= *ait
;
157 bool atomUsesGOT
= false;
158 const ld::Atom
* targetOfGOT
= NULL
;
159 bool targetIsWeakImport
= false;
160 for (ld::Fixup::iterator fit
= atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
161 if ( fit
->firstInCluster() )
163 switch ( fit
->binding
) {
164 case ld::Fixup::bindingsIndirectlyBound
:
165 targetOfGOT
= internal
.indirectBindingTable
[fit
->u
.bindingIndex
];
166 targetIsWeakImport
= fit
->weakImport
;
168 case ld::Fixup::bindingDirectlyBound
:
169 targetOfGOT
= fit
->u
.target
;
170 targetIsWeakImport
= fit
->weakImport
;
176 if ( !gotFixup(opts
, internal
, targetOfGOT
, fit
, &optimizable
) )
179 // change from load of GOT entry to lea of target
180 if ( log
) fprintf(stderr
, "optimized GOT usage in %s to %s\n", atom
->name(), targetOfGOT
->name());
181 switch ( fit
->binding
) {
182 case ld::Fixup::bindingsIndirectlyBound
:
183 case ld::Fixup::bindingDirectlyBound
:
184 fit
->binding
= ld::Fixup::bindingDirectlyBound
;
185 fit
->u
.target
= targetOfGOT
;
186 fit
->kind
= ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoadNowLEA
;
189 assert(0 && "unsupported GOT reference");
194 // remember that we need to use GOT in this function
195 if ( log
) fprintf(stderr
, "found GOT use in %s to %s\n", atom
->name(), targetOfGOT
->name());
196 if ( !atomUsesGOT
) {
197 atomsReferencingGOT
.push_back(atom
);
200 gotMap
[targetOfGOT
] = NULL
;
201 // record weak_import attribute
202 std::map
<const ld::Atom
*,bool>::iterator pos
= weakImportMap
.find(targetOfGOT
);
203 if ( pos
== weakImportMap
.end() ) {
204 // target not in weakImportMap, so add
205 if ( log
) fprintf(stderr
, "weakImportMap[%s] = %d\n", targetOfGOT
->name(), targetIsWeakImport
);
206 weakImportMap
[targetOfGOT
] = targetIsWeakImport
;
209 // target in weakImportMap, check for weakness mismatch
210 if ( pos
->second
!= targetIsWeakImport
) {
212 switch ( opts
.weakReferenceMismatchTreatment() ) {
213 case Options::kWeakReferenceMismatchError
:
214 throwf("mismatching weak references for symbol: %s", targetOfGOT
->name());
215 case Options::kWeakReferenceMismatchWeak
:
218 case Options::kWeakReferenceMismatchNonWeak
:
230 for (std::map
<const ld::Atom
*,ld::Atom
*>::iterator it
= gotMap
.begin(); it
!= gotMap
.end(); ++it
) {
231 it
->second
= new GOTEntryAtom(internal
, it
->first
, weakImportMap
[it
->first
]);
234 // update atoms to use GOT entries
235 for (std::vector
<const ld::Atom
*>::iterator it
=atomsReferencingGOT
.begin(); it
!= atomsReferencingGOT
.end(); ++it
) {
236 const ld::Atom
* atom
= *it
;
237 const ld::Atom
* targetOfGOT
= NULL
;
238 ld::Fixup::iterator fitThatSetTarget
= NULL
;
239 for (ld::Fixup::iterator fit
= atom
->fixupsBegin(), end
=atom
->fixupsEnd(); fit
!= end
; ++fit
) {
240 if ( fit
->firstInCluster() ) {
242 fitThatSetTarget
= NULL
;
244 switch ( fit
->binding
) {
245 case ld::Fixup::bindingsIndirectlyBound
:
246 targetOfGOT
= internal
.indirectBindingTable
[fit
->u
.bindingIndex
];
247 fitThatSetTarget
= fit
;
249 case ld::Fixup::bindingDirectlyBound
:
250 targetOfGOT
= fit
->u
.target
;
251 fitThatSetTarget
= fit
;
257 if ( (targetOfGOT
== NULL
) || !gotFixup(opts
, internal
, targetOfGOT
, fit
, &optimizable
) )
259 if ( !optimizable
) {
260 // GOT use not optimized away, update to bind to GOT entry
261 assert(fitThatSetTarget
!= NULL
);
262 switch ( fitThatSetTarget
->binding
) {
263 case ld::Fixup::bindingsIndirectlyBound
:
264 case ld::Fixup::bindingDirectlyBound
:
265 fitThatSetTarget
->binding
= ld::Fixup::bindingDirectlyBound
;
266 fitThatSetTarget
->u
.target
= gotMap
[targetOfGOT
];
269 assert(0 && "unsupported GOT reference");
276 // sort new atoms so links are consistent
277 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
=internal
.sections
.begin(); sit
!= internal
.sections
.end(); ++sit
) {
278 ld::Internal::FinalSection
* sect
= *sit
;
279 if ( sect
->type() == ld::Section::typeNonLazyPointer
) {
280 std::sort(sect
->atoms
.begin(), sect
->atoms
.end(), AtomByNameSorter());
287 } // namespace passes