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