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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
//===- ExpandTls.cpp - Convert TLS variables to a concrete layout----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This pass expands out uses of thread-local (TLS) variables into
// more primitive operations.
//
// A reference to the address of a TLS variable is expanded into code
// which gets the current thread's thread pointer using
// @llvm.nacl.read.tp() and adds a fixed offset.
//
// This pass allocates the offsets (relative to the thread pointer)
// that will be used for TLS variables. It sets up the global
// variables __tls_template_start, __tls_template_end etc. to contain
// a template for initializing TLS variables' values for each thread.
// This is a task normally performed by the linker in ELF systems.
//
//===----------------------------------------------------------------------===//
#include <vector>
#include "llvm/Pass.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/NaCl.h"
using namespace llvm;
namespace {
struct VarInfo {
GlobalVariable *TlsVar;
bool IsBss; // Whether variable is in zero-intialized part of template
int TemplateIndex;
};
class PassState {
public:
PassState(Module *M): M(M), DL(M), Offset(0), Alignment(1) {}
Module *M;
DataLayout DL;
uint64_t Offset;
// 'Alignment' is the maximum variable alignment seen so far, in
// bytes. After visiting all TLS variables, this is the overall
// alignment required for the TLS template.
uint32_t Alignment;
};
class ExpandTls : public ModulePass {
public:
static char ID; // Pass identification, replacement for typeid
ExpandTls() : ModulePass(ID) {
initializeExpandTlsPass(*PassRegistry::getPassRegistry());
}
virtual bool runOnModule(Module &M);
};
}
char ExpandTls::ID = 0;
INITIALIZE_PASS(ExpandTls, "nacl-expand-tls",
"Expand out TLS variables and fix TLS variable layout",
false, false)
static void setGlobalVariableValue(Module &M, const char *Name,
Constant *Value) {
GlobalVariable *Var = M.getNamedGlobal(Name);
if (!Var) {
// This warning can happen in a program that does not use a libc
// and does not initialize TLS variables. Such a program might be
// linked with "-nostdlib".
errs() << "Warning: Variable " << Name << " not referenced\n";
} else {
if (Var->hasInitializer()) {
report_fatal_error(std::string("Variable ") + Name +
" already has an initializer");
}
Var->replaceAllUsesWith(ConstantExpr::getBitCast(Value, Var->getType()));
Var->eraseFromParent();
}
}
// Insert alignment padding into the TLS template.
static void padToAlignment(PassState *State,
std::vector<Type*> *FieldTypes,
std::vector<Constant*> *FieldValues,
unsigned Alignment) {
if ((State->Offset & (Alignment - 1)) != 0) {
unsigned PadSize = Alignment - (State->Offset & (Alignment - 1));
Type *i8 = Type::getInt8Ty(State->M->getContext());
Type *PadType = ArrayType::get(i8, PadSize);
FieldTypes->push_back(PadType);
if (FieldValues)
FieldValues->push_back(Constant::getNullValue(PadType));
State->Offset += PadSize;
}
if (State->Alignment < Alignment) {
State->Alignment = Alignment;
}
}
static void addVarToTlsTemplate(PassState *State,
std::vector<Type*> *FieldTypes,
std::vector<Constant*> *FieldValues,
GlobalVariable *TlsVar) {
unsigned Alignment = State->DL.getPreferredAlignment(TlsVar);
padToAlignment(State, FieldTypes, FieldValues, Alignment);
FieldTypes->push_back(TlsVar->getType()->getElementType());
if (FieldValues)
FieldValues->push_back(TlsVar->getInitializer());
State->Offset +=
State->DL.getTypeAllocSize(TlsVar->getType()->getElementType());
}
static PointerType *buildTlsTemplate(Module &M, std::vector<VarInfo> *TlsVars) {
std::vector<Type*> FieldBssTypes;
std::vector<Type*> FieldInitTypes;
std::vector<Constant*> FieldInitValues;
PassState State(&M);
for (Module::global_iterator GV = M.global_begin();
GV != M.global_end();
++GV) {
if (GV->isThreadLocal()) {
if (!GV->hasInitializer()) {
// Since this is a whole-program transformation, "extern" TLS
// variables are not allowed at this point.
report_fatal_error(std::string("TLS variable without an initializer: ")
+ GV->getName());
}
if (!GV->getInitializer()->isNullValue()) {
addVarToTlsTemplate(&State, &FieldInitTypes,
&FieldInitValues, GV);
VarInfo Info;
Info.TlsVar = GV;
Info.IsBss = false;
Info.TemplateIndex = FieldInitTypes.size() - 1;
TlsVars->push_back(Info);
}
}
}
// Handle zero-initialized TLS variables in a second pass, because
// these should follow non-zero-initialized TLS variables.
for (Module::global_iterator GV = M.global_begin();
GV != M.global_end();
++GV) {
if (GV->isThreadLocal() && GV->getInitializer()->isNullValue()) {
addVarToTlsTemplate(&State, &FieldBssTypes, NULL, GV);
VarInfo Info;
Info.TlsVar = GV;
Info.IsBss = true;
Info.TemplateIndex = FieldBssTypes.size() - 1;
TlsVars->push_back(Info);
}
}
// Add final alignment padding so that
// (struct tls_struct *) __nacl_read_tp() - 1
// gives the correct, aligned start of the TLS variables given the
// x86-style layout we are using. This requires some more bytes to
// be memset() to zero at runtime. This wastage doesn't seem
// important gives that we're not trying to optimize packing by
// reordering to put similarly-aligned variables together.
padToAlignment(&State, &FieldBssTypes, NULL, State.Alignment);
// We create the TLS template structs as "packed" because we insert
// alignment padding ourselves, and LLVM's implicit insertion of
// padding would interfere with ours. tls_bss_template can start at
// a non-aligned address immediately following the last field in
// tls_init_template.
StructType *InitTemplateType =
StructType::create(M.getContext(), "tls_init_template");
InitTemplateType->setBody(FieldInitTypes, /*isPacked=*/true);
StructType *BssTemplateType =
StructType::create(M.getContext(), "tls_bss_template");
BssTemplateType->setBody(FieldBssTypes, /*isPacked=*/true);
StructType *TemplateType = StructType::create(M.getContext(), "tls_struct");
SmallVector<Type*, 2> TemplateTopFields;
TemplateTopFields.push_back(InitTemplateType);
TemplateTopFields.push_back(BssTemplateType);
TemplateType->setBody(TemplateTopFields, /*isPacked=*/true);
PointerType *TemplatePtrType = PointerType::get(TemplateType, 0);
// We define the following symbols, which are the same as those
// defined by NaCl's original customized binutils linker scripts:
// __tls_template_start
// __tls_template_tdata_end
// __tls_template_end
// We also define __tls_template_alignment, which was not defined by
// the original linker scripts.
const char *StartSymbol = "__tls_template_start";
Constant *TemplateData = ConstantStruct::get(InitTemplateType,
FieldInitValues);
GlobalVariable *TemplateDataVar =
new GlobalVariable(M, InitTemplateType, /*isConstant=*/true,
GlobalValue::InternalLinkage, TemplateData);
setGlobalVariableValue(M, StartSymbol, TemplateDataVar);
TemplateDataVar->setName(StartSymbol);
Constant *TdataEnd = ConstantExpr::getGetElementPtr(
TemplateDataVar,
ConstantInt::get(M.getContext(), APInt(32, 1)));
setGlobalVariableValue(M, "__tls_template_tdata_end", TdataEnd);
Constant *TotalEnd = ConstantExpr::getGetElementPtr(
ConstantExpr::getBitCast(TemplateDataVar, TemplatePtrType),
ConstantInt::get(M.getContext(), APInt(32, 1)));
setGlobalVariableValue(M, "__tls_template_end", TotalEnd);
const char *AlignmentSymbol = "__tls_template_alignment";
Type *i32 = Type::getInt32Ty(M.getContext());
GlobalVariable *AlignmentVar = new GlobalVariable(
M, i32, /*isConstant=*/true,
GlobalValue::InternalLinkage,
ConstantInt::get(M.getContext(), APInt(32, State.Alignment)));
setGlobalVariableValue(M, AlignmentSymbol, AlignmentVar);
AlignmentVar->setName(AlignmentSymbol);
return TemplatePtrType;
}
static void rewriteTlsVars(Module &M, std::vector<VarInfo> *TlsVars,
PointerType *TemplatePtrType) {
// Set up the intrinsic that reads the thread pointer.
Function *ReadTpFunc = Intrinsic::getDeclaration(&M, Intrinsic::nacl_read_tp);
for (std::vector<VarInfo>::iterator VarInfo = TlsVars->begin();
VarInfo != TlsVars->end();
++VarInfo) {
GlobalVariable *Var = VarInfo->TlsVar;
while (!Var->use_empty()) {
Use *U = &Var->use_begin().getUse();
Instruction *InsertPt = PhiSafeInsertPt(U);
Value *RawThreadPtr = CallInst::Create(ReadTpFunc, "tls_raw", InsertPt);
Value *TypedThreadPtr = new BitCastInst(RawThreadPtr, TemplatePtrType,
"tls_struct", InsertPt);
SmallVector<Value*, 3> Indexes;
// We use -1 because we use the x86-style TLS layout in which
// the TLS data is stored at addresses below the thread pointer.
// This is largely because a check in nacl_irt_thread_create()
// in irt/irt_thread.c requires the thread pointer to be a
// self-pointer on x86-32.
// TODO(mseaborn): I intend to remove that check because it is
// non-portable. In the mean time, we want PNaCl pexes to work
// in older Chromium releases when translated to nexes.
Indexes.push_back(ConstantInt::get(
M.getContext(), APInt(32, -1)));
Indexes.push_back(ConstantInt::get(
M.getContext(), APInt(32, VarInfo->IsBss ? 1 : 0)));
Indexes.push_back(ConstantInt::get(
M.getContext(), APInt(32, VarInfo->TemplateIndex)));
Value *TlsField = GetElementPtrInst::Create(TypedThreadPtr, Indexes,
"field", InsertPt);
PhiSafeReplaceUses(U, TlsField);
}
VarInfo->TlsVar->eraseFromParent();
}
}
// Provide fixed definitions for PNaCl's TLS layout intrinsics. We
// adopt the x86-style layout: ExpandTls will output a program that
// uses the x86-style layout wherever it runs. This overrides any
// architecture-specific definitions of the intrinsics that the LLVM
// backend might provide.
static void defineTlsLayoutIntrinsics(Module &M) {
Type *i32 = Type::getInt32Ty(M.getContext());
SmallVector<Type*, 1> ArgTypes;
ArgTypes.push_back(i32);
FunctionType *FuncType = FunctionType::get(i32, ArgTypes, /*isVarArg=*/false);
Function *NewFunc;
BasicBlock *BB;
// Define the intrinsic as follows:
// uint32_t __nacl_tp_tdb_offset(uint32_t tdb_size) {
// return 0;
// }
// This means the thread pointer points to the TDB.
NewFunc = Function::Create(FuncType, GlobalValue::InternalLinkage,
"nacl_tp_tdb_offset", &M);
BB = BasicBlock::Create(M.getContext(), "entry", NewFunc);
ReturnInst::Create(M.getContext(),
ConstantInt::get(M.getContext(), APInt(32, 0)), BB);
if (Function *Intrinsic = M.getFunction("llvm.nacl.tp.tdb.offset")) {
Intrinsic->replaceAllUsesWith(NewFunc);
Intrinsic->eraseFromParent();
}
// Define the intrinsic as follows:
// uint32_t __nacl_tp_tls_offset(uint32_t tls_size) {
// return -tls_size;
// }
// This means the TLS variables are stored below the thread pointer.
NewFunc = Function::Create(FuncType, GlobalValue::InternalLinkage,
"nacl_tp_tls_offset", &M);
BB = BasicBlock::Create(M.getContext(), "entry", NewFunc);
Value *Arg = NewFunc->arg_begin();
Arg->setName("size");
Value *Result = BinaryOperator::CreateNeg(Arg, "result", BB);
ReturnInst::Create(M.getContext(), Result, BB);
if (Function *Intrinsic = M.getFunction("llvm.nacl.tp.tls.offset")) {
Intrinsic->replaceAllUsesWith(NewFunc);
Intrinsic->eraseFromParent();
}
}
bool ExpandTls::runOnModule(Module &M) {
ModulePass *Pass = createExpandTlsConstantExprPass();
Pass->runOnModule(M);
delete Pass;
std::vector<VarInfo> TlsVars;
PointerType *TemplatePtrType = buildTlsTemplate(M, &TlsVars);
rewriteTlsVars(M, &TlsVars, TemplatePtrType);
defineTlsLayoutIntrinsics(M);
return true;
}
ModulePass *llvm::createExpandTlsPass() {
return new ExpandTls();
}
|