Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 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 | |
Dylan Noblesmith | 9e5b178 | 2011-12-22 23:04:07 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" // plugin-api.h requires HAVE_STDINT_H |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 16 | #include "llvm-c/lto.h" |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/StringSet.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/CommandFlags.h" |
| 19 | #include "llvm/LTO/LTOCodeGenerator.h" |
| 20 | #include "llvm/LTO/LTOModule.h" |
Michael J. Spencer | ab425d8 | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Errno.h" |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | ab425d8 | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Path.h" |
| 25 | #include "llvm/Support/Program.h" |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 26 | #include "llvm/Support/TargetSelect.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ToolOutputFile.h" |
Torok Edwin | a488ee0e | 2009-02-04 21:00:02 +0000 | [diff] [blame] | 28 | #include <cerrno> |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 29 | #include <cstdlib> |
| 30 | #include <cstring> |
Nick Lewycky | 338d07e | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 31 | #include <fstream> |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 32 | #include <list> |
Chandler Carruth | 07baed5 | 2014-01-13 08:04:33 +0000 | [diff] [blame] | 33 | #include <plugin-api.h> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 34 | #include <system_error> |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 35 | #include <vector> |
| 36 | |
Michael J. Spencer | 06f5223 | 2011-01-20 05:43:16 +0000 | [diff] [blame] | 37 | // Support Windows/MinGW crazyness. |
| 38 | #ifdef _WIN32 |
| 39 | # include <io.h> |
| 40 | # define lseek _lseek |
| 41 | # define read _read |
| 42 | #endif |
| 43 | |
Sylvestre Ledru | 5399979 | 2014-02-11 17:30:18 +0000 | [diff] [blame] | 44 | #ifndef LDPO_PIE |
| 45 | // FIXME: remove this declaration when we stop maintaining Ubuntu Quantal and |
| 46 | // Precise and Debian Wheezy (binutils 2.23 is required) |
| 47 | # define LDPO_PIE 3 |
| 48 | #endif |
| 49 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 50 | using namespace llvm; |
| 51 | |
| 52 | namespace { |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 53 | struct claimed_file { |
| 54 | void *handle; |
| 55 | std::vector<ld_plugin_symbol> syms; |
| 56 | }; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 57 | } |
Rafael Espindola | bfb8b91 | 2014-06-20 01:37:35 +0000 | [diff] [blame] | 58 | |
| 59 | static ld_plugin_status discard_message(int level, const char *format, ...) { |
| 60 | // Die loudly. Recent versions of Gold pass ld_plugin_message as the first |
| 61 | // callback in the transfer vector. This should never be called. |
| 62 | abort(); |
| 63 | } |
| 64 | |
| 65 | static ld_plugin_add_symbols add_symbols = NULL; |
| 66 | static ld_plugin_get_symbols get_symbols = NULL; |
| 67 | static ld_plugin_add_input_file add_input_file = NULL; |
| 68 | static ld_plugin_set_extra_library_path set_extra_library_path = NULL; |
| 69 | static ld_plugin_get_view get_view = NULL; |
| 70 | static ld_plugin_message message = discard_message; |
| 71 | static lto_codegen_model output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 72 | static std::string output_name = ""; |
| 73 | static std::list<claimed_file> Modules; |
| 74 | static std::vector<std::string> Cleanup; |
| 75 | static LTOCodeGenerator *CodeGen = nullptr; |
| 76 | static StringSet<> CannotBeHidden; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 77 | static llvm::TargetOptions TargetOpts; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 78 | |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 79 | namespace options { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 80 | enum generate_bc { BC_NO, BC_ALSO, BC_ONLY }; |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 81 | static bool generate_api_file = false; |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 82 | static generate_bc generate_bc_file = BC_NO; |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 83 | static std::string bc_path; |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 84 | static std::string obj_path; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 85 | static std::string extra_library_path; |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 86 | static std::string triple; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 87 | static std::string mcpu; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 88 | // Additional options to pass into the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 89 | // Note: This array will contain all plugin options which are not claimed |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 90 | // as plugin exclusive to pass to the code generator. |
Nick Lewycky | 0ac5e22 | 2010-06-03 17:10:17 +0000 | [diff] [blame] | 91 | // For example, "generate-api-file" and "as"options are for the plugin |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 92 | // use only and will not be passed. |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 93 | static std::vector<std::string> extra; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 94 | |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 95 | static void process_plugin_option(const char* opt_) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 96 | { |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 97 | if (opt_ == NULL) |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 98 | return; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 99 | llvm::StringRef opt = opt_; |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 100 | |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 101 | if (opt == "generate-api-file") { |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 102 | generate_api_file = true; |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 103 | } else if (opt.startswith("mcpu=")) { |
| 104 | mcpu = opt.substr(strlen("mcpu=")); |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 105 | } else if (opt.startswith("extra-library-path=")) { |
| 106 | extra_library_path = opt.substr(strlen("extra_library_path=")); |
Rafael Espindola | 148c328 | 2010-08-10 16:32:15 +0000 | [diff] [blame] | 107 | } else if (opt.startswith("mtriple=")) { |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 108 | triple = opt.substr(strlen("mtriple=")); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 109 | } else if (opt.startswith("obj-path=")) { |
| 110 | obj_path = opt.substr(strlen("obj-path=")); |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 111 | } else if (opt == "emit-llvm") { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 112 | generate_bc_file = BC_ONLY; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 113 | } else if (opt == "also-emit-llvm") { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 114 | generate_bc_file = BC_ALSO; |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 115 | } else if (opt.startswith("also-emit-llvm=")) { |
| 116 | llvm::StringRef path = opt.substr(strlen("also-emit-llvm=")); |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 117 | generate_bc_file = BC_ALSO; |
Nick Lewycky | ca4180c | 2010-06-03 17:13:23 +0000 | [diff] [blame] | 118 | if (!bc_path.empty()) { |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 119 | (*message)(LDPL_WARNING, "Path to the output IL file specified twice. " |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 120 | "Discarding %s", opt_); |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 121 | } else { |
| 122 | bc_path = path; |
| 123 | } |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 124 | } else { |
| 125 | // Save this option to pass to the code generator. |
Rafael Espindola | c4dca3a | 2010-06-07 16:45:22 +0000 | [diff] [blame] | 126 | extra.push_back(opt); |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 131 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 132 | int *claimed); |
| 133 | static ld_plugin_status all_symbols_read_hook(void); |
| 134 | static ld_plugin_status cleanup_hook(void); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 135 | |
| 136 | extern "C" ld_plugin_status onload(ld_plugin_tv *tv); |
| 137 | ld_plugin_status onload(ld_plugin_tv *tv) { |
Peter Collingbourne | 1505c0a | 2014-07-03 23:28:03 +0000 | [diff] [blame] | 138 | InitializeAllTargetInfos(); |
| 139 | InitializeAllTargets(); |
| 140 | InitializeAllTargetMCs(); |
| 141 | InitializeAllAsmParsers(); |
| 142 | InitializeAllAsmPrinters(); |
| 143 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 144 | // We're given a pointer to the first transfer vector. We read through them |
| 145 | // until we find one where tv_tag == LDPT_NULL. The REGISTER_* tagged values |
| 146 | // contain pointers to functions that we need to call to register our own |
| 147 | // hooks. The others are addresses of functions we can use to call into gold |
| 148 | // for services. |
| 149 | |
| 150 | bool registeredClaimFile = false; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 151 | bool RegisteredAllSymbolsRead = false; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 152 | |
| 153 | for (; tv->tv_tag != LDPT_NULL; ++tv) { |
| 154 | switch (tv->tv_tag) { |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 155 | case LDPT_OUTPUT_NAME: |
| 156 | output_name = tv->tv_u.tv_string; |
| 157 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 158 | case LDPT_LINKER_OUTPUT: |
| 159 | switch (tv->tv_u.tv_val) { |
| 160 | case LDPO_REL: // .o |
| 161 | case LDPO_DYN: // .so |
Rafael Espindola | 76e376d | 2014-02-10 20:38:38 +0000 | [diff] [blame] | 162 | case LDPO_PIE: // position independent executable |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 163 | output_type = LTO_CODEGEN_PIC_MODEL_DYNAMIC; |
| 164 | break; |
| 165 | case LDPO_EXEC: // .exe |
| 166 | output_type = LTO_CODEGEN_PIC_MODEL_STATIC; |
| 167 | break; |
| 168 | default: |
| 169 | (*message)(LDPL_ERROR, "Unknown output file type %d", |
| 170 | tv->tv_u.tv_val); |
| 171 | return LDPS_ERR; |
| 172 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 173 | break; |
| 174 | case LDPT_OPTION: |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 175 | options::process_plugin_option(tv->tv_u.tv_string); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 176 | break; |
| 177 | case LDPT_REGISTER_CLAIM_FILE_HOOK: { |
| 178 | ld_plugin_register_claim_file callback; |
| 179 | callback = tv->tv_u.tv_register_claim_file; |
| 180 | |
| 181 | if ((*callback)(claim_file_hook) != LDPS_OK) |
| 182 | return LDPS_ERR; |
| 183 | |
| 184 | registeredClaimFile = true; |
| 185 | } break; |
| 186 | case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: { |
| 187 | ld_plugin_register_all_symbols_read callback; |
| 188 | callback = tv->tv_u.tv_register_all_symbols_read; |
| 189 | |
| 190 | if ((*callback)(all_symbols_read_hook) != LDPS_OK) |
| 191 | return LDPS_ERR; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 192 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 193 | RegisteredAllSymbolsRead = true; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 194 | } break; |
| 195 | case LDPT_REGISTER_CLEANUP_HOOK: { |
| 196 | ld_plugin_register_cleanup callback; |
| 197 | callback = tv->tv_u.tv_register_cleanup; |
| 198 | |
| 199 | if ((*callback)(cleanup_hook) != LDPS_OK) |
| 200 | return LDPS_ERR; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 201 | } break; |
| 202 | case LDPT_ADD_SYMBOLS: |
| 203 | add_symbols = tv->tv_u.tv_add_symbols; |
| 204 | break; |
Rafael Espindola | cda2911 | 2013-10-03 18:29:09 +0000 | [diff] [blame] | 205 | case LDPT_GET_SYMBOLS_V2: |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 206 | get_symbols = tv->tv_u.tv_get_symbols; |
| 207 | break; |
| 208 | case LDPT_ADD_INPUT_FILE: |
| 209 | add_input_file = tv->tv_u.tv_add_input_file; |
| 210 | break; |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 211 | case LDPT_SET_EXTRA_LIBRARY_PATH: |
| 212 | set_extra_library_path = tv->tv_u.tv_set_extra_library_path; |
| 213 | break; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 214 | case LDPT_GET_VIEW: |
| 215 | get_view = tv->tv_u.tv_get_view; |
| 216 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 217 | case LDPT_MESSAGE: |
| 218 | message = tv->tv_u.tv_message; |
| 219 | break; |
| 220 | default: |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 225 | if (!registeredClaimFile) { |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 226 | (*message)(LDPL_ERROR, "register_claim_file not passed to LLVMgold."); |
| 227 | return LDPS_ERR; |
| 228 | } |
Rafael Espindola | e08484d | 2009-02-18 08:30:15 +0000 | [diff] [blame] | 229 | if (!add_symbols) { |
Rafael Espindola | 6add618 | 2009-02-18 17:49:06 +0000 | [diff] [blame] | 230 | (*message)(LDPL_ERROR, "add_symbols not passed to LLVMgold."); |
| 231 | return LDPS_ERR; |
| 232 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 233 | |
Rafael Espindola | a0d30a9 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 234 | if (!RegisteredAllSymbolsRead) |
| 235 | return LDPS_OK; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 236 | |
Rafael Espindola | a0d30a9 | 2014-06-19 22:20:07 +0000 | [diff] [blame] | 237 | CodeGen = new LTOCodeGenerator(); |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 238 | |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 239 | // Pass through extra options to the code generator. |
| 240 | if (!options::extra.empty()) { |
| 241 | for (std::vector<std::string>::iterator it = options::extra.begin(); |
| 242 | it != options::extra.end(); ++it) { |
| 243 | CodeGen->setCodeGenDebugOptions((*it).c_str()); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | CodeGen->parseCodeGenDebugOptions(); |
Tom Roeder | b508119 | 2014-06-26 20:43:27 +0000 | [diff] [blame] | 248 | if (MAttrs.size()) { |
| 249 | std::string Attrs; |
| 250 | for (unsigned I = 0; I < MAttrs.size(); ++I) { |
| 251 | if (I > 0) |
| 252 | Attrs.append(","); |
| 253 | Attrs.append(MAttrs[I]); |
| 254 | } |
| 255 | CodeGen->setAttr(Attrs.c_str()); |
| 256 | } |
| 257 | |
Rafael Espindola | c273aac | 2014-06-19 22:54:47 +0000 | [diff] [blame] | 258 | TargetOpts = InitTargetOptionsFromCodeGenFlags(); |
| 259 | CodeGen->setTargetOptions(TargetOpts); |
| 260 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 261 | return LDPS_OK; |
| 262 | } |
| 263 | |
Rafael Espindola | e54d821 | 2014-07-06 14:31:22 +0000 | [diff] [blame^] | 264 | /// Called by gold to see whether this file is one that our plugin can handle. |
| 265 | /// We'll try to open it and register all the symbols with add_symbol if |
| 266 | /// possible. |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 267 | static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file, |
| 268 | int *claimed) { |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 269 | const void *view; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 270 | std::unique_ptr<MemoryBuffer> buffer; |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 271 | if (get_view) { |
Rafael Espindola | ece7c9c | 2011-04-07 21:11:00 +0000 | [diff] [blame] | 272 | if (get_view(file->handle, &view) != LDPS_OK) { |
| 273 | (*message)(LDPL_ERROR, "Failed to get a view of %s", file->name); |
| 274 | return LDPS_ERR; |
| 275 | } |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 276 | } else { |
Ivan Krasin | 639222d | 2011-09-15 23:13:00 +0000 | [diff] [blame] | 277 | int64_t offset = 0; |
Nick Lewycky | 8691c47 | 2009-02-05 04:14:23 +0000 | [diff] [blame] | 278 | // Gold has found what might be IR part-way inside of a file, such as |
| 279 | // an .a archive. |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 280 | if (file->offset) { |
| 281 | offset = file->offset; |
| 282 | } |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 283 | if (std::error_code ec = MemoryBuffer::getOpenFileSlice( |
Rafael Espindola | 3d2ac2e | 2013-07-23 20:25:01 +0000 | [diff] [blame] | 284 | file->fd, file->name, buffer, file->filesize, offset)) { |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 285 | (*message)(LDPL_ERROR, ec.message().c_str()); |
| 286 | return LDPS_ERR; |
| 287 | } |
| 288 | view = buffer->getBufferStart(); |
Rafael Espindola | 56e41f7 | 2011-02-08 22:40:47 +0000 | [diff] [blame] | 289 | } |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 290 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 291 | if (!LTOModule::isBitcodeFile(view, file->filesize)) |
Ivan Krasin | 5021af5 | 2011-09-12 21:47:50 +0000 | [diff] [blame] | 292 | return LDPS_OK; |
| 293 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 294 | std::string Error; |
Rafael Espindola | e54d821 | 2014-07-06 14:31:22 +0000 | [diff] [blame^] | 295 | LTOModule *M = |
| 296 | LTOModule::createFromBuffer(view, file->filesize, TargetOpts, Error); |
Ivan Krasin | d5f2d8c | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 297 | if (!M) { |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 298 | (*message)(LDPL_ERROR, |
| 299 | "LLVM gold plugin has failed to create LTO module: %s", |
| 300 | Error.c_str()); |
Rafael Espindola | b39c7c7 | 2011-03-17 00:36:11 +0000 | [diff] [blame] | 301 | return LDPS_OK; |
Ivan Krasin | d5f2d8c | 2011-09-09 00:14:04 +0000 | [diff] [blame] | 302 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 303 | |
| 304 | *claimed = 1; |
| 305 | Modules.resize(Modules.size() + 1); |
| 306 | claimed_file &cf = Modules.back(); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 307 | |
| 308 | if (!options::triple.empty()) |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 309 | M->setTargetTriple(options::triple.c_str()); |
Rafael Espindola | 4ef89f5 | 2010-08-09 21:09:46 +0000 | [diff] [blame] | 310 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 311 | cf.handle = file->handle; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 312 | unsigned sym_count = M->getSymbolCount(); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 313 | cf.syms.reserve(sym_count); |
| 314 | |
| 315 | for (unsigned i = 0; i != sym_count; ++i) { |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 316 | lto_symbol_attributes attrs = M->getSymbolAttributes(i); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 317 | if ((attrs & LTO_SYMBOL_SCOPE_MASK) == LTO_SYMBOL_SCOPE_INTERNAL) |
| 318 | continue; |
| 319 | |
| 320 | cf.syms.push_back(ld_plugin_symbol()); |
| 321 | ld_plugin_symbol &sym = cf.syms.back(); |
Rafael Espindola | b201bfc | 2014-06-19 22:33:23 +0000 | [diff] [blame] | 322 | sym.name = strdup(M->getSymbolName(i)); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 323 | sym.version = NULL; |
| 324 | |
| 325 | int scope = attrs & LTO_SYMBOL_SCOPE_MASK; |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 326 | bool CanBeHidden = scope == LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN; |
| 327 | if (!CanBeHidden) |
| 328 | CannotBeHidden.insert(sym.name); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 329 | switch (scope) { |
| 330 | case LTO_SYMBOL_SCOPE_HIDDEN: |
| 331 | sym.visibility = LDPV_HIDDEN; |
| 332 | break; |
| 333 | case LTO_SYMBOL_SCOPE_PROTECTED: |
| 334 | sym.visibility = LDPV_PROTECTED; |
| 335 | break; |
| 336 | case 0: // extern |
| 337 | case LTO_SYMBOL_SCOPE_DEFAULT: |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 338 | case LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN: |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 339 | sym.visibility = LDPV_DEFAULT; |
| 340 | break; |
| 341 | default: |
| 342 | (*message)(LDPL_ERROR, "Unknown scope attribute: %d", scope); |
| 343 | return LDPS_ERR; |
| 344 | } |
| 345 | |
| 346 | int definition = attrs & LTO_SYMBOL_DEFINITION_MASK; |
Rafael Espindola | 70d8015 | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 347 | sym.comdat_key = NULL; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 348 | switch (definition) { |
| 349 | case LTO_SYMBOL_DEFINITION_REGULAR: |
| 350 | sym.def = LDPK_DEF; |
| 351 | break; |
| 352 | case LTO_SYMBOL_DEFINITION_UNDEFINED: |
| 353 | sym.def = LDPK_UNDEF; |
| 354 | break; |
| 355 | case LTO_SYMBOL_DEFINITION_TENTATIVE: |
| 356 | sym.def = LDPK_COMMON; |
| 357 | break; |
| 358 | case LTO_SYMBOL_DEFINITION_WEAK: |
Rafael Espindola | 70d8015 | 2011-02-14 22:23:49 +0000 | [diff] [blame] | 359 | sym.comdat_key = sym.name; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 360 | sym.def = LDPK_WEAKDEF; |
| 361 | break; |
Rafael Espindola | 5654852 | 2009-04-24 16:55:21 +0000 | [diff] [blame] | 362 | case LTO_SYMBOL_DEFINITION_WEAKUNDEF: |
| 363 | sym.def = LDPK_WEAKUNDEF; |
| 364 | break; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 365 | default: |
| 366 | (*message)(LDPL_ERROR, "Unknown definition attribute: %d", definition); |
| 367 | return LDPS_ERR; |
| 368 | } |
| 369 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 370 | sym.size = 0; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 371 | |
| 372 | sym.resolution = LDPR_UNKNOWN; |
| 373 | } |
| 374 | |
| 375 | cf.syms.reserve(cf.syms.size()); |
| 376 | |
| 377 | if (!cf.syms.empty()) { |
| 378 | if ((*add_symbols)(cf.handle, cf.syms.size(), &cf.syms[0]) != LDPS_OK) { |
| 379 | (*message)(LDPL_ERROR, "Unable to add symbols!"); |
| 380 | return LDPS_ERR; |
| 381 | } |
| 382 | } |
| 383 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 384 | if (CodeGen) { |
| 385 | std::string Error; |
| 386 | if (!CodeGen->addModule(M, Error)) { |
| 387 | (*message)(LDPL_ERROR, "Error linking module: %s", Error.c_str()); |
Rafael Espindola | d578b69 | 2013-10-18 19:32:06 +0000 | [diff] [blame] | 388 | return LDPS_ERR; |
| 389 | } |
| 390 | } |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 391 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 392 | delete M; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 393 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 394 | return LDPS_OK; |
| 395 | } |
| 396 | |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 397 | static bool mustPreserve(const claimed_file &F, int i) { |
| 398 | if (F.syms[i].resolution == LDPR_PREVAILING_DEF) |
| 399 | return true; |
| 400 | if (F.syms[i].resolution == LDPR_PREVAILING_DEF_IRONLY_EXP) |
| 401 | return CannotBeHidden.count(F.syms[i].name); |
| 402 | return false; |
| 403 | } |
| 404 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 405 | /// all_symbols_read_hook - gold informs us that all symbols have been read. |
| 406 | /// At this point, we use get_symbols to see if any of our definitions have |
| 407 | /// been overridden by a native object file. Then, perform optimization and |
| 408 | /// codegen. |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 409 | static ld_plugin_status all_symbols_read_hook(void) { |
Nick Lewycky | 338d07e | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 410 | std::ofstream api_file; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 411 | assert(CodeGen); |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 412 | |
Viktor Kutuzov | fd7ddd9 | 2009-10-28 18:55:55 +0000 | [diff] [blame] | 413 | if (options::generate_api_file) { |
Nick Lewycky | 338d07e | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 414 | api_file.open("apifile.txt", std::ofstream::out | std::ofstream::trunc); |
| 415 | if (!api_file.is_open()) { |
| 416 | (*message)(LDPL_FATAL, "Unable to open apifile.txt for writing."); |
| 417 | abort(); |
| 418 | } |
| 419 | } |
| 420 | |
Rafael Espindola | 0012182 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 421 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 422 | E = Modules.end(); I != E; ++I) { |
Nick Lewycky | 1fcd0f1 | 2011-07-26 08:40:36 +0000 | [diff] [blame] | 423 | if (I->syms.empty()) |
| 424 | continue; |
Rafael Espindola | 0012182 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 425 | (*get_symbols)(I->handle, I->syms.size(), &I->syms[0]); |
| 426 | for (unsigned i = 0, e = I->syms.size(); i != e; i++) { |
Rafael Espindola | 282a470 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 427 | if (mustPreserve(*I, i)) { |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 428 | CodeGen->addMustPreserveSymbol(I->syms[i].name); |
Nick Lewycky | 338d07e | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 429 | |
Rafael Espindola | 0012182 | 2010-06-03 14:45:44 +0000 | [diff] [blame] | 430 | if (options::generate_api_file) |
| 431 | api_file << I->syms[i].name << "\n"; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
Rafael Espindola | 77b6d01 | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 434 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 435 | |
Rafael Espindola | 77b6d01 | 2010-06-14 21:20:52 +0000 | [diff] [blame] | 436 | if (options::generate_api_file) |
| 437 | api_file.close(); |
Nick Lewycky | 338d07e | 2009-02-22 22:15:44 +0000 | [diff] [blame] | 438 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 439 | CodeGen->setCodePICModel(output_type); |
| 440 | CodeGen->setDebugInfo(LTO_DEBUG_MODEL_DWARF); |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 441 | if (!options::mcpu.empty()) |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 442 | CodeGen->setCpu(options::mcpu.c_str()); |
Rafael Espindola | ccab1dd | 2010-08-11 00:15:13 +0000 | [diff] [blame] | 443 | |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 444 | if (options::generate_bc_file != options::BC_NO) { |
| 445 | std::string path; |
| 446 | if (options::generate_bc_file == options::BC_ONLY) |
| 447 | path = output_name; |
| 448 | else if (!options::bc_path.empty()) |
| 449 | path = options::bc_path; |
| 450 | else |
| 451 | path = output_name + ".bc"; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 452 | std::string Error; |
| 453 | if (!CodeGen->writeMergedModules(path.c_str(), Error)) |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 454 | (*message)(LDPL_FATAL, "Failed to write the output file."); |
Rafael Espindola | fb7a0b8 | 2013-10-03 00:07:30 +0000 | [diff] [blame] | 455 | if (options::generate_bc_file == options::BC_ONLY) { |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 456 | delete CodeGen; |
Rafael Espindola | 8fb957e | 2010-06-03 21:11:20 +0000 | [diff] [blame] | 457 | exit(0); |
Rafael Espindola | fb7a0b8 | 2013-10-03 00:07:30 +0000 | [diff] [blame] | 458 | } |
Rafael Espindola | ba3398b | 2010-05-13 13:39:31 +0000 | [diff] [blame] | 459 | } |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 460 | |
| 461 | std::string ObjPath; |
| 462 | { |
| 463 | const char *Temp; |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 464 | std::string Error; |
| 465 | if (!CodeGen->compile_to_file(&Temp, /*DisableOpt*/ false, /*DisableInline*/ |
| 466 | false, /*DisableGVNLoadPRE*/ false, Error)) |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 467 | (*message)(LDPL_ERROR, "Could not produce a combined object file\n"); |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 468 | ObjPath = Temp; |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 469 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 470 | |
Rafael Espindola | 6b244b1 | 2014-06-19 21:14:13 +0000 | [diff] [blame] | 471 | delete CodeGen; |
Rafael Espindola | 9ef90d5 | 2011-02-20 18:28:29 +0000 | [diff] [blame] | 472 | for (std::list<claimed_file>::iterator I = Modules.begin(), |
| 473 | E = Modules.end(); I != E; ++I) { |
| 474 | for (unsigned i = 0; i != I->syms.size(); ++i) { |
| 475 | ld_plugin_symbol &sym = I->syms[i]; |
| 476 | free(sym.name); |
| 477 | } |
| 478 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 479 | |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 480 | if ((*add_input_file)(ObjPath.c_str()) != LDPS_OK) { |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 481 | (*message)(LDPL_ERROR, "Unable to add .o file to the link."); |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 482 | (*message)(LDPL_ERROR, "File left behind in: %s", ObjPath.c_str()); |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 483 | return LDPS_ERR; |
| 484 | } |
| 485 | |
Rafael Espindola | ef49815 | 2010-06-23 20:20:59 +0000 | [diff] [blame] | 486 | if (!options::extra_library_path.empty() && |
| 487 | set_extra_library_path(options::extra_library_path.c_str()) != LDPS_OK) { |
| 488 | (*message)(LDPL_ERROR, "Unable to set the extra library path."); |
| 489 | return LDPS_ERR; |
| 490 | } |
| 491 | |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 492 | if (options::obj_path.empty()) |
Rafael Espindola | 143fc3b | 2013-10-16 12:47:04 +0000 | [diff] [blame] | 493 | Cleanup.push_back(ObjPath); |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 494 | |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 495 | return LDPS_OK; |
| 496 | } |
| 497 | |
Dan Gohman | ebb4ae0 | 2010-04-16 00:42:57 +0000 | [diff] [blame] | 498 | static ld_plugin_status cleanup_hook(void) { |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 499 | for (int i = 0, e = Cleanup.size(); i != e; ++i) { |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 500 | std::error_code EC = sys::fs::remove(Cleanup[i]); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 501 | if (EC) |
Shuxin Yang | 1826ae2 | 2013-08-12 21:07:31 +0000 | [diff] [blame] | 502 | (*message)(LDPL_ERROR, "Failed to delete '%s': %s", Cleanup[i].c_str(), |
Rafael Espindola | d9a5608 | 2013-06-17 22:24:06 +0000 | [diff] [blame] | 503 | EC.message().c_str()); |
Rafael Espindola | 55ab87f | 2013-06-17 18:38:18 +0000 | [diff] [blame] | 504 | } |
Nick Lewycky | fb643e4 | 2009-02-03 07:13:24 +0000 | [diff] [blame] | 505 | |
| 506 | return LDPS_OK; |
| 507 | } |