1 //===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
3 // This file implements stripping symbols out of symbol tables.
5 // Specifically, this allows you to strip all of the symbols out of:
7 // * All functions in a module
8 // * All symbols in a module (all function symbols + all module scope symbols)
11 // * This pass makes code much less readable, so it should only be used in
12 // situations where the 'strip' utility would be used (such as reducing
13 // code size, and making it harder to reverse engineer code).
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Module.h"
19 #include "llvm/Function.h"
20 #include "llvm/SymbolTable.h"
21 #include "llvm/Pass.h"
23 static bool StripSymbolTable(SymbolTable *SymTab) {
24 if (SymTab == 0) return false; // No symbol table? No problem.
25 bool RemovedSymbol = false;
27 for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
28 std::map<const std::string, Value *> &Plane = I->second;
30 SymbolTable::type_iterator B;
31 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
33 if (isa<Constant>(V) || isa<Type>(V))
34 SymTab->type_remove(B);
36 V->setName("", SymTab); // Set name to "", removing from symbol table!
38 assert(Plane.begin() != B && "Symbol not removed from table!");
46 struct SymbolStripping : public FunctionPass {
47 virtual bool runOnFunction(Function &F) {
48 return StripSymbolTable(F.getSymbolTable());
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
56 struct FullSymbolStripping : public SymbolStripping {
57 virtual bool doInitialization(Module &M) {
58 return StripSymbolTable(M.getSymbolTable());
61 RegisterOpt<FullSymbolStripping> Y("mstrip",
62 "Strip symbols from module and functions");
65 Pass *createSymbolStrippingPass() {
66 return new SymbolStripping();
69 Pass *createFullSymbolStrippingPass() {
70 return new FullSymbolStripping();