blob: b19f3277ab10fc0cd9f18e9ce4bff6f763751128 [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//
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 Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattnere3ad43c2004-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 Brukmanfd939082005-04-21 23:48:37 +000020// situations where the 'strip' utility would be used (such as reducing
Chris Lattnere3ad43c2004-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 Lattnerdd0ecf62004-12-03 16:22:08 +000026#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Instructions.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000029#include "llvm/Module.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000030#include "llvm/Pass.h"
Reid Spenceref9b9a72007-02-05 20:47:22 +000031#include "llvm/ValueSymbolTable.h"
Reid Spencer78d033e2007-01-06 07:24:44 +000032#include "llvm/TypeSymbolTable.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000033#include "llvm/Support/Compiler.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000034using namespace llvm;
35
36namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000037 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000038 bool OnlyDebugInfo;
39 public:
Devang Patel19974732007-05-03 01:11:54 +000040 static char ID; // Pass identifcation, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000041 StripSymbols(bool ODI = false)
42 : ModulePass((intptr_t)&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000043
44 virtual bool runOnModule(Module &M);
45
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 }
49 };
Devang Patel794fd752007-05-01 21:15:47 +000050
Devang Patel19974732007-05-03 01:11:54 +000051 char StripSymbols::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000052 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
Chris Lattnere3ad43c2004-12-02 21:25:03 +000053}
54
55ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
56 return new StripSymbols(OnlyDebugInfo);
57}
58
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000059static void RemoveDeadConstant(Constant *C) {
60 assert(C->use_empty() && "Constant is not dead!");
61 std::vector<Constant*> Operands;
62 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
63 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
64 C->getOperand(i)->hasOneUse())
65 Operands.push_back(C->getOperand(i));
66 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
67 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
68 GV->eraseFromParent();
69 }
70 else if (!isa<Function>(C))
71 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +000072
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000073 // If the constant referenced anything, see if we can delete it as well.
74 while (!Operands.empty()) {
75 RemoveDeadConstant(Operands.back());
76 Operands.pop_back();
77 }
78}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000079
Chris Lattner7f1444b2007-02-07 06:22:45 +000080// Strip the symbol table of its names.
81//
82static void StripSymtab(ValueSymbolTable &ST) {
83 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +000084 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +000085 ++VI;
86 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
87 // Set name to "", removing from symbol table!
88 V->setName("");
89 }
90 }
91}
92
93// Strip the symbol table of its names.
94static void StripTypeSymtab(TypeSymbolTable &ST) {
95 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
96 ST.remove(TI++);
97}
98
99
100
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000101bool StripSymbols::runOnModule(Module &M) {
102 // If we're not just stripping debug info, strip all symbols from the
103 // functions and the names from any internal globals.
104 if (!OnlyDebugInfo) {
Chris Lattner7f8897f2006-08-27 22:42:52 +0000105 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
106 I != E; ++I)
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000107 if (I->hasInternalLinkage())
108 I->setName(""); // Internal symbols can't participate in linkage
109
110 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
111 if (I->hasInternalLinkage())
112 I->setName(""); // Internal symbols can't participate in linkage
Chris Lattner7f1444b2007-02-07 06:22:45 +0000113 StripSymtab(I->getValueSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000114 }
Chris Lattnerb2f6c002006-03-15 19:22:41 +0000115
116 // Remove all names from types.
Chris Lattner7f1444b2007-02-07 06:22:45 +0000117 StripTypeSymtab(M.getTypeSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000118 }
119
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000120 // Strip debug info in the module if it exists. To do this, we remove
121 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
122 // any globals they point to if now dead.
Reid Spencer688b0492007-02-05 21:19:13 +0000123 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
124 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
125 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
126 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
127 Function *Declare = M.getFunction("llvm.dbg.declare");
Jim Laskey4ca97572006-03-23 18:11:33 +0000128 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000129 return true;
130
131 std::vector<GlobalVariable*> DeadGlobals;
132
133 // Remove all of the calls to the debugger intrinsics, and remove them from
134 // the module.
135 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000136 while (!FuncStart->use_empty()) {
137 CallInst *CI = cast<CallInst>(FuncStart->use_back());
138 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000139 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000140 CI->eraseFromParent();
141 if (Arg->use_empty())
142 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
143 DeadGlobals.push_back(GV);
144 }
145 FuncStart->eraseFromParent();
146 }
147 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000148 while (!StopPoint->use_empty()) {
149 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000150 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000151 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000152 CI->eraseFromParent();
153 if (Arg->use_empty())
154 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
155 DeadGlobals.push_back(GV);
156 }
157 StopPoint->eraseFromParent();
158 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000159 if (RegionStart) {
160 while (!RegionStart->use_empty()) {
161 CallInst *CI = cast<CallInst>(RegionStart->use_back());
162 Value *Arg = CI->getOperand(1);
163 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
164 CI->eraseFromParent();
165 if (Arg->use_empty())
166 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
167 DeadGlobals.push_back(GV);
168 }
169 RegionStart->eraseFromParent();
170 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000171 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000172 while (!RegionEnd->use_empty()) {
173 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000174 Value *Arg = CI->getOperand(1);
175 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000176 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000177 if (Arg->use_empty())
178 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
179 DeadGlobals.push_back(GV);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000180 }
181 RegionEnd->eraseFromParent();
182 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000183 if (Declare) {
184 while (!Declare->use_empty()) {
185 CallInst *CI = cast<CallInst>(Declare->use_back());
186 Value *Arg = CI->getOperand(2);
187 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
188 CI->eraseFromParent();
189 if (Arg->use_empty())
190 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
191 DeadGlobals.push_back(GV);
192 }
193 Declare->eraseFromParent();
194 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000195
196 // Finally, delete any internal globals that were only used by the debugger
197 // intrinsics.
198 while (!DeadGlobals.empty()) {
199 GlobalVariable *GV = DeadGlobals.back();
200 DeadGlobals.pop_back();
201 if (GV->hasInternalLinkage())
202 RemoveDeadConstant(GV);
203 }
204
Misha Brukmanfd939082005-04-21 23:48:37 +0000205 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000206}