2010-03-22 Rafael Espindola <espindola@google.com>
[external/binutils.git] / gold / plugin.cc
1 // plugin.cc -- plugin manager for gold      -*- C++ -*-
2
3 // Copyright 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@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 #include <dlfcn.h>
33 #endif
34
35 #include "parameters.h"
36 #include "errors.h"
37 #include "fileread.h"
38 #include "layout.h"
39 #include "options.h"
40 #include "plugin.h"
41 #include "target.h"
42 #include "readsyms.h"
43 #include "symtab.h"
44 #include "elfcpp.h"
45
46 namespace gold
47 {
48
49 #ifdef ENABLE_PLUGINS
50
51 // The linker's exported interfaces.
52
53 extern "C"
54 {
55
56 static enum ld_plugin_status
57 register_claim_file(ld_plugin_claim_file_handler handler);
58
59 static enum ld_plugin_status
60 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
61
62 static enum ld_plugin_status
63 register_cleanup(ld_plugin_cleanup_handler handler);
64
65 static enum ld_plugin_status
66 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
67
68 static enum ld_plugin_status
69 get_input_file(const void *handle, struct ld_plugin_input_file *file);
70
71 static enum ld_plugin_status
72 release_input_file(const void *handle);
73
74 static enum ld_plugin_status
75 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
76
77 static enum ld_plugin_status
78 add_input_file(char *pathname);
79
80 static enum ld_plugin_status
81 add_input_library(char *pathname);
82
83 static enum ld_plugin_status
84 message(int level, const char *format, ...);
85
86 };
87
88 #endif // ENABLE_PLUGINS
89
90 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
91                                            off_t offset, off_t filesize);
92
93 // Plugin methods.
94
95 // Load one plugin library.
96
97 void
98 Plugin::load()
99 {
100 #ifdef ENABLE_PLUGINS
101   // Load the plugin library.
102   // FIXME: Look for the library in standard locations.
103   this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
104   if (this->handle_ == NULL)
105     {
106       gold_error(_("%s: could not load plugin library"),
107                  this->filename_.c_str());
108       return;
109     }
110
111   // Find the plugin's onload entry point.
112   void* ptr = dlsym(this->handle_, "onload");
113   if (ptr == NULL)
114     {
115       gold_error(_("%s: could not find onload entry point"),
116                  this->filename_.c_str());
117       return;
118     }
119   ld_plugin_onload onload;
120   gold_assert(sizeof(onload) == sizeof(ptr));
121   memcpy(&onload, &ptr, sizeof(ptr));
122
123   // Get the linker's version number.
124   const char* ver = get_version_string();
125   int major = 0;
126   int minor = 0;
127   sscanf(ver, "%d.%d", &major, &minor);
128
129   // Allocate and populate a transfer vector.
130   const int tv_fixed_size = 14;
131   int tv_size = this->args_.size() + tv_fixed_size;
132   ld_plugin_tv *tv = new ld_plugin_tv[tv_size];
133
134   // Put LDPT_MESSAGE at the front of the list so the plugin can use it
135   // while processing subsequent entries.
136   int i = 0;
137   tv[i].tv_tag = LDPT_MESSAGE;
138   tv[i].tv_u.tv_message = message;
139
140   ++i;
141   tv[i].tv_tag = LDPT_API_VERSION;
142   tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
143
144   ++i;
145   tv[i].tv_tag = LDPT_GOLD_VERSION;
146   tv[i].tv_u.tv_val = major * 100 + minor;
147
148   ++i;
149   tv[i].tv_tag = LDPT_LINKER_OUTPUT;
150   if (parameters->options().relocatable())
151     tv[i].tv_u.tv_val = LDPO_REL;
152   else if (parameters->options().shared())
153     tv[i].tv_u.tv_val = LDPO_DYN;
154   else
155     tv[i].tv_u.tv_val = LDPO_EXEC;
156
157   for (unsigned int j = 0; j < this->args_.size(); ++j)
158     {
159       ++i;
160       tv[i].tv_tag = LDPT_OPTION;
161       tv[i].tv_u.tv_string = this->args_[j].c_str();
162     }
163
164   ++i;
165   tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
166   tv[i].tv_u.tv_register_claim_file = register_claim_file;
167
168   ++i;
169   tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
170   tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
171
172   ++i;
173   tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
174   tv[i].tv_u.tv_register_cleanup = register_cleanup;
175
176   ++i;
177   tv[i].tv_tag = LDPT_ADD_SYMBOLS;
178   tv[i].tv_u.tv_add_symbols = add_symbols;
179
180   ++i;
181   tv[i].tv_tag = LDPT_GET_INPUT_FILE;
182   tv[i].tv_u.tv_get_input_file = get_input_file;
183
184   ++i;
185   tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
186   tv[i].tv_u.tv_release_input_file = release_input_file;
187
188   ++i;
189   tv[i].tv_tag = LDPT_GET_SYMBOLS;
190   tv[i].tv_u.tv_get_symbols = get_symbols;
191
192   ++i;
193   tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
194   tv[i].tv_u.tv_add_input_file = add_input_file;
195
196   ++i;
197   tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
198   tv[i].tv_u.tv_add_input_library = add_input_library;
199
200   ++i;
201   tv[i].tv_tag = LDPT_NULL;
202   tv[i].tv_u.tv_val = 0;
203
204   gold_assert(i == tv_size - 1);
205
206   // Call the onload entry point.
207   (*onload)(tv);
208
209   delete[] tv;
210 #endif // ENABLE_PLUGINS
211 }
212
213 // Call the plugin claim-file handler.
214
215 inline bool
216 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
217 {
218   int claimed = 0;
219
220   if (this->claim_file_handler_ != NULL)
221     {
222       (*this->claim_file_handler_)(plugin_input_file, &claimed);
223       if (claimed)
224         return true;
225     }
226   return false;
227 }
228
229 // Call the all-symbols-read handler.
230
231 inline void
232 Plugin::all_symbols_read()
233 {
234   if (this->all_symbols_read_handler_ != NULL)
235     (*this->all_symbols_read_handler_)();
236 }
237
238 // Call the cleanup handler.
239
240 inline void
241 Plugin::cleanup()
242 {
243   if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
244     {
245       // Set this flag before calling to prevent a recursive plunge
246       // in the event that a plugin's cleanup handler issues a
247       // fatal error.
248       this->cleanup_done_ = true;
249       (*this->cleanup_handler_)();
250     }
251 }
252
253 // Plugin_manager methods.
254
255 Plugin_manager::~Plugin_manager()
256 {
257   for (Plugin_list::iterator p = this->plugins_.begin();
258        p != this->plugins_.end();
259        ++p)
260     delete *p;
261   this->plugins_.clear();
262   for (Object_list::iterator obj = this->objects_.begin();
263        obj != this->objects_.end();
264        ++obj)
265     delete *obj;
266   this->objects_.clear();
267 }
268
269 // Load all plugin libraries.
270
271 void
272 Plugin_manager::load_plugins()
273 {
274   for (this->current_ = this->plugins_.begin();
275        this->current_ != this->plugins_.end();
276        ++this->current_)
277     (*this->current_)->load();
278 }
279
280 // Call the plugin claim-file handlers in turn to see if any claim the file.
281
282 Pluginobj*
283 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
284                            off_t filesize)
285 {
286   if (this->in_replacement_phase_)
287     return NULL;
288
289   unsigned int handle = this->objects_.size();
290   this->input_file_ = input_file;
291   this->plugin_input_file_.name = input_file->filename().c_str();
292   this->plugin_input_file_.fd = input_file->file().descriptor();
293   this->plugin_input_file_.offset = offset;
294   this->plugin_input_file_.filesize = filesize;
295   this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
296
297   for (this->current_ = this->plugins_.begin();
298        this->current_ != this->plugins_.end();
299        ++this->current_)
300     {
301       if ((*this->current_)->claim_file(&this->plugin_input_file_))
302         {
303           if (this->objects_.size() > handle)
304             return this->objects_[handle];
305
306           // If the plugin claimed the file but did not call the
307           // add_symbols callback, we need to create the Pluginobj now.
308           Pluginobj* obj = this->make_plugin_object(handle);
309           return obj;
310         }
311     }
312
313   return NULL;
314 }
315
316 // Call the all-symbols-read handlers.
317
318 void
319 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
320                                  Input_objects* input_objects,
321                                  Symbol_table* symtab, Layout* layout,
322                                  Dirsearch* dirpath, Mapfile* mapfile,
323                                  Task_token** last_blocker)
324 {
325   this->in_replacement_phase_ = true;
326   this->workqueue_ = workqueue;
327   this->task_ = task;
328   this->input_objects_ = input_objects;
329   this->symtab_ = symtab;
330   this->layout_ = layout;
331   this->dirpath_ = dirpath;
332   this->mapfile_ = mapfile;
333   this->this_blocker_ = NULL;
334
335   for (this->current_ = this->plugins_.begin();
336        this->current_ != this->plugins_.end();
337        ++this->current_)
338     (*this->current_)->all_symbols_read();
339
340   *last_blocker = this->this_blocker_;
341 }
342
343 // Layout deferred objects.
344
345 void
346 Plugin_manager::layout_deferred_objects()
347 {
348   Deferred_layout_list::iterator obj;
349
350   for (obj = this->deferred_layout_objects_.begin();
351        obj != this->deferred_layout_objects_.end();
352        ++obj)
353     (*obj)->layout_deferred_sections(this->layout_);
354 }
355
356 // Call the cleanup handlers.
357
358 void
359 Plugin_manager::cleanup()
360 {
361   for (this->current_ = this->plugins_.begin();
362        this->current_ != this->plugins_.end();
363        ++this->current_)
364     (*this->current_)->cleanup();
365 }
366
367 // Make a new Pluginobj object.  This is called when the plugin calls
368 // the add_symbols API.
369
370 Pluginobj*
371 Plugin_manager::make_plugin_object(unsigned int handle)
372 {
373   // Make sure we aren't asked to make an object for the same handle twice.
374   if (this->objects_.size() != handle)
375     return NULL;
376
377   Pluginobj* obj = make_sized_plugin_object(this->input_file_,
378                                             this->plugin_input_file_.offset,
379                                             this->plugin_input_file_.filesize);
380   this->objects_.push_back(obj);
381   return obj;
382 }
383
384 // Get the input file information with an open (possibly re-opened)
385 // file descriptor.
386
387 ld_plugin_status
388 Plugin_manager::get_input_file(unsigned int handle,
389                                struct ld_plugin_input_file *file)
390 {
391   Pluginobj* obj = this->object(handle);
392   if (obj == NULL)
393     return LDPS_BAD_HANDLE;
394
395   obj->lock(this->task_);
396   file->name = obj->filename().c_str();
397   file->fd = obj->descriptor();
398   file->offset = obj->offset();
399   file->filesize = obj->filesize();
400   file->handle = reinterpret_cast<void*>(handle);
401   return LDPS_OK;
402 }
403
404 // Release the input file.
405
406 ld_plugin_status
407 Plugin_manager::release_input_file(unsigned int handle)
408 {
409   Pluginobj* obj = this->object(handle);
410   if (obj == NULL)
411     return LDPS_BAD_HANDLE;
412
413   obj->unlock(this->task_);
414   return LDPS_OK;
415 }
416
417 // Add a new input file.
418
419 ld_plugin_status
420 Plugin_manager::add_input_file(char *pathname, bool is_lib)
421 {
422   Input_file_argument file(pathname,
423                            (is_lib
424                             ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
425                             : Input_file_argument::INPUT_FILE_TYPE_FILE),
426                            "", false, this->options_);
427   Input_argument* input_argument = new Input_argument(file);
428   Task_token* next_blocker = new Task_token(true);
429   next_blocker->add_blocker();
430   if (this->layout_->incremental_inputs())
431     gold_error(_("input files added by plug-ins in --incremental mode not "
432                  "supported yet"));
433   this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
434                                                 this->symtab_,
435                                                 this->layout_,
436                                                 this->dirpath_,
437                                                 0,
438                                                 this->mapfile_,
439                                                 input_argument,
440                                                 NULL,
441                                                 NULL,
442                                                 this->this_blocker_,
443                                                 next_blocker));
444   this->this_blocker_ = next_blocker;
445   return LDPS_OK;
446 }
447
448 // Class Pluginobj.
449
450 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
451                      off_t offset, off_t filesize)
452   : Object(name, input_file, false, offset),
453     nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
454 {
455 }
456
457 // Return TRUE if a defined symbol might be reachable from outside the
458 // universe of claimed objects.
459
460 static inline bool
461 is_visible_from_outside(Symbol* lsym)
462 {
463   if (lsym->in_real_elf())
464     return true;
465   if (parameters->options().relocatable())
466     return true;
467   if (parameters->options().export_dynamic() || parameters->options().shared())
468     return lsym->is_externally_visible();
469   return false;
470 }
471
472 // Get symbol resolution info.
473
474 ld_plugin_status
475 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
476 {
477   if (nsyms > this->nsyms_)
478     return LDPS_NO_SYMS;
479
480   if (static_cast<size_t>(nsyms) > this->symbols_.size())
481     {
482       // We never decided to include this object. We mark all symbols as
483       // preempted.
484       gold_assert (this->symbols_.size() == 0);
485       for (int i = 0; i < nsyms; i++)
486         syms[i].resolution = LDPR_PREEMPTED_REG;
487       return LDPS_OK;
488     }
489
490   for (int i = 0; i < nsyms; i++)
491     {
492       ld_plugin_symbol* isym = &syms[i];
493       Symbol* lsym = this->symbols_[i];
494       ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
495
496       if (lsym->is_undefined())
497         // The symbol remains undefined.
498         res = LDPR_UNDEF;
499       else if (isym->def == LDPK_UNDEF
500                || isym->def == LDPK_WEAKUNDEF
501                || isym->def == LDPK_COMMON)
502         {
503           // The original symbol was undefined or common.
504           if (lsym->source() != Symbol::FROM_OBJECT)
505             res = LDPR_RESOLVED_EXEC;
506           else if (lsym->object()->pluginobj() != NULL)
507             res = LDPR_RESOLVED_IR;
508           else if (lsym->object()->is_dynamic())
509             res = LDPR_RESOLVED_DYN;
510           else
511             res = LDPR_RESOLVED_EXEC;
512         }
513       else
514         {
515           // The original symbol was a definition.
516           if (lsym->source() != Symbol::FROM_OBJECT)
517             res = LDPR_PREEMPTED_REG;
518           else if (lsym->object() == static_cast<const Object*>(this))
519             res = (is_visible_from_outside(lsym)
520                    ? LDPR_PREVAILING_DEF
521                    : LDPR_PREVAILING_DEF_IRONLY);
522           else
523             res = (lsym->object()->pluginobj() != NULL
524                    ? LDPR_PREEMPTED_IR
525                    : LDPR_PREEMPTED_REG);
526         }
527       isym->resolution = res;
528     }
529   return LDPS_OK;
530 }
531
532 // Return TRUE if the comdat group with key COMDAT_KEY from this object
533 // should be kept.
534
535 bool
536 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
537 {
538   std::pair<Comdat_map::iterator, bool> ins =
539     this->comdat_map_.insert(std::make_pair(comdat_key, false));
540
541   // If this is the first time we've seen this comdat key, ask the
542   // layout object whether it should be included.
543   if (ins.second)
544     ins.first->second = layout->find_or_add_kept_section(comdat_key,
545                                                          NULL, 0, true,
546                                                          true, NULL);
547
548   return ins.first->second;
549 }
550
551 // Class Sized_pluginobj.
552
553 template<int size, bool big_endian>
554 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
555     const std::string& name,
556     Input_file* input_file,
557     off_t offset,
558     off_t filesize)
559   : Pluginobj(name, input_file, offset, filesize)
560 {
561 }
562
563 // Read the symbols.  Not used for plugin objects.
564
565 template<int size, bool big_endian>
566 void
567 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
568 {
569   gold_unreachable();
570 }
571
572 // Lay out the input sections.  Not used for plugin objects.
573
574 template<int size, bool big_endian>
575 void
576 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
577                                              Read_symbols_data*)
578 {
579   gold_unreachable();
580 }
581
582 // Add the symbols to the symbol table.
583
584 template<int size, bool big_endian>
585 void
586 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
587                                                   Read_symbols_data*,
588                                                   Layout* layout)
589 {
590   const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
591   unsigned char symbuf[sym_size];
592   elfcpp::Sym<size, big_endian> sym(symbuf);
593   elfcpp::Sym_write<size, big_endian> osym(symbuf);
594
595   typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
596
597   this->symbols_.resize(this->nsyms_);
598
599   for (int i = 0; i < this->nsyms_; ++i)
600     {
601       const struct ld_plugin_symbol *isym = &this->syms_[i];
602       const char* name = isym->name;
603       const char* ver = isym->version;
604       elfcpp::Elf_Half shndx;
605       elfcpp::STB bind;
606       elfcpp::STV vis;
607
608       if (name != NULL && name[0] == '\0')
609         name = NULL;
610       if (ver != NULL && ver[0] == '\0')
611         ver = NULL;
612
613       switch (isym->def)
614         {
615         case LDPK_WEAKDEF:
616         case LDPK_WEAKUNDEF:
617           bind = elfcpp::STB_WEAK;
618           break;
619         case LDPK_DEF:
620         case LDPK_UNDEF:
621         case LDPK_COMMON:
622         default:
623           bind = elfcpp::STB_GLOBAL;
624           break;
625         }
626
627       switch (isym->def)
628         {
629         case LDPK_DEF:
630         case LDPK_WEAKDEF:
631           shndx = elfcpp::SHN_ABS;
632           break;
633         case LDPK_COMMON:
634           shndx = elfcpp::SHN_COMMON;
635           break;
636         case LDPK_UNDEF:
637         case LDPK_WEAKUNDEF:
638         default:
639           shndx = elfcpp::SHN_UNDEF;
640           break;
641         }
642
643       switch (isym->visibility)
644         {
645         case LDPV_PROTECTED:
646           vis = elfcpp::STV_DEFAULT;
647           break;
648         case LDPV_INTERNAL:
649           vis = elfcpp::STV_DEFAULT;
650           break;
651         case LDPV_HIDDEN:
652           vis = elfcpp::STV_DEFAULT;
653           break;
654         case LDPV_DEFAULT:
655         default:
656           vis = elfcpp::STV_DEFAULT;
657           break;
658         }
659
660       if (isym->comdat_key != NULL
661           && isym->comdat_key[0] != '\0'
662           && !this->include_comdat_group(isym->comdat_key, layout))
663         shndx = elfcpp::SHN_UNDEF;
664
665       osym.put_st_name(0);
666       osym.put_st_value(0);
667       osym.put_st_size(static_cast<Elf_size_type>(isym->size));
668       osym.put_st_info(bind, elfcpp::STT_NOTYPE);
669       osym.put_st_other(vis, 0);
670       osym.put_st_shndx(shndx);
671
672       this->symbols_[i] =
673         symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
674     }
675 }
676
677 template<int size, bool big_endian>
678 Archive::Should_include
679 Sized_pluginobj<size, big_endian>::do_should_include_member(
680     Symbol_table* symtab, Read_symbols_data*, std::string* why)
681 {
682   char* tmpbuf = NULL;
683   size_t tmpbuflen = 0;
684
685   for (int i = 0; i < this->nsyms_; ++i) {
686     const struct ld_plugin_symbol& sym = this->syms_[i];
687     const char* name = sym.name;
688     Symbol* symbol;
689     Archive::Should_include t = Archive::should_include_member(symtab, name,
690                                                                &symbol, why,
691                                                                &tmpbuf,
692                                                                &tmpbuflen);
693       if (t == Archive::SHOULD_INCLUDE_YES)
694         {
695           if (tmpbuf != NULL)
696             free(tmpbuf);
697           return t;
698         }
699   }
700   if (tmpbuf != NULL)
701     free(tmpbuf);
702   return Archive::SHOULD_INCLUDE_UNKNOWN;
703 }
704
705 // Get the size of a section.  Not used for plugin objects.
706
707 template<int size, bool big_endian>
708 uint64_t
709 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
710 {
711   gold_unreachable();
712   return 0;
713 }
714
715 // Get the name of a section.  Not used for plugin objects.
716
717 template<int size, bool big_endian>
718 std::string
719 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
720 {
721   gold_unreachable();
722   return std::string();
723 }
724
725 // Return a view of the contents of a section.  Not used for plugin objects.
726
727 template<int size, bool big_endian>
728 Object::Location
729 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
730 {
731   Location loc(0, 0);
732
733   gold_unreachable();
734   return loc;
735 }
736
737 // Return section flags.  Not used for plugin objects.
738
739 template<int size, bool big_endian>
740 uint64_t
741 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
742 {
743   gold_unreachable();
744   return 0;
745 }
746
747 // Return section entsize.  Not used for plugin objects.
748
749 template<int size, bool big_endian>
750 uint64_t
751 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
752 {
753   gold_unreachable();
754   return 0;
755 }
756
757 // Return section address.  Not used for plugin objects.
758
759 template<int size, bool big_endian>
760 uint64_t
761 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
762 {
763   gold_unreachable();
764   return 0;
765 }
766
767 // Return section type.  Not used for plugin objects.
768
769 template<int size, bool big_endian>
770 unsigned int
771 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
772 {
773   gold_unreachable();
774   return 0;
775 }
776
777 // Return the section link field.  Not used for plugin objects.
778
779 template<int size, bool big_endian>
780 unsigned int
781 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
782 {
783   gold_unreachable();
784   return 0;
785 }
786
787 // Return the section link field.  Not used for plugin objects.
788
789 template<int size, bool big_endian>
790 unsigned int
791 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
792 {
793   gold_unreachable();
794   return 0;
795 }
796
797 // Return the section alignment.  Not used for plugin objects.
798
799 template<int size, bool big_endian>
800 uint64_t
801 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
802 {
803   gold_unreachable();
804   return 0;
805 }
806
807 // Return the Xindex structure to use.  Not used for plugin objects.
808
809 template<int size, bool big_endian>
810 Xindex*
811 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
812 {
813   gold_unreachable();
814   return NULL;
815 }
816
817 // Get symbol counts.  Not used for plugin objects.
818
819 template<int size, bool big_endian>
820 void
821 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
822                                                    size_t*, size_t*) const
823 {
824   gold_unreachable();
825 }
826
827 // Get symbols.  Not used for plugin objects.
828
829 template<int size, bool big_endian>
830 const Object::Symbols*
831 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
832 {
833   gold_unreachable();
834 }
835
836 // Class Plugin_finish.  This task runs after all replacement files have
837 // been added.  It calls each plugin's cleanup handler.
838
839 class Plugin_finish : public Task
840 {
841  public:
842   Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
843     : this_blocker_(this_blocker), next_blocker_(next_blocker)
844   { }
845
846   ~Plugin_finish()
847   {
848     if (this->this_blocker_ != NULL)
849       delete this->this_blocker_;
850   }
851
852   Task_token*
853   is_runnable()
854   {
855     if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
856       return this->this_blocker_;
857     return NULL;
858   }
859
860   void
861   locks(Task_locker* tl)
862   { tl->add(this, this->next_blocker_); }
863
864   void
865   run(Workqueue*)
866   {
867     Plugin_manager* plugins = parameters->options().plugins();
868     gold_assert(plugins != NULL);
869     plugins->cleanup();
870   }
871
872   std::string
873   get_name() const
874   { return "Plugin_finish"; }
875
876  private:
877   Task_token* this_blocker_;
878   Task_token* next_blocker_;
879 };
880
881 // Class Plugin_hook.
882
883 Plugin_hook::~Plugin_hook()
884 {
885 }
886
887 // Return whether a Plugin_hook task is runnable.
888
889 Task_token*
890 Plugin_hook::is_runnable()
891 {
892   if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
893     return this->this_blocker_;
894   return NULL;
895 }
896
897 // Return a Task_locker for a Plugin_hook task.  We don't need any
898 // locks here.
899
900 void
901 Plugin_hook::locks(Task_locker*)
902 {
903 }
904
905 // Run the "all symbols read" plugin hook.
906
907 void
908 Plugin_hook::run(Workqueue* workqueue)
909 {
910   gold_assert(this->options_.has_plugins());
911   this->options_.plugins()->all_symbols_read(workqueue,
912                                              this,
913                                              this->input_objects_,
914                                              this->symtab_,
915                                              this->layout_,
916                                              this->dirpath_,
917                                              this->mapfile_,
918                                              &this->this_blocker_);
919   workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
920                                           this->next_blocker_));
921 }
922
923 // The C interface routines called by the plugins.
924
925 #ifdef ENABLE_PLUGINS
926
927 // Register a claim-file handler.
928
929 static enum ld_plugin_status
930 register_claim_file(ld_plugin_claim_file_handler handler)
931 {
932   gold_assert(parameters->options().has_plugins());
933   parameters->options().plugins()->set_claim_file_handler(handler);
934   return LDPS_OK;
935 }
936
937 // Register an all-symbols-read handler.
938
939 static enum ld_plugin_status
940 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
941 {
942   gold_assert(parameters->options().has_plugins());
943   parameters->options().plugins()->set_all_symbols_read_handler(handler);
944   return LDPS_OK;
945 }
946
947 // Register a cleanup handler.
948
949 static enum ld_plugin_status
950 register_cleanup(ld_plugin_cleanup_handler handler)
951 {
952   gold_assert(parameters->options().has_plugins());
953   parameters->options().plugins()->set_cleanup_handler(handler);
954   return LDPS_OK;
955 }
956
957 // Add symbols from a plugin-claimed input file.
958
959 static enum ld_plugin_status
960 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
961 {
962   gold_assert(parameters->options().has_plugins());
963   Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
964       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
965   if (obj == NULL)
966     return LDPS_ERR;
967   obj->store_incoming_symbols(nsyms, syms);
968   return LDPS_OK;
969 }
970
971 // Get the input file information with an open (possibly re-opened)
972 // file descriptor.
973
974 static enum ld_plugin_status
975 get_input_file(const void *handle, struct ld_plugin_input_file *file)
976 {
977   gold_assert(parameters->options().has_plugins());
978   unsigned int obj_index =
979       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
980   return parameters->options().plugins()->get_input_file(obj_index, file);
981 }
982
983 // Release the input file.
984
985 static enum ld_plugin_status
986 release_input_file(const void *handle)
987 {
988   gold_assert(parameters->options().has_plugins());
989   unsigned int obj_index =
990       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
991   return parameters->options().plugins()->release_input_file(obj_index);
992 }
993
994 // Get the symbol resolution info for a plugin-claimed input file.
995
996 static enum ld_plugin_status
997 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
998 {
999   gold_assert(parameters->options().has_plugins());
1000   Pluginobj* obj = parameters->options().plugins()->object(
1001       static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1002   if (obj == NULL)
1003     return LDPS_ERR;
1004   return obj->get_symbol_resolution_info(nsyms, syms);
1005 }
1006
1007 // Add a new (real) input file generated by a plugin.
1008
1009 static enum ld_plugin_status
1010 add_input_file(char *pathname)
1011 {
1012   gold_assert(parameters->options().has_plugins());
1013   return parameters->options().plugins()->add_input_file(pathname, false);
1014 }
1015
1016 // Add a new (real) library required by a plugin.
1017
1018 static enum ld_plugin_status
1019 add_input_library(char *pathname)
1020 {
1021   gold_assert(parameters->options().has_plugins());
1022   return parameters->options().plugins()->add_input_file(pathname, true);
1023 }
1024
1025 // Issue a diagnostic message from a plugin.
1026
1027 static enum ld_plugin_status
1028 message(int level, const char * format, ...)
1029 {
1030   va_list args;
1031   va_start(args, format);
1032
1033   switch (level)
1034     {
1035     case LDPL_INFO:
1036       parameters->errors()->info(format, args);
1037       break;
1038     case LDPL_WARNING:
1039       parameters->errors()->warning(format, args);
1040       break;
1041     case LDPL_ERROR:
1042     default:
1043       parameters->errors()->error(format, args);
1044       break;
1045     case LDPL_FATAL:
1046       parameters->errors()->fatal(format, args);
1047       break;
1048     }
1049
1050   va_end(args);
1051   return LDPS_OK;
1052 }
1053
1054 #endif // ENABLE_PLUGINS
1055
1056 // Allocate a Pluginobj object of the appropriate size and endianness.
1057
1058 static Pluginobj*
1059 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1060 {
1061   Pluginobj* obj = NULL;
1062
1063   parameters_force_valid_target();
1064   const Target& target(parameters->target());
1065
1066   if (target.get_size() == 32)
1067     {
1068       if (target.is_big_endian())
1069 #ifdef HAVE_TARGET_32_BIG
1070         obj = new Sized_pluginobj<32, true>(input_file->filename(),
1071                                             input_file, offset, filesize);
1072 #else
1073         gold_error(_("%s: not configured to support "
1074                      "32-bit big-endian object"),
1075                    input_file->filename().c_str());
1076 #endif
1077       else
1078 #ifdef HAVE_TARGET_32_LITTLE
1079         obj = new Sized_pluginobj<32, false>(input_file->filename(),
1080                                              input_file, offset, filesize);
1081 #else
1082         gold_error(_("%s: not configured to support "
1083                      "32-bit little-endian object"),
1084                    input_file->filename().c_str());
1085 #endif
1086     }
1087   else if (target.get_size() == 64)
1088     {
1089       if (target.is_big_endian())
1090 #ifdef HAVE_TARGET_64_BIG
1091         obj = new Sized_pluginobj<64, true>(input_file->filename(),
1092                                             input_file, offset, filesize);
1093 #else
1094         gold_error(_("%s: not configured to support "
1095                      "64-bit big-endian object"),
1096                    input_file->filename().c_str());
1097 #endif
1098       else
1099 #ifdef HAVE_TARGET_64_LITTLE
1100         obj = new Sized_pluginobj<64, false>(input_file->filename(),
1101                                              input_file, offset, filesize);
1102 #else
1103         gold_error(_("%s: not configured to support "
1104                      "64-bit little-endian object"),
1105                    input_file->filename().c_str());
1106 #endif
1107     }
1108
1109   gold_assert(obj != NULL);
1110   return obj;
1111 }
1112
1113 } // End namespace gold.