1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements stripping symbols out of symbol tables.
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
16 // * Debug information.
19 // * This pass makes code much less readable, so it should only be used in
20 // situations where the 'strip' utility would be used (such as reducing
21 // code size, and making it harder to reverse engineer code).
23 //===----------------------------------------------------------------------===//
25 #include "llvm/Transforms/IPO.h"
26 #include "llvm/Constants.h"
27 #include "llvm/DerivedTypes.h"
28 #include "llvm/Instructions.h"
29 #include "llvm/Module.h"
30 #include "llvm/Pass.h"
31 #include "llvm/ValueSymbolTable.h"
32 #include "llvm/TypeSymbolTable.h"
33 #include "llvm/Support/Compiler.h"
37 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
40 StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {}
42 virtual bool runOnModule(Module &M);
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
51 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
52 return new StripSymbols(OnlyDebugInfo);
55 static 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();
66 else if (!isa<Function>(C))
69 // If the constant referenced anything, see if we can delete it as well.
70 while (!Operands.empty()) {
71 RemoveDeadConstant(Operands.back());
76 // Strip the symbol table of its names.
78 static void StripSymtab(ValueSymbolTable &ST) {
79 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
80 Value *V = VI->second;
82 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
83 // Set name to "", removing from symbol table!
89 // Strip the symbol table of its names.
90 static void StripTypeSymtab(TypeSymbolTable &ST) {
91 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
97 bool 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) {
101 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
103 if (I->hasInternalLinkage())
104 I->setName(""); // Internal symbols can't participate in linkage
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
109 StripSymtab(I->getValueSymbolTable());
112 // Remove all names from types.
113 StripTypeSymtab(M.getTypeSymbolTable());
116 // 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.
119 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");
124 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
127 std::vector<GlobalVariable*> DeadGlobals;
129 // Remove all of the calls to the debugger intrinsics, and remove them from
132 while (!FuncStart->use_empty()) {
133 CallInst *CI = cast<CallInst>(FuncStart->use_back());
134 Value *Arg = CI->getOperand(1);
135 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
136 CI->eraseFromParent();
137 if (Arg->use_empty())
138 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
139 DeadGlobals.push_back(GV);
141 FuncStart->eraseFromParent();
144 while (!StopPoint->use_empty()) {
145 CallInst *CI = cast<CallInst>(StopPoint->use_back());
146 Value *Arg = CI->getOperand(3);
147 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
148 CI->eraseFromParent();
149 if (Arg->use_empty())
150 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
151 DeadGlobals.push_back(GV);
153 StopPoint->eraseFromParent();
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);
165 RegionStart->eraseFromParent();
168 while (!RegionEnd->use_empty()) {
169 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
170 Value *Arg = CI->getOperand(1);
171 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
172 CI->eraseFromParent();
173 if (Arg->use_empty())
174 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
175 DeadGlobals.push_back(GV);
177 RegionEnd->eraseFromParent();
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);
189 Declare->eraseFromParent();
192 // Finally, delete any internal globals that were only used by the debugger
194 while (!DeadGlobals.empty()) {
195 GlobalVariable *GV = DeadGlobals.back();
196 DeadGlobals.pop_back();
197 if (GV->hasInternalLinkage())
198 RemoveDeadConstant(GV);