blob: 99e596e8f2afe5abc31d80bfafe901373e4d1921 [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 Lattner022103b2002-05-07 20:03:00 +000017#include "llvm/Transforms/Scalar.h"
Chris Lattner00950542001-06-06 20:29:01 +000018#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000019#include "llvm/SymbolTable.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000020#include "llvm/Pass.h"
Chris Lattner00950542001-06-06 20:29:01 +000021
Chris Lattner6e6026b2002-11-20 18:36:02 +000022static bool StripSymbolTable(SymbolTable &SymTab) {
Chris Lattner00950542001-06-06 20:29:01 +000023 bool RemovedSymbol = false;
24
Chris Lattner6e6026b2002-11-20 18:36:02 +000025 for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end(); ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +000026 std::map<const std::string, Value *> &Plane = I->second;
Chris Lattner00950542001-06-06 20:29:01 +000027
Chris Lattner13b1f0c2001-09-07 16:43:50 +000028 SymbolTable::type_iterator B;
Chris Lattner00950542001-06-06 20:29:01 +000029 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
Chris Lattner13b1f0c2001-09-07 16:43:50 +000030 Value *V = B->second;
Chris Lattnere9bb2df2001-12-03 22:26:30 +000031 if (isa<Constant>(V) || isa<Type>(V))
Chris Lattner6e6026b2002-11-20 18:36:02 +000032 SymTab.type_remove(B);
Chris Lattner13b1f0c2001-09-07 16:43:50 +000033 else
Chris Lattner6e6026b2002-11-20 18:36:02 +000034 V->setName("", &SymTab); // Set name to "", removing from symbol table!
Chris Lattner00950542001-06-06 20:29:01 +000035 RemovedSymbol = true;
Chris Lattner13b1f0c2001-09-07 16:43:50 +000036 assert(Plane.begin() != B && "Symbol not removed from table!");
Chris Lattner00950542001-06-06 20:29:01 +000037 }
38 }
39
40 return RemovedSymbol;
41}
42
Chris Lattnerbd0ef772002-02-26 21:46:54 +000043namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000044 struct SymbolStripping : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000045 virtual bool runOnFunction(Function &F) {
46 return StripSymbolTable(F.getSymbolTable());
Chris Lattnerbd0ef772002-02-26 21:46:54 +000047 }
Chris Lattner97e52e42002-04-28 21:27:06 +000048 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesAll();
50 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000051 };
Chris Lattnera6275cc2002-07-26 21:12:46 +000052 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000053
54 struct FullSymbolStripping : public SymbolStripping {
Chris Lattner7e708292002-06-25 16:13:24 +000055 virtual bool doInitialization(Module &M) {
56 return StripSymbolTable(M.getSymbolTable());
Chris Lattnerbd0ef772002-02-26 21:46:54 +000057 }
58 };
Chris Lattnera6275cc2002-07-26 21:12:46 +000059 RegisterOpt<FullSymbolStripping> Y("mstrip",
60 "Strip symbols from module and functions");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061}
62
63Pass *createSymbolStrippingPass() {
64 return new SymbolStripping();
65}
66
67Pass *createFullSymbolStrippingPass() {
68 return new FullSymbolStripping();
69}