StripSymbols.cpp revision 791211fbaf493253b5a7ea96f03e4cdc4456fb4f
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/LLVMContext.h"
28#include "llvm/Module.h"
29#include "llvm/Pass.h"
30#include "llvm/Analysis/DebugInfo.h"
31#include "llvm/ValueSymbolTable.h"
32#include "llvm/TypeSymbolTable.h"
33#include "llvm/Transforms/Utils/Local.h"
34#include "llvm/Support/Compiler.h"
35#include "llvm/ADT/SmallPtrSet.h"
36using namespace llvm;
37
38namespace {
39  class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
40    bool OnlyDebugInfo;
41  public:
42    static char ID; // Pass identification, replacement for typeid
43    explicit StripSymbols(bool ODI = false)
44      : ModulePass(&ID), OnlyDebugInfo(ODI) {}
45
46    virtual bool runOnModule(Module &M);
47
48    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49      AU.setPreservesAll();
50    }
51  };
52
53  class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
54  public:
55    static char ID; // Pass identification, replacement for typeid
56    explicit StripNonDebugSymbols()
57      : ModulePass(&ID) {}
58
59    virtual bool runOnModule(Module &M);
60
61    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
62      AU.setPreservesAll();
63    }
64  };
65
66  class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
67  public:
68    static char ID; // Pass identification, replacement for typeid
69    explicit StripDebugDeclare()
70      : ModulePass(&ID) {}
71
72    virtual bool runOnModule(Module &M);
73
74    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75      AU.setPreservesAll();
76    }
77  };
78}
79
80char StripSymbols::ID = 0;
81static RegisterPass<StripSymbols>
82X("strip", "Strip all symbols from a module");
83
84ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
85  return new StripSymbols(OnlyDebugInfo);
86}
87
88char StripNonDebugSymbols::ID = 0;
89static RegisterPass<StripNonDebugSymbols>
90Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
91
92ModulePass *llvm::createStripNonDebugSymbolsPass() {
93  return new StripNonDebugSymbols();
94}
95
96char StripDebugDeclare::ID = 0;
97static RegisterPass<StripDebugDeclare>
98Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
99
100ModulePass *llvm::createStripDebugDeclarePass() {
101  return new StripDebugDeclare();
102}
103
104/// OnlyUsedBy - Return true if V is only used by Usr.
105static bool OnlyUsedBy(Value *V, Value *Usr) {
106  for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
107    User *U = *I;
108    if (U != Usr)
109      return false;
110  }
111  return true;
112}
113
114static void RemoveDeadConstant(Constant *C) {
115  assert(C->use_empty() && "Constant is not dead!");
116  SmallPtrSet<Constant *, 4> Operands;
117  for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
118    if (isa<DerivedType>(C->getOperand(i)->getType()) &&
119        OnlyUsedBy(C->getOperand(i), C))
120      Operands.insert(C->getOperand(i));
121  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
122    if (!GV->hasLocalLinkage()) return;   // Don't delete non static globals.
123    GV->eraseFromParent();
124  }
125  else if (!isa<Function>(C))
126    if (isa<CompositeType>(C->getType()))
127      C->destroyConstant();
128
129  // If the constant referenced anything, see if we can delete it as well.
130  for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
131         OE = Operands.end(); OI != OE; ++OI)
132    RemoveDeadConstant(*OI);
133}
134
135// Strip the symbol table of its names.
136//
137static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
138  for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
139    Value *V = VI->getValue();
140    ++VI;
141    if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
142      if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
143        // Set name to "", removing from symbol table!
144        V->setName("");
145    }
146  }
147}
148
149// Strip the symbol table of its names.
150static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
151  for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
152    if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
153      ++TI;
154    else
155      ST.remove(TI++);
156  }
157}
158
159/// Find values that are marked as llvm.used.
160static void findUsedValues(GlobalVariable *LLVMUsed,
161                           SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
162  if (LLVMUsed == 0) return;
163  UsedValues.insert(LLVMUsed);
164
165  ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
166  if (Inits == 0) return;
167
168  for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
169    if (GlobalValue *GV =
170          dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
171      UsedValues.insert(GV);
172}
173
174/// StripSymbolNames - Strip symbol names.
175bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
176
177  SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
178  findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
179  findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
180
181  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
182       I != E; ++I) {
183    if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
184      if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
185        I->setName("");     // Internal symbols can't participate in linkage
186  }
187
188  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
189    if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
190      if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
191        I->setName("");     // Internal symbols can't participate in linkage
192    StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
193  }
194
195  // Remove all names from types.
196  StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
197
198  return true;
199}
200
201// StripDebugInfo - Strip debug info in the module if it exists.
202// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
203// llvm.dbg.region.end calls, and any globals they point to if now dead.
204bool StripDebugInfo(Module &M) {
205
206  SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
207  findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
208  findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
209
210  DebugInfoFinder DbgFinder;
211  DbgFinder.processModule(M);
212
213  // These anchors use LinkOnce linkage so that the optimizer does not
214  // remove them accidently. Set InternalLinkage for all these debug
215  // info anchors.
216  for (DebugInfoFinder::iterator I = DbgFinder.compile_unit_begin(),
217         E = DbgFinder.compile_unit_end(); I != E; ++I)
218    (*I)->setLinkage(GlobalValue::InternalLinkage);
219  for (DebugInfoFinder::iterator I = DbgFinder.global_variable_begin(),
220         E = DbgFinder.global_variable_end(); I != E; ++I)
221    (*I)->setLinkage(GlobalValue::InternalLinkage);
222  for (DebugInfoFinder::iterator I = DbgFinder.subprogram_begin(),
223         E = DbgFinder.subprogram_end(); I != E; ++I)
224    (*I)->setLinkage(GlobalValue::InternalLinkage);
225
226
227 // Delete all dbg variables.
228  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
229       I != E; ++I) {
230    GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
231    if (!GV) continue;
232    if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
233      if (GV->getName().startswith("llvm.dbg")) {
234        GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
235      }
236    }
237  }
238
239  Function *FuncStart = M.getFunction("llvm.dbg.func.start");
240  Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
241  Function *RegionStart = M.getFunction("llvm.dbg.region.start");
242  Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
243  Function *Declare = M.getFunction("llvm.dbg.declare");
244
245  std::vector<Constant*> DeadConstants;
246
247  // Remove all of the calls to the debugger intrinsics, and remove them from
248  // the module.
249  if (FuncStart) {
250    while (!FuncStart->use_empty()) {
251      CallInst *CI = cast<CallInst>(FuncStart->use_back());
252      Value *Arg = CI->getOperand(1);
253      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
254      CI->eraseFromParent();
255      if (Arg->use_empty())
256        if (Constant *C = dyn_cast<Constant>(Arg))
257          DeadConstants.push_back(C);
258    }
259    FuncStart->eraseFromParent();
260  }
261  if (StopPoint) {
262    while (!StopPoint->use_empty()) {
263      CallInst *CI = cast<CallInst>(StopPoint->use_back());
264      Value *Arg = CI->getOperand(3);
265      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
266      CI->eraseFromParent();
267      if (Arg->use_empty())
268        if (Constant *C = dyn_cast<Constant>(Arg))
269          DeadConstants.push_back(C);
270    }
271    StopPoint->eraseFromParent();
272  }
273  if (RegionStart) {
274    while (!RegionStart->use_empty()) {
275      CallInst *CI = cast<CallInst>(RegionStart->use_back());
276      Value *Arg = CI->getOperand(1);
277      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
278      CI->eraseFromParent();
279      if (Arg->use_empty())
280        if (Constant *C = dyn_cast<Constant>(Arg))
281          DeadConstants.push_back(C);
282    }
283    RegionStart->eraseFromParent();
284  }
285  if (RegionEnd) {
286    while (!RegionEnd->use_empty()) {
287      CallInst *CI = cast<CallInst>(RegionEnd->use_back());
288      Value *Arg = CI->getOperand(1);
289      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
290      CI->eraseFromParent();
291      if (Arg->use_empty())
292        if (Constant *C = dyn_cast<Constant>(Arg))
293          DeadConstants.push_back(C);
294    }
295    RegionEnd->eraseFromParent();
296  }
297  if (Declare) {
298    while (!Declare->use_empty()) {
299      CallInst *CI = cast<CallInst>(Declare->use_back());
300      Value *Arg1 = CI->getOperand(1);
301      Value *Arg2 = CI->getOperand(2);
302      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
303      CI->eraseFromParent();
304      if (Arg1->use_empty()) {
305        if (Constant *C = dyn_cast<Constant>(Arg1))
306          DeadConstants.push_back(C);
307        else
308          RecursivelyDeleteTriviallyDeadInstructions(Arg1);
309      }
310      if (Arg2->use_empty())
311        if (Constant *C = dyn_cast<Constant>(Arg2))
312          DeadConstants.push_back(C);
313    }
314    Declare->eraseFromParent();
315  }
316
317  // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
318  // but since we are removing all debug information, make them internal now.
319  // FIXME: Use private linkage maybe?
320  if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
321    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
322      GV->setLinkage(GlobalValue::InternalLinkage);
323
324  if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
325    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
326      GV->setLinkage(GlobalValue::InternalLinkage);
327
328  if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
329    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
330      GV->setLinkage(GlobalValue::InternalLinkage);
331
332  // Delete all dbg variables.
333  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
334       I != E; ++I) {
335    GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
336    if (!GV) continue;
337    if (GV->use_empty() && llvmUsedValues.count(I) == 0
338        && (!GV->hasSection()
339            || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
340      DeadConstants.push_back(GV);
341  }
342
343  if (DeadConstants.empty())
344    return false;
345
346  // Delete any internal globals that were only used by the debugger intrinsics.
347  while (!DeadConstants.empty()) {
348    Constant *C = DeadConstants.back();
349    DeadConstants.pop_back();
350    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
351      if (GV->hasLocalLinkage())
352        RemoveDeadConstant(GV);
353    }
354    else
355      RemoveDeadConstant(C);
356  }
357
358  // Remove all llvm.dbg types.
359  TypeSymbolTable &ST = M.getTypeSymbolTable();
360  for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
361    if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
362      ST.remove(TI++);
363    else
364      ++TI;
365  }
366
367  return true;
368}
369
370bool StripSymbols::runOnModule(Module &M) {
371  bool Changed = false;
372  Changed |= StripDebugInfo(M);
373  if (!OnlyDebugInfo)
374    Changed |= StripSymbolNames(M, false);
375  return Changed;
376}
377
378bool StripNonDebugSymbols::runOnModule(Module &M) {
379  return StripSymbolNames(M, true);
380}
381
382bool StripDebugDeclare::runOnModule(Module &M) {
383
384  Function *Declare = M.getFunction("llvm.dbg.declare");
385  std::vector<Constant*> DeadConstants;
386
387  if (Declare) {
388    while (!Declare->use_empty()) {
389      CallInst *CI = cast<CallInst>(Declare->use_back());
390      Value *Arg1 = CI->getOperand(1);
391      Value *Arg2 = CI->getOperand(2);
392      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
393      CI->eraseFromParent();
394      if (Arg1->use_empty()) {
395        if (Constant *C = dyn_cast<Constant>(Arg1))
396          DeadConstants.push_back(C);
397        else
398          RecursivelyDeleteTriviallyDeadInstructions(Arg1);
399      }
400      if (Arg2->use_empty())
401        if (Constant *C = dyn_cast<Constant>(Arg2))
402          DeadConstants.push_back(C);
403    }
404    Declare->eraseFromParent();
405  }
406
407  // Delete all llvm.dbg.global_variables.
408  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
409       I != E; ++I) {
410    GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
411    if (!GV) continue;
412    if (GV->use_empty() && GV->getName().startswith("llvm.dbg.global_variable"))
413      DeadConstants.push_back(GV);
414  }
415
416  while (!DeadConstants.empty()) {
417    Constant *C = DeadConstants.back();
418    DeadConstants.pop_back();
419    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
420      if (GV->hasLocalLinkage())
421        RemoveDeadConstant(GV);
422    }
423    else
424      RemoveDeadConstant(C);
425  }
426
427  return true;
428}
429