PR ld/11843
[binutils.git] / gold / plugin.cc
blob1c86c03b422415647dec82a80c0b781ae55e24ab
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(const char *pathname);
80 static enum ld_plugin_status
81 add_input_library(const char *pathname);
83 static enum ld_plugin_status
84 set_extra_library_path(const char *path);
86 static enum ld_plugin_status
87 message(int level, const char *format, ...);
91 #endif // ENABLE_PLUGINS
93 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
94 off_t offset, off_t filesize);
96 // Plugin methods.
98 // Load one plugin library.
100 void
101 Plugin::load()
103 #ifdef ENABLE_PLUGINS
104 // Load the plugin library.
105 // FIXME: Look for the library in standard locations.
106 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
107 if (this->handle_ == NULL)
109 gold_error(_("%s: could not load plugin library: %s"),
110 this->filename_.c_str(), dlerror());
111 return;
114 // Find the plugin's onload entry point.
115 void* ptr = dlsym(this->handle_, "onload");
116 if (ptr == NULL)
118 gold_error(_("%s: could not find onload entry point"),
119 this->filename_.c_str());
120 return;
122 ld_plugin_onload onload;
123 gold_assert(sizeof(onload) == sizeof(ptr));
124 memcpy(&onload, &ptr, sizeof(ptr));
126 // Get the linker's version number.
127 const char* ver = get_version_string();
128 int major = 0;
129 int minor = 0;
130 sscanf(ver, "%d.%d", &major, &minor);
132 // Allocate and populate a transfer vector.
133 const int tv_fixed_size = 16;
134 int tv_size = this->args_.size() + tv_fixed_size;
135 ld_plugin_tv *tv = new ld_plugin_tv[tv_size];
137 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
138 // while processing subsequent entries.
139 int i = 0;
140 tv[i].tv_tag = LDPT_MESSAGE;
141 tv[i].tv_u.tv_message = message;
143 ++i;
144 tv[i].tv_tag = LDPT_API_VERSION;
145 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
147 ++i;
148 tv[i].tv_tag = LDPT_GOLD_VERSION;
149 tv[i].tv_u.tv_val = major * 100 + minor;
151 ++i;
152 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
153 if (parameters->options().relocatable())
154 tv[i].tv_u.tv_val = LDPO_REL;
155 else if (parameters->options().shared())
156 tv[i].tv_u.tv_val = LDPO_DYN;
157 else
158 tv[i].tv_u.tv_val = LDPO_EXEC;
160 ++i;
161 tv[i].tv_tag = LDPT_OUTPUT_NAME;
162 tv[i].tv_u.tv_string = parameters->options().output();
164 for (unsigned int j = 0; j < this->args_.size(); ++j)
166 ++i;
167 tv[i].tv_tag = LDPT_OPTION;
168 tv[i].tv_u.tv_string = this->args_[j].c_str();
171 ++i;
172 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
173 tv[i].tv_u.tv_register_claim_file = register_claim_file;
175 ++i;
176 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
177 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
179 ++i;
180 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
181 tv[i].tv_u.tv_register_cleanup = register_cleanup;
183 ++i;
184 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
185 tv[i].tv_u.tv_add_symbols = add_symbols;
187 ++i;
188 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
189 tv[i].tv_u.tv_get_input_file = get_input_file;
191 ++i;
192 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
193 tv[i].tv_u.tv_release_input_file = release_input_file;
195 ++i;
196 tv[i].tv_tag = LDPT_GET_SYMBOLS;
197 tv[i].tv_u.tv_get_symbols = get_symbols;
199 ++i;
200 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
201 tv[i].tv_u.tv_add_input_file = add_input_file;
203 ++i;
204 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
205 tv[i].tv_u.tv_add_input_library = add_input_library;
207 ++i;
208 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
209 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
211 ++i;
212 tv[i].tv_tag = LDPT_NULL;
213 tv[i].tv_u.tv_val = 0;
215 gold_assert(i == tv_size - 1);
217 // Call the onload entry point.
218 (*onload)(tv);
220 delete[] tv;
221 #endif // ENABLE_PLUGINS
224 // Call the plugin claim-file handler.
226 inline bool
227 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
229 int claimed = 0;
231 if (this->claim_file_handler_ != NULL)
233 (*this->claim_file_handler_)(plugin_input_file, &claimed);
234 if (claimed)
235 return true;
237 return false;
240 // Call the all-symbols-read handler.
242 inline void
243 Plugin::all_symbols_read()
245 if (this->all_symbols_read_handler_ != NULL)
246 (*this->all_symbols_read_handler_)();
249 // Call the cleanup handler.
251 inline void
252 Plugin::cleanup()
254 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
256 // Set this flag before calling to prevent a recursive plunge
257 // in the event that a plugin's cleanup handler issues a
258 // fatal error.
259 this->cleanup_done_ = true;
260 (*this->cleanup_handler_)();
264 // Plugin_manager methods.
266 Plugin_manager::~Plugin_manager()
268 for (Plugin_list::iterator p = this->plugins_.begin();
269 p != this->plugins_.end();
270 ++p)
271 delete *p;
272 this->plugins_.clear();
273 for (Object_list::iterator obj = this->objects_.begin();
274 obj != this->objects_.end();
275 ++obj)
276 delete *obj;
277 this->objects_.clear();
280 // Load all plugin libraries.
282 void
283 Plugin_manager::load_plugins()
285 for (this->current_ = this->plugins_.begin();
286 this->current_ != this->plugins_.end();
287 ++this->current_)
288 (*this->current_)->load();
291 // Call the plugin claim-file handlers in turn to see if any claim the file.
293 Pluginobj*
294 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
295 off_t filesize)
297 if (this->in_replacement_phase_)
298 return NULL;
300 unsigned int handle = this->objects_.size();
301 this->input_file_ = input_file;
302 this->plugin_input_file_.name = input_file->filename().c_str();
303 this->plugin_input_file_.fd = input_file->file().descriptor();
304 this->plugin_input_file_.offset = offset;
305 this->plugin_input_file_.filesize = filesize;
306 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
308 for (this->current_ = this->plugins_.begin();
309 this->current_ != this->plugins_.end();
310 ++this->current_)
312 if ((*this->current_)->claim_file(&this->plugin_input_file_))
314 if (this->objects_.size() > handle)
315 return this->objects_[handle];
317 // If the plugin claimed the file but did not call the
318 // add_symbols callback, we need to create the Pluginobj now.
319 Pluginobj* obj = this->make_plugin_object(handle);
320 return obj;
324 return NULL;
327 // Call the all-symbols-read handlers.
329 void
330 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
331 Input_objects* input_objects,
332 Symbol_table* symtab, Layout* layout,
333 Dirsearch* dirpath, Mapfile* mapfile,
334 Task_token** last_blocker)
336 this->in_replacement_phase_ = true;
337 this->workqueue_ = workqueue;
338 this->task_ = task;
339 this->input_objects_ = input_objects;
340 this->symtab_ = symtab;
341 this->layout_ = layout;
342 this->dirpath_ = dirpath;
343 this->mapfile_ = mapfile;
344 this->this_blocker_ = NULL;
346 for (this->current_ = this->plugins_.begin();
347 this->current_ != this->plugins_.end();
348 ++this->current_)
349 (*this->current_)->all_symbols_read();
351 *last_blocker = this->this_blocker_;
354 // Layout deferred objects.
356 void
357 Plugin_manager::layout_deferred_objects()
359 Deferred_layout_list::iterator obj;
361 for (obj = this->deferred_layout_objects_.begin();
362 obj != this->deferred_layout_objects_.end();
363 ++obj)
364 (*obj)->layout_deferred_sections(this->layout_);
367 // Call the cleanup handlers.
369 void
370 Plugin_manager::cleanup()
372 for (this->current_ = this->plugins_.begin();
373 this->current_ != this->plugins_.end();
374 ++this->current_)
375 (*this->current_)->cleanup();
378 // Make a new Pluginobj object. This is called when the plugin calls
379 // the add_symbols API.
381 Pluginobj*
382 Plugin_manager::make_plugin_object(unsigned int handle)
384 // Make sure we aren't asked to make an object for the same handle twice.
385 if (this->objects_.size() != handle)
386 return NULL;
388 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
389 this->plugin_input_file_.offset,
390 this->plugin_input_file_.filesize);
391 this->objects_.push_back(obj);
392 return obj;
395 // Get the input file information with an open (possibly re-opened)
396 // file descriptor.
398 ld_plugin_status
399 Plugin_manager::get_input_file(unsigned int handle,
400 struct ld_plugin_input_file *file)
402 Pluginobj* obj = this->object(handle);
403 if (obj == NULL)
404 return LDPS_BAD_HANDLE;
406 obj->lock(this->task_);
407 file->name = obj->filename().c_str();
408 file->fd = obj->descriptor();
409 file->offset = obj->offset();
410 file->filesize = obj->filesize();
411 file->handle = reinterpret_cast<void*>(handle);
412 return LDPS_OK;
415 // Release the input file.
417 ld_plugin_status
418 Plugin_manager::release_input_file(unsigned int handle)
420 Pluginobj* obj = this->object(handle);
421 if (obj == NULL)
422 return LDPS_BAD_HANDLE;
424 obj->unlock(this->task_);
425 return LDPS_OK;
428 // Add a new library path.
430 ld_plugin_status
431 Plugin_manager::set_extra_library_path(const char *path)
433 this->extra_search_path_ = std::string(path);
434 return LDPS_OK;
437 // Add a new input file.
439 ld_plugin_status
440 Plugin_manager::add_input_file(const char *pathname, bool is_lib)
442 Input_file_argument file(pathname,
443 (is_lib
444 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
445 : Input_file_argument::INPUT_FILE_TYPE_FILE),
446 (is_lib
447 ? this->extra_search_path_.c_str()
448 : ""),
449 false,
450 this->options_);
451 Input_argument* input_argument = new Input_argument(file);
452 Task_token* next_blocker = new Task_token(true);
453 next_blocker->add_blocker();
454 if (this->layout_->incremental_inputs())
455 gold_error(_("input files added by plug-ins in --incremental mode not "
456 "supported yet"));
457 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
458 this->symtab_,
459 this->layout_,
460 this->dirpath_,
462 this->mapfile_,
463 input_argument,
464 NULL,
465 NULL,
466 this->this_blocker_,
467 next_blocker));
468 this->this_blocker_ = next_blocker;
469 return LDPS_OK;
472 // Class Pluginobj.
474 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
475 off_t offset, off_t filesize)
476 : Object(name, input_file, false, offset),
477 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
481 // Return TRUE if a defined symbol might be reachable from outside the
482 // universe of claimed objects.
484 static inline bool
485 is_visible_from_outside(Symbol* lsym)
487 if (lsym->in_real_elf())
488 return true;
489 if (parameters->options().relocatable())
490 return true;
491 if (parameters->options().export_dynamic() || parameters->options().shared())
492 return lsym->is_externally_visible();
493 return false;
496 // Get symbol resolution info.
498 ld_plugin_status
499 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
501 if (nsyms > this->nsyms_)
502 return LDPS_NO_SYMS;
504 if (static_cast<size_t>(nsyms) > this->symbols_.size())
506 // We never decided to include this object. We mark all symbols as
507 // preempted.
508 gold_assert (this->symbols_.size() == 0);
509 for (int i = 0; i < nsyms; i++)
510 syms[i].resolution = LDPR_PREEMPTED_REG;
511 return LDPS_OK;
514 for (int i = 0; i < nsyms; i++)
516 ld_plugin_symbol* isym = &syms[i];
517 Symbol* lsym = this->symbols_[i];
518 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
520 if (lsym->is_undefined())
521 // The symbol remains undefined.
522 res = LDPR_UNDEF;
523 else if (isym->def == LDPK_UNDEF
524 || isym->def == LDPK_WEAKUNDEF
525 || isym->def == LDPK_COMMON)
527 // The original symbol was undefined or common.
528 if (lsym->source() != Symbol::FROM_OBJECT)
529 res = LDPR_RESOLVED_EXEC;
530 else if (lsym->object()->pluginobj() == this)
531 res = (is_visible_from_outside(lsym)
532 ? LDPR_PREVAILING_DEF
533 : LDPR_PREVAILING_DEF_IRONLY);
534 else if (lsym->object()->pluginobj() != NULL)
535 res = LDPR_RESOLVED_IR;
536 else if (lsym->object()->is_dynamic())
537 res = LDPR_RESOLVED_DYN;
538 else
539 res = LDPR_RESOLVED_EXEC;
541 else
543 // The original symbol was a definition.
544 if (lsym->source() != Symbol::FROM_OBJECT)
545 res = LDPR_PREEMPTED_REG;
546 else if (lsym->object() == static_cast<const Object*>(this))
547 res = (is_visible_from_outside(lsym)
548 ? LDPR_PREVAILING_DEF
549 : LDPR_PREVAILING_DEF_IRONLY);
550 else
551 res = (lsym->object()->pluginobj() != NULL
552 ? LDPR_PREEMPTED_IR
553 : LDPR_PREEMPTED_REG);
555 isym->resolution = res;
557 return LDPS_OK;
560 // Return TRUE if the comdat group with key COMDAT_KEY from this object
561 // should be kept.
563 bool
564 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
566 std::pair<Comdat_map::iterator, bool> ins =
567 this->comdat_map_.insert(std::make_pair(comdat_key, false));
569 // If this is the first time we've seen this comdat key, ask the
570 // layout object whether it should be included.
571 if (ins.second)
572 ins.first->second = layout->find_or_add_kept_section(comdat_key,
573 NULL, 0, true,
574 true, NULL);
576 return ins.first->second;
579 // Class Sized_pluginobj.
581 template<int size, bool big_endian>
582 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
583 const std::string& name,
584 Input_file* input_file,
585 off_t offset,
586 off_t filesize)
587 : Pluginobj(name, input_file, offset, filesize)
591 // Read the symbols. Not used for plugin objects.
593 template<int size, bool big_endian>
594 void
595 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
597 gold_unreachable();
600 // Lay out the input sections. Not used for plugin objects.
602 template<int size, bool big_endian>
603 void
604 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
605 Read_symbols_data*)
607 gold_unreachable();
610 // Add the symbols to the symbol table.
612 template<int size, bool big_endian>
613 void
614 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
615 Read_symbols_data*,
616 Layout* layout)
618 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
619 unsigned char symbuf[sym_size];
620 elfcpp::Sym<size, big_endian> sym(symbuf);
621 elfcpp::Sym_write<size, big_endian> osym(symbuf);
623 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
625 this->symbols_.resize(this->nsyms_);
627 for (int i = 0; i < this->nsyms_; ++i)
629 const struct ld_plugin_symbol *isym = &this->syms_[i];
630 const char* name = isym->name;
631 const char* ver = isym->version;
632 elfcpp::Elf_Half shndx;
633 elfcpp::STB bind;
634 elfcpp::STV vis;
636 if (name != NULL && name[0] == '\0')
637 name = NULL;
638 if (ver != NULL && ver[0] == '\0')
639 ver = NULL;
641 switch (isym->def)
643 case LDPK_WEAKDEF:
644 case LDPK_WEAKUNDEF:
645 bind = elfcpp::STB_WEAK;
646 break;
647 case LDPK_DEF:
648 case LDPK_UNDEF:
649 case LDPK_COMMON:
650 default:
651 bind = elfcpp::STB_GLOBAL;
652 break;
655 switch (isym->def)
657 case LDPK_DEF:
658 case LDPK_WEAKDEF:
659 shndx = elfcpp::SHN_ABS;
660 break;
661 case LDPK_COMMON:
662 shndx = elfcpp::SHN_COMMON;
663 break;
664 case LDPK_UNDEF:
665 case LDPK_WEAKUNDEF:
666 default:
667 shndx = elfcpp::SHN_UNDEF;
668 break;
671 switch (isym->visibility)
673 case LDPV_PROTECTED:
674 vis = elfcpp::STV_PROTECTED;
675 break;
676 case LDPV_INTERNAL:
677 vis = elfcpp::STV_INTERNAL;
678 break;
679 case LDPV_HIDDEN:
680 vis = elfcpp::STV_HIDDEN;
681 break;
682 case LDPV_DEFAULT:
683 default:
684 vis = elfcpp::STV_DEFAULT;
685 break;
688 if (isym->comdat_key != NULL
689 && isym->comdat_key[0] != '\0'
690 && !this->include_comdat_group(isym->comdat_key, layout))
691 shndx = elfcpp::SHN_UNDEF;
693 osym.put_st_name(0);
694 osym.put_st_value(0);
695 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
696 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
697 osym.put_st_other(vis, 0);
698 osym.put_st_shndx(shndx);
700 this->symbols_[i] =
701 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
705 template<int size, bool big_endian>
706 Archive::Should_include
707 Sized_pluginobj<size, big_endian>::do_should_include_member(
708 Symbol_table* symtab,
709 Layout* layout,
710 Read_symbols_data*,
711 std::string* why)
713 char* tmpbuf = NULL;
714 size_t tmpbuflen = 0;
716 for (int i = 0; i < this->nsyms_; ++i)
718 const struct ld_plugin_symbol& sym = this->syms_[i];
719 const char* name = sym.name;
720 Symbol* symbol;
721 Archive::Should_include t = Archive::should_include_member(symtab,
722 layout,
723 name,
724 &symbol, why,
725 &tmpbuf,
726 &tmpbuflen);
727 if (t == Archive::SHOULD_INCLUDE_YES)
729 if (tmpbuf != NULL)
730 free(tmpbuf);
731 return t;
734 if (tmpbuf != NULL)
735 free(tmpbuf);
736 return Archive::SHOULD_INCLUDE_UNKNOWN;
739 // Get the size of a section. Not used for plugin objects.
741 template<int size, bool big_endian>
742 uint64_t
743 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
745 gold_unreachable();
746 return 0;
749 // Get the name of a section. Not used for plugin objects.
751 template<int size, bool big_endian>
752 std::string
753 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
755 gold_unreachable();
756 return std::string();
759 // Return a view of the contents of a section. Not used for plugin objects.
761 template<int size, bool big_endian>
762 Object::Location
763 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
765 Location loc(0, 0);
767 gold_unreachable();
768 return loc;
771 // Return section flags. Not used for plugin objects.
773 template<int size, bool big_endian>
774 uint64_t
775 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
777 gold_unreachable();
778 return 0;
781 // Return section entsize. Not used for plugin objects.
783 template<int size, bool big_endian>
784 uint64_t
785 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
787 gold_unreachable();
788 return 0;
791 // Return section address. Not used for plugin objects.
793 template<int size, bool big_endian>
794 uint64_t
795 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
797 gold_unreachable();
798 return 0;
801 // Return section type. Not used for plugin objects.
803 template<int size, bool big_endian>
804 unsigned int
805 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
807 gold_unreachable();
808 return 0;
811 // Return the section link field. Not used for plugin objects.
813 template<int size, bool big_endian>
814 unsigned int
815 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
817 gold_unreachable();
818 return 0;
821 // Return the section link field. Not used for plugin objects.
823 template<int size, bool big_endian>
824 unsigned int
825 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
827 gold_unreachable();
828 return 0;
831 // Return the section alignment. Not used for plugin objects.
833 template<int size, bool big_endian>
834 uint64_t
835 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
837 gold_unreachable();
838 return 0;
841 // Return the Xindex structure to use. Not used for plugin objects.
843 template<int size, bool big_endian>
844 Xindex*
845 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
847 gold_unreachable();
848 return NULL;
851 // Get symbol counts. Not used for plugin objects.
853 template<int size, bool big_endian>
854 void
855 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
856 size_t*, size_t*) const
858 gold_unreachable();
861 // Get symbols. Not used for plugin objects.
863 template<int size, bool big_endian>
864 const Object::Symbols*
865 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
867 gold_unreachable();
870 // Class Plugin_finish. This task runs after all replacement files have
871 // been added. For now, it's a placeholder for a possible plugin API
872 // to allow the plugin to release most of its resources. The cleanup
873 // handlers must be called later, because they can remove the temporary
874 // object files that are needed until the end of the link.
876 class Plugin_finish : public Task
878 public:
879 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
880 : this_blocker_(this_blocker), next_blocker_(next_blocker)
883 ~Plugin_finish()
885 if (this->this_blocker_ != NULL)
886 delete this->this_blocker_;
889 Task_token*
890 is_runnable()
892 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
893 return this->this_blocker_;
894 return NULL;
897 void
898 locks(Task_locker* tl)
899 { tl->add(this, this->next_blocker_); }
901 void
902 run(Workqueue*)
904 // We could call early cleanup handlers here.
907 std::string
908 get_name() const
909 { return "Plugin_finish"; }
911 private:
912 Task_token* this_blocker_;
913 Task_token* next_blocker_;
916 // Class Plugin_hook.
918 Plugin_hook::~Plugin_hook()
922 // Return whether a Plugin_hook task is runnable.
924 Task_token*
925 Plugin_hook::is_runnable()
927 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
928 return this->this_blocker_;
929 return NULL;
932 // Return a Task_locker for a Plugin_hook task. We don't need any
933 // locks here.
935 void
936 Plugin_hook::locks(Task_locker*)
940 // Run the "all symbols read" plugin hook.
942 void
943 Plugin_hook::run(Workqueue* workqueue)
945 gold_assert(this->options_.has_plugins());
946 Symbol* start_sym;
947 if (parameters->options().entry())
948 start_sym = this->symtab_->lookup(parameters->options().entry());
949 else
950 start_sym = this->symtab_->lookup("_start");
951 if (start_sym != NULL)
952 start_sym->set_in_real_elf();
954 this->options_.plugins()->all_symbols_read(workqueue,
955 this,
956 this->input_objects_,
957 this->symtab_,
958 this->layout_,
959 this->dirpath_,
960 this->mapfile_,
961 &this->this_blocker_);
962 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
963 this->next_blocker_));
966 // The C interface routines called by the plugins.
968 #ifdef ENABLE_PLUGINS
970 // Register a claim-file handler.
972 static enum ld_plugin_status
973 register_claim_file(ld_plugin_claim_file_handler handler)
975 gold_assert(parameters->options().has_plugins());
976 parameters->options().plugins()->set_claim_file_handler(handler);
977 return LDPS_OK;
980 // Register an all-symbols-read handler.
982 static enum ld_plugin_status
983 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
985 gold_assert(parameters->options().has_plugins());
986 parameters->options().plugins()->set_all_symbols_read_handler(handler);
987 return LDPS_OK;
990 // Register a cleanup handler.
992 static enum ld_plugin_status
993 register_cleanup(ld_plugin_cleanup_handler handler)
995 gold_assert(parameters->options().has_plugins());
996 parameters->options().plugins()->set_cleanup_handler(handler);
997 return LDPS_OK;
1000 // Add symbols from a plugin-claimed input file.
1002 static enum ld_plugin_status
1003 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
1005 gold_assert(parameters->options().has_plugins());
1006 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1007 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1008 if (obj == NULL)
1009 return LDPS_ERR;
1010 obj->store_incoming_symbols(nsyms, syms);
1011 return LDPS_OK;
1014 // Get the input file information with an open (possibly re-opened)
1015 // file descriptor.
1017 static enum ld_plugin_status
1018 get_input_file(const void *handle, struct ld_plugin_input_file *file)
1020 gold_assert(parameters->options().has_plugins());
1021 unsigned int obj_index =
1022 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1023 return parameters->options().plugins()->get_input_file(obj_index, file);
1026 // Release the input file.
1028 static enum ld_plugin_status
1029 release_input_file(const void *handle)
1031 gold_assert(parameters->options().has_plugins());
1032 unsigned int obj_index =
1033 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1034 return parameters->options().plugins()->release_input_file(obj_index);
1037 // Get the symbol resolution info for a plugin-claimed input file.
1039 static enum ld_plugin_status
1040 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
1042 gold_assert(parameters->options().has_plugins());
1043 Pluginobj* obj = parameters->options().plugins()->object(
1044 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1045 if (obj == NULL)
1046 return LDPS_ERR;
1047 return obj->get_symbol_resolution_info(nsyms, syms);
1050 // Add a new (real) input file generated by a plugin.
1052 static enum ld_plugin_status
1053 add_input_file(const char *pathname)
1055 gold_assert(parameters->options().has_plugins());
1056 return parameters->options().plugins()->add_input_file(pathname, false);
1059 // Add a new (real) library required by a plugin.
1061 static enum ld_plugin_status
1062 add_input_library(const char *pathname)
1064 gold_assert(parameters->options().has_plugins());
1065 return parameters->options().plugins()->add_input_file(pathname, true);
1068 // Set the extra library path to be used by libraries added via
1069 // add_input_library
1071 static enum ld_plugin_status
1072 set_extra_library_path(const char *path)
1074 gold_assert(parameters->options().has_plugins());
1075 return parameters->options().plugins()->set_extra_library_path(path);
1078 // Issue a diagnostic message from a plugin.
1080 static enum ld_plugin_status
1081 message(int level, const char * format, ...)
1083 va_list args;
1084 va_start(args, format);
1086 switch (level)
1088 case LDPL_INFO:
1089 parameters->errors()->info(format, args);
1090 break;
1091 case LDPL_WARNING:
1092 parameters->errors()->warning(format, args);
1093 break;
1094 case LDPL_ERROR:
1095 default:
1096 parameters->errors()->error(format, args);
1097 break;
1098 case LDPL_FATAL:
1099 parameters->errors()->fatal(format, args);
1100 break;
1103 va_end(args);
1104 return LDPS_OK;
1107 #endif // ENABLE_PLUGINS
1109 // Allocate a Pluginobj object of the appropriate size and endianness.
1111 static Pluginobj*
1112 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1114 Pluginobj* obj = NULL;
1116 parameters_force_valid_target();
1117 const Target& target(parameters->target());
1119 if (target.get_size() == 32)
1121 if (target.is_big_endian())
1122 #ifdef HAVE_TARGET_32_BIG
1123 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1124 input_file, offset, filesize);
1125 #else
1126 gold_error(_("%s: not configured to support "
1127 "32-bit big-endian object"),
1128 input_file->filename().c_str());
1129 #endif
1130 else
1131 #ifdef HAVE_TARGET_32_LITTLE
1132 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1133 input_file, offset, filesize);
1134 #else
1135 gold_error(_("%s: not configured to support "
1136 "32-bit little-endian object"),
1137 input_file->filename().c_str());
1138 #endif
1140 else if (target.get_size() == 64)
1142 if (target.is_big_endian())
1143 #ifdef HAVE_TARGET_64_BIG
1144 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1145 input_file, offset, filesize);
1146 #else
1147 gold_error(_("%s: not configured to support "
1148 "64-bit big-endian object"),
1149 input_file->filename().c_str());
1150 #endif
1151 else
1152 #ifdef HAVE_TARGET_64_LITTLE
1153 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1154 input_file, offset, filesize);
1155 #else
1156 gold_error(_("%s: not configured to support "
1157 "64-bit little-endian object"),
1158 input_file->filename().c_str());
1159 #endif
1162 gold_assert(obj != NULL);
1163 return obj;
1166 } // End namespace gold.