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.
89 std::vector<std::string> args;
91 // Parse the filename and arguments, each separated by commas.
92 // FIXME: Temporarily allowing semicolon as an argument separator
93 // so args can be passed through gcc's -Wl,... option, which
94 // breaks arguments at the commas.
95 const char* p = this->args_;
96 int n = strcspn(p, ",;");
97 filename.assign(p, n);
99 while (*p == ',' || *p == ';')
102 n = strcspn(p, ",;");
103 args.push_back(std::string(p, n));
107 // Load the plugin library.
108 // FIXME: Look for the library in standard locations.
109 this->handle_ = dlopen(filename.c_str(), RTLD_NOW);
110 if (this->handle_ == NULL)
112 gold_error(_("%s: could not load plugin library"), filename.c_str());
116 // Find the plugin's onload entry point.
117 ld_plugin_onload onload = reinterpret_cast<ld_plugin_onload>
118 (dlsym(this->handle_, "onload"));
121 gold_error(_("%s: could not find onload entry point"), filename.c_str());
125 // Get the linker's version number.
126 const char* ver = get_version_string();
129 sscanf(ver, "%d.%d", &major, &minor);
131 // Allocate and populate a transfer vector.
132 const int tv_fixed_size = 11;
133 int tv_size = args.size() + tv_fixed_size;
134 ld_plugin_tv *tv = new ld_plugin_tv[tv_size];
137 tv[i].tv_tag = LDPT_API_VERSION;
138 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
141 tv[i].tv_tag = LDPT_GOLD_VERSION;
142 tv[i].tv_u.tv_val = major * 100 + minor;
145 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
146 if (parameters->options().relocatable())
147 tv[i].tv_u.tv_val = LDPO_REL;
148 else if (parameters->options().shared())
149 tv[i].tv_u.tv_val = LDPO_DYN;
151 tv[i].tv_u.tv_val = LDPO_EXEC;
153 for (unsigned int j = 0; j < args.size(); ++j)
156 tv[i].tv_tag = LDPT_OPTION;
157 tv[i].tv_u.tv_string = args[j].c_str();
161 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
162 tv[i].tv_u.tv_register_claim_file = register_claim_file;
165 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
166 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
169 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
170 tv[i].tv_u.tv_register_cleanup = register_cleanup;
173 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
174 tv[i].tv_u.tv_add_symbols = add_symbols;
177 tv[i].tv_tag = LDPT_GET_SYMBOLS;
178 tv[i].tv_u.tv_get_symbols = get_symbols;
181 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
182 tv[i].tv_u.tv_add_input_file = add_input_file;
185 tv[i].tv_tag = LDPT_MESSAGE;
186 tv[i].tv_u.tv_message = message;
189 tv[i].tv_tag = LDPT_NULL;
190 tv[i].tv_u.tv_val = 0;
192 gold_assert(i == tv_size - 1);
194 // Call the onload entry point.
198 #endif // ENABLE_PLUGINS
201 // Call the plugin claim-file handler.
204 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
208 if (this->claim_file_handler_ != NULL)
210 (*this->claim_file_handler_)(plugin_input_file, &claimed);
217 // Call the all-symbols-read handler.
220 Plugin::all_symbols_read()
222 if (this->all_symbols_read_handler_ != NULL)
223 (*this->all_symbols_read_handler_)();
226 // Call the cleanup handler.
231 if (this->cleanup_handler_ != NULL)
232 (*this->cleanup_handler_)();
235 // Plugin_manager methods.
237 Plugin_manager::~Plugin_manager()
239 for (Plugin_list::iterator p = this->plugins_.begin();
240 p != this->plugins_.end();
243 this->plugins_.clear();
244 for (Object_list::iterator obj = this->objects_.begin();
245 obj != this->objects_.end();
248 this->objects_.clear();
251 // Load all plugin libraries.
254 Plugin_manager::load_plugins()
256 for (this->current_ = this->plugins_.begin();
257 this->current_ != this->plugins_.end();
259 (*this->current_)->load();
262 // Call the plugin claim-file handlers in turn to see if any claim the file.
265 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
268 if (this->in_replacement_phase_)
271 unsigned int handle = this->objects_.size();
272 this->input_file_ = input_file;
273 this->plugin_input_file_.name = input_file->filename().c_str();
274 this->plugin_input_file_.fd = input_file->file().descriptor();
275 this->plugin_input_file_.offset = offset;
276 this->plugin_input_file_.filesize = filesize;
277 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
279 for (this->current_ = this->plugins_.begin();
280 this->current_ != this->plugins_.end();
283 if ((*this->current_)->claim_file(&this->plugin_input_file_))
285 if (this->objects_.size() <= handle)
287 gold_error(_("%s: plugin claimed the file "
288 "but did not provide any symbols"),
289 this->plugin_input_file_.name);
292 return this->objects_[handle];
299 // Call the all-symbols-read handlers.
302 Plugin_manager::all_symbols_read(Workqueue* workqueue,
303 Input_objects* input_objects,
304 Symbol_table* symtab, Layout* layout,
305 Dirsearch* dirpath, Mapfile* mapfile,
306 Task_token** last_blocker)
308 this->in_replacement_phase_ = true;
309 this->workqueue_ = workqueue;
310 this->input_objects_ = input_objects;
311 this->symtab_ = symtab;
312 this->layout_ = layout;
313 this->dirpath_ = dirpath;
314 this->mapfile_ = mapfile;
315 this->this_blocker_ = NULL;
317 for (this->current_ = this->plugins_.begin();
318 this->current_ != this->plugins_.end();
320 (*this->current_)->all_symbols_read();
322 *last_blocker = this->this_blocker_;
325 // Call the cleanup handlers.
328 Plugin_manager::cleanup()
330 for (this->current_ = this->plugins_.begin();
331 this->current_ != this->plugins_.end();
333 (*this->current_)->cleanup();
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 // Get symbol resolution info.
387 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
389 if (this->nsyms_ == 0)
391 for (int i = 0; i < nsyms; i++)
393 ld_plugin_symbol* isym = &syms[i];
394 Symbol* lsym = this->symbols_[i];
395 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
397 if (lsym->is_undefined())
398 // The symbol remains undefined.
400 else if (isym->def == LDPK_UNDEF
401 || isym->def == LDPK_WEAKUNDEF
402 || isym->def == LDPK_COMMON)
404 // The original symbol was undefined or common.
405 if (lsym->source() != Symbol::FROM_OBJECT)
406 res = LDPR_RESOLVED_EXEC;
407 else if (lsym->object()->pluginobj() != NULL)
408 res = LDPR_RESOLVED_IR;
409 else if (lsym->object()->is_dynamic())
410 res = LDPR_RESOLVED_DYN;
412 res = LDPR_RESOLVED_EXEC;
416 // The original symbol was a definition.
417 if (lsym->source() != Symbol::FROM_OBJECT)
418 res = LDPR_PREEMPTED_REG;
419 else if (lsym->object() == static_cast<const Object*>(this))
420 res = (lsym->in_real_elf()
421 ? LDPR_PREVAILING_DEF
422 : LDPR_PREVAILING_DEF_IRONLY);
424 res = (lsym->object()->pluginobj() != NULL
426 : LDPR_PREEMPTED_REG);
428 isym->resolution = res;
433 // Return TRUE if the comdat group with key COMDAT_KEY from this object
437 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
439 std::pair<Comdat_map::iterator, bool> ins =
440 this->comdat_map_.insert(std::make_pair(comdat_key, false));
442 // If this is the first time we've seen this comdat key, ask the
443 // layout object whether it should be included.
445 ins.first->second = layout->add_comdat(NULL, 1, comdat_key, true);
447 return ins.first->second;
450 // Class Sized_pluginobj.
452 template<int size, bool big_endian>
453 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
454 const std::string& name,
455 Input_file* input_file,
457 : Pluginobj(name, input_file, offset)
461 // Read the symbols. Not used for plugin objects.
463 template<int size, bool big_endian>
465 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
470 // Lay out the input sections. Not used for plugin objects.
472 template<int size, bool big_endian>
474 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
480 // Add the symbols to the symbol table.
482 template<int size, bool big_endian>
484 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table*,
490 template<int size, bool big_endian>
492 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
495 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
496 unsigned char symbuf[sym_size];
497 elfcpp::Sym<size, big_endian> sym(symbuf);
498 elfcpp::Sym_write<size, big_endian> osym(symbuf);
500 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
502 this->symbols_.resize(this->nsyms_);
504 for (int i = 0; i < this->nsyms_; ++i)
506 const struct ld_plugin_symbol *isym = &this->syms_[i];
507 const char* name = isym->name;
508 const char* ver = isym->version;
509 elfcpp::Elf_Half shndx;
513 if (name != NULL && name[0] == '\0')
515 if (ver != NULL && ver[0] == '\0')
522 bind = elfcpp::STB_WEAK;
528 bind = elfcpp::STB_GLOBAL;
536 shndx = elfcpp::SHN_ABS;
539 shndx = elfcpp::SHN_COMMON;
544 shndx = elfcpp::SHN_UNDEF;
548 switch (isym->visibility)
551 vis = elfcpp::STV_DEFAULT;
554 vis = elfcpp::STV_DEFAULT;
557 vis = elfcpp::STV_DEFAULT;
561 vis = elfcpp::STV_DEFAULT;
565 if (isym->comdat_key != NULL
566 && isym->comdat_key[0] != '\0'
567 && !this->include_comdat_group(isym->comdat_key, layout))
568 shndx = elfcpp::SHN_UNDEF;
571 osym.put_st_value(0);
572 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
573 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
574 osym.put_st_other(vis, 0);
575 osym.put_st_shndx(shndx);
578 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
582 // Get the size of a section. Not used for plugin objects.
584 template<int size, bool big_endian>
586 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
592 // Get the name of a section. Not used for plugin objects.
594 template<int size, bool big_endian>
596 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
599 return std::string();
602 // Return a view of the contents of a section. Not used for plugin objects.
604 template<int size, bool big_endian>
606 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
614 // Return section flags. Not used for plugin objects.
616 template<int size, bool big_endian>
618 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
624 // Return section address. Not used for plugin objects.
626 template<int size, bool big_endian>
628 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
634 // Return section type. Not used for plugin objects.
636 template<int size, bool big_endian>
638 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
644 // Return the section link field. Not used for plugin objects.
646 template<int size, bool big_endian>
648 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
654 // Return the section link field. Not used for plugin objects.
656 template<int size, bool big_endian>
658 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
664 // Return the section alignment. Not used for plugin objects.
666 template<int size, bool big_endian>
668 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
674 // Return the Xindex structure to use. Not used for plugin objects.
676 template<int size, bool big_endian>
678 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
684 // Get symbol counts. Not used for plugin objects.
686 template<int size, bool big_endian>
688 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
689 size_t*, size_t*) const
694 // Class Add_plugin_symbols.
696 Add_plugin_symbols::~Add_plugin_symbols()
698 if (this->this_blocker_ != NULL)
699 delete this->this_blocker_;
700 // next_blocker_ is deleted by the task associated with the next
704 // We are blocked by this_blocker_. We block next_blocker_. We also
708 Add_plugin_symbols::is_runnable()
710 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
711 return this->this_blocker_;
712 if (this->obj_->is_locked())
713 return this->obj_->token();
718 Add_plugin_symbols::locks(Task_locker* tl)
720 tl->add(this, this->next_blocker_);
721 tl->add(this, this->obj_->token());
724 // Add the symbols in the object to the symbol table.
727 Add_plugin_symbols::run(Workqueue*)
729 this->obj_->add_symbols(this->symtab_, this->layout_);
732 // Class Plugin_cleanup. This task calls the plugin cleanup hooks once all
733 // replacement files have been added.
735 class Plugin_cleanup : public Task
738 Plugin_cleanup(Task_token* this_blocker, Task_token* next_blocker)
739 : this_blocker_(this_blocker), next_blocker_(next_blocker)
744 if (this->this_blocker_ != NULL)
745 delete this->this_blocker_;
751 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
752 return this->this_blocker_;
757 locks(Task_locker* tl)
758 { tl->add(this, this->next_blocker_); }
762 { parameters->options().plugins()->cleanup(); }
766 { return "Plugin_cleanup"; }
769 Task_token* this_blocker_;
770 Task_token* next_blocker_;
773 // Class Plugin_hook.
775 Plugin_hook::~Plugin_hook()
779 // Return whether a Plugin_hook task is runnable.
782 Plugin_hook::is_runnable()
784 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
785 return this->this_blocker_;
789 // Return a Task_locker for a Plugin_hook task. We don't need any
793 Plugin_hook::locks(Task_locker*)
797 // Run a Plugin_hook task.
800 Plugin_hook::run(Workqueue* workqueue)
802 this->do_plugin_hook(workqueue);
805 // Run the "all symbols read" plugin hook.
808 Plugin_hook::do_plugin_hook(Workqueue* workqueue)
810 gold_assert(this->options_.has_plugins());
811 this->options_.plugins()->all_symbols_read(workqueue,
812 this->input_objects_,
817 &this->this_blocker_);
818 workqueue->queue_soon(new Plugin_cleanup(this->this_blocker_,
819 this->next_blocker_));
822 // The C interface routines called by the plugins.
824 #ifdef ENABLE_PLUGINS
826 // Register a claim-file handler.
828 static enum ld_plugin_status
829 register_claim_file(ld_plugin_claim_file_handler handler)
831 gold_assert(parameters->options().has_plugins());
832 parameters->options().plugins()->set_claim_file_handler(handler);
836 // Register an all-symbols-read handler.
838 static enum ld_plugin_status
839 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
841 gold_assert(parameters->options().has_plugins());
842 parameters->options().plugins()->set_all_symbols_read_handler(handler);
846 // Register a cleanup handler.
848 static enum ld_plugin_status
849 register_cleanup(ld_plugin_cleanup_handler handler)
851 gold_assert(parameters->options().has_plugins());
852 parameters->options().plugins()->set_cleanup_handler(handler);
856 // Add symbols from a plugin-claimed input file.
858 static enum ld_plugin_status
859 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
861 gold_assert(parameters->options().has_plugins());
862 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
863 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
866 obj->store_incoming_symbols(nsyms, syms);
870 // Get the symbol resolution info for a plugin-claimed input file.
872 static enum ld_plugin_status
873 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
875 gold_assert(parameters->options().has_plugins());
876 Pluginobj* obj = parameters->options().plugins()->object(
877 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
880 return obj->get_symbol_resolution_info(nsyms, syms);
883 // Add a new (real) input file generated by a plugin.
885 static enum ld_plugin_status
886 add_input_file(char *pathname)
888 gold_assert(parameters->options().has_plugins());
889 return parameters->options().plugins()->add_input_file(pathname);
892 // Issue a diagnostic message from a plugin.
894 static enum ld_plugin_status
895 message(int level, const char * format, ...)
898 va_start(args, format);
903 parameters->errors()->info(format, args);
906 parameters->errors()->warning(format, args);
910 parameters->errors()->error(format, args);
913 parameters->errors()->fatal(format, args);
921 #endif // ENABLE_PLUGINS
923 // Allocate a Pluginobj object of the appropriate size and endianness.
926 make_sized_plugin_object(Input_file* input_file, off_t offset)
929 Pluginobj* obj = NULL;
931 if (parameters->target_valid())
932 target = const_cast<Target*>(¶meters->target());
934 target = const_cast<Target*>(¶meters->default_target());
936 if (target->get_size() == 32)
938 if (target->is_big_endian())
939 #ifdef HAVE_TARGET_32_BIG
940 obj = new Sized_pluginobj<32, true>(input_file->filename(),
943 gold_error(_("%s: not configured to support "
944 "32-bit big-endian object"),
945 input_file->filename().c_str());
948 #ifdef HAVE_TARGET_32_LITTLE
949 obj = new Sized_pluginobj<32, false>(input_file->filename(),
952 gold_error(_("%s: not configured to support "
953 "32-bit little-endian object"),
954 input_file->filename().c_str());
957 else if (target->get_size() == 64)
959 if (target->is_big_endian())
960 #ifdef HAVE_TARGET_64_BIG
961 obj = new Sized_pluginobj<64, true>(input_file->filename(),
964 gold_error(_("%s: not configured to support "
965 "64-bit big-endian object"),
966 input_file->filename().c_str());
969 #ifdef HAVE_TARGET_64_LITTLE
970 obj = new Sized_pluginobj<64, false>(input_file->filename(),
973 gold_error(_("%s: not configured to support "
974 "64-bit little-endian object"),
975 input_file->filename().c_str());
979 gold_assert(obj != NULL);
980 obj->set_target(target);
984 } // End namespace gold.