2004-12-02 21:25:03 +00:00
|
|
|
//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-12-02 21:25:03 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-12-02 21:25:03 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// The StripSymbols transformation implements code stripping. Specifically, it
|
|
|
|
// can delete:
|
|
|
|
//
|
|
|
|
// * names for virtual registers
|
|
|
|
// * symbols for internal globals and functions
|
|
|
|
// * debug information
|
2004-12-02 21:25:03 +00:00
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// Note that this transformation makes code much less readable, so it should
|
|
|
|
// only be used in situations where the 'strip' utility would be used, such as
|
|
|
|
// reducing code size or making it harder to reverse engineer code.
|
2004-12-02 21:25:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2004-12-03 16:22:08 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2004-12-02 21:25:03 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2007-02-05 20:47:22 +00:00
|
|
|
#include "llvm/ValueSymbolTable.h"
|
2007-01-06 07:24:44 +00:00
|
|
|
#include "llvm/TypeSymbolTable.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-01-16 03:33:05 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-12-02 21:25:03 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
|
2004-12-02 21:25:03 +00:00
|
|
|
bool OnlyDebugInfo;
|
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-08-01 15:32:29 +00:00
|
|
|
explicit StripSymbols(bool ODI = false)
|
2008-09-04 17:05:41 +00:00
|
|
|
: ModulePass(&ID), OnlyDebugInfo(ODI) {}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char StripSymbols::ID = 0;
|
|
|
|
static RegisterPass<StripSymbols>
|
|
|
|
X("strip", "Strip all symbols from a module");
|
|
|
|
|
2004-12-02 21:25:03 +00:00
|
|
|
ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
|
|
|
|
return new StripSymbols(OnlyDebugInfo);
|
|
|
|
}
|
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
/// OnlyUsedBy - Return true if V is only used by Usr.
|
|
|
|
static bool OnlyUsedBy(Value *V, Value *Usr) {
|
|
|
|
for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
|
|
|
|
User *U = *I;
|
|
|
|
if (U != Usr)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-03 16:22:08 +00:00
|
|
|
static void RemoveDeadConstant(Constant *C) {
|
|
|
|
assert(C->use_empty() && "Constant is not dead!");
|
2008-11-13 01:28:40 +00:00
|
|
|
SmallPtrSet<Constant *, 4> Operands;
|
2004-12-03 16:22:08 +00:00
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
|
|
|
if (isa<DerivedType>(C->getOperand(i)->getType()) &&
|
2008-11-13 01:28:40 +00:00
|
|
|
OnlyUsedBy(C->getOperand(i), C))
|
|
|
|
Operands.insert(C->getOperand(i));
|
2004-12-03 16:22:08 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
|
|
|
if (!GV->hasInternalLinkage()) return; // Don't delete non static globals.
|
|
|
|
GV->eraseFromParent();
|
|
|
|
}
|
|
|
|
else if (!isa<Function>(C))
|
|
|
|
C->destroyConstant();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-12-03 16:22:08 +00:00
|
|
|
// If the constant referenced anything, see if we can delete it as well.
|
2008-11-13 01:28:40 +00:00
|
|
|
for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
|
|
|
|
OE = Operands.end(); OI != OE; ++OI)
|
|
|
|
RemoveDeadConstant(*OI);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2007-02-07 06:22:45 +00:00
|
|
|
// Strip the symbol table of its names.
|
|
|
|
//
|
|
|
|
static void StripSymtab(ValueSymbolTable &ST) {
|
|
|
|
for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
|
2007-02-12 05:18:08 +00:00
|
|
|
Value *V = VI->getValue();
|
2007-02-07 06:22:45 +00:00
|
|
|
++VI;
|
|
|
|
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
|
|
|
|
// Set name to "", removing from symbol table!
|
|
|
|
V->setName("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the symbol table of its names.
|
|
|
|
static void StripTypeSymtab(TypeSymbolTable &ST) {
|
|
|
|
for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
|
|
|
|
ST.remove(TI++);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-12-02 21:25:03 +00:00
|
|
|
bool StripSymbols::runOnModule(Module &M) {
|
|
|
|
// If we're not just stripping debug info, strip all symbols from the
|
|
|
|
// functions and the names from any internal globals.
|
|
|
|
if (!OnlyDebugInfo) {
|
2008-01-16 21:35:43 +00:00
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
|
|
|
if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
|
|
|
|
llvmUsedValues.insert(LLVMUsed);
|
2008-01-16 03:33:05 +00:00
|
|
|
// Collect values that are preserved as per explicit request.
|
|
|
|
// llvm.used is used to list these values.
|
2008-01-16 21:35:43 +00:00
|
|
|
if (ConstantArray *Inits =
|
|
|
|
dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
|
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
|
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
|
|
|
|
llvmUsedValues.insert(GV);
|
|
|
|
else if (ConstantExpr *CE =
|
|
|
|
dyn_cast<ConstantExpr>(Inits->getOperand(i)))
|
|
|
|
if (CE->getOpcode() == Instruction::BitCast)
|
|
|
|
if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
|
|
|
|
llvmUsedValues.insert(GV);
|
2008-01-16 03:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-27 22:42:52 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
2008-01-16 03:33:05 +00:00
|
|
|
I != E; ++I) {
|
|
|
|
if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
|
2004-12-02 21:25:03 +00:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
2008-01-16 03:33:05 +00:00
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2008-01-16 03:33:05 +00:00
|
|
|
if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
|
2004-12-02 21:25:03 +00:00
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
2007-02-07 06:22:45 +00:00
|
|
|
StripSymtab(I->getValueSymbolTable());
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|
2006-03-15 19:22:41 +00:00
|
|
|
|
|
|
|
// Remove all names from types.
|
2007-02-07 06:22:45 +00:00
|
|
|
StripTypeSymtab(M.getTypeSymbolTable());
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|
|
|
|
|
2004-12-03 16:22:08 +00:00
|
|
|
// Strip debug info in the module if it exists. To do this, we remove
|
|
|
|
// llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
|
|
|
|
// any globals they point to if now dead.
|
2007-02-05 21:19:13 +00:00
|
|
|
Function *FuncStart = M.getFunction("llvm.dbg.func.start");
|
|
|
|
Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
|
|
|
|
Function *RegionStart = M.getFunction("llvm.dbg.region.start");
|
|
|
|
Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
|
|
|
|
Function *Declare = M.getFunction("llvm.dbg.declare");
|
2004-12-03 16:22:08 +00:00
|
|
|
|
|
|
|
std::vector<GlobalVariable*> DeadGlobals;
|
|
|
|
|
|
|
|
// Remove all of the calls to the debugger intrinsics, and remove them from
|
|
|
|
// the module.
|
|
|
|
if (FuncStart) {
|
|
|
|
while (!FuncStart->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(FuncStart->use_back());
|
|
|
|
Value *Arg = CI->getOperand(1);
|
2006-03-23 18:11:33 +00:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
2004-12-03 16:22:08 +00:00
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg->use_empty())
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
|
|
|
|
DeadGlobals.push_back(GV);
|
|
|
|
}
|
|
|
|
FuncStart->eraseFromParent();
|
|
|
|
}
|
|
|
|
if (StopPoint) {
|
|
|
|
while (!StopPoint->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(StopPoint->use_back());
|
2006-03-13 13:07:37 +00:00
|
|
|
Value *Arg = CI->getOperand(3);
|
2006-03-23 18:11:33 +00:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
2004-12-03 16:22:08 +00:00
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg->use_empty())
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
|
|
|
|
DeadGlobals.push_back(GV);
|
|
|
|
}
|
|
|
|
StopPoint->eraseFromParent();
|
|
|
|
}
|
2006-03-23 18:11:33 +00:00
|
|
|
if (RegionStart) {
|
|
|
|
while (!RegionStart->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(RegionStart->use_back());
|
|
|
|
Value *Arg = CI->getOperand(1);
|
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg->use_empty())
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
|
|
|
|
DeadGlobals.push_back(GV);
|
|
|
|
}
|
|
|
|
RegionStart->eraseFromParent();
|
|
|
|
}
|
2004-12-03 16:22:08 +00:00
|
|
|
if (RegionEnd) {
|
|
|
|
while (!RegionEnd->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(RegionEnd->use_back());
|
2006-03-23 18:11:33 +00:00
|
|
|
Value *Arg = CI->getOperand(1);
|
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
2004-12-03 16:22:08 +00:00
|
|
|
CI->eraseFromParent();
|
2006-03-23 18:11:33 +00:00
|
|
|
if (Arg->use_empty())
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
|
|
|
|
DeadGlobals.push_back(GV);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
RegionEnd->eraseFromParent();
|
|
|
|
}
|
2006-03-23 18:11:33 +00:00
|
|
|
if (Declare) {
|
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
|
|
|
Value *Arg = CI->getOperand(2);
|
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg->use_empty())
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
|
|
|
|
DeadGlobals.push_back(GV);
|
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
|
|
|
}
|
2004-12-03 16:22:08 +00:00
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
// llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
|
|
|
|
// but since we are removing all debug information, make them internal now.
|
|
|
|
if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
|
|
|
|
if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
|
|
|
|
// Delete all dbg variables.
|
|
|
|
const Type *DbgVTy = M.getTypeByName("llvm.dbg.variable.type");
|
|
|
|
const Type *DbgGVTy = M.getTypeByName("llvm.dbg.global_variable.type");
|
|
|
|
if (DbgVTy || DbgGVTy)
|
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I)
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(I))
|
|
|
|
if (GV->hasName() && GV->use_empty()
|
|
|
|
&& !strncmp(GV->getNameStart(), "llvm.dbg", 8)
|
|
|
|
&& (GV->getType()->getElementType() == DbgVTy
|
|
|
|
|| GV->getType()->getElementType() == DbgGVTy))
|
|
|
|
DeadGlobals.push_back(GV);
|
|
|
|
|
|
|
|
// Delete any internal globals that were only used by the debugger intrinsics.
|
2004-12-03 16:22:08 +00:00
|
|
|
while (!DeadGlobals.empty()) {
|
|
|
|
GlobalVariable *GV = DeadGlobals.back();
|
|
|
|
DeadGlobals.pop_back();
|
|
|
|
if (GV->hasInternalLinkage())
|
|
|
|
RemoveDeadConstant(GV);
|
|
|
|
}
|
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
// Remove all llvm.dbg types.
|
|
|
|
TypeSymbolTable &ST = M.getTypeSymbolTable();
|
|
|
|
TypeSymbolTable::iterator TI = ST.begin();
|
|
|
|
TypeSymbolTable::iterator TE = ST.end();
|
|
|
|
while ( TI != TE ) {
|
|
|
|
const std::string &Name = TI->first;
|
|
|
|
if (!strncmp(Name.c_str(), "llvm.dbg.", 9))
|
|
|
|
ST.remove(TI++);
|
|
|
|
else
|
|
|
|
++TI;
|
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
return true;
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|