2009-05-25 Tristan Gingold <gingold@adacore.com>
[binutils.git] / gold / plugin.cc
blobb1007a7e63fe74174533a1d002e588010ef7927c
1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008, 2009 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 <cstdio>
24 #include <cstdarg>
25 #include <cstring>
26 #include <string>
27 #include <vector>
28 #include <dlfcn.h>
30 #include "gold.h"
31 #include "parameters.h"
32 #include "errors.h"
33 #include "fileread.h"
34 #include "layout.h"
35 #include "options.h"
36 #include "plugin.h"
37 #include "target.h"
38 #include "readsyms.h"
39 #include "symtab.h"
40 #include "elfcpp.h"
42 namespace gold
45 #ifdef ENABLE_PLUGINS
47 // The linker's exported interfaces.
49 extern "C"
52 static enum ld_plugin_status
53 register_claim_file(ld_plugin_claim_file_handler handler);
55 static enum ld_plugin_status
56 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
58 static enum ld_plugin_status
59 register_cleanup(ld_plugin_cleanup_handler handler);
61 static enum ld_plugin_status
62 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
64 static enum ld_plugin_status
65 get_input_file(const void *handle, struct ld_plugin_input_file *file);
67 static enum ld_plugin_status
68 release_input_file(const void *handle);
70 static enum ld_plugin_status
71 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
73 static enum ld_plugin_status
74 add_input_file(char *pathname);
76 static enum ld_plugin_status
77 message(int level, const char *format, ...);
81 #endif // ENABLE_PLUGINS
83 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
84 off_t offset, off_t filesize);
86 // Plugin methods.
88 // Load one plugin library.
90 void
91 Plugin::load()
93 #ifdef ENABLE_PLUGINS
94 // Load the plugin library.
95 // FIXME: Look for the library in standard locations.
96 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
97 if (this->handle_ == NULL)
99 gold_error(_("%s: could not load plugin library"),
100 this->filename_.c_str());
101 return;
104 // Find the plugin's onload entry point.
105 ld_plugin_onload onload = reinterpret_cast<ld_plugin_onload>
106 (dlsym(this->handle_, "onload"));
107 if (onload == NULL)
109 gold_error(_("%s: could not find onload entry point"),
110 this->filename_.c_str());
111 return;
114 // Get the linker's version number.
115 const char* ver = get_version_string();
116 int major = 0;
117 int minor = 0;
118 sscanf(ver, "%d.%d", &major, &minor);
120 // Allocate and populate a transfer vector.
121 const int tv_fixed_size = 13;
122 int tv_size = this->args_.size() + tv_fixed_size;
123 ld_plugin_tv *tv = new ld_plugin_tv[tv_size];
125 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
126 // while processing subsequent entries.
127 int i = 0;
128 tv[i].tv_tag = LDPT_MESSAGE;
129 tv[i].tv_u.tv_message = message;
131 ++i;
132 tv[i].tv_tag = LDPT_API_VERSION;
133 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
135 ++i;
136 tv[i].tv_tag = LDPT_GOLD_VERSION;
137 tv[i].tv_u.tv_val = major * 100 + minor;
139 ++i;
140 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
141 if (parameters->options().relocatable())
142 tv[i].tv_u.tv_val = LDPO_REL;
143 else if (parameters->options().shared())
144 tv[i].tv_u.tv_val = LDPO_DYN;
145 else
146 tv[i].tv_u.tv_val = LDPO_EXEC;
148 for (unsigned int j = 0; j < this->args_.size(); ++j)
150 ++i;
151 tv[i].tv_tag = LDPT_OPTION;
152 tv[i].tv_u.tv_string = this->args_[j].c_str();
155 ++i;
156 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
157 tv[i].tv_u.tv_register_claim_file = register_claim_file;
159 ++i;
160 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
161 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
163 ++i;
164 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
165 tv[i].tv_u.tv_register_cleanup = register_cleanup;
167 ++i;
168 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
169 tv[i].tv_u.tv_add_symbols = add_symbols;
171 ++i;
172 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
173 tv[i].tv_u.tv_get_input_file = get_input_file;
175 ++i;
176 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
177 tv[i].tv_u.tv_release_input_file = release_input_file;
179 ++i;
180 tv[i].tv_tag = LDPT_GET_SYMBOLS;
181 tv[i].tv_u.tv_get_symbols = get_symbols;
183 ++i;
184 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
185 tv[i].tv_u.tv_add_input_file = add_input_file;
187 ++i;
188 tv[i].tv_tag = LDPT_NULL;
189 tv[i].tv_u.tv_val = 0;
191 gold_assert(i == tv_size - 1);
193 // Call the onload entry point.
194 (*onload)(tv);
196 delete[] tv;
197 #endif // ENABLE_PLUGINS
200 // Call the plugin claim-file handler.
202 inline bool
203 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
205 int claimed = 0;
207 if (this->claim_file_handler_ != NULL)
209 (*this->claim_file_handler_)(plugin_input_file, &claimed);
210 if (claimed)
211 return true;
213 return false;
216 // Call the all-symbols-read handler.
218 inline void
219 Plugin::all_symbols_read()
221 if (this->all_symbols_read_handler_ != NULL)
222 (*this->all_symbols_read_handler_)();
225 // Call the cleanup handler.
227 inline void
228 Plugin::cleanup()
230 if (this->cleanup_handler_ != NULL)
231 (*this->cleanup_handler_)();
234 // Plugin_manager methods.
236 Plugin_manager::~Plugin_manager()
238 for (Plugin_list::iterator p = this->plugins_.begin();
239 p != this->plugins_.end();
240 ++p)
241 delete *p;
242 this->plugins_.clear();
243 for (Object_list::iterator obj = this->objects_.begin();
244 obj != this->objects_.end();
245 ++obj)
246 delete *obj;
247 this->objects_.clear();
250 // Load all plugin libraries.
252 void
253 Plugin_manager::load_plugins()
255 for (this->current_ = this->plugins_.begin();
256 this->current_ != this->plugins_.end();
257 ++this->current_)
258 (*this->current_)->load();
261 // Call the plugin claim-file handlers in turn to see if any claim the file.
263 Pluginobj*
264 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
265 off_t filesize)
267 if (this->in_replacement_phase_)
268 return NULL;
270 unsigned int handle = this->objects_.size();
271 this->input_file_ = input_file;
272 this->plugin_input_file_.name = input_file->filename().c_str();
273 this->plugin_input_file_.fd = input_file->file().descriptor();
274 this->plugin_input_file_.offset = offset;
275 this->plugin_input_file_.filesize = filesize;
276 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
278 for (this->current_ = this->plugins_.begin();
279 this->current_ != this->plugins_.end();
280 ++this->current_)
282 if ((*this->current_)->claim_file(&this->plugin_input_file_))
284 if (this->objects_.size() > handle)
285 return this->objects_[handle];
287 // If the plugin claimed the file but did not call the
288 // add_symbols callback, we need to create the Pluginobj now.
289 Pluginobj* obj = this->make_plugin_object(handle);
290 return obj;
294 return NULL;
297 // Call the all-symbols-read handlers.
299 void
300 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
301 Input_objects* input_objects,
302 Symbol_table* symtab, Layout* layout,
303 Dirsearch* dirpath, Mapfile* mapfile,
304 Task_token** last_blocker)
306 this->in_replacement_phase_ = true;
307 this->workqueue_ = workqueue;
308 this->task_ = task;
309 this->input_objects_ = input_objects;
310 this->symtab_ = symtab;
311 this->layout_ = layout;
312 this->dirpath_ = dirpath;
313 this->mapfile_ = mapfile;
314 this->this_blocker_ = NULL;
316 for (this->current_ = this->plugins_.begin();
317 this->current_ != this->plugins_.end();
318 ++this->current_)
319 (*this->current_)->all_symbols_read();
321 *last_blocker = this->this_blocker_;
324 // Layout deferred objects.
326 void
327 Plugin_manager::layout_deferred_objects()
329 Deferred_layout_list::iterator obj;
331 for (obj = this->deferred_layout_objects_.begin();
332 obj != this->deferred_layout_objects_.end();
333 ++obj)
334 (*obj)->layout_deferred_sections(this->layout_);
337 // Call the cleanup handlers.
339 void
340 Plugin_manager::cleanup()
342 if (this->cleanup_done_)
343 return;
344 for (this->current_ = this->plugins_.begin();
345 this->current_ != this->plugins_.end();
346 ++this->current_)
347 (*this->current_)->cleanup();
348 this->cleanup_done_ = true;
351 // Make a new Pluginobj object. This is called when the plugin calls
352 // the add_symbols API.
354 Pluginobj*
355 Plugin_manager::make_plugin_object(unsigned int handle)
357 // Make sure we aren't asked to make an object for the same handle twice.
358 if (this->objects_.size() != handle)
359 return NULL;
361 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
362 this->plugin_input_file_.offset,
363 this->plugin_input_file_.filesize);
364 this->objects_.push_back(obj);
365 return obj;
368 // Get the input file information with an open (possibly re-opened)
369 // file descriptor.
371 ld_plugin_status
372 Plugin_manager::get_input_file(unsigned int handle,
373 struct ld_plugin_input_file *file)
375 Pluginobj* obj = this->object(handle);
376 if (obj == NULL)
377 return LDPS_BAD_HANDLE;
379 obj->lock(this->task_);
380 file->name = obj->filename().c_str();
381 file->fd = obj->descriptor();
382 file->offset = obj->offset();
383 file->filesize = obj->filesize();
384 file->handle = reinterpret_cast<void*>(handle);
385 return LDPS_OK;
388 // Release the input file.
390 ld_plugin_status
391 Plugin_manager::release_input_file(unsigned int handle)
393 Pluginobj* obj = this->object(handle);
394 if (obj == NULL)
395 return LDPS_BAD_HANDLE;
397 obj->unlock(this->task_);
398 return LDPS_OK;
401 // Add a new input file.
403 ld_plugin_status
404 Plugin_manager::add_input_file(char *pathname)
406 Input_file_argument file(pathname, false, "", false, this->options_);
407 Input_argument* input_argument = new Input_argument(file);
408 Task_token* next_blocker = new Task_token(true);
409 next_blocker->add_blocker();
410 if (this->layout_->incremental_inputs())
411 gold_error(_("Input files added by plug-ins in --incremental mode not "
412 "supported yet.\n"));
413 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
414 this->symtab_,
415 this->layout_,
416 this->dirpath_,
418 this->mapfile_,
419 input_argument,
420 NULL,
421 this->this_blocker_,
422 next_blocker));
423 this->this_blocker_ = next_blocker;
424 return LDPS_OK;
427 // Class Pluginobj.
429 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
430 off_t offset, off_t filesize)
431 : Object(name, input_file, false, offset),
432 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
436 // Return TRUE if a defined symbol might be reachable from outside the
437 // universe of claimed objects.
439 static inline bool
440 is_visible_from_outside(Symbol* lsym)
442 if (lsym->in_real_elf())
443 return true;
444 if (parameters->options().relocatable())
445 return true;
446 if (parameters->options().export_dynamic() || parameters->options().shared())
447 return lsym->is_externally_visible();
448 return false;
451 // Get symbol resolution info.
453 ld_plugin_status
454 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
456 if (nsyms > this->nsyms_)
457 return LDPS_NO_SYMS;
458 for (int i = 0; i < nsyms; i++)
460 ld_plugin_symbol* isym = &syms[i];
461 Symbol* lsym = this->symbols_[i];
462 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
464 if (lsym->is_undefined())
465 // The symbol remains undefined.
466 res = LDPR_UNDEF;
467 else if (isym->def == LDPK_UNDEF
468 || isym->def == LDPK_WEAKUNDEF
469 || isym->def == LDPK_COMMON)
471 // The original symbol was undefined or common.
472 if (lsym->source() != Symbol::FROM_OBJECT)
473 res = LDPR_RESOLVED_EXEC;
474 else if (lsym->object()->pluginobj() != NULL)
475 res = LDPR_RESOLVED_IR;
476 else if (lsym->object()->is_dynamic())
477 res = LDPR_RESOLVED_DYN;
478 else
479 res = LDPR_RESOLVED_EXEC;
481 else
483 // The original symbol was a definition.
484 if (lsym->source() != Symbol::FROM_OBJECT)
485 res = LDPR_PREEMPTED_REG;
486 else if (lsym->object() == static_cast<const Object*>(this))
487 res = (is_visible_from_outside(lsym)
488 ? LDPR_PREVAILING_DEF
489 : LDPR_PREVAILING_DEF_IRONLY);
490 else
491 res = (lsym->object()->pluginobj() != NULL
492 ? LDPR_PREEMPTED_IR
493 : LDPR_PREEMPTED_REG);
495 isym->resolution = res;
497 return LDPS_OK;
500 // Return TRUE if the comdat group with key COMDAT_KEY from this object
501 // should be kept.
503 bool
504 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
506 std::pair<Comdat_map::iterator, bool> ins =
507 this->comdat_map_.insert(std::make_pair(comdat_key, false));
509 // If this is the first time we've seen this comdat key, ask the
510 // layout object whether it should be included.
511 if (ins.second)
513 Kept_section to_add(NULL, 1, true);
514 ins.first->second = layout->find_or_add_kept_section(comdat_key,
515 &to_add,
516 NULL);
519 return ins.first->second;
522 // Class Sized_pluginobj.
524 template<int size, bool big_endian>
525 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
526 const std::string& name,
527 Input_file* input_file,
528 off_t offset,
529 off_t filesize)
530 : Pluginobj(name, input_file, offset, filesize)
534 // Read the symbols. Not used for plugin objects.
536 template<int size, bool big_endian>
537 void
538 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
540 gold_unreachable();
543 // Lay out the input sections. Not used for plugin objects.
545 template<int size, bool big_endian>
546 void
547 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
548 Read_symbols_data*)
550 gold_unreachable();
553 // Add the symbols to the symbol table.
555 template<int size, bool big_endian>
556 void
557 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
558 Read_symbols_data*,
559 Layout* layout)
561 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
562 unsigned char symbuf[sym_size];
563 elfcpp::Sym<size, big_endian> sym(symbuf);
564 elfcpp::Sym_write<size, big_endian> osym(symbuf);
566 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
568 this->symbols_.resize(this->nsyms_);
570 for (int i = 0; i < this->nsyms_; ++i)
572 const struct ld_plugin_symbol *isym = &this->syms_[i];
573 const char* name = isym->name;
574 const char* ver = isym->version;
575 elfcpp::Elf_Half shndx;
576 elfcpp::STB bind;
577 elfcpp::STV vis;
579 if (name != NULL && name[0] == '\0')
580 name = NULL;
581 if (ver != NULL && ver[0] == '\0')
582 ver = NULL;
584 switch (isym->def)
586 case LDPK_WEAKDEF:
587 case LDPK_WEAKUNDEF:
588 bind = elfcpp::STB_WEAK;
589 break;
590 case LDPK_DEF:
591 case LDPK_UNDEF:
592 case LDPK_COMMON:
593 default:
594 bind = elfcpp::STB_GLOBAL;
595 break;
598 switch (isym->def)
600 case LDPK_DEF:
601 case LDPK_WEAKDEF:
602 shndx = elfcpp::SHN_ABS;
603 break;
604 case LDPK_COMMON:
605 shndx = elfcpp::SHN_COMMON;
606 break;
607 case LDPK_UNDEF:
608 case LDPK_WEAKUNDEF:
609 default:
610 shndx = elfcpp::SHN_UNDEF;
611 break;
614 switch (isym->visibility)
616 case LDPV_PROTECTED:
617 vis = elfcpp::STV_DEFAULT;
618 break;
619 case LDPV_INTERNAL:
620 vis = elfcpp::STV_DEFAULT;
621 break;
622 case LDPV_HIDDEN:
623 vis = elfcpp::STV_DEFAULT;
624 break;
625 case LDPV_DEFAULT:
626 default:
627 vis = elfcpp::STV_DEFAULT;
628 break;
631 if (isym->comdat_key != NULL
632 && isym->comdat_key[0] != '\0'
633 && !this->include_comdat_group(isym->comdat_key, layout))
634 shndx = elfcpp::SHN_UNDEF;
636 osym.put_st_name(0);
637 osym.put_st_value(0);
638 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
639 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
640 osym.put_st_other(vis, 0);
641 osym.put_st_shndx(shndx);
643 this->symbols_[i] =
644 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
648 // Get the size of a section. Not used for plugin objects.
650 template<int size, bool big_endian>
651 uint64_t
652 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
654 gold_unreachable();
655 return 0;
658 // Get the name of a section. Not used for plugin objects.
660 template<int size, bool big_endian>
661 std::string
662 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
664 gold_unreachable();
665 return std::string();
668 // Return a view of the contents of a section. Not used for plugin objects.
670 template<int size, bool big_endian>
671 Object::Location
672 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
674 Location loc(0, 0);
676 gold_unreachable();
677 return loc;
680 // Return section flags. Not used for plugin objects.
682 template<int size, bool big_endian>
683 uint64_t
684 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
686 gold_unreachable();
687 return 0;
690 // Return section address. Not used for plugin objects.
692 template<int size, bool big_endian>
693 uint64_t
694 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
696 gold_unreachable();
697 return 0;
700 // Return section type. Not used for plugin objects.
702 template<int size, bool big_endian>
703 unsigned int
704 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
706 gold_unreachable();
707 return 0;
710 // Return the section link field. Not used for plugin objects.
712 template<int size, bool big_endian>
713 unsigned int
714 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
716 gold_unreachable();
717 return 0;
720 // Return the section link field. Not used for plugin objects.
722 template<int size, bool big_endian>
723 unsigned int
724 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
726 gold_unreachable();
727 return 0;
730 // Return the section alignment. Not used for plugin objects.
732 template<int size, bool big_endian>
733 uint64_t
734 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
736 gold_unreachable();
737 return 0;
740 // Return the Xindex structure to use. Not used for plugin objects.
742 template<int size, bool big_endian>
743 Xindex*
744 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
746 gold_unreachable();
747 return NULL;
750 // Get symbol counts. Not used for plugin objects.
752 template<int size, bool big_endian>
753 void
754 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
755 size_t*, size_t*) const
757 gold_unreachable();
760 // Class Plugin_finish. This task runs after all replacement files have
761 // been added. It calls each plugin's cleanup handler.
763 class Plugin_finish : public Task
765 public:
766 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
767 : this_blocker_(this_blocker), next_blocker_(next_blocker)
770 ~Plugin_finish()
772 if (this->this_blocker_ != NULL)
773 delete this->this_blocker_;
776 Task_token*
777 is_runnable()
779 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
780 return this->this_blocker_;
781 return NULL;
784 void
785 locks(Task_locker* tl)
786 { tl->add(this, this->next_blocker_); }
788 void
789 run(Workqueue*)
791 Plugin_manager* plugins = parameters->options().plugins();
792 gold_assert(plugins != NULL);
793 plugins->cleanup();
796 std::string
797 get_name() const
798 { return "Plugin_finish"; }
800 private:
801 Task_token* this_blocker_;
802 Task_token* next_blocker_;
805 // Class Plugin_hook.
807 Plugin_hook::~Plugin_hook()
811 // Return whether a Plugin_hook task is runnable.
813 Task_token*
814 Plugin_hook::is_runnable()
816 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
817 return this->this_blocker_;
818 return NULL;
821 // Return a Task_locker for a Plugin_hook task. We don't need any
822 // locks here.
824 void
825 Plugin_hook::locks(Task_locker*)
829 // Run the "all symbols read" plugin hook.
831 void
832 Plugin_hook::run(Workqueue* workqueue)
834 gold_assert(this->options_.has_plugins());
835 this->options_.plugins()->all_symbols_read(workqueue,
836 this,
837 this->input_objects_,
838 this->symtab_,
839 this->layout_,
840 this->dirpath_,
841 this->mapfile_,
842 &this->this_blocker_);
843 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
844 this->next_blocker_));
847 // The C interface routines called by the plugins.
849 #ifdef ENABLE_PLUGINS
851 // Register a claim-file handler.
853 static enum ld_plugin_status
854 register_claim_file(ld_plugin_claim_file_handler handler)
856 gold_assert(parameters->options().has_plugins());
857 parameters->options().plugins()->set_claim_file_handler(handler);
858 return LDPS_OK;
861 // Register an all-symbols-read handler.
863 static enum ld_plugin_status
864 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
866 gold_assert(parameters->options().has_plugins());
867 parameters->options().plugins()->set_all_symbols_read_handler(handler);
868 return LDPS_OK;
871 // Register a cleanup handler.
873 static enum ld_plugin_status
874 register_cleanup(ld_plugin_cleanup_handler handler)
876 gold_assert(parameters->options().has_plugins());
877 parameters->options().plugins()->set_cleanup_handler(handler);
878 return LDPS_OK;
881 // Add symbols from a plugin-claimed input file.
883 static enum ld_plugin_status
884 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
886 gold_assert(parameters->options().has_plugins());
887 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
888 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
889 if (obj == NULL)
890 return LDPS_ERR;
891 obj->store_incoming_symbols(nsyms, syms);
892 return LDPS_OK;
895 // Get the input file information with an open (possibly re-opened)
896 // file descriptor.
898 static enum ld_plugin_status
899 get_input_file(const void *handle, struct ld_plugin_input_file *file)
901 gold_assert(parameters->options().has_plugins());
902 unsigned int obj_index =
903 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
904 return parameters->options().plugins()->get_input_file(obj_index, file);
907 // Release the input file.
909 static enum ld_plugin_status
910 release_input_file(const void *handle)
912 gold_assert(parameters->options().has_plugins());
913 unsigned int obj_index =
914 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
915 return parameters->options().plugins()->release_input_file(obj_index);
918 // Get the symbol resolution info for a plugin-claimed input file.
920 static enum ld_plugin_status
921 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
923 gold_assert(parameters->options().has_plugins());
924 Pluginobj* obj = parameters->options().plugins()->object(
925 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
926 if (obj == NULL)
927 return LDPS_ERR;
928 return obj->get_symbol_resolution_info(nsyms, syms);
931 // Add a new (real) input file generated by a plugin.
933 static enum ld_plugin_status
934 add_input_file(char *pathname)
936 gold_assert(parameters->options().has_plugins());
937 return parameters->options().plugins()->add_input_file(pathname);
940 // Issue a diagnostic message from a plugin.
942 static enum ld_plugin_status
943 message(int level, const char * format, ...)
945 va_list args;
946 va_start(args, format);
948 switch (level)
950 case LDPL_INFO:
951 parameters->errors()->info(format, args);
952 break;
953 case LDPL_WARNING:
954 parameters->errors()->warning(format, args);
955 break;
956 case LDPL_ERROR:
957 default:
958 parameters->errors()->error(format, args);
959 break;
960 case LDPL_FATAL:
961 parameters->errors()->fatal(format, args);
962 break;
965 va_end(args);
966 return LDPS_OK;
969 #endif // ENABLE_PLUGINS
971 // Allocate a Pluginobj object of the appropriate size and endianness.
973 static Pluginobj*
974 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
976 Target* target;
977 Pluginobj* obj = NULL;
979 if (parameters->target_valid())
980 target = const_cast<Target*>(&parameters->target());
981 else
982 target = const_cast<Target*>(&parameters->default_target());
984 if (target->get_size() == 32)
986 if (target->is_big_endian())
987 #ifdef HAVE_TARGET_32_BIG
988 obj = new Sized_pluginobj<32, true>(input_file->filename(),
989 input_file, offset, filesize);
990 #else
991 gold_error(_("%s: not configured to support "
992 "32-bit big-endian object"),
993 input_file->filename().c_str());
994 #endif
995 else
996 #ifdef HAVE_TARGET_32_LITTLE
997 obj = new Sized_pluginobj<32, false>(input_file->filename(),
998 input_file, offset, filesize);
999 #else
1000 gold_error(_("%s: not configured to support "
1001 "32-bit little-endian object"),
1002 input_file->filename().c_str());
1003 #endif
1005 else if (target->get_size() == 64)
1007 if (target->is_big_endian())
1008 #ifdef HAVE_TARGET_64_BIG
1009 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1010 input_file, offset, filesize);
1011 #else
1012 gold_error(_("%s: not configured to support "
1013 "64-bit big-endian object"),
1014 input_file->filename().c_str());
1015 #endif
1016 else
1017 #ifdef HAVE_TARGET_64_LITTLE
1018 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1019 input_file, offset, filesize);
1020 #else
1021 gold_error(_("%s: not configured to support "
1022 "64-bit little-endian object"),
1023 input_file->filename().c_str());
1024 #endif
1027 gold_assert(obj != NULL);
1028 obj->set_target(target);
1029 return obj;
1032 } // End namespace gold.