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