1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements stripping symbols out of symbol tables.
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
16 // * Debug information.
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).
23 //===----------------------------------------------------------------------===//
25 #include "llvm/Transforms/IPO.h"
26 #include "llvm/Constants.h"
27 #include "llvm/DerivedTypes.h"
28 #include "llvm/Instructions.h"
29 #include "llvm/Module.h"
30 #include "llvm/Pass.h"
31 #include "llvm/SymbolTable.h"
35 class StripSymbols : public ModulePass {
38 StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {}
40 virtual bool runOnModule(Module &M);
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 RegisterOpt<StripSymbols> X("strip", "Strip all symbols from a module");
49 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
50 return new StripSymbols(OnlyDebugInfo);
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();
64 else if (!isa<Function>(C))
67 // If the constant referenced anything, see if we can delete it as well.
68 while (!Operands.empty()) {
69 RemoveDeadConstant(Operands.back());
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.
78 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
79 if (I->hasInternalLinkage())
80 I->setName(""); // Internal symbols can't participate in linkage
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();
88 // Remove all names from types.
89 SymbolTable &SymTab = M.getSymbolTable();
90 while (SymTab.type_begin() != SymTab.type_end())
91 SymTab.remove(SymTab.type_begin());
94 // Strip debug info in the module if it exists. To do this, we remove
95 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
96 // any globals they point to if now dead.
97 Function *FuncStart = M.getNamedFunction("llvm.dbg.func.start");
98 Function *StopPoint = M.getNamedFunction("llvm.dbg.stoppoint");
99 Function *RegionEnd = M.getNamedFunction("llvm.dbg.region.end");
100 if (!FuncStart && !StopPoint && !RegionEnd)
103 std::vector<GlobalVariable*> DeadGlobals;
105 // Remove all of the calls to the debugger intrinsics, and remove them from
108 Value *RV = UndefValue::get(FuncStart->getFunctionType()->getReturnType());
109 while (!FuncStart->use_empty()) {
110 CallInst *CI = cast<CallInst>(FuncStart->use_back());
111 Value *Arg = CI->getOperand(1);
112 CI->replaceAllUsesWith(RV);
113 CI->eraseFromParent();
114 if (Arg->use_empty())
115 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
116 DeadGlobals.push_back(GV);
118 FuncStart->eraseFromParent();
121 Value *RV = UndefValue::get(StopPoint->getFunctionType()->getReturnType());
122 while (!StopPoint->use_empty()) {
123 CallInst *CI = cast<CallInst>(StopPoint->use_back());
124 Value *Arg = CI->getOperand(3);
125 CI->replaceAllUsesWith(RV);
126 CI->eraseFromParent();
127 if (Arg->use_empty())
128 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
129 DeadGlobals.push_back(GV);
131 StopPoint->eraseFromParent();
134 Value *RV = UndefValue::get(RegionEnd->getFunctionType()->getReturnType());
135 while (!RegionEnd->use_empty()) {
136 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
137 CI->replaceAllUsesWith(RV);
138 CI->eraseFromParent();
140 RegionEnd->eraseFromParent();
143 // Finally, delete any internal globals that were only used by the debugger
145 while (!DeadGlobals.empty()) {
146 GlobalVariable *GV = DeadGlobals.back();
147 DeadGlobals.pop_back();
148 if (GV->hasInternalLinkage())
149 RemoveDeadConstant(GV);