2002-04-07 20:49:59 +00:00
|
|
|
//===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// This file implements stripping symbols out of symbol tables.
|
|
|
|
//
|
|
|
|
// Specifically, this allows you to strip all of the symbols out of:
|
2002-04-07 20:49:59 +00:00
|
|
|
// * A function
|
|
|
|
// * All functions in a module
|
|
|
|
// * All symbols in a module (all function symbols + all module scope symbols)
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
|
|
|
// Notice that:
|
|
|
|
// * This pass 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, and making it harder to reverse engineer code).
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-05-07 20:03:00 +00:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
|
|
|
|
static bool StripSymbolTable(SymbolTable *SymTab) {
|
|
|
|
if (SymTab == 0) return false; // No symbol table? No problem.
|
|
|
|
bool RemovedSymbol = false;
|
|
|
|
|
2001-06-27 23:41:11 +00:00
|
|
|
for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::map<const std::string, Value *> &Plane = I->second;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-09-07 16:43:50 +00:00
|
|
|
SymbolTable::type_iterator B;
|
2001-06-06 20:29:01 +00:00
|
|
|
while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
|
2001-09-07 16:43:50 +00:00
|
|
|
Value *V = B->second;
|
2001-12-03 22:26:30 +00:00
|
|
|
if (isa<Constant>(V) || isa<Type>(V))
|
2001-09-07 16:43:50 +00:00
|
|
|
SymTab->type_remove(B);
|
|
|
|
else
|
|
|
|
V->setName("", SymTab); // Set name to "", removing from symbol table!
|
2001-06-06 20:29:01 +00:00
|
|
|
RemovedSymbol = true;
|
2001-09-07 16:43:50 +00:00
|
|
|
assert(Plane.begin() != B && "Symbol not removed from table!");
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RemovedSymbol;
|
|
|
|
}
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-04-27 06:56:12 +00:00
|
|
|
struct SymbolStripping : public FunctionPass {
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool runOnFunction(Function &F) {
|
|
|
|
return StripSymbolTable(F.getSymbolTable());
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
2002-04-28 21:27:06 +00:00
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
};
|
2002-07-26 21:12:46 +00:00
|
|
|
RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
struct FullSymbolStripping : public SymbolStripping {
|
2002-06-25 16:13:24 +00:00
|
|
|
virtual bool doInitialization(Module &M) {
|
|
|
|
return StripSymbolTable(M.getSymbolTable());
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
};
|
2002-07-26 21:12:46 +00:00
|
|
|
RegisterOpt<FullSymbolStripping> Y("mstrip",
|
|
|
|
"Strip symbols from module and functions");
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createSymbolStrippingPass() {
|
|
|
|
return new SymbolStripping();
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createFullSymbolStrippingPass() {
|
|
|
|
return new FullSymbolStripping();
|
|
|
|
}
|