1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2019 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.
33 #include "libiberty.h"
38 #elif defined (HAVE_WINDOWS_H)
41 #error Unknown how to handle dynamic-load-libraries.
44 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
46 #define RTLD_NOW 0 /* Dummy value. */
48 dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
50 return LoadLibrary(file);
54 dlsym(void *handle, const char *name)
56 return reinterpret_cast<void *>(
57 GetProcAddress(static_cast<HMODULE>(handle),name));
63 return "unable to load dll";
66 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
67 #endif /* ENABLE_PLUGINS */
69 #include "parameters.h"
79 #include "descriptors.h"
87 // The linker's exported interfaces.
92 static enum ld_plugin_status
93 register_claim_file(ld_plugin_claim_file_handler handler);
95 static enum ld_plugin_status
96 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
98 static enum ld_plugin_status
99 register_cleanup(ld_plugin_cleanup_handler handler);
101 static enum ld_plugin_status
102 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
104 static enum ld_plugin_status
105 get_input_file(const void *handle, struct ld_plugin_input_file *file);
107 static enum ld_plugin_status
108 get_view(const void *handle, const void **viewp);
110 static enum ld_plugin_status
111 release_input_file(const void *handle);
113 static enum ld_plugin_status
114 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
116 static enum ld_plugin_status
117 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
119 static enum ld_plugin_status
120 get_symbols_v3(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
122 static enum ld_plugin_status
123 add_input_file(const char *pathname);
125 static enum ld_plugin_status
126 add_input_library(const char *pathname);
128 static enum ld_plugin_status
129 set_extra_library_path(const char *path);
131 static enum ld_plugin_status
132 message(int level, const char *format, ...);
134 static enum ld_plugin_status
135 get_input_section_count(const void* handle, unsigned int* count);
137 static enum ld_plugin_status
138 get_input_section_type(const struct ld_plugin_section section,
141 static enum ld_plugin_status
142 get_input_section_name(const struct ld_plugin_section section,
143 char** section_name_ptr);
145 static enum ld_plugin_status
146 get_input_section_contents(const struct ld_plugin_section section,
147 const unsigned char** section_contents,
150 static enum ld_plugin_status
151 update_section_order(const struct ld_plugin_section *section_list,
152 unsigned int num_sections);
154 static enum ld_plugin_status
155 allow_section_ordering();
157 static enum ld_plugin_status
158 allow_unique_segment_for_sections();
160 static enum ld_plugin_status
161 unique_segment_for_sections(const char* segment_name,
164 const struct ld_plugin_section *section_list,
165 unsigned int num_sections);
167 static enum ld_plugin_status
168 get_input_section_alignment(const struct ld_plugin_section section,
169 unsigned int* addralign);
171 static enum ld_plugin_status
172 get_input_section_size(const struct ld_plugin_section section,
175 static enum ld_plugin_status
176 register_new_input(ld_plugin_new_input_handler handler);
178 static enum ld_plugin_status
179 get_wrap_symbols(uint64_t *num_symbols, const char ***wrap_symbol_list);
183 #endif // ENABLE_PLUGINS
185 static Pluginobj* make_sized_plugin_object(const std::string& filename,
186 Input_file* input_file,
187 off_t offset, off_t filesize);
191 // Load one plugin library.
196 #ifdef ENABLE_PLUGINS
197 // Load the plugin library.
198 // FIXME: Look for the library in standard locations.
199 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
200 if (this->handle_ == NULL)
202 #ifdef IGNORE_BROKEN_PLUGINS
203 gold_warning(_("%s: could not load plugin library: %s"),
204 this->filename_.c_str(), dlerror());
208 gold_error(_("%s: could not load plugin library: %s"),
209 this->filename_.c_str(), dlerror());
214 // Find the plugin's onload entry point.
215 void* ptr = dlsym(this->handle_, "onload");
218 gold_error(_("%s: could not find onload entry point"),
219 this->filename_.c_str());
222 ld_plugin_onload onload;
223 gold_assert(sizeof(onload) == sizeof(ptr));
224 memcpy(&onload, &ptr, sizeof(ptr));
226 // Get the linker's version number.
227 const char* ver = get_version_string();
230 sscanf(ver, "%d.%d", &major, &minor);
232 // Allocate and populate a transfer vector.
233 const int tv_fixed_size = 31;
235 int tv_size = this->args_.size() + tv_fixed_size;
236 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
238 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
239 // while processing subsequent entries.
241 tv[i].tv_tag = LDPT_MESSAGE;
242 tv[i].tv_u.tv_message = message;
245 tv[i].tv_tag = LDPT_API_VERSION;
246 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
249 tv[i].tv_tag = LDPT_GOLD_VERSION;
250 tv[i].tv_u.tv_val = major * 100 + minor;
253 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
254 if (parameters->options().relocatable())
255 tv[i].tv_u.tv_val = LDPO_REL;
256 else if (parameters->options().shared())
257 tv[i].tv_u.tv_val = LDPO_DYN;
258 else if (parameters->options().pie())
259 tv[i].tv_u.tv_val = LDPO_PIE;
261 tv[i].tv_u.tv_val = LDPO_EXEC;
264 tv[i].tv_tag = LDPT_OUTPUT_NAME;
265 tv[i].tv_u.tv_string = parameters->options().output();
267 for (unsigned int j = 0; j < this->args_.size(); ++j)
270 tv[i].tv_tag = LDPT_OPTION;
271 tv[i].tv_u.tv_string = this->args_[j].c_str();
275 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
276 tv[i].tv_u.tv_register_claim_file = register_claim_file;
279 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
280 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
283 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
284 tv[i].tv_u.tv_register_cleanup = register_cleanup;
287 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
288 tv[i].tv_u.tv_add_symbols = add_symbols;
291 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
292 tv[i].tv_u.tv_get_input_file = get_input_file;
295 tv[i].tv_tag = LDPT_GET_VIEW;
296 tv[i].tv_u.tv_get_view = get_view;
299 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
300 tv[i].tv_u.tv_release_input_file = release_input_file;
303 tv[i].tv_tag = LDPT_GET_SYMBOLS;
304 tv[i].tv_u.tv_get_symbols = get_symbols;
307 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
308 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
311 tv[i].tv_tag = LDPT_GET_SYMBOLS_V3;
312 tv[i].tv_u.tv_get_symbols = get_symbols_v3;
315 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
316 tv[i].tv_u.tv_add_input_file = add_input_file;
319 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
320 tv[i].tv_u.tv_add_input_library = add_input_library;
323 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
324 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
327 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
328 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
331 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
332 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
335 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
336 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
339 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
340 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
343 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
344 tv[i].tv_u.tv_update_section_order = update_section_order;
347 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
348 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
351 tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
352 tv[i].tv_u.tv_allow_unique_segment_for_sections
353 = allow_unique_segment_for_sections;
356 tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
357 tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
360 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_ALIGNMENT;
361 tv[i].tv_u.tv_get_input_section_alignment = get_input_section_alignment;
364 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_SIZE;
365 tv[i].tv_u.tv_get_input_section_size = get_input_section_size;
368 tv[i].tv_tag = LDPT_REGISTER_NEW_INPUT_HOOK;
369 tv[i].tv_u.tv_register_new_input = register_new_input;
372 tv[i].tv_tag = LDPT_GET_WRAP_SYMBOLS;
373 tv[i].tv_u.tv_get_wrap_symbols = get_wrap_symbols;
376 tv[i].tv_tag = LDPT_NULL;
377 tv[i].tv_u.tv_val = 0;
379 gold_assert(i == tv_size - 1);
381 // Call the onload entry point.
385 #endif // ENABLE_PLUGINS
388 // Call the plugin claim-file handler.
391 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
395 if (this->claim_file_handler_ != NULL)
397 (*this->claim_file_handler_)(plugin_input_file, &claimed);
404 // Call the all-symbols-read handler.
407 Plugin::all_symbols_read()
409 if (this->all_symbols_read_handler_ != NULL)
410 (*this->all_symbols_read_handler_)();
413 // Call the new_input handler.
416 Plugin::new_input(struct ld_plugin_input_file* plugin_input_file)
418 if (this->new_input_handler_ != NULL)
419 (*this->new_input_handler_)(plugin_input_file);
422 // Call the cleanup handler.
427 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
429 // Set this flag before calling to prevent a recursive plunge
430 // in the event that a plugin's cleanup handler issues a
432 this->cleanup_done_ = true;
433 (*this->cleanup_handler_)();
437 // This task is used to rescan archives as needed.
439 class Plugin_rescan : public Task
442 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
443 : this_blocker_(this_blocker), next_blocker_(next_blocker)
448 delete this->this_blocker_;
454 if (this->this_blocker_->is_blocked())
455 return this->this_blocker_;
460 locks(Task_locker* tl)
461 { tl->add(this, this->next_blocker_); }
465 { parameters->options().plugins()->rescan(this); }
469 { return "Plugin_rescan"; }
472 Task_token* this_blocker_;
473 Task_token* next_blocker_;
476 // Plugin_recorder logs plugin actions and saves intermediate files
479 class Plugin_recorder
482 Plugin_recorder() : file_count_(0), tempdir_(NULL), logfile_(NULL)
489 claimed_file(const std::string& obj_name, off_t offset, off_t filesize,
490 const std::string& plugin_name);
493 unclaimed_file(const std::string& obj_name, off_t offset, off_t filesize);
496 replacement_file(const char* name, bool is_lib);
499 record_symbols(const Object* obj, int nsyms,
500 const struct ld_plugin_symbol* syms);
504 { ::fclose(this->logfile_); }
507 unsigned int file_count_;
508 const char* tempdir_;
513 Plugin_recorder::init()
515 // Create a temporary directory where we can stash the log and
516 // copies of replacement files.
517 char dir_template[] = "gold-recording-XXXXXX";
519 if (mkdtemp(dir_template) == NULL)
522 if (mktemp(dir_template) == NULL)
524 #if defined (_WIN32) && !defined (__CYGWIN32__)
525 if (mkdir(dir_template) != 0)
528 if (mkdir(dir_template, 0700) != 0)
533 size_t len = strlen(dir_template) + 1;
534 char* tempdir = new char[len];
535 strncpy(tempdir, dir_template, len);
537 // Create the log file.
538 std::string logname(tempdir);
539 logname.append("/log");
540 FILE* logfile = ::fopen(logname.c_str(), "w");
544 this->tempdir_ = tempdir;
545 this->logfile_ = logfile;
547 gold_info(_("%s: recording to %s"), program_name, this->tempdir_);
553 Plugin_recorder::claimed_file(const std::string& obj_name,
556 const std::string& plugin_name)
558 fprintf(this->logfile_, "PLUGIN: %s\n", plugin_name.c_str());
559 fprintf(this->logfile_, "CLAIMED: %s", obj_name.c_str());
561 fprintf(this->logfile_, " @%ld", static_cast<long>(offset));
562 fprintf(this->logfile_, " %ld\n", static_cast<long>(filesize));
566 Plugin_recorder::unclaimed_file(const std::string& obj_name,
570 fprintf(this->logfile_, "UNCLAIMED: %s", obj_name.c_str());
572 fprintf(this->logfile_, " @%ld", static_cast<long>(offset));
573 fprintf(this->logfile_, " %ld\n", static_cast<long>(filesize));
576 // Make a hard link to INNAME from OUTNAME, if possible.
577 // If not, copy the file.
580 link_or_copy_file(const char* inname, const char* outname)
582 static char buf[4096];
585 if (::link(inname, outname) == 0)
589 int in = ::open(inname, O_RDONLY);
592 gold_warning(_("%s: can't open (%s)"), inname, strerror(errno));
595 int out = ::open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0600);
598 gold_warning(_("%s: can't create (%s)"), outname, strerror(errno));
603 while ((len = ::read(in, buf, sizeof(buf))) > 0)
605 if (::write(out, buf, len) != len)
607 gold_warning(_("%s: write error while making copy of file (%s)"),
608 inname, strerror(errno));
618 Plugin_recorder::replacement_file(const char* name, bool is_lib)
620 fprintf(this->logfile_, "REPLACEMENT: %s", name);
622 fprintf(this->logfile_, "(lib)");
626 const char* basename = lbasename(name);
627 snprintf(counter, sizeof(counter), "%05d", this->file_count_);
629 std::string outname(this->tempdir_);
631 outname.append(counter);
633 outname.append(basename);
634 if (link_or_copy_file(name, outname.c_str()))
635 fprintf(this->logfile_, " -> %s", outname.c_str());
637 fprintf(this->logfile_, "\n");
641 Plugin_recorder::record_symbols(const Object* obj, int nsyms,
642 const struct ld_plugin_symbol* syms)
644 fprintf(this->logfile_, "SYMBOLS: %d %s\n", nsyms, obj->name().c_str());
645 for (int i = 0; i < nsyms; ++i)
647 const struct ld_plugin_symbol* isym = &syms[i];
673 switch (isym->visibility)
692 fprintf(this->logfile_, " %5d: %-2s %c %s", i, def, vis, isym->name);
693 if (isym->version != NULL && isym->version[0] != '\0')
694 fprintf(this->logfile_, "@%s", isym->version);
695 if (isym->comdat_key != NULL && isym->comdat_key[0] != '\0')
697 if (strcmp(isym->name, isym->comdat_key) == 0)
698 fprintf(this->logfile_, " [comdat]");
700 fprintf(this->logfile_, " [comdat: %s]", isym->comdat_key);
702 fprintf(this->logfile_, "\n");
706 // Plugin_manager methods.
708 Plugin_manager::~Plugin_manager()
710 for (Plugin_list::iterator p = this->plugins_.begin();
711 p != this->plugins_.end();
714 this->plugins_.clear();
715 for (Object_list::iterator obj = this->objects_.begin();
716 obj != this->objects_.end();
719 this->objects_.clear();
721 delete this->recorder_;
724 // Load all plugin libraries.
725 #ifdef IGNORE_BROKEN_PLUGINS
726 bool delete_broken(const Plugin* a){
728 // remove broken plugin as it will be deleted from list
738 Plugin_manager::load_plugins(Layout* layout)
740 this->layout_ = layout;
742 if (is_debugging_enabled(DEBUG_PLUGIN))
744 this->recorder_ = new Plugin_recorder();
745 this->recorder_->init();
748 for (this->current_ = this->plugins_.begin();
749 this->current_ != this->plugins_.end();
751 (*this->current_)->load();
752 #ifdef IGNORE_BROKEN_PLUGINS
753 this->plugins_.remove_if(delete_broken);
757 // Call the plugin claim-file handlers in turn to see if any claim the file.
760 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
761 off_t filesize, Object* elf_object)
763 bool lock_initialized = this->initialize_lock_.initialize();
765 gold_assert(lock_initialized);
766 Hold_lock hl(*this->lock_);
768 unsigned int handle = this->objects_.size();
769 this->input_file_ = input_file;
770 this->plugin_input_file_.name = input_file->filename().c_str();
771 this->plugin_input_file_.fd = input_file->file().descriptor();
772 this->plugin_input_file_.offset = offset;
773 this->plugin_input_file_.filesize = filesize;
774 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
775 if (elf_object != NULL)
776 this->objects_.push_back(elf_object);
777 this->in_claim_file_handler_ = true;
779 for (this->current_ = this->plugins_.begin();
780 this->current_ != this->plugins_.end();
783 // If we aren't yet in replacement phase, allow plugins to claim input
784 // files, otherwise notify the plugin of the new input file, if needed.
785 if (!this->in_replacement_phase_)
787 if ((*this->current_)->claim_file(&this->plugin_input_file_))
789 this->any_claimed_ = true;
790 this->in_claim_file_handler_ = false;
792 if (this->recorder_ != NULL)
794 const std::string& objname = (elf_object == NULL
795 ? input_file->filename()
796 : elf_object->name());
797 this->recorder_->claimed_file(objname,
799 (*this->current_)->filename());
802 if (this->objects_.size() > handle
803 && this->objects_[handle]->pluginobj() != NULL)
804 return this->objects_[handle]->pluginobj();
806 // If the plugin claimed the file but did not call the
807 // add_symbols callback, we need to create the Pluginobj now.
808 Pluginobj* obj = this->make_plugin_object(handle);
814 (*this->current_)->new_input(&this->plugin_input_file_);
818 this->in_claim_file_handler_ = false;
820 if (this->recorder_ != NULL)
821 this->recorder_->unclaimed_file(input_file->filename(), offset, filesize);
826 // Save an archive. This is used so that a plugin can add a file
827 // which refers to a symbol which was not previously referenced. In
828 // that case we want to pretend that the symbol was referenced before,
829 // and pull in the archive object.
832 Plugin_manager::save_archive(Archive* archive)
834 if (this->in_replacement_phase_ || !this->any_claimed_)
837 this->rescannable_.push_back(Rescannable(archive));
840 // Save an Input_group. This is like save_archive.
843 Plugin_manager::save_input_group(Input_group* input_group)
845 if (this->in_replacement_phase_ || !this->any_claimed_)
848 this->rescannable_.push_back(Rescannable(input_group));
851 // Call the all-symbols-read handlers.
854 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
855 Input_objects* input_objects,
856 Symbol_table* symtab,
857 Dirsearch* dirpath, Mapfile* mapfile,
858 Task_token** last_blocker)
860 this->in_replacement_phase_ = true;
861 this->workqueue_ = workqueue;
863 this->input_objects_ = input_objects;
864 this->symtab_ = symtab;
865 this->dirpath_ = dirpath;
866 this->mapfile_ = mapfile;
867 this->this_blocker_ = NULL;
869 // Set symbols used in defsym expressions as seen in real ELF.
870 Layout *layout = parameters->options().plugins()->layout();
871 layout->script_options()->set_defsym_uses_in_real_elf(symtab);
872 layout->script_options()->find_defsym_defs(this->defsym_defines_set_);
874 for (this->current_ = this->plugins_.begin();
875 this->current_ != this->plugins_.end();
877 (*this->current_)->all_symbols_read();
879 if (this->any_added_)
881 Task_token* next_blocker = new Task_token(true);
882 next_blocker->add_blocker();
883 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
884 this->this_blocker_ = next_blocker;
887 *last_blocker = this->this_blocker_;
890 // This is called when we see a new undefined symbol. If we are in
891 // the replacement phase, this means that we may need to rescan some
892 // archives we have previously seen.
895 Plugin_manager::new_undefined_symbol(Symbol* sym)
897 if (this->in_replacement_phase_)
898 this->undefined_symbols_.push_back(sym);
901 // Rescan archives as needed. This handles the case where a new
902 // object file added by a plugin has an undefined reference to some
903 // symbol defined in an archive.
906 Plugin_manager::rescan(Task* task)
908 size_t rescan_pos = 0;
909 size_t rescan_size = this->rescannable_.size();
910 while (!this->undefined_symbols_.empty())
912 if (rescan_pos >= rescan_size)
914 this->undefined_symbols_.clear();
918 Undefined_symbol_list undefs;
919 undefs.reserve(this->undefined_symbols_.size());
920 this->undefined_symbols_.swap(undefs);
922 size_t min_rescan_pos = rescan_size;
924 for (Undefined_symbol_list::const_iterator p = undefs.begin();
928 if (!(*p)->is_undefined())
931 this->undefined_symbols_.push_back(*p);
933 // Find the first rescan archive which defines this symbol,
934 // starting at the current rescan position. The rescan position
935 // exists so that given -la -lb -lc we don't look for undefined
936 // symbols in -lb back in -la, but instead get the definition
937 // from -lc. Don't bother to look past the current minimum
939 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
941 if (this->rescannable_defines(i, *p))
949 if (min_rescan_pos >= rescan_size)
951 // We didn't find any rescannable archives which define any
952 // undefined symbols.
956 const Rescannable& r(this->rescannable_[min_rescan_pos]);
959 Task_lock_obj<Archive> tl(task, r.u.archive);
960 r.u.archive->add_symbols(this->symtab_, this->layout_,
961 this->input_objects_, this->mapfile_);
965 size_t next_saw_undefined = this->symtab_->saw_undefined();
966 size_t saw_undefined;
969 saw_undefined = next_saw_undefined;
971 for (Input_group::const_iterator p = r.u.input_group->begin();
972 p != r.u.input_group->end();
975 Task_lock_obj<Archive> tl(task, *p);
977 (*p)->add_symbols(this->symtab_, this->layout_,
978 this->input_objects_, this->mapfile_);
981 next_saw_undefined = this->symtab_->saw_undefined();
983 while (saw_undefined != next_saw_undefined);
986 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
988 if (this->rescannable_[i].is_archive)
989 delete this->rescannable_[i].u.archive;
991 delete this->rescannable_[i].u.input_group;
994 rescan_pos = min_rescan_pos + 1;
998 // Return whether the rescannable at index I defines SYM.
1001 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
1003 const Rescannable& r(this->rescannable_[i]);
1005 return r.u.archive->defines_symbol(sym);
1008 for (Input_group::const_iterator p = r.u.input_group->begin();
1009 p != r.u.input_group->end();
1012 if ((*p)->defines_symbol(sym))
1019 // Layout deferred objects.
1022 Plugin_manager::layout_deferred_objects()
1024 Deferred_layout_list::iterator obj;
1026 for (obj = this->deferred_layout_objects_.begin();
1027 obj != this->deferred_layout_objects_.end();
1030 // Lock the object so we can read from it. This is only called
1031 // single-threaded from queue_middle_tasks, so it is OK to lock.
1032 // Unfortunately we have no way to pass in a Task token.
1033 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
1034 Task_lock_obj<Object> tl(dummy_task, *obj);
1035 (*obj)->layout_deferred_sections(this->layout_);
1039 // Call the cleanup handlers.
1042 Plugin_manager::cleanup()
1044 if (this->any_added_)
1046 // If any input files were added, close all the input files.
1047 // This is because the plugin may want to remove them, and on
1048 // Windows you are not allowed to remove an open file.
1049 close_all_descriptors();
1052 for (this->current_ = this->plugins_.begin();
1053 this->current_ != this->plugins_.end();
1055 (*this->current_)->cleanup();
1058 // Make a new Pluginobj object. This is called when the plugin calls
1059 // the add_symbols API.
1062 Plugin_manager::make_plugin_object(unsigned int handle)
1064 // Make sure we aren't asked to make an object for the same handle twice.
1065 if (this->objects_.size() != handle
1066 && this->objects_[handle]->pluginobj() != NULL)
1069 const std::string* filename = &this->input_file_->filename();
1071 // If the elf object for this file was pushed into the objects_ vector,
1072 // use its filename, then delete it to make room for the Pluginobj as
1073 // this file is claimed.
1074 if (this->objects_.size() != handle)
1076 filename = &this->objects_.back()->name();
1077 this->objects_.pop_back();
1080 Pluginobj* obj = make_sized_plugin_object(*filename,
1082 this->plugin_input_file_.offset,
1083 this->plugin_input_file_.filesize);
1087 this->objects_.push_back(obj);
1091 // Get the input file information with an open (possibly re-opened)
1095 Plugin_manager::get_input_file(unsigned int handle,
1096 struct ld_plugin_input_file* file)
1098 Pluginobj* obj = this->object(handle)->pluginobj();
1100 return LDPS_BAD_HANDLE;
1102 obj->lock(this->task_);
1103 file->name = obj->filename().c_str();
1104 file->fd = obj->descriptor();
1105 file->offset = obj->offset();
1106 file->filesize = obj->filesize();
1107 file->handle = reinterpret_cast<void*>(handle);
1111 // Release the input file.
1114 Plugin_manager::release_input_file(unsigned int handle)
1116 if (this->object(handle) == NULL)
1117 return LDPS_BAD_HANDLE;
1119 Pluginobj* obj = this->object(handle)->pluginobj();
1122 return LDPS_BAD_HANDLE;
1124 obj->unlock(this->task_);
1128 // Get the elf object corresponding to the handle. Return NULL if we
1129 // found a Pluginobj instead.
1132 Plugin_manager::get_elf_object(const void* handle)
1134 Object* obj = this->object(
1135 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1137 // The object should not be a Pluginobj.
1139 || obj->pluginobj() != NULL)
1146 Plugin_manager::get_view(unsigned int handle, const void **viewp)
1150 Input_file *input_file;
1151 if (this->in_claim_file_handler_)
1153 // We are being called from the claim_file hook.
1154 const struct ld_plugin_input_file &f = this->plugin_input_file_;
1156 filesize = f.filesize;
1157 input_file = this->input_file_;
1161 // An already claimed file.
1162 if (this->object(handle) == NULL)
1163 return LDPS_BAD_HANDLE;
1164 Pluginobj* obj = this->object(handle)->pluginobj();
1166 return LDPS_BAD_HANDLE;
1167 offset = obj->offset();
1168 filesize = obj->filesize();
1169 input_file = obj->input_file();
1171 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
1176 // Add a new library path.
1179 Plugin_manager::set_extra_library_path(const char* path)
1181 this->extra_search_path_ = std::string(path);
1185 // Add a new input file.
1188 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
1190 Input_file_argument file(pathname,
1192 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
1193 : Input_file_argument::INPUT_FILE_TYPE_FILE),
1195 ? this->extra_search_path_.c_str()
1199 Input_argument* input_argument = new Input_argument(file);
1200 Task_token* next_blocker = new Task_token(true);
1201 next_blocker->add_blocker();
1202 if (parameters->incremental())
1203 gold_error(_("input files added by plug-ins in --incremental mode not "
1206 if (this->recorder_ != NULL)
1207 this->recorder_->replacement_file(pathname, is_lib);
1209 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
1218 this->this_blocker_,
1220 this->this_blocker_ = next_blocker;
1221 this->any_added_ = true;
1227 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
1228 off_t offset, off_t filesize)
1229 : Object(name, input_file, false, offset),
1230 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
1234 // Return TRUE if a defined symbol is referenced from outside the
1235 // universe of claimed objects. Only references from relocatable,
1236 // non-IR (unclaimed) objects count as a reference. References from
1237 // dynamic objects count only as "visible".
1240 is_referenced_from_outside(Symbol* lsym)
1242 if (lsym->in_real_elf())
1244 if (parameters->options().relocatable())
1246 if (parameters->options().is_undefined(lsym->name()))
1251 // Return TRUE if a defined symbol might be reachable from outside the
1255 is_visible_from_outside(Symbol* lsym)
1259 if (parameters->options().export_dynamic() || parameters->options().shared()
1260 || parameters->options().in_dynamic_list(lsym->name())
1261 || parameters->options().is_export_dynamic_symbol(lsym->name()))
1262 return lsym->is_externally_visible();
1266 // Get symbol resolution info.
1269 Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
1271 ld_plugin_symbol* syms,
1274 // For version 1 of this interface, we cannot use
1275 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
1277 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
1279 ? LDPR_PREVAILING_DEF_IRONLY_EXP
1280 : LDPR_PREVAILING_DEF);
1282 if (nsyms > this->nsyms_)
1283 return LDPS_NO_SYMS;
1285 if (static_cast<size_t>(nsyms) > this->symbols_.size())
1287 // We never decided to include this object. We mark all symbols as
1289 gold_assert(this->symbols_.size() == 0);
1290 for (int i = 0; i < nsyms; i++)
1291 syms[i].resolution = LDPR_PREEMPTED_REG;
1292 return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
1295 Plugin_manager* plugins = parameters->options().plugins();
1296 for (int i = 0; i < nsyms; i++)
1298 ld_plugin_symbol* isym = &syms[i];
1299 Symbol* lsym = this->symbols_[i];
1300 if (lsym->is_forwarder())
1301 lsym = symtab->resolve_forwards(lsym);
1302 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
1304 if (plugins->is_defsym_def(lsym->name()))
1306 // The symbol is redefined via defsym.
1307 res = LDPR_PREEMPTED_REG;
1309 else if (lsym->is_undefined())
1311 // The symbol remains undefined.
1314 else if (isym->def == LDPK_UNDEF
1315 || isym->def == LDPK_WEAKUNDEF
1316 || isym->def == LDPK_COMMON)
1318 // The original symbol was undefined or common.
1319 if (lsym->source() != Symbol::FROM_OBJECT)
1320 res = LDPR_RESOLVED_EXEC;
1321 else if (lsym->object()->pluginobj() == this)
1323 if (is_referenced_from_outside(lsym))
1324 res = LDPR_PREVAILING_DEF;
1325 else if (is_visible_from_outside(lsym))
1326 res = ldpr_prevailing_def_ironly_exp;
1328 res = LDPR_PREVAILING_DEF_IRONLY;
1330 else if (lsym->object()->pluginobj() != NULL)
1331 res = LDPR_RESOLVED_IR;
1332 else if (lsym->object()->is_dynamic())
1333 res = LDPR_RESOLVED_DYN;
1335 res = LDPR_RESOLVED_EXEC;
1339 // The original symbol was a definition.
1340 if (lsym->source() != Symbol::FROM_OBJECT)
1341 res = LDPR_PREEMPTED_REG;
1342 else if (lsym->object() == static_cast<const Object*>(this))
1344 if (is_referenced_from_outside(lsym))
1345 res = LDPR_PREVAILING_DEF;
1346 else if (is_visible_from_outside(lsym))
1347 res = ldpr_prevailing_def_ironly_exp;
1349 res = LDPR_PREVAILING_DEF_IRONLY;
1352 res = (lsym->object()->pluginobj() != NULL
1354 : LDPR_PREEMPTED_REG);
1356 isym->resolution = res;
1361 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1365 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
1367 std::pair<Comdat_map::iterator, bool> ins =
1368 this->comdat_map_.insert(std::make_pair(comdat_key, false));
1370 // If this is the first time we've seen this comdat key, ask the
1371 // layout object whether it should be included.
1373 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1377 return ins.first->second;
1380 // Class Sized_pluginobj.
1382 template<int size, bool big_endian>
1383 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1384 const std::string& name,
1385 Input_file* input_file,
1388 : Pluginobj(name, input_file, offset, filesize)
1392 // Read the symbols. Not used for plugin objects.
1394 template<int size, bool big_endian>
1396 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1401 // Lay out the input sections. Not used for plugin objects.
1403 template<int size, bool big_endian>
1405 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1411 // Add the symbols to the symbol table.
1413 template<int size, bool big_endian>
1415 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1419 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1420 unsigned char symbuf[sym_size];
1421 elfcpp::Sym<size, big_endian> sym(symbuf);
1422 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1424 Plugin_recorder* recorder = parameters->options().plugins()->recorder();
1425 if (recorder != NULL)
1426 recorder->record_symbols(this, this->nsyms_, this->syms_);
1428 this->symbols_.resize(this->nsyms_);
1430 for (int i = 0; i < this->nsyms_; ++i)
1432 const struct ld_plugin_symbol* isym = &this->syms_[i];
1433 const char* name = isym->name;
1434 const char* ver = isym->version;
1435 elfcpp::Elf_Half shndx;
1439 if (name != NULL && name[0] == '\0')
1441 if (ver != NULL && ver[0] == '\0')
1447 case LDPK_WEAKUNDEF:
1448 bind = elfcpp::STB_WEAK;
1454 bind = elfcpp::STB_GLOBAL;
1462 // We use an arbitrary section number for a defined symbol.
1466 shndx = elfcpp::SHN_COMMON;
1469 case LDPK_WEAKUNDEF:
1471 shndx = elfcpp::SHN_UNDEF;
1475 switch (isym->visibility)
1477 case LDPV_PROTECTED:
1478 vis = elfcpp::STV_PROTECTED;
1481 vis = elfcpp::STV_INTERNAL;
1484 vis = elfcpp::STV_HIDDEN;
1488 vis = elfcpp::STV_DEFAULT;
1492 if (isym->comdat_key != NULL
1493 && isym->comdat_key[0] != '\0'
1494 && !this->include_comdat_group(isym->comdat_key, layout))
1495 shndx = elfcpp::SHN_UNDEF;
1497 osym.put_st_name(0);
1498 osym.put_st_value(0);
1499 osym.put_st_size(0);
1500 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1501 osym.put_st_other(vis, 0);
1502 osym.put_st_shndx(shndx);
1505 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1509 template<int size, bool big_endian>
1510 Archive::Should_include
1511 Sized_pluginobj<size, big_endian>::do_should_include_member(
1512 Symbol_table* symtab,
1517 char* tmpbuf = NULL;
1518 size_t tmpbuflen = 0;
1520 for (int i = 0; i < this->nsyms_; ++i)
1522 const struct ld_plugin_symbol& sym = this->syms_[i];
1523 if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
1525 const char* name = sym.name;
1527 Archive::Should_include t = Archive::should_include_member(symtab,
1533 if (t == Archive::SHOULD_INCLUDE_YES)
1542 return Archive::SHOULD_INCLUDE_UNKNOWN;
1545 // Iterate over global symbols, calling a visitor class V for each.
1547 template<int size, bool big_endian>
1549 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1551 Library_base::Symbol_visitor_base* v)
1553 for (int i = 0; i < this->nsyms_; ++i)
1555 const struct ld_plugin_symbol& sym = this->syms_[i];
1556 if (sym.def != LDPK_UNDEF)
1561 // Iterate over local symbols, calling a visitor class V for each GOT offset
1562 // associated with a local symbol.
1563 template<int size, bool big_endian>
1565 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1566 Got_offset_list::Visitor*) const
1571 // Get the size of a section. Not used for plugin objects.
1573 template<int size, bool big_endian>
1575 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1581 // Get the name of a section. Not used for plugin objects.
1583 template<int size, bool big_endian>
1585 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
1588 return std::string();
1591 // Return a view of the contents of a section. Not used for plugin objects.
1593 template<int size, bool big_endian>
1594 const unsigned char*
1595 Sized_pluginobj<size, big_endian>::do_section_contents(
1604 // Return section flags. Not used for plugin objects.
1606 template<int size, bool big_endian>
1608 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1614 // Return section entsize. Not used for plugin objects.
1616 template<int size, bool big_endian>
1618 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1624 // Return section address. Not used for plugin objects.
1626 template<int size, bool big_endian>
1628 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1634 // Return section type. Not used for plugin objects.
1636 template<int size, bool big_endian>
1638 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1644 // Return the section link field. Not used for plugin objects.
1646 template<int size, bool big_endian>
1648 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1654 // Return the section link field. Not used for plugin objects.
1656 template<int size, bool big_endian>
1658 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1664 // Return the section alignment. Not used for plugin objects.
1666 template<int size, bool big_endian>
1668 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1674 // Return the Xindex structure to use. Not used for plugin objects.
1676 template<int size, bool big_endian>
1678 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1684 // Get symbol counts. Don't count plugin objects; the replacement
1685 // files will provide the counts.
1687 template<int size, bool big_endian>
1689 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1690 const Symbol_table*,
1698 // Get symbols. Not used for plugin objects.
1700 template<int size, bool big_endian>
1701 const Object::Symbols*
1702 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1707 // Class Plugin_finish. This task runs after all replacement files have
1708 // been added. For now, it's a placeholder for a possible plugin API
1709 // to allow the plugin to release most of its resources. The cleanup
1710 // handlers must be called later, because they can remove the temporary
1711 // object files that are needed until the end of the link.
1713 class Plugin_finish : public Task
1716 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1717 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1722 if (this->this_blocker_ != NULL)
1723 delete this->this_blocker_;
1729 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1730 return this->this_blocker_;
1735 locks(Task_locker* tl)
1736 { tl->add(this, this->next_blocker_); }
1741 Plugin_manager* plugins = parameters->options().plugins();
1742 gold_assert(plugins != NULL);
1743 // We could call early cleanup handlers here.
1744 if (plugins->recorder())
1745 plugins->recorder()->finish();
1750 { return "Plugin_finish"; }
1753 Task_token* this_blocker_;
1754 Task_token* next_blocker_;
1757 // Class Plugin_hook.
1759 Plugin_hook::~Plugin_hook()
1763 // Return whether a Plugin_hook task is runnable.
1766 Plugin_hook::is_runnable()
1768 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1769 return this->this_blocker_;
1773 // Return a Task_locker for a Plugin_hook task. We don't need any
1777 Plugin_hook::locks(Task_locker*)
1781 // Run the "all symbols read" plugin hook.
1784 Plugin_hook::run(Workqueue* workqueue)
1786 gold_assert(this->options_.has_plugins());
1787 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1788 if (start_sym != NULL)
1789 start_sym->set_in_real_elf();
1791 this->options_.plugins()->all_symbols_read(workqueue,
1793 this->input_objects_,
1797 &this->this_blocker_);
1798 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1799 this->next_blocker_));
1802 // The C interface routines called by the plugins.
1804 #ifdef ENABLE_PLUGINS
1806 // Register a claim-file handler.
1808 static enum ld_plugin_status
1809 register_claim_file(ld_plugin_claim_file_handler handler)
1811 gold_assert(parameters->options().has_plugins());
1812 parameters->options().plugins()->set_claim_file_handler(handler);
1816 // Register an all-symbols-read handler.
1818 static enum ld_plugin_status
1819 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1821 gold_assert(parameters->options().has_plugins());
1822 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1826 // Register a cleanup handler.
1828 static enum ld_plugin_status
1829 register_cleanup(ld_plugin_cleanup_handler handler)
1831 gold_assert(parameters->options().has_plugins());
1832 parameters->options().plugins()->set_cleanup_handler(handler);
1836 // Add symbols from a plugin-claimed input file.
1838 static enum ld_plugin_status
1839 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1841 gold_assert(parameters->options().has_plugins());
1842 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1843 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1846 obj->store_incoming_symbols(nsyms, syms);
1850 // Get the input file information with an open (possibly re-opened)
1853 static enum ld_plugin_status
1854 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1856 gold_assert(parameters->options().has_plugins());
1857 unsigned int obj_index =
1858 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1859 return parameters->options().plugins()->get_input_file(obj_index, file);
1862 // Release the input file.
1864 static enum ld_plugin_status
1865 release_input_file(const void* handle)
1867 gold_assert(parameters->options().has_plugins());
1868 unsigned int obj_index =
1869 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1870 return parameters->options().plugins()->release_input_file(obj_index);
1873 static enum ld_plugin_status
1874 get_view(const void *handle, const void **viewp)
1876 gold_assert(parameters->options().has_plugins());
1877 unsigned int obj_index =
1878 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1879 return parameters->options().plugins()->get_view(obj_index, viewp);
1882 // Get the symbol resolution info for a plugin-claimed input file.
1884 static enum ld_plugin_status
1885 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1887 gold_assert(parameters->options().has_plugins());
1888 Plugin_manager* plugins = parameters->options().plugins();
1889 Object* obj = plugins->object(
1890 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1893 Pluginobj* plugin_obj = obj->pluginobj();
1894 if (plugin_obj == NULL)
1896 Symbol_table* symtab = plugins->symtab();
1897 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
1900 // Version 2 of the above. The only difference is that this version
1901 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1903 static enum ld_plugin_status
1904 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1906 gold_assert(parameters->options().has_plugins());
1907 Plugin_manager* plugins = parameters->options().plugins();
1908 Object* obj = plugins->object(
1909 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1912 Pluginobj* plugin_obj = obj->pluginobj();
1913 if (plugin_obj == NULL)
1915 Symbol_table* symtab = plugins->symtab();
1916 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
1919 // Version 3 of the above. The only difference from v2 is that it
1920 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1921 // decided to include.
1923 static enum ld_plugin_status
1924 get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
1926 gold_assert(parameters->options().has_plugins());
1927 Plugin_manager* plugins = parameters->options().plugins();
1928 Object* obj = plugins->object(
1929 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1932 Pluginobj* plugin_obj = obj->pluginobj();
1933 if (plugin_obj == NULL)
1935 Symbol_table* symtab = plugins->symtab();
1936 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
1939 // Add a new (real) input file generated by a plugin.
1941 static enum ld_plugin_status
1942 add_input_file(const char* pathname)
1944 gold_assert(parameters->options().has_plugins());
1945 return parameters->options().plugins()->add_input_file(pathname, false);
1948 // Add a new (real) library required by a plugin.
1950 static enum ld_plugin_status
1951 add_input_library(const char* pathname)
1953 gold_assert(parameters->options().has_plugins());
1954 return parameters->options().plugins()->add_input_file(pathname, true);
1957 // Set the extra library path to be used by libraries added via
1958 // add_input_library
1960 static enum ld_plugin_status
1961 set_extra_library_path(const char* path)
1963 gold_assert(parameters->options().has_plugins());
1964 return parameters->options().plugins()->set_extra_library_path(path);
1967 // Issue a diagnostic message from a plugin.
1969 static enum ld_plugin_status
1970 message(int level, const char* format, ...)
1973 va_start(args, format);
1978 parameters->errors()->info(format, args);
1981 parameters->errors()->warning(format, args);
1985 parameters->errors()->error(format, args);
1988 parameters->errors()->fatal(format, args);
1996 // Get the section count of the object corresponding to the handle. This
1997 // plugin interface can only be called in the claim_file handler of the plugin.
1999 static enum ld_plugin_status
2000 get_input_section_count(const void* handle, unsigned int* count)
2002 gold_assert(parameters->options().has_plugins());
2004 if (!parameters->options().plugins()->in_claim_file_handler())
2007 Object* obj = parameters->options().plugins()->get_elf_object(handle);
2012 *count = obj->shnum();
2016 // Get the type of the specified section in the object corresponding
2017 // to the handle. This plugin interface can only be called in the
2018 // claim_file handler of the plugin.
2020 static enum ld_plugin_status
2021 get_input_section_type(const struct ld_plugin_section section,
2024 gold_assert(parameters->options().has_plugins());
2026 if (!parameters->options().plugins()->in_claim_file_handler())
2030 = parameters->options().plugins()->get_elf_object(section.handle);
2033 return LDPS_BAD_HANDLE;
2035 *type = obj->section_type(section.shndx);
2039 // Get the name of the specified section in the object corresponding
2040 // to the handle. This plugin interface can only be called in the
2041 // claim_file handler of the plugin.
2043 static enum ld_plugin_status
2044 get_input_section_name(const struct ld_plugin_section section,
2045 char** section_name_ptr)
2047 gold_assert(parameters->options().has_plugins());
2049 if (!parameters->options().plugins()->in_claim_file_handler())
2053 = parameters->options().plugins()->get_elf_object(section.handle);
2056 return LDPS_BAD_HANDLE;
2058 // Check if the object is locked before getting the section name.
2059 gold_assert(obj->is_locked());
2061 const std::string section_name = obj->section_name(section.shndx);
2062 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
2063 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
2067 // Get the contents of the specified section in the object corresponding
2068 // to the handle. This plugin interface can only be called in the
2069 // claim_file handler of the plugin.
2071 static enum ld_plugin_status
2072 get_input_section_contents(const struct ld_plugin_section section,
2073 const unsigned char** section_contents_ptr,
2076 gold_assert(parameters->options().has_plugins());
2078 if (!parameters->options().plugins()->in_claim_file_handler())
2082 = parameters->options().plugins()->get_elf_object(section.handle);
2085 return LDPS_BAD_HANDLE;
2087 // Check if the object is locked before getting the section contents.
2088 gold_assert(obj->is_locked());
2090 section_size_type plen;
2091 *section_contents_ptr
2092 = obj->section_contents(section.shndx, &plen, false);
2097 // Get the alignment of the specified section in the object corresponding
2098 // to the handle. This plugin interface can only be called in the
2099 // claim_file handler of the plugin.
2101 static enum ld_plugin_status
2102 get_input_section_alignment(const struct ld_plugin_section section,
2103 unsigned int* addralign)
2105 gold_assert(parameters->options().has_plugins());
2107 if (!parameters->options().plugins()->in_claim_file_handler())
2111 = parameters->options().plugins()->get_elf_object(section.handle);
2114 return LDPS_BAD_HANDLE;
2116 *addralign = obj->section_addralign(section.shndx);
2120 // Get the size of the specified section in the object corresponding
2121 // to the handle. This plugin interface can only be called in the
2122 // claim_file handler of the plugin.
2124 static enum ld_plugin_status
2125 get_input_section_size(const struct ld_plugin_section section,
2128 gold_assert(parameters->options().has_plugins());
2130 if (!parameters->options().plugins()->in_claim_file_handler())
2134 = parameters->options().plugins()->get_elf_object(section.handle);
2137 return LDPS_BAD_HANDLE;
2139 *secsize = obj->section_size(section.shndx);
2143 static enum ld_plugin_status
2144 get_wrap_symbols(uint64_t *count, const char ***wrap_symbols)
2146 gold_assert(parameters->options().has_plugins());
2147 *count = parameters->options().wrap_size();
2152 *wrap_symbols = new const char *[*count];
2154 for (options::String_set::const_iterator
2155 it = parameters->options().wrap_begin();
2156 it != parameters->options().wrap_end(); ++it, ++i) {
2157 (*wrap_symbols)[i] = it->c_str();
2163 // Specify the ordering of sections in the final layout. The sections are
2164 // specified as (handle,shndx) pairs in the two arrays in the order in
2165 // which they should appear in the final layout.
2167 static enum ld_plugin_status
2168 update_section_order(const struct ld_plugin_section* section_list,
2169 unsigned int num_sections)
2171 gold_assert(parameters->options().has_plugins());
2173 if (num_sections == 0)
2176 if (section_list == NULL)
2179 Layout* layout = parameters->options().plugins()->layout();
2180 gold_assert (layout != NULL);
2182 std::map<Section_id, unsigned int>* order_map
2183 = layout->get_section_order_map();
2185 /* Store the mapping from Section_id to section position in layout's
2186 order_map to consult after output sections are added. */
2187 for (unsigned int i = 0; i < num_sections; ++i)
2189 Object* obj = parameters->options().plugins()->get_elf_object(
2190 section_list[i].handle);
2191 if (obj == NULL || obj->is_dynamic())
2192 return LDPS_BAD_HANDLE;
2193 unsigned int shndx = section_list[i].shndx;
2194 Section_id secn_id(static_cast<Relobj*>(obj), shndx);
2195 (*order_map)[secn_id] = i + 1;
2201 // Let the linker know that the sections could be reordered.
2203 static enum ld_plugin_status
2204 allow_section_ordering()
2206 gold_assert(parameters->options().has_plugins());
2207 Layout* layout = parameters->options().plugins()->layout();
2208 layout->set_section_ordering_specified();
2212 // Let the linker know that a subset of sections could be mapped
2213 // to a unique segment.
2215 static enum ld_plugin_status
2216 allow_unique_segment_for_sections()
2218 gold_assert(parameters->options().has_plugins());
2219 Layout* layout = parameters->options().plugins()->layout();
2220 layout->set_unique_segment_for_sections_specified();
2224 // This function should map the list of sections specified in the
2225 // SECTION_LIST to a unique segment. ELF segments do not have names
2226 // and the NAME is used to identify Output Section which should contain
2227 // the list of sections. This Output Section will then be mapped to
2228 // a unique segment. FLAGS is used to specify if any additional segment
2229 // flags need to be set. For instance, a specific segment flag can be
2230 // set to identify this segment. Unsetting segment flags is not possible.
2231 // ALIGN specifies the alignment of the segment.
2233 static enum ld_plugin_status
2234 unique_segment_for_sections(const char* segment_name,
2237 const struct ld_plugin_section* section_list,
2238 unsigned int num_sections)
2240 gold_assert(parameters->options().has_plugins());
2242 if (num_sections == 0)
2245 if (section_list == NULL)
2248 Layout* layout = parameters->options().plugins()->layout();
2249 gold_assert (layout != NULL);
2251 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
2252 s->name = segment_name;
2256 for (unsigned int i = 0; i < num_sections; ++i)
2258 Object* obj = parameters->options().plugins()->get_elf_object(
2259 section_list[i].handle);
2260 if (obj == NULL || obj->is_dynamic())
2261 return LDPS_BAD_HANDLE;
2262 unsigned int shndx = section_list[i].shndx;
2263 Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
2264 layout->insert_section_segment_map(secn_id, s);
2270 // Register a new_input handler.
2272 static enum ld_plugin_status
2273 register_new_input(ld_plugin_new_input_handler handler)
2275 gold_assert(parameters->options().has_plugins());
2276 parameters->options().plugins()->set_new_input_handler(handler);
2280 #endif // ENABLE_PLUGINS
2282 // Allocate a Pluginobj object of the appropriate size and endianness.
2285 make_sized_plugin_object(const std::string& filename,
2286 Input_file* input_file, off_t offset, off_t filesize)
2288 Pluginobj* obj = NULL;
2290 parameters_force_valid_target();
2291 const Target& target(parameters->target());
2293 if (target.get_size() == 32)
2295 if (target.is_big_endian())
2296 #ifdef HAVE_TARGET_32_BIG
2297 obj = new Sized_pluginobj<32, true>(filename, input_file,
2300 gold_error(_("%s: not configured to support "
2301 "32-bit big-endian object"),
2305 #ifdef HAVE_TARGET_32_LITTLE
2306 obj = new Sized_pluginobj<32, false>(filename, input_file,
2309 gold_error(_("%s: not configured to support "
2310 "32-bit little-endian object"),
2314 else if (target.get_size() == 64)
2316 if (target.is_big_endian())
2317 #ifdef HAVE_TARGET_64_BIG
2318 obj = new Sized_pluginobj<64, true>(filename, input_file,
2321 gold_error(_("%s: not configured to support "
2322 "64-bit big-endian object"),
2326 #ifdef HAVE_TARGET_64_LITTLE
2327 obj = new Sized_pluginobj<64, false>(filename, input_file,
2330 gold_error(_("%s: not configured to support "
2331 "64-bit little-endian object"),
2336 gold_assert(obj != NULL);
2340 } // End namespace gold.