blob: 4c0b35a1a4e78ee597c6d5fd1f602b4765000d04 [file] [log] [blame]
Chris Lattnere8ebcb32004-12-02 21:25:03 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnere8ebcb32004-12-02 21:25:03 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnere8ebcb32004-12-02 21:25:03 +00008//===----------------------------------------------------------------------===//
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
Misha Brukmanb1c93172005-04-21 23:48:37 +000020// situations where the 'strip' utility would be used (such as reducing
Chris Lattnere8ebcb32004-12-02 21:25:03 +000021// code size, and making it harder to reverse engineer code).
22//
23//===----------------------------------------------------------------------===//
24
25#include "llvm/Transforms/IPO.h"
Chris Lattner9019e5c2004-12-03 16:22:08 +000026#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Instructions.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000029#include "llvm/Module.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000030#include "llvm/Pass.h"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000031#include "llvm/ValueSymbolTable.h"
Reid Spencer32af9e82007-01-06 07:24:44 +000032#include "llvm/TypeSymbolTable.h"
Reid Spencer557ab152007-02-05 23:32:05 +000033#include "llvm/Support/Compiler.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000034using namespace llvm;
35
36namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000037 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000038 bool OnlyDebugInfo;
39 public:
40 StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {}
41
42 virtual bool runOnModule(Module &M);
43
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 }
47 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +000048 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
Chris Lattnere8ebcb32004-12-02 21:25:03 +000049}
50
51ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
52 return new StripSymbols(OnlyDebugInfo);
53}
54
Chris Lattner9019e5c2004-12-03 16:22:08 +000055static void RemoveDeadConstant(Constant *C) {
56 assert(C->use_empty() && "Constant is not dead!");
57 std::vector<Constant*> Operands;
58 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
59 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
60 C->getOperand(i)->hasOneUse())
61 Operands.push_back(C->getOperand(i));
62 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
63 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
64 GV->eraseFromParent();
65 }
66 else if (!isa<Function>(C))
67 C->destroyConstant();
Misha Brukmanb1c93172005-04-21 23:48:37 +000068
Chris Lattner9019e5c2004-12-03 16:22:08 +000069 // If the constant referenced anything, see if we can delete it as well.
70 while (!Operands.empty()) {
71 RemoveDeadConstant(Operands.back());
72 Operands.pop_back();
73 }
74}
Chris Lattnere8ebcb32004-12-02 21:25:03 +000075
76bool StripSymbols::runOnModule(Module &M) {
77 // If we're not just stripping debug info, strip all symbols from the
78 // functions and the names from any internal globals.
79 if (!OnlyDebugInfo) {
Chris Lattnerc2d3d312006-08-27 22:42:52 +000080 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
81 I != E; ++I)
Chris Lattnere8ebcb32004-12-02 21:25:03 +000082 if (I->hasInternalLinkage())
83 I->setName(""); // Internal symbols can't participate in linkage
84
85 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
86 if (I->hasInternalLinkage())
87 I->setName(""); // Internal symbols can't participate in linkage
Reid Spencer32af9e82007-01-06 07:24:44 +000088 I->getValueSymbolTable().strip();
Chris Lattnere8ebcb32004-12-02 21:25:03 +000089 }
Chris Lattner6d6084f2006-03-15 19:22:41 +000090
91 // Remove all names from types.
Reid Spencer32af9e82007-01-06 07:24:44 +000092 M.getTypeSymbolTable().strip();
Chris Lattnere8ebcb32004-12-02 21:25:03 +000093 }
94
Chris Lattner9019e5c2004-12-03 16:22:08 +000095 // Strip debug info in the module if it exists. To do this, we remove
96 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
97 // any globals they point to if now dead.
Reid Spencer1241d6d2007-02-05 21:19:13 +000098 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
99 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
100 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
101 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
102 Function *Declare = M.getFunction("llvm.dbg.declare");
Jim Laskey8f644262006-03-23 18:11:33 +0000103 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
Chris Lattner9019e5c2004-12-03 16:22:08 +0000104 return true;
105
106 std::vector<GlobalVariable*> DeadGlobals;
107
108 // Remove all of the calls to the debugger intrinsics, and remove them from
109 // the module.
110 if (FuncStart) {
Chris Lattner9019e5c2004-12-03 16:22:08 +0000111 while (!FuncStart->use_empty()) {
112 CallInst *CI = cast<CallInst>(FuncStart->use_back());
113 Value *Arg = CI->getOperand(1);
Jim Laskey8f644262006-03-23 18:11:33 +0000114 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattner9019e5c2004-12-03 16:22:08 +0000115 CI->eraseFromParent();
116 if (Arg->use_empty())
117 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
118 DeadGlobals.push_back(GV);
119 }
120 FuncStart->eraseFromParent();
121 }
122 if (StopPoint) {
Chris Lattner9019e5c2004-12-03 16:22:08 +0000123 while (!StopPoint->use_empty()) {
124 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyacb6e342006-03-13 13:07:37 +0000125 Value *Arg = CI->getOperand(3);
Jim Laskey8f644262006-03-23 18:11:33 +0000126 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattner9019e5c2004-12-03 16:22:08 +0000127 CI->eraseFromParent();
128 if (Arg->use_empty())
129 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
130 DeadGlobals.push_back(GV);
131 }
132 StopPoint->eraseFromParent();
133 }
Jim Laskey8f644262006-03-23 18:11:33 +0000134 if (RegionStart) {
135 while (!RegionStart->use_empty()) {
136 CallInst *CI = cast<CallInst>(RegionStart->use_back());
137 Value *Arg = CI->getOperand(1);
138 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
139 CI->eraseFromParent();
140 if (Arg->use_empty())
141 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
142 DeadGlobals.push_back(GV);
143 }
144 RegionStart->eraseFromParent();
145 }
Chris Lattner9019e5c2004-12-03 16:22:08 +0000146 if (RegionEnd) {
Chris Lattner9019e5c2004-12-03 16:22:08 +0000147 while (!RegionEnd->use_empty()) {
148 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey8f644262006-03-23 18:11:33 +0000149 Value *Arg = CI->getOperand(1);
150 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattner9019e5c2004-12-03 16:22:08 +0000151 CI->eraseFromParent();
Jim Laskey8f644262006-03-23 18:11:33 +0000152 if (Arg->use_empty())
153 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
154 DeadGlobals.push_back(GV);
Chris Lattner9019e5c2004-12-03 16:22:08 +0000155 }
156 RegionEnd->eraseFromParent();
157 }
Jim Laskey8f644262006-03-23 18:11:33 +0000158 if (Declare) {
159 while (!Declare->use_empty()) {
160 CallInst *CI = cast<CallInst>(Declare->use_back());
161 Value *Arg = CI->getOperand(2);
162 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
163 CI->eraseFromParent();
164 if (Arg->use_empty())
165 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
166 DeadGlobals.push_back(GV);
167 }
168 Declare->eraseFromParent();
169 }
Chris Lattner9019e5c2004-12-03 16:22:08 +0000170
171 // Finally, delete any internal globals that were only used by the debugger
172 // intrinsics.
173 while (!DeadGlobals.empty()) {
174 GlobalVariable *GV = DeadGlobals.back();
175 DeadGlobals.pop_back();
176 if (GV->hasInternalLinkage())
177 RemoveDeadConstant(GV);
178 }
179
Misha Brukmanb1c93172005-04-21 23:48:37 +0000180 return true;
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000181}