blob: bb4f01c90b5f7a0e52a739b33f0dff4fbd68f779 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===- SymbolStripping.cpp - Code to string symbols for methods and modules -=//
2//
3// This file implements stripping symbols out of symbol tables.
4//
5// Specifically, this allows you to strip all of the symbols out of:
6// * A method
7// * All methods in a module
8// * All symbols in a module (all method symbols + all module scope symbols)
9//
10// Notice that:
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).
14//
15//===----------------------------------------------------------------------===//
16
Chris Lattner7e02b7e2001-06-30 04:36:40 +000017#include "llvm/Optimizations/AllOpts.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
19#include "llvm/Method.h"
20#include "llvm/SymbolTable.h"
Chris Lattner00950542001-06-06 20:29:01 +000021
22static bool StripSymbolTable(SymbolTable *SymTab) {
23 if (SymTab == 0) return false; // No symbol table? No problem.
24 bool RemovedSymbol = false;
25
Chris Lattner7fc9fe32001-06-27 23:41:11 +000026 for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
Chris Lattner00950542001-06-06 20:29:01 +000027 map<const string, Value *> &Plane = I->second;
28
Chris Lattner13b1f0c2001-09-07 16:43:50 +000029 SymbolTable::type_iterator B;
Chris Lattner00950542001-06-06 20:29:01 +000030 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
Chris Lattner13b1f0c2001-09-07 16:43:50 +000031 Value *V = B->second;
Chris Lattnere9bb2df2001-12-03 22:26:30 +000032 if (isa<Constant>(V) || isa<Type>(V))
Chris Lattner13b1f0c2001-09-07 16:43:50 +000033 SymTab->type_remove(B);
34 else
35 V->setName("", SymTab); // Set name to "", removing from symbol table!
Chris Lattner00950542001-06-06 20:29:01 +000036 RemovedSymbol = true;
Chris Lattner13b1f0c2001-09-07 16:43:50 +000037 assert(Plane.begin() != B && "Symbol not removed from table!");
Chris Lattner00950542001-06-06 20:29:01 +000038 }
39 }
40
41 return RemovedSymbol;
42}
43
44
45// DoSymbolStripping - Remove all symbolic information from a method
46//
Chris Lattner5680ee62001-10-18 01:32:34 +000047bool opt::SymbolStripping::doSymbolStripping(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +000048 return StripSymbolTable(M->getSymbolTable());
49}
50
Chris Lattner5680ee62001-10-18 01:32:34 +000051// doStripGlobalSymbols - Remove all symbolic information from all methods
Chris Lattner00950542001-06-06 20:29:01 +000052// in a module, and all module level symbols. (method names, etc...)
53//
Chris Lattner5680ee62001-10-18 01:32:34 +000054bool opt::FullSymbolStripping::doStripGlobalSymbols(Module *M) {
Chris Lattner00950542001-06-06 20:29:01 +000055 // Remove all symbols from methods in this module... and then strip all of the
56 // symbols in this module...
57 //
Chris Lattner5680ee62001-10-18 01:32:34 +000058 return StripSymbolTable(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000059}