1 // plugin.h -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008, 2009, 2010 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
;
46 // This class represents a single plugin library.
51 Plugin(const char* filename
)
55 claim_file_handler_(NULL
),
56 all_symbols_read_handler_(NULL
),
57 cleanup_handler_(NULL
),
64 // Load the library and call its entry point.
68 // Call the claim-file handler.
70 claim_file(struct ld_plugin_input_file
* plugin_input_file
);
72 // Call the all-symbols-read handler.
76 // Call the cleanup handler.
80 // Register a claim-file handler.
82 set_claim_file_handler(ld_plugin_claim_file_handler handler
)
83 { this->claim_file_handler_
= handler
; }
85 // Register an all-symbols-read handler.
87 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler
)
88 { this->all_symbols_read_handler_
= handler
; }
90 // Register a claim-file handler.
92 set_cleanup_handler(ld_plugin_cleanup_handler handler
)
93 { this->cleanup_handler_
= handler
; }
97 add_option(const char* arg
)
99 this->args_
.push_back(arg
);
103 Plugin(const Plugin
&);
104 Plugin
& operator=(const Plugin
&);
106 // The shared library handle returned by dlopen.
108 // The argument string given to --plugin.
109 std::string filename_
;
110 // The list of argument string given to --plugin-opt.
111 std::vector
<std::string
> args_
;
112 // The plugin's event handlers.
113 ld_plugin_claim_file_handler claim_file_handler_
;
114 ld_plugin_all_symbols_read_handler all_symbols_read_handler_
;
115 ld_plugin_cleanup_handler cleanup_handler_
;
116 // TRUE if the cleanup handlers have been called.
120 // A manager class for plugins.
125 Plugin_manager(const General_options
& options
)
126 : plugins_(), objects_(), deferred_layout_objects_(), input_file_(NULL
),
127 plugin_input_file_(), in_replacement_phase_(false),
128 options_(options
), workqueue_(NULL
), task_(NULL
), input_objects_(NULL
),
129 symtab_(NULL
), layout_(NULL
), dirpath_(NULL
), mapfile_(NULL
),
130 this_blocker_(NULL
), extra_search_path_()
131 { this->current_
= plugins_
.end(); }
135 // Add a plugin library.
137 add_plugin(const char* filename
)
138 { this->plugins_
.push_back(new Plugin(filename
)); }
140 // Add an argument to the current plugin.
142 add_plugin_option(const char* opt
)
144 Plugin
* last
= this->plugins_
.back();
145 last
->add_option(opt
);
148 // Load all plugin libraries.
152 // Call the plugin claim-file handlers in turn to see if any claim the file.
154 claim_file(Input_file
* input_file
, off_t offset
, off_t filesize
);
156 // Call the all-symbols-read handlers.
158 all_symbols_read(Workqueue
* workqueue
, Task
* task
,
159 Input_objects
* input_objects
, Symbol_table
* symtab
,
160 Layout
* layout
, Dirsearch
* dirpath
, Mapfile
* mapfile
,
161 Task_token
** last_blocker
);
163 // Run deferred layout.
165 layout_deferred_objects();
167 // Call the cleanup handlers.
171 // Register a claim-file handler.
173 set_claim_file_handler(ld_plugin_claim_file_handler handler
)
175 gold_assert(this->current_
!= plugins_
.end());
176 (*this->current_
)->set_claim_file_handler(handler
);
179 // Register an all-symbols-read handler.
181 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler
)
183 gold_assert(this->current_
!= plugins_
.end());
184 (*this->current_
)->set_all_symbols_read_handler(handler
);
187 // Register a claim-file handler.
189 set_cleanup_handler(ld_plugin_cleanup_handler handler
)
191 gold_assert(this->current_
!= plugins_
.end());
192 (*this->current_
)->set_cleanup_handler(handler
);
195 // Make a new Pluginobj object. This is called when the plugin calls
196 // the add_symbols API.
198 make_plugin_object(unsigned int handle
);
200 // Return the Pluginobj associated with the given HANDLE.
202 object(unsigned int handle
) const
204 if (handle
>= this->objects_
.size())
206 return this->objects_
[handle
];
209 // Return TRUE if any input files have been claimed by a plugin
210 // and we are still in the initial input phase.
212 should_defer_layout() const
213 { return !this->objects_
.empty() && !this->in_replacement_phase_
; }
215 // Add a regular object to the deferred layout list. These are
216 // objects whose layout has been deferred until after the
217 // replacement files have arrived.
219 add_deferred_layout_object(Relobj
* obj
)
220 { this->deferred_layout_objects_
.push_back(obj
); }
222 // Get input file information with an open (possibly re-opened)
225 get_input_file(unsigned int handle
, struct ld_plugin_input_file
* file
);
227 // Release an input file.
229 release_input_file(unsigned int handle
);
231 // Add a new input file.
233 add_input_file(const char* pathname
, bool is_lib
);
235 // Set the extra library path.
237 set_extra_library_path(const char* path
);
239 // Return TRUE if we are in the replacement phase.
241 in_replacement_phase() const
242 { return this->in_replacement_phase_
; }
245 Plugin_manager(const Plugin_manager
&);
246 Plugin_manager
& operator=(const Plugin_manager
&);
248 typedef std::list
<Plugin
*> Plugin_list
;
249 typedef std::vector
<Pluginobj
*> Object_list
;
250 typedef std::vector
<Relobj
*> Deferred_layout_list
;
252 // The list of plugin libraries.
253 Plugin_list plugins_
;
254 // A pointer to the current plugin. Used while loading plugins.
255 Plugin_list::iterator current_
;
257 // The list of plugin objects. The index of an item in this list
258 // serves as the "handle" that we pass to the plugins.
259 Object_list objects_
;
261 // The list of regular objects whose layout has been deferred.
262 Deferred_layout_list deferred_layout_objects_
;
264 // The file currently up for claim by the plugins.
265 Input_file
* input_file_
;
266 struct ld_plugin_input_file plugin_input_file_
;
268 // TRUE after the all symbols read event; indicates that we are
269 // processing replacement files whose symbols should replace the
270 // placeholder symbols from the Pluginobj objects.
271 bool in_replacement_phase_
;
273 const General_options
& options_
;
274 Workqueue
* workqueue_
;
276 Input_objects
* input_objects_
;
277 Symbol_table
* symtab_
;
281 Task_token
* this_blocker_
;
283 // An extra directory to seach for the libraries passed by
284 // add_input_library.
285 std::string extra_search_path_
;
289 // An object file claimed by a plugin. This is an abstract base class.
290 // The implementation is the template class Sized_pluginobj.
292 class Pluginobj
: public Object
296 typedef std::vector
<Symbol
*> Symbols
;
298 Pluginobj(const std::string
& name
, Input_file
* input_file
, off_t offset
,
301 // Fill in the symbol resolution status for the given plugin symbols.
303 get_symbol_resolution_info(int nsyms
, ld_plugin_symbol
* syms
) const;
305 // Store the incoming symbols from the plugin for later processing.
307 store_incoming_symbols(int nsyms
, const struct ld_plugin_symbol
* syms
)
309 this->nsyms_
= nsyms
;
313 // Return TRUE if the comdat group with key COMDAT_KEY from this object
316 include_comdat_group(std::string comdat_key
, Layout
* layout
);
318 // Return the filename.
321 { return this->input_file()->filename(); }
323 // Return the file descriptor.
326 { return this->input_file()->file().descriptor(); }
328 // Return the size of the file or archive member.
331 { return this->filesize_
; }
334 // Return TRUE if this is an object claimed by a plugin.
339 // The number of symbols provided by the plugin.
342 // The symbols provided by the plugin.
343 const struct ld_plugin_symbol
* syms_
;
345 // The entries in the symbol table for the external symbols.
349 // Size of the file (or archive member).
351 // Map a comdat key symbol to a boolean indicating whether the comdat
352 // group in this object with that key should be kept.
353 typedef Unordered_map
<std::string
, bool> Comdat_map
;
354 Comdat_map comdat_map_
;
357 // A plugin object, size-specific version.
359 template<int size
, bool big_endian
>
360 class Sized_pluginobj
: public Pluginobj
363 Sized_pluginobj(const std::string
& name
, Input_file
* input_file
,
364 off_t offset
, off_t filesize
);
368 do_read_symbols(Read_symbols_data
*);
370 // Lay out the input sections.
372 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
374 // Add the symbols to the symbol table.
376 do_add_symbols(Symbol_table
*, Read_symbols_data
*, Layout
*);
378 Archive::Should_include
379 do_should_include_member(Symbol_table
* symtab
, Layout
*, Read_symbols_data
*,
382 // Get the size of a section.
384 do_section_size(unsigned int shndx
);
386 // Get the name of a section.
388 do_section_name(unsigned int shndx
);
390 // Return a view of the contents of a section.
392 do_section_contents(unsigned int shndx
);
394 // Return section flags.
396 do_section_flags(unsigned int shndx
);
398 // Return section entsize.
400 do_section_entsize(unsigned int shndx
);
402 // Return section address.
404 do_section_address(unsigned int shndx
);
406 // Return section type.
408 do_section_type(unsigned int shndx
);
410 // Return the section link field.
412 do_section_link(unsigned int shndx
);
414 // Return the section link field.
416 do_section_info(unsigned int shndx
);
418 // Return the section alignment.
420 do_section_addralign(unsigned int shndx
);
422 // Return the Xindex structure to use.
424 do_initialize_xindex();
426 // Get symbol counts.
428 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const;
430 // Get global symbols.
432 do_get_global_symbols() const;
434 // Add placeholder symbols from a claimed file.
436 add_symbols_from_plugin(int nsyms
, const ld_plugin_symbol
* syms
);
443 // This Task handles handles the "all symbols read" event hook.
444 // The plugin may add additional input files at this time, which must
445 // be queued for reading.
447 class Plugin_hook
: public Task
450 Plugin_hook(const General_options
& options
, Input_objects
* input_objects
,
451 Symbol_table
* symtab
, Layout
* layout
, Dirsearch
* dirpath
,
452 Mapfile
* mapfile
, Task_token
* this_blocker
,
453 Task_token
* next_blocker
)
454 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
455 layout_(layout
), dirpath_(dirpath
), mapfile_(mapfile
),
456 this_blocker_(this_blocker
), next_blocker_(next_blocker
)
461 // The standard Task methods.
474 { return "Plugin_hook"; }
477 const General_options
& options_
;
478 Input_objects
* input_objects_
;
479 Symbol_table
* symtab_
;
483 Task_token
* this_blocker_
;
484 Task_token
* next_blocker_
;
487 } // End namespace gold.
489 #endif // !defined(GOLD_PLUGIN_H)