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