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