1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008, 2009, 2010, 2011 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.
35 #include "parameters.h"
51 // The linker's exported interfaces.
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 get_view(const void *handle, const void **viewp);
74 static enum ld_plugin_status
75 release_input_file(const void *handle);
77 static enum ld_plugin_status
78 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
80 static enum ld_plugin_status
81 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
83 static enum ld_plugin_status
84 add_input_file(const char *pathname);
86 static enum ld_plugin_status
87 add_input_library(const char *pathname);
89 static enum ld_plugin_status
90 set_extra_library_path(const char *path);
92 static enum ld_plugin_status
93 message(int level, const char *format, ...);
95 static enum ld_plugin_status
96 get_input_section_count(const void* handle, unsigned int* count);
98 static enum ld_plugin_status
99 get_input_section_type(const struct ld_plugin_section section,
102 static enum ld_plugin_status
103 get_input_section_name(const struct ld_plugin_section section,
104 char** section_name_ptr);
106 static enum ld_plugin_status
107 get_input_section_contents(const struct ld_plugin_section section,
108 const unsigned char** section_contents,
111 static enum ld_plugin_status
112 update_section_order(const struct ld_plugin_section *section_list,
113 unsigned int num_sections);
115 static enum ld_plugin_status
116 allow_section_ordering();
120 #endif // ENABLE_PLUGINS
122 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
123 off_t offset, off_t filesize);
127 // Load one plugin library.
132 #ifdef ENABLE_PLUGINS
133 // Load the plugin library.
134 // FIXME: Look for the library in standard locations.
135 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
136 if (this->handle_ == NULL)
138 gold_error(_("%s: could not load plugin library: %s"),
139 this->filename_.c_str(), dlerror());
143 // Find the plugin's onload entry point.
144 void* ptr = dlsym(this->handle_, "onload");
147 gold_error(_("%s: could not find onload entry point"),
148 this->filename_.c_str());
151 ld_plugin_onload onload;
152 gold_assert(sizeof(onload) == sizeof(ptr));
153 memcpy(&onload, &ptr, sizeof(ptr));
155 // Get the linker's version number.
156 const char* ver = get_version_string();
159 sscanf(ver, "%d.%d", &major, &minor);
161 // Allocate and populate a transfer vector.
162 const int tv_fixed_size = 24;
164 int tv_size = this->args_.size() + tv_fixed_size;
165 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
167 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
168 // while processing subsequent entries.
170 tv[i].tv_tag = LDPT_MESSAGE;
171 tv[i].tv_u.tv_message = message;
174 tv[i].tv_tag = LDPT_API_VERSION;
175 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
178 tv[i].tv_tag = LDPT_GOLD_VERSION;
179 tv[i].tv_u.tv_val = major * 100 + minor;
182 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
183 if (parameters->options().relocatable())
184 tv[i].tv_u.tv_val = LDPO_REL;
185 else if (parameters->options().shared())
186 tv[i].tv_u.tv_val = LDPO_DYN;
188 tv[i].tv_u.tv_val = LDPO_EXEC;
191 tv[i].tv_tag = LDPT_OUTPUT_NAME;
192 tv[i].tv_u.tv_string = parameters->options().output();
194 for (unsigned int j = 0; j < this->args_.size(); ++j)
197 tv[i].tv_tag = LDPT_OPTION;
198 tv[i].tv_u.tv_string = this->args_[j].c_str();
202 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
203 tv[i].tv_u.tv_register_claim_file = register_claim_file;
206 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
207 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
210 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
211 tv[i].tv_u.tv_register_cleanup = register_cleanup;
214 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
215 tv[i].tv_u.tv_add_symbols = add_symbols;
218 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
219 tv[i].tv_u.tv_get_input_file = get_input_file;
222 tv[i].tv_tag = LDPT_GET_VIEW;
223 tv[i].tv_u.tv_get_view = get_view;
226 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
227 tv[i].tv_u.tv_release_input_file = release_input_file;
230 tv[i].tv_tag = LDPT_GET_SYMBOLS;
231 tv[i].tv_u.tv_get_symbols = get_symbols;
234 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
235 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
238 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
239 tv[i].tv_u.tv_add_input_file = add_input_file;
242 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
243 tv[i].tv_u.tv_add_input_library = add_input_library;
246 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
247 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
250 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
251 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
254 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
255 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
258 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
259 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
262 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
263 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
266 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
267 tv[i].tv_u.tv_update_section_order = update_section_order;
270 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
271 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
274 tv[i].tv_tag = LDPT_NULL;
275 tv[i].tv_u.tv_val = 0;
277 gold_assert(i == tv_size - 1);
279 // Call the onload entry point.
283 #endif // ENABLE_PLUGINS
286 // Call the plugin claim-file handler.
289 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
293 if (this->claim_file_handler_ != NULL)
295 (*this->claim_file_handler_)(plugin_input_file, &claimed);
302 // Call the all-symbols-read handler.
305 Plugin::all_symbols_read()
307 if (this->all_symbols_read_handler_ != NULL)
308 (*this->all_symbols_read_handler_)();
311 // Call the cleanup handler.
316 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
318 // Set this flag before calling to prevent a recursive plunge
319 // in the event that a plugin's cleanup handler issues a
321 this->cleanup_done_ = true;
322 (*this->cleanup_handler_)();
326 // This task is used to rescan archives as needed.
328 class Plugin_rescan : public Task
331 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
332 : this_blocker_(this_blocker), next_blocker_(next_blocker)
337 delete this->this_blocker_;
343 if (this->this_blocker_->is_blocked())
344 return this->this_blocker_;
349 locks(Task_locker* tl)
350 { tl->add(this, this->next_blocker_); }
354 { parameters->options().plugins()->rescan(this); }
358 { return "Plugin_rescan"; }
361 Task_token* this_blocker_;
362 Task_token* next_blocker_;
365 // Plugin_manager methods.
367 Plugin_manager::~Plugin_manager()
369 for (Plugin_list::iterator p = this->plugins_.begin();
370 p != this->plugins_.end();
373 this->plugins_.clear();
374 for (Object_list::iterator obj = this->objects_.begin();
375 obj != this->objects_.end();
378 this->objects_.clear();
381 // Load all plugin libraries.
384 Plugin_manager::load_plugins(Layout* layout)
386 this->layout_ = layout;
387 for (this->current_ = this->plugins_.begin();
388 this->current_ != this->plugins_.end();
390 (*this->current_)->load();
393 // Call the plugin claim-file handlers in turn to see if any claim the file.
396 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
397 off_t filesize, Object* elf_object)
399 if (this->in_replacement_phase_)
402 unsigned int handle = this->objects_.size();
403 this->input_file_ = input_file;
404 this->plugin_input_file_.name = input_file->filename().c_str();
405 this->plugin_input_file_.fd = input_file->file().descriptor();
406 this->plugin_input_file_.offset = offset;
407 this->plugin_input_file_.filesize = filesize;
408 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
409 if (elf_object != NULL)
410 this->objects_.push_back(elf_object);
411 this->in_claim_file_handler_ = true;
413 for (this->current_ = this->plugins_.begin();
414 this->current_ != this->plugins_.end();
417 if ((*this->current_)->claim_file(&this->plugin_input_file_))
419 this->any_claimed_ = true;
420 this->in_claim_file_handler_ = false;
422 if (this->objects_.size() > handle
423 && this->objects_[handle]->pluginobj() != NULL)
424 return this->objects_[handle]->pluginobj();
426 // If the plugin claimed the file but did not call the
427 // add_symbols callback, we need to create the Pluginobj now.
428 Pluginobj* obj = this->make_plugin_object(handle);
433 this->in_claim_file_handler_ = false;
437 // Save an archive. This is used so that a plugin can add a file
438 // which refers to a symbol which was not previously referenced. In
439 // that case we want to pretend that the symbol was referenced before,
440 // and pull in the archive object.
443 Plugin_manager::save_archive(Archive* archive)
445 if (this->in_replacement_phase_ || !this->any_claimed_)
448 this->rescannable_.push_back(Rescannable(archive));
451 // Save an Input_group. This is like save_archive.
454 Plugin_manager::save_input_group(Input_group* input_group)
456 if (this->in_replacement_phase_ || !this->any_claimed_)
459 this->rescannable_.push_back(Rescannable(input_group));
462 // Call the all-symbols-read handlers.
465 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
466 Input_objects* input_objects,
467 Symbol_table* symtab,
468 Dirsearch* dirpath, Mapfile* mapfile,
469 Task_token** last_blocker)
471 this->in_replacement_phase_ = true;
472 this->workqueue_ = workqueue;
474 this->input_objects_ = input_objects;
475 this->symtab_ = symtab;
476 this->dirpath_ = dirpath;
477 this->mapfile_ = mapfile;
478 this->this_blocker_ = NULL;
480 for (this->current_ = this->plugins_.begin();
481 this->current_ != this->plugins_.end();
483 (*this->current_)->all_symbols_read();
485 if (this->any_added_)
487 Task_token* next_blocker = new Task_token(true);
488 next_blocker->add_blocker();
489 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
490 this->this_blocker_ = next_blocker;
493 *last_blocker = this->this_blocker_;
496 // This is called when we see a new undefined symbol. If we are in
497 // the replacement phase, this means that we may need to rescan some
498 // archives we have previously seen.
501 Plugin_manager::new_undefined_symbol(Symbol* sym)
503 if (this->in_replacement_phase_)
504 this->undefined_symbols_.push_back(sym);
507 // Rescan archives as needed. This handles the case where a new
508 // object file added by a plugin has an undefined reference to some
509 // symbol defined in an archive.
512 Plugin_manager::rescan(Task* task)
514 size_t rescan_pos = 0;
515 size_t rescan_size = this->rescannable_.size();
516 while (!this->undefined_symbols_.empty())
518 if (rescan_pos >= rescan_size)
520 this->undefined_symbols_.clear();
524 Undefined_symbol_list undefs;
525 undefs.reserve(this->undefined_symbols_.size());
526 this->undefined_symbols_.swap(undefs);
528 size_t min_rescan_pos = rescan_size;
530 for (Undefined_symbol_list::const_iterator p = undefs.begin();
534 if (!(*p)->is_undefined())
537 this->undefined_symbols_.push_back(*p);
539 // Find the first rescan archive which defines this symbol,
540 // starting at the current rescan position. The rescan position
541 // exists so that given -la -lb -lc we don't look for undefined
542 // symbols in -lb back in -la, but instead get the definition
543 // from -lc. Don't bother to look past the current minimum
545 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
547 if (this->rescannable_defines(i, *p))
555 if (min_rescan_pos >= rescan_size)
557 // We didn't find any rescannable archives which define any
558 // undefined symbols.
562 const Rescannable& r(this->rescannable_[min_rescan_pos]);
565 Task_lock_obj<Archive> tl(task, r.u.archive);
566 r.u.archive->add_symbols(this->symtab_, this->layout_,
567 this->input_objects_, this->mapfile_);
571 size_t next_saw_undefined = this->symtab_->saw_undefined();
572 size_t saw_undefined;
575 saw_undefined = next_saw_undefined;
577 for (Input_group::const_iterator p = r.u.input_group->begin();
578 p != r.u.input_group->end();
581 Task_lock_obj<Archive> tl(task, *p);
583 (*p)->add_symbols(this->symtab_, this->layout_,
584 this->input_objects_, this->mapfile_);
587 next_saw_undefined = this->symtab_->saw_undefined();
589 while (saw_undefined != next_saw_undefined);
592 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
594 if (this->rescannable_[i].is_archive)
595 delete this->rescannable_[i].u.archive;
597 delete this->rescannable_[i].u.input_group;
600 rescan_pos = min_rescan_pos + 1;
604 // Return whether the rescannable at index I defines SYM.
607 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
609 const Rescannable& r(this->rescannable_[i]);
611 return r.u.archive->defines_symbol(sym);
614 for (Input_group::const_iterator p = r.u.input_group->begin();
615 p != r.u.input_group->end();
618 if ((*p)->defines_symbol(sym))
625 // Layout deferred objects.
628 Plugin_manager::layout_deferred_objects()
630 Deferred_layout_list::iterator obj;
632 for (obj = this->deferred_layout_objects_.begin();
633 obj != this->deferred_layout_objects_.end();
636 // Lock the object so we can read from it. This is only called
637 // single-threaded from queue_middle_tasks, so it is OK to lock.
638 // Unfortunately we have no way to pass in a Task token.
639 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
640 Task_lock_obj<Object> tl(dummy_task, *obj);
641 (*obj)->layout_deferred_sections(this->layout_);
645 // Call the cleanup handlers.
648 Plugin_manager::cleanup()
650 for (this->current_ = this->plugins_.begin();
651 this->current_ != this->plugins_.end();
653 (*this->current_)->cleanup();
656 // Make a new Pluginobj object. This is called when the plugin calls
657 // the add_symbols API.
660 Plugin_manager::make_plugin_object(unsigned int handle)
662 // Make sure we aren't asked to make an object for the same handle twice.
663 if (this->objects_.size() != handle
664 && this->objects_[handle]->pluginobj() != NULL)
667 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
668 this->plugin_input_file_.offset,
669 this->plugin_input_file_.filesize);
672 // If the elf object for this file was pushed into the objects_ vector, delete
673 // it to make room for the Pluginobj as this file is claimed.
674 if (this->objects_.size() != handle)
675 this->objects_.pop_back();
677 this->objects_.push_back(obj);
681 // Get the input file information with an open (possibly re-opened)
685 Plugin_manager::get_input_file(unsigned int handle,
686 struct ld_plugin_input_file* file)
688 Pluginobj* obj = this->object(handle)->pluginobj();
690 return LDPS_BAD_HANDLE;
692 obj->lock(this->task_);
693 file->name = obj->filename().c_str();
694 file->fd = obj->descriptor();
695 file->offset = obj->offset();
696 file->filesize = obj->filesize();
697 file->handle = reinterpret_cast<void*>(handle);
701 // Release the input file.
704 Plugin_manager::release_input_file(unsigned int handle)
706 if (this->object(handle) == NULL)
707 return LDPS_BAD_HANDLE;
709 Pluginobj* obj = this->object(handle)->pluginobj();
712 return LDPS_BAD_HANDLE;
714 obj->unlock(this->task_);
718 // Get the elf object corresponding to the handle. Return NULL if we
719 // found a Pluginobj instead.
722 Plugin_manager::get_elf_object(const void* handle)
724 Object* obj = this->object(
725 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
727 // The object should not be a Pluginobj.
729 || obj->pluginobj() != NULL)
736 Plugin_manager::get_view(unsigned int handle, const void **viewp)
740 Input_file *input_file;
741 if (this->in_claim_file_handler_)
743 // We are being called from the claim_file hook.
744 const struct ld_plugin_input_file &f = this->plugin_input_file_;
746 filesize = f.filesize;
747 input_file = this->input_file_;
751 // An already claimed file.
752 if (this->object(handle) == NULL)
753 return LDPS_BAD_HANDLE;
754 Pluginobj* obj = this->object(handle)->pluginobj();
756 return LDPS_BAD_HANDLE;
757 offset = obj->offset();
758 filesize = obj->filesize();
759 input_file = obj->input_file();
761 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
766 // Add a new library path.
769 Plugin_manager::set_extra_library_path(const char* path)
771 this->extra_search_path_ = std::string(path);
775 // Add a new input file.
778 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
780 Input_file_argument file(pathname,
782 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
783 : Input_file_argument::INPUT_FILE_TYPE_FILE),
785 ? this->extra_search_path_.c_str()
789 Input_argument* input_argument = new Input_argument(file);
790 Task_token* next_blocker = new Task_token(true);
791 next_blocker->add_blocker();
792 if (parameters->incremental())
793 gold_error(_("input files added by plug-ins in --incremental mode not "
795 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
806 this->this_blocker_ = next_blocker;
807 this->any_added_ = true;
813 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
814 off_t offset, off_t filesize)
815 : Object(name, input_file, false, offset),
816 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
820 // Return TRUE if a defined symbol is referenced from outside the
821 // universe of claimed objects.
824 is_referenced_from_outside(Symbol* lsym)
826 if (lsym->in_real_elf())
828 if (parameters->options().relocatable())
830 if (parameters->options().is_undefined(lsym->name()))
835 // Return TRUE if a defined symbol might be reachable from outside the
839 is_visible_from_outside(Symbol* lsym)
841 if (parameters->options().export_dynamic() || parameters->options().shared())
842 return lsym->is_externally_visible();
846 // Get symbol resolution info.
849 Pluginobj::get_symbol_resolution_info(int nsyms,
850 ld_plugin_symbol* syms,
853 // For version 1 of this interface, we cannot use
854 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
856 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
858 ? LDPR_PREVAILING_DEF_IRONLY_EXP
859 : LDPR_PREVAILING_DEF);
861 if (nsyms > this->nsyms_)
864 if (static_cast<size_t>(nsyms) > this->symbols_.size())
866 // We never decided to include this object. We mark all symbols as
868 gold_assert(this->symbols_.size() == 0);
869 for (int i = 0; i < nsyms; i++)
870 syms[i].resolution = LDPR_PREEMPTED_REG;
874 for (int i = 0; i < nsyms; i++)
876 ld_plugin_symbol* isym = &syms[i];
877 Symbol* lsym = this->symbols_[i];
878 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
880 if (lsym->is_undefined())
881 // The symbol remains undefined.
883 else if (isym->def == LDPK_UNDEF
884 || isym->def == LDPK_WEAKUNDEF
885 || isym->def == LDPK_COMMON)
887 // The original symbol was undefined or common.
888 if (lsym->source() != Symbol::FROM_OBJECT)
889 res = LDPR_RESOLVED_EXEC;
890 else if (lsym->object()->pluginobj() == this)
892 if (is_referenced_from_outside(lsym))
893 res = LDPR_PREVAILING_DEF;
894 else if (is_visible_from_outside(lsym))
895 res = ldpr_prevailing_def_ironly_exp;
897 res = LDPR_PREVAILING_DEF_IRONLY;
899 else if (lsym->object()->pluginobj() != NULL)
900 res = LDPR_RESOLVED_IR;
901 else if (lsym->object()->is_dynamic())
902 res = LDPR_RESOLVED_DYN;
904 res = LDPR_RESOLVED_EXEC;
908 // The original symbol was a definition.
909 if (lsym->source() != Symbol::FROM_OBJECT)
910 res = LDPR_PREEMPTED_REG;
911 else if (lsym->object() == static_cast<const Object*>(this))
913 if (is_referenced_from_outside(lsym))
914 res = LDPR_PREVAILING_DEF;
915 else if (is_visible_from_outside(lsym))
916 res = ldpr_prevailing_def_ironly_exp;
918 res = LDPR_PREVAILING_DEF_IRONLY;
921 res = (lsym->object()->pluginobj() != NULL
923 : LDPR_PREEMPTED_REG);
925 isym->resolution = res;
930 // Return TRUE if the comdat group with key COMDAT_KEY from this object
934 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
936 std::pair<Comdat_map::iterator, bool> ins =
937 this->comdat_map_.insert(std::make_pair(comdat_key, false));
939 // If this is the first time we've seen this comdat key, ask the
940 // layout object whether it should be included.
942 ins.first->second = layout->find_or_add_kept_section(comdat_key,
946 return ins.first->second;
949 // Class Sized_pluginobj.
951 template<int size, bool big_endian>
952 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
953 const std::string& name,
954 Input_file* input_file,
957 : Pluginobj(name, input_file, offset, filesize)
961 // Read the symbols. Not used for plugin objects.
963 template<int size, bool big_endian>
965 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
970 // Lay out the input sections. Not used for plugin objects.
972 template<int size, bool big_endian>
974 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
980 // Add the symbols to the symbol table.
982 template<int size, bool big_endian>
984 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
988 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
989 unsigned char symbuf[sym_size];
990 elfcpp::Sym<size, big_endian> sym(symbuf);
991 elfcpp::Sym_write<size, big_endian> osym(symbuf);
993 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
995 this->symbols_.resize(this->nsyms_);
997 for (int i = 0; i < this->nsyms_; ++i)
999 const struct ld_plugin_symbol* isym = &this->syms_[i];
1000 const char* name = isym->name;
1001 const char* ver = isym->version;
1002 elfcpp::Elf_Half shndx;
1006 if (name != NULL && name[0] == '\0')
1008 if (ver != NULL && ver[0] == '\0')
1014 case LDPK_WEAKUNDEF:
1015 bind = elfcpp::STB_WEAK;
1021 bind = elfcpp::STB_GLOBAL;
1029 shndx = elfcpp::SHN_ABS;
1032 shndx = elfcpp::SHN_COMMON;
1035 case LDPK_WEAKUNDEF:
1037 shndx = elfcpp::SHN_UNDEF;
1041 switch (isym->visibility)
1043 case LDPV_PROTECTED:
1044 vis = elfcpp::STV_PROTECTED;
1047 vis = elfcpp::STV_INTERNAL;
1050 vis = elfcpp::STV_HIDDEN;
1054 vis = elfcpp::STV_DEFAULT;
1058 if (isym->comdat_key != NULL
1059 && isym->comdat_key[0] != '\0'
1060 && !this->include_comdat_group(isym->comdat_key, layout))
1061 shndx = elfcpp::SHN_UNDEF;
1063 osym.put_st_name(0);
1064 osym.put_st_value(0);
1065 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
1066 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1067 osym.put_st_other(vis, 0);
1068 osym.put_st_shndx(shndx);
1071 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1075 template<int size, bool big_endian>
1076 Archive::Should_include
1077 Sized_pluginobj<size, big_endian>::do_should_include_member(
1078 Symbol_table* symtab,
1083 char* tmpbuf = NULL;
1084 size_t tmpbuflen = 0;
1086 for (int i = 0; i < this->nsyms_; ++i)
1088 const struct ld_plugin_symbol& sym = this->syms_[i];
1089 const char* name = sym.name;
1091 Archive::Should_include t = Archive::should_include_member(symtab,
1097 if (t == Archive::SHOULD_INCLUDE_YES)
1106 return Archive::SHOULD_INCLUDE_UNKNOWN;
1109 // Iterate over global symbols, calling a visitor class V for each.
1111 template<int size, bool big_endian>
1113 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1115 Library_base::Symbol_visitor_base* v)
1117 for (int i = 0; i < this->nsyms_; ++i)
1119 const struct ld_plugin_symbol& sym = this->syms_[i];
1120 if (sym.def != LDPK_UNDEF)
1125 // Iterate over local symbols, calling a visitor class V for each GOT offset
1126 // associated with a local symbol.
1127 template<int size, bool big_endian>
1129 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1130 Got_offset_list::Visitor*) const
1135 // Get the size of a section. Not used for plugin objects.
1137 template<int size, bool big_endian>
1139 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1145 // Get the name of a section. Not used for plugin objects.
1147 template<int size, bool big_endian>
1149 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
1152 return std::string();
1155 // Return a view of the contents of a section. Not used for plugin objects.
1157 template<int size, bool big_endian>
1159 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
1167 // Return section flags. Not used for plugin objects.
1169 template<int size, bool big_endian>
1171 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1177 // Return section entsize. Not used for plugin objects.
1179 template<int size, bool big_endian>
1181 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1187 // Return section address. Not used for plugin objects.
1189 template<int size, bool big_endian>
1191 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1197 // Return section type. Not used for plugin objects.
1199 template<int size, bool big_endian>
1201 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1207 // Return the section link field. Not used for plugin objects.
1209 template<int size, bool big_endian>
1211 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1217 // Return the section link field. Not used for plugin objects.
1219 template<int size, bool big_endian>
1221 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1227 // Return the section alignment. Not used for plugin objects.
1229 template<int size, bool big_endian>
1231 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1237 // Return the Xindex structure to use. Not used for plugin objects.
1239 template<int size, bool big_endian>
1241 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1247 // Get symbol counts. Don't count plugin objects; the replacement
1248 // files will provide the counts.
1250 template<int size, bool big_endian>
1252 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1253 const Symbol_table*,
1261 // Get symbols. Not used for plugin objects.
1263 template<int size, bool big_endian>
1264 const Object::Symbols*
1265 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1270 // Class Plugin_finish. This task runs after all replacement files have
1271 // been added. For now, it's a placeholder for a possible plugin API
1272 // to allow the plugin to release most of its resources. The cleanup
1273 // handlers must be called later, because they can remove the temporary
1274 // object files that are needed until the end of the link.
1276 class Plugin_finish : public Task
1279 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1280 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1285 if (this->this_blocker_ != NULL)
1286 delete this->this_blocker_;
1292 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1293 return this->this_blocker_;
1298 locks(Task_locker* tl)
1299 { tl->add(this, this->next_blocker_); }
1304 // We could call early cleanup handlers here.
1309 { return "Plugin_finish"; }
1312 Task_token* this_blocker_;
1313 Task_token* next_blocker_;
1316 // Class Plugin_hook.
1318 Plugin_hook::~Plugin_hook()
1322 // Return whether a Plugin_hook task is runnable.
1325 Plugin_hook::is_runnable()
1327 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1328 return this->this_blocker_;
1332 // Return a Task_locker for a Plugin_hook task. We don't need any
1336 Plugin_hook::locks(Task_locker*)
1340 // Run the "all symbols read" plugin hook.
1343 Plugin_hook::run(Workqueue* workqueue)
1345 gold_assert(this->options_.has_plugins());
1346 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1347 if (start_sym != NULL)
1348 start_sym->set_in_real_elf();
1350 this->options_.plugins()->all_symbols_read(workqueue,
1352 this->input_objects_,
1356 &this->this_blocker_);
1357 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1358 this->next_blocker_));
1361 // The C interface routines called by the plugins.
1363 #ifdef ENABLE_PLUGINS
1365 // Register a claim-file handler.
1367 static enum ld_plugin_status
1368 register_claim_file(ld_plugin_claim_file_handler handler)
1370 gold_assert(parameters->options().has_plugins());
1371 parameters->options().plugins()->set_claim_file_handler(handler);
1375 // Register an all-symbols-read handler.
1377 static enum ld_plugin_status
1378 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1380 gold_assert(parameters->options().has_plugins());
1381 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1385 // Register a cleanup handler.
1387 static enum ld_plugin_status
1388 register_cleanup(ld_plugin_cleanup_handler handler)
1390 gold_assert(parameters->options().has_plugins());
1391 parameters->options().plugins()->set_cleanup_handler(handler);
1395 // Add symbols from a plugin-claimed input file.
1397 static enum ld_plugin_status
1398 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1400 gold_assert(parameters->options().has_plugins());
1401 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1402 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1405 obj->store_incoming_symbols(nsyms, syms);
1409 // Get the input file information with an open (possibly re-opened)
1412 static enum ld_plugin_status
1413 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1415 gold_assert(parameters->options().has_plugins());
1416 unsigned int obj_index =
1417 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1418 return parameters->options().plugins()->get_input_file(obj_index, file);
1421 // Release the input file.
1423 static enum ld_plugin_status
1424 release_input_file(const void* handle)
1426 gold_assert(parameters->options().has_plugins());
1427 unsigned int obj_index =
1428 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1429 return parameters->options().plugins()->release_input_file(obj_index);
1432 static enum ld_plugin_status
1433 get_view(const void *handle, const void **viewp)
1435 gold_assert(parameters->options().has_plugins());
1436 unsigned int obj_index =
1437 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1438 return parameters->options().plugins()->get_view(obj_index, viewp);
1441 // Get the symbol resolution info for a plugin-claimed input file.
1443 static enum ld_plugin_status
1444 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1446 gold_assert(parameters->options().has_plugins());
1447 Object* obj = parameters->options().plugins()->object(
1448 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1451 Pluginobj* plugin_obj = obj->pluginobj();
1452 if (plugin_obj == NULL)
1454 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 1);
1457 // Version 2 of the above. The only difference is that this version
1458 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1460 static enum ld_plugin_status
1461 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1463 gold_assert(parameters->options().has_plugins());
1464 Object* obj = parameters->options().plugins()->object(
1465 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1468 Pluginobj* plugin_obj = obj->pluginobj();
1469 if (plugin_obj == NULL)
1471 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 2);
1474 // Add a new (real) input file generated by a plugin.
1476 static enum ld_plugin_status
1477 add_input_file(const char* pathname)
1479 gold_assert(parameters->options().has_plugins());
1480 return parameters->options().plugins()->add_input_file(pathname, false);
1483 // Add a new (real) library required by a plugin.
1485 static enum ld_plugin_status
1486 add_input_library(const char* pathname)
1488 gold_assert(parameters->options().has_plugins());
1489 return parameters->options().plugins()->add_input_file(pathname, true);
1492 // Set the extra library path to be used by libraries added via
1493 // add_input_library
1495 static enum ld_plugin_status
1496 set_extra_library_path(const char* path)
1498 gold_assert(parameters->options().has_plugins());
1499 return parameters->options().plugins()->set_extra_library_path(path);
1502 // Issue a diagnostic message from a plugin.
1504 static enum ld_plugin_status
1505 message(int level, const char* format, ...)
1508 va_start(args, format);
1513 parameters->errors()->info(format, args);
1516 parameters->errors()->warning(format, args);
1520 parameters->errors()->error(format, args);
1523 parameters->errors()->fatal(format, args);
1531 // Get the section count of the object corresponding to the handle. This
1532 // plugin interface can only be called in the claim_file handler of the plugin.
1534 static enum ld_plugin_status
1535 get_input_section_count(const void* handle, unsigned int* count)
1537 gold_assert(parameters->options().has_plugins());
1539 if (!parameters->options().plugins()->in_claim_file_handler())
1542 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1547 *count = obj->shnum();
1551 // Get the type of the specified section in the object corresponding
1552 // to the handle. This plugin interface can only be called in the
1553 // claim_file handler of the plugin.
1555 static enum ld_plugin_status
1556 get_input_section_type(const struct ld_plugin_section section,
1559 gold_assert(parameters->options().has_plugins());
1561 if (!parameters->options().plugins()->in_claim_file_handler())
1565 = parameters->options().plugins()->get_elf_object(section.handle);
1568 return LDPS_BAD_HANDLE;
1570 *type = obj->section_type(section.shndx);
1574 // Get the name of the specified section in the object corresponding
1575 // to the handle. This plugin interface can only be called in the
1576 // claim_file handler of the plugin.
1578 static enum ld_plugin_status
1579 get_input_section_name(const struct ld_plugin_section section,
1580 char** section_name_ptr)
1582 gold_assert(parameters->options().has_plugins());
1584 if (!parameters->options().plugins()->in_claim_file_handler())
1588 = parameters->options().plugins()->get_elf_object(section.handle);
1591 return LDPS_BAD_HANDLE;
1593 // Check if the object is locked before getting the section name.
1594 gold_assert(obj->is_locked());
1596 const std::string section_name = obj->section_name(section.shndx);
1597 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1598 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1602 // Get the contents of the specified section in the object corresponding
1603 // to the handle. This plugin interface can only be called in the
1604 // claim_file handler of the plugin.
1606 static enum ld_plugin_status
1607 get_input_section_contents(const struct ld_plugin_section section,
1608 const unsigned char** section_contents_ptr,
1611 gold_assert(parameters->options().has_plugins());
1613 if (!parameters->options().plugins()->in_claim_file_handler())
1617 = parameters->options().plugins()->get_elf_object(section.handle);
1620 return LDPS_BAD_HANDLE;
1622 // Check if the object is locked before getting the section contents.
1623 gold_assert(obj->is_locked());
1625 section_size_type plen;
1626 *section_contents_ptr
1627 = obj->section_contents(section.shndx, &plen, false);
1632 // Specify the ordering of sections in the final layout. The sections are
1633 // specified as (handle,shndx) pairs in the two arrays in the order in
1634 // which they should appear in the final layout.
1636 static enum ld_plugin_status
1637 update_section_order(const struct ld_plugin_section* section_list,
1638 unsigned int num_sections)
1640 gold_assert(parameters->options().has_plugins());
1642 if (num_sections == 0)
1645 if (section_list == NULL)
1648 Layout* layout = parameters->options().plugins()->layout();
1649 gold_assert (layout != NULL);
1651 std::map<Section_id, unsigned int>* order_map
1652 = layout->get_section_order_map();
1654 /* Store the mapping from Section_id to section position in layout's
1655 order_map to consult after output sections are added. */
1656 for (unsigned int i = 0; i < num_sections; ++i)
1658 Object* obj = parameters->options().plugins()->get_elf_object(
1659 section_list[i].handle);
1661 return LDPS_BAD_HANDLE;
1662 unsigned int shndx = section_list[i].shndx;
1663 Section_id secn_id(obj, shndx);
1664 (*order_map)[secn_id] = i + 1;
1670 // Let the linker know that the sections could be reordered.
1672 static enum ld_plugin_status
1673 allow_section_ordering()
1675 gold_assert(parameters->options().has_plugins());
1676 Layout* layout = parameters->options().plugins()->layout();
1677 layout->set_section_ordering_specified();
1681 #endif // ENABLE_PLUGINS
1683 // Allocate a Pluginobj object of the appropriate size and endianness.
1686 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1688 Pluginobj* obj = NULL;
1690 parameters_force_valid_target();
1691 const Target& target(parameters->target());
1693 if (target.get_size() == 32)
1695 if (target.is_big_endian())
1696 #ifdef HAVE_TARGET_32_BIG
1697 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1698 input_file, offset, filesize);
1700 gold_error(_("%s: not configured to support "
1701 "32-bit big-endian object"),
1702 input_file->filename().c_str());
1705 #ifdef HAVE_TARGET_32_LITTLE
1706 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1707 input_file, offset, filesize);
1709 gold_error(_("%s: not configured to support "
1710 "32-bit little-endian object"),
1711 input_file->filename().c_str());
1714 else if (target.get_size() == 64)
1716 if (target.is_big_endian())
1717 #ifdef HAVE_TARGET_64_BIG
1718 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1719 input_file, offset, filesize);
1721 gold_error(_("%s: not configured to support "
1722 "64-bit big-endian object"),
1723 input_file->filename().c_str());
1726 #ifdef HAVE_TARGET_64_LITTLE
1727 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1728 input_file, offset, filesize);
1730 gold_error(_("%s: not configured to support "
1731 "64-bit little-endian object"),
1732 input_file->filename().c_str());
1736 gold_assert(obj != NULL);
1740 } // End namespace gold.