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