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