blob: a8ec19c732ee231a8aed607aacff4636e4148715 [file] [log] [blame]
Shih-wei Liao835a7b72010-08-06 02:29:11 -07001#include "llvm/Linker.h"
2#include "llvm/LLVMContext.h"
3#include "llvm/Metadata.h"
4#include "llvm/Module.h"
5#include "llvm/Analysis/Verifier.h"
6#include "llvm/Bitcode/ReaderWriter.h"
7#include "llvm/Support/CommandLine.h"
8#include "llvm/Support/ManagedStatic.h"
9#include "llvm/Support/MemoryBuffer.h"
10#include "llvm/Support/PrettyStackTrace.h"
11#include "llvm/Support/StandardPasses.h"
12#include "llvm/Support/SystemUtils.h"
13#include "llvm/Support/raw_ostream.h"
14#include "llvm/System/Signals.h"
15#include "llvm/System/Path.h"
16#include "llvm/Target/TargetData.h"
17#include "llvm/Target/TargetMachine.h"
18#include "llvm/LinkAllVMCore.h"
19#include <memory>
20#include <string>
21#include <vector>
22
23using namespace llvm;
24
25static cl::list<std::string>
26InputFilenames(cl::Positional, cl::OneOrMore,
27 cl::desc("<input bitcode files>"));
28
29static cl::opt<std::string>
30OutputFilename("o", cl::Required, cl::desc("Override output filename"),
31 cl::value_desc("<output bitcode file>"));
32
33// GetExported - ...
34static void GetExported(NamedMDNode *N, std::vector<std::string> &Names) {
35 for (int i = 0, e = N->getNumOperands(); i != e; ++i) {
36 MDNode *exported_var = N->getOperand(i);
37 if (!exported_var) continue;
38
39 if (exported_var->getNumOperands() == 0) {
40 errs() << "Invalid RenderScript reflection data.\n";
41 abort();
42 }
43
44 MDString *name = dyn_cast<MDString>(exported_var->getOperand(0));
45 if (!name) {
46 errs() << "Invalid RenderScript reflection data.\n";
47 abort();
48 }
49
50 Names.push_back(name->getString());
51 }
52}
53
54// GetExportedGlobals - ...
55static void GetExportedGlobals(Module *M, std::vector<std::string> &Names) {
56 if (NamedMDNode *rs_export_var = M->getNamedMetadata("#rs_export_var")) {
57 GetExported(rs_export_var, Names);
58 }
59 if (NamedMDNode *rs_export_func = M->getNamedMetadata("#rs_export_func")) {
60 GetExported(rs_export_func, Names);
61 }
62}
63
64// LoadFile - Read the specified bitcode file in and return it. This routine
65// searches the link path for the specified file to try to find it...
66//
67static inline std::auto_ptr<Module> LoadFile(const char *argv0,
68 const std::string &FN,
69 LLVMContext& Context) {
70 sys::Path Filename;
71 if (!Filename.set(FN)) {
72 errs() << "Invalid file name: '" << FN << "'\n";
73 return std::auto_ptr<Module>();
74 }
75
76 std::string Err;
77 Module *Result = 0;
78
79 const std::string &FNStr = Filename.str();
80 MemoryBuffer *Buffer = MemoryBuffer::getFile(FNStr, &Err);
81 if (!Buffer) {
82 errs() << Err;
83 return std::auto_ptr<Module>();
84 }
85 Result = ParseBitcodeFile(Buffer, Context, &Err);
86 if (Result) return std::auto_ptr<Module>(Result); // Load successful!
87
88 errs() << Err;
89 return std::auto_ptr<Module>();
90}
91
92int main(int argc, char **argv) {
93 llvm_shutdown_obj X;
94
95 cl::ParseCommandLineOptions(argc, argv, "slang linker\n");
96
97 LLVMContext &Context = getGlobalContext();
98 std::auto_ptr<Module> Composite = LoadFile(argv[0], InputFilenames[0],
99 Context);
100 std::string ErrorMessage;
101 if (Composite.get() == 0) {
102 errs() << argv[0] << ": error loading file '"
103 << InputFilenames[0] << "'\n";
104 return 1;
105 }
106 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
107 std::auto_ptr<Module> M(LoadFile(argv[0], InputFilenames[i], Context));
108 if (M.get() == 0) {
109 errs() << argv[0] << ": error loading file '" <<InputFilenames[i]<< "'\n";
110 return 1;
111 }
112 if (Linker::LinkModules(Composite.get(), M.get(), &ErrorMessage)) {
113 errs() << argv[0] << ": link error in '" << InputFilenames[i]
114 << "': " << ErrorMessage << "\n";
115 return 1;
116 }
117 }
118
119 std::string ErrorInfo;
120 std::auto_ptr<raw_ostream>
121 Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
122 raw_fd_ostream::F_Binary));
123 if (!ErrorInfo.empty()) {
124 errs() << ErrorInfo << '\n';
125 return 1;
126 }
127
128 if (verifyModule(*Composite)) {
129 errs() << argv[0] << ": linked module is broken!\n";
130 return 1;
131 }
132
133 PassManager Passes;
134
135 const std::string &ModuleDataLayout = Composite.get()->getDataLayout();
136 if (!ModuleDataLayout.empty()) {
137 if (TargetData *TD = new TargetData(ModuleDataLayout))
138 Passes.add(TD);
139 }
140
141 std::vector<std::string> PublicAPINames;
142 PublicAPINames.push_back("init");
143 PublicAPINames.push_back("root");
144 GetExportedGlobals(Composite.get(), PublicAPINames);
145
146 std::vector<const char *> PublicAPINamesCStr;
147 for (int i = 0, e = PublicAPINames.size(); i != e; ++i) {
148 // errs() << "not internalizing: " << PublicAPINames[i] << '\n';
149 PublicAPINamesCStr.push_back(PublicAPINames[i].c_str());
150 }
151
152 Passes.add(createInternalizePass(PublicAPINamesCStr));
153 createStandardLTOPasses(&Passes, false, true, false);
154 Passes.run(*Composite.get());
155
156 WriteBitcodeToFile(Composite.get(), *Out);
157 return 0;
158}