1 /* Plugin control for the GNU linker. 2 Copyright (C) 2010-2016 Free Software Foundation, Inc. 3 4 This file is part of the GNU Binutils. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21 #include "sysdep.h" 22 #include "libiberty.h" 23 #include "bfd.h" 24 #include "libbfd.h" 25 #include "bfdlink.h" 26 #include "bfdver.h" 27 #include "ld.h" 28 #include "ldmain.h" 29 #include "ldmisc.h" 30 #include "ldexp.h" 31 #include "ldlang.h" 32 #include "ldfile.h" 33 #include "../bfd/plugin.h" 34 #include "plugin.h" 35 #include "plugin-api.h" 36 #include "elf-bfd.h" 37 #if HAVE_MMAP 38 # include <sys/mman.h> 39 # ifndef MAP_FAILED 40 # define MAP_FAILED ((void *) -1) 41 # endif 42 # ifndef PROT_READ 43 # define PROT_READ 0 44 # endif 45 # ifndef MAP_PRIVATE 46 # define MAP_PRIVATE 0 47 # endif 48 #endif 49 #include <errno.h> 50 #if !(defined(errno) || defined(_MSC_VER) && defined(_INC_ERRNO)) 51 extern int errno; 52 #endif 53 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) 54 #include <windows.h> 55 #endif 56 57 /* Report plugin symbols. */ 58 bfd_boolean report_plugin_symbols; 59 60 /* The suffix to append to the name of the real (claimed) object file 61 when generating a dummy BFD to hold the IR symbols sent from the 62 plugin. For cosmetic use only; appears in maps, crefs etc. */ 63 #define IRONLY_SUFFIX " (symbol from plugin)" 64 65 /* Stores a single argument passed to a plugin. */ 66 typedef struct plugin_arg 67 { 68 struct plugin_arg *next; 69 const char *arg; 70 } plugin_arg_t; 71 72 /* Holds all details of a single plugin. */ 73 typedef struct plugin 74 { 75 /* Next on the list of plugins, or NULL at end of chain. */ 76 struct plugin *next; 77 /* The argument string given to --plugin. */ 78 const char *name; 79 /* The shared library handle returned by dlopen. */ 80 void *dlhandle; 81 /* The list of argument string given to --plugin-opt. */ 82 plugin_arg_t *args; 83 /* Number of args in the list, for convenience. */ 84 size_t n_args; 85 /* The plugin's event handlers. */ 86 ld_plugin_claim_file_handler claim_file_handler; 87 ld_plugin_all_symbols_read_handler all_symbols_read_handler; 88 ld_plugin_cleanup_handler cleanup_handler; 89 /* TRUE if the cleanup handlers have been called. */ 90 bfd_boolean cleanup_done; 91 } plugin_t; 92 93 typedef struct view_buffer 94 { 95 char *addr; 96 size_t filesize; 97 off_t offset; 98 } view_buffer_t; 99 100 /* The internal version of struct ld_plugin_input_file with a BFD 101 pointer. */ 102 typedef struct plugin_input_file 103 { 104 bfd *abfd; 105 view_buffer_t view_buffer; 106 char *name; 107 int fd; 108 bfd_boolean use_mmap; 109 off_t offset; 110 off_t filesize; 111 } plugin_input_file_t; 112 113 /* The master list of all plugins. */ 114 static plugin_t *plugins_list = NULL; 115 116 /* We keep a tail pointer for easy linking on the end. */ 117 static plugin_t **plugins_tail_chain_ptr = &plugins_list; 118 119 /* The last plugin added to the list, for receiving args. */ 120 static plugin_t *last_plugin = NULL; 121 122 /* The tail of the arg chain of the last plugin added to the list. */ 123 static plugin_arg_t **last_plugin_args_tail_chain_ptr = NULL; 124 125 /* The plugin which is currently having a callback executed. */ 126 static plugin_t *called_plugin = NULL; 127 128 /* Last plugin to cause an error, if any. */ 129 static const char *error_plugin = NULL; 130 131 /* State of linker "notice" interface before we poked at it. */ 132 static bfd_boolean orig_notice_all; 133 134 /* Original linker callbacks, and the plugin version. */ 135 static const struct bfd_link_callbacks *orig_callbacks; 136 static struct bfd_link_callbacks plugin_callbacks; 137 138 /* Set at all symbols read time, to avoid recursively offering the plugin 139 its own newly-added input files and libs to claim. */ 140 bfd_boolean no_more_claiming = FALSE; 141 142 #if HAVE_MMAP && HAVE_GETPAGESIZE 143 /* Page size used by mmap. */ 144 static off_t plugin_pagesize; 145 #endif 146 147 /* List of tags to set in the constant leading part of the tv array. */ 148 static const enum ld_plugin_tag tv_header_tags[] = 149 { 150 LDPT_MESSAGE, 151 LDPT_API_VERSION, 152 LDPT_GNU_LD_VERSION, 153 LDPT_LINKER_OUTPUT, 154 LDPT_OUTPUT_NAME, 155 LDPT_REGISTER_CLAIM_FILE_HOOK, 156 LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK, 157 LDPT_REGISTER_CLEANUP_HOOK, 158 LDPT_ADD_SYMBOLS, 159 LDPT_GET_INPUT_FILE, 160 LDPT_GET_VIEW, 161 LDPT_RELEASE_INPUT_FILE, 162 LDPT_GET_SYMBOLS, 163 LDPT_GET_SYMBOLS_V2, 164 LDPT_ADD_INPUT_FILE, 165 LDPT_ADD_INPUT_LIBRARY, 166 LDPT_SET_EXTRA_LIBRARY_PATH 167 }; 168 169 /* How many entries in the constant leading part of the tv array. */ 170 static const size_t tv_header_size = ARRAY_SIZE (tv_header_tags); 171 172 /* Forward references. */ 173 static bfd_boolean plugin_notice (struct bfd_link_info *, 174 struct bfd_link_hash_entry *, 175 struct bfd_link_hash_entry *, 176 bfd *, asection *, bfd_vma, flagword); 177 178 static const bfd_target * plugin_object_p (bfd *); 179 180 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) 181 182 #define RTLD_NOW 0 /* Dummy value. */ 183 184 static void * 185 dlopen (const char *file, int mode ATTRIBUTE_UNUSED) 186 { 187 // Use LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR to search the loaded library's 188 // directory to satisfy dependencies. 189 return LoadLibraryEx(file, NULL, 190 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | 191 LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); 192 } 193 194 static void * 195 dlsym (void *handle, const char *name) 196 { 197 return GetProcAddress (handle, name); 198 } 199 200 static int 201 dlclose (void *handle) 202 { 203 FreeLibrary (handle); 204 return 0; 205 } 206 207 static const char * 208 dlerror (void) 209 { 210 DWORD error = GetLastError(); 211 static char error_buf[512]; 212 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 213 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_buf, 214 sizeof(error_buf), NULL); 215 return error_buf; 216 } 217 218 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */ 219 220 #if !defined (HAVE_DLFCN_H) && !defined (HAVE_WINDOWS_H) 221 static const char * 222 dlerror (void) 223 { 224 return ""; 225 } 226 #endif 227 228 /* Helper function for exiting with error status. */ 229 static int 230 set_plugin_error (const char *plugin) 231 { 232 error_plugin = plugin; 233 return -1; 234 } 235 236 /* Test if an error occurred. */ 237 static bfd_boolean 238 plugin_error_p (void) 239 { 240 return error_plugin != NULL; 241 } 242 243 /* Return name of plugin which caused an error if any. */ 244 const char * 245 plugin_error_plugin (void) 246 { 247 return error_plugin ? error_plugin : _("<no plugin>"); 248 } 249 250 /* Handle -plugin arg: find and load plugin, or return error. */ 251 void 252 plugin_opt_plugin (const char *plugin) 253 { 254 plugin_t *newplug; 255 256 newplug = xmalloc (sizeof *newplug); 257 memset (newplug, 0, sizeof *newplug); 258 newplug->name = plugin; 259 newplug->dlhandle = dlopen (plugin, RTLD_NOW); 260 if (!newplug->dlhandle) 261 einfo (_("%P%F: %s: error loading plugin: %s\n"), plugin, dlerror ()); 262 263 /* Chain on end, so when we run list it is in command-line order. */ 264 *plugins_tail_chain_ptr = newplug; 265 plugins_tail_chain_ptr = &newplug->next; 266 267 /* Record it as current plugin for receiving args. */ 268 last_plugin = newplug; 269 last_plugin_args_tail_chain_ptr = &newplug->args; 270 } 271 272 /* Accumulate option arguments for last-loaded plugin, or return 273 error if none. */ 274 int 275 plugin_opt_plugin_arg (const char *arg) 276 { 277 plugin_arg_t *newarg; 278 279 if (!last_plugin) 280 return set_plugin_error (_("<no plugin>")); 281 282 /* Ignore -pass-through= from GCC driver. */ 283 if (*arg == '-') 284 { 285 const char *p = arg + 1; 286 287 if (*p == '-') 288 ++p; 289 if (strncmp (p, "pass-through=", 13) == 0) 290 return 0; 291 } 292 293 newarg = xmalloc (sizeof *newarg); 294 newarg->arg = arg; 295 newarg->next = NULL; 296 297 /* Chain on end to preserve command-line order. */ 298 *last_plugin_args_tail_chain_ptr = newarg; 299 last_plugin_args_tail_chain_ptr = &newarg->next; 300 last_plugin->n_args++; 301 return 0; 302 } 303 304 /* Generate a dummy BFD to represent an IR file, for any callers of 305 plugin_call_claim_file to use as the handle in the ld_plugin_input_file 306 struct that they build to pass in. The BFD is initially writable, so 307 that symbols can be added to it; it must be made readable after the 308 add_symbols hook has been called so that it can be read when linking. */ 309 static bfd * 310 plugin_get_ir_dummy_bfd (const char *name, bfd *srctemplate) 311 { 312 bfd *abfd; 313 bfd_boolean bfd_plugin_target; 314 315 bfd_use_reserved_id = 1; 316 bfd_plugin_target = bfd_plugin_target_p (srctemplate->xvec); 317 abfd = bfd_create (concat (name, IRONLY_SUFFIX, (const char *) NULL), 318 bfd_plugin_target ? link_info.output_bfd : srctemplate); 319 if (abfd != NULL) 320 { 321 abfd->flags |= BFD_LINKER_CREATED | BFD_PLUGIN; 322 if (!bfd_make_writable (abfd)) 323 goto report_error; 324 if (!bfd_plugin_target) 325 { 326 bfd_set_arch_info (abfd, bfd_get_arch_info (srctemplate)); 327 bfd_set_gp_size (abfd, bfd_get_gp_size (srctemplate)); 328 if (!bfd_copy_private_bfd_data (srctemplate, abfd)) 329 goto report_error; 330 } 331 { 332 flagword flags; 333 334 /* Create section to own the symbols. */ 335 flags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY 336 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE); 337 if (bfd_make_section_anyway_with_flags (abfd, ".text", flags)) 338 return abfd; 339 } 340 } 341 report_error: 342 einfo (_("could not create dummy IR bfd: %F%E\n")); 343 return NULL; 344 } 345 346 /* Check if the BFD passed in is an IR dummy object file. */ 347 static inline bfd_boolean 348 is_ir_dummy_bfd (const bfd *abfd) 349 { 350 /* ABFD can sometimes legitimately be NULL, e.g. when called from one 351 of the linker callbacks for a symbol in the *ABS* or *UND* sections. */ 352 return abfd != NULL && (abfd->flags & BFD_PLUGIN) != 0; 353 } 354 355 /* Helpers to convert between BFD and GOLD symbol formats. */ 356 static enum ld_plugin_status 357 asymbol_from_plugin_symbol (bfd *abfd, asymbol *asym, 358 const struct ld_plugin_symbol *ldsym) 359 { 360 flagword flags = BSF_NO_FLAGS; 361 struct bfd_section *section; 362 363 asym->the_bfd = abfd; 364 asym->name = (ldsym->version 365 ? concat (ldsym->name, "@", ldsym->version, (const char *) NULL) 366 : ldsym->name); 367 asym->value = 0; 368 switch (ldsym->def) 369 { 370 case LDPK_WEAKDEF: 371 flags = BSF_WEAK; 372 /* FALLTHRU */ 373 case LDPK_DEF: 374 flags |= BSF_GLOBAL; 375 if (ldsym->comdat_key) 376 { 377 char *name = concat (".gnu.linkonce.t.", ldsym->comdat_key, 378 (const char *) NULL); 379 section = bfd_get_section_by_name (abfd, name); 380 if (section != NULL) 381 free (name); 382 else 383 { 384 flagword sflags; 385 386 sflags = (SEC_CODE | SEC_HAS_CONTENTS | SEC_READONLY 387 | SEC_ALLOC | SEC_LOAD | SEC_KEEP | SEC_EXCLUDE 388 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD); 389 section = bfd_make_section_anyway_with_flags (abfd, name, sflags); 390 if (section == NULL) 391 return LDPS_ERR; 392 } 393 } 394 else 395 section = bfd_get_section_by_name (abfd, ".text"); 396 break; 397 398 case LDPK_WEAKUNDEF: 399 flags = BSF_WEAK; 400 /* FALLTHRU */ 401 case LDPK_UNDEF: 402 section = bfd_und_section_ptr; 403 break; 404 405 case LDPK_COMMON: 406 flags = BSF_GLOBAL; 407 section = bfd_com_section_ptr; 408 asym->value = ldsym->size; 409 /* For ELF targets, set alignment of common symbol to 1. */ 410 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) 411 { 412 ((elf_symbol_type *) asym)->internal_elf_sym.st_shndx = SHN_COMMON; 413 ((elf_symbol_type *) asym)->internal_elf_sym.st_value = 1; 414 } 415 break; 416 417 default: 418 return LDPS_ERR; 419 } 420 asym->flags = flags; 421 asym->section = section; 422 423 /* Visibility only applies on ELF targets. */ 424 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour) 425 { 426 elf_symbol_type *elfsym = elf_symbol_from (abfd, asym); 427 unsigned char visibility; 428 429 if (!elfsym) 430 einfo (_("%P%F: %s: non-ELF symbol in ELF BFD!\n"), asym->name); 431 switch (ldsym->visibility) 432 { 433 default: 434 einfo (_("%P%F: unknown ELF symbol visibility: %d!\n"), 435 ldsym->visibility); 436 case LDPV_DEFAULT: 437 visibility = STV_DEFAULT; 438 break; 439 case LDPV_PROTECTED: 440 visibility = STV_PROTECTED; 441 break; 442 case LDPV_INTERNAL: 443 visibility = STV_INTERNAL; 444 break; 445 case LDPV_HIDDEN: 446 visibility = STV_HIDDEN; 447 break; 448 } 449 elfsym->internal_elf_sym.st_other 450 = (visibility | (elfsym->internal_elf_sym.st_other 451 & ~ELF_ST_VISIBILITY (-1))); 452 } 453 454 return LDPS_OK; 455 } 456 457 /* Register a claim-file handler. */ 458 static enum ld_plugin_status 459 register_claim_file (ld_plugin_claim_file_handler handler) 460 { 461 ASSERT (called_plugin); 462 called_plugin->claim_file_handler = handler; 463 return LDPS_OK; 464 } 465 466 /* Register an all-symbols-read handler. */ 467 static enum ld_plugin_status 468 register_all_symbols_read (ld_plugin_all_symbols_read_handler handler) 469 { 470 ASSERT (called_plugin); 471 called_plugin->all_symbols_read_handler = handler; 472 return LDPS_OK; 473 } 474 475 /* Register a cleanup handler. */ 476 static enum ld_plugin_status 477 register_cleanup (ld_plugin_cleanup_handler handler) 478 { 479 ASSERT (called_plugin); 480 called_plugin->cleanup_handler = handler; 481 return LDPS_OK; 482 } 483 484 /* Add symbols from a plugin-claimed input file. */ 485 static enum ld_plugin_status 486 add_symbols (void *handle, int nsyms, const struct ld_plugin_symbol *syms) 487 { 488 asymbol **symptrs; 489 plugin_input_file_t *input = handle; 490 bfd *abfd = input->abfd; 491 int n; 492 493 ASSERT (called_plugin); 494 symptrs = xmalloc (nsyms * sizeof *symptrs); 495 for (n = 0; n < nsyms; n++) 496 { 497 enum ld_plugin_status rv; 498 asymbol *bfdsym; 499 500 bfdsym = bfd_make_empty_symbol (abfd); 501 symptrs[n] = bfdsym; 502 rv = asymbol_from_plugin_symbol (abfd, bfdsym, syms + n); 503 if (rv != LDPS_OK) 504 return rv; 505 } 506 bfd_set_symtab (abfd, symptrs, nsyms); 507 return LDPS_OK; 508 } 509 510 /* Get the input file information with an open (possibly re-opened) 511 file descriptor. */ 512 static enum ld_plugin_status 513 get_input_file (const void *handle, struct ld_plugin_input_file *file) 514 { 515 const plugin_input_file_t *input = handle; 516 517 ASSERT (called_plugin); 518 519 file->name = input->name; 520 file->offset = input->offset; 521 file->filesize = input->filesize; 522 file->handle = (void *) handle; 523 524 return LDPS_OK; 525 } 526 527 /* Get view of the input file. */ 528 static enum ld_plugin_status 529 get_view (const void *handle, const void **viewp) 530 { 531 plugin_input_file_t *input = (plugin_input_file_t *) handle; 532 char *buffer; 533 size_t size = input->filesize; 534 off_t offset = input->offset; 535 #if HAVE_MMAP && HAVE_GETPAGESIZE 536 off_t bias; 537 #endif 538 539 ASSERT (called_plugin); 540 541 /* FIXME: einfo should support %lld. */ 542 if ((off_t) size != input->filesize) 543 einfo (_("%P%F: unsupported input file size: %s (%ld bytes)\n"), 544 input->name, (long) input->filesize); 545 546 /* Check the cached view buffer. */ 547 if (input->view_buffer.addr != NULL 548 && input->view_buffer.filesize == size 549 && input->view_buffer.offset == offset) 550 { 551 *viewp = input->view_buffer.addr; 552 return LDPS_OK; 553 } 554 555 input->view_buffer.filesize = size; 556 input->view_buffer.offset = offset; 557 558 #if HAVE_MMAP 559 # if HAVE_GETPAGESIZE 560 bias = offset % plugin_pagesize; 561 offset -= bias; 562 size += bias; 563 # endif 564 buffer = mmap (NULL, size, PROT_READ, MAP_PRIVATE, input->fd, offset); 565 if (buffer != MAP_FAILED) 566 { 567 input->use_mmap = TRUE; 568 # if HAVE_GETPAGESIZE 569 buffer += bias; 570 # endif 571 } 572 else 573 #endif 574 { 575 char *p; 576 577 input->use_mmap = FALSE; 578 579 if (lseek (input->fd, offset, SEEK_SET) < 0) 580 return LDPS_ERR; 581 582 buffer = bfd_alloc (input->abfd, size); 583 if (buffer == NULL) 584 return LDPS_ERR; 585 586 p = buffer; 587 do 588 { 589 ssize_t got = read (input->fd, p, size); 590 if (got == 0) 591 break; 592 else if (got > 0) 593 { 594 p += got; 595 size -= got; 596 } 597 else if (errno != EINTR) 598 return LDPS_ERR; 599 } 600 while (size > 0); 601 } 602 603 input->view_buffer.addr = buffer; 604 *viewp = buffer; 605 606 return LDPS_OK; 607 } 608 609 /* Release the input file. */ 610 static enum ld_plugin_status 611 release_input_file (const void *handle) 612 { 613 plugin_input_file_t *input = (plugin_input_file_t *) handle; 614 ASSERT (called_plugin); 615 if (input->fd != -1) 616 { 617 close (input->fd); 618 input->fd = -1; 619 } 620 return LDPS_OK; 621 } 622 623 /* Return TRUE if a defined symbol might be reachable from outside the 624 universe of claimed objects. */ 625 static inline bfd_boolean 626 is_visible_from_outside (struct ld_plugin_symbol *lsym, 627 struct bfd_link_hash_entry *blhe) 628 { 629 struct bfd_sym_chain *sym; 630 631 if (bfd_link_relocatable (&link_info)) 632 return TRUE; 633 if (link_info.export_dynamic || bfd_link_dll (&link_info)) 634 { 635 /* Check if symbol is hidden by version script. */ 636 if (bfd_hide_sym_by_version (link_info.version_info, 637 blhe->root.string)) 638 return FALSE; 639 /* Only ELF symbols really have visibility. */ 640 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) 641 { 642 struct elf_link_hash_entry *el = (struct elf_link_hash_entry *)blhe; 643 int vis = ELF_ST_VISIBILITY (el->other); 644 return vis == STV_DEFAULT || vis == STV_PROTECTED; 645 } 646 /* On non-ELF targets, we can safely make inferences by considering 647 what visibility the plugin would have liked to apply when it first 648 sent us the symbol. During ELF symbol processing, visibility only 649 ever becomes more restrictive, not less, when symbols are merged, 650 so this is a conservative estimate; it may give false positives, 651 declaring something visible from outside when it in fact would 652 not have been, but this will only lead to missed optimisation 653 opportunities during LTRANS at worst; it will not give false 654 negatives, which can lead to the disastrous conclusion that the 655 related symbol is IRONLY. (See GCC PR46319 for an example.) */ 656 return (lsym->visibility == LDPV_DEFAULT 657 || lsym->visibility == LDPV_PROTECTED); 658 } 659 660 for (sym = &entry_symbol; sym != NULL; sym = sym->next) 661 if (sym->name 662 && strcmp (sym->name, blhe->root.string) == 0) 663 return TRUE; 664 665 return FALSE; 666 } 667 668 /* Get the symbol resolution info for a plugin-claimed input file. */ 669 static enum ld_plugin_status 670 get_symbols (const void *handle, int nsyms, struct ld_plugin_symbol *syms, 671 int def_ironly_exp) 672 { 673 const plugin_input_file_t *input = handle; 674 const bfd *abfd = (const bfd *) input->abfd; 675 int n; 676 677 ASSERT (called_plugin); 678 for (n = 0; n < nsyms; n++) 679 { 680 struct bfd_link_hash_entry *blhe; 681 asection *owner_sec; 682 int res; 683 684 if (syms[n].def != LDPK_UNDEF) 685 blhe = bfd_link_hash_lookup (link_info.hash, syms[n].name, 686 FALSE, FALSE, TRUE); 687 else 688 blhe = bfd_wrapped_link_hash_lookup (link_info.output_bfd, &link_info, 689 syms[n].name, FALSE, FALSE, TRUE); 690 if (!blhe) 691 { 692 /* The plugin is called to claim symbols in an archive element 693 from plugin_object_p. But those symbols aren't needed to 694 create output. They are defined and referenced only within 695 IR. */ 696 switch (syms[n].def) 697 { 698 default: 699 abort (); 700 case LDPK_UNDEF: 701 case LDPK_WEAKUNDEF: 702 res = LDPR_UNDEF; 703 break; 704 case LDPK_DEF: 705 case LDPK_WEAKDEF: 706 case LDPK_COMMON: 707 res = LDPR_PREVAILING_DEF_IRONLY; 708 break; 709 } 710 goto report_symbol; 711 } 712 713 /* Determine resolution from blhe type and symbol's original type. */ 714 if (blhe->type == bfd_link_hash_undefined 715 || blhe->type == bfd_link_hash_undefweak) 716 { 717 res = LDPR_UNDEF; 718 goto report_symbol; 719 } 720 if (blhe->type != bfd_link_hash_defined 721 && blhe->type != bfd_link_hash_defweak 722 && blhe->type != bfd_link_hash_common) 723 { 724 /* We should not have a new, indirect or warning symbol here. */ 725 einfo ("%P%F: %s: plugin symbol table corrupt (sym type %d)\n", 726 called_plugin->name, blhe->type); 727 } 728 729 /* Find out which section owns the symbol. Since it's not undef, 730 it must have an owner; if it's not a common symbol, both defs 731 and weakdefs keep it in the same place. */ 732 owner_sec = (blhe->type == bfd_link_hash_common 733 ? blhe->u.c.p->section 734 : blhe->u.def.section); 735 736 737 /* If it was originally undefined or common, then it has been 738 resolved; determine how. */ 739 if (syms[n].def == LDPK_UNDEF 740 || syms[n].def == LDPK_WEAKUNDEF 741 || syms[n].def == LDPK_COMMON) 742 { 743 if (owner_sec->owner == link_info.output_bfd) 744 res = LDPR_RESOLVED_EXEC; 745 else if (owner_sec->owner == abfd) 746 res = LDPR_PREVAILING_DEF_IRONLY; 747 else if (is_ir_dummy_bfd (owner_sec->owner)) 748 res = LDPR_RESOLVED_IR; 749 else if (owner_sec->owner != NULL 750 && (owner_sec->owner->flags & DYNAMIC) != 0) 751 res = LDPR_RESOLVED_DYN; 752 else 753 res = LDPR_RESOLVED_EXEC; 754 } 755 756 /* Was originally def, or weakdef. Does it prevail? If the 757 owner is the original dummy bfd that supplied it, then this 758 is the definition that has prevailed. */ 759 else if (owner_sec->owner == link_info.output_bfd) 760 res = LDPR_PREEMPTED_REG; 761 else if (owner_sec->owner == abfd) 762 res = LDPR_PREVAILING_DEF_IRONLY; 763 764 /* Was originally def, weakdef, or common, but has been pre-empted. */ 765 else if (is_ir_dummy_bfd (owner_sec->owner)) 766 res = LDPR_PREEMPTED_IR; 767 else 768 res = LDPR_PREEMPTED_REG; 769 770 if (res == LDPR_PREVAILING_DEF_IRONLY) 771 { 772 /* We need to know if the sym is referenced from non-IR files. Or 773 even potentially-referenced, perhaps in a future final link if 774 this is a partial one, perhaps dynamically at load-time if the 775 symbol is externally visible. */ 776 if (blhe->non_ir_ref) 777 res = LDPR_PREVAILING_DEF; 778 else if (is_visible_from_outside (&syms[n], blhe)) 779 res = def_ironly_exp; 780 } 781 782 report_symbol: 783 syms[n].resolution = res; 784 if (report_plugin_symbols) 785 einfo (_("%P: %B: symbol `%s' " 786 "definition: %d, visibility: %d, resolution: %d\n"), 787 abfd, syms[n].name, 788 syms[n].def, syms[n].visibility, res); 789 } 790 return LDPS_OK; 791 } 792 793 static enum ld_plugin_status 794 get_symbols_v1 (const void *handle, int nsyms, struct ld_plugin_symbol *syms) 795 { 796 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF); 797 } 798 799 static enum ld_plugin_status 800 get_symbols_v2 (const void *handle, int nsyms, struct ld_plugin_symbol *syms) 801 { 802 return get_symbols (handle, nsyms, syms, LDPR_PREVAILING_DEF_IRONLY_EXP); 803 } 804 805 /* Add a new (real) input file generated by a plugin. */ 806 static enum ld_plugin_status 807 add_input_file (const char *pathname) 808 { 809 lang_input_statement_type *is; 810 811 ASSERT (called_plugin); 812 is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_file_enum, 813 NULL); 814 if (!is) 815 return LDPS_ERR; 816 is->flags.lto_output = 1; 817 return LDPS_OK; 818 } 819 820 /* Add a new (real) library required by a plugin. */ 821 static enum ld_plugin_status 822 add_input_library (const char *pathname) 823 { 824 lang_input_statement_type *is; 825 826 ASSERT (called_plugin); 827 is = lang_add_input_file (xstrdup (pathname), lang_input_file_is_l_enum, 828 NULL); 829 if (!is) 830 return LDPS_ERR; 831 is->flags.lto_output = 1; 832 return LDPS_OK; 833 } 834 835 /* Set the extra library path to be used by libraries added via 836 add_input_library. */ 837 static enum ld_plugin_status 838 set_extra_library_path (const char *path) 839 { 840 ASSERT (called_plugin); 841 ldfile_add_library_path (xstrdup (path), FALSE); 842 return LDPS_OK; 843 } 844 845 /* Issue a diagnostic message from a plugin. */ 846 static enum ld_plugin_status 847 message (int level, const char *format, ...) 848 { 849 va_list args; 850 va_start (args, format); 851 852 switch (level) 853 { 854 case LDPL_INFO: 855 vfinfo (stdout, format, args, FALSE); 856 putchar ('\n'); 857 break; 858 case LDPL_WARNING: 859 { 860 char *newfmt = concat ("%P: warning: ", format, "\n", 861 (const char *) NULL); 862 vfinfo (stdout, newfmt, args, TRUE); 863 free (newfmt); 864 } 865 break; 866 case LDPL_FATAL: 867 case LDPL_ERROR: 868 default: 869 { 870 char *newfmt = concat (level == LDPL_FATAL ? "%P%F" : "%P%X", 871 ": error: ", format, "\n", 872 (const char *) NULL); 873 fflush (stdout); 874 vfinfo (stderr, newfmt, args, TRUE); 875 fflush (stderr); 876 free (newfmt); 877 } 878 break; 879 } 880 881 va_end (args); 882 return LDPS_OK; 883 } 884 885 /* Helper to size leading part of tv array and set it up. */ 886 static void 887 set_tv_header (struct ld_plugin_tv *tv) 888 { 889 size_t i; 890 891 /* Version info. */ 892 static const unsigned int major = (unsigned)(BFD_VERSION / 100000000UL); 893 static const unsigned int minor = (unsigned)(BFD_VERSION / 1000000UL) % 100; 894 895 for (i = 0; i < tv_header_size; i++) 896 { 897 tv[i].tv_tag = tv_header_tags[i]; 898 #define TVU(x) tv[i].tv_u.tv_ ## x 899 switch (tv[i].tv_tag) 900 { 901 case LDPT_MESSAGE: 902 TVU(message) = message; 903 break; 904 case LDPT_API_VERSION: 905 TVU(val) = LD_PLUGIN_API_VERSION; 906 break; 907 case LDPT_GNU_LD_VERSION: 908 TVU(val) = major * 100 + minor; 909 break; 910 case LDPT_LINKER_OUTPUT: 911 TVU(val) = (bfd_link_relocatable (&link_info) ? LDPO_REL 912 : bfd_link_pde (&link_info) ? LDPO_EXEC 913 : bfd_link_pie (&link_info) ? LDPO_PIE 914 : LDPO_DYN); 915 break; 916 case LDPT_OUTPUT_NAME: 917 TVU(string) = output_filename; 918 break; 919 case LDPT_REGISTER_CLAIM_FILE_HOOK: 920 TVU(register_claim_file) = register_claim_file; 921 break; 922 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: 923 TVU(register_all_symbols_read) = register_all_symbols_read; 924 break; 925 case LDPT_REGISTER_CLEANUP_HOOK: 926 TVU(register_cleanup) = register_cleanup; 927 break; 928 case LDPT_ADD_SYMBOLS: 929 TVU(add_symbols) = add_symbols; 930 break; 931 case LDPT_GET_INPUT_FILE: 932 TVU(get_input_file) = get_input_file; 933 break; 934 case LDPT_GET_VIEW: 935 TVU(get_view) = get_view; 936 break; 937 case LDPT_RELEASE_INPUT_FILE: 938 TVU(release_input_file) = release_input_file; 939 break; 940 case LDPT_GET_SYMBOLS: 941 TVU(get_symbols) = get_symbols_v1; 942 break; 943 case LDPT_GET_SYMBOLS_V2: 944 TVU(get_symbols) = get_symbols_v2; 945 break; 946 case LDPT_ADD_INPUT_FILE: 947 TVU(add_input_file) = add_input_file; 948 break; 949 case LDPT_ADD_INPUT_LIBRARY: 950 TVU(add_input_library) = add_input_library; 951 break; 952 case LDPT_SET_EXTRA_LIBRARY_PATH: 953 TVU(set_extra_library_path) = set_extra_library_path; 954 break; 955 default: 956 /* Added a new entry to the array without adding 957 a new case to set up its value is a bug. */ 958 FAIL (); 959 } 960 #undef TVU 961 } 962 } 963 964 /* Append the per-plugin args list and trailing LDPT_NULL to tv. */ 965 static void 966 set_tv_plugin_args (plugin_t *plugin, struct ld_plugin_tv *tv) 967 { 968 plugin_arg_t *arg = plugin->args; 969 while (arg) 970 { 971 tv->tv_tag = LDPT_OPTION; 972 tv->tv_u.tv_string = arg->arg; 973 arg = arg->next; 974 tv++; 975 } 976 tv->tv_tag = LDPT_NULL; 977 tv->tv_u.tv_val = 0; 978 } 979 980 /* Load up and initialise all plugins after argument parsing. */ 981 void 982 plugin_load_plugins (void) 983 { 984 struct ld_plugin_tv *my_tv; 985 unsigned int max_args = 0; 986 plugin_t *curplug = plugins_list; 987 988 /* If there are no plugins, we need do nothing this run. */ 989 if (!curplug) 990 return; 991 992 /* First pass over plugins to find max # args needed so that we 993 can size and allocate the tv array. */ 994 while (curplug) 995 { 996 if (curplug->n_args > max_args) 997 max_args = curplug->n_args; 998 curplug = curplug->next; 999 } 1000 1001 /* Allocate tv array and initialise constant part. */ 1002 my_tv = xmalloc ((max_args + 1 + tv_header_size) * sizeof *my_tv); 1003 set_tv_header (my_tv); 1004 1005 /* Pass over plugins again, activating them. */ 1006 curplug = plugins_list; 1007 while (curplug) 1008 { 1009 enum ld_plugin_status rv; 1010 ld_plugin_onload onloadfn; 1011 1012 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "onload"); 1013 if (!onloadfn) 1014 onloadfn = (ld_plugin_onload) dlsym (curplug->dlhandle, "_onload"); 1015 if (!onloadfn) 1016 einfo (_("%P%F: %s: error loading plugin: %s\n"), 1017 curplug->name, dlerror ()); 1018 set_tv_plugin_args (curplug, &my_tv[tv_header_size]); 1019 called_plugin = curplug; 1020 rv = (*onloadfn) (my_tv); 1021 called_plugin = NULL; 1022 if (rv != LDPS_OK) 1023 einfo (_("%P%F: %s: plugin error: %d\n"), curplug->name, rv); 1024 curplug = curplug->next; 1025 } 1026 1027 /* Since plugin(s) inited ok, assume they're going to want symbol 1028 resolutions, which needs us to track which symbols are referenced 1029 by non-IR files using the linker's notice callback. */ 1030 orig_notice_all = link_info.notice_all; 1031 orig_callbacks = link_info.callbacks; 1032 plugin_callbacks = *orig_callbacks; 1033 plugin_callbacks.notice = &plugin_notice; 1034 link_info.notice_all = TRUE; 1035 link_info.lto_plugin_active = TRUE; 1036 link_info.callbacks = &plugin_callbacks; 1037 1038 register_ld_plugin_object_p (plugin_object_p); 1039 1040 #if HAVE_MMAP && HAVE_GETPAGESIZE 1041 plugin_pagesize = getpagesize (); 1042 #endif 1043 } 1044 1045 /* Call 'claim file' hook for all plugins. */ 1046 static int 1047 plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed) 1048 { 1049 plugin_t *curplug = plugins_list; 1050 *claimed = FALSE; 1051 while (curplug && !*claimed) 1052 { 1053 if (curplug->claim_file_handler) 1054 { 1055 enum ld_plugin_status rv; 1056 called_plugin = curplug; 1057 rv = (*curplug->claim_file_handler) (file, claimed); 1058 called_plugin = NULL; 1059 if (rv != LDPS_OK) 1060 set_plugin_error (curplug->name); 1061 } 1062 curplug = curplug->next; 1063 } 1064 return plugin_error_p () ? -1 : 0; 1065 } 1066 1067 /* Duplicates a character string with memory attached to ABFD. */ 1068 1069 static char * 1070 plugin_strdup (bfd *abfd, const char *str) 1071 { 1072 size_t strlength; 1073 char *copy; 1074 strlength = strlen (str) + 1; 1075 copy = bfd_alloc (abfd, strlength); 1076 if (copy == NULL) 1077 einfo (_("%P%F: plugin_strdup failed to allocate memory: %s\n"), 1078 bfd_get_error ()); 1079 memcpy (copy, str, strlength); 1080 return copy; 1081 } 1082 1083 static const bfd_target * 1084 plugin_object_p (bfd *ibfd) 1085 { 1086 int claimed; 1087 plugin_input_file_t *input; 1088 off_t offset, filesize; 1089 struct ld_plugin_input_file file; 1090 bfd *abfd; 1091 bfd_boolean inarchive; 1092 const char *name; 1093 int fd; 1094 1095 /* Don't try the dummy object file. */ 1096 if ((ibfd->flags & BFD_PLUGIN) != 0) 1097 return NULL; 1098 1099 if (ibfd->plugin_format != bfd_plugin_unknown) 1100 { 1101 if (ibfd->plugin_format == bfd_plugin_yes) 1102 return ibfd->plugin_dummy_bfd->xvec; 1103 else 1104 return NULL; 1105 } 1106 1107 inarchive = (ibfd->my_archive != NULL 1108 && !bfd_is_thin_archive (ibfd->my_archive)); 1109 name = inarchive ? ibfd->my_archive->filename : ibfd->filename; 1110 fd = open (name, O_RDONLY | O_BINARY); 1111 1112 if (fd < 0) 1113 return NULL; 1114 1115 /* We create a dummy BFD, initially empty, to house whatever symbols 1116 the plugin may want to add. */ 1117 abfd = plugin_get_ir_dummy_bfd (ibfd->filename, ibfd); 1118 1119 input = bfd_alloc (abfd, sizeof (*input)); 1120 if (input == NULL) 1121 einfo (_("%P%F: plugin failed to allocate memory for input: %s\n"), 1122 bfd_get_error ()); 1123 1124 if (inarchive) 1125 { 1126 /* Offset and filesize must refer to the individual archive 1127 member, not the whole file, and must exclude the header. 1128 Fortunately for us, that is how the data is stored in the 1129 origin field of the bfd and in the arelt_data. */ 1130 offset = ibfd->origin; 1131 filesize = arelt_size (ibfd); 1132 } 1133 else 1134 { 1135 offset = 0; 1136 filesize = lseek (fd, 0, SEEK_END); 1137 1138 /* We must copy filename attached to ibfd if it is not an archive 1139 member since it may be freed by bfd_close below. */ 1140 name = plugin_strdup (abfd, name); 1141 } 1142 1143 file.name = name; 1144 file.offset = offset; 1145 file.filesize = filesize; 1146 file.fd = fd; 1147 file.handle = input; 1148 1149 input->abfd = abfd; 1150 input->view_buffer.addr = NULL; 1151 input->view_buffer.filesize = 0; 1152 input->view_buffer.offset = 0; 1153 input->fd = fd; 1154 input->use_mmap = FALSE; 1155 input->offset = offset; 1156 input->filesize = filesize; 1157 input->name = plugin_strdup (abfd, ibfd->filename); 1158 1159 claimed = 0; 1160 1161 if (plugin_call_claim_file (&file, &claimed)) 1162 einfo (_("%P%F: %s: plugin reported error claiming file\n"), 1163 plugin_error_plugin ()); 1164 1165 if (input->fd != -1 && ! bfd_plugin_target_p (ibfd->xvec)) 1166 { 1167 /* FIXME: fd belongs to us, not the plugin. GCC plugin, which 1168 doesn't need fd after plugin_call_claim_file, doesn't use 1169 BFD plugin target vector. Since GCC plugin doesn't call 1170 release_input_file, we close it here. LLVM plugin, which 1171 needs fd after plugin_call_claim_file and calls 1172 release_input_file after it is done, uses BFD plugin target 1173 vector. This scheme doesn't work when a plugin needs fd and 1174 doesn't use BFD plugin target vector neither. */ 1175 close (fd); 1176 input->fd = -1; 1177 } 1178 1179 if (claimed) 1180 { 1181 ibfd->plugin_format = bfd_plugin_yes; 1182 ibfd->plugin_dummy_bfd = abfd; 1183 bfd_make_readable (abfd); 1184 return abfd->xvec; 1185 } 1186 else 1187 { 1188 #if HAVE_MMAP 1189 if (input->use_mmap) 1190 { 1191 /* If plugin didn't claim the file, unmap the buffer. */ 1192 char *addr = input->view_buffer.addr; 1193 off_t size = input->view_buffer.filesize; 1194 # if HAVE_GETPAGESIZE 1195 off_t bias = input->view_buffer.offset % plugin_pagesize; 1196 size += bias; 1197 addr -= bias; 1198 # endif 1199 munmap (addr, size); 1200 } 1201 #endif 1202 1203 /* If plugin didn't claim the file, we don't need the dummy bfd. 1204 Can't avoid speculatively creating it, alas. */ 1205 ibfd->plugin_format = bfd_plugin_no; 1206 bfd_close_all_done (abfd); 1207 return NULL; 1208 } 1209 } 1210 1211 void 1212 plugin_maybe_claim (lang_input_statement_type *entry) 1213 { 1214 if (plugin_object_p (entry->the_bfd)) 1215 { 1216 bfd *abfd = entry->the_bfd->plugin_dummy_bfd; 1217 1218 /* Discard the real file's BFD and substitute the dummy one. */ 1219 1220 /* We can't call bfd_close on archives. BFD archive handling 1221 caches elements, and add_archive_element keeps pointers to 1222 the_bfd and the_bfd->filename in a lang_input_statement_type 1223 linker script statement. */ 1224 if (entry->the_bfd->my_archive == NULL) 1225 bfd_close (entry->the_bfd); 1226 entry->the_bfd = abfd; 1227 entry->flags.claimed = 1; 1228 } 1229 } 1230 1231 /* Call 'all symbols read' hook for all plugins. */ 1232 int 1233 plugin_call_all_symbols_read (void) 1234 { 1235 plugin_t *curplug = plugins_list; 1236 1237 /* Disable any further file-claiming. */ 1238 no_more_claiming = TRUE; 1239 1240 while (curplug) 1241 { 1242 if (curplug->all_symbols_read_handler) 1243 { 1244 enum ld_plugin_status rv; 1245 called_plugin = curplug; 1246 rv = (*curplug->all_symbols_read_handler) (); 1247 called_plugin = NULL; 1248 if (rv != LDPS_OK) 1249 set_plugin_error (curplug->name); 1250 } 1251 curplug = curplug->next; 1252 } 1253 return plugin_error_p () ? -1 : 0; 1254 } 1255 1256 /* Call 'cleanup' hook for all plugins at exit. */ 1257 void 1258 plugin_call_cleanup (void) 1259 { 1260 plugin_t *curplug = plugins_list; 1261 while (curplug) 1262 { 1263 if (curplug->cleanup_handler && !curplug->cleanup_done) 1264 { 1265 enum ld_plugin_status rv; 1266 curplug->cleanup_done = TRUE; 1267 called_plugin = curplug; 1268 rv = (*curplug->cleanup_handler) (); 1269 called_plugin = NULL; 1270 if (rv != LDPS_OK) 1271 info_msg (_("%P: %s: error in plugin cleanup: %d (ignored)\n"), 1272 curplug->name, rv); 1273 dlclose (curplug->dlhandle); 1274 } 1275 curplug = curplug->next; 1276 } 1277 } 1278 1279 /* To determine which symbols should be resolved LDPR_PREVAILING_DEF 1280 and which LDPR_PREVAILING_DEF_IRONLY, we notice all the symbols as 1281 the linker adds them to the linker hash table. Mark those 1282 referenced from a non-IR file with non_ir_ref. We have to 1283 notice_all symbols, because we won't necessarily know until later 1284 which ones will be contributed by IR files. */ 1285 static bfd_boolean 1286 plugin_notice (struct bfd_link_info *info, 1287 struct bfd_link_hash_entry *h, 1288 struct bfd_link_hash_entry *inh, 1289 bfd *abfd, 1290 asection *section, 1291 bfd_vma value, 1292 flagword flags) 1293 { 1294 struct bfd_link_hash_entry *orig_h = h; 1295 1296 if (h != NULL) 1297 { 1298 bfd *sym_bfd; 1299 1300 if (h->type == bfd_link_hash_warning) 1301 h = h->u.i.link; 1302 1303 /* Nothing to do here if this def/ref is from an IR dummy BFD. */ 1304 if (is_ir_dummy_bfd (abfd)) 1305 ; 1306 1307 /* Making an indirect symbol counts as a reference unless this 1308 is a brand new symbol. */ 1309 else if (bfd_is_ind_section (section) 1310 || (flags & BSF_INDIRECT) != 0) 1311 { 1312 /* ??? Some of this is questionable. See comments in 1313 _bfd_generic_link_add_one_symbol for case IND. */ 1314 if (h->type != bfd_link_hash_new) 1315 { 1316 h->non_ir_ref = TRUE; 1317 inh->non_ir_ref = TRUE; 1318 } 1319 else if (inh->type == bfd_link_hash_new) 1320 inh->non_ir_ref = TRUE; 1321 } 1322 1323 /* Nothing to do here for warning symbols. */ 1324 else if ((flags & BSF_WARNING) != 0) 1325 ; 1326 1327 /* Nothing to do here for constructor symbols. */ 1328 else if ((flags & BSF_CONSTRUCTOR) != 0) 1329 ; 1330 1331 /* If this is a ref, set non_ir_ref. */ 1332 else if (bfd_is_und_section (section)) 1333 { 1334 /* Replace the undefined dummy bfd with the real one. */ 1335 if ((h->type == bfd_link_hash_undefined 1336 || h->type == bfd_link_hash_undefweak) 1337 && (h->u.undef.abfd == NULL 1338 || (h->u.undef.abfd->flags & BFD_PLUGIN) != 0)) 1339 h->u.undef.abfd = abfd; 1340 h->non_ir_ref = TRUE; 1341 } 1342 1343 /* Otherwise, it must be a new def. */ 1344 else 1345 { 1346 /* A common symbol should be merged with other commons or 1347 defs with the same name. In particular, a common ought 1348 to be overridden by a def in a -flto object. In that 1349 sense a common is also a ref. */ 1350 if (bfd_is_com_section (section)) 1351 h->non_ir_ref = TRUE; 1352 1353 /* Ensure any symbol defined in an IR dummy BFD takes on a 1354 new value from a real BFD. Weak symbols are not normally 1355 overridden by a new weak definition, and strong symbols 1356 will normally cause multiple definition errors. Avoid 1357 this by making the symbol appear to be undefined. */ 1358 if (((h->type == bfd_link_hash_defweak 1359 || h->type == bfd_link_hash_defined) 1360 && is_ir_dummy_bfd (sym_bfd = h->u.def.section->owner)) 1361 || (h->type == bfd_link_hash_common 1362 && is_ir_dummy_bfd (sym_bfd = h->u.c.p->section->owner))) 1363 { 1364 h->type = bfd_link_hash_undefweak; 1365 h->u.undef.abfd = sym_bfd; 1366 } 1367 } 1368 } 1369 1370 /* Continue with cref/nocrossref/trace-sym processing. */ 1371 if (orig_h == NULL 1372 || orig_notice_all 1373 || (info->notice_hash != NULL 1374 && bfd_hash_lookup (info->notice_hash, orig_h->root.string, 1375 FALSE, FALSE) != NULL)) 1376 return (*orig_callbacks->notice) (info, orig_h, inh, 1377 abfd, section, value, flags); 1378 return TRUE; 1379 } 1380