2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-06-27 02:20:32 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2014-05-01 02:35:20 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-01-21 04:28:48 +08:00
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
2014-08-23 06:24:28 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2012-01-09 06:09:58 +08:00
|
|
|
|
2014-06-21 17:20:31 +08:00
|
|
|
#include <system_error>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2012-01-09 06:09:58 +08:00
|
|
|
static cl::opt<std::string>
|
|
|
|
InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OutputFilename("o", cl::desc("Output filename"),
|
|
|
|
cl::value_desc("filename"));
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2016-04-15 22:18:58 +08:00
|
|
|
LLVMContext Context;
|
2012-01-09 06:09:58 +08:00
|
|
|
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
|
|
|
|
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "libclc builtin preparation tool\n");
|
|
|
|
|
|
|
|
std::string ErrorMessage;
|
2015-06-27 20:35:54 +08:00
|
|
|
Module *M = nullptr;
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
{
|
2014-07-08 01:46:45 +08:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
|
|
|
|
MemoryBuffer::getFile(InputFilename);
|
|
|
|
std::unique_ptr<MemoryBuffer> &BufferPtr = BufferOrErr.get();
|
|
|
|
if (std::error_code ec = BufferOrErr.getError())
|
2012-01-09 06:09:58 +08:00
|
|
|
ErrorMessage = ec.message();
|
2014-01-21 04:28:48 +08:00
|
|
|
else {
|
2015-08-07 16:31:37 +08:00
|
|
|
ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
|
2015-06-25 01:03:50 +08:00
|
|
|
parseBitcodeFile(BufferPtr.get()->getMemBufferRef(), Context);
|
2014-12-31 23:27:53 +08:00
|
|
|
if (std::error_code ec = ModuleOrErr.getError())
|
2014-01-21 04:28:48 +08:00
|
|
|
ErrorMessage = ec.message();
|
2015-08-07 16:31:37 +08:00
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
M = ModuleOrErr.get().release();
|
2014-01-21 04:28:48 +08:00
|
|
|
}
|
2012-01-09 06:09:58 +08:00
|
|
|
}
|
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
if (!M) {
|
2012-01-09 06:09:58 +08:00
|
|
|
errs() << argv[0] << ": ";
|
|
|
|
if (ErrorMessage.size())
|
|
|
|
errs() << ErrorMessage << "\n";
|
|
|
|
else
|
|
|
|
errs() << "bitcode didn't read correctly.\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set linkage of every external definition to linkonce_odr.
|
|
|
|
for (Module::iterator i = M->begin(), e = M->end(); i != e; ++i) {
|
|
|
|
if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
|
|
|
|
i->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::global_iterator i = M->global_begin(), e = M->global_end();
|
|
|
|
i != e; ++i) {
|
|
|
|
if (!i->isDeclaration() && i->getLinkage() == GlobalValue::ExternalLinkage)
|
|
|
|
i->setLinkage(GlobalValue::LinkOnceODRLinkage);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OutputFilename.empty()) {
|
|
|
|
errs() << "no output file\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-08-28 14:19:33 +08:00
|
|
|
std::error_code EC;
|
2014-12-31 23:27:53 +08:00
|
|
|
std::unique_ptr<tool_output_file> Out
|
2014-08-28 14:19:33 +08:00
|
|
|
(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
|
|
|
|
if (EC) {
|
|
|
|
errs() << EC.message() << '\n';
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-09 06:09:58 +08:00
|
|
|
|
2015-06-25 01:03:50 +08:00
|
|
|
WriteBitcodeToFile(M, Out->os());
|
2012-01-09 06:09:58 +08:00
|
|
|
|
|
|
|
// Declare success.
|
|
|
|
Out->keep();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|