* gdbint.texinfo, stabs.texinfo: Move the @setchapternewpage
[binutils-gdb.git] / gold / plugin.cc
1 // plugin.cc -- 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 #include "gold.h"
24
25 #include <cstdio>
26 #include <cstdarg>
27 #include <cstring>
28 #include <string>
29 #include <vector>
30
31 #ifdef ENABLE_PLUGINS
32 #include <dlfcn.h>
33 #endif
34
35 #include "parameters.h"
36 #include "errors.h"
37 #include "fileread.h"
38 #include "layout.h"
39 #include "options.h"
40 #include "plugin.h"
41 #include "target.h"
42 #include "readsyms.h"
43 #include "symtab.h"
44 #include "elfcpp.h"
45
46 namespace gold
47 {
48
49 #ifdef ENABLE_PLUGINS
50
51 // The linker's exported interfaces.
52
53 extern "C"
54 {
55
56 static enum ld_plugin_status
57 register_claim_file(ld_plugin_claim_file_handler handler);
58
59 static enum ld_plugin_status
60 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
61
62 static enum ld_plugin_status
63 register_cleanup(ld_plugin_cleanup_handler handler);
64
65 static enum ld_plugin_status
66 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
67
68 static enum ld_plugin_status
69 get_input_file(const void *handle, struct ld_plugin_input_file *file);
70
71 static enum ld_plugin_status
72 release_input_file(const void *handle);
73
74 static enum ld_plugin_status
75 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
76
77 static enum ld_plugin_status
78 add_input_file(char *pathname);
79
80 static enum ld_plugin_status
81 add_input_library(char *pathname);
82
83 static enum ld_plugin_status
84 message(int level, const char *format, ...);
85
86 };
87
88 #endif // ENABLE_PLUGINS
89
90 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
91 off_t offset, off_t filesize);
92
93 // Plugin methods.
94
95 // Load one plugin library.
96
97 void
98 Plugin::load()
99 {
100 #ifdef ENABLE_PLUGINS
101 // Load the plugin library.
102 // FIXME: Look for the library in standard locations.
103 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
104 if (this->handle_ == NULL)
105 {
106 gold_error(_("%s: could not load plugin library"),
107 this->filename_.c_str());
108 return;
109 }
110
111 // Find the plugin's onload entry point.
112 ld_plugin_onload onload = reinterpret_cast<ld_plugin_onload>
113 (dlsym(this->handle_, "onload"));
114 if (onload == NULL)
115 {
116 gold_error(_("%s: could not find onload entry point"),
117 this->filename_.c_str());
118 return;
119 }
120
121 // Get the linker's version number.
122 const char* ver = get_version_string();
123 int major = 0;
124 int minor = 0;
125 sscanf(ver, "%d.%d", &major, &minor);
126
127 // Allocate and populate a transfer vector.
128 const int tv_fixed_size = 14;
129 int tv_size = this->args_.size() + tv_fixed_size;
130 ld_plugin_tv *tv = new ld_plugin_tv[tv_size];
131
132 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
133 // while processing subsequent entries.
134 int i = 0;
135 tv[i].tv_tag = LDPT_MESSAGE;
136 tv[i].tv_u.tv_message = message;
137
138 ++i;
139 tv[i].tv_tag = LDPT_API_VERSION;
140 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
141
142 ++i;
143 tv[i].tv_tag = LDPT_GOLD_VERSION;
144 tv[i].tv_u.tv_val = major * 100 + minor;
145
146 ++i;
147 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
148 if (parameters->options().relocatable())
149 tv[i].tv_u.tv_val = LDPO_REL;
150 else if (parameters->options().shared())
151 tv[i].tv_u.tv_val = LDPO_DYN;
152 else
153 tv[i].tv_u.tv_val = LDPO_EXEC;
154
155 for (unsigned int j = 0; j < this->args_.size(); ++j)
156 {
157 ++i;
158 tv[i].tv_tag = LDPT_OPTION;
159 tv[i].tv_u.tv_string = this->args_[j].c_str();
160 }
161
162 ++i;
163 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
164 tv[i].tv_u.tv_register_claim_file = register_claim_file;
165
166 ++i;
167 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
168 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
169
170 ++i;
171 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
172 tv[i].tv_u.tv_register_cleanup = register_cleanup;
173
174 ++i;
175 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
176 tv[i].tv_u.tv_add_symbols = add_symbols;
177
178 ++i;
179 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
180 tv[i].tv_u.tv_get_input_file = get_input_file;
181
182 ++i;
183 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
184 tv[i].tv_u.tv_release_input_file = release_input_file;
185
186 ++i;
187 tv[i].tv_tag = LDPT_GET_SYMBOLS;
188 tv[i].tv_u.tv_get_symbols = get_symbols;
189
190 ++i;
191 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
192 tv[i].tv_u.tv_add_input_file = add_input_file;
193
194 ++i;
195 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
196 tv[i].tv_u.tv_add_input_library = add_input_library;
197
198 ++i;
199 tv[i].tv_tag = LDPT_NULL;
200 tv[i].tv_u.tv_val = 0;
201
202 gold_assert(i == tv_size - 1);
203
204 // Call the onload entry point.
205 (*onload)(tv);
206
207 delete[] tv;
208 #endif // ENABLE_PLUGINS
209 }
210
211 // Call the plugin claim-file handler.
212
213 inline bool
214 Plugin::claim_file(struct ld_plugin_input_file *plugin_input_file)
215 {
216 int claimed = 0;
217
218 if (this->claim_file_handler_ != NULL)
219 {
220 (*this->claim_file_handler_)(plugin_input_file, &claimed);
221 if (claimed)
222 return true;
223 }
224 return false;
225 }
226
227 // Call the all-symbols-read handler.
228
229 inline void
230 Plugin::all_symbols_read()
231 {
232 if (this->all_symbols_read_handler_ != NULL)
233 (*this->all_symbols_read_handler_)();
234 }
235
236 // Call the cleanup handler.
237
238 inline void
239 Plugin::cleanup()
240 {
241 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
242 {
243 // Set this flag before calling to prevent a recursive plunge
244 // in the event that a plugin's cleanup handler issues a
245 // fatal error.
246 this->cleanup_done_ = true;
247 (*this->cleanup_handler_)();
248 }
249 }
250
251 // Plugin_manager methods.
252
253 Plugin_manager::~Plugin_manager()
254 {
255 for (Plugin_list::iterator p = this->plugins_.begin();
256 p != this->plugins_.end();
257 ++p)
258 delete *p;
259 this->plugins_.clear();
260 for (Object_list::iterator obj = this->objects_.begin();
261 obj != this->objects_.end();
262 ++obj)
263 delete *obj;
264 this->objects_.clear();
265 }
266
267 // Load all plugin libraries.
268
269 void
270 Plugin_manager::load_plugins()
271 {
272 for (this->current_ = this->plugins_.begin();
273 this->current_ != this->plugins_.end();
274 ++this->current_)
275 (*this->current_)->load();
276 }
277
278 // Call the plugin claim-file handlers in turn to see if any claim the file.
279
280 Pluginobj*
281 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
282 off_t filesize)
283 {
284 if (this->in_replacement_phase_)
285 return NULL;
286
287 unsigned int handle = this->objects_.size();
288 this->input_file_ = input_file;
289 this->plugin_input_file_.name = input_file->filename().c_str();
290 this->plugin_input_file_.fd = input_file->file().descriptor();
291 this->plugin_input_file_.offset = offset;
292 this->plugin_input_file_.filesize = filesize;
293 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
294
295 for (this->current_ = this->plugins_.begin();
296 this->current_ != this->plugins_.end();
297 ++this->current_)
298 {
299 if ((*this->current_)->claim_file(&this->plugin_input_file_))
300 {
301 if (this->objects_.size() > handle)
302 return this->objects_[handle];
303
304 // If the plugin claimed the file but did not call the
305 // add_symbols callback, we need to create the Pluginobj now.
306 Pluginobj* obj = this->make_plugin_object(handle);
307 return obj;
308 }
309 }
310
311 return NULL;
312 }
313
314 // Call the all-symbols-read handlers.
315
316 void
317 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
318 Input_objects* input_objects,
319 Symbol_table* symtab, Layout* layout,
320 Dirsearch* dirpath, Mapfile* mapfile,
321 Task_token** last_blocker)
322 {
323 this->in_replacement_phase_ = true;
324 this->workqueue_ = workqueue;
325 this->task_ = task;
326 this->input_objects_ = input_objects;
327 this->symtab_ = symtab;
328 this->layout_ = layout;
329 this->dirpath_ = dirpath;
330 this->mapfile_ = mapfile;
331 this->this_blocker_ = NULL;
332
333 for (this->current_ = this->plugins_.begin();
334 this->current_ != this->plugins_.end();
335 ++this->current_)
336 (*this->current_)->all_symbols_read();
337
338 *last_blocker = this->this_blocker_;
339 }
340
341 // Layout deferred objects.
342
343 void
344 Plugin_manager::layout_deferred_objects()
345 {
346 Deferred_layout_list::iterator obj;
347
348 for (obj = this->deferred_layout_objects_.begin();
349 obj != this->deferred_layout_objects_.end();
350 ++obj)
351 (*obj)->layout_deferred_sections(this->layout_);
352 }
353
354 // Call the cleanup handlers.
355
356 void
357 Plugin_manager::cleanup()
358 {
359 for (this->current_ = this->plugins_.begin();
360 this->current_ != this->plugins_.end();
361 ++this->current_)
362 (*this->current_)->cleanup();
363 }
364
365 // Make a new Pluginobj object. This is called when the plugin calls
366 // the add_symbols API.
367
368 Pluginobj*
369 Plugin_manager::make_plugin_object(unsigned int handle)
370 {
371 // Make sure we aren't asked to make an object for the same handle twice.
372 if (this->objects_.size() != handle)
373 return NULL;
374
375 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
376 this->plugin_input_file_.offset,
377 this->plugin_input_file_.filesize);
378 this->objects_.push_back(obj);
379 return obj;
380 }
381
382 // Get the input file information with an open (possibly re-opened)
383 // file descriptor.
384
385 ld_plugin_status
386 Plugin_manager::get_input_file(unsigned int handle,
387 struct ld_plugin_input_file *file)
388 {
389 Pluginobj* obj = this->object(handle);
390 if (obj == NULL)
391 return LDPS_BAD_HANDLE;
392
393 obj->lock(this->task_);
394 file->name = obj->filename().c_str();
395 file->fd = obj->descriptor();
396 file->offset = obj->offset();
397 file->filesize = obj->filesize();
398 file->handle = reinterpret_cast<void*>(handle);
399 return LDPS_OK;
400 }
401
402 // Release the input file.
403
404 ld_plugin_status
405 Plugin_manager::release_input_file(unsigned int handle)
406 {
407 Pluginobj* obj = this->object(handle);
408 if (obj == NULL)
409 return LDPS_BAD_HANDLE;
410
411 obj->unlock(this->task_);
412 return LDPS_OK;
413 }
414
415 // Add a new input file.
416
417 ld_plugin_status
418 Plugin_manager::add_input_file(char *pathname, bool is_lib)
419 {
420 Input_file_argument file(pathname,
421 (is_lib
422 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
423 : Input_file_argument::INPUT_FILE_TYPE_FILE),
424 "", false, this->options_);
425 Input_argument* input_argument = new Input_argument(file);
426 Task_token* next_blocker = new Task_token(true);
427 next_blocker->add_blocker();
428 if (this->layout_->incremental_inputs())
429 gold_error(_("input files added by plug-ins in --incremental mode not "
430 "supported yet"));
431 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
432 this->symtab_,
433 this->layout_,
434 this->dirpath_,
435 0,
436 this->mapfile_,
437 input_argument,
438 NULL,
439 this->this_blocker_,
440 next_blocker));
441 this->this_blocker_ = next_blocker;
442 return LDPS_OK;
443 }
444
445 // Class Pluginobj.
446
447 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
448 off_t offset, off_t filesize)
449 : Object(name, input_file, false, offset),
450 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
451 {
452 }
453
454 // Return TRUE if a defined symbol might be reachable from outside the
455 // universe of claimed objects.
456
457 static inline bool
458 is_visible_from_outside(Symbol* lsym)
459 {
460 if (lsym->in_real_elf())
461 return true;
462 if (parameters->options().relocatable())
463 return true;
464 if (parameters->options().export_dynamic() || parameters->options().shared())
465 return lsym->is_externally_visible();
466 return false;
467 }
468
469 // Get symbol resolution info.
470
471 ld_plugin_status
472 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
473 {
474 if (nsyms > this->nsyms_)
475 return LDPS_NO_SYMS;
476 for (int i = 0; i < nsyms; i++)
477 {
478 ld_plugin_symbol* isym = &syms[i];
479 Symbol* lsym = this->symbols_[i];
480 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
481
482 if (lsym->is_undefined())
483 // The symbol remains undefined.
484 res = LDPR_UNDEF;
485 else if (isym->def == LDPK_UNDEF
486 || isym->def == LDPK_WEAKUNDEF
487 || isym->def == LDPK_COMMON)
488 {
489 // The original symbol was undefined or common.
490 if (lsym->source() != Symbol::FROM_OBJECT)
491 res = LDPR_RESOLVED_EXEC;
492 else if (lsym->object()->pluginobj() != NULL)
493 res = LDPR_RESOLVED_IR;
494 else if (lsym->object()->is_dynamic())
495 res = LDPR_RESOLVED_DYN;
496 else
497 res = LDPR_RESOLVED_EXEC;
498 }
499 else
500 {
501 // The original symbol was a definition.
502 if (lsym->source() != Symbol::FROM_OBJECT)
503 res = LDPR_PREEMPTED_REG;
504 else if (lsym->object() == static_cast<const Object*>(this))
505 res = (is_visible_from_outside(lsym)
506 ? LDPR_PREVAILING_DEF
507 : LDPR_PREVAILING_DEF_IRONLY);
508 else
509 res = (lsym->object()->pluginobj() != NULL
510 ? LDPR_PREEMPTED_IR
511 : LDPR_PREEMPTED_REG);
512 }
513 isym->resolution = res;
514 }
515 return LDPS_OK;
516 }
517
518 // Return TRUE if the comdat group with key COMDAT_KEY from this object
519 // should be kept.
520
521 bool
522 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
523 {
524 std::pair<Comdat_map::iterator, bool> ins =
525 this->comdat_map_.insert(std::make_pair(comdat_key, false));
526
527 // If this is the first time we've seen this comdat key, ask the
528 // layout object whether it should be included.
529 if (ins.second)
530 ins.first->second = layout->find_or_add_kept_section(comdat_key,
531 NULL, 0, true,
532 true, NULL);
533
534 return ins.first->second;
535 }
536
537 // Class Sized_pluginobj.
538
539 template<int size, bool big_endian>
540 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
541 const std::string& name,
542 Input_file* input_file,
543 off_t offset,
544 off_t filesize)
545 : Pluginobj(name, input_file, offset, filesize)
546 {
547 }
548
549 // Read the symbols. Not used for plugin objects.
550
551 template<int size, bool big_endian>
552 void
553 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
554 {
555 gold_unreachable();
556 }
557
558 // Lay out the input sections. Not used for plugin objects.
559
560 template<int size, bool big_endian>
561 void
562 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
563 Read_symbols_data*)
564 {
565 gold_unreachable();
566 }
567
568 // Add the symbols to the symbol table.
569
570 template<int size, bool big_endian>
571 void
572 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
573 Read_symbols_data*,
574 Layout* layout)
575 {
576 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
577 unsigned char symbuf[sym_size];
578 elfcpp::Sym<size, big_endian> sym(symbuf);
579 elfcpp::Sym_write<size, big_endian> osym(symbuf);
580
581 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
582
583 this->symbols_.resize(this->nsyms_);
584
585 for (int i = 0; i < this->nsyms_; ++i)
586 {
587 const struct ld_plugin_symbol *isym = &this->syms_[i];
588 const char* name = isym->name;
589 const char* ver = isym->version;
590 elfcpp::Elf_Half shndx;
591 elfcpp::STB bind;
592 elfcpp::STV vis;
593
594 if (name != NULL && name[0] == '\0')
595 name = NULL;
596 if (ver != NULL && ver[0] == '\0')
597 ver = NULL;
598
599 switch (isym->def)
600 {
601 case LDPK_WEAKDEF:
602 case LDPK_WEAKUNDEF:
603 bind = elfcpp::STB_WEAK;
604 break;
605 case LDPK_DEF:
606 case LDPK_UNDEF:
607 case LDPK_COMMON:
608 default:
609 bind = elfcpp::STB_GLOBAL;
610 break;
611 }
612
613 switch (isym->def)
614 {
615 case LDPK_DEF:
616 case LDPK_WEAKDEF:
617 shndx = elfcpp::SHN_ABS;
618 break;
619 case LDPK_COMMON:
620 shndx = elfcpp::SHN_COMMON;
621 break;
622 case LDPK_UNDEF:
623 case LDPK_WEAKUNDEF:
624 default:
625 shndx = elfcpp::SHN_UNDEF;
626 break;
627 }
628
629 switch (isym->visibility)
630 {
631 case LDPV_PROTECTED:
632 vis = elfcpp::STV_DEFAULT;
633 break;
634 case LDPV_INTERNAL:
635 vis = elfcpp::STV_DEFAULT;
636 break;
637 case LDPV_HIDDEN:
638 vis = elfcpp::STV_DEFAULT;
639 break;
640 case LDPV_DEFAULT:
641 default:
642 vis = elfcpp::STV_DEFAULT;
643 break;
644 }
645
646 if (isym->comdat_key != NULL
647 && isym->comdat_key[0] != '\0'
648 && !this->include_comdat_group(isym->comdat_key, layout))
649 shndx = elfcpp::SHN_UNDEF;
650
651 osym.put_st_name(0);
652 osym.put_st_value(0);
653 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
654 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
655 osym.put_st_other(vis, 0);
656 osym.put_st_shndx(shndx);
657
658 this->symbols_[i] =
659 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
660 }
661 }
662
663 // Get the size of a section. Not used for plugin objects.
664
665 template<int size, bool big_endian>
666 uint64_t
667 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
668 {
669 gold_unreachable();
670 return 0;
671 }
672
673 // Get the name of a section. Not used for plugin objects.
674
675 template<int size, bool big_endian>
676 std::string
677 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
678 {
679 gold_unreachable();
680 return std::string();
681 }
682
683 // Return a view of the contents of a section. Not used for plugin objects.
684
685 template<int size, bool big_endian>
686 Object::Location
687 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
688 {
689 Location loc(0, 0);
690
691 gold_unreachable();
692 return loc;
693 }
694
695 // Return section flags. Not used for plugin objects.
696
697 template<int size, bool big_endian>
698 uint64_t
699 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
700 {
701 gold_unreachable();
702 return 0;
703 }
704
705 // Return section entsize. Not used for plugin objects.
706
707 template<int size, bool big_endian>
708 uint64_t
709 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
710 {
711 gold_unreachable();
712 return 0;
713 }
714
715 // Return section address. Not used for plugin objects.
716
717 template<int size, bool big_endian>
718 uint64_t
719 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
720 {
721 gold_unreachable();
722 return 0;
723 }
724
725 // Return section type. Not used for plugin objects.
726
727 template<int size, bool big_endian>
728 unsigned int
729 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
730 {
731 gold_unreachable();
732 return 0;
733 }
734
735 // Return the section link field. Not used for plugin objects.
736
737 template<int size, bool big_endian>
738 unsigned int
739 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
740 {
741 gold_unreachable();
742 return 0;
743 }
744
745 // Return the section link field. Not used for plugin objects.
746
747 template<int size, bool big_endian>
748 unsigned int
749 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
750 {
751 gold_unreachable();
752 return 0;
753 }
754
755 // Return the section alignment. Not used for plugin objects.
756
757 template<int size, bool big_endian>
758 uint64_t
759 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
760 {
761 gold_unreachable();
762 return 0;
763 }
764
765 // Return the Xindex structure to use. Not used for plugin objects.
766
767 template<int size, bool big_endian>
768 Xindex*
769 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
770 {
771 gold_unreachable();
772 return NULL;
773 }
774
775 // Get symbol counts. Not used for plugin objects.
776
777 template<int size, bool big_endian>
778 void
779 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
780 size_t*, size_t*) const
781 {
782 gold_unreachable();
783 }
784
785 // Class Plugin_finish. This task runs after all replacement files have
786 // been added. It calls each plugin's cleanup handler.
787
788 class Plugin_finish : public Task
789 {
790 public:
791 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
792 : this_blocker_(this_blocker), next_blocker_(next_blocker)
793 { }
794
795 ~Plugin_finish()
796 {
797 if (this->this_blocker_ != NULL)
798 delete this->this_blocker_;
799 }
800
801 Task_token*
802 is_runnable()
803 {
804 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
805 return this->this_blocker_;
806 return NULL;
807 }
808
809 void
810 locks(Task_locker* tl)
811 { tl->add(this, this->next_blocker_); }
812
813 void
814 run(Workqueue*)
815 {
816 Plugin_manager* plugins = parameters->options().plugins();
817 gold_assert(plugins != NULL);
818 plugins->cleanup();
819 }
820
821 std::string
822 get_name() const
823 { return "Plugin_finish"; }
824
825 private:
826 Task_token* this_blocker_;
827 Task_token* next_blocker_;
828 };
829
830 // Class Plugin_hook.
831
832 Plugin_hook::~Plugin_hook()
833 {
834 }
835
836 // Return whether a Plugin_hook task is runnable.
837
838 Task_token*
839 Plugin_hook::is_runnable()
840 {
841 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
842 return this->this_blocker_;
843 return NULL;
844 }
845
846 // Return a Task_locker for a Plugin_hook task. We don't need any
847 // locks here.
848
849 void
850 Plugin_hook::locks(Task_locker*)
851 {
852 }
853
854 // Run the "all symbols read" plugin hook.
855
856 void
857 Plugin_hook::run(Workqueue* workqueue)
858 {
859 gold_assert(this->options_.has_plugins());
860 this->options_.plugins()->all_symbols_read(workqueue,
861 this,
862 this->input_objects_,
863 this->symtab_,
864 this->layout_,
865 this->dirpath_,
866 this->mapfile_,
867 &this->this_blocker_);
868 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
869 this->next_blocker_));
870 }
871
872 // The C interface routines called by the plugins.
873
874 #ifdef ENABLE_PLUGINS
875
876 // Register a claim-file handler.
877
878 static enum ld_plugin_status
879 register_claim_file(ld_plugin_claim_file_handler handler)
880 {
881 gold_assert(parameters->options().has_plugins());
882 parameters->options().plugins()->set_claim_file_handler(handler);
883 return LDPS_OK;
884 }
885
886 // Register an all-symbols-read handler.
887
888 static enum ld_plugin_status
889 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
890 {
891 gold_assert(parameters->options().has_plugins());
892 parameters->options().plugins()->set_all_symbols_read_handler(handler);
893 return LDPS_OK;
894 }
895
896 // Register a cleanup handler.
897
898 static enum ld_plugin_status
899 register_cleanup(ld_plugin_cleanup_handler handler)
900 {
901 gold_assert(parameters->options().has_plugins());
902 parameters->options().plugins()->set_cleanup_handler(handler);
903 return LDPS_OK;
904 }
905
906 // Add symbols from a plugin-claimed input file.
907
908 static enum ld_plugin_status
909 add_symbols(void* handle, int nsyms, const ld_plugin_symbol *syms)
910 {
911 gold_assert(parameters->options().has_plugins());
912 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
913 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
914 if (obj == NULL)
915 return LDPS_ERR;
916 obj->store_incoming_symbols(nsyms, syms);
917 return LDPS_OK;
918 }
919
920 // Get the input file information with an open (possibly re-opened)
921 // file descriptor.
922
923 static enum ld_plugin_status
924 get_input_file(const void *handle, struct ld_plugin_input_file *file)
925 {
926 gold_assert(parameters->options().has_plugins());
927 unsigned int obj_index =
928 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
929 return parameters->options().plugins()->get_input_file(obj_index, file);
930 }
931
932 // Release the input file.
933
934 static enum ld_plugin_status
935 release_input_file(const void *handle)
936 {
937 gold_assert(parameters->options().has_plugins());
938 unsigned int obj_index =
939 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
940 return parameters->options().plugins()->release_input_file(obj_index);
941 }
942
943 // Get the symbol resolution info for a plugin-claimed input file.
944
945 static enum ld_plugin_status
946 get_symbols(const void * handle, int nsyms, ld_plugin_symbol* syms)
947 {
948 gold_assert(parameters->options().has_plugins());
949 Pluginobj* obj = parameters->options().plugins()->object(
950 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
951 if (obj == NULL)
952 return LDPS_ERR;
953 return obj->get_symbol_resolution_info(nsyms, syms);
954 }
955
956 // Add a new (real) input file generated by a plugin.
957
958 static enum ld_plugin_status
959 add_input_file(char *pathname)
960 {
961 gold_assert(parameters->options().has_plugins());
962 return parameters->options().plugins()->add_input_file(pathname, false);
963 }
964
965 // Add a new (real) library required by a plugin.
966
967 static enum ld_plugin_status
968 add_input_library(char *pathname)
969 {
970 gold_assert(parameters->options().has_plugins());
971 return parameters->options().plugins()->add_input_file(pathname, true);
972 }
973
974 // Issue a diagnostic message from a plugin.
975
976 static enum ld_plugin_status
977 message(int level, const char * format, ...)
978 {
979 va_list args;
980 va_start(args, format);
981
982 switch (level)
983 {
984 case LDPL_INFO:
985 parameters->errors()->info(format, args);
986 break;
987 case LDPL_WARNING:
988 parameters->errors()->warning(format, args);
989 break;
990 case LDPL_ERROR:
991 default:
992 parameters->errors()->error(format, args);
993 break;
994 case LDPL_FATAL:
995 parameters->errors()->fatal(format, args);
996 break;
997 }
998
999 va_end(args);
1000 return LDPS_OK;
1001 }
1002
1003 #endif // ENABLE_PLUGINS
1004
1005 // Allocate a Pluginobj object of the appropriate size and endianness.
1006
1007 static Pluginobj*
1008 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1009 {
1010 Pluginobj* obj = NULL;
1011
1012 parameters_force_valid_target();
1013 const Target& target(parameters->target());
1014
1015 if (target.get_size() == 32)
1016 {
1017 if (target.is_big_endian())
1018 #ifdef HAVE_TARGET_32_BIG
1019 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1020 input_file, offset, filesize);
1021 #else
1022 gold_error(_("%s: not configured to support "
1023 "32-bit big-endian object"),
1024 input_file->filename().c_str());
1025 #endif
1026 else
1027 #ifdef HAVE_TARGET_32_LITTLE
1028 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1029 input_file, offset, filesize);
1030 #else
1031 gold_error(_("%s: not configured to support "
1032 "32-bit little-endian object"),
1033 input_file->filename().c_str());
1034 #endif
1035 }
1036 else if (target.get_size() == 64)
1037 {
1038 if (target.is_big_endian())
1039 #ifdef HAVE_TARGET_64_BIG
1040 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1041 input_file, offset, filesize);
1042 #else
1043 gold_error(_("%s: not configured to support "
1044 "64-bit big-endian object"),
1045 input_file->filename().c_str());
1046 #endif
1047 else
1048 #ifdef HAVE_TARGET_64_LITTLE
1049 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1050 input_file, offset, filesize);
1051 #else
1052 gold_error(_("%s: not configured to support "
1053 "64-bit little-endian object"),
1054 input_file->filename().c_str());
1055 #endif
1056 }
1057
1058 gold_assert(obj != NULL);
1059 return obj;
1060 }
1061
1062 } // End namespace gold.