1 // plugin.h -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2019 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;
50 class Plugin_recorder;
52 // This class represents a single plugin library.
57 Plugin(const char* filename)
61 claim_file_handler_(NULL),
62 all_symbols_read_handler_(NULL),
63 cleanup_handler_(NULL),
64 new_input_handler_(NULL),
65 #ifdef IGNORE_BROKEN_PLUGINS
74 // Load the library and call its entry point.
78 // Call the claim-file handler.
80 claim_file(struct ld_plugin_input_file* plugin_input_file);
82 // Call the all-symbols-read handler.
86 // Call the new_input handler.
88 new_input(struct ld_plugin_input_file* plugin_input_file);
90 // Call the cleanup handler.
94 // Register a claim-file handler.
96 set_claim_file_handler(ld_plugin_claim_file_handler handler)
97 { this->claim_file_handler_ = handler; }
99 // Register an all-symbols-read handler.
101 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler)
102 { this->all_symbols_read_handler_ = handler; }
104 // Register a claim-file handler.
106 set_cleanup_handler(ld_plugin_cleanup_handler handler)
107 { this->cleanup_handler_ = handler; }
109 // Register a new_input handler.
111 set_new_input_handler(ld_plugin_new_input_handler handler)
112 { this->new_input_handler_ = handler; }
116 add_option(const char* arg)
118 this->args_.push_back(arg);
120 #ifdef IGNORE_BROKEN_PLUGINS
123 { return this->broken_; }
128 { return this->filename_; }
131 Plugin(const Plugin&);
132 Plugin& operator=(const Plugin&);
134 // The shared library handle returned by dlopen.
136 // The argument string given to --plugin.
137 std::string filename_;
138 // The list of argument string given to --plugin-opt.
139 std::vector<std::string> args_;
140 // The plugin's event handlers.
141 ld_plugin_claim_file_handler claim_file_handler_;
142 ld_plugin_all_symbols_read_handler all_symbols_read_handler_;
143 ld_plugin_cleanup_handler cleanup_handler_;
144 ld_plugin_new_input_handler new_input_handler_;
145 #ifdef IGNORE_BROKEN_PLUGINS
146 // TRUE if we cannot open this plugin
149 // TRUE if the cleanup handlers have been called.
153 // A manager class for plugins.
158 Plugin_manager(const General_options& options)
159 : plugins_(), objects_(), deferred_layout_objects_(), input_file_(NULL),
160 plugin_input_file_(), rescannable_(), undefined_symbols_(),
161 any_claimed_(false), in_replacement_phase_(false), any_added_(false),
162 in_claim_file_handler_(false),
163 options_(options), workqueue_(NULL), task_(NULL), input_objects_(NULL),
164 symtab_(NULL), layout_(NULL), dirpath_(NULL), mapfile_(NULL),
165 this_blocker_(NULL), extra_search_path_(), lock_(NULL),
166 initialize_lock_(&lock_), defsym_defines_set_(),
168 { this->current_ = plugins_.end(); }
172 // Returns true if the symbol name is used in the LHS of a defsym.
174 is_defsym_def(const char* sym_name) const
176 return defsym_defines_set_.find(sym_name) != defsym_defines_set_.end();
179 // Add a plugin library.
181 add_plugin(const char* filename)
182 { this->plugins_.push_back(new Plugin(filename)); }
184 // Add an argument to the current plugin.
186 add_plugin_option(const char* opt)
188 Plugin* last = this->plugins_.back();
189 last->add_option(opt);
192 // Load all plugin libraries.
194 load_plugins(Layout* layout);
196 // Call the plugin claim-file handlers in turn to see if any claim the file.
198 claim_file(Input_file* input_file, off_t offset, off_t filesize,
201 // Get the object associated with the handle and check if it is an elf object.
202 // If it is not a Pluginobj, it is an elf object.
204 get_elf_object(const void* handle);
206 // True if the claim_file handler of the plugins is being called.
208 in_claim_file_handler()
209 { return in_claim_file_handler_; }
211 // Let the plugin manager save an archive for later rescanning.
212 // This takes ownership of the Archive pointer.
214 save_archive(Archive*);
216 // Let the plugin manager save an input group for later rescanning.
217 // This takes ownership of the Input_group pointer.
219 save_input_group(Input_group*);
221 // Call the all-symbols-read handlers.
223 all_symbols_read(Workqueue* workqueue, Task* task,
224 Input_objects* input_objects, Symbol_table* symtab,
225 Dirsearch* dirpath, Mapfile* mapfile,
226 Task_token** last_blocker);
228 // Tell the plugin manager that we've a new undefined symbol which
229 // may require rescanning.
231 new_undefined_symbol(Symbol*);
233 // Run deferred layout.
235 layout_deferred_objects();
237 // Call the cleanup handlers.
241 // Register a claim-file handler.
243 set_claim_file_handler(ld_plugin_claim_file_handler handler)
245 gold_assert(this->current_ != plugins_.end());
246 (*this->current_)->set_claim_file_handler(handler);
249 // Register an all-symbols-read handler.
251 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler)
253 gold_assert(this->current_ != plugins_.end());
254 (*this->current_)->set_all_symbols_read_handler(handler);
257 // Register a new_input handler.
259 set_new_input_handler(ld_plugin_new_input_handler handler)
261 gold_assert(this->current_ != plugins_.end());
262 (*this->current_)->set_new_input_handler(handler);
265 // Register a claim-file handler.
267 set_cleanup_handler(ld_plugin_cleanup_handler handler)
269 gold_assert(this->current_ != plugins_.end());
270 (*this->current_)->set_cleanup_handler(handler);
273 // Make a new Pluginobj object. This is called when the plugin calls
274 // the add_symbols API.
276 make_plugin_object(unsigned int handle);
278 // Return the object associated with the given HANDLE.
280 object(unsigned int handle) const
282 if (handle >= this->objects_.size())
284 return this->objects_[handle];
287 // Return TRUE if any input files have been claimed by a plugin
288 // and we are still in the initial input phase.
290 should_defer_layout() const
291 { return this->any_claimed_ && !this->in_replacement_phase_; }
293 // Add a regular object to the deferred layout list. These are
294 // objects whose layout has been deferred until after the
295 // replacement files have arrived.
297 add_deferred_layout_object(Relobj* obj)
298 { this->deferred_layout_objects_.push_back(obj); }
300 // Get input file information with an open (possibly re-opened)
303 get_input_file(unsigned int handle, struct ld_plugin_input_file* file);
306 get_view(unsigned int handle, const void **viewp);
308 // Release an input file.
310 release_input_file(unsigned int handle);
312 // Add a new input file.
314 add_input_file(const char* pathname, bool is_lib);
316 // Set the extra library path.
318 set_extra_library_path(const char* path);
320 // Return TRUE if we are in the replacement phase.
322 in_replacement_phase() const
323 { return this->in_replacement_phase_; }
326 input_objects() const
327 { return this->input_objects_; }
331 { return this->symtab_; }
335 { return this->layout_; }
339 { return this->recorder_; }
342 Plugin_manager(const Plugin_manager&);
343 Plugin_manager& operator=(const Plugin_manager&);
345 // Plugin_rescan is a Task which calls the private rescan method.
346 friend class Plugin_rescan;
348 // An archive or input group which may have to be rescanned if a
349 // plugin adds a new file.
356 Input_group* input_group;
359 Rescannable(Archive* archive)
361 { this->u.archive = archive; }
363 Rescannable(Input_group* input_group)
365 { this->u.input_group = input_group; }
368 typedef std::list<Plugin*> Plugin_list;
369 typedef std::vector<Object*> Object_list;
370 typedef std::vector<Relobj*> Deferred_layout_list;
371 typedef std::vector<Rescannable> Rescannable_list;
372 typedef std::vector<Symbol*> Undefined_symbol_list;
374 // Rescan archives for undefined symbols.
378 // See whether the rescannable at index I defines SYM.
380 rescannable_defines(size_t i, Symbol* sym);
382 // The list of plugin libraries.
383 Plugin_list plugins_;
384 // A pointer to the current plugin. Used while loading plugins.
385 Plugin_list::iterator current_;
387 // The list of plugin objects. The index of an item in this list
388 // serves as the "handle" that we pass to the plugins.
389 Object_list objects_;
391 // The list of regular objects whose layout has been deferred.
392 Deferred_layout_list deferred_layout_objects_;
394 // The file currently up for claim by the plugins.
395 Input_file* input_file_;
396 struct ld_plugin_input_file plugin_input_file_;
398 // A list of archives and input groups being saved for possible
400 Rescannable_list rescannable_;
402 // A list of undefined symbols found in added files.
403 Undefined_symbol_list undefined_symbols_;
405 // Whether any input files have been claimed by a plugin.
408 // Set to true after the all symbols read event; indicates that we
409 // are processing replacement files whose symbols should replace the
410 // placeholder symbols from the Pluginobj objects.
411 bool in_replacement_phase_;
413 // Whether any input files or libraries were added by a plugin.
416 // Set to true when the claim_file handler of a plugin is called.
417 bool in_claim_file_handler_;
419 const General_options& options_;
420 Workqueue* workqueue_;
422 Input_objects* input_objects_;
423 Symbol_table* symtab_;
427 Task_token* this_blocker_;
429 // An extra directory to search for the libraries passed by
430 // add_input_library.
431 std::string extra_search_path_;
433 Initialize_lock initialize_lock_;
435 // Keep track of all symbols defined by defsym.
436 typedef Unordered_set<std::string> Defsym_defines_set;
437 Defsym_defines_set defsym_defines_set_;
439 // Class to record plugin actions.
440 Plugin_recorder* recorder_;
444 // An object file claimed by a plugin. This is an abstract base class.
445 // The implementation is the template class Sized_pluginobj.
447 class Pluginobj : public Object
451 typedef std::vector<Symbol*> Symbols;
453 Pluginobj(const std::string& name, Input_file* input_file, off_t offset,
456 // Fill in the symbol resolution status for the given plugin symbols.
458 get_symbol_resolution_info(Symbol_table* symtab,
460 ld_plugin_symbol* syms,
463 // Store the incoming symbols from the plugin for later processing.
465 store_incoming_symbols(int nsyms, const struct ld_plugin_symbol* syms)
467 this->nsyms_ = nsyms;
471 // Return TRUE if the comdat group with key COMDAT_KEY from this object
474 include_comdat_group(std::string comdat_key, Layout* layout);
476 // Return the filename.
479 { return this->input_file()->filename(); }
481 // Return the file descriptor.
484 { return this->input_file()->file().descriptor(); }
486 // Return the size of the file or archive member.
489 { return this->filesize_; }
491 // Return the word size of the object file.
494 { gold_unreachable(); }
496 // Return TRUE if this is a big-endian object file.
498 is_big_endian() const
499 { gold_unreachable(); }
502 // Return TRUE if this is an object claimed by a plugin.
507 // The number of symbols provided by the plugin.
510 // The symbols provided by the plugin.
511 const struct ld_plugin_symbol* syms_;
513 // The entries in the symbol table for the external symbols.
517 // Size of the file (or archive member).
519 // Map a comdat key symbol to a boolean indicating whether the comdat
520 // group in this object with that key should be kept.
521 typedef Unordered_map<std::string, bool> Comdat_map;
522 Comdat_map comdat_map_;
525 // A plugin object, size-specific version.
527 template<int size, bool big_endian>
528 class Sized_pluginobj : public Pluginobj
531 Sized_pluginobj(const std::string& name, Input_file* input_file,
532 off_t offset, off_t filesize);
536 do_read_symbols(Read_symbols_data*);
538 // Lay out the input sections.
540 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
542 // Add the symbols to the symbol table.
544 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
546 Archive::Should_include
547 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
550 // Iterate over global symbols, calling a visitor class V for each.
552 do_for_all_global_symbols(Read_symbols_data* sd,
553 Library_base::Symbol_visitor_base* v);
555 // Iterate over local symbols, calling a visitor class V for each GOT offset
556 // associated with a local symbol.
558 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
560 // Get the size of a section.
562 do_section_size(unsigned int shndx);
564 // Get the name of a section.
566 do_section_name(unsigned int shndx) const;
568 // Return a view of the contents of a section.
570 do_section_contents(unsigned int shndx, section_size_type* plen,
573 // Return section flags.
575 do_section_flags(unsigned int shndx);
577 // Return section entsize.
579 do_section_entsize(unsigned int shndx);
581 // Return section address.
583 do_section_address(unsigned int shndx);
585 // Return section type.
587 do_section_type(unsigned int shndx);
589 // Return the section link field.
591 do_section_link(unsigned int shndx);
593 // Return the section link field.
595 do_section_info(unsigned int shndx);
597 // Return the section alignment.
599 do_section_addralign(unsigned int shndx);
601 // Return the Xindex structure to use.
603 do_initialize_xindex();
605 // Get symbol counts.
607 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
609 // Get global symbols.
611 do_get_global_symbols() const;
613 // Add placeholder symbols from a claimed file.
615 add_symbols_from_plugin(int nsyms, const ld_plugin_symbol* syms);
622 // This Task handles handles the "all symbols read" event hook.
623 // The plugin may add additional input files at this time, which must
624 // be queued for reading.
626 class Plugin_hook : public Task
629 Plugin_hook(const General_options& options, Input_objects* input_objects,
630 Symbol_table* symtab, Layout* /*layout*/, Dirsearch* dirpath,
631 Mapfile* mapfile, Task_token* this_blocker,
632 Task_token* next_blocker)
633 : options_(options), input_objects_(input_objects), symtab_(symtab),
634 dirpath_(dirpath), mapfile_(mapfile),
635 this_blocker_(this_blocker), next_blocker_(next_blocker)
640 // The standard Task methods.
653 { return "Plugin_hook"; }
656 const General_options& options_;
657 Input_objects* input_objects_;
658 Symbol_table* symtab_;
661 Task_token* this_blocker_;
662 Task_token* next_blocker_;
665 } // End namespace gold.
667 #endif // !defined(GOLD_PLUGIN_H)