Fix mingw gold build with plugins enabled
[deliverable/binutils-gdb.git] / gold / plugin.cc
1 // plugin.cc -- plugin manager for gold -*- C++ -*-
2
3 // Copyright 2008, 2009, 2010, 2011, 2012, 2013 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 #ifdef HAVE_DLFCN_H
33 #include <dlfcn.h>
34 #elif defined (HAVE_WINDOWS_H)
35 #include <windows.h>
36 #else
37 #error Unknown how to handle dynamic-load-libraries.
38 #endif
39
40 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
41
42 #define RTLD_NOW 0 /* Dummy value. */
43 static void *
44 dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
45 {
46 return LoadLibrary(file);
47 }
48
49 static void *
50 dlsym(void *handle, const char *name)
51 {
52 return reinterpret_cast<void *>(
53 GetProcAddress(static_cast<HMODULE>(handle),name));
54 }
55
56 static const char *
57 dlerror(void)
58 {
59 return "unable to load dll";
60 }
61
62 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
63 #endif /* ENABLE_PLUGINS */
64
65 #include "parameters.h"
66 #include "errors.h"
67 #include "fileread.h"
68 #include "layout.h"
69 #include "options.h"
70 #include "plugin.h"
71 #include "target.h"
72 #include "readsyms.h"
73 #include "symtab.h"
74 #include "elfcpp.h"
75
76 namespace gold
77 {
78
79 #ifdef ENABLE_PLUGINS
80
81 // The linker's exported interfaces.
82
83 extern "C"
84 {
85
86 static enum ld_plugin_status
87 register_claim_file(ld_plugin_claim_file_handler handler);
88
89 static enum ld_plugin_status
90 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
91
92 static enum ld_plugin_status
93 register_cleanup(ld_plugin_cleanup_handler handler);
94
95 static enum ld_plugin_status
96 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
97
98 static enum ld_plugin_status
99 get_input_file(const void *handle, struct ld_plugin_input_file *file);
100
101 static enum ld_plugin_status
102 get_view(const void *handle, const void **viewp);
103
104 static enum ld_plugin_status
105 release_input_file(const void *handle);
106
107 static enum ld_plugin_status
108 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
109
110 static enum ld_plugin_status
111 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
112
113 static enum ld_plugin_status
114 add_input_file(const char *pathname);
115
116 static enum ld_plugin_status
117 add_input_library(const char *pathname);
118
119 static enum ld_plugin_status
120 set_extra_library_path(const char *path);
121
122 static enum ld_plugin_status
123 message(int level, const char *format, ...);
124
125 static enum ld_plugin_status
126 get_input_section_count(const void* handle, unsigned int* count);
127
128 static enum ld_plugin_status
129 get_input_section_type(const struct ld_plugin_section section,
130 unsigned int* type);
131
132 static enum ld_plugin_status
133 get_input_section_name(const struct ld_plugin_section section,
134 char** section_name_ptr);
135
136 static enum ld_plugin_status
137 get_input_section_contents(const struct ld_plugin_section section,
138 const unsigned char** section_contents,
139 size_t* len);
140
141 static enum ld_plugin_status
142 update_section_order(const struct ld_plugin_section *section_list,
143 unsigned int num_sections);
144
145 static enum ld_plugin_status
146 allow_section_ordering();
147
148 static enum ld_plugin_status
149 allow_unique_segment_for_sections();
150
151 static enum ld_plugin_status
152 unique_segment_for_sections(const char* segment_name,
153 uint64_t flags,
154 uint64_t align,
155 const struct ld_plugin_section *section_list,
156 unsigned int num_sections);
157 };
158
159 #endif // ENABLE_PLUGINS
160
161 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
162 off_t offset, off_t filesize);
163
164 // Plugin methods.
165
166 // Load one plugin library.
167
168 void
169 Plugin::load()
170 {
171 #ifdef ENABLE_PLUGINS
172 // Load the plugin library.
173 // FIXME: Look for the library in standard locations.
174 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
175 if (this->handle_ == NULL)
176 {
177 gold_error(_("%s: could not load plugin library: %s"),
178 this->filename_.c_str(), dlerror());
179 return;
180 }
181
182 // Find the plugin's onload entry point.
183 void* ptr = dlsym(this->handle_, "onload");
184 if (ptr == NULL)
185 {
186 gold_error(_("%s: could not find onload entry point"),
187 this->filename_.c_str());
188 return;
189 }
190 ld_plugin_onload onload;
191 gold_assert(sizeof(onload) == sizeof(ptr));
192 memcpy(&onload, &ptr, sizeof(ptr));
193
194 // Get the linker's version number.
195 const char* ver = get_version_string();
196 int major = 0;
197 int minor = 0;
198 sscanf(ver, "%d.%d", &major, &minor);
199
200 // Allocate and populate a transfer vector.
201 const int tv_fixed_size = 26;
202
203 int tv_size = this->args_.size() + tv_fixed_size;
204 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
205
206 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
207 // while processing subsequent entries.
208 int i = 0;
209 tv[i].tv_tag = LDPT_MESSAGE;
210 tv[i].tv_u.tv_message = message;
211
212 ++i;
213 tv[i].tv_tag = LDPT_API_VERSION;
214 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
215
216 ++i;
217 tv[i].tv_tag = LDPT_GOLD_VERSION;
218 tv[i].tv_u.tv_val = major * 100 + minor;
219
220 ++i;
221 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
222 if (parameters->options().relocatable())
223 tv[i].tv_u.tv_val = LDPO_REL;
224 else if (parameters->options().shared())
225 tv[i].tv_u.tv_val = LDPO_DYN;
226 else if (parameters->options().pie())
227 tv[i].tv_u.tv_val = LDPO_PIE;
228 else
229 tv[i].tv_u.tv_val = LDPO_EXEC;
230
231 ++i;
232 tv[i].tv_tag = LDPT_OUTPUT_NAME;
233 tv[i].tv_u.tv_string = parameters->options().output();
234
235 for (unsigned int j = 0; j < this->args_.size(); ++j)
236 {
237 ++i;
238 tv[i].tv_tag = LDPT_OPTION;
239 tv[i].tv_u.tv_string = this->args_[j].c_str();
240 }
241
242 ++i;
243 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
244 tv[i].tv_u.tv_register_claim_file = register_claim_file;
245
246 ++i;
247 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
248 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
249
250 ++i;
251 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
252 tv[i].tv_u.tv_register_cleanup = register_cleanup;
253
254 ++i;
255 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
256 tv[i].tv_u.tv_add_symbols = add_symbols;
257
258 ++i;
259 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
260 tv[i].tv_u.tv_get_input_file = get_input_file;
261
262 ++i;
263 tv[i].tv_tag = LDPT_GET_VIEW;
264 tv[i].tv_u.tv_get_view = get_view;
265
266 ++i;
267 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
268 tv[i].tv_u.tv_release_input_file = release_input_file;
269
270 ++i;
271 tv[i].tv_tag = LDPT_GET_SYMBOLS;
272 tv[i].tv_u.tv_get_symbols = get_symbols;
273
274 ++i;
275 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
276 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
277
278 ++i;
279 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
280 tv[i].tv_u.tv_add_input_file = add_input_file;
281
282 ++i;
283 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
284 tv[i].tv_u.tv_add_input_library = add_input_library;
285
286 ++i;
287 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
288 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
289
290 ++i;
291 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
292 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
293
294 ++i;
295 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
296 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
297
298 ++i;
299 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
300 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
301
302 ++i;
303 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
304 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
305
306 ++i;
307 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
308 tv[i].tv_u.tv_update_section_order = update_section_order;
309
310 ++i;
311 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
312 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
313
314 ++i;
315 tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
316 tv[i].tv_u.tv_allow_unique_segment_for_sections
317 = allow_unique_segment_for_sections;
318
319 ++i;
320 tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
321 tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
322
323 ++i;
324 tv[i].tv_tag = LDPT_NULL;
325 tv[i].tv_u.tv_val = 0;
326
327 gold_assert(i == tv_size - 1);
328
329 // Call the onload entry point.
330 (*onload)(tv);
331
332 delete[] tv;
333 #endif // ENABLE_PLUGINS
334 }
335
336 // Call the plugin claim-file handler.
337
338 inline bool
339 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
340 {
341 int claimed = 0;
342
343 if (this->claim_file_handler_ != NULL)
344 {
345 (*this->claim_file_handler_)(plugin_input_file, &claimed);
346 if (claimed)
347 return true;
348 }
349 return false;
350 }
351
352 // Call the all-symbols-read handler.
353
354 inline void
355 Plugin::all_symbols_read()
356 {
357 if (this->all_symbols_read_handler_ != NULL)
358 (*this->all_symbols_read_handler_)();
359 }
360
361 // Call the cleanup handler.
362
363 inline void
364 Plugin::cleanup()
365 {
366 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
367 {
368 // Set this flag before calling to prevent a recursive plunge
369 // in the event that a plugin's cleanup handler issues a
370 // fatal error.
371 this->cleanup_done_ = true;
372 (*this->cleanup_handler_)();
373 }
374 }
375
376 // This task is used to rescan archives as needed.
377
378 class Plugin_rescan : public Task
379 {
380 public:
381 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
382 : this_blocker_(this_blocker), next_blocker_(next_blocker)
383 { }
384
385 ~Plugin_rescan()
386 {
387 delete this->this_blocker_;
388 }
389
390 Task_token*
391 is_runnable()
392 {
393 if (this->this_blocker_->is_blocked())
394 return this->this_blocker_;
395 return NULL;
396 }
397
398 void
399 locks(Task_locker* tl)
400 { tl->add(this, this->next_blocker_); }
401
402 void
403 run(Workqueue*)
404 { parameters->options().plugins()->rescan(this); }
405
406 std::string
407 get_name() const
408 { return "Plugin_rescan"; }
409
410 private:
411 Task_token* this_blocker_;
412 Task_token* next_blocker_;
413 };
414
415 // Plugin_manager methods.
416
417 Plugin_manager::~Plugin_manager()
418 {
419 for (Plugin_list::iterator p = this->plugins_.begin();
420 p != this->plugins_.end();
421 ++p)
422 delete *p;
423 this->plugins_.clear();
424 for (Object_list::iterator obj = this->objects_.begin();
425 obj != this->objects_.end();
426 ++obj)
427 delete *obj;
428 this->objects_.clear();
429 }
430
431 // Load all plugin libraries.
432
433 void
434 Plugin_manager::load_plugins(Layout* layout)
435 {
436 this->layout_ = layout;
437 for (this->current_ = this->plugins_.begin();
438 this->current_ != this->plugins_.end();
439 ++this->current_)
440 (*this->current_)->load();
441 }
442
443 // Call the plugin claim-file handlers in turn to see if any claim the file.
444
445 Pluginobj*
446 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
447 off_t filesize, Object* elf_object)
448 {
449 if (this->in_replacement_phase_)
450 return NULL;
451
452 unsigned int handle = this->objects_.size();
453 this->input_file_ = input_file;
454 this->plugin_input_file_.name = input_file->filename().c_str();
455 this->plugin_input_file_.fd = input_file->file().descriptor();
456 this->plugin_input_file_.offset = offset;
457 this->plugin_input_file_.filesize = filesize;
458 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
459 if (elf_object != NULL)
460 this->objects_.push_back(elf_object);
461 this->in_claim_file_handler_ = true;
462
463 for (this->current_ = this->plugins_.begin();
464 this->current_ != this->plugins_.end();
465 ++this->current_)
466 {
467 if ((*this->current_)->claim_file(&this->plugin_input_file_))
468 {
469 this->any_claimed_ = true;
470 this->in_claim_file_handler_ = false;
471
472 if (this->objects_.size() > handle
473 && this->objects_[handle]->pluginobj() != NULL)
474 return this->objects_[handle]->pluginobj();
475
476 // If the plugin claimed the file but did not call the
477 // add_symbols callback, we need to create the Pluginobj now.
478 Pluginobj* obj = this->make_plugin_object(handle);
479 return obj;
480 }
481 }
482
483 this->in_claim_file_handler_ = false;
484 return NULL;
485 }
486
487 // Save an archive. This is used so that a plugin can add a file
488 // which refers to a symbol which was not previously referenced. In
489 // that case we want to pretend that the symbol was referenced before,
490 // and pull in the archive object.
491
492 void
493 Plugin_manager::save_archive(Archive* archive)
494 {
495 if (this->in_replacement_phase_ || !this->any_claimed_)
496 delete archive;
497 else
498 this->rescannable_.push_back(Rescannable(archive));
499 }
500
501 // Save an Input_group. This is like save_archive.
502
503 void
504 Plugin_manager::save_input_group(Input_group* input_group)
505 {
506 if (this->in_replacement_phase_ || !this->any_claimed_)
507 delete input_group;
508 else
509 this->rescannable_.push_back(Rescannable(input_group));
510 }
511
512 // Call the all-symbols-read handlers.
513
514 void
515 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
516 Input_objects* input_objects,
517 Symbol_table* symtab,
518 Dirsearch* dirpath, Mapfile* mapfile,
519 Task_token** last_blocker)
520 {
521 this->in_replacement_phase_ = true;
522 this->workqueue_ = workqueue;
523 this->task_ = task;
524 this->input_objects_ = input_objects;
525 this->symtab_ = symtab;
526 this->dirpath_ = dirpath;
527 this->mapfile_ = mapfile;
528 this->this_blocker_ = NULL;
529
530 for (this->current_ = this->plugins_.begin();
531 this->current_ != this->plugins_.end();
532 ++this->current_)
533 (*this->current_)->all_symbols_read();
534
535 if (this->any_added_)
536 {
537 Task_token* next_blocker = new Task_token(true);
538 next_blocker->add_blocker();
539 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
540 this->this_blocker_ = next_blocker;
541 }
542
543 *last_blocker = this->this_blocker_;
544 }
545
546 // This is called when we see a new undefined symbol. If we are in
547 // the replacement phase, this means that we may need to rescan some
548 // archives we have previously seen.
549
550 void
551 Plugin_manager::new_undefined_symbol(Symbol* sym)
552 {
553 if (this->in_replacement_phase_)
554 this->undefined_symbols_.push_back(sym);
555 }
556
557 // Rescan archives as needed. This handles the case where a new
558 // object file added by a plugin has an undefined reference to some
559 // symbol defined in an archive.
560
561 void
562 Plugin_manager::rescan(Task* task)
563 {
564 size_t rescan_pos = 0;
565 size_t rescan_size = this->rescannable_.size();
566 while (!this->undefined_symbols_.empty())
567 {
568 if (rescan_pos >= rescan_size)
569 {
570 this->undefined_symbols_.clear();
571 return;
572 }
573
574 Undefined_symbol_list undefs;
575 undefs.reserve(this->undefined_symbols_.size());
576 this->undefined_symbols_.swap(undefs);
577
578 size_t min_rescan_pos = rescan_size;
579
580 for (Undefined_symbol_list::const_iterator p = undefs.begin();
581 p != undefs.end();
582 ++p)
583 {
584 if (!(*p)->is_undefined())
585 continue;
586
587 this->undefined_symbols_.push_back(*p);
588
589 // Find the first rescan archive which defines this symbol,
590 // starting at the current rescan position. The rescan position
591 // exists so that given -la -lb -lc we don't look for undefined
592 // symbols in -lb back in -la, but instead get the definition
593 // from -lc. Don't bother to look past the current minimum
594 // rescan position.
595 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
596 {
597 if (this->rescannable_defines(i, *p))
598 {
599 min_rescan_pos = i;
600 break;
601 }
602 }
603 }
604
605 if (min_rescan_pos >= rescan_size)
606 {
607 // We didn't find any rescannable archives which define any
608 // undefined symbols.
609 return;
610 }
611
612 const Rescannable& r(this->rescannable_[min_rescan_pos]);
613 if (r.is_archive)
614 {
615 Task_lock_obj<Archive> tl(task, r.u.archive);
616 r.u.archive->add_symbols(this->symtab_, this->layout_,
617 this->input_objects_, this->mapfile_);
618 }
619 else
620 {
621 size_t next_saw_undefined = this->symtab_->saw_undefined();
622 size_t saw_undefined;
623 do
624 {
625 saw_undefined = next_saw_undefined;
626
627 for (Input_group::const_iterator p = r.u.input_group->begin();
628 p != r.u.input_group->end();
629 ++p)
630 {
631 Task_lock_obj<Archive> tl(task, *p);
632
633 (*p)->add_symbols(this->symtab_, this->layout_,
634 this->input_objects_, this->mapfile_);
635 }
636
637 next_saw_undefined = this->symtab_->saw_undefined();
638 }
639 while (saw_undefined != next_saw_undefined);
640 }
641
642 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
643 {
644 if (this->rescannable_[i].is_archive)
645 delete this->rescannable_[i].u.archive;
646 else
647 delete this->rescannable_[i].u.input_group;
648 }
649
650 rescan_pos = min_rescan_pos + 1;
651 }
652 }
653
654 // Return whether the rescannable at index I defines SYM.
655
656 bool
657 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
658 {
659 const Rescannable& r(this->rescannable_[i]);
660 if (r.is_archive)
661 return r.u.archive->defines_symbol(sym);
662 else
663 {
664 for (Input_group::const_iterator p = r.u.input_group->begin();
665 p != r.u.input_group->end();
666 ++p)
667 {
668 if ((*p)->defines_symbol(sym))
669 return true;
670 }
671 return false;
672 }
673 }
674
675 // Layout deferred objects.
676
677 void
678 Plugin_manager::layout_deferred_objects()
679 {
680 Deferred_layout_list::iterator obj;
681
682 for (obj = this->deferred_layout_objects_.begin();
683 obj != this->deferred_layout_objects_.end();
684 ++obj)
685 {
686 // Lock the object so we can read from it. This is only called
687 // single-threaded from queue_middle_tasks, so it is OK to lock.
688 // Unfortunately we have no way to pass in a Task token.
689 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
690 Task_lock_obj<Object> tl(dummy_task, *obj);
691 (*obj)->layout_deferred_sections(this->layout_);
692 }
693 }
694
695 // Call the cleanup handlers.
696
697 void
698 Plugin_manager::cleanup()
699 {
700 for (this->current_ = this->plugins_.begin();
701 this->current_ != this->plugins_.end();
702 ++this->current_)
703 (*this->current_)->cleanup();
704 }
705
706 // Make a new Pluginobj object. This is called when the plugin calls
707 // the add_symbols API.
708
709 Pluginobj*
710 Plugin_manager::make_plugin_object(unsigned int handle)
711 {
712 // Make sure we aren't asked to make an object for the same handle twice.
713 if (this->objects_.size() != handle
714 && this->objects_[handle]->pluginobj() != NULL)
715 return NULL;
716
717 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
718 this->plugin_input_file_.offset,
719 this->plugin_input_file_.filesize);
720
721
722 // If the elf object for this file was pushed into the objects_ vector, delete
723 // it to make room for the Pluginobj as this file is claimed.
724 if (this->objects_.size() != handle)
725 this->objects_.pop_back();
726
727 this->objects_.push_back(obj);
728 return obj;
729 }
730
731 // Get the input file information with an open (possibly re-opened)
732 // file descriptor.
733
734 ld_plugin_status
735 Plugin_manager::get_input_file(unsigned int handle,
736 struct ld_plugin_input_file* file)
737 {
738 Pluginobj* obj = this->object(handle)->pluginobj();
739 if (obj == NULL)
740 return LDPS_BAD_HANDLE;
741
742 obj->lock(this->task_);
743 file->name = obj->filename().c_str();
744 file->fd = obj->descriptor();
745 file->offset = obj->offset();
746 file->filesize = obj->filesize();
747 file->handle = reinterpret_cast<void*>(handle);
748 return LDPS_OK;
749 }
750
751 // Release the input file.
752
753 ld_plugin_status
754 Plugin_manager::release_input_file(unsigned int handle)
755 {
756 if (this->object(handle) == NULL)
757 return LDPS_BAD_HANDLE;
758
759 Pluginobj* obj = this->object(handle)->pluginobj();
760
761 if (obj == NULL)
762 return LDPS_BAD_HANDLE;
763
764 obj->unlock(this->task_);
765 return LDPS_OK;
766 }
767
768 // Get the elf object corresponding to the handle. Return NULL if we
769 // found a Pluginobj instead.
770
771 Object*
772 Plugin_manager::get_elf_object(const void* handle)
773 {
774 Object* obj = this->object(
775 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
776
777 // The object should not be a Pluginobj.
778 if (obj == NULL
779 || obj->pluginobj() != NULL)
780 return NULL;
781
782 return obj;
783 }
784
785 ld_plugin_status
786 Plugin_manager::get_view(unsigned int handle, const void **viewp)
787 {
788 off_t offset;
789 size_t filesize;
790 Input_file *input_file;
791 if (this->in_claim_file_handler_)
792 {
793 // We are being called from the claim_file hook.
794 const struct ld_plugin_input_file &f = this->plugin_input_file_;
795 offset = f.offset;
796 filesize = f.filesize;
797 input_file = this->input_file_;
798 }
799 else
800 {
801 // An already claimed file.
802 if (this->object(handle) == NULL)
803 return LDPS_BAD_HANDLE;
804 Pluginobj* obj = this->object(handle)->pluginobj();
805 if (obj == NULL)
806 return LDPS_BAD_HANDLE;
807 offset = obj->offset();
808 filesize = obj->filesize();
809 input_file = obj->input_file();
810 }
811 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
812 false);
813 return LDPS_OK;
814 }
815
816 // Add a new library path.
817
818 ld_plugin_status
819 Plugin_manager::set_extra_library_path(const char* path)
820 {
821 this->extra_search_path_ = std::string(path);
822 return LDPS_OK;
823 }
824
825 // Add a new input file.
826
827 ld_plugin_status
828 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
829 {
830 Input_file_argument file(pathname,
831 (is_lib
832 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
833 : Input_file_argument::INPUT_FILE_TYPE_FILE),
834 (is_lib
835 ? this->extra_search_path_.c_str()
836 : ""),
837 false,
838 this->options_);
839 Input_argument* input_argument = new Input_argument(file);
840 Task_token* next_blocker = new Task_token(true);
841 next_blocker->add_blocker();
842 if (parameters->incremental())
843 gold_error(_("input files added by plug-ins in --incremental mode not "
844 "supported yet"));
845 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
846 this->symtab_,
847 this->layout_,
848 this->dirpath_,
849 0,
850 this->mapfile_,
851 input_argument,
852 NULL,
853 NULL,
854 this->this_blocker_,
855 next_blocker));
856 this->this_blocker_ = next_blocker;
857 this->any_added_ = true;
858 return LDPS_OK;
859 }
860
861 // Class Pluginobj.
862
863 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
864 off_t offset, off_t filesize)
865 : Object(name, input_file, false, offset),
866 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
867 {
868 }
869
870 // Return TRUE if a defined symbol is referenced from outside the
871 // universe of claimed objects. Only references from relocatable,
872 // non-IR (unclaimed) objects count as a reference. References from
873 // dynamic objects count only as "visible".
874
875 static inline bool
876 is_referenced_from_outside(Symbol* lsym)
877 {
878 if (lsym->in_real_elf())
879 return true;
880 if (parameters->options().relocatable())
881 return true;
882 if (parameters->options().is_undefined(lsym->name()))
883 return true;
884 return false;
885 }
886
887 // Return TRUE if a defined symbol might be reachable from outside the
888 // load module.
889
890 static inline bool
891 is_visible_from_outside(Symbol* lsym)
892 {
893 if (lsym->in_dyn())
894 return true;
895 if (parameters->options().export_dynamic() || parameters->options().shared())
896 return lsym->is_externally_visible();
897 return false;
898 }
899
900 // Get symbol resolution info.
901
902 ld_plugin_status
903 Pluginobj::get_symbol_resolution_info(int nsyms,
904 ld_plugin_symbol* syms,
905 int version) const
906 {
907 // For version 1 of this interface, we cannot use
908 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
909 // instead.
910 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
911 = (version > 1
912 ? LDPR_PREVAILING_DEF_IRONLY_EXP
913 : LDPR_PREVAILING_DEF);
914
915 if (nsyms > this->nsyms_)
916 return LDPS_NO_SYMS;
917
918 if (static_cast<size_t>(nsyms) > this->symbols_.size())
919 {
920 // We never decided to include this object. We mark all symbols as
921 // preempted.
922 gold_assert(this->symbols_.size() == 0);
923 for (int i = 0; i < nsyms; i++)
924 syms[i].resolution = LDPR_PREEMPTED_REG;
925 return LDPS_OK;
926 }
927
928 for (int i = 0; i < nsyms; i++)
929 {
930 ld_plugin_symbol* isym = &syms[i];
931 Symbol* lsym = this->symbols_[i];
932 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
933
934 if (lsym->is_undefined())
935 // The symbol remains undefined.
936 res = LDPR_UNDEF;
937 else if (isym->def == LDPK_UNDEF
938 || isym->def == LDPK_WEAKUNDEF
939 || isym->def == LDPK_COMMON)
940 {
941 // The original symbol was undefined or common.
942 if (lsym->source() != Symbol::FROM_OBJECT)
943 res = LDPR_RESOLVED_EXEC;
944 else if (lsym->object()->pluginobj() == this)
945 {
946 if (is_referenced_from_outside(lsym))
947 res = LDPR_PREVAILING_DEF;
948 else if (is_visible_from_outside(lsym))
949 res = ldpr_prevailing_def_ironly_exp;
950 else
951 res = LDPR_PREVAILING_DEF_IRONLY;
952 }
953 else if (lsym->object()->pluginobj() != NULL)
954 res = LDPR_RESOLVED_IR;
955 else if (lsym->object()->is_dynamic())
956 res = LDPR_RESOLVED_DYN;
957 else
958 res = LDPR_RESOLVED_EXEC;
959 }
960 else
961 {
962 // The original symbol was a definition.
963 if (lsym->source() != Symbol::FROM_OBJECT)
964 res = LDPR_PREEMPTED_REG;
965 else if (lsym->object() == static_cast<const Object*>(this))
966 {
967 if (is_referenced_from_outside(lsym))
968 res = LDPR_PREVAILING_DEF;
969 else if (is_visible_from_outside(lsym))
970 res = ldpr_prevailing_def_ironly_exp;
971 else
972 res = LDPR_PREVAILING_DEF_IRONLY;
973 }
974 else
975 res = (lsym->object()->pluginobj() != NULL
976 ? LDPR_PREEMPTED_IR
977 : LDPR_PREEMPTED_REG);
978 }
979 isym->resolution = res;
980 }
981 return LDPS_OK;
982 }
983
984 // Return TRUE if the comdat group with key COMDAT_KEY from this object
985 // should be kept.
986
987 bool
988 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
989 {
990 std::pair<Comdat_map::iterator, bool> ins =
991 this->comdat_map_.insert(std::make_pair(comdat_key, false));
992
993 // If this is the first time we've seen this comdat key, ask the
994 // layout object whether it should be included.
995 if (ins.second)
996 ins.first->second = layout->find_or_add_kept_section(comdat_key,
997 NULL, 0, true,
998 true, NULL);
999
1000 return ins.first->second;
1001 }
1002
1003 // Class Sized_pluginobj.
1004
1005 template<int size, bool big_endian>
1006 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1007 const std::string& name,
1008 Input_file* input_file,
1009 off_t offset,
1010 off_t filesize)
1011 : Pluginobj(name, input_file, offset, filesize)
1012 {
1013 }
1014
1015 // Read the symbols. Not used for plugin objects.
1016
1017 template<int size, bool big_endian>
1018 void
1019 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1020 {
1021 gold_unreachable();
1022 }
1023
1024 // Lay out the input sections. Not used for plugin objects.
1025
1026 template<int size, bool big_endian>
1027 void
1028 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1029 Read_symbols_data*)
1030 {
1031 gold_unreachable();
1032 }
1033
1034 // Add the symbols to the symbol table.
1035
1036 template<int size, bool big_endian>
1037 void
1038 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1039 Read_symbols_data*,
1040 Layout* layout)
1041 {
1042 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1043 unsigned char symbuf[sym_size];
1044 elfcpp::Sym<size, big_endian> sym(symbuf);
1045 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1046
1047 typedef typename elfcpp::Elf_types<size>::Elf_WXword Elf_size_type;
1048
1049 this->symbols_.resize(this->nsyms_);
1050
1051 for (int i = 0; i < this->nsyms_; ++i)
1052 {
1053 const struct ld_plugin_symbol* isym = &this->syms_[i];
1054 const char* name = isym->name;
1055 const char* ver = isym->version;
1056 elfcpp::Elf_Half shndx;
1057 elfcpp::STB bind;
1058 elfcpp::STV vis;
1059
1060 if (name != NULL && name[0] == '\0')
1061 name = NULL;
1062 if (ver != NULL && ver[0] == '\0')
1063 ver = NULL;
1064
1065 switch (isym->def)
1066 {
1067 case LDPK_WEAKDEF:
1068 case LDPK_WEAKUNDEF:
1069 bind = elfcpp::STB_WEAK;
1070 break;
1071 case LDPK_DEF:
1072 case LDPK_UNDEF:
1073 case LDPK_COMMON:
1074 default:
1075 bind = elfcpp::STB_GLOBAL;
1076 break;
1077 }
1078
1079 switch (isym->def)
1080 {
1081 case LDPK_DEF:
1082 case LDPK_WEAKDEF:
1083 shndx = elfcpp::SHN_ABS;
1084 break;
1085 case LDPK_COMMON:
1086 shndx = elfcpp::SHN_COMMON;
1087 break;
1088 case LDPK_UNDEF:
1089 case LDPK_WEAKUNDEF:
1090 default:
1091 shndx = elfcpp::SHN_UNDEF;
1092 break;
1093 }
1094
1095 switch (isym->visibility)
1096 {
1097 case LDPV_PROTECTED:
1098 vis = elfcpp::STV_PROTECTED;
1099 break;
1100 case LDPV_INTERNAL:
1101 vis = elfcpp::STV_INTERNAL;
1102 break;
1103 case LDPV_HIDDEN:
1104 vis = elfcpp::STV_HIDDEN;
1105 break;
1106 case LDPV_DEFAULT:
1107 default:
1108 vis = elfcpp::STV_DEFAULT;
1109 break;
1110 }
1111
1112 if (isym->comdat_key != NULL
1113 && isym->comdat_key[0] != '\0'
1114 && !this->include_comdat_group(isym->comdat_key, layout))
1115 shndx = elfcpp::SHN_UNDEF;
1116
1117 osym.put_st_name(0);
1118 osym.put_st_value(0);
1119 osym.put_st_size(static_cast<Elf_size_type>(isym->size));
1120 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1121 osym.put_st_other(vis, 0);
1122 osym.put_st_shndx(shndx);
1123
1124 this->symbols_[i] =
1125 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1126 }
1127 }
1128
1129 template<int size, bool big_endian>
1130 Archive::Should_include
1131 Sized_pluginobj<size, big_endian>::do_should_include_member(
1132 Symbol_table* symtab,
1133 Layout* layout,
1134 Read_symbols_data*,
1135 std::string* why)
1136 {
1137 char* tmpbuf = NULL;
1138 size_t tmpbuflen = 0;
1139
1140 for (int i = 0; i < this->nsyms_; ++i)
1141 {
1142 const struct ld_plugin_symbol& sym = this->syms_[i];
1143 const char* name = sym.name;
1144 Symbol* symbol;
1145 Archive::Should_include t = Archive::should_include_member(symtab,
1146 layout,
1147 name,
1148 &symbol, why,
1149 &tmpbuf,
1150 &tmpbuflen);
1151 if (t == Archive::SHOULD_INCLUDE_YES)
1152 {
1153 if (tmpbuf != NULL)
1154 free(tmpbuf);
1155 return t;
1156 }
1157 }
1158 if (tmpbuf != NULL)
1159 free(tmpbuf);
1160 return Archive::SHOULD_INCLUDE_UNKNOWN;
1161 }
1162
1163 // Iterate over global symbols, calling a visitor class V for each.
1164
1165 template<int size, bool big_endian>
1166 void
1167 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1168 Read_symbols_data*,
1169 Library_base::Symbol_visitor_base* v)
1170 {
1171 for (int i = 0; i < this->nsyms_; ++i)
1172 {
1173 const struct ld_plugin_symbol& sym = this->syms_[i];
1174 if (sym.def != LDPK_UNDEF)
1175 v->visit(sym.name);
1176 }
1177 }
1178
1179 // Iterate over local symbols, calling a visitor class V for each GOT offset
1180 // associated with a local symbol.
1181 template<int size, bool big_endian>
1182 void
1183 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1184 Got_offset_list::Visitor*) const
1185 {
1186 gold_unreachable();
1187 }
1188
1189 // Get the size of a section. Not used for plugin objects.
1190
1191 template<int size, bool big_endian>
1192 uint64_t
1193 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1194 {
1195 gold_unreachable();
1196 return 0;
1197 }
1198
1199 // Get the name of a section. Not used for plugin objects.
1200
1201 template<int size, bool big_endian>
1202 std::string
1203 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int)
1204 {
1205 gold_unreachable();
1206 return std::string();
1207 }
1208
1209 // Return a view of the contents of a section. Not used for plugin objects.
1210
1211 template<int size, bool big_endian>
1212 const unsigned char*
1213 Sized_pluginobj<size, big_endian>::do_section_contents(
1214 unsigned int,
1215 section_size_type*,
1216 bool)
1217 {
1218 gold_unreachable();
1219 return NULL;
1220 }
1221
1222 // Return section flags. Not used for plugin objects.
1223
1224 template<int size, bool big_endian>
1225 uint64_t
1226 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1227 {
1228 gold_unreachable();
1229 return 0;
1230 }
1231
1232 // Return section entsize. Not used for plugin objects.
1233
1234 template<int size, bool big_endian>
1235 uint64_t
1236 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1237 {
1238 gold_unreachable();
1239 return 0;
1240 }
1241
1242 // Return section address. Not used for plugin objects.
1243
1244 template<int size, bool big_endian>
1245 uint64_t
1246 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1247 {
1248 gold_unreachable();
1249 return 0;
1250 }
1251
1252 // Return section type. Not used for plugin objects.
1253
1254 template<int size, bool big_endian>
1255 unsigned int
1256 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1257 {
1258 gold_unreachable();
1259 return 0;
1260 }
1261
1262 // Return the section link field. Not used for plugin objects.
1263
1264 template<int size, bool big_endian>
1265 unsigned int
1266 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1267 {
1268 gold_unreachable();
1269 return 0;
1270 }
1271
1272 // Return the section link field. Not used for plugin objects.
1273
1274 template<int size, bool big_endian>
1275 unsigned int
1276 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1277 {
1278 gold_unreachable();
1279 return 0;
1280 }
1281
1282 // Return the section alignment. Not used for plugin objects.
1283
1284 template<int size, bool big_endian>
1285 uint64_t
1286 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1287 {
1288 gold_unreachable();
1289 return 0;
1290 }
1291
1292 // Return the Xindex structure to use. Not used for plugin objects.
1293
1294 template<int size, bool big_endian>
1295 Xindex*
1296 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1297 {
1298 gold_unreachable();
1299 return NULL;
1300 }
1301
1302 // Get symbol counts. Don't count plugin objects; the replacement
1303 // files will provide the counts.
1304
1305 template<int size, bool big_endian>
1306 void
1307 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1308 const Symbol_table*,
1309 size_t* defined,
1310 size_t* used) const
1311 {
1312 *defined = 0;
1313 *used = 0;
1314 }
1315
1316 // Get symbols. Not used for plugin objects.
1317
1318 template<int size, bool big_endian>
1319 const Object::Symbols*
1320 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1321 {
1322 gold_unreachable();
1323 }
1324
1325 // Class Plugin_finish. This task runs after all replacement files have
1326 // been added. For now, it's a placeholder for a possible plugin API
1327 // to allow the plugin to release most of its resources. The cleanup
1328 // handlers must be called later, because they can remove the temporary
1329 // object files that are needed until the end of the link.
1330
1331 class Plugin_finish : public Task
1332 {
1333 public:
1334 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1335 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1336 { }
1337
1338 ~Plugin_finish()
1339 {
1340 if (this->this_blocker_ != NULL)
1341 delete this->this_blocker_;
1342 }
1343
1344 Task_token*
1345 is_runnable()
1346 {
1347 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1348 return this->this_blocker_;
1349 return NULL;
1350 }
1351
1352 void
1353 locks(Task_locker* tl)
1354 { tl->add(this, this->next_blocker_); }
1355
1356 void
1357 run(Workqueue*)
1358 {
1359 // We could call early cleanup handlers here.
1360 }
1361
1362 std::string
1363 get_name() const
1364 { return "Plugin_finish"; }
1365
1366 private:
1367 Task_token* this_blocker_;
1368 Task_token* next_blocker_;
1369 };
1370
1371 // Class Plugin_hook.
1372
1373 Plugin_hook::~Plugin_hook()
1374 {
1375 }
1376
1377 // Return whether a Plugin_hook task is runnable.
1378
1379 Task_token*
1380 Plugin_hook::is_runnable()
1381 {
1382 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1383 return this->this_blocker_;
1384 return NULL;
1385 }
1386
1387 // Return a Task_locker for a Plugin_hook task. We don't need any
1388 // locks here.
1389
1390 void
1391 Plugin_hook::locks(Task_locker*)
1392 {
1393 }
1394
1395 // Run the "all symbols read" plugin hook.
1396
1397 void
1398 Plugin_hook::run(Workqueue* workqueue)
1399 {
1400 gold_assert(this->options_.has_plugins());
1401 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1402 if (start_sym != NULL)
1403 start_sym->set_in_real_elf();
1404
1405 this->options_.plugins()->all_symbols_read(workqueue,
1406 this,
1407 this->input_objects_,
1408 this->symtab_,
1409 this->dirpath_,
1410 this->mapfile_,
1411 &this->this_blocker_);
1412 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1413 this->next_blocker_));
1414 }
1415
1416 // The C interface routines called by the plugins.
1417
1418 #ifdef ENABLE_PLUGINS
1419
1420 // Register a claim-file handler.
1421
1422 static enum ld_plugin_status
1423 register_claim_file(ld_plugin_claim_file_handler handler)
1424 {
1425 gold_assert(parameters->options().has_plugins());
1426 parameters->options().plugins()->set_claim_file_handler(handler);
1427 return LDPS_OK;
1428 }
1429
1430 // Register an all-symbols-read handler.
1431
1432 static enum ld_plugin_status
1433 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1434 {
1435 gold_assert(parameters->options().has_plugins());
1436 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1437 return LDPS_OK;
1438 }
1439
1440 // Register a cleanup handler.
1441
1442 static enum ld_plugin_status
1443 register_cleanup(ld_plugin_cleanup_handler handler)
1444 {
1445 gold_assert(parameters->options().has_plugins());
1446 parameters->options().plugins()->set_cleanup_handler(handler);
1447 return LDPS_OK;
1448 }
1449
1450 // Add symbols from a plugin-claimed input file.
1451
1452 static enum ld_plugin_status
1453 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1454 {
1455 gold_assert(parameters->options().has_plugins());
1456 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1457 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1458 if (obj == NULL)
1459 return LDPS_ERR;
1460 obj->store_incoming_symbols(nsyms, syms);
1461 return LDPS_OK;
1462 }
1463
1464 // Get the input file information with an open (possibly re-opened)
1465 // file descriptor.
1466
1467 static enum ld_plugin_status
1468 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1469 {
1470 gold_assert(parameters->options().has_plugins());
1471 unsigned int obj_index =
1472 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1473 return parameters->options().plugins()->get_input_file(obj_index, file);
1474 }
1475
1476 // Release the input file.
1477
1478 static enum ld_plugin_status
1479 release_input_file(const void* handle)
1480 {
1481 gold_assert(parameters->options().has_plugins());
1482 unsigned int obj_index =
1483 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1484 return parameters->options().plugins()->release_input_file(obj_index);
1485 }
1486
1487 static enum ld_plugin_status
1488 get_view(const void *handle, const void **viewp)
1489 {
1490 gold_assert(parameters->options().has_plugins());
1491 unsigned int obj_index =
1492 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1493 return parameters->options().plugins()->get_view(obj_index, viewp);
1494 }
1495
1496 // Get the symbol resolution info for a plugin-claimed input file.
1497
1498 static enum ld_plugin_status
1499 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1500 {
1501 gold_assert(parameters->options().has_plugins());
1502 Object* obj = parameters->options().plugins()->object(
1503 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1504 if (obj == NULL)
1505 return LDPS_ERR;
1506 Pluginobj* plugin_obj = obj->pluginobj();
1507 if (plugin_obj == NULL)
1508 return LDPS_ERR;
1509 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 1);
1510 }
1511
1512 // Version 2 of the above. The only difference is that this version
1513 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1514
1515 static enum ld_plugin_status
1516 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1517 {
1518 gold_assert(parameters->options().has_plugins());
1519 Object* obj = parameters->options().plugins()->object(
1520 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1521 if (obj == NULL)
1522 return LDPS_ERR;
1523 Pluginobj* plugin_obj = obj->pluginobj();
1524 if (plugin_obj == NULL)
1525 return LDPS_ERR;
1526 return plugin_obj->get_symbol_resolution_info(nsyms, syms, 2);
1527 }
1528
1529 // Add a new (real) input file generated by a plugin.
1530
1531 static enum ld_plugin_status
1532 add_input_file(const char* pathname)
1533 {
1534 gold_assert(parameters->options().has_plugins());
1535 return parameters->options().plugins()->add_input_file(pathname, false);
1536 }
1537
1538 // Add a new (real) library required by a plugin.
1539
1540 static enum ld_plugin_status
1541 add_input_library(const char* pathname)
1542 {
1543 gold_assert(parameters->options().has_plugins());
1544 return parameters->options().plugins()->add_input_file(pathname, true);
1545 }
1546
1547 // Set the extra library path to be used by libraries added via
1548 // add_input_library
1549
1550 static enum ld_plugin_status
1551 set_extra_library_path(const char* path)
1552 {
1553 gold_assert(parameters->options().has_plugins());
1554 return parameters->options().plugins()->set_extra_library_path(path);
1555 }
1556
1557 // Issue a diagnostic message from a plugin.
1558
1559 static enum ld_plugin_status
1560 message(int level, const char* format, ...)
1561 {
1562 va_list args;
1563 va_start(args, format);
1564
1565 switch (level)
1566 {
1567 case LDPL_INFO:
1568 parameters->errors()->info(format, args);
1569 break;
1570 case LDPL_WARNING:
1571 parameters->errors()->warning(format, args);
1572 break;
1573 case LDPL_ERROR:
1574 default:
1575 parameters->errors()->error(format, args);
1576 break;
1577 case LDPL_FATAL:
1578 parameters->errors()->fatal(format, args);
1579 break;
1580 }
1581
1582 va_end(args);
1583 return LDPS_OK;
1584 }
1585
1586 // Get the section count of the object corresponding to the handle. This
1587 // plugin interface can only be called in the claim_file handler of the plugin.
1588
1589 static enum ld_plugin_status
1590 get_input_section_count(const void* handle, unsigned int* count)
1591 {
1592 gold_assert(parameters->options().has_plugins());
1593
1594 if (!parameters->options().plugins()->in_claim_file_handler())
1595 return LDPS_ERR;
1596
1597 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1598
1599 if (obj == NULL)
1600 return LDPS_ERR;
1601
1602 *count = obj->shnum();
1603 return LDPS_OK;
1604 }
1605
1606 // Get the type of the specified section in the object corresponding
1607 // to the handle. This plugin interface can only be called in the
1608 // claim_file handler of the plugin.
1609
1610 static enum ld_plugin_status
1611 get_input_section_type(const struct ld_plugin_section section,
1612 unsigned int* type)
1613 {
1614 gold_assert(parameters->options().has_plugins());
1615
1616 if (!parameters->options().plugins()->in_claim_file_handler())
1617 return LDPS_ERR;
1618
1619 Object* obj
1620 = parameters->options().plugins()->get_elf_object(section.handle);
1621
1622 if (obj == NULL)
1623 return LDPS_BAD_HANDLE;
1624
1625 *type = obj->section_type(section.shndx);
1626 return LDPS_OK;
1627 }
1628
1629 // Get the name of the specified section in the object corresponding
1630 // to the handle. This plugin interface can only be called in the
1631 // claim_file handler of the plugin.
1632
1633 static enum ld_plugin_status
1634 get_input_section_name(const struct ld_plugin_section section,
1635 char** section_name_ptr)
1636 {
1637 gold_assert(parameters->options().has_plugins());
1638
1639 if (!parameters->options().plugins()->in_claim_file_handler())
1640 return LDPS_ERR;
1641
1642 Object* obj
1643 = parameters->options().plugins()->get_elf_object(section.handle);
1644
1645 if (obj == NULL)
1646 return LDPS_BAD_HANDLE;
1647
1648 // Check if the object is locked before getting the section name.
1649 gold_assert(obj->is_locked());
1650
1651 const std::string section_name = obj->section_name(section.shndx);
1652 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1653 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1654 return LDPS_OK;
1655 }
1656
1657 // Get the contents of the specified section in the object corresponding
1658 // to the handle. This plugin interface can only be called in the
1659 // claim_file handler of the plugin.
1660
1661 static enum ld_plugin_status
1662 get_input_section_contents(const struct ld_plugin_section section,
1663 const unsigned char** section_contents_ptr,
1664 size_t* len)
1665 {
1666 gold_assert(parameters->options().has_plugins());
1667
1668 if (!parameters->options().plugins()->in_claim_file_handler())
1669 return LDPS_ERR;
1670
1671 Object* obj
1672 = parameters->options().plugins()->get_elf_object(section.handle);
1673
1674 if (obj == NULL)
1675 return LDPS_BAD_HANDLE;
1676
1677 // Check if the object is locked before getting the section contents.
1678 gold_assert(obj->is_locked());
1679
1680 section_size_type plen;
1681 *section_contents_ptr
1682 = obj->section_contents(section.shndx, &plen, false);
1683 *len = plen;
1684 return LDPS_OK;
1685 }
1686
1687 // Specify the ordering of sections in the final layout. The sections are
1688 // specified as (handle,shndx) pairs in the two arrays in the order in
1689 // which they should appear in the final layout.
1690
1691 static enum ld_plugin_status
1692 update_section_order(const struct ld_plugin_section* section_list,
1693 unsigned int num_sections)
1694 {
1695 gold_assert(parameters->options().has_plugins());
1696
1697 if (num_sections == 0)
1698 return LDPS_OK;
1699
1700 if (section_list == NULL)
1701 return LDPS_ERR;
1702
1703 Layout* layout = parameters->options().plugins()->layout();
1704 gold_assert (layout != NULL);
1705
1706 std::map<Section_id, unsigned int>* order_map
1707 = layout->get_section_order_map();
1708
1709 /* Store the mapping from Section_id to section position in layout's
1710 order_map to consult after output sections are added. */
1711 for (unsigned int i = 0; i < num_sections; ++i)
1712 {
1713 Object* obj = parameters->options().plugins()->get_elf_object(
1714 section_list[i].handle);
1715 if (obj == NULL)
1716 return LDPS_BAD_HANDLE;
1717 unsigned int shndx = section_list[i].shndx;
1718 Section_id secn_id(obj, shndx);
1719 (*order_map)[secn_id] = i + 1;
1720 }
1721
1722 return LDPS_OK;
1723 }
1724
1725 // Let the linker know that the sections could be reordered.
1726
1727 static enum ld_plugin_status
1728 allow_section_ordering()
1729 {
1730 gold_assert(parameters->options().has_plugins());
1731 Layout* layout = parameters->options().plugins()->layout();
1732 layout->set_section_ordering_specified();
1733 return LDPS_OK;
1734 }
1735
1736 // Let the linker know that a subset of sections could be mapped
1737 // to a unique segment.
1738
1739 static enum ld_plugin_status
1740 allow_unique_segment_for_sections()
1741 {
1742 gold_assert(parameters->options().has_plugins());
1743 Layout* layout = parameters->options().plugins()->layout();
1744 layout->set_unique_segment_for_sections_specified();
1745 return LDPS_OK;
1746 }
1747
1748 // This function should map the list of sections specified in the
1749 // SECTION_LIST to a unique segment. ELF segments do not have names
1750 // and the NAME is used to identify Output Section which should contain
1751 // the list of sections. This Output Section will then be mapped to
1752 // a unique segment. FLAGS is used to specify if any additional segment
1753 // flags need to be set. For instance, a specific segment flag can be
1754 // set to identify this segment. Unsetting segment flags is not possible.
1755 // ALIGN specifies the alignment of the segment.
1756
1757 static enum ld_plugin_status
1758 unique_segment_for_sections(const char* segment_name,
1759 uint64_t flags,
1760 uint64_t align,
1761 const struct ld_plugin_section* section_list,
1762 unsigned int num_sections)
1763 {
1764 gold_assert(parameters->options().has_plugins());
1765
1766 if (num_sections == 0)
1767 return LDPS_OK;
1768
1769 if (section_list == NULL)
1770 return LDPS_ERR;
1771
1772 Layout* layout = parameters->options().plugins()->layout();
1773 gold_assert (layout != NULL);
1774
1775 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
1776 s->name = segment_name;
1777 s->flags = flags;
1778 s->align = align;
1779
1780 for (unsigned int i = 0; i < num_sections; ++i)
1781 {
1782 Object* obj = parameters->options().plugins()->get_elf_object(
1783 section_list[i].handle);
1784 if (obj == NULL)
1785 return LDPS_BAD_HANDLE;
1786 unsigned int shndx = section_list[i].shndx;
1787 Const_section_id secn_id(obj, shndx);
1788 layout->insert_section_segment_map(secn_id, s);
1789 }
1790
1791 return LDPS_OK;
1792 }
1793
1794 #endif // ENABLE_PLUGINS
1795
1796 // Allocate a Pluginobj object of the appropriate size and endianness.
1797
1798 static Pluginobj*
1799 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1800 {
1801 Pluginobj* obj = NULL;
1802
1803 parameters_force_valid_target();
1804 const Target& target(parameters->target());
1805
1806 if (target.get_size() == 32)
1807 {
1808 if (target.is_big_endian())
1809 #ifdef HAVE_TARGET_32_BIG
1810 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1811 input_file, offset, filesize);
1812 #else
1813 gold_error(_("%s: not configured to support "
1814 "32-bit big-endian object"),
1815 input_file->filename().c_str());
1816 #endif
1817 else
1818 #ifdef HAVE_TARGET_32_LITTLE
1819 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1820 input_file, offset, filesize);
1821 #else
1822 gold_error(_("%s: not configured to support "
1823 "32-bit little-endian object"),
1824 input_file->filename().c_str());
1825 #endif
1826 }
1827 else if (target.get_size() == 64)
1828 {
1829 if (target.is_big_endian())
1830 #ifdef HAVE_TARGET_64_BIG
1831 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1832 input_file, offset, filesize);
1833 #else
1834 gold_error(_("%s: not configured to support "
1835 "64-bit big-endian object"),
1836 input_file->filename().c_str());
1837 #endif
1838 else
1839 #ifdef HAVE_TARGET_64_LITTLE
1840 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1841 input_file, offset, filesize);
1842 #else
1843 gold_error(_("%s: not configured to support "
1844 "64-bit little-endian object"),
1845 input_file->filename().c_str());
1846 #endif
1847 }
1848
1849 gold_assert(obj != NULL);
1850 return obj;
1851 }
1852
1853 } // End namespace gold.
This page took 0.066035 seconds and 5 git commands to generate.