blob: 297c2763dfe7aebac5303f5e860b90014b3df8bf [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"
Devang Patel8c231e52008-01-16 03:33:05 +000032#include "llvm/ADT/SmallPtrSet.h"
Chris Lattnere3ad43c2004-12-02 21:25:03 +000033using namespace llvm;
34
35namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000036 class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
Chris Lattnere3ad43c2004-12-02 21:25:03 +000037 bool OnlyDebugInfo;
38 public:
Nick Lewyckyecd94c82007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Dan Gohmanc2bbfc12007-08-01 15:32:29 +000040 explicit StripSymbols(bool ODI = false)
Devang Patel794fd752007-05-01 21:15:47 +000041 : ModulePass((intptr_t)&ID), OnlyDebugInfo(ODI) {}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000042
43 virtual bool runOnModule(Module &M);
44
45 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46 AU.setPreservesAll();
47 }
48 };
Devang Patel794fd752007-05-01 21:15:47 +000049
Devang Patel19974732007-05-03 01:11:54 +000050 char StripSymbols::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000051 RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
Chris Lattnere3ad43c2004-12-02 21:25:03 +000052}
53
54ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
55 return new StripSymbols(OnlyDebugInfo);
56}
57
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000058static void RemoveDeadConstant(Constant *C) {
59 assert(C->use_empty() && "Constant is not dead!");
60 std::vector<Constant*> Operands;
61 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
62 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
63 C->getOperand(i)->hasOneUse())
64 Operands.push_back(C->getOperand(i));
65 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
66 if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
67 GV->eraseFromParent();
68 }
69 else if (!isa<Function>(C))
70 C->destroyConstant();
Misha Brukmanfd939082005-04-21 23:48:37 +000071
Chris Lattnerdd0ecf62004-12-03 16:22:08 +000072 // If the constant referenced anything, see if we can delete it as well.
73 while (!Operands.empty()) {
74 RemoveDeadConstant(Operands.back());
75 Operands.pop_back();
76 }
77}
Chris Lattnere3ad43c2004-12-02 21:25:03 +000078
Chris Lattner7f1444b2007-02-07 06:22:45 +000079// Strip the symbol table of its names.
80//
81static void StripSymtab(ValueSymbolTable &ST) {
82 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattnerdec628e2007-02-12 05:18:08 +000083 Value *V = VI->getValue();
Chris Lattner7f1444b2007-02-07 06:22:45 +000084 ++VI;
85 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
86 // Set name to "", removing from symbol table!
87 V->setName("");
88 }
89 }
90}
91
92// Strip the symbol table of its names.
93static void StripTypeSymtab(TypeSymbolTable &ST) {
94 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
95 ST.remove(TI++);
96}
97
98
99
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000100bool StripSymbols::runOnModule(Module &M) {
101 // If we're not just stripping debug info, strip all symbols from the
102 // functions and the names from any internal globals.
103 if (!OnlyDebugInfo) {
Devang Patel8c231e52008-01-16 03:33:05 +0000104 SmallPtrSet<const Constant *, 8> llvmUsedValues;
105 Value *LLVMUsed = M.getValueSymbolTable().lookup("llvm.used");
106 if (LLVMUsed) {
107 // Collect values that are preserved as per explicit request.
108 // llvm.used is used to list these values.
109 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(LLVMUsed)) {
110 if (ConstantArray *InitList =
111 dyn_cast<ConstantArray>(GV->getInitializer())) {
112 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
113 if (ConstantExpr *CE =
114 dyn_cast<ConstantExpr>(InitList->getOperand(i)))
115 if (CE->isCast())
116 llvmUsedValues.insert(CE->getOperand(0));
117 }
118 }
119 }
120 }
121
Chris Lattner7f8897f2006-08-27 22:42:52 +0000122 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Devang Patel8c231e52008-01-16 03:33:05 +0000123 I != E; ++I) {
124 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000125 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel8c231e52008-01-16 03:33:05 +0000126 else if (I->getName() == "llvm.used") {
127 }
128 }
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000129
130 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Devang Patel8c231e52008-01-16 03:33:05 +0000131 if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000132 I->setName(""); // Internal symbols can't participate in linkage
Chris Lattner7f1444b2007-02-07 06:22:45 +0000133 StripSymtab(I->getValueSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000134 }
Chris Lattnerb2f6c002006-03-15 19:22:41 +0000135
136 // Remove all names from types.
Chris Lattner7f1444b2007-02-07 06:22:45 +0000137 StripTypeSymtab(M.getTypeSymbolTable());
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000138 }
139
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000140 // Strip debug info in the module if it exists. To do this, we remove
141 // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
142 // any globals they point to if now dead.
Reid Spencer688b0492007-02-05 21:19:13 +0000143 Function *FuncStart = M.getFunction("llvm.dbg.func.start");
144 Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
145 Function *RegionStart = M.getFunction("llvm.dbg.region.start");
146 Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
147 Function *Declare = M.getFunction("llvm.dbg.declare");
Jim Laskey4ca97572006-03-23 18:11:33 +0000148 if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000149 return true;
150
151 std::vector<GlobalVariable*> DeadGlobals;
152
153 // Remove all of the calls to the debugger intrinsics, and remove them from
154 // the module.
155 if (FuncStart) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000156 while (!FuncStart->use_empty()) {
157 CallInst *CI = cast<CallInst>(FuncStart->use_back());
158 Value *Arg = CI->getOperand(1);
Jim Laskey4ca97572006-03-23 18:11:33 +0000159 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000160 CI->eraseFromParent();
161 if (Arg->use_empty())
162 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
163 DeadGlobals.push_back(GV);
164 }
165 FuncStart->eraseFromParent();
166 }
167 if (StopPoint) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000168 while (!StopPoint->use_empty()) {
169 CallInst *CI = cast<CallInst>(StopPoint->use_back());
Jim Laskeyf4321a32006-03-13 13:07:37 +0000170 Value *Arg = CI->getOperand(3);
Jim Laskey4ca97572006-03-23 18:11:33 +0000171 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000172 CI->eraseFromParent();
173 if (Arg->use_empty())
174 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
175 DeadGlobals.push_back(GV);
176 }
177 StopPoint->eraseFromParent();
178 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000179 if (RegionStart) {
180 while (!RegionStart->use_empty()) {
181 CallInst *CI = cast<CallInst>(RegionStart->use_back());
182 Value *Arg = CI->getOperand(1);
183 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
184 CI->eraseFromParent();
185 if (Arg->use_empty())
186 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
187 DeadGlobals.push_back(GV);
188 }
189 RegionStart->eraseFromParent();
190 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000191 if (RegionEnd) {
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000192 while (!RegionEnd->use_empty()) {
193 CallInst *CI = cast<CallInst>(RegionEnd->use_back());
Jim Laskey4ca97572006-03-23 18:11:33 +0000194 Value *Arg = CI->getOperand(1);
195 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000196 CI->eraseFromParent();
Jim Laskey4ca97572006-03-23 18:11:33 +0000197 if (Arg->use_empty())
198 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
199 DeadGlobals.push_back(GV);
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000200 }
201 RegionEnd->eraseFromParent();
202 }
Jim Laskey4ca97572006-03-23 18:11:33 +0000203 if (Declare) {
204 while (!Declare->use_empty()) {
205 CallInst *CI = cast<CallInst>(Declare->use_back());
206 Value *Arg = CI->getOperand(2);
207 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
208 CI->eraseFromParent();
209 if (Arg->use_empty())
210 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
211 DeadGlobals.push_back(GV);
212 }
213 Declare->eraseFromParent();
214 }
Chris Lattnerdd0ecf62004-12-03 16:22:08 +0000215
216 // Finally, delete any internal globals that were only used by the debugger
217 // intrinsics.
218 while (!DeadGlobals.empty()) {
219 GlobalVariable *GV = DeadGlobals.back();
220 DeadGlobals.pop_back();
221 if (GV->hasInternalLinkage())
222 RemoveDeadConstant(GV);
223 }
224
Misha Brukmanfd939082005-04-21 23:48:37 +0000225 return true;
Chris Lattnere3ad43c2004-12-02 21:25:03 +0000226}