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 {
50 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
52 static char ID; // Pass identification, replacement for typeid
53 explicit StripNonDebugSymbols()
56 virtual bool runOnModule(Module &M);
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 char StripSymbols::ID = 0;
65 static RegisterPass<StripSymbols>
66 X("strip", "Strip all symbols from a module");
68 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
69 return new StripSymbols(OnlyDebugInfo);
72 char StripNonDebugSymbols::ID = 0;
73 static RegisterPass<StripNonDebugSymbols>
74 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
76 ModulePass *llvm::createStripNonDebugSymbolsPass() {
77 return new StripNonDebugSymbols();
80 /// OnlyUsedBy - Return true if V is only used by Usr.
81 static bool OnlyUsedBy(Value *V, Value *Usr) {
82 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
90 static void RemoveDeadConstant(Constant *C) {
91 assert(C->use_empty() && "Constant is not dead!");
92 SmallPtrSet<Constant *, 4> Operands;
93 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
94 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
95 OnlyUsedBy(C->getOperand(i), C))
96 Operands.insert(C->getOperand(i));
97 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
98 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
99 GV->eraseFromParent();
101 else if (!isa<Function>(C))
102 if (isa<CompositeType>(C->getType()))
103 C->destroyConstant();
105 // If the constant referenced anything, see if we can delete it as well.
106 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
107 OE = Operands.end(); OI != OE; ++OI)
108 RemoveDeadConstant(*OI);
111 // Strip the symbol table of its names.
113 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
114 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
115 Value *V = VI->getValue();
117 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
118 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
119 // Set name to "", removing from symbol table!
125 // Strip the symbol table of its names.
126 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
127 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
128 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
135 /// Find values that are marked as llvm.used.
136 void findUsedValues(Module &M,
137 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
138 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
139 llvmUsedValues.insert(LLVMUsed);
140 // Collect values that are preserved as per explicit request.
141 // llvm.used is used to list these values.
142 if (ConstantArray *Inits =
143 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
144 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
145 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
146 llvmUsedValues.insert(GV);
147 else if (ConstantExpr *CE =
148 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
149 if (CE->getOpcode() == Instruction::BitCast)
150 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
151 llvmUsedValues.insert(GV);
157 /// StripSymbolNames - Strip symbol names.
158 bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
160 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
161 findUsedValues(M, llvmUsedValues);
163 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
165 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
166 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
167 I->setName(""); // Internal symbols can't participate in linkage
170 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
171 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
172 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
173 I->setName(""); // Internal symbols can't participate in linkage
174 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
177 // Remove all names from types.
178 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
183 // StripDebugInfo - Strip debug info in the module if it exists.
184 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
185 // llvm.dbg.region.end calls, and any globals they point to if now dead.
186 bool StripDebugInfo(Module &M) {
188 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
189 findUsedValues(M, llvmUsedValues);
191 // Delete all dbg variables.
192 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
194 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
196 if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
197 if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
198 GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
203 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
204 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
205 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
206 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
207 Function *Declare = M.getFunction("llvm.dbg.declare");
209 std::vector<Constant*> DeadConstants;
211 // Remove all of the calls to the debugger intrinsics, and remove them from
214 while (!FuncStart->use_empty()) {
215 CallInst *CI = cast<CallInst>(FuncStart->use_back());
216 Value *Arg = CI->getOperand(1);
217 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
218 CI->eraseFromParent();
219 if (Arg->use_empty())
220 if (Constant *C = dyn_cast<Constant>(Arg))
221 DeadConstants.push_back(C);
223 FuncStart->eraseFromParent();
226 while (!StopPoint->use_empty()) {
227 CallInst *CI = cast<CallInst>(StopPoint->use_back());
228 Value *Arg = CI->getOperand(3);
229 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
230 CI->eraseFromParent();
231 if (Arg->use_empty())
232 if (Constant *C = dyn_cast<Constant>(Arg))
233 DeadConstants.push_back(C);
235 StopPoint->eraseFromParent();
238 while (!RegionStart->use_empty()) {
239 CallInst *CI = cast<CallInst>(RegionStart->use_back());
240 Value *Arg = CI->getOperand(1);
241 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
242 CI->eraseFromParent();
243 if (Arg->use_empty())
244 if (Constant *C = dyn_cast<Constant>(Arg))
245 DeadConstants.push_back(C);
247 RegionStart->eraseFromParent();
250 while (!RegionEnd->use_empty()) {
251 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
252 Value *Arg = CI->getOperand(1);
253 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
254 CI->eraseFromParent();
255 if (Arg->use_empty())
256 if (Constant *C = dyn_cast<Constant>(Arg))
257 DeadConstants.push_back(C);
259 RegionEnd->eraseFromParent();
262 while (!Declare->use_empty()) {
263 CallInst *CI = cast<CallInst>(Declare->use_back());
264 Value *Arg1 = CI->getOperand(1);
265 Value *Arg2 = CI->getOperand(2);
266 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
267 CI->eraseFromParent();
268 if (Arg1->use_empty()) {
269 if (Constant *C = dyn_cast<Constant>(Arg1))
270 DeadConstants.push_back(C);
271 if (Instruction *I = dyn_cast<Instruction>(Arg1))
272 I->eraseFromParent();
274 if (Arg2->use_empty())
275 if (Constant *C = dyn_cast<Constant>(Arg2))
276 DeadConstants.push_back(C);
278 Declare->eraseFromParent();
281 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
282 // but since we are removing all debug information, make them internal now.
283 // FIXME: Use private linkage maybe?
284 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
285 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
286 GV->setLinkage(GlobalValue::InternalLinkage);
288 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
289 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
290 GV->setLinkage(GlobalValue::InternalLinkage);
292 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
293 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
294 GV->setLinkage(GlobalValue::InternalLinkage);
296 // Delete all dbg variables.
297 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
299 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
301 if (GV->use_empty() && llvmUsedValues.count(I) == 0
302 && (!GV->hasSection()
303 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
304 DeadConstants.push_back(GV);
307 if (DeadConstants.empty())
310 // Delete any internal globals that were only used by the debugger intrinsics.
311 while (!DeadConstants.empty()) {
312 Constant *C = DeadConstants.back();
313 DeadConstants.pop_back();
314 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
315 if (GV->hasLocalLinkage())
316 RemoveDeadConstant(GV);
319 RemoveDeadConstant(C);
322 // Remove all llvm.dbg types.
323 TypeSymbolTable &ST = M.getTypeSymbolTable();
324 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
325 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
334 bool StripSymbols::runOnModule(Module &M) {
335 bool Changed = false;
336 Changed |= StripDebugInfo(M);
338 Changed |= StripSymbolNames(M, false);
342 bool StripNonDebugSymbols::runOnModule(Module &M) {
343 return StripSymbolNames(M, true);