StripSymbols.cpp revision b876cc193c163c269d9cf7ad1274fc31a5c1b106
1//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// 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
16//
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.
20//
21//===----------------------------------------------------------------------===//
22
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"
33using namespace llvm;
34
35namespace {
36  class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
37    bool OnlyDebugInfo;
38  public:
39    static char ID; // Pass identification, replacement for typeid
40    explicit StripSymbols(bool ODI = false)
41      : ModulePass(&ID), OnlyDebugInfo(ODI) {}
42
43    virtual bool runOnModule(Module &M);
44
45    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) {}
55
56    virtual bool runOnModule(Module &M);
57
58    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59      AU.setPreservesAll();
60    }
61  };
62}
63
64char StripSymbols::ID = 0;
65static RegisterPass<StripSymbols>
66X("strip", "Strip all symbols from a module");
67
68ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
69  return new StripSymbols(OnlyDebugInfo);
70}
71
72char 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
80/// 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
90static void RemoveDeadConstant(Constant *C) {
91  assert(C->use_empty() && "Constant is not dead!");
92  SmallPtrSet<Constant *, 4> Operands;
93  for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
94    if (isa<DerivedType>(C->getOperand(i)->getType()) &&
95        OnlyUsedBy(C->getOperand(i), C))
96      Operands.insert(C->getOperand(i));
97  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();
103
104  // If the constant referenced anything, see if we can delete it as well.
105  for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
106         OE = Operands.end(); OI != OE; ++OI)
107    RemoveDeadConstant(*OI);
108}
109
110// Strip the symbol table of its names.
111//
112static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
113  for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
114    Value *V = VI->getValue();
115    ++VI;
116    if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
117      if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
118        // Set name to "", removing from symbol table!
119        V->setName("");
120    }
121  }
122}
123
124// Strip the symbol table of its names.
125static 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  }
132}
133
134/// Find values that are marked as llvm.used.
135void findUsedValues(Module &M,
136                    SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
137  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);
151      }
152    }
153  }
154}
155
156/// StripSymbolNames - Strip symbol names.
157bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
158
159  SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
160  findUsedValues(M, llvmUsedValues);
161
162  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
163       I != E; ++I) {
164    if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
165      if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
166        I->setName("");     // Internal symbols can't participate in linkage
167  }
168
169  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
170    if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
171      if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
172        I->setName("");     // Internal symbols can't participate in linkage
173    StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
174  }
175
176  // Remove all names from types.
177  StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
178
179  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.
185bool StripDebugInfo(Module &M) {
186
187  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");
192
193  std::vector<Constant*> DeadConstants;
194
195  // Remove all of the calls to the debugger intrinsics, and remove them from
196  // the module.
197  if (FuncStart) {
198    while (!FuncStart->use_empty()) {
199      CallInst *CI = cast<CallInst>(FuncStart->use_back());
200      Value *Arg = CI->getOperand(1);
201      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
202      CI->eraseFromParent();
203      if (Arg->use_empty())
204        if (Constant *C = dyn_cast<Constant>(Arg))
205          DeadConstants.push_back(C);
206    }
207    FuncStart->eraseFromParent();
208  }
209  if (StopPoint) {
210    while (!StopPoint->use_empty()) {
211      CallInst *CI = cast<CallInst>(StopPoint->use_back());
212      Value *Arg = CI->getOperand(3);
213      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
214      CI->eraseFromParent();
215      if (Arg->use_empty())
216        if (Constant *C = dyn_cast<Constant>(Arg))
217          DeadConstants.push_back(C);
218    }
219    StopPoint->eraseFromParent();
220  }
221  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())
228        if (Constant *C = dyn_cast<Constant>(Arg))
229          DeadConstants.push_back(C);
230    }
231    RegionStart->eraseFromParent();
232  }
233  if (RegionEnd) {
234    while (!RegionEnd->use_empty()) {
235      CallInst *CI = cast<CallInst>(RegionEnd->use_back());
236      Value *Arg = CI->getOperand(1);
237      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
238      CI->eraseFromParent();
239      if (Arg->use_empty())
240        if (Constant *C = dyn_cast<Constant>(Arg))
241          DeadConstants.push_back(C);
242    }
243    RegionEnd->eraseFromParent();
244  }
245  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())
252        if (Constant *C = dyn_cast<Constant>(Arg))
253          DeadConstants.push_back(C);
254    }
255    Declare->eraseFromParent();
256  }
257
258  SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
259  findUsedValues(M, llvmUsedValues);
260
261  // 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);
270
271  if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
272    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
273      GV->setLinkage(GlobalValue::InternalLinkage);
274
275  // Delete all dbg variables.
276  const Type *DbgVTy = M.getTypeByName("llvm.dbg.variable.type");
277  const Type *DbgGVTy = M.getTypeByName("llvm.dbg.global_variable.type");
278  if (DbgVTy || DbgGVTy)
279    for (Module::global_iterator I = M.global_begin(), E = M.global_end();
280         I != E; ++I) {
281      GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
282      if (!GV) continue;
283      if (GV->use_empty() && llvmUsedValues.count(I) == 0
284          && (!GV->hasSection()
285              || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
286          DeadConstants.push_back(GV);
287    }
288
289  if (DeadConstants.empty())
290    return false;
291
292  // Delete any internal globals that were only used by the debugger intrinsics.
293  while (!DeadConstants.empty()) {
294    Constant *C = DeadConstants.back();
295    DeadConstants.pop_back();
296    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
297      if (GV->hasInternalLinkage())
298        RemoveDeadConstant(GV);
299    }
300    else
301      RemoveDeadConstant(C);
302  }
303
304  // Remove all llvm.dbg types.
305  TypeSymbolTable &ST = M.getTypeSymbolTable();
306  for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
307    if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
308      ST.remove(TI++);
309    else
310      ++TI;
311  }
312
313  return true;
314}
315
316bool StripSymbols::runOnModule(Module &M) {
317  bool Changed = false;
318  Changed |= StripDebugInfo(M);
319  if (!OnlyDebugInfo)
320    Changed |= StripSymbolNames(M, false);
321  return Changed;
322}
323
324bool StripNonDebugSymbols::runOnModule(Module &M) {
325  return StripSymbolNames(M, true);
326}
327
328