blob: 9b26dc5dd3a207cf6be8d5434de8636d9b1d631b [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
43 virtual bool runOnModule(Module &M);
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
47 }
48 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049}
50
Dan Gohman089efff2008-05-13 00:00:25 +000051char StripSymbols::ID = 0;
52static RegisterPass<StripSymbols>
53X("strip", "Strip all symbols from a module");
54
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
56 return new StripSymbols(OnlyDebugInfo);
57}
58
Devang Patel2fa85562008-11-13 01:28:40 +000059/// OnlyUsedBy - Return true if V is only used by Usr.
60static bool OnlyUsedBy(Value *V, Value *Usr) {
61 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
62 User *U = *I;
63 if (U != Usr)
64 return false;
65 }
66 return true;
67}
68
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069static void RemoveDeadConstant(Constant *C) {
70 assert(C->use_empty() && "Constant is not dead!");
Devang Patel2fa85562008-11-13 01:28:40 +000071 SmallPtrSet<Constant *, 4> Operands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
73 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patel2fa85562008-11-13 01:28:40 +000074 OnlyUsedBy(C->getOperand(i), C))
75 Operands.insert(C->getOperand(i));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
77 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
78 GV->eraseFromParent();
79 }
80 else if (!isa<Function>(C))
81 C->destroyConstant();
82
83 // If the constant referenced anything, see if we can delete it as well.
Devang Patel2fa85562008-11-13 01:28:40 +000084 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
85 OE = Operands.end(); OI != OE; ++OI)
86 RemoveDeadConstant(*OI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087}
88
89// Strip the symbol table of its names.
90//
91static void StripSymtab(ValueSymbolTable &ST) {
92 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
93 Value *V = VI->getValue();
94 ++VI;
95 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
96 // Set name to "", removing from symbol table!
97 V->setName("");
98 }
99 }
100}
101
102// Strip the symbol table of its names.
103static void StripTypeSymtab(TypeSymbolTable &ST) {
104 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
105 ST.remove(TI++);
106}
107
108
109
110bool 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) {
Chris Lattner476eea92008-01-16 21:35:43 +0000114 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
115 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
116 llvmUsedValues.insert(LLVMUsed);
Devang Patel5dc8fde2008-01-16 03:33:05 +0000117 // Collect values that are preserved as per explicit request.
118 // llvm.used is used to list these values.
Chris Lattner476eea92008-01-16 21:35:43 +0000119 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);
Devang Patel5dc8fde2008-01-16 03:33:05 +0000129 }
130 }
131 }
132
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Devang Patel5dc8fde2008-01-16 03:33:05 +0000134 I != E; ++I) {
135 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel5dc8fde2008-01-16 03:33:05 +0000137 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
139 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Devang Patel5dc8fde2008-01-16 03:33:05 +0000140 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 I->setName(""); // Internal symbols can't participate in linkage
142 StripSymtab(I->getValueSymbolTable());
143 }
144
145 // Remove all names from types.
146 StripTypeSymtab(M.getTypeSymbolTable());
147 }
148
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");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157
158 std::vector<GlobalVariable*> DeadGlobals;
159
160 // Remove all of the calls to the debugger intrinsics, and remove them from
161 // the module.
162 if (FuncStart) {
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);
171 }
172 FuncStart->eraseFromParent();
173 }
174 if (StopPoint) {
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);
183 }
184 StopPoint->eraseFromParent();
185 }
186 if (RegionStart) {
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);
195 }
196 RegionStart->eraseFromParent();
197 }
198 if (RegionEnd) {
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);
207 }
208 RegionEnd->eraseFromParent();
209 }
210 if (Declare) {
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);
219 }
220 Declare->eraseFromParent();
221 }
222
Devang Patel2fa85562008-11-13 01:28:40 +0000223 // 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);
228
229 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
230 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
231 GV->setLinkage(GlobalValue::InternalLinkage);
232
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();
238 I != E; ++I)
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);
245
246 // Delete any internal globals that were only used by the debugger intrinsics.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 while (!DeadGlobals.empty()) {
248 GlobalVariable *GV = DeadGlobals.back();
249 DeadGlobals.pop_back();
250 if (GV->hasInternalLinkage())
251 RemoveDeadConstant(GV);
252 }
253
Devang Patel2fa85562008-11-13 01:28:40 +0000254 // Remove all llvm.dbg types.
255 TypeSymbolTable &ST = M.getTypeSymbolTable();
256 TypeSymbolTable::iterator TI = ST.begin();
257 TypeSymbolTable::iterator TE = ST.end();
258 while ( TI != TE ) {
259 const std::string &Name = TI->first;
260 if (!strncmp(Name.c_str(), "llvm.dbg.", 9))
261 ST.remove(TI++);
262 else
263 ++TI;
264 }
265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 return true;
267}