blob: f99684faaf671b549304a2876f58b5a2e8e10407 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
Chris Lattner00950542001-06-06 20:29:01 +00002//
3// This file implements stripping symbols out of symbol tables.
4//
5// Specifically, this allows you to strip all of the symbols out of:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00006// * A function
7// * All functions in a module
8// * All symbols in a module (all function symbols + all module scope symbols)
Chris Lattner00950542001-06-06 20:29:01 +00009//
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"
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000019#include "llvm/Function.h"
Chris Lattner00950542001-06-06 20:29:01 +000020#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
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000046// DoSymbolStripping - Remove all symbolic information from a function
Chris Lattner00950542001-06-06 20:29:01 +000047//
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000048static bool doSymbolStripping(Function *F) {
49 return StripSymbolTable(F->getSymbolTable());
Chris Lattner00950542001-06-06 20:29:01 +000050}
51
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000052// doStripGlobalSymbols - Remove all symbolic information from all functions
53// in a module, and all module level symbols. (function names, etc...)
Chris Lattner00950542001-06-06 20:29:01 +000054//
Chris Lattnerbd0ef772002-02-26 21:46:54 +000055static bool doStripGlobalSymbols(Module *M) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000056 // Remove all symbols from functions in this module... and then strip all of
57 // the symbols in this module...
Chris Lattner00950542001-06-06 20:29:01 +000058 //
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 {
Chris Lattnerf57b8452002-04-27 06:56:12 +000063 struct SymbolStripping : public FunctionPass {
64 virtual bool runOnFunction(Function *F) {
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000065 return doSymbolStripping(F);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000066 }
Chris Lattner97e52e42002-04-28 21:27:06 +000067 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.setPreservesAll();
69 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070 };
71
72 struct FullSymbolStripping : public SymbolStripping {
73 virtual bool doInitialization(Module *M) {
74 return doStripGlobalSymbols(M);
75 }
76 };
77}
78
79Pass *createSymbolStrippingPass() {
80 return new SymbolStripping();
81}
82
83Pass *createFullSymbolStrippingPass() {
84 return new FullSymbolStripping();
85}