blob: 6a0458c512865eac4669d44e12e7fd74805c45de [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
Devang Patelf17fc462008-11-18 21:34:39 +000043 virtual bool runOnModule(Module &M);
Devang Patel229de952008-11-14 22:49:37 +000044
Devang Patelf17fc462008-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 Patel229de952008-11-14 22:49:37 +000055
Chris Lattnere3ad43c2004-12-02 21:25:03 +000056 virtual bool runOnModule(Module &M);
57
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesAll();
60 }
61 };
Chris Lattnere3ad43c2004-12-02 21:25:03 +000062}
63
Dan Gohman844731a2008-05-13 00:00:25 +000064char StripSymbols::ID = 0;
65static RegisterPass<StripSymbols>
66X("strip", "Strip all symbols from a module");
67
Chris Lattnere3ad43c2004-12-02 21:25:03 +000068ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
69 return new StripSymbols(OnlyDebugInfo);
70}
71
Devang Patelf17fc462008-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 Patelbf5db812008-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
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000090static void RemoveDeadConstant(Constant *C) {
91 assert(C->use_empty() && "Constant is not dead!");
Devang Patelbf5db812008-11-13 01:28:40 +000092 SmallPtrSet<Constant *, 4> Operands;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000093 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
94 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
Devang Patelbf5db812008-11-13 01:28:40 +000095 OnlyUsedBy(C->getOperand(i), C))
96 Operands.insert(C->getOperand(i));
Chris Lattnerdd0ecf62004-12-03 16:22:08 +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))
102 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +0000103
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000104 // If the constant referenced anything, see if we can delete it as well.
Devang Patelbf5db812008-11-13 01:28:40 +0000105 for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
106 OE = Operands.end(); OI != OE; ++OI)
107 RemoveDeadConstant(*OI);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000108}
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000109
Chris Lattner7f1444b2007-02-07 06:22:45 +0000110// Strip the symbol table of its names.
111//
Devang Patelf17fc462008-11-18 21:34:39 +0000112static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner7f1444b2007-02-07 06:22:45 +0000113 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +0000114 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +0000115 ++VI;
116 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
Devang Patelf17fc462008-11-18 21:34:39 +0000117 if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
118 // Set name to "", removing from symbol table!
119 V->setName("");
Chris Lattner7f1444b2007-02-07 06:22:45 +0000120 }
121 }
122}
123
124// Strip the symbol table of its names.
Devang Patelf17fc462008-11-18 21:34:39 +0000125static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
126 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
127 if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
128 ++TI;
129 else
130 ST.remove(TI++);
131 }
Chris Lattner7f1444b2007-02-07 06:22:45 +0000132}
133
Devang Patel4460a7e2008-11-18 21:13:41 +0000134/// Find values that are marked as llvm.used.
135void findUsedValues(Module &M,
136 SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
Devang Patel229de952008-11-14 22:49:37 +0000137 if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
138 llvmUsedValues.insert(LLVMUsed);
139 // Collect values that are preserved as per explicit request.
140 // llvm.used is used to list these values.
141 if (ConstantArray *Inits =
142 dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
143 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
144 if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
145 llvmUsedValues.insert(GV);
146 else if (ConstantExpr *CE =
147 dyn_cast<ConstantExpr>(Inits->getOperand(i)))
148 if (CE->getOpcode() == Instruction::BitCast)
149 if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
150 llvmUsedValues.insert(GV);
Devang Patel8c231e52008-01-16 03:33:05 +0000151 }
152 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000153 }
Devang Patel4460a7e2008-11-18 21:13:41 +0000154}
155
156/// StripSymbolNames - Strip symbol names.
Devang Patelf17fc462008-11-18 21:34:39 +0000157bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel4460a7e2008-11-18 21:13:41 +0000158
159 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
160 findUsedValues(M, llvmUsedValues);
161
Devang Patel229de952008-11-14 22:49:37 +0000162 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
163 I != E; ++I) {
164 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000165 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
166 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel229de952008-11-14 22:49:37 +0000167 }
168
169 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
170 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Devang Patelf17fc462008-11-18 21:34:39 +0000171 if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
172 I->setName(""); // Internal symbols can't participate in linkage
173 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel229de952008-11-14 22:49:37 +0000174 }
175
176 // Remove all names from types.
Devang Patelf17fc462008-11-18 21:34:39 +0000177 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000178
Devang Patel229de952008-11-14 22:49:37 +0000179 return true;
180}
181
182// StripDebugInfo - Strip debug info in the module if it exists.
183// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
184// llvm.dbg.region.end calls, and any globals they point to if now dead.
Devang Patelf17fc462008-11-18 21:34:39 +0000185bool StripDebugInfo(Module &M) {
Devang Patel229de952008-11-14 22:49:37 +0000186
Reid Spencer688b0492007-02-05 21:19:13 +0000187 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
188 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
189 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
190 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
191 Function *Declare = M.getFunction("llvm.dbg.declare");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000192
Devang Patel4460a7e2008-11-18 21:13:41 +0000193 std::vector<Constant*> DeadConstants;
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000194
195 // Remove all of the calls to the debugger intrinsics, and remove them from
196 // the module.
197 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000198 while (!FuncStart->use_empty()) {
199 CallInst *CI = cast<CallInst>(FuncStart->use_back());
200 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000201 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000202 CI->eraseFromParent();
203 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000204 if (Constant *C = dyn_cast<Constant>(Arg))
205 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000206 }
207 FuncStart->eraseFromParent();
208 }
209 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000210 while (!StopPoint->use_empty()) {
211 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000212 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000213 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000214 CI->eraseFromParent();
215 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000216 if (Constant *C = dyn_cast<Constant>(Arg))
217 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000218 }
219 StopPoint->eraseFromParent();
220 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000221 if (RegionStart) {
222 while (!RegionStart->use_empty()) {
223 CallInst *CI = cast<CallInst>(RegionStart->use_back());
224 Value *Arg = CI->getOperand(1);
225 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
226 CI->eraseFromParent();
227 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000228 if (Constant *C = dyn_cast<Constant>(Arg))
229 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000230 }
231 RegionStart->eraseFromParent();
232 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000233 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000234 while (!RegionEnd->use_empty()) {
235 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000236 Value *Arg = CI->getOperand(1);
237 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000238 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000239 if (Arg->use_empty())
Devang Patel4460a7e2008-11-18 21:13:41 +0000240 if (Constant *C = dyn_cast<Constant>(Arg))
241 DeadConstants.push_back(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000242 }
243 RegionEnd->eraseFromParent();
244 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000245 if (Declare) {
246 while (!Declare->use_empty()) {
247 CallInst *CI = cast<CallInst>(Declare->use_back());
248 Value *Arg = CI->getOperand(2);
249 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
250 CI->eraseFromParent();
251 if (Arg->use_empty())
Devang Patelb876cc12008-11-19 00:19:18 +0000252 if (Constant *C = dyn_cast<Constant>(Arg))
Devang Patel4460a7e2008-11-18 21:13:41 +0000253 DeadConstants.push_back(C);
Jim Laskey4ca97572006-03-23 18:11:33 +0000254 }
255 Declare->eraseFromParent();
256 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000257
Devang Patel4460a7e2008-11-18 21:13:41 +0000258 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
259 findUsedValues(M, llvmUsedValues);
260
Devang Patelbf5db812008-11-13 01:28:40 +0000261 // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
262 // but since we are removing all debug information, make them internal now.
263 if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
264 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
265 GV->setLinkage(GlobalValue::InternalLinkage);
266
267 if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
268 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
269 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patel4460a7e2008-11-18 21:13:41 +0000270
271 if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
272 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
273 GV->setLinkage(GlobalValue::InternalLinkage);
Devang Patelbf5db812008-11-13 01:28:40 +0000274
275 // Delete all dbg variables.
Devang Patelfb5fd5a2008-11-19 00:22:02 +0000276 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
277 I != E; ++I) {
278 GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
279 if (!GV) continue;
280 if (GV->use_empty() && llvmUsedValues.count(I) == 0
281 && (!GV->hasSection()
282 || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
283 DeadConstants.push_back(GV);
284 }
Devang Patelbf5db812008-11-13 01:28:40 +0000285
Devang Patel4460a7e2008-11-18 21:13:41 +0000286 if (DeadConstants.empty())
Devang Patel229de952008-11-14 22:49:37 +0000287 return false;
288
Devang Patelbf5db812008-11-13 01:28:40 +0000289 // Delete any internal globals that were only used by the debugger intrinsics.
Devang Patel4460a7e2008-11-18 21:13:41 +0000290 while (!DeadConstants.empty()) {
291 Constant *C = DeadConstants.back();
292 DeadConstants.pop_back();
293 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
294 if (GV->hasInternalLinkage())
295 RemoveDeadConstant(GV);
296 }
297 else
298 RemoveDeadConstant(C);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000299 }
300
Devang Patelbf5db812008-11-13 01:28:40 +0000301 // Remove all llvm.dbg types.
302 TypeSymbolTable &ST = M.getTypeSymbolTable();
Chris Lattner3f914f02008-11-16 06:35:18 +0000303 for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
304 if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
Devang Patelbf5db812008-11-13 01:28:40 +0000305 ST.remove(TI++);
306 else
307 ++TI;
308 }
309
Misha Brukmanfd939082005-04-21 23:48:37 +0000310 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000311}
Devang Patelf17fc462008-11-18 21:34:39 +0000312
313bool StripSymbols::runOnModule(Module &M) {
314 bool Changed = false;
315 Changed |= StripDebugInfo(M);
316 if (!OnlyDebugInfo)
317 Changed |= StripSymbolNames(M, false);
318 return Changed;
319}
320
321bool StripNonDebugSymbols::runOnModule(Module &M) {
322 return StripSymbolNames(M, true);
323}
324