1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // The StripSymbols transformation implements code stripping. Specifically, it
13 // * names for virtual registers
14 // * symbols for internal globals and functions
15 // * debug information
17 // Note that this transformation makes code much less readable, so it should
18 // only be used in situations where the 'strip' utility would be used, such as
19 // reducing code size or making it harder to reverse engineer code.
21 //===----------------------------------------------------------------------===//
23 #include "llvm/Transforms/IPO.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/ValueSymbolTable.h"
30 #include "llvm/TypeSymbolTable.h"
31 #include "llvm/Transforms/Utils/Local.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/ADT/SmallPtrSet.h"
37 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
40 static char ID; // Pass identification, replacement for typeid
41 explicit StripSymbols(bool ODI = false)
42 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
44 virtual bool runOnModule(Module &M);
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
53 static char ID; // Pass identification, replacement for typeid
54 explicit StripNonDebugSymbols()
57 virtual bool runOnModule(Module &M);
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
66 static char ID; // Pass identification, replacement for typeid
67 explicit StripDebugDeclare()
70 virtual bool runOnModule(Module &M);
72 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
78 char StripSymbols::ID = 0;
79 static RegisterPass<StripSymbols>
80 X("strip", "Strip all symbols from a module");
82 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
83 return new StripSymbols(OnlyDebugInfo);
86 char StripNonDebugSymbols::ID = 0;
87 static RegisterPass<StripNonDebugSymbols>
88 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
90 ModulePass *llvm::createStripNonDebugSymbolsPass() {
91 return new StripNonDebugSymbols();
94 char StripDebugDeclare::ID = 0;
95 static RegisterPass<StripDebugDeclare>
96 Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
98 ModulePass *llvm::createStripDebugDeclarePass() {
99 return new StripDebugDeclare();
102 /// OnlyUsedBy - Return true if V is only used by Usr.
103 static bool OnlyUsedBy(Value *V, Value *Usr) {
104 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
112 static void RemoveDeadConstant(Constant *C) {
113 assert(C->use_empty() && "Constant is not dead!");
114 SmallPtrSet<Constant *, 4> Operands;
115 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
116 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
117 OnlyUsedBy(C->getOperand(i), C))
118 Operands.insert(C->getOperand(i));
119 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
120 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
121 GV->eraseFromParent();
123 else if (!isa<Function>(C))
124 if (isa<CompositeType>(C->getType()))
125 C->destroyConstant();
127 // If the constant referenced anything, see if we can delete it as well.
128 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
129 OE = Operands.end(); OI != OE; ++OI)
130 RemoveDeadConstant(*OI);
133 // Strip the symbol table of its names.
135 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
136 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
137 Value *V = VI->getValue();
139 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
140 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
141 // Set name to "", removing from symbol table!
147 // Strip the symbol table of its names.
148 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
149 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
150 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
157 /// Find values that are marked as llvm.used.
158 void findUsedValues(Module &M,
159 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
160 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
161 llvmUsedValues.insert(LLVMUsed);
162 // Collect values that are preserved as per explicit request.
163 // llvm.used is used to list these values.
164 if (ConstantArray *Inits =
165 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
166 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
167 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
168 llvmUsedValues.insert(GV);
169 else if (ConstantExpr *CE =
170 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
171 if (CE->getOpcode() == Instruction::BitCast)
172 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
173 llvmUsedValues.insert(GV);
179 /// StripSymbolNames - Strip symbol names.
180 bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
182 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
183 findUsedValues(M, llvmUsedValues);
185 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
187 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
188 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
189 I->setName(""); // Internal symbols can't participate in linkage
192 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
193 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
194 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
195 I->setName(""); // Internal symbols can't participate in linkage
196 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
199 // Remove all names from types.
200 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
205 // StripDebugInfo - Strip debug info in the module if it exists.
206 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
207 // llvm.dbg.region.end calls, and any globals they point to if now dead.
208 bool StripDebugInfo(Module &M) {
210 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
211 findUsedValues(M, llvmUsedValues);
213 // Delete all dbg variables.
214 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
216 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
218 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
219 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
220 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
225 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
226 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
227 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
228 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
229 Function *Declare = M.getFunction("llvm.dbg.declare");
231 std::vector<Constant*> DeadConstants;
233 // Remove all of the calls to the debugger intrinsics, and remove them from
236 while (!FuncStart->use_empty()) {
237 CallInst *CI = cast<CallInst>(FuncStart->use_back());
238 Value *Arg = CI->getOperand(1);
239 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
240 CI->eraseFromParent();
241 if (Arg->use_empty())
242 if (Constant *C = dyn_cast<Constant>(Arg))
243 DeadConstants.push_back(C);
245 FuncStart->eraseFromParent();
248 while (!StopPoint->use_empty()) {
249 CallInst *CI = cast<CallInst>(StopPoint->use_back());
250 Value *Arg = CI->getOperand(3);
251 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
252 CI->eraseFromParent();
253 if (Arg->use_empty())
254 if (Constant *C = dyn_cast<Constant>(Arg))
255 DeadConstants.push_back(C);
257 StopPoint->eraseFromParent();
260 while (!RegionStart->use_empty()) {
261 CallInst *CI = cast<CallInst>(RegionStart->use_back());
262 Value *Arg = CI->getOperand(1);
263 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
264 CI->eraseFromParent();
265 if (Arg->use_empty())
266 if (Constant *C = dyn_cast<Constant>(Arg))
267 DeadConstants.push_back(C);
269 RegionStart->eraseFromParent();
272 while (!RegionEnd->use_empty()) {
273 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
274 Value *Arg = CI->getOperand(1);
275 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
276 CI->eraseFromParent();
277 if (Arg->use_empty())
278 if (Constant *C = dyn_cast<Constant>(Arg))
279 DeadConstants.push_back(C);
281 RegionEnd->eraseFromParent();
284 while (!Declare->use_empty()) {
285 CallInst *CI = cast<CallInst>(Declare->use_back());
286 Value *Arg1 = CI->getOperand(1);
287 Value *Arg2 = CI->getOperand(2);
288 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
289 CI->eraseFromParent();
290 if (Arg1->use_empty()) {
291 if (Constant *C = dyn_cast<Constant>(Arg1))
292 DeadConstants.push_back(C);
294 RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
296 if (Arg2->use_empty())
297 if (Constant *C = dyn_cast<Constant>(Arg2))
298 DeadConstants.push_back(C);
300 Declare->eraseFromParent();
303 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
304 // but since we are removing all debug information, make them internal now.
305 // FIXME: Use private linkage maybe?
306 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
307 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
308 GV->setLinkage(GlobalValue::InternalLinkage);
310 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
311 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
312 GV->setLinkage(GlobalValue::InternalLinkage);
314 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
315 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
316 GV->setLinkage(GlobalValue::InternalLinkage);
318 // Delete all dbg variables.
319 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
321 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
323 if (GV->use_empty() && llvmUsedValues.count(I) == 0
324 && (!GV->hasSection()
325 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
326 DeadConstants.push_back(GV);
329 if (DeadConstants.empty())
332 // Delete any internal globals that were only used by the debugger intrinsics.
333 while (!DeadConstants.empty()) {
334 Constant *C = DeadConstants.back();
335 DeadConstants.pop_back();
336 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
337 if (GV->hasLocalLinkage())
338 RemoveDeadConstant(GV);
341 RemoveDeadConstant(C);
344 // Remove all llvm.dbg types.
345 TypeSymbolTable &ST = M.getTypeSymbolTable();
346 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
347 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
356 bool StripSymbols::runOnModule(Module &M) {
357 bool Changed = false;
358 Changed |= StripDebugInfo(M);
360 Changed |= StripSymbolNames(M, false);
364 bool StripNonDebugSymbols::runOnModule(Module &M) {
365 return StripSymbolNames(M, true);
368 bool StripDebugDeclare::runOnModule(Module &M) {
370 Function *Declare = M.getFunction("llvm.dbg.declare");
375 std::vector<Constant*> DeadConstants;
377 while (!Declare->use_empty()) {
378 CallInst *CI = cast<CallInst>(Declare->use_back());
379 Value *Arg1 = CI->getOperand(1);
380 Value *Arg2 = CI->getOperand(2);
381 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
382 CI->eraseFromParent();
383 if (Arg1->use_empty()) {
384 if (Constant *C = dyn_cast<Constant>(Arg1))
385 DeadConstants.push_back(C);
387 RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
389 if (Arg2->use_empty())
390 if (Constant *C = dyn_cast<Constant>(Arg2))
391 DeadConstants.push_back(C);
393 Declare->eraseFromParent();
395 while (!DeadConstants.empty()) {
396 Constant *C = DeadConstants.back();
397 DeadConstants.pop_back();
398 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
399 if (GV->hasLocalLinkage())
400 RemoveDeadConstant(GV);
403 RemoveDeadConstant(C);