1 // plugin.h -- plugin manager for gold -*- C++ -*-
3 // Copyright 2008 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
)
63 // Load the library and call its entry point.
67 // Call the claim-file handler.
69 claim_file(struct ld_plugin_input_file
*plugin_input_file
);
71 // Call the all-symbols-read handler.
75 // Call the cleanup handler.
79 // Register a claim-file handler.
81 set_claim_file_handler(ld_plugin_claim_file_handler handler
)
82 { this->claim_file_handler_
= handler
; }
84 // Register an all-symbols-read handler.
86 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler
)
87 { this->all_symbols_read_handler_
= handler
; }
89 // Register a claim-file handler.
91 set_cleanup_handler(ld_plugin_cleanup_handler handler
)
92 { this->cleanup_handler_
= handler
; }
96 add_option(const char *arg
)
98 this->args_
.push_back(arg
);
102 Plugin(const Plugin
&);
103 Plugin
& operator=(const Plugin
&);
105 // The shared library handle returned by dlopen.
107 // The argument string given to --plugin.
108 std::string filename_
;
109 // The list of argument string given to --plugin-opt.
110 std::vector
<std::string
> args_
;
111 // The plugin's event handlers.
112 ld_plugin_claim_file_handler claim_file_handler_
;
113 ld_plugin_all_symbols_read_handler all_symbols_read_handler_
;
114 ld_plugin_cleanup_handler cleanup_handler_
;
117 // A manager class for plugins.
122 Plugin_manager(const General_options
& options
)
123 : plugins_(), objects_(), deferred_layout_objects_(), input_file_(NULL
),
124 plugin_input_file_(), in_replacement_phase_(false), cleanup_done_(false),
125 options_(options
), workqueue_(NULL
), input_objects_(NULL
), symtab_(NULL
),
126 layout_(NULL
), dirpath_(NULL
), mapfile_(NULL
), this_blocker_(NULL
)
127 { this->current_
= plugins_
.end(); }
131 // Add a plugin library.
133 add_plugin(const char* filename
)
134 { this->plugins_
.push_back(new Plugin(filename
)); }
136 // Add an argument to the current plugin.
138 add_plugin_option(const char* opt
)
140 Plugin
* last
= this->plugins_
.back();
141 last
->add_option(opt
);
144 // Load all plugin libraries.
148 // Call the plugin claim-file handlers in turn to see if any claim the file.
150 claim_file(Input_file
*input_file
, off_t offset
, off_t filesize
);
152 // Call the all-symbols-read handlers.
154 all_symbols_read(Workqueue
* workqueue
, Input_objects
* input_objects
,
155 Symbol_table
* symtab
, Layout
* layout
, Dirsearch
* dirpath
,
156 Mapfile
* mapfile
, Task_token
** last_blocker
);
158 // Run deferred layout.
160 layout_deferred_objects();
162 // Call the cleanup handlers.
166 // Register a claim-file handler.
168 set_claim_file_handler(ld_plugin_claim_file_handler handler
)
170 gold_assert(this->current_
!= plugins_
.end());
171 (*this->current_
)->set_claim_file_handler(handler
);
174 // Register an all-symbols-read handler.
176 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler
)
178 gold_assert(this->current_
!= plugins_
.end());
179 (*this->current_
)->set_all_symbols_read_handler(handler
);
182 // Register a claim-file handler.
184 set_cleanup_handler(ld_plugin_cleanup_handler handler
)
186 gold_assert(this->current_
!= plugins_
.end());
187 (*this->current_
)->set_cleanup_handler(handler
);
190 // Make a new Pluginobj object. This is called when the plugin calls
191 // the add_symbols API.
193 make_plugin_object(unsigned int handle
);
195 // Return the Pluginobj associated with the given HANDLE.
197 object(unsigned int handle
) const
199 if (handle
>= this->objects_
.size())
201 return this->objects_
[handle
];
204 // Return TRUE if any input files have been claimed by a plugin
205 // and we are still in the initial input phase.
207 should_defer_layout() const
208 { return !this->objects_
.empty() && !this->in_replacement_phase_
; }
210 // Add a regular object to the deferred layout list. These are
211 // objects whose layout has been deferred until after the
212 // replacement files have arrived.
214 add_deferred_layout_object(Relobj
* obj
)
215 { this->deferred_layout_objects_
.push_back(obj
); }
217 // Add a new input file.
219 add_input_file(char *pathname
);
221 // Return TRUE if we are in the replacement phase.
223 in_replacement_phase() const
224 { return this->in_replacement_phase_
; }
227 Plugin_manager(const Plugin_manager
&);
228 Plugin_manager
& operator=(const Plugin_manager
&);
230 typedef std::list
<Plugin
*> Plugin_list
;
231 typedef std::vector
<Pluginobj
*> Object_list
;
232 typedef std::vector
<Relobj
*> Deferred_layout_list
;
234 // The list of plugin libraries.
235 Plugin_list plugins_
;
236 // A pointer to the current plugin. Used while loading plugins.
237 Plugin_list::iterator current_
;
239 // The list of plugin objects. The index of an item in this list
240 // serves as the "handle" that we pass to the plugins.
241 Object_list objects_
;
243 // The list of regular objects whose layout has been deferred.
244 Deferred_layout_list deferred_layout_objects_
;
246 // The file currently up for claim by the plugins.
247 Input_file
* input_file_
;
248 struct ld_plugin_input_file plugin_input_file_
;
250 // TRUE after the all symbols read event; indicates that we are
251 // processing replacement files whose symbols should replace the
252 // placeholder symbols from the Pluginobj objects.
253 bool in_replacement_phase_
;
255 // TRUE if the cleanup handlers have been called.
258 const General_options
& options_
;
259 Workqueue
* workqueue_
;
260 Input_objects
* input_objects_
;
261 Symbol_table
* symtab_
;
265 Task_token
* this_blocker_
;
269 // An object file claimed by a plugin. This is an abstract base class.
270 // The implementation is the template class Sized_pluginobj.
272 class Pluginobj
: public Object
276 typedef std::vector
<Symbol
*> Symbols
;
278 Pluginobj(const std::string
& name
, Input_file
* input_file
, off_t offset
);
280 // Fill in the symbol resolution status for the given plugin symbols.
282 get_symbol_resolution_info(int nsyms
, ld_plugin_symbol
* syms
) const;
284 // Add symbol information to the global symbol table.
286 add_symbols(Symbol_table
* symtab
, Layout
* layout
)
287 { this->do_add_symbols(symtab
, layout
); }
289 // Store the incoming symbols from the plugin for later processing.
291 store_incoming_symbols(int nsyms
, const struct ld_plugin_symbol
* syms
)
293 this->nsyms_
= nsyms
;
297 // Return TRUE if the comdat group with key COMDAT_KEY from this object
300 include_comdat_group(std::string comdat_key
, Layout
* layout
);
303 // Return TRUE if this is an object claimed by a plugin.
308 // Add symbol information to the global symbol table--implemented by
311 do_add_symbols(Symbol_table
*, Layout
*) = 0;
313 // The number of symbols provided by the plugin.
316 // The symbols provided by the plugin.
317 const struct ld_plugin_symbol
* syms_
;
319 // The entries in the symbol table for the external symbols.
323 // Map a comdat key symbol to a boolean indicating whether the comdat
324 // group in this object with that key should be kept.
325 typedef Unordered_map
<std::string
, bool> Comdat_map
;
326 Comdat_map comdat_map_
;
329 // A plugin object, size-specific version.
331 template<int size
, bool big_endian
>
332 class Sized_pluginobj
: public Pluginobj
335 Sized_pluginobj(const std::string
& name
, Input_file
* input_file
,
340 do_read_symbols(Read_symbols_data
*);
342 // Lay out the input sections.
344 do_layout(Symbol_table
*, Layout
*, Read_symbols_data
*);
346 // Add the symbols to the symbol table.
348 do_add_symbols(Symbol_table
*, Read_symbols_data
*);
351 do_add_symbols(Symbol_table
*, Layout
*);
353 // Get the size of a section.
355 do_section_size(unsigned int shndx
);
357 // Get the name of a section.
359 do_section_name(unsigned int shndx
);
361 // Return a view of the contents of a section.
363 do_section_contents(unsigned int shndx
);
365 // Return section flags.
367 do_section_flags(unsigned int shndx
);
369 // Return section address.
371 do_section_address(unsigned int shndx
);
373 // Return section type.
375 do_section_type(unsigned int shndx
);
377 // Return the section link field.
379 do_section_link(unsigned int shndx
);
381 // Return the section link field.
383 do_section_info(unsigned int shndx
);
385 // Return the section alignment.
387 do_section_addralign(unsigned int shndx
);
389 // Return the Xindex structure to use.
391 do_initialize_xindex();
393 // Get symbol counts.
395 do_get_global_symbol_counts(const Symbol_table
*, size_t*, size_t*) const;
397 // Add placeholder symbols from a claimed file.
399 add_symbols_from_plugin(int nsyms
, const ld_plugin_symbol
* syms
);
406 // This Task handles adding the symbols to the symbol table. These
407 // tasks must be run in the same order as the arguments appear on the
410 class Add_plugin_symbols
: public Task
413 // THIS_BLOCKER is used to prevent this task from running before the
414 // one for the previous input file. NEXT_BLOCKER is used to prevent
415 // the next task from running.
416 Add_plugin_symbols(Symbol_table
* symtab
,
419 Task_token
* this_blocker
,
420 Task_token
* next_blocker
)
421 : symtab_(symtab
), layout_(layout
), obj_(obj
),
422 this_blocker_(this_blocker
), next_blocker_(next_blocker
)
425 ~Add_plugin_symbols();
427 // The standard Task methods.
440 { return "Add_plugin_symbols " + this->obj_
->name(); }
443 Symbol_table
* symtab_
;
446 Task_token
* this_blocker_
;
447 Task_token
* next_blocker_
;
450 // This Task handles handles the "all symbols read" event hook.
451 // The plugin may add additional input files at this time, which must
452 // be queued for reading.
454 class Plugin_hook
: public Task
457 Plugin_hook(const General_options
& options
, Input_objects
* input_objects
,
458 Symbol_table
* symtab
, Layout
* layout
, Dirsearch
* dirpath
,
459 Mapfile
* mapfile
, Task_token
* this_blocker
,
460 Task_token
* next_blocker
)
461 : options_(options
), input_objects_(input_objects
), symtab_(symtab
),
462 layout_(layout
), dirpath_(dirpath
), mapfile_(mapfile
),
463 this_blocker_(this_blocker
), next_blocker_(next_blocker
)
468 // The standard Task methods.
481 { return "Plugin_hook"; }
484 const General_options
& options_
;
485 Input_objects
* input_objects_
;
486 Symbol_table
* symtab_
;
490 Task_token
* this_blocker_
;
491 Task_token
* next_blocker_
;
494 } // End namespace gold.
496 #endif // !defined(GOLD_PLUGIN_H)