1 // plugin.h -- 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.
30 #include "plugin-api.h"
31 #include "workqueue.h"
36 class General_options
;
51 // This class represents a single plugin library.
56 Plugin(const char* filename
)
60 claim_file_handler_(NULL
),
61 all_symbols_read_handler_(NULL
),
62 cleanup_handler_(NULL
),
69 // Load the library and call its entry point.
73 // Call the claim-file handler.
75 claim_file(struct ld_plugin_input_file
* plugin_input_file
);
77 // Call the all-symbols-read handler.
81 // Call the cleanup handler.
85 // Register a claim-file handler.
87 set_claim_file_handler(ld_plugin_claim_file_handler handler
)
88 { this->claim_file_handler_
= handler
; }
90 // Register an all-symbols-read handler.
92 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler
)
93 { this->all_symbols_read_handler_
= handler
; }
95 // Register a claim-file handler.
97 set_cleanup_handler(ld_plugin_cleanup_handler handler
)
98 { this->cleanup_handler_
= handler
; }
102 add_option(const char* arg
)
104 this->args_
.push_back(arg
);
108 Plugin(const Plugin
&);
109 Plugin
& operator=(const Plugin
&);
111 // The shared library handle returned by dlopen.
113 // The argument string given to --plugin.
114 std::string filename_
;
115 // The list of argument string given to --plugin-opt.
116 std::vector
<std::string
> args_
;
117 // The plugin's event handlers.
118 ld_plugin_claim_file_handler claim_file_handler_
;
119 ld_plugin_all_symbols_read_handler all_symbols_read_handler_
;
120 ld_plugin_cleanup_handler cleanup_handler_
;
121 // TRUE if the cleanup handlers have been called.
125 // A manager class for plugins.
130 Plugin_manager(const General_options
& options
)
131 : plugins_(), objects_(), deferred_layout_objects_(), input_file_(NULL
),
132 plugin_input_file_(), rescannable_(), undefined_symbols_(),
133 any_claimed_(false), in_replacement_phase_(false), any_added_(false),
134 options_(options
), workqueue_(NULL
), task_(NULL
), input_objects_(NULL
),
135 symtab_(NULL
), layout_(NULL
), dirpath_(NULL
), mapfile_(NULL
),
136 this_blocker_(NULL
), extra_search_path_()
137 { this->current_
= plugins_
.end(); }
141 // Add a plugin library.
143 add_plugin(const char* filename
)
144 { this->plugins_
.push_back(new Plugin(filename
)); }
146 // Add an argument to the current plugin.
148 add_plugin_option(const char* opt
)
150 Plugin
* last
= this->plugins_
.back();
151 last
->add_option(opt
);
154 // Load all plugin libraries.
158 // Call the plugin claim-file handlers in turn to see if any claim the file.
160 claim_file(Input_file
* input_file
, off_t offset
, off_t filesize
);
162 // Let the plugin manager save an archive for later rescanning.
163 // This takes ownership of the Archive pointer.
165 save_archive(Archive
*);
167 // Let the plugin manager save an input group for later rescanning.
168 // This takes ownership of the Input_group pointer.
170 save_input_group(Input_group
*);
172 // Call the all-symbols-read handlers.
174 all_symbols_read(Workqueue
* workqueue
, Task
* task
,
175 Input_objects
* input_objects
, Symbol_table
* symtab
,
176 Layout
* layout
, Dirsearch
* dirpath
, Mapfile
* mapfile
,
177 Task_token
** last_blocker
);
179 // Tell the plugin manager that we've a new undefined symbol which
180 // may require rescanning.
182 new_undefined_symbol(Symbol
*);
184 // Run deferred layout.
186 layout_deferred_objects();
188 // Call the cleanup handlers.
192 // Register a claim-file handler.
194 set_claim_file_handler(ld_plugin_claim_file_handler handler
)
196 gold_assert(this->current_
!= plugins_
.end());
197 (*this->current_
)->set_claim_file_handler(handler
);
200 // Register an all-symbols-read handler.
202 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler
)
204 gold_assert(this->current_
!= plugins_
.end());
205 (*this->current_
)->set_all_symbols_read_handler(handler
);
208 // Register a claim-file handler.
210 set_cleanup_handler(ld_plugin_cleanup_handler handler
)
212 gold_assert(this->current_
!= plugins_
.end());
213 (*this->current_
)->set_cleanup_handler(handler
);
216 // Make a new Pluginobj object. This is called when the plugin calls
217 // the add_symbols API.
219 make_plugin_object(unsigned int handle
);
221 // Return the Pluginobj associated with the given HANDLE.
223 object(unsigned int handle
) const
225 if (handle
>= this->objects_
.size())
227 return this->objects_
[handle
];
230 // Return TRUE if any input files have been claimed by a plugin
231 // and we are still in the initial input phase.
233 should_defer_layout() const
234 { return !this->objects_
.empty() && !this->in_replacement_phase_
; }
236 // Add a regular object to the deferred layout list. These are
237 // objects whose layout has been deferred until after the
238 // replacement files have arrived.
240 add_deferred_layout_object(Relobj
* obj
)
241 { this->deferred_layout_objects_
.push_back(obj
); }
243 // Get input file information with an open (possibly re-opened)
246 get_input_file(unsigned int handle
, struct ld_plugin_input_file
* file
);
248 // Release an input file.
250 release_input_file(unsigned int handle
);
252 // Add a new input file.
254 add_input_file(const char* pathname
, bool is_lib
);
256 // Set the extra library path.
258 set_extra_library_path(const char* path
);
260 // Return TRUE if we are in the replacement phase.
262 in_replacement_phase() const
263 { return this->in_replacement_phase_
; }
266 Plugin_manager(const Plugin_manager
&);
267 Plugin_manager
& operator=(const Plugin_manager
&);
269 // Plugin_rescan is a Task which calls the private rescan method.
270 friend class Plugin_rescan
;
272 // An archive or input group which may have to be rescanned if a
273 // plugin adds a new file.
280 Input_group
* input_group
;
283 Rescannable(Archive
* archive
)
285 { this->u
.archive
= archive
; }
287 Rescannable(Input_group
* input_group
)
289 { this->u
.input_group
= input_group
; }
292 typedef std::list
<Plugin
*> Plugin_list
;
293 typedef std::vector
<Pluginobj
*> Object_list
;
294 typedef std::vector
<Relobj
*> Deferred_layout_list
;
295 typedef std::vector
<Rescannable
> Rescannable_list
;
296 typedef std::vector
<Symbol
*> Undefined_symbol_list
;
298 // Rescan archives for undefined symbols.
302 // See whether the rescannable at index I defines SYM.
304 rescannable_defines(size_t i
, Symbol
* sym
);
306 // The list of plugin libraries.
307 Plugin_list plugins_
;
308 // A pointer to the current plugin. Used while loading plugins.
309 Plugin_list::iterator current_
;
311 // The list of plugin objects. The index of an item in this list
312 // serves as the "handle" that we pass to the plugins.
313 Object_list objects_
;
315 // The list of regular objects whose layout has been deferred.
316 Deferred_layout_list deferred_layout_objects_
;
318 // The file currently up for claim by the plugins.
319 Input_file
* input_file_
;
320 struct ld_plugin_input_file plugin_input_file_
;
322 // A list of archives and input groups being saved for possible
324 Rescannable_list rescannable_
;
326 // A list of undefined symbols found in added files.
327 Undefined_symbol_list undefined_symbols_
;
329 // Whether any input files have been claimed by a plugin.
332 // Set to true after the all symbols read event; indicates that we
333 // are processing replacement files whose symbols should replace the
334 // placeholder symbols from the Pluginobj objects.
335 bool in_replacement_phase_
;
337 // Whether any input files or libraries were added by a plugin.
340 const General_options
& options_
;
341 Workqueue
* workqueue_
;
343 Input_objects
* input_objects_
;
344 Symbol_table
* symtab_
;
348 Task_token
* this_blocker_
;
350 // An extra directory to seach for the libraries passed by
351 // add_input_library.
352 std::string extra_search_path_
;
356 // An object file claimed by a plugin. This is an abstract base class.
357 // The implementation is the template class Sized_pluginobj.
359 class Pluginobj
: public Object
363 typedef std::vector
<Symbol
*> Symbols
;
365 Pluginobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
368 // Fill in the symbol resolution status for the given plugin symbols.
370 get_symbol_resolution_info(int nsyms
, ld_plugin_symbol
* syms
) const;
372 // Store the incoming symbols from the plugin for later processing.
374 store_incoming_symbols(int nsyms
, const struct ld_plugin_symbol
* syms
)
376 this->nsyms_
= nsyms
;
380 // Return TRUE if the comdat group with key COMDAT_KEY from this object
383 include_comdat_group(std::string comdat_key
, Layout
* layout
);
385 // Return the filename.
388 { return this->input_file()->filename(); }
390 // Return the file descriptor.
393 { return this->input_file()->file().descriptor(); }
395 // Return the size of the file or archive member.
398 { return this->filesize_
; }
401 // Return TRUE if this is an object claimed by a plugin.
406 // The number of symbols provided by the plugin.
409 // The symbols provided by the plugin.
410 const struct ld_plugin_symbol
* syms_
;
412 // The entries in the symbol table for the external symbols.
416 // Size of the file (or archive member).
418 // Map a comdat key symbol to a boolean indicating whether the comdat
419 // group in this object with that key should be kept.
420 typedef Unordered_map
<std::string
, bool> Comdat_map
;
421 Comdat_map comdat_map_
;
424 // A plugin object, size-specific version.
426 template<int size
, bool big_endian
>
427 class Sized_pluginobj
: public Pluginobj
430 Sized_pluginobj(const std::string
& name
, Input_file
* input_file
,
431 off_t offset
, off_t filesize
);
435 do_read_symbols(Read_symbols_data
*);
437 // Lay out the input sections.
439 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
441 // Add the symbols to the symbol table.
443 do_add_symbols(Symbol_table
*, Read_symbols_data
*, Layout
*);
445 Archive::Should_include
446 do_should_include_member(Symbol_table
* symtab
, Layout
*, Read_symbols_data
*,
449 // Get the size of a section.
451 do_section_size(unsigned int shndx
);
453 // Get the name of a section.
455 do_section_name(unsigned int shndx
);
457 // Return a view of the contents of a section.
459 do_section_contents(unsigned int shndx
);
461 // Return section flags.
463 do_section_flags(unsigned int shndx
);
465 // Return section entsize.
467 do_section_entsize(unsigned int shndx
);
469 // Return section address.
471 do_section_address(unsigned int shndx
);
473 // Return section type.
475 do_section_type(unsigned int shndx
);
477 // Return the section link field.
479 do_section_link(unsigned int shndx
);
481 // Return the section link field.
483 do_section_info(unsigned int shndx
);
485 // Return the section alignment.
487 do_section_addralign(unsigned int shndx
);
489 // Return the Xindex structure to use.
491 do_initialize_xindex();
493 // Get symbol counts.
495 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const;
497 // Get global symbols.
499 do_get_global_symbols() const;
501 // Add placeholder symbols from a claimed file.
503 add_symbols_from_plugin(int nsyms
, const ld_plugin_symbol
* syms
);
510 // This Task handles handles the "all symbols read" event hook.
511 // The plugin may add additional input files at this time, which must
512 // be queued for reading.
514 class Plugin_hook
: public Task
517 Plugin_hook(const General_options
& options
, Input_objects
* input_objects
,
518 Symbol_table
* symtab
, Layout
* layout
, Dirsearch
* dirpath
,
519 Mapfile
* mapfile
, Task_token
* this_blocker
,
520 Task_token
* next_blocker
)
521 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
522 layout_(layout
), dirpath_(dirpath
), mapfile_(mapfile
),
523 this_blocker_(this_blocker
), next_blocker_(next_blocker
)
528 // The standard Task methods.
541 { return "Plugin_hook"; }
544 const General_options
& options_
;
545 Input_objects
* input_objects_
;
546 Symbol_table
* symtab_
;
550 Task_token
* this_blocker_
;
551 Task_token
* next_blocker_
;
554 } // End namespace gold.
556 #endif // !defined(GOLD_PLUGIN_H)