1 #include "llvm/ADT/OwningPtr.h" 2 #include "llvm/Bitcode/ReaderWriter.h" 3 #include "llvm/IR/Function.h" 4 #include "llvm/IR/GlobalVariable.h" 5 #include "llvm/IR/LLVMContext.h" 6 #include "llvm/IR/Module.h" 7 #include "llvm/Support/CommandLine.h" 8 #include "llvm/Support/ManagedStatic.h" 9 #include "llvm/Support/MemoryBuffer.h" 10 #include "llvm/Support/raw_ostream.h" 11 #include "llvm/Support/system_error.h" 12 #include "llvm/Support/ToolOutputFile.h" 13 #include "llvm/Config/config.h" 14 15 using namespace llvm; 16 17 static cl::opt<std::string> 18 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); 19 20 static cl::opt<std::string> 21 OutputFilename("o", cl::desc("Output filename"), 22 cl::value_desc("filename")); 23 24 int main(int argc, char **argv) { 25 LLVMContext &Context = getGlobalContext(); 26 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. 27 28 cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n"); 29 30 std::string ErrorMessage; 31 std::auto_ptr<Module> M; 32 33 { 34 OwningPtr<MemoryBuffer> BufferPtr; 35 if (error_code ec = MemoryBuffer::getFileOrSTDIN(InputFilename, BufferPtr)) 36 ErrorMessage = ec.message(); 37 else 38 M.reset(ParseBitcodeFile(BufferPtr.get(), Context, &ErrorMessage)); 39 } 40 41 if (M.get() == 0) { 42 errs() << argv[0] << ": "; 43 if (ErrorMessage.size()) 44 errs() << ErrorMessage << "\n"; 45 else 46 errs() << "bitcode didn't read correctly.\n"; 47 return 1; 48 } 49 50 // Set linkage of every external definition to linkonce_odr. 51 for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) { 52 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage) 53 i->setLinkage(GlobalValue::LinkOnceODRLinkage); 54 } 55 56 for (Module::global_iterator i = M->global_begin(), e = M->global_end(); 57 i != e; ++i) { 58 if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage) 59 i->setLinkage(GlobalValue::LinkOnceODRLinkage); 60 } 61 62 if (OutputFilename.empty()) { 63 errs() << "no output file\n"; 64 return 1; 65 } 66 67 std::string ErrorInfo; 68 OwningPtr<tool_output_file> Out 69 (new tool_output_file(OutputFilename.c_str(), ErrorInfo, 70 #if LLVM_VERSION_MAJOR > 3 || (LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR > 3) 71 sys::fs::F_Binary)); 72 #else 73 raw_fd_ostream::F_Binary)); 74 #endif 75 if (!ErrorInfo.empty()) { 76 errs() << ErrorInfo << '\n'; 77 exit(1); 78 } 79 80 WriteBitcodeToFile(M.get(), Out->os()); 81 82 // Declare success. 83 Out->keep(); 84 return 0; 85 } 86 87