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/Support/Compiler.h"
32 #include "llvm/ADT/SmallPtrSet.h"
36 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
39 static char ID; // Pass identification, replacement for typeid
40 explicit StripSymbols(bool ODI = false)
41 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
43 virtual bool runOnModule(Module &M);
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 char StripSymbols::ID = 0;
52 static RegisterPass<StripSymbols>
53 X("strip", "Strip all symbols from a module");
55 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
56 return new StripSymbols(OnlyDebugInfo);
59 /// OnlyUsedBy - Return true if V is only used by Usr.
60 static bool OnlyUsedBy(Value *V, Value *Usr) {
61 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
69 static void RemoveDeadConstant(Constant *C) {
70 assert(C->use_empty() && "Constant is not dead!");
71 SmallPtrSet<Constant *, 4> Operands;
72 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
73 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
74 OnlyUsedBy(C->getOperand(i), C))
75 Operands.insert(C->getOperand(i));
76 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
77 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
78 GV->eraseFromParent();
80 else if (!isa<Function>(C))
83 // If the constant referenced anything, see if we can delete it as well.
84 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
85 OE = Operands.end(); OI != OE; ++OI)
86 RemoveDeadConstant(*OI);
89 // Strip the symbol table of its names.
91 static void StripSymtab(ValueSymbolTable &ST) {
92 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
93 Value *V = VI->getValue();
95 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
96 // Set name to "", removing from symbol table!
102 // Strip the symbol table of its names.
103 static void StripTypeSymtab(TypeSymbolTable &ST) {
104 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
110 bool StripSymbols::runOnModule(Module &M) {
111 // If we're not just stripping debug info, strip all symbols from the
112 // functions and the names from any internal globals.
113 if (!OnlyDebugInfo) {
114 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
115 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
116 llvmUsedValues.insert(LLVMUsed);
117 // Collect values that are preserved as per explicit request.
118 // llvm.used is used to list these values.
119 if (ConstantArray *Inits =
120 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
121 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
122 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
123 llvmUsedValues.insert(GV);
124 else if (ConstantExpr *CE =
125 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
126 if (CE->getOpcode() == Instruction::BitCast)
127 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
128 llvmUsedValues.insert(GV);
133 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
135 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
136 I->setName(""); // Internal symbols can't participate in linkage
139 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
140 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
141 I->setName(""); // Internal symbols can't participate in linkage
142 StripSymtab(I->getValueSymbolTable());
145 // Remove all names from types.
146 StripTypeSymtab(M.getTypeSymbolTable());
149 // Strip debug info in the module if it exists. To do this, we remove
150 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
151 // any globals they point to if now dead.
152 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
153 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
154 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
155 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
156 Function *Declare = M.getFunction("llvm.dbg.declare");
158 std::vector<GlobalVariable*> DeadGlobals;
160 // Remove all of the calls to the debugger intrinsics, and remove them from
163 while (!FuncStart->use_empty()) {
164 CallInst *CI = cast<CallInst>(FuncStart->use_back());
165 Value *Arg = CI->getOperand(1);
166 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
167 CI->eraseFromParent();
168 if (Arg->use_empty())
169 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
170 DeadGlobals.push_back(GV);
172 FuncStart->eraseFromParent();
175 while (!StopPoint->use_empty()) {
176 CallInst *CI = cast<CallInst>(StopPoint->use_back());
177 Value *Arg = CI->getOperand(3);
178 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
179 CI->eraseFromParent();
180 if (Arg->use_empty())
181 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
182 DeadGlobals.push_back(GV);
184 StopPoint->eraseFromParent();
187 while (!RegionStart->use_empty()) {
188 CallInst *CI = cast<CallInst>(RegionStart->use_back());
189 Value *Arg = CI->getOperand(1);
190 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
191 CI->eraseFromParent();
192 if (Arg->use_empty())
193 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
194 DeadGlobals.push_back(GV);
196 RegionStart->eraseFromParent();
199 while (!RegionEnd->use_empty()) {
200 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
201 Value *Arg = CI->getOperand(1);
202 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
203 CI->eraseFromParent();
204 if (Arg->use_empty())
205 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
206 DeadGlobals.push_back(GV);
208 RegionEnd->eraseFromParent();
211 while (!Declare->use_empty()) {
212 CallInst *CI = cast<CallInst>(Declare->use_back());
213 Value *Arg = CI->getOperand(2);
214 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
215 CI->eraseFromParent();
216 if (Arg->use_empty())
217 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
218 DeadGlobals.push_back(GV);
220 Declare->eraseFromParent();
223 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
224 // but since we are removing all debug information, make them internal now.
225 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
226 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
227 GV->setLinkage(GlobalValue::InternalLinkage);
229 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
230 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
231 GV->setLinkage(GlobalValue::InternalLinkage);
233 // Delete all dbg variables.
234 const Type *DbgVTy = M.getTypeByName("llvm.dbg.variable.type");
235 const Type *DbgGVTy = M.getTypeByName("llvm.dbg.global_variable.type");
236 if (DbgVTy || DbgGVTy)
237 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
239 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(I))
240 if (GV->hasName() && GV->use_empty()
241 && !strncmp(GV->getNameStart(), "llvm.dbg", 8)
242 && (GV->getType()->getElementType() == DbgVTy
243 || GV->getType()->getElementType() == DbgGVTy))
244 DeadGlobals.push_back(GV);
246 // Delete any internal globals that were only used by the debugger intrinsics.
247 while (!DeadGlobals.empty()) {
248 GlobalVariable *GV = DeadGlobals.back();
249 DeadGlobals.pop_back();
250 if (GV->hasInternalLinkage())
251 RemoveDeadConstant(GV);
254 // Remove all llvm.dbg types.
255 TypeSymbolTable &ST = M.getTypeSymbolTable();
256 TypeSymbolTable::iterator TI = ST.begin();
257 TypeSymbolTable::iterator TE = ST.end();
259 const std::string &Name = TI->first;
260 if (!strncmp(Name.c_str(), "llvm.dbg.", 9))