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