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"
|
2002-04-07 20:49:59 +00:00
|
|
|
#include "llvm/Function.h"
|
2001-06-06 20:29:01 +00:00
|
|
|
#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-04-07 20:49:59 +00:00
|
|
|
// DoSymbolStripping - Remove all symbolic information from a function
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2002-04-07 20:49:59 +00:00
|
|
|
static bool doSymbolStripping(Function *F) {
|
|
|
|
return StripSymbolTable(F->getSymbolTable());
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2002-04-07 20:49:59 +00:00
|
|
|
// doStripGlobalSymbols - Remove all symbolic information from all functions
|
|
|
|
// in a module, and all module level symbols. (function names, etc...)
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2002-02-26 21:46:54 +00:00
|
|
|
static bool doStripGlobalSymbols(Module *M) {
|
2002-04-07 20:49:59 +00:00
|
|
|
// Remove all symbols from functions in this module... and then strip all of
|
|
|
|
// the symbols in this module...
|
2001-06-06 20:29:01 +00:00
|
|
|
//
|
2001-10-18 01:32:34 +00:00
|
|
|
return StripSymbolTable(M->getSymbolTable());
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
namespace {
|
2002-04-27 06:56:12 +00:00
|
|
|
struct SymbolStripping : public FunctionPass {
|
2002-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const { return "Strip Symbols from Functions"; }
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
virtual bool runOnFunction(Function *F) {
|
2002-04-07 20:49:59 +00:00
|
|
|
return doSymbolStripping(F);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
struct FullSymbolStripping : public SymbolStripping {
|
2002-04-29 14:57:45 +00:00
|
|
|
const char *getPassName() const { return "Strip Symbols from Module"; }
|
2002-02-26 21:46:54 +00:00
|
|
|
virtual bool doInitialization(Module *M) {
|
|
|
|
return doStripGlobalSymbols(M);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createSymbolStrippingPass() {
|
|
|
|
return new SymbolStripping();
|
|
|
|
}
|
|
|
|
|
|
|
|
Pass *createFullSymbolStrippingPass() {
|
|
|
|
return new FullSymbolStripping();
|
|
|
|
}
|