* elf64-ppc.c (ppc64_elf_edit_toc): Don't segfault on NULL
[deliverable/binutils-gdb.git] / gold / plugin.cc
CommitLineData
6d03d481 1// plugin.cc -- plugin manager for gold -*- C++ -*-
89fc3421 2
0f3b89d8 3// Copyright 2008, 2009, 2010, 2011 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;
ca09d69a 135 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
89fc3421 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
ca09d69a 227Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
89fc3421
CC
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
0f3b89d8
ILT
264// This task is used to rescan archives as needed.
265
266class Plugin_rescan : public Task
267{
268 public:
269 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
270 : this_blocker_(this_blocker), next_blocker_(next_blocker)
271 { }
272
273 ~Plugin_rescan()
274 {
275 delete this->this_blocker_;
276 }
277
278 Task_token*
279 is_runnable()
280 {
281 if (this->this_blocker_->is_blocked())
282 return this->this_blocker_;
283 return NULL;
284 }
285
286 void
287 locks(Task_locker* tl)
288 { tl->add(this, this->next_blocker_); }
289
290 void
291 run(Workqueue*)
292 { parameters->options().plugins()->rescan(this); }
293
294 std::string
295 get_name() const
296 { return "Plugin_rescan"; }
297
298 private:
299 Task_token* this_blocker_;
300 Task_token* next_blocker_;
301};
302
89fc3421
CC
303// Plugin_manager methods.
304
305Plugin_manager::~Plugin_manager()
306{
307 for (Plugin_list::iterator p = this->plugins_.begin();
308 p != this->plugins_.end();
309 ++p)
310 delete *p;
311 this->plugins_.clear();
312 for (Object_list::iterator obj = this->objects_.begin();
313 obj != this->objects_.end();
314 ++obj)
315 delete *obj;
316 this->objects_.clear();
317}
318
319// Load all plugin libraries.
320
321void
322Plugin_manager::load_plugins()
323{
324 for (this->current_ = this->plugins_.begin();
325 this->current_ != this->plugins_.end();
326 ++this->current_)
327 (*this->current_)->load();
328}
329
330// Call the plugin claim-file handlers in turn to see if any claim the file.
331
332Pluginobj*
333Plugin_manager::claim_file(Input_file* input_file, off_t offset,
334 off_t filesize)
335{
336 if (this->in_replacement_phase_)
337 return NULL;
338
339 unsigned int handle = this->objects_.size();
340 this->input_file_ = input_file;
341 this->plugin_input_file_.name = input_file->filename().c_str();
342 this->plugin_input_file_.fd = input_file->file().descriptor();
343 this->plugin_input_file_.offset = offset;
344 this->plugin_input_file_.filesize = filesize;
345 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
346
347 for (this->current_ = this->plugins_.begin();
348 this->current_ != this->plugins_.end();
349 ++this->current_)
350 {
351 if ((*this->current_)->claim_file(&this->plugin_input_file_))
352 {
0f3b89d8
ILT
353 this->any_claimed_ = true;
354
abc8dcba
CC
355 if (this->objects_.size() > handle)
356 return this->objects_[handle];
357
358 // If the plugin claimed the file but did not call the
359 // add_symbols callback, we need to create the Pluginobj now.
360 Pluginobj* obj = this->make_plugin_object(handle);
361 return obj;
89fc3421
CC
362 }
363 }
364
365 return NULL;
366}
367
0f3b89d8
ILT
368// Save an archive. This is used so that a plugin can add a file
369// which refers to a symbol which was not previously referenced. In
370// that case we want to pretend that the symbol was referenced before,
371// and pull in the archive object.
372
373void
374Plugin_manager::save_archive(Archive* archive)
375{
376 if (this->in_replacement_phase_ || !this->any_claimed_)
377 delete archive;
378 else
379 this->rescannable_.push_back(Rescannable(archive));
380}
381
382// Save an Input_group. This is like save_archive.
383
384void
385Plugin_manager::save_input_group(Input_group* input_group)
386{
387 if (this->in_replacement_phase_ || !this->any_claimed_)
388 delete input_group;
389 else
390 this->rescannable_.push_back(Rescannable(input_group));
391}
392
89fc3421
CC
393// Call the all-symbols-read handlers.
394
395void
0f7c0701 396Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
89fc3421 397 Input_objects* input_objects,
2ea97941 398 Symbol_table* symtab, Layout* layout,
89fc3421
CC
399 Dirsearch* dirpath, Mapfile* mapfile,
400 Task_token** last_blocker)
401{
402 this->in_replacement_phase_ = true;
403 this->workqueue_ = workqueue;
0f7c0701 404 this->task_ = task;
89fc3421
CC
405 this->input_objects_ = input_objects;
406 this->symtab_ = symtab;
2ea97941 407 this->layout_ = layout;
89fc3421
CC
408 this->dirpath_ = dirpath;
409 this->mapfile_ = mapfile;
410 this->this_blocker_ = NULL;
411
412 for (this->current_ = this->plugins_.begin();
413 this->current_ != this->plugins_.end();
414 ++this->current_)
415 (*this->current_)->all_symbols_read();
416
0f3b89d8
ILT
417 if (this->any_added_)
418 {
419 Task_token* next_blocker = new Task_token(true);
420 next_blocker->add_blocker();
421 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
422 this->this_blocker_ = next_blocker;
423 }
424
89fc3421
CC
425 *last_blocker = this->this_blocker_;
426}
427
0f3b89d8
ILT
428// This is called when we see a new undefined symbol. If we are in
429// the replacement phase, this means that we may need to rescan some
430// archives we have previously seen.
431
432void
433Plugin_manager::new_undefined_symbol(Symbol* sym)
434{
435 if (this->in_replacement_phase_)
436 this->undefined_symbols_.push_back(sym);
437}
438
439// Rescan archives as needed. This handles the case where a new
440// object file added by a plugin has an undefined reference to some
441// symbol defined in an archive.
442
443void
444Plugin_manager::rescan(Task* task)
445{
446 size_t rescan_pos = 0;
447 size_t rescan_size = this->rescannable_.size();
448 while (!this->undefined_symbols_.empty())
449 {
450 if (rescan_pos >= rescan_size)
451 {
452 this->undefined_symbols_.clear();
453 return;
454 }
455
456 Undefined_symbol_list undefs;
457 undefs.reserve(this->undefined_symbols_.size());
458 this->undefined_symbols_.swap(undefs);
459
460 size_t min_rescan_pos = rescan_size;
461
462 for (Undefined_symbol_list::const_iterator p = undefs.begin();
463 p != undefs.end();
464 ++p)
465 {
466 if (!(*p)->is_undefined())
467 continue;
468
469 this->undefined_symbols_.push_back(*p);
470
471 // Find the first rescan archive which defines this symbol,
472 // starting at the current rescan position. The rescan position
473 // exists so that given -la -lb -lc we don't look for undefined
474 // symbols in -lb back in -la, but instead get the definition
475 // from -lc. Don't bother to look past the current minimum
476 // rescan position.
477 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
478 {
479 if (this->rescannable_defines(i, *p))
480 {
481 min_rescan_pos = i;
482 break;
483 }
484 }
485 }
486
487 if (min_rescan_pos >= rescan_size)
488 {
489 // We didn't find any rescannable archives which define any
490 // undefined symbols.
491 return;
492 }
493
494 const Rescannable& r(this->rescannable_[min_rescan_pos]);
495 if (r.is_archive)
496 {
497 Task_lock_obj<Archive> tl(task, r.u.archive);
498 r.u.archive->add_symbols(this->symtab_, this->layout_,
499 this->input_objects_, this->mapfile_);
500 }
501 else
502 {
503 size_t next_saw_undefined = this->symtab_->saw_undefined();
504 size_t saw_undefined;
505 do
506 {
507 saw_undefined = next_saw_undefined;
508
509 for (Input_group::const_iterator p = r.u.input_group->begin();
510 p != r.u.input_group->end();
511 ++p)
512 {
513 Task_lock_obj<Archive> tl(task, *p);
514
515 (*p)->add_symbols(this->symtab_, this->layout_,
516 this->input_objects_, this->mapfile_);
517 }
518
519 next_saw_undefined = this->symtab_->saw_undefined();
520 }
521 while (saw_undefined != next_saw_undefined);
522 }
523
524 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
525 {
526 if (this->rescannable_[i].is_archive)
527 delete this->rescannable_[i].u.archive;
528 else
529 delete this->rescannable_[i].u.input_group;
530 }
531
532 rescan_pos = min_rescan_pos + 1;
533 }
534}
535
536// Return whether the rescannable at index I defines SYM.
537
538bool
539Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
540{
541 const Rescannable& r(this->rescannable_[i]);
542 if (r.is_archive)
543 return r.u.archive->defines_symbol(sym);
544 else
545 {
546 for (Input_group::const_iterator p = r.u.input_group->begin();
547 p != r.u.input_group->end();
548 ++p)
549 {
550 if ((*p)->defines_symbol(sym))
551 return true;
552 }
553 return false;
554 }
555}
556
483620e8 557// Layout deferred objects.
89fc3421
CC
558
559void
483620e8 560Plugin_manager::layout_deferred_objects()
89fc3421 561{
5995b570
CC
562 Deferred_layout_list::iterator obj;
563
564 for (obj = this->deferred_layout_objects_.begin();
565 obj != this->deferred_layout_objects_.end();
566 ++obj)
5f9bcf58
CC
567 {
568 // Lock the object so we can read from it. This is only called
569 // single-threaded from queue_middle_tasks, so it is OK to lock.
570 // Unfortunately we have no way to pass in a Task token.
571 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
572 Task_lock_obj<Object> tl(dummy_task, *obj);
573 (*obj)->layout_deferred_sections(this->layout_);
574 }
483620e8
CC
575}
576
577// Call the cleanup handlers.
5995b570 578
483620e8
CC
579void
580Plugin_manager::cleanup()
581{
89fc3421
CC
582 for (this->current_ = this->plugins_.begin();
583 this->current_ != this->plugins_.end();
584 ++this->current_)
585 (*this->current_)->cleanup();
586}
587
588// Make a new Pluginobj object. This is called when the plugin calls
589// the add_symbols API.
590
591Pluginobj*
592Plugin_manager::make_plugin_object(unsigned int handle)
593{
594 // Make sure we aren't asked to make an object for the same handle twice.
595 if (this->objects_.size() != handle)
596 return NULL;
597
598 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
0f7c0701
CC
599 this->plugin_input_file_.offset,
600 this->plugin_input_file_.filesize);
89fc3421
CC
601 this->objects_.push_back(obj);
602 return obj;
603}
604
0f7c0701
CC
605// Get the input file information with an open (possibly re-opened)
606// file descriptor.
607
608ld_plugin_status
609Plugin_manager::get_input_file(unsigned int handle,
ca09d69a 610 struct ld_plugin_input_file* file)
0f7c0701
CC
611{
612 Pluginobj* obj = this->object(handle);
613 if (obj == NULL)
614 return LDPS_BAD_HANDLE;
615
616 obj->lock(this->task_);
617 file->name = obj->filename().c_str();
618 file->fd = obj->descriptor();
619 file->offset = obj->offset();
620 file->filesize = obj->filesize();
621 file->handle = reinterpret_cast<void*>(handle);
622 return LDPS_OK;
623}
624
625// Release the input file.
626
627ld_plugin_status
628Plugin_manager::release_input_file(unsigned int handle)
629{
630 Pluginobj* obj = this->object(handle);
631 if (obj == NULL)
632 return LDPS_BAD_HANDLE;
633
634 obj->unlock(this->task_);
635 return LDPS_OK;
636}
637
42218b9f
RÁE
638// Add a new library path.
639
640ld_plugin_status
ca09d69a 641Plugin_manager::set_extra_library_path(const char* path)
42218b9f
RÁE
642{
643 this->extra_search_path_ = std::string(path);
644 return LDPS_OK;
645}
646
89fc3421
CC
647// Add a new input file.
648
649ld_plugin_status
ca09d69a 650Plugin_manager::add_input_file(const char* pathname, bool is_lib)
89fc3421 651{
ae3b5189
CD
652 Input_file_argument file(pathname,
653 (is_lib
654 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
655 : Input_file_argument::INPUT_FILE_TYPE_FILE),
42218b9f
RÁE
656 (is_lib
657 ? this->extra_search_path_.c_str()
658 : ""),
659 false,
660 this->options_);
89fc3421
CC
661 Input_argument* input_argument = new Input_argument(file);
662 Task_token* next_blocker = new Task_token(true);
663 next_blocker->add_blocker();
8c21d9d3 664 if (parameters->incremental())
40f36857
CC
665 gold_error(_("input files added by plug-ins in --incremental mode not "
666 "supported yet"));
f1ed28fb 667 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
89fc3421
CC
668 this->symtab_,
669 this->layout_,
670 this->dirpath_,
15f8229b 671 0,
89fc3421
CC
672 this->mapfile_,
673 input_argument,
674 NULL,
b0193076 675 NULL,
89fc3421
CC
676 this->this_blocker_,
677 next_blocker));
678 this->this_blocker_ = next_blocker;
0f3b89d8 679 this->any_added_ = true;
89fc3421
CC
680 return LDPS_OK;
681}
682
683// Class Pluginobj.
684
2ea97941
ILT
685Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
686 off_t offset, off_t filesize)
687 : Object(name, input_file, false, offset),
688 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
89fc3421
CC
689{
690}
691
d66a9eb3
CC
692// Return TRUE if a defined symbol might be reachable from outside the
693// universe of claimed objects.
694
695static inline bool
696is_visible_from_outside(Symbol* lsym)
697{
698 if (lsym->in_real_elf())
699 return true;
700 if (parameters->options().relocatable())
701 return true;
702 if (parameters->options().export_dynamic() || parameters->options().shared())
703 return lsym->is_externally_visible();
704 return false;
705}
706
89fc3421
CC
707// Get symbol resolution info.
708
709ld_plugin_status
710Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
711{
abc8dcba 712 if (nsyms > this->nsyms_)
89fc3421 713 return LDPS_NO_SYMS;
b0193076
RÁE
714
715 if (static_cast<size_t>(nsyms) > this->symbols_.size())
716 {
717 // We never decided to include this object. We mark all symbols as
718 // preempted.
ca09d69a 719 gold_assert(this->symbols_.size() == 0);
b0193076
RÁE
720 for (int i = 0; i < nsyms; i++)
721 syms[i].resolution = LDPR_PREEMPTED_REG;
722 return LDPS_OK;
723 }
724
89fc3421
CC
725 for (int i = 0; i < nsyms; i++)
726 {
727 ld_plugin_symbol* isym = &syms[i];
728 Symbol* lsym = this->symbols_[i];
729 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
730
731 if (lsym->is_undefined())
732 // The symbol remains undefined.
733 res = LDPR_UNDEF;
734 else if (isym->def == LDPK_UNDEF
735 || isym->def == LDPK_WEAKUNDEF
736 || isym->def == LDPK_COMMON)
737 {
738 // The original symbol was undefined or common.
739 if (lsym->source() != Symbol::FROM_OBJECT)
740 res = LDPR_RESOLVED_EXEC;
be234d88
CC
741 else if (lsym->object()->pluginobj() == this)
742 res = (is_visible_from_outside(lsym)
743 ? LDPR_PREVAILING_DEF
744 : LDPR_PREVAILING_DEF_IRONLY);
89fc3421
CC
745 else if (lsym->object()->pluginobj() != NULL)
746 res = LDPR_RESOLVED_IR;
747 else if (lsym->object()->is_dynamic())
748 res = LDPR_RESOLVED_DYN;
749 else
750 res = LDPR_RESOLVED_EXEC;
751 }
752 else
753 {
754 // The original symbol was a definition.
755 if (lsym->source() != Symbol::FROM_OBJECT)
756 res = LDPR_PREEMPTED_REG;
757 else if (lsym->object() == static_cast<const Object*>(this))
d66a9eb3 758 res = (is_visible_from_outside(lsym)
89fc3421
CC
759 ? LDPR_PREVAILING_DEF
760 : LDPR_PREVAILING_DEF_IRONLY);
761 else
762 res = (lsym->object()->pluginobj() != NULL
763 ? LDPR_PREEMPTED_IR
764 : LDPR_PREEMPTED_REG);
765 }
766 isym->resolution = res;
767 }
768 return LDPS_OK;
769}
770
771// Return TRUE if the comdat group with key COMDAT_KEY from this object
772// should be kept.
773
774bool
2ea97941 775Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
89fc3421
CC
776{
777 std::pair<Comdat_map::iterator, bool> ins =
778 this->comdat_map_.insert(std::make_pair(comdat_key, false));
779
780 // If this is the first time we've seen this comdat key, ask the
781 // layout object whether it should be included.
782 if (ins.second)
2ea97941
ILT
783 ins.first->second = layout->find_or_add_kept_section(comdat_key,
784 NULL, 0, true,
785 true, NULL);
89fc3421
CC
786
787 return ins.first->second;
788}
789
790// Class Sized_pluginobj.
791
792template<int size, bool big_endian>
793Sized_pluginobj<size, big_endian>::Sized_pluginobj(
2ea97941
ILT
794 const std::string& name,
795 Input_file* input_file,
796 off_t offset,
797 off_t filesize)
798 : Pluginobj(name, input_file, offset, filesize)
89fc3421
CC
799{
800}
801
802// Read the symbols. Not used for plugin objects.
803
804template<int size, bool big_endian>
805void
806Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
807{
808 gold_unreachable();
809}
810
811// Lay out the input sections. Not used for plugin objects.
812
813template<int size, bool big_endian>
814void
815Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
816 Read_symbols_data*)
817{
818 gold_unreachable();
819}
820
821// Add the symbols to the symbol table.
822
89fc3421
CC
823template<int size, bool big_endian>
824void
825Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
f488e4b0 826 Read_symbols_data*,
2ea97941 827 Layout* layout)
89fc3421
CC
828{
829 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
830 unsigned char symbuf[sym_size];
831 elfcpp::Sym<size, big_endian> sym(symbuf);
832 elfcpp::Sym_write<size, big_endian> osym(symbuf);
833
834 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
835
836 this->symbols_.resize(this->nsyms_);
837
838 for (int i = 0; i < this->nsyms_; ++i)
839 {
ca09d69a 840 const struct ld_plugin_symbol* isym = &this->syms_[i];
2ea97941 841 const char* name = isym->name;
89fc3421
CC
842 const char* ver = isym->version;
843 elfcpp::Elf_Half shndx;
844 elfcpp::STB bind;
845 elfcpp::STV vis;
846
2ea97941
ILT
847 if (name != NULL && name[0] == '\0')
848 name = NULL;
89fc3421
CC
849 if (ver != NULL && ver[0] == '\0')
850 ver = NULL;
851
852 switch (isym->def)
853 {
854 case LDPK_WEAKDEF:
855 case LDPK_WEAKUNDEF:
856 bind = elfcpp::STB_WEAK;
857 break;
858 case LDPK_DEF:
859 case LDPK_UNDEF:
860 case LDPK_COMMON:
861 default:
862 bind = elfcpp::STB_GLOBAL;
863 break;
864 }
865
866 switch (isym->def)
867 {
868 case LDPK_DEF:
869 case LDPK_WEAKDEF:
870 shndx = elfcpp::SHN_ABS;
871 break;
872 case LDPK_COMMON:
873 shndx = elfcpp::SHN_COMMON;
874 break;
875 case LDPK_UNDEF:
876 case LDPK_WEAKUNDEF:
877 default:
878 shndx = elfcpp::SHN_UNDEF;
879 break;
880 }
881
882 switch (isym->visibility)
883 {
884 case LDPV_PROTECTED:
105b6afd 885 vis = elfcpp::STV_PROTECTED;
89fc3421
CC
886 break;
887 case LDPV_INTERNAL:
105b6afd 888 vis = elfcpp::STV_INTERNAL;
89fc3421
CC
889 break;
890 case LDPV_HIDDEN:
105b6afd 891 vis = elfcpp::STV_HIDDEN;
89fc3421
CC
892 break;
893 case LDPV_DEFAULT:
894 default:
895 vis = elfcpp::STV_DEFAULT;
896 break;
897 }
898
899 if (isym->comdat_key != NULL
900 && isym->comdat_key[0] != '\0'
2ea97941 901 && !this->include_comdat_group(isym->comdat_key, layout))
89fc3421
CC
902 shndx = elfcpp::SHN_UNDEF;
903
904 osym.put_st_name(0);
905 osym.put_st_value(0);
906 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
907 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
908 osym.put_st_other(vis, 0);
909 osym.put_st_shndx(shndx);
910
911 this->symbols_[i] =
2ea97941 912 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
89fc3421
CC
913 }
914}
915
b0193076
RÁE
916template<int size, bool big_endian>
917Archive::Should_include
918Sized_pluginobj<size, big_endian>::do_should_include_member(
88a4108b
ILT
919 Symbol_table* symtab,
920 Layout* layout,
921 Read_symbols_data*,
922 std::string* why)
b0193076
RÁE
923{
924 char* tmpbuf = NULL;
925 size_t tmpbuflen = 0;
926
88a4108b
ILT
927 for (int i = 0; i < this->nsyms_; ++i)
928 {
929 const struct ld_plugin_symbol& sym = this->syms_[i];
930 const char* name = sym.name;
931 Symbol* symbol;
932 Archive::Should_include t = Archive::should_include_member(symtab,
933 layout,
934 name,
935 &symbol, why,
936 &tmpbuf,
937 &tmpbuflen);
b0193076
RÁE
938 if (t == Archive::SHOULD_INCLUDE_YES)
939 {
940 if (tmpbuf != NULL)
941 free(tmpbuf);
942 return t;
943 }
88a4108b 944 }
b0193076
RÁE
945 if (tmpbuf != NULL)
946 free(tmpbuf);
947 return Archive::SHOULD_INCLUDE_UNKNOWN;
948}
949
89fc3421
CC
950// Get the size of a section. Not used for plugin objects.
951
952template<int size, bool big_endian>
953uint64_t
954Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
955{
956 gold_unreachable();
957 return 0;
958}
959
960// Get the name of a section. Not used for plugin objects.
961
962template<int size, bool big_endian>
963std::string
964Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
965{
966 gold_unreachable();
967 return std::string();
968}
969
970// Return a view of the contents of a section. Not used for plugin objects.
971
972template<int size, bool big_endian>
973Object::Location
974Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
975{
976 Location loc(0, 0);
977
978 gold_unreachable();
979 return loc;
980}
981
982// Return section flags. Not used for plugin objects.
983
984template<int size, bool big_endian>
985uint64_t
986Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
987{
988 gold_unreachable();
989 return 0;
990}
991
ef15dade
ST
992// Return section entsize. Not used for plugin objects.
993
994template<int size, bool big_endian>
995uint64_t
996Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
997{
998 gold_unreachable();
999 return 0;
1000}
1001
89fc3421
CC
1002// Return section address. Not used for plugin objects.
1003
1004template<int size, bool big_endian>
1005uint64_t
1006Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1007{
1008 gold_unreachable();
1009 return 0;
1010}
1011
1012// Return section type. Not used for plugin objects.
1013
1014template<int size, bool big_endian>
1015unsigned int
1016Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1017{
1018 gold_unreachable();
1019 return 0;
1020}
1021
1022// Return the section link field. Not used for plugin objects.
1023
1024template<int size, bool big_endian>
1025unsigned int
1026Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1027{
1028 gold_unreachable();
1029 return 0;
1030}
1031
1032// Return the section link field. Not used for plugin objects.
1033
1034template<int size, bool big_endian>
1035unsigned int
1036Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1037{
1038 gold_unreachable();
1039 return 0;
1040}
1041
1042// Return the section alignment. Not used for plugin objects.
1043
1044template<int size, bool big_endian>
1045uint64_t
1046Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1047{
1048 gold_unreachable();
1049 return 0;
1050}
1051
1052// Return the Xindex structure to use. Not used for plugin objects.
1053
1054template<int size, bool big_endian>
1055Xindex*
1056Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1057{
1058 gold_unreachable();
1059 return NULL;
1060}
1061
1062// Get symbol counts. Not used for plugin objects.
1063
1064template<int size, bool big_endian>
1065void
1066Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
1067 size_t*, size_t*) const
1068{
1069 gold_unreachable();
1070}
1071
dde3f402
ILT
1072// Get symbols. Not used for plugin objects.
1073
1074template<int size, bool big_endian>
1075const Object::Symbols*
1076Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1077{
1078 gold_unreachable();
1079}
1080
5995b570 1081// Class Plugin_finish. This task runs after all replacement files have
78384e8f
CC
1082// been added. For now, it's a placeholder for a possible plugin API
1083// to allow the plugin to release most of its resources. The cleanup
1084// handlers must be called later, because they can remove the temporary
1085// object files that are needed until the end of the link.
89fc3421 1086
5995b570 1087class Plugin_finish : public Task
89fc3421
CC
1088{
1089 public:
5995b570 1090 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
89fc3421
CC
1091 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1092 { }
1093
5995b570 1094 ~Plugin_finish()
89fc3421
CC
1095 {
1096 if (this->this_blocker_ != NULL)
1097 delete this->this_blocker_;
1098 }
1099
1100 Task_token*
1101 is_runnable()
1102 {
1103 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1104 return this->this_blocker_;
1105 return NULL;
1106 }
1107
1108 void
1109 locks(Task_locker* tl)
1110 { tl->add(this, this->next_blocker_); }
1111
1112 void
1113 run(Workqueue*)
483620e8 1114 {
78384e8f 1115 // We could call early cleanup handlers here.
483620e8 1116 }
89fc3421
CC
1117
1118 std::string
1119 get_name() const
5995b570 1120 { return "Plugin_finish"; }
89fc3421
CC
1121
1122 private:
1123 Task_token* this_blocker_;
1124 Task_token* next_blocker_;
1125};
1126
1127// Class Plugin_hook.
1128
1129Plugin_hook::~Plugin_hook()
1130{
1131}
1132
1133// Return whether a Plugin_hook task is runnable.
1134
1135Task_token*
1136Plugin_hook::is_runnable()
1137{
1138 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1139 return this->this_blocker_;
1140 return NULL;
1141}
1142
1143// Return a Task_locker for a Plugin_hook task. We don't need any
1144// locks here.
1145
1146void
1147Plugin_hook::locks(Task_locker*)
1148{
1149}
1150
89fc3421
CC
1151// Run the "all symbols read" plugin hook.
1152
1153void
5995b570 1154Plugin_hook::run(Workqueue* workqueue)
89fc3421
CC
1155{
1156 gold_assert(this->options_.has_plugins());
91ff43fe
RÁE
1157 Symbol* start_sym;
1158 if (parameters->options().entry())
1159 start_sym = this->symtab_->lookup(parameters->options().entry());
1160 else
1161 start_sym = this->symtab_->lookup("_start");
1162 if (start_sym != NULL)
1163 start_sym->set_in_real_elf();
1164
89fc3421 1165 this->options_.plugins()->all_symbols_read(workqueue,
0f7c0701 1166 this,
89fc3421
CC
1167 this->input_objects_,
1168 this->symtab_,
1169 this->layout_,
1170 this->dirpath_,
1171 this->mapfile_,
1172 &this->this_blocker_);
5995b570
CC
1173 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1174 this->next_blocker_));
89fc3421
CC
1175}
1176
1177// The C interface routines called by the plugins.
1178
1179#ifdef ENABLE_PLUGINS
1180
1181// Register a claim-file handler.
1182
1183static enum ld_plugin_status
1184register_claim_file(ld_plugin_claim_file_handler handler)
1185{
1186 gold_assert(parameters->options().has_plugins());
1187 parameters->options().plugins()->set_claim_file_handler(handler);
1188 return LDPS_OK;
1189}
1190
1191// Register an all-symbols-read handler.
1192
1193static enum ld_plugin_status
1194register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1195{
1196 gold_assert(parameters->options().has_plugins());
1197 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1198 return LDPS_OK;
1199}
1200
1201// Register a cleanup handler.
1202
1203static enum ld_plugin_status
1204register_cleanup(ld_plugin_cleanup_handler handler)
1205{
1206 gold_assert(parameters->options().has_plugins());
1207 parameters->options().plugins()->set_cleanup_handler(handler);
1208 return LDPS_OK;
1209}
1210
1211// Add symbols from a plugin-claimed input file.
1212
1213static enum ld_plugin_status
ca09d69a 1214add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
89fc3421
CC
1215{
1216 gold_assert(parameters->options().has_plugins());
1217 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1218 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1219 if (obj == NULL)
1220 return LDPS_ERR;
1221 obj->store_incoming_symbols(nsyms, syms);
1222 return LDPS_OK;
1223}
1224
0f7c0701
CC
1225// Get the input file information with an open (possibly re-opened)
1226// file descriptor.
1227
1228static enum ld_plugin_status
ca09d69a 1229get_input_file(const void* handle, struct ld_plugin_input_file* file)
0f7c0701
CC
1230{
1231 gold_assert(parameters->options().has_plugins());
1232 unsigned int obj_index =
1233 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1234 return parameters->options().plugins()->get_input_file(obj_index, file);
1235}
1236
1237// Release the input file.
1238
1239static enum ld_plugin_status
ca09d69a 1240release_input_file(const void* handle)
0f7c0701
CC
1241{
1242 gold_assert(parameters->options().has_plugins());
1243 unsigned int obj_index =
1244 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1245 return parameters->options().plugins()->release_input_file(obj_index);
1246}
1247
89fc3421
CC
1248// Get the symbol resolution info for a plugin-claimed input file.
1249
1250static enum ld_plugin_status
ca09d69a 1251get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
89fc3421
CC
1252{
1253 gold_assert(parameters->options().has_plugins());
1254 Pluginobj* obj = parameters->options().plugins()->object(
1255 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1256 if (obj == NULL)
1257 return LDPS_ERR;
1258 return obj->get_symbol_resolution_info(nsyms, syms);
1259}
1260
1261// Add a new (real) input file generated by a plugin.
1262
1263static enum ld_plugin_status
ca09d69a 1264add_input_file(const char* pathname)
89fc3421
CC
1265{
1266 gold_assert(parameters->options().has_plugins());
e99daf92
ILT
1267 return parameters->options().plugins()->add_input_file(pathname, false);
1268}
1269
1270// Add a new (real) library required by a plugin.
1271
1272static enum ld_plugin_status
ca09d69a 1273add_input_library(const char* pathname)
e99daf92
ILT
1274{
1275 gold_assert(parameters->options().has_plugins());
1276 return parameters->options().plugins()->add_input_file(pathname, true);
89fc3421
CC
1277}
1278
42218b9f
RÁE
1279// Set the extra library path to be used by libraries added via
1280// add_input_library
1281
1282static enum ld_plugin_status
ca09d69a 1283set_extra_library_path(const char* path)
42218b9f
RÁE
1284{
1285 gold_assert(parameters->options().has_plugins());
1286 return parameters->options().plugins()->set_extra_library_path(path);
1287}
1288
89fc3421
CC
1289// Issue a diagnostic message from a plugin.
1290
1291static enum ld_plugin_status
ca09d69a 1292message(int level, const char* format, ...)
89fc3421
CC
1293{
1294 va_list args;
1295 va_start(args, format);
1296
1297 switch (level)
1298 {
1299 case LDPL_INFO:
1300 parameters->errors()->info(format, args);
1301 break;
1302 case LDPL_WARNING:
1303 parameters->errors()->warning(format, args);
1304 break;
1305 case LDPL_ERROR:
1306 default:
1307 parameters->errors()->error(format, args);
1308 break;
1309 case LDPL_FATAL:
1310 parameters->errors()->fatal(format, args);
1311 break;
1312 }
1313
1314 va_end(args);
1315 return LDPS_OK;
1316}
1317
1318#endif // ENABLE_PLUGINS
1319
1320// Allocate a Pluginobj object of the appropriate size and endianness.
1321
1322static Pluginobj*
0f7c0701 1323make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
89fc3421 1324{
89fc3421
CC
1325 Pluginobj* obj = NULL;
1326
029ba973
ILT
1327 parameters_force_valid_target();
1328 const Target& target(parameters->target());
89fc3421 1329
029ba973 1330 if (target.get_size() == 32)
89fc3421 1331 {
029ba973 1332 if (target.is_big_endian())
92f03fcb 1333#ifdef HAVE_TARGET_32_BIG
89fc3421 1334 obj = new Sized_pluginobj<32, true>(input_file->filename(),
0f7c0701 1335 input_file, offset, filesize);
92f03fcb
CC
1336#else
1337 gold_error(_("%s: not configured to support "
1338 "32-bit big-endian object"),
1339 input_file->filename().c_str());
89fc3421 1340#endif
89fc3421 1341 else
92f03fcb 1342#ifdef HAVE_TARGET_32_LITTLE
89fc3421 1343 obj = new Sized_pluginobj<32, false>(input_file->filename(),
0f7c0701 1344 input_file, offset, filesize);
92f03fcb
CC
1345#else
1346 gold_error(_("%s: not configured to support "
1347 "32-bit little-endian object"),
1348 input_file->filename().c_str());
89fc3421
CC
1349#endif
1350 }
029ba973 1351 else if (target.get_size() == 64)
89fc3421 1352 {
029ba973 1353 if (target.is_big_endian())
92f03fcb 1354#ifdef HAVE_TARGET_64_BIG
89fc3421 1355 obj = new Sized_pluginobj<64, true>(input_file->filename(),
0f7c0701 1356 input_file, offset, filesize);
92f03fcb
CC
1357#else
1358 gold_error(_("%s: not configured to support "
1359 "64-bit big-endian object"),
1360 input_file->filename().c_str());
89fc3421 1361#endif
89fc3421 1362 else
92f03fcb 1363#ifdef HAVE_TARGET_64_LITTLE
89fc3421 1364 obj = new Sized_pluginobj<64, false>(input_file->filename(),
0f7c0701 1365 input_file, offset, filesize);
92f03fcb
CC
1366#else
1367 gold_error(_("%s: not configured to support "
1368 "64-bit little-endian object"),
1369 input_file->filename().c_str());
89fc3421
CC
1370#endif
1371 }
1372
1373 gold_assert(obj != NULL);
89fc3421
CC
1374 return obj;
1375}
1376
1377} // End namespace gold.
This page took 0.208399 seconds and 4 git commands to generate.