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
|
|
|
|
2008-11-18 21:34:39 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-11-14 22:49:37 +00:00
|
|
|
|
2008-11-18 21:34:39 +00:00
|
|
|
class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripNonDebugSymbols()
|
|
|
|
: ModulePass(&ID) {}
|
2008-11-14 22:49:37 +00:00
|
|
|
|
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-18 21:34:39 +00:00
|
|
|
char StripNonDebugSymbols::ID = 0;
|
|
|
|
static RegisterPass<StripNonDebugSymbols>
|
|
|
|
Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
|
|
|
|
|
|
|
|
ModulePass *llvm::createStripNonDebugSymbolsPass() {
|
|
|
|
return new StripNonDebugSymbols();
|
|
|
|
}
|
|
|
|
|
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))
|
2008-11-20 01:20:42 +00:00
|
|
|
if (isa<CompositeType>(C->getType()))
|
|
|
|
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.
|
|
|
|
//
|
2008-11-18 21:34:39 +00:00
|
|
|
static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
|
2007-02-07 06:22:45 +00:00
|
|
|
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()) {
|
2008-11-18 21:34:39 +00:00
|
|
|
if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
|
|
|
|
// Set name to "", removing from symbol table!
|
|
|
|
V->setName("");
|
2007-02-07 06:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the symbol table of its names.
|
2008-11-18 21:34:39 +00:00
|
|
|
static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
|
|
|
|
for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
|
|
|
|
if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
|
|
|
|
++TI;
|
|
|
|
else
|
|
|
|
ST.remove(TI++);
|
|
|
|
}
|
2007-02-07 06:22:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
/// Find values that are marked as llvm.used.
|
|
|
|
void findUsedValues(Module &M,
|
|
|
|
SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
|
2008-11-14 22:49:37 +00:00
|
|
|
if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
|
|
|
|
llvmUsedValues.insert(LLVMUsed);
|
|
|
|
// Collect values that are preserved as per explicit request.
|
|
|
|
// llvm.used is used to list these values.
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2008-11-14 22:49:37 +00:00
|
|
|
}
|
2008-11-18 21:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// StripSymbolNames - Strip symbol names.
|
2008-11-18 21:34:39 +00:00
|
|
|
bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
|
2008-11-18 21:13:41 +00:00
|
|
|
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
|
|
|
findUsedValues(M, llvmUsedValues);
|
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
|
2008-11-18 21:34:39 +00:00
|
|
|
if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
|
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
2008-11-14 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
|
|
|
if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
|
2008-11-18 21:34:39 +00:00
|
|
|
if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
|
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
|
|
|
StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
|
2008-11-14 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all names from types.
|
2008-11-18 21:34:39 +00:00
|
|
|
StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
|
2008-01-16 03:33:05 +00:00
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
// StripDebugInfo - 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.
|
2008-11-18 21:34:39 +00:00
|
|
|
bool StripDebugInfo(Module &M) {
|
2004-12-02 21:25:03 +00:00
|
|
|
|
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
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
std::vector<Constant*> DeadConstants;
|
2004-12-03 16:22:08 +00:00
|
|
|
|
|
|
|
// 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())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
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())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
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())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
2006-03-23 18:11:33 +00:00
|
|
|
}
|
|
|
|
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())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
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());
|
2008-11-20 01:20:42 +00:00
|
|
|
Value *Arg1 = CI->getOperand(1);
|
|
|
|
Value *Arg2 = CI->getOperand(2);
|
2006-03-23 18:11:33 +00:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
2008-11-20 01:20:42 +00:00
|
|
|
if (Arg1->use_empty()) {
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg1))
|
|
|
|
DeadConstants.push_back(C);
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Arg1))
|
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
2008-11-18 21:13:41 +00:00
|
|
|
DeadConstants.push_back(C);
|
2006-03-23 18:11:33 +00:00
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
|
|
|
}
|
2004-12-03 16:22:08 +00:00
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
|
|
|
findUsedValues(M, llvmUsedValues);
|
|
|
|
|
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);
|
2008-11-18 21:13:41 +00:00
|
|
|
|
|
|
|
if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
2008-11-13 01:28:40 +00:00
|
|
|
|
|
|
|
// Delete all dbg variables.
|
2008-11-19 00:22:02 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
|
|
|
|
if (!GV) continue;
|
|
|
|
if (GV->use_empty() && llvmUsedValues.count(I) == 0
|
|
|
|
&& (!GV->hasSection()
|
|
|
|
|| strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
|
|
|
|
DeadConstants.push_back(GV);
|
|
|
|
}
|
2008-11-13 01:28:40 +00:00
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
if (DeadConstants.empty())
|
2008-11-14 22:49:37 +00:00
|
|
|
return false;
|
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
// Delete any internal globals that were only used by the debugger intrinsics.
|
2008-11-18 21:13:41 +00:00
|
|
|
while (!DeadConstants.empty()) {
|
|
|
|
Constant *C = DeadConstants.back();
|
|
|
|
DeadConstants.pop_back();
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
|
|
|
if (GV->hasInternalLinkage())
|
|
|
|
RemoveDeadConstant(GV);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
RemoveDeadConstant(C);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
// Remove all llvm.dbg types.
|
|
|
|
TypeSymbolTable &ST = M.getTypeSymbolTable();
|
2008-11-16 06:35:18 +00:00
|
|
|
for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
|
|
|
|
if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
|
2008-11-13 01:28:40 +00:00
|
|
|
ST.remove(TI++);
|
|
|
|
else
|
|
|
|
++TI;
|
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
return true;
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|
2008-11-18 21:34:39 +00:00
|
|
|
|
|
|
|
bool StripSymbols::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
Changed |= StripDebugInfo(M);
|
|
|
|
if (!OnlyDebugInfo)
|
|
|
|
Changed |= StripSymbolNames(M, false);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StripNonDebugSymbols::runOnModule(Module &M) {
|
|
|
|
return StripSymbolNames(M, true);
|
|
|
|
}
|
|
|
|
|