* archive.cc (Archive::get_elf_object_for_member): Remove call
[external/binutils.git] / gold / plugin.h
1 // plugin.h -- plugin manager for gold      -*- C++ -*-
2
3 // Copyright 2008, 2009 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 Symbol_table;
40 class Layout;
41 class Dirsearch;
42 class Mapfile;
43 class Task_token;
44 class Pluginobj;
45
46 // This class represents a single plugin library.
47
48 class Plugin
49 {
50  public:
51   Plugin(const char* filename)
52     : handle_(NULL),
53       filename_(filename),
54       args_(),
55       claim_file_handler_(NULL),
56       all_symbols_read_handler_(NULL),
57       cleanup_handler_(NULL)      
58   { }
59
60   ~Plugin()
61   { }
62
63   // Load the library and call its entry point.
64   void
65   load();
66
67   // Call the claim-file handler.
68   bool
69   claim_file(struct ld_plugin_input_file *plugin_input_file);
70
71   // Call the all-symbols-read handler.
72   void
73   all_symbols_read();
74
75   // Call the cleanup handler.
76   void
77   cleanup();
78
79   // Register a claim-file handler.
80   void
81   set_claim_file_handler(ld_plugin_claim_file_handler handler)
82   { this->claim_file_handler_ = handler; }
83
84   // Register an all-symbols-read handler.
85   void
86   set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler)
87   { this->all_symbols_read_handler_ = handler; }
88
89   // Register a claim-file handler.
90   void
91   set_cleanup_handler(ld_plugin_cleanup_handler handler)
92   { this->cleanup_handler_ = handler; }
93
94   // Add an argument
95   void
96   add_option(const char *arg)
97   {
98     this->args_.push_back(arg);
99   }
100
101  private:
102   Plugin(const Plugin&);
103   Plugin& operator=(const Plugin&);
104
105   // The shared library handle returned by dlopen.
106   void* handle_;
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_;
115 };
116
117 // A manager class for plugins.
118
119 class Plugin_manager
120 {
121  public:
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), task_(NULL), input_objects_(NULL),
126       symtab_(NULL), layout_(NULL), dirpath_(NULL), mapfile_(NULL),
127       this_blocker_(NULL)
128   { this->current_ = plugins_.end(); }
129
130   ~Plugin_manager();
131
132   // Add a plugin library.
133   void
134   add_plugin(const char* filename)
135   { this->plugins_.push_back(new Plugin(filename)); }
136
137   // Add an argument to the current plugin.
138   void
139   add_plugin_option(const char* opt)
140   {
141     Plugin* last = this->plugins_.back();
142     last->add_option(opt);
143   }
144
145   // Load all plugin libraries.
146   void
147   load_plugins();
148
149   // Call the plugin claim-file handlers in turn to see if any claim the file.
150   Pluginobj*
151   claim_file(Input_file *input_file, off_t offset, off_t filesize);
152
153   // Call the all-symbols-read handlers.
154   void
155   all_symbols_read(Workqueue* workqueue, Task* task,
156                    Input_objects* input_objects, Symbol_table* symtab,
157                    Layout* layout, Dirsearch* dirpath, Mapfile* mapfile,
158                    Task_token** last_blocker);
159
160   // Run deferred layout.
161   void
162   layout_deferred_objects();
163
164   // Call the cleanup handlers.
165   void
166   cleanup();
167
168   // Register a claim-file handler.
169   void
170   set_claim_file_handler(ld_plugin_claim_file_handler handler)
171   {
172     gold_assert(this->current_ != plugins_.end());
173     (*this->current_)->set_claim_file_handler(handler);
174   }
175
176   // Register an all-symbols-read handler.
177   void
178   set_all_symbols_read_handler(ld_plugin_all_symbols_read_handler handler)
179   {
180     gold_assert(this->current_ != plugins_.end());
181     (*this->current_)->set_all_symbols_read_handler(handler);
182   }
183
184   // Register a claim-file handler.
185   void
186   set_cleanup_handler(ld_plugin_cleanup_handler handler)
187   {
188     gold_assert(this->current_ != plugins_.end());
189     (*this->current_)->set_cleanup_handler(handler);
190   }
191
192   // Make a new Pluginobj object.  This is called when the plugin calls
193   // the add_symbols API.
194   Pluginobj*
195   make_plugin_object(unsigned int handle);
196
197   // Return the Pluginobj associated with the given HANDLE.
198   Pluginobj*
199   object(unsigned int handle) const
200   {
201     if (handle >= this->objects_.size())
202       return NULL;
203     return this->objects_[handle];
204   }
205
206   // Return TRUE if any input files have been claimed by a plugin
207   // and we are still in the initial input phase.
208   bool
209   should_defer_layout() const
210   { return !this->objects_.empty() && !this->in_replacement_phase_; }
211
212   // Add a regular object to the deferred layout list.  These are
213   // objects whose layout has been deferred until after the
214   // replacement files have arrived.
215   void
216   add_deferred_layout_object(Relobj* obj)
217   { this->deferred_layout_objects_.push_back(obj); }
218
219   // Get input file information with an open (possibly re-opened)
220   // file descriptor.
221   ld_plugin_status
222   get_input_file(unsigned int handle, struct ld_plugin_input_file *file);
223
224   // Release an input file.
225   ld_plugin_status
226   release_input_file(unsigned int handle);
227
228   // Add a new input file.
229   ld_plugin_status
230   add_input_file(char *pathname);
231
232   // Return TRUE if we are in the replacement phase.
233   bool
234   in_replacement_phase() const
235   { return this->in_replacement_phase_; }
236
237  private:
238   Plugin_manager(const Plugin_manager&);
239   Plugin_manager& operator=(const Plugin_manager&);
240
241   typedef std::list<Plugin*> Plugin_list;
242   typedef std::vector<Pluginobj*> Object_list;
243   typedef std::vector<Relobj*> Deferred_layout_list;
244
245   // The list of plugin libraries.
246   Plugin_list plugins_;
247   // A pointer to the current plugin.  Used while loading plugins.
248   Plugin_list::iterator current_;
249
250   // The list of plugin objects.  The index of an item in this list
251   // serves as the "handle" that we pass to the plugins.
252   Object_list objects_;
253
254   // The list of regular objects whose layout has been deferred.
255   Deferred_layout_list deferred_layout_objects_;
256
257   // The file currently up for claim by the plugins.
258   Input_file* input_file_;
259   struct ld_plugin_input_file plugin_input_file_;
260
261   // TRUE after the all symbols read event; indicates that we are
262   // processing replacement files whose symbols should replace the
263   // placeholder symbols from the Pluginobj objects.
264   bool in_replacement_phase_;
265
266   // TRUE if the cleanup handlers have been called.
267   bool cleanup_done_;
268
269   const General_options& options_;
270   Workqueue* workqueue_;
271   Task* task_;
272   Input_objects* input_objects_;
273   Symbol_table* symtab_;
274   Layout* layout_;
275   Dirsearch* dirpath_;
276   Mapfile* mapfile_;
277   Task_token* this_blocker_;
278 };
279
280
281 // An object file claimed by a plugin.  This is an abstract base class.
282 // The implementation is the template class Sized_pluginobj.
283
284 class Pluginobj : public Object
285 {
286  public:
287
288   typedef std::vector<Symbol*> Symbols;
289
290   Pluginobj(const std::string& name, Input_file* input_file, off_t offset,
291             off_t filesize);
292
293   // Fill in the symbol resolution status for the given plugin symbols.
294   ld_plugin_status
295   get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const;
296
297   // Add symbol information to the global symbol table.
298   void
299   add_symbols(Symbol_table* symtab, Layout* layout)
300   { this->do_add_symbols(symtab, layout); }
301
302   // Store the incoming symbols from the plugin for later processing.
303   void
304   store_incoming_symbols(int nsyms, const struct ld_plugin_symbol* syms)
305   {
306     this->nsyms_ = nsyms;
307     this->syms_ = syms;
308   }
309
310   // Return TRUE if the comdat group with key COMDAT_KEY from this object
311   // should be kept.
312   bool
313   include_comdat_group(std::string comdat_key, Layout* layout);
314
315   // Return the filename.
316   const std::string&
317   filename() const
318   { return this->input_file()->filename(); }
319
320   // Return the file descriptor.
321   int
322   descriptor()
323   { return this->input_file()->file().descriptor(); }
324
325   // Return the size of the file or archive member.
326   off_t
327   filesize()
328   { return this->filesize_; }
329
330  protected:
331   // Return TRUE if this is an object claimed by a plugin.
332   virtual Pluginobj*
333   do_pluginobj()
334   { return this; }
335
336   // Add symbol information to the global symbol table--implemented by
337   // child class.
338   virtual void
339   do_add_symbols(Symbol_table*, Layout*) = 0;
340
341   // The number of symbols provided by the plugin.
342   int nsyms_;
343   
344   // The symbols provided by the plugin.
345   const struct ld_plugin_symbol* syms_;
346
347   // The entries in the symbol table for the external symbols.
348   Symbols symbols_;
349
350  private:
351   // Size of the file (or archive member).
352   off_t filesize_;
353   // Map a comdat key symbol to a boolean indicating whether the comdat
354   // group in this object with that key should be kept.
355   typedef Unordered_map<std::string, bool> Comdat_map;
356   Comdat_map comdat_map_;
357 };
358
359 // A plugin object, size-specific version.
360
361 template<int size, bool big_endian>
362 class Sized_pluginobj : public Pluginobj
363 {
364  public:
365   Sized_pluginobj(const std::string& name, Input_file* input_file,
366                   off_t offset, off_t filesize);
367
368   // Read the symbols.
369   void
370   do_read_symbols(Read_symbols_data*);
371
372   // Lay out the input sections.
373   void
374   do_layout(Symbol_table*, Layout*, Read_symbols_data*);
375
376   // Add the symbols to the symbol table.
377   void
378   do_add_symbols(Symbol_table*, Read_symbols_data*);
379
380   void
381   do_add_symbols(Symbol_table*, Layout*);
382
383   // Get the size of a section.
384   uint64_t
385   do_section_size(unsigned int shndx);
386
387   // Get the name of a section.
388   std::string
389   do_section_name(unsigned int shndx);
390
391   // Return a view of the contents of a section.
392   Object::Location
393   do_section_contents(unsigned int shndx);
394
395   // Return section flags.
396   uint64_t
397   do_section_flags(unsigned int shndx);
398
399   // Return section address.
400   uint64_t
401   do_section_address(unsigned int shndx);
402
403   // Return section type.
404   unsigned int
405   do_section_type(unsigned int shndx);
406
407   // Return the section link field.
408   unsigned int
409   do_section_link(unsigned int shndx);
410
411   // Return the section link field.
412   unsigned int
413   do_section_info(unsigned int shndx);
414
415   // Return the section alignment.
416   uint64_t
417   do_section_addralign(unsigned int shndx);
418
419   // Return the Xindex structure to use.
420   Xindex*
421   do_initialize_xindex();
422
423   // Get symbol counts.
424   void
425   do_get_global_symbol_counts(const Symbol_table*, size_t*, size_t*) const;
426
427   // Add placeholder symbols from a claimed file.
428   ld_plugin_status
429   add_symbols_from_plugin(int nsyms, const ld_plugin_symbol* syms);
430
431  protected:
432
433  private:
434 };
435
436 // This Task handles adding the symbols to the symbol table.  These
437 // tasks must be run in the same order as the arguments appear on the
438 // command line.
439
440 class Add_plugin_symbols : public Task
441 {
442  public:
443   // THIS_BLOCKER is used to prevent this task from running before the
444   // one for the previous input file.  NEXT_BLOCKER is used to prevent
445   // the next task from running.
446   Add_plugin_symbols(Symbol_table* symtab,
447                      Layout* layout,
448                      Pluginobj* obj,
449                      Task_token* this_blocker,
450                      Task_token* next_blocker)
451     : symtab_(symtab), layout_(layout), obj_(obj),
452       this_blocker_(this_blocker), next_blocker_(next_blocker)
453   { }
454
455   ~Add_plugin_symbols();
456
457   // The standard Task methods.
458
459   Task_token*
460   is_runnable();
461
462   void
463   locks(Task_locker*);
464
465   void
466   run(Workqueue*);
467
468   std::string
469   get_name() const
470   { return "Add_plugin_symbols " + this->obj_->name(); }
471
472 private:
473   Symbol_table* symtab_;
474   Layout* layout_;
475   Pluginobj* obj_;
476   Task_token* this_blocker_;
477   Task_token* next_blocker_;
478 };
479
480 // This Task handles handles the "all symbols read" event hook.
481 // The plugin may add additional input files at this time, which must
482 // be queued for reading.
483
484 class Plugin_hook : public Task
485 {
486  public:
487   Plugin_hook(const General_options& options, Input_objects* input_objects,
488               Symbol_table* symtab, Layout* layout, Dirsearch* dirpath,
489               Mapfile* mapfile, Task_token* this_blocker,
490               Task_token* next_blocker)
491     : options_(options), input_objects_(input_objects), symtab_(symtab),
492       layout_(layout), dirpath_(dirpath), mapfile_(mapfile),
493       this_blocker_(this_blocker), next_blocker_(next_blocker)
494   { }
495
496   ~Plugin_hook();
497
498   // The standard Task methods.
499
500   Task_token*
501   is_runnable();
502
503   void
504   locks(Task_locker*);
505
506   void
507   run(Workqueue*);
508
509   std::string
510   get_name() const
511   { return "Plugin_hook"; }
512
513  private:
514   const General_options& options_;
515   Input_objects* input_objects_;
516   Symbol_table* symtab_;
517   Layout* layout_;
518   Dirsearch* dirpath_;
519   Mapfile* mapfile_;
520   Task_token* this_blocker_;
521   Task_token* next_blocker_;
522 };
523
524 } // End namespace gold.
525
526 #endif // !defined(GOLD_PLUGIN_H)