1 // plugin.c -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008 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.
31 #include "parameters.h"
47 // The linker's exported interfaces.
52 static enum ld_plugin_status
53 register_claim_file(ld_plugin_claim_file_handler handler);
55 static enum ld_plugin_status
56 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
58 static enum ld_plugin_status
59 register_cleanup(ld_plugin_cleanup_handler handler);
61 static enum ld_plugin_status
62 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
64 static enum ld_plugin_status
65 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
67 static enum ld_plugin_status
68 add_input_file(char *pathname);
70 static enum ld_plugin_status
71 message(int level, const char *format, ...);
75 #endif // ENABLE_PLUGINS
77 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
82 // Load one plugin library.
88 // Load the plugin library.
89 // FIXME: Look for the library in standard locations.
90 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
91 if (this->handle_ == NULL)
93 gold_error(_("%s: could not load plugin library"),
94 this->filename_.c_str());
98 // Find the plugin's onload entry point.
99 ld_plugin_onload onload = reinterpret_cast<ld_plugin_onload>
100 (dlsym(this->handle_, "onload"));
103 gold_error(_("%s: could not find onload entry point"),
104 this->filename_.c_str());
108 // Get the linker's version number.
109 const char* ver = get_version_string();
112 sscanf(ver, "%d.%d", &major, &minor);
114 // Allocate and populate a transfer vector.
115 const int tv_fixed_size = 11;
116 int tv_size = this->args_.size() + tv_fixed_size;
117 ld_plugin_tv *tv = new ld_plugin_tv[tv_size];
119 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
120 // while processing subsequent entries.
122 tv[i].tv_tag = LDPT_MESSAGE;
123 tv[i].tv_u.tv_message = message;
126 tv[i].tv_tag = LDPT_API_VERSION;
127 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
130 tv[i].tv_tag = LDPT_GOLD_VERSION;
131 tv[i].tv_u.tv_val = major * 100 + minor;
134 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
135 if (parameters->options().relocatable())
136 tv[i].tv_u.tv_val = LDPO_REL;
137 else if (parameters->options().shared())
138 tv[i].tv_u.tv_val = LDPO_DYN;
140 tv[i].tv_u.tv_val = LDPO_EXEC;
142 for (unsigned int j = 0; j < this->args_.size(); ++j)
145 tv[i].tv_tag = LDPT_OPTION;
146 tv[i].tv_u.tv_string = this->args_[j].c_str();
150 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
151 tv[i].tv_u.tv_register_claim_file = register_claim_file;
154 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
155 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
158 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
159 tv[i].tv_u.tv_register_cleanup = register_cleanup;
162 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
163 tv[i].tv_u.tv_add_symbols = add_symbols;
166 tv[i].tv_tag = LDPT_GET_SYMBOLS;
167 tv[i].tv_u.tv_get_symbols = get_symbols;
170 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
171 tv[i].tv_u.tv_add_input_file = add_input_file;
174 tv[i].tv_tag = LDPT_NULL;
175 tv[i].tv_u.tv_val = 0;
177 gold_assert(i == tv_size - 1);
179 // Call the onload entry point.
183 #endif // ENABLE_PLUGINS
186 // Call the plugin claim-file handler.
189 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
193 if (this->claim_file_handler_ != NULL)
195 (*this->claim_file_handler_)(plugin_input_file, &claimed);
202 // Call the all-symbols-read handler.
205 Plugin::all_symbols_read()
207 if (this->all_symbols_read_handler_ != NULL)
208 (*this->all_symbols_read_handler_)();
211 // Call the cleanup handler.
216 if (this->cleanup_handler_ != NULL)
217 (*this->cleanup_handler_)();
220 // Plugin_manager methods.
222 Plugin_manager::~Plugin_manager()
224 for (Plugin_list::iterator p = this->plugins_.begin();
225 p != this->plugins_.end();
228 this->plugins_.clear();
229 for (Object_list::iterator obj = this->objects_.begin();
230 obj != this->objects_.end();
233 this->objects_.clear();
236 // Load all plugin libraries.
239 Plugin_manager::load_plugins()
241 for (this->current_ = this->plugins_.begin();
242 this->current_ != this->plugins_.end();
244 (*this->current_)->load();
247 // Call the plugin claim-file handlers in turn to see if any claim the file.
250 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
253 if (this->in_replacement_phase_)
256 unsigned int handle = this->objects_.size();
257 this->input_file_ = input_file;
258 this->plugin_input_file_.name = input_file->filename().c_str();
259 this->plugin_input_file_.fd = input_file->file().descriptor();
260 this->plugin_input_file_.offset = offset;
261 this->plugin_input_file_.filesize = filesize;
262 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
264 for (this->current_ = this->plugins_.begin();
265 this->current_ != this->plugins_.end();
268 if ((*this->current_)->claim_file(&this->plugin_input_file_))
270 if (this->objects_.size() > handle)
271 return this->objects_[handle];
273 // If the plugin claimed the file but did not call the
274 // add_symbols callback, we need to create the Pluginobj now.
275 Pluginobj* obj = this->make_plugin_object(handle);
283 // Call the all-symbols-read handlers.
286 Plugin_manager::all_symbols_read(Workqueue* workqueue,
287 Input_objects* input_objects,
288 Symbol_table* symtab, Layout* layout,
289 Dirsearch* dirpath, Mapfile* mapfile,
290 Task_token** last_blocker)
292 this->in_replacement_phase_ = true;
293 this->workqueue_ = workqueue;
294 this->input_objects_ = input_objects;
295 this->symtab_ = symtab;
296 this->layout_ = layout;
297 this->dirpath_ = dirpath;
298 this->mapfile_ = mapfile;
299 this->this_blocker_ = NULL;
301 for (this->current_ = this->plugins_.begin();
302 this->current_ != this->plugins_.end();
304 (*this->current_)->all_symbols_read();
306 *last_blocker = this->this_blocker_;
309 // Layout deferred objects.
312 Plugin_manager::layout_deferred_objects()
314 Deferred_layout_list::iterator obj;
316 for (obj = this->deferred_layout_objects_.begin();
317 obj != this->deferred_layout_objects_.end();
319 (*obj)->layout_deferred_sections(this->layout_);
322 // Call the cleanup handlers.
325 Plugin_manager::cleanup()
327 if (this->cleanup_done_)
329 for (this->current_ = this->plugins_.begin();
330 this->current_ != this->plugins_.end();
332 (*this->current_)->cleanup();
333 this->cleanup_done_ = true;
336 // Make a new Pluginobj object. This is called when the plugin calls
337 // the add_symbols API.
340 Plugin_manager::make_plugin_object(unsigned int handle)
342 // Make sure we aren't asked to make an object for the same handle twice.
343 if (this->objects_.size() != handle)
346 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
347 this->plugin_input_file_.offset);
348 this->objects_.push_back(obj);
352 // Add a new input file.
355 Plugin_manager::add_input_file(char *pathname)
357 Input_file_argument file(pathname, false, "", false, this->options_);
358 Input_argument* input_argument = new Input_argument(file);
359 Task_token* next_blocker = new Task_token(true);
360 next_blocker->add_blocker();
361 this->workqueue_->queue_soon(new Read_symbols(this->options_,
362 this->input_objects_,
371 this->this_blocker_ = next_blocker;
377 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
379 : Object(name, input_file, false, offset),
380 nsyms_(0), syms_(NULL), symbols_(), comdat_map_()
384 // Return TRUE if a defined symbol might be reachable from outside the
385 // universe of claimed objects.
388 is_visible_from_outside(Symbol* lsym)
390 if (lsym->in_real_elf())
392 if (parameters->options().relocatable())
394 if (parameters->options().export_dynamic() || parameters->options().shared())
395 return lsym->is_externally_visible();
399 // Get symbol resolution info.
402 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
404 if (nsyms > this->nsyms_)
406 for (int i = 0; i < nsyms; i++)
408 ld_plugin_symbol* isym = &syms[i];
409 Symbol* lsym = this->symbols_[i];
410 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
412 if (lsym->is_undefined())
413 // The symbol remains undefined.
415 else if (isym->def == LDPK_UNDEF
416 || isym->def == LDPK_WEAKUNDEF
417 || isym->def == LDPK_COMMON)
419 // The original symbol was undefined or common.
420 if (lsym->source() != Symbol::FROM_OBJECT)
421 res = LDPR_RESOLVED_EXEC;
422 else if (lsym->object()->pluginobj() != NULL)
423 res = LDPR_RESOLVED_IR;
424 else if (lsym->object()->is_dynamic())
425 res = LDPR_RESOLVED_DYN;
427 res = LDPR_RESOLVED_EXEC;
431 // The original symbol was a definition.
432 if (lsym->source() != Symbol::FROM_OBJECT)
433 res = LDPR_PREEMPTED_REG;
434 else if (lsym->object() == static_cast<const Object*>(this))
435 res = (is_visible_from_outside(lsym)
436 ? LDPR_PREVAILING_DEF
437 : LDPR_PREVAILING_DEF_IRONLY);
439 res = (lsym->object()->pluginobj() != NULL
441 : LDPR_PREEMPTED_REG);
443 isym->resolution = res;
448 // Return TRUE if the comdat group with key COMDAT_KEY from this object
452 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
454 std::pair<Comdat_map::iterator, bool> ins =
455 this->comdat_map_.insert(std::make_pair(comdat_key, false));
457 // If this is the first time we've seen this comdat key, ask the
458 // layout object whether it should be included.
460 ins.first->second = layout->add_comdat(NULL, 1, comdat_key, true);
462 return ins.first->second;
465 // Class Sized_pluginobj.
467 template<int size, bool big_endian>
468 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
469 const std::string& name,
470 Input_file* input_file,
472 : Pluginobj(name, input_file, offset)
476 // Read the symbols. Not used for plugin objects.
478 template<int size, bool big_endian>
480 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
485 // Lay out the input sections. Not used for plugin objects.
487 template<int size, bool big_endian>
489 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
495 // Add the symbols to the symbol table.
497 template<int size, bool big_endian>
499 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table*,
505 template<int size, bool big_endian>
507 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
510 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
511 unsigned char symbuf[sym_size];
512 elfcpp::Sym<size, big_endian> sym(symbuf);
513 elfcpp::Sym_write<size, big_endian> osym(symbuf);
515 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
517 this->symbols_.resize(this->nsyms_);
519 for (int i = 0; i < this->nsyms_; ++i)
521 const struct ld_plugin_symbol *isym = &this->syms_[i];
522 const char* name = isym->name;
523 const char* ver = isym->version;
524 elfcpp::Elf_Half shndx;
528 if (name != NULL && name[0] == '\0')
530 if (ver != NULL && ver[0] == '\0')
537 bind = elfcpp::STB_WEAK;
543 bind = elfcpp::STB_GLOBAL;
551 shndx = elfcpp::SHN_ABS;
554 shndx = elfcpp::SHN_COMMON;
559 shndx = elfcpp::SHN_UNDEF;
563 switch (isym->visibility)
566 vis = elfcpp::STV_DEFAULT;
569 vis = elfcpp::STV_DEFAULT;
572 vis = elfcpp::STV_DEFAULT;
576 vis = elfcpp::STV_DEFAULT;
580 if (isym->comdat_key != NULL
581 && isym->comdat_key[0] != '\0'
582 && !this->include_comdat_group(isym->comdat_key, layout))
583 shndx = elfcpp::SHN_UNDEF;
586 osym.put_st_value(0);
587 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
588 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
589 osym.put_st_other(vis, 0);
590 osym.put_st_shndx(shndx);
593 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
597 // Get the size of a section. Not used for plugin objects.
599 template<int size, bool big_endian>
601 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
607 // Get the name of a section. Not used for plugin objects.
609 template<int size, bool big_endian>
611 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
614 return std::string();
617 // Return a view of the contents of a section. Not used for plugin objects.
619 template<int size, bool big_endian>
621 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
629 // Return section flags. Not used for plugin objects.
631 template<int size, bool big_endian>
633 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
639 // Return section address. Not used for plugin objects.
641 template<int size, bool big_endian>
643 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
649 // Return section type. Not used for plugin objects.
651 template<int size, bool big_endian>
653 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
659 // Return the section link field. Not used for plugin objects.
661 template<int size, bool big_endian>
663 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
669 // Return the section link field. Not used for plugin objects.
671 template<int size, bool big_endian>
673 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
679 // Return the section alignment. Not used for plugin objects.
681 template<int size, bool big_endian>
683 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
689 // Return the Xindex structure to use. Not used for plugin objects.
691 template<int size, bool big_endian>
693 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
699 // Get symbol counts. Not used for plugin objects.
701 template<int size, bool big_endian>
703 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
704 size_t*, size_t*) const
709 // Class Add_plugin_symbols.
711 Add_plugin_symbols::~Add_plugin_symbols()
713 if (this->this_blocker_ != NULL)
714 delete this->this_blocker_;
715 // next_blocker_ is deleted by the task associated with the next
719 // We are blocked by this_blocker_. We block next_blocker_. We also
723 Add_plugin_symbols::is_runnable()
725 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
726 return this->this_blocker_;
727 if (this->obj_->is_locked())
728 return this->obj_->token();
733 Add_plugin_symbols::locks(Task_locker* tl)
735 tl->add(this, this->next_blocker_);
736 tl->add(this, this->obj_->token());
739 // Add the symbols in the object to the symbol table.
742 Add_plugin_symbols::run(Workqueue*)
744 this->obj_->add_symbols(this->symtab_, this->layout_);
747 // Class Plugin_finish. This task runs after all replacement files have
748 // been added. It calls Layout::layout for any deferred sections and
749 // calls each plugin's cleanup handler.
751 class Plugin_finish : public Task
754 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
755 : this_blocker_(this_blocker), next_blocker_(next_blocker)
760 if (this->this_blocker_ != NULL)
761 delete this->this_blocker_;
767 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
768 return this->this_blocker_;
773 locks(Task_locker* tl)
774 { tl->add(this, this->next_blocker_); }
779 Plugin_manager* plugins = parameters->options().plugins();
780 gold_assert(plugins != NULL);
781 plugins->layout_deferred_objects();
787 { return "Plugin_finish"; }
790 Task_token* this_blocker_;
791 Task_token* next_blocker_;
794 // Class Plugin_hook.
796 Plugin_hook::~Plugin_hook()
800 // Return whether a Plugin_hook task is runnable.
803 Plugin_hook::is_runnable()
805 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
806 return this->this_blocker_;
810 // Return a Task_locker for a Plugin_hook task. We don't need any
814 Plugin_hook::locks(Task_locker*)
818 // Run the "all symbols read" plugin hook.
821 Plugin_hook::run(Workqueue* workqueue)
823 gold_assert(this->options_.has_plugins());
824 this->options_.plugins()->all_symbols_read(workqueue,
825 this->input_objects_,
830 &this->this_blocker_);
831 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
832 this->next_blocker_));
835 // The C interface routines called by the plugins.
837 #ifdef ENABLE_PLUGINS
839 // Register a claim-file handler.
841 static enum ld_plugin_status
842 register_claim_file(ld_plugin_claim_file_handler handler)
844 gold_assert(parameters->options().has_plugins());
845 parameters->options().plugins()->set_claim_file_handler(handler);
849 // Register an all-symbols-read handler.
851 static enum ld_plugin_status
852 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
854 gold_assert(parameters->options().has_plugins());
855 parameters->options().plugins()->set_all_symbols_read_handler(handler);
859 // Register a cleanup handler.
861 static enum ld_plugin_status
862 register_cleanup(ld_plugin_cleanup_handler handler)
864 gold_assert(parameters->options().has_plugins());
865 parameters->options().plugins()->set_cleanup_handler(handler);
869 // Add symbols from a plugin-claimed input file.
871 static enum ld_plugin_status
872 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
874 gold_assert(parameters->options().has_plugins());
875 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
876 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
879 obj->store_incoming_symbols(nsyms, syms);
883 // Get the symbol resolution info for a plugin-claimed input file.
885 static enum ld_plugin_status
886 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
888 gold_assert(parameters->options().has_plugins());
889 Pluginobj* obj = parameters->options().plugins()->object(
890 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
893 return obj->get_symbol_resolution_info(nsyms, syms);
896 // Add a new (real) input file generated by a plugin.
898 static enum ld_plugin_status
899 add_input_file(char *pathname)
901 gold_assert(parameters->options().has_plugins());
902 return parameters->options().plugins()->add_input_file(pathname);
905 // Issue a diagnostic message from a plugin.
907 static enum ld_plugin_status
908 message(int level, const char * format, ...)
911 va_start(args, format);
916 parameters->errors()->info(format, args);
919 parameters->errors()->warning(format, args);
923 parameters->errors()->error(format, args);
926 parameters->errors()->fatal(format, args);
934 #endif // ENABLE_PLUGINS
936 // Allocate a Pluginobj object of the appropriate size and endianness.
939 make_sized_plugin_object(Input_file* input_file, off_t offset)
942 Pluginobj* obj = NULL;
944 if (parameters->target_valid())
945 target = const_cast<Target*>(¶meters->target());
947 target = const_cast<Target*>(¶meters->default_target());
949 if (target->get_size() == 32)
951 if (target->is_big_endian())
952 #ifdef HAVE_TARGET_32_BIG
953 obj = new Sized_pluginobj<32, true>(input_file->filename(),
956 gold_error(_("%s: not configured to support "
957 "32-bit big-endian object"),
958 input_file->filename().c_str());
961 #ifdef HAVE_TARGET_32_LITTLE
962 obj = new Sized_pluginobj<32, false>(input_file->filename(),
965 gold_error(_("%s: not configured to support "
966 "32-bit little-endian object"),
967 input_file->filename().c_str());
970 else if (target->get_size() == 64)
972 if (target->is_big_endian())
973 #ifdef HAVE_TARGET_64_BIG
974 obj = new Sized_pluginobj<64, true>(input_file->filename(),
977 gold_error(_("%s: not configured to support "
978 "64-bit big-endian object"),
979 input_file->filename().c_str());
982 #ifdef HAVE_TARGET_64_LITTLE
983 obj = new Sized_pluginobj<64, false>(input_file->filename(),
986 gold_error(_("%s: not configured to support "
987 "64-bit little-endian object"),
988 input_file->filename().c_str());
992 gold_assert(obj != NULL);
993 obj->set_target(target);
997 } // End namespace gold.