1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
32 #include "llvm/ADT/SmallPtrSet.h"
36 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
39 static char ID; // Pass identification, replacement for typeid
40 explicit StripSymbols(bool ODI = false)
41 : ModulePass((intptr_t)&ID), OnlyDebugInfo(ODI) {}
43 virtual bool runOnModule(Module &M);
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 char StripSymbols::ID = 0;
51 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
54 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
55 return new StripSymbols(OnlyDebugInfo);
58 static void RemoveDeadConstant(Constant *C) {
59 assert(C->use_empty() && "Constant is not dead!");
60 std::vector<Constant*> Operands;
61 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
62 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
63 C->getOperand(i)->hasOneUse())
64 Operands.push_back(C->getOperand(i));
65 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
66 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
67 GV->eraseFromParent();
69 else if (!isa<Function>(C))
72 // If the constant referenced anything, see if we can delete it as well.
73 while (!Operands.empty()) {
74 RemoveDeadConstant(Operands.back());
79 // Strip the symbol table of its names.
81 static void StripSymtab(ValueSymbolTable &ST) {
82 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
83 Value *V = VI->getValue();
85 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
86 // Set name to "", removing from symbol table!
92 // Strip the symbol table of its names.
93 static void StripTypeSymtab(TypeSymbolTable &ST) {
94 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
100 bool StripSymbols::runOnModule(Module &M) {
101 // If we're not just stripping debug info, strip all symbols from the
102 // functions and the names from any internal globals.
103 if (!OnlyDebugInfo) {
104 SmallPtrSet<const Constant *, 8> llvmUsedValues;
105 Value *LLVMUsed = M.getValueSymbolTable().lookup("llvm.used");
107 // Collect values that are preserved as per explicit request.
108 // llvm.used is used to list these values.
109 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LLVMUsed)) {
110 if (ConstantArray *InitList =
111 dyn_cast<ConstantArray>(GV->getInitializer())) {
112 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
113 if (ConstantExpr *CE =
114 dyn_cast<ConstantExpr>(InitList->getOperand(i)))
116 llvmUsedValues.insert(CE->getOperand(0));
122 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
124 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
125 I->setName(""); // Internal symbols can't participate in linkage
126 else if (I->getName() == "llvm.used") {
130 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
131 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
132 I->setName(""); // Internal symbols can't participate in linkage
133 StripSymtab(I->getValueSymbolTable());
136 // Remove all names from types.
137 StripTypeSymtab(M.getTypeSymbolTable());
140 // Strip debug info in the module if it exists. To do this, we remove
141 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
142 // any globals they point to if now dead.
143 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
144 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
145 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
146 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
147 Function *Declare = M.getFunction("llvm.dbg.declare");
148 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
151 std::vector<GlobalVariable*> DeadGlobals;
153 // Remove all of the calls to the debugger intrinsics, and remove them from
156 while (!FuncStart->use_empty()) {
157 CallInst *CI = cast<CallInst>(FuncStart->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 FuncStart->eraseFromParent();
168 while (!StopPoint->use_empty()) {
169 CallInst *CI = cast<CallInst>(StopPoint->use_back());
170 Value *Arg = CI->getOperand(3);
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 StopPoint->eraseFromParent();
180 while (!RegionStart->use_empty()) {
181 CallInst *CI = cast<CallInst>(RegionStart->use_back());
182 Value *Arg = CI->getOperand(1);
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 RegionStart->eraseFromParent();
192 while (!RegionEnd->use_empty()) {
193 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
194 Value *Arg = CI->getOperand(1);
195 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
196 CI->eraseFromParent();
197 if (Arg->use_empty())
198 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
199 DeadGlobals.push_back(GV);
201 RegionEnd->eraseFromParent();
204 while (!Declare->use_empty()) {
205 CallInst *CI = cast<CallInst>(Declare->use_back());
206 Value *Arg = CI->getOperand(2);
207 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
208 CI->eraseFromParent();
209 if (Arg->use_empty())
210 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
211 DeadGlobals.push_back(GV);
213 Declare->eraseFromParent();
216 // Finally, delete any internal globals that were only used by the debugger
218 while (!DeadGlobals.empty()) {
219 GlobalVariable *GV = DeadGlobals.back();
220 DeadGlobals.pop_back();
221 if (GV->hasInternalLinkage())
222 RemoveDeadConstant(GV);