1 //===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements stripping symbols out of symbol tables.
12 // Specifically, this allows you to strip all of the symbols out of:
14 // * All functions in a module
15 // * All symbols in a module (all function symbols + all module scope symbols)
18 // * This pass makes code much less readable, so it should only be used in
19 // situations where the 'strip' utility would be used (such as reducing
20 // code size, and making it harder to reverse engineer code).
22 //===----------------------------------------------------------------------===//
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Module.h"
26 #include "llvm/SymbolTable.h"
27 #include "llvm/Pass.h"
31 static bool StripSymbolTable(SymbolTable &SymTab) {
32 bool RemovedSymbol = false;
34 for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end(); ++I) {
35 std::map<const std::string, Value *> &Plane = I->second;
37 SymbolTable::type_iterator B;
38 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
40 if (isa<Constant>(V) || isa<Type>(V))
41 SymTab.type_remove(B);
43 V->setName("", &SymTab); // Set name to "", removing from symbol table!
45 assert(Plane.begin() != B && "Symbol not removed from table!");
53 struct SymbolStripping : public FunctionPass {
54 virtual bool runOnFunction(Function &F) {
55 return StripSymbolTable(F.getSymbolTable());
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
63 struct FullSymbolStripping : public SymbolStripping {
64 virtual bool doInitialization(Module &M) {
65 return StripSymbolTable(M.getSymbolTable());
68 RegisterOpt<FullSymbolStripping> Y("mstrip",
69 "Strip symbols from module and functions");
72 Pass *createSymbolStrippingPass() {
73 return new SymbolStripping();
76 Pass *createFullSymbolStrippingPass() {
77 return new FullSymbolStripping();
80 } // End llvm namespace