]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/got.cpp
bb94ddff4df855b55fb0760f39dcca5e1236b919
[apple/ld64.git] / src / ld / passes / got.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009 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
31 #include <vector>
32 #include <map>
33 #include <ext/hash_map>
34
35 #include "ld.hpp"
36 #include "got.h"
37
38 namespace ld {
39 namespace passes {
40 namespace got {
41
42 class File; // forward reference
43
44 class GOTEntryAtom : public ld::Atom {
45 public:
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),
51 _target(target)
52 { _fixup.weakImport = weakImport; internal.addAtom(*this); }
53
54 virtual const ld::File* file() const { return NULL; }
55 virtual bool translationUnitSource(const char** dir, const char**) const
56 { return false; }
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]; }
64
65 private:
66 mutable ld::Fixup _fixup;
67 const ld::Atom* _target;
68
69 static ld::Section _s_section;
70 };
71
72 ld::Section GOTEntryAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
73
74
75 static bool gotFixup(const Options& opts, ld::Internal& internal, const ld::Atom* targetOfGOT, const ld::Fixup* fixup, bool* optimizable)
76 {
77 switch (fixup->kind) {
78 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad:
79 // start by assuming this can be optimized
80 *optimizable = true;
81 // cannot do LEA optimization if target is in another dylib
82 if ( targetOfGOT->definition() == ld::Atom::definitionProxy )
83 *optimizable = false;
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)) ) {
88 *optimizable = false;
89 }
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 *optimizable = false;
94 // cannot do LEA optimization if target is interposable
95 if ( opts.interposable(targetOfGOT->name()) )
96 *optimizable = false;
97 // cannot do LEA optimization if target is resolver function
98 if ( targetOfGOT->contentType() == ld::Atom::typeResolver )
99 *optimizable = false;
100 // cannot do LEA optimization for flat-namespace
101 if ( opts.nameSpace() != Options::kTwoLevelNameSpace )
102 *optimizable = false;
103 }
104 return true;
105 case ld::Fixup::kindStoreX86PCRel32GOT:
106 *optimizable = false;
107 return true;
108 case ld::Fixup::kindNoneGroupSubordinatePersonality:
109 *optimizable = false;
110 return true;
111 default:
112 break;
113 }
114
115 return false;
116 }
117
118 struct AtomByNameSorter
119 {
120 bool operator()(const ld::Atom* left, const ld::Atom* right)
121 {
122 return (strcmp(left->name(), right->name()) < 0);
123 }
124 };
125
126 void doPass(const Options& opts, ld::Internal& internal)
127 {
128 const bool log = false;
129
130 // only make got section in final linked images
131 if ( opts.outputKind() == Options::kObjectFile )
132 return;
133
134 // walk all atoms and fixups looking for GOT-able references
135 // don't create GOT atoms during this loop because that could invalidate the sections iterator
136 std::vector<const ld::Atom*> atomsReferencingGOT;
137 std::map<const ld::Atom*,ld::Atom*> gotMap;
138 std::map<const ld::Atom*,bool> weakImportMap;
139 atomsReferencingGOT.reserve(128);
140 for (std::vector<ld::Internal::FinalSection*>::iterator sit=internal.sections.begin(); sit != internal.sections.end(); ++sit) {
141 ld::Internal::FinalSection* sect = *sit;
142 for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
143 const ld::Atom* atom = *ait;
144 bool atomUsesGOT = false;
145 const ld::Atom* targetOfGOT = NULL;
146 bool targetIsWeakImport = false;
147 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
148 if ( fit->firstInCluster() )
149 targetOfGOT = NULL;
150 switch ( fit->binding ) {
151 case ld::Fixup::bindingsIndirectlyBound:
152 targetOfGOT = internal.indirectBindingTable[fit->u.bindingIndex];
153 targetIsWeakImport = fit->weakImport;
154 break;
155 case ld::Fixup::bindingDirectlyBound:
156 targetOfGOT = fit->u.target;
157 targetIsWeakImport = fit->weakImport;
158 break;
159 default:
160 break;
161 }
162 bool optimizable;
163 if ( !gotFixup(opts, internal, targetOfGOT, fit, &optimizable) )
164 continue;
165 if ( optimizable ) {
166 // change from load of GOT entry to lea of target
167 if ( log ) fprintf(stderr, "optimized GOT usage in %s to %s\n", atom->name(), targetOfGOT->name());
168 switch ( fit->binding ) {
169 case ld::Fixup::bindingsIndirectlyBound:
170 case ld::Fixup::bindingDirectlyBound:
171 fit->binding = ld::Fixup::bindingDirectlyBound;
172 fit->u.target = targetOfGOT;
173 fit->kind = ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoadNowLEA;
174 break;
175 default:
176 assert(0 && "unsupported GOT reference");
177 break;
178 }
179 }
180 else {
181 // remember that we need to use GOT in this function
182 if ( log ) fprintf(stderr, "found GOT use in %s to %s\n", atom->name(), targetOfGOT->name());
183 if ( !atomUsesGOT ) {
184 atomsReferencingGOT.push_back(atom);
185 atomUsesGOT = true;
186 }
187 gotMap[targetOfGOT] = NULL;
188 // record weak_import attribute
189 std::map<const ld::Atom*,bool>::iterator pos = weakImportMap.find(targetOfGOT);
190 if ( pos == weakImportMap.end() ) {
191 // target not in weakImportMap, so add
192 if ( log ) fprintf(stderr, "weakImportMap[%s] = %d\n", targetOfGOT->name(), targetIsWeakImport);
193 weakImportMap[targetOfGOT] = targetIsWeakImport;
194 }
195 else {
196 // target in weakImportMap, check for weakness mismatch
197 if ( pos->second != targetIsWeakImport ) {
198 // found mismatch
199 switch ( opts.weakReferenceMismatchTreatment() ) {
200 case Options::kWeakReferenceMismatchError:
201 throwf("mismatching weak references for symbol: %s", targetOfGOT->name());
202 case Options::kWeakReferenceMismatchWeak:
203 pos->second = true;
204 break;
205 case Options::kWeakReferenceMismatchNonWeak:
206 pos->second = false;
207 break;
208 }
209 }
210 }
211 }
212 }
213 }
214 }
215
216 // make GOT entries
217 for (std::map<const ld::Atom*,ld::Atom*>::iterator it = gotMap.begin(); it != gotMap.end(); ++it) {
218 it->second = new GOTEntryAtom(internal, it->first, weakImportMap[it->first]);
219 }
220
221 // update atoms to use GOT entries
222 for (std::vector<const ld::Atom*>::iterator it=atomsReferencingGOT.begin(); it != atomsReferencingGOT.end(); ++it) {
223 const ld::Atom* atom = *it;
224 const ld::Atom* targetOfGOT = NULL;
225 ld::Fixup::iterator fitThatSetTarget = NULL;
226 for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
227 if ( fit->firstInCluster() ) {
228 targetOfGOT = NULL;
229 fitThatSetTarget = NULL;
230 }
231 switch ( fit->binding ) {
232 case ld::Fixup::bindingsIndirectlyBound:
233 targetOfGOT = internal.indirectBindingTable[fit->u.bindingIndex];
234 fitThatSetTarget = fit;
235 break;
236 case ld::Fixup::bindingDirectlyBound:
237 targetOfGOT = fit->u.target;
238 fitThatSetTarget = fit;
239 break;
240 default:
241 break;
242 }
243 bool optimizable;
244 if ( (targetOfGOT == NULL) || !gotFixup(opts, internal, targetOfGOT, fit, &optimizable) )
245 continue;
246 if ( !optimizable ) {
247 // GOT use not optimized away, update to bind to GOT entry
248 assert(fitThatSetTarget != NULL);
249 switch ( fitThatSetTarget->binding ) {
250 case ld::Fixup::bindingsIndirectlyBound:
251 case ld::Fixup::bindingDirectlyBound:
252 fitThatSetTarget->binding = ld::Fixup::bindingDirectlyBound;
253 fitThatSetTarget->u.target = gotMap[targetOfGOT];
254 break;
255 default:
256 assert(0 && "unsupported GOT reference");
257 break;
258 }
259 }
260 }
261 }
262
263 // sort new atoms so links are consistent
264 for (std::vector<ld::Internal::FinalSection*>::iterator sit=internal.sections.begin(); sit != internal.sections.end(); ++sit) {
265 ld::Internal::FinalSection* sect = *sit;
266 if ( sect->type() == ld::Section::typeNonLazyPointer ) {
267 std::sort(sect->atoms.begin(), sect->atoms.end(), AtomByNameSorter());
268 }
269 }
270 }
271
272
273 } // namespace got
274 } // namespace passes
275 } // namespace ld