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];
120 tv[i].tv_tag = LDPT_API_VERSION;
121 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
124 tv[i].tv_tag = LDPT_GOLD_VERSION;
125 tv[i].tv_u.tv_val = major * 100 + minor;
128 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
129 if (parameters->options().relocatable())
130 tv[i].tv_u.tv_val = LDPO_REL;
131 else if (parameters->options().shared())
132 tv[i].tv_u.tv_val = LDPO_DYN;
134 tv[i].tv_u.tv_val = LDPO_EXEC;
136 for (unsigned int j = 0; j < this->args_.size(); ++j)
139 tv[i].tv_tag = LDPT_OPTION;
140 tv[i].tv_u.tv_string = this->args_[j].c_str();
144 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
145 tv[i].tv_u.tv_register_claim_file = register_claim_file;
148 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
149 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
152 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
153 tv[i].tv_u.tv_register_cleanup = register_cleanup;
156 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
157 tv[i].tv_u.tv_add_symbols = add_symbols;
160 tv[i].tv_tag = LDPT_GET_SYMBOLS;
161 tv[i].tv_u.tv_get_symbols = get_symbols;
164 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
165 tv[i].tv_u.tv_add_input_file = add_input_file;
168 tv[i].tv_tag = LDPT_MESSAGE;
169 tv[i].tv_u.tv_message = message;
172 tv[i].tv_tag = LDPT_NULL;
173 tv[i].tv_u.tv_val = 0;
175 gold_assert(i == tv_size - 1);
177 // Call the onload entry point.
181 #endif // ENABLE_PLUGINS
184 // Call the plugin claim-file handler.
187 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
191 if (this->claim_file_handler_ != NULL)
193 (*this->claim_file_handler_)(plugin_input_file, &claimed);
200 // Call the all-symbols-read handler.
203 Plugin::all_symbols_read()
205 if (this->all_symbols_read_handler_ != NULL)
206 (*this->all_symbols_read_handler_)();
209 // Call the cleanup handler.
214 if (this->cleanup_handler_ != NULL)
215 (*this->cleanup_handler_)();
218 // Plugin_manager methods.
220 Plugin_manager::~Plugin_manager()
222 for (Plugin_list::iterator p = this->plugins_.begin();
223 p != this->plugins_.end();
226 this->plugins_.clear();
227 for (Object_list::iterator obj = this->objects_.begin();
228 obj != this->objects_.end();
231 this->objects_.clear();
234 // Load all plugin libraries.
237 Plugin_manager::load_plugins()
239 for (this->current_ = this->plugins_.begin();
240 this->current_ != this->plugins_.end();
242 (*this->current_)->load();
245 // Call the plugin claim-file handlers in turn to see if any claim the file.
248 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
251 if (this->in_replacement_phase_)
254 unsigned int handle = this->objects_.size();
255 this->input_file_ = input_file;
256 this->plugin_input_file_.name = input_file->filename().c_str();
257 this->plugin_input_file_.fd = input_file->file().descriptor();
258 this->plugin_input_file_.offset = offset;
259 this->plugin_input_file_.filesize = filesize;
260 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
262 for (this->current_ = this->plugins_.begin();
263 this->current_ != this->plugins_.end();
266 if ((*this->current_)->claim_file(&this->plugin_input_file_))
268 if (this->objects_.size() <= handle)
270 gold_error(_("%s: plugin claimed the file "
271 "but did not provide any symbols"),
272 this->plugin_input_file_.name);
275 return this->objects_[handle];
282 // Call the all-symbols-read handlers.
285 Plugin_manager::all_symbols_read(Workqueue* workqueue,
286 Input_objects* input_objects,
287 Symbol_table* symtab, Layout* layout,
288 Dirsearch* dirpath, Mapfile* mapfile,
289 Task_token** last_blocker)
291 this->in_replacement_phase_ = true;
292 this->workqueue_ = workqueue;
293 this->input_objects_ = input_objects;
294 this->symtab_ = symtab;
295 this->layout_ = layout;
296 this->dirpath_ = dirpath;
297 this->mapfile_ = mapfile;
298 this->this_blocker_ = NULL;
300 for (this->current_ = this->plugins_.begin();
301 this->current_ != this->plugins_.end();
303 (*this->current_)->all_symbols_read();
305 *last_blocker = this->this_blocker_;
308 // Call the cleanup handlers.
311 Plugin_manager::cleanup()
313 for (this->current_ = this->plugins_.begin();
314 this->current_ != this->plugins_.end();
316 (*this->current_)->cleanup();
319 // Make a new Pluginobj object. This is called when the plugin calls
320 // the add_symbols API.
323 Plugin_manager::make_plugin_object(unsigned int handle)
325 // Make sure we aren't asked to make an object for the same handle twice.
326 if (this->objects_.size() != handle)
329 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
330 this->plugin_input_file_.offset);
331 this->objects_.push_back(obj);
335 // Add a new input file.
338 Plugin_manager::add_input_file(char *pathname)
340 Input_file_argument file(pathname, false, "", false, this->options_);
341 Input_argument* input_argument = new Input_argument(file);
342 Task_token* next_blocker = new Task_token(true);
343 next_blocker->add_blocker();
344 this->workqueue_->queue_soon(new Read_symbols(this->options_,
345 this->input_objects_,
354 this->this_blocker_ = next_blocker;
360 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
362 : Object(name, input_file, false, offset),
363 nsyms_(0), syms_(NULL), symbols_(), comdat_map_()
367 // Get symbol resolution info.
370 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
372 if (this->nsyms_ == 0)
374 for (int i = 0; i < nsyms; i++)
376 ld_plugin_symbol* isym = &syms[i];
377 Symbol* lsym = this->symbols_[i];
378 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
380 if (lsym->is_undefined())
381 // The symbol remains undefined.
383 else if (isym->def == LDPK_UNDEF
384 || isym->def == LDPK_WEAKUNDEF
385 || isym->def == LDPK_COMMON)
387 // The original symbol was undefined or common.
388 if (lsym->source() != Symbol::FROM_OBJECT)
389 res = LDPR_RESOLVED_EXEC;
390 else if (lsym->object()->pluginobj() != NULL)
391 res = LDPR_RESOLVED_IR;
392 else if (lsym->object()->is_dynamic())
393 res = LDPR_RESOLVED_DYN;
395 res = LDPR_RESOLVED_EXEC;
399 // The original symbol was a definition.
400 if (lsym->source() != Symbol::FROM_OBJECT)
401 res = LDPR_PREEMPTED_REG;
402 else if (lsym->object() == static_cast<const Object*>(this))
403 res = (lsym->in_real_elf()
404 ? LDPR_PREVAILING_DEF
405 : LDPR_PREVAILING_DEF_IRONLY);
407 res = (lsym->object()->pluginobj() != NULL
409 : LDPR_PREEMPTED_REG);
411 isym->resolution = res;
416 // Return TRUE if the comdat group with key COMDAT_KEY from this object
420 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
422 std::pair<Comdat_map::iterator, bool> ins =
423 this->comdat_map_.insert(std::make_pair(comdat_key, false));
425 // If this is the first time we've seen this comdat key, ask the
426 // layout object whether it should be included.
428 ins.first->second = layout->add_comdat(NULL, 1, comdat_key, true);
430 return ins.first->second;
433 // Class Sized_pluginobj.
435 template<int size, bool big_endian>
436 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
437 const std::string& name,
438 Input_file* input_file,
440 : Pluginobj(name, input_file, offset)
444 // Read the symbols. Not used for plugin objects.
446 template<int size, bool big_endian>
448 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
453 // Lay out the input sections. Not used for plugin objects.
455 template<int size, bool big_endian>
457 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
463 // Add the symbols to the symbol table.
465 template<int size, bool big_endian>
467 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table*,
473 template<int size, bool big_endian>
475 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
478 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
479 unsigned char symbuf[sym_size];
480 elfcpp::Sym<size, big_endian> sym(symbuf);
481 elfcpp::Sym_write<size, big_endian> osym(symbuf);
483 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
485 this->symbols_.resize(this->nsyms_);
487 for (int i = 0; i < this->nsyms_; ++i)
489 const struct ld_plugin_symbol *isym = &this->syms_[i];
490 const char* name = isym->name;
491 const char* ver = isym->version;
492 elfcpp::Elf_Half shndx;
496 if (name != NULL && name[0] == '\0')
498 if (ver != NULL && ver[0] == '\0')
505 bind = elfcpp::STB_WEAK;
511 bind = elfcpp::STB_GLOBAL;
519 shndx = elfcpp::SHN_ABS;
522 shndx = elfcpp::SHN_COMMON;
527 shndx = elfcpp::SHN_UNDEF;
531 switch (isym->visibility)
534 vis = elfcpp::STV_DEFAULT;
537 vis = elfcpp::STV_DEFAULT;
540 vis = elfcpp::STV_DEFAULT;
544 vis = elfcpp::STV_DEFAULT;
548 if (isym->comdat_key != NULL
549 && isym->comdat_key[0] != '\0'
550 && !this->include_comdat_group(isym->comdat_key, layout))
551 shndx = elfcpp::SHN_UNDEF;
554 osym.put_st_value(0);
555 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
556 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
557 osym.put_st_other(vis, 0);
558 osym.put_st_shndx(shndx);
561 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
565 // Get the size of a section. Not used for plugin objects.
567 template<int size, bool big_endian>
569 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
575 // Get the name of a section. Not used for plugin objects.
577 template<int size, bool big_endian>
579 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
582 return std::string();
585 // Return a view of the contents of a section. Not used for plugin objects.
587 template<int size, bool big_endian>
589 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
597 // Return section flags. Not used for plugin objects.
599 template<int size, bool big_endian>
601 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
607 // Return section address. Not used for plugin objects.
609 template<int size, bool big_endian>
611 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
617 // Return section type. Not used for plugin objects.
619 template<int size, bool big_endian>
621 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
627 // Return the section link field. Not used for plugin objects.
629 template<int size, bool big_endian>
631 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
637 // Return the section link field. Not used for plugin objects.
639 template<int size, bool big_endian>
641 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
647 // Return the section alignment. Not used for plugin objects.
649 template<int size, bool big_endian>
651 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
657 // Return the Xindex structure to use. Not used for plugin objects.
659 template<int size, bool big_endian>
661 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
667 // Get symbol counts. Not used for plugin objects.
669 template<int size, bool big_endian>
671 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
672 size_t*, size_t*) const
677 // Class Add_plugin_symbols.
679 Add_plugin_symbols::~Add_plugin_symbols()
681 if (this->this_blocker_ != NULL)
682 delete this->this_blocker_;
683 // next_blocker_ is deleted by the task associated with the next
687 // We are blocked by this_blocker_. We block next_blocker_. We also
691 Add_plugin_symbols::is_runnable()
693 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
694 return this->this_blocker_;
695 if (this->obj_->is_locked())
696 return this->obj_->token();
701 Add_plugin_symbols::locks(Task_locker* tl)
703 tl->add(this, this->next_blocker_);
704 tl->add(this, this->obj_->token());
707 // Add the symbols in the object to the symbol table.
710 Add_plugin_symbols::run(Workqueue*)
712 this->obj_->add_symbols(this->symtab_, this->layout_);
715 // Class Plugin_cleanup. This task calls the plugin cleanup hooks once all
716 // replacement files have been added.
718 class Plugin_cleanup : public Task
721 Plugin_cleanup(Task_token* this_blocker, Task_token* next_blocker)
722 : this_blocker_(this_blocker), next_blocker_(next_blocker)
727 if (this->this_blocker_ != NULL)
728 delete this->this_blocker_;
734 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
735 return this->this_blocker_;
740 locks(Task_locker* tl)
741 { tl->add(this, this->next_blocker_); }
745 { parameters->options().plugins()->cleanup(); }
749 { return "Plugin_cleanup"; }
752 Task_token* this_blocker_;
753 Task_token* next_blocker_;
756 // Class Plugin_hook.
758 Plugin_hook::~Plugin_hook()
762 // Return whether a Plugin_hook task is runnable.
765 Plugin_hook::is_runnable()
767 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
768 return this->this_blocker_;
772 // Return a Task_locker for a Plugin_hook task. We don't need any
776 Plugin_hook::locks(Task_locker*)
780 // Run a Plugin_hook task.
783 Plugin_hook::run(Workqueue* workqueue)
785 this->do_plugin_hook(workqueue);
788 // Run the "all symbols read" plugin hook.
791 Plugin_hook::do_plugin_hook(Workqueue* workqueue)
793 gold_assert(this->options_.has_plugins());
794 this->options_.plugins()->all_symbols_read(workqueue,
795 this->input_objects_,
800 &this->this_blocker_);
801 workqueue->queue_soon(new Plugin_cleanup(this->this_blocker_,
802 this->next_blocker_));
805 // The C interface routines called by the plugins.
807 #ifdef ENABLE_PLUGINS
809 // Register a claim-file handler.
811 static enum ld_plugin_status
812 register_claim_file(ld_plugin_claim_file_handler handler)
814 gold_assert(parameters->options().has_plugins());
815 parameters->options().plugins()->set_claim_file_handler(handler);
819 // Register an all-symbols-read handler.
821 static enum ld_plugin_status
822 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
824 gold_assert(parameters->options().has_plugins());
825 parameters->options().plugins()->set_all_symbols_read_handler(handler);
829 // Register a cleanup handler.
831 static enum ld_plugin_status
832 register_cleanup(ld_plugin_cleanup_handler handler)
834 gold_assert(parameters->options().has_plugins());
835 parameters->options().plugins()->set_cleanup_handler(handler);
839 // Add symbols from a plugin-claimed input file.
841 static enum ld_plugin_status
842 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
844 gold_assert(parameters->options().has_plugins());
845 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
846 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
849 obj->store_incoming_symbols(nsyms, syms);
853 // Get the symbol resolution info for a plugin-claimed input file.
855 static enum ld_plugin_status
856 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
858 gold_assert(parameters->options().has_plugins());
859 Pluginobj* obj = parameters->options().plugins()->object(
860 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
863 return obj->get_symbol_resolution_info(nsyms, syms);
866 // Add a new (real) input file generated by a plugin.
868 static enum ld_plugin_status
869 add_input_file(char *pathname)
871 gold_assert(parameters->options().has_plugins());
872 return parameters->options().plugins()->add_input_file(pathname);
875 // Issue a diagnostic message from a plugin.
877 static enum ld_plugin_status
878 message(int level, const char * format, ...)
881 va_start(args, format);
886 parameters->errors()->info(format, args);
889 parameters->errors()->warning(format, args);
893 parameters->errors()->error(format, args);
896 parameters->errors()->fatal(format, args);
904 #endif // ENABLE_PLUGINS
906 // Allocate a Pluginobj object of the appropriate size and endianness.
909 make_sized_plugin_object(Input_file* input_file, off_t offset)
912 Pluginobj* obj = NULL;
914 if (parameters->target_valid())
915 target = const_cast<Target*>(¶meters->target());
917 target = const_cast<Target*>(¶meters->default_target());
919 if (target->get_size() == 32)
921 if (target->is_big_endian())
922 #ifdef HAVE_TARGET_32_BIG
923 obj = new Sized_pluginobj<32, true>(input_file->filename(),
926 gold_error(_("%s: not configured to support "
927 "32-bit big-endian object"),
928 input_file->filename().c_str());
931 #ifdef HAVE_TARGET_32_LITTLE
932 obj = new Sized_pluginobj<32, false>(input_file->filename(),
935 gold_error(_("%s: not configured to support "
936 "32-bit little-endian object"),
937 input_file->filename().c_str());
940 else if (target->get_size() == 64)
942 if (target->is_big_endian())
943 #ifdef HAVE_TARGET_64_BIG
944 obj = new Sized_pluginobj<64, true>(input_file->filename(),
947 gold_error(_("%s: not configured to support "
948 "64-bit big-endian object"),
949 input_file->filename().c_str());
952 #ifdef HAVE_TARGET_64_LITTLE
953 obj = new Sized_pluginobj<64, false>(input_file->filename(),
956 gold_error(_("%s: not configured to support "
957 "64-bit little-endian object"),
958 input_file->filename().c_str());
962 gold_assert(obj != NULL);
963 obj->set_target(target);
967 } // End namespace gold.