blob: 7e225e2bd555f33e74d53ac786af5d33ae715dc7 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gordon Henriksen79ed5872007-11-04 16:15:04 +000010// The StripSymbols transformation implements code stripping. Specifically, it
11// can delete:
12//
13// * names for virtual registers
14// * symbols for internal globals and functions
15// * debug information
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016//
Gordon Henriksen79ed5872007-11-04 16:15:04 +000017// 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.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020//
21//===----------------------------------------------------------------------===//
22
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"
Devang Patel5dc8fde2008-01-16 03:33:05 +000032#include "llvm/ADT/SmallPtrSet.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35namespace {
36 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
37 bool OnlyDebugInfo;
38 public:
39 static char ID; // Pass identification, replacement for typeid
Dan Gohman34c280e2007-08-01 15:32:29 +000040 explicit StripSymbols(bool ODI = false)
Dan Gohman26f8c272008-09-04 17:05:41 +000041 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
Devang Patel159f7ce2008-11-18 21:34:39 +000043 virtual bool runOnModule(Module &M);
Devang Patel2c98b522008-11-14 22:49:37 +000044
Devang Patel159f7ce2008-11-18 21:34:39 +000045 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
47 }
48 };
49
50 class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
51 public:
52 static char ID; // Pass identification, replacement for typeid
53 explicit StripNonDebugSymbols()
54 : ModulePass(&ID) {}
Devang Patel2c98b522008-11-14 22:49:37 +000055
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 virtual bool runOnModule(Module &M);
57
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
60 }
61 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062}
63
Dan Gohman089efff2008-05-13 00:00:25 +000064char StripSymbols::ID = 0;
65static RegisterPass<StripSymbols>
66X("strip", "Strip all symbols from a module");
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
69 return new StripSymbols(OnlyDebugInfo);
70}
71
Devang Patel159f7ce2008-11-18 21:34:39 +000072char StripNonDebugSymbols::ID = 0;
73static RegisterPass<StripNonDebugSymbols>
74Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
75
76ModulePass *llvm::createStripNonDebugSymbolsPass() {
77 return new StripNonDebugSymbols();
78}
79
Devang Patel2fa85562008-11-13 01:28:40 +000080/// OnlyUsedBy - Return true if V is only used by Usr.
81static bool OnlyUsedBy(Value *V, Value *Usr) {
82 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
83 User *U = *I;
84 if (U != Usr)
85 return false;
86 }
87 return true;
88}
89
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090static void RemoveDeadConstant(Constant *C) {
91 assert(C->use_empty() && "Constant is not dead!");
Devang Patel2fa85562008-11-13 01:28:40 +000092 SmallPtrSet<Constant *, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
94 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patel2fa85562008-11-13 01:28:40 +000095 OnlyUsedBy(C->getOperand(i), C))
96 Operands.insert(C->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
98 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
99 GV->eraseFromParent();
100 }
101 else if (!isa<Function>(C))
Devang Patelabd69112008-11-20 01:20:42 +0000102 if (isa<CompositeType>(C->getType()))
103 C->destroyConstant();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
105 // If the constant referenced anything, see if we can delete it as well.
Devang Patel2fa85562008-11-13 01:28:40 +0000106 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
107 OE = Operands.end(); OI != OE; ++OI)
108 RemoveDeadConstant(*OI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109}
110
111// Strip the symbol table of its names.
112//
Devang Patel159f7ce2008-11-18 21:34:39 +0000113static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
115 Value *V = VI->getValue();
116 ++VI;
117 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
Devang Patel159f7ce2008-11-18 21:34:39 +0000118 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
119 // Set name to "", removing from symbol table!
120 V->setName("");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 }
122 }
123}
124
125// Strip the symbol table of its names.
Devang Patel159f7ce2008-11-18 21:34:39 +0000126static 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)
129 ++TI;
130 else
131 ST.remove(TI++);
132 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133}
134
Devang Patel2d53e2d2008-11-18 21:13:41 +0000135/// Find values that are marked as llvm.used.
136void findUsedValues(Module &M,
137 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel2c98b522008-11-14 22:49:37 +0000138 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);
Devang Patel5dc8fde2008-01-16 03:33:05 +0000152 }
153 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 }
Devang Patel2d53e2d2008-11-18 21:13:41 +0000155}
156
157/// StripSymbolNames - Strip symbol names.
Devang Patel159f7ce2008-11-18 21:34:39 +0000158bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel2d53e2d2008-11-18 21:13:41 +0000159
160 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
161 findUsedValues(M, llvmUsedValues);
162
Devang Patel2c98b522008-11-14 22:49:37 +0000163 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
164 I != E; ++I) {
165 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patel159f7ce2008-11-18 21:34:39 +0000166 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
167 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel2c98b522008-11-14 22:49:37 +0000168 }
169
170 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
171 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patel159f7ce2008-11-18 21:34:39 +0000172 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
173 I->setName(""); // Internal symbols can't participate in linkage
174 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel2c98b522008-11-14 22:49:37 +0000175 }
176
177 // Remove all names from types.
Devang Patel159f7ce2008-11-18 21:34:39 +0000178 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
Devang Patel2c98b522008-11-14 22:49:37 +0000180 return true;
181}
182
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.
Devang Patel159f7ce2008-11-18 21:34:39 +0000186bool StripDebugInfo(Module &M) {
Devang Patel2c98b522008-11-14 22:49:37 +0000187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
189 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
190 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
191 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
192 Function *Declare = M.getFunction("llvm.dbg.declare");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
Devang Patel2d53e2d2008-11-18 21:13:41 +0000194 std::vector<Constant*> DeadConstants;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195
196 // Remove all of the calls to the debugger intrinsics, and remove them from
197 // the module.
198 if (FuncStart) {
199 while (!FuncStart->use_empty()) {
200 CallInst *CI = cast<CallInst>(FuncStart->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())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000205 if (Constant *C = dyn_cast<Constant>(Arg))
206 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 }
208 FuncStart->eraseFromParent();
209 }
210 if (StopPoint) {
211 while (!StopPoint->use_empty()) {
212 CallInst *CI = cast<CallInst>(StopPoint->use_back());
213 Value *Arg = CI->getOperand(3);
214 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
215 CI->eraseFromParent();
216 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000217 if (Constant *C = dyn_cast<Constant>(Arg))
218 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 }
220 StopPoint->eraseFromParent();
221 }
222 if (RegionStart) {
223 while (!RegionStart->use_empty()) {
224 CallInst *CI = cast<CallInst>(RegionStart->use_back());
225 Value *Arg = CI->getOperand(1);
226 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
227 CI->eraseFromParent();
228 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000229 if (Constant *C = dyn_cast<Constant>(Arg))
230 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 }
232 RegionStart->eraseFromParent();
233 }
234 if (RegionEnd) {
235 while (!RegionEnd->use_empty()) {
236 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
237 Value *Arg = CI->getOperand(1);
238 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
239 CI->eraseFromParent();
240 if (Arg->use_empty())
Devang Patel2d53e2d2008-11-18 21:13:41 +0000241 if (Constant *C = dyn_cast<Constant>(Arg))
242 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 }
244 RegionEnd->eraseFromParent();
245 }
246 if (Declare) {
247 while (!Declare->use_empty()) {
248 CallInst *CI = cast<CallInst>(Declare->use_back());
Devang Patelabd69112008-11-20 01:20:42 +0000249 Value *Arg1 = CI->getOperand(1);
250 Value *Arg2 = CI->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
252 CI->eraseFromParent();
Devang Patelabd69112008-11-20 01:20:42 +0000253 if (Arg1->use_empty()) {
254 if (Constant *C = dyn_cast<Constant>(Arg1))
255 DeadConstants.push_back(C);
256 if (Instruction *I = dyn_cast<Instruction>(Arg1))
257 I->eraseFromParent();
258 }
259 if (Arg2->use_empty())
260 if (Constant *C = dyn_cast<Constant>(Arg2))
Devang Patel2d53e2d2008-11-18 21:13:41 +0000261 DeadConstants.push_back(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 }
263 Declare->eraseFromParent();
264 }
265
Devang Patel2d53e2d2008-11-18 21:13:41 +0000266 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
267 findUsedValues(M, llvmUsedValues);
268
Devang Patel2fa85562008-11-13 01:28:40 +0000269 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
270 // but since we are removing all debug information, make them internal now.
271 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
272 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
273 GV->setLinkage(GlobalValue::InternalLinkage);
274
275 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
276 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
277 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel2d53e2d2008-11-18 21:13:41 +0000278
279 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
280 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
281 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel2fa85562008-11-13 01:28:40 +0000282
283 // Delete all dbg variables.
Devang Patel43f586b2008-11-19 00:22:02 +0000284 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
285 I != E; ++I) {
286 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
287 if (!GV) continue;
288 if (GV->use_empty() && llvmUsedValues.count(I) == 0
289 && (!GV->hasSection()
290 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
291 DeadConstants.push_back(GV);
292 }
Devang Patel2fa85562008-11-13 01:28:40 +0000293
Devang Patel2d53e2d2008-11-18 21:13:41 +0000294 if (DeadConstants.empty())
Devang Patel2c98b522008-11-14 22:49:37 +0000295 return false;
296
Devang Patel2fa85562008-11-13 01:28:40 +0000297 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel2d53e2d2008-11-18 21:13:41 +0000298 while (!DeadConstants.empty()) {
299 Constant *C = DeadConstants.back();
300 DeadConstants.pop_back();
301 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
302 if (GV->hasInternalLinkage())
303 RemoveDeadConstant(GV);
304 }
305 else
306 RemoveDeadConstant(C);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307 }
308
Devang Patel2fa85562008-11-13 01:28:40 +0000309 // Remove all llvm.dbg types.
310 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattnerba58d9c2008-11-16 06:35:18 +0000311 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
312 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patel2fa85562008-11-13 01:28:40 +0000313 ST.remove(TI++);
314 else
315 ++TI;
316 }
317
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 return true;
319}
Devang Patel159f7ce2008-11-18 21:34:39 +0000320
321bool StripSymbols::runOnModule(Module &M) {
322 bool Changed = false;
323 Changed |= StripDebugInfo(M);
324 if (!OnlyDebugInfo)
325 Changed |= StripSymbolNames(M, false);
326 return Changed;
327}
328
329bool StripNonDebugSymbols::runOnModule(Module &M) {
330 return StripSymbolNames(M, true);
331}
332