f611d9cf0a823962114af73c3bce1cdc17ad6482
[oota-llvm.git] / lib / Transforms / IPO / StripSymbols.cpp
1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements stripping symbols out of symbol tables.
11 //
12 // Specifically, this allows you to strip all of the symbols out of:
13 //   * All functions in a module
14 //   * All non-essential symbols in a module (all function symbols + all module
15 //     scope symbols)
16 //   * Debug information.
17 //
18 // Notice that:
19 //   * This pass makes code much less readable, so it should only be used in
20 //     situations where the 'strip' utility would be used (such as reducing 
21 //     code size, and making it harder to reverse engineer code).
22 //
23 //===----------------------------------------------------------------------===//
24
25 #include "llvm/Transforms/IPO.h"
26 #include "llvm/Module.h"
27 #include "llvm/SymbolTable.h"
28 #include "llvm/Pass.h"
29 using namespace llvm;
30
31 namespace {
32   class StripSymbols : public ModulePass {
33     bool OnlyDebugInfo;
34   public:
35     StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {}
36
37     virtual bool runOnModule(Module &M);
38
39     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40       AU.setPreservesAll();
41     }
42   };
43   RegisterOpt<StripSymbols> X("strip", "Strip all symbols from a module");
44 }
45
46 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
47   return new StripSymbols(OnlyDebugInfo);
48 }
49
50
51 bool StripSymbols::runOnModule(Module &M) {
52   // If we're not just stripping debug info, strip all symbols from the
53   // functions and the names from any internal globals.
54   if (!OnlyDebugInfo) {
55     for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
56       if (I->hasInternalLinkage())
57         I->setName("");     // Internal symbols can't participate in linkage
58
59     for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
60       if (I->hasInternalLinkage())
61         I->setName("");     // Internal symbols can't participate in linkage
62       I->getSymbolTable().strip();
63     }
64   }
65
66   // FIXME: implement stripping of debug info.
67   return true; 
68 }