• 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/Module.h"
20 #include "llvm/IR/Verifier.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/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 
runOnFunction(Function & F)54     bool runOnFunction(Function &F) {
55       // Remove any target-cpu and/or target-features attributes from each
56       // Function or Function declaration.
57       if (F.hasFnAttribute("target-cpu") ||
58           F.hasFnAttribute("target-features")) {
59 
60         AttrBuilder B;
61         B.addAttribute("target-cpu").addAttribute("target-features");
62         AttributeSet ToStrip = AttributeSet::get(F.getContext(),
63             AttributeSet::FunctionIndex, B);
64         F.removeAttributes(AttributeSet::FunctionIndex, ToStrip);
65         return true;
66       }
67       return false;
68     }
69 
70     // We have to use a ModulePass, since a FunctionPass only gets run on
71     // defined Functions (and not declared Functions).
runOnModule(Module & M)72     virtual bool runOnModule(Module &M) {
73       bool Changed = false;
74       for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
75         Changed |= runOnFunction(*I);
76       }
77       return Changed;
78     }
79   };
80 
createStripAttributePass()81   llvm::ModulePass * createStripAttributePass() {
82     return new StripAttributes();
83   }
84 }
85 
86 
87 char StripAttributes::ID = 0;
88 static RegisterPass<StripAttributes> RPSA("StripAttributes",
89     "Strip Function Attributes Pass");
90 
91 
LoadFile(const char * argv0,const std::string & FN,LLVMContext & Context)92 static inline std::unique_ptr<Module> LoadFile(const char *argv0,
93                                                const std::string &FN,
94                                                LLVMContext& Context) {
95   SMDiagnostic Err;
96   Module* Result = ParseIRFile(FN, Err, Context);
97   if (Result) {
98     return std::unique_ptr<Module>(Result);   // Load successful!
99   }
100 
101   Err.print(argv0, errs());
102   return std::unique_ptr<Module>();
103 }
104 
105 
main(int argc,char ** argv)106 int main(int argc, char **argv) {
107   // Print a stack trace if we signal out.
108   sys::PrintStackTraceOnErrorSignal();
109   PrettyStackTraceProgram X(argc, argv);
110 
111   LLVMContext &Context = getGlobalContext();
112   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
113   cl::ParseCommandLineOptions(argc, argv, "strip function attribute pass\n");
114 
115   std::string ErrorMessage;
116 
117   std::unique_ptr<Module> M(LoadFile(argv[0], InputFilenames[0], Context));
118   if (M.get() == 0) {
119     errs() << argv[0] << ": error loading file '"
120            << InputFilenames[0] << "'\n";
121     return 1;
122   }
123 
124   // Perform the actual function attribute stripping.
125   PassManager PM;
126   PM.add(createStripAttributePass());
127   PM.run(*M.get());
128 
129   std::string ErrorInfo;
130   tool_output_file Out(OutputFilename.c_str(), ErrorInfo,
131                        sys::fs::F_None);
132   if (!ErrorInfo.empty()) {
133     errs() << ErrorInfo << '\n';
134     return 1;
135   }
136 
137   if (verifyModule(*M)) {
138     errs() << argv[0] << ": stripped module is broken!\n";
139     return 1;
140   }
141 
142   if (OutputAssembly) {
143     Out.os() << *M;
144   } else if (!CheckBitcodeOutputToConsole(Out.os(), true)) {
145     WriteBitcodeToFile(M.get(), Out.os());
146   }
147 
148   Out.keep();
149 
150   return 0;
151 }
152