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