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