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