blob: 9427eb67824fef300e17ba75be960d3c26ab7226 [file] [log] [blame]
Chris Lattnere3ad43c2004-12-02 21:25:03 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnere3ad43c2004-12-02 21:25:03 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattnere3ad43c2004-12-02 21:25:03 +00008//===----------------------------------------------------------------------===//
9//
Gordon Henriksenc86b6772007-11-04 16:15:04 +000010// 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
Chris Lattnere3ad43c2004-12-02 21:25:03 +000016//
Gordon Henriksenc86b6772007-11-04 16:15:04 +000017// 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.
Chris Lattnere3ad43c2004-12-02 21:25:03 +000020//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/Transforms/IPO.h"
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000024#include "llvm/Constants.h"
25#include "llvm/DerivedTypes.h"
26#include "llvm/Instructions.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000027#include "llvm/Module.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000028#include "llvm/Pass.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000029#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000030#include "llvm/TypeSymbolTable.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000031#include "llvm/Support/Compiler.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000032using namespace llvm;
33
34namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000035 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000036 bool OnlyDebugInfo;
37 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000039 explicit StripSymbols(bool ODI = false)
Devang Patel794fd752007-05-01 21:15:47 +000040 : ModulePass((intptr_t)&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000041
42 virtual bool runOnModule(Module &M);
43
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
46 }
47 };
Devang Patel794fd752007-05-01 21:15:47 +000048
Devang Patel19974732007-05-03 01:11:54 +000049 char StripSymbols::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000050 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
Chris Lattnere3ad43c2004-12-02 21:25:03 +000051}
52
53ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
54 return new StripSymbols(OnlyDebugInfo);
55}
56
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000057static void RemoveDeadConstant(Constant *C) {
58 assert(C->use_empty() && "Constant is not dead!");
59 std::vector<Constant*> Operands;
60 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
61 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
62 C->getOperand(i)->hasOneUse())
63 Operands.push_back(C->getOperand(i));
64 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
65 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
66 GV->eraseFromParent();
67 }
68 else if (!isa<Function>(C))
69 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +000070
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000071 // If the constant referenced anything, see if we can delete it as well.
72 while (!Operands.empty()) {
73 RemoveDeadConstant(Operands.back());
74 Operands.pop_back();
75 }
76}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000077
Chris Lattner7f1444b2007-02-07 06:22:45 +000078// Strip the symbol table of its names.
79//
80static void StripSymtab(ValueSymbolTable &ST) {
81 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +000082 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +000083 ++VI;
84 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
85 // Set name to "", removing from symbol table!
86 V->setName("");
87 }
88 }
89}
90
91// Strip the symbol table of its names.
92static void StripTypeSymtab(TypeSymbolTable &ST) {
93 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
94 ST.remove(TI++);
95}
96
97
98
Chris Lattnere3ad43c2004-12-02 21:25:03 +000099bool StripSymbols::runOnModule(Module &M) {
100 // If we're not just stripping debug info, strip all symbols from the
101 // functions and the names from any internal globals.
102 if (!OnlyDebugInfo) {
Chris Lattner7f8897f2006-08-27 22:42:52 +0000103 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
104 I != E; ++I)
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000105 if (I->hasInternalLinkage())
106 I->setName(""); // Internal symbols can't participate in linkage
107
108 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
109 if (I->hasInternalLinkage())
110 I->setName(""); // Internal symbols can't participate in linkage
Chris Lattner7f1444b2007-02-07 06:22:45 +0000111 StripSymtab(I->getValueSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000112 }
Chris Lattnerb2f6c002006-03-15 19:22:41 +0000113
114 // Remove all names from types.
Chris Lattner7f1444b2007-02-07 06:22:45 +0000115 StripTypeSymtab(M.getTypeSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000116 }
117
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000118 // Strip debug info in the module if it exists. To do this, we remove
119 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
120 // any globals they point to if now dead.
Reid Spencer688b0492007-02-05 21:19:13 +0000121 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
122 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
123 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
124 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
125 Function *Declare = M.getFunction("llvm.dbg.declare");
Jim Laskey4ca97572006-03-23 18:11:33 +0000126 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000127 return true;
128
129 std::vector<GlobalVariable*> DeadGlobals;
130
131 // Remove all of the calls to the debugger intrinsics, and remove them from
132 // the module.
133 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000134 while (!FuncStart->use_empty()) {
135 CallInst *CI = cast<CallInst>(FuncStart->use_back());
136 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000137 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000138 CI->eraseFromParent();
139 if (Arg->use_empty())
140 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
141 DeadGlobals.push_back(GV);
142 }
143 FuncStart->eraseFromParent();
144 }
145 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000146 while (!StopPoint->use_empty()) {
147 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000148 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000149 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000150 CI->eraseFromParent();
151 if (Arg->use_empty())
152 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
153 DeadGlobals.push_back(GV);
154 }
155 StopPoint->eraseFromParent();
156 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000157 if (RegionStart) {
158 while (!RegionStart->use_empty()) {
159 CallInst *CI = cast<CallInst>(RegionStart->use_back());
160 Value *Arg = CI->getOperand(1);
161 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
162 CI->eraseFromParent();
163 if (Arg->use_empty())
164 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
165 DeadGlobals.push_back(GV);
166 }
167 RegionStart->eraseFromParent();
168 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000169 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000170 while (!RegionEnd->use_empty()) {
171 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000172 Value *Arg = CI->getOperand(1);
173 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000174 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000175 if (Arg->use_empty())
176 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
177 DeadGlobals.push_back(GV);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000178 }
179 RegionEnd->eraseFromParent();
180 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000181 if (Declare) {
182 while (!Declare->use_empty()) {
183 CallInst *CI = cast<CallInst>(Declare->use_back());
184 Value *Arg = CI->getOperand(2);
185 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
186 CI->eraseFromParent();
187 if (Arg->use_empty())
188 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
189 DeadGlobals.push_back(GV);
190 }
191 Declare->eraseFromParent();
192 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000193
194 // Finally, delete any internal globals that were only used by the debugger
195 // intrinsics.
196 while (!DeadGlobals.empty()) {
197 GlobalVariable *GV = DeadGlobals.back();
198 DeadGlobals.pop_back();
199 if (GV->hasInternalLinkage())
200 RemoveDeadConstant(GV);
201 }
202
Misha Brukmanfd939082005-04-21 23:48:37 +0000203 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000204}