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/SymbolTable.h"
20 #include "llvm/Pass.h"
22 static bool StripSymbolTable(SymbolTable &SymTab) {
23 bool RemovedSymbol = false;
25 for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end(); ++I) {
26 std::map<const std::string, Value *> &Plane = I->second;
28 SymbolTable::type_iterator B;
29 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
31 if (isa<Constant>(V) || isa<Type>(V))
32 SymTab.type_remove(B);
34 V->setName("", &SymTab); // Set name to "", removing from symbol table!
36 assert(Plane.begin() != B && "Symbol not removed from table!");
44 struct SymbolStripping : public FunctionPass {
45 virtual bool runOnFunction(Function &F) {
46 return StripSymbolTable(F.getSymbolTable());
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
54 struct FullSymbolStripping : public SymbolStripping {
55 virtual bool doInitialization(Module &M) {
56 return StripSymbolTable(M.getSymbolTable());
59 RegisterOpt<FullSymbolStripping> Y("mstrip",
60 "Strip symbols from module and functions");
63 Pass *createSymbolStrippingPass() {
64 return new SymbolStripping();
67 Pass *createFullSymbolStrippingPass() {
68 return new FullSymbolStripping();