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