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