1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2015 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();
433 // Load all plugin libraries.
436 Plugin_manager::load_plugins(Layout* layout)
438 this->layout_ = layout;
439 for (this->current_ = this->plugins_.begin();
440 this->current_ != this->plugins_.end();
442 (*this->current_)->load();
445 // Call the plugin claim-file handlers in turn to see if any claim the file.
448 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
449 off_t filesize, Object* elf_object)
451 bool lock_initialized = this->initialize_lock_.initialize();
453 gold_assert(lock_initialized);
454 Hold_lock hl(*this->lock_);
455 if (this->in_replacement_phase_)
458 unsigned int handle = this->objects_.size();
459 this->input_file_ = input_file;
460 this->plugin_input_file_.name = input_file->filename().c_str();
461 this->plugin_input_file_.fd = input_file->file().descriptor();
462 this->plugin_input_file_.offset = offset;
463 this->plugin_input_file_.filesize = filesize;
464 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
465 if (elf_object != NULL)
466 this->objects_.push_back(elf_object);
467 this->in_claim_file_handler_ = true;
469 for (this->current_ = this->plugins_.begin();
470 this->current_ != this->plugins_.end();
473 if ((*this->current_)->claim_file(&this->plugin_input_file_))
475 this->any_claimed_ = true;
476 this->in_claim_file_handler_ = false;
478 if (this->objects_.size() > handle
479 && this->objects_[handle]->pluginobj() != NULL)
480 return this->objects_[handle]->pluginobj();
482 // If the plugin claimed the file but did not call the
483 // add_symbols callback, we need to create the Pluginobj now.
484 Pluginobj* obj = this->make_plugin_object(handle);
489 this->in_claim_file_handler_ = false;
493 // Save an archive. This is used so that a plugin can add a file
494 // which refers to a symbol which was not previously referenced. In
495 // that case we want to pretend that the symbol was referenced before,
496 // and pull in the archive object.
499 Plugin_manager::save_archive(Archive* archive)
501 if (this->in_replacement_phase_ || !this->any_claimed_)
504 this->rescannable_.push_back(Rescannable(archive));
507 // Save an Input_group. This is like save_archive.
510 Plugin_manager::save_input_group(Input_group* input_group)
512 if (this->in_replacement_phase_ || !this->any_claimed_)
515 this->rescannable_.push_back(Rescannable(input_group));
518 // Call the all-symbols-read handlers.
521 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
522 Input_objects* input_objects,
523 Symbol_table* symtab,
524 Dirsearch* dirpath, Mapfile* mapfile,
525 Task_token** last_blocker)
527 this->in_replacement_phase_ = true;
528 this->workqueue_ = workqueue;
530 this->input_objects_ = input_objects;
531 this->symtab_ = symtab;
532 this->dirpath_ = dirpath;
533 this->mapfile_ = mapfile;
534 this->this_blocker_ = NULL;
536 for (this->current_ = this->plugins_.begin();
537 this->current_ != this->plugins_.end();
539 (*this->current_)->all_symbols_read();
541 if (this->any_added_)
543 Task_token* next_blocker = new Task_token(true);
544 next_blocker->add_blocker();
545 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
546 this->this_blocker_ = next_blocker;
549 *last_blocker = this->this_blocker_;
552 // This is called when we see a new undefined symbol. If we are in
553 // the replacement phase, this means that we may need to rescan some
554 // archives we have previously seen.
557 Plugin_manager::new_undefined_symbol(Symbol* sym)
559 if (this->in_replacement_phase_)
560 this->undefined_symbols_.push_back(sym);
563 // Rescan archives as needed. This handles the case where a new
564 // object file added by a plugin has an undefined reference to some
565 // symbol defined in an archive.
568 Plugin_manager::rescan(Task* task)
570 size_t rescan_pos = 0;
571 size_t rescan_size = this->rescannable_.size();
572 while (!this->undefined_symbols_.empty())
574 if (rescan_pos >= rescan_size)
576 this->undefined_symbols_.clear();
580 Undefined_symbol_list undefs;
581 undefs.reserve(this->undefined_symbols_.size());
582 this->undefined_symbols_.swap(undefs);
584 size_t min_rescan_pos = rescan_size;
586 for (Undefined_symbol_list::const_iterator p = undefs.begin();
590 if (!(*p)->is_undefined())
593 this->undefined_symbols_.push_back(*p);
595 // Find the first rescan archive which defines this symbol,
596 // starting at the current rescan position. The rescan position
597 // exists so that given -la -lb -lc we don't look for undefined
598 // symbols in -lb back in -la, but instead get the definition
599 // from -lc. Don't bother to look past the current minimum
601 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
603 if (this->rescannable_defines(i, *p))
611 if (min_rescan_pos >= rescan_size)
613 // We didn't find any rescannable archives which define any
614 // undefined symbols.
618 const Rescannable& r(this->rescannable_[min_rescan_pos]);
621 Task_lock_obj<Archive> tl(task, r.u.archive);
622 r.u.archive->add_symbols(this->symtab_, this->layout_,
623 this->input_objects_, this->mapfile_);
627 size_t next_saw_undefined = this->symtab_->saw_undefined();
628 size_t saw_undefined;
631 saw_undefined = next_saw_undefined;
633 for (Input_group::const_iterator p = r.u.input_group->begin();
634 p != r.u.input_group->end();
637 Task_lock_obj<Archive> tl(task, *p);
639 (*p)->add_symbols(this->symtab_, this->layout_,
640 this->input_objects_, this->mapfile_);
643 next_saw_undefined = this->symtab_->saw_undefined();
645 while (saw_undefined != next_saw_undefined);
648 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
650 if (this->rescannable_[i].is_archive)
651 delete this->rescannable_[i].u.archive;
653 delete this->rescannable_[i].u.input_group;
656 rescan_pos = min_rescan_pos + 1;
660 // Return whether the rescannable at index I defines SYM.
663 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
665 const Rescannable& r(this->rescannable_[i]);
667 return r.u.archive->defines_symbol(sym);
670 for (Input_group::const_iterator p = r.u.input_group->begin();
671 p != r.u.input_group->end();
674 if ((*p)->defines_symbol(sym))
681 // Layout deferred objects.
684 Plugin_manager::layout_deferred_objects()
686 Deferred_layout_list::iterator obj;
688 for (obj = this->deferred_layout_objects_.begin();
689 obj != this->deferred_layout_objects_.end();
692 // Lock the object so we can read from it. This is only called
693 // single-threaded from queue_middle_tasks, so it is OK to lock.
694 // Unfortunately we have no way to pass in a Task token.
695 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
696 Task_lock_obj<Object> tl(dummy_task, *obj);
697 (*obj)->layout_deferred_sections(this->layout_);
701 // Call the cleanup handlers.
704 Plugin_manager::cleanup()
706 if (this->any_added_)
708 // If any input files were added, close all the input files.
709 // This is because the plugin may want to remove them, and on
710 // Windows you are not allowed to remove an open file.
711 close_all_descriptors();
714 for (this->current_ = this->plugins_.begin();
715 this->current_ != this->plugins_.end();
717 (*this->current_)->cleanup();
720 // Make a new Pluginobj object. This is called when the plugin calls
721 // the add_symbols API.
724 Plugin_manager::make_plugin_object(unsigned int handle)
726 // Make sure we aren't asked to make an object for the same handle twice.
727 if (this->objects_.size() != handle
728 && this->objects_[handle]->pluginobj() != NULL)
731 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
732 this->plugin_input_file_.offset,
733 this->plugin_input_file_.filesize);
736 // If the elf object for this file was pushed into the objects_ vector, delete
737 // it to make room for the Pluginobj as this file is claimed.
738 if (this->objects_.size() != handle)
739 this->objects_.pop_back();
741 this->objects_.push_back(obj);
745 // Get the input file information with an open (possibly re-opened)
749 Plugin_manager::get_input_file(unsigned int handle,
750 struct ld_plugin_input_file* file)
752 Pluginobj* obj = this->object(handle)->pluginobj();
754 return LDPS_BAD_HANDLE;
756 obj->lock(this->task_);
757 file->name = obj->filename().c_str();
758 file->fd = obj->descriptor();
759 file->offset = obj->offset();
760 file->filesize = obj->filesize();
761 file->handle = reinterpret_cast<void*>(handle);
765 // Release the input file.
768 Plugin_manager::release_input_file(unsigned int handle)
770 if (this->object(handle) == NULL)
771 return LDPS_BAD_HANDLE;
773 Pluginobj* obj = this->object(handle)->pluginobj();
776 return LDPS_BAD_HANDLE;
778 obj->unlock(this->task_);
782 // Get the elf object corresponding to the handle. Return NULL if we
783 // found a Pluginobj instead.
786 Plugin_manager::get_elf_object(const void* handle)
788 Object* obj = this->object(
789 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
791 // The object should not be a Pluginobj.
793 || obj->pluginobj() != NULL)
800 Plugin_manager::get_view(unsigned int handle, const void **viewp)
804 Input_file *input_file;
805 if (this->in_claim_file_handler_)
807 // We are being called from the claim_file hook.
808 const struct ld_plugin_input_file &f = this->plugin_input_file_;
810 filesize = f.filesize;
811 input_file = this->input_file_;
815 // An already claimed file.
816 if (this->object(handle) == NULL)
817 return LDPS_BAD_HANDLE;
818 Pluginobj* obj = this->object(handle)->pluginobj();
820 return LDPS_BAD_HANDLE;
821 offset = obj->offset();
822 filesize = obj->filesize();
823 input_file = obj->input_file();
825 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
830 // Add a new library path.
833 Plugin_manager::set_extra_library_path(const char* path)
835 this->extra_search_path_ = std::string(path);
839 // Add a new input file.
842 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
844 Input_file_argument file(pathname,
846 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
847 : Input_file_argument::INPUT_FILE_TYPE_FILE),
849 ? this->extra_search_path_.c_str()
853 Input_argument* input_argument = new Input_argument(file);
854 Task_token* next_blocker = new Task_token(true);
855 next_blocker->add_blocker();
856 if (parameters->incremental())
857 gold_error(_("input files added by plug-ins in --incremental mode not "
859 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
870 this->this_blocker_ = next_blocker;
871 this->any_added_ = true;
877 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
878 off_t offset, off_t filesize)
879 : Object(name, input_file, false, offset),
880 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
884 // Return TRUE if a defined symbol is referenced from outside the
885 // universe of claimed objects. Only references from relocatable,
886 // non-IR (unclaimed) objects count as a reference. References from
887 // dynamic objects count only as "visible".
890 is_referenced_from_outside(Symbol* lsym)
892 if (lsym->in_real_elf())
894 if (parameters->options().relocatable())
896 if (parameters->options().is_undefined(lsym->name()))
901 // Return TRUE if a defined symbol might be reachable from outside the
905 is_visible_from_outside(Symbol* lsym)
909 if (parameters->options().export_dynamic() || parameters->options().shared())
910 return lsym->is_externally_visible();
914 // Get symbol resolution info.
917 Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
919 ld_plugin_symbol* syms,
922 // For version 1 of this interface, we cannot use
923 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
925 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
927 ? LDPR_PREVAILING_DEF_IRONLY_EXP
928 : LDPR_PREVAILING_DEF);
930 if (nsyms > this->nsyms_)
933 if (static_cast<size_t>(nsyms) > this->symbols_.size())
935 // We never decided to include this object. We mark all symbols as
937 gold_assert(this->symbols_.size() == 0);
938 for (int i = 0; i < nsyms; i++)
939 syms[i].resolution = LDPR_PREEMPTED_REG;
943 for (int i = 0; i < nsyms; i++)
945 ld_plugin_symbol* isym = &syms[i];
946 Symbol* lsym = this->symbols_[i];
947 if (lsym->is_forwarder())
948 lsym = symtab->resolve_forwards(lsym);
949 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
951 if (lsym->is_undefined())
952 // The symbol remains undefined.
954 else if (isym->def == LDPK_UNDEF
955 || isym->def == LDPK_WEAKUNDEF
956 || isym->def == LDPK_COMMON)
958 // The original symbol was undefined or common.
959 if (lsym->source() != Symbol::FROM_OBJECT)
960 res = LDPR_RESOLVED_EXEC;
961 else if (lsym->object()->pluginobj() == this)
963 if (is_referenced_from_outside(lsym))
964 res = LDPR_PREVAILING_DEF;
965 else if (is_visible_from_outside(lsym))
966 res = ldpr_prevailing_def_ironly_exp;
968 res = LDPR_PREVAILING_DEF_IRONLY;
970 else if (lsym->object()->pluginobj() != NULL)
971 res = LDPR_RESOLVED_IR;
972 else if (lsym->object()->is_dynamic())
973 res = LDPR_RESOLVED_DYN;
975 res = LDPR_RESOLVED_EXEC;
979 // The original symbol was a definition.
980 if (lsym->source() != Symbol::FROM_OBJECT)
981 res = LDPR_PREEMPTED_REG;
982 else if (lsym->object() == static_cast<const Object*>(this))
984 if (is_referenced_from_outside(lsym))
985 res = LDPR_PREVAILING_DEF;
986 else if (is_visible_from_outside(lsym))
987 res = ldpr_prevailing_def_ironly_exp;
989 res = LDPR_PREVAILING_DEF_IRONLY;
992 res = (lsym->object()->pluginobj() != NULL
994 : LDPR_PREEMPTED_REG);
996 isym->resolution = res;
1001 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1005 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
1007 std::pair<Comdat_map::iterator, bool> ins =
1008 this->comdat_map_.insert(std::make_pair(comdat_key, false));
1010 // If this is the first time we've seen this comdat key, ask the
1011 // layout object whether it should be included.
1013 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1017 return ins.first->second;
1020 // Class Sized_pluginobj.
1022 template<int size, bool big_endian>
1023 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1024 const std::string& name,
1025 Input_file* input_file,
1028 : Pluginobj(name, input_file, offset, filesize)
1032 // Read the symbols. Not used for plugin objects.
1034 template<int size, bool big_endian>
1036 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1041 // Lay out the input sections. Not used for plugin objects.
1043 template<int size, bool big_endian>
1045 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1051 // Add the symbols to the symbol table.
1053 template<int size, bool big_endian>
1055 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1059 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1060 unsigned char symbuf[sym_size];
1061 elfcpp::Sym<size, big_endian> sym(symbuf);
1062 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1064 this->symbols_.resize(this->nsyms_);
1066 for (int i = 0; i < this->nsyms_; ++i)
1068 const struct ld_plugin_symbol* isym = &this->syms_[i];
1069 const char* name = isym->name;
1070 const char* ver = isym->version;
1071 elfcpp::Elf_Half shndx;
1075 if (name != NULL && name[0] == '\0')
1077 if (ver != NULL && ver[0] == '\0')
1083 case LDPK_WEAKUNDEF:
1084 bind = elfcpp::STB_WEAK;
1090 bind = elfcpp::STB_GLOBAL;
1098 shndx = elfcpp::SHN_ABS;
1101 shndx = elfcpp::SHN_COMMON;
1104 case LDPK_WEAKUNDEF:
1106 shndx = elfcpp::SHN_UNDEF;
1110 switch (isym->visibility)
1112 case LDPV_PROTECTED:
1113 vis = elfcpp::STV_PROTECTED;
1116 vis = elfcpp::STV_INTERNAL;
1119 vis = elfcpp::STV_HIDDEN;
1123 vis = elfcpp::STV_DEFAULT;
1127 if (isym->comdat_key != NULL
1128 && isym->comdat_key[0] != '\0'
1129 && !this->include_comdat_group(isym->comdat_key, layout))
1130 shndx = elfcpp::SHN_UNDEF;
1132 osym.put_st_name(0);
1133 osym.put_st_value(0);
1134 osym.put_st_size(0);
1135 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1136 osym.put_st_other(vis, 0);
1137 osym.put_st_shndx(shndx);
1140 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1144 template<int size, bool big_endian>
1145 Archive::Should_include
1146 Sized_pluginobj<size, big_endian>::do_should_include_member(
1147 Symbol_table* symtab,
1152 char* tmpbuf = NULL;
1153 size_t tmpbuflen = 0;
1155 for (int i = 0; i < this->nsyms_; ++i)
1157 const struct ld_plugin_symbol& sym = this->syms_[i];
1158 const char* name = sym.name;
1160 Archive::Should_include t = Archive::should_include_member(symtab,
1166 if (t == Archive::SHOULD_INCLUDE_YES)
1175 return Archive::SHOULD_INCLUDE_UNKNOWN;
1178 // Iterate over global symbols, calling a visitor class V for each.
1180 template<int size, bool big_endian>
1182 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1184 Library_base::Symbol_visitor_base* v)
1186 for (int i = 0; i < this->nsyms_; ++i)
1188 const struct ld_plugin_symbol& sym = this->syms_[i];
1189 if (sym.def != LDPK_UNDEF)
1194 // Iterate over local symbols, calling a visitor class V for each GOT offset
1195 // associated with a local symbol.
1196 template<int size, bool big_endian>
1198 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1199 Got_offset_list::Visitor*) const
1204 // Get the size of a section. Not used for plugin objects.
1206 template<int size, bool big_endian>
1208 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1214 // Get the name of a section. Not used for plugin objects.
1216 template<int size, bool big_endian>
1218 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
1221 return std::string();
1224 // Return a view of the contents of a section. Not used for plugin objects.
1226 template<int size, bool big_endian>
1227 const unsigned char*
1228 Sized_pluginobj<size, big_endian>::do_section_contents(
1237 // Return section flags. Not used for plugin objects.
1239 template<int size, bool big_endian>
1241 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1247 // Return section entsize. Not used for plugin objects.
1249 template<int size, bool big_endian>
1251 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1257 // Return section address. Not used for plugin objects.
1259 template<int size, bool big_endian>
1261 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1267 // Return section type. Not used for plugin objects.
1269 template<int size, bool big_endian>
1271 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1277 // Return the section link field. Not used for plugin objects.
1279 template<int size, bool big_endian>
1281 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1287 // Return the section link field. Not used for plugin objects.
1289 template<int size, bool big_endian>
1291 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1297 // Return the section alignment. Not used for plugin objects.
1299 template<int size, bool big_endian>
1301 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1307 // Return the Xindex structure to use. Not used for plugin objects.
1309 template<int size, bool big_endian>
1311 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1317 // Get symbol counts. Don't count plugin objects; the replacement
1318 // files will provide the counts.
1320 template<int size, bool big_endian>
1322 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1323 const Symbol_table*,
1331 // Get symbols. Not used for plugin objects.
1333 template<int size, bool big_endian>
1334 const Object::Symbols*
1335 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1340 // Class Plugin_finish. This task runs after all replacement files have
1341 // been added. For now, it's a placeholder for a possible plugin API
1342 // to allow the plugin to release most of its resources. The cleanup
1343 // handlers must be called later, because they can remove the temporary
1344 // object files that are needed until the end of the link.
1346 class Plugin_finish : public Task
1349 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1350 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1355 if (this->this_blocker_ != NULL)
1356 delete this->this_blocker_;
1362 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1363 return this->this_blocker_;
1368 locks(Task_locker* tl)
1369 { tl->add(this, this->next_blocker_); }
1374 // We could call early cleanup handlers here.
1379 { return "Plugin_finish"; }
1382 Task_token* this_blocker_;
1383 Task_token* next_blocker_;
1386 // Class Plugin_hook.
1388 Plugin_hook::~Plugin_hook()
1392 // Return whether a Plugin_hook task is runnable.
1395 Plugin_hook::is_runnable()
1397 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1398 return this->this_blocker_;
1402 // Return a Task_locker for a Plugin_hook task. We don't need any
1406 Plugin_hook::locks(Task_locker*)
1410 // Run the "all symbols read" plugin hook.
1413 Plugin_hook::run(Workqueue* workqueue)
1415 gold_assert(this->options_.has_plugins());
1416 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1417 if (start_sym != NULL)
1418 start_sym->set_in_real_elf();
1420 this->options_.plugins()->all_symbols_read(workqueue,
1422 this->input_objects_,
1426 &this->this_blocker_);
1427 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1428 this->next_blocker_));
1431 // The C interface routines called by the plugins.
1433 #ifdef ENABLE_PLUGINS
1435 // Register a claim-file handler.
1437 static enum ld_plugin_status
1438 register_claim_file(ld_plugin_claim_file_handler handler)
1440 gold_assert(parameters->options().has_plugins());
1441 parameters->options().plugins()->set_claim_file_handler(handler);
1445 // Register an all-symbols-read handler.
1447 static enum ld_plugin_status
1448 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1450 gold_assert(parameters->options().has_plugins());
1451 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1455 // Register a cleanup handler.
1457 static enum ld_plugin_status
1458 register_cleanup(ld_plugin_cleanup_handler handler)
1460 gold_assert(parameters->options().has_plugins());
1461 parameters->options().plugins()->set_cleanup_handler(handler);
1465 // Add symbols from a plugin-claimed input file.
1467 static enum ld_plugin_status
1468 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1470 gold_assert(parameters->options().has_plugins());
1471 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1472 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1475 obj->store_incoming_symbols(nsyms, syms);
1479 // Get the input file information with an open (possibly re-opened)
1482 static enum ld_plugin_status
1483 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1485 gold_assert(parameters->options().has_plugins());
1486 unsigned int obj_index =
1487 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1488 return parameters->options().plugins()->get_input_file(obj_index, file);
1491 // Release the input file.
1493 static enum ld_plugin_status
1494 release_input_file(const void* handle)
1496 gold_assert(parameters->options().has_plugins());
1497 unsigned int obj_index =
1498 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1499 return parameters->options().plugins()->release_input_file(obj_index);
1502 static enum ld_plugin_status
1503 get_view(const void *handle, const void **viewp)
1505 gold_assert(parameters->options().has_plugins());
1506 unsigned int obj_index =
1507 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1508 return parameters->options().plugins()->get_view(obj_index, viewp);
1511 // Get the symbol resolution info for a plugin-claimed input file.
1513 static enum ld_plugin_status
1514 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1516 gold_assert(parameters->options().has_plugins());
1517 Plugin_manager* plugins = parameters->options().plugins();
1518 Object* obj = plugins->object(
1519 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1522 Pluginobj* plugin_obj = obj->pluginobj();
1523 if (plugin_obj == NULL)
1525 Symbol_table* symtab = plugins->symtab();
1526 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
1529 // Version 2 of the above. The only difference is that this version
1530 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1532 static enum ld_plugin_status
1533 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1535 gold_assert(parameters->options().has_plugins());
1536 Plugin_manager* plugins = parameters->options().plugins();
1537 Object* obj = plugins->object(
1538 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1541 Pluginobj* plugin_obj = obj->pluginobj();
1542 if (plugin_obj == NULL)
1544 Symbol_table* symtab = plugins->symtab();
1545 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
1548 // Add a new (real) input file generated by a plugin.
1550 static enum ld_plugin_status
1551 add_input_file(const char* pathname)
1553 gold_assert(parameters->options().has_plugins());
1554 return parameters->options().plugins()->add_input_file(pathname, false);
1557 // Add a new (real) library required by a plugin.
1559 static enum ld_plugin_status
1560 add_input_library(const char* pathname)
1562 gold_assert(parameters->options().has_plugins());
1563 return parameters->options().plugins()->add_input_file(pathname, true);
1566 // Set the extra library path to be used by libraries added via
1567 // add_input_library
1569 static enum ld_plugin_status
1570 set_extra_library_path(const char* path)
1572 gold_assert(parameters->options().has_plugins());
1573 return parameters->options().plugins()->set_extra_library_path(path);
1576 // Issue a diagnostic message from a plugin.
1578 static enum ld_plugin_status
1579 message(int level, const char* format, ...)
1582 va_start(args, format);
1587 parameters->errors()->info(format, args);
1590 parameters->errors()->warning(format, args);
1594 parameters->errors()->error(format, args);
1597 parameters->errors()->fatal(format, args);
1605 // Get the section count of the object corresponding to the handle. This
1606 // plugin interface can only be called in the claim_file handler of the plugin.
1608 static enum ld_plugin_status
1609 get_input_section_count(const void* handle, unsigned int* count)
1611 gold_assert(parameters->options().has_plugins());
1613 if (!parameters->options().plugins()->in_claim_file_handler())
1616 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1621 *count = obj->shnum();
1625 // Get the type of the specified section in the object corresponding
1626 // to the handle. This plugin interface can only be called in the
1627 // claim_file handler of the plugin.
1629 static enum ld_plugin_status
1630 get_input_section_type(const struct ld_plugin_section section,
1633 gold_assert(parameters->options().has_plugins());
1635 if (!parameters->options().plugins()->in_claim_file_handler())
1639 = parameters->options().plugins()->get_elf_object(section.handle);
1642 return LDPS_BAD_HANDLE;
1644 *type = obj->section_type(section.shndx);
1648 // Get the name of the specified section in the object corresponding
1649 // to the handle. This plugin interface can only be called in the
1650 // claim_file handler of the plugin.
1652 static enum ld_plugin_status
1653 get_input_section_name(const struct ld_plugin_section section,
1654 char** section_name_ptr)
1656 gold_assert(parameters->options().has_plugins());
1658 if (!parameters->options().plugins()->in_claim_file_handler())
1662 = parameters->options().plugins()->get_elf_object(section.handle);
1665 return LDPS_BAD_HANDLE;
1667 // Check if the object is locked before getting the section name.
1668 gold_assert(obj->is_locked());
1670 const std::string section_name = obj->section_name(section.shndx);
1671 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1672 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1676 // Get the contents of the specified section in the object corresponding
1677 // to the handle. This plugin interface can only be called in the
1678 // claim_file handler of the plugin.
1680 static enum ld_plugin_status
1681 get_input_section_contents(const struct ld_plugin_section section,
1682 const unsigned char** section_contents_ptr,
1685 gold_assert(parameters->options().has_plugins());
1687 if (!parameters->options().plugins()->in_claim_file_handler())
1691 = parameters->options().plugins()->get_elf_object(section.handle);
1694 return LDPS_BAD_HANDLE;
1696 // Check if the object is locked before getting the section contents.
1697 gold_assert(obj->is_locked());
1699 section_size_type plen;
1700 *section_contents_ptr
1701 = obj->section_contents(section.shndx, &plen, false);
1706 // Specify the ordering of sections in the final layout. The sections are
1707 // specified as (handle,shndx) pairs in the two arrays in the order in
1708 // which they should appear in the final layout.
1710 static enum ld_plugin_status
1711 update_section_order(const struct ld_plugin_section* section_list,
1712 unsigned int num_sections)
1714 gold_assert(parameters->options().has_plugins());
1716 if (num_sections == 0)
1719 if (section_list == NULL)
1722 Layout* layout = parameters->options().plugins()->layout();
1723 gold_assert (layout != NULL);
1725 std::map<Section_id, unsigned int>* order_map
1726 = layout->get_section_order_map();
1728 /* Store the mapping from Section_id to section position in layout's
1729 order_map to consult after output sections are added. */
1730 for (unsigned int i = 0; i < num_sections; ++i)
1732 Object* obj = parameters->options().plugins()->get_elf_object(
1733 section_list[i].handle);
1734 if (obj == NULL || obj->is_dynamic())
1735 return LDPS_BAD_HANDLE;
1736 unsigned int shndx = section_list[i].shndx;
1737 Section_id secn_id(static_cast<Relobj*>(obj), shndx);
1738 (*order_map)[secn_id] = i + 1;
1744 // Let the linker know that the sections could be reordered.
1746 static enum ld_plugin_status
1747 allow_section_ordering()
1749 gold_assert(parameters->options().has_plugins());
1750 Layout* layout = parameters->options().plugins()->layout();
1751 layout->set_section_ordering_specified();
1755 // Let the linker know that a subset of sections could be mapped
1756 // to a unique segment.
1758 static enum ld_plugin_status
1759 allow_unique_segment_for_sections()
1761 gold_assert(parameters->options().has_plugins());
1762 Layout* layout = parameters->options().plugins()->layout();
1763 layout->set_unique_segment_for_sections_specified();
1767 // This function should map the list of sections specified in the
1768 // SECTION_LIST to a unique segment. ELF segments do not have names
1769 // and the NAME is used to identify Output Section which should contain
1770 // the list of sections. This Output Section will then be mapped to
1771 // a unique segment. FLAGS is used to specify if any additional segment
1772 // flags need to be set. For instance, a specific segment flag can be
1773 // set to identify this segment. Unsetting segment flags is not possible.
1774 // ALIGN specifies the alignment of the segment.
1776 static enum ld_plugin_status
1777 unique_segment_for_sections(const char* segment_name,
1780 const struct ld_plugin_section* section_list,
1781 unsigned int num_sections)
1783 gold_assert(parameters->options().has_plugins());
1785 if (num_sections == 0)
1788 if (section_list == NULL)
1791 Layout* layout = parameters->options().plugins()->layout();
1792 gold_assert (layout != NULL);
1794 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
1795 s->name = segment_name;
1799 for (unsigned int i = 0; i < num_sections; ++i)
1801 Object* obj = parameters->options().plugins()->get_elf_object(
1802 section_list[i].handle);
1803 if (obj == NULL || obj->is_dynamic())
1804 return LDPS_BAD_HANDLE;
1805 unsigned int shndx = section_list[i].shndx;
1806 Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
1807 layout->insert_section_segment_map(secn_id, s);
1813 #endif // ENABLE_PLUGINS
1815 // Allocate a Pluginobj object of the appropriate size and endianness.
1818 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1820 Pluginobj* obj = NULL;
1822 parameters_force_valid_target();
1823 const Target& target(parameters->target());
1825 if (target.get_size() == 32)
1827 if (target.is_big_endian())
1828 #ifdef HAVE_TARGET_32_BIG
1829 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1830 input_file, offset, filesize);
1832 gold_error(_("%s: not configured to support "
1833 "32-bit big-endian object"),
1834 input_file->filename().c_str());
1837 #ifdef HAVE_TARGET_32_LITTLE
1838 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1839 input_file, offset, filesize);
1841 gold_error(_("%s: not configured to support "
1842 "32-bit little-endian object"),
1843 input_file->filename().c_str());
1846 else if (target.get_size() == 64)
1848 if (target.is_big_endian())
1849 #ifdef HAVE_TARGET_64_BIG
1850 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1851 input_file, offset, filesize);
1853 gold_error(_("%s: not configured to support "
1854 "64-bit big-endian object"),
1855 input_file->filename().c_str());
1858 #ifdef HAVE_TARGET_64_LITTLE
1859 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1860 input_file, offset, filesize);
1862 gold_error(_("%s: not configured to support "
1863 "64-bit little-endian object"),
1864 input_file->filename().c_str());
1868 gold_assert(obj != NULL);
1872 } // End namespace gold.