Add plugin API for processing plugin-added input files
[deliverable/binutils-gdb.git] / gold / plugin.h
1 // plugin.h -- plugin manager for gold -*- C++ -*-
2
3 // Copyright (C) 2008-2017 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #ifndef GOLD_PLUGIN_H
24 #define GOLD_PLUGIN_H
25
26 #include <list>
27 #include <string>
28
29 #include "object.h"
30 #include "plugin-api.h"
31 #include "workqueue.h"
32
33 namespace gold
34 {
35
36 class General_options;
37 class Input_file;
38 class Input_objects;
39 class Archive;
40 class Input_group;
41 class Symbol;
42 class Symbol_table;
43 class Layout;
44 class Dirsearch;
45 class Mapfile;
46 class Task;
47 class Task_token;
48 class Pluginobj;
49 class Plugin_rescan;
50
51 // This class represents a single plugin library.
52
53 class Plugin
54 {
55 public:
56 Plugin(const char* filename)
57 : handle_(NULL),
58 filename_(filename),
59 args_(),
60 claim_file_handler_(NULL),
61 all_symbols_read_handler_(NULL),
62 cleanup_handler_(NULL),
63 new_input_handler_(NULL),
64 cleanup_done_(false)
65 { }
66
67 ~Plugin()
68 { }
69
70 // Load the library and call its entry point.
71 void
72 load();
73
74 // Call the claim-file handler.
75 bool
76 claim_file(struct ld_plugin_input_file* plugin_input_file);
77
78 // Call the all-symbols-read handler.
79 void
80 all_symbols_read();
81
82 // Call the new_input handler.
83 void
84 new_input(struct ld_plugin_input_file* plugin_input_file);
85
86 // Call the cleanup handler.
87 void
88 cleanup();
89
90 // Register a claim-file handler.
91 void
92 set_claim_file_handler(ld_plugin_claim_file_handler handler)
93 { this->claim_file_handler_ = handler; }
94
95 // Register an all-symbols-read handler.
96 void
97 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler)
98 { this->all_symbols_read_handler_ = handler; }
99
100 // Register a claim-file handler.
101 void
102 set_cleanup_handler(ld_plugin_cleanup_handler handler)
103 { this->cleanup_handler_ = handler; }
104
105 // Register a new_input handler.
106 void
107 set_new_input_handler(ld_plugin_new_input_handler handler)
108 { this->new_input_handler_ = handler; }
109
110 // Add an argument
111 void
112 add_option(const char* arg)
113 {
114 this->args_.push_back(arg);
115 }
116
117 private:
118 Plugin(const Plugin&);
119 Plugin& operator=(const Plugin&);
120
121 // The shared library handle returned by dlopen.
122 void* handle_;
123 // The argument string given to --plugin.
124 std::string filename_;
125 // The list of argument string given to --plugin-opt.
126 std::vector<std::string> args_;
127 // The plugin's event handlers.
128 ld_plugin_claim_file_handler claim_file_handler_;
129 ld_plugin_all_symbols_read_handler all_symbols_read_handler_;
130 ld_plugin_cleanup_handler cleanup_handler_;
131 ld_plugin_new_input_handler new_input_handler_;
132 // TRUE if the cleanup handlers have been called.
133 bool cleanup_done_;
134 };
135
136 // A manager class for plugins.
137
138 class Plugin_manager
139 {
140 public:
141 Plugin_manager(const General_options& options)
142 : plugins_(), objects_(), deferred_layout_objects_(), input_file_(NULL),
143 plugin_input_file_(), rescannable_(), undefined_symbols_(),
144 any_claimed_(false), in_replacement_phase_(false), any_added_(false),
145 in_claim_file_handler_(false),
146 options_(options), workqueue_(NULL), task_(NULL), input_objects_(NULL),
147 symtab_(NULL), layout_(NULL), dirpath_(NULL), mapfile_(NULL),
148 this_blocker_(NULL), extra_search_path_(), lock_(NULL),
149 initialize_lock_(&lock_)
150 { this->current_ = plugins_.end(); }
151
152 ~Plugin_manager();
153
154 // Add a plugin library.
155 void
156 add_plugin(const char* filename)
157 { this->plugins_.push_back(new Plugin(filename)); }
158
159 // Add an argument to the current plugin.
160 void
161 add_plugin_option(const char* opt)
162 {
163 Plugin* last = this->plugins_.back();
164 last->add_option(opt);
165 }
166
167 // Load all plugin libraries.
168 void
169 load_plugins(Layout* layout);
170
171 // Call the plugin claim-file handlers in turn to see if any claim the file.
172 Pluginobj*
173 claim_file(Input_file* input_file, off_t offset, off_t filesize,
174 Object* elf_object);
175
176 // Get the object associated with the handle and check if it is an elf object.
177 // If it is not a Pluginobj, it is an elf object.
178 Object*
179 get_elf_object(const void* handle);
180
181 // True if the claim_file handler of the plugins is being called.
182 bool
183 in_claim_file_handler()
184 { return in_claim_file_handler_; }
185
186 // Let the plugin manager save an archive for later rescanning.
187 // This takes ownership of the Archive pointer.
188 void
189 save_archive(Archive*);
190
191 // Let the plugin manager save an input group for later rescanning.
192 // This takes ownership of the Input_group pointer.
193 void
194 save_input_group(Input_group*);
195
196 // Call the all-symbols-read handlers.
197 void
198 all_symbols_read(Workqueue* workqueue, Task* task,
199 Input_objects* input_objects, Symbol_table* symtab,
200 Dirsearch* dirpath, Mapfile* mapfile,
201 Task_token** last_blocker);
202
203 // Tell the plugin manager that we've a new undefined symbol which
204 // may require rescanning.
205 void
206 new_undefined_symbol(Symbol*);
207
208 // Run deferred layout.
209 void
210 layout_deferred_objects();
211
212 // Call the cleanup handlers.
213 void
214 cleanup();
215
216 // Register a claim-file handler.
217 void
218 set_claim_file_handler(ld_plugin_claim_file_handler handler)
219 {
220 gold_assert(this->current_ != plugins_.end());
221 (*this->current_)->set_claim_file_handler(handler);
222 }
223
224 // Register an all-symbols-read handler.
225 void
226 set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler)
227 {
228 gold_assert(this->current_ != plugins_.end());
229 (*this->current_)->set_all_symbols_read_handler(handler);
230 }
231
232 // Register a new_input handler.
233 void
234 set_new_input_handler(ld_plugin_new_input_handler handler)
235 {
236 gold_assert(this->current_ != plugins_.end());
237 (*this->current_)->set_new_input_handler(handler);
238 }
239
240 // Register a claim-file handler.
241 void
242 set_cleanup_handler(ld_plugin_cleanup_handler handler)
243 {
244 gold_assert(this->current_ != plugins_.end());
245 (*this->current_)->set_cleanup_handler(handler);
246 }
247
248 // Make a new Pluginobj object. This is called when the plugin calls
249 // the add_symbols API.
250 Pluginobj*
251 make_plugin_object(unsigned int handle);
252
253 // Return the object associated with the given HANDLE.
254 Object*
255 object(unsigned int handle) const
256 {
257 if (handle >= this->objects_.size())
258 return NULL;
259 return this->objects_[handle];
260 }
261
262 // Return TRUE if any input files have been claimed by a plugin
263 // and we are still in the initial input phase.
264 bool
265 should_defer_layout() const
266 { return this->any_claimed_ && !this->in_replacement_phase_; }
267
268 // Add a regular object to the deferred layout list. These are
269 // objects whose layout has been deferred until after the
270 // replacement files have arrived.
271 void
272 add_deferred_layout_object(Relobj* obj)
273 { this->deferred_layout_objects_.push_back(obj); }
274
275 // Get input file information with an open (possibly re-opened)
276 // file descriptor.
277 ld_plugin_status
278 get_input_file(unsigned int handle, struct ld_plugin_input_file* file);
279
280 ld_plugin_status
281 get_view(unsigned int handle, const void **viewp);
282
283 // Release an input file.
284 ld_plugin_status
285 release_input_file(unsigned int handle);
286
287 // Add a new input file.
288 ld_plugin_status
289 add_input_file(const char* pathname, bool is_lib);
290
291 // Set the extra library path.
292 ld_plugin_status
293 set_extra_library_path(const char* path);
294
295 // Return TRUE if we are in the replacement phase.
296 bool
297 in_replacement_phase() const
298 { return this->in_replacement_phase_; }
299
300 Input_objects*
301 input_objects() const
302 { return this->input_objects_; }
303
304 Symbol_table*
305 symtab()
306 { return this->symtab_; }
307
308 Layout*
309 layout()
310 { return this->layout_; }
311
312 private:
313 Plugin_manager(const Plugin_manager&);
314 Plugin_manager& operator=(const Plugin_manager&);
315
316 // Plugin_rescan is a Task which calls the private rescan method.
317 friend class Plugin_rescan;
318
319 // An archive or input group which may have to be rescanned if a
320 // plugin adds a new file.
321 struct Rescannable
322 {
323 bool is_archive;
324 union
325 {
326 Archive* archive;
327 Input_group* input_group;
328 } u;
329
330 Rescannable(Archive* archive)
331 : is_archive(true)
332 { this->u.archive = archive; }
333
334 Rescannable(Input_group* input_group)
335 : is_archive(false)
336 { this->u.input_group = input_group; }
337 };
338
339 typedef std::list<Plugin*> Plugin_list;
340 typedef std::vector<Object*> Object_list;
341 typedef std::vector<Relobj*> Deferred_layout_list;
342 typedef std::vector<Rescannable> Rescannable_list;
343 typedef std::vector<Symbol*> Undefined_symbol_list;
344
345 // Rescan archives for undefined symbols.
346 void
347 rescan(Task*);
348
349 // See whether the rescannable at index I defines SYM.
350 bool
351 rescannable_defines(size_t i, Symbol* sym);
352
353 // The list of plugin libraries.
354 Plugin_list plugins_;
355 // A pointer to the current plugin. Used while loading plugins.
356 Plugin_list::iterator current_;
357
358 // The list of plugin objects. The index of an item in this list
359 // serves as the "handle" that we pass to the plugins.
360 Object_list objects_;
361
362 // The list of regular objects whose layout has been deferred.
363 Deferred_layout_list deferred_layout_objects_;
364
365 // The file currently up for claim by the plugins.
366 Input_file* input_file_;
367 struct ld_plugin_input_file plugin_input_file_;
368
369 // A list of archives and input groups being saved for possible
370 // later rescanning.
371 Rescannable_list rescannable_;
372
373 // A list of undefined symbols found in added files.
374 Undefined_symbol_list undefined_symbols_;
375
376 // Whether any input files have been claimed by a plugin.
377 bool any_claimed_;
378
379 // Set to true after the all symbols read event; indicates that we
380 // are processing replacement files whose symbols should replace the
381 // placeholder symbols from the Pluginobj objects.
382 bool in_replacement_phase_;
383
384 // Whether any input files or libraries were added by a plugin.
385 bool any_added_;
386
387 // Set to true when the claim_file handler of a plugin is called.
388 bool in_claim_file_handler_;
389
390 const General_options& options_;
391 Workqueue* workqueue_;
392 Task* task_;
393 Input_objects* input_objects_;
394 Symbol_table* symtab_;
395 Layout* layout_;
396 Dirsearch* dirpath_;
397 Mapfile* mapfile_;
398 Task_token* this_blocker_;
399
400 // An extra directory to search for the libraries passed by
401 // add_input_library.
402 std::string extra_search_path_;
403 Lock* lock_;
404 Initialize_lock initialize_lock_;
405 };
406
407
408 // An object file claimed by a plugin. This is an abstract base class.
409 // The implementation is the template class Sized_pluginobj.
410
411 class Pluginobj : public Object
412 {
413 public:
414
415 typedef std::vector<Symbol*> Symbols;
416
417 Pluginobj(const std::string& name, Input_file* input_file, off_t offset,
418 off_t filesize);
419
420 // Fill in the symbol resolution status for the given plugin symbols.
421 ld_plugin_status
422 get_symbol_resolution_info(Symbol_table* symtab,
423 int nsyms,
424 ld_plugin_symbol* syms,
425 int version) const;
426
427 // Store the incoming symbols from the plugin for later processing.
428 void
429 store_incoming_symbols(int nsyms, const struct ld_plugin_symbol* syms)
430 {
431 this->nsyms_ = nsyms;
432 this->syms_ = syms;
433 }
434
435 // Return TRUE if the comdat group with key COMDAT_KEY from this object
436 // should be kept.
437 bool
438 include_comdat_group(std::string comdat_key, Layout* layout);
439
440 // Return the filename.
441 const std::string&
442 filename() const
443 { return this->input_file()->filename(); }
444
445 // Return the file descriptor.
446 int
447 descriptor()
448 { return this->input_file()->file().descriptor(); }
449
450 // Return the size of the file or archive member.
451 off_t
452 filesize()
453 { return this->filesize_; }
454
455 // Return the word size of the object file.
456 int
457 elfsize() const
458 { gold_unreachable(); }
459
460 // Return TRUE if this is a big-endian object file.
461 bool
462 is_big_endian() const
463 { gold_unreachable(); }
464
465 protected:
466 // Return TRUE if this is an object claimed by a plugin.
467 virtual Pluginobj*
468 do_pluginobj()
469 { return this; }
470
471 // The number of symbols provided by the plugin.
472 int nsyms_;
473
474 // The symbols provided by the plugin.
475 const struct ld_plugin_symbol* syms_;
476
477 // The entries in the symbol table for the external symbols.
478 Symbols symbols_;
479
480 private:
481 // Size of the file (or archive member).
482 off_t filesize_;
483 // Map a comdat key symbol to a boolean indicating whether the comdat
484 // group in this object with that key should be kept.
485 typedef Unordered_map<std::string, bool> Comdat_map;
486 Comdat_map comdat_map_;
487 };
488
489 // A plugin object, size-specific version.
490
491 template<int size, bool big_endian>
492 class Sized_pluginobj : public Pluginobj
493 {
494 public:
495 Sized_pluginobj(const std::string& name, Input_file* input_file,
496 off_t offset, off_t filesize);
497
498 // Read the symbols.
499 void
500 do_read_symbols(Read_symbols_data*);
501
502 // Lay out the input sections.
503 void
504 do_layout(Symbol_table*, Layout*, Read_symbols_data*);
505
506 // Add the symbols to the symbol table.
507 void
508 do_add_symbols(Symbol_table*, Read_symbols_data*, Layout*);
509
510 Archive::Should_include
511 do_should_include_member(Symbol_table* symtab, Layout*, Read_symbols_data*,
512 std::string* why);
513
514 // Iterate over global symbols, calling a visitor class V for each.
515 void
516 do_for_all_global_symbols(Read_symbols_data* sd,
517 Library_base::Symbol_visitor_base* v);
518
519 // Iterate over local symbols, calling a visitor class V for each GOT offset
520 // associated with a local symbol.
521 void
522 do_for_all_local_got_entries(Got_offset_list::Visitor* v) const;
523
524 // Get the size of a section.
525 uint64_t
526 do_section_size(unsigned int shndx);
527
528 // Get the name of a section.
529 std::string
530 do_section_name(unsigned int shndx) const;
531
532 // Return a view of the contents of a section.
533 const unsigned char*
534 do_section_contents(unsigned int shndx, section_size_type* plen,
535 bool cache);
536
537 // Return section flags.
538 uint64_t
539 do_section_flags(unsigned int shndx);
540
541 // Return section entsize.
542 uint64_t
543 do_section_entsize(unsigned int shndx);
544
545 // Return section address.
546 uint64_t
547 do_section_address(unsigned int shndx);
548
549 // Return section type.
550 unsigned int
551 do_section_type(unsigned int shndx);
552
553 // Return the section link field.
554 unsigned int
555 do_section_link(unsigned int shndx);
556
557 // Return the section link field.
558 unsigned int
559 do_section_info(unsigned int shndx);
560
561 // Return the section alignment.
562 uint64_t
563 do_section_addralign(unsigned int shndx);
564
565 // Return the Xindex structure to use.
566 Xindex*
567 do_initialize_xindex();
568
569 // Get symbol counts.
570 void
571 do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
572
573 // Get global symbols.
574 const Symbols*
575 do_get_global_symbols() const;
576
577 // Add placeholder symbols from a claimed file.
578 ld_plugin_status
579 add_symbols_from_plugin(int nsyms, const ld_plugin_symbol* syms);
580
581 protected:
582
583 private:
584 };
585
586 // This Task handles handles the "all symbols read" event hook.
587 // The plugin may add additional input files at this time, which must
588 // be queued for reading.
589
590 class Plugin_hook : public Task
591 {
592 public:
593 Plugin_hook(const General_options& options, Input_objects* input_objects,
594 Symbol_table* symtab, Layout* /*layout*/, Dirsearch* dirpath,
595 Mapfile* mapfile, Task_token* this_blocker,
596 Task_token* next_blocker)
597 : options_(options), input_objects_(input_objects), symtab_(symtab),
598 dirpath_(dirpath), mapfile_(mapfile),
599 this_blocker_(this_blocker), next_blocker_(next_blocker)
600 { }
601
602 ~Plugin_hook();
603
604 // The standard Task methods.
605
606 Task_token*
607 is_runnable();
608
609 void
610 locks(Task_locker*);
611
612 void
613 run(Workqueue*);
614
615 std::string
616 get_name() const
617 { return "Plugin_hook"; }
618
619 private:
620 const General_options& options_;
621 Input_objects* input_objects_;
622 Symbol_table* symtab_;
623 Dirsearch* dirpath_;
624 Mapfile* mapfile_;
625 Task_token* this_blocker_;
626 Task_token* next_blocker_;
627 };
628
629 } // End namespace gold.
630
631 #endif // !defined(GOLD_PLUGIN_H)
This page took 0.052585 seconds and 5 git commands to generate.