blob: 8502082a15dc5ead3f4aab143ffa90f9589dc0f4 [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 Lattner59b6b8e2002-01-21 23:17:48 +000017#include "llvm/Transforms/SymbolStripping.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
19#include "llvm/Method.h"
20#include "llvm/SymbolTable.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000021#include "llvm/Pass.h"
Chris Lattner00950542001-06-06 20:29:01 +000022
23static bool StripSymbolTable(SymbolTable *SymTab) {
24 if (SymTab == 0) return false; // No symbol table? No problem.
25 bool RemovedSymbol = false;
26
Chris Lattner7fc9fe32001-06-27 23:41:11 +000027 for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +000028 std::map<const std::string, Value *> &Plane = I->second;
Chris Lattner00950542001-06-06 20:29:01 +000029
Chris Lattner13b1f0c2001-09-07 16:43:50 +000030 SymbolTable::type_iterator B;
Chris Lattner00950542001-06-06 20:29:01 +000031 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
Chris Lattner13b1f0c2001-09-07 16:43:50 +000032 Value *V = B->second;
Chris Lattnere9bb2df2001-12-03 22:26:30 +000033 if (isa<Constant>(V) || isa<Type>(V))
Chris Lattner13b1f0c2001-09-07 16:43:50 +000034 SymTab->type_remove(B);
35 else
36 V->setName("", SymTab); // Set name to "", removing from symbol table!
Chris Lattner00950542001-06-06 20:29:01 +000037 RemovedSymbol = true;
Chris Lattner13b1f0c2001-09-07 16:43:50 +000038 assert(Plane.begin() != B && "Symbol not removed from table!");
Chris Lattner00950542001-06-06 20:29:01 +000039 }
40 }
41
42 return RemovedSymbol;
43}
44
45
46// DoSymbolStripping - Remove all symbolic information from a method
47//
Chris Lattnerbd0ef772002-02-26 21:46:54 +000048static bool doSymbolStripping(Method *M) {
Chris Lattner00950542001-06-06 20:29:01 +000049 return StripSymbolTable(M->getSymbolTable());
50}
51
Chris Lattner5680ee62001-10-18 01:32:34 +000052// doStripGlobalSymbols - Remove all symbolic information from all methods
Chris Lattner00950542001-06-06 20:29:01 +000053// in a module, and all module level symbols. (method names, etc...)
54//
Chris Lattnerbd0ef772002-02-26 21:46:54 +000055static bool doStripGlobalSymbols(Module *M) {
Chris Lattner00950542001-06-06 20:29:01 +000056 // Remove all symbols from methods in this module... and then strip all of the
57 // symbols in this module...
58 //
Chris Lattner5680ee62001-10-18 01:32:34 +000059 return StripSymbolTable(M->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000060}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061
62namespace {
63 struct SymbolStripping : public MethodPass {
64 virtual bool runOnMethod(Method *M) {
65 return doSymbolStripping(M);
66 }
67 };
68
69 struct FullSymbolStripping : public SymbolStripping {
70 virtual bool doInitialization(Module *M) {
71 return doStripGlobalSymbols(M);
72 }
73 };
74}
75
76Pass *createSymbolStrippingPass() {
77 return new SymbolStripping();
78}
79
80Pass *createFullSymbolStrippingPass() {
81 return new FullSymbolStripping();
82}