blob: 9b26dc5dd3a207cf6be8d5434de8636d9b1d631b [file] [log] [blame]
Chris Lattnere3ad43c2004-12-02 21:25:03 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnere3ad43c2004-12-02 21:25:03 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattnere3ad43c2004-12-02 21:25:03 +00008//===----------------------------------------------------------------------===//
9//
Gordon Henriksenc86b6772007-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
Chris Lattnere3ad43c2004-12-02 21:25:03 +000016//
Gordon Henriksenc86b6772007-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.
Chris Lattnere3ad43c2004-12-02 21:25:03 +000020//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/Transforms/IPO.h"
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Instructions.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000027#include "llvm/Module.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000028#include "llvm/Pass.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000031#include "llvm/Support/Compiler.h"
Devang Patel8c231e52008-01-16 03:33:05 +000032#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000033using namespace llvm;
34
35namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000036 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000037 bool OnlyDebugInfo;
38 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000040 explicit StripSymbols(bool ODI = false)
Dan Gohmanae73dc12008-09-04 17:05:41 +000041 : ModulePass(&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000042
43 virtual bool runOnModule(Module &M);
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
47 }
48 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000049}
50
Dan Gohman844731a2008-05-13 00:00:25 +000051char StripSymbols::ID = 0;
52static RegisterPass<StripSymbols>
53X("strip", "Strip all symbols from a module");
54
Chris Lattnere3ad43c2004-12-02 21:25:03 +000055ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
56 return new StripSymbols(OnlyDebugInfo);
57}
58
Devang Patelbf5db812008-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
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000069static void RemoveDeadConstant(Constant *C) {
70 assert(C->use_empty() && "Constant is not dead!");
Devang Patelbf5db812008-11-13 01:28:40 +000071 SmallPtrSet<Constant *, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000072 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
73 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +000074 OnlyUsedBy(C->getOperand(i), C))
75 Operands.insert(C->getOperand(i));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +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();
Misha Brukmanfd939082005-04-21 23:48:37 +000082
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000083 // If the constant referenced anything, see if we can delete it as well.
Devang Patelbf5db812008-11-13 01:28:40 +000084 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
85 OE = Operands.end(); OI != OE; ++OI)
86 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000087}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000088
Chris Lattner7f1444b2007-02-07 06:22:45 +000089// 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; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +000093 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +000094 ++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
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000110bool 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 Lattner1e9aa712008-01-16 21:35:43 +0000114 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
115 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
116 llvmUsedValues.insert(LLVMUsed);
Devang Patel8c231e52008-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 Lattner1e9aa712008-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 Patel8c231e52008-01-16 03:33:05 +0000129 }
130 }
131 }
132
Chris Lattner7f8897f2006-08-27 22:42:52 +0000133 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Devang Patel8c231e52008-01-16 03:33:05 +0000134 I != E; ++I) {
135 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000136 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel8c231e52008-01-16 03:33:05 +0000137 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000138
139 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Devang Patel8c231e52008-01-16 03:33:05 +0000140 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000141 I->setName(""); // Internal symbols can't participate in linkage
Chris Lattner7f1444b2007-02-07 06:22:45 +0000142 StripSymtab(I->getValueSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000143 }
Chris Lattnerb2f6c002006-03-15 19:22:41 +0000144
145 // Remove all names from types.
Chris Lattner7f1444b2007-02-07 06:22:45 +0000146 StripTypeSymtab(M.getTypeSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000147 }
148
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000149 // 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.
Reid Spencer688b0492007-02-05 21:19:13 +0000152 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");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +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) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000163 while (!FuncStart->use_empty()) {
164 CallInst *CI = cast<CallInst>(FuncStart->use_back());
165 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000166 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000167 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) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000175 while (!StopPoint->use_empty()) {
176 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000177 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000178 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000179 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 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000186 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 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000198 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000199 while (!RegionEnd->use_empty()) {
200 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000201 Value *Arg = CI->getOperand(1);
202 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000203 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000204 if (Arg->use_empty())
205 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
206 DeadGlobals.push_back(GV);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000207 }
208 RegionEnd->eraseFromParent();
209 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000210 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 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000222
Devang Patelbf5db812008-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.
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000247 while (!DeadGlobals.empty()) {
248 GlobalVariable *GV = DeadGlobals.back();
249 DeadGlobals.pop_back();
250 if (GV->hasInternalLinkage())
251 RemoveDeadConstant(GV);
252 }
253
Devang Patelbf5db812008-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
Misha Brukmanfd939082005-04-21 23:48:37 +0000266 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000267}