OLD | NEW |
1 //===- ExpandCtors.cpp - Convert ctors/dtors to concrete arrays -----------===// | 1 //===- ExpandCtors.cpp - Convert ctors/dtors to concrete arrays -----------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This pass converts LLVM's special symbols llvm.global_ctors and | 10 // This pass converts LLVM's special symbols llvm.global_ctors and |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 | 66 |
67 struct FuncArrayEntry { | 67 struct FuncArrayEntry { |
68 uint64_t priority; | 68 uint64_t priority; |
69 Constant *func; | 69 Constant *func; |
70 }; | 70 }; |
71 | 71 |
72 static bool compareEntries(FuncArrayEntry Entry1, FuncArrayEntry Entry2) { | 72 static bool compareEntries(FuncArrayEntry Entry1, FuncArrayEntry Entry2) { |
73 return Entry1.priority < Entry2.priority; | 73 return Entry1.priority < Entry2.priority; |
74 } | 74 } |
75 | 75 |
| 76 static void readFuncList(GlobalVariable *Array, std::vector<Constant*> *Funcs) { |
| 77 if (!Array->hasInitializer()) |
| 78 return; |
| 79 Constant *Init = Array->getInitializer(); |
| 80 ArrayType *Ty = dyn_cast<ArrayType>(Init->getType()); |
| 81 if (!Ty) { |
| 82 errs() << "Initializer: " << *Array->getInitializer() << "\n"; |
| 83 report_fatal_error("ExpandCtors: Initializer is not of array type"); |
| 84 } |
| 85 if (Ty->getNumElements() == 0) |
| 86 return; |
| 87 ConstantArray *InitList = dyn_cast<ConstantArray>(Init); |
| 88 if (!InitList) { |
| 89 errs() << "Initializer: " << *Array->getInitializer() << "\n"; |
| 90 report_fatal_error("ExpandCtors: Unexpected initializer ConstantExpr"); |
| 91 } |
| 92 std::vector<FuncArrayEntry> FuncsToSort; |
| 93 for (unsigned Index = 0; Index < InitList->getNumOperands(); ++Index) { |
| 94 ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(Index)); |
| 95 FuncArrayEntry Entry; |
| 96 Entry.priority = cast<ConstantInt>(CS->getOperand(0))->getZExtValue(); |
| 97 Entry.func = CS->getOperand(1); |
| 98 FuncsToSort.push_back(Entry); |
| 99 } |
| 100 |
| 101 std::sort(FuncsToSort.begin(), FuncsToSort.end(), compareEntries); |
| 102 for (std::vector<FuncArrayEntry>::iterator Iter = FuncsToSort.begin(); |
| 103 Iter != FuncsToSort.end(); |
| 104 ++Iter) { |
| 105 Funcs->push_back(Iter->func); |
| 106 } |
| 107 } |
| 108 |
76 static void defineFuncArray(Module &M, const char *LlvmArrayName, | 109 static void defineFuncArray(Module &M, const char *LlvmArrayName, |
77 const char *StartSymbol, | 110 const char *StartSymbol, |
78 const char *EndSymbol) { | 111 const char *EndSymbol) { |
79 std::vector<Constant*> Funcs; | 112 std::vector<Constant*> Funcs; |
80 | 113 |
81 GlobalVariable *Array = M.getNamedGlobal(LlvmArrayName); | 114 GlobalVariable *Array = M.getNamedGlobal(LlvmArrayName); |
82 if (Array) { | 115 if (Array) { |
83 if (Array->hasInitializer() && !Array->getInitializer()->isNullValue()) { | 116 readFuncList(Array, &Funcs); |
84 ConstantArray *InitList = cast<ConstantArray>(Array->getInitializer()); | |
85 std::vector<FuncArrayEntry> FuncsToSort; | |
86 for (unsigned Index = 0; Index < InitList->getNumOperands(); ++Index) { | |
87 ConstantStruct *CS = cast<ConstantStruct>(InitList->getOperand(Index)); | |
88 FuncArrayEntry Entry; | |
89 Entry.priority = cast<ConstantInt>(CS->getOperand(0))->getZExtValue(); | |
90 Entry.func = CS->getOperand(1); | |
91 FuncsToSort.push_back(Entry); | |
92 } | |
93 | |
94 std::sort(FuncsToSort.begin(), FuncsToSort.end(), compareEntries); | |
95 for (std::vector<FuncArrayEntry>::iterator Iter = FuncsToSort.begin(); | |
96 Iter != FuncsToSort.end(); | |
97 ++Iter) { | |
98 Funcs.push_back(Iter->func); | |
99 } | |
100 } | |
101 // No code should be referencing global_ctors/global_dtors, | 117 // No code should be referencing global_ctors/global_dtors, |
102 // because this symbol is internal to LLVM. | 118 // because this symbol is internal to LLVM. |
103 Array->eraseFromParent(); | 119 Array->eraseFromParent(); |
104 } | 120 } |
105 | 121 |
106 Type *FuncTy = FunctionType::get(Type::getVoidTy(M.getContext()), false); | 122 Type *FuncTy = FunctionType::get(Type::getVoidTy(M.getContext()), false); |
107 Type *FuncPtrTy = FuncTy->getPointerTo(); | 123 Type *FuncPtrTy = FuncTy->getPointerTo(); |
108 ArrayType *ArrayTy = ArrayType::get(FuncPtrTy, Funcs.size()); | 124 ArrayType *ArrayTy = ArrayType::get(FuncPtrTy, Funcs.size()); |
109 GlobalVariable *NewArray = | 125 GlobalVariable *NewArray = |
110 new GlobalVariable(M, ArrayTy, /* isConstant= */ true, | 126 new GlobalVariable(M, ArrayTy, /* isConstant= */ true, |
(...skipping 25 matching lines...) Expand all Loading... |
136 defineFuncArray(M, "llvm.global_ctors", | 152 defineFuncArray(M, "llvm.global_ctors", |
137 "__init_array_start", "__init_array_end"); | 153 "__init_array_start", "__init_array_end"); |
138 defineFuncArray(M, "llvm.global_dtors", | 154 defineFuncArray(M, "llvm.global_dtors", |
139 "__fini_array_start", "__fini_array_end"); | 155 "__fini_array_start", "__fini_array_end"); |
140 return true; | 156 return true; |
141 } | 157 } |
142 | 158 |
143 ModulePass *llvm::createExpandCtorsPass() { | 159 ModulePass *llvm::createExpandCtorsPass() { |
144 return new ExpandCtors(); | 160 return new ExpandCtors(); |
145 } | 161 } |
OLD | NEW |