• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Bitcode/ReaderWriter.h"
18 #include "llvm/IR/LLVMContext.h"
19 #include "llvm/IR/LegacyPassManager.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/Verifier.h"
22 #include "llvm/IRReader/IRReader.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/ManagedStatic.h"
27 #include "llvm/Support/PrettyStackTrace.h"
28 #include "llvm/Support/Signals.h"
29 #include "llvm/Support/SourceMgr.h"
30 #include "llvm/Support/SystemUtils.h"
31 #include "llvm/Support/ToolOutputFile.h"
32 using namespace llvm;
33 
34 static cl::list<std::string>
35 InputFilenames(cl::Positional,
36                cl::desc("<input bitcode files>"));
37 
38 static cl::opt<std::string>
39 OutputFilename("o", cl::desc("Override output filename"), cl::init("-"),
40                cl::value_desc("filename"));
41 
42 static cl::opt<bool>
43 OutputAssembly("S",
44                cl::desc("Write output as LLVM assembly"), cl::Hidden);
45 
46 namespace {
47   class StripAttributes : public ModulePass {
48   public:
49     static char ID;
50 
StripAttributes()51     StripAttributes() : ModulePass(ID) {
52     }
53 
getAnalysisUsage(llvm::AnalysisUsage & AU) const54     virtual void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
55       AU.setPreservesAll();
56     }
57 
runOnFunction(Function & F)58     bool runOnFunction(Function &F) {
59       // Remove any target-cpu and/or target-features attributes from each
60       // Function or Function declaration.
61       if (F.hasFnAttribute("target-cpu") ||
62           F.hasFnAttribute("target-features")) {
63 
64         AttrBuilder B;
65         B.addAttribute("target-cpu").addAttribute("target-features");
66         AttributeSet ToStrip = AttributeSet::get(F.getContext(),
67             AttributeSet::FunctionIndex, B);
68         F.removeAttributes(AttributeSet::FunctionIndex, ToStrip);
69         return true;
70       }
71       return false;
72     }
73 
74     // We have to use a ModulePass, since a FunctionPass only gets run on
75     // defined Functions (and not declared Functions).
runOnModule(Module & M)76     virtual bool runOnModule(Module &M) {
77       bool Changed = false;
78       for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
79         Changed |= runOnFunction(*I);
80       }
81       return Changed;
82     }
83   };
84 
createStripAttributePass()85   llvm::ModulePass * createStripAttributePass() {
86     return new StripAttributes();
87   }
88 }
89 
90 
91 char StripAttributes::ID = 0;
92 static RegisterPass<StripAttributes> RPSA("StripAttributes",
93     "Strip Function Attributes Pass");
94 
95 
LoadFile(const char * argv0,const std::string & FN,LLVMContext & Context)96 static inline std::unique_ptr<Module> LoadFile(const char *argv0,
97                                                const std::string &FN,
98                                                LLVMContext& Context) {
99   SMDiagnostic Err;
100   std::unique_ptr<Module> Result = parseIRFile(FN, Err, Context);
101   if (Result) {
102     return Result;   // Load successful!
103   }
104 
105   Err.print(argv0, errs());
106   return std::unique_ptr<Module>();
107 }
108 
109 
main(int argc,char ** argv)110 int main(int argc, char **argv) {
111   // Print a stack trace if we signal out.
112   sys::PrintStackTraceOnErrorSignal(argv[0]);
113   PrettyStackTraceProgram X(argc, argv);
114 
115   LLVMContext Context;
116   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
117   cl::ParseCommandLineOptions(argc, argv, "strip function attribute pass\n");
118 
119   std::string ErrorMessage;
120 
121   std::unique_ptr<Module> M(LoadFile(argv[0], InputFilenames[0], Context));
122   if (M.get() == 0) {
123     errs() << argv[0] << ": error loading file '"
124            << InputFilenames[0] << "'\n";
125     return 1;
126   }
127 
128   // Perform the actual function attribute stripping.
129   legacy::PassManager PM;
130   PM.add(createStripAttributePass());
131   PM.run(*M.get());
132 
133   std::error_code EC;
134   tool_output_file Out(OutputFilename.c_str(), EC,
135                        sys::fs::F_None);
136   if (EC) {
137     errs() << EC.message() << '\n';
138     return 1;
139   }
140 
141   if (verifyModule(*M)) {
142     errs() << argv[0] << ": stripped module is broken!\n";
143     return 1;
144   }
145 
146   if (OutputAssembly) {
147     Out.os() << *M;
148   } else if (!CheckBitcodeOutputToConsole(Out.os(), true)) {
149     WriteBitcodeToFile(M.get(), Out.os());
150   }
151 
152   Out.keep();
153 
154   return 0;
155 }
156