blob: 58395c55ab83b777d962809c2f9856fc821ed864 [file] [log] [blame]
Chris Lattner62b7fd12002-04-07 20:49:59 +00001//===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
10// This file implements stripping symbols out of symbol tables.
11//
12// Specifically, this allows you to strip all of the symbols out of:
Chris Lattner62b7fd12002-04-07 20:49:59 +000013// * A function
14// * All functions in a module
15// * All symbols in a module (all function symbols + all module scope symbols)
Chris Lattner2f7c9632001-06-06 20:29:01 +000016//
17// Notice that:
18// * This pass makes code much less readable, so it should only be used in
19// situations where the 'strip' utility would be used (such as reducing
20// code size, and making it harder to reverse engineer code).
21//
22//===----------------------------------------------------------------------===//
23
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000025#include "llvm/Module.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000026#include "llvm/SymbolTable.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000027#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000028
Chris Lattner98cf1f52002-11-20 18:36:02 +000029static bool StripSymbolTable(SymbolTable &SymTab) {
Chris Lattner2f7c9632001-06-06 20:29:01 +000030 bool RemovedSymbol = false;
31
Chris Lattner98cf1f52002-11-20 18:36:02 +000032 for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end(); ++I) {
Chris Lattner7f74a562002-01-20 22:54:45 +000033 std::map<const std::string, Value *> &Plane = I->second;
Chris Lattner2f7c9632001-06-06 20:29:01 +000034
Chris Lattnerd707ec62001-09-07 16:43:50 +000035 SymbolTable::type_iterator B;
Chris Lattner2f7c9632001-06-06 20:29:01 +000036 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
Chris Lattnerd707ec62001-09-07 16:43:50 +000037 Value *V = B->second;
Chris Lattner3462ae32001-12-03 22:26:30 +000038 if (isa<Constant>(V) || isa<Type>(V))
Chris Lattner98cf1f52002-11-20 18:36:02 +000039 SymTab.type_remove(B);
Chris Lattnerd707ec62001-09-07 16:43:50 +000040 else
Chris Lattner98cf1f52002-11-20 18:36:02 +000041 V->setName("", &SymTab); // Set name to "", removing from symbol table!
Chris Lattner2f7c9632001-06-06 20:29:01 +000042 RemovedSymbol = true;
Chris Lattnerd707ec62001-09-07 16:43:50 +000043 assert(Plane.begin() != B && "Symbol not removed from table!");
Chris Lattner2f7c9632001-06-06 20:29:01 +000044 }
45 }
46
47 return RemovedSymbol;
48}
49
Chris Lattner04805fa2002-02-26 21:46:54 +000050namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +000051 struct SymbolStripping : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +000052 virtual bool runOnFunction(Function &F) {
53 return StripSymbolTable(F.getSymbolTable());
Chris Lattner04805fa2002-02-26 21:46:54 +000054 }
Chris Lattnerf12cc842002-04-28 21:27:06 +000055 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.setPreservesAll();
57 }
Chris Lattner04805fa2002-02-26 21:46:54 +000058 };
Chris Lattnerc8b70922002-07-26 21:12:46 +000059 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
Chris Lattner04805fa2002-02-26 21:46:54 +000060
61 struct FullSymbolStripping : public SymbolStripping {
Chris Lattner113f4f42002-06-25 16:13:24 +000062 virtual bool doInitialization(Module &M) {
63 return StripSymbolTable(M.getSymbolTable());
Chris Lattner04805fa2002-02-26 21:46:54 +000064 }
65 };
Chris Lattnerc8b70922002-07-26 21:12:46 +000066 RegisterOpt<FullSymbolStripping> Y("mstrip",
67 "Strip symbols from module and functions");
Chris Lattner04805fa2002-02-26 21:46:54 +000068}
69
70Pass *createSymbolStrippingPass() {
71 return new SymbolStripping();
72}
73
74Pass *createFullSymbolStrippingPass() {
75 return new FullSymbolStripping();
76}