2010-05-21 Tristan Gingold <gingold@adacore.com>
[binutils.git] / gold / plugin.cc
blob2ee1301a537a735ddf0eab525210d4435c92c2e7
1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cstdio>
26 #include <cstdarg>
27 #include <cstring>
28 #include <string>
29 #include <vector>
31 #ifdef ENABLE_PLUGINS
32 #include <dlfcn.h>
33 #endif
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"
46 namespace gold
49 #ifdef ENABLE_PLUGINS
51 // The linker's exported interfaces.
53 extern "C"
56 static enum ld_plugin_status
57 register_claim_file(ld_plugin_claim_file_handler handler);
59 static enum ld_plugin_status
60 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
62 static enum ld_plugin_status
63 register_cleanup(ld_plugin_cleanup_handler handler);
65 static enum ld_plugin_status
66 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
68 static enum ld_plugin_status
69 get_input_file(const void *handle, struct ld_plugin_input_file *file);
71 static enum ld_plugin_status
72 release_input_file(const void *handle);
74 static enum ld_plugin_status
75 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
77 static enum ld_plugin_status
78 add_input_file(char *pathname);
80 static enum ld_plugin_status
81 add_input_library(char *pathname);
83 static enum ld_plugin_status
84 message(int level, const char *format, ...);
88 #endif // ENABLE_PLUGINS
90 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
91 off_t offset, off_t filesize);
93 // Plugin methods.
95 // Load one plugin library.
97 void
98 Plugin::load()
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)
106 gold_error(_("%s: could not load plugin library"),
107 this->filename_.c_str());
108 return;
111 // Find the plugin's onload entry point.
112 void* ptr = dlsym(this->handle_, "onload");
113 if (ptr == NULL)
115 gold_error(_("%s: could not find onload entry point"),
116 this->filename_.c_str());
117 return;
119 ld_plugin_onload onload;
120 gold_assert(sizeof(onload) == sizeof(ptr));
121 memcpy(&onload, &ptr, sizeof(ptr));
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);
129 // Allocate and populate a transfer vector.
130 const int tv_fixed_size = 14;
131 int tv_size = this->args_.size() + tv_fixed_size;
132 ld_plugin_tv *tv = new ld_plugin_tv[tv_size];
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;
140 ++i;
141 tv[i].tv_tag = LDPT_API_VERSION;
142 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
144 ++i;
145 tv[i].tv_tag = LDPT_GOLD_VERSION;
146 tv[i].tv_u.tv_val = major * 100 + minor;
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;
157 for (unsigned int j = 0; j < this->args_.size(); ++j)
159 ++i;
160 tv[i].tv_tag = LDPT_OPTION;
161 tv[i].tv_u.tv_string = this->args_[j].c_str();
164 ++i;
165 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
166 tv[i].tv_u.tv_register_claim_file = register_claim_file;
168 ++i;
169 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
170 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
172 ++i;
173 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
174 tv[i].tv_u.tv_register_cleanup = register_cleanup;
176 ++i;
177 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
178 tv[i].tv_u.tv_add_symbols = add_symbols;
180 ++i;
181 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
182 tv[i].tv_u.tv_get_input_file = get_input_file;
184 ++i;
185 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
186 tv[i].tv_u.tv_release_input_file = release_input_file;
188 ++i;
189 tv[i].tv_tag = LDPT_GET_SYMBOLS;
190 tv[i].tv_u.tv_get_symbols = get_symbols;
192 ++i;
193 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
194 tv[i].tv_u.tv_add_input_file = add_input_file;
196 ++i;
197 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
198 tv[i].tv_u.tv_add_input_library = add_input_library;
200 ++i;
201 tv[i].tv_tag = LDPT_NULL;
202 tv[i].tv_u.tv_val = 0;
204 gold_assert(i == tv_size - 1);
206 // Call the onload entry point.
207 (*onload)(tv);
209 delete[] tv;
210 #endif // ENABLE_PLUGINS
213 // Call the plugin claim-file handler.
215 inline bool
216 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
218 int claimed = 0;
220 if (this->claim_file_handler_ != NULL)
222 (*this->claim_file_handler_)(plugin_input_file, &claimed);
223 if (claimed)
224 return true;
226 return false;
229 // Call the all-symbols-read handler.
231 inline void
232 Plugin::all_symbols_read()
234 if (this->all_symbols_read_handler_ != NULL)
235 (*this->all_symbols_read_handler_)();
238 // Call the cleanup handler.
240 inline void
241 Plugin::cleanup()
243 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
245 // Set this flag before calling to prevent a recursive plunge
246 // in the event that a plugin's cleanup handler issues a
247 // fatal error.
248 this->cleanup_done_ = true;
249 (*this->cleanup_handler_)();
253 // Plugin_manager methods.
255 Plugin_manager::~Plugin_manager()
257 for (Plugin_list::iterator p = this->plugins_.begin();
258 p != this->plugins_.end();
259 ++p)
260 delete *p;
261 this->plugins_.clear();
262 for (Object_list::iterator obj = this->objects_.begin();
263 obj != this->objects_.end();
264 ++obj)
265 delete *obj;
266 this->objects_.clear();
269 // Load all plugin libraries.
271 void
272 Plugin_manager::load_plugins()
274 for (this->current_ = this->plugins_.begin();
275 this->current_ != this->plugins_.end();
276 ++this->current_)
277 (*this->current_)->load();
280 // Call the plugin claim-file handlers in turn to see if any claim the file.
282 Pluginobj*
283 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
284 off_t filesize)
286 if (this->in_replacement_phase_)
287 return NULL;
289 unsigned int handle = this->objects_.size();
290 this->input_file_ = input_file;
291 this->plugin_input_file_.name = input_file->filename().c_str();
292 this->plugin_input_file_.fd = input_file->file().descriptor();
293 this->plugin_input_file_.offset = offset;
294 this->plugin_input_file_.filesize = filesize;
295 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
297 for (this->current_ = this->plugins_.begin();
298 this->current_ != this->plugins_.end();
299 ++this->current_)
301 if ((*this->current_)->claim_file(&this->plugin_input_file_))
303 if (this->objects_.size() > handle)
304 return this->objects_[handle];
306 // If the plugin claimed the file but did not call the
307 // add_symbols callback, we need to create the Pluginobj now.
308 Pluginobj* obj = this->make_plugin_object(handle);
309 return obj;
313 return NULL;
316 // Call the all-symbols-read handlers.
318 void
319 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
320 Input_objects* input_objects,
321 Symbol_table* symtab, Layout* layout,
322 Dirsearch* dirpath, Mapfile* mapfile,
323 Task_token** last_blocker)
325 this->in_replacement_phase_ = true;
326 this->workqueue_ = workqueue;
327 this->task_ = task;
328 this->input_objects_ = input_objects;
329 this->symtab_ = symtab;
330 this->layout_ = layout;
331 this->dirpath_ = dirpath;
332 this->mapfile_ = mapfile;
333 this->this_blocker_ = NULL;
335 for (this->current_ = this->plugins_.begin();
336 this->current_ != this->plugins_.end();
337 ++this->current_)
338 (*this->current_)->all_symbols_read();
340 *last_blocker = this->this_blocker_;
343 // Layout deferred objects.
345 void
346 Plugin_manager::layout_deferred_objects()
348 Deferred_layout_list::iterator obj;
350 for (obj = this->deferred_layout_objects_.begin();
351 obj != this->deferred_layout_objects_.end();
352 ++obj)
353 (*obj)->layout_deferred_sections(this->layout_);
356 // Call the cleanup handlers.
358 void
359 Plugin_manager::cleanup()
361 for (this->current_ = this->plugins_.begin();
362 this->current_ != this->plugins_.end();
363 ++this->current_)
364 (*this->current_)->cleanup();
367 // Make a new Pluginobj object. This is called when the plugin calls
368 // the add_symbols API.
370 Pluginobj*
371 Plugin_manager::make_plugin_object(unsigned int handle)
373 // Make sure we aren't asked to make an object for the same handle twice.
374 if (this->objects_.size() != handle)
375 return NULL;
377 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
378 this->plugin_input_file_.offset,
379 this->plugin_input_file_.filesize);
380 this->objects_.push_back(obj);
381 return obj;
384 // Get the input file information with an open (possibly re-opened)
385 // file descriptor.
387 ld_plugin_status
388 Plugin_manager::get_input_file(unsigned int handle,
389 struct ld_plugin_input_file *file)
391 Pluginobj* obj = this->object(handle);
392 if (obj == NULL)
393 return LDPS_BAD_HANDLE;
395 obj->lock(this->task_);
396 file->name = obj->filename().c_str();
397 file->fd = obj->descriptor();
398 file->offset = obj->offset();
399 file->filesize = obj->filesize();
400 file->handle = reinterpret_cast<void*>(handle);
401 return LDPS_OK;
404 // Release the input file.
406 ld_plugin_status
407 Plugin_manager::release_input_file(unsigned int handle)
409 Pluginobj* obj = this->object(handle);
410 if (obj == NULL)
411 return LDPS_BAD_HANDLE;
413 obj->unlock(this->task_);
414 return LDPS_OK;
417 // Add a new input file.
419 ld_plugin_status
420 Plugin_manager::add_input_file(char *pathname, bool is_lib)
422 Input_file_argument file(pathname,
423 (is_lib
424 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
425 : Input_file_argument::INPUT_FILE_TYPE_FILE),
426 "", false, this->options_);
427 Input_argument* input_argument = new Input_argument(file);
428 Task_token* next_blocker = new Task_token(true);
429 next_blocker->add_blocker();
430 if (this->layout_->incremental_inputs())
431 gold_error(_("input files added by plug-ins in --incremental mode not "
432 "supported yet"));
433 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
434 this->symtab_,
435 this->layout_,
436 this->dirpath_,
438 this->mapfile_,
439 input_argument,
440 NULL,
441 NULL,
442 this->this_blocker_,
443 next_blocker));
444 this->this_blocker_ = next_blocker;
445 return LDPS_OK;
448 // Class Pluginobj.
450 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
451 off_t offset, off_t filesize)
452 : Object(name, input_file, false, offset),
453 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
457 // Return TRUE if a defined symbol might be reachable from outside the
458 // universe of claimed objects.
460 static inline bool
461 is_visible_from_outside(Symbol* lsym)
463 if (lsym->in_real_elf())
464 return true;
465 if (parameters->options().relocatable())
466 return true;
467 if (parameters->options().export_dynamic() || parameters->options().shared())
468 return lsym->is_externally_visible();
469 return false;
472 // Get symbol resolution info.
474 ld_plugin_status
475 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
477 if (nsyms > this->nsyms_)
478 return LDPS_NO_SYMS;
480 if (static_cast<size_t>(nsyms) > this->symbols_.size())
482 // We never decided to include this object. We mark all symbols as
483 // preempted.
484 gold_assert (this->symbols_.size() == 0);
485 for (int i = 0; i < nsyms; i++)
486 syms[i].resolution = LDPR_PREEMPTED_REG;
487 return LDPS_OK;
490 for (int i = 0; i < nsyms; i++)
492 ld_plugin_symbol* isym = &syms[i];
493 Symbol* lsym = this->symbols_[i];
494 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
496 if (lsym->is_undefined())
497 // The symbol remains undefined.
498 res = LDPR_UNDEF;
499 else if (isym->def == LDPK_UNDEF
500 || isym->def == LDPK_WEAKUNDEF
501 || isym->def == LDPK_COMMON)
503 // The original symbol was undefined or common.
504 if (lsym->source() != Symbol::FROM_OBJECT)
505 res = LDPR_RESOLVED_EXEC;
506 else if (lsym->object()->pluginobj() == this)
507 res = (is_visible_from_outside(lsym)
508 ? LDPR_PREVAILING_DEF
509 : LDPR_PREVAILING_DEF_IRONLY);
510 else if (lsym->object()->pluginobj() != NULL)
511 res = LDPR_RESOLVED_IR;
512 else if (lsym->object()->is_dynamic())
513 res = LDPR_RESOLVED_DYN;
514 else
515 res = LDPR_RESOLVED_EXEC;
517 else
519 // The original symbol was a definition.
520 if (lsym->source() != Symbol::FROM_OBJECT)
521 res = LDPR_PREEMPTED_REG;
522 else if (lsym->object() == static_cast<const Object*>(this))
523 res = (is_visible_from_outside(lsym)
524 ? LDPR_PREVAILING_DEF
525 : LDPR_PREVAILING_DEF_IRONLY);
526 else
527 res = (lsym->object()->pluginobj() != NULL
528 ? LDPR_PREEMPTED_IR
529 : LDPR_PREEMPTED_REG);
531 isym->resolution = res;
533 return LDPS_OK;
536 // Return TRUE if the comdat group with key COMDAT_KEY from this object
537 // should be kept.
539 bool
540 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
542 std::pair<Comdat_map::iterator, bool> ins =
543 this->comdat_map_.insert(std::make_pair(comdat_key, false));
545 // If this is the first time we've seen this comdat key, ask the
546 // layout object whether it should be included.
547 if (ins.second)
548 ins.first->second = layout->find_or_add_kept_section(comdat_key,
549 NULL, 0, true,
550 true, NULL);
552 return ins.first->second;
555 // Class Sized_pluginobj.
557 template<int size, bool big_endian>
558 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
559 const std::string& name,
560 Input_file* input_file,
561 off_t offset,
562 off_t filesize)
563 : Pluginobj(name, input_file, offset, filesize)
567 // Read the symbols. Not used for plugin objects.
569 template<int size, bool big_endian>
570 void
571 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
573 gold_unreachable();
576 // Lay out the input sections. Not used for plugin objects.
578 template<int size, bool big_endian>
579 void
580 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
581 Read_symbols_data*)
583 gold_unreachable();
586 // Add the symbols to the symbol table.
588 template<int size, bool big_endian>
589 void
590 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
591 Read_symbols_data*,
592 Layout* layout)
594 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
595 unsigned char symbuf[sym_size];
596 elfcpp::Sym<size, big_endian> sym(symbuf);
597 elfcpp::Sym_write<size, big_endian> osym(symbuf);
599 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
601 this->symbols_.resize(this->nsyms_);
603 for (int i = 0; i < this->nsyms_; ++i)
605 const struct ld_plugin_symbol *isym = &this->syms_[i];
606 const char* name = isym->name;
607 const char* ver = isym->version;
608 elfcpp::Elf_Half shndx;
609 elfcpp::STB bind;
610 elfcpp::STV vis;
612 if (name != NULL && name[0] == '\0')
613 name = NULL;
614 if (ver != NULL && ver[0] == '\0')
615 ver = NULL;
617 switch (isym->def)
619 case LDPK_WEAKDEF:
620 case LDPK_WEAKUNDEF:
621 bind = elfcpp::STB_WEAK;
622 break;
623 case LDPK_DEF:
624 case LDPK_UNDEF:
625 case LDPK_COMMON:
626 default:
627 bind = elfcpp::STB_GLOBAL;
628 break;
631 switch (isym->def)
633 case LDPK_DEF:
634 case LDPK_WEAKDEF:
635 shndx = elfcpp::SHN_ABS;
636 break;
637 case LDPK_COMMON:
638 shndx = elfcpp::SHN_COMMON;
639 break;
640 case LDPK_UNDEF:
641 case LDPK_WEAKUNDEF:
642 default:
643 shndx = elfcpp::SHN_UNDEF;
644 break;
647 switch (isym->visibility)
649 case LDPV_PROTECTED:
650 vis = elfcpp::STV_DEFAULT;
651 break;
652 case LDPV_INTERNAL:
653 vis = elfcpp::STV_DEFAULT;
654 break;
655 case LDPV_HIDDEN:
656 vis = elfcpp::STV_DEFAULT;
657 break;
658 case LDPV_DEFAULT:
659 default:
660 vis = elfcpp::STV_DEFAULT;
661 break;
664 if (isym->comdat_key != NULL
665 && isym->comdat_key[0] != '\0'
666 && !this->include_comdat_group(isym->comdat_key, layout))
667 shndx = elfcpp::SHN_UNDEF;
669 osym.put_st_name(0);
670 osym.put_st_value(0);
671 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
672 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
673 osym.put_st_other(vis, 0);
674 osym.put_st_shndx(shndx);
676 this->symbols_[i] =
677 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
681 template<int size, bool big_endian>
682 Archive::Should_include
683 Sized_pluginobj<size, big_endian>::do_should_include_member(
684 Symbol_table* symtab, Read_symbols_data*, std::string* why)
686 char* tmpbuf = NULL;
687 size_t tmpbuflen = 0;
689 for (int i = 0; i < this->nsyms_; ++i) {
690 const struct ld_plugin_symbol& sym = this->syms_[i];
691 const char* name = sym.name;
692 Symbol* symbol;
693 Archive::Should_include t = Archive::should_include_member(symtab, name,
694 &symbol, why,
695 &tmpbuf,
696 &tmpbuflen);
697 if (t == Archive::SHOULD_INCLUDE_YES)
699 if (tmpbuf != NULL)
700 free(tmpbuf);
701 return t;
704 if (tmpbuf != NULL)
705 free(tmpbuf);
706 return Archive::SHOULD_INCLUDE_UNKNOWN;
709 // Get the size of a section. Not used for plugin objects.
711 template<int size, bool big_endian>
712 uint64_t
713 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
715 gold_unreachable();
716 return 0;
719 // Get the name of a section. Not used for plugin objects.
721 template<int size, bool big_endian>
722 std::string
723 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
725 gold_unreachable();
726 return std::string();
729 // Return a view of the contents of a section. Not used for plugin objects.
731 template<int size, bool big_endian>
732 Object::Location
733 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
735 Location loc(0, 0);
737 gold_unreachable();
738 return loc;
741 // Return section flags. Not used for plugin objects.
743 template<int size, bool big_endian>
744 uint64_t
745 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
747 gold_unreachable();
748 return 0;
751 // Return section entsize. Not used for plugin objects.
753 template<int size, bool big_endian>
754 uint64_t
755 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
757 gold_unreachable();
758 return 0;
761 // Return section address. Not used for plugin objects.
763 template<int size, bool big_endian>
764 uint64_t
765 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
767 gold_unreachable();
768 return 0;
771 // Return section type. Not used for plugin objects.
773 template<int size, bool big_endian>
774 unsigned int
775 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
777 gold_unreachable();
778 return 0;
781 // Return the section link field. Not used for plugin objects.
783 template<int size, bool big_endian>
784 unsigned int
785 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
787 gold_unreachable();
788 return 0;
791 // Return the section link field. Not used for plugin objects.
793 template<int size, bool big_endian>
794 unsigned int
795 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
797 gold_unreachable();
798 return 0;
801 // Return the section alignment. Not used for plugin objects.
803 template<int size, bool big_endian>
804 uint64_t
805 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
807 gold_unreachable();
808 return 0;
811 // Return the Xindex structure to use. Not used for plugin objects.
813 template<int size, bool big_endian>
814 Xindex*
815 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
817 gold_unreachable();
818 return NULL;
821 // Get symbol counts. Not used for plugin objects.
823 template<int size, bool big_endian>
824 void
825 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
826 size_t*, size_t*) const
828 gold_unreachable();
831 // Get symbols. Not used for plugin objects.
833 template<int size, bool big_endian>
834 const Object::Symbols*
835 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
837 gold_unreachable();
840 // Class Plugin_finish. This task runs after all replacement files have
841 // been added. It calls each plugin's cleanup handler.
843 class Plugin_finish : public Task
845 public:
846 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
847 : this_blocker_(this_blocker), next_blocker_(next_blocker)
850 ~Plugin_finish()
852 if (this->this_blocker_ != NULL)
853 delete this->this_blocker_;
856 Task_token*
857 is_runnable()
859 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
860 return this->this_blocker_;
861 return NULL;
864 void
865 locks(Task_locker* tl)
866 { tl->add(this, this->next_blocker_); }
868 void
869 run(Workqueue*)
871 Plugin_manager* plugins = parameters->options().plugins();
872 gold_assert(plugins != NULL);
873 plugins->cleanup();
876 std::string
877 get_name() const
878 { return "Plugin_finish"; }
880 private:
881 Task_token* this_blocker_;
882 Task_token* next_blocker_;
885 // Class Plugin_hook.
887 Plugin_hook::~Plugin_hook()
891 // Return whether a Plugin_hook task is runnable.
893 Task_token*
894 Plugin_hook::is_runnable()
896 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
897 return this->this_blocker_;
898 return NULL;
901 // Return a Task_locker for a Plugin_hook task. We don't need any
902 // locks here.
904 void
905 Plugin_hook::locks(Task_locker*)
909 // Run the "all symbols read" plugin hook.
911 void
912 Plugin_hook::run(Workqueue* workqueue)
914 gold_assert(this->options_.has_plugins());
915 this->options_.plugins()->all_symbols_read(workqueue,
916 this,
917 this->input_objects_,
918 this->symtab_,
919 this->layout_,
920 this->dirpath_,
921 this->mapfile_,
922 &this->this_blocker_);
923 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
924 this->next_blocker_));
927 // The C interface routines called by the plugins.
929 #ifdef ENABLE_PLUGINS
931 // Register a claim-file handler.
933 static enum ld_plugin_status
934 register_claim_file(ld_plugin_claim_file_handler handler)
936 gold_assert(parameters->options().has_plugins());
937 parameters->options().plugins()->set_claim_file_handler(handler);
938 return LDPS_OK;
941 // Register an all-symbols-read handler.
943 static enum ld_plugin_status
944 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
946 gold_assert(parameters->options().has_plugins());
947 parameters->options().plugins()->set_all_symbols_read_handler(handler);
948 return LDPS_OK;
951 // Register a cleanup handler.
953 static enum ld_plugin_status
954 register_cleanup(ld_plugin_cleanup_handler handler)
956 gold_assert(parameters->options().has_plugins());
957 parameters->options().plugins()->set_cleanup_handler(handler);
958 return LDPS_OK;
961 // Add symbols from a plugin-claimed input file.
963 static enum ld_plugin_status
964 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
966 gold_assert(parameters->options().has_plugins());
967 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
968 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
969 if (obj == NULL)
970 return LDPS_ERR;
971 obj->store_incoming_symbols(nsyms, syms);
972 return LDPS_OK;
975 // Get the input file information with an open (possibly re-opened)
976 // file descriptor.
978 static enum ld_plugin_status
979 get_input_file(const void *handle, struct ld_plugin_input_file *file)
981 gold_assert(parameters->options().has_plugins());
982 unsigned int obj_index =
983 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
984 return parameters->options().plugins()->get_input_file(obj_index, file);
987 // Release the input file.
989 static enum ld_plugin_status
990 release_input_file(const void *handle)
992 gold_assert(parameters->options().has_plugins());
993 unsigned int obj_index =
994 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
995 return parameters->options().plugins()->release_input_file(obj_index);
998 // Get the symbol resolution info for a plugin-claimed input file.
1000 static enum ld_plugin_status
1001 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
1003 gold_assert(parameters->options().has_plugins());
1004 Pluginobj* obj = parameters->options().plugins()->object(
1005 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1006 if (obj == NULL)
1007 return LDPS_ERR;
1008 return obj->get_symbol_resolution_info(nsyms, syms);
1011 // Add a new (real) input file generated by a plugin.
1013 static enum ld_plugin_status
1014 add_input_file(char *pathname)
1016 gold_assert(parameters->options().has_plugins());
1017 return parameters->options().plugins()->add_input_file(pathname, false);
1020 // Add a new (real) library required by a plugin.
1022 static enum ld_plugin_status
1023 add_input_library(char *pathname)
1025 gold_assert(parameters->options().has_plugins());
1026 return parameters->options().plugins()->add_input_file(pathname, true);
1029 // Issue a diagnostic message from a plugin.
1031 static enum ld_plugin_status
1032 message(int level, const char * format, ...)
1034 va_list args;
1035 va_start(args, format);
1037 switch (level)
1039 case LDPL_INFO:
1040 parameters->errors()->info(format, args);
1041 break;
1042 case LDPL_WARNING:
1043 parameters->errors()->warning(format, args);
1044 break;
1045 case LDPL_ERROR:
1046 default:
1047 parameters->errors()->error(format, args);
1048 break;
1049 case LDPL_FATAL:
1050 parameters->errors()->fatal(format, args);
1051 break;
1054 va_end(args);
1055 return LDPS_OK;
1058 #endif // ENABLE_PLUGINS
1060 // Allocate a Pluginobj object of the appropriate size and endianness.
1062 static Pluginobj*
1063 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1065 Pluginobj* obj = NULL;
1067 parameters_force_valid_target();
1068 const Target& target(parameters->target());
1070 if (target.get_size() == 32)
1072 if (target.is_big_endian())
1073 #ifdef HAVE_TARGET_32_BIG
1074 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1075 input_file, offset, filesize);
1076 #else
1077 gold_error(_("%s: not configured to support "
1078 "32-bit big-endian object"),
1079 input_file->filename().c_str());
1080 #endif
1081 else
1082 #ifdef HAVE_TARGET_32_LITTLE
1083 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1084 input_file, offset, filesize);
1085 #else
1086 gold_error(_("%s: not configured to support "
1087 "32-bit little-endian object"),
1088 input_file->filename().c_str());
1089 #endif
1091 else if (target.get_size() == 64)
1093 if (target.is_big_endian())
1094 #ifdef HAVE_TARGET_64_BIG
1095 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1096 input_file, offset, filesize);
1097 #else
1098 gold_error(_("%s: not configured to support "
1099 "64-bit big-endian object"),
1100 input_file->filename().c_str());
1101 #endif
1102 else
1103 #ifdef HAVE_TARGET_64_LITTLE
1104 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1105 input_file, offset, filesize);
1106 #else
1107 gold_error(_("%s: not configured to support "
1108 "64-bit little-endian object"),
1109 input_file->filename().c_str());
1110 #endif
1113 gold_assert(obj != NULL);
1114 return obj;
1117 } // End namespace gold.