Automatic date update in version.in
[binutils-gdb.git] / gold / plugin.cc
blobc051805cec38c4941eb4e3b7e812fcbb7ccc147e
1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2017 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cstdio>
26 #include <cstdarg>
27 #include <cstring>
28 #include <string>
29 #include <vector>
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
40 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
42 #define RTLD_NOW 0 /* Dummy value. */
43 static void *
44 dlopen(const char *file, int mode ATTRIBUTE_UNUSED)
46 return LoadLibrary(file);
49 static void *
50 dlsym(void *handle, const char *name)
52 return reinterpret_cast<void *>(
53 GetProcAddress(static_cast<HMODULE>(handle),name));
56 static const char *
57 dlerror(void)
59 return "unable to load dll";
62 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
63 #endif /* ENABLE_PLUGINS */
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"
77 namespace gold
80 #ifdef ENABLE_PLUGINS
82 // The linker's exported interfaces.
84 extern "C"
87 static enum ld_plugin_status
88 register_claim_file(ld_plugin_claim_file_handler handler);
90 static enum ld_plugin_status
91 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler);
93 static enum ld_plugin_status
94 register_cleanup(ld_plugin_cleanup_handler handler);
96 static enum ld_plugin_status
97 add_symbols(void *handle, int nsyms, const struct ld_plugin_symbol *syms);
99 static enum ld_plugin_status
100 get_input_file(const void *handle, struct ld_plugin_input_file *file);
102 static enum ld_plugin_status
103 get_view(const void *handle, const void **viewp);
105 static enum ld_plugin_status
106 release_input_file(const void *handle);
108 static enum ld_plugin_status
109 get_symbols(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
111 static enum ld_plugin_status
112 get_symbols_v2(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
114 static enum ld_plugin_status
115 get_symbols_v3(const void *handle, int nsyms, struct ld_plugin_symbol *syms);
117 static enum ld_plugin_status
118 add_input_file(const char *pathname);
120 static enum ld_plugin_status
121 add_input_library(const char *pathname);
123 static enum ld_plugin_status
124 set_extra_library_path(const char *path);
126 static enum ld_plugin_status
127 message(int level, const char *format, ...);
129 static enum ld_plugin_status
130 get_input_section_count(const void* handle, unsigned int* count);
132 static enum ld_plugin_status
133 get_input_section_type(const struct ld_plugin_section section,
134 unsigned int* type);
136 static enum ld_plugin_status
137 get_input_section_name(const struct ld_plugin_section section,
138 char** section_name_ptr);
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);
145 static enum ld_plugin_status
146 update_section_order(const struct ld_plugin_section *section_list,
147 unsigned int num_sections);
149 static enum ld_plugin_status
150 allow_section_ordering();
152 static enum ld_plugin_status
153 allow_unique_segment_for_sections();
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);
162 static enum ld_plugin_status
163 get_input_section_alignment(const struct ld_plugin_section section,
164 unsigned int* addralign);
166 static enum ld_plugin_status
167 get_input_section_size(const struct ld_plugin_section section,
168 uint64_t* secsize);
172 #endif // ENABLE_PLUGINS
174 static Pluginobj* make_sized_plugin_object(Input_file* input_file,
175 off_t offset, off_t filesize);
177 // Plugin methods.
179 // Load one plugin library.
181 void
182 Plugin::load()
184 #ifdef ENABLE_PLUGINS
185 // Load the plugin library.
186 // FIXME: Look for the library in standard locations.
187 this->handle_ = dlopen(this->filename_.c_str(), RTLD_NOW);
188 if (this->handle_ == NULL)
190 gold_error(_("%s: could not load plugin library: %s"),
191 this->filename_.c_str(), dlerror());
192 return;
195 // Find the plugin's onload entry point.
196 void* ptr = dlsym(this->handle_, "onload");
197 if (ptr == NULL)
199 gold_error(_("%s: could not find onload entry point"),
200 this->filename_.c_str());
201 return;
203 ld_plugin_onload onload;
204 gold_assert(sizeof(onload) == sizeof(ptr));
205 memcpy(&onload, &ptr, sizeof(ptr));
207 // Get the linker's version number.
208 const char* ver = get_version_string();
209 int major = 0;
210 int minor = 0;
211 sscanf(ver, "%d.%d", &major, &minor);
213 // Allocate and populate a transfer vector.
214 const int tv_fixed_size = 29;
216 int tv_size = this->args_.size() + tv_fixed_size;
217 ld_plugin_tv* tv = new ld_plugin_tv[tv_size];
219 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
220 // while processing subsequent entries.
221 int i = 0;
222 tv[i].tv_tag = LDPT_MESSAGE;
223 tv[i].tv_u.tv_message = message;
225 ++i;
226 tv[i].tv_tag = LDPT_API_VERSION;
227 tv[i].tv_u.tv_val = LD_PLUGIN_API_VERSION;
229 ++i;
230 tv[i].tv_tag = LDPT_GOLD_VERSION;
231 tv[i].tv_u.tv_val = major * 100 + minor;
233 ++i;
234 tv[i].tv_tag = LDPT_LINKER_OUTPUT;
235 if (parameters->options().relocatable())
236 tv[i].tv_u.tv_val = LDPO_REL;
237 else if (parameters->options().shared())
238 tv[i].tv_u.tv_val = LDPO_DYN;
239 else if (parameters->options().pie())
240 tv[i].tv_u.tv_val = LDPO_PIE;
241 else
242 tv[i].tv_u.tv_val = LDPO_EXEC;
244 ++i;
245 tv[i].tv_tag = LDPT_OUTPUT_NAME;
246 tv[i].tv_u.tv_string = parameters->options().output();
248 for (unsigned int j = 0; j < this->args_.size(); ++j)
250 ++i;
251 tv[i].tv_tag = LDPT_OPTION;
252 tv[i].tv_u.tv_string = this->args_[j].c_str();
255 ++i;
256 tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK;
257 tv[i].tv_u.tv_register_claim_file = register_claim_file;
259 ++i;
260 tv[i].tv_tag = LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK;
261 tv[i].tv_u.tv_register_all_symbols_read = register_all_symbols_read;
263 ++i;
264 tv[i].tv_tag = LDPT_REGISTER_CLEANUP_HOOK;
265 tv[i].tv_u.tv_register_cleanup = register_cleanup;
267 ++i;
268 tv[i].tv_tag = LDPT_ADD_SYMBOLS;
269 tv[i].tv_u.tv_add_symbols = add_symbols;
271 ++i;
272 tv[i].tv_tag = LDPT_GET_INPUT_FILE;
273 tv[i].tv_u.tv_get_input_file = get_input_file;
275 ++i;
276 tv[i].tv_tag = LDPT_GET_VIEW;
277 tv[i].tv_u.tv_get_view = get_view;
279 ++i;
280 tv[i].tv_tag = LDPT_RELEASE_INPUT_FILE;
281 tv[i].tv_u.tv_release_input_file = release_input_file;
283 ++i;
284 tv[i].tv_tag = LDPT_GET_SYMBOLS;
285 tv[i].tv_u.tv_get_symbols = get_symbols;
287 ++i;
288 tv[i].tv_tag = LDPT_GET_SYMBOLS_V2;
289 tv[i].tv_u.tv_get_symbols = get_symbols_v2;
291 ++i;
292 tv[i].tv_tag = LDPT_GET_SYMBOLS_V3;
293 tv[i].tv_u.tv_get_symbols = get_symbols_v3;
295 ++i;
296 tv[i].tv_tag = LDPT_ADD_INPUT_FILE;
297 tv[i].tv_u.tv_add_input_file = add_input_file;
299 ++i;
300 tv[i].tv_tag = LDPT_ADD_INPUT_LIBRARY;
301 tv[i].tv_u.tv_add_input_library = add_input_library;
303 ++i;
304 tv[i].tv_tag = LDPT_SET_EXTRA_LIBRARY_PATH;
305 tv[i].tv_u.tv_set_extra_library_path = set_extra_library_path;
307 ++i;
308 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_COUNT;
309 tv[i].tv_u.tv_get_input_section_count = get_input_section_count;
311 ++i;
312 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_TYPE;
313 tv[i].tv_u.tv_get_input_section_type = get_input_section_type;
315 ++i;
316 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_NAME;
317 tv[i].tv_u.tv_get_input_section_name = get_input_section_name;
319 ++i;
320 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_CONTENTS;
321 tv[i].tv_u.tv_get_input_section_contents = get_input_section_contents;
323 ++i;
324 tv[i].tv_tag = LDPT_UPDATE_SECTION_ORDER;
325 tv[i].tv_u.tv_update_section_order = update_section_order;
327 ++i;
328 tv[i].tv_tag = LDPT_ALLOW_SECTION_ORDERING;
329 tv[i].tv_u.tv_allow_section_ordering = allow_section_ordering;
331 ++i;
332 tv[i].tv_tag = LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS;
333 tv[i].tv_u.tv_allow_unique_segment_for_sections
334 = allow_unique_segment_for_sections;
336 ++i;
337 tv[i].tv_tag = LDPT_UNIQUE_SEGMENT_FOR_SECTIONS;
338 tv[i].tv_u.tv_unique_segment_for_sections = unique_segment_for_sections;
340 ++i;
341 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_ALIGNMENT;
342 tv[i].tv_u.tv_get_input_section_alignment = get_input_section_alignment;
344 ++i;
345 tv[i].tv_tag = LDPT_GET_INPUT_SECTION_SIZE;
346 tv[i].tv_u.tv_get_input_section_size = get_input_section_size;
348 ++i;
349 tv[i].tv_tag = LDPT_NULL;
350 tv[i].tv_u.tv_val = 0;
352 gold_assert(i == tv_size - 1);
354 // Call the onload entry point.
355 (*onload)(tv);
357 delete[] tv;
358 #endif // ENABLE_PLUGINS
361 // Call the plugin claim-file handler.
363 inline bool
364 Plugin::claim_file(struct ld_plugin_input_file* plugin_input_file)
366 int claimed = 0;
368 if (this->claim_file_handler_ != NULL)
370 (*this->claim_file_handler_)(plugin_input_file, &claimed);
371 if (claimed)
372 return true;
374 return false;
377 // Call the all-symbols-read handler.
379 inline void
380 Plugin::all_symbols_read()
382 if (this->all_symbols_read_handler_ != NULL)
383 (*this->all_symbols_read_handler_)();
386 // Call the cleanup handler.
388 inline void
389 Plugin::cleanup()
391 if (this->cleanup_handler_ != NULL && !this->cleanup_done_)
393 // Set this flag before calling to prevent a recursive plunge
394 // in the event that a plugin's cleanup handler issues a
395 // fatal error.
396 this->cleanup_done_ = true;
397 (*this->cleanup_handler_)();
401 // This task is used to rescan archives as needed.
403 class Plugin_rescan : public Task
405 public:
406 Plugin_rescan(Task_token* this_blocker, Task_token* next_blocker)
407 : this_blocker_(this_blocker), next_blocker_(next_blocker)
410 ~Plugin_rescan()
412 delete this->this_blocker_;
415 Task_token*
416 is_runnable()
418 if (this->this_blocker_->is_blocked())
419 return this->this_blocker_;
420 return NULL;
423 void
424 locks(Task_locker* tl)
425 { tl->add(this, this->next_blocker_); }
427 void
428 run(Workqueue*)
429 { parameters->options().plugins()->rescan(this); }
431 std::string
432 get_name() const
433 { return "Plugin_rescan"; }
435 private:
436 Task_token* this_blocker_;
437 Task_token* next_blocker_;
440 // Plugin_manager methods.
442 Plugin_manager::~Plugin_manager()
444 for (Plugin_list::iterator p = this->plugins_.begin();
445 p != this->plugins_.end();
446 ++p)
447 delete *p;
448 this->plugins_.clear();
449 for (Object_list::iterator obj = this->objects_.begin();
450 obj != this->objects_.end();
451 ++obj)
452 delete *obj;
453 this->objects_.clear();
454 delete this->lock_;
457 // Load all plugin libraries.
459 void
460 Plugin_manager::load_plugins(Layout* layout)
462 this->layout_ = layout;
463 for (this->current_ = this->plugins_.begin();
464 this->current_ != this->plugins_.end();
465 ++this->current_)
466 (*this->current_)->load();
469 // Call the plugin claim-file handlers in turn to see if any claim the file.
471 Pluginobj*
472 Plugin_manager::claim_file(Input_file* input_file, off_t offset,
473 off_t filesize, Object* elf_object)
475 bool lock_initialized = this->initialize_lock_.initialize();
477 gold_assert(lock_initialized);
478 Hold_lock hl(*this->lock_);
479 if (this->in_replacement_phase_)
480 return NULL;
482 unsigned int handle = this->objects_.size();
483 this->input_file_ = input_file;
484 this->plugin_input_file_.name = input_file->filename().c_str();
485 this->plugin_input_file_.fd = input_file->file().descriptor();
486 this->plugin_input_file_.offset = offset;
487 this->plugin_input_file_.filesize = filesize;
488 this->plugin_input_file_.handle = reinterpret_cast<void*>(handle);
489 if (elf_object != NULL)
490 this->objects_.push_back(elf_object);
491 this->in_claim_file_handler_ = true;
493 for (this->current_ = this->plugins_.begin();
494 this->current_ != this->plugins_.end();
495 ++this->current_)
497 if ((*this->current_)->claim_file(&this->plugin_input_file_))
499 this->any_claimed_ = true;
500 this->in_claim_file_handler_ = false;
502 if (this->objects_.size() > handle
503 && this->objects_[handle]->pluginobj() != NULL)
504 return this->objects_[handle]->pluginobj();
506 // If the plugin claimed the file but did not call the
507 // add_symbols callback, we need to create the Pluginobj now.
508 Pluginobj* obj = this->make_plugin_object(handle);
509 return obj;
513 this->in_claim_file_handler_ = false;
514 return NULL;
517 // Save an archive. This is used so that a plugin can add a file
518 // which refers to a symbol which was not previously referenced. In
519 // that case we want to pretend that the symbol was referenced before,
520 // and pull in the archive object.
522 void
523 Plugin_manager::save_archive(Archive* archive)
525 if (this->in_replacement_phase_ || !this->any_claimed_)
526 delete archive;
527 else
528 this->rescannable_.push_back(Rescannable(archive));
531 // Save an Input_group. This is like save_archive.
533 void
534 Plugin_manager::save_input_group(Input_group* input_group)
536 if (this->in_replacement_phase_ || !this->any_claimed_)
537 delete input_group;
538 else
539 this->rescannable_.push_back(Rescannable(input_group));
542 // Call the all-symbols-read handlers.
544 void
545 Plugin_manager::all_symbols_read(Workqueue* workqueue, Task* task,
546 Input_objects* input_objects,
547 Symbol_table* symtab,
548 Dirsearch* dirpath, Mapfile* mapfile,
549 Task_token** last_blocker)
551 this->in_replacement_phase_ = true;
552 this->workqueue_ = workqueue;
553 this->task_ = task;
554 this->input_objects_ = input_objects;
555 this->symtab_ = symtab;
556 this->dirpath_ = dirpath;
557 this->mapfile_ = mapfile;
558 this->this_blocker_ = NULL;
560 for (this->current_ = this->plugins_.begin();
561 this->current_ != this->plugins_.end();
562 ++this->current_)
563 (*this->current_)->all_symbols_read();
565 if (this->any_added_)
567 Task_token* next_blocker = new Task_token(true);
568 next_blocker->add_blocker();
569 workqueue->queue(new Plugin_rescan(this->this_blocker_, next_blocker));
570 this->this_blocker_ = next_blocker;
573 *last_blocker = this->this_blocker_;
576 // This is called when we see a new undefined symbol. If we are in
577 // the replacement phase, this means that we may need to rescan some
578 // archives we have previously seen.
580 void
581 Plugin_manager::new_undefined_symbol(Symbol* sym)
583 if (this->in_replacement_phase_)
584 this->undefined_symbols_.push_back(sym);
587 // Rescan archives as needed. This handles the case where a new
588 // object file added by a plugin has an undefined reference to some
589 // symbol defined in an archive.
591 void
592 Plugin_manager::rescan(Task* task)
594 size_t rescan_pos = 0;
595 size_t rescan_size = this->rescannable_.size();
596 while (!this->undefined_symbols_.empty())
598 if (rescan_pos >= rescan_size)
600 this->undefined_symbols_.clear();
601 return;
604 Undefined_symbol_list undefs;
605 undefs.reserve(this->undefined_symbols_.size());
606 this->undefined_symbols_.swap(undefs);
608 size_t min_rescan_pos = rescan_size;
610 for (Undefined_symbol_list::const_iterator p = undefs.begin();
611 p != undefs.end();
612 ++p)
614 if (!(*p)->is_undefined())
615 continue;
617 this->undefined_symbols_.push_back(*p);
619 // Find the first rescan archive which defines this symbol,
620 // starting at the current rescan position. The rescan position
621 // exists so that given -la -lb -lc we don't look for undefined
622 // symbols in -lb back in -la, but instead get the definition
623 // from -lc. Don't bother to look past the current minimum
624 // rescan position.
625 for (size_t i = rescan_pos; i < min_rescan_pos; ++i)
627 if (this->rescannable_defines(i, *p))
629 min_rescan_pos = i;
630 break;
635 if (min_rescan_pos >= rescan_size)
637 // We didn't find any rescannable archives which define any
638 // undefined symbols.
639 return;
642 const Rescannable& r(this->rescannable_[min_rescan_pos]);
643 if (r.is_archive)
645 Task_lock_obj<Archive> tl(task, r.u.archive);
646 r.u.archive->add_symbols(this->symtab_, this->layout_,
647 this->input_objects_, this->mapfile_);
649 else
651 size_t next_saw_undefined = this->symtab_->saw_undefined();
652 size_t saw_undefined;
655 saw_undefined = next_saw_undefined;
657 for (Input_group::const_iterator p = r.u.input_group->begin();
658 p != r.u.input_group->end();
659 ++p)
661 Task_lock_obj<Archive> tl(task, *p);
663 (*p)->add_symbols(this->symtab_, this->layout_,
664 this->input_objects_, this->mapfile_);
667 next_saw_undefined = this->symtab_->saw_undefined();
669 while (saw_undefined != next_saw_undefined);
672 for (size_t i = rescan_pos; i < min_rescan_pos + 1; ++i)
674 if (this->rescannable_[i].is_archive)
675 delete this->rescannable_[i].u.archive;
676 else
677 delete this->rescannable_[i].u.input_group;
680 rescan_pos = min_rescan_pos + 1;
684 // Return whether the rescannable at index I defines SYM.
686 bool
687 Plugin_manager::rescannable_defines(size_t i, Symbol* sym)
689 const Rescannable& r(this->rescannable_[i]);
690 if (r.is_archive)
691 return r.u.archive->defines_symbol(sym);
692 else
694 for (Input_group::const_iterator p = r.u.input_group->begin();
695 p != r.u.input_group->end();
696 ++p)
698 if ((*p)->defines_symbol(sym))
699 return true;
701 return false;
705 // Layout deferred objects.
707 void
708 Plugin_manager::layout_deferred_objects()
710 Deferred_layout_list::iterator obj;
712 for (obj = this->deferred_layout_objects_.begin();
713 obj != this->deferred_layout_objects_.end();
714 ++obj)
716 // Lock the object so we can read from it. This is only called
717 // single-threaded from queue_middle_tasks, so it is OK to lock.
718 // Unfortunately we have no way to pass in a Task token.
719 const Task* dummy_task = reinterpret_cast<const Task*>(-1);
720 Task_lock_obj<Object> tl(dummy_task, *obj);
721 (*obj)->layout_deferred_sections(this->layout_);
725 // Call the cleanup handlers.
727 void
728 Plugin_manager::cleanup()
730 if (this->any_added_)
732 // If any input files were added, close all the input files.
733 // This is because the plugin may want to remove them, and on
734 // Windows you are not allowed to remove an open file.
735 close_all_descriptors();
738 for (this->current_ = this->plugins_.begin();
739 this->current_ != this->plugins_.end();
740 ++this->current_)
741 (*this->current_)->cleanup();
744 // Make a new Pluginobj object. This is called when the plugin calls
745 // the add_symbols API.
747 Pluginobj*
748 Plugin_manager::make_plugin_object(unsigned int handle)
750 // Make sure we aren't asked to make an object for the same handle twice.
751 if (this->objects_.size() != handle
752 && this->objects_[handle]->pluginobj() != NULL)
753 return NULL;
755 Pluginobj* obj = make_sized_plugin_object(this->input_file_,
756 this->plugin_input_file_.offset,
757 this->plugin_input_file_.filesize);
760 // If the elf object for this file was pushed into the objects_ vector, delete
761 // it to make room for the Pluginobj as this file is claimed.
762 if (this->objects_.size() != handle)
763 this->objects_.pop_back();
765 this->objects_.push_back(obj);
766 return obj;
769 // Get the input file information with an open (possibly re-opened)
770 // file descriptor.
772 ld_plugin_status
773 Plugin_manager::get_input_file(unsigned int handle,
774 struct ld_plugin_input_file* file)
776 Pluginobj* obj = this->object(handle)->pluginobj();
777 if (obj == NULL)
778 return LDPS_BAD_HANDLE;
780 obj->lock(this->task_);
781 file->name = obj->filename().c_str();
782 file->fd = obj->descriptor();
783 file->offset = obj->offset();
784 file->filesize = obj->filesize();
785 file->handle = reinterpret_cast<void*>(handle);
786 return LDPS_OK;
789 // Release the input file.
791 ld_plugin_status
792 Plugin_manager::release_input_file(unsigned int handle)
794 if (this->object(handle) == NULL)
795 return LDPS_BAD_HANDLE;
797 Pluginobj* obj = this->object(handle)->pluginobj();
799 if (obj == NULL)
800 return LDPS_BAD_HANDLE;
802 obj->unlock(this->task_);
803 return LDPS_OK;
806 // Get the elf object corresponding to the handle. Return NULL if we
807 // found a Pluginobj instead.
809 Object*
810 Plugin_manager::get_elf_object(const void* handle)
812 Object* obj = this->object(
813 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
815 // The object should not be a Pluginobj.
816 if (obj == NULL
817 || obj->pluginobj() != NULL)
818 return NULL;
820 return obj;
823 ld_plugin_status
824 Plugin_manager::get_view(unsigned int handle, const void **viewp)
826 off_t offset;
827 size_t filesize;
828 Input_file *input_file;
829 if (this->in_claim_file_handler_)
831 // We are being called from the claim_file hook.
832 const struct ld_plugin_input_file &f = this->plugin_input_file_;
833 offset = f.offset;
834 filesize = f.filesize;
835 input_file = this->input_file_;
837 else
839 // An already claimed file.
840 if (this->object(handle) == NULL)
841 return LDPS_BAD_HANDLE;
842 Pluginobj* obj = this->object(handle)->pluginobj();
843 if (obj == NULL)
844 return LDPS_BAD_HANDLE;
845 offset = obj->offset();
846 filesize = obj->filesize();
847 input_file = obj->input_file();
849 *viewp = (void*) input_file->file().get_view(offset, 0, filesize, false,
850 false);
851 return LDPS_OK;
854 // Add a new library path.
856 ld_plugin_status
857 Plugin_manager::set_extra_library_path(const char* path)
859 this->extra_search_path_ = std::string(path);
860 return LDPS_OK;
863 // Add a new input file.
865 ld_plugin_status
866 Plugin_manager::add_input_file(const char* pathname, bool is_lib)
868 Input_file_argument file(pathname,
869 (is_lib
870 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
871 : Input_file_argument::INPUT_FILE_TYPE_FILE),
872 (is_lib
873 ? this->extra_search_path_.c_str()
874 : ""),
875 false,
876 this->options_);
877 Input_argument* input_argument = new Input_argument(file);
878 Task_token* next_blocker = new Task_token(true);
879 next_blocker->add_blocker();
880 if (parameters->incremental())
881 gold_error(_("input files added by plug-ins in --incremental mode not "
882 "supported yet"));
883 this->workqueue_->queue_soon(new Read_symbols(this->input_objects_,
884 this->symtab_,
885 this->layout_,
886 this->dirpath_,
888 this->mapfile_,
889 input_argument,
890 NULL,
891 NULL,
892 this->this_blocker_,
893 next_blocker));
894 this->this_blocker_ = next_blocker;
895 this->any_added_ = true;
896 return LDPS_OK;
899 // Class Pluginobj.
901 Pluginobj::Pluginobj(const std::string& name, Input_file* input_file,
902 off_t offset, off_t filesize)
903 : Object(name, input_file, false, offset),
904 nsyms_(0), syms_(NULL), symbols_(), filesize_(filesize), comdat_map_()
908 // Return TRUE if a defined symbol is referenced from outside the
909 // universe of claimed objects. Only references from relocatable,
910 // non-IR (unclaimed) objects count as a reference. References from
911 // dynamic objects count only as "visible".
913 static inline bool
914 is_referenced_from_outside(Symbol* lsym)
916 if (lsym->in_real_elf())
917 return true;
918 if (parameters->options().relocatable())
919 return true;
920 if (parameters->options().is_undefined(lsym->name()))
921 return true;
922 return false;
925 // Return TRUE if a defined symbol might be reachable from outside the
926 // load module.
928 static inline bool
929 is_visible_from_outside(Symbol* lsym)
931 if (lsym->in_dyn())
932 return true;
933 if (parameters->options().export_dynamic() || parameters->options().shared())
934 return lsym->is_externally_visible();
935 return false;
938 // Get symbol resolution info.
940 ld_plugin_status
941 Pluginobj::get_symbol_resolution_info(Symbol_table* symtab,
942 int nsyms,
943 ld_plugin_symbol* syms,
944 int version) const
946 // For version 1 of this interface, we cannot use
947 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
948 // instead.
949 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
950 = (version > 1
951 ? LDPR_PREVAILING_DEF_IRONLY_EXP
952 : LDPR_PREVAILING_DEF);
954 if (nsyms > this->nsyms_)
955 return LDPS_NO_SYMS;
957 if (static_cast<size_t>(nsyms) > this->symbols_.size())
959 // We never decided to include this object. We mark all symbols as
960 // preempted.
961 gold_assert(this->symbols_.size() == 0);
962 for (int i = 0; i < nsyms; i++)
963 syms[i].resolution = LDPR_PREEMPTED_REG;
964 return version > 2 ? LDPS_NO_SYMS : LDPS_OK;
967 for (int i = 0; i < nsyms; i++)
969 ld_plugin_symbol* isym = &syms[i];
970 Symbol* lsym = this->symbols_[i];
971 if (lsym->is_forwarder())
972 lsym = symtab->resolve_forwards(lsym);
973 ld_plugin_symbol_resolution res = LDPR_UNKNOWN;
975 if (lsym->is_undefined())
976 // The symbol remains undefined.
977 res = LDPR_UNDEF;
978 else if (isym->def == LDPK_UNDEF
979 || isym->def == LDPK_WEAKUNDEF
980 || isym->def == LDPK_COMMON)
982 // The original symbol was undefined or common.
983 if (lsym->source() != Symbol::FROM_OBJECT)
984 res = LDPR_RESOLVED_EXEC;
985 else if (lsym->object()->pluginobj() == this)
987 if (is_referenced_from_outside(lsym))
988 res = LDPR_PREVAILING_DEF;
989 else if (is_visible_from_outside(lsym))
990 res = ldpr_prevailing_def_ironly_exp;
991 else
992 res = LDPR_PREVAILING_DEF_IRONLY;
994 else if (lsym->object()->pluginobj() != NULL)
995 res = LDPR_RESOLVED_IR;
996 else if (lsym->object()->is_dynamic())
997 res = LDPR_RESOLVED_DYN;
998 else
999 res = LDPR_RESOLVED_EXEC;
1001 else
1003 // The original symbol was a definition.
1004 if (lsym->source() != Symbol::FROM_OBJECT)
1005 res = LDPR_PREEMPTED_REG;
1006 else if (lsym->object() == static_cast<const Object*>(this))
1008 if (is_referenced_from_outside(lsym))
1009 res = LDPR_PREVAILING_DEF;
1010 else if (is_visible_from_outside(lsym))
1011 res = ldpr_prevailing_def_ironly_exp;
1012 else
1013 res = LDPR_PREVAILING_DEF_IRONLY;
1015 else
1016 res = (lsym->object()->pluginobj() != NULL
1017 ? LDPR_PREEMPTED_IR
1018 : LDPR_PREEMPTED_REG);
1020 isym->resolution = res;
1022 return LDPS_OK;
1025 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1026 // should be kept.
1028 bool
1029 Pluginobj::include_comdat_group(std::string comdat_key, Layout* layout)
1031 std::pair<Comdat_map::iterator, bool> ins =
1032 this->comdat_map_.insert(std::make_pair(comdat_key, false));
1034 // If this is the first time we've seen this comdat key, ask the
1035 // layout object whether it should be included.
1036 if (ins.second)
1037 ins.first->second = layout->find_or_add_kept_section(comdat_key,
1038 NULL, 0, true,
1039 true, NULL);
1041 return ins.first->second;
1044 // Class Sized_pluginobj.
1046 template<int size, bool big_endian>
1047 Sized_pluginobj<size, big_endian>::Sized_pluginobj(
1048 const std::string& name,
1049 Input_file* input_file,
1050 off_t offset,
1051 off_t filesize)
1052 : Pluginobj(name, input_file, offset, filesize)
1056 // Read the symbols. Not used for plugin objects.
1058 template<int size, bool big_endian>
1059 void
1060 Sized_pluginobj<size, big_endian>::do_read_symbols(Read_symbols_data*)
1062 gold_unreachable();
1065 // Lay out the input sections. Not used for plugin objects.
1067 template<int size, bool big_endian>
1068 void
1069 Sized_pluginobj<size, big_endian>::do_layout(Symbol_table*, Layout*,
1070 Read_symbols_data*)
1072 gold_unreachable();
1075 // Add the symbols to the symbol table.
1077 template<int size, bool big_endian>
1078 void
1079 Sized_pluginobj<size, big_endian>::do_add_symbols(Symbol_table* symtab,
1080 Read_symbols_data*,
1081 Layout* layout)
1083 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
1084 unsigned char symbuf[sym_size];
1085 elfcpp::Sym<size, big_endian> sym(symbuf);
1086 elfcpp::Sym_write<size, big_endian> osym(symbuf);
1088 this->symbols_.resize(this->nsyms_);
1090 for (int i = 0; i < this->nsyms_; ++i)
1092 const struct ld_plugin_symbol* isym = &this->syms_[i];
1093 const char* name = isym->name;
1094 const char* ver = isym->version;
1095 elfcpp::Elf_Half shndx;
1096 elfcpp::STB bind;
1097 elfcpp::STV vis;
1099 if (name != NULL && name[0] == '\0')
1100 name = NULL;
1101 if (ver != NULL && ver[0] == '\0')
1102 ver = NULL;
1104 switch (isym->def)
1106 case LDPK_WEAKDEF:
1107 case LDPK_WEAKUNDEF:
1108 bind = elfcpp::STB_WEAK;
1109 break;
1110 case LDPK_DEF:
1111 case LDPK_UNDEF:
1112 case LDPK_COMMON:
1113 default:
1114 bind = elfcpp::STB_GLOBAL;
1115 break;
1118 switch (isym->def)
1120 case LDPK_DEF:
1121 case LDPK_WEAKDEF:
1122 shndx = elfcpp::SHN_ABS;
1123 break;
1124 case LDPK_COMMON:
1125 shndx = elfcpp::SHN_COMMON;
1126 break;
1127 case LDPK_UNDEF:
1128 case LDPK_WEAKUNDEF:
1129 default:
1130 shndx = elfcpp::SHN_UNDEF;
1131 break;
1134 switch (isym->visibility)
1136 case LDPV_PROTECTED:
1137 vis = elfcpp::STV_PROTECTED;
1138 break;
1139 case LDPV_INTERNAL:
1140 vis = elfcpp::STV_INTERNAL;
1141 break;
1142 case LDPV_HIDDEN:
1143 vis = elfcpp::STV_HIDDEN;
1144 break;
1145 case LDPV_DEFAULT:
1146 default:
1147 vis = elfcpp::STV_DEFAULT;
1148 break;
1151 if (isym->comdat_key != NULL
1152 && isym->comdat_key[0] != '\0'
1153 && !this->include_comdat_group(isym->comdat_key, layout))
1154 shndx = elfcpp::SHN_UNDEF;
1156 osym.put_st_name(0);
1157 osym.put_st_value(0);
1158 osym.put_st_size(0);
1159 osym.put_st_info(bind, elfcpp::STT_NOTYPE);
1160 osym.put_st_other(vis, 0);
1161 osym.put_st_shndx(shndx);
1163 this->symbols_[i] =
1164 symtab->add_from_pluginobj<size, big_endian>(this, name, ver, &sym);
1168 template<int size, bool big_endian>
1169 Archive::Should_include
1170 Sized_pluginobj<size, big_endian>::do_should_include_member(
1171 Symbol_table* symtab,
1172 Layout* layout,
1173 Read_symbols_data*,
1174 std::string* why)
1176 char* tmpbuf = NULL;
1177 size_t tmpbuflen = 0;
1179 for (int i = 0; i < this->nsyms_; ++i)
1181 const struct ld_plugin_symbol& sym = this->syms_[i];
1182 if (sym.def == LDPK_UNDEF || sym.def == LDPK_WEAKUNDEF)
1183 continue;
1184 const char* name = sym.name;
1185 Symbol* symbol;
1186 Archive::Should_include t = Archive::should_include_member(symtab,
1187 layout,
1188 name,
1189 &symbol, why,
1190 &tmpbuf,
1191 &tmpbuflen);
1192 if (t == Archive::SHOULD_INCLUDE_YES)
1194 if (tmpbuf != NULL)
1195 free(tmpbuf);
1196 return t;
1199 if (tmpbuf != NULL)
1200 free(tmpbuf);
1201 return Archive::SHOULD_INCLUDE_UNKNOWN;
1204 // Iterate over global symbols, calling a visitor class V for each.
1206 template<int size, bool big_endian>
1207 void
1208 Sized_pluginobj<size, big_endian>::do_for_all_global_symbols(
1209 Read_symbols_data*,
1210 Library_base::Symbol_visitor_base* v)
1212 for (int i = 0; i < this->nsyms_; ++i)
1214 const struct ld_plugin_symbol& sym = this->syms_[i];
1215 if (sym.def != LDPK_UNDEF)
1216 v->visit(sym.name);
1220 // Iterate over local symbols, calling a visitor class V for each GOT offset
1221 // associated with a local symbol.
1222 template<int size, bool big_endian>
1223 void
1224 Sized_pluginobj<size, big_endian>::do_for_all_local_got_entries(
1225 Got_offset_list::Visitor*) const
1227 gold_unreachable();
1230 // Get the size of a section. Not used for plugin objects.
1232 template<int size, bool big_endian>
1233 uint64_t
1234 Sized_pluginobj<size, big_endian>::do_section_size(unsigned int)
1236 gold_unreachable();
1237 return 0;
1240 // Get the name of a section. Not used for plugin objects.
1242 template<int size, bool big_endian>
1243 std::string
1244 Sized_pluginobj<size, big_endian>::do_section_name(unsigned int) const
1246 gold_unreachable();
1247 return std::string();
1250 // Return a view of the contents of a section. Not used for plugin objects.
1252 template<int size, bool big_endian>
1253 const unsigned char*
1254 Sized_pluginobj<size, big_endian>::do_section_contents(
1255 unsigned int,
1256 section_size_type*,
1257 bool)
1259 gold_unreachable();
1260 return NULL;
1263 // Return section flags. Not used for plugin objects.
1265 template<int size, bool big_endian>
1266 uint64_t
1267 Sized_pluginobj<size, big_endian>::do_section_flags(unsigned int)
1269 gold_unreachable();
1270 return 0;
1273 // Return section entsize. Not used for plugin objects.
1275 template<int size, bool big_endian>
1276 uint64_t
1277 Sized_pluginobj<size, big_endian>::do_section_entsize(unsigned int)
1279 gold_unreachable();
1280 return 0;
1283 // Return section address. Not used for plugin objects.
1285 template<int size, bool big_endian>
1286 uint64_t
1287 Sized_pluginobj<size, big_endian>::do_section_address(unsigned int)
1289 gold_unreachable();
1290 return 0;
1293 // Return section type. Not used for plugin objects.
1295 template<int size, bool big_endian>
1296 unsigned int
1297 Sized_pluginobj<size, big_endian>::do_section_type(unsigned int)
1299 gold_unreachable();
1300 return 0;
1303 // Return the section link field. Not used for plugin objects.
1305 template<int size, bool big_endian>
1306 unsigned int
1307 Sized_pluginobj<size, big_endian>::do_section_link(unsigned int)
1309 gold_unreachable();
1310 return 0;
1313 // Return the section link field. Not used for plugin objects.
1315 template<int size, bool big_endian>
1316 unsigned int
1317 Sized_pluginobj<size, big_endian>::do_section_info(unsigned int)
1319 gold_unreachable();
1320 return 0;
1323 // Return the section alignment. Not used for plugin objects.
1325 template<int size, bool big_endian>
1326 uint64_t
1327 Sized_pluginobj<size, big_endian>::do_section_addralign(unsigned int)
1329 gold_unreachable();
1330 return 0;
1333 // Return the Xindex structure to use. Not used for plugin objects.
1335 template<int size, bool big_endian>
1336 Xindex*
1337 Sized_pluginobj<size, big_endian>::do_initialize_xindex()
1339 gold_unreachable();
1340 return NULL;
1343 // Get symbol counts. Don't count plugin objects; the replacement
1344 // files will provide the counts.
1346 template<int size, bool big_endian>
1347 void
1348 Sized_pluginobj<size, big_endian>::do_get_global_symbol_counts(
1349 const Symbol_table*,
1350 size_t* defined,
1351 size_t* used) const
1353 *defined = 0;
1354 *used = 0;
1357 // Get symbols. Not used for plugin objects.
1359 template<int size, bool big_endian>
1360 const Object::Symbols*
1361 Sized_pluginobj<size, big_endian>::do_get_global_symbols() const
1363 gold_unreachable();
1366 // Class Plugin_finish. This task runs after all replacement files have
1367 // been added. For now, it's a placeholder for a possible plugin API
1368 // to allow the plugin to release most of its resources. The cleanup
1369 // handlers must be called later, because they can remove the temporary
1370 // object files that are needed until the end of the link.
1372 class Plugin_finish : public Task
1374 public:
1375 Plugin_finish(Task_token* this_blocker, Task_token* next_blocker)
1376 : this_blocker_(this_blocker), next_blocker_(next_blocker)
1379 ~Plugin_finish()
1381 if (this->this_blocker_ != NULL)
1382 delete this->this_blocker_;
1385 Task_token*
1386 is_runnable()
1388 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1389 return this->this_blocker_;
1390 return NULL;
1393 void
1394 locks(Task_locker* tl)
1395 { tl->add(this, this->next_blocker_); }
1397 void
1398 run(Workqueue*)
1400 // We could call early cleanup handlers here.
1403 std::string
1404 get_name() const
1405 { return "Plugin_finish"; }
1407 private:
1408 Task_token* this_blocker_;
1409 Task_token* next_blocker_;
1412 // Class Plugin_hook.
1414 Plugin_hook::~Plugin_hook()
1418 // Return whether a Plugin_hook task is runnable.
1420 Task_token*
1421 Plugin_hook::is_runnable()
1423 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
1424 return this->this_blocker_;
1425 return NULL;
1428 // Return a Task_locker for a Plugin_hook task. We don't need any
1429 // locks here.
1431 void
1432 Plugin_hook::locks(Task_locker*)
1436 // Run the "all symbols read" plugin hook.
1438 void
1439 Plugin_hook::run(Workqueue* workqueue)
1441 gold_assert(this->options_.has_plugins());
1442 Symbol* start_sym = this->symtab_->lookup(parameters->entry());
1443 if (start_sym != NULL)
1444 start_sym->set_in_real_elf();
1446 this->options_.plugins()->all_symbols_read(workqueue,
1447 this,
1448 this->input_objects_,
1449 this->symtab_,
1450 this->dirpath_,
1451 this->mapfile_,
1452 &this->this_blocker_);
1453 workqueue->queue_soon(new Plugin_finish(this->this_blocker_,
1454 this->next_blocker_));
1457 // The C interface routines called by the plugins.
1459 #ifdef ENABLE_PLUGINS
1461 // Register a claim-file handler.
1463 static enum ld_plugin_status
1464 register_claim_file(ld_plugin_claim_file_handler handler)
1466 gold_assert(parameters->options().has_plugins());
1467 parameters->options().plugins()->set_claim_file_handler(handler);
1468 return LDPS_OK;
1471 // Register an all-symbols-read handler.
1473 static enum ld_plugin_status
1474 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler)
1476 gold_assert(parameters->options().has_plugins());
1477 parameters->options().plugins()->set_all_symbols_read_handler(handler);
1478 return LDPS_OK;
1481 // Register a cleanup handler.
1483 static enum ld_plugin_status
1484 register_cleanup(ld_plugin_cleanup_handler handler)
1486 gold_assert(parameters->options().has_plugins());
1487 parameters->options().plugins()->set_cleanup_handler(handler);
1488 return LDPS_OK;
1491 // Add symbols from a plugin-claimed input file.
1493 static enum ld_plugin_status
1494 add_symbols(void* handle, int nsyms, const ld_plugin_symbol* syms)
1496 gold_assert(parameters->options().has_plugins());
1497 Pluginobj* obj = parameters->options().plugins()->make_plugin_object(
1498 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1499 if (obj == NULL)
1500 return LDPS_ERR;
1501 obj->store_incoming_symbols(nsyms, syms);
1502 return LDPS_OK;
1505 // Get the input file information with an open (possibly re-opened)
1506 // file descriptor.
1508 static enum ld_plugin_status
1509 get_input_file(const void* handle, struct ld_plugin_input_file* file)
1511 gold_assert(parameters->options().has_plugins());
1512 unsigned int obj_index =
1513 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1514 return parameters->options().plugins()->get_input_file(obj_index, file);
1517 // Release the input file.
1519 static enum ld_plugin_status
1520 release_input_file(const void* handle)
1522 gold_assert(parameters->options().has_plugins());
1523 unsigned int obj_index =
1524 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1525 return parameters->options().plugins()->release_input_file(obj_index);
1528 static enum ld_plugin_status
1529 get_view(const void *handle, const void **viewp)
1531 gold_assert(parameters->options().has_plugins());
1532 unsigned int obj_index =
1533 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle));
1534 return parameters->options().plugins()->get_view(obj_index, viewp);
1537 // Get the symbol resolution info for a plugin-claimed input file.
1539 static enum ld_plugin_status
1540 get_symbols(const void* handle, int nsyms, ld_plugin_symbol* syms)
1542 gold_assert(parameters->options().has_plugins());
1543 Plugin_manager* plugins = parameters->options().plugins();
1544 Object* obj = plugins->object(
1545 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1546 if (obj == NULL)
1547 return LDPS_ERR;
1548 Pluginobj* plugin_obj = obj->pluginobj();
1549 if (plugin_obj == NULL)
1550 return LDPS_ERR;
1551 Symbol_table* symtab = plugins->symtab();
1552 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 1);
1555 // Version 2 of the above. The only difference is that this version
1556 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1558 static enum ld_plugin_status
1559 get_symbols_v2(const void* handle, int nsyms, ld_plugin_symbol* syms)
1561 gold_assert(parameters->options().has_plugins());
1562 Plugin_manager* plugins = parameters->options().plugins();
1563 Object* obj = plugins->object(
1564 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1565 if (obj == NULL)
1566 return LDPS_ERR;
1567 Pluginobj* plugin_obj = obj->pluginobj();
1568 if (plugin_obj == NULL)
1569 return LDPS_ERR;
1570 Symbol_table* symtab = plugins->symtab();
1571 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 2);
1574 // Version 3 of the above. The only difference from v2 is that it
1575 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1576 // decided to include.
1578 static enum ld_plugin_status
1579 get_symbols_v3(const void* handle, int nsyms, ld_plugin_symbol* syms)
1581 gold_assert(parameters->options().has_plugins());
1582 Plugin_manager* plugins = parameters->options().plugins();
1583 Object* obj = plugins->object(
1584 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle)));
1585 if (obj == NULL)
1586 return LDPS_ERR;
1587 Pluginobj* plugin_obj = obj->pluginobj();
1588 if (plugin_obj == NULL)
1589 return LDPS_ERR;
1590 Symbol_table* symtab = plugins->symtab();
1591 return plugin_obj->get_symbol_resolution_info(symtab, nsyms, syms, 3);
1594 // Add a new (real) input file generated by a plugin.
1596 static enum ld_plugin_status
1597 add_input_file(const char* pathname)
1599 gold_assert(parameters->options().has_plugins());
1600 return parameters->options().plugins()->add_input_file(pathname, false);
1603 // Add a new (real) library required by a plugin.
1605 static enum ld_plugin_status
1606 add_input_library(const char* pathname)
1608 gold_assert(parameters->options().has_plugins());
1609 return parameters->options().plugins()->add_input_file(pathname, true);
1612 // Set the extra library path to be used by libraries added via
1613 // add_input_library
1615 static enum ld_plugin_status
1616 set_extra_library_path(const char* path)
1618 gold_assert(parameters->options().has_plugins());
1619 return parameters->options().plugins()->set_extra_library_path(path);
1622 // Issue a diagnostic message from a plugin.
1624 static enum ld_plugin_status
1625 message(int level, const char* format, ...)
1627 va_list args;
1628 va_start(args, format);
1630 switch (level)
1632 case LDPL_INFO:
1633 parameters->errors()->info(format, args);
1634 break;
1635 case LDPL_WARNING:
1636 parameters->errors()->warning(format, args);
1637 break;
1638 case LDPL_ERROR:
1639 default:
1640 parameters->errors()->error(format, args);
1641 break;
1642 case LDPL_FATAL:
1643 parameters->errors()->fatal(format, args);
1644 break;
1647 va_end(args);
1648 return LDPS_OK;
1651 // Get the section count of the object corresponding to the handle. This
1652 // plugin interface can only be called in the claim_file handler of the plugin.
1654 static enum ld_plugin_status
1655 get_input_section_count(const void* handle, unsigned int* count)
1657 gold_assert(parameters->options().has_plugins());
1659 if (!parameters->options().plugins()->in_claim_file_handler())
1660 return LDPS_ERR;
1662 Object* obj = parameters->options().plugins()->get_elf_object(handle);
1664 if (obj == NULL)
1665 return LDPS_ERR;
1667 *count = obj->shnum();
1668 return LDPS_OK;
1671 // Get the type of the specified section in the object corresponding
1672 // to the handle. This plugin interface can only be called in the
1673 // claim_file handler of the plugin.
1675 static enum ld_plugin_status
1676 get_input_section_type(const struct ld_plugin_section section,
1677 unsigned int* type)
1679 gold_assert(parameters->options().has_plugins());
1681 if (!parameters->options().plugins()->in_claim_file_handler())
1682 return LDPS_ERR;
1684 Object* obj
1685 = parameters->options().plugins()->get_elf_object(section.handle);
1687 if (obj == NULL)
1688 return LDPS_BAD_HANDLE;
1690 *type = obj->section_type(section.shndx);
1691 return LDPS_OK;
1694 // Get the name of the specified section in the object corresponding
1695 // to the handle. This plugin interface can only be called in the
1696 // claim_file handler of the plugin.
1698 static enum ld_plugin_status
1699 get_input_section_name(const struct ld_plugin_section section,
1700 char** section_name_ptr)
1702 gold_assert(parameters->options().has_plugins());
1704 if (!parameters->options().plugins()->in_claim_file_handler())
1705 return LDPS_ERR;
1707 Object* obj
1708 = parameters->options().plugins()->get_elf_object(section.handle);
1710 if (obj == NULL)
1711 return LDPS_BAD_HANDLE;
1713 // Check if the object is locked before getting the section name.
1714 gold_assert(obj->is_locked());
1716 const std::string section_name = obj->section_name(section.shndx);
1717 *section_name_ptr = static_cast<char*>(malloc(section_name.length() + 1));
1718 memcpy(*section_name_ptr, section_name.c_str(), section_name.length() + 1);
1719 return LDPS_OK;
1722 // Get the contents of the specified section in the object corresponding
1723 // to the handle. This plugin interface can only be called in the
1724 // claim_file handler of the plugin.
1726 static enum ld_plugin_status
1727 get_input_section_contents(const struct ld_plugin_section section,
1728 const unsigned char** section_contents_ptr,
1729 size_t* len)
1731 gold_assert(parameters->options().has_plugins());
1733 if (!parameters->options().plugins()->in_claim_file_handler())
1734 return LDPS_ERR;
1736 Object* obj
1737 = parameters->options().plugins()->get_elf_object(section.handle);
1739 if (obj == NULL)
1740 return LDPS_BAD_HANDLE;
1742 // Check if the object is locked before getting the section contents.
1743 gold_assert(obj->is_locked());
1745 section_size_type plen;
1746 *section_contents_ptr
1747 = obj->section_contents(section.shndx, &plen, false);
1748 *len = plen;
1749 return LDPS_OK;
1752 // Get the alignment of the specified section in the object corresponding
1753 // to the handle. This plugin interface can only be called in the
1754 // claim_file handler of the plugin.
1756 static enum ld_plugin_status
1757 get_input_section_alignment(const struct ld_plugin_section section,
1758 unsigned int* addralign)
1760 gold_assert(parameters->options().has_plugins());
1762 if (!parameters->options().plugins()->in_claim_file_handler())
1763 return LDPS_ERR;
1765 Object* obj
1766 = parameters->options().plugins()->get_elf_object(section.handle);
1768 if (obj == NULL)
1769 return LDPS_BAD_HANDLE;
1771 *addralign = obj->section_addralign(section.shndx);
1772 return LDPS_OK;
1775 // Get the size of the specified section in the object corresponding
1776 // to the handle. This plugin interface can only be called in the
1777 // claim_file handler of the plugin.
1779 static enum ld_plugin_status
1780 get_input_section_size(const struct ld_plugin_section section,
1781 uint64_t* secsize)
1783 gold_assert(parameters->options().has_plugins());
1785 if (!parameters->options().plugins()->in_claim_file_handler())
1786 return LDPS_ERR;
1788 Object* obj
1789 = parameters->options().plugins()->get_elf_object(section.handle);
1791 if (obj == NULL)
1792 return LDPS_BAD_HANDLE;
1794 *secsize = obj->section_size(section.shndx);
1795 return LDPS_OK;
1799 // Specify the ordering of sections in the final layout. The sections are
1800 // specified as (handle,shndx) pairs in the two arrays in the order in
1801 // which they should appear in the final layout.
1803 static enum ld_plugin_status
1804 update_section_order(const struct ld_plugin_section* section_list,
1805 unsigned int num_sections)
1807 gold_assert(parameters->options().has_plugins());
1809 if (num_sections == 0)
1810 return LDPS_OK;
1812 if (section_list == NULL)
1813 return LDPS_ERR;
1815 Layout* layout = parameters->options().plugins()->layout();
1816 gold_assert (layout != NULL);
1818 std::map<Section_id, unsigned int>* order_map
1819 = layout->get_section_order_map();
1821 /* Store the mapping from Section_id to section position in layout's
1822 order_map to consult after output sections are added. */
1823 for (unsigned int i = 0; i < num_sections; ++i)
1825 Object* obj = parameters->options().plugins()->get_elf_object(
1826 section_list[i].handle);
1827 if (obj == NULL || obj->is_dynamic())
1828 return LDPS_BAD_HANDLE;
1829 unsigned int shndx = section_list[i].shndx;
1830 Section_id secn_id(static_cast<Relobj*>(obj), shndx);
1831 (*order_map)[secn_id] = i + 1;
1834 return LDPS_OK;
1837 // Let the linker know that the sections could be reordered.
1839 static enum ld_plugin_status
1840 allow_section_ordering()
1842 gold_assert(parameters->options().has_plugins());
1843 Layout* layout = parameters->options().plugins()->layout();
1844 layout->set_section_ordering_specified();
1845 return LDPS_OK;
1848 // Let the linker know that a subset of sections could be mapped
1849 // to a unique segment.
1851 static enum ld_plugin_status
1852 allow_unique_segment_for_sections()
1854 gold_assert(parameters->options().has_plugins());
1855 Layout* layout = parameters->options().plugins()->layout();
1856 layout->set_unique_segment_for_sections_specified();
1857 return LDPS_OK;
1860 // This function should map the list of sections specified in the
1861 // SECTION_LIST to a unique segment. ELF segments do not have names
1862 // and the NAME is used to identify Output Section which should contain
1863 // the list of sections. This Output Section will then be mapped to
1864 // a unique segment. FLAGS is used to specify if any additional segment
1865 // flags need to be set. For instance, a specific segment flag can be
1866 // set to identify this segment. Unsetting segment flags is not possible.
1867 // ALIGN specifies the alignment of the segment.
1869 static enum ld_plugin_status
1870 unique_segment_for_sections(const char* segment_name,
1871 uint64_t flags,
1872 uint64_t align,
1873 const struct ld_plugin_section* section_list,
1874 unsigned int num_sections)
1876 gold_assert(parameters->options().has_plugins());
1878 if (num_sections == 0)
1879 return LDPS_OK;
1881 if (section_list == NULL)
1882 return LDPS_ERR;
1884 Layout* layout = parameters->options().plugins()->layout();
1885 gold_assert (layout != NULL);
1887 Layout::Unique_segment_info* s = new Layout::Unique_segment_info;
1888 s->name = segment_name;
1889 s->flags = flags;
1890 s->align = align;
1892 for (unsigned int i = 0; i < num_sections; ++i)
1894 Object* obj = parameters->options().plugins()->get_elf_object(
1895 section_list[i].handle);
1896 if (obj == NULL || obj->is_dynamic())
1897 return LDPS_BAD_HANDLE;
1898 unsigned int shndx = section_list[i].shndx;
1899 Const_section_id secn_id(static_cast<Relobj*>(obj), shndx);
1900 layout->insert_section_segment_map(secn_id, s);
1903 return LDPS_OK;
1906 #endif // ENABLE_PLUGINS
1908 // Allocate a Pluginobj object of the appropriate size and endianness.
1910 static Pluginobj*
1911 make_sized_plugin_object(Input_file* input_file, off_t offset, off_t filesize)
1913 Pluginobj* obj = NULL;
1915 parameters_force_valid_target();
1916 const Target& target(parameters->target());
1918 if (target.get_size() == 32)
1920 if (target.is_big_endian())
1921 #ifdef HAVE_TARGET_32_BIG
1922 obj = new Sized_pluginobj<32, true>(input_file->filename(),
1923 input_file, offset, filesize);
1924 #else
1925 gold_error(_("%s: not configured to support "
1926 "32-bit big-endian object"),
1927 input_file->filename().c_str());
1928 #endif
1929 else
1930 #ifdef HAVE_TARGET_32_LITTLE
1931 obj = new Sized_pluginobj<32, false>(input_file->filename(),
1932 input_file, offset, filesize);
1933 #else
1934 gold_error(_("%s: not configured to support "
1935 "32-bit little-endian object"),
1936 input_file->filename().c_str());
1937 #endif
1939 else if (target.get_size() == 64)
1941 if (target.is_big_endian())
1942 #ifdef HAVE_TARGET_64_BIG
1943 obj = new Sized_pluginobj<64, true>(input_file->filename(),
1944 input_file, offset, filesize);
1945 #else
1946 gold_error(_("%s: not configured to support "
1947 "64-bit big-endian object"),
1948 input_file->filename().c_str());
1949 #endif
1950 else
1951 #ifdef HAVE_TARGET_64_LITTLE
1952 obj = new Sized_pluginobj<64, false>(input_file->filename(),
1953 input_file, offset, filesize);
1954 #else
1955 gold_error(_("%s: not configured to support "
1956 "64-bit little-endian object"),
1957 input_file->filename().c_str());
1958 #endif
1961 gold_assert(obj != NULL);
1962 return obj;
1965 } // End namespace gold.