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