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 // The StripSymbols transformation implements code stripping. Specifically, it
13 // * names for virtual registers
14 // * symbols for internal globals and functions
15 // * debug information
17 // 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.
21 //===----------------------------------------------------------------------===//
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"
35 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
38 static char ID; // Pass identification, replacement for typeid
39 explicit StripSymbols(bool ODI = false)
40 : ModulePass((intptr_t)&ID), OnlyDebugInfo(ODI) {}
42 virtual bool runOnModule(Module &M);
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 char StripSymbols::ID = 0;
50 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
53 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
54 return new StripSymbols(OnlyDebugInfo);
57 static void RemoveDeadConstant(Constant *C) {
58 assert(C->use_empty() && "Constant is not dead!");
59 std::vector<Constant*> Operands;
60 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
61 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
62 C->getOperand(i)->hasOneUse())
63 Operands.push_back(C->getOperand(i));
64 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
65 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
66 GV->eraseFromParent();
68 else if (!isa<Function>(C))
71 // If the constant referenced anything, see if we can delete it as well.
72 while (!Operands.empty()) {
73 RemoveDeadConstant(Operands.back());
78 // Strip the symbol table of its names.
80 static void StripSymtab(ValueSymbolTable &ST) {
81 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
82 Value *V = VI->getValue();
84 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
85 // Set name to "", removing from symbol table!
91 // Strip the symbol table of its names.
92 static void StripTypeSymtab(TypeSymbolTable &ST) {
93 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
99 bool StripSymbols::runOnModule(Module &M) {
100 // If we're not just stripping debug info, strip all symbols from the
101 // functions and the names from any internal globals.
102 if (!OnlyDebugInfo) {
103 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
105 if (I->hasInternalLinkage())
106 I->setName(""); // Internal symbols can't participate in linkage
108 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
109 if (I->hasInternalLinkage())
110 I->setName(""); // Internal symbols can't participate in linkage
111 StripSymtab(I->getValueSymbolTable());
114 // Remove all names from types.
115 StripTypeSymtab(M.getTypeSymbolTable());
118 // Strip debug info in the module if it exists. To do this, we remove
119 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
120 // any globals they point to if now dead.
121 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
122 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
123 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
124 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
125 Function *Declare = M.getFunction("llvm.dbg.declare");
126 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
129 std::vector<GlobalVariable*> DeadGlobals;
131 // Remove all of the calls to the debugger intrinsics, and remove them from
134 while (!FuncStart->use_empty()) {
135 CallInst *CI = cast<CallInst>(FuncStart->use_back());
136 Value *Arg = CI->getOperand(1);
137 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
138 CI->eraseFromParent();
139 if (Arg->use_empty())
140 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
141 DeadGlobals.push_back(GV);
143 FuncStart->eraseFromParent();
146 while (!StopPoint->use_empty()) {
147 CallInst *CI = cast<CallInst>(StopPoint->use_back());
148 Value *Arg = CI->getOperand(3);
149 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
150 CI->eraseFromParent();
151 if (Arg->use_empty())
152 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
153 DeadGlobals.push_back(GV);
155 StopPoint->eraseFromParent();
158 while (!RegionStart->use_empty()) {
159 CallInst *CI = cast<CallInst>(RegionStart->use_back());
160 Value *Arg = CI->getOperand(1);
161 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
162 CI->eraseFromParent();
163 if (Arg->use_empty())
164 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
165 DeadGlobals.push_back(GV);
167 RegionStart->eraseFromParent();
170 while (!RegionEnd->use_empty()) {
171 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
172 Value *Arg = CI->getOperand(1);
173 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
174 CI->eraseFromParent();
175 if (Arg->use_empty())
176 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
177 DeadGlobals.push_back(GV);
179 RegionEnd->eraseFromParent();
182 while (!Declare->use_empty()) {
183 CallInst *CI = cast<CallInst>(Declare->use_back());
184 Value *Arg = CI->getOperand(2);
185 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
186 CI->eraseFromParent();
187 if (Arg->use_empty())
188 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
189 DeadGlobals.push_back(GV);
191 Declare->eraseFromParent();
194 // Finally, delete any internal globals that were only used by the debugger
196 while (!DeadGlobals.empty()) {
197 GlobalVariable *GV = DeadGlobals.back();
198 DeadGlobals.pop_back();
199 if (GV->hasInternalLinkage())
200 RemoveDeadConstant(GV);