1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2016 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 get_symbols_v3(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
117 static enum ld_plugin_status
118 add_input_file(const char *pathname);
120 static enum ld_plugin_status
121 add_input_library(const char *pathname);
123 static enum ld_plugin_status
124 set_extra_library_path(const char *path);
126 static enum ld_plugin_status
127 message(int level, const char *format, ...);
129 static enum ld_plugin_status
130 get_input_section_count(const void* handle, unsigned int* count);
132 static enum ld_plugin_status
133 get_input_section_type(const struct ld_plugin_section section,
136 static enum ld_plugin_status
137 get_input_section_name(const struct ld_plugin_section section,
138 char** section_name_ptr);
140 static enum ld_plugin_status
141 get_input_section_contents(const struct ld_plugin_section section,
142 const unsigned char** section_contents,
145 static enum ld_plugin_status
146 update_section_order(const struct ld_plugin_section *section_list,
147 unsigned int num_sections);
149 static enum ld_plugin_status
150 allow_section_ordering();
152 static enum ld_plugin_status
153 allow_unique_segment_for_sections();
155 static enum ld_plugin_status
156 unique_segment_for_sections(const char* segment_name,
159 const struct ld_plugin_section *section_list,
160 unsigned int num_sections);
163 #endif // ENABLE_PLUGINS
165 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
166 off_t offset, off_t filesize);
170 // Load one plugin library.
175 #ifdef ENABLE_PLUGINS
176 // Load the plugin library.
177 // FIXME: Look for the library in standard locations.
178 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
179 if (this->handle_ == NULL)
181 gold_error(_("%s: could not load plugin library: %s"),
182 this->filename_.c_str(), dlerror());
186 // Find the plugin's onload entry point.
187 void* ptr = dlsym(this->handle_, "onload");
190 gold_error(_("%s: could not find onload entry point"),
191 this->filename_.c_str());
194 ld_plugin_onload onload;
195 gold_assert(sizeof(onload) == sizeof(ptr));
196 memcpy(&onload, &ptr, sizeof(ptr));
198 // Get the linker's version number.
199 const char* ver = get_version_string();
202 sscanf(ver, "%d.%d", &major, &minor);
204 // Allocate and populate a transfer vector.
205 const int tv_fixed_size = 27;
207 int tv_size = this->args_.size() + tv_fixed_size;
208 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
210 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
211 // while processing subsequent entries.
213 tv[i].tv_tag = LDPT_MESSAGE;
214 tv[i].tv_u.tv_message = message;
217 tv[i].tv_tag = LDPT_API_VERSION;
218 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
221 tv[i].tv_tag = LDPT_GOLD_VERSION;
222 tv[i].tv_u.tv_val = major * 100 + minor;
225 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
226 if (parameters->options().relocatable())
227 tv[i].tv_u.tv_val = LDPO_REL;
228 else if (parameters->options().shared())
229 tv[i].tv_u.tv_val = LDPO_DYN;
230 else if (parameters->options().pie())
231 tv[i].tv_u.tv_val = LDPO_PIE;
233 tv[i].tv_u.tv_val = LDPO_EXEC;
236 tv[i].tv_tag = LDPT_OUTPUT_NAME;
237 tv[i].tv_u.tv_string = parameters->options().output();
239 for (unsigned int j = 0; j < this->args_.size(); ++j)
242 tv[i].tv_tag = LDPT_OPTION;
243 tv[i].tv_u.tv_string = this->args_[j].c_str();
247 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
248 tv[i].tv_u.tv_register_claim_file = register_claim_file;
251 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
252 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
255 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
256 tv[i].tv_u.tv_register_cleanup = register_cleanup;
259 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
260 tv[i].tv_u.tv_add_symbols = add_symbols;
263 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
264 tv[i].tv_u.tv_get_input_file = get_input_file;
267 tv[i].tv_tag = LDPT_GET_VIEW;
268 tv[i].tv_u.tv_get_view = get_view;
271 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
272 tv[i].tv_u.tv_release_input_file = release_input_file;
275 tv[i].tv_tag = LDPT_GET_SYMBOLS;
276 tv[i].tv_u.tv_get_symbols = get_symbols;
279 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
280 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
283 tv[i].tv_tag = LDPT_GET_SYMBOLS_V3;
284 tv[i].tv_u.tv_get_symbols = get_symbols_v3;
287 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
288 tv[i].tv_u.tv_add_input_file = add_input_file;
291 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
292 tv[i].tv_u.tv_add_input_library = add_input_library;
295 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
296 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
299 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
300 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
303 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
304 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
307 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
308 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
311 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
312 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
315 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
316 tv[i].tv_u.tv_update_section_order = update_section_order;
319 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
320 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
323 tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
324 tv[i].tv_u.tv_allow_unique_segment_for_sections
325 = allow_unique_segment_for_sections;
328 tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
329 tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
332 tv[i].tv_tag = LDPT_NULL;
333 tv[i].tv_u.tv_val = 0;
335 gold_assert(i == tv_size - 1);
337 // Call the onload entry point.
341 #endif // ENABLE_PLUGINS
344 // Call the plugin claim-file handler.
347 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
351 if (this->claim_file_handler_ != NULL)
353 (*this->claim_file_handler_)(plugin_input_file, &claimed);
360 // Call the all-symbols-read handler.
363 Plugin::all_symbols_read()
365 if (this->all_symbols_read_handler_ != NULL)
366 (*this->all_symbols_read_handler_)();
369 // Call the cleanup handler.
374 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
376 // Set this flag before calling to prevent a recursive plunge
377 // in the event that a plugin's cleanup handler issues a
379 this->cleanup_done_ = true;
380 (*this->cleanup_handler_)();
384 // This task is used to rescan archives as needed.
386 class Plugin_rescan : public Task
389 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
390 : this_blocker_(this_blocker), next_blocker_(next_blocker)
395 delete this->this_blocker_;
401 if (this->this_blocker_->is_blocked())
402 return this->this_blocker_;
407 locks(Task_locker* tl)
408 { tl->add(this, this->next_blocker_); }
412 { parameters->options().plugins()->rescan(this); }
416 { return "Plugin_rescan"; }
419 Task_token* this_blocker_;
420 Task_token* next_blocker_;
423 // Plugin_manager methods.
425 Plugin_manager::~Plugin_manager()
427 for (Plugin_list::iterator p = this->plugins_.begin();
428 p != this->plugins_.end();
431 this->plugins_.clear();
432 for (Object_list::iterator obj = this->objects_.begin();
433 obj != this->objects_.end();
436 this->objects_.clear();
440 // Load all plugin libraries.
443 Plugin_manager::load_plugins(Layout* layout)
445 this->layout_ = layout;
446 for (this->current_ = this->plugins_.begin();
447 this->current_ != this->plugins_.end();
449 (*this->current_)->load();
452 // Call the plugin claim-file handlers in turn to see if any claim the file.
455 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
456 off_t filesize, Object* elf_object)
458 bool lock_initialized = this->initialize_lock_.initialize();
460 gold_assert(lock_initialized);
461 Hold_lock hl(*this->lock_);
462 if (this->in_replacement_phase_)
465 unsigned int handle = this->objects_.size();
466 this->input_file_ = input_file;
467 this->plugin_input_file_.name = input_file->filename().c_str();
468 this->plugin_input_file_.fd = input_file->file().descriptor();
469 this->plugin_input_file_.offset = offset;
470 this->plugin_input_file_.filesize = filesize;
471 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
472 if (elf_object != NULL)
473 this->objects_.push_back(elf_object);
474 this->in_claim_file_handler_ = true;
476 for (this->current_ = this->plugins_.begin();
477 this->current_ != this->plugins_.end();
480 if ((*this->current_)->claim_file(&this->plugin_input_file_))
482 this->any_claimed_ = true;
483 this->in_claim_file_handler_ = false;
485 if (this->objects_.size() > handle
486 && this->objects_[handle]->pluginobj() != NULL)
487 return this->objects_[handle]->pluginobj();
489 // If the plugin claimed the file but did not call the
490 // add_symbols callback, we need to create the Pluginobj now.
491 Pluginobj* obj = this->make_plugin_object(handle);
496 this->in_claim_file_handler_ = false;
500 // Save an archive. This is used so that a plugin can add a file
501 // which refers to a symbol which was not previously referenced. In
502 // that case we want to pretend that the symbol was referenced before,
503 // and pull in the archive object.
506 Plugin_manager::save_archive(Archive* archive)
508 if (this->in_replacement_phase_ || !this->any_claimed_)
511 this->rescannable_.push_back(Rescannable(archive));
514 // Save an Input_group. This is like save_archive.
517 Plugin_manager::save_input_group(Input_group* input_group)
519 if (this->in_replacement_phase_ || !this->any_claimed_)
522 this->rescannable_.push_back(Rescannable(input_group));
525 // Call the all-symbols-read handlers.
528 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
529 Input_objects* input_objects,
530 Symbol_table* symtab,
531 Dirsearch* dirpath, Mapfile* mapfile,
532 Task_token** last_blocker)
534 this->in_replacement_phase_ = true;
535 this->workqueue_ = workqueue;
537 this->input_objects_ = input_objects;
538 this->symtab_ = symtab;
539 this->dirpath_ = dirpath;
540 this->mapfile_ = mapfile;
541 this->this_blocker_ = NULL;
543 for (this->current_ = this->plugins_.begin();
544 this->current_ != this->plugins_.end();
546 (*this->current_)->all_symbols_read();
548 if (this->any_added_)
550 Task_token* next_blocker = new Task_token(true);
551 next_blocker->add_blocker();
552 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
553 this->this_blocker_ = next_blocker;
556 *last_blocker = this->this_blocker_;
559 // This is called when we see a new undefined symbol. If we are in
560 // the replacement phase, this means that we may need to rescan some
561 // archives we have previously seen.
564 Plugin_manager::new_undefined_symbol(Symbol* sym)
566 if (this->in_replacement_phase_)
567 this->undefined_symbols_.push_back(sym);
570 // Rescan archives as needed. This handles the case where a new
571 // object file added by a plugin has an undefined reference to some
572 // symbol defined in an archive.
575 Plugin_manager::rescan(Task* task)
577 size_t rescan_pos = 0;
578 size_t rescan_size = this->rescannable_.size();
579 while (!this->undefined_symbols_.empty())
581 if (rescan_pos >= rescan_size)
583 this->undefined_symbols_.clear();
587 Undefined_symbol_list undefs;
588 undefs.reserve(this->undefined_symbols_.size());
589 this->undefined_symbols_.swap(undefs);
591 size_t min_rescan_pos = rescan_size;
593 for (Undefined_symbol_list::const_iterator p = undefs.begin();
597 if (!(*p)->is_undefined())
600 this->undefined_symbols_.push_back(*p);
602 // Find the first rescan archive which defines this symbol,
603 // starting at the current rescan position. The rescan position
604 // exists so that given -la -lb -lc we don't look for undefined
605 // symbols in -lb back in -la, but instead get the definition
606 // from -lc. Don't bother to look past the current minimum
608 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
610 if (this->rescannable_defines(i, *p))
618 if (min_rescan_pos >= rescan_size)
620 // We didn't find any rescannable archives which define any
621 // undefined symbols.
625 const Rescannable& r(this->rescannable_[min_rescan_pos]);
628 Task_lock_obj<Archive> tl(task, r.u.archive);
629 r.u.archive->add_symbols(this->symtab_, this->layout_,
630 this->input_objects_, this->mapfile_);
634 size_t next_saw_undefined = this->symtab_->saw_undefined();
635 size_t saw_undefined;
638 saw_undefined = next_saw_undefined;
640 for (Input_group::const_iterator p = r.u.input_group->begin();
641 p != r.u.input_group->end();
644 Task_lock_obj<Archive> tl(task, *p);
646 (*p)->add_symbols(this->symtab_, this->layout_,
647 this->input_objects_, this->mapfile_);
650 next_saw_undefined = this->symtab_->saw_undefined();
652 while (saw_undefined != next_saw_undefined);
655 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
657 if (this->rescannable_[i].is_archive)
658 delete this->rescannable_[i].u.archive;
660 delete this->rescannable_[i].u.input_group;
663 rescan_pos = min_rescan_pos + 1;
667 // Return whether the rescannable at index I defines SYM.
670 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
672 const Rescannable& r(this->rescannable_[i]);
674 return r.u.archive->defines_symbol(sym);
677 for (Input_group::const_iterator p = r.u.input_group->begin();
678 p != r.u.input_group->end();
681 if ((*p)->defines_symbol(sym))
688 // Layout deferred objects.
691 Plugin_manager::layout_deferred_objects()
693 Deferred_layout_list::iterator obj;
695 for (obj = this->deferred_layout_objects_.begin();
696 obj != this->deferred_layout_objects_.end();
699 // Lock the object so we can read from it. This is only called
700 // single-threaded from queue_middle_tasks, so it is OK to lock.
701 // Unfortunately we have no way to pass in a Task token.
702 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
703 Task_lock_obj<Object> tl(dummy_task, *obj);
704 (*obj)->layout_deferred_sections(this->layout_);
708 // Call the cleanup handlers.
711 Plugin_manager::cleanup()
713 if (this->any_added_)
715 // If any input files were added, close all the input files.
716 // This is because the plugin may want to remove them, and on
717 // Windows you are not allowed to remove an open file.
718 close_all_descriptors();
721 for (this->current_ = this->plugins_.begin();
722 this->current_ != this->plugins_.end();
724 (*this->current_)->cleanup();
727 // Make a new Pluginobj object. This is called when the plugin calls
728 // the add_symbols API.
731 Plugin_manager::make_plugin_object(unsigned int handle)
733 // Make sure we aren't asked to make an object for the same handle twice.
734 if (this->objects_.size() != handle
735 && this->objects_[handle]->pluginobj() != NULL)
738 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
739 this->plugin_input_file_.offset,
740 this->plugin_input_file_.filesize);
743 // If the elf object for this file was pushed into the objects_ vector, delete
744 // it to make room for the Pluginobj as this file is claimed.
745 if (this->objects_.size() != handle)
746 this->objects_.pop_back();
748 this->objects_.push_back(obj);
752 // Get the input file information with an open (possibly re-opened)
756 Plugin_manager::get_input_file(unsigned int handle,
757 struct ld_plugin_input_file* file)
759 Pluginobj* obj = this->object(handle)->pluginobj();
761 return LDPS_BAD_HANDLE;
763 obj->lock(this->task_);
764 file->name = obj->filename().c_str();
765 file->fd = obj->descriptor();
766 file->offset = obj->offset();
767 file->filesize = obj->filesize();
768 file->handle = reinterpret_cast<void*>(handle);
772 // Release the input file.
775 Plugin_manager::release_input_file(unsigned int handle)
777 if (this->object(handle) == NULL)
778 return LDPS_BAD_HANDLE;
780 Pluginobj* obj = this->object(handle)->pluginobj();
783 return LDPS_BAD_HANDLE;
785 obj->unlock(this->task_);
789 // Get the elf object corresponding to the handle. Return NULL if we
790 // found a Pluginobj instead.
793 Plugin_manager::get_elf_object(const void* handle)
795 Object* obj = this->object(
796 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
798 // The object should not be a Pluginobj.
800 || obj->pluginobj() != NULL)
807 Plugin_manager::get_view(unsigned int handle, const void **viewp)
811 Input_file *input_file;
812 if (this->in_claim_file_handler_)
814 // We are being called from the claim_file hook.
815 const struct ld_plugin_input_file &f = this->plugin_input_file_;
817 filesize = f.filesize;
818 input_file = this->input_file_;
822 // An already claimed file.
823 if (this->object(handle) == NULL)
824 return LDPS_BAD_HANDLE;
825 Pluginobj* obj = this->object(handle)->pluginobj();
827 return LDPS_BAD_HANDLE;
828 offset = obj->offset();
829 filesize = obj->filesize();
830 input_file = obj->input_file();
832 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
837 // Add a new library path.
840 Plugin_manager::set_extra_library_path(const char* path)
842 this->extra_search_path_ = std::string(path);
846 // Add a new input file.
849 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
851 Input_file_argument file(pathname,
853 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
854 : Input_file_argument::INPUT_FILE_TYPE_FILE),
856 ? this->extra_search_path_.c_str()
860 Input_argument* input_argument = new Input_argument(file);
861 Task_token* next_blocker = new Task_token(true);
862 next_blocker->add_blocker();
863 if (parameters->incremental())
864 gold_error(_("input files added by plug-ins in --incremental mode not "
866 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
877 this->this_blocker_ = next_blocker;
878 this->any_added_ = true;
884 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
885 off_t offset, off_t filesize)
886 : Object(name, input_file, false, offset),
887 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
891 // Return TRUE if a defined symbol is referenced from outside the
892 // universe of claimed objects. Only references from relocatable,
893 // non-IR (unclaimed) objects count as a reference. References from
894 // dynamic objects count only as "visible".
897 is_referenced_from_outside(Symbol* lsym)
899 if (lsym->in_real_elf())
901 if (parameters->options().relocatable())
903 if (parameters->options().is_undefined(lsym->name()))
908 // Return TRUE if a defined symbol might be reachable from outside the
912 is_visible_from_outside(Symbol* lsym)
916 if (parameters->options().export_dynamic() || parameters->options().shared())
917 return lsym->is_externally_visible();
921 // Get symbol resolution info.
924 Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
926 ld_plugin_symbol* syms,
929 // For version 1 of this interface, we cannot use
930 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
932 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
934 ? LDPR_PREVAILING_DEF_IRONLY_EXP
935 : LDPR_PREVAILING_DEF);
937 if (nsyms > this->nsyms_)
940 if (static_cast<size_t>(nsyms) > this->symbols_.size())
942 // We never decided to include this object. We mark all symbols as
944 gold_assert(this->symbols_.size() == 0);
945 for (int i = 0; i < nsyms; i++)
946 syms[i].resolution = LDPR_PREEMPTED_REG;
947 return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
950 for (int i = 0; i < nsyms; i++)
952 ld_plugin_symbol* isym = &syms[i];
953 Symbol* lsym = this->symbols_[i];
954 if (lsym->is_forwarder())
955 lsym = symtab->resolve_forwards(lsym);
956 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
958 if (lsym->is_undefined())
959 // The symbol remains undefined.
961 else if (isym->def == LDPK_UNDEF
962 || isym->def == LDPK_WEAKUNDEF
963 || isym->def == LDPK_COMMON)
965 // The original symbol was undefined or common.
966 if (lsym->source() != Symbol::FROM_OBJECT)
967 res = LDPR_RESOLVED_EXEC;
968 else if (lsym->object()->pluginobj() == this)
970 if (is_referenced_from_outside(lsym))
971 res = LDPR_PREVAILING_DEF;
972 else if (is_visible_from_outside(lsym))
973 res = ldpr_prevailing_def_ironly_exp;
975 res = LDPR_PREVAILING_DEF_IRONLY;
977 else if (lsym->object()->pluginobj() != NULL)
978 res = LDPR_RESOLVED_IR;
979 else if (lsym->object()->is_dynamic())
980 res = LDPR_RESOLVED_DYN;
982 res = LDPR_RESOLVED_EXEC;
986 // The original symbol was a definition.
987 if (lsym->source() != Symbol::FROM_OBJECT)
988 res = LDPR_PREEMPTED_REG;
989 else if (lsym->object() == static_cast<const Object*>(this))
991 if (is_referenced_from_outside(lsym))
992 res = LDPR_PREVAILING_DEF;
993 else if (is_visible_from_outside(lsym))
994 res = ldpr_prevailing_def_ironly_exp;
996 res = LDPR_PREVAILING_DEF_IRONLY;
999 res = (lsym->object()->pluginobj() != NULL
1001 : LDPR_PREEMPTED_REG);
1003 isym->resolution = res;
1008 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1012 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
1014 std::pair<Comdat_map::iterator, bool> ins =
1015 this->comdat_map_.insert(std::make_pair(comdat_key, false));
1017 // If this is the first time we've seen this comdat key, ask the
1018 // layout object whether it should be included.
1020 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1024 return ins.first->second;
1027 // Class Sized_pluginobj.
1029 template<int size, bool big_endian>
1030 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1031 const std::string& name,
1032 Input_file* input_file,
1035 : Pluginobj(name, input_file, offset, filesize)
1039 // Read the symbols. Not used for plugin objects.
1041 template<int size, bool big_endian>
1043 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1048 // Lay out the input sections. Not used for plugin objects.
1050 template<int size, bool big_endian>
1052 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1058 // Add the symbols to the symbol table.
1060 template<int size, bool big_endian>
1062 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1066 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1067 unsigned char symbuf[sym_size];
1068 elfcpp::Sym<size, big_endian> sym(symbuf);
1069 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1071 this->symbols_.resize(this->nsyms_);
1073 for (int i = 0; i < this->nsyms_; ++i)
1075 const struct ld_plugin_symbol* isym = &this->syms_[i];
1076 const char* name = isym->name;
1077 const char* ver = isym->version;
1078 elfcpp::Elf_Half shndx;
1082 if (name != NULL && name[0] == '\0')
1084 if (ver != NULL && ver[0] == '\0')
1090 case LDPK_WEAKUNDEF:
1091 bind = elfcpp::STB_WEAK;
1097 bind = elfcpp::STB_GLOBAL;
1105 shndx = elfcpp::SHN_ABS;
1108 shndx = elfcpp::SHN_COMMON;
1111 case LDPK_WEAKUNDEF:
1113 shndx = elfcpp::SHN_UNDEF;
1117 switch (isym->visibility)
1119 case LDPV_PROTECTED:
1120 vis = elfcpp::STV_PROTECTED;
1123 vis = elfcpp::STV_INTERNAL;
1126 vis = elfcpp::STV_HIDDEN;
1130 vis = elfcpp::STV_DEFAULT;
1134 if (isym->comdat_key != NULL
1135 && isym->comdat_key[0] != '\0'
1136 && !this->include_comdat_group(isym->comdat_key, layout))
1137 shndx = elfcpp::SHN_UNDEF;
1139 osym.put_st_name(0);
1140 osym.put_st_value(0);
1141 osym.put_st_size(0);
1142 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1143 osym.put_st_other(vis, 0);
1144 osym.put_st_shndx(shndx);
1147 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1151 template<int size, bool big_endian>
1152 Archive::Should_include
1153 Sized_pluginobj<size, big_endian>::do_should_include_member(
1154 Symbol_table* symtab,
1159 char* tmpbuf = NULL;
1160 size_t tmpbuflen = 0;
1162 for (int i = 0; i < this->nsyms_; ++i)
1164 const struct ld_plugin_symbol& sym = this->syms_[i];
1165 const char* name = sym.name;
1167 Archive::Should_include t = Archive::should_include_member(symtab,
1173 if (t == Archive::SHOULD_INCLUDE_YES)
1182 return Archive::SHOULD_INCLUDE_UNKNOWN;
1185 // Iterate over global symbols, calling a visitor class V for each.
1187 template<int size, bool big_endian>
1189 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1191 Library_base::Symbol_visitor_base* v)
1193 for (int i = 0; i < this->nsyms_; ++i)
1195 const struct ld_plugin_symbol& sym = this->syms_[i];
1196 if (sym.def != LDPK_UNDEF)
1201 // Iterate over local symbols, calling a visitor class V for each GOT offset
1202 // associated with a local symbol.
1203 template<int size, bool big_endian>
1205 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1206 Got_offset_list::Visitor*) const
1211 // Get the size of a section. Not used for plugin objects.
1213 template<int size, bool big_endian>
1215 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1221 // Get the name of a section. Not used for plugin objects.
1223 template<int size, bool big_endian>
1225 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
1228 return std::string();
1231 // Return a view of the contents of a section. Not used for plugin objects.
1233 template<int size, bool big_endian>
1234 const unsigned char*
1235 Sized_pluginobj<size, big_endian>::do_section_contents(
1244 // Return section flags. Not used for plugin objects.
1246 template<int size, bool big_endian>
1248 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1254 // Return section entsize. Not used for plugin objects.
1256 template<int size, bool big_endian>
1258 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1264 // Return section address. Not used for plugin objects.
1266 template<int size, bool big_endian>
1268 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1274 // Return section type. Not used for plugin objects.
1276 template<int size, bool big_endian>
1278 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1284 // Return the section link field. Not used for plugin objects.
1286 template<int size, bool big_endian>
1288 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1294 // Return the section link field. Not used for plugin objects.
1296 template<int size, bool big_endian>
1298 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1304 // Return the section alignment. Not used for plugin objects.
1306 template<int size, bool big_endian>
1308 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1314 // Return the Xindex structure to use. Not used for plugin objects.
1316 template<int size, bool big_endian>
1318 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1324 // Get symbol counts. Don't count plugin objects; the replacement
1325 // files will provide the counts.
1327 template<int size, bool big_endian>
1329 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1330 const Symbol_table*,
1338 // Get symbols. Not used for plugin objects.
1340 template<int size, bool big_endian>
1341 const Object::Symbols*
1342 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1347 // Class Plugin_finish. This task runs after all replacement files have
1348 // been added. For now, it's a placeholder for a possible plugin API
1349 // to allow the plugin to release most of its resources. The cleanup
1350 // handlers must be called later, because they can remove the temporary
1351 // object files that are needed until the end of the link.
1353 class Plugin_finish : public Task
1356 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1357 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1362 if (this->this_blocker_ != NULL)
1363 delete this->this_blocker_;
1369 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1370 return this->this_blocker_;
1375 locks(Task_locker* tl)
1376 { tl->add(this, this->next_blocker_); }
1381 // We could call early cleanup handlers here.
1386 { return "Plugin_finish"; }
1389 Task_token* this_blocker_;
1390 Task_token* next_blocker_;
1393 // Class Plugin_hook.
1395 Plugin_hook::~Plugin_hook()
1399 // Return whether a Plugin_hook task is runnable.
1402 Plugin_hook::is_runnable()
1404 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1405 return this->this_blocker_;
1409 // Return a Task_locker for a Plugin_hook task. We don't need any
1413 Plugin_hook::locks(Task_locker*)
1417 // Run the "all symbols read" plugin hook.
1420 Plugin_hook::run(Workqueue* workqueue)
1422 gold_assert(this->options_.has_plugins());
1423 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1424 if (start_sym != NULL)
1425 start_sym->set_in_real_elf();
1427 this->options_.plugins()->all_symbols_read(workqueue,
1429 this->input_objects_,
1433 &this->this_blocker_);
1434 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1435 this->next_blocker_));
1438 // The C interface routines called by the plugins.
1440 #ifdef ENABLE_PLUGINS
1442 // Register a claim-file handler.
1444 static enum ld_plugin_status
1445 register_claim_file(ld_plugin_claim_file_handler handler)
1447 gold_assert(parameters->options().has_plugins());
1448 parameters->options().plugins()->set_claim_file_handler(handler);
1452 // Register an all-symbols-read handler.
1454 static enum ld_plugin_status
1455 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1457 gold_assert(parameters->options().has_plugins());
1458 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1462 // Register a cleanup handler.
1464 static enum ld_plugin_status
1465 register_cleanup(ld_plugin_cleanup_handler handler)
1467 gold_assert(parameters->options().has_plugins());
1468 parameters->options().plugins()->set_cleanup_handler(handler);
1472 // Add symbols from a plugin-claimed input file.
1474 static enum ld_plugin_status
1475 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1477 gold_assert(parameters->options().has_plugins());
1478 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1479 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1482 obj->store_incoming_symbols(nsyms, syms);
1486 // Get the input file information with an open (possibly re-opened)
1489 static enum ld_plugin_status
1490 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1492 gold_assert(parameters->options().has_plugins());
1493 unsigned int obj_index =
1494 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1495 return parameters->options().plugins()->get_input_file(obj_index, file);
1498 // Release the input file.
1500 static enum ld_plugin_status
1501 release_input_file(const void* handle)
1503 gold_assert(parameters->options().has_plugins());
1504 unsigned int obj_index =
1505 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1506 return parameters->options().plugins()->release_input_file(obj_index);
1509 static enum ld_plugin_status
1510 get_view(const void *handle, const void **viewp)
1512 gold_assert(parameters->options().has_plugins());
1513 unsigned int obj_index =
1514 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1515 return parameters->options().plugins()->get_view(obj_index, viewp);
1518 // Get the symbol resolution info for a plugin-claimed input file.
1520 static enum ld_plugin_status
1521 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1523 gold_assert(parameters->options().has_plugins());
1524 Plugin_manager* plugins = parameters->options().plugins();
1525 Object* obj = plugins->object(
1526 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1529 Pluginobj* plugin_obj = obj->pluginobj();
1530 if (plugin_obj == NULL)
1532 Symbol_table* symtab = plugins->symtab();
1533 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
1536 // Version 2 of the above. The only difference is that this version
1537 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1539 static enum ld_plugin_status
1540 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1542 gold_assert(parameters->options().has_plugins());
1543 Plugin_manager* plugins = parameters->options().plugins();
1544 Object* obj = plugins->object(
1545 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1548 Pluginobj* plugin_obj = obj->pluginobj();
1549 if (plugin_obj == NULL)
1551 Symbol_table* symtab = plugins->symtab();
1552 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
1555 // Version 3 of the above. The only difference from v2 is that it
1556 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1557 // decided to include.
1559 static enum ld_plugin_status
1560 get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
1562 gold_assert(parameters->options().has_plugins());
1563 Plugin_manager* plugins = parameters->options().plugins();
1564 Object* obj = plugins->object(
1565 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1568 Pluginobj* plugin_obj = obj->pluginobj();
1569 if (plugin_obj == NULL)
1571 Symbol_table* symtab = plugins->symtab();
1572 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
1575 // Add a new (real) input file generated by a plugin.
1577 static enum ld_plugin_status
1578 add_input_file(const char* pathname)
1580 gold_assert(parameters->options().has_plugins());
1581 return parameters->options().plugins()->add_input_file(pathname, false);
1584 // Add a new (real) library required by a plugin.
1586 static enum ld_plugin_status
1587 add_input_library(const char* pathname)
1589 gold_assert(parameters->options().has_plugins());
1590 return parameters->options().plugins()->add_input_file(pathname, true);
1593 // Set the extra library path to be used by libraries added via
1594 // add_input_library
1596 static enum ld_plugin_status
1597 set_extra_library_path(const char* path)
1599 gold_assert(parameters->options().has_plugins());
1600 return parameters->options().plugins()->set_extra_library_path(path);
1603 // Issue a diagnostic message from a plugin.
1605 static enum ld_plugin_status
1606 message(int level, const char* format, ...)
1609 va_start(args, format);
1614 parameters->errors()->info(format, args);
1617 parameters->errors()->warning(format, args);
1621 parameters->errors()->error(format, args);
1624 parameters->errors()->fatal(format, args);
1632 // Get the section count of the object corresponding to the handle. This
1633 // plugin interface can only be called in the claim_file handler of the plugin.
1635 static enum ld_plugin_status
1636 get_input_section_count(const void* handle, unsigned int* count)
1638 gold_assert(parameters->options().has_plugins());
1640 if (!parameters->options().plugins()->in_claim_file_handler())
1643 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1648 *count = obj->shnum();
1652 // Get the type of the specified section in the object corresponding
1653 // to the handle. This plugin interface can only be called in the
1654 // claim_file handler of the plugin.
1656 static enum ld_plugin_status
1657 get_input_section_type(const struct ld_plugin_section section,
1660 gold_assert(parameters->options().has_plugins());
1662 if (!parameters->options().plugins()->in_claim_file_handler())
1666 = parameters->options().plugins()->get_elf_object(section.handle);
1669 return LDPS_BAD_HANDLE;
1671 *type = obj->section_type(section.shndx);
1675 // Get the name of the specified section in the object corresponding
1676 // to the handle. This plugin interface can only be called in the
1677 // claim_file handler of the plugin.
1679 static enum ld_plugin_status
1680 get_input_section_name(const struct ld_plugin_section section,
1681 char** section_name_ptr)
1683 gold_assert(parameters->options().has_plugins());
1685 if (!parameters->options().plugins()->in_claim_file_handler())
1689 = parameters->options().plugins()->get_elf_object(section.handle);
1692 return LDPS_BAD_HANDLE;
1694 // Check if the object is locked before getting the section name.
1695 gold_assert(obj->is_locked());
1697 const std::string section_name = obj->section_name(section.shndx);
1698 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1699 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1703 // Get the contents of the specified section in the object corresponding
1704 // to the handle. This plugin interface can only be called in the
1705 // claim_file handler of the plugin.
1707 static enum ld_plugin_status
1708 get_input_section_contents(const struct ld_plugin_section section,
1709 const unsigned char** section_contents_ptr,
1712 gold_assert(parameters->options().has_plugins());
1714 if (!parameters->options().plugins()->in_claim_file_handler())
1718 = parameters->options().plugins()->get_elf_object(section.handle);
1721 return LDPS_BAD_HANDLE;
1723 // Check if the object is locked before getting the section contents.
1724 gold_assert(obj->is_locked());
1726 section_size_type plen;
1727 *section_contents_ptr
1728 = obj->section_contents(section.shndx, &plen, false);
1733 // Specify the ordering of sections in the final layout. The sections are
1734 // specified as (handle,shndx) pairs in the two arrays in the order in
1735 // which they should appear in the final layout.
1737 static enum ld_plugin_status
1738 update_section_order(const struct ld_plugin_section* section_list,
1739 unsigned int num_sections)
1741 gold_assert(parameters->options().has_plugins());
1743 if (num_sections == 0)
1746 if (section_list == NULL)
1749 Layout* layout = parameters->options().plugins()->layout();
1750 gold_assert (layout != NULL);
1752 std::map<Section_id, unsigned int>* order_map
1753 = layout->get_section_order_map();
1755 /* Store the mapping from Section_id to section position in layout's
1756 order_map to consult after output sections are added. */
1757 for (unsigned int i = 0; i < num_sections; ++i)
1759 Object* obj = parameters->options().plugins()->get_elf_object(
1760 section_list[i].handle);
1761 if (obj == NULL || obj->is_dynamic())
1762 return LDPS_BAD_HANDLE;
1763 unsigned int shndx = section_list[i].shndx;
1764 Section_id secn_id(static_cast<Relobj*>(obj), shndx);
1765 (*order_map)[secn_id] = i + 1;
1771 // Let the linker know that the sections could be reordered.
1773 static enum ld_plugin_status
1774 allow_section_ordering()
1776 gold_assert(parameters->options().has_plugins());
1777 Layout* layout = parameters->options().plugins()->layout();
1778 layout->set_section_ordering_specified();
1782 // Let the linker know that a subset of sections could be mapped
1783 // to a unique segment.
1785 static enum ld_plugin_status
1786 allow_unique_segment_for_sections()
1788 gold_assert(parameters->options().has_plugins());
1789 Layout* layout = parameters->options().plugins()->layout();
1790 layout->set_unique_segment_for_sections_specified();
1794 // This function should map the list of sections specified in the
1795 // SECTION_LIST to a unique segment. ELF segments do not have names
1796 // and the NAME is used to identify Output Section which should contain
1797 // the list of sections. This Output Section will then be mapped to
1798 // a unique segment. FLAGS is used to specify if any additional segment
1799 // flags need to be set. For instance, a specific segment flag can be
1800 // set to identify this segment. Unsetting segment flags is not possible.
1801 // ALIGN specifies the alignment of the segment.
1803 static enum ld_plugin_status
1804 unique_segment_for_sections(const char* segment_name,
1807 const struct ld_plugin_section* section_list,
1808 unsigned int num_sections)
1810 gold_assert(parameters->options().has_plugins());
1812 if (num_sections == 0)
1815 if (section_list == NULL)
1818 Layout* layout = parameters->options().plugins()->layout();
1819 gold_assert (layout != NULL);
1821 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
1822 s->name = segment_name;
1826 for (unsigned int i = 0; i < num_sections; ++i)
1828 Object* obj = parameters->options().plugins()->get_elf_object(
1829 section_list[i].handle);
1830 if (obj == NULL || obj->is_dynamic())
1831 return LDPS_BAD_HANDLE;
1832 unsigned int shndx = section_list[i].shndx;
1833 Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
1834 layout->insert_section_segment_map(secn_id, s);
1840 #endif // ENABLE_PLUGINS
1842 // Allocate a Pluginobj object of the appropriate size and endianness.
1845 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1847 Pluginobj* obj = NULL;
1849 parameters_force_valid_target();
1850 const Target& target(parameters->target());
1852 if (target.get_size() == 32)
1854 if (target.is_big_endian())
1855 #ifdef HAVE_TARGET_32_BIG
1856 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1857 input_file, offset, filesize);
1859 gold_error(_("%s: not configured to support "
1860 "32-bit big-endian object"),
1861 input_file->filename().c_str());
1864 #ifdef HAVE_TARGET_32_LITTLE
1865 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1866 input_file, offset, filesize);
1868 gold_error(_("%s: not configured to support "
1869 "32-bit little-endian object"),
1870 input_file->filename().c_str());
1873 else if (target.get_size() == 64)
1875 if (target.is_big_endian())
1876 #ifdef HAVE_TARGET_64_BIG
1877 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1878 input_file, offset, filesize);
1880 gold_error(_("%s: not configured to support "
1881 "64-bit big-endian object"),
1882 input_file->filename().c_str());
1885 #ifdef HAVE_TARGET_64_LITTLE
1886 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1887 input_file, offset, filesize);
1889 gold_error(_("%s: not configured to support "
1890 "64-bit little-endian object"),
1891 input_file->filename().c_str());
1895 gold_assert(obj != NULL);
1899 } // End namespace gold.