1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2018 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.
34 #elif defined (HAVE_WINDOWS_H)
37 #error Unknown how to handle dynamic-load-libraries.
40 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
42 #define RTLD_NOW 0 /* Dummy value. */
44 dlopen(const char *file
, int mode ATTRIBUTE_UNUSED
)
46 return LoadLibrary(file
);
50 dlsym(void *handle
, const char *name
)
52 return reinterpret_cast<void *>(
53 GetProcAddress(static_cast<HMODULE
>(handle
),name
));
59 return "unable to load dll";
62 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
63 #endif /* ENABLE_PLUGINS */
65 #include "parameters.h"
74 #include "descriptors.h"
82 // The linker's exported interfaces.
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
,
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
,
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
,
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
,
170 static enum ld_plugin_status
171 register_new_input(ld_plugin_new_input_handler handler
);
175 #endif // ENABLE_PLUGINS
177 static Pluginobj
* make_sized_plugin_object(Input_file
* input_file
,
178 off_t offset
, off_t filesize
);
182 // Load one plugin library.
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
)
193 gold_error(_("%s: could not load plugin library: %s"),
194 this->filename_
.c_str(), dlerror());
198 // Find the plugin's onload entry point.
199 void* ptr
= dlsym(this->handle_
, "onload");
202 gold_error(_("%s: could not find onload entry point"),
203 this->filename_
.c_str());
206 ld_plugin_onload onload
;
207 gold_assert(sizeof(onload
) == sizeof(ptr
));
208 memcpy(&onload
, &ptr
, sizeof(ptr
));
210 // Get the linker's version number.
211 const char* ver
= get_version_string();
214 sscanf(ver
, "%d.%d", &major
, &minor
);
216 // Allocate and populate a transfer vector.
217 const int tv_fixed_size
= 30;
219 int tv_size
= this->args_
.size() + tv_fixed_size
;
220 ld_plugin_tv
* tv
= new ld_plugin_tv
[tv_size
];
222 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
223 // while processing subsequent entries.
225 tv
[i
].tv_tag
= LDPT_MESSAGE
;
226 tv
[i
].tv_u
.tv_message
= message
;
229 tv
[i
].tv_tag
= LDPT_API_VERSION
;
230 tv
[i
].tv_u
.tv_val
= LD_PLUGIN_API_VERSION
;
233 tv
[i
].tv_tag
= LDPT_GOLD_VERSION
;
234 tv
[i
].tv_u
.tv_val
= major
* 100 + minor
;
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
;
245 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
248 tv
[i
].tv_tag
= LDPT_OUTPUT_NAME
;
249 tv
[i
].tv_u
.tv_string
= parameters
->options().output();
251 for (unsigned int j
= 0; j
< this->args_
.size(); ++j
)
254 tv
[i
].tv_tag
= LDPT_OPTION
;
255 tv
[i
].tv_u
.tv_string
= this->args_
[j
].c_str();
259 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
260 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
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
;
267 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
268 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
271 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
272 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
275 tv
[i
].tv_tag
= LDPT_GET_INPUT_FILE
;
276 tv
[i
].tv_u
.tv_get_input_file
= get_input_file
;
279 tv
[i
].tv_tag
= LDPT_GET_VIEW
;
280 tv
[i
].tv_u
.tv_get_view
= get_view
;
283 tv
[i
].tv_tag
= LDPT_RELEASE_INPUT_FILE
;
284 tv
[i
].tv_u
.tv_release_input_file
= release_input_file
;
287 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
288 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
291 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V2
;
292 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v2
;
295 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V3
;
296 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v3
;
299 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
300 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
303 tv
[i
].tv_tag
= LDPT_ADD_INPUT_LIBRARY
;
304 tv
[i
].tv_u
.tv_add_input_library
= add_input_library
;
307 tv
[i
].tv_tag
= LDPT_SET_EXTRA_LIBRARY_PATH
;
308 tv
[i
].tv_u
.tv_set_extra_library_path
= set_extra_library_path
;
311 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_COUNT
;
312 tv
[i
].tv_u
.tv_get_input_section_count
= get_input_section_count
;
315 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_TYPE
;
316 tv
[i
].tv_u
.tv_get_input_section_type
= get_input_section_type
;
319 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_NAME
;
320 tv
[i
].tv_u
.tv_get_input_section_name
= get_input_section_name
;
323 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_CONTENTS
;
324 tv
[i
].tv_u
.tv_get_input_section_contents
= get_input_section_contents
;
327 tv
[i
].tv_tag
= LDPT_UPDATE_SECTION_ORDER
;
328 tv
[i
].tv_u
.tv_update_section_order
= update_section_order
;
331 tv
[i
].tv_tag
= LDPT_ALLOW_SECTION_ORDERING
;
332 tv
[i
].tv_u
.tv_allow_section_ordering
= allow_section_ordering
;
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
;
340 tv
[i
].tv_tag
= LDPT_UNIQUE_SEGMENT_FOR_SECTIONS
;
341 tv
[i
].tv_u
.tv_unique_segment_for_sections
= unique_segment_for_sections
;
344 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_ALIGNMENT
;
345 tv
[i
].tv_u
.tv_get_input_section_alignment
= get_input_section_alignment
;
348 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_SIZE
;
349 tv
[i
].tv_u
.tv_get_input_section_size
= get_input_section_size
;
352 tv
[i
].tv_tag
= LDPT_REGISTER_NEW_INPUT_HOOK
;
353 tv
[i
].tv_u
.tv_register_new_input
= register_new_input
;
356 tv
[i
].tv_tag
= LDPT_NULL
;
357 tv
[i
].tv_u
.tv_val
= 0;
359 gold_assert(i
== tv_size
- 1);
361 // Call the onload entry point.
365 #endif // ENABLE_PLUGINS
368 // Call the plugin claim-file handler.
371 Plugin::claim_file(struct ld_plugin_input_file
* plugin_input_file
)
375 if (this->claim_file_handler_
!= NULL
)
377 (*this->claim_file_handler_
)(plugin_input_file
, &claimed
);
384 // Call the all-symbols-read handler.
387 Plugin::all_symbols_read()
389 if (this->all_symbols_read_handler_
!= NULL
)
390 (*this->all_symbols_read_handler_
)();
393 // Call the new_input handler.
396 Plugin::new_input(struct ld_plugin_input_file
* plugin_input_file
)
398 if (this->new_input_handler_
!= NULL
)
399 (*this->new_input_handler_
)(plugin_input_file
);
402 // Call the cleanup handler.
407 if (this->cleanup_handler_
!= NULL
&& !this->cleanup_done_
)
409 // Set this flag before calling to prevent a recursive plunge
410 // in the event that a plugin's cleanup handler issues a
412 this->cleanup_done_
= true;
413 (*this->cleanup_handler_
)();
417 // This task is used to rescan archives as needed.
419 class Plugin_rescan
: public Task
422 Plugin_rescan(Task_token
* this_blocker
, Task_token
* next_blocker
)
423 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
428 delete this->this_blocker_
;
434 if (this->this_blocker_
->is_blocked())
435 return this->this_blocker_
;
440 locks(Task_locker
* tl
)
441 { tl
->add(this, this->next_blocker_
); }
445 { parameters
->options().plugins()->rescan(this); }
449 { return "Plugin_rescan"; }
452 Task_token
* this_blocker_
;
453 Task_token
* next_blocker_
;
456 // Plugin_manager methods.
458 Plugin_manager::~Plugin_manager()
460 for (Plugin_list::iterator p
= this->plugins_
.begin();
461 p
!= this->plugins_
.end();
464 this->plugins_
.clear();
465 for (Object_list::iterator obj
= this->objects_
.begin();
466 obj
!= this->objects_
.end();
469 this->objects_
.clear();
473 // Load all plugin libraries.
476 Plugin_manager::load_plugins(Layout
* layout
)
478 this->layout_
= layout
;
479 for (this->current_
= this->plugins_
.begin();
480 this->current_
!= this->plugins_
.end();
482 (*this->current_
)->load();
485 // Call the plugin claim-file handlers in turn to see if any claim the file.
488 Plugin_manager::claim_file(Input_file
* input_file
, off_t offset
,
489 off_t filesize
, Object
* elf_object
)
491 bool lock_initialized
= this->initialize_lock_
.initialize();
493 gold_assert(lock_initialized
);
494 Hold_lock
hl(*this->lock_
);
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;
507 for (this->current_
= this->plugins_
.begin();
508 this->current_
!= this->plugins_
.end();
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_
)
515 if ((*this->current_
)->claim_file(&this->plugin_input_file_
))
517 this->any_claimed_
= true;
518 this->in_claim_file_handler_
= false;
520 if (this->objects_
.size() > handle
521 && this->objects_
[handle
]->pluginobj() != NULL
)
522 return this->objects_
[handle
]->pluginobj();
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
);
532 (*this->current_
)->new_input(&this->plugin_input_file_
);
536 this->in_claim_file_handler_
= false;
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.
546 Plugin_manager::save_archive(Archive
* archive
)
548 if (this->in_replacement_phase_
|| !this->any_claimed_
)
551 this->rescannable_
.push_back(Rescannable(archive
));
554 // Save an Input_group. This is like save_archive.
557 Plugin_manager::save_input_group(Input_group
* input_group
)
559 if (this->in_replacement_phase_
|| !this->any_claimed_
)
562 this->rescannable_
.push_back(Rescannable(input_group
));
565 // Call the all-symbols-read handlers.
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
)
574 this->in_replacement_phase_
= true;
575 this->workqueue_
= workqueue
;
577 this->input_objects_
= input_objects
;
578 this->symtab_
= symtab
;
579 this->dirpath_
= dirpath
;
580 this->mapfile_
= mapfile
;
581 this->this_blocker_
= NULL
;
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_
);
588 for (this->current_
= this->plugins_
.begin();
589 this->current_
!= this->plugins_
.end();
591 (*this->current_
)->all_symbols_read();
593 if (this->any_added_
)
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
;
601 *last_blocker
= this->this_blocker_
;
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.
609 Plugin_manager::new_undefined_symbol(Symbol
* sym
)
611 if (this->in_replacement_phase_
)
612 this->undefined_symbols_
.push_back(sym
);
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.
620 Plugin_manager::rescan(Task
* task
)
622 size_t rescan_pos
= 0;
623 size_t rescan_size
= this->rescannable_
.size();
624 while (!this->undefined_symbols_
.empty())
626 if (rescan_pos
>= rescan_size
)
628 this->undefined_symbols_
.clear();
632 Undefined_symbol_list undefs
;
633 undefs
.reserve(this->undefined_symbols_
.size());
634 this->undefined_symbols_
.swap(undefs
);
636 size_t min_rescan_pos
= rescan_size
;
638 for (Undefined_symbol_list::const_iterator p
= undefs
.begin();
642 if (!(*p
)->is_undefined())
645 this->undefined_symbols_
.push_back(*p
);
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
653 for (size_t i
= rescan_pos
; i
< min_rescan_pos
; ++i
)
655 if (this->rescannable_defines(i
, *p
))
663 if (min_rescan_pos
>= rescan_size
)
665 // We didn't find any rescannable archives which define any
666 // undefined symbols.
670 const Rescannable
& r(this->rescannable_
[min_rescan_pos
]);
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_
);
679 size_t next_saw_undefined
= this->symtab_
->saw_undefined();
680 size_t saw_undefined
;
683 saw_undefined
= next_saw_undefined
;
685 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
686 p
!= r
.u
.input_group
->end();
689 Task_lock_obj
<Archive
> tl(task
, *p
);
691 (*p
)->add_symbols(this->symtab_
, this->layout_
,
692 this->input_objects_
, this->mapfile_
);
695 next_saw_undefined
= this->symtab_
->saw_undefined();
697 while (saw_undefined
!= next_saw_undefined
);
700 for (size_t i
= rescan_pos
; i
< min_rescan_pos
+ 1; ++i
)
702 if (this->rescannable_
[i
].is_archive
)
703 delete this->rescannable_
[i
].u
.archive
;
705 delete this->rescannable_
[i
].u
.input_group
;
708 rescan_pos
= min_rescan_pos
+ 1;
712 // Return whether the rescannable at index I defines SYM.
715 Plugin_manager::rescannable_defines(size_t i
, Symbol
* sym
)
717 const Rescannable
& r(this->rescannable_
[i
]);
719 return r
.u
.archive
->defines_symbol(sym
);
722 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
723 p
!= r
.u
.input_group
->end();
726 if ((*p
)->defines_symbol(sym
))
733 // Layout deferred objects.
736 Plugin_manager::layout_deferred_objects()
738 Deferred_layout_list::iterator obj
;
740 for (obj
= this->deferred_layout_objects_
.begin();
741 obj
!= this->deferred_layout_objects_
.end();
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_
);
753 // Call the cleanup handlers.
756 Plugin_manager::cleanup()
758 if (this->any_added_
)
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();
766 for (this->current_
= this->plugins_
.begin();
767 this->current_
!= this->plugins_
.end();
769 (*this->current_
)->cleanup();
772 // Make a new Pluginobj object. This is called when the plugin calls
773 // the add_symbols API.
776 Plugin_manager::make_plugin_object(unsigned int handle
)
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
)
783 Pluginobj
* obj
= make_sized_plugin_object(this->input_file_
,
784 this->plugin_input_file_
.offset
,
785 this->plugin_input_file_
.filesize
);
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();
793 this->objects_
.push_back(obj
);
797 // Get the input file information with an open (possibly re-opened)
801 Plugin_manager::get_input_file(unsigned int handle
,
802 struct ld_plugin_input_file
* file
)
804 Pluginobj
* obj
= this->object(handle
)->pluginobj();
806 return LDPS_BAD_HANDLE
;
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
);
817 // Release the input file.
820 Plugin_manager::release_input_file(unsigned int handle
)
822 if (this->object(handle
) == NULL
)
823 return LDPS_BAD_HANDLE
;
825 Pluginobj
* obj
= this->object(handle
)->pluginobj();
828 return LDPS_BAD_HANDLE
;
830 obj
->unlock(this->task_
);
834 // Get the elf object corresponding to the handle. Return NULL if we
835 // found a Pluginobj instead.
838 Plugin_manager::get_elf_object(const void* handle
)
840 Object
* obj
= this->object(
841 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
843 // The object should not be a Pluginobj.
845 || obj
->pluginobj() != NULL
)
852 Plugin_manager::get_view(unsigned int handle
, const void **viewp
)
856 Input_file
*input_file
;
857 if (this->in_claim_file_handler_
)
859 // We are being called from the claim_file hook.
860 const struct ld_plugin_input_file
&f
= this->plugin_input_file_
;
862 filesize
= f
.filesize
;
863 input_file
= this->input_file_
;
867 // An already claimed file.
868 if (this->object(handle
) == NULL
)
869 return LDPS_BAD_HANDLE
;
870 Pluginobj
* obj
= this->object(handle
)->pluginobj();
872 return LDPS_BAD_HANDLE
;
873 offset
= obj
->offset();
874 filesize
= obj
->filesize();
875 input_file
= obj
->input_file();
877 *viewp
= (void*) input_file
->file().get_view(offset
, 0, filesize
, false,
882 // Add a new library path.
885 Plugin_manager::set_extra_library_path(const char* path
)
887 this->extra_search_path_
= std::string(path
);
891 // Add a new input file.
894 Plugin_manager::add_input_file(const char* pathname
, bool is_lib
)
896 Input_file_argument
file(pathname
,
898 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
899 : Input_file_argument::INPUT_FILE_TYPE_FILE
),
901 ? this->extra_search_path_
.c_str()
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 "
911 this->workqueue_
->queue_soon(new Read_symbols(this->input_objects_
,
922 this->this_blocker_
= next_blocker
;
923 this->any_added_
= true;
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_()
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".
942 is_referenced_from_outside(Symbol
* lsym
)
944 if (lsym
->in_real_elf())
946 if (parameters
->options().relocatable())
948 if (parameters
->options().is_undefined(lsym
->name()))
953 // Return TRUE if a defined symbol might be reachable from outside the
957 is_visible_from_outside(Symbol
* lsym
)
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();
968 // Get symbol resolution info.
971 Pluginobj::get_symbol_resolution_info(Symbol_table
* symtab
,
973 ld_plugin_symbol
* syms
,
976 // For version 1 of this interface, we cannot use
977 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
979 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
981 ? LDPR_PREVAILING_DEF_IRONLY_EXP
982 : LDPR_PREVAILING_DEF
);
984 if (nsyms
> this->nsyms_
)
987 if (static_cast<size_t>(nsyms
) > this->symbols_
.size())
989 // We never decided to include this object. We mark all symbols as
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
;
997 Plugin_manager
* plugins
= parameters
->options().plugins();
998 for (int i
= 0; i
< nsyms
; i
++)
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
;
1006 if (plugins
->is_defsym_def(lsym
->name()))
1008 // The symbol is redefined via defsym.
1009 res
= LDPR_PREEMPTED_REG
;
1011 else if (lsym
->is_undefined())
1013 // The symbol remains undefined.
1016 else if (isym
->def
== LDPK_UNDEF
1017 || isym
->def
== LDPK_WEAKUNDEF
1018 || isym
->def
== LDPK_COMMON
)
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)
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
;
1030 res
= LDPR_PREVAILING_DEF_IRONLY
;
1032 else if (lsym
->object()->pluginobj() != NULL
)
1033 res
= LDPR_RESOLVED_IR
;
1034 else if (lsym
->object()->is_dynamic())
1035 res
= LDPR_RESOLVED_DYN
;
1037 res
= LDPR_RESOLVED_EXEC
;
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))
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
;
1051 res
= LDPR_PREVAILING_DEF_IRONLY
;
1054 res
= (lsym
->object()->pluginobj() != NULL
1056 : LDPR_PREEMPTED_REG
);
1058 isym
->resolution
= res
;
1063 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1067 Pluginobj::include_comdat_group(std::string comdat_key
, Layout
* layout
)
1069 std::pair
<Comdat_map::iterator
, bool> ins
=
1070 this->comdat_map_
.insert(std::make_pair(comdat_key
, false));
1072 // If this is the first time we've seen this comdat key, ask the
1073 // layout object whether it should be included.
1075 ins
.first
->second
= layout
->find_or_add_kept_section(comdat_key
,
1079 return ins
.first
->second
;
1082 // Class Sized_pluginobj.
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
,
1090 : Pluginobj(name
, input_file
, offset
, filesize
)
1094 // Read the symbols. Not used for plugin objects.
1096 template<int size
, bool big_endian
>
1098 Sized_pluginobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1103 // Lay out the input sections. Not used for plugin objects.
1105 template<int size
, bool big_endian
>
1107 Sized_pluginobj
<size
, big_endian
>::do_layout(Symbol_table
*, Layout
*,
1113 // Add the symbols to the symbol table.
1115 template<int size
, bool big_endian
>
1117 Sized_pluginobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
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
);
1126 this->symbols_
.resize(this->nsyms_
);
1128 for (int i
= 0; i
< this->nsyms_
; ++i
)
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
;
1137 if (name
!= NULL
&& name
[0] == '\0')
1139 if (ver
!= NULL
&& ver
[0] == '\0')
1145 case LDPK_WEAKUNDEF
:
1146 bind
= elfcpp::STB_WEAK
;
1152 bind
= elfcpp::STB_GLOBAL
;
1160 shndx
= elfcpp::SHN_ABS
;
1163 shndx
= elfcpp::SHN_COMMON
;
1166 case LDPK_WEAKUNDEF
:
1168 shndx
= elfcpp::SHN_UNDEF
;
1172 switch (isym
->visibility
)
1174 case LDPV_PROTECTED
:
1175 vis
= elfcpp::STV_PROTECTED
;
1178 vis
= elfcpp::STV_INTERNAL
;
1181 vis
= elfcpp::STV_HIDDEN
;
1185 vis
= elfcpp::STV_DEFAULT
;
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
;
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
);
1202 symtab
->add_from_pluginobj
<size
, big_endian
>(this, name
, ver
, &sym
);
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
,
1214 char* tmpbuf
= NULL
;
1215 size_t tmpbuflen
= 0;
1217 for (int i
= 0; i
< this->nsyms_
; ++i
)
1219 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1220 if (sym
.def
== LDPK_UNDEF
|| sym
.def
== LDPK_WEAKUNDEF
)
1222 const char* name
= sym
.name
;
1224 Archive::Should_include t
= Archive::should_include_member(symtab
,
1230 if (t
== Archive::SHOULD_INCLUDE_YES
)
1239 return Archive::SHOULD_INCLUDE_UNKNOWN
;
1242 // Iterate over global symbols, calling a visitor class V for each.
1244 template<int size
, bool big_endian
>
1246 Sized_pluginobj
<size
, big_endian
>::do_for_all_global_symbols(
1248 Library_base::Symbol_visitor_base
* v
)
1250 for (int i
= 0; i
< this->nsyms_
; ++i
)
1252 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1253 if (sym
.def
!= LDPK_UNDEF
)
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
>
1262 Sized_pluginobj
<size
, big_endian
>::do_for_all_local_got_entries(
1263 Got_offset_list::Visitor
*) const
1268 // Get the size of a section. Not used for plugin objects.
1270 template<int size
, bool big_endian
>
1272 Sized_pluginobj
<size
, big_endian
>::do_section_size(unsigned int)
1278 // Get the name of a section. Not used for plugin objects.
1280 template<int size
, bool big_endian
>
1282 Sized_pluginobj
<size
, big_endian
>::do_section_name(unsigned int) const
1285 return std::string();
1288 // Return a view of the contents of a section. Not used for plugin objects.
1290 template<int size
, bool big_endian
>
1291 const unsigned char*
1292 Sized_pluginobj
<size
, big_endian
>::do_section_contents(
1301 // Return section flags. Not used for plugin objects.
1303 template<int size
, bool big_endian
>
1305 Sized_pluginobj
<size
, big_endian
>::do_section_flags(unsigned int)
1311 // Return section entsize. Not used for plugin objects.
1313 template<int size
, bool big_endian
>
1315 Sized_pluginobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1321 // Return section address. Not used for plugin objects.
1323 template<int size
, bool big_endian
>
1325 Sized_pluginobj
<size
, big_endian
>::do_section_address(unsigned int)
1331 // Return section type. Not used for plugin objects.
1333 template<int size
, bool big_endian
>
1335 Sized_pluginobj
<size
, big_endian
>::do_section_type(unsigned int)
1341 // Return the section link field. Not used for plugin objects.
1343 template<int size
, bool big_endian
>
1345 Sized_pluginobj
<size
, big_endian
>::do_section_link(unsigned int)
1351 // Return the section link field. Not used for plugin objects.
1353 template<int size
, bool big_endian
>
1355 Sized_pluginobj
<size
, big_endian
>::do_section_info(unsigned int)
1361 // Return the section alignment. Not used for plugin objects.
1363 template<int size
, bool big_endian
>
1365 Sized_pluginobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1371 // Return the Xindex structure to use. Not used for plugin objects.
1373 template<int size
, bool big_endian
>
1375 Sized_pluginobj
<size
, big_endian
>::do_initialize_xindex()
1381 // Get symbol counts. Don't count plugin objects; the replacement
1382 // files will provide the counts.
1384 template<int size
, bool big_endian
>
1386 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbol_counts(
1387 const Symbol_table
*,
1395 // Get symbols. Not used for plugin objects.
1397 template<int size
, bool big_endian
>
1398 const Object::Symbols
*
1399 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbols() const
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.
1410 class Plugin_finish
: public Task
1413 Plugin_finish(Task_token
* this_blocker
, Task_token
* next_blocker
)
1414 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
1419 if (this->this_blocker_
!= NULL
)
1420 delete this->this_blocker_
;
1426 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1427 return this->this_blocker_
;
1432 locks(Task_locker
* tl
)
1433 { tl
->add(this, this->next_blocker_
); }
1438 // We could call early cleanup handlers here.
1443 { return "Plugin_finish"; }
1446 Task_token
* this_blocker_
;
1447 Task_token
* next_blocker_
;
1450 // Class Plugin_hook.
1452 Plugin_hook::~Plugin_hook()
1456 // Return whether a Plugin_hook task is runnable.
1459 Plugin_hook::is_runnable()
1461 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1462 return this->this_blocker_
;
1466 // Return a Task_locker for a Plugin_hook task. We don't need any
1470 Plugin_hook::locks(Task_locker
*)
1474 // Run the "all symbols read" plugin hook.
1477 Plugin_hook::run(Workqueue
* workqueue
)
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();
1484 this->options_
.plugins()->all_symbols_read(workqueue
,
1486 this->input_objects_
,
1490 &this->this_blocker_
);
1491 workqueue
->queue_soon(new Plugin_finish(this->this_blocker_
,
1492 this->next_blocker_
));
1495 // The C interface routines called by the plugins.
1497 #ifdef ENABLE_PLUGINS
1499 // Register a claim-file handler.
1501 static enum ld_plugin_status
1502 register_claim_file(ld_plugin_claim_file_handler handler
)
1504 gold_assert(parameters
->options().has_plugins());
1505 parameters
->options().plugins()->set_claim_file_handler(handler
);
1509 // Register an all-symbols-read handler.
1511 static enum ld_plugin_status
1512 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
)
1514 gold_assert(parameters
->options().has_plugins());
1515 parameters
->options().plugins()->set_all_symbols_read_handler(handler
);
1519 // Register a cleanup handler.
1521 static enum ld_plugin_status
1522 register_cleanup(ld_plugin_cleanup_handler handler
)
1524 gold_assert(parameters
->options().has_plugins());
1525 parameters
->options().plugins()->set_cleanup_handler(handler
);
1529 // Add symbols from a plugin-claimed input file.
1531 static enum ld_plugin_status
1532 add_symbols(void* handle
, int nsyms
, const ld_plugin_symbol
* syms
)
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
)));
1539 obj
->store_incoming_symbols(nsyms
, syms
);
1543 // Get the input file information with an open (possibly re-opened)
1546 static enum ld_plugin_status
1547 get_input_file(const void* handle
, struct ld_plugin_input_file
* file
)
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
);
1555 // Release the input file.
1557 static enum ld_plugin_status
1558 release_input_file(const void* handle
)
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
);
1566 static enum ld_plugin_status
1567 get_view(const void *handle
, const void **viewp
)
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
);
1575 // Get the symbol resolution info for a plugin-claimed input file.
1577 static enum ld_plugin_status
1578 get_symbols(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
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
)));
1586 Pluginobj
* plugin_obj
= obj
->pluginobj();
1587 if (plugin_obj
== NULL
)
1589 Symbol_table
* symtab
= plugins
->symtab();
1590 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 1);
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.
1596 static enum ld_plugin_status
1597 get_symbols_v2(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
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
)));
1605 Pluginobj
* plugin_obj
= obj
->pluginobj();
1606 if (plugin_obj
== NULL
)
1608 Symbol_table
* symtab
= plugins
->symtab();
1609 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 2);
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.
1616 static enum ld_plugin_status
1617 get_symbols_v3(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
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
)));
1625 Pluginobj
* plugin_obj
= obj
->pluginobj();
1626 if (plugin_obj
== NULL
)
1628 Symbol_table
* symtab
= plugins
->symtab();
1629 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 3);
1632 // Add a new (real) input file generated by a plugin.
1634 static enum ld_plugin_status
1635 add_input_file(const char* pathname
)
1637 gold_assert(parameters
->options().has_plugins());
1638 return parameters
->options().plugins()->add_input_file(pathname
, false);
1641 // Add a new (real) library required by a plugin.
1643 static enum ld_plugin_status
1644 add_input_library(const char* pathname
)
1646 gold_assert(parameters
->options().has_plugins());
1647 return parameters
->options().plugins()->add_input_file(pathname
, true);
1650 // Set the extra library path to be used by libraries added via
1651 // add_input_library
1653 static enum ld_plugin_status
1654 set_extra_library_path(const char* path
)
1656 gold_assert(parameters
->options().has_plugins());
1657 return parameters
->options().plugins()->set_extra_library_path(path
);
1660 // Issue a diagnostic message from a plugin.
1662 static enum ld_plugin_status
1663 message(int level
, const char* format
, ...)
1666 va_start(args
, format
);
1671 parameters
->errors()->info(format
, args
);
1674 parameters
->errors()->warning(format
, args
);
1678 parameters
->errors()->error(format
, args
);
1681 parameters
->errors()->fatal(format
, args
);
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.
1692 static enum ld_plugin_status
1693 get_input_section_count(const void* handle
, unsigned int* count
)
1695 gold_assert(parameters
->options().has_plugins());
1697 if (!parameters
->options().plugins()->in_claim_file_handler())
1700 Object
* obj
= parameters
->options().plugins()->get_elf_object(handle
);
1705 *count
= obj
->shnum();
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.
1713 static enum ld_plugin_status
1714 get_input_section_type(const struct ld_plugin_section section
,
1717 gold_assert(parameters
->options().has_plugins());
1719 if (!parameters
->options().plugins()->in_claim_file_handler())
1723 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1726 return LDPS_BAD_HANDLE
;
1728 *type
= obj
->section_type(section
.shndx
);
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.
1736 static enum ld_plugin_status
1737 get_input_section_name(const struct ld_plugin_section section
,
1738 char** section_name_ptr
)
1740 gold_assert(parameters
->options().has_plugins());
1742 if (!parameters
->options().plugins()->in_claim_file_handler())
1746 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1749 return LDPS_BAD_HANDLE
;
1751 // Check if the object is locked before getting the section name.
1752 gold_assert(obj
->is_locked());
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);
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.
1764 static enum ld_plugin_status
1765 get_input_section_contents(const struct ld_plugin_section section
,
1766 const unsigned char** section_contents_ptr
,
1769 gold_assert(parameters
->options().has_plugins());
1771 if (!parameters
->options().plugins()->in_claim_file_handler())
1775 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1778 return LDPS_BAD_HANDLE
;
1780 // Check if the object is locked before getting the section contents.
1781 gold_assert(obj
->is_locked());
1783 section_size_type plen
;
1784 *section_contents_ptr
1785 = obj
->section_contents(section
.shndx
, &plen
, false);
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.
1794 static enum ld_plugin_status
1795 get_input_section_alignment(const struct ld_plugin_section section
,
1796 unsigned int* addralign
)
1798 gold_assert(parameters
->options().has_plugins());
1800 if (!parameters
->options().plugins()->in_claim_file_handler())
1804 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1807 return LDPS_BAD_HANDLE
;
1809 *addralign
= obj
->section_addralign(section
.shndx
);
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.
1817 static enum ld_plugin_status
1818 get_input_section_size(const struct ld_plugin_section section
,
1821 gold_assert(parameters
->options().has_plugins());
1823 if (!parameters
->options().plugins()->in_claim_file_handler())
1827 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1830 return LDPS_BAD_HANDLE
;
1832 *secsize
= obj
->section_size(section
.shndx
);
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.
1841 static enum ld_plugin_status
1842 update_section_order(const struct ld_plugin_section
* section_list
,
1843 unsigned int num_sections
)
1845 gold_assert(parameters
->options().has_plugins());
1847 if (num_sections
== 0)
1850 if (section_list
== NULL
)
1853 Layout
* layout
= parameters
->options().plugins()->layout();
1854 gold_assert (layout
!= NULL
);
1856 std::map
<Section_id
, unsigned int>* order_map
1857 = layout
->get_section_order_map();
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
)
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;
1875 // Let the linker know that the sections could be reordered.
1877 static enum ld_plugin_status
1878 allow_section_ordering()
1880 gold_assert(parameters
->options().has_plugins());
1881 Layout
* layout
= parameters
->options().plugins()->layout();
1882 layout
->set_section_ordering_specified();
1886 // Let the linker know that a subset of sections could be mapped
1887 // to a unique segment.
1889 static enum ld_plugin_status
1890 allow_unique_segment_for_sections()
1892 gold_assert(parameters
->options().has_plugins());
1893 Layout
* layout
= parameters
->options().plugins()->layout();
1894 layout
->set_unique_segment_for_sections_specified();
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.
1907 static enum ld_plugin_status
1908 unique_segment_for_sections(const char* segment_name
,
1911 const struct ld_plugin_section
* section_list
,
1912 unsigned int num_sections
)
1914 gold_assert(parameters
->options().has_plugins());
1916 if (num_sections
== 0)
1919 if (section_list
== NULL
)
1922 Layout
* layout
= parameters
->options().plugins()->layout();
1923 gold_assert (layout
!= NULL
);
1925 Layout::Unique_segment_info
* s
= new Layout::Unique_segment_info
;
1926 s
->name
= segment_name
;
1930 for (unsigned int i
= 0; i
< num_sections
; ++i
)
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
);
1944 // Register a new_input handler.
1946 static enum ld_plugin_status
1947 register_new_input(ld_plugin_new_input_handler handler
)
1949 gold_assert(parameters
->options().has_plugins());
1950 parameters
->options().plugins()->set_new_input_handler(handler
);
1954 #endif // ENABLE_PLUGINS
1956 // Allocate a Pluginobj object of the appropriate size and endianness.
1959 make_sized_plugin_object(Input_file
* input_file
, off_t offset
, off_t filesize
)
1961 Pluginobj
* obj
= NULL
;
1963 parameters_force_valid_target();
1964 const Target
& target(parameters
->target());
1966 if (target
.get_size() == 32)
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
);
1973 gold_error(_("%s: not configured to support "
1974 "32-bit big-endian object"),
1975 input_file
->filename().c_str());
1978 #ifdef HAVE_TARGET_32_LITTLE
1979 obj
= new Sized_pluginobj
<32, false>(input_file
->filename(),
1980 input_file
, offset
, filesize
);
1982 gold_error(_("%s: not configured to support "
1983 "32-bit little-endian object"),
1984 input_file
->filename().c_str());
1987 else if (target
.get_size() == 64)
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
);
1994 gold_error(_("%s: not configured to support "
1995 "64-bit big-endian object"),
1996 input_file
->filename().c_str());
1999 #ifdef HAVE_TARGET_64_LITTLE
2000 obj
= new Sized_pluginobj
<64, false>(input_file
->filename(),
2001 input_file
, offset
, filesize
);
2003 gold_error(_("%s: not configured to support "
2004 "64-bit little-endian object"),
2005 input_file
->filename().c_str());
2009 gold_assert(obj
!= NULL
);
2013 } // End namespace gold.