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
);
173 static enum ld_plugin_status
174 get_wrap_symbols(uint64_t *num_symbols
, const char ***wrap_symbol_list
);
178 #endif // ENABLE_PLUGINS
180 static Pluginobj
* make_sized_plugin_object(Input_file
* input_file
,
181 off_t offset
, off_t filesize
);
185 // Load one plugin library.
190 #ifdef ENABLE_PLUGINS
191 // Load the plugin library.
192 // FIXME: Look for the library in standard locations.
193 this->handle_
= dlopen(this->filename_
.c_str(), RTLD_NOW
);
194 if (this->handle_
== NULL
)
196 gold_error(_("%s: could not load plugin library: %s"),
197 this->filename_
.c_str(), dlerror());
201 // Find the plugin's onload entry point.
202 void* ptr
= dlsym(this->handle_
, "onload");
205 gold_error(_("%s: could not find onload entry point"),
206 this->filename_
.c_str());
209 ld_plugin_onload onload
;
210 gold_assert(sizeof(onload
) == sizeof(ptr
));
211 memcpy(&onload
, &ptr
, sizeof(ptr
));
213 // Get the linker's version number.
214 const char* ver
= get_version_string();
217 sscanf(ver
, "%d.%d", &major
, &minor
);
219 // Allocate and populate a transfer vector.
220 const int tv_fixed_size
= 31;
222 int tv_size
= this->args_
.size() + tv_fixed_size
;
223 ld_plugin_tv
* tv
= new ld_plugin_tv
[tv_size
];
225 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
226 // while processing subsequent entries.
228 tv
[i
].tv_tag
= LDPT_MESSAGE
;
229 tv
[i
].tv_u
.tv_message
= message
;
232 tv
[i
].tv_tag
= LDPT_API_VERSION
;
233 tv
[i
].tv_u
.tv_val
= LD_PLUGIN_API_VERSION
;
236 tv
[i
].tv_tag
= LDPT_GOLD_VERSION
;
237 tv
[i
].tv_u
.tv_val
= major
* 100 + minor
;
240 tv
[i
].tv_tag
= LDPT_LINKER_OUTPUT
;
241 if (parameters
->options().relocatable())
242 tv
[i
].tv_u
.tv_val
= LDPO_REL
;
243 else if (parameters
->options().shared())
244 tv
[i
].tv_u
.tv_val
= LDPO_DYN
;
245 else if (parameters
->options().pie())
246 tv
[i
].tv_u
.tv_val
= LDPO_PIE
;
248 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
251 tv
[i
].tv_tag
= LDPT_OUTPUT_NAME
;
252 tv
[i
].tv_u
.tv_string
= parameters
->options().output();
254 for (unsigned int j
= 0; j
< this->args_
.size(); ++j
)
257 tv
[i
].tv_tag
= LDPT_OPTION
;
258 tv
[i
].tv_u
.tv_string
= this->args_
[j
].c_str();
262 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
263 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
266 tv
[i
].tv_tag
= LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
;
267 tv
[i
].tv_u
.tv_register_all_symbols_read
= register_all_symbols_read
;
270 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
271 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
274 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
275 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
278 tv
[i
].tv_tag
= LDPT_GET_INPUT_FILE
;
279 tv
[i
].tv_u
.tv_get_input_file
= get_input_file
;
282 tv
[i
].tv_tag
= LDPT_GET_VIEW
;
283 tv
[i
].tv_u
.tv_get_view
= get_view
;
286 tv
[i
].tv_tag
= LDPT_RELEASE_INPUT_FILE
;
287 tv
[i
].tv_u
.tv_release_input_file
= release_input_file
;
290 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
291 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
294 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V2
;
295 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v2
;
298 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V3
;
299 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v3
;
302 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
303 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
306 tv
[i
].tv_tag
= LDPT_ADD_INPUT_LIBRARY
;
307 tv
[i
].tv_u
.tv_add_input_library
= add_input_library
;
310 tv
[i
].tv_tag
= LDPT_SET_EXTRA_LIBRARY_PATH
;
311 tv
[i
].tv_u
.tv_set_extra_library_path
= set_extra_library_path
;
314 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_COUNT
;
315 tv
[i
].tv_u
.tv_get_input_section_count
= get_input_section_count
;
318 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_TYPE
;
319 tv
[i
].tv_u
.tv_get_input_section_type
= get_input_section_type
;
322 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_NAME
;
323 tv
[i
].tv_u
.tv_get_input_section_name
= get_input_section_name
;
326 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_CONTENTS
;
327 tv
[i
].tv_u
.tv_get_input_section_contents
= get_input_section_contents
;
330 tv
[i
].tv_tag
= LDPT_UPDATE_SECTION_ORDER
;
331 tv
[i
].tv_u
.tv_update_section_order
= update_section_order
;
334 tv
[i
].tv_tag
= LDPT_ALLOW_SECTION_ORDERING
;
335 tv
[i
].tv_u
.tv_allow_section_ordering
= allow_section_ordering
;
338 tv
[i
].tv_tag
= LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS
;
339 tv
[i
].tv_u
.tv_allow_unique_segment_for_sections
340 = allow_unique_segment_for_sections
;
343 tv
[i
].tv_tag
= LDPT_UNIQUE_SEGMENT_FOR_SECTIONS
;
344 tv
[i
].tv_u
.tv_unique_segment_for_sections
= unique_segment_for_sections
;
347 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_ALIGNMENT
;
348 tv
[i
].tv_u
.tv_get_input_section_alignment
= get_input_section_alignment
;
351 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_SIZE
;
352 tv
[i
].tv_u
.tv_get_input_section_size
= get_input_section_size
;
355 tv
[i
].tv_tag
= LDPT_REGISTER_NEW_INPUT_HOOK
;
356 tv
[i
].tv_u
.tv_register_new_input
= register_new_input
;
359 tv
[i
].tv_tag
= LDPT_GET_WRAP_SYMBOLS
;
360 tv
[i
].tv_u
.tv_get_wrap_symbols
= get_wrap_symbols
;
363 tv
[i
].tv_tag
= LDPT_NULL
;
364 tv
[i
].tv_u
.tv_val
= 0;
366 gold_assert(i
== tv_size
- 1);
368 // Call the onload entry point.
372 #endif // ENABLE_PLUGINS
375 // Call the plugin claim-file handler.
378 Plugin::claim_file(struct ld_plugin_input_file
* plugin_input_file
)
382 if (this->claim_file_handler_
!= NULL
)
384 (*this->claim_file_handler_
)(plugin_input_file
, &claimed
);
391 // Call the all-symbols-read handler.
394 Plugin::all_symbols_read()
396 if (this->all_symbols_read_handler_
!= NULL
)
397 (*this->all_symbols_read_handler_
)();
400 // Call the new_input handler.
403 Plugin::new_input(struct ld_plugin_input_file
* plugin_input_file
)
405 if (this->new_input_handler_
!= NULL
)
406 (*this->new_input_handler_
)(plugin_input_file
);
409 // Call the cleanup handler.
414 if (this->cleanup_handler_
!= NULL
&& !this->cleanup_done_
)
416 // Set this flag before calling to prevent a recursive plunge
417 // in the event that a plugin's cleanup handler issues a
419 this->cleanup_done_
= true;
420 (*this->cleanup_handler_
)();
424 // This task is used to rescan archives as needed.
426 class Plugin_rescan
: public Task
429 Plugin_rescan(Task_token
* this_blocker
, Task_token
* next_blocker
)
430 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
435 delete this->this_blocker_
;
441 if (this->this_blocker_
->is_blocked())
442 return this->this_blocker_
;
447 locks(Task_locker
* tl
)
448 { tl
->add(this, this->next_blocker_
); }
452 { parameters
->options().plugins()->rescan(this); }
456 { return "Plugin_rescan"; }
459 Task_token
* this_blocker_
;
460 Task_token
* next_blocker_
;
463 // Plugin_manager methods.
465 Plugin_manager::~Plugin_manager()
467 for (Plugin_list::iterator p
= this->plugins_
.begin();
468 p
!= this->plugins_
.end();
471 this->plugins_
.clear();
472 for (Object_list::iterator obj
= this->objects_
.begin();
473 obj
!= this->objects_
.end();
476 this->objects_
.clear();
480 // Load all plugin libraries.
483 Plugin_manager::load_plugins(Layout
* layout
)
485 this->layout_
= layout
;
486 for (this->current_
= this->plugins_
.begin();
487 this->current_
!= this->plugins_
.end();
489 (*this->current_
)->load();
492 // Call the plugin claim-file handlers in turn to see if any claim the file.
495 Plugin_manager::claim_file(Input_file
* input_file
, off_t offset
,
496 off_t filesize
, Object
* elf_object
)
498 bool lock_initialized
= this->initialize_lock_
.initialize();
500 gold_assert(lock_initialized
);
501 Hold_lock
hl(*this->lock_
);
503 unsigned int handle
= this->objects_
.size();
504 this->input_file_
= input_file
;
505 this->plugin_input_file_
.name
= input_file
->filename().c_str();
506 this->plugin_input_file_
.fd
= input_file
->file().descriptor();
507 this->plugin_input_file_
.offset
= offset
;
508 this->plugin_input_file_
.filesize
= filesize
;
509 this->plugin_input_file_
.handle
= reinterpret_cast<void*>(handle
);
510 if (elf_object
!= NULL
)
511 this->objects_
.push_back(elf_object
);
512 this->in_claim_file_handler_
= true;
514 for (this->current_
= this->plugins_
.begin();
515 this->current_
!= this->plugins_
.end();
518 // If we aren't yet in replacement phase, allow plugins to claim input
519 // files, otherwise notify the plugin of the new input file, if needed.
520 if (!this->in_replacement_phase_
)
522 if ((*this->current_
)->claim_file(&this->plugin_input_file_
))
524 this->any_claimed_
= true;
525 this->in_claim_file_handler_
= false;
527 if (this->objects_
.size() > handle
528 && this->objects_
[handle
]->pluginobj() != NULL
)
529 return this->objects_
[handle
]->pluginobj();
531 // If the plugin claimed the file but did not call the
532 // add_symbols callback, we need to create the Pluginobj now.
533 Pluginobj
* obj
= this->make_plugin_object(handle
);
539 (*this->current_
)->new_input(&this->plugin_input_file_
);
543 this->in_claim_file_handler_
= false;
547 // Save an archive. This is used so that a plugin can add a file
548 // which refers to a symbol which was not previously referenced. In
549 // that case we want to pretend that the symbol was referenced before,
550 // and pull in the archive object.
553 Plugin_manager::save_archive(Archive
* archive
)
555 if (this->in_replacement_phase_
|| !this->any_claimed_
)
558 this->rescannable_
.push_back(Rescannable(archive
));
561 // Save an Input_group. This is like save_archive.
564 Plugin_manager::save_input_group(Input_group
* input_group
)
566 if (this->in_replacement_phase_
|| !this->any_claimed_
)
569 this->rescannable_
.push_back(Rescannable(input_group
));
572 // Call the all-symbols-read handlers.
575 Plugin_manager::all_symbols_read(Workqueue
* workqueue
, Task
* task
,
576 Input_objects
* input_objects
,
577 Symbol_table
* symtab
,
578 Dirsearch
* dirpath
, Mapfile
* mapfile
,
579 Task_token
** last_blocker
)
581 this->in_replacement_phase_
= true;
582 this->workqueue_
= workqueue
;
584 this->input_objects_
= input_objects
;
585 this->symtab_
= symtab
;
586 this->dirpath_
= dirpath
;
587 this->mapfile_
= mapfile
;
588 this->this_blocker_
= NULL
;
590 // Set symbols used in defsym expressions as seen in real ELF.
591 Layout
*layout
= parameters
->options().plugins()->layout();
592 layout
->script_options()->set_defsym_uses_in_real_elf(symtab
);
593 layout
->script_options()->find_defsym_defs(this->defsym_defines_set_
);
595 for (this->current_
= this->plugins_
.begin();
596 this->current_
!= this->plugins_
.end();
598 (*this->current_
)->all_symbols_read();
600 if (this->any_added_
)
602 Task_token
* next_blocker
= new Task_token(true);
603 next_blocker
->add_blocker();
604 workqueue
->queue(new Plugin_rescan(this->this_blocker_
, next_blocker
));
605 this->this_blocker_
= next_blocker
;
608 *last_blocker
= this->this_blocker_
;
611 // This is called when we see a new undefined symbol. If we are in
612 // the replacement phase, this means that we may need to rescan some
613 // archives we have previously seen.
616 Plugin_manager::new_undefined_symbol(Symbol
* sym
)
618 if (this->in_replacement_phase_
)
619 this->undefined_symbols_
.push_back(sym
);
622 // Rescan archives as needed. This handles the case where a new
623 // object file added by a plugin has an undefined reference to some
624 // symbol defined in an archive.
627 Plugin_manager::rescan(Task
* task
)
629 size_t rescan_pos
= 0;
630 size_t rescan_size
= this->rescannable_
.size();
631 while (!this->undefined_symbols_
.empty())
633 if (rescan_pos
>= rescan_size
)
635 this->undefined_symbols_
.clear();
639 Undefined_symbol_list undefs
;
640 undefs
.reserve(this->undefined_symbols_
.size());
641 this->undefined_symbols_
.swap(undefs
);
643 size_t min_rescan_pos
= rescan_size
;
645 for (Undefined_symbol_list::const_iterator p
= undefs
.begin();
649 if (!(*p
)->is_undefined())
652 this->undefined_symbols_
.push_back(*p
);
654 // Find the first rescan archive which defines this symbol,
655 // starting at the current rescan position. The rescan position
656 // exists so that given -la -lb -lc we don't look for undefined
657 // symbols in -lb back in -la, but instead get the definition
658 // from -lc. Don't bother to look past the current minimum
660 for (size_t i
= rescan_pos
; i
< min_rescan_pos
; ++i
)
662 if (this->rescannable_defines(i
, *p
))
670 if (min_rescan_pos
>= rescan_size
)
672 // We didn't find any rescannable archives which define any
673 // undefined symbols.
677 const Rescannable
& r(this->rescannable_
[min_rescan_pos
]);
680 Task_lock_obj
<Archive
> tl(task
, r
.u
.archive
);
681 r
.u
.archive
->add_symbols(this->symtab_
, this->layout_
,
682 this->input_objects_
, this->mapfile_
);
686 size_t next_saw_undefined
= this->symtab_
->saw_undefined();
687 size_t saw_undefined
;
690 saw_undefined
= next_saw_undefined
;
692 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
693 p
!= r
.u
.input_group
->end();
696 Task_lock_obj
<Archive
> tl(task
, *p
);
698 (*p
)->add_symbols(this->symtab_
, this->layout_
,
699 this->input_objects_
, this->mapfile_
);
702 next_saw_undefined
= this->symtab_
->saw_undefined();
704 while (saw_undefined
!= next_saw_undefined
);
707 for (size_t i
= rescan_pos
; i
< min_rescan_pos
+ 1; ++i
)
709 if (this->rescannable_
[i
].is_archive
)
710 delete this->rescannable_
[i
].u
.archive
;
712 delete this->rescannable_
[i
].u
.input_group
;
715 rescan_pos
= min_rescan_pos
+ 1;
719 // Return whether the rescannable at index I defines SYM.
722 Plugin_manager::rescannable_defines(size_t i
, Symbol
* sym
)
724 const Rescannable
& r(this->rescannable_
[i
]);
726 return r
.u
.archive
->defines_symbol(sym
);
729 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
730 p
!= r
.u
.input_group
->end();
733 if ((*p
)->defines_symbol(sym
))
740 // Layout deferred objects.
743 Plugin_manager::layout_deferred_objects()
745 Deferred_layout_list::iterator obj
;
747 for (obj
= this->deferred_layout_objects_
.begin();
748 obj
!= this->deferred_layout_objects_
.end();
751 // Lock the object so we can read from it. This is only called
752 // single-threaded from queue_middle_tasks, so it is OK to lock.
753 // Unfortunately we have no way to pass in a Task token.
754 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
755 Task_lock_obj
<Object
> tl(dummy_task
, *obj
);
756 (*obj
)->layout_deferred_sections(this->layout_
);
760 // Call the cleanup handlers.
763 Plugin_manager::cleanup()
765 if (this->any_added_
)
767 // If any input files were added, close all the input files.
768 // This is because the plugin may want to remove them, and on
769 // Windows you are not allowed to remove an open file.
770 close_all_descriptors();
773 for (this->current_
= this->plugins_
.begin();
774 this->current_
!= this->plugins_
.end();
776 (*this->current_
)->cleanup();
779 // Make a new Pluginobj object. This is called when the plugin calls
780 // the add_symbols API.
783 Plugin_manager::make_plugin_object(unsigned int handle
)
785 // Make sure we aren't asked to make an object for the same handle twice.
786 if (this->objects_
.size() != handle
787 && this->objects_
[handle
]->pluginobj() != NULL
)
790 Pluginobj
* obj
= make_sized_plugin_object(this->input_file_
,
791 this->plugin_input_file_
.offset
,
792 this->plugin_input_file_
.filesize
);
795 // If the elf object for this file was pushed into the objects_ vector, delete
796 // it to make room for the Pluginobj as this file is claimed.
797 if (this->objects_
.size() != handle
)
798 this->objects_
.pop_back();
800 this->objects_
.push_back(obj
);
804 // Get the input file information with an open (possibly re-opened)
808 Plugin_manager::get_input_file(unsigned int handle
,
809 struct ld_plugin_input_file
* file
)
811 Pluginobj
* obj
= this->object(handle
)->pluginobj();
813 return LDPS_BAD_HANDLE
;
815 obj
->lock(this->task_
);
816 file
->name
= obj
->filename().c_str();
817 file
->fd
= obj
->descriptor();
818 file
->offset
= obj
->offset();
819 file
->filesize
= obj
->filesize();
820 file
->handle
= reinterpret_cast<void*>(handle
);
824 // Release the input file.
827 Plugin_manager::release_input_file(unsigned int handle
)
829 if (this->object(handle
) == NULL
)
830 return LDPS_BAD_HANDLE
;
832 Pluginobj
* obj
= this->object(handle
)->pluginobj();
835 return LDPS_BAD_HANDLE
;
837 obj
->unlock(this->task_
);
841 // Get the elf object corresponding to the handle. Return NULL if we
842 // found a Pluginobj instead.
845 Plugin_manager::get_elf_object(const void* handle
)
847 Object
* obj
= this->object(
848 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
850 // The object should not be a Pluginobj.
852 || obj
->pluginobj() != NULL
)
859 Plugin_manager::get_view(unsigned int handle
, const void **viewp
)
863 Input_file
*input_file
;
864 if (this->in_claim_file_handler_
)
866 // We are being called from the claim_file hook.
867 const struct ld_plugin_input_file
&f
= this->plugin_input_file_
;
869 filesize
= f
.filesize
;
870 input_file
= this->input_file_
;
874 // An already claimed file.
875 if (this->object(handle
) == NULL
)
876 return LDPS_BAD_HANDLE
;
877 Pluginobj
* obj
= this->object(handle
)->pluginobj();
879 return LDPS_BAD_HANDLE
;
880 offset
= obj
->offset();
881 filesize
= obj
->filesize();
882 input_file
= obj
->input_file();
884 *viewp
= (void*) input_file
->file().get_view(offset
, 0, filesize
, false,
889 // Add a new library path.
892 Plugin_manager::set_extra_library_path(const char* path
)
894 this->extra_search_path_
= std::string(path
);
898 // Add a new input file.
901 Plugin_manager::add_input_file(const char* pathname
, bool is_lib
)
903 Input_file_argument
file(pathname
,
905 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
906 : Input_file_argument::INPUT_FILE_TYPE_FILE
),
908 ? this->extra_search_path_
.c_str()
912 Input_argument
* input_argument
= new Input_argument(file
);
913 Task_token
* next_blocker
= new Task_token(true);
914 next_blocker
->add_blocker();
915 if (parameters
->incremental())
916 gold_error(_("input files added by plug-ins in --incremental mode not "
918 this->workqueue_
->queue_soon(new Read_symbols(this->input_objects_
,
929 this->this_blocker_
= next_blocker
;
930 this->any_added_
= true;
936 Pluginobj::Pluginobj(const std::string
& name
, Input_file
* input_file
,
937 off_t offset
, off_t filesize
)
938 : Object(name
, input_file
, false, offset
),
939 nsyms_(0), syms_(NULL
), symbols_(), filesize_(filesize
), comdat_map_()
943 // Return TRUE if a defined symbol is referenced from outside the
944 // universe of claimed objects. Only references from relocatable,
945 // non-IR (unclaimed) objects count as a reference. References from
946 // dynamic objects count only as "visible".
949 is_referenced_from_outside(Symbol
* lsym
)
951 if (lsym
->in_real_elf())
953 if (parameters
->options().relocatable())
955 if (parameters
->options().is_undefined(lsym
->name()))
960 // Return TRUE if a defined symbol might be reachable from outside the
964 is_visible_from_outside(Symbol
* lsym
)
968 if (parameters
->options().export_dynamic() || parameters
->options().shared()
969 || parameters
->options().in_dynamic_list(lsym
->name())
970 || parameters
->options().is_export_dynamic_symbol(lsym
->name()))
971 return lsym
->is_externally_visible();
975 // Get symbol resolution info.
978 Pluginobj::get_symbol_resolution_info(Symbol_table
* symtab
,
980 ld_plugin_symbol
* syms
,
983 // For version 1 of this interface, we cannot use
984 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
986 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
988 ? LDPR_PREVAILING_DEF_IRONLY_EXP
989 : LDPR_PREVAILING_DEF
);
991 if (nsyms
> this->nsyms_
)
994 if (static_cast<size_t>(nsyms
) > this->symbols_
.size())
996 // We never decided to include this object. We mark all symbols as
998 gold_assert(this->symbols_
.size() == 0);
999 for (int i
= 0; i
< nsyms
; i
++)
1000 syms
[i
].resolution
= LDPR_PREEMPTED_REG
;
1001 return version
> 2 ? LDPS_NO_SYMS
: LDPS_OK
;
1004 Plugin_manager
* plugins
= parameters
->options().plugins();
1005 for (int i
= 0; i
< nsyms
; i
++)
1007 ld_plugin_symbol
* isym
= &syms
[i
];
1008 Symbol
* lsym
= this->symbols_
[i
];
1009 if (lsym
->is_forwarder())
1010 lsym
= symtab
->resolve_forwards(lsym
);
1011 ld_plugin_symbol_resolution res
= LDPR_UNKNOWN
;
1013 if (plugins
->is_defsym_def(lsym
->name()))
1015 // The symbol is redefined via defsym.
1016 res
= LDPR_PREEMPTED_REG
;
1018 else if (lsym
->is_undefined())
1020 // The symbol remains undefined.
1023 else if (isym
->def
== LDPK_UNDEF
1024 || isym
->def
== LDPK_WEAKUNDEF
1025 || isym
->def
== LDPK_COMMON
)
1027 // The original symbol was undefined or common.
1028 if (lsym
->source() != Symbol::FROM_OBJECT
)
1029 res
= LDPR_RESOLVED_EXEC
;
1030 else if (lsym
->object()->pluginobj() == this)
1032 if (is_referenced_from_outside(lsym
))
1033 res
= LDPR_PREVAILING_DEF
;
1034 else if (is_visible_from_outside(lsym
))
1035 res
= ldpr_prevailing_def_ironly_exp
;
1037 res
= LDPR_PREVAILING_DEF_IRONLY
;
1039 else if (lsym
->object()->pluginobj() != NULL
)
1040 res
= LDPR_RESOLVED_IR
;
1041 else if (lsym
->object()->is_dynamic())
1042 res
= LDPR_RESOLVED_DYN
;
1044 res
= LDPR_RESOLVED_EXEC
;
1048 // The original symbol was a definition.
1049 if (lsym
->source() != Symbol::FROM_OBJECT
)
1050 res
= LDPR_PREEMPTED_REG
;
1051 else if (lsym
->object() == static_cast<const Object
*>(this))
1053 if (is_referenced_from_outside(lsym
))
1054 res
= LDPR_PREVAILING_DEF
;
1055 else if (is_visible_from_outside(lsym
))
1056 res
= ldpr_prevailing_def_ironly_exp
;
1058 res
= LDPR_PREVAILING_DEF_IRONLY
;
1061 res
= (lsym
->object()->pluginobj() != NULL
1063 : LDPR_PREEMPTED_REG
);
1065 isym
->resolution
= res
;
1070 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1074 Pluginobj::include_comdat_group(std::string comdat_key
, Layout
* layout
)
1076 std::pair
<Comdat_map::iterator
, bool> ins
=
1077 this->comdat_map_
.insert(std::make_pair(comdat_key
, false));
1079 // If this is the first time we've seen this comdat key, ask the
1080 // layout object whether it should be included.
1082 ins
.first
->second
= layout
->find_or_add_kept_section(comdat_key
,
1086 return ins
.first
->second
;
1089 // Class Sized_pluginobj.
1091 template<int size
, bool big_endian
>
1092 Sized_pluginobj
<size
, big_endian
>::Sized_pluginobj(
1093 const std::string
& name
,
1094 Input_file
* input_file
,
1097 : Pluginobj(name
, input_file
, offset
, filesize
)
1101 // Read the symbols. Not used for plugin objects.
1103 template<int size
, bool big_endian
>
1105 Sized_pluginobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1110 // Lay out the input sections. Not used for plugin objects.
1112 template<int size
, bool big_endian
>
1114 Sized_pluginobj
<size
, big_endian
>::do_layout(Symbol_table
*, Layout
*,
1120 // Add the symbols to the symbol table.
1122 template<int size
, bool big_endian
>
1124 Sized_pluginobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1128 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1129 unsigned char symbuf
[sym_size
];
1130 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
1131 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
1133 this->symbols_
.resize(this->nsyms_
);
1135 for (int i
= 0; i
< this->nsyms_
; ++i
)
1137 const struct ld_plugin_symbol
* isym
= &this->syms_
[i
];
1138 const char* name
= isym
->name
;
1139 const char* ver
= isym
->version
;
1140 elfcpp::Elf_Half shndx
;
1144 if (name
!= NULL
&& name
[0] == '\0')
1146 if (ver
!= NULL
&& ver
[0] == '\0')
1152 case LDPK_WEAKUNDEF
:
1153 bind
= elfcpp::STB_WEAK
;
1159 bind
= elfcpp::STB_GLOBAL
;
1167 shndx
= elfcpp::SHN_ABS
;
1170 shndx
= elfcpp::SHN_COMMON
;
1173 case LDPK_WEAKUNDEF
:
1175 shndx
= elfcpp::SHN_UNDEF
;
1179 switch (isym
->visibility
)
1181 case LDPV_PROTECTED
:
1182 vis
= elfcpp::STV_PROTECTED
;
1185 vis
= elfcpp::STV_INTERNAL
;
1188 vis
= elfcpp::STV_HIDDEN
;
1192 vis
= elfcpp::STV_DEFAULT
;
1196 if (isym
->comdat_key
!= NULL
1197 && isym
->comdat_key
[0] != '\0'
1198 && !this->include_comdat_group(isym
->comdat_key
, layout
))
1199 shndx
= elfcpp::SHN_UNDEF
;
1201 osym
.put_st_name(0);
1202 osym
.put_st_value(0);
1203 osym
.put_st_size(0);
1204 osym
.put_st_info(bind
, elfcpp::STT_NOTYPE
);
1205 osym
.put_st_other(vis
, 0);
1206 osym
.put_st_shndx(shndx
);
1209 symtab
->add_from_pluginobj
<size
, big_endian
>(this, name
, ver
, &sym
);
1213 template<int size
, bool big_endian
>
1214 Archive::Should_include
1215 Sized_pluginobj
<size
, big_endian
>::do_should_include_member(
1216 Symbol_table
* symtab
,
1221 char* tmpbuf
= NULL
;
1222 size_t tmpbuflen
= 0;
1224 for (int i
= 0; i
< this->nsyms_
; ++i
)
1226 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1227 if (sym
.def
== LDPK_UNDEF
|| sym
.def
== LDPK_WEAKUNDEF
)
1229 const char* name
= sym
.name
;
1231 Archive::Should_include t
= Archive::should_include_member(symtab
,
1237 if (t
== Archive::SHOULD_INCLUDE_YES
)
1246 return Archive::SHOULD_INCLUDE_UNKNOWN
;
1249 // Iterate over global symbols, calling a visitor class V for each.
1251 template<int size
, bool big_endian
>
1253 Sized_pluginobj
<size
, big_endian
>::do_for_all_global_symbols(
1255 Library_base::Symbol_visitor_base
* v
)
1257 for (int i
= 0; i
< this->nsyms_
; ++i
)
1259 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1260 if (sym
.def
!= LDPK_UNDEF
)
1265 // Iterate over local symbols, calling a visitor class V for each GOT offset
1266 // associated with a local symbol.
1267 template<int size
, bool big_endian
>
1269 Sized_pluginobj
<size
, big_endian
>::do_for_all_local_got_entries(
1270 Got_offset_list::Visitor
*) const
1275 // Get the size of a section. Not used for plugin objects.
1277 template<int size
, bool big_endian
>
1279 Sized_pluginobj
<size
, big_endian
>::do_section_size(unsigned int)
1285 // Get the name of a section. Not used for plugin objects.
1287 template<int size
, bool big_endian
>
1289 Sized_pluginobj
<size
, big_endian
>::do_section_name(unsigned int) const
1292 return std::string();
1295 // Return a view of the contents of a section. Not used for plugin objects.
1297 template<int size
, bool big_endian
>
1298 const unsigned char*
1299 Sized_pluginobj
<size
, big_endian
>::do_section_contents(
1308 // Return section flags. Not used for plugin objects.
1310 template<int size
, bool big_endian
>
1312 Sized_pluginobj
<size
, big_endian
>::do_section_flags(unsigned int)
1318 // Return section entsize. Not used for plugin objects.
1320 template<int size
, bool big_endian
>
1322 Sized_pluginobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1328 // Return section address. Not used for plugin objects.
1330 template<int size
, bool big_endian
>
1332 Sized_pluginobj
<size
, big_endian
>::do_section_address(unsigned int)
1338 // Return section type. Not used for plugin objects.
1340 template<int size
, bool big_endian
>
1342 Sized_pluginobj
<size
, big_endian
>::do_section_type(unsigned int)
1348 // Return the section link field. Not used for plugin objects.
1350 template<int size
, bool big_endian
>
1352 Sized_pluginobj
<size
, big_endian
>::do_section_link(unsigned int)
1358 // Return the section link field. Not used for plugin objects.
1360 template<int size
, bool big_endian
>
1362 Sized_pluginobj
<size
, big_endian
>::do_section_info(unsigned int)
1368 // Return the section alignment. Not used for plugin objects.
1370 template<int size
, bool big_endian
>
1372 Sized_pluginobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1378 // Return the Xindex structure to use. Not used for plugin objects.
1380 template<int size
, bool big_endian
>
1382 Sized_pluginobj
<size
, big_endian
>::do_initialize_xindex()
1388 // Get symbol counts. Don't count plugin objects; the replacement
1389 // files will provide the counts.
1391 template<int size
, bool big_endian
>
1393 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbol_counts(
1394 const Symbol_table
*,
1402 // Get symbols. Not used for plugin objects.
1404 template<int size
, bool big_endian
>
1405 const Object::Symbols
*
1406 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbols() const
1411 // Class Plugin_finish. This task runs after all replacement files have
1412 // been added. For now, it's a placeholder for a possible plugin API
1413 // to allow the plugin to release most of its resources. The cleanup
1414 // handlers must be called later, because they can remove the temporary
1415 // object files that are needed until the end of the link.
1417 class Plugin_finish
: public Task
1420 Plugin_finish(Task_token
* this_blocker
, Task_token
* next_blocker
)
1421 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
1426 if (this->this_blocker_
!= NULL
)
1427 delete this->this_blocker_
;
1433 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1434 return this->this_blocker_
;
1439 locks(Task_locker
* tl
)
1440 { tl
->add(this, this->next_blocker_
); }
1445 // We could call early cleanup handlers here.
1450 { return "Plugin_finish"; }
1453 Task_token
* this_blocker_
;
1454 Task_token
* next_blocker_
;
1457 // Class Plugin_hook.
1459 Plugin_hook::~Plugin_hook()
1463 // Return whether a Plugin_hook task is runnable.
1466 Plugin_hook::is_runnable()
1468 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1469 return this->this_blocker_
;
1473 // Return a Task_locker for a Plugin_hook task. We don't need any
1477 Plugin_hook::locks(Task_locker
*)
1481 // Run the "all symbols read" plugin hook.
1484 Plugin_hook::run(Workqueue
* workqueue
)
1486 gold_assert(this->options_
.has_plugins());
1487 Symbol
* start_sym
= this->symtab_
->lookup(parameters
->entry());
1488 if (start_sym
!= NULL
)
1489 start_sym
->set_in_real_elf();
1491 this->options_
.plugins()->all_symbols_read(workqueue
,
1493 this->input_objects_
,
1497 &this->this_blocker_
);
1498 workqueue
->queue_soon(new Plugin_finish(this->this_blocker_
,
1499 this->next_blocker_
));
1502 // The C interface routines called by the plugins.
1504 #ifdef ENABLE_PLUGINS
1506 // Register a claim-file handler.
1508 static enum ld_plugin_status
1509 register_claim_file(ld_plugin_claim_file_handler handler
)
1511 gold_assert(parameters
->options().has_plugins());
1512 parameters
->options().plugins()->set_claim_file_handler(handler
);
1516 // Register an all-symbols-read handler.
1518 static enum ld_plugin_status
1519 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
)
1521 gold_assert(parameters
->options().has_plugins());
1522 parameters
->options().plugins()->set_all_symbols_read_handler(handler
);
1526 // Register a cleanup handler.
1528 static enum ld_plugin_status
1529 register_cleanup(ld_plugin_cleanup_handler handler
)
1531 gold_assert(parameters
->options().has_plugins());
1532 parameters
->options().plugins()->set_cleanup_handler(handler
);
1536 // Add symbols from a plugin-claimed input file.
1538 static enum ld_plugin_status
1539 add_symbols(void* handle
, int nsyms
, const ld_plugin_symbol
* syms
)
1541 gold_assert(parameters
->options().has_plugins());
1542 Pluginobj
* obj
= parameters
->options().plugins()->make_plugin_object(
1543 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1546 obj
->store_incoming_symbols(nsyms
, syms
);
1550 // Get the input file information with an open (possibly re-opened)
1553 static enum ld_plugin_status
1554 get_input_file(const void* handle
, struct ld_plugin_input_file
* file
)
1556 gold_assert(parameters
->options().has_plugins());
1557 unsigned int obj_index
=
1558 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1559 return parameters
->options().plugins()->get_input_file(obj_index
, file
);
1562 // Release the input file.
1564 static enum ld_plugin_status
1565 release_input_file(const void* handle
)
1567 gold_assert(parameters
->options().has_plugins());
1568 unsigned int obj_index
=
1569 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1570 return parameters
->options().plugins()->release_input_file(obj_index
);
1573 static enum ld_plugin_status
1574 get_view(const void *handle
, const void **viewp
)
1576 gold_assert(parameters
->options().has_plugins());
1577 unsigned int obj_index
=
1578 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1579 return parameters
->options().plugins()->get_view(obj_index
, viewp
);
1582 // Get the symbol resolution info for a plugin-claimed input file.
1584 static enum ld_plugin_status
1585 get_symbols(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1587 gold_assert(parameters
->options().has_plugins());
1588 Plugin_manager
* plugins
= parameters
->options().plugins();
1589 Object
* obj
= plugins
->object(
1590 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1593 Pluginobj
* plugin_obj
= obj
->pluginobj();
1594 if (plugin_obj
== NULL
)
1596 Symbol_table
* symtab
= plugins
->symtab();
1597 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 1);
1600 // Version 2 of the above. The only difference is that this version
1601 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1603 static enum ld_plugin_status
1604 get_symbols_v2(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1606 gold_assert(parameters
->options().has_plugins());
1607 Plugin_manager
* plugins
= parameters
->options().plugins();
1608 Object
* obj
= plugins
->object(
1609 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1612 Pluginobj
* plugin_obj
= obj
->pluginobj();
1613 if (plugin_obj
== NULL
)
1615 Symbol_table
* symtab
= plugins
->symtab();
1616 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 2);
1619 // Version 3 of the above. The only difference from v2 is that it
1620 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1621 // decided to include.
1623 static enum ld_plugin_status
1624 get_symbols_v3(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1626 gold_assert(parameters
->options().has_plugins());
1627 Plugin_manager
* plugins
= parameters
->options().plugins();
1628 Object
* obj
= plugins
->object(
1629 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1632 Pluginobj
* plugin_obj
= obj
->pluginobj();
1633 if (plugin_obj
== NULL
)
1635 Symbol_table
* symtab
= plugins
->symtab();
1636 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 3);
1639 // Add a new (real) input file generated by a plugin.
1641 static enum ld_plugin_status
1642 add_input_file(const char* pathname
)
1644 gold_assert(parameters
->options().has_plugins());
1645 return parameters
->options().plugins()->add_input_file(pathname
, false);
1648 // Add a new (real) library required by a plugin.
1650 static enum ld_plugin_status
1651 add_input_library(const char* pathname
)
1653 gold_assert(parameters
->options().has_plugins());
1654 return parameters
->options().plugins()->add_input_file(pathname
, true);
1657 // Set the extra library path to be used by libraries added via
1658 // add_input_library
1660 static enum ld_plugin_status
1661 set_extra_library_path(const char* path
)
1663 gold_assert(parameters
->options().has_plugins());
1664 return parameters
->options().plugins()->set_extra_library_path(path
);
1667 // Issue a diagnostic message from a plugin.
1669 static enum ld_plugin_status
1670 message(int level
, const char* format
, ...)
1673 va_start(args
, format
);
1678 parameters
->errors()->info(format
, args
);
1681 parameters
->errors()->warning(format
, args
);
1685 parameters
->errors()->error(format
, args
);
1688 parameters
->errors()->fatal(format
, args
);
1696 // Get the section count of the object corresponding to the handle. This
1697 // plugin interface can only be called in the claim_file handler of the plugin.
1699 static enum ld_plugin_status
1700 get_input_section_count(const void* handle
, unsigned int* count
)
1702 gold_assert(parameters
->options().has_plugins());
1704 if (!parameters
->options().plugins()->in_claim_file_handler())
1707 Object
* obj
= parameters
->options().plugins()->get_elf_object(handle
);
1712 *count
= obj
->shnum();
1716 // Get the type of the specified section in the object corresponding
1717 // to the handle. This plugin interface can only be called in the
1718 // claim_file handler of the plugin.
1720 static enum ld_plugin_status
1721 get_input_section_type(const struct ld_plugin_section section
,
1724 gold_assert(parameters
->options().has_plugins());
1726 if (!parameters
->options().plugins()->in_claim_file_handler())
1730 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1733 return LDPS_BAD_HANDLE
;
1735 *type
= obj
->section_type(section
.shndx
);
1739 // Get the name of the specified section in the object corresponding
1740 // to the handle. This plugin interface can only be called in the
1741 // claim_file handler of the plugin.
1743 static enum ld_plugin_status
1744 get_input_section_name(const struct ld_plugin_section section
,
1745 char** section_name_ptr
)
1747 gold_assert(parameters
->options().has_plugins());
1749 if (!parameters
->options().plugins()->in_claim_file_handler())
1753 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1756 return LDPS_BAD_HANDLE
;
1758 // Check if the object is locked before getting the section name.
1759 gold_assert(obj
->is_locked());
1761 const std::string section_name
= obj
->section_name(section
.shndx
);
1762 *section_name_ptr
= static_cast<char*>(malloc(section_name
.length() + 1));
1763 memcpy(*section_name_ptr
, section_name
.c_str(), section_name
.length() + 1);
1767 // Get the contents of the specified section in the object corresponding
1768 // to the handle. This plugin interface can only be called in the
1769 // claim_file handler of the plugin.
1771 static enum ld_plugin_status
1772 get_input_section_contents(const struct ld_plugin_section section
,
1773 const unsigned char** section_contents_ptr
,
1776 gold_assert(parameters
->options().has_plugins());
1778 if (!parameters
->options().plugins()->in_claim_file_handler())
1782 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1785 return LDPS_BAD_HANDLE
;
1787 // Check if the object is locked before getting the section contents.
1788 gold_assert(obj
->is_locked());
1790 section_size_type plen
;
1791 *section_contents_ptr
1792 = obj
->section_contents(section
.shndx
, &plen
, false);
1797 // Get the alignment of the specified section in the object corresponding
1798 // to the handle. This plugin interface can only be called in the
1799 // claim_file handler of the plugin.
1801 static enum ld_plugin_status
1802 get_input_section_alignment(const struct ld_plugin_section section
,
1803 unsigned int* addralign
)
1805 gold_assert(parameters
->options().has_plugins());
1807 if (!parameters
->options().plugins()->in_claim_file_handler())
1811 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1814 return LDPS_BAD_HANDLE
;
1816 *addralign
= obj
->section_addralign(section
.shndx
);
1820 // Get the size of the specified section in the object corresponding
1821 // to the handle. This plugin interface can only be called in the
1822 // claim_file handler of the plugin.
1824 static enum ld_plugin_status
1825 get_input_section_size(const struct ld_plugin_section section
,
1828 gold_assert(parameters
->options().has_plugins());
1830 if (!parameters
->options().plugins()->in_claim_file_handler())
1834 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1837 return LDPS_BAD_HANDLE
;
1839 *secsize
= obj
->section_size(section
.shndx
);
1843 static enum ld_plugin_status
1844 get_wrap_symbols(uint64_t *count
, const char ***wrap_symbols
)
1846 gold_assert(parameters
->options().has_plugins());
1847 *count
= parameters
->options().wrap_size();
1852 *wrap_symbols
= new const char *[*count
];
1854 for (options::String_set::const_iterator
1855 it
= parameters
->options().wrap_begin();
1856 it
!= parameters
->options().wrap_end(); ++it
, ++i
) {
1857 (*wrap_symbols
)[i
] = it
->c_str();
1863 // Specify the ordering of sections in the final layout. The sections are
1864 // specified as (handle,shndx) pairs in the two arrays in the order in
1865 // which they should appear in the final layout.
1867 static enum ld_plugin_status
1868 update_section_order(const struct ld_plugin_section
* section_list
,
1869 unsigned int num_sections
)
1871 gold_assert(parameters
->options().has_plugins());
1873 if (num_sections
== 0)
1876 if (section_list
== NULL
)
1879 Layout
* layout
= parameters
->options().plugins()->layout();
1880 gold_assert (layout
!= NULL
);
1882 std::map
<Section_id
, unsigned int>* order_map
1883 = layout
->get_section_order_map();
1885 /* Store the mapping from Section_id to section position in layout's
1886 order_map to consult after output sections are added. */
1887 for (unsigned int i
= 0; i
< num_sections
; ++i
)
1889 Object
* obj
= parameters
->options().plugins()->get_elf_object(
1890 section_list
[i
].handle
);
1891 if (obj
== NULL
|| obj
->is_dynamic())
1892 return LDPS_BAD_HANDLE
;
1893 unsigned int shndx
= section_list
[i
].shndx
;
1894 Section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
1895 (*order_map
)[secn_id
] = i
+ 1;
1901 // Let the linker know that the sections could be reordered.
1903 static enum ld_plugin_status
1904 allow_section_ordering()
1906 gold_assert(parameters
->options().has_plugins());
1907 Layout
* layout
= parameters
->options().plugins()->layout();
1908 layout
->set_section_ordering_specified();
1912 // Let the linker know that a subset of sections could be mapped
1913 // to a unique segment.
1915 static enum ld_plugin_status
1916 allow_unique_segment_for_sections()
1918 gold_assert(parameters
->options().has_plugins());
1919 Layout
* layout
= parameters
->options().plugins()->layout();
1920 layout
->set_unique_segment_for_sections_specified();
1924 // This function should map the list of sections specified in the
1925 // SECTION_LIST to a unique segment. ELF segments do not have names
1926 // and the NAME is used to identify Output Section which should contain
1927 // the list of sections. This Output Section will then be mapped to
1928 // a unique segment. FLAGS is used to specify if any additional segment
1929 // flags need to be set. For instance, a specific segment flag can be
1930 // set to identify this segment. Unsetting segment flags is not possible.
1931 // ALIGN specifies the alignment of the segment.
1933 static enum ld_plugin_status
1934 unique_segment_for_sections(const char* segment_name
,
1937 const struct ld_plugin_section
* section_list
,
1938 unsigned int num_sections
)
1940 gold_assert(parameters
->options().has_plugins());
1942 if (num_sections
== 0)
1945 if (section_list
== NULL
)
1948 Layout
* layout
= parameters
->options().plugins()->layout();
1949 gold_assert (layout
!= NULL
);
1951 Layout::Unique_segment_info
* s
= new Layout::Unique_segment_info
;
1952 s
->name
= segment_name
;
1956 for (unsigned int i
= 0; i
< num_sections
; ++i
)
1958 Object
* obj
= parameters
->options().plugins()->get_elf_object(
1959 section_list
[i
].handle
);
1960 if (obj
== NULL
|| obj
->is_dynamic())
1961 return LDPS_BAD_HANDLE
;
1962 unsigned int shndx
= section_list
[i
].shndx
;
1963 Const_section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
1964 layout
->insert_section_segment_map(secn_id
, s
);
1970 // Register a new_input handler.
1972 static enum ld_plugin_status
1973 register_new_input(ld_plugin_new_input_handler handler
)
1975 gold_assert(parameters
->options().has_plugins());
1976 parameters
->options().plugins()->set_new_input_handler(handler
);
1980 #endif // ENABLE_PLUGINS
1982 // Allocate a Pluginobj object of the appropriate size and endianness.
1985 make_sized_plugin_object(Input_file
* input_file
, off_t offset
, off_t filesize
)
1987 Pluginobj
* obj
= NULL
;
1989 parameters_force_valid_target();
1990 const Target
& target(parameters
->target());
1992 if (target
.get_size() == 32)
1994 if (target
.is_big_endian())
1995 #ifdef HAVE_TARGET_32_BIG
1996 obj
= new Sized_pluginobj
<32, true>(input_file
->filename(),
1997 input_file
, offset
, filesize
);
1999 gold_error(_("%s: not configured to support "
2000 "32-bit big-endian object"),
2001 input_file
->filename().c_str());
2004 #ifdef HAVE_TARGET_32_LITTLE
2005 obj
= new Sized_pluginobj
<32, false>(input_file
->filename(),
2006 input_file
, offset
, filesize
);
2008 gold_error(_("%s: not configured to support "
2009 "32-bit little-endian object"),
2010 input_file
->filename().c_str());
2013 else if (target
.get_size() == 64)
2015 if (target
.is_big_endian())
2016 #ifdef HAVE_TARGET_64_BIG
2017 obj
= new Sized_pluginobj
<64, true>(input_file
->filename(),
2018 input_file
, offset
, filesize
);
2020 gold_error(_("%s: not configured to support "
2021 "64-bit big-endian object"),
2022 input_file
->filename().c_str());
2025 #ifdef HAVE_TARGET_64_LITTLE
2026 obj
= new Sized_pluginobj
<64, false>(input_file
->filename(),
2027 input_file
, offset
, filesize
);
2029 gold_error(_("%s: not configured to support "
2030 "64-bit little-endian object"),
2031 input_file
->filename().c_str());
2035 gold_assert(obj
!= NULL
);
2039 } // End namespace gold.