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 if (SymTab == 0) return false; // No symbol table? No problem.
24 bool RemovedSymbol = false;
26 for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
27 std::map<const std::string, Value *> &Plane = I->second;
29 SymbolTable::type_iterator B;
30 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
32 if (isa<Constant>(V) || isa<Type>(V))
33 SymTab->type_remove(B);
35 V->setName("", SymTab); // Set name to "", removing from symbol table!
37 assert(Plane.begin() != B && "Symbol not removed from table!");
45 struct SymbolStripping : public FunctionPass {
46 virtual bool runOnFunction(Function &F) {
47 return StripSymbolTable(F.getSymbolTable());
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
55 struct FullSymbolStripping : public SymbolStripping {
56 virtual bool doInitialization(Module &M) {
57 return StripSymbolTable(M.getSymbolTable());
60 RegisterOpt<FullSymbolStripping> Y("mstrip",
61 "Strip symbols from module and functions");
64 Pass *createSymbolStrippingPass() {
65 return new SymbolStripping();
68 Pass *createFullSymbolStrippingPass() {
69 return new FullSymbolStripping();