2010-02-08 Rafael Ávila de Espíndola <respindola@mozilla.com>
[deliverable/binutils-gdb.git] / gold / plugin.cc
1 // plugin.cc -- plugin manager for gold -*- C++ -*-
2
3 // Copyright 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstdio>
26 #include <cstdarg>
27 #include <cstring>
28 #include <string>
29 #include <vector>
30
31 #ifdef ENABLE_PLUGINS
32 #include <dlfcn.h>
33 #endif
34
35 #include "parameters.h"
36 #include "errors.h"
37 #include "fileread.h"
38 #include "layout.h"
39 #include "options.h"
40 #include "plugin.h"
41 #include "target.h"
42 #include "readsyms.h"
43 #include "symtab.h"
44 #include "elfcpp.h"
45
46 namespace gold
47 {
48
49 #ifdef ENABLE_PLUGINS
50
51 // The linker's exported interfaces.
52
53 extern "C"
54 {
55
56 static enum ld_plugin_status
57 register_claim_file(ld_plugin_claim_file_handler handler);
58
59 static enum ld_plugin_status
60 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
61
62 static enum ld_plugin_status
63 register_cleanup(ld_plugin_cleanup_handler handler);
64
65 static enum ld_plugin_status
66 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
67
68 static enum ld_plugin_status
69 get_input_file(const void *handle, struct ld_plugin_input_file *file);
70
71 static enum ld_plugin_status
72 release_input_file(const void *handle);
73
74 static enum ld_plugin_status
75 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
76
77 static enum ld_plugin_status
78 add_input_file(const char *pathname);
79
80 static enum ld_plugin_status
81 add_input_library(const char *pathname);
82
83 static enum ld_plugin_status
84 set_extra_library_path(const char *path);
85
86 static enum ld_plugin_status
87 message(int level, const char *format, ...);
88
89 };
90
91 #endif // ENABLE_PLUGINS
92
93 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
94 off_t offset, off_t filesize);
95
96 // Plugin methods.
97
98 // Load one plugin library.
99
100 void
101 Plugin::load()
102 {
103 #ifdef ENABLE_PLUGINS
104 // Load the plugin library.
105 // FIXME: Look for the library in standard locations.
106 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
107 if (this->handle_ == NULL)
108 {
109 gold_error(_("%s: could not load plugin library: %s"),
110 this->filename_.c_str(), dlerror());
111 return;
112 }
113
114 // Find the plugin's onload entry point.
115 void* ptr = dlsym(this->handle_, "onload");
116 if (ptr == NULL)
117 {
118 gold_error(_("%s: could not find onload entry point"),
119 this->filename_.c_str());
120 return;
121 }
122 ld_plugin_onload onload;
123 gold_assert(sizeof(onload) == sizeof(ptr));
124 memcpy(&onload, &ptr, sizeof(ptr));
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.
133 const int tv_fixed_size = 16;
134 int tv_size = this->args_.size() + tv_fixed_size;
135 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
136
137 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
138 // while processing subsequent entries.
139 int i = 0;
140 tv[i].tv_tag = LDPT_MESSAGE;
141 tv[i].tv_u.tv_message = message;
142
143 ++i;
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
160 ++i;
161 tv[i].tv_tag = LDPT_OUTPUT_NAME;
162 tv[i].tv_u.tv_string = parameters->options().output();
163
164 for (unsigned int j = 0; j < this->args_.size(); ++j)
165 {
166 ++i;
167 tv[i].tv_tag = LDPT_OPTION;
168 tv[i].tv_u.tv_string = this->args_[j].c_str();
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
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
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
203 ++i;
204 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
205 tv[i].tv_u.tv_add_input_library = add_input_library;
206
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
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
220 delete[] tv;
221 #endif // ENABLE_PLUGINS
222 }
223
224 // Call the plugin claim-file handler.
225
226 inline bool
227 Plugin::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
242 inline void
243 Plugin::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
251 inline void
252 Plugin::cleanup()
253 {
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 }
262 }
263
264 // This task is used to rescan archives as needed.
265
266 class 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
303 // Plugin_manager methods.
304
305 Plugin_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
321 void
322 Plugin_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
332 Pluginobj*
333 Plugin_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 {
353 this->any_claimed_ = true;
354
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;
362 }
363 }
364
365 return NULL;
366 }
367
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
373 void
374 Plugin_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
384 void
385 Plugin_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
393 // Call the all-symbols-read handlers.
394
395 void
396 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
397 Input_objects* input_objects,
398 Symbol_table* symtab, Layout* layout,
399 Dirsearch* dirpath, Mapfile* mapfile,
400 Task_token** last_blocker)
401 {
402 this->in_replacement_phase_ = true;
403 this->workqueue_ = workqueue;
404 this->task_ = task;
405 this->input_objects_ = input_objects;
406 this->symtab_ = symtab;
407 this->layout_ = layout;
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
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
425 *last_blocker = this->this_blocker_;
426 }
427
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
432 void
433 Plugin_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
443 void
444 Plugin_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
538 bool
539 Plugin_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
557 // Layout deferred objects.
558
559 void
560 Plugin_manager::layout_deferred_objects()
561 {
562 Deferred_layout_list::iterator obj;
563
564 for (obj = this->deferred_layout_objects_.begin();
565 obj != this->deferred_layout_objects_.end();
566 ++obj)
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 }
575 }
576
577 // Call the cleanup handlers.
578
579 void
580 Plugin_manager::cleanup()
581 {
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
591 Pluginobj*
592 Plugin_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_,
599 this->plugin_input_file_.offset,
600 this->plugin_input_file_.filesize);
601 this->objects_.push_back(obj);
602 return obj;
603 }
604
605 // Get the input file information with an open (possibly re-opened)
606 // file descriptor.
607
608 ld_plugin_status
609 Plugin_manager::get_input_file(unsigned int handle,
610 struct ld_plugin_input_file* file)
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
627 ld_plugin_status
628 Plugin_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
638 // Add a new library path.
639
640 ld_plugin_status
641 Plugin_manager::set_extra_library_path(const char* path)
642 {
643 this->extra_search_path_ = std::string(path);
644 return LDPS_OK;
645 }
646
647 // Add a new input file.
648
649 ld_plugin_status
650 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
651 {
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),
656 (is_lib
657 ? this->extra_search_path_.c_str()
658 : ""),
659 false,
660 this->options_);
661 Input_argument* input_argument = new Input_argument(file);
662 Task_token* next_blocker = new Task_token(true);
663 next_blocker->add_blocker();
664 if (parameters->incremental())
665 gold_error(_("input files added by plug-ins in --incremental mode not "
666 "supported yet"));
667 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
668 this->symtab_,
669 this->layout_,
670 this->dirpath_,
671 0,
672 this->mapfile_,
673 input_argument,
674 NULL,
675 NULL,
676 this->this_blocker_,
677 next_blocker));
678 this->this_blocker_ = next_blocker;
679 this->any_added_ = true;
680 return LDPS_OK;
681 }
682
683 // Class Pluginobj.
684
685 Pluginobj::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_()
689 {
690 }
691
692 // Return TRUE if a defined symbol might be reachable from outside the
693 // universe of claimed objects.
694
695 static inline bool
696 is_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().is_undefined(lsym->name()))
703 return true;
704 if (parameters->options().export_dynamic() || parameters->options().shared())
705 return lsym->is_externally_visible();
706 return false;
707 }
708
709 // Get symbol resolution info.
710
711 ld_plugin_status
712 Pluginobj::get_symbol_resolution_info(int nsyms, ld_plugin_symbol* syms) const
713 {
714 if (nsyms > this->nsyms_)
715 return LDPS_NO_SYMS;
716
717 if (static_cast<size_t>(nsyms) > this->symbols_.size())
718 {
719 // We never decided to include this object. We mark all symbols as
720 // preempted.
721 gold_assert(this->symbols_.size() == 0);
722 for (int i = 0; i < nsyms; i++)
723 syms[i].resolution = LDPR_PREEMPTED_REG;
724 return LDPS_OK;
725 }
726
727 for (int i = 0; i < nsyms; i++)
728 {
729 ld_plugin_symbol* isym = &syms[i];
730 Symbol* lsym = this->symbols_[i];
731 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
732
733 if (lsym->is_undefined())
734 // The symbol remains undefined.
735 res = LDPR_UNDEF;
736 else if (isym->def == LDPK_UNDEF
737 || isym->def == LDPK_WEAKUNDEF
738 || isym->def == LDPK_COMMON)
739 {
740 // The original symbol was undefined or common.
741 if (lsym->source() != Symbol::FROM_OBJECT)
742 res = LDPR_RESOLVED_EXEC;
743 else if (lsym->object()->pluginobj() == this)
744 res = (is_visible_from_outside(lsym)
745 ? LDPR_PREVAILING_DEF
746 : LDPR_PREVAILING_DEF_IRONLY);
747 else if (lsym->object()->pluginobj() != NULL)
748 res = LDPR_RESOLVED_IR;
749 else if (lsym->object()->is_dynamic())
750 res = LDPR_RESOLVED_DYN;
751 else
752 res = LDPR_RESOLVED_EXEC;
753 }
754 else
755 {
756 // The original symbol was a definition.
757 if (lsym->source() != Symbol::FROM_OBJECT)
758 res = LDPR_PREEMPTED_REG;
759 else if (lsym->object() == static_cast<const Object*>(this))
760 res = (is_visible_from_outside(lsym)
761 ? LDPR_PREVAILING_DEF
762 : LDPR_PREVAILING_DEF_IRONLY);
763 else
764 res = (lsym->object()->pluginobj() != NULL
765 ? LDPR_PREEMPTED_IR
766 : LDPR_PREEMPTED_REG);
767 }
768 isym->resolution = res;
769 }
770 return LDPS_OK;
771 }
772
773 // Return TRUE if the comdat group with key COMDAT_KEY from this object
774 // should be kept.
775
776 bool
777 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
778 {
779 std::pair<Comdat_map::iterator, bool> ins =
780 this->comdat_map_.insert(std::make_pair(comdat_key, false));
781
782 // If this is the first time we've seen this comdat key, ask the
783 // layout object whether it should be included.
784 if (ins.second)
785 ins.first->second = layout->find_or_add_kept_section(comdat_key,
786 NULL, 0, true,
787 true, NULL);
788
789 return ins.first->second;
790 }
791
792 // Class Sized_pluginobj.
793
794 template<int size, bool big_endian>
795 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
796 const std::string& name,
797 Input_file* input_file,
798 off_t offset,
799 off_t filesize)
800 : Pluginobj(name, input_file, offset, filesize)
801 {
802 }
803
804 // Read the symbols. Not used for plugin objects.
805
806 template<int size, bool big_endian>
807 void
808 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
809 {
810 gold_unreachable();
811 }
812
813 // Lay out the input sections. Not used for plugin objects.
814
815 template<int size, bool big_endian>
816 void
817 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
818 Read_symbols_data*)
819 {
820 gold_unreachable();
821 }
822
823 // Add the symbols to the symbol table.
824
825 template<int size, bool big_endian>
826 void
827 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
828 Read_symbols_data*,
829 Layout* layout)
830 {
831 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
832 unsigned char symbuf[sym_size];
833 elfcpp::Sym<size, big_endian> sym(symbuf);
834 elfcpp::Sym_write<size, big_endian> osym(symbuf);
835
836 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
837
838 this->symbols_.resize(this->nsyms_);
839
840 for (int i = 0; i < this->nsyms_; ++i)
841 {
842 const struct ld_plugin_symbol* isym = &this->syms_[i];
843 const char* name = isym->name;
844 const char* ver = isym->version;
845 elfcpp::Elf_Half shndx;
846 elfcpp::STB bind;
847 elfcpp::STV vis;
848
849 if (name != NULL && name[0] == '\0')
850 name = NULL;
851 if (ver != NULL && ver[0] == '\0')
852 ver = NULL;
853
854 switch (isym->def)
855 {
856 case LDPK_WEAKDEF:
857 case LDPK_WEAKUNDEF:
858 bind = elfcpp::STB_WEAK;
859 break;
860 case LDPK_DEF:
861 case LDPK_UNDEF:
862 case LDPK_COMMON:
863 default:
864 bind = elfcpp::STB_GLOBAL;
865 break;
866 }
867
868 switch (isym->def)
869 {
870 case LDPK_DEF:
871 case LDPK_WEAKDEF:
872 shndx = elfcpp::SHN_ABS;
873 break;
874 case LDPK_COMMON:
875 shndx = elfcpp::SHN_COMMON;
876 break;
877 case LDPK_UNDEF:
878 case LDPK_WEAKUNDEF:
879 default:
880 shndx = elfcpp::SHN_UNDEF;
881 break;
882 }
883
884 switch (isym->visibility)
885 {
886 case LDPV_PROTECTED:
887 vis = elfcpp::STV_PROTECTED;
888 break;
889 case LDPV_INTERNAL:
890 vis = elfcpp::STV_INTERNAL;
891 break;
892 case LDPV_HIDDEN:
893 vis = elfcpp::STV_HIDDEN;
894 break;
895 case LDPV_DEFAULT:
896 default:
897 vis = elfcpp::STV_DEFAULT;
898 break;
899 }
900
901 if (isym->comdat_key != NULL
902 && isym->comdat_key[0] != '\0'
903 && !this->include_comdat_group(isym->comdat_key, layout))
904 shndx = elfcpp::SHN_UNDEF;
905
906 osym.put_st_name(0);
907 osym.put_st_value(0);
908 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
909 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
910 osym.put_st_other(vis, 0);
911 osym.put_st_shndx(shndx);
912
913 this->symbols_[i] =
914 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
915 }
916 }
917
918 template<int size, bool big_endian>
919 Archive::Should_include
920 Sized_pluginobj<size, big_endian>::do_should_include_member(
921 Symbol_table* symtab,
922 Layout* layout,
923 Read_symbols_data*,
924 std::string* why)
925 {
926 char* tmpbuf = NULL;
927 size_t tmpbuflen = 0;
928
929 for (int i = 0; i < this->nsyms_; ++i)
930 {
931 const struct ld_plugin_symbol& sym = this->syms_[i];
932 const char* name = sym.name;
933 Symbol* symbol;
934 Archive::Should_include t = Archive::should_include_member(symtab,
935 layout,
936 name,
937 &symbol, why,
938 &tmpbuf,
939 &tmpbuflen);
940 if (t == Archive::SHOULD_INCLUDE_YES)
941 {
942 if (tmpbuf != NULL)
943 free(tmpbuf);
944 return t;
945 }
946 }
947 if (tmpbuf != NULL)
948 free(tmpbuf);
949 return Archive::SHOULD_INCLUDE_UNKNOWN;
950 }
951
952 // Get the size of a section. Not used for plugin objects.
953
954 template<int size, bool big_endian>
955 uint64_t
956 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
957 {
958 gold_unreachable();
959 return 0;
960 }
961
962 // Get the name of a section. Not used for plugin objects.
963
964 template<int size, bool big_endian>
965 std::string
966 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
967 {
968 gold_unreachable();
969 return std::string();
970 }
971
972 // Return a view of the contents of a section. Not used for plugin objects.
973
974 template<int size, bool big_endian>
975 Object::Location
976 Sized_pluginobj<size, big_endian>::do_section_contents(unsigned int)
977 {
978 Location loc(0, 0);
979
980 gold_unreachable();
981 return loc;
982 }
983
984 // Return section flags. Not used for plugin objects.
985
986 template<int size, bool big_endian>
987 uint64_t
988 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
989 {
990 gold_unreachable();
991 return 0;
992 }
993
994 // Return section entsize. Not used for plugin objects.
995
996 template<int size, bool big_endian>
997 uint64_t
998 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
999 {
1000 gold_unreachable();
1001 return 0;
1002 }
1003
1004 // Return section address. Not used for plugin objects.
1005
1006 template<int size, bool big_endian>
1007 uint64_t
1008 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1009 {
1010 gold_unreachable();
1011 return 0;
1012 }
1013
1014 // Return section type. Not used for plugin objects.
1015
1016 template<int size, bool big_endian>
1017 unsigned int
1018 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1019 {
1020 gold_unreachable();
1021 return 0;
1022 }
1023
1024 // Return the section link field. Not used for plugin objects.
1025
1026 template<int size, bool big_endian>
1027 unsigned int
1028 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1029 {
1030 gold_unreachable();
1031 return 0;
1032 }
1033
1034 // Return the section link field. Not used for plugin objects.
1035
1036 template<int size, bool big_endian>
1037 unsigned int
1038 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1039 {
1040 gold_unreachable();
1041 return 0;
1042 }
1043
1044 // Return the section alignment. Not used for plugin objects.
1045
1046 template<int size, bool big_endian>
1047 uint64_t
1048 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1049 {
1050 gold_unreachable();
1051 return 0;
1052 }
1053
1054 // Return the Xindex structure to use. Not used for plugin objects.
1055
1056 template<int size, bool big_endian>
1057 Xindex*
1058 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1059 {
1060 gold_unreachable();
1061 return NULL;
1062 }
1063
1064 // Get symbol counts. Not used for plugin objects.
1065
1066 template<int size, bool big_endian>
1067 void
1068 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(const Symbol_table*,
1069 size_t*, size_t*) const
1070 {
1071 gold_unreachable();
1072 }
1073
1074 // Get symbols. Not used for plugin objects.
1075
1076 template<int size, bool big_endian>
1077 const Object::Symbols*
1078 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1079 {
1080 gold_unreachable();
1081 }
1082
1083 // Class Plugin_finish. This task runs after all replacement files have
1084 // been added. For now, it's a placeholder for a possible plugin API
1085 // to allow the plugin to release most of its resources. The cleanup
1086 // handlers must be called later, because they can remove the temporary
1087 // object files that are needed until the end of the link.
1088
1089 class Plugin_finish : public Task
1090 {
1091 public:
1092 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1093 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1094 { }
1095
1096 ~Plugin_finish()
1097 {
1098 if (this->this_blocker_ != NULL)
1099 delete this->this_blocker_;
1100 }
1101
1102 Task_token*
1103 is_runnable()
1104 {
1105 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1106 return this->this_blocker_;
1107 return NULL;
1108 }
1109
1110 void
1111 locks(Task_locker* tl)
1112 { tl->add(this, this->next_blocker_); }
1113
1114 void
1115 run(Workqueue*)
1116 {
1117 // We could call early cleanup handlers here.
1118 }
1119
1120 std::string
1121 get_name() const
1122 { return "Plugin_finish"; }
1123
1124 private:
1125 Task_token* this_blocker_;
1126 Task_token* next_blocker_;
1127 };
1128
1129 // Class Plugin_hook.
1130
1131 Plugin_hook::~Plugin_hook()
1132 {
1133 }
1134
1135 // Return whether a Plugin_hook task is runnable.
1136
1137 Task_token*
1138 Plugin_hook::is_runnable()
1139 {
1140 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1141 return this->this_blocker_;
1142 return NULL;
1143 }
1144
1145 // Return a Task_locker for a Plugin_hook task. We don't need any
1146 // locks here.
1147
1148 void
1149 Plugin_hook::locks(Task_locker*)
1150 {
1151 }
1152
1153 // Run the "all symbols read" plugin hook.
1154
1155 void
1156 Plugin_hook::run(Workqueue* workqueue)
1157 {
1158 gold_assert(this->options_.has_plugins());
1159 Symbol* start_sym;
1160 if (parameters->options().entry())
1161 start_sym = this->symtab_->lookup(parameters->options().entry());
1162 else
1163 start_sym = this->symtab_->lookup("_start");
1164 if (start_sym != NULL)
1165 start_sym->set_in_real_elf();
1166
1167 this->options_.plugins()->all_symbols_read(workqueue,
1168 this,
1169 this->input_objects_,
1170 this->symtab_,
1171 this->layout_,
1172 this->dirpath_,
1173 this->mapfile_,
1174 &this->this_blocker_);
1175 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1176 this->next_blocker_));
1177 }
1178
1179 // The C interface routines called by the plugins.
1180
1181 #ifdef ENABLE_PLUGINS
1182
1183 // Register a claim-file handler.
1184
1185 static enum ld_plugin_status
1186 register_claim_file(ld_plugin_claim_file_handler handler)
1187 {
1188 gold_assert(parameters->options().has_plugins());
1189 parameters->options().plugins()->set_claim_file_handler(handler);
1190 return LDPS_OK;
1191 }
1192
1193 // Register an all-symbols-read handler.
1194
1195 static enum ld_plugin_status
1196 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1197 {
1198 gold_assert(parameters->options().has_plugins());
1199 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1200 return LDPS_OK;
1201 }
1202
1203 // Register a cleanup handler.
1204
1205 static enum ld_plugin_status
1206 register_cleanup(ld_plugin_cleanup_handler handler)
1207 {
1208 gold_assert(parameters->options().has_plugins());
1209 parameters->options().plugins()->set_cleanup_handler(handler);
1210 return LDPS_OK;
1211 }
1212
1213 // Add symbols from a plugin-claimed input file.
1214
1215 static enum ld_plugin_status
1216 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1217 {
1218 gold_assert(parameters->options().has_plugins());
1219 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1220 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1221 if (obj == NULL)
1222 return LDPS_ERR;
1223 obj->store_incoming_symbols(nsyms, syms);
1224 return LDPS_OK;
1225 }
1226
1227 // Get the input file information with an open (possibly re-opened)
1228 // file descriptor.
1229
1230 static enum ld_plugin_status
1231 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1232 {
1233 gold_assert(parameters->options().has_plugins());
1234 unsigned int obj_index =
1235 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1236 return parameters->options().plugins()->get_input_file(obj_index, file);
1237 }
1238
1239 // Release the input file.
1240
1241 static enum ld_plugin_status
1242 release_input_file(const void* handle)
1243 {
1244 gold_assert(parameters->options().has_plugins());
1245 unsigned int obj_index =
1246 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1247 return parameters->options().plugins()->release_input_file(obj_index);
1248 }
1249
1250 // Get the symbol resolution info for a plugin-claimed input file.
1251
1252 static enum ld_plugin_status
1253 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1254 {
1255 gold_assert(parameters->options().has_plugins());
1256 Pluginobj* obj = parameters->options().plugins()->object(
1257 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1258 if (obj == NULL)
1259 return LDPS_ERR;
1260 return obj->get_symbol_resolution_info(nsyms, syms);
1261 }
1262
1263 // Add a new (real) input file generated by a plugin.
1264
1265 static enum ld_plugin_status
1266 add_input_file(const char* pathname)
1267 {
1268 gold_assert(parameters->options().has_plugins());
1269 return parameters->options().plugins()->add_input_file(pathname, false);
1270 }
1271
1272 // Add a new (real) library required by a plugin.
1273
1274 static enum ld_plugin_status
1275 add_input_library(const char* pathname)
1276 {
1277 gold_assert(parameters->options().has_plugins());
1278 return parameters->options().plugins()->add_input_file(pathname, true);
1279 }
1280
1281 // Set the extra library path to be used by libraries added via
1282 // add_input_library
1283
1284 static enum ld_plugin_status
1285 set_extra_library_path(const char* path)
1286 {
1287 gold_assert(parameters->options().has_plugins());
1288 return parameters->options().plugins()->set_extra_library_path(path);
1289 }
1290
1291 // Issue a diagnostic message from a plugin.
1292
1293 static enum ld_plugin_status
1294 message(int level, const char* format, ...)
1295 {
1296 va_list args;
1297 va_start(args, format);
1298
1299 switch (level)
1300 {
1301 case LDPL_INFO:
1302 parameters->errors()->info(format, args);
1303 break;
1304 case LDPL_WARNING:
1305 parameters->errors()->warning(format, args);
1306 break;
1307 case LDPL_ERROR:
1308 default:
1309 parameters->errors()->error(format, args);
1310 break;
1311 case LDPL_FATAL:
1312 parameters->errors()->fatal(format, args);
1313 break;
1314 }
1315
1316 va_end(args);
1317 return LDPS_OK;
1318 }
1319
1320 #endif // ENABLE_PLUGINS
1321
1322 // Allocate a Pluginobj object of the appropriate size and endianness.
1323
1324 static Pluginobj*
1325 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1326 {
1327 Pluginobj* obj = NULL;
1328
1329 parameters_force_valid_target();
1330 const Target& target(parameters->target());
1331
1332 if (target.get_size() == 32)
1333 {
1334 if (target.is_big_endian())
1335 #ifdef HAVE_TARGET_32_BIG
1336 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1337 input_file, offset, filesize);
1338 #else
1339 gold_error(_("%s: not configured to support "
1340 "32-bit big-endian object"),
1341 input_file->filename().c_str());
1342 #endif
1343 else
1344 #ifdef HAVE_TARGET_32_LITTLE
1345 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1346 input_file, offset, filesize);
1347 #else
1348 gold_error(_("%s: not configured to support "
1349 "32-bit little-endian object"),
1350 input_file->filename().c_str());
1351 #endif
1352 }
1353 else if (target.get_size() == 64)
1354 {
1355 if (target.is_big_endian())
1356 #ifdef HAVE_TARGET_64_BIG
1357 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1358 input_file, offset, filesize);
1359 #else
1360 gold_error(_("%s: not configured to support "
1361 "64-bit big-endian object"),
1362 input_file->filename().c_str());
1363 #endif
1364 else
1365 #ifdef HAVE_TARGET_64_LITTLE
1366 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1367 input_file, offset, filesize);
1368 #else
1369 gold_error(_("%s: not configured to support "
1370 "64-bit little-endian object"),
1371 input_file->filename().c_str());
1372 #endif
1373 }
1374
1375 gold_assert(obj != NULL);
1376 return obj;
1377 }
1378
1379 } // End namespace gold.
This page took 0.064427 seconds and 5 git commands to generate.