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