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