1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2014 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"
74 #include "descriptors.h"
82 // The linker's exported interfaces.
87 static enum ld_plugin_status
88 register_claim_file(ld_plugin_claim_file_handler handler);
90 static enum ld_plugin_status
91 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
93 static enum ld_plugin_status
94 register_cleanup(ld_plugin_cleanup_handler handler);
96 static enum ld_plugin_status
97 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
99 static enum ld_plugin_status
100 get_input_file(const void *handle, struct ld_plugin_input_file *file);
102 static enum ld_plugin_status
103 get_view(const void *handle, const void **viewp);
105 static enum ld_plugin_status
106 release_input_file(const void *handle);
108 static enum ld_plugin_status
109 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
111 static enum ld_plugin_status
112 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
114 static enum ld_plugin_status
115 add_input_file(const char *pathname);
117 static enum ld_plugin_status
118 add_input_library(const char *pathname);
120 static enum ld_plugin_status
121 set_extra_library_path(const char *path);
123 static enum ld_plugin_status
124 message(int level, const char *format, ...);
126 static enum ld_plugin_status
127 get_input_section_count(const void* handle, unsigned int* count);
129 static enum ld_plugin_status
130 get_input_section_type(const struct ld_plugin_section section,
133 static enum ld_plugin_status
134 get_input_section_name(const struct ld_plugin_section section,
135 char** section_name_ptr);
137 static enum ld_plugin_status
138 get_input_section_contents(const struct ld_plugin_section section,
139 const unsigned char** section_contents,
142 static enum ld_plugin_status
143 update_section_order(const struct ld_plugin_section *section_list,
144 unsigned int num_sections);
146 static enum ld_plugin_status
147 allow_section_ordering();
149 static enum ld_plugin_status
150 allow_unique_segment_for_sections();
152 static enum ld_plugin_status
153 unique_segment_for_sections(const char* segment_name,
156 const struct ld_plugin_section *section_list,
157 unsigned int num_sections);
160 #endif // ENABLE_PLUGINS
162 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
163 off_t offset, off_t filesize);
167 // Load one plugin library.
172 #ifdef ENABLE_PLUGINS
173 // Load the plugin library.
174 // FIXME: Look for the library in standard locations.
175 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
176 if (this->handle_ == NULL)
178 gold_error(_("%s: could not load plugin library: %s"),
179 this->filename_.c_str(), dlerror());
183 // Find the plugin's onload entry point.
184 void* ptr = dlsym(this->handle_, "onload");
187 gold_error(_("%s: could not find onload entry point"),
188 this->filename_.c_str());
191 ld_plugin_onload onload;
192 gold_assert(sizeof(onload) == sizeof(ptr));
193 memcpy(&onload, &ptr, sizeof(ptr));
195 // Get the linker's version number.
196 const char* ver = get_version_string();
199 sscanf(ver, "%d.%d", &major, &minor);
201 // Allocate and populate a transfer vector.
202 const int tv_fixed_size = 26;
204 int tv_size = this->args_.size() + tv_fixed_size;
205 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
207 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
208 // while processing subsequent entries.
210 tv[i].tv_tag = LDPT_MESSAGE;
211 tv[i].tv_u.tv_message = message;
214 tv[i].tv_tag = LDPT_API_VERSION;
215 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
218 tv[i].tv_tag = LDPT_GOLD_VERSION;
219 tv[i].tv_u.tv_val = major * 100 + minor;
222 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
223 if (parameters->options().relocatable())
224 tv[i].tv_u.tv_val = LDPO_REL;
225 else if (parameters->options().shared())
226 tv[i].tv_u.tv_val = LDPO_DYN;
227 else if (parameters->options().pie())
228 tv[i].tv_u.tv_val = LDPO_PIE;
230 tv[i].tv_u.tv_val = LDPO_EXEC;
233 tv[i].tv_tag = LDPT_OUTPUT_NAME;
234 tv[i].tv_u.tv_string = parameters->options().output();
236 for (unsigned int j = 0; j < this->args_.size(); ++j)
239 tv[i].tv_tag = LDPT_OPTION;
240 tv[i].tv_u.tv_string = this->args_[j].c_str();
244 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
245 tv[i].tv_u.tv_register_claim_file = register_claim_file;
248 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
249 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
252 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
253 tv[i].tv_u.tv_register_cleanup = register_cleanup;
256 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
257 tv[i].tv_u.tv_add_symbols = add_symbols;
260 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
261 tv[i].tv_u.tv_get_input_file = get_input_file;
264 tv[i].tv_tag = LDPT_GET_VIEW;
265 tv[i].tv_u.tv_get_view = get_view;
268 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
269 tv[i].tv_u.tv_release_input_file = release_input_file;
272 tv[i].tv_tag = LDPT_GET_SYMBOLS;
273 tv[i].tv_u.tv_get_symbols = get_symbols;
276 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
277 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
280 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
281 tv[i].tv_u.tv_add_input_file = add_input_file;
284 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
285 tv[i].tv_u.tv_add_input_library = add_input_library;
288 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
289 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
292 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
293 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
296 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
297 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
300 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
301 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
304 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
305 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
308 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
309 tv[i].tv_u.tv_update_section_order = update_section_order;
312 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
313 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
316 tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
317 tv[i].tv_u.tv_allow_unique_segment_for_sections
318 = allow_unique_segment_for_sections;
321 tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
322 tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
325 tv[i].tv_tag = LDPT_NULL;
326 tv[i].tv_u.tv_val = 0;
328 gold_assert(i == tv_size - 1);
330 // Call the onload entry point.
334 #endif // ENABLE_PLUGINS
337 // Call the plugin claim-file handler.
340 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
344 if (this->claim_file_handler_ != NULL)
346 (*this->claim_file_handler_)(plugin_input_file, &claimed);
353 // Call the all-symbols-read handler.
356 Plugin::all_symbols_read()
358 if (this->all_symbols_read_handler_ != NULL)
359 (*this->all_symbols_read_handler_)();
362 // Call the cleanup handler.
367 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
369 // Set this flag before calling to prevent a recursive plunge
370 // in the event that a plugin's cleanup handler issues a
372 this->cleanup_done_ = true;
373 (*this->cleanup_handler_)();
377 // This task is used to rescan archives as needed.
379 class Plugin_rescan : public Task
382 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
383 : this_blocker_(this_blocker), next_blocker_(next_blocker)
388 delete this->this_blocker_;
394 if (this->this_blocker_->is_blocked())
395 return this->this_blocker_;
400 locks(Task_locker* tl)
401 { tl->add(this, this->next_blocker_); }
405 { parameters->options().plugins()->rescan(this); }
409 { return "Plugin_rescan"; }
412 Task_token* this_blocker_;
413 Task_token* next_blocker_;
416 // Plugin_manager methods.
418 Plugin_manager::~Plugin_manager()
420 for (Plugin_list::iterator p = this->plugins_.begin();
421 p != this->plugins_.end();
424 this->plugins_.clear();
425 for (Object_list::iterator obj = this->objects_.begin();
426 obj != this->objects_.end();
429 this->objects_.clear();
432 // Load all plugin libraries.
435 Plugin_manager::load_plugins(Layout* layout)
437 this->layout_ = layout;
438 for (this->current_ = this->plugins_.begin();
439 this->current_ != this->plugins_.end();
441 (*this->current_)->load();
444 // Call the plugin claim-file handlers in turn to see if any claim the file.
447 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
448 off_t filesize, Object* elf_object)
450 if (this->in_replacement_phase_)
453 unsigned int handle = this->objects_.size();
454 this->input_file_ = input_file;
455 this->plugin_input_file_.name = input_file->filename().c_str();
456 this->plugin_input_file_.fd = input_file->file().descriptor();
457 this->plugin_input_file_.offset = offset;
458 this->plugin_input_file_.filesize = filesize;
459 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
460 if (elf_object != NULL)
461 this->objects_.push_back(elf_object);
462 this->in_claim_file_handler_ = true;
464 for (this->current_ = this->plugins_.begin();
465 this->current_ != this->plugins_.end();
468 if ((*this->current_)->claim_file(&this->plugin_input_file_))
470 this->any_claimed_ = true;
471 this->in_claim_file_handler_ = false;
473 if (this->objects_.size() > handle
474 && this->objects_[handle]->pluginobj() != NULL)
475 return this->objects_[handle]->pluginobj();
477 // If the plugin claimed the file but did not call the
478 // add_symbols callback, we need to create the Pluginobj now.
479 Pluginobj* obj = this->make_plugin_object(handle);
484 this->in_claim_file_handler_ = false;
488 // Save an archive. This is used so that a plugin can add a file
489 // which refers to a symbol which was not previously referenced. In
490 // that case we want to pretend that the symbol was referenced before,
491 // and pull in the archive object.
494 Plugin_manager::save_archive(Archive* archive)
496 if (this->in_replacement_phase_ || !this->any_claimed_)
499 this->rescannable_.push_back(Rescannable(archive));
502 // Save an Input_group. This is like save_archive.
505 Plugin_manager::save_input_group(Input_group* input_group)
507 if (this->in_replacement_phase_ || !this->any_claimed_)
510 this->rescannable_.push_back(Rescannable(input_group));
513 // Call the all-symbols-read handlers.
516 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
517 Input_objects* input_objects,
518 Symbol_table* symtab,
519 Dirsearch* dirpath, Mapfile* mapfile,
520 Task_token** last_blocker)
522 this->in_replacement_phase_ = true;
523 this->workqueue_ = workqueue;
525 this->input_objects_ = input_objects;
526 this->symtab_ = symtab;
527 this->dirpath_ = dirpath;
528 this->mapfile_ = mapfile;
529 this->this_blocker_ = NULL;
531 for (this->current_ = this->plugins_.begin();
532 this->current_ != this->plugins_.end();
534 (*this->current_)->all_symbols_read();
536 if (this->any_added_)
538 Task_token* next_blocker = new Task_token(true);
539 next_blocker->add_blocker();
540 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
541 this->this_blocker_ = next_blocker;
544 *last_blocker = this->this_blocker_;
547 // This is called when we see a new undefined symbol. If we are in
548 // the replacement phase, this means that we may need to rescan some
549 // archives we have previously seen.
552 Plugin_manager::new_undefined_symbol(Symbol* sym)
554 if (this->in_replacement_phase_)
555 this->undefined_symbols_.push_back(sym);
558 // Rescan archives as needed. This handles the case where a new
559 // object file added by a plugin has an undefined reference to some
560 // symbol defined in an archive.
563 Plugin_manager::rescan(Task* task)
565 size_t rescan_pos = 0;
566 size_t rescan_size = this->rescannable_.size();
567 while (!this->undefined_symbols_.empty())
569 if (rescan_pos >= rescan_size)
571 this->undefined_symbols_.clear();
575 Undefined_symbol_list undefs;
576 undefs.reserve(this->undefined_symbols_.size());
577 this->undefined_symbols_.swap(undefs);
579 size_t min_rescan_pos = rescan_size;
581 for (Undefined_symbol_list::const_iterator p = undefs.begin();
585 if (!(*p)->is_undefined())
588 this->undefined_symbols_.push_back(*p);
590 // Find the first rescan archive which defines this symbol,
591 // starting at the current rescan position. The rescan position
592 // exists so that given -la -lb -lc we don't look for undefined
593 // symbols in -lb back in -la, but instead get the definition
594 // from -lc. Don't bother to look past the current minimum
596 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
598 if (this->rescannable_defines(i, *p))
606 if (min_rescan_pos >= rescan_size)
608 // We didn't find any rescannable archives which define any
609 // undefined symbols.
613 const Rescannable& r(this->rescannable_[min_rescan_pos]);
616 Task_lock_obj<Archive> tl(task, r.u.archive);
617 r.u.archive->add_symbols(this->symtab_, this->layout_,
618 this->input_objects_, this->mapfile_);
622 size_t next_saw_undefined = this->symtab_->saw_undefined();
623 size_t saw_undefined;
626 saw_undefined = next_saw_undefined;
628 for (Input_group::const_iterator p = r.u.input_group->begin();
629 p != r.u.input_group->end();
632 Task_lock_obj<Archive> tl(task, *p);
634 (*p)->add_symbols(this->symtab_, this->layout_,
635 this->input_objects_, this->mapfile_);
638 next_saw_undefined = this->symtab_->saw_undefined();
640 while (saw_undefined != next_saw_undefined);
643 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
645 if (this->rescannable_[i].is_archive)
646 delete this->rescannable_[i].u.archive;
648 delete this->rescannable_[i].u.input_group;
651 rescan_pos = min_rescan_pos + 1;
655 // Return whether the rescannable at index I defines SYM.
658 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
660 const Rescannable& r(this->rescannable_[i]);
662 return r.u.archive->defines_symbol(sym);
665 for (Input_group::const_iterator p = r.u.input_group->begin();
666 p != r.u.input_group->end();
669 if ((*p)->defines_symbol(sym))
676 // Layout deferred objects.
679 Plugin_manager::layout_deferred_objects()
681 Deferred_layout_list::iterator obj;
683 for (obj = this->deferred_layout_objects_.begin();
684 obj != this->deferred_layout_objects_.end();
687 // Lock the object so we can read from it. This is only called
688 // single-threaded from queue_middle_tasks, so it is OK to lock.
689 // Unfortunately we have no way to pass in a Task token.
690 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
691 Task_lock_obj<Object> tl(dummy_task, *obj);
692 (*obj)->layout_deferred_sections(this->layout_);
696 // Call the cleanup handlers.
699 Plugin_manager::cleanup()
701 if (this->any_added_)
703 // If any input files were added, close all the input files.
704 // This is because the plugin may want to remove them, and on
705 // Windows you are not allowed to remove an open file.
706 close_all_descriptors();
709 for (this->current_ = this->plugins_.begin();
710 this->current_ != this->plugins_.end();
712 (*this->current_)->cleanup();
715 // Make a new Pluginobj object. This is called when the plugin calls
716 // the add_symbols API.
719 Plugin_manager::make_plugin_object(unsigned int handle)
721 // Make sure we aren't asked to make an object for the same handle twice.
722 if (this->objects_.size() != handle
723 && this->objects_[handle]->pluginobj() != NULL)
726 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
727 this->plugin_input_file_.offset,
728 this->plugin_input_file_.filesize);
731 // If the elf object for this file was pushed into the objects_ vector, delete
732 // it to make room for the Pluginobj as this file is claimed.
733 if (this->objects_.size() != handle)
734 this->objects_.pop_back();
736 this->objects_.push_back(obj);
740 // Get the input file information with an open (possibly re-opened)
744 Plugin_manager::get_input_file(unsigned int handle,
745 struct ld_plugin_input_file* file)
747 Pluginobj* obj = this->object(handle)->pluginobj();
749 return LDPS_BAD_HANDLE;
751 obj->lock(this->task_);
752 file->name = obj->filename().c_str();
753 file->fd = obj->descriptor();
754 file->offset = obj->offset();
755 file->filesize = obj->filesize();
756 file->handle = reinterpret_cast<void*>(handle);
760 // Release the input file.
763 Plugin_manager::release_input_file(unsigned int handle)
765 if (this->object(handle) == NULL)
766 return LDPS_BAD_HANDLE;
768 Pluginobj* obj = this->object(handle)->pluginobj();
771 return LDPS_BAD_HANDLE;
773 obj->unlock(this->task_);
777 // Get the elf object corresponding to the handle. Return NULL if we
778 // found a Pluginobj instead.
781 Plugin_manager::get_elf_object(const void* handle)
783 Object* obj = this->object(
784 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
786 // The object should not be a Pluginobj.
788 || obj->pluginobj() != NULL)
795 Plugin_manager::get_view(unsigned int handle, const void **viewp)
799 Input_file *input_file;
800 if (this->in_claim_file_handler_)
802 // We are being called from the claim_file hook.
803 const struct ld_plugin_input_file &f = this->plugin_input_file_;
805 filesize = f.filesize;
806 input_file = this->input_file_;
810 // An already claimed file.
811 if (this->object(handle) == NULL)
812 return LDPS_BAD_HANDLE;
813 Pluginobj* obj = this->object(handle)->pluginobj();
815 return LDPS_BAD_HANDLE;
816 offset = obj->offset();
817 filesize = obj->filesize();
818 input_file = obj->input_file();
820 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
825 // Add a new library path.
828 Plugin_manager::set_extra_library_path(const char* path)
830 this->extra_search_path_ = std::string(path);
834 // Add a new input file.
837 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
839 Input_file_argument file(pathname,
841 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
842 : Input_file_argument::INPUT_FILE_TYPE_FILE),
844 ? this->extra_search_path_.c_str()
848 Input_argument* input_argument = new Input_argument(file);
849 Task_token* next_blocker = new Task_token(true);
850 next_blocker->add_blocker();
851 if (parameters->incremental())
852 gold_error(_("input files added by plug-ins in --incremental mode not "
854 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
865 this->this_blocker_ = next_blocker;
866 this->any_added_ = true;
872 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
873 off_t offset, off_t filesize)
874 : Object(name, input_file, false, offset),
875 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
879 // Return TRUE if a defined symbol is referenced from outside the
880 // universe of claimed objects. Only references from relocatable,
881 // non-IR (unclaimed) objects count as a reference. References from
882 // dynamic objects count only as "visible".
885 is_referenced_from_outside(Symbol* lsym)
887 if (lsym->in_real_elf())
889 if (parameters->options().relocatable())
891 if (parameters->options().is_undefined(lsym->name()))
896 // Return TRUE if a defined symbol might be reachable from outside the
900 is_visible_from_outside(Symbol* lsym)
904 if (parameters->options().export_dynamic() || parameters->options().shared())
905 return lsym->is_externally_visible();
909 // Get symbol resolution info.
912 Pluginobj::get_symbol_resolution_info(int nsyms,
913 ld_plugin_symbol* syms,
916 // For version 1 of this interface, we cannot use
917 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
919 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
921 ? LDPR_PREVAILING_DEF_IRONLY_EXP
922 : LDPR_PREVAILING_DEF);
924 if (nsyms > this->nsyms_)
927 if (static_cast<size_t>(nsyms) > this->symbols_.size())
929 // We never decided to include this object. We mark all symbols as
931 gold_assert(this->symbols_.size() == 0);
932 for (int i = 0; i < nsyms; i++)
933 syms[i].resolution = LDPR_PREEMPTED_REG;
937 for (int i = 0; i < nsyms; i++)
939 ld_plugin_symbol* isym = &syms[i];
940 Symbol* lsym = this->symbols_[i];
941 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
943 if (lsym->is_undefined())
944 // The symbol remains undefined.
946 else if (isym->def == LDPK_UNDEF
947 || isym->def == LDPK_WEAKUNDEF
948 || isym->def == LDPK_COMMON)
950 // The original symbol was undefined or common.
951 if (lsym->source() != Symbol::FROM_OBJECT)
952 res = LDPR_RESOLVED_EXEC;
953 else if (lsym->object()->pluginobj() == this)
955 if (is_referenced_from_outside(lsym))
956 res = LDPR_PREVAILING_DEF;
957 else if (is_visible_from_outside(lsym))
958 res = ldpr_prevailing_def_ironly_exp;
960 res = LDPR_PREVAILING_DEF_IRONLY;
962 else if (lsym->object()->pluginobj() != NULL)
963 res = LDPR_RESOLVED_IR;
964 else if (lsym->object()->is_dynamic())
965 res = LDPR_RESOLVED_DYN;
967 res = LDPR_RESOLVED_EXEC;
971 // The original symbol was a definition.
972 if (lsym->source() != Symbol::FROM_OBJECT)
973 res = LDPR_PREEMPTED_REG;
974 else if (lsym->object() == static_cast<const Object*>(this))
976 if (is_referenced_from_outside(lsym))
977 res = LDPR_PREVAILING_DEF;
978 else if (is_visible_from_outside(lsym))
979 res = ldpr_prevailing_def_ironly_exp;
981 res = LDPR_PREVAILING_DEF_IRONLY;
984 res = (lsym->object()->pluginobj() != NULL
986 : LDPR_PREEMPTED_REG);
988 isym->resolution = res;
993 // Return TRUE if the comdat group with key COMDAT_KEY from this object
997 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
999 std::pair<Comdat_map::iterator, bool> ins =
1000 this->comdat_map_.insert(std::make_pair(comdat_key, false));
1002 // If this is the first time we've seen this comdat key, ask the
1003 // layout object whether it should be included.
1005 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1009 return ins.first->second;
1012 // Class Sized_pluginobj.
1014 template<int size, bool big_endian>
1015 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1016 const std::string& name,
1017 Input_file* input_file,
1020 : Pluginobj(name, input_file, offset, filesize)
1024 // Read the symbols. Not used for plugin objects.
1026 template<int size, bool big_endian>
1028 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1033 // Lay out the input sections. Not used for plugin objects.
1035 template<int size, bool big_endian>
1037 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1043 // Add the symbols to the symbol table.
1045 template<int size, bool big_endian>
1047 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1051 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1052 unsigned char symbuf[sym_size];
1053 elfcpp::Sym<size, big_endian> sym(symbuf);
1054 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1056 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
1058 this->symbols_.resize(this->nsyms_);
1060 for (int i = 0; i < this->nsyms_; ++i)
1062 const struct ld_plugin_symbol* isym = &this->syms_[i];
1063 const char* name = isym->name;
1064 const char* ver = isym->version;
1065 elfcpp::Elf_Half shndx;
1069 if (name != NULL && name[0] == '\0')
1071 if (ver != NULL && ver[0] == '\0')
1077 case LDPK_WEAKUNDEF:
1078 bind = elfcpp::STB_WEAK;
1084 bind = elfcpp::STB_GLOBAL;
1092 shndx = elfcpp::SHN_ABS;
1095 shndx = elfcpp::SHN_COMMON;
1098 case LDPK_WEAKUNDEF:
1100 shndx = elfcpp::SHN_UNDEF;
1104 switch (isym->visibility)
1106 case LDPV_PROTECTED:
1107 vis = elfcpp::STV_PROTECTED;
1110 vis = elfcpp::STV_INTERNAL;
1113 vis = elfcpp::STV_HIDDEN;
1117 vis = elfcpp::STV_DEFAULT;
1121 if (isym->comdat_key != NULL
1122 && isym->comdat_key[0] != '\0'
1123 && !this->include_comdat_group(isym->comdat_key, layout))
1124 shndx = elfcpp::SHN_UNDEF;
1126 osym.put_st_name(0);
1127 osym.put_st_value(0);
1128 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
1129 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1130 osym.put_st_other(vis, 0);
1131 osym.put_st_shndx(shndx);
1134 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1138 template<int size, bool big_endian>
1139 Archive::Should_include
1140 Sized_pluginobj<size, big_endian>::do_should_include_member(
1141 Symbol_table* symtab,
1146 char* tmpbuf = NULL;
1147 size_t tmpbuflen = 0;
1149 for (int i = 0; i < this->nsyms_; ++i)
1151 const struct ld_plugin_symbol& sym = this->syms_[i];
1152 const char* name = sym.name;
1154 Archive::Should_include t = Archive::should_include_member(symtab,
1160 if (t == Archive::SHOULD_INCLUDE_YES)
1169 return Archive::SHOULD_INCLUDE_UNKNOWN;
1172 // Iterate over global symbols, calling a visitor class V for each.
1174 template<int size, bool big_endian>
1176 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1178 Library_base::Symbol_visitor_base* v)
1180 for (int i = 0; i < this->nsyms_; ++i)
1182 const struct ld_plugin_symbol& sym = this->syms_[i];
1183 if (sym.def != LDPK_UNDEF)
1188 // Iterate over local symbols, calling a visitor class V for each GOT offset
1189 // associated with a local symbol.
1190 template<int size, bool big_endian>
1192 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1193 Got_offset_list::Visitor*) const
1198 // Get the size of a section. Not used for plugin objects.
1200 template<int size, bool big_endian>
1202 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1208 // Get the name of a section. Not used for plugin objects.
1210 template<int size, bool big_endian>
1212 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
1215 return std::string();
1218 // Return a view of the contents of a section. Not used for plugin objects.
1220 template<int size, bool big_endian>
1221 const unsigned char*
1222 Sized_pluginobj<size, big_endian>::do_section_contents(
1231 // Return section flags. Not used for plugin objects.
1233 template<int size, bool big_endian>
1235 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1241 // Return section entsize. Not used for plugin objects.
1243 template<int size, bool big_endian>
1245 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1251 // Return section address. Not used for plugin objects.
1253 template<int size, bool big_endian>
1255 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1261 // Return section type. Not used for plugin objects.
1263 template<int size, bool big_endian>
1265 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1271 // Return the section link field. Not used for plugin objects.
1273 template<int size, bool big_endian>
1275 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1281 // Return the section link field. Not used for plugin objects.
1283 template<int size, bool big_endian>
1285 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1291 // Return the section alignment. Not used for plugin objects.
1293 template<int size, bool big_endian>
1295 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1301 // Return the Xindex structure to use. Not used for plugin objects.
1303 template<int size, bool big_endian>
1305 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1311 // Get symbol counts. Don't count plugin objects; the replacement
1312 // files will provide the counts.
1314 template<int size, bool big_endian>
1316 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1317 const Symbol_table*,
1325 // Get symbols. Not used for plugin objects.
1327 template<int size, bool big_endian>
1328 const Object::Symbols*
1329 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1334 // Class Plugin_finish. This task runs after all replacement files have
1335 // been added. For now, it's a placeholder for a possible plugin API
1336 // to allow the plugin to release most of its resources. The cleanup
1337 // handlers must be called later, because they can remove the temporary
1338 // object files that are needed until the end of the link.
1340 class Plugin_finish : public Task
1343 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1344 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1349 if (this->this_blocker_ != NULL)
1350 delete this->this_blocker_;
1356 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1357 return this->this_blocker_;
1362 locks(Task_locker* tl)
1363 { tl->add(this, this->next_blocker_); }
1368 // We could call early cleanup handlers here.
1373 { return "Plugin_finish"; }
1376 Task_token* this_blocker_;
1377 Task_token* next_blocker_;
1380 // Class Plugin_hook.
1382 Plugin_hook::~Plugin_hook()
1386 // Return whether a Plugin_hook task is runnable.
1389 Plugin_hook::is_runnable()
1391 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1392 return this->this_blocker_;
1396 // Return a Task_locker for a Plugin_hook task. We don't need any
1400 Plugin_hook::locks(Task_locker*)
1404 // Run the "all symbols read" plugin hook.
1407 Plugin_hook::run(Workqueue* workqueue)
1409 gold_assert(this->options_.has_plugins());
1410 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1411 if (start_sym != NULL)
1412 start_sym->set_in_real_elf();
1414 this->options_.plugins()->all_symbols_read(workqueue,
1416 this->input_objects_,
1420 &this->this_blocker_);
1421 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1422 this->next_blocker_));
1425 // The C interface routines called by the plugins.
1427 #ifdef ENABLE_PLUGINS
1429 // Register a claim-file handler.
1431 static enum ld_plugin_status
1432 register_claim_file(ld_plugin_claim_file_handler handler)
1434 gold_assert(parameters->options().has_plugins());
1435 parameters->options().plugins()->set_claim_file_handler(handler);
1439 // Register an all-symbols-read handler.
1441 static enum ld_plugin_status
1442 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1444 gold_assert(parameters->options().has_plugins());
1445 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1449 // Register a cleanup handler.
1451 static enum ld_plugin_status
1452 register_cleanup(ld_plugin_cleanup_handler handler)
1454 gold_assert(parameters->options().has_plugins());
1455 parameters->options().plugins()->set_cleanup_handler(handler);
1459 // Add symbols from a plugin-claimed input file.
1461 static enum ld_plugin_status
1462 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1464 gold_assert(parameters->options().has_plugins());
1465 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1466 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1469 obj->store_incoming_symbols(nsyms, syms);
1473 // Get the input file information with an open (possibly re-opened)
1476 static enum ld_plugin_status
1477 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1479 gold_assert(parameters->options().has_plugins());
1480 unsigned int obj_index =
1481 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1482 return parameters->options().plugins()->get_input_file(obj_index, file);
1485 // Release the input file.
1487 static enum ld_plugin_status
1488 release_input_file(const void* handle)
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()->release_input_file(obj_index);
1496 static enum ld_plugin_status
1497 get_view(const void *handle, const void **viewp)
1499 gold_assert(parameters->options().has_plugins());
1500 unsigned int obj_index =
1501 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1502 return parameters->options().plugins()->get_view(obj_index, viewp);
1505 // Get the symbol resolution info for a plugin-claimed input file.
1507 static enum ld_plugin_status
1508 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1510 gold_assert(parameters->options().has_plugins());
1511 Object* obj = parameters->options().plugins()->object(
1512 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1515 Pluginobj* plugin_obj = obj->pluginobj();
1516 if (plugin_obj == NULL)
1518 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 1);
1521 // Version 2 of the above. The only difference is that this version
1522 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1524 static enum ld_plugin_status
1525 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1527 gold_assert(parameters->options().has_plugins());
1528 Object* obj = parameters->options().plugins()->object(
1529 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1532 Pluginobj* plugin_obj = obj->pluginobj();
1533 if (plugin_obj == NULL)
1535 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 2);
1538 // Add a new (real) input file generated by a plugin.
1540 static enum ld_plugin_status
1541 add_input_file(const char* pathname)
1543 gold_assert(parameters->options().has_plugins());
1544 return parameters->options().plugins()->add_input_file(pathname, false);
1547 // Add a new (real) library required by a plugin.
1549 static enum ld_plugin_status
1550 add_input_library(const char* pathname)
1552 gold_assert(parameters->options().has_plugins());
1553 return parameters->options().plugins()->add_input_file(pathname, true);
1556 // Set the extra library path to be used by libraries added via
1557 // add_input_library
1559 static enum ld_plugin_status
1560 set_extra_library_path(const char* path)
1562 gold_assert(parameters->options().has_plugins());
1563 return parameters->options().plugins()->set_extra_library_path(path);
1566 // Issue a diagnostic message from a plugin.
1568 static enum ld_plugin_status
1569 message(int level, const char* format, ...)
1572 va_start(args, format);
1577 parameters->errors()->info(format, args);
1580 parameters->errors()->warning(format, args);
1584 parameters->errors()->error(format, args);
1587 parameters->errors()->fatal(format, args);
1595 // Get the section count of the object corresponding to the handle. This
1596 // plugin interface can only be called in the claim_file handler of the plugin.
1598 static enum ld_plugin_status
1599 get_input_section_count(const void* handle, unsigned int* count)
1601 gold_assert(parameters->options().has_plugins());
1603 if (!parameters->options().plugins()->in_claim_file_handler())
1606 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1611 *count = obj->shnum();
1615 // Get the type of the specified section in the object corresponding
1616 // to the handle. This plugin interface can only be called in the
1617 // claim_file handler of the plugin.
1619 static enum ld_plugin_status
1620 get_input_section_type(const struct ld_plugin_section section,
1623 gold_assert(parameters->options().has_plugins());
1625 if (!parameters->options().plugins()->in_claim_file_handler())
1629 = parameters->options().plugins()->get_elf_object(section.handle);
1632 return LDPS_BAD_HANDLE;
1634 *type = obj->section_type(section.shndx);
1638 // Get the name of the specified section in the object corresponding
1639 // to the handle. This plugin interface can only be called in the
1640 // claim_file handler of the plugin.
1642 static enum ld_plugin_status
1643 get_input_section_name(const struct ld_plugin_section section,
1644 char** section_name_ptr)
1646 gold_assert(parameters->options().has_plugins());
1648 if (!parameters->options().plugins()->in_claim_file_handler())
1652 = parameters->options().plugins()->get_elf_object(section.handle);
1655 return LDPS_BAD_HANDLE;
1657 // Check if the object is locked before getting the section name.
1658 gold_assert(obj->is_locked());
1660 const std::string section_name = obj->section_name(section.shndx);
1661 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1662 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1666 // Get the contents of the specified section in the object corresponding
1667 // to the handle. This plugin interface can only be called in the
1668 // claim_file handler of the plugin.
1670 static enum ld_plugin_status
1671 get_input_section_contents(const struct ld_plugin_section section,
1672 const unsigned char** section_contents_ptr,
1675 gold_assert(parameters->options().has_plugins());
1677 if (!parameters->options().plugins()->in_claim_file_handler())
1681 = parameters->options().plugins()->get_elf_object(section.handle);
1684 return LDPS_BAD_HANDLE;
1686 // Check if the object is locked before getting the section contents.
1687 gold_assert(obj->is_locked());
1689 section_size_type plen;
1690 *section_contents_ptr
1691 = obj->section_contents(section.shndx, &plen, false);
1696 // Specify the ordering of sections in the final layout. The sections are
1697 // specified as (handle,shndx) pairs in the two arrays in the order in
1698 // which they should appear in the final layout.
1700 static enum ld_plugin_status
1701 update_section_order(const struct ld_plugin_section* section_list,
1702 unsigned int num_sections)
1704 gold_assert(parameters->options().has_plugins());
1706 if (num_sections == 0)
1709 if (section_list == NULL)
1712 Layout* layout = parameters->options().plugins()->layout();
1713 gold_assert (layout != NULL);
1715 std::map<Section_id, unsigned int>* order_map
1716 = layout->get_section_order_map();
1718 /* Store the mapping from Section_id to section position in layout's
1719 order_map to consult after output sections are added. */
1720 for (unsigned int i = 0; i < num_sections; ++i)
1722 Object* obj = parameters->options().plugins()->get_elf_object(
1723 section_list[i].handle);
1725 return LDPS_BAD_HANDLE;
1726 unsigned int shndx = section_list[i].shndx;
1727 Section_id secn_id(obj, shndx);
1728 (*order_map)[secn_id] = i + 1;
1734 // Let the linker know that the sections could be reordered.
1736 static enum ld_plugin_status
1737 allow_section_ordering()
1739 gold_assert(parameters->options().has_plugins());
1740 Layout* layout = parameters->options().plugins()->layout();
1741 layout->set_section_ordering_specified();
1745 // Let the linker know that a subset of sections could be mapped
1746 // to a unique segment.
1748 static enum ld_plugin_status
1749 allow_unique_segment_for_sections()
1751 gold_assert(parameters->options().has_plugins());
1752 Layout* layout = parameters->options().plugins()->layout();
1753 layout->set_unique_segment_for_sections_specified();
1757 // This function should map the list of sections specified in the
1758 // SECTION_LIST to a unique segment. ELF segments do not have names
1759 // and the NAME is used to identify Output Section which should contain
1760 // the list of sections. This Output Section will then be mapped to
1761 // a unique segment. FLAGS is used to specify if any additional segment
1762 // flags need to be set. For instance, a specific segment flag can be
1763 // set to identify this segment. Unsetting segment flags is not possible.
1764 // ALIGN specifies the alignment of the segment.
1766 static enum ld_plugin_status
1767 unique_segment_for_sections(const char* segment_name,
1770 const struct ld_plugin_section* section_list,
1771 unsigned int num_sections)
1773 gold_assert(parameters->options().has_plugins());
1775 if (num_sections == 0)
1778 if (section_list == NULL)
1781 Layout* layout = parameters->options().plugins()->layout();
1782 gold_assert (layout != NULL);
1784 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
1785 s->name = segment_name;
1789 for (unsigned int i = 0; i < num_sections; ++i)
1791 Object* obj = parameters->options().plugins()->get_elf_object(
1792 section_list[i].handle);
1794 return LDPS_BAD_HANDLE;
1795 unsigned int shndx = section_list[i].shndx;
1796 Const_section_id secn_id(obj, shndx);
1797 layout->insert_section_segment_map(secn_id, s);
1803 #endif // ENABLE_PLUGINS
1805 // Allocate a Pluginobj object of the appropriate size and endianness.
1808 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1810 Pluginobj* obj = NULL;
1812 parameters_force_valid_target();
1813 const Target& target(parameters->target());
1815 if (target.get_size() == 32)
1817 if (target.is_big_endian())
1818 #ifdef HAVE_TARGET_32_BIG
1819 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1820 input_file, offset, filesize);
1822 gold_error(_("%s: not configured to support "
1823 "32-bit big-endian object"),
1824 input_file->filename().c_str());
1827 #ifdef HAVE_TARGET_32_LITTLE
1828 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1829 input_file, offset, filesize);
1831 gold_error(_("%s: not configured to support "
1832 "32-bit little-endian object"),
1833 input_file->filename().c_str());
1836 else if (target.get_size() == 64)
1838 if (target.is_big_endian())
1839 #ifdef HAVE_TARGET_64_BIG
1840 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1841 input_file, offset, filesize);
1843 gold_error(_("%s: not configured to support "
1844 "64-bit big-endian object"),
1845 input_file->filename().c_str());
1848 #ifdef HAVE_TARGET_64_LITTLE
1849 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1850 input_file, offset, filesize);
1852 gold_error(_("%s: not configured to support "
1853 "64-bit little-endian object"),
1854 input_file->filename().c_str());
1858 gold_assert(obj != NULL);
1862 } // End namespace gold.