1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008, 2009, 2010, 2011 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.
35 #include "parameters.h"
51 // The linker's exported interfaces.
56 static enum ld_plugin_status
57 register_claim_file(ld_plugin_claim_file_handler handler
);
59 static enum ld_plugin_status
60 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
);
62 static enum ld_plugin_status
63 register_cleanup(ld_plugin_cleanup_handler handler
);
65 static enum ld_plugin_status
66 add_symbols(void *handle
, int nsyms
, const struct ld_plugin_symbol
*syms
);
68 static enum ld_plugin_status
69 get_input_file(const void *handle
, struct ld_plugin_input_file
*file
);
71 static enum ld_plugin_status
72 release_input_file(const void *handle
);
74 static enum ld_plugin_status
75 get_symbols(const void *handle
, int nsyms
, struct ld_plugin_symbol
*syms
);
77 static enum ld_plugin_status
78 add_input_file(const char *pathname
);
80 static enum ld_plugin_status
81 add_input_library(const char *pathname
);
83 static enum ld_plugin_status
84 set_extra_library_path(const char *path
);
86 static enum ld_plugin_status
87 message(int level
, const char *format
, ...);
91 #endif // ENABLE_PLUGINS
93 static Pluginobj
* make_sized_plugin_object(Input_file
* input_file
,
94 off_t offset
, off_t filesize
);
98 // Load one plugin library.
103 #ifdef ENABLE_PLUGINS
104 // Load the plugin library.
105 // FIXME: Look for the library in standard locations.
106 this->handle_
= dlopen(this->filename_
.c_str(), RTLD_NOW
);
107 if (this->handle_
== NULL
)
109 gold_error(_("%s: could not load plugin library: %s"),
110 this->filename_
.c_str(), dlerror());
114 // Find the plugin's onload entry point.
115 void* ptr
= dlsym(this->handle_
, "onload");
118 gold_error(_("%s: could not find onload entry point"),
119 this->filename_
.c_str());
122 ld_plugin_onload onload
;
123 gold_assert(sizeof(onload
) == sizeof(ptr
));
124 memcpy(&onload
, &ptr
, sizeof(ptr
));
126 // Get the linker's version number.
127 const char* ver
= get_version_string();
130 sscanf(ver
, "%d.%d", &major
, &minor
);
132 // Allocate and populate a transfer vector.
133 const int tv_fixed_size
= 16;
134 int tv_size
= this->args_
.size() + tv_fixed_size
;
135 ld_plugin_tv
* tv
= new ld_plugin_tv
[tv_size
];
137 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
138 // while processing subsequent entries.
140 tv
[i
].tv_tag
= LDPT_MESSAGE
;
141 tv
[i
].tv_u
.tv_message
= message
;
144 tv
[i
].tv_tag
= LDPT_API_VERSION
;
145 tv
[i
].tv_u
.tv_val
= LD_PLUGIN_API_VERSION
;
148 tv
[i
].tv_tag
= LDPT_GOLD_VERSION
;
149 tv
[i
].tv_u
.tv_val
= major
* 100 + minor
;
152 tv
[i
].tv_tag
= LDPT_LINKER_OUTPUT
;
153 if (parameters
->options().relocatable())
154 tv
[i
].tv_u
.tv_val
= LDPO_REL
;
155 else if (parameters
->options().shared())
156 tv
[i
].tv_u
.tv_val
= LDPO_DYN
;
158 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
161 tv
[i
].tv_tag
= LDPT_OUTPUT_NAME
;
162 tv
[i
].tv_u
.tv_string
= parameters
->options().output();
164 for (unsigned int j
= 0; j
< this->args_
.size(); ++j
)
167 tv
[i
].tv_tag
= LDPT_OPTION
;
168 tv
[i
].tv_u
.tv_string
= this->args_
[j
].c_str();
172 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
173 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
176 tv
[i
].tv_tag
= LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
;
177 tv
[i
].tv_u
.tv_register_all_symbols_read
= register_all_symbols_read
;
180 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
181 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
184 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
185 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
188 tv
[i
].tv_tag
= LDPT_GET_INPUT_FILE
;
189 tv
[i
].tv_u
.tv_get_input_file
= get_input_file
;
192 tv
[i
].tv_tag
= LDPT_RELEASE_INPUT_FILE
;
193 tv
[i
].tv_u
.tv_release_input_file
= release_input_file
;
196 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
197 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
200 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
201 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
204 tv
[i
].tv_tag
= LDPT_ADD_INPUT_LIBRARY
;
205 tv
[i
].tv_u
.tv_add_input_library
= add_input_library
;
208 tv
[i
].tv_tag
= LDPT_SET_EXTRA_LIBRARY_PATH
;
209 tv
[i
].tv_u
.tv_set_extra_library_path
= set_extra_library_path
;
212 tv
[i
].tv_tag
= LDPT_NULL
;
213 tv
[i
].tv_u
.tv_val
= 0;
215 gold_assert(i
== tv_size
- 1);
217 // Call the onload entry point.
221 #endif // ENABLE_PLUGINS
224 // Call the plugin claim-file handler.
227 Plugin::claim_file(struct ld_plugin_input_file
* plugin_input_file
)
231 if (this->claim_file_handler_
!= NULL
)
233 (*this->claim_file_handler_
)(plugin_input_file
, &claimed
);
240 // Call the all-symbols-read handler.
243 Plugin::all_symbols_read()
245 if (this->all_symbols_read_handler_
!= NULL
)
246 (*this->all_symbols_read_handler_
)();
249 // Call the cleanup handler.
254 if (this->cleanup_handler_
!= NULL
&& !this->cleanup_done_
)
256 // Set this flag before calling to prevent a recursive plunge
257 // in the event that a plugin's cleanup handler issues a
259 this->cleanup_done_
= true;
260 (*this->cleanup_handler_
)();
264 // This task is used to rescan archives as needed.
266 class Plugin_rescan
: public Task
269 Plugin_rescan(Task_token
* this_blocker
, Task_token
* next_blocker
)
270 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
275 delete this->this_blocker_
;
281 if (this->this_blocker_
->is_blocked())
282 return this->this_blocker_
;
287 locks(Task_locker
* tl
)
288 { tl
->add(this, this->next_blocker_
); }
292 { parameters
->options().plugins()->rescan(this); }
296 { return "Plugin_rescan"; }
299 Task_token
* this_blocker_
;
300 Task_token
* next_blocker_
;
303 // Plugin_manager methods.
305 Plugin_manager::~Plugin_manager()
307 for (Plugin_list::iterator p
= this->plugins_
.begin();
308 p
!= this->plugins_
.end();
311 this->plugins_
.clear();
312 for (Object_list::iterator obj
= this->objects_
.begin();
313 obj
!= this->objects_
.end();
316 this->objects_
.clear();
319 // Load all plugin libraries.
322 Plugin_manager::load_plugins()
324 for (this->current_
= this->plugins_
.begin();
325 this->current_
!= this->plugins_
.end();
327 (*this->current_
)->load();
330 // Call the plugin claim-file handlers in turn to see if any claim the file.
333 Plugin_manager::claim_file(Input_file
* input_file
, off_t offset
,
336 if (this->in_replacement_phase_
)
339 unsigned int handle
= this->objects_
.size();
340 this->input_file_
= input_file
;
341 this->plugin_input_file_
.name
= input_file
->filename().c_str();
342 this->plugin_input_file_
.fd
= input_file
->file().descriptor();
343 this->plugin_input_file_
.offset
= offset
;
344 this->plugin_input_file_
.filesize
= filesize
;
345 this->plugin_input_file_
.handle
= reinterpret_cast<void*>(handle
);
347 for (this->current_
= this->plugins_
.begin();
348 this->current_
!= this->plugins_
.end();
351 if ((*this->current_
)->claim_file(&this->plugin_input_file_
))
353 this->any_claimed_
= true;
355 if (this->objects_
.size() > handle
)
356 return this->objects_
[handle
];
358 // If the plugin claimed the file but did not call the
359 // add_symbols callback, we need to create the Pluginobj now.
360 Pluginobj
* obj
= this->make_plugin_object(handle
);
368 // Save an archive. This is used so that a plugin can add a file
369 // which refers to a symbol which was not previously referenced. In
370 // that case we want to pretend that the symbol was referenced before,
371 // and pull in the archive object.
374 Plugin_manager::save_archive(Archive
* archive
)
376 if (this->in_replacement_phase_
|| !this->any_claimed_
)
379 this->rescannable_
.push_back(Rescannable(archive
));
382 // Save an Input_group. This is like save_archive.
385 Plugin_manager::save_input_group(Input_group
* input_group
)
387 if (this->in_replacement_phase_
|| !this->any_claimed_
)
390 this->rescannable_
.push_back(Rescannable(input_group
));
393 // Call the all-symbols-read handlers.
396 Plugin_manager::all_symbols_read(Workqueue
* workqueue
, Task
* task
,
397 Input_objects
* input_objects
,
398 Symbol_table
* symtab
, Layout
* layout
,
399 Dirsearch
* dirpath
, Mapfile
* mapfile
,
400 Task_token
** last_blocker
)
402 this->in_replacement_phase_
= true;
403 this->workqueue_
= workqueue
;
405 this->input_objects_
= input_objects
;
406 this->symtab_
= symtab
;
407 this->layout_
= layout
;
408 this->dirpath_
= dirpath
;
409 this->mapfile_
= mapfile
;
410 this->this_blocker_
= NULL
;
412 for (this->current_
= this->plugins_
.begin();
413 this->current_
!= this->plugins_
.end();
415 (*this->current_
)->all_symbols_read();
417 if (this->any_added_
)
419 Task_token
* next_blocker
= new Task_token(true);
420 next_blocker
->add_blocker();
421 workqueue
->queue(new Plugin_rescan(this->this_blocker_
, next_blocker
));
422 this->this_blocker_
= next_blocker
;
425 *last_blocker
= this->this_blocker_
;
428 // This is called when we see a new undefined symbol. If we are in
429 // the replacement phase, this means that we may need to rescan some
430 // archives we have previously seen.
433 Plugin_manager::new_undefined_symbol(Symbol
* sym
)
435 if (this->in_replacement_phase_
)
436 this->undefined_symbols_
.push_back(sym
);
439 // Rescan archives as needed. This handles the case where a new
440 // object file added by a plugin has an undefined reference to some
441 // symbol defined in an archive.
444 Plugin_manager::rescan(Task
* task
)
446 size_t rescan_pos
= 0;
447 size_t rescan_size
= this->rescannable_
.size();
448 while (!this->undefined_symbols_
.empty())
450 if (rescan_pos
>= rescan_size
)
452 this->undefined_symbols_
.clear();
456 Undefined_symbol_list undefs
;
457 undefs
.reserve(this->undefined_symbols_
.size());
458 this->undefined_symbols_
.swap(undefs
);
460 size_t min_rescan_pos
= rescan_size
;
462 for (Undefined_symbol_list::const_iterator p
= undefs
.begin();
466 if (!(*p
)->is_undefined())
469 this->undefined_symbols_
.push_back(*p
);
471 // Find the first rescan archive which defines this symbol,
472 // starting at the current rescan position. The rescan position
473 // exists so that given -la -lb -lc we don't look for undefined
474 // symbols in -lb back in -la, but instead get the definition
475 // from -lc. Don't bother to look past the current minimum
477 for (size_t i
= rescan_pos
; i
< min_rescan_pos
; ++i
)
479 if (this->rescannable_defines(i
, *p
))
487 if (min_rescan_pos
>= rescan_size
)
489 // We didn't find any rescannable archives which define any
490 // undefined symbols.
494 const Rescannable
& r(this->rescannable_
[min_rescan_pos
]);
497 Task_lock_obj
<Archive
> tl(task
, r
.u
.archive
);
498 r
.u
.archive
->add_symbols(this->symtab_
, this->layout_
,
499 this->input_objects_
, this->mapfile_
);
503 size_t next_saw_undefined
= this->symtab_
->saw_undefined();
504 size_t saw_undefined
;
507 saw_undefined
= next_saw_undefined
;
509 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
510 p
!= r
.u
.input_group
->end();
513 Task_lock_obj
<Archive
> tl(task
, *p
);
515 (*p
)->add_symbols(this->symtab_
, this->layout_
,
516 this->input_objects_
, this->mapfile_
);
519 next_saw_undefined
= this->symtab_
->saw_undefined();
521 while (saw_undefined
!= next_saw_undefined
);
524 for (size_t i
= rescan_pos
; i
< min_rescan_pos
+ 1; ++i
)
526 if (this->rescannable_
[i
].is_archive
)
527 delete this->rescannable_
[i
].u
.archive
;
529 delete this->rescannable_
[i
].u
.input_group
;
532 rescan_pos
= min_rescan_pos
+ 1;
536 // Return whether the rescannable at index I defines SYM.
539 Plugin_manager::rescannable_defines(size_t i
, Symbol
* sym
)
541 const Rescannable
& r(this->rescannable_
[i
]);
543 return r
.u
.archive
->defines_symbol(sym
);
546 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
547 p
!= r
.u
.input_group
->end();
550 if ((*p
)->defines_symbol(sym
))
557 // Layout deferred objects.
560 Plugin_manager::layout_deferred_objects()
562 Deferred_layout_list::iterator obj
;
564 for (obj
= this->deferred_layout_objects_
.begin();
565 obj
!= this->deferred_layout_objects_
.end();
568 // Lock the object so we can read from it. This is only called
569 // single-threaded from queue_middle_tasks, so it is OK to lock.
570 // Unfortunately we have no way to pass in a Task token.
571 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
572 Task_lock_obj
<Object
> tl(dummy_task
, *obj
);
573 (*obj
)->layout_deferred_sections(this->layout_
);
577 // Call the cleanup handlers.
580 Plugin_manager::cleanup()
582 for (this->current_
= this->plugins_
.begin();
583 this->current_
!= this->plugins_
.end();
585 (*this->current_
)->cleanup();
588 // Make a new Pluginobj object. This is called when the plugin calls
589 // the add_symbols API.
592 Plugin_manager::make_plugin_object(unsigned int handle
)
594 // Make sure we aren't asked to make an object for the same handle twice.
595 if (this->objects_
.size() != handle
)
598 Pluginobj
* obj
= make_sized_plugin_object(this->input_file_
,
599 this->plugin_input_file_
.offset
,
600 this->plugin_input_file_
.filesize
);
601 this->objects_
.push_back(obj
);
605 // Get the input file information with an open (possibly re-opened)
609 Plugin_manager::get_input_file(unsigned int handle
,
610 struct ld_plugin_input_file
* file
)
612 Pluginobj
* obj
= this->object(handle
);
614 return LDPS_BAD_HANDLE
;
616 obj
->lock(this->task_
);
617 file
->name
= obj
->filename().c_str();
618 file
->fd
= obj
->descriptor();
619 file
->offset
= obj
->offset();
620 file
->filesize
= obj
->filesize();
621 file
->handle
= reinterpret_cast<void*>(handle
);
625 // Release the input file.
628 Plugin_manager::release_input_file(unsigned int handle
)
630 Pluginobj
* obj
= this->object(handle
);
632 return LDPS_BAD_HANDLE
;
634 obj
->unlock(this->task_
);
638 // Add a new library path.
641 Plugin_manager::set_extra_library_path(const char* path
)
643 this->extra_search_path_
= std::string(path
);
647 // Add a new input file.
650 Plugin_manager::add_input_file(const char* pathname
, bool is_lib
)
652 Input_file_argument
file(pathname
,
654 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
655 : Input_file_argument::INPUT_FILE_TYPE_FILE
),
657 ? this->extra_search_path_
.c_str()
661 Input_argument
* input_argument
= new Input_argument(file
);
662 Task_token
* next_blocker
= new Task_token(true);
663 next_blocker
->add_blocker();
664 if (parameters
->incremental())
665 gold_error(_("input files added by plug-ins in --incremental mode not "
667 this->workqueue_
->queue_soon(new Read_symbols(this->input_objects_
,
678 this->this_blocker_
= next_blocker
;
679 this->any_added_
= true;
685 Pluginobj::Pluginobj(const std::string
& name
, Input_file
* input_file
,
686 off_t offset
, off_t filesize
)
687 : Object(name
, input_file
, false, offset
),
688 nsyms_(0), syms_(NULL
), symbols_(), filesize_(filesize
), comdat_map_()
692 // Return TRUE if a defined symbol might be reachable from outside the
693 // universe of claimed objects.
696 is_visible_from_outside(Symbol
* lsym
)
698 if (lsym
->in_real_elf())
700 if (parameters
->options().relocatable())
702 if (parameters
->options().export_dynamic() || parameters
->options().shared())
703 return lsym
->is_externally_visible();
707 // Get symbol resolution info.
710 Pluginobj::get_symbol_resolution_info(int nsyms
, ld_plugin_symbol
* syms
) const
712 if (nsyms
> this->nsyms_
)
715 if (static_cast<size_t>(nsyms
) > this->symbols_
.size())
717 // We never decided to include this object. We mark all symbols as
719 gold_assert(this->symbols_
.size() == 0);
720 for (int i
= 0; i
< nsyms
; i
++)
721 syms
[i
].resolution
= LDPR_PREEMPTED_REG
;
725 for (int i
= 0; i
< nsyms
; i
++)
727 ld_plugin_symbol
* isym
= &syms
[i
];
728 Symbol
* lsym
= this->symbols_
[i
];
729 ld_plugin_symbol_resolution res
= LDPR_UNKNOWN
;
731 if (lsym
->is_undefined())
732 // The symbol remains undefined.
734 else if (isym
->def
== LDPK_UNDEF
735 || isym
->def
== LDPK_WEAKUNDEF
736 || isym
->def
== LDPK_COMMON
)
738 // The original symbol was undefined or common.
739 if (lsym
->source() != Symbol::FROM_OBJECT
)
740 res
= LDPR_RESOLVED_EXEC
;
741 else if (lsym
->object()->pluginobj() == this)
742 res
= (is_visible_from_outside(lsym
)
743 ? LDPR_PREVAILING_DEF
744 : LDPR_PREVAILING_DEF_IRONLY
);
745 else if (lsym
->object()->pluginobj() != NULL
)
746 res
= LDPR_RESOLVED_IR
;
747 else if (lsym
->object()->is_dynamic())
748 res
= LDPR_RESOLVED_DYN
;
750 res
= LDPR_RESOLVED_EXEC
;
754 // The original symbol was a definition.
755 if (lsym
->source() != Symbol::FROM_OBJECT
)
756 res
= LDPR_PREEMPTED_REG
;
757 else if (lsym
->object() == static_cast<const Object
*>(this))
758 res
= (is_visible_from_outside(lsym
)
759 ? LDPR_PREVAILING_DEF
760 : LDPR_PREVAILING_DEF_IRONLY
);
762 res
= (lsym
->object()->pluginobj() != NULL
764 : LDPR_PREEMPTED_REG
);
766 isym
->resolution
= res
;
771 // Return TRUE if the comdat group with key COMDAT_KEY from this object
775 Pluginobj::include_comdat_group(std::string comdat_key
, Layout
* layout
)
777 std::pair
<Comdat_map::iterator
, bool> ins
=
778 this->comdat_map_
.insert(std::make_pair(comdat_key
, false));
780 // If this is the first time we've seen this comdat key, ask the
781 // layout object whether it should be included.
783 ins
.first
->second
= layout
->find_or_add_kept_section(comdat_key
,
787 return ins
.first
->second
;
790 // Class Sized_pluginobj.
792 template<int size
, bool big_endian
>
793 Sized_pluginobj
<size
, big_endian
>::Sized_pluginobj(
794 const std::string
& name
,
795 Input_file
* input_file
,
798 : Pluginobj(name
, input_file
, offset
, filesize
)
802 // Read the symbols. Not used for plugin objects.
804 template<int size
, bool big_endian
>
806 Sized_pluginobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
811 // Lay out the input sections. Not used for plugin objects.
813 template<int size
, bool big_endian
>
815 Sized_pluginobj
<size
, big_endian
>::do_layout(Symbol_table
*, Layout
*,
821 // Add the symbols to the symbol table.
823 template<int size
, bool big_endian
>
825 Sized_pluginobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
829 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
830 unsigned char symbuf
[sym_size
];
831 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
832 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
834 typedef typename
elfcpp::Elf_types
<size
>::Elf_WXword Elf_size_type
;
836 this->symbols_
.resize(this->nsyms_
);
838 for (int i
= 0; i
< this->nsyms_
; ++i
)
840 const struct ld_plugin_symbol
* isym
= &this->syms_
[i
];
841 const char* name
= isym
->name
;
842 const char* ver
= isym
->version
;
843 elfcpp::Elf_Half shndx
;
847 if (name
!= NULL
&& name
[0] == '\0')
849 if (ver
!= NULL
&& ver
[0] == '\0')
856 bind
= elfcpp::STB_WEAK
;
862 bind
= elfcpp::STB_GLOBAL
;
870 shndx
= elfcpp::SHN_ABS
;
873 shndx
= elfcpp::SHN_COMMON
;
878 shndx
= elfcpp::SHN_UNDEF
;
882 switch (isym
->visibility
)
885 vis
= elfcpp::STV_PROTECTED
;
888 vis
= elfcpp::STV_INTERNAL
;
891 vis
= elfcpp::STV_HIDDEN
;
895 vis
= elfcpp::STV_DEFAULT
;
899 if (isym
->comdat_key
!= NULL
900 && isym
->comdat_key
[0] != '\0'
901 && !this->include_comdat_group(isym
->comdat_key
, layout
))
902 shndx
= elfcpp::SHN_UNDEF
;
905 osym
.put_st_value(0);
906 osym
.put_st_size(static_cast<Elf_size_type
>(isym
->size
));
907 osym
.put_st_info(bind
, elfcpp::STT_NOTYPE
);
908 osym
.put_st_other(vis
, 0);
909 osym
.put_st_shndx(shndx
);
912 symtab
->add_from_pluginobj
<size
, big_endian
>(this, name
, ver
, &sym
);
916 template<int size
, bool big_endian
>
917 Archive::Should_include
918 Sized_pluginobj
<size
, big_endian
>::do_should_include_member(
919 Symbol_table
* symtab
,
925 size_t tmpbuflen
= 0;
927 for (int i
= 0; i
< this->nsyms_
; ++i
)
929 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
930 const char* name
= sym
.name
;
932 Archive::Should_include t
= Archive::should_include_member(symtab
,
938 if (t
== Archive::SHOULD_INCLUDE_YES
)
947 return Archive::SHOULD_INCLUDE_UNKNOWN
;
950 // Get the size of a section. Not used for plugin objects.
952 template<int size
, bool big_endian
>
954 Sized_pluginobj
<size
, big_endian
>::do_section_size(unsigned int)
960 // Get the name of a section. Not used for plugin objects.
962 template<int size
, bool big_endian
>
964 Sized_pluginobj
<size
, big_endian
>::do_section_name(unsigned int)
967 return std::string();
970 // Return a view of the contents of a section. Not used for plugin objects.
972 template<int size
, bool big_endian
>
974 Sized_pluginobj
<size
, big_endian
>::do_section_contents(unsigned int)
982 // Return section flags. Not used for plugin objects.
984 template<int size
, bool big_endian
>
986 Sized_pluginobj
<size
, big_endian
>::do_section_flags(unsigned int)
992 // Return section entsize. Not used for plugin objects.
994 template<int size
, bool big_endian
>
996 Sized_pluginobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1002 // Return section address. Not used for plugin objects.
1004 template<int size
, bool big_endian
>
1006 Sized_pluginobj
<size
, big_endian
>::do_section_address(unsigned int)
1012 // Return section type. Not used for plugin objects.
1014 template<int size
, bool big_endian
>
1016 Sized_pluginobj
<size
, big_endian
>::do_section_type(unsigned int)
1022 // Return the section link field. Not used for plugin objects.
1024 template<int size
, bool big_endian
>
1026 Sized_pluginobj
<size
, big_endian
>::do_section_link(unsigned int)
1032 // Return the section link field. Not used for plugin objects.
1034 template<int size
, bool big_endian
>
1036 Sized_pluginobj
<size
, big_endian
>::do_section_info(unsigned int)
1042 // Return the section alignment. Not used for plugin objects.
1044 template<int size
, bool big_endian
>
1046 Sized_pluginobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1052 // Return the Xindex structure to use. Not used for plugin objects.
1054 template<int size
, bool big_endian
>
1056 Sized_pluginobj
<size
, big_endian
>::do_initialize_xindex()
1062 // Get symbol counts. Not used for plugin objects.
1064 template<int size
, bool big_endian
>
1066 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbol_counts(const Symbol_table
*,
1067 size_t*, size_t*) const
1072 // Get symbols. Not used for plugin objects.
1074 template<int size
, bool big_endian
>
1075 const Object::Symbols
*
1076 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbols() const
1081 // Class Plugin_finish. This task runs after all replacement files have
1082 // been added. For now, it's a placeholder for a possible plugin API
1083 // to allow the plugin to release most of its resources. The cleanup
1084 // handlers must be called later, because they can remove the temporary
1085 // object files that are needed until the end of the link.
1087 class Plugin_finish
: public Task
1090 Plugin_finish(Task_token
* this_blocker
, Task_token
* next_blocker
)
1091 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
1096 if (this->this_blocker_
!= NULL
)
1097 delete this->this_blocker_
;
1103 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1104 return this->this_blocker_
;
1109 locks(Task_locker
* tl
)
1110 { tl
->add(this, this->next_blocker_
); }
1115 // We could call early cleanup handlers here.
1120 { return "Plugin_finish"; }
1123 Task_token
* this_blocker_
;
1124 Task_token
* next_blocker_
;
1127 // Class Plugin_hook.
1129 Plugin_hook::~Plugin_hook()
1133 // Return whether a Plugin_hook task is runnable.
1136 Plugin_hook::is_runnable()
1138 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1139 return this->this_blocker_
;
1143 // Return a Task_locker for a Plugin_hook task. We don't need any
1147 Plugin_hook::locks(Task_locker
*)
1151 // Run the "all symbols read" plugin hook.
1154 Plugin_hook::run(Workqueue
* workqueue
)
1156 gold_assert(this->options_
.has_plugins());
1158 if (parameters
->options().entry())
1159 start_sym
= this->symtab_
->lookup(parameters
->options().entry());
1161 start_sym
= this->symtab_
->lookup("_start");
1162 if (start_sym
!= NULL
)
1163 start_sym
->set_in_real_elf();
1165 this->options_
.plugins()->all_symbols_read(workqueue
,
1167 this->input_objects_
,
1172 &this->this_blocker_
);
1173 workqueue
->queue_soon(new Plugin_finish(this->this_blocker_
,
1174 this->next_blocker_
));
1177 // The C interface routines called by the plugins.
1179 #ifdef ENABLE_PLUGINS
1181 // Register a claim-file handler.
1183 static enum ld_plugin_status
1184 register_claim_file(ld_plugin_claim_file_handler handler
)
1186 gold_assert(parameters
->options().has_plugins());
1187 parameters
->options().plugins()->set_claim_file_handler(handler
);
1191 // Register an all-symbols-read handler.
1193 static enum ld_plugin_status
1194 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
)
1196 gold_assert(parameters
->options().has_plugins());
1197 parameters
->options().plugins()->set_all_symbols_read_handler(handler
);
1201 // Register a cleanup handler.
1203 static enum ld_plugin_status
1204 register_cleanup(ld_plugin_cleanup_handler handler
)
1206 gold_assert(parameters
->options().has_plugins());
1207 parameters
->options().plugins()->set_cleanup_handler(handler
);
1211 // Add symbols from a plugin-claimed input file.
1213 static enum ld_plugin_status
1214 add_symbols(void* handle
, int nsyms
, const ld_plugin_symbol
* syms
)
1216 gold_assert(parameters
->options().has_plugins());
1217 Pluginobj
* obj
= parameters
->options().plugins()->make_plugin_object(
1218 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1221 obj
->store_incoming_symbols(nsyms
, syms
);
1225 // Get the input file information with an open (possibly re-opened)
1228 static enum ld_plugin_status
1229 get_input_file(const void* handle
, struct ld_plugin_input_file
* file
)
1231 gold_assert(parameters
->options().has_plugins());
1232 unsigned int obj_index
=
1233 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1234 return parameters
->options().plugins()->get_input_file(obj_index
, file
);
1237 // Release the input file.
1239 static enum ld_plugin_status
1240 release_input_file(const void* handle
)
1242 gold_assert(parameters
->options().has_plugins());
1243 unsigned int obj_index
=
1244 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1245 return parameters
->options().plugins()->release_input_file(obj_index
);
1248 // Get the symbol resolution info for a plugin-claimed input file.
1250 static enum ld_plugin_status
1251 get_symbols(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1253 gold_assert(parameters
->options().has_plugins());
1254 Pluginobj
* obj
= parameters
->options().plugins()->object(
1255 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1258 return obj
->get_symbol_resolution_info(nsyms
, syms
);
1261 // Add a new (real) input file generated by a plugin.
1263 static enum ld_plugin_status
1264 add_input_file(const char* pathname
)
1266 gold_assert(parameters
->options().has_plugins());
1267 return parameters
->options().plugins()->add_input_file(pathname
, false);
1270 // Add a new (real) library required by a plugin.
1272 static enum ld_plugin_status
1273 add_input_library(const char* pathname
)
1275 gold_assert(parameters
->options().has_plugins());
1276 return parameters
->options().plugins()->add_input_file(pathname
, true);
1279 // Set the extra library path to be used by libraries added via
1280 // add_input_library
1282 static enum ld_plugin_status
1283 set_extra_library_path(const char* path
)
1285 gold_assert(parameters
->options().has_plugins());
1286 return parameters
->options().plugins()->set_extra_library_path(path
);
1289 // Issue a diagnostic message from a plugin.
1291 static enum ld_plugin_status
1292 message(int level
, const char* format
, ...)
1295 va_start(args
, format
);
1300 parameters
->errors()->info(format
, args
);
1303 parameters
->errors()->warning(format
, args
);
1307 parameters
->errors()->error(format
, args
);
1310 parameters
->errors()->fatal(format
, args
);
1318 #endif // ENABLE_PLUGINS
1320 // Allocate a Pluginobj object of the appropriate size and endianness.
1323 make_sized_plugin_object(Input_file
* input_file
, off_t offset
, off_t filesize
)
1325 Pluginobj
* obj
= NULL
;
1327 parameters_force_valid_target();
1328 const Target
& target(parameters
->target());
1330 if (target
.get_size() == 32)
1332 if (target
.is_big_endian())
1333 #ifdef HAVE_TARGET_32_BIG
1334 obj
= new Sized_pluginobj
<32, true>(input_file
->filename(),
1335 input_file
, offset
, filesize
);
1337 gold_error(_("%s: not configured to support "
1338 "32-bit big-endian object"),
1339 input_file
->filename().c_str());
1342 #ifdef HAVE_TARGET_32_LITTLE
1343 obj
= new Sized_pluginobj
<32, false>(input_file
->filename(),
1344 input_file
, offset
, filesize
);
1346 gold_error(_("%s: not configured to support "
1347 "32-bit little-endian object"),
1348 input_file
->filename().c_str());
1351 else if (target
.get_size() == 64)
1353 if (target
.is_big_endian())
1354 #ifdef HAVE_TARGET_64_BIG
1355 obj
= new Sized_pluginobj
<64, true>(input_file
->filename(),
1356 input_file
, offset
, filesize
);
1358 gold_error(_("%s: not configured to support "
1359 "64-bit big-endian object"),
1360 input_file
->filename().c_str());
1363 #ifdef HAVE_TARGET_64_LITTLE
1364 obj
= new Sized_pluginobj
<64, false>(input_file
->filename(),
1365 input_file
, offset
, filesize
);
1367 gold_error(_("%s: not configured to support "
1368 "64-bit little-endian object"),
1369 input_file
->filename().c_str());
1373 gold_assert(obj
!= NULL
);
1377 } // End namespace gold.