Home | History | Annotate | Download | only in gold
      1 // plugin.cc -- plugin manager for gold      -*- C++ -*-
      2 
      3 // Copyright (C) 2008-2016 Free Software Foundation, Inc.
      4 // Written by Cary Coutant <ccoutant (at) google.com>.
      5 
      6 // This file is part of gold.
      7 
      8 // This program is free software; you can redistribute it and/or modify
      9 // it under the terms of the GNU General Public License as published by
     10 // the Free Software Foundation; either version 3 of the License, or
     11 // (at your option) any later version.
     12 
     13 // This program is distributed in the hope that it will be useful,
     14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 // GNU General Public License for more details.
     17 
     18 // You should have received a copy of the GNU General Public License
     19 // along with this program; if not, write to the Free Software
     20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     21 // MA 02110-1301, USA.
     22 
     23 #include "gold.h"
     24 
     25 #include <cstdio>
     26 #include <cstdarg>
     27 #include <cstring>
     28 #include <string>
     29 #include <vector>
     30 
     31 #ifdef ENABLE_PLUGINS
     32 #ifdef HAVE_DLFCN_H
     33 #include <dlfcn.h>
     34 #elif defined (HAVE_WINDOWS_H)
     35 #include <windows.h>
     36 #else
     37 #error Unknown how to handle dynamic-load-libraries.
     38 #endif
     39 
     40 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
     41 
     42 #define RTLD_NOW 0      /* Dummy value.  */
     43 static void *
     44 dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
     45 {
     46   // Use LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR to search the loaded library's
     47   // directory to satisfy dependencies.
     48   return LoadLibraryEx(file, NULL,
     49                        LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR |
     50                            LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
     51 }
     52 
     53 static void *
     54 dlsym(void *handle, const char *name)
     55 {
     56   return reinterpret_cast<void *>(
     57      GetProcAddress(static_cast<HMODULE>(handle),name));
     58 }
     59 
     60 static const char *
     61 dlerror(void)
     62 {
     63   DWORD error = GetLastError();
     64   static char error_buf[512];
     65   FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error,
     66                 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), error_buf,
     67                 sizeof(error_buf), NULL);
     68   return error_buf;
     69 }
     70 
     71 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)  */
     72 #endif /* ENABLE_PLUGINS */
     73 
     74 #include "parameters.h"
     75 #include "errors.h"
     76 #include "fileread.h"
     77 #include "layout.h"
     78 #include "options.h"
     79 #include "plugin.h"
     80 #include "target.h"
     81 #include "readsyms.h"
     82 #include "symtab.h"
     83 #include "descriptors.h"
     84 #include "elfcpp.h"
     85 
     86 namespace gold
     87 {
     88 
     89 #ifdef ENABLE_PLUGINS
     90 
     91 // The linker's exported interfaces.
     92 
     93 extern "C"
     94 {
     95 
     96 static enum ld_plugin_status
     97 register_claim_file(ld_plugin_claim_file_handler handler);
     98 
     99 static enum ld_plugin_status
    100 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
    101 
    102 static enum ld_plugin_status
    103 register_cleanup(ld_plugin_cleanup_handler handler);
    104 
    105 static enum ld_plugin_status
    106 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
    107 
    108 static enum ld_plugin_status
    109 get_input_file(const void *handle, struct ld_plugin_input_file *file);
    110 
    111 static enum ld_plugin_status
    112 get_view(const void *handle, const void **viewp);
    113 
    114 static enum ld_plugin_status
    115 release_input_file(const void *handle);
    116 
    117 static enum ld_plugin_status
    118 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
    119 
    120 static enum ld_plugin_status
    121 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
    122 
    123 static enum ld_plugin_status
    124 get_symbols_v3(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
    125 
    126 static enum ld_plugin_status
    127 add_input_file(const char *pathname);
    128 
    129 static enum ld_plugin_status
    130 add_input_library(const char *pathname);
    131 
    132 static enum ld_plugin_status
    133 set_extra_library_path(const char *path);
    134 
    135 static enum ld_plugin_status
    136 message(int level, const char *format, ...);
    137 
    138 static enum ld_plugin_status
    139 get_input_section_count(const void* handle, unsigned int* count);
    140 
    141 static enum ld_plugin_status
    142 get_input_section_type(const struct ld_plugin_section section,
    143                        unsigned int* type);
    144 
    145 static enum ld_plugin_status
    146 get_input_section_name(const struct ld_plugin_section section,
    147                        char** section_name_ptr);
    148 
    149 static enum ld_plugin_status
    150 get_input_section_contents(const struct ld_plugin_section section,
    151                            const unsigned char** section_contents,
    152 		           size_t* len);
    153 
    154 static enum ld_plugin_status
    155 update_section_order(const struct ld_plugin_section *section_list,
    156 		     unsigned int num_sections);
    157 
    158 static enum ld_plugin_status
    159 allow_section_ordering();
    160 
    161 static enum ld_plugin_status
    162 allow_unique_segment_for_sections();
    163 
    164 static enum ld_plugin_status
    165 unique_segment_for_sections(const char* segment_name,
    166 			    uint64_t flags,
    167 			    uint64_t align,
    168 			    const struct ld_plugin_section *section_list,
    169 			    unsigned int num_sections);
    170 
    171 static enum ld_plugin_status
    172 get_input_section_alignment(const struct ld_plugin_section section,
    173                             unsigned int* addralign);
    174 
    175 static enum ld_plugin_status
    176 get_input_section_size(const struct ld_plugin_section section,
    177                        uint64_t* secsize);
    178 
    179 };
    180 
    181 #endif // ENABLE_PLUGINS
    182 
    183 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
    184                                            off_t offset, off_t filesize);
    185 
    186 // Plugin methods.
    187 
    188 // Load one plugin library.
    189 
    190 void
    191 Plugin::load()
    192 {
    193 #ifdef ENABLE_PLUGINS
    194   // Load the plugin library.
    195   // FIXME: Look for the library in standard locations.
    196   this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
    197   if (this->handle_ == NULL)
    198     {
    199       gold_error(_("%s: could not load plugin library: %s"),
    200                  this->filename_.c_str(), dlerror());
    201       return;
    202     }
    203 
    204   // Find the plugin's onload entry point.
    205   void* ptr = dlsym(this->handle_, "onload");
    206   if (ptr == NULL)
    207     {
    208       gold_error(_("%s: could not find onload entry point"),
    209                  this->filename_.c_str());
    210       return;
    211     }
    212   ld_plugin_onload onload;
    213   gold_assert(sizeof(onload) == sizeof(ptr));
    214   memcpy(&onload, &ptr, sizeof(ptr));
    215 
    216   // Get the linker's version number.
    217   const char* ver = get_version_string();
    218   int major = 0;
    219   int minor = 0;
    220   sscanf(ver, "%d.%d", &major, &minor);
    221 
    222   // Allocate and populate a transfer vector.
    223   const int tv_fixed_size = 29;
    224 
    225   int tv_size = this->args_.size() + tv_fixed_size;
    226   ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
    227 
    228   // Put LDPT_MESSAGE at the front of the list so the plugin can use it
    229   // while processing subsequent entries.
    230   int i = 0;
    231   tv[i].tv_tag = LDPT_MESSAGE;
    232   tv[i].tv_u.tv_message = message;
    233 
    234   ++i;
    235   tv[i].tv_tag = LDPT_API_VERSION;
    236   tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
    237 
    238   ++i;
    239   tv[i].tv_tag = LDPT_GOLD_VERSION;
    240   tv[i].tv_u.tv_val = major * 100 + minor;
    241 
    242   ++i;
    243   tv[i].tv_tag = LDPT_LINKER_OUTPUT;
    244   if (parameters->options().relocatable())
    245     tv[i].tv_u.tv_val = LDPO_REL;
    246   else if (parameters->options().shared())
    247     tv[i].tv_u.tv_val = LDPO_DYN;
    248   else if (parameters->options().pie())
    249     tv[i].tv_u.tv_val = LDPO_PIE;
    250   else
    251     tv[i].tv_u.tv_val = LDPO_EXEC;
    252 
    253   ++i;
    254   tv[i].tv_tag = LDPT_OUTPUT_NAME;
    255   tv[i].tv_u.tv_string = parameters->options().output();
    256 
    257   for (unsigned int j = 0; j < this->args_.size(); ++j)
    258     {
    259       ++i;
    260       tv[i].tv_tag = LDPT_OPTION;
    261       tv[i].tv_u.tv_string = this->args_[j].c_str();
    262     }
    263 
    264   ++i;
    265   tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
    266   tv[i].tv_u.tv_register_claim_file = register_claim_file;
    267 
    268   ++i;
    269   tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
    270   tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
    271 
    272   ++i;
    273   tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
    274   tv[i].tv_u.tv_register_cleanup = register_cleanup;
    275 
    276   ++i;
    277   tv[i].tv_tag = LDPT_ADD_SYMBOLS;
    278   tv[i].tv_u.tv_add_symbols = add_symbols;
    279 
    280   ++i;
    281   tv[i].tv_tag = LDPT_GET_INPUT_FILE;
    282   tv[i].tv_u.tv_get_input_file = get_input_file;
    283 
    284   ++i;
    285   tv[i].tv_tag = LDPT_GET_VIEW;
    286   tv[i].tv_u.tv_get_view = get_view;
    287 
    288   ++i;
    289   tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
    290   tv[i].tv_u.tv_release_input_file = release_input_file;
    291 
    292   ++i;
    293   tv[i].tv_tag = LDPT_GET_SYMBOLS;
    294   tv[i].tv_u.tv_get_symbols = get_symbols;
    295 
    296   ++i;
    297   tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
    298   tv[i].tv_u.tv_get_symbols = get_symbols_v2;
    299 
    300   ++i;
    301   tv[i].tv_tag = LDPT_GET_SYMBOLS_V3;
    302   tv[i].tv_u.tv_get_symbols = get_symbols_v3;
    303 
    304   ++i;
    305   tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
    306   tv[i].tv_u.tv_add_input_file = add_input_file;
    307 
    308   ++i;
    309   tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
    310   tv[i].tv_u.tv_add_input_library = add_input_library;
    311 
    312   ++i;
    313   tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
    314   tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
    315 
    316   ++i;
    317   tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
    318   tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
    319 
    320   ++i;
    321   tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
    322   tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
    323 
    324   ++i;
    325   tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
    326   tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
    327 
    328   ++i;
    329   tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
    330   tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
    331 
    332   ++i;
    333   tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
    334   tv[i].tv_u.tv_update_section_order = update_section_order;
    335 
    336   ++i;
    337   tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
    338   tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
    339 
    340   ++i;
    341   tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
    342   tv[i].tv_u.tv_allow_unique_segment_for_sections
    343     = allow_unique_segment_for_sections;
    344 
    345   ++i;
    346   tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
    347   tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
    348 
    349   ++i;
    350   tv[i].tv_tag = LDPT_GET_INPUT_SECTION_ALIGNMENT;
    351   tv[i].tv_u.tv_get_input_section_alignment = get_input_section_alignment;
    352 
    353   ++i;
    354   tv[i].tv_tag = LDPT_GET_INPUT_SECTION_SIZE;
    355   tv[i].tv_u.tv_get_input_section_size = get_input_section_size;
    356 
    357   ++i;
    358   tv[i].tv_tag = LDPT_NULL;
    359   tv[i].tv_u.tv_val = 0;
    360 
    361   gold_assert(i == tv_size - 1);
    362 
    363   // Call the onload entry point.
    364   (*onload)(tv);
    365 
    366   delete[] tv;
    367 #endif // ENABLE_PLUGINS
    368 }
    369 
    370 // Call the plugin claim-file handler.
    371 
    372 inline bool
    373 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
    374 {
    375   int claimed = 0;
    376 
    377   if (this->claim_file_handler_ != NULL)
    378     {
    379       (*this->claim_file_handler_)(plugin_input_file, &claimed);
    380       if (claimed)
    381         return true;
    382     }
    383   return false;
    384 }
    385 
    386 // Call the all-symbols-read handler.
    387 
    388 inline void
    389 Plugin::all_symbols_read()
    390 {
    391   if (this->all_symbols_read_handler_ != NULL)
    392     (*this->all_symbols_read_handler_)();
    393 }
    394 
    395 // Call the cleanup handler.
    396 
    397 inline void
    398 Plugin::cleanup()
    399 {
    400   if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
    401     {
    402       // Set this flag before calling to prevent a recursive plunge
    403       // in the event that a plugin's cleanup handler issues a
    404       // fatal error.
    405       this->cleanup_done_ = true;
    406       (*this->cleanup_handler_)();
    407     }
    408 }
    409 
    410 // This task is used to rescan archives as needed.
    411 
    412 class Plugin_rescan : public Task
    413 {
    414  public:
    415   Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
    416     : this_blocker_(this_blocker), next_blocker_(next_blocker)
    417   { }
    418 
    419   ~Plugin_rescan()
    420   {
    421     delete this->this_blocker_;
    422   }
    423 
    424   Task_token*
    425   is_runnable()
    426   {
    427     if (this->this_blocker_->is_blocked())
    428       return this->this_blocker_;
    429     return NULL;
    430   }
    431 
    432   void
    433   locks(Task_locker* tl)
    434   { tl->add(this, this->next_blocker_); }
    435 
    436   void
    437   run(Workqueue*)
    438   { parameters->options().plugins()->rescan(this); }
    439 
    440   std::string
    441   get_name() const
    442   { return "Plugin_rescan"; }
    443 
    444  private:
    445   Task_token* this_blocker_;
    446   Task_token* next_blocker_;
    447 };
    448 
    449 // Plugin_manager methods.
    450 
    451 Plugin_manager::~Plugin_manager()
    452 {
    453   for (Plugin_list::iterator p = this->plugins_.begin();
    454        p != this->plugins_.end();
    455        ++p)
    456     delete *p;
    457   this->plugins_.clear();
    458   for (Object_list::iterator obj = this->objects_.begin();
    459        obj != this->objects_.end();
    460        ++obj)
    461     delete *obj;
    462   this->objects_.clear();
    463   delete this->lock_;
    464 }
    465 
    466 // Load all plugin libraries.
    467 
    468 void
    469 Plugin_manager::load_plugins(Layout* layout)
    470 {
    471   this->layout_ = layout;
    472   for (this->current_ = this->plugins_.begin();
    473        this->current_ != this->plugins_.end();
    474        ++this->current_)
    475     (*this->current_)->load();
    476 }
    477 
    478 // Call the plugin claim-file handlers in turn to see if any claim the file.
    479 
    480 Pluginobj*
    481 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
    482                            off_t filesize, Object* elf_object)
    483 {
    484   bool lock_initialized = this->initialize_lock_.initialize();
    485 
    486   gold_assert(lock_initialized);
    487   Hold_lock hl(*this->lock_);
    488   if (this->in_replacement_phase_)
    489     return NULL;
    490 
    491   unsigned int handle = this->objects_.size();
    492   this->input_file_ = input_file;
    493   this->plugin_input_file_.name = input_file->filename().c_str();
    494   this->plugin_input_file_.fd = input_file->file().descriptor();
    495   this->plugin_input_file_.offset = offset;
    496   this->plugin_input_file_.filesize = filesize;
    497   this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
    498   if (elf_object != NULL)
    499     this->objects_.push_back(elf_object);
    500   this->in_claim_file_handler_ = true;
    501 
    502   for (this->current_ = this->plugins_.begin();
    503        this->current_ != this->plugins_.end();
    504        ++this->current_)
    505     {
    506       if ((*this->current_)->claim_file(&this->plugin_input_file_))
    507         {
    508 	  this->any_claimed_ = true;
    509 	  this->in_claim_file_handler_ = false;
    510 
    511           if (this->objects_.size() > handle
    512               && this->objects_[handle]->pluginobj() != NULL)
    513             return this->objects_[handle]->pluginobj();
    514 
    515           // If the plugin claimed the file but did not call the
    516           // add_symbols callback, we need to create the Pluginobj now.
    517           Pluginobj* obj = this->make_plugin_object(handle);
    518           return obj;
    519         }
    520     }
    521 
    522   this->in_claim_file_handler_ = false;
    523   return NULL;
    524 }
    525 
    526 // Save an archive.  This is used so that a plugin can add a file
    527 // which refers to a symbol which was not previously referenced.  In
    528 // that case we want to pretend that the symbol was referenced before,
    529 // and pull in the archive object.
    530 
    531 void
    532 Plugin_manager::save_archive(Archive* archive)
    533 {
    534   if (this->in_replacement_phase_ || !this->any_claimed_)
    535     delete archive;
    536   else
    537     this->rescannable_.push_back(Rescannable(archive));
    538 }
    539 
    540 // Save an Input_group.  This is like save_archive.
    541 
    542 void
    543 Plugin_manager::save_input_group(Input_group* input_group)
    544 {
    545   if (this->in_replacement_phase_ || !this->any_claimed_)
    546     delete input_group;
    547   else
    548     this->rescannable_.push_back(Rescannable(input_group));
    549 }
    550 
    551 // Call the all-symbols-read handlers.
    552 
    553 void
    554 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
    555                                  Input_objects* input_objects,
    556 	                         Symbol_table* symtab,
    557 	                         Dirsearch* dirpath, Mapfile* mapfile,
    558 	                         Task_token** last_blocker)
    559 {
    560   this->in_replacement_phase_ = true;
    561   this->workqueue_ = workqueue;
    562   this->task_ = task;
    563   this->input_objects_ = input_objects;
    564   this->symtab_ = symtab;
    565   this->dirpath_ = dirpath;
    566   this->mapfile_ = mapfile;
    567   this->this_blocker_ = NULL;
    568 
    569   for (this->current_ = this->plugins_.begin();
    570        this->current_ != this->plugins_.end();
    571        ++this->current_)
    572     (*this->current_)->all_symbols_read();
    573 
    574   if (this->any_added_)
    575     {
    576       Task_token* next_blocker = new Task_token(true);
    577       next_blocker->add_blocker();
    578       workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
    579       this->this_blocker_ = next_blocker;
    580     }
    581 
    582   *last_blocker = this->this_blocker_;
    583 }
    584 
    585 // This is called when we see a new undefined symbol.  If we are in
    586 // the replacement phase, this means that we may need to rescan some
    587 // archives we have previously seen.
    588 
    589 void
    590 Plugin_manager::new_undefined_symbol(Symbol* sym)
    591 {
    592   if (this->in_replacement_phase_)
    593     this->undefined_symbols_.push_back(sym);
    594 }
    595 
    596 // Rescan archives as needed.  This handles the case where a new
    597 // object file added by a plugin has an undefined reference to some
    598 // symbol defined in an archive.
    599 
    600 void
    601 Plugin_manager::rescan(Task* task)
    602 {
    603   size_t rescan_pos = 0;
    604   size_t rescan_size = this->rescannable_.size();
    605   while (!this->undefined_symbols_.empty())
    606     {
    607       if (rescan_pos >= rescan_size)
    608 	{
    609 	  this->undefined_symbols_.clear();
    610 	  return;
    611 	}
    612 
    613       Undefined_symbol_list undefs;
    614       undefs.reserve(this->undefined_symbols_.size());
    615       this->undefined_symbols_.swap(undefs);
    616 
    617       size_t min_rescan_pos = rescan_size;
    618 
    619       for (Undefined_symbol_list::const_iterator p = undefs.begin();
    620 	   p != undefs.end();
    621 	   ++p)
    622 	{
    623 	  if (!(*p)->is_undefined())
    624 	    continue;
    625 
    626 	  this->undefined_symbols_.push_back(*p);
    627 
    628 	  // Find the first rescan archive which defines this symbol,
    629 	  // starting at the current rescan position.  The rescan position
    630 	  // exists so that given -la -lb -lc we don't look for undefined
    631 	  // symbols in -lb back in -la, but instead get the definition
    632 	  // from -lc.  Don't bother to look past the current minimum
    633 	  // rescan position.
    634 	  for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
    635 	    {
    636 	      if (this->rescannable_defines(i, *p))
    637 		{
    638 		  min_rescan_pos = i;
    639 		  break;
    640 		}
    641 	    }
    642 	}
    643 
    644       if (min_rescan_pos >= rescan_size)
    645 	{
    646 	  // We didn't find any rescannable archives which define any
    647 	  // undefined symbols.
    648 	  return;
    649 	}
    650 
    651       const Rescannable& r(this->rescannable_[min_rescan_pos]);
    652       if (r.is_archive)
    653 	{
    654 	  Task_lock_obj<Archive> tl(task, r.u.archive);
    655 	  r.u.archive->add_symbols(this->symtab_, this->layout_,
    656 				   this->input_objects_, this->mapfile_);
    657 	}
    658       else
    659 	{
    660 	  size_t next_saw_undefined = this->symtab_->saw_undefined();
    661 	  size_t saw_undefined;
    662 	  do
    663 	    {
    664 	      saw_undefined = next_saw_undefined;
    665 
    666 	      for (Input_group::const_iterator p = r.u.input_group->begin();
    667 		   p != r.u.input_group->end();
    668 		   ++p)
    669 		{
    670 		  Task_lock_obj<Archive> tl(task, *p);
    671 
    672 		  (*p)->add_symbols(this->symtab_, this->layout_,
    673 				    this->input_objects_, this->mapfile_);
    674 		}
    675 
    676 	      next_saw_undefined = this->symtab_->saw_undefined();
    677 	    }
    678 	  while (saw_undefined != next_saw_undefined);
    679 	}
    680 
    681       for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
    682 	{
    683 	  if (this->rescannable_[i].is_archive)
    684 	    delete this->rescannable_[i].u.archive;
    685 	  else
    686 	    delete this->rescannable_[i].u.input_group;
    687 	}
    688 
    689       rescan_pos = min_rescan_pos + 1;
    690     }
    691 }
    692 
    693 // Return whether the rescannable at index I defines SYM.
    694 
    695 bool
    696 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
    697 {
    698   const Rescannable& r(this->rescannable_[i]);
    699   if (r.is_archive)
    700     return r.u.archive->defines_symbol(sym);
    701   else
    702     {
    703       for (Input_group::const_iterator p = r.u.input_group->begin();
    704 	   p != r.u.input_group->end();
    705 	   ++p)
    706 	{
    707 	  if ((*p)->defines_symbol(sym))
    708 	    return true;
    709 	}
    710       return false;
    711     }
    712 }
    713 
    714 // Layout deferred objects.
    715 
    716 void
    717 Plugin_manager::layout_deferred_objects()
    718 {
    719   Deferred_layout_list::iterator obj;
    720 
    721   for (obj = this->deferred_layout_objects_.begin();
    722        obj != this->deferred_layout_objects_.end();
    723        ++obj)
    724     {
    725       // Lock the object so we can read from it.  This is only called
    726       // single-threaded from queue_middle_tasks, so it is OK to lock.
    727       // Unfortunately we have no way to pass in a Task token.
    728       const Task* dummy_task = reinterpret_cast<const Task*>(-1);
    729       Task_lock_obj<Object> tl(dummy_task, *obj);
    730       (*obj)->layout_deferred_sections(this->layout_);
    731     }
    732 }
    733 
    734 // Call the cleanup handlers.
    735 
    736 void
    737 Plugin_manager::cleanup()
    738 {
    739   if (this->any_added_)
    740     {
    741       // If any input files were added, close all the input files.
    742       // This is because the plugin may want to remove them, and on
    743       // Windows you are not allowed to remove an open file.
    744       close_all_descriptors();
    745     }
    746 
    747   for (this->current_ = this->plugins_.begin();
    748        this->current_ != this->plugins_.end();
    749        ++this->current_)
    750     (*this->current_)->cleanup();
    751 }
    752 
    753 // Make a new Pluginobj object.  This is called when the plugin calls
    754 // the add_symbols API.
    755 
    756 Pluginobj*
    757 Plugin_manager::make_plugin_object(unsigned int handle)
    758 {
    759   // Make sure we aren't asked to make an object for the same handle twice.
    760   if (this->objects_.size() != handle
    761       && this->objects_[handle]->pluginobj() != NULL)
    762     return NULL;
    763 
    764   Pluginobj* obj = make_sized_plugin_object(this->input_file_,
    765                                             this->plugin_input_file_.offset,
    766                                             this->plugin_input_file_.filesize);
    767 
    768 
    769   // If the elf object for this file was pushed into the objects_ vector, delete
    770   // it to make room for the Pluginobj as this file is claimed.
    771   if (this->objects_.size() != handle)
    772     this->objects_.pop_back();
    773 
    774   this->objects_.push_back(obj);
    775   return obj;
    776 }
    777 
    778 // Get the input file information with an open (possibly re-opened)
    779 // file descriptor.
    780 
    781 ld_plugin_status
    782 Plugin_manager::get_input_file(unsigned int handle,
    783                                struct ld_plugin_input_file* file)
    784 {
    785   Pluginobj* obj = this->object(handle)->pluginobj();
    786   if (obj == NULL)
    787     return LDPS_BAD_HANDLE;
    788 
    789   obj->lock(this->task_);
    790   file->name = obj->filename().c_str();
    791   file->fd = obj->descriptor();
    792   file->offset = obj->offset();
    793   file->filesize = obj->filesize();
    794   file->handle = reinterpret_cast<void*>(handle);
    795   return LDPS_OK;
    796 }
    797 
    798 // Release the input file.
    799 
    800 ld_plugin_status
    801 Plugin_manager::release_input_file(unsigned int handle)
    802 {
    803   if (this->object(handle) == NULL)
    804     return LDPS_BAD_HANDLE;
    805 
    806   Pluginobj* obj = this->object(handle)->pluginobj();
    807 
    808   if (obj == NULL)
    809     return LDPS_BAD_HANDLE;
    810 
    811   obj->unlock(this->task_);
    812   return LDPS_OK;
    813 }
    814 
    815 // Get the elf object corresponding to the handle. Return NULL if we
    816 // found a Pluginobj instead.
    817 
    818 Object*
    819 Plugin_manager::get_elf_object(const void* handle)
    820 {
    821   Object* obj = this->object(
    822       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
    823 
    824   // The object should not be a Pluginobj.
    825   if (obj == NULL
    826       || obj->pluginobj() != NULL)
    827     return NULL;
    828 
    829   return obj;
    830 }
    831 
    832 ld_plugin_status
    833 Plugin_manager::get_view(unsigned int handle, const void **viewp)
    834 {
    835   off_t offset;
    836   size_t filesize;
    837   Input_file *input_file;
    838   if (this->in_claim_file_handler_)
    839     {
    840       // We are being called from the claim_file hook.
    841       const struct ld_plugin_input_file &f = this->plugin_input_file_;
    842       offset = f.offset;
    843       filesize = f.filesize;
    844       input_file = this->input_file_;
    845     }
    846   else
    847     {
    848       // An already claimed file.
    849       if (this->object(handle) == NULL)
    850         return LDPS_BAD_HANDLE;
    851       Pluginobj* obj = this->object(handle)->pluginobj();
    852       if (obj == NULL)
    853         return LDPS_BAD_HANDLE;
    854       offset = obj->offset();
    855       filesize = obj->filesize();
    856       input_file = obj->input_file();
    857     }
    858   *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
    859                                                false);
    860   return LDPS_OK;
    861 }
    862 
    863 // Add a new library path.
    864 
    865 ld_plugin_status
    866 Plugin_manager::set_extra_library_path(const char* path)
    867 {
    868   this->extra_search_path_ = std::string(path);
    869   return LDPS_OK;
    870 }
    871 
    872 // Add a new input file.
    873 
    874 ld_plugin_status
    875 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
    876 {
    877   Input_file_argument file(pathname,
    878                            (is_lib
    879                             ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
    880                             : Input_file_argument::INPUT_FILE_TYPE_FILE),
    881                            (is_lib
    882                             ? this->extra_search_path_.c_str()
    883                             : ""),
    884                            false,
    885                            this->options_);
    886   Input_argument* input_argument = new Input_argument(file);
    887   Task_token* next_blocker = new Task_token(true);
    888   next_blocker->add_blocker();
    889   if (parameters->incremental())
    890     gold_error(_("input files added by plug-ins in --incremental mode not "
    891 		 "supported yet"));
    892   this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
    893                                                 this->symtab_,
    894                                                 this->layout_,
    895                                                 this->dirpath_,
    896 						0,
    897                                                 this->mapfile_,
    898                                                 input_argument,
    899                                                 NULL,
    900                                                 NULL,
    901                                                 this->this_blocker_,
    902                                                 next_blocker));
    903   this->this_blocker_ = next_blocker;
    904   this->any_added_ = true;
    905   return LDPS_OK;
    906 }
    907 
    908 // Class Pluginobj.
    909 
    910 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
    911                      off_t offset, off_t filesize)
    912   : Object(name, input_file, false, offset),
    913     nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
    914 {
    915 }
    916 
    917 // Return TRUE if a defined symbol is referenced from outside the
    918 // universe of claimed objects.  Only references from relocatable,
    919 // non-IR (unclaimed) objects count as a reference.  References from
    920 // dynamic objects count only as "visible".
    921 
    922 static inline bool
    923 is_referenced_from_outside(Symbol* lsym)
    924 {
    925   if (lsym->in_real_elf())
    926     return true;
    927   if (parameters->options().relocatable())
    928     return true;
    929   if (parameters->options().is_undefined(lsym->name()))
    930     return true;
    931   return false;
    932 }
    933 
    934 // Return TRUE if a defined symbol might be reachable from outside the
    935 // load module.
    936 
    937 static inline bool
    938 is_visible_from_outside(Symbol* lsym)
    939 {
    940   if (lsym->in_dyn())
    941     return true;
    942   if (parameters->options().export_dynamic() || parameters->options().shared())
    943     return lsym->is_externally_visible();
    944   return false;
    945 }
    946 
    947 // Get symbol resolution info.
    948 
    949 ld_plugin_status
    950 Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
    951 				      int nsyms,
    952 				      ld_plugin_symbol* syms,
    953 				      int version) const
    954 {
    955   // For version 1 of this interface, we cannot use
    956   // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
    957   // instead.
    958   const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
    959       = (version > 1
    960 	 ? LDPR_PREVAILING_DEF_IRONLY_EXP
    961 	 : LDPR_PREVAILING_DEF);
    962 
    963   if (nsyms > this->nsyms_)
    964     return LDPS_NO_SYMS;
    965 
    966   if (static_cast<size_t>(nsyms) > this->symbols_.size())
    967     {
    968       // We never decided to include this object. We mark all symbols as
    969       // preempted.
    970       gold_assert(this->symbols_.size() == 0);
    971       for (int i = 0; i < nsyms; i++)
    972         syms[i].resolution = LDPR_PREEMPTED_REG;
    973       return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
    974     }
    975 
    976   for (int i = 0; i < nsyms; i++)
    977     {
    978       ld_plugin_symbol* isym = &syms[i];
    979       Symbol* lsym = this->symbols_[i];
    980       if (lsym->is_forwarder())
    981         lsym = symtab->resolve_forwards(lsym);
    982       ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
    983 
    984       if (lsym->is_undefined())
    985         // The symbol remains undefined.
    986         res = LDPR_UNDEF;
    987       else if (isym->def == LDPK_UNDEF
    988                || isym->def == LDPK_WEAKUNDEF
    989                || isym->def == LDPK_COMMON)
    990         {
    991           // The original symbol was undefined or common.
    992           if (lsym->source() != Symbol::FROM_OBJECT)
    993             res = LDPR_RESOLVED_EXEC;
    994           else if (lsym->object()->pluginobj() == this)
    995 	    {
    996 	      if (is_referenced_from_outside(lsym))
    997 		res = LDPR_PREVAILING_DEF;
    998 	      else if (is_visible_from_outside(lsym))
    999 		res = ldpr_prevailing_def_ironly_exp;
   1000 	      else
   1001 		res = LDPR_PREVAILING_DEF_IRONLY;
   1002 	    }
   1003           else if (lsym->object()->pluginobj() != NULL)
   1004             res = LDPR_RESOLVED_IR;
   1005           else if (lsym->object()->is_dynamic())
   1006             res = LDPR_RESOLVED_DYN;
   1007           else
   1008             res = LDPR_RESOLVED_EXEC;
   1009         }
   1010       else
   1011         {
   1012           // The original symbol was a definition.
   1013           if (lsym->source() != Symbol::FROM_OBJECT)
   1014             res = LDPR_PREEMPTED_REG;
   1015           else if (lsym->object() == static_cast<const Object*>(this))
   1016 	    {
   1017 	      if (is_referenced_from_outside(lsym))
   1018 		res = LDPR_PREVAILING_DEF;
   1019 	      else if (is_visible_from_outside(lsym))
   1020 		res = ldpr_prevailing_def_ironly_exp;
   1021 	      else
   1022 		res = LDPR_PREVAILING_DEF_IRONLY;
   1023 	    }
   1024           else
   1025             res = (lsym->object()->pluginobj() != NULL
   1026                    ? LDPR_PREEMPTED_IR
   1027                    : LDPR_PREEMPTED_REG);
   1028         }
   1029       isym->resolution = res;
   1030     }
   1031   return LDPS_OK;
   1032 }
   1033 
   1034 // Return TRUE if the comdat group with key COMDAT_KEY from this object
   1035 // should be kept.
   1036 
   1037 bool
   1038 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
   1039 {
   1040   std::pair<Comdat_map::iterator, bool> ins =
   1041     this->comdat_map_.insert(std::make_pair(comdat_key, false));
   1042 
   1043   // If this is the first time we've seen this comdat key, ask the
   1044   // layout object whether it should be included.
   1045   if (ins.second)
   1046     ins.first->second = layout->find_or_add_kept_section(comdat_key,
   1047 							 NULL, 0, true,
   1048 							 true, NULL);
   1049 
   1050   return ins.first->second;
   1051 }
   1052 
   1053 // Class Sized_pluginobj.
   1054 
   1055 template<int size, bool big_endian>
   1056 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
   1057     const std::string& name,
   1058     Input_file* input_file,
   1059     off_t offset,
   1060     off_t filesize)
   1061   : Pluginobj(name, input_file, offset, filesize)
   1062 {
   1063 }
   1064 
   1065 // Read the symbols.  Not used for plugin objects.
   1066 
   1067 template<int size, bool big_endian>
   1068 void
   1069 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
   1070 {
   1071   gold_unreachable();
   1072 }
   1073 
   1074 // Lay out the input sections.  Not used for plugin objects.
   1075 
   1076 template<int size, bool big_endian>
   1077 void
   1078 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
   1079                                              Read_symbols_data*)
   1080 {
   1081   gold_unreachable();
   1082 }
   1083 
   1084 // Add the symbols to the symbol table.
   1085 
   1086 template<int size, bool big_endian>
   1087 void
   1088 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
   1089                                                   Read_symbols_data*,
   1090                                                   Layout* layout)
   1091 {
   1092   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
   1093   unsigned char symbuf[sym_size];
   1094   elfcpp::Sym<size, big_endian> sym(symbuf);
   1095   elfcpp::Sym_write<size, big_endian> osym(symbuf);
   1096 
   1097   this->symbols_.resize(this->nsyms_);
   1098 
   1099   for (int i = 0; i < this->nsyms_; ++i)
   1100     {
   1101       const struct ld_plugin_symbol* isym = &this->syms_[i];
   1102       const char* name = isym->name;
   1103       const char* ver = isym->version;
   1104       elfcpp::Elf_Half shndx;
   1105       elfcpp::STB bind;
   1106       elfcpp::STV vis;
   1107 
   1108       if (name != NULL && name[0] == '\0')
   1109         name = NULL;
   1110       if (ver != NULL && ver[0] == '\0')
   1111         ver = NULL;
   1112 
   1113       switch (isym->def)
   1114         {
   1115         case LDPK_WEAKDEF:
   1116         case LDPK_WEAKUNDEF:
   1117           bind = elfcpp::STB_WEAK;
   1118           break;
   1119         case LDPK_DEF:
   1120         case LDPK_UNDEF:
   1121         case LDPK_COMMON:
   1122         default:
   1123           bind = elfcpp::STB_GLOBAL;
   1124           break;
   1125         }
   1126 
   1127       switch (isym->def)
   1128         {
   1129         case LDPK_DEF:
   1130         case LDPK_WEAKDEF:
   1131           shndx = elfcpp::SHN_ABS;
   1132           break;
   1133         case LDPK_COMMON:
   1134           shndx = elfcpp::SHN_COMMON;
   1135           break;
   1136         case LDPK_UNDEF:
   1137         case LDPK_WEAKUNDEF:
   1138         default:
   1139           shndx = elfcpp::SHN_UNDEF;
   1140           break;
   1141         }
   1142 
   1143       switch (isym->visibility)
   1144         {
   1145         case LDPV_PROTECTED:
   1146           vis = elfcpp::STV_PROTECTED;
   1147           break;
   1148         case LDPV_INTERNAL:
   1149           vis = elfcpp::STV_INTERNAL;
   1150           break;
   1151         case LDPV_HIDDEN:
   1152           vis = elfcpp::STV_HIDDEN;
   1153           break;
   1154         case LDPV_DEFAULT:
   1155         default:
   1156           vis = elfcpp::STV_DEFAULT;
   1157           break;
   1158         }
   1159 
   1160       if (isym->comdat_key != NULL
   1161           && isym->comdat_key[0] != '\0'
   1162           && !this->include_comdat_group(isym->comdat_key, layout))
   1163         shndx = elfcpp::SHN_UNDEF;
   1164 
   1165       osym.put_st_name(0);
   1166       osym.put_st_value(0);
   1167       osym.put_st_size(0);
   1168       osym.put_st_info(bind, elfcpp::STT_NOTYPE);
   1169       osym.put_st_other(vis, 0);
   1170       osym.put_st_shndx(shndx);
   1171 
   1172       this->symbols_[i] =
   1173         symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
   1174     }
   1175 }
   1176 
   1177 template<int size, bool big_endian>
   1178 Archive::Should_include
   1179 Sized_pluginobj<size, big_endian>::do_should_include_member(
   1180     Symbol_table* symtab,
   1181     Layout* layout,
   1182     Read_symbols_data*,
   1183     std::string* why)
   1184 {
   1185   char* tmpbuf = NULL;
   1186   size_t tmpbuflen = 0;
   1187 
   1188   for (int i = 0; i < this->nsyms_; ++i)
   1189     {
   1190       const struct ld_plugin_symbol& sym = this->syms_[i];
   1191       if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
   1192         continue;
   1193       const char* name = sym.name;
   1194       Symbol* symbol;
   1195       Archive::Should_include t = Archive::should_include_member(symtab,
   1196 								 layout,
   1197 								 name,
   1198 								 &symbol, why,
   1199 								 &tmpbuf,
   1200 								 &tmpbuflen);
   1201       if (t == Archive::SHOULD_INCLUDE_YES)
   1202 	{
   1203 	  if (tmpbuf != NULL)
   1204 	    free(tmpbuf);
   1205 	  return t;
   1206 	}
   1207     }
   1208   if (tmpbuf != NULL)
   1209     free(tmpbuf);
   1210   return Archive::SHOULD_INCLUDE_UNKNOWN;
   1211 }
   1212 
   1213 // Iterate over global symbols, calling a visitor class V for each.
   1214 
   1215 template<int size, bool big_endian>
   1216 void
   1217 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
   1218     Read_symbols_data*,
   1219     Library_base::Symbol_visitor_base* v)
   1220 {
   1221   for (int i = 0; i < this->nsyms_; ++i)
   1222     {
   1223       const struct ld_plugin_symbol& sym = this->syms_[i];
   1224       if (sym.def != LDPK_UNDEF)
   1225 	v->visit(sym.name);
   1226     }
   1227 }
   1228 
   1229 // Iterate over local symbols, calling a visitor class V for each GOT offset
   1230 // associated with a local symbol.
   1231 template<int size, bool big_endian>
   1232 void
   1233 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
   1234     Got_offset_list::Visitor*) const
   1235 {
   1236   gold_unreachable();
   1237 }
   1238 
   1239 // Get the size of a section.  Not used for plugin objects.
   1240 
   1241 template<int size, bool big_endian>
   1242 uint64_t
   1243 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
   1244 {
   1245   gold_unreachable();
   1246   return 0;
   1247 }
   1248 
   1249 // Get the name of a section.  Not used for plugin objects.
   1250 
   1251 template<int size, bool big_endian>
   1252 std::string
   1253 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
   1254 {
   1255   gold_unreachable();
   1256   return std::string();
   1257 }
   1258 
   1259 // Return a view of the contents of a section.  Not used for plugin objects.
   1260 
   1261 template<int size, bool big_endian>
   1262 const unsigned char*
   1263 Sized_pluginobj<size, big_endian>::do_section_contents(
   1264     unsigned int,
   1265     section_size_type*,
   1266     bool)
   1267 {
   1268   gold_unreachable();
   1269   return NULL;
   1270 }
   1271 
   1272 // Return section flags.  Not used for plugin objects.
   1273 
   1274 template<int size, bool big_endian>
   1275 uint64_t
   1276 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
   1277 {
   1278   gold_unreachable();
   1279   return 0;
   1280 }
   1281 
   1282 // Return section entsize.  Not used for plugin objects.
   1283 
   1284 template<int size, bool big_endian>
   1285 uint64_t
   1286 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
   1287 {
   1288   gold_unreachable();
   1289   return 0;
   1290 }
   1291 
   1292 // Return section address.  Not used for plugin objects.
   1293 
   1294 template<int size, bool big_endian>
   1295 uint64_t
   1296 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
   1297 {
   1298   gold_unreachable();
   1299   return 0;
   1300 }
   1301 
   1302 // Return section type.  Not used for plugin objects.
   1303 
   1304 template<int size, bool big_endian>
   1305 unsigned int
   1306 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
   1307 {
   1308   gold_unreachable();
   1309   return 0;
   1310 }
   1311 
   1312 // Return the section link field.  Not used for plugin objects.
   1313 
   1314 template<int size, bool big_endian>
   1315 unsigned int
   1316 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
   1317 {
   1318   gold_unreachable();
   1319   return 0;
   1320 }
   1321 
   1322 // Return the section link field.  Not used for plugin objects.
   1323 
   1324 template<int size, bool big_endian>
   1325 unsigned int
   1326 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
   1327 {
   1328   gold_unreachable();
   1329   return 0;
   1330 }
   1331 
   1332 // Return the section alignment.  Not used for plugin objects.
   1333 
   1334 template<int size, bool big_endian>
   1335 uint64_t
   1336 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
   1337 {
   1338   gold_unreachable();
   1339   return 0;
   1340 }
   1341 
   1342 // Return the Xindex structure to use.  Not used for plugin objects.
   1343 
   1344 template<int size, bool big_endian>
   1345 Xindex*
   1346 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
   1347 {
   1348   gold_unreachable();
   1349   return NULL;
   1350 }
   1351 
   1352 // Get symbol counts.  Don't count plugin objects; the replacement
   1353 // files will provide the counts.
   1354 
   1355 template<int size, bool big_endian>
   1356 void
   1357 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
   1358     const Symbol_table*,
   1359     size_t* defined,
   1360     size_t* used) const
   1361 {
   1362   *defined = 0;
   1363   *used = 0;
   1364 }
   1365 
   1366 // Get symbols.  Not used for plugin objects.
   1367 
   1368 template<int size, bool big_endian>
   1369 const Object::Symbols*
   1370 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
   1371 {
   1372   gold_unreachable();
   1373 }
   1374 
   1375 // Class Plugin_finish.  This task runs after all replacement files have
   1376 // been added.  For now, it's a placeholder for a possible plugin API
   1377 // to allow the plugin to release most of its resources.  The cleanup
   1378 // handlers must be called later, because they can remove the temporary
   1379 // object files that are needed until the end of the link.
   1380 
   1381 class Plugin_finish : public Task
   1382 {
   1383  public:
   1384   Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
   1385     : this_blocker_(this_blocker), next_blocker_(next_blocker)
   1386   { }
   1387 
   1388   ~Plugin_finish()
   1389   {
   1390     if (this->this_blocker_ != NULL)
   1391       delete this->this_blocker_;
   1392   }
   1393 
   1394   Task_token*
   1395   is_runnable()
   1396   {
   1397     if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
   1398       return this->this_blocker_;
   1399     return NULL;
   1400   }
   1401 
   1402   void
   1403   locks(Task_locker* tl)
   1404   { tl->add(this, this->next_blocker_); }
   1405 
   1406   void
   1407   run(Workqueue*)
   1408   {
   1409     // We could call early cleanup handlers here.
   1410   }
   1411 
   1412   std::string
   1413   get_name() const
   1414   { return "Plugin_finish"; }
   1415 
   1416  private:
   1417   Task_token* this_blocker_;
   1418   Task_token* next_blocker_;
   1419 };
   1420 
   1421 // Class Plugin_hook.
   1422 
   1423 Plugin_hook::~Plugin_hook()
   1424 {
   1425 }
   1426 
   1427 // Return whether a Plugin_hook task is runnable.
   1428 
   1429 Task_token*
   1430 Plugin_hook::is_runnable()
   1431 {
   1432   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
   1433     return this->this_blocker_;
   1434   return NULL;
   1435 }
   1436 
   1437 // Return a Task_locker for a Plugin_hook task.  We don't need any
   1438 // locks here.
   1439 
   1440 void
   1441 Plugin_hook::locks(Task_locker*)
   1442 {
   1443 }
   1444 
   1445 // Run the "all symbols read" plugin hook.
   1446 
   1447 void
   1448 Plugin_hook::run(Workqueue* workqueue)
   1449 {
   1450   gold_assert(this->options_.has_plugins());
   1451   Symbol* start_sym = this->symtab_->lookup(parameters->entry());
   1452   if (start_sym != NULL)
   1453     start_sym->set_in_real_elf();
   1454 
   1455   this->options_.plugins()->all_symbols_read(workqueue,
   1456                                              this,
   1457                                              this->input_objects_,
   1458                                              this->symtab_,
   1459                                              this->dirpath_,
   1460                                              this->mapfile_,
   1461                                              &this->this_blocker_);
   1462   workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
   1463 					  this->next_blocker_));
   1464 }
   1465 
   1466 // The C interface routines called by the plugins.
   1467 
   1468 #ifdef ENABLE_PLUGINS
   1469 
   1470 // Register a claim-file handler.
   1471 
   1472 static enum ld_plugin_status
   1473 register_claim_file(ld_plugin_claim_file_handler handler)
   1474 {
   1475   gold_assert(parameters->options().has_plugins());
   1476   parameters->options().plugins()->set_claim_file_handler(handler);
   1477   return LDPS_OK;
   1478 }
   1479 
   1480 // Register an all-symbols-read handler.
   1481 
   1482 static enum ld_plugin_status
   1483 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
   1484 {
   1485   gold_assert(parameters->options().has_plugins());
   1486   parameters->options().plugins()->set_all_symbols_read_handler(handler);
   1487   return LDPS_OK;
   1488 }
   1489 
   1490 // Register a cleanup handler.
   1491 
   1492 static enum ld_plugin_status
   1493 register_cleanup(ld_plugin_cleanup_handler handler)
   1494 {
   1495   gold_assert(parameters->options().has_plugins());
   1496   parameters->options().plugins()->set_cleanup_handler(handler);
   1497   return LDPS_OK;
   1498 }
   1499 
   1500 // Add symbols from a plugin-claimed input file.
   1501 
   1502 static enum ld_plugin_status
   1503 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
   1504 {
   1505   gold_assert(parameters->options().has_plugins());
   1506   Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
   1507       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
   1508   if (obj == NULL)
   1509     return LDPS_ERR;
   1510   obj->store_incoming_symbols(nsyms, syms);
   1511   return LDPS_OK;
   1512 }
   1513 
   1514 // Get the input file information with an open (possibly re-opened)
   1515 // file descriptor.
   1516 
   1517 static enum ld_plugin_status
   1518 get_input_file(const void* handle, struct ld_plugin_input_file* file)
   1519 {
   1520   gold_assert(parameters->options().has_plugins());
   1521   unsigned int obj_index =
   1522       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
   1523   return parameters->options().plugins()->get_input_file(obj_index, file);
   1524 }
   1525 
   1526 // Release the input file.
   1527 
   1528 static enum ld_plugin_status
   1529 release_input_file(const void* handle)
   1530 {
   1531   gold_assert(parameters->options().has_plugins());
   1532   unsigned int obj_index =
   1533       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
   1534   return parameters->options().plugins()->release_input_file(obj_index);
   1535 }
   1536 
   1537 static enum ld_plugin_status
   1538 get_view(const void *handle, const void **viewp)
   1539 {
   1540   gold_assert(parameters->options().has_plugins());
   1541   unsigned int obj_index =
   1542       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
   1543   return parameters->options().plugins()->get_view(obj_index, viewp);
   1544 }
   1545 
   1546 // Get the symbol resolution info for a plugin-claimed input file.
   1547 
   1548 static enum ld_plugin_status
   1549 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
   1550 {
   1551   gold_assert(parameters->options().has_plugins());
   1552   Plugin_manager* plugins = parameters->options().plugins();
   1553   Object* obj = plugins->object(
   1554     static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
   1555   if (obj == NULL)
   1556     return LDPS_ERR;
   1557   Pluginobj* plugin_obj = obj->pluginobj();
   1558   if (plugin_obj == NULL)
   1559     return LDPS_ERR;
   1560   Symbol_table* symtab = plugins->symtab();
   1561   return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
   1562 }
   1563 
   1564 // Version 2 of the above.  The only difference is that this version
   1565 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
   1566 
   1567 static enum ld_plugin_status
   1568 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
   1569 {
   1570   gold_assert(parameters->options().has_plugins());
   1571   Plugin_manager* plugins = parameters->options().plugins();
   1572   Object* obj = plugins->object(
   1573     static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
   1574   if (obj == NULL)
   1575     return LDPS_ERR;
   1576   Pluginobj* plugin_obj = obj->pluginobj();
   1577   if (plugin_obj == NULL)
   1578     return LDPS_ERR;
   1579   Symbol_table* symtab = plugins->symtab();
   1580   return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
   1581 }
   1582 
   1583 // Version 3 of the above.  The only difference from v2 is that it
   1584 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
   1585 // decided to include.
   1586 
   1587 static enum ld_plugin_status
   1588 get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
   1589 {
   1590   gold_assert(parameters->options().has_plugins());
   1591   Plugin_manager* plugins = parameters->options().plugins();
   1592   Object* obj = plugins->object(
   1593     static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
   1594   if (obj == NULL)
   1595     return LDPS_ERR;
   1596   Pluginobj* plugin_obj = obj->pluginobj();
   1597   if (plugin_obj == NULL)
   1598     return LDPS_ERR;
   1599   Symbol_table* symtab = plugins->symtab();
   1600   return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
   1601 }
   1602 
   1603 // Add a new (real) input file generated by a plugin.
   1604 
   1605 static enum ld_plugin_status
   1606 add_input_file(const char* pathname)
   1607 {
   1608   gold_assert(parameters->options().has_plugins());
   1609   return parameters->options().plugins()->add_input_file(pathname, false);
   1610 }
   1611 
   1612 // Add a new (real) library required by a plugin.
   1613 
   1614 static enum ld_plugin_status
   1615 add_input_library(const char* pathname)
   1616 {
   1617   gold_assert(parameters->options().has_plugins());
   1618   return parameters->options().plugins()->add_input_file(pathname, true);
   1619 }
   1620 
   1621 // Set the extra library path to be used by libraries added via
   1622 // add_input_library
   1623 
   1624 static enum ld_plugin_status
   1625 set_extra_library_path(const char* path)
   1626 {
   1627   gold_assert(parameters->options().has_plugins());
   1628   return parameters->options().plugins()->set_extra_library_path(path);
   1629 }
   1630 
   1631 // Issue a diagnostic message from a plugin.
   1632 
   1633 static enum ld_plugin_status
   1634 message(int level, const char* format, ...)
   1635 {
   1636   va_list args;
   1637   va_start(args, format);
   1638 
   1639   switch (level)
   1640     {
   1641     case LDPL_INFO:
   1642       parameters->errors()->info(format, args);
   1643       break;
   1644     case LDPL_WARNING:
   1645       parameters->errors()->warning(format, args);
   1646       break;
   1647     case LDPL_ERROR:
   1648     default:
   1649       parameters->errors()->error(format, args);
   1650       break;
   1651     case LDPL_FATAL:
   1652       parameters->errors()->fatal(format, args);
   1653       break;
   1654     }
   1655 
   1656   va_end(args);
   1657   return LDPS_OK;
   1658 }
   1659 
   1660 // Get the section count of the object corresponding to the handle.  This
   1661 // plugin interface can only be called in the claim_file handler of the plugin.
   1662 
   1663 static enum ld_plugin_status
   1664 get_input_section_count(const void* handle, unsigned int* count)
   1665 {
   1666   gold_assert(parameters->options().has_plugins());
   1667 
   1668   if (!parameters->options().plugins()->in_claim_file_handler())
   1669     return LDPS_ERR;
   1670 
   1671   Object* obj = parameters->options().plugins()->get_elf_object(handle);
   1672 
   1673   if (obj == NULL)
   1674     return LDPS_ERR;
   1675 
   1676   *count = obj->shnum();
   1677   return LDPS_OK;
   1678 }
   1679 
   1680 // Get the type of the specified section in the object corresponding
   1681 // to the handle.  This plugin interface can only be called in the
   1682 // claim_file handler of the plugin.
   1683 
   1684 static enum ld_plugin_status
   1685 get_input_section_type(const struct ld_plugin_section section,
   1686                        unsigned int* type)
   1687 {
   1688   gold_assert(parameters->options().has_plugins());
   1689 
   1690   if (!parameters->options().plugins()->in_claim_file_handler())
   1691     return LDPS_ERR;
   1692 
   1693   Object* obj
   1694     = parameters->options().plugins()->get_elf_object(section.handle);
   1695 
   1696   if (obj == NULL)
   1697     return LDPS_BAD_HANDLE;
   1698 
   1699   *type = obj->section_type(section.shndx);
   1700   return LDPS_OK;
   1701 }
   1702 
   1703 // Get the name of the specified section in the object corresponding
   1704 // to the handle.  This plugin interface can only be called in the
   1705 // claim_file handler of the plugin.
   1706 
   1707 static enum ld_plugin_status
   1708 get_input_section_name(const struct ld_plugin_section section,
   1709                        char** section_name_ptr)
   1710 {
   1711   gold_assert(parameters->options().has_plugins());
   1712 
   1713   if (!parameters->options().plugins()->in_claim_file_handler())
   1714     return LDPS_ERR;
   1715 
   1716   Object* obj
   1717     = parameters->options().plugins()->get_elf_object(section.handle);
   1718 
   1719   if (obj == NULL)
   1720     return LDPS_BAD_HANDLE;
   1721 
   1722   // Check if the object is locked before getting the section name.
   1723   gold_assert(obj->is_locked());
   1724 
   1725   const std::string section_name = obj->section_name(section.shndx);
   1726   *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
   1727   memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
   1728   return LDPS_OK;
   1729 }
   1730 
   1731 // Get the contents of the specified section in the object corresponding
   1732 // to the handle.  This plugin interface can only be called in the
   1733 // claim_file handler of the plugin.
   1734 
   1735 static enum ld_plugin_status
   1736 get_input_section_contents(const struct ld_plugin_section section,
   1737 			   const unsigned char** section_contents_ptr,
   1738 			   size_t* len)
   1739 {
   1740   gold_assert(parameters->options().has_plugins());
   1741 
   1742   if (!parameters->options().plugins()->in_claim_file_handler())
   1743     return LDPS_ERR;
   1744 
   1745   Object* obj
   1746     = parameters->options().plugins()->get_elf_object(section.handle);
   1747 
   1748   if (obj == NULL)
   1749     return LDPS_BAD_HANDLE;
   1750 
   1751   // Check if the object is locked before getting the section contents.
   1752   gold_assert(obj->is_locked());
   1753 
   1754   section_size_type plen;
   1755   *section_contents_ptr
   1756       = obj->section_contents(section.shndx, &plen, false);
   1757   *len = plen;
   1758   return LDPS_OK;
   1759 }
   1760 
   1761 // Get the alignment of the specified section in the object corresponding
   1762 // to the handle.  This plugin interface can only be called in the
   1763 // claim_file handler of the plugin.
   1764 
   1765 static enum ld_plugin_status
   1766 get_input_section_alignment(const struct ld_plugin_section section,
   1767                             unsigned int* addralign)
   1768 {
   1769   gold_assert(parameters->options().has_plugins());
   1770 
   1771   if (!parameters->options().plugins()->in_claim_file_handler())
   1772     return LDPS_ERR;
   1773 
   1774   Object* obj
   1775     = parameters->options().plugins()->get_elf_object(section.handle);
   1776 
   1777   if (obj == NULL)
   1778     return LDPS_BAD_HANDLE;
   1779 
   1780   *addralign = obj->section_addralign(section.shndx);
   1781   return LDPS_OK;
   1782 }
   1783 
   1784 // Get the size of the specified section in the object corresponding
   1785 // to the handle.  This plugin interface can only be called in the
   1786 // claim_file handler of the plugin.
   1787 
   1788 static enum ld_plugin_status
   1789 get_input_section_size(const struct ld_plugin_section section,
   1790                        uint64_t* secsize)
   1791 {
   1792   gold_assert(parameters->options().has_plugins());
   1793 
   1794   if (!parameters->options().plugins()->in_claim_file_handler())
   1795     return LDPS_ERR;
   1796 
   1797   Object* obj
   1798     = parameters->options().plugins()->get_elf_object(section.handle);
   1799 
   1800   if (obj == NULL)
   1801     return LDPS_BAD_HANDLE;
   1802 
   1803   *secsize = obj->section_size(section.shndx);
   1804   return LDPS_OK;
   1805 }
   1806 
   1807 
   1808 // Specify the ordering of sections in the final layout. The sections are
   1809 // specified as (handle,shndx) pairs in the two arrays in the order in
   1810 // which they should appear in the final layout.
   1811 
   1812 static enum ld_plugin_status
   1813 update_section_order(const struct ld_plugin_section* section_list,
   1814 		     unsigned int num_sections)
   1815 {
   1816   gold_assert(parameters->options().has_plugins());
   1817 
   1818   if (num_sections == 0)
   1819     return LDPS_OK;
   1820 
   1821   if (section_list == NULL)
   1822     return LDPS_ERR;
   1823 
   1824   Layout* layout = parameters->options().plugins()->layout();
   1825   gold_assert (layout != NULL);
   1826 
   1827   std::map<Section_id, unsigned int>* order_map
   1828     = layout->get_section_order_map();
   1829 
   1830   /* Store the mapping from Section_id to section position in layout's
   1831      order_map to consult after output sections are added.  */
   1832   for (unsigned int i = 0; i < num_sections; ++i)
   1833     {
   1834       Object* obj = parameters->options().plugins()->get_elf_object(
   1835           section_list[i].handle);
   1836       if (obj == NULL || obj->is_dynamic())
   1837 	return LDPS_BAD_HANDLE;
   1838       unsigned int shndx = section_list[i].shndx;
   1839       Section_id secn_id(static_cast<Relobj*>(obj), shndx);
   1840       (*order_map)[secn_id] = i + 1;
   1841     }
   1842 
   1843   return LDPS_OK;
   1844 }
   1845 
   1846 // Let the linker know that the sections could be reordered.
   1847 
   1848 static enum ld_plugin_status
   1849 allow_section_ordering()
   1850 {
   1851   gold_assert(parameters->options().has_plugins());
   1852   Layout* layout = parameters->options().plugins()->layout();
   1853   layout->set_section_ordering_specified();
   1854   return LDPS_OK;
   1855 }
   1856 
   1857 // Let the linker know that a subset of sections could be mapped
   1858 // to a unique segment.
   1859 
   1860 static enum ld_plugin_status
   1861 allow_unique_segment_for_sections()
   1862 {
   1863   gold_assert(parameters->options().has_plugins());
   1864   Layout* layout = parameters->options().plugins()->layout();
   1865   layout->set_unique_segment_for_sections_specified();
   1866   return LDPS_OK;
   1867 }
   1868 
   1869 // This function should map the list of sections specified in the
   1870 // SECTION_LIST to a unique segment.  ELF segments do not have names
   1871 // and the NAME is used to identify Output Section which should contain
   1872 // the list of sections.  This Output Section will then be mapped to
   1873 // a unique segment.  FLAGS is used to specify if any additional segment
   1874 // flags need to be set.  For instance, a specific segment flag can be
   1875 // set to identify this segment.  Unsetting segment flags is not possible.
   1876 // ALIGN specifies the alignment of the segment.
   1877 
   1878 static enum ld_plugin_status
   1879 unique_segment_for_sections(const char* segment_name,
   1880 			    uint64_t flags,
   1881 			    uint64_t align,
   1882 			    const struct ld_plugin_section* section_list,
   1883 			    unsigned int num_sections)
   1884 {
   1885   gold_assert(parameters->options().has_plugins());
   1886 
   1887   if (num_sections == 0)
   1888     return LDPS_OK;
   1889 
   1890   if (section_list == NULL)
   1891     return LDPS_ERR;
   1892 
   1893   Layout* layout = parameters->options().plugins()->layout();
   1894   gold_assert (layout != NULL);
   1895 
   1896   Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
   1897   s->name = segment_name;
   1898   s->flags = flags;
   1899   s->align = align;
   1900 
   1901   for (unsigned int i = 0; i < num_sections; ++i)
   1902     {
   1903       Object* obj = parameters->options().plugins()->get_elf_object(
   1904           section_list[i].handle);
   1905       if (obj == NULL || obj->is_dynamic())
   1906 	return LDPS_BAD_HANDLE;
   1907       unsigned int shndx = section_list[i].shndx;
   1908       Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
   1909       layout->insert_section_segment_map(secn_id, s);
   1910     }
   1911 
   1912   return LDPS_OK;
   1913 }
   1914 
   1915 #endif // ENABLE_PLUGINS
   1916 
   1917 // Allocate a Pluginobj object of the appropriate size and endianness.
   1918 
   1919 static Pluginobj*
   1920 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
   1921 {
   1922   Pluginobj* obj = NULL;
   1923 
   1924   parameters_force_valid_target();
   1925   const Target& target(parameters->target());
   1926 
   1927   if (target.get_size() == 32)
   1928     {
   1929       if (target.is_big_endian())
   1930 #ifdef HAVE_TARGET_32_BIG
   1931         obj = new Sized_pluginobj<32, true>(input_file->filename(),
   1932                                             input_file, offset, filesize);
   1933 #else
   1934         gold_error(_("%s: not configured to support "
   1935 		     "32-bit big-endian object"),
   1936 		   input_file->filename().c_str());
   1937 #endif
   1938       else
   1939 #ifdef HAVE_TARGET_32_LITTLE
   1940         obj = new Sized_pluginobj<32, false>(input_file->filename(),
   1941                                              input_file, offset, filesize);
   1942 #else
   1943         gold_error(_("%s: not configured to support "
   1944 		     "32-bit little-endian object"),
   1945 		   input_file->filename().c_str());
   1946 #endif
   1947     }
   1948   else if (target.get_size() == 64)
   1949     {
   1950       if (target.is_big_endian())
   1951 #ifdef HAVE_TARGET_64_BIG
   1952         obj = new Sized_pluginobj<64, true>(input_file->filename(),
   1953                                             input_file, offset, filesize);
   1954 #else
   1955         gold_error(_("%s: not configured to support "
   1956 		     "64-bit big-endian object"),
   1957 		   input_file->filename().c_str());
   1958 #endif
   1959       else
   1960 #ifdef HAVE_TARGET_64_LITTLE
   1961         obj = new Sized_pluginobj<64, false>(input_file->filename(),
   1962                                              input_file, offset, filesize);
   1963 #else
   1964         gold_error(_("%s: not configured to support "
   1965 		     "64-bit little-endian object"),
   1966 		   input_file->filename().c_str());
   1967 #endif
   1968     }
   1969 
   1970   gold_assert(obj != NULL);
   1971   return obj;
   1972 }
   1973 
   1974 } // End namespace gold.
   1975