[PATCH 25/57][Arm][GAS] Add support for MVE instruction: vmvn, vqabs and vqneg
[binutils-gdb.git] / gold / plugin.cc
blob0bb277e126d3b4829110b6ce70528bdde8f1b815
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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstdio>
27 #include <cstdarg>
28 #include <cstring>
29 #include <string>
30 #include <vector>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include "libiberty.h"
35 #ifdef ENABLE_PLUGINS
36 #ifdef HAVE_DLFCN_H
37 #include <dlfcn.h>
38 #elif defined (HAVE_WINDOWS_H)
39 #include <windows.h>
40 #else
41 #error Unknown how to handle dynamic-load-libraries.
42 #endif
44 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
46 #define RTLD_NOW 0 /* Dummy value. */
47 static void *
48 dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
50 return LoadLibrary(file);
53 static void *
54 dlsym(void *handle, const char *name)
56 return reinterpret_cast<void *>(
57 GetProcAddress(static_cast<HMODULE>(handle),name));
60 static const char *
61 dlerror(void)
63 return "unable to load dll";
66 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
67 #endif /* ENABLE_PLUGINS */
69 #include "parameters.h"
70 #include "debug.h"
71 #include "errors.h"
72 #include "fileread.h"
73 #include "layout.h"
74 #include "options.h"
75 #include "plugin.h"
76 #include "target.h"
77 #include "readsyms.h"
78 #include "symtab.h"
79 #include "descriptors.h"
80 #include "elfcpp.h"
82 namespace gold
85 #ifdef ENABLE_PLUGINS
87 // The linker's exported interfaces.
89 extern "C"
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,
139 unsigned int* type);
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,
148 size_t* len);
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,
162 uint64_t flags,
163 uint64_t align,
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,
173 uint64_t* secsize);
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);
189 // Plugin methods.
191 // Load one plugin library.
193 void
194 Plugin::load()
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 gold_error(_("%s: could not load plugin library: %s"),
203 this->filename_.c_str(), dlerror());
204 return;
207 // Find the plugin's onload entry point.
208 void* ptr = dlsym(this->handle_, "onload");
209 if (ptr == NULL)
211 gold_error(_("%s: could not find onload entry point"),
212 this->filename_.c_str());
213 return;
215 ld_plugin_onload onload;
216 gold_assert(sizeof(onload) == sizeof(ptr));
217 memcpy(&onload, &ptr, sizeof(ptr));
219 // Get the linker's version number.
220 const char* ver = get_version_string();
221 int major = 0;
222 int minor = 0;
223 sscanf(ver, "%d.%d", &major, &minor);
225 // Allocate and populate a transfer vector.
226 const int tv_fixed_size = 31;
228 int tv_size = this->args_.size() + tv_fixed_size;
229 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
231 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
232 // while processing subsequent entries.
233 int i = 0;
234 tv[i].tv_tag = LDPT_MESSAGE;
235 tv[i].tv_u.tv_message = message;
237 ++i;
238 tv[i].tv_tag = LDPT_API_VERSION;
239 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
241 ++i;
242 tv[i].tv_tag = LDPT_GOLD_VERSION;
243 tv[i].tv_u.tv_val = major * 100 + minor;
245 ++i;
246 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
247 if (parameters->options().relocatable())
248 tv[i].tv_u.tv_val = LDPO_REL;
249 else if (parameters->options().shared())
250 tv[i].tv_u.tv_val = LDPO_DYN;
251 else if (parameters->options().pie())
252 tv[i].tv_u.tv_val = LDPO_PIE;
253 else
254 tv[i].tv_u.tv_val = LDPO_EXEC;
256 ++i;
257 tv[i].tv_tag = LDPT_OUTPUT_NAME;
258 tv[i].tv_u.tv_string = parameters->options().output();
260 for (unsigned int j = 0; j < this->args_.size(); ++j)
262 ++i;
263 tv[i].tv_tag = LDPT_OPTION;
264 tv[i].tv_u.tv_string = this->args_[j].c_str();
267 ++i;
268 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
269 tv[i].tv_u.tv_register_claim_file = register_claim_file;
271 ++i;
272 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
273 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
275 ++i;
276 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
277 tv[i].tv_u.tv_register_cleanup = register_cleanup;
279 ++i;
280 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
281 tv[i].tv_u.tv_add_symbols = add_symbols;
283 ++i;
284 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
285 tv[i].tv_u.tv_get_input_file = get_input_file;
287 ++i;
288 tv[i].tv_tag = LDPT_GET_VIEW;
289 tv[i].tv_u.tv_get_view = get_view;
291 ++i;
292 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
293 tv[i].tv_u.tv_release_input_file = release_input_file;
295 ++i;
296 tv[i].tv_tag = LDPT_GET_SYMBOLS;
297 tv[i].tv_u.tv_get_symbols = get_symbols;
299 ++i;
300 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
301 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
303 ++i;
304 tv[i].tv_tag = LDPT_GET_SYMBOLS_V3;
305 tv[i].tv_u.tv_get_symbols = get_symbols_v3;
307 ++i;
308 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
309 tv[i].tv_u.tv_add_input_file = add_input_file;
311 ++i;
312 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
313 tv[i].tv_u.tv_add_input_library = add_input_library;
315 ++i;
316 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
317 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
319 ++i;
320 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
321 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
323 ++i;
324 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
325 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
327 ++i;
328 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
329 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
331 ++i;
332 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
333 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
335 ++i;
336 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
337 tv[i].tv_u.tv_update_section_order = update_section_order;
339 ++i;
340 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
341 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
343 ++i;
344 tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
345 tv[i].tv_u.tv_allow_unique_segment_for_sections
346 = allow_unique_segment_for_sections;
348 ++i;
349 tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
350 tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
352 ++i;
353 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_ALIGNMENT;
354 tv[i].tv_u.tv_get_input_section_alignment = get_input_section_alignment;
356 ++i;
357 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_SIZE;
358 tv[i].tv_u.tv_get_input_section_size = get_input_section_size;
360 ++i;
361 tv[i].tv_tag = LDPT_REGISTER_NEW_INPUT_HOOK;
362 tv[i].tv_u.tv_register_new_input = register_new_input;
364 ++i;
365 tv[i].tv_tag = LDPT_GET_WRAP_SYMBOLS;
366 tv[i].tv_u.tv_get_wrap_symbols = get_wrap_symbols;
368 ++i;
369 tv[i].tv_tag = LDPT_NULL;
370 tv[i].tv_u.tv_val = 0;
372 gold_assert(i == tv_size - 1);
374 // Call the onload entry point.
375 (*onload)(tv);
377 delete[] tv;
378 #endif // ENABLE_PLUGINS
381 // Call the plugin claim-file handler.
383 inline bool
384 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
386 int claimed = 0;
388 if (this->claim_file_handler_ != NULL)
390 (*this->claim_file_handler_)(plugin_input_file, &claimed);
391 if (claimed)
392 return true;
394 return false;
397 // Call the all-symbols-read handler.
399 inline void
400 Plugin::all_symbols_read()
402 if (this->all_symbols_read_handler_ != NULL)
403 (*this->all_symbols_read_handler_)();
406 // Call the new_input handler.
408 inline void
409 Plugin::new_input(struct ld_plugin_input_file* plugin_input_file)
411 if (this->new_input_handler_ != NULL)
412 (*this->new_input_handler_)(plugin_input_file);
415 // Call the cleanup handler.
417 inline void
418 Plugin::cleanup()
420 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
422 // Set this flag before calling to prevent a recursive plunge
423 // in the event that a plugin's cleanup handler issues a
424 // fatal error.
425 this->cleanup_done_ = true;
426 (*this->cleanup_handler_)();
430 // This task is used to rescan archives as needed.
432 class Plugin_rescan : public Task
434 public:
435 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
436 : this_blocker_(this_blocker), next_blocker_(next_blocker)
439 ~Plugin_rescan()
441 delete this->this_blocker_;
444 Task_token*
445 is_runnable()
447 if (this->this_blocker_->is_blocked())
448 return this->this_blocker_;
449 return NULL;
452 void
453 locks(Task_locker* tl)
454 { tl->add(this, this->next_blocker_); }
456 void
457 run(Workqueue*)
458 { parameters->options().plugins()->rescan(this); }
460 std::string
461 get_name() const
462 { return "Plugin_rescan"; }
464 private:
465 Task_token* this_blocker_;
466 Task_token* next_blocker_;
469 // Plugin_recorder logs plugin actions and saves intermediate files
470 // for later replay.
472 class Plugin_recorder
474 public:
475 Plugin_recorder() : file_count_(0), tempdir_(NULL), logfile_(NULL)
478 bool
479 init();
481 void
482 claimed_file(const std::string& obj_name, off_t offset, off_t filesize,
483 const std::string& plugin_name);
485 void
486 unclaimed_file(const std::string& obj_name, off_t offset, off_t filesize);
488 void
489 replacement_file(const char* name, bool is_lib);
491 void
492 record_symbols(const Object* obj, int nsyms,
493 const struct ld_plugin_symbol* syms);
495 void
496 finish()
497 { ::fclose(this->logfile_); }
499 private:
500 unsigned int file_count_;
501 const char* tempdir_;
502 FILE* logfile_;
505 bool
506 Plugin_recorder::init()
508 // Create a temporary directory where we can stash the log and
509 // copies of replacement files.
510 char dir_template[] = "gold-recording-XXXXXX";
511 #ifdef HAVE_MKDTEMP
512 if (mkdtemp(dir_template) == NULL)
513 return false;
514 #else
515 if (mktemp(dir_template) == NULL)
516 return false;
517 #if defined (_WIN32) && !defined (__CYGWIN32__)
518 if (mkdir(dir_template) != 0)
519 return false;
520 #else
521 if (mkdir(dir_template, 0700) != 0)
522 return false;
523 #endif
524 #endif
526 size_t len = strlen(dir_template) + 1;
527 char* tempdir = new char[len];
528 strncpy(tempdir, dir_template, len);
530 // Create the log file.
531 std::string logname(tempdir);
532 logname.append("/log");
533 FILE* logfile = ::fopen(logname.c_str(), "w");
534 if (logfile == NULL)
535 return false;
537 this->tempdir_ = tempdir;
538 this->logfile_ = logfile;
540 gold_info(_("%s: recording to %s"), program_name, this->tempdir_);
542 return true;
545 void
546 Plugin_recorder::claimed_file(const std::string& obj_name,
547 off_t offset,
548 off_t filesize,
549 const std::string& plugin_name)
551 fprintf(this->logfile_, "PLUGIN: %s\n", plugin_name.c_str());
552 fprintf(this->logfile_, "CLAIMED: %s", obj_name.c_str());
553 if (offset > 0)
554 fprintf(this->logfile_, " @%ld", static_cast<long>(offset));
555 fprintf(this->logfile_, " %ld\n", static_cast<long>(filesize));
558 void
559 Plugin_recorder::unclaimed_file(const std::string& obj_name,
560 off_t offset,
561 off_t filesize)
563 fprintf(this->logfile_, "UNCLAIMED: %s", obj_name.c_str());
564 if (offset > 0)
565 fprintf(this->logfile_, " @%ld", static_cast<long>(offset));
566 fprintf(this->logfile_, " %ld\n", static_cast<long>(filesize));
569 // Make a hard link to INNAME from OUTNAME, if possible.
570 // If not, copy the file.
572 static bool
573 link_or_copy_file(const char* inname, const char* outname)
575 static char buf[4096];
577 #ifdef HAVE_LINK
578 if (::link(inname, outname) == 0)
579 return true;
580 #endif
582 int in = ::open(inname, O_RDONLY);
583 if (in < 0)
585 gold_warning(_("%s: can't open (%s)"), inname, strerror(errno));
586 return false;
588 int out = ::open(outname, O_CREAT | O_TRUNC | O_WRONLY, 0600);
589 if (out < 0)
591 gold_warning(_("%s: can't create (%s)"), outname, strerror(errno));
592 ::close(in);
593 return false;
595 ssize_t len;
596 while ((len = ::read(in, buf, sizeof(buf))) > 0)
598 if (::write(out, buf, len) != len)
600 gold_warning(_("%s: write error while making copy of file (%s)"),
601 inname, strerror(errno));
602 break;
605 ::close(in);
606 ::close(out);
607 return true;
610 void
611 Plugin_recorder::replacement_file(const char* name, bool is_lib)
613 fprintf(this->logfile_, "REPLACEMENT: %s", name);
614 if (is_lib)
615 fprintf(this->logfile_, "(lib)");
616 else
618 char counter[10];
619 const char* basename = lbasename(name);
620 snprintf(counter, sizeof(counter), "%05d", this->file_count_);
621 ++this->file_count_;
622 std::string outname(this->tempdir_);
623 outname.append("/");
624 outname.append(counter);
625 outname.append("-");
626 outname.append(basename);
627 if (link_or_copy_file(name, outname.c_str()))
628 fprintf(this->logfile_, " -> %s", outname.c_str());
630 fprintf(this->logfile_, "\n");
633 void
634 Plugin_recorder::record_symbols(const Object* obj, int nsyms,
635 const struct ld_plugin_symbol* syms)
637 fprintf(this->logfile_, "SYMBOLS: %d %s\n", nsyms, obj->name().c_str());
638 for (int i = 0; i < nsyms; ++i)
640 const struct ld_plugin_symbol* isym = &syms[i];
642 const char* def;
643 switch (isym->def)
645 case LDPK_DEF:
646 def = "D";
647 break;
648 case LDPK_WEAKDEF:
649 def = "WD";
650 break;
651 case LDPK_UNDEF:
652 def = "U";
653 break;
654 case LDPK_WEAKUNDEF:
655 def = "WU";
656 break;
657 case LDPK_COMMON:
658 def = "C";
659 break;
660 default:
661 def = "?";
662 break;
665 char vis;
666 switch (isym->visibility)
668 case LDPV_PROTECTED:
669 vis = 'P';
670 break;
671 case LDPV_INTERNAL:
672 vis = 'I';
673 break;
674 case LDPV_HIDDEN:
675 vis = 'H';
676 break;
677 case LDPV_DEFAULT:
678 vis = 'D';
679 break;
680 default:
681 vis = '?';
682 break;
685 fprintf(this->logfile_, " %5d: %-2s %c %s", i, def, vis, isym->name);
686 if (isym->version != NULL && isym->version[0] != '\0')
687 fprintf(this->logfile_, "@%s", isym->version);
688 if (isym->comdat_key != NULL && isym->comdat_key[0] != '\0')
690 if (strcmp(isym->name, isym->comdat_key) == 0)
691 fprintf(this->logfile_, " [comdat]");
692 else
693 fprintf(this->logfile_, " [comdat: %s]", isym->comdat_key);
695 fprintf(this->logfile_, "\n");
699 // Plugin_manager methods.
701 Plugin_manager::~Plugin_manager()
703 for (Plugin_list::iterator p = this->plugins_.begin();
704 p != this->plugins_.end();
705 ++p)
706 delete *p;
707 this->plugins_.clear();
708 for (Object_list::iterator obj = this->objects_.begin();
709 obj != this->objects_.end();
710 ++obj)
711 delete *obj;
712 this->objects_.clear();
713 delete this->lock_;
714 delete this->recorder_;
717 // Load all plugin libraries.
719 void
720 Plugin_manager::load_plugins(Layout* layout)
722 this->layout_ = layout;
724 if (is_debugging_enabled(DEBUG_PLUGIN))
726 this->recorder_ = new Plugin_recorder();
727 this->recorder_->init();
730 for (this->current_ = this->plugins_.begin();
731 this->current_ != this->plugins_.end();
732 ++this->current_)
733 (*this->current_)->load();
736 // Call the plugin claim-file handlers in turn to see if any claim the file.
738 Pluginobj*
739 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
740 off_t filesize, Object* elf_object)
742 bool lock_initialized = this->initialize_lock_.initialize();
744 gold_assert(lock_initialized);
745 Hold_lock hl(*this->lock_);
747 unsigned int handle = this->objects_.size();
748 this->input_file_ = input_file;
749 this->plugin_input_file_.name = input_file->filename().c_str();
750 this->plugin_input_file_.fd = input_file->file().descriptor();
751 this->plugin_input_file_.offset = offset;
752 this->plugin_input_file_.filesize = filesize;
753 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
754 if (elf_object != NULL)
755 this->objects_.push_back(elf_object);
756 this->in_claim_file_handler_ = true;
758 for (this->current_ = this->plugins_.begin();
759 this->current_ != this->plugins_.end();
760 ++this->current_)
762 // If we aren't yet in replacement phase, allow plugins to claim input
763 // files, otherwise notify the plugin of the new input file, if needed.
764 if (!this->in_replacement_phase_)
766 if ((*this->current_)->claim_file(&this->plugin_input_file_))
768 this->any_claimed_ = true;
769 this->in_claim_file_handler_ = false;
771 if (this->recorder_ != NULL)
773 const std::string& objname = (elf_object == NULL
774 ? input_file->filename()
775 : elf_object->name());
776 this->recorder_->claimed_file(objname,
777 offset, filesize,
778 (*this->current_)->filename());
781 if (this->objects_.size() > handle
782 && this->objects_[handle]->pluginobj() != NULL)
783 return this->objects_[handle]->pluginobj();
785 // If the plugin claimed the file but did not call the
786 // add_symbols callback, we need to create the Pluginobj now.
787 Pluginobj* obj = this->make_plugin_object(handle);
788 return obj;
791 else
793 (*this->current_)->new_input(&this->plugin_input_file_);
797 this->in_claim_file_handler_ = false;
799 if (this->recorder_ != NULL)
800 this->recorder_->unclaimed_file(input_file->filename(), offset, filesize);
802 return NULL;
805 // Save an archive. This is used so that a plugin can add a file
806 // which refers to a symbol which was not previously referenced. In
807 // that case we want to pretend that the symbol was referenced before,
808 // and pull in the archive object.
810 void
811 Plugin_manager::save_archive(Archive* archive)
813 if (this->in_replacement_phase_ || !this->any_claimed_)
814 delete archive;
815 else
816 this->rescannable_.push_back(Rescannable(archive));
819 // Save an Input_group. This is like save_archive.
821 void
822 Plugin_manager::save_input_group(Input_group* input_group)
824 if (this->in_replacement_phase_ || !this->any_claimed_)
825 delete input_group;
826 else
827 this->rescannable_.push_back(Rescannable(input_group));
830 // Call the all-symbols-read handlers.
832 void
833 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
834 Input_objects* input_objects,
835 Symbol_table* symtab,
836 Dirsearch* dirpath, Mapfile* mapfile,
837 Task_token** last_blocker)
839 this->in_replacement_phase_ = true;
840 this->workqueue_ = workqueue;
841 this->task_ = task;
842 this->input_objects_ = input_objects;
843 this->symtab_ = symtab;
844 this->dirpath_ = dirpath;
845 this->mapfile_ = mapfile;
846 this->this_blocker_ = NULL;
848 // Set symbols used in defsym expressions as seen in real ELF.
849 Layout *layout = parameters->options().plugins()->layout();
850 layout->script_options()->set_defsym_uses_in_real_elf(symtab);
851 layout->script_options()->find_defsym_defs(this->defsym_defines_set_);
853 for (this->current_ = this->plugins_.begin();
854 this->current_ != this->plugins_.end();
855 ++this->current_)
856 (*this->current_)->all_symbols_read();
858 if (this->any_added_)
860 Task_token* next_blocker = new Task_token(true);
861 next_blocker->add_blocker();
862 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
863 this->this_blocker_ = next_blocker;
866 *last_blocker = this->this_blocker_;
869 // This is called when we see a new undefined symbol. If we are in
870 // the replacement phase, this means that we may need to rescan some
871 // archives we have previously seen.
873 void
874 Plugin_manager::new_undefined_symbol(Symbol* sym)
876 if (this->in_replacement_phase_)
877 this->undefined_symbols_.push_back(sym);
880 // Rescan archives as needed. This handles the case where a new
881 // object file added by a plugin has an undefined reference to some
882 // symbol defined in an archive.
884 void
885 Plugin_manager::rescan(Task* task)
887 size_t rescan_pos = 0;
888 size_t rescan_size = this->rescannable_.size();
889 while (!this->undefined_symbols_.empty())
891 if (rescan_pos >= rescan_size)
893 this->undefined_symbols_.clear();
894 return;
897 Undefined_symbol_list undefs;
898 undefs.reserve(this->undefined_symbols_.size());
899 this->undefined_symbols_.swap(undefs);
901 size_t min_rescan_pos = rescan_size;
903 for (Undefined_symbol_list::const_iterator p = undefs.begin();
904 p != undefs.end();
905 ++p)
907 if (!(*p)->is_undefined())
908 continue;
910 this->undefined_symbols_.push_back(*p);
912 // Find the first rescan archive which defines this symbol,
913 // starting at the current rescan position. The rescan position
914 // exists so that given -la -lb -lc we don't look for undefined
915 // symbols in -lb back in -la, but instead get the definition
916 // from -lc. Don't bother to look past the current minimum
917 // rescan position.
918 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
920 if (this->rescannable_defines(i, *p))
922 min_rescan_pos = i;
923 break;
928 if (min_rescan_pos >= rescan_size)
930 // We didn't find any rescannable archives which define any
931 // undefined symbols.
932 return;
935 const Rescannable& r(this->rescannable_[min_rescan_pos]);
936 if (r.is_archive)
938 Task_lock_obj<Archive> tl(task, r.u.archive);
939 r.u.archive->add_symbols(this->symtab_, this->layout_,
940 this->input_objects_, this->mapfile_);
942 else
944 size_t next_saw_undefined = this->symtab_->saw_undefined();
945 size_t saw_undefined;
948 saw_undefined = next_saw_undefined;
950 for (Input_group::const_iterator p = r.u.input_group->begin();
951 p != r.u.input_group->end();
952 ++p)
954 Task_lock_obj<Archive> tl(task, *p);
956 (*p)->add_symbols(this->symtab_, this->layout_,
957 this->input_objects_, this->mapfile_);
960 next_saw_undefined = this->symtab_->saw_undefined();
962 while (saw_undefined != next_saw_undefined);
965 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
967 if (this->rescannable_[i].is_archive)
968 delete this->rescannable_[i].u.archive;
969 else
970 delete this->rescannable_[i].u.input_group;
973 rescan_pos = min_rescan_pos + 1;
977 // Return whether the rescannable at index I defines SYM.
979 bool
980 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
982 const Rescannable& r(this->rescannable_[i]);
983 if (r.is_archive)
984 return r.u.archive->defines_symbol(sym);
985 else
987 for (Input_group::const_iterator p = r.u.input_group->begin();
988 p != r.u.input_group->end();
989 ++p)
991 if ((*p)->defines_symbol(sym))
992 return true;
994 return false;
998 // Layout deferred objects.
1000 void
1001 Plugin_manager::layout_deferred_objects()
1003 Deferred_layout_list::iterator obj;
1005 for (obj = this->deferred_layout_objects_.begin();
1006 obj != this->deferred_layout_objects_.end();
1007 ++obj)
1009 // Lock the object so we can read from it. This is only called
1010 // single-threaded from queue_middle_tasks, so it is OK to lock.
1011 // Unfortunately we have no way to pass in a Task token.
1012 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
1013 Task_lock_obj<Object> tl(dummy_task, *obj);
1014 (*obj)->layout_deferred_sections(this->layout_);
1018 // Call the cleanup handlers.
1020 void
1021 Plugin_manager::cleanup()
1023 if (this->any_added_)
1025 // If any input files were added, close all the input files.
1026 // This is because the plugin may want to remove them, and on
1027 // Windows you are not allowed to remove an open file.
1028 close_all_descriptors();
1031 for (this->current_ = this->plugins_.begin();
1032 this->current_ != this->plugins_.end();
1033 ++this->current_)
1034 (*this->current_)->cleanup();
1037 // Make a new Pluginobj object. This is called when the plugin calls
1038 // the add_symbols API.
1040 Pluginobj*
1041 Plugin_manager::make_plugin_object(unsigned int handle)
1043 // Make sure we aren't asked to make an object for the same handle twice.
1044 if (this->objects_.size() != handle
1045 && this->objects_[handle]->pluginobj() != NULL)
1046 return NULL;
1048 const std::string* filename = &this->input_file_->filename();
1050 // If the elf object for this file was pushed into the objects_ vector,
1051 // use its filename, then delete it to make room for the Pluginobj as
1052 // this file is claimed.
1053 if (this->objects_.size() != handle)
1055 filename = &this->objects_.back()->name();
1056 this->objects_.pop_back();
1059 Pluginobj* obj = make_sized_plugin_object(*filename,
1060 this->input_file_,
1061 this->plugin_input_file_.offset,
1062 this->plugin_input_file_.filesize);
1066 this->objects_.push_back(obj);
1067 return obj;
1070 // Get the input file information with an open (possibly re-opened)
1071 // file descriptor.
1073 ld_plugin_status
1074 Plugin_manager::get_input_file(unsigned int handle,
1075 struct ld_plugin_input_file* file)
1077 Pluginobj* obj = this->object(handle)->pluginobj();
1078 if (obj == NULL)
1079 return LDPS_BAD_HANDLE;
1081 obj->lock(this->task_);
1082 file->name = obj->filename().c_str();
1083 file->fd = obj->descriptor();
1084 file->offset = obj->offset();
1085 file->filesize = obj->filesize();
1086 file->handle = reinterpret_cast<void*>(handle);
1087 return LDPS_OK;
1090 // Release the input file.
1092 ld_plugin_status
1093 Plugin_manager::release_input_file(unsigned int handle)
1095 if (this->object(handle) == NULL)
1096 return LDPS_BAD_HANDLE;
1098 Pluginobj* obj = this->object(handle)->pluginobj();
1100 if (obj == NULL)
1101 return LDPS_BAD_HANDLE;
1103 obj->unlock(this->task_);
1104 return LDPS_OK;
1107 // Get the elf object corresponding to the handle. Return NULL if we
1108 // found a Pluginobj instead.
1110 Object*
1111 Plugin_manager::get_elf_object(const void* handle)
1113 Object* obj = this->object(
1114 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1116 // The object should not be a Pluginobj.
1117 if (obj == NULL
1118 || obj->pluginobj() != NULL)
1119 return NULL;
1121 return obj;
1124 ld_plugin_status
1125 Plugin_manager::get_view(unsigned int handle, const void **viewp)
1127 off_t offset;
1128 size_t filesize;
1129 Input_file *input_file;
1130 if (this->in_claim_file_handler_)
1132 // We are being called from the claim_file hook.
1133 const struct ld_plugin_input_file &f = this->plugin_input_file_;
1134 offset = f.offset;
1135 filesize = f.filesize;
1136 input_file = this->input_file_;
1138 else
1140 // An already claimed file.
1141 if (this->object(handle) == NULL)
1142 return LDPS_BAD_HANDLE;
1143 Pluginobj* obj = this->object(handle)->pluginobj();
1144 if (obj == NULL)
1145 return LDPS_BAD_HANDLE;
1146 offset = obj->offset();
1147 filesize = obj->filesize();
1148 input_file = obj->input_file();
1150 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
1151 false);
1152 return LDPS_OK;
1155 // Add a new library path.
1157 ld_plugin_status
1158 Plugin_manager::set_extra_library_path(const char* path)
1160 this->extra_search_path_ = std::string(path);
1161 return LDPS_OK;
1164 // Add a new input file.
1166 ld_plugin_status
1167 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
1169 Input_file_argument file(pathname,
1170 (is_lib
1171 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
1172 : Input_file_argument::INPUT_FILE_TYPE_FILE),
1173 (is_lib
1174 ? this->extra_search_path_.c_str()
1175 : ""),
1176 false,
1177 this->options_);
1178 Input_argument* input_argument = new Input_argument(file);
1179 Task_token* next_blocker = new Task_token(true);
1180 next_blocker->add_blocker();
1181 if (parameters->incremental())
1182 gold_error(_("input files added by plug-ins in --incremental mode not "
1183 "supported yet"));
1185 if (this->recorder_ != NULL)
1186 this->recorder_->replacement_file(pathname, is_lib);
1188 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
1189 this->symtab_,
1190 this->layout_,
1191 this->dirpath_,
1193 this->mapfile_,
1194 input_argument,
1195 NULL,
1196 NULL,
1197 this->this_blocker_,
1198 next_blocker));
1199 this->this_blocker_ = next_blocker;
1200 this->any_added_ = true;
1201 return LDPS_OK;
1204 // Class Pluginobj.
1206 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
1207 off_t offset, off_t filesize)
1208 : Object(name, input_file, false, offset),
1209 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
1213 // Return TRUE if a defined symbol is referenced from outside the
1214 // universe of claimed objects. Only references from relocatable,
1215 // non-IR (unclaimed) objects count as a reference. References from
1216 // dynamic objects count only as "visible".
1218 static inline bool
1219 is_referenced_from_outside(Symbol* lsym)
1221 if (lsym->in_real_elf())
1222 return true;
1223 if (parameters->options().relocatable())
1224 return true;
1225 if (parameters->options().is_undefined(lsym->name()))
1226 return true;
1227 return false;
1230 // Return TRUE if a defined symbol might be reachable from outside the
1231 // load module.
1233 static inline bool
1234 is_visible_from_outside(Symbol* lsym)
1236 if (lsym->in_dyn())
1237 return true;
1238 if (parameters->options().export_dynamic() || parameters->options().shared()
1239 || parameters->options().in_dynamic_list(lsym->name())
1240 || parameters->options().is_export_dynamic_symbol(lsym->name()))
1241 return lsym->is_externally_visible();
1242 return false;
1245 // Get symbol resolution info.
1247 ld_plugin_status
1248 Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
1249 int nsyms,
1250 ld_plugin_symbol* syms,
1251 int version) const
1253 // For version 1 of this interface, we cannot use
1254 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
1255 // instead.
1256 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
1257 = (version > 1
1258 ? LDPR_PREVAILING_DEF_IRONLY_EXP
1259 : LDPR_PREVAILING_DEF);
1261 if (nsyms > this->nsyms_)
1262 return LDPS_NO_SYMS;
1264 if (static_cast<size_t>(nsyms) > this->symbols_.size())
1266 // We never decided to include this object. We mark all symbols as
1267 // preempted.
1268 gold_assert(this->symbols_.size() == 0);
1269 for (int i = 0; i < nsyms; i++)
1270 syms[i].resolution = LDPR_PREEMPTED_REG;
1271 return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
1274 Plugin_manager* plugins = parameters->options().plugins();
1275 for (int i = 0; i < nsyms; i++)
1277 ld_plugin_symbol* isym = &syms[i];
1278 Symbol* lsym = this->symbols_[i];
1279 if (lsym->is_forwarder())
1280 lsym = symtab->resolve_forwards(lsym);
1281 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
1283 if (plugins->is_defsym_def(lsym->name()))
1285 // The symbol is redefined via defsym.
1286 res = LDPR_PREEMPTED_REG;
1288 else if (lsym->is_undefined())
1290 // The symbol remains undefined.
1291 res = LDPR_UNDEF;
1293 else if (isym->def == LDPK_UNDEF
1294 || isym->def == LDPK_WEAKUNDEF
1295 || isym->def == LDPK_COMMON)
1297 // The original symbol was undefined or common.
1298 if (lsym->source() != Symbol::FROM_OBJECT)
1299 res = LDPR_RESOLVED_EXEC;
1300 else if (lsym->object()->pluginobj() == this)
1302 if (is_referenced_from_outside(lsym))
1303 res = LDPR_PREVAILING_DEF;
1304 else if (is_visible_from_outside(lsym))
1305 res = ldpr_prevailing_def_ironly_exp;
1306 else
1307 res = LDPR_PREVAILING_DEF_IRONLY;
1309 else if (lsym->object()->pluginobj() != NULL)
1310 res = LDPR_RESOLVED_IR;
1311 else if (lsym->object()->is_dynamic())
1312 res = LDPR_RESOLVED_DYN;
1313 else
1314 res = LDPR_RESOLVED_EXEC;
1316 else
1318 // The original symbol was a definition.
1319 if (lsym->source() != Symbol::FROM_OBJECT)
1320 res = LDPR_PREEMPTED_REG;
1321 else if (lsym->object() == static_cast<const Object*>(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;
1327 else
1328 res = LDPR_PREVAILING_DEF_IRONLY;
1330 else
1331 res = (lsym->object()->pluginobj() != NULL
1332 ? LDPR_PREEMPTED_IR
1333 : LDPR_PREEMPTED_REG);
1335 isym->resolution = res;
1337 return LDPS_OK;
1340 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1341 // should be kept.
1343 bool
1344 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
1346 std::pair<Comdat_map::iterator, bool> ins =
1347 this->comdat_map_.insert(std::make_pair(comdat_key, false));
1349 // If this is the first time we've seen this comdat key, ask the
1350 // layout object whether it should be included.
1351 if (ins.second)
1352 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1353 NULL, 0, true,
1354 true, NULL);
1356 return ins.first->second;
1359 // Class Sized_pluginobj.
1361 template<int size, bool big_endian>
1362 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1363 const std::string& name,
1364 Input_file* input_file,
1365 off_t offset,
1366 off_t filesize)
1367 : Pluginobj(name, input_file, offset, filesize)
1371 // Read the symbols. Not used for plugin objects.
1373 template<int size, bool big_endian>
1374 void
1375 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1377 gold_unreachable();
1380 // Lay out the input sections. Not used for plugin objects.
1382 template<int size, bool big_endian>
1383 void
1384 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1385 Read_symbols_data*)
1387 gold_unreachable();
1390 // Add the symbols to the symbol table.
1392 template<int size, bool big_endian>
1393 void
1394 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1395 Read_symbols_data*,
1396 Layout* layout)
1398 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1399 unsigned char symbuf[sym_size];
1400 elfcpp::Sym<size, big_endian> sym(symbuf);
1401 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1403 Plugin_recorder* recorder = parameters->options().plugins()->recorder();
1404 if (recorder != NULL)
1405 recorder->record_symbols(this, this->nsyms_, this->syms_);
1407 this->symbols_.resize(this->nsyms_);
1409 for (int i = 0; i < this->nsyms_; ++i)
1411 const struct ld_plugin_symbol* isym = &this->syms_[i];
1412 const char* name = isym->name;
1413 const char* ver = isym->version;
1414 elfcpp::Elf_Half shndx;
1415 elfcpp::STB bind;
1416 elfcpp::STV vis;
1418 if (name != NULL && name[0] == '\0')
1419 name = NULL;
1420 if (ver != NULL && ver[0] == '\0')
1421 ver = NULL;
1423 switch (isym->def)
1425 case LDPK_WEAKDEF:
1426 case LDPK_WEAKUNDEF:
1427 bind = elfcpp::STB_WEAK;
1428 break;
1429 case LDPK_DEF:
1430 case LDPK_UNDEF:
1431 case LDPK_COMMON:
1432 default:
1433 bind = elfcpp::STB_GLOBAL;
1434 break;
1437 switch (isym->def)
1439 case LDPK_DEF:
1440 case LDPK_WEAKDEF:
1441 // We use an arbitrary section number for a defined symbol.
1442 shndx = 1;
1443 break;
1444 case LDPK_COMMON:
1445 shndx = elfcpp::SHN_COMMON;
1446 break;
1447 case LDPK_UNDEF:
1448 case LDPK_WEAKUNDEF:
1449 default:
1450 shndx = elfcpp::SHN_UNDEF;
1451 break;
1454 switch (isym->visibility)
1456 case LDPV_PROTECTED:
1457 vis = elfcpp::STV_PROTECTED;
1458 break;
1459 case LDPV_INTERNAL:
1460 vis = elfcpp::STV_INTERNAL;
1461 break;
1462 case LDPV_HIDDEN:
1463 vis = elfcpp::STV_HIDDEN;
1464 break;
1465 case LDPV_DEFAULT:
1466 default:
1467 vis = elfcpp::STV_DEFAULT;
1468 break;
1471 if (isym->comdat_key != NULL
1472 && isym->comdat_key[0] != '\0'
1473 && !this->include_comdat_group(isym->comdat_key, layout))
1474 shndx = elfcpp::SHN_UNDEF;
1476 osym.put_st_name(0);
1477 osym.put_st_value(0);
1478 osym.put_st_size(0);
1479 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1480 osym.put_st_other(vis, 0);
1481 osym.put_st_shndx(shndx);
1483 this->symbols_[i] =
1484 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1488 template<int size, bool big_endian>
1489 Archive::Should_include
1490 Sized_pluginobj<size, big_endian>::do_should_include_member(
1491 Symbol_table* symtab,
1492 Layout* layout,
1493 Read_symbols_data*,
1494 std::string* why)
1496 char* tmpbuf = NULL;
1497 size_t tmpbuflen = 0;
1499 for (int i = 0; i < this->nsyms_; ++i)
1501 const struct ld_plugin_symbol& sym = this->syms_[i];
1502 if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
1503 continue;
1504 const char* name = sym.name;
1505 Symbol* symbol;
1506 Archive::Should_include t = Archive::should_include_member(symtab,
1507 layout,
1508 name,
1509 &symbol, why,
1510 &tmpbuf,
1511 &tmpbuflen);
1512 if (t == Archive::SHOULD_INCLUDE_YES)
1514 if (tmpbuf != NULL)
1515 free(tmpbuf);
1516 return t;
1519 if (tmpbuf != NULL)
1520 free(tmpbuf);
1521 return Archive::SHOULD_INCLUDE_UNKNOWN;
1524 // Iterate over global symbols, calling a visitor class V for each.
1526 template<int size, bool big_endian>
1527 void
1528 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1529 Read_symbols_data*,
1530 Library_base::Symbol_visitor_base* v)
1532 for (int i = 0; i < this->nsyms_; ++i)
1534 const struct ld_plugin_symbol& sym = this->syms_[i];
1535 if (sym.def != LDPK_UNDEF)
1536 v->visit(sym.name);
1540 // Iterate over local symbols, calling a visitor class V for each GOT offset
1541 // associated with a local symbol.
1542 template<int size, bool big_endian>
1543 void
1544 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1545 Got_offset_list::Visitor*) const
1547 gold_unreachable();
1550 // Get the size of a section. Not used for plugin objects.
1552 template<int size, bool big_endian>
1553 uint64_t
1554 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1556 gold_unreachable();
1557 return 0;
1560 // Get the name of a section. Not used for plugin objects.
1562 template<int size, bool big_endian>
1563 std::string
1564 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
1566 gold_unreachable();
1567 return std::string();
1570 // Return a view of the contents of a section. Not used for plugin objects.
1572 template<int size, bool big_endian>
1573 const unsigned char*
1574 Sized_pluginobj<size, big_endian>::do_section_contents(
1575 unsigned int,
1576 section_size_type*,
1577 bool)
1579 gold_unreachable();
1580 return NULL;
1583 // Return section flags. Not used for plugin objects.
1585 template<int size, bool big_endian>
1586 uint64_t
1587 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1589 gold_unreachable();
1590 return 0;
1593 // Return section entsize. Not used for plugin objects.
1595 template<int size, bool big_endian>
1596 uint64_t
1597 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1599 gold_unreachable();
1600 return 0;
1603 // Return section address. Not used for plugin objects.
1605 template<int size, bool big_endian>
1606 uint64_t
1607 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1609 gold_unreachable();
1610 return 0;
1613 // Return section type. Not used for plugin objects.
1615 template<int size, bool big_endian>
1616 unsigned int
1617 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1619 gold_unreachable();
1620 return 0;
1623 // Return the section link field. Not used for plugin objects.
1625 template<int size, bool big_endian>
1626 unsigned int
1627 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1629 gold_unreachable();
1630 return 0;
1633 // Return the section link field. Not used for plugin objects.
1635 template<int size, bool big_endian>
1636 unsigned int
1637 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1639 gold_unreachable();
1640 return 0;
1643 // Return the section alignment. Not used for plugin objects.
1645 template<int size, bool big_endian>
1646 uint64_t
1647 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1649 gold_unreachable();
1650 return 0;
1653 // Return the Xindex structure to use. Not used for plugin objects.
1655 template<int size, bool big_endian>
1656 Xindex*
1657 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1659 gold_unreachable();
1660 return NULL;
1663 // Get symbol counts. Don't count plugin objects; the replacement
1664 // files will provide the counts.
1666 template<int size, bool big_endian>
1667 void
1668 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1669 const Symbol_table*,
1670 size_t* defined,
1671 size_t* used) const
1673 *defined = 0;
1674 *used = 0;
1677 // Get symbols. Not used for plugin objects.
1679 template<int size, bool big_endian>
1680 const Object::Symbols*
1681 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1683 gold_unreachable();
1686 // Class Plugin_finish. This task runs after all replacement files have
1687 // been added. For now, it's a placeholder for a possible plugin API
1688 // to allow the plugin to release most of its resources. The cleanup
1689 // handlers must be called later, because they can remove the temporary
1690 // object files that are needed until the end of the link.
1692 class Plugin_finish : public Task
1694 public:
1695 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1696 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1699 ~Plugin_finish()
1701 if (this->this_blocker_ != NULL)
1702 delete this->this_blocker_;
1705 Task_token*
1706 is_runnable()
1708 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1709 return this->this_blocker_;
1710 return NULL;
1713 void
1714 locks(Task_locker* tl)
1715 { tl->add(this, this->next_blocker_); }
1717 void
1718 run(Workqueue*)
1720 Plugin_manager* plugins = parameters->options().plugins();
1721 gold_assert(plugins != NULL);
1722 // We could call early cleanup handlers here.
1723 if (plugins->recorder())
1724 plugins->recorder()->finish();
1727 std::string
1728 get_name() const
1729 { return "Plugin_finish"; }
1731 private:
1732 Task_token* this_blocker_;
1733 Task_token* next_blocker_;
1736 // Class Plugin_hook.
1738 Plugin_hook::~Plugin_hook()
1742 // Return whether a Plugin_hook task is runnable.
1744 Task_token*
1745 Plugin_hook::is_runnable()
1747 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1748 return this->this_blocker_;
1749 return NULL;
1752 // Return a Task_locker for a Plugin_hook task. We don't need any
1753 // locks here.
1755 void
1756 Plugin_hook::locks(Task_locker*)
1760 // Run the "all symbols read" plugin hook.
1762 void
1763 Plugin_hook::run(Workqueue* workqueue)
1765 gold_assert(this->options_.has_plugins());
1766 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1767 if (start_sym != NULL)
1768 start_sym->set_in_real_elf();
1770 this->options_.plugins()->all_symbols_read(workqueue,
1771 this,
1772 this->input_objects_,
1773 this->symtab_,
1774 this->dirpath_,
1775 this->mapfile_,
1776 &this->this_blocker_);
1777 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1778 this->next_blocker_));
1781 // The C interface routines called by the plugins.
1783 #ifdef ENABLE_PLUGINS
1785 // Register a claim-file handler.
1787 static enum ld_plugin_status
1788 register_claim_file(ld_plugin_claim_file_handler handler)
1790 gold_assert(parameters->options().has_plugins());
1791 parameters->options().plugins()->set_claim_file_handler(handler);
1792 return LDPS_OK;
1795 // Register an all-symbols-read handler.
1797 static enum ld_plugin_status
1798 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1800 gold_assert(parameters->options().has_plugins());
1801 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1802 return LDPS_OK;
1805 // Register a cleanup handler.
1807 static enum ld_plugin_status
1808 register_cleanup(ld_plugin_cleanup_handler handler)
1810 gold_assert(parameters->options().has_plugins());
1811 parameters->options().plugins()->set_cleanup_handler(handler);
1812 return LDPS_OK;
1815 // Add symbols from a plugin-claimed input file.
1817 static enum ld_plugin_status
1818 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1820 gold_assert(parameters->options().has_plugins());
1821 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1822 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1823 if (obj == NULL)
1824 return LDPS_ERR;
1825 obj->store_incoming_symbols(nsyms, syms);
1826 return LDPS_OK;
1829 // Get the input file information with an open (possibly re-opened)
1830 // file descriptor.
1832 static enum ld_plugin_status
1833 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1835 gold_assert(parameters->options().has_plugins());
1836 unsigned int obj_index =
1837 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1838 return parameters->options().plugins()->get_input_file(obj_index, file);
1841 // Release the input file.
1843 static enum ld_plugin_status
1844 release_input_file(const void* handle)
1846 gold_assert(parameters->options().has_plugins());
1847 unsigned int obj_index =
1848 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1849 return parameters->options().plugins()->release_input_file(obj_index);
1852 static enum ld_plugin_status
1853 get_view(const void *handle, const void **viewp)
1855 gold_assert(parameters->options().has_plugins());
1856 unsigned int obj_index =
1857 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1858 return parameters->options().plugins()->get_view(obj_index, viewp);
1861 // Get the symbol resolution info for a plugin-claimed input file.
1863 static enum ld_plugin_status
1864 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1866 gold_assert(parameters->options().has_plugins());
1867 Plugin_manager* plugins = parameters->options().plugins();
1868 Object* obj = plugins->object(
1869 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1870 if (obj == NULL)
1871 return LDPS_ERR;
1872 Pluginobj* plugin_obj = obj->pluginobj();
1873 if (plugin_obj == NULL)
1874 return LDPS_ERR;
1875 Symbol_table* symtab = plugins->symtab();
1876 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
1879 // Version 2 of the above. The only difference is that this version
1880 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1882 static enum ld_plugin_status
1883 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1885 gold_assert(parameters->options().has_plugins());
1886 Plugin_manager* plugins = parameters->options().plugins();
1887 Object* obj = plugins->object(
1888 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1889 if (obj == NULL)
1890 return LDPS_ERR;
1891 Pluginobj* plugin_obj = obj->pluginobj();
1892 if (plugin_obj == NULL)
1893 return LDPS_ERR;
1894 Symbol_table* symtab = plugins->symtab();
1895 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
1898 // Version 3 of the above. The only difference from v2 is that it
1899 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1900 // decided to include.
1902 static enum ld_plugin_status
1903 get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
1905 gold_assert(parameters->options().has_plugins());
1906 Plugin_manager* plugins = parameters->options().plugins();
1907 Object* obj = plugins->object(
1908 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1909 if (obj == NULL)
1910 return LDPS_ERR;
1911 Pluginobj* plugin_obj = obj->pluginobj();
1912 if (plugin_obj == NULL)
1913 return LDPS_ERR;
1914 Symbol_table* symtab = plugins->symtab();
1915 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
1918 // Add a new (real) input file generated by a plugin.
1920 static enum ld_plugin_status
1921 add_input_file(const char* pathname)
1923 gold_assert(parameters->options().has_plugins());
1924 return parameters->options().plugins()->add_input_file(pathname, false);
1927 // Add a new (real) library required by a plugin.
1929 static enum ld_plugin_status
1930 add_input_library(const char* pathname)
1932 gold_assert(parameters->options().has_plugins());
1933 return parameters->options().plugins()->add_input_file(pathname, true);
1936 // Set the extra library path to be used by libraries added via
1937 // add_input_library
1939 static enum ld_plugin_status
1940 set_extra_library_path(const char* path)
1942 gold_assert(parameters->options().has_plugins());
1943 return parameters->options().plugins()->set_extra_library_path(path);
1946 // Issue a diagnostic message from a plugin.
1948 static enum ld_plugin_status
1949 message(int level, const char* format, ...)
1951 va_list args;
1952 va_start(args, format);
1954 switch (level)
1956 case LDPL_INFO:
1957 parameters->errors()->info(format, args);
1958 break;
1959 case LDPL_WARNING:
1960 parameters->errors()->warning(format, args);
1961 break;
1962 case LDPL_ERROR:
1963 default:
1964 parameters->errors()->error(format, args);
1965 break;
1966 case LDPL_FATAL:
1967 parameters->errors()->fatal(format, args);
1968 break;
1971 va_end(args);
1972 return LDPS_OK;
1975 // Get the section count of the object corresponding to the handle. This
1976 // plugin interface can only be called in the claim_file handler of the plugin.
1978 static enum ld_plugin_status
1979 get_input_section_count(const void* handle, unsigned int* count)
1981 gold_assert(parameters->options().has_plugins());
1983 if (!parameters->options().plugins()->in_claim_file_handler())
1984 return LDPS_ERR;
1986 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1988 if (obj == NULL)
1989 return LDPS_ERR;
1991 *count = obj->shnum();
1992 return LDPS_OK;
1995 // Get the type of the specified section in the object corresponding
1996 // to the handle. This plugin interface can only be called in the
1997 // claim_file handler of the plugin.
1999 static enum ld_plugin_status
2000 get_input_section_type(const struct ld_plugin_section section,
2001 unsigned int* type)
2003 gold_assert(parameters->options().has_plugins());
2005 if (!parameters->options().plugins()->in_claim_file_handler())
2006 return LDPS_ERR;
2008 Object* obj
2009 = parameters->options().plugins()->get_elf_object(section.handle);
2011 if (obj == NULL)
2012 return LDPS_BAD_HANDLE;
2014 *type = obj->section_type(section.shndx);
2015 return LDPS_OK;
2018 // Get the name of the specified section in the object corresponding
2019 // to the handle. This plugin interface can only be called in the
2020 // claim_file handler of the plugin.
2022 static enum ld_plugin_status
2023 get_input_section_name(const struct ld_plugin_section section,
2024 char** section_name_ptr)
2026 gold_assert(parameters->options().has_plugins());
2028 if (!parameters->options().plugins()->in_claim_file_handler())
2029 return LDPS_ERR;
2031 Object* obj
2032 = parameters->options().plugins()->get_elf_object(section.handle);
2034 if (obj == NULL)
2035 return LDPS_BAD_HANDLE;
2037 // Check if the object is locked before getting the section name.
2038 gold_assert(obj->is_locked());
2040 const std::string section_name = obj->section_name(section.shndx);
2041 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
2042 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
2043 return LDPS_OK;
2046 // Get the contents of the specified section in the object corresponding
2047 // to the handle. This plugin interface can only be called in the
2048 // claim_file handler of the plugin.
2050 static enum ld_plugin_status
2051 get_input_section_contents(const struct ld_plugin_section section,
2052 const unsigned char** section_contents_ptr,
2053 size_t* len)
2055 gold_assert(parameters->options().has_plugins());
2057 if (!parameters->options().plugins()->in_claim_file_handler())
2058 return LDPS_ERR;
2060 Object* obj
2061 = parameters->options().plugins()->get_elf_object(section.handle);
2063 if (obj == NULL)
2064 return LDPS_BAD_HANDLE;
2066 // Check if the object is locked before getting the section contents.
2067 gold_assert(obj->is_locked());
2069 section_size_type plen;
2070 *section_contents_ptr
2071 = obj->section_contents(section.shndx, &plen, false);
2072 *len = plen;
2073 return LDPS_OK;
2076 // Get the alignment of the specified section in the object corresponding
2077 // to the handle. This plugin interface can only be called in the
2078 // claim_file handler of the plugin.
2080 static enum ld_plugin_status
2081 get_input_section_alignment(const struct ld_plugin_section section,
2082 unsigned int* addralign)
2084 gold_assert(parameters->options().has_plugins());
2086 if (!parameters->options().plugins()->in_claim_file_handler())
2087 return LDPS_ERR;
2089 Object* obj
2090 = parameters->options().plugins()->get_elf_object(section.handle);
2092 if (obj == NULL)
2093 return LDPS_BAD_HANDLE;
2095 *addralign = obj->section_addralign(section.shndx);
2096 return LDPS_OK;
2099 // Get the size of the specified section in the object corresponding
2100 // to the handle. This plugin interface can only be called in the
2101 // claim_file handler of the plugin.
2103 static enum ld_plugin_status
2104 get_input_section_size(const struct ld_plugin_section section,
2105 uint64_t* secsize)
2107 gold_assert(parameters->options().has_plugins());
2109 if (!parameters->options().plugins()->in_claim_file_handler())
2110 return LDPS_ERR;
2112 Object* obj
2113 = parameters->options().plugins()->get_elf_object(section.handle);
2115 if (obj == NULL)
2116 return LDPS_BAD_HANDLE;
2118 *secsize = obj->section_size(section.shndx);
2119 return LDPS_OK;
2122 static enum ld_plugin_status
2123 get_wrap_symbols(uint64_t *count, const char ***wrap_symbols)
2125 gold_assert(parameters->options().has_plugins());
2126 *count = parameters->options().wrap_size();
2128 if (*count == 0)
2129 return LDPS_OK;
2131 *wrap_symbols = new const char *[*count];
2132 int i = 0;
2133 for (options::String_set::const_iterator
2134 it = parameters->options().wrap_begin();
2135 it != parameters->options().wrap_end(); ++it, ++i) {
2136 (*wrap_symbols)[i] = it->c_str();
2138 return LDPS_OK;
2142 // Specify the ordering of sections in the final layout. The sections are
2143 // specified as (handle,shndx) pairs in the two arrays in the order in
2144 // which they should appear in the final layout.
2146 static enum ld_plugin_status
2147 update_section_order(const struct ld_plugin_section* section_list,
2148 unsigned int num_sections)
2150 gold_assert(parameters->options().has_plugins());
2152 if (num_sections == 0)
2153 return LDPS_OK;
2155 if (section_list == NULL)
2156 return LDPS_ERR;
2158 Layout* layout = parameters->options().plugins()->layout();
2159 gold_assert (layout != NULL);
2161 std::map<Section_id, unsigned int>* order_map
2162 = layout->get_section_order_map();
2164 /* Store the mapping from Section_id to section position in layout's
2165 order_map to consult after output sections are added. */
2166 for (unsigned int i = 0; i < num_sections; ++i)
2168 Object* obj = parameters->options().plugins()->get_elf_object(
2169 section_list[i].handle);
2170 if (obj == NULL || obj->is_dynamic())
2171 return LDPS_BAD_HANDLE;
2172 unsigned int shndx = section_list[i].shndx;
2173 Section_id secn_id(static_cast<Relobj*>(obj), shndx);
2174 (*order_map)[secn_id] = i + 1;
2177 return LDPS_OK;
2180 // Let the linker know that the sections could be reordered.
2182 static enum ld_plugin_status
2183 allow_section_ordering()
2185 gold_assert(parameters->options().has_plugins());
2186 Layout* layout = parameters->options().plugins()->layout();
2187 layout->set_section_ordering_specified();
2188 return LDPS_OK;
2191 // Let the linker know that a subset of sections could be mapped
2192 // to a unique segment.
2194 static enum ld_plugin_status
2195 allow_unique_segment_for_sections()
2197 gold_assert(parameters->options().has_plugins());
2198 Layout* layout = parameters->options().plugins()->layout();
2199 layout->set_unique_segment_for_sections_specified();
2200 return LDPS_OK;
2203 // This function should map the list of sections specified in the
2204 // SECTION_LIST to a unique segment. ELF segments do not have names
2205 // and the NAME is used to identify Output Section which should contain
2206 // the list of sections. This Output Section will then be mapped to
2207 // a unique segment. FLAGS is used to specify if any additional segment
2208 // flags need to be set. For instance, a specific segment flag can be
2209 // set to identify this segment. Unsetting segment flags is not possible.
2210 // ALIGN specifies the alignment of the segment.
2212 static enum ld_plugin_status
2213 unique_segment_for_sections(const char* segment_name,
2214 uint64_t flags,
2215 uint64_t align,
2216 const struct ld_plugin_section* section_list,
2217 unsigned int num_sections)
2219 gold_assert(parameters->options().has_plugins());
2221 if (num_sections == 0)
2222 return LDPS_OK;
2224 if (section_list == NULL)
2225 return LDPS_ERR;
2227 Layout* layout = parameters->options().plugins()->layout();
2228 gold_assert (layout != NULL);
2230 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
2231 s->name = segment_name;
2232 s->flags = flags;
2233 s->align = align;
2235 for (unsigned int i = 0; i < num_sections; ++i)
2237 Object* obj = parameters->options().plugins()->get_elf_object(
2238 section_list[i].handle);
2239 if (obj == NULL || obj->is_dynamic())
2240 return LDPS_BAD_HANDLE;
2241 unsigned int shndx = section_list[i].shndx;
2242 Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
2243 layout->insert_section_segment_map(secn_id, s);
2246 return LDPS_OK;
2249 // Register a new_input handler.
2251 static enum ld_plugin_status
2252 register_new_input(ld_plugin_new_input_handler handler)
2254 gold_assert(parameters->options().has_plugins());
2255 parameters->options().plugins()->set_new_input_handler(handler);
2256 return LDPS_OK;
2259 #endif // ENABLE_PLUGINS
2261 // Allocate a Pluginobj object of the appropriate size and endianness.
2263 static Pluginobj*
2264 make_sized_plugin_object(const std::string& filename,
2265 Input_file* input_file, off_t offset, off_t filesize)
2267 Pluginobj* obj = NULL;
2269 parameters_force_valid_target();
2270 const Target& target(parameters->target());
2272 if (target.get_size() == 32)
2274 if (target.is_big_endian())
2275 #ifdef HAVE_TARGET_32_BIG
2276 obj = new Sized_pluginobj<32, true>(filename, input_file,
2277 offset, filesize);
2278 #else
2279 gold_error(_("%s: not configured to support "
2280 "32-bit big-endian object"),
2281 filename.c_str());
2282 #endif
2283 else
2284 #ifdef HAVE_TARGET_32_LITTLE
2285 obj = new Sized_pluginobj<32, false>(filename, input_file,
2286 offset, filesize);
2287 #else
2288 gold_error(_("%s: not configured to support "
2289 "32-bit little-endian object"),
2290 filename.c_str());
2291 #endif
2293 else if (target.get_size() == 64)
2295 if (target.is_big_endian())
2296 #ifdef HAVE_TARGET_64_BIG
2297 obj = new Sized_pluginobj<64, true>(filename, input_file,
2298 offset, filesize);
2299 #else
2300 gold_error(_("%s: not configured to support "
2301 "64-bit big-endian object"),
2302 filename.c_str());
2303 #endif
2304 else
2305 #ifdef HAVE_TARGET_64_LITTLE
2306 obj = new Sized_pluginobj<64, false>(filename, input_file,
2307 offset, filesize);
2308 #else
2309 gold_error(_("%s: not configured to support "
2310 "64-bit little-endian object"),
2311 filename.c_str());
2312 #endif
2315 gold_assert(obj != NULL);
2316 return obj;
2319 } // End namespace gold.