blob: c43a2cb76e0e44f52f6fd6db9d2de737aed22562 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
John Criswellb576c942003-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 Lattner00950542001-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 Lattner2fbfdcf2002-04-07 20:49:59 +000013// * All functions in a module
Chris Lattner2cacc0a2004-01-10 21:36:49 +000014// * All non-essential symbols in a module (all function symbols + all module
15// scope symbols)
Chris Lattner00950542001-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 Lattner022103b2002-05-07 20:03:00 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include "llvm/SymbolTable.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000027#include "llvm/Pass.h"
Chris Lattner35bb52f2003-11-22 01:29:35 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner6e6026b2002-11-20 18:36:02 +000030static bool StripSymbolTable(SymbolTable &SymTab) {
Chris Lattner00950542001-06-06 20:29:01 +000031 bool RemovedSymbol = false;
32
Chris Lattner2cacc0a2004-01-10 21:36:49 +000033 for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end();) {
34 // Removing items from the plane can cause the plane itself to get deleted.
35 // If this happens, make sure we incremented our plane iterator already!
36 std::map<const std::string, Value *> &Plane = (I++)->second;
Chris Lattner00950542001-06-06 20:29:01 +000037
Chris Lattner2cacc0a2004-01-10 21:36:49 +000038 SymbolTable::type_iterator B = Plane.begin(), Bend = Plane.end();
39 while (B != Bend) { // Found nonempty type plane!
Chris Lattner13b1f0c2001-09-07 16:43:50 +000040 Value *V = B->second;
Chris Lattner2cacc0a2004-01-10 21:36:49 +000041
Chris Lattner35bb52f2003-11-22 01:29:35 +000042 if (isa<Constant>(V) || isa<Type>(V)) {
43 SymTab.type_remove(B++);
44 RemovedSymbol = true;
45 } else {
46 ++B;
47 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()){
48 // Set name to "", removing from symbol table!
49 V->setName("", &SymTab);
50 RemovedSymbol = true;
51 }
52 }
Chris Lattner00950542001-06-06 20:29:01 +000053 }
54 }
55
56 return RemovedSymbol;
57}
58
Chris Lattnerbd0ef772002-02-26 21:46:54 +000059namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000060 struct SymbolStripping : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000061 virtual bool runOnFunction(Function &F) {
62 return StripSymbolTable(F.getSymbolTable());
Chris Lattnerbd0ef772002-02-26 21:46:54 +000063 }
Chris Lattner97e52e42002-04-28 21:27:06 +000064 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.setPreservesAll();
66 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000067 };
Chris Lattnera6275cc2002-07-26 21:12:46 +000068 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000069
70 struct FullSymbolStripping : public SymbolStripping {
Chris Lattner7e708292002-06-25 16:13:24 +000071 virtual bool doInitialization(Module &M) {
72 return StripSymbolTable(M.getSymbolTable());
Chris Lattnerbd0ef772002-02-26 21:46:54 +000073 }
74 };
Chris Lattnera6275cc2002-07-26 21:12:46 +000075 RegisterOpt<FullSymbolStripping> Y("mstrip",
76 "Strip symbols from module and functions");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000077}
78
Chris Lattner35bb52f2003-11-22 01:29:35 +000079Pass *llvm::createSymbolStrippingPass() {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000080 return new SymbolStripping();
81}
82
Chris Lattner35bb52f2003-11-22 01:29:35 +000083Pass *llvm::createFullSymbolStrippingPass() {
Chris Lattnerbd0ef772002-02-26 21:46:54 +000084 return new FullSymbolStripping();
85}