Chris Lattner | e3ad43c | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 1 | //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements stripping symbols out of symbol tables. |
| 11 | // |
| 12 | // Specifically, this allows you to strip all of the symbols out of: |
| 13 | // * All functions in a module |
| 14 | // * All non-essential symbols in a module (all function symbols + all module |
| 15 | // scope symbols) |
| 16 | // * Debug information. |
| 17 | // |
| 18 | // Notice that: |
| 19 | // * This pass makes code much less readable, so it should only be used in |
| 20 | // situations where the 'strip' utility would be used (such as reducing |
| 21 | // code size, and making it harder to reverse engineer code). |
| 22 | // |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | #include "llvm/Transforms/IPO.h" |
Chris Lattner | dd0ecf6 | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 26 | #include "llvm/Constants.h" |
| 27 | #include "llvm/DerivedTypes.h" |
| 28 | #include "llvm/Instructions.h" |
Chris Lattner | e3ad43c | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 29 | #include "llvm/Module.h" |
Chris Lattner | e3ad43c | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 30 | #include "llvm/Pass.h" |
Chris Lattner | dd0ecf6 | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 31 | #include "llvm/SymbolTable.h" |
Chris Lattner | e3ad43c | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | class StripSymbols : public ModulePass { |
| 36 | bool OnlyDebugInfo; |
| 37 | public: |
| 38 | StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {} |
| 39 | |
| 40 | virtual bool runOnModule(Module &M); |
| 41 | |
| 42 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 43 | AU.setPreservesAll(); |
| 44 | } |
| 45 | }; |
| 46 | RegisterOpt<StripSymbols> X("strip", "Strip all symbols from a module"); |
| 47 | } |
| 48 | |
| 49 | ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) { |
| 50 | return new StripSymbols(OnlyDebugInfo); |
| 51 | } |
| 52 | |
Chris Lattner | dd0ecf6 | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 53 | static void RemoveDeadConstant(Constant *C) { |
| 54 | assert(C->use_empty() && "Constant is not dead!"); |
| 55 | std::vector<Constant*> Operands; |
| 56 | for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) |
| 57 | if (isa<DerivedType>(C->getOperand(i)->getType()) && |
| 58 | C->getOperand(i)->hasOneUse()) |
| 59 | Operands.push_back(C->getOperand(i)); |
| 60 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) { |
| 61 | if (!GV->hasInternalLinkage()) return; // Don't delete non static globals. |
| 62 | GV->eraseFromParent(); |
| 63 | } |
| 64 | else if (!isa<Function>(C)) |
| 65 | C->destroyConstant(); |
| 66 | |
| 67 | // If the constant referenced anything, see if we can delete it as well. |
| 68 | while (!Operands.empty()) { |
| 69 | RemoveDeadConstant(Operands.back()); |
| 70 | Operands.pop_back(); |
| 71 | } |
| 72 | } |
Chris Lattner | e3ad43c | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 73 | |
| 74 | bool StripSymbols::runOnModule(Module &M) { |
| 75 | // If we're not just stripping debug info, strip all symbols from the |
| 76 | // functions and the names from any internal globals. |
| 77 | if (!OnlyDebugInfo) { |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame^] | 78 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) |
Chris Lattner | e3ad43c | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 79 | if (I->hasInternalLinkage()) |
| 80 | I->setName(""); // Internal symbols can't participate in linkage |
| 81 | |
| 82 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 83 | if (I->hasInternalLinkage()) |
| 84 | I->setName(""); // Internal symbols can't participate in linkage |
| 85 | I->getSymbolTable().strip(); |
| 86 | } |
| 87 | } |
| 88 | |
Chris Lattner | dd0ecf6 | 2004-12-03 16:22:08 +0000 | [diff] [blame] | 89 | // Strip debug info in the module if it exists. To do this, we remove |
| 90 | // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and |
| 91 | // any globals they point to if now dead. |
| 92 | Function *FuncStart = M.getNamedFunction("llvm.dbg.func.start"); |
| 93 | Function *StopPoint = M.getNamedFunction("llvm.dbg.stoppoint"); |
| 94 | Function *RegionEnd = M.getNamedFunction("llvm.dbg.region.end"); |
| 95 | if (!FuncStart && !StopPoint && !RegionEnd) |
| 96 | return true; |
| 97 | |
| 98 | std::vector<GlobalVariable*> DeadGlobals; |
| 99 | |
| 100 | // Remove all of the calls to the debugger intrinsics, and remove them from |
| 101 | // the module. |
| 102 | if (FuncStart) { |
| 103 | Value *RV = UndefValue::get(StopPoint->getFunctionType()->getReturnType()); |
| 104 | while (!FuncStart->use_empty()) { |
| 105 | CallInst *CI = cast<CallInst>(FuncStart->use_back()); |
| 106 | Value *Arg = CI->getOperand(1); |
| 107 | CI->replaceAllUsesWith(RV); |
| 108 | CI->eraseFromParent(); |
| 109 | if (Arg->use_empty()) |
| 110 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) |
| 111 | DeadGlobals.push_back(GV); |
| 112 | } |
| 113 | FuncStart->eraseFromParent(); |
| 114 | } |
| 115 | if (StopPoint) { |
| 116 | Value *RV = UndefValue::get(StopPoint->getFunctionType()->getReturnType()); |
| 117 | while (!StopPoint->use_empty()) { |
| 118 | CallInst *CI = cast<CallInst>(StopPoint->use_back()); |
| 119 | Value *Arg = CI->getOperand(4); |
| 120 | CI->replaceAllUsesWith(RV); |
| 121 | CI->eraseFromParent(); |
| 122 | if (Arg->use_empty()) |
| 123 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg)) |
| 124 | DeadGlobals.push_back(GV); |
| 125 | } |
| 126 | StopPoint->eraseFromParent(); |
| 127 | } |
| 128 | if (RegionEnd) { |
| 129 | Value *RV = UndefValue::get(RegionEnd->getFunctionType()->getReturnType()); |
| 130 | while (!RegionEnd->use_empty()) { |
| 131 | CallInst *CI = cast<CallInst>(RegionEnd->use_back()); |
| 132 | CI->replaceAllUsesWith(RV); |
| 133 | CI->eraseFromParent(); |
| 134 | } |
| 135 | RegionEnd->eraseFromParent(); |
| 136 | } |
| 137 | |
| 138 | // Finally, delete any internal globals that were only used by the debugger |
| 139 | // intrinsics. |
| 140 | while (!DeadGlobals.empty()) { |
| 141 | GlobalVariable *GV = DeadGlobals.back(); |
| 142 | DeadGlobals.pop_back(); |
| 143 | if (GV->hasInternalLinkage()) |
| 144 | RemoveDeadConstant(GV); |
| 145 | } |
| 146 | |
Chris Lattner | e3ad43c | 2004-12-02 21:25:03 +0000 | [diff] [blame] | 147 | return true; |
| 148 | } |