gold-plugin.cpp revision f29140106f74d15ba357aa0a7f109adc939c3104
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"
16#include "plugin-api.h"
17
18#include "llvm-c/lto.h"
19
20#include "llvm/Support/raw_ostream.h"
21#include "llvm/System/Errno.h"
22#include "llvm/System/Path.h"
23#include "llvm/System/Program.h"
24
25#include <cerrno>
26#include <cstdlib>
27#include <cstring>
28#include <fstream>
29#include <list>
30#include <vector>
31
32using namespace llvm;
33
34namespace {
35  ld_plugin_status discard_message(int level, const char *format, ...) {
36    // Die loudly. Recent versions of Gold pass ld_plugin_message as the first
37    // callback in the transfer vector. This should never be called.
38    abort();
39  }
40
41  ld_plugin_add_symbols add_symbols = NULL;
42  ld_plugin_get_symbols get_symbols = NULL;
43  ld_plugin_add_input_file add_input_file = NULL;
44  ld_plugin_add_input_library add_input_library = NULL;
45  ld_plugin_set_extra_library_path set_extra_library_path = NULL;
46  ld_plugin_message message = discard_message;
47
48  int api_version = 0;
49  int gold_version = 0;
50
51  struct claimed_file {
52    lto_module_t M;
53    void *handle;
54    std::vector<ld_plugin_symbol> syms;
55  };
56
57  lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
58  std::string output_name = "";
59  std::list<claimed_file> Modules;
60  std::vector<sys::Path> Cleanup;
61}
62
63namespace options {
64  enum generate_bc { BC_NO, BC_ALSO, BC_ONLY };
65  static bool generate_api_file = false;
66  static generate_bc generate_bc_file = BC_NO;
67  static std::string bc_path;
68  static std::string as_path;
69  static std::vector<std::string> as_args;
70  static std::vector<std::string> pass_through;
71  static std::string extra_library_path;
72  static std::string triple;
73  static std::string mcpu;
74  // Additional options to pass into the code generator.
75  // Note: This array will contain all plugin options which are not claimed
76  // as plugin exclusive to pass to the code generator.
77  // For example, "generate-api-file" and "as"options are for the plugin
78  // use only and will not be passed.
79  static std::vector<std::string> extra;
80
81  static void process_plugin_option(const char* opt_)
82  {
83    if (opt_ == NULL)
84      return;
85    llvm::StringRef opt = opt_;
86
87    if (opt == "generate-api-file") {
88      generate_api_file = true;
89    } else if (opt.startswith("mcpu=")) {
90      mcpu = opt.substr(strlen("mcpu="));
91    } else if (opt.startswith("as=")) {
92      if (!as_path.empty()) {
93        (*message)(LDPL_WARNING, "Path to as specified twice. "
94                   "Discarding %s", opt_);
95      } else {
96        as_path = opt.substr(strlen("as="));
97      }
98    } else if (opt.startswith("as-arg=")) {
99      llvm::StringRef item = opt.substr(strlen("as-arg="));
100      as_args.push_back(item.str());
101    } else if (opt.startswith("extra-library-path=")) {
102      extra_library_path = opt.substr(strlen("extra_library_path="));
103    } else if (opt.startswith("pass-through=")) {
104      llvm::StringRef item = opt.substr(strlen("pass-through="));
105      pass_through.push_back(item.str());
106    } else if (opt.startswith("mtriple=")) {
107      triple = opt.substr(strlen("mtriple="));
108    } else if (opt == "emit-llvm") {
109      generate_bc_file = BC_ONLY;
110    } else if (opt == "also-emit-llvm") {
111      generate_bc_file = BC_ALSO;
112    } else if (opt.startswith("also-emit-llvm=")) {
113      llvm::StringRef path = opt.substr(strlen("also-emit-llvm="));
114      generate_bc_file = BC_ALSO;
115      if (!bc_path.empty()) {
116        (*message)(LDPL_WARNING, "Path to the output IL file specified twice. "
117                   "Discarding %s", opt_);
118      } else {
119        bc_path = path;
120      }
121    } else {
122      // Save this option to pass to the code generator.
123      extra.push_back(opt);
124    }
125  }
126}
127
128static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
129                                        int *claimed);
130static ld_plugin_status all_symbols_read_hook(void);
131static ld_plugin_status cleanup_hook(void);
132
133extern "C" ld_plugin_status onload(ld_plugin_tv *tv);
134ld_plugin_status onload(ld_plugin_tv *tv) {
135  // We're given a pointer to the first transfer vector. We read through them
136  // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values
137  // contain pointers to functions that we need to call to register our own
138  // hooks. The others are addresses of functions we can use to call into gold
139  // for services.
140
141  bool registeredClaimFile = false;
142
143  for (; tv->tv_tag != LDPT_NULL; ++tv) {
144    switch (tv->tv_tag) {
145      case LDPT_API_VERSION:
146        api_version = tv->tv_u.tv_val;
147        break;
148      case LDPT_GOLD_VERSION:  // major * 100 + minor
149        gold_version = tv->tv_u.tv_val;
150        break;
151      case LDPT_OUTPUT_NAME:
152        output_name = tv->tv_u.tv_string;
153        break;
154      case LDPT_LINKER_OUTPUT:
155        switch (tv->tv_u.tv_val) {
156          case LDPO_REL:  // .o
157          case LDPO_DYN:  // .so
158            output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC;
159            break;
160          case LDPO_EXEC:  // .exe
161            output_type = LTO_CODEGEN_PIC_MODEL_STATIC;
162            break;
163          default:
164            (*message)(LDPL_ERROR, "Unknown output file type %d",
165                       tv->tv_u.tv_val);
166            return LDPS_ERR;
167        }
168        // TODO: add an option to disable PIC.
169        //output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC;
170        break;
171      case LDPT_OPTION:
172        options::process_plugin_option(tv->tv_u.tv_string);
173        break;
174      case LDPT_REGISTER_CLAIM_FILE_HOOK: {
175        ld_plugin_register_claim_file callback;
176        callback = tv->tv_u.tv_register_claim_file;
177
178        if ((*callback)(claim_file_hook) != LDPS_OK)
179          return LDPS_ERR;
180
181        registeredClaimFile = true;
182      } break;
183      case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: {
184        ld_plugin_register_all_symbols_read callback;
185        callback = tv->tv_u.tv_register_all_symbols_read;
186
187        if ((*callback)(all_symbols_read_hook) != LDPS_OK)
188          return LDPS_ERR;
189      } break;
190      case LDPT_REGISTER_CLEANUP_HOOK: {
191        ld_plugin_register_cleanup callback;
192        callback = tv->tv_u.tv_register_cleanup;
193
194        if ((*callback)(cleanup_hook) != LDPS_OK)
195          return LDPS_ERR;
196      } break;
197      case LDPT_ADD_SYMBOLS:
198        add_symbols = tv->tv_u.tv_add_symbols;
199        break;
200      case LDPT_GET_SYMBOLS:
201        get_symbols = tv->tv_u.tv_get_symbols;
202        break;
203      case LDPT_ADD_INPUT_FILE:
204        add_input_file = tv->tv_u.tv_add_input_file;
205        break;
206      case LDPT_ADD_INPUT_LIBRARY:
207        add_input_library = tv->tv_u.tv_add_input_file;
208        break;
209      case LDPT_SET_EXTRA_LIBRARY_PATH:
210        set_extra_library_path = tv->tv_u.tv_set_extra_library_path;
211        break;
212      case LDPT_MESSAGE:
213        message = tv->tv_u.tv_message;
214        break;
215      default:
216        break;
217    }
218  }
219
220  if (!registeredClaimFile) {
221    (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold.");
222    return LDPS_ERR;
223  }
224  if (!add_symbols) {
225    (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold.");
226    return LDPS_ERR;
227  }
228
229  return LDPS_OK;
230}
231
232/// claim_file_hook - called by gold to see whether this file is one that
233/// our plugin can handle. We'll try to open it and register all the symbols
234/// with add_symbol if possible.
235static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
236                                        int *claimed) {
237  void *buf = NULL;
238  if (file->offset) {
239    // Gold has found what might be IR part-way inside of a file, such as
240    // an .a archive.
241    if (lseek(file->fd, file->offset, SEEK_SET) == -1) {
242      (*message)(LDPL_ERROR,
243                 "Failed to seek to archive member of %s at offset %d: %s\n",
244                 file->name,
245                 file->offset, sys::StrError(errno).c_str());
246      return LDPS_ERR;
247    }
248    buf = malloc(file->filesize);
249    if (!buf) {
250      (*message)(LDPL_ERROR,
251                 "Failed to allocate buffer for archive member of size: %d\n",
252                 file->filesize);
253      return LDPS_ERR;
254    }
255    if (read(file->fd, buf, file->filesize) != file->filesize) {
256      (*message)(LDPL_ERROR,
257                 "Failed to read archive member of %s at offset %d: %s\n",
258                 file->name,
259                 file->offset,
260                 sys::StrError(errno).c_str());
261      free(buf);
262      return LDPS_ERR;
263    }
264    if (!lto_module_is_object_file_in_memory(buf, file->filesize)) {
265      free(buf);
266      return LDPS_OK;
267    }
268  } else if (!lto_module_is_object_file(file->name))
269    return LDPS_OK;
270
271  *claimed = 1;
272  Modules.resize(Modules.size() + 1);
273  claimed_file &cf = Modules.back();
274
275  cf.M = buf ? lto_module_create_from_memory(buf, file->filesize) :
276               lto_module_create(file->name);
277  free(buf);
278  if (!cf.M) {
279    (*message)(LDPL_ERROR, "Failed to create LLVM module: %s",
280               lto_get_error_message());
281    return LDPS_ERR;
282  }
283
284  if (!options::triple.empty())
285    lto_module_set_target_triple(cf.M, options::triple.c_str());
286
287  cf.handle = file->handle;
288  unsigned sym_count = lto_module_get_num_symbols(cf.M);
289  cf.syms.reserve(sym_count);
290
291  for (unsigned i = 0; i != sym_count; ++i) {
292    lto_symbol_attributes attrs = lto_module_get_symbol_attribute(cf.M, i);
293    if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL)
294      continue;
295
296    cf.syms.push_back(ld_plugin_symbol());
297    ld_plugin_symbol &sym = cf.syms.back();
298    sym.name = const_cast<char *>(lto_module_get_symbol_name(cf.M, i));
299    sym.version = NULL;
300
301    int scope = attrs & LTO_SYMBOL_SCOPE_MASK;
302    switch (scope) {
303      case LTO_SYMBOL_SCOPE_HIDDEN:
304        sym.visibility = LDPV_HIDDEN;
305        break;
306      case LTO_SYMBOL_SCOPE_PROTECTED:
307        sym.visibility = LDPV_PROTECTED;
308        break;
309      case 0: // extern
310      case LTO_SYMBOL_SCOPE_DEFAULT:
311        sym.visibility = LDPV_DEFAULT;
312        break;
313      default:
314        (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope);
315        return LDPS_ERR;
316    }
317
318    int definition = attrs & LTO_SYMBOL_DEFINITION_MASK;
319    switch (definition) {
320      case LTO_SYMBOL_DEFINITION_REGULAR:
321        sym.def = LDPK_DEF;
322        break;
323      case LTO_SYMBOL_DEFINITION_UNDEFINED:
324        sym.def = LDPK_UNDEF;
325        break;
326      case LTO_SYMBOL_DEFINITION_TENTATIVE:
327        sym.def = LDPK_COMMON;
328        break;
329      case LTO_SYMBOL_DEFINITION_WEAK:
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    // LLVM never emits COMDAT.
341    sym.size = 0;
342    sym.comdat_key = NULL;
343
344    sym.resolution = LDPR_UNKNOWN;
345  }
346
347  cf.syms.reserve(cf.syms.size());
348
349  if (!cf.syms.empty()) {
350    if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) {
351      (*message)(LDPL_ERROR, "Unable to add symbols!");
352      return LDPS_ERR;
353    }
354  }
355
356  return LDPS_OK;
357}
358
359/// all_symbols_read_hook - gold informs us that all symbols have been read.
360/// At this point, we use get_symbols to see if any of our definitions have
361/// been overridden by a native object file. Then, perform optimization and
362/// codegen.
363static ld_plugin_status all_symbols_read_hook(void) {
364  lto_code_gen_t cg = lto_codegen_create();
365
366  for (std::list<claimed_file>::iterator I = Modules.begin(),
367       E = Modules.end(); I != E; ++I)
368    lto_codegen_add_module(cg, I->M);
369
370  std::ofstream api_file;
371  if (options::generate_api_file) {
372    api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc);
373    if (!api_file.is_open()) {
374      (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing.");
375      abort();
376    }
377  }
378
379  // If we don't preserve any symbols, libLTO will assume that all symbols are
380  // needed. Keep all symbols unless we're producing a final executable.
381  bool anySymbolsPreserved = false;
382  for (std::list<claimed_file>::iterator I = Modules.begin(),
383         E = Modules.end(); I != E; ++I) {
384    (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]);
385    for (unsigned i = 0, e = I->syms.size(); i != e; i++) {
386      if (I->syms[i].resolution == LDPR_PREVAILING_DEF) {
387        lto_codegen_add_must_preserve_symbol(cg, I->syms[i].name);
388        anySymbolsPreserved = true;
389
390        if (options::generate_api_file)
391          api_file << I->syms[i].name << "\n";
392      }
393    }
394  }
395
396  if (options::generate_api_file)
397    api_file.close();
398
399  if (!anySymbolsPreserved) {
400    // All of the IL is unnecessary!
401    lto_codegen_dispose(cg);
402    return LDPS_OK;
403  }
404
405  lto_codegen_set_pic_model(cg, output_type);
406  lto_codegen_set_debug_model(cg, LTO_DEBUG_MODEL_DWARF);
407  if (!options::as_path.empty()) {
408    sys::Path p = sys::Program::FindProgramByName(options::as_path);
409    lto_codegen_set_assembler_path(cg, p.c_str());
410  }
411  if (!options::as_args.empty()) {
412    std::vector<const char *> as_args_p;
413    for (std::vector<std::string>::iterator I = options::as_args.begin(),
414           E = options::as_args.end(); I != E; ++I) {
415      as_args_p.push_back(I->c_str());
416    }
417    lto_codegen_set_assembler_args(cg, &as_args_p[0], as_args_p.size());
418  }
419  if (!options::mcpu.empty())
420    lto_codegen_set_cpu(cg, options::mcpu.c_str());
421
422  // Pass through extra options to the code generator.
423  if (!options::extra.empty()) {
424    for (std::vector<std::string>::iterator it = options::extra.begin();
425         it != options::extra.end(); ++it) {
426      lto_codegen_debug_options(cg, (*it).c_str());
427    }
428  }
429
430
431  if (options::generate_bc_file != options::BC_NO) {
432    std::string path;
433    if (options::generate_bc_file == options::BC_ONLY)
434      path = output_name;
435    else if (!options::bc_path.empty())
436      path = options::bc_path;
437    else
438      path = output_name + ".bc";
439    bool err = lto_codegen_write_merged_modules(cg, path.c_str());
440    if (err)
441      (*message)(LDPL_FATAL, "Failed to write the output file.");
442    if (options::generate_bc_file == options::BC_ONLY)
443      exit(0);
444  }
445  size_t bufsize = 0;
446  const char *buffer = static_cast<const char *>(lto_codegen_compile(cg,
447                                                                     &bufsize));
448
449  std::string ErrMsg;
450
451  sys::Path uniqueObjPath("/tmp/llvmgold.o");
452  if (uniqueObjPath.createTemporaryFileOnDisk(true, &ErrMsg)) {
453    (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
454    return LDPS_ERR;
455  }
456  tool_output_file objFile(uniqueObjPath.c_str(), ErrMsg,
457                           raw_fd_ostream::F_Binary);
458  if (!ErrMsg.empty()) {
459    (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
460    return LDPS_ERR;
461  }
462
463  objFile.write(buffer, bufsize);
464  objFile.close();
465  if (objFile.has_error()) {
466    (*message)(LDPL_ERROR, "Error writing output file '%s'",
467               uniqueObjPath.c_str());
468    objFile.clear_error();
469    return LDPS_ERR;
470  }
471  objFile.keep();
472
473  lto_codegen_dispose(cg);
474
475  if ((*add_input_file)(uniqueObjPath.c_str()) != LDPS_OK) {
476    (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
477    (*message)(LDPL_ERROR, "File left behind in: %s", uniqueObjPath.c_str());
478    return LDPS_ERR;
479  }
480
481  if (!options::extra_library_path.empty() &&
482      set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) {
483    (*message)(LDPL_ERROR, "Unable to set the extra library path.");
484    return LDPS_ERR;
485  }
486
487  for (std::vector<std::string>::iterator i = options::pass_through.begin(),
488                                          e = options::pass_through.end();
489       i != e; ++i) {
490    std::string &item = *i;
491    const char *item_p = item.c_str();
492    if (llvm::StringRef(item).startswith("-l")) {
493      if (add_input_library(item_p + 2) != LDPS_OK) {
494        (*message)(LDPL_ERROR, "Unable to add library to the link.");
495        return LDPS_ERR;
496      }
497    } else {
498      if (add_input_file(item_p) != LDPS_OK) {
499        (*message)(LDPL_ERROR, "Unable to add .o file to the link.");
500        return LDPS_ERR;
501      }
502    }
503  }
504
505  Cleanup.push_back(uniqueObjPath);
506
507  return LDPS_OK;
508}
509
510static ld_plugin_status cleanup_hook(void) {
511  std::string ErrMsg;
512
513  for (int i = 0, e = Cleanup.size(); i != e; ++i)
514    if (Cleanup[i].eraseFromDisk(false, &ErrMsg))
515      (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(),
516                 ErrMsg.c_str());
517
518  return LDPS_OK;
519}
520