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