blob: 58b7b7146703faa31ab27eadca82e56fc3fee59a [file] [log] [blame]
Chris Lattnere8ebcb32004-12-02 21:25:03 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnere8ebcb32004-12-02 21:25:03 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnere8ebcb32004-12-02 21:25:03 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements stripping symbols out of symbol tables.
11//
12// Specifically, this allows you to strip all of the symbols out of:
13// * All functions in a module
14// * All non-essential symbols in a module (all function symbols + all module
15// scope symbols)
16// * Debug information.
17//
18// Notice that:
19// * This pass makes code much less readable, so it should only be used in
Misha Brukmanb1c93172005-04-21 23:48:37 +000020// situations where the 'strip' utility would be used (such as reducing
Chris Lattnere8ebcb32004-12-02 21:25:03 +000021// code size, and making it harder to reverse engineer code).
22//
23//===----------------------------------------------------------------------===//
24
25#include "llvm/Transforms/IPO.h"
Chris Lattner9019e5c2004-12-03 16:22:08 +000026#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Instructions.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000029#include "llvm/Module.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000030#include "llvm/Pass.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000031#include "llvm/ValueSymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000032#include "llvm/TypeSymbolTable.h"
Reid Spencer557ab152007-02-05 23:32:05 +000033#include "llvm/Support/Compiler.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000034using namespace llvm;
35
36namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000037 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000038 bool OnlyDebugInfo;
39 public:
40 StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {}
41
42 virtual bool runOnModule(Module &M);
43
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 }
47 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000048 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
Chris Lattnere8ebcb32004-12-02 21:25:03 +000049}
50
51ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
52 return new StripSymbols(OnlyDebugInfo);
53}
54
Chris Lattner9019e5c2004-12-03 16:22:08 +000055static void RemoveDeadConstant(Constant *C) {
56 assert(C->use_empty() && "Constant is not dead!");
57 std::vector<Constant*> Operands;
58 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
59 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
60 C->getOperand(i)->hasOneUse())
61 Operands.push_back(C->getOperand(i));
62 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
63 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
64 GV->eraseFromParent();
65 }
66 else if (!isa<Function>(C))
67 C->destroyConstant();
Misha Brukmanb1c93172005-04-21 23:48:37 +000068
Chris Lattner9019e5c2004-12-03 16:22:08 +000069 // If the constant referenced anything, see if we can delete it as well.
70 while (!Operands.empty()) {
71 RemoveDeadConstant(Operands.back());
72 Operands.pop_back();
73 }
74}
Chris Lattnere8ebcb32004-12-02 21:25:03 +000075
Chris Lattner88051b02007-02-07 06:22:45 +000076// Strip the symbol table of its names.
77//
78static void StripSymtab(ValueSymbolTable &ST) {
79 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
80 Value *V = VI->second;
81 ++VI;
82 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
83 // Set name to "", removing from symbol table!
84 V->setName("");
85 }
86 }
87}
88
89// Strip the symbol table of its names.
90static void StripTypeSymtab(TypeSymbolTable &ST) {
91 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
92 ST.remove(TI++);
93}
94
95
96
Chris Lattnere8ebcb32004-12-02 21:25:03 +000097bool StripSymbols::runOnModule(Module &M) {
98 // If we're not just stripping debug info, strip all symbols from the
99 // functions and the names from any internal globals.
100 if (!OnlyDebugInfo) {
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000101 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
102 I != E; ++I)
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000103 if (I->hasInternalLinkage())
104 I->setName(""); // Internal symbols can't participate in linkage
105
106 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
107 if (I->hasInternalLinkage())
108 I->setName(""); // Internal symbols can't participate in linkage
Chris Lattner88051b02007-02-07 06:22:45 +0000109 StripSymtab(I->getValueSymbolTable());
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000110 }
Chris Lattner6d6084f2006-03-15 19:22:41 +0000111
112 // Remove all names from types.
Chris Lattner88051b02007-02-07 06:22:45 +0000113 StripTypeSymtab(M.getTypeSymbolTable());
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000114 }
115
Chris Lattner9019e5c2004-12-03 16:22:08 +0000116 // Strip debug info in the module if it exists. To do this, we remove
117 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
118 // any globals they point to if now dead.
Reid Spencer1241d6d2007-02-05 21:19:13 +0000119 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
120 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
121 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
122 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
123 Function *Declare = M.getFunction("llvm.dbg.declare");
Jim Laskey8f644262006-03-23 18:11:33 +0000124 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
Chris Lattner9019e5c2004-12-03 16:22:08 +0000125 return true;
126
127 std::vector<GlobalVariable*> DeadGlobals;
128
129 // Remove all of the calls to the debugger intrinsics, and remove them from
130 // the module.
131 if (FuncStart) {
Chris Lattner9019e5c2004-12-03 16:22:08 +0000132 while (!FuncStart->use_empty()) {
133 CallInst *CI = cast<CallInst>(FuncStart->use_back());
134 Value *Arg = CI->getOperand(1);
Jim Laskey8f644262006-03-23 18:11:33 +0000135 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattner9019e5c2004-12-03 16:22:08 +0000136 CI->eraseFromParent();
137 if (Arg->use_empty())
138 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
139 DeadGlobals.push_back(GV);
140 }
141 FuncStart->eraseFromParent();
142 }
143 if (StopPoint) {
Chris Lattner9019e5c2004-12-03 16:22:08 +0000144 while (!StopPoint->use_empty()) {
145 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyacb6e342006-03-13 13:07:37 +0000146 Value *Arg = CI->getOperand(3);
Jim Laskey8f644262006-03-23 18:11:33 +0000147 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattner9019e5c2004-12-03 16:22:08 +0000148 CI->eraseFromParent();
149 if (Arg->use_empty())
150 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
151 DeadGlobals.push_back(GV);
152 }
153 StopPoint->eraseFromParent();
154 }
Jim Laskey8f644262006-03-23 18:11:33 +0000155 if (RegionStart) {
156 while (!RegionStart->use_empty()) {
157 CallInst *CI = cast<CallInst>(RegionStart->use_back());
158 Value *Arg = CI->getOperand(1);
159 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
160 CI->eraseFromParent();
161 if (Arg->use_empty())
162 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
163 DeadGlobals.push_back(GV);
164 }
165 RegionStart->eraseFromParent();
166 }
Chris Lattner9019e5c2004-12-03 16:22:08 +0000167 if (RegionEnd) {
Chris Lattner9019e5c2004-12-03 16:22:08 +0000168 while (!RegionEnd->use_empty()) {
169 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey8f644262006-03-23 18:11:33 +0000170 Value *Arg = CI->getOperand(1);
171 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattner9019e5c2004-12-03 16:22:08 +0000172 CI->eraseFromParent();
Jim Laskey8f644262006-03-23 18:11:33 +0000173 if (Arg->use_empty())
174 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
175 DeadGlobals.push_back(GV);
Chris Lattner9019e5c2004-12-03 16:22:08 +0000176 }
177 RegionEnd->eraseFromParent();
178 }
Jim Laskey8f644262006-03-23 18:11:33 +0000179 if (Declare) {
180 while (!Declare->use_empty()) {
181 CallInst *CI = cast<CallInst>(Declare->use_back());
182 Value *Arg = CI->getOperand(2);
183 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
184 CI->eraseFromParent();
185 if (Arg->use_empty())
186 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
187 DeadGlobals.push_back(GV);
188 }
189 Declare->eraseFromParent();
190 }
Chris Lattner9019e5c2004-12-03 16:22:08 +0000191
192 // Finally, delete any internal globals that were only used by the debugger
193 // intrinsics.
194 while (!DeadGlobals.empty()) {
195 GlobalVariable *GV = DeadGlobals.back();
196 DeadGlobals.pop_back();
197 if (GV->hasInternalLinkage())
198 RemoveDeadConstant(GV);
199 }
200
Misha Brukmanb1c93172005-04-21 23:48:37 +0000201 return true;
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000202}