-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlvp.cpp
293 lines (264 loc) · 10.8 KB
/
tlvp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
*
* Copyright (c) 2010 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include <stdint.h>
#include <math.h>
#include <unistd.h>
#include <dlfcn.h>
#include <vector>
#include <map>
#include <ext/hash_map>
#include "ld.hpp"
#include "tlvp.h"
namespace ld {
namespace passes {
namespace tlvp {
class File; // forward reference
class TLVEntryAtom : public ld::Atom {
public:
TLVEntryAtom(ld::Internal& internal, const ld::Atom* target, bool weakImport)
: ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)),
_fixup(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64, target),
_target(target)
{ _fixup.weakImport = weakImport; internal.addAtom(*this); }
virtual const ld::File* file() const { return NULL; }
virtual const char* name() const { return _target->name(); }
virtual uint64_t size() const { return 8; }
virtual uint64_t objectAddress() const { return 0; }
virtual void copyRawContent(uint8_t buffer[]) const { }
virtual void setScope(Scope) { }
virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup; }
virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
private:
mutable ld::Fixup _fixup;
const ld::Atom* _target;
static ld::Section _s_section;
};
ld::Section TLVEntryAtom::_s_section("__DATA", "__thread_ptrs", ld::Section::typeTLVPointers);
static bool optimizable(const Options& opts, const ld::Atom* targetOfTLV)
{
// cannot do LEA optimization if target is in another dylib
if ( targetOfTLV->definition() == ld::Atom::definitionProxy )
return false;
if ( targetOfTLV->scope() == ld::Atom::scopeGlobal ) {
// cannot do LEA optimization if target is weak exported symbol
if ( (targetOfTLV->definition() == ld::Atom::definitionRegular) && (targetOfTLV->combine() == ld::Atom::combineByName) )
return false;
// cannot do LEA optimization if target is interposable
if ( opts.interposable(targetOfTLV->name()) )
return false;
// cannot do LEA optimization for flat-namespace
if ( opts.nameSpace() != Options::kTwoLevelNameSpace )
return false;
}
return true;
}
struct AtomByNameSorter
{
bool operator()(const ld::Atom* left, const ld::Atom* right)
{
return (strcmp(left->name(), right->name()) < 0);
}
};
struct TlVReferenceCluster
{
const ld::Atom* targetOfTLV;
ld::Fixup* fixupWithTarget;
ld::Fixup* fixupWithTLVStore;
bool optimizable;
};
void doPass(const Options& opts, ld::Internal& internal)
{
const bool log = false;
// only make tlv section in final linked images
if ( opts.outputKind() == Options::kObjectFile )
return;
// walk all atoms and fixups looking for TLV references and add them to list
std::vector<TlVReferenceCluster> references;
for (std::vector<ld::Internal::FinalSection*>::iterator sit=internal.sections.begin(); sit != internal.sections.end(); ++sit) {
ld::Internal::FinalSection* sect = *sit;
for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
const ld::Atom* atom = *ait;
TlVReferenceCluster ref;
for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
if ( fit->firstInCluster() ) {
ref.targetOfTLV = NULL;
ref.fixupWithTarget = NULL;
ref.fixupWithTLVStore = NULL;
}
switch ( fit->binding ) {
case ld::Fixup::bindingsIndirectlyBound:
ref.targetOfTLV = internal.indirectBindingTable[fit->u.bindingIndex];
ref.fixupWithTarget = fit;
break;
case ld::Fixup::bindingDirectlyBound:
ref.targetOfTLV = fit->u.target;
ref.fixupWithTarget = fit;
break;
default:
break;
}
switch ( fit->kind ) {
case ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad:
case ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad:
case ld::Fixup::kindStoreX86PCRel32TLVLoad:
case ld::Fixup::kindStoreX86Abs32TLVLoad:
ref.fixupWithTLVStore = fit;
break;
default:
break;
}
if ( fit->lastInCluster() && (ref.fixupWithTLVStore != NULL) ) {
ref.optimizable = optimizable(opts, ref.targetOfTLV);
if (log) fprintf(stderr, "found reference to TLV at %s+0x%X to %s\n",
atom->name(), ref.fixupWithTLVStore->offsetInAtom, ref.targetOfTLV->name());
if ( ! opts.canUseThreadLocalVariables() ) {
throwf("targeted OS version does not support use of thread local variables in %s", atom->name());
}
references.push_back(ref);
}
}
}
}
// compute which TLV references will be weak_imports
std::map<const ld::Atom*,bool> weakImportMap;
for(std::vector<TlVReferenceCluster>::iterator it=references.begin(); it != references.end(); ++it) {
if ( !it->optimizable ) {
// record weak_import attribute
std::map<const ld::Atom*,bool>::iterator pos = weakImportMap.find(it->targetOfTLV);
if ( pos == weakImportMap.end() ) {
// target not in weakImportMap, so add
weakImportMap[it->targetOfTLV] = it->fixupWithTarget->weakImport;
}
else {
// target in weakImportMap, check for weakness mismatch
if ( pos->second != it->fixupWithTarget->weakImport ) {
// found mismatch
switch ( opts.weakReferenceMismatchTreatment() ) {
case Options::kWeakReferenceMismatchError:
throwf("mismatching weak references for symbol: %s", it->targetOfTLV->name());
case Options::kWeakReferenceMismatchWeak:
pos->second = true;
break;
case Options::kWeakReferenceMismatchNonWeak:
pos->second = false;
break;
}
}
}
}
}
// create TLV pointers for TLV references that cannot be optimized
std::map<const ld::Atom*,ld::Atom*> variableToPointerMap;
for(std::map<const ld::Atom*,bool>::iterator it=weakImportMap.begin(); it != weakImportMap.end(); ++it) {
std::map<const ld::Atom*,ld::Atom*>::iterator pos = variableToPointerMap.find(it->first);
if ( pos == variableToPointerMap.end() ) {
if (log) fprintf(stderr, "make TLV pointer for %s\n", it->first->name());
if ( it->first->contentType() != ld::Atom::typeTLV )
throwf("illegal thread local variable reference to regular symbol %s", it->first->name());
TLVEntryAtom* tlvp = new TLVEntryAtom(internal, it->first, it->second);
variableToPointerMap[it->first] = tlvp;
}
}
// sort new tvlp atoms so links are consistent
for (std::vector<ld::Internal::FinalSection*>::iterator sit=internal.sections.begin(); sit != internal.sections.end(); ++sit) {
ld::Internal::FinalSection* sect = *sit;
if ( sect->type() == ld::Section::typeTLVPointers ) {
std::sort(sect->atoms.begin(), sect->atoms.end(), AtomByNameSorter());
}
}
// update references to use TLV pointers or TLV object directly
for(std::vector<TlVReferenceCluster>::iterator it=references.begin(); it != references.end(); ++it) {
if ( it->optimizable ) {
// change store to be LEA instead load (mov)
if (log) fprintf(stderr, "optimizing load of TLV to %s into an LEA\n", it->targetOfTLV->name());
it->fixupWithTLVStore->binding = ld::Fixup::bindingDirectlyBound;
it->fixupWithTLVStore->u.target = it->targetOfTLV;
switch ( it->fixupWithTLVStore->kind ) {
case ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad:
it->fixupWithTLVStore->kind = ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoadNowLEA;
break;
case ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad:
it->fixupWithTLVStore->kind = ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoadNowLEA;
break;
case ld::Fixup::kindStoreX86PCRel32TLVLoad:
it->fixupWithTLVStore->kind = ld::Fixup::kindStoreX86PCRel32TLVLoadNowLEA;
break;
case ld::Fixup::kindStoreX86Abs32TLVLoad:
it->fixupWithTLVStore->kind = ld::Fixup::kindStoreX86Abs32TLVLoadNowLEA;
break;
default:
assert(0 && "bad store kind for TLV optimization");
}
}
else {
// change target to be new TLV pointer atom
if (log) fprintf(stderr, "updating load of TLV to %s to load from TLV pointer\n", it->targetOfTLV->name());
const ld::Atom* tlvpAtom = variableToPointerMap[it->targetOfTLV];
assert(tlvpAtom != NULL);
it->fixupWithTarget->binding = ld::Fixup::bindingDirectlyBound;
it->fixupWithTarget->u.target = tlvpAtom;
}
}
// alter tlv definitions to have an offset as third field
for (std::vector<ld::Internal::FinalSection*>::iterator sit=internal.sections.begin(); sit != internal.sections.end(); ++sit) {
ld::Internal::FinalSection* sect = *sit;
if ( sect->type() != ld::Section::typeTLVDefs )
continue;
for (std::vector<const ld::Atom*>::iterator ait=sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
const ld::Atom* atom = *ait;
for (ld::Fixup::iterator fit = atom->fixupsBegin(), end=atom->fixupsEnd(); fit != end; ++fit) {
if ( fit->offsetInAtom != 0 ) {
assert(fit->binding == ld::Fixup::bindingDirectlyBound && "thread variable def contains pointer to global");
switch( fit->u.target->contentType() ) {
case ld::Atom::typeTLVZeroFill:
case ld::Atom::typeTLVInitialValue:
switch ( fit->kind ) {
case ld::Fixup::kindSetTargetAddress:
fit->kind = ld::Fixup::kindSetTargetTLVTemplateOffset;
break;
case ld::Fixup::kindStoreTargetAddressLittleEndian32:
fit->kind = ld::Fixup::kindSetTargetTLVTemplateOffsetLittleEndian32;
break;
case ld::Fixup::kindStoreTargetAddressLittleEndian64:
fit->kind = ld::Fixup::kindSetTargetTLVTemplateOffsetLittleEndian64;
break;
default:
assert(0 && "bad kind for target in tlv defs");
break;
}
break;
default:
assert(0 && "wrong content type for target in tlv defs");
break;
}
}
}
}
}
}
} // namespace tlvp
} // namespace passes
} // namespace ld