1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008, 2009, 2010, 2011, 2012, 2013 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.
34 #elif defined (HAVE_WINDOWS_H)
37 #error Unknown how to handle dynamic-load-libraries.
40 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
42 #define RTLD_NOW 0 /* Dummy value. */
44 dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
46 return LoadLibrary(file);
50 dlsym(void *handle, const char *name)
52 return reinterpret_cast<void *>(
53 GetProcAddress(static_cast<HMODULE>(handle),name));
59 return "unable to load dll";
62 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
63 #endif /* ENABLE_PLUGINS */
65 #include "parameters.h"
81 // The linker's exported interfaces.
86 static enum ld_plugin_status
87 register_claim_file(ld_plugin_claim_file_handler handler);
89 static enum ld_plugin_status
90 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
92 static enum ld_plugin_status
93 register_cleanup(ld_plugin_cleanup_handler handler);
95 static enum ld_plugin_status
96 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
98 static enum ld_plugin_status
99 get_input_file(const void *handle, struct ld_plugin_input_file *file);
101 static enum ld_plugin_status
102 get_view(const void *handle, const void **viewp);
104 static enum ld_plugin_status
105 release_input_file(const void *handle);
107 static enum ld_plugin_status
108 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
110 static enum ld_plugin_status
111 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
113 static enum ld_plugin_status
114 add_input_file(const char *pathname);
116 static enum ld_plugin_status
117 add_input_library(const char *pathname);
119 static enum ld_plugin_status
120 set_extra_library_path(const char *path);
122 static enum ld_plugin_status
123 message(int level, const char *format, ...);
125 static enum ld_plugin_status
126 get_input_section_count(const void* handle, unsigned int* count);
128 static enum ld_plugin_status
129 get_input_section_type(const struct ld_plugin_section section,
132 static enum ld_plugin_status
133 get_input_section_name(const struct ld_plugin_section section,
134 char** section_name_ptr);
136 static enum ld_plugin_status
137 get_input_section_contents(const struct ld_plugin_section section,
138 const unsigned char** section_contents,
141 static enum ld_plugin_status
142 update_section_order(const struct ld_plugin_section *section_list,
143 unsigned int num_sections);
145 static enum ld_plugin_status
146 allow_section_ordering();
148 static enum ld_plugin_status
149 allow_unique_segment_for_sections();
151 static enum ld_plugin_status
152 unique_segment_for_sections(const char* segment_name,
155 const struct ld_plugin_section *section_list,
156 unsigned int num_sections);
159 #endif // ENABLE_PLUGINS
161 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
162 off_t offset, off_t filesize);
166 // Load one plugin library.
171 #ifdef ENABLE_PLUGINS
172 // Load the plugin library.
173 // FIXME: Look for the library in standard locations.
174 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
175 if (this->handle_ == NULL)
177 gold_error(_("%s: could not load plugin library: %s"),
178 this->filename_.c_str(), dlerror());
182 // Find the plugin's onload entry point.
183 void* ptr = dlsym(this->handle_, "onload");
186 gold_error(_("%s: could not find onload entry point"),
187 this->filename_.c_str());
190 ld_plugin_onload onload;
191 gold_assert(sizeof(onload) == sizeof(ptr));
192 memcpy(&onload, &ptr, sizeof(ptr));
194 // Get the linker's version number.
195 const char* ver = get_version_string();
198 sscanf(ver, "%d.%d", &major, &minor);
200 // Allocate and populate a transfer vector.
201 const int tv_fixed_size = 26;
203 int tv_size = this->args_.size() + tv_fixed_size;
204 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
206 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
207 // while processing subsequent entries.
209 tv[i].tv_tag = LDPT_MESSAGE;
210 tv[i].tv_u.tv_message = message;
213 tv[i].tv_tag = LDPT_API_VERSION;
214 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
217 tv[i].tv_tag = LDPT_GOLD_VERSION;
218 tv[i].tv_u.tv_val = major * 100 + minor;
221 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
222 if (parameters->options().relocatable())
223 tv[i].tv_u.tv_val = LDPO_REL;
224 else if (parameters->options().shared())
225 tv[i].tv_u.tv_val = LDPO_DYN;
226 else if (parameters->options().pie())
227 tv[i].tv_u.tv_val = LDPO_PIE;
229 tv[i].tv_u.tv_val = LDPO_EXEC;
232 tv[i].tv_tag = LDPT_OUTPUT_NAME;
233 tv[i].tv_u.tv_string = parameters->options().output();
235 for (unsigned int j = 0; j < this->args_.size(); ++j)
238 tv[i].tv_tag = LDPT_OPTION;
239 tv[i].tv_u.tv_string = this->args_[j].c_str();
243 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
244 tv[i].tv_u.tv_register_claim_file = register_claim_file;
247 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
248 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
251 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
252 tv[i].tv_u.tv_register_cleanup = register_cleanup;
255 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
256 tv[i].tv_u.tv_add_symbols = add_symbols;
259 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
260 tv[i].tv_u.tv_get_input_file = get_input_file;
263 tv[i].tv_tag = LDPT_GET_VIEW;
264 tv[i].tv_u.tv_get_view = get_view;
267 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
268 tv[i].tv_u.tv_release_input_file = release_input_file;
271 tv[i].tv_tag = LDPT_GET_SYMBOLS;
272 tv[i].tv_u.tv_get_symbols = get_symbols;
275 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
276 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
279 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
280 tv[i].tv_u.tv_add_input_file = add_input_file;
283 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
284 tv[i].tv_u.tv_add_input_library = add_input_library;
287 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
288 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
291 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
292 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
295 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
296 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
299 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
300 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
303 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
304 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
307 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
308 tv[i].tv_u.tv_update_section_order = update_section_order;
311 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
312 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
315 tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
316 tv[i].tv_u.tv_allow_unique_segment_for_sections
317 = allow_unique_segment_for_sections;
320 tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
321 tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
324 tv[i].tv_tag = LDPT_NULL;
325 tv[i].tv_u.tv_val = 0;
327 gold_assert(i == tv_size - 1);
329 // Call the onload entry point.
333 #endif // ENABLE_PLUGINS
336 // Call the plugin claim-file handler.
339 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
343 if (this->claim_file_handler_ != NULL)
345 (*this->claim_file_handler_)(plugin_input_file, &claimed);
352 // Call the all-symbols-read handler.
355 Plugin::all_symbols_read()
357 if (this->all_symbols_read_handler_ != NULL)
358 (*this->all_symbols_read_handler_)();
361 // Call the cleanup handler.
366 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
368 // Set this flag before calling to prevent a recursive plunge
369 // in the event that a plugin's cleanup handler issues a
371 this->cleanup_done_ = true;
372 (*this->cleanup_handler_)();
376 // This task is used to rescan archives as needed.
378 class Plugin_rescan : public Task
381 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
382 : this_blocker_(this_blocker), next_blocker_(next_blocker)
387 delete this->this_blocker_;
393 if (this->this_blocker_->is_blocked())
394 return this->this_blocker_;
399 locks(Task_locker* tl)
400 { tl->add(this, this->next_blocker_); }
404 { parameters->options().plugins()->rescan(this); }
408 { return "Plugin_rescan"; }
411 Task_token* this_blocker_;
412 Task_token* next_blocker_;
415 // Plugin_manager methods.
417 Plugin_manager::~Plugin_manager()
419 for (Plugin_list::iterator p = this->plugins_.begin();
420 p != this->plugins_.end();
423 this->plugins_.clear();
424 for (Object_list::iterator obj = this->objects_.begin();
425 obj != this->objects_.end();
428 this->objects_.clear();
431 // Load all plugin libraries.
434 Plugin_manager::load_plugins(Layout* layout)
436 this->layout_ = layout;
437 for (this->current_ = this->plugins_.begin();
438 this->current_ != this->plugins_.end();
440 (*this->current_)->load();
443 // Call the plugin claim-file handlers in turn to see if any claim the file.
446 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
447 off_t filesize, Object* elf_object)
449 if (this->in_replacement_phase_)
452 unsigned int handle = this->objects_.size();
453 this->input_file_ = input_file;
454 this->plugin_input_file_.name = input_file->filename().c_str();
455 this->plugin_input_file_.fd = input_file->file().descriptor();
456 this->plugin_input_file_.offset = offset;
457 this->plugin_input_file_.filesize = filesize;
458 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
459 if (elf_object != NULL)
460 this->objects_.push_back(elf_object);
461 this->in_claim_file_handler_ = true;
463 for (this->current_ = this->plugins_.begin();
464 this->current_ != this->plugins_.end();
467 if ((*this->current_)->claim_file(&this->plugin_input_file_))
469 this->any_claimed_ = true;
470 this->in_claim_file_handler_ = false;
472 if (this->objects_.size() > handle
473 && this->objects_[handle]->pluginobj() != NULL)
474 return this->objects_[handle]->pluginobj();
476 // If the plugin claimed the file but did not call the
477 // add_symbols callback, we need to create the Pluginobj now.
478 Pluginobj* obj = this->make_plugin_object(handle);
483 this->in_claim_file_handler_ = false;
487 // Save an archive. This is used so that a plugin can add a file
488 // which refers to a symbol which was not previously referenced. In
489 // that case we want to pretend that the symbol was referenced before,
490 // and pull in the archive object.
493 Plugin_manager::save_archive(Archive* archive)
495 if (this->in_replacement_phase_ || !this->any_claimed_)
498 this->rescannable_.push_back(Rescannable(archive));
501 // Save an Input_group. This is like save_archive.
504 Plugin_manager::save_input_group(Input_group* input_group)
506 if (this->in_replacement_phase_ || !this->any_claimed_)
509 this->rescannable_.push_back(Rescannable(input_group));
512 // Call the all-symbols-read handlers.
515 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
516 Input_objects* input_objects,
517 Symbol_table* symtab,
518 Dirsearch* dirpath, Mapfile* mapfile,
519 Task_token** last_blocker)
521 this->in_replacement_phase_ = true;
522 this->workqueue_ = workqueue;
524 this->input_objects_ = input_objects;
525 this->symtab_ = symtab;
526 this->dirpath_ = dirpath;
527 this->mapfile_ = mapfile;
528 this->this_blocker_ = NULL;
530 for (this->current_ = this->plugins_.begin();
531 this->current_ != this->plugins_.end();
533 (*this->current_)->all_symbols_read();
535 if (this->any_added_)
537 Task_token* next_blocker = new Task_token(true);
538 next_blocker->add_blocker();
539 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
540 this->this_blocker_ = next_blocker;
543 *last_blocker = this->this_blocker_;
546 // This is called when we see a new undefined symbol. If we are in
547 // the replacement phase, this means that we may need to rescan some
548 // archives we have previously seen.
551 Plugin_manager::new_undefined_symbol(Symbol* sym)
553 if (this->in_replacement_phase_)
554 this->undefined_symbols_.push_back(sym);
557 // Rescan archives as needed. This handles the case where a new
558 // object file added by a plugin has an undefined reference to some
559 // symbol defined in an archive.
562 Plugin_manager::rescan(Task* task)
564 size_t rescan_pos = 0;
565 size_t rescan_size = this->rescannable_.size();
566 while (!this->undefined_symbols_.empty())
568 if (rescan_pos >= rescan_size)
570 this->undefined_symbols_.clear();
574 Undefined_symbol_list undefs;
575 undefs.reserve(this->undefined_symbols_.size());
576 this->undefined_symbols_.swap(undefs);
578 size_t min_rescan_pos = rescan_size;
580 for (Undefined_symbol_list::const_iterator p = undefs.begin();
584 if (!(*p)->is_undefined())
587 this->undefined_symbols_.push_back(*p);
589 // Find the first rescan archive which defines this symbol,
590 // starting at the current rescan position. The rescan position
591 // exists so that given -la -lb -lc we don't look for undefined
592 // symbols in -lb back in -la, but instead get the definition
593 // from -lc. Don't bother to look past the current minimum
595 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
597 if (this->rescannable_defines(i, *p))
605 if (min_rescan_pos >= rescan_size)
607 // We didn't find any rescannable archives which define any
608 // undefined symbols.
612 const Rescannable& r(this->rescannable_[min_rescan_pos]);
615 Task_lock_obj<Archive> tl(task, r.u.archive);
616 r.u.archive->add_symbols(this->symtab_, this->layout_,
617 this->input_objects_, this->mapfile_);
621 size_t next_saw_undefined = this->symtab_->saw_undefined();
622 size_t saw_undefined;
625 saw_undefined = next_saw_undefined;
627 for (Input_group::const_iterator p = r.u.input_group->begin();
628 p != r.u.input_group->end();
631 Task_lock_obj<Archive> tl(task, *p);
633 (*p)->add_symbols(this->symtab_, this->layout_,
634 this->input_objects_, this->mapfile_);
637 next_saw_undefined = this->symtab_->saw_undefined();
639 while (saw_undefined != next_saw_undefined);
642 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
644 if (this->rescannable_[i].is_archive)
645 delete this->rescannable_[i].u.archive;
647 delete this->rescannable_[i].u.input_group;
650 rescan_pos = min_rescan_pos + 1;
654 // Return whether the rescannable at index I defines SYM.
657 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
659 const Rescannable& r(this->rescannable_[i]);
661 return r.u.archive->defines_symbol(sym);
664 for (Input_group::const_iterator p = r.u.input_group->begin();
665 p != r.u.input_group->end();
668 if ((*p)->defines_symbol(sym))
675 // Layout deferred objects.
678 Plugin_manager::layout_deferred_objects()
680 Deferred_layout_list::iterator obj;
682 for (obj = this->deferred_layout_objects_.begin();
683 obj != this->deferred_layout_objects_.end();
686 // Lock the object so we can read from it. This is only called
687 // single-threaded from queue_middle_tasks, so it is OK to lock.
688 // Unfortunately we have no way to pass in a Task token.
689 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
690 Task_lock_obj<Object> tl(dummy_task, *obj);
691 (*obj)->layout_deferred_sections(this->layout_);
695 // Call the cleanup handlers.
698 Plugin_manager::cleanup()
700 for (this->current_ = this->plugins_.begin();
701 this->current_ != this->plugins_.end();
703 (*this->current_)->cleanup();
706 // Make a new Pluginobj object. This is called when the plugin calls
707 // the add_symbols API.
710 Plugin_manager::make_plugin_object(unsigned int handle)
712 // Make sure we aren't asked to make an object for the same handle twice.
713 if (this->objects_.size() != handle
714 && this->objects_[handle]->pluginobj() != NULL)
717 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
718 this->plugin_input_file_.offset,
719 this->plugin_input_file_.filesize);
722 // If the elf object for this file was pushed into the objects_ vector, delete
723 // it to make room for the Pluginobj as this file is claimed.
724 if (this->objects_.size() != handle)
725 this->objects_.pop_back();
727 this->objects_.push_back(obj);
731 // Get the input file information with an open (possibly re-opened)
735 Plugin_manager::get_input_file(unsigned int handle,
736 struct ld_plugin_input_file* file)
738 Pluginobj* obj = this->object(handle)->pluginobj();
740 return LDPS_BAD_HANDLE;
742 obj->lock(this->task_);
743 file->name = obj->filename().c_str();
744 file->fd = obj->descriptor();
745 file->offset = obj->offset();
746 file->filesize = obj->filesize();
747 file->handle = reinterpret_cast<void*>(handle);
751 // Release the input file.
754 Plugin_manager::release_input_file(unsigned int handle)
756 if (this->object(handle) == NULL)
757 return LDPS_BAD_HANDLE;
759 Pluginobj* obj = this->object(handle)->pluginobj();
762 return LDPS_BAD_HANDLE;
764 obj->unlock(this->task_);
768 // Get the elf object corresponding to the handle. Return NULL if we
769 // found a Pluginobj instead.
772 Plugin_manager::get_elf_object(const void* handle)
774 Object* obj = this->object(
775 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
777 // The object should not be a Pluginobj.
779 || obj->pluginobj() != NULL)
786 Plugin_manager::get_view(unsigned int handle, const void **viewp)
790 Input_file *input_file;
791 if (this->in_claim_file_handler_)
793 // We are being called from the claim_file hook.
794 const struct ld_plugin_input_file &f = this->plugin_input_file_;
796 filesize = f.filesize;
797 input_file = this->input_file_;
801 // An already claimed file.
802 if (this->object(handle) == NULL)
803 return LDPS_BAD_HANDLE;
804 Pluginobj* obj = this->object(handle)->pluginobj();
806 return LDPS_BAD_HANDLE;
807 offset = obj->offset();
808 filesize = obj->filesize();
809 input_file = obj->input_file();
811 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
816 // Add a new library path.
819 Plugin_manager::set_extra_library_path(const char* path)
821 this->extra_search_path_ = std::string(path);
825 // Add a new input file.
828 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
830 Input_file_argument file(pathname,
832 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
833 : Input_file_argument::INPUT_FILE_TYPE_FILE),
835 ? this->extra_search_path_.c_str()
839 Input_argument* input_argument = new Input_argument(file);
840 Task_token* next_blocker = new Task_token(true);
841 next_blocker->add_blocker();
842 if (parameters->incremental())
843 gold_error(_("input files added by plug-ins in --incremental mode not "
845 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
856 this->this_blocker_ = next_blocker;
857 this->any_added_ = true;
863 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
864 off_t offset, off_t filesize)
865 : Object(name, input_file, false, offset),
866 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
870 // Return TRUE if a defined symbol is referenced from outside the
871 // universe of claimed objects. Only references from relocatable,
872 // non-IR (unclaimed) objects count as a reference. References from
873 // dynamic objects count only as "visible".
876 is_referenced_from_outside(Symbol* lsym)
878 if (lsym->in_real_elf())
880 if (parameters->options().relocatable())
882 if (parameters->options().is_undefined(lsym->name()))
887 // Return TRUE if a defined symbol might be reachable from outside the
891 is_visible_from_outside(Symbol* lsym)
895 if (parameters->options().export_dynamic() || parameters->options().shared())
896 return lsym->is_externally_visible();
900 // Get symbol resolution info.
903 Pluginobj::get_symbol_resolution_info(int nsyms,
904 ld_plugin_symbol* syms,
907 // For version 1 of this interface, we cannot use
908 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
910 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
912 ? LDPR_PREVAILING_DEF_IRONLY_EXP
913 : LDPR_PREVAILING_DEF);
915 if (nsyms > this->nsyms_)
918 if (static_cast<size_t>(nsyms) > this->symbols_.size())
920 // We never decided to include this object. We mark all symbols as
922 gold_assert(this->symbols_.size() == 0);
923 for (int i = 0; i < nsyms; i++)
924 syms[i].resolution = LDPR_PREEMPTED_REG;
928 for (int i = 0; i < nsyms; i++)
930 ld_plugin_symbol* isym = &syms[i];
931 Symbol* lsym = this->symbols_[i];
932 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
934 if (lsym->is_undefined())
935 // The symbol remains undefined.
937 else if (isym->def == LDPK_UNDEF
938 || isym->def == LDPK_WEAKUNDEF
939 || isym->def == LDPK_COMMON)
941 // The original symbol was undefined or common.
942 if (lsym->source() != Symbol::FROM_OBJECT)
943 res = LDPR_RESOLVED_EXEC;
944 else if (lsym->object()->pluginobj() == this)
946 if (is_referenced_from_outside(lsym))
947 res = LDPR_PREVAILING_DEF;
948 else if (is_visible_from_outside(lsym))
949 res = ldpr_prevailing_def_ironly_exp;
951 res = LDPR_PREVAILING_DEF_IRONLY;
953 else if (lsym->object()->pluginobj() != NULL)
954 res = LDPR_RESOLVED_IR;
955 else if (lsym->object()->is_dynamic())
956 res = LDPR_RESOLVED_DYN;
958 res = LDPR_RESOLVED_EXEC;
962 // The original symbol was a definition.
963 if (lsym->source() != Symbol::FROM_OBJECT)
964 res = LDPR_PREEMPTED_REG;
965 else if (lsym->object() == static_cast<const Object*>(this))
967 if (is_referenced_from_outside(lsym))
968 res = LDPR_PREVAILING_DEF;
969 else if (is_visible_from_outside(lsym))
970 res = ldpr_prevailing_def_ironly_exp;
972 res = LDPR_PREVAILING_DEF_IRONLY;
975 res = (lsym->object()->pluginobj() != NULL
977 : LDPR_PREEMPTED_REG);
979 isym->resolution = res;
984 // Return TRUE if the comdat group with key COMDAT_KEY from this object
988 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
990 std::pair<Comdat_map::iterator, bool> ins =
991 this->comdat_map_.insert(std::make_pair(comdat_key, false));
993 // If this is the first time we've seen this comdat key, ask the
994 // layout object whether it should be included.
996 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1000 return ins.first->second;
1003 // Class Sized_pluginobj.
1005 template<int size, bool big_endian>
1006 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1007 const std::string& name,
1008 Input_file* input_file,
1011 : Pluginobj(name, input_file, offset, filesize)
1015 // Read the symbols. Not used for plugin objects.
1017 template<int size, bool big_endian>
1019 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1024 // Lay out the input sections. Not used for plugin objects.
1026 template<int size, bool big_endian>
1028 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1034 // Add the symbols to the symbol table.
1036 template<int size, bool big_endian>
1038 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1042 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1043 unsigned char symbuf[sym_size];
1044 elfcpp::Sym<size, big_endian> sym(symbuf);
1045 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1047 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
1049 this->symbols_.resize(this->nsyms_);
1051 for (int i = 0; i < this->nsyms_; ++i)
1053 const struct ld_plugin_symbol* isym = &this->syms_[i];
1054 const char* name = isym->name;
1055 const char* ver = isym->version;
1056 elfcpp::Elf_Half shndx;
1060 if (name != NULL && name[0] == '\0')
1062 if (ver != NULL && ver[0] == '\0')
1068 case LDPK_WEAKUNDEF:
1069 bind = elfcpp::STB_WEAK;
1075 bind = elfcpp::STB_GLOBAL;
1083 shndx = elfcpp::SHN_ABS;
1086 shndx = elfcpp::SHN_COMMON;
1089 case LDPK_WEAKUNDEF:
1091 shndx = elfcpp::SHN_UNDEF;
1095 switch (isym->visibility)
1097 case LDPV_PROTECTED:
1098 vis = elfcpp::STV_PROTECTED;
1101 vis = elfcpp::STV_INTERNAL;
1104 vis = elfcpp::STV_HIDDEN;
1108 vis = elfcpp::STV_DEFAULT;
1112 if (isym->comdat_key != NULL
1113 && isym->comdat_key[0] != '\0'
1114 && !this->include_comdat_group(isym->comdat_key, layout))
1115 shndx = elfcpp::SHN_UNDEF;
1117 osym.put_st_name(0);
1118 osym.put_st_value(0);
1119 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
1120 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1121 osym.put_st_other(vis, 0);
1122 osym.put_st_shndx(shndx);
1125 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1129 template<int size, bool big_endian>
1130 Archive::Should_include
1131 Sized_pluginobj<size, big_endian>::do_should_include_member(
1132 Symbol_table* symtab,
1137 char* tmpbuf = NULL;
1138 size_t tmpbuflen = 0;
1140 for (int i = 0; i < this->nsyms_; ++i)
1142 const struct ld_plugin_symbol& sym = this->syms_[i];
1143 const char* name = sym.name;
1145 Archive::Should_include t = Archive::should_include_member(symtab,
1151 if (t == Archive::SHOULD_INCLUDE_YES)
1160 return Archive::SHOULD_INCLUDE_UNKNOWN;
1163 // Iterate over global symbols, calling a visitor class V for each.
1165 template<int size, bool big_endian>
1167 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1169 Library_base::Symbol_visitor_base* v)
1171 for (int i = 0; i < this->nsyms_; ++i)
1173 const struct ld_plugin_symbol& sym = this->syms_[i];
1174 if (sym.def != LDPK_UNDEF)
1179 // Iterate over local symbols, calling a visitor class V for each GOT offset
1180 // associated with a local symbol.
1181 template<int size, bool big_endian>
1183 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1184 Got_offset_list::Visitor*) const
1189 // Get the size of a section. Not used for plugin objects.
1191 template<int size, bool big_endian>
1193 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1199 // Get the name of a section. Not used for plugin objects.
1201 template<int size, bool big_endian>
1203 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
1206 return std::string();
1209 // Return a view of the contents of a section. Not used for plugin objects.
1211 template<int size, bool big_endian>
1212 const unsigned char*
1213 Sized_pluginobj<size, big_endian>::do_section_contents(
1222 // Return section flags. Not used for plugin objects.
1224 template<int size, bool big_endian>
1226 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1232 // Return section entsize. Not used for plugin objects.
1234 template<int size, bool big_endian>
1236 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1242 // Return section address. Not used for plugin objects.
1244 template<int size, bool big_endian>
1246 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1252 // Return section type. Not used for plugin objects.
1254 template<int size, bool big_endian>
1256 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1262 // Return the section link field. Not used for plugin objects.
1264 template<int size, bool big_endian>
1266 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1272 // Return the section link field. Not used for plugin objects.
1274 template<int size, bool big_endian>
1276 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1282 // Return the section alignment. Not used for plugin objects.
1284 template<int size, bool big_endian>
1286 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1292 // Return the Xindex structure to use. Not used for plugin objects.
1294 template<int size, bool big_endian>
1296 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1302 // Get symbol counts. Don't count plugin objects; the replacement
1303 // files will provide the counts.
1305 template<int size, bool big_endian>
1307 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1308 const Symbol_table*,
1316 // Get symbols. Not used for plugin objects.
1318 template<int size, bool big_endian>
1319 const Object::Symbols*
1320 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1325 // Class Plugin_finish. This task runs after all replacement files have
1326 // been added. For now, it's a placeholder for a possible plugin API
1327 // to allow the plugin to release most of its resources. The cleanup
1328 // handlers must be called later, because they can remove the temporary
1329 // object files that are needed until the end of the link.
1331 class Plugin_finish : public Task
1334 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1335 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1340 if (this->this_blocker_ != NULL)
1341 delete this->this_blocker_;
1347 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1348 return this->this_blocker_;
1353 locks(Task_locker* tl)
1354 { tl->add(this, this->next_blocker_); }
1359 // We could call early cleanup handlers here.
1364 { return "Plugin_finish"; }
1367 Task_token* this_blocker_;
1368 Task_token* next_blocker_;
1371 // Class Plugin_hook.
1373 Plugin_hook::~Plugin_hook()
1377 // Return whether a Plugin_hook task is runnable.
1380 Plugin_hook::is_runnable()
1382 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1383 return this->this_blocker_;
1387 // Return a Task_locker for a Plugin_hook task. We don't need any
1391 Plugin_hook::locks(Task_locker*)
1395 // Run the "all symbols read" plugin hook.
1398 Plugin_hook::run(Workqueue* workqueue)
1400 gold_assert(this->options_.has_plugins());
1401 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1402 if (start_sym != NULL)
1403 start_sym->set_in_real_elf();
1405 this->options_.plugins()->all_symbols_read(workqueue,
1407 this->input_objects_,
1411 &this->this_blocker_);
1412 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1413 this->next_blocker_));
1416 // The C interface routines called by the plugins.
1418 #ifdef ENABLE_PLUGINS
1420 // Register a claim-file handler.
1422 static enum ld_plugin_status
1423 register_claim_file(ld_plugin_claim_file_handler handler)
1425 gold_assert(parameters->options().has_plugins());
1426 parameters->options().plugins()->set_claim_file_handler(handler);
1430 // Register an all-symbols-read handler.
1432 static enum ld_plugin_status
1433 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1435 gold_assert(parameters->options().has_plugins());
1436 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1440 // Register a cleanup handler.
1442 static enum ld_plugin_status
1443 register_cleanup(ld_plugin_cleanup_handler handler)
1445 gold_assert(parameters->options().has_plugins());
1446 parameters->options().plugins()->set_cleanup_handler(handler);
1450 // Add symbols from a plugin-claimed input file.
1452 static enum ld_plugin_status
1453 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1455 gold_assert(parameters->options().has_plugins());
1456 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1457 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1460 obj->store_incoming_symbols(nsyms, syms);
1464 // Get the input file information with an open (possibly re-opened)
1467 static enum ld_plugin_status
1468 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1470 gold_assert(parameters->options().has_plugins());
1471 unsigned int obj_index =
1472 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1473 return parameters->options().plugins()->get_input_file(obj_index, file);
1476 // Release the input file.
1478 static enum ld_plugin_status
1479 release_input_file(const void* handle)
1481 gold_assert(parameters->options().has_plugins());
1482 unsigned int obj_index =
1483 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1484 return parameters->options().plugins()->release_input_file(obj_index);
1487 static enum ld_plugin_status
1488 get_view(const void *handle, const void **viewp)
1490 gold_assert(parameters->options().has_plugins());
1491 unsigned int obj_index =
1492 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1493 return parameters->options().plugins()->get_view(obj_index, viewp);
1496 // Get the symbol resolution info for a plugin-claimed input file.
1498 static enum ld_plugin_status
1499 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1501 gold_assert(parameters->options().has_plugins());
1502 Object* obj = parameters->options().plugins()->object(
1503 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1506 Pluginobj* plugin_obj = obj->pluginobj();
1507 if (plugin_obj == NULL)
1509 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 1);
1512 // Version 2 of the above. The only difference is that this version
1513 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1515 static enum ld_plugin_status
1516 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1518 gold_assert(parameters->options().has_plugins());
1519 Object* obj = parameters->options().plugins()->object(
1520 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1523 Pluginobj* plugin_obj = obj->pluginobj();
1524 if (plugin_obj == NULL)
1526 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 2);
1529 // Add a new (real) input file generated by a plugin.
1531 static enum ld_plugin_status
1532 add_input_file(const char* pathname)
1534 gold_assert(parameters->options().has_plugins());
1535 return parameters->options().plugins()->add_input_file(pathname, false);
1538 // Add a new (real) library required by a plugin.
1540 static enum ld_plugin_status
1541 add_input_library(const char* pathname)
1543 gold_assert(parameters->options().has_plugins());
1544 return parameters->options().plugins()->add_input_file(pathname, true);
1547 // Set the extra library path to be used by libraries added via
1548 // add_input_library
1550 static enum ld_plugin_status
1551 set_extra_library_path(const char* path)
1553 gold_assert(parameters->options().has_plugins());
1554 return parameters->options().plugins()->set_extra_library_path(path);
1557 // Issue a diagnostic message from a plugin.
1559 static enum ld_plugin_status
1560 message(int level, const char* format, ...)
1563 va_start(args, format);
1568 parameters->errors()->info(format, args);
1571 parameters->errors()->warning(format, args);
1575 parameters->errors()->error(format, args);
1578 parameters->errors()->fatal(format, args);
1586 // Get the section count of the object corresponding to the handle. This
1587 // plugin interface can only be called in the claim_file handler of the plugin.
1589 static enum ld_plugin_status
1590 get_input_section_count(const void* handle, unsigned int* count)
1592 gold_assert(parameters->options().has_plugins());
1594 if (!parameters->options().plugins()->in_claim_file_handler())
1597 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1602 *count = obj->shnum();
1606 // Get the type of the specified section in the object corresponding
1607 // to the handle. This plugin interface can only be called in the
1608 // claim_file handler of the plugin.
1610 static enum ld_plugin_status
1611 get_input_section_type(const struct ld_plugin_section section,
1614 gold_assert(parameters->options().has_plugins());
1616 if (!parameters->options().plugins()->in_claim_file_handler())
1620 = parameters->options().plugins()->get_elf_object(section.handle);
1623 return LDPS_BAD_HANDLE;
1625 *type = obj->section_type(section.shndx);
1629 // Get the name of the specified section in the object corresponding
1630 // to the handle. This plugin interface can only be called in the
1631 // claim_file handler of the plugin.
1633 static enum ld_plugin_status
1634 get_input_section_name(const struct ld_plugin_section section,
1635 char** section_name_ptr)
1637 gold_assert(parameters->options().has_plugins());
1639 if (!parameters->options().plugins()->in_claim_file_handler())
1643 = parameters->options().plugins()->get_elf_object(section.handle);
1646 return LDPS_BAD_HANDLE;
1648 // Check if the object is locked before getting the section name.
1649 gold_assert(obj->is_locked());
1651 const std::string section_name = obj->section_name(section.shndx);
1652 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1653 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1657 // Get the contents of the specified section in the object corresponding
1658 // to the handle. This plugin interface can only be called in the
1659 // claim_file handler of the plugin.
1661 static enum ld_plugin_status
1662 get_input_section_contents(const struct ld_plugin_section section,
1663 const unsigned char** section_contents_ptr,
1666 gold_assert(parameters->options().has_plugins());
1668 if (!parameters->options().plugins()->in_claim_file_handler())
1672 = parameters->options().plugins()->get_elf_object(section.handle);
1675 return LDPS_BAD_HANDLE;
1677 // Check if the object is locked before getting the section contents.
1678 gold_assert(obj->is_locked());
1680 section_size_type plen;
1681 *section_contents_ptr
1682 = obj->section_contents(section.shndx, &plen, false);
1687 // Specify the ordering of sections in the final layout. The sections are
1688 // specified as (handle,shndx) pairs in the two arrays in the order in
1689 // which they should appear in the final layout.
1691 static enum ld_plugin_status
1692 update_section_order(const struct ld_plugin_section* section_list,
1693 unsigned int num_sections)
1695 gold_assert(parameters->options().has_plugins());
1697 if (num_sections == 0)
1700 if (section_list == NULL)
1703 Layout* layout = parameters->options().plugins()->layout();
1704 gold_assert (layout != NULL);
1706 std::map<Section_id, unsigned int>* order_map
1707 = layout->get_section_order_map();
1709 /* Store the mapping from Section_id to section position in layout's
1710 order_map to consult after output sections are added. */
1711 for (unsigned int i = 0; i < num_sections; ++i)
1713 Object* obj = parameters->options().plugins()->get_elf_object(
1714 section_list[i].handle);
1716 return LDPS_BAD_HANDLE;
1717 unsigned int shndx = section_list[i].shndx;
1718 Section_id secn_id(obj, shndx);
1719 (*order_map)[secn_id] = i + 1;
1725 // Let the linker know that the sections could be reordered.
1727 static enum ld_plugin_status
1728 allow_section_ordering()
1730 gold_assert(parameters->options().has_plugins());
1731 Layout* layout = parameters->options().plugins()->layout();
1732 layout->set_section_ordering_specified();
1736 // Let the linker know that a subset of sections could be mapped
1737 // to a unique segment.
1739 static enum ld_plugin_status
1740 allow_unique_segment_for_sections()
1742 gold_assert(parameters->options().has_plugins());
1743 Layout* layout = parameters->options().plugins()->layout();
1744 layout->set_unique_segment_for_sections_specified();
1748 // This function should map the list of sections specified in the
1749 // SECTION_LIST to a unique segment. ELF segments do not have names
1750 // and the NAME is used to identify Output Section which should contain
1751 // the list of sections. This Output Section will then be mapped to
1752 // a unique segment. FLAGS is used to specify if any additional segment
1753 // flags need to be set. For instance, a specific segment flag can be
1754 // set to identify this segment. Unsetting segment flags is not possible.
1755 // ALIGN specifies the alignment of the segment.
1757 static enum ld_plugin_status
1758 unique_segment_for_sections(const char* segment_name,
1761 const struct ld_plugin_section* section_list,
1762 unsigned int num_sections)
1764 gold_assert(parameters->options().has_plugins());
1766 if (num_sections == 0)
1769 if (section_list == NULL)
1772 Layout* layout = parameters->options().plugins()->layout();
1773 gold_assert (layout != NULL);
1775 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
1776 s->name = segment_name;
1780 for (unsigned int i = 0; i < num_sections; ++i)
1782 Object* obj = parameters->options().plugins()->get_elf_object(
1783 section_list[i].handle);
1785 return LDPS_BAD_HANDLE;
1786 unsigned int shndx = section_list[i].shndx;
1787 Const_section_id secn_id(obj, shndx);
1788 layout->insert_section_segment_map(secn_id, s);
1794 #endif // ENABLE_PLUGINS
1796 // Allocate a Pluginobj object of the appropriate size and endianness.
1799 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1801 Pluginobj* obj = NULL;
1803 parameters_force_valid_target();
1804 const Target& target(parameters->target());
1806 if (target.get_size() == 32)
1808 if (target.is_big_endian())
1809 #ifdef HAVE_TARGET_32_BIG
1810 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1811 input_file, offset, filesize);
1813 gold_error(_("%s: not configured to support "
1814 "32-bit big-endian object"),
1815 input_file->filename().c_str());
1818 #ifdef HAVE_TARGET_32_LITTLE
1819 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1820 input_file, offset, filesize);
1822 gold_error(_("%s: not configured to support "
1823 "32-bit little-endian object"),
1824 input_file->filename().c_str());
1827 else if (target.get_size() == 64)
1829 if (target.is_big_endian())
1830 #ifdef HAVE_TARGET_64_BIG
1831 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1832 input_file, offset, filesize);
1834 gold_error(_("%s: not configured to support "
1835 "64-bit big-endian object"),
1836 input_file->filename().c_str());
1839 #ifdef HAVE_TARGET_64_LITTLE
1840 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1841 input_file, offset, filesize);
1843 gold_error(_("%s: not configured to support "
1844 "64-bit little-endian object"),
1845 input_file->filename().c_str());
1849 gold_assert(obj != NULL);
1853 } // End namespace gold.