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