Avoid duplicating logic on frame register selecting when lowering frameaddr
[oota-llvm.git] / tools / gold / gold-plugin.cpp
1 //===-- gold-plugin.cpp - Plugin to gold for Link Time Optimization  ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is a gold plugin for LLVM. It provides an LLVM implementation of the
11 // interface described in http://gcc.gnu.org/wiki/whopr/driver .
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H
16 #include "plugin-api.h"
17 #include "llvm-c/lto.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/Support/Errno.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Support/ToolOutputFile.h"
24 #include "llvm/Support/system_error.h"
25 #include <cerrno>
26 #include <cstdlib>
27 #include <cstring>
28 #include <fstream>
29 #include <list>
30 #include <vector>
31
32 // Support Windows/MinGW crazyness.
33 #ifdef _WIN32
34 # include <io.h>
35 # define lseek _lseek
36 # define read _read
37 #endif
38
39 using namespace llvm;
40
41 namespace {
42   ld_plugin_status discard_message(int level, const char *format, ...) {
43     // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
44     // callback in the transfer vector. This should never be called.
45     abort();
46   }
47
48   ld_plugin_add_symbols add_symbols = NULL;
49   ld_plugin_get_symbols get_symbols = NULL;
50   ld_plugin_add_input_file add_input_file = NULL;
51   ld_plugin_add_input_library add_input_library = NULL;
52   ld_plugin_set_extra_library_path set_extra_library_path = NULL;
53   ld_plugin_get_view get_view = NULL;
54   ld_plugin_message message = discard_message;
55
56   int api_version = 0;
57   int gold_version = 0;
58
59   struct claimed_file {
60     void *handle;
61     std::vector<ld_plugin_symbol> syms;
62   };
63
64   lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
65   std::string output_name = "";
66   std::list<claimed_file> Modules;
67   std::vector<sys::Path> Cleanup;
68   lto_code_gen_t code_gen = NULL;
69 }
70
71 namespace options {
72   enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
73   static bool generate_api_file = false;
74   static generate_bc generate_bc_file = BC_NO;
75   static std::string bc_path;
76   static std::string obj_path;
77   static std::string extra_library_path;
78   static std::string triple;
79   static std::string mcpu;
80   // Additional options to pass into the code generator.
81   // Note: This array will contain all plugin options which are not claimed
82   // as plugin exclusive to pass to the code generator.
83   // For example, "generate-api-file" and "as"options are for the plugin
84   // use only and will not be passed.
85   static std::vector<std::string> extra;
86
87   static void process_plugin_option(const char* opt_)
88   {
89     if (opt_ == NULL)
90       return;
91     llvm::StringRef opt = opt_;
92
93     if (opt == "generate-api-file") {
94       generate_api_file = true;
95     } else if (opt.startswith("mcpu=")) {
96       mcpu = opt.substr(strlen("mcpu="));
97     } else if (opt.startswith("extra-library-path=")) {
98       extra_library_path = opt.substr(strlen("extra_library_path="));
99     } else if (opt.startswith("mtriple=")) {
100       triple = opt.substr(strlen("mtriple="));
101     } else if (opt.startswith("obj-path=")) {
102       obj_path = opt.substr(strlen("obj-path="));
103     } else if (opt == "emit-llvm") {
104       generate_bc_file = BC_ONLY;
105     } else if (opt == "also-emit-llvm") {
106       generate_bc_file = BC_ALSO;
107     } else if (opt.startswith("also-emit-llvm=")) {
108       llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
109       generate_bc_file = BC_ALSO;
110       if (!bc_path.empty()) {
111         (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
112                    "Discarding %s", opt_);
113       } else {
114         bc_path = path;
115       }
116     } else {
117       // Save this option to pass to the code generator.
118       extra.push_back(opt);
119     }
120   }
121 }
122
123 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
124                                         int *claimed);
125 static ld_plugin_status all_symbols_read_hook(void);
126 static ld_plugin_status cleanup_hook(void);
127
128 extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
129 ld_plugin_status onload(ld_plugin_tv *tv) {
130   // We're given a pointer to the first transfer vector. We read through them
131   // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
132   // contain pointers to functions that we need to call to register our own
133   // hooks. The others are addresses of functions we can use to call into gold
134   // for services.
135
136   bool registeredClaimFile = false;
137
138   for (; tv->tv_tag != LDPT_NULL; ++tv) {
139     switch (tv->tv_tag) {
140       case LDPT_API_VERSION:
141         api_version = tv->tv_u.tv_val;
142         break;
143       case LDPT_GOLD_VERSION:  // major * 100 + minor
144         gold_version = tv->tv_u.tv_val;
145         break;
146       case LDPT_OUTPUT_NAME:
147         output_name = tv->tv_u.tv_string;
148         break;
149       case LDPT_LINKER_OUTPUT:
150         switch (tv->tv_u.tv_val) {
151           case LDPO_REL:  // .o
152           case LDPO_DYN:  // .so
153           // FIXME: Replace 3 with LDPO_PIE once that is in a released binutils.
154           case 3: // position independent executable
155             output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
156             break;
157           case LDPO_EXEC:  // .exe
158             output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
159             break;
160           default:
161             (*message)(LDPL_ERROR, "Unknown output file type %d",
162                        tv->tv_u.tv_val);
163             return LDPS_ERR;
164         }
165         // TODO: add an option to disable PIC.
166         //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
167         break;
168       case LDPT_OPTION:
169         options::process_plugin_option(tv->tv_u.tv_string);
170         break;
171       case LDPT_REGISTER_CLAIM_FILE_HOOK: {
172         ld_plugin_register_claim_file callback;
173         callback = tv->tv_u.tv_register_claim_file;
174
175         if ((*callback)(claim_file_hook) != LDPS_OK)
176           return LDPS_ERR;
177
178         registeredClaimFile = true;
179       } break;
180       case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
181         ld_plugin_register_all_symbols_read callback;
182         callback = tv->tv_u.tv_register_all_symbols_read;
183
184         if ((*callback)(all_symbols_read_hook) != LDPS_OK)
185           return LDPS_ERR;
186
187         code_gen = lto_codegen_create();
188       } break;
189       case LDPT_REGISTER_CLEANUP_HOOK: {
190         ld_plugin_register_cleanup callback;
191         callback = tv->tv_u.tv_register_cleanup;
192
193         if ((*callback)(cleanup_hook) != LDPS_OK)
194           return LDPS_ERR;
195       } break;
196       case LDPT_ADD_SYMBOLS:
197         add_symbols = tv->tv_u.tv_add_symbols;
198         break;
199       case LDPT_GET_SYMBOLS:
200         get_symbols = tv->tv_u.tv_get_symbols;
201         break;
202       case LDPT_ADD_INPUT_FILE:
203         add_input_file = tv->tv_u.tv_add_input_file;
204         break;
205       case LDPT_ADD_INPUT_LIBRARY:
206         add_input_library = tv->tv_u.tv_add_input_file;
207         break;
208       case LDPT_SET_EXTRA_LIBRARY_PATH:
209         set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
210         break;
211       case LDPT_GET_VIEW:
212         get_view = tv->tv_u.tv_get_view;
213         break;
214       case LDPT_MESSAGE:
215         message = tv->tv_u.tv_message;
216         break;
217       default:
218         break;
219     }
220   }
221
222   if (!registeredClaimFile) {
223     (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
224     return LDPS_ERR;
225   }
226   if (!add_symbols) {
227     (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
228     return LDPS_ERR;
229   }
230
231   return LDPS_OK;
232 }
233
234 /// claim_file_hook - called by gold to see whether this file is one that
235 /// our plugin can handle. We'll try to open it and register all the symbols
236 /// with add_symbol if possible.
237 static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
238                                         int *claimed) {
239   lto_module_t M;
240   const void *view;
241   OwningPtr<MemoryBuffer> buffer;
242   if (get_view) {
243     if (get_view(file->handle, &view) != LDPS_OK) {
244       (*message)(LDPL_ERROR, "Failed to get a view of %s", file->name);
245       return LDPS_ERR;
246     }
247   } else {
248     int64_t offset = 0;
249     // Gold has found what might be IR part-way inside of a file, such as
250     // an .a archive.
251     if (file->offset) {
252       offset = file->offset;
253     }
254     if (error_code ec =
255         MemoryBuffer::getOpenFile(file->fd, file->name, buffer, file->filesize,
256                                   -1, offset, false)) {
257       (*message)(LDPL_ERROR, ec.message().c_str());
258       return LDPS_ERR;
259     }
260     view = buffer->getBufferStart();
261   }
262
263   if (!lto_module_is_object_file_in_memory(view, file->filesize))
264     return LDPS_OK;
265
266   M = lto_module_create_from_memory(view, file->filesize);
267   if (!M) {
268     if (const char* msg = lto_get_error_message()) {
269       (*message)(LDPL_ERROR,
270                  "LLVM gold plugin has failed to create LTO module: %s",
271                  msg);
272       return LDPS_ERR;
273     }
274     return LDPS_OK;
275   }
276
277   *claimed = 1;
278   Modules.resize(Modules.size() + 1);
279   claimed_file &cf = Modules.back();
280
281   if (!options::triple.empty())
282     lto_module_set_target_triple(M, options::triple.c_str());
283
284   cf.handle = file->handle;
285   unsigned sym_count = lto_module_get_num_symbols(M);
286   cf.syms.reserve(sym_count);
287
288   for (unsigned i = 0; i != sym_count; ++i) {
289     lto_symbol_attributes attrs = lto_module_get_symbol_attribute(M, i);
290     if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
291       continue;
292
293     cf.syms.push_back(ld_plugin_symbol());
294     ld_plugin_symbol &sym = cf.syms.back();
295     sym.name = const_cast<char *>(lto_module_get_symbol_name(M, i));
296     sym.name = strdup(sym.name);
297     sym.version = NULL;
298
299     int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
300     switch (scope) {
301       case LTO_SYMBOL_SCOPE_HIDDEN:
302         sym.visibility = LDPV_HIDDEN;
303         break;
304       case LTO_SYMBOL_SCOPE_PROTECTED:
305         sym.visibility = LDPV_PROTECTED;
306         break;
307       case 0: // extern
308       case LTO_SYMBOL_SCOPE_DEFAULT:
309         sym.visibility = LDPV_DEFAULT;
310         break;
311       default:
312         (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
313         return LDPS_ERR;
314     }
315
316     int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
317     sym.comdat_key = NULL;
318     switch (definition) {
319       case LTO_SYMBOL_DEFINITION_REGULAR:
320         sym.def = LDPK_DEF;
321         break;
322       case LTO_SYMBOL_DEFINITION_UNDEFINED:
323         sym.def = LDPK_UNDEF;
324         break;
325       case LTO_SYMBOL_DEFINITION_TENTATIVE:
326         sym.def = LDPK_COMMON;
327         break;
328       case LTO_SYMBOL_DEFINITION_WEAK:
329         sym.comdat_key = sym.name;
330         sym.def = LDPK_WEAKDEF;
331         break;
332       case LTO_SYMBOL_DEFINITION_WEAKUNDEF:
333         sym.def = LDPK_WEAKUNDEF;
334         break;
335       default:
336         (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition);
337         return LDPS_ERR;
338     }
339
340     sym.size = 0;
341
342     sym.resolution = LDPR_UNKNOWN;
343   }
344
345   cf.syms.reserve(cf.syms.size());
346
347   if (!cf.syms.empty()) {
348     if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
349       (*message)(LDPL_ERROR, "Unable to add symbols!");
350       return LDPS_ERR;
351     }
352   }
353
354   if (code_gen)
355     lto_codegen_add_module(code_gen, M);
356
357   lto_module_dispose(M);
358
359   return LDPS_OK;
360 }
361
362 /// all_symbols_read_hook - gold informs us that all symbols have been read.
363 /// At this point, we use get_symbols to see if any of our definitions have
364 /// been overridden by a native object file. Then, perform optimization and
365 /// codegen.
366 static ld_plugin_status all_symbols_read_hook(void) {
367   std::ofstream api_file;
368   assert(code_gen);
369
370   if (options::generate_api_file) {
371     api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
372     if (!api_file.is_open()) {
373       (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
374       abort();
375     }
376   }
377
378   for (std::list<claimed_file>::iterator I = Modules.begin(),
379          E = Modules.end(); I != E; ++I) {
380     if (I->syms.empty())
381       continue;
382     (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
383     for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
384       if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
385         lto_codegen_add_must_preserve_symbol(code_gen, I->syms[i].name);
386
387         if (options::generate_api_file)
388           api_file << I->syms[i].name << "\n";
389       }
390     }
391   }
392
393   if (options::generate_api_file)
394     api_file.close();
395
396   lto_codegen_set_pic_model(code_gen, output_type);
397   lto_codegen_set_debug_model(code_gen, LTO_DEBUG_MODEL_DWARF);
398   if (!options::mcpu.empty())
399     lto_codegen_set_cpu(code_gen, options::mcpu.c_str());
400
401   // Pass through extra options to the code generator.
402   if (!options::extra.empty()) {
403     for (std::vector<std::string>::iterator it = options::extra.begin();
404          it != options::extra.end(); ++it) {
405       lto_codegen_debug_options(code_gen, (*it).c_str());
406     }
407   }
408
409   if (options::generate_bc_file != options::BC_NO) {
410     std::string path;
411     if (options::generate_bc_file == options::BC_ONLY)
412       path = output_name;
413     else if (!options::bc_path.empty())
414       path = options::bc_path;
415     else
416       path = output_name + ".bc";
417     bool err = lto_codegen_write_merged_modules(code_gen, path.c_str());
418     if (err)
419       (*message)(LDPL_FATAL, "Failed to write the output file.");
420     if (options::generate_bc_file == options::BC_ONLY)
421       exit(0);
422   }
423   const char *objPath;
424   if (lto_codegen_compile_to_file(code_gen, &objPath)) {
425     (*message)(LDPL_ERROR, "Could not produce a combined object file\n");
426   }
427
428   lto_codegen_dispose(code_gen);
429   for (std::list<claimed_file>::iterator I = Modules.begin(),
430          E = Modules.end(); I != E; ++I) {
431     for (unsigned i = 0; i != I->syms.size(); ++i) {
432       ld_plugin_symbol &sym = I->syms[i];
433       free(sym.name);
434     }
435   }
436
437   if ((*add_input_file)(objPath) != LDPS_OK) {
438     (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
439     (*message)(LDPL_ERROR, "File left behind in: %s", objPath);
440     return LDPS_ERR;
441   }
442
443   if (!options::extra_library_path.empty() &&
444       set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
445     (*message)(LDPL_ERROR, "Unable to set the extra library path.");
446     return LDPS_ERR;
447   }
448
449   if (options::obj_path.empty())
450     Cleanup.push_back(sys::Path(objPath));
451
452   return LDPS_OK;
453 }
454
455 static ld_plugin_status cleanup_hook(void) {
456   std::string ErrMsg;
457
458   for (int i = 0, e = Cleanup.size(); i != e; ++i)
459     if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
460       (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
461                  ErrMsg.c_str());
462
463   return LDPS_OK;
464 }