bcc_strip_attr.cpp revision b10c3a7d91556ef31ce91ef018fee4722b783960
1/*
2 * Copyright 2013, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "llvm/Analysis/Verifier.h"
18#include "llvm/Bitcode/ReaderWriter.h"
19#include "llvm/IR/LLVMContext.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IRReader/IRReader.h"
22#include "llvm/Pass.h"
23#include "llvm/PassManager.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/ManagedStatic.h"
26#include "llvm/Support/PrettyStackTrace.h"
27#include "llvm/Support/Signals.h"
28#include "llvm/Support/SourceMgr.h"
29#include "llvm/Support/SystemUtils.h"
30#include "llvm/Support/ToolOutputFile.h"
31using namespace llvm;
32
33static cl::list<std::string>
34InputFilenames(cl::Positional,
35               cl::desc("<input bitcode files>"));
36
37static cl::opt<std::string>
38OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
39               cl::value_desc("filename"));
40
41static cl::opt<bool>
42OutputAssembly("S",
43               cl::desc("Write output as LLVM assembly"), cl::Hidden);
44
45namespace {
46  class StripAttributes : public ModulePass {
47  public:
48    static char ID;
49
50    StripAttributes() : ModulePass(ID) {
51    }
52
53    bool runOnFunction(Function &F) {
54      // Remove any target-cpu and/or target-features attributes from each
55      // Function or Function declaration.
56      if (F.hasFnAttribute("target-cpu") ||
57          F.hasFnAttribute("target-features")) {
58
59        AttrBuilder B;
60        B.addAttribute("target-cpu").addAttribute("target-features");
61        AttributeSet ToStrip = AttributeSet::get(F.getContext(),
62            AttributeSet::FunctionIndex, B);
63        F.removeAttributes(AttributeSet::FunctionIndex, ToStrip);
64        return true;
65      }
66      return false;
67    }
68
69    // We have to use a ModulePass, since a FunctionPass only gets run on
70    // defined Functions (and not declared Functions).
71    virtual bool runOnModule(Module &M) {
72      bool Changed = false;
73      for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
74        Changed |= runOnFunction(*I);
75      }
76      return Changed;
77    }
78  };
79
80  llvm::ModulePass * createStripAttributePass() {
81    return new StripAttributes();
82  }
83}
84
85
86char StripAttributes::ID = 0;
87static RegisterPass<StripAttributes> RPSA("StripAttributes",
88    "Strip Function Attributes Pass");
89
90
91static inline std::auto_ptr<Module> LoadFile(const char *argv0,
92                                             const std::string &FN,
93                                             LLVMContext& Context) {
94  SMDiagnostic Err;
95  Module* Result = ParseIRFile(FN, Err, Context);
96  if (Result) {
97    return std::auto_ptr<Module>(Result);   // Load successful!
98  }
99
100  Err.print(argv0, errs());
101  return std::auto_ptr<Module>();
102}
103
104
105int main(int argc, char **argv) {
106  // Print a stack trace if we signal out.
107  sys::PrintStackTraceOnErrorSignal();
108  PrettyStackTraceProgram X(argc, argv);
109
110  LLVMContext &Context = getGlobalContext();
111  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
112  cl::ParseCommandLineOptions(argc, argv, "strip function attribute pass\n");
113
114  std::string ErrorMessage;
115
116  std::auto_ptr<Module> M(LoadFile(argv[0], InputFilenames[0], Context));
117  if (M.get() == 0) {
118    errs() << argv[0] << ": error loading file '"
119           << InputFilenames[0] << "'\n";
120    return 1;
121  }
122
123  // Perform the actual function attribute stripping.
124  PassManager PM;
125  PM.add(createStripAttributePass());
126  PM.run(*M.get());
127
128  std::string ErrorInfo;
129  tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
130                       sys::fs::F_Binary);
131  if (!ErrorInfo.empty()) {
132    errs() << ErrorInfo << '\n';
133    return 1;
134  }
135
136  if (verifyModule(*M)) {
137    errs() << argv[0] << ": stripped module is broken!\n";
138    return 1;
139  }
140
141  if (OutputAssembly) {
142    Out.os() << *M;
143  } else if (!CheckBitcodeOutputToConsole(Out.os(), true)) {
144    WriteBitcodeToFile(M.get(), Out.os());
145  }
146
147  Out.keep();
148
149  return 0;
150}
151