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