1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2017 Free Software Foundation, Inc.
4 // Written by Cary Coutant <ccoutant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
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
,
172 #endif // ENABLE_PLUGINS
174 static Pluginobj
* make_sized_plugin_object(Input_file
* input_file
,
175 off_t offset
, off_t filesize
);
179 // Load one plugin library.
184 #ifdef ENABLE_PLUGINS
185 // Load the plugin library.
186 // FIXME: Look for the library in standard locations.
187 this->handle_
= dlopen(this->filename_
.c_str(), RTLD_NOW
);
188 if (this->handle_
== NULL
)
190 gold_error(_("%s: could not load plugin library: %s"),
191 this->filename_
.c_str(), dlerror());
195 // Find the plugin's onload entry point.
196 void* ptr
= dlsym(this->handle_
, "onload");
199 gold_error(_("%s: could not find onload entry point"),
200 this->filename_
.c_str());
203 ld_plugin_onload onload
;
204 gold_assert(sizeof(onload
) == sizeof(ptr
));
205 memcpy(&onload
, &ptr
, sizeof(ptr
));
207 // Get the linker's version number.
208 const char* ver
= get_version_string();
211 sscanf(ver
, "%d.%d", &major
, &minor
);
213 // Allocate and populate a transfer vector.
214 const int tv_fixed_size
= 29;
216 int tv_size
= this->args_
.size() + tv_fixed_size
;
217 ld_plugin_tv
* tv
= new ld_plugin_tv
[tv_size
];
219 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
220 // while processing subsequent entries.
222 tv
[i
].tv_tag
= LDPT_MESSAGE
;
223 tv
[i
].tv_u
.tv_message
= message
;
226 tv
[i
].tv_tag
= LDPT_API_VERSION
;
227 tv
[i
].tv_u
.tv_val
= LD_PLUGIN_API_VERSION
;
230 tv
[i
].tv_tag
= LDPT_GOLD_VERSION
;
231 tv
[i
].tv_u
.tv_val
= major
* 100 + minor
;
234 tv
[i
].tv_tag
= LDPT_LINKER_OUTPUT
;
235 if (parameters
->options().relocatable())
236 tv
[i
].tv_u
.tv_val
= LDPO_REL
;
237 else if (parameters
->options().shared())
238 tv
[i
].tv_u
.tv_val
= LDPO_DYN
;
239 else if (parameters
->options().pie())
240 tv
[i
].tv_u
.tv_val
= LDPO_PIE
;
242 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
245 tv
[i
].tv_tag
= LDPT_OUTPUT_NAME
;
246 tv
[i
].tv_u
.tv_string
= parameters
->options().output();
248 for (unsigned int j
= 0; j
< this->args_
.size(); ++j
)
251 tv
[i
].tv_tag
= LDPT_OPTION
;
252 tv
[i
].tv_u
.tv_string
= this->args_
[j
].c_str();
256 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
257 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
260 tv
[i
].tv_tag
= LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
;
261 tv
[i
].tv_u
.tv_register_all_symbols_read
= register_all_symbols_read
;
264 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
265 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
268 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
269 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
272 tv
[i
].tv_tag
= LDPT_GET_INPUT_FILE
;
273 tv
[i
].tv_u
.tv_get_input_file
= get_input_file
;
276 tv
[i
].tv_tag
= LDPT_GET_VIEW
;
277 tv
[i
].tv_u
.tv_get_view
= get_view
;
280 tv
[i
].tv_tag
= LDPT_RELEASE_INPUT_FILE
;
281 tv
[i
].tv_u
.tv_release_input_file
= release_input_file
;
284 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
285 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
288 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V2
;
289 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v2
;
292 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V3
;
293 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v3
;
296 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
297 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
300 tv
[i
].tv_tag
= LDPT_ADD_INPUT_LIBRARY
;
301 tv
[i
].tv_u
.tv_add_input_library
= add_input_library
;
304 tv
[i
].tv_tag
= LDPT_SET_EXTRA_LIBRARY_PATH
;
305 tv
[i
].tv_u
.tv_set_extra_library_path
= set_extra_library_path
;
308 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_COUNT
;
309 tv
[i
].tv_u
.tv_get_input_section_count
= get_input_section_count
;
312 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_TYPE
;
313 tv
[i
].tv_u
.tv_get_input_section_type
= get_input_section_type
;
316 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_NAME
;
317 tv
[i
].tv_u
.tv_get_input_section_name
= get_input_section_name
;
320 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_CONTENTS
;
321 tv
[i
].tv_u
.tv_get_input_section_contents
= get_input_section_contents
;
324 tv
[i
].tv_tag
= LDPT_UPDATE_SECTION_ORDER
;
325 tv
[i
].tv_u
.tv_update_section_order
= update_section_order
;
328 tv
[i
].tv_tag
= LDPT_ALLOW_SECTION_ORDERING
;
329 tv
[i
].tv_u
.tv_allow_section_ordering
= allow_section_ordering
;
332 tv
[i
].tv_tag
= LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS
;
333 tv
[i
].tv_u
.tv_allow_unique_segment_for_sections
334 = allow_unique_segment_for_sections
;
337 tv
[i
].tv_tag
= LDPT_UNIQUE_SEGMENT_FOR_SECTIONS
;
338 tv
[i
].tv_u
.tv_unique_segment_for_sections
= unique_segment_for_sections
;
341 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_ALIGNMENT
;
342 tv
[i
].tv_u
.tv_get_input_section_alignment
= get_input_section_alignment
;
345 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_SIZE
;
346 tv
[i
].tv_u
.tv_get_input_section_size
= get_input_section_size
;
349 tv
[i
].tv_tag
= LDPT_NULL
;
350 tv
[i
].tv_u
.tv_val
= 0;
352 gold_assert(i
== tv_size
- 1);
354 // Call the onload entry point.
358 #endif // ENABLE_PLUGINS
361 // Call the plugin claim-file handler.
364 Plugin::claim_file(struct ld_plugin_input_file
* plugin_input_file
)
368 if (this->claim_file_handler_
!= NULL
)
370 (*this->claim_file_handler_
)(plugin_input_file
, &claimed
);
377 // Call the all-symbols-read handler.
380 Plugin::all_symbols_read()
382 if (this->all_symbols_read_handler_
!= NULL
)
383 (*this->all_symbols_read_handler_
)();
386 // Call the cleanup handler.
391 if (this->cleanup_handler_
!= NULL
&& !this->cleanup_done_
)
393 // Set this flag before calling to prevent a recursive plunge
394 // in the event that a plugin's cleanup handler issues a
396 this->cleanup_done_
= true;
397 (*this->cleanup_handler_
)();
401 // This task is used to rescan archives as needed.
403 class Plugin_rescan
: public Task
406 Plugin_rescan(Task_token
* this_blocker
, Task_token
* next_blocker
)
407 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
412 delete this->this_blocker_
;
418 if (this->this_blocker_
->is_blocked())
419 return this->this_blocker_
;
424 locks(Task_locker
* tl
)
425 { tl
->add(this, this->next_blocker_
); }
429 { parameters
->options().plugins()->rescan(this); }
433 { return "Plugin_rescan"; }
436 Task_token
* this_blocker_
;
437 Task_token
* next_blocker_
;
440 // Plugin_manager methods.
442 Plugin_manager::~Plugin_manager()
444 for (Plugin_list::iterator p
= this->plugins_
.begin();
445 p
!= this->plugins_
.end();
448 this->plugins_
.clear();
449 for (Object_list::iterator obj
= this->objects_
.begin();
450 obj
!= this->objects_
.end();
453 this->objects_
.clear();
457 // Load all plugin libraries.
460 Plugin_manager::load_plugins(Layout
* layout
)
462 this->layout_
= layout
;
463 for (this->current_
= this->plugins_
.begin();
464 this->current_
!= this->plugins_
.end();
466 (*this->current_
)->load();
469 // Call the plugin claim-file handlers in turn to see if any claim the file.
472 Plugin_manager::claim_file(Input_file
* input_file
, off_t offset
,
473 off_t filesize
, Object
* elf_object
)
475 bool lock_initialized
= this->initialize_lock_
.initialize();
477 gold_assert(lock_initialized
);
478 Hold_lock
hl(*this->lock_
);
479 if (this->in_replacement_phase_
)
482 unsigned int handle
= this->objects_
.size();
483 this->input_file_
= input_file
;
484 this->plugin_input_file_
.name
= input_file
->filename().c_str();
485 this->plugin_input_file_
.fd
= input_file
->file().descriptor();
486 this->plugin_input_file_
.offset
= offset
;
487 this->plugin_input_file_
.filesize
= filesize
;
488 this->plugin_input_file_
.handle
= reinterpret_cast<void*>(handle
);
489 if (elf_object
!= NULL
)
490 this->objects_
.push_back(elf_object
);
491 this->in_claim_file_handler_
= true;
493 for (this->current_
= this->plugins_
.begin();
494 this->current_
!= this->plugins_
.end();
497 if ((*this->current_
)->claim_file(&this->plugin_input_file_
))
499 this->any_claimed_
= true;
500 this->in_claim_file_handler_
= false;
502 if (this->objects_
.size() > handle
503 && this->objects_
[handle
]->pluginobj() != NULL
)
504 return this->objects_
[handle
]->pluginobj();
506 // If the plugin claimed the file but did not call the
507 // add_symbols callback, we need to create the Pluginobj now.
508 Pluginobj
* obj
= this->make_plugin_object(handle
);
513 this->in_claim_file_handler_
= false;
517 // Save an archive. This is used so that a plugin can add a file
518 // which refers to a symbol which was not previously referenced. In
519 // that case we want to pretend that the symbol was referenced before,
520 // and pull in the archive object.
523 Plugin_manager::save_archive(Archive
* archive
)
525 if (this->in_replacement_phase_
|| !this->any_claimed_
)
528 this->rescannable_
.push_back(Rescannable(archive
));
531 // Save an Input_group. This is like save_archive.
534 Plugin_manager::save_input_group(Input_group
* input_group
)
536 if (this->in_replacement_phase_
|| !this->any_claimed_
)
539 this->rescannable_
.push_back(Rescannable(input_group
));
542 // Call the all-symbols-read handlers.
545 Plugin_manager::all_symbols_read(Workqueue
* workqueue
, Task
* task
,
546 Input_objects
* input_objects
,
547 Symbol_table
* symtab
,
548 Dirsearch
* dirpath
, Mapfile
* mapfile
,
549 Task_token
** last_blocker
)
551 this->in_replacement_phase_
= true;
552 this->workqueue_
= workqueue
;
554 this->input_objects_
= input_objects
;
555 this->symtab_
= symtab
;
556 this->dirpath_
= dirpath
;
557 this->mapfile_
= mapfile
;
558 this->this_blocker_
= NULL
;
560 for (this->current_
= this->plugins_
.begin();
561 this->current_
!= this->plugins_
.end();
563 (*this->current_
)->all_symbols_read();
565 if (this->any_added_
)
567 Task_token
* next_blocker
= new Task_token(true);
568 next_blocker
->add_blocker();
569 workqueue
->queue(new Plugin_rescan(this->this_blocker_
, next_blocker
));
570 this->this_blocker_
= next_blocker
;
573 *last_blocker
= this->this_blocker_
;
576 // This is called when we see a new undefined symbol. If we are in
577 // the replacement phase, this means that we may need to rescan some
578 // archives we have previously seen.
581 Plugin_manager::new_undefined_symbol(Symbol
* sym
)
583 if (this->in_replacement_phase_
)
584 this->undefined_symbols_
.push_back(sym
);
587 // Rescan archives as needed. This handles the case where a new
588 // object file added by a plugin has an undefined reference to some
589 // symbol defined in an archive.
592 Plugin_manager::rescan(Task
* task
)
594 size_t rescan_pos
= 0;
595 size_t rescan_size
= this->rescannable_
.size();
596 while (!this->undefined_symbols_
.empty())
598 if (rescan_pos
>= rescan_size
)
600 this->undefined_symbols_
.clear();
604 Undefined_symbol_list undefs
;
605 undefs
.reserve(this->undefined_symbols_
.size());
606 this->undefined_symbols_
.swap(undefs
);
608 size_t min_rescan_pos
= rescan_size
;
610 for (Undefined_symbol_list::const_iterator p
= undefs
.begin();
614 if (!(*p
)->is_undefined())
617 this->undefined_symbols_
.push_back(*p
);
619 // Find the first rescan archive which defines this symbol,
620 // starting at the current rescan position. The rescan position
621 // exists so that given -la -lb -lc we don't look for undefined
622 // symbols in -lb back in -la, but instead get the definition
623 // from -lc. Don't bother to look past the current minimum
625 for (size_t i
= rescan_pos
; i
< min_rescan_pos
; ++i
)
627 if (this->rescannable_defines(i
, *p
))
635 if (min_rescan_pos
>= rescan_size
)
637 // We didn't find any rescannable archives which define any
638 // undefined symbols.
642 const Rescannable
& r(this->rescannable_
[min_rescan_pos
]);
645 Task_lock_obj
<Archive
> tl(task
, r
.u
.archive
);
646 r
.u
.archive
->add_symbols(this->symtab_
, this->layout_
,
647 this->input_objects_
, this->mapfile_
);
651 size_t next_saw_undefined
= this->symtab_
->saw_undefined();
652 size_t saw_undefined
;
655 saw_undefined
= next_saw_undefined
;
657 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
658 p
!= r
.u
.input_group
->end();
661 Task_lock_obj
<Archive
> tl(task
, *p
);
663 (*p
)->add_symbols(this->symtab_
, this->layout_
,
664 this->input_objects_
, this->mapfile_
);
667 next_saw_undefined
= this->symtab_
->saw_undefined();
669 while (saw_undefined
!= next_saw_undefined
);
672 for (size_t i
= rescan_pos
; i
< min_rescan_pos
+ 1; ++i
)
674 if (this->rescannable_
[i
].is_archive
)
675 delete this->rescannable_
[i
].u
.archive
;
677 delete this->rescannable_
[i
].u
.input_group
;
680 rescan_pos
= min_rescan_pos
+ 1;
684 // Return whether the rescannable at index I defines SYM.
687 Plugin_manager::rescannable_defines(size_t i
, Symbol
* sym
)
689 const Rescannable
& r(this->rescannable_
[i
]);
691 return r
.u
.archive
->defines_symbol(sym
);
694 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
695 p
!= r
.u
.input_group
->end();
698 if ((*p
)->defines_symbol(sym
))
705 // Layout deferred objects.
708 Plugin_manager::layout_deferred_objects()
710 Deferred_layout_list::iterator obj
;
712 for (obj
= this->deferred_layout_objects_
.begin();
713 obj
!= this->deferred_layout_objects_
.end();
716 // Lock the object so we can read from it. This is only called
717 // single-threaded from queue_middle_tasks, so it is OK to lock.
718 // Unfortunately we have no way to pass in a Task token.
719 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
720 Task_lock_obj
<Object
> tl(dummy_task
, *obj
);
721 (*obj
)->layout_deferred_sections(this->layout_
);
725 // Call the cleanup handlers.
728 Plugin_manager::cleanup()
730 if (this->any_added_
)
732 // If any input files were added, close all the input files.
733 // This is because the plugin may want to remove them, and on
734 // Windows you are not allowed to remove an open file.
735 close_all_descriptors();
738 for (this->current_
= this->plugins_
.begin();
739 this->current_
!= this->plugins_
.end();
741 (*this->current_
)->cleanup();
744 // Make a new Pluginobj object. This is called when the plugin calls
745 // the add_symbols API.
748 Plugin_manager::make_plugin_object(unsigned int handle
)
750 // Make sure we aren't asked to make an object for the same handle twice.
751 if (this->objects_
.size() != handle
752 && this->objects_
[handle
]->pluginobj() != NULL
)
755 Pluginobj
* obj
= make_sized_plugin_object(this->input_file_
,
756 this->plugin_input_file_
.offset
,
757 this->plugin_input_file_
.filesize
);
760 // If the elf object for this file was pushed into the objects_ vector, delete
761 // it to make room for the Pluginobj as this file is claimed.
762 if (this->objects_
.size() != handle
)
763 this->objects_
.pop_back();
765 this->objects_
.push_back(obj
);
769 // Get the input file information with an open (possibly re-opened)
773 Plugin_manager::get_input_file(unsigned int handle
,
774 struct ld_plugin_input_file
* file
)
776 Pluginobj
* obj
= this->object(handle
)->pluginobj();
778 return LDPS_BAD_HANDLE
;
780 obj
->lock(this->task_
);
781 file
->name
= obj
->filename().c_str();
782 file
->fd
= obj
->descriptor();
783 file
->offset
= obj
->offset();
784 file
->filesize
= obj
->filesize();
785 file
->handle
= reinterpret_cast<void*>(handle
);
789 // Release the input file.
792 Plugin_manager::release_input_file(unsigned int handle
)
794 if (this->object(handle
) == NULL
)
795 return LDPS_BAD_HANDLE
;
797 Pluginobj
* obj
= this->object(handle
)->pluginobj();
800 return LDPS_BAD_HANDLE
;
802 obj
->unlock(this->task_
);
806 // Get the elf object corresponding to the handle. Return NULL if we
807 // found a Pluginobj instead.
810 Plugin_manager::get_elf_object(const void* handle
)
812 Object
* obj
= this->object(
813 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
815 // The object should not be a Pluginobj.
817 || obj
->pluginobj() != NULL
)
824 Plugin_manager::get_view(unsigned int handle
, const void **viewp
)
828 Input_file
*input_file
;
829 if (this->in_claim_file_handler_
)
831 // We are being called from the claim_file hook.
832 const struct ld_plugin_input_file
&f
= this->plugin_input_file_
;
834 filesize
= f
.filesize
;
835 input_file
= this->input_file_
;
839 // An already claimed file.
840 if (this->object(handle
) == NULL
)
841 return LDPS_BAD_HANDLE
;
842 Pluginobj
* obj
= this->object(handle
)->pluginobj();
844 return LDPS_BAD_HANDLE
;
845 offset
= obj
->offset();
846 filesize
= obj
->filesize();
847 input_file
= obj
->input_file();
849 *viewp
= (void*) input_file
->file().get_view(offset
, 0, filesize
, false,
854 // Add a new library path.
857 Plugin_manager::set_extra_library_path(const char* path
)
859 this->extra_search_path_
= std::string(path
);
863 // Add a new input file.
866 Plugin_manager::add_input_file(const char* pathname
, bool is_lib
)
868 Input_file_argument
file(pathname
,
870 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
871 : Input_file_argument::INPUT_FILE_TYPE_FILE
),
873 ? this->extra_search_path_
.c_str()
877 Input_argument
* input_argument
= new Input_argument(file
);
878 Task_token
* next_blocker
= new Task_token(true);
879 next_blocker
->add_blocker();
880 if (parameters
->incremental())
881 gold_error(_("input files added by plug-ins in --incremental mode not "
883 this->workqueue_
->queue_soon(new Read_symbols(this->input_objects_
,
894 this->this_blocker_
= next_blocker
;
895 this->any_added_
= true;
901 Pluginobj::Pluginobj(const std::string
& name
, Input_file
* input_file
,
902 off_t offset
, off_t filesize
)
903 : Object(name
, input_file
, false, offset
),
904 nsyms_(0), syms_(NULL
), symbols_(), filesize_(filesize
), comdat_map_()
908 // Return TRUE if a defined symbol is referenced from outside the
909 // universe of claimed objects. Only references from relocatable,
910 // non-IR (unclaimed) objects count as a reference. References from
911 // dynamic objects count only as "visible".
914 is_referenced_from_outside(Symbol
* lsym
)
916 if (lsym
->in_real_elf())
918 if (parameters
->options().relocatable())
920 if (parameters
->options().is_undefined(lsym
->name()))
925 // Return TRUE if a defined symbol might be reachable from outside the
929 is_visible_from_outside(Symbol
* lsym
)
933 if (parameters
->options().export_dynamic() || parameters
->options().shared()
934 || parameters
->options().in_dynamic_list(lsym
->name())
935 || parameters
->options().is_export_dynamic_symbol(lsym
->name()))
936 return lsym
->is_externally_visible();
940 // Get symbol resolution info.
943 Pluginobj::get_symbol_resolution_info(Symbol_table
* symtab
,
945 ld_plugin_symbol
* syms
,
948 // For version 1 of this interface, we cannot use
949 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
951 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
953 ? LDPR_PREVAILING_DEF_IRONLY_EXP
954 : LDPR_PREVAILING_DEF
);
956 if (nsyms
> this->nsyms_
)
959 if (static_cast<size_t>(nsyms
) > this->symbols_
.size())
961 // We never decided to include this object. We mark all symbols as
963 gold_assert(this->symbols_
.size() == 0);
964 for (int i
= 0; i
< nsyms
; i
++)
965 syms
[i
].resolution
= LDPR_PREEMPTED_REG
;
966 return version
> 2 ? LDPS_NO_SYMS
: LDPS_OK
;
969 for (int i
= 0; i
< nsyms
; i
++)
971 ld_plugin_symbol
* isym
= &syms
[i
];
972 Symbol
* lsym
= this->symbols_
[i
];
973 if (lsym
->is_forwarder())
974 lsym
= symtab
->resolve_forwards(lsym
);
975 ld_plugin_symbol_resolution res
= LDPR_UNKNOWN
;
977 if (lsym
->is_undefined())
978 // The symbol remains undefined.
980 else if (isym
->def
== LDPK_UNDEF
981 || isym
->def
== LDPK_WEAKUNDEF
982 || isym
->def
== LDPK_COMMON
)
984 // The original symbol was undefined or common.
985 if (lsym
->source() != Symbol::FROM_OBJECT
)
986 res
= LDPR_RESOLVED_EXEC
;
987 else if (lsym
->object()->pluginobj() == this)
989 if (is_referenced_from_outside(lsym
))
990 res
= LDPR_PREVAILING_DEF
;
991 else if (is_visible_from_outside(lsym
))
992 res
= ldpr_prevailing_def_ironly_exp
;
994 res
= LDPR_PREVAILING_DEF_IRONLY
;
996 else if (lsym
->object()->pluginobj() != NULL
)
997 res
= LDPR_RESOLVED_IR
;
998 else if (lsym
->object()->is_dynamic())
999 res
= LDPR_RESOLVED_DYN
;
1001 res
= LDPR_RESOLVED_EXEC
;
1005 // The original symbol was a definition.
1006 if (lsym
->source() != Symbol::FROM_OBJECT
)
1007 res
= LDPR_PREEMPTED_REG
;
1008 else if (lsym
->object() == static_cast<const Object
*>(this))
1010 if (is_referenced_from_outside(lsym
))
1011 res
= LDPR_PREVAILING_DEF
;
1012 else if (is_visible_from_outside(lsym
))
1013 res
= ldpr_prevailing_def_ironly_exp
;
1015 res
= LDPR_PREVAILING_DEF_IRONLY
;
1018 res
= (lsym
->object()->pluginobj() != NULL
1020 : LDPR_PREEMPTED_REG
);
1022 isym
->resolution
= res
;
1027 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1031 Pluginobj::include_comdat_group(std::string comdat_key
, Layout
* layout
)
1033 std::pair
<Comdat_map::iterator
, bool> ins
=
1034 this->comdat_map_
.insert(std::make_pair(comdat_key
, false));
1036 // If this is the first time we've seen this comdat key, ask the
1037 // layout object whether it should be included.
1039 ins
.first
->second
= layout
->find_or_add_kept_section(comdat_key
,
1043 return ins
.first
->second
;
1046 // Class Sized_pluginobj.
1048 template<int size
, bool big_endian
>
1049 Sized_pluginobj
<size
, big_endian
>::Sized_pluginobj(
1050 const std::string
& name
,
1051 Input_file
* input_file
,
1054 : Pluginobj(name
, input_file
, offset
, filesize
)
1058 // Read the symbols. Not used for plugin objects.
1060 template<int size
, bool big_endian
>
1062 Sized_pluginobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1067 // Lay out the input sections. Not used for plugin objects.
1069 template<int size
, bool big_endian
>
1071 Sized_pluginobj
<size
, big_endian
>::do_layout(Symbol_table
*, Layout
*,
1077 // Add the symbols to the symbol table.
1079 template<int size
, bool big_endian
>
1081 Sized_pluginobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1085 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1086 unsigned char symbuf
[sym_size
];
1087 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
1088 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
1090 this->symbols_
.resize(this->nsyms_
);
1092 for (int i
= 0; i
< this->nsyms_
; ++i
)
1094 const struct ld_plugin_symbol
* isym
= &this->syms_
[i
];
1095 const char* name
= isym
->name
;
1096 const char* ver
= isym
->version
;
1097 elfcpp::Elf_Half shndx
;
1101 if (name
!= NULL
&& name
[0] == '\0')
1103 if (ver
!= NULL
&& ver
[0] == '\0')
1109 case LDPK_WEAKUNDEF
:
1110 bind
= elfcpp::STB_WEAK
;
1116 bind
= elfcpp::STB_GLOBAL
;
1124 shndx
= elfcpp::SHN_ABS
;
1127 shndx
= elfcpp::SHN_COMMON
;
1130 case LDPK_WEAKUNDEF
:
1132 shndx
= elfcpp::SHN_UNDEF
;
1136 switch (isym
->visibility
)
1138 case LDPV_PROTECTED
:
1139 vis
= elfcpp::STV_PROTECTED
;
1142 vis
= elfcpp::STV_INTERNAL
;
1145 vis
= elfcpp::STV_HIDDEN
;
1149 vis
= elfcpp::STV_DEFAULT
;
1153 if (isym
->comdat_key
!= NULL
1154 && isym
->comdat_key
[0] != '\0'
1155 && !this->include_comdat_group(isym
->comdat_key
, layout
))
1156 shndx
= elfcpp::SHN_UNDEF
;
1158 osym
.put_st_name(0);
1159 osym
.put_st_value(0);
1160 osym
.put_st_size(0);
1161 osym
.put_st_info(bind
, elfcpp::STT_NOTYPE
);
1162 osym
.put_st_other(vis
, 0);
1163 osym
.put_st_shndx(shndx
);
1166 symtab
->add_from_pluginobj
<size
, big_endian
>(this, name
, ver
, &sym
);
1170 template<int size
, bool big_endian
>
1171 Archive::Should_include
1172 Sized_pluginobj
<size
, big_endian
>::do_should_include_member(
1173 Symbol_table
* symtab
,
1178 char* tmpbuf
= NULL
;
1179 size_t tmpbuflen
= 0;
1181 for (int i
= 0; i
< this->nsyms_
; ++i
)
1183 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1184 if (sym
.def
== LDPK_UNDEF
|| sym
.def
== LDPK_WEAKUNDEF
)
1186 const char* name
= sym
.name
;
1188 Archive::Should_include t
= Archive::should_include_member(symtab
,
1194 if (t
== Archive::SHOULD_INCLUDE_YES
)
1203 return Archive::SHOULD_INCLUDE_UNKNOWN
;
1206 // Iterate over global symbols, calling a visitor class V for each.
1208 template<int size
, bool big_endian
>
1210 Sized_pluginobj
<size
, big_endian
>::do_for_all_global_symbols(
1212 Library_base::Symbol_visitor_base
* v
)
1214 for (int i
= 0; i
< this->nsyms_
; ++i
)
1216 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1217 if (sym
.def
!= LDPK_UNDEF
)
1222 // Iterate over local symbols, calling a visitor class V for each GOT offset
1223 // associated with a local symbol.
1224 template<int size
, bool big_endian
>
1226 Sized_pluginobj
<size
, big_endian
>::do_for_all_local_got_entries(
1227 Got_offset_list::Visitor
*) const
1232 // Get the size of a section. Not used for plugin objects.
1234 template<int size
, bool big_endian
>
1236 Sized_pluginobj
<size
, big_endian
>::do_section_size(unsigned int)
1242 // Get the name of a section. Not used for plugin objects.
1244 template<int size
, bool big_endian
>
1246 Sized_pluginobj
<size
, big_endian
>::do_section_name(unsigned int) const
1249 return std::string();
1252 // Return a view of the contents of a section. Not used for plugin objects.
1254 template<int size
, bool big_endian
>
1255 const unsigned char*
1256 Sized_pluginobj
<size
, big_endian
>::do_section_contents(
1265 // Return section flags. Not used for plugin objects.
1267 template<int size
, bool big_endian
>
1269 Sized_pluginobj
<size
, big_endian
>::do_section_flags(unsigned int)
1275 // Return section entsize. Not used for plugin objects.
1277 template<int size
, bool big_endian
>
1279 Sized_pluginobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1285 // Return section address. Not used for plugin objects.
1287 template<int size
, bool big_endian
>
1289 Sized_pluginobj
<size
, big_endian
>::do_section_address(unsigned int)
1295 // Return section type. Not used for plugin objects.
1297 template<int size
, bool big_endian
>
1299 Sized_pluginobj
<size
, big_endian
>::do_section_type(unsigned int)
1305 // Return the section link field. Not used for plugin objects.
1307 template<int size
, bool big_endian
>
1309 Sized_pluginobj
<size
, big_endian
>::do_section_link(unsigned int)
1315 // Return the section link field. Not used for plugin objects.
1317 template<int size
, bool big_endian
>
1319 Sized_pluginobj
<size
, big_endian
>::do_section_info(unsigned int)
1325 // Return the section alignment. Not used for plugin objects.
1327 template<int size
, bool big_endian
>
1329 Sized_pluginobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1335 // Return the Xindex structure to use. Not used for plugin objects.
1337 template<int size
, bool big_endian
>
1339 Sized_pluginobj
<size
, big_endian
>::do_initialize_xindex()
1345 // Get symbol counts. Don't count plugin objects; the replacement
1346 // files will provide the counts.
1348 template<int size
, bool big_endian
>
1350 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbol_counts(
1351 const Symbol_table
*,
1359 // Get symbols. Not used for plugin objects.
1361 template<int size
, bool big_endian
>
1362 const Object::Symbols
*
1363 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbols() const
1368 // Class Plugin_finish. This task runs after all replacement files have
1369 // been added. For now, it's a placeholder for a possible plugin API
1370 // to allow the plugin to release most of its resources. The cleanup
1371 // handlers must be called later, because they can remove the temporary
1372 // object files that are needed until the end of the link.
1374 class Plugin_finish
: public Task
1377 Plugin_finish(Task_token
* this_blocker
, Task_token
* next_blocker
)
1378 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
1383 if (this->this_blocker_
!= NULL
)
1384 delete this->this_blocker_
;
1390 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1391 return this->this_blocker_
;
1396 locks(Task_locker
* tl
)
1397 { tl
->add(this, this->next_blocker_
); }
1402 // We could call early cleanup handlers here.
1407 { return "Plugin_finish"; }
1410 Task_token
* this_blocker_
;
1411 Task_token
* next_blocker_
;
1414 // Class Plugin_hook.
1416 Plugin_hook::~Plugin_hook()
1420 // Return whether a Plugin_hook task is runnable.
1423 Plugin_hook::is_runnable()
1425 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1426 return this->this_blocker_
;
1430 // Return a Task_locker for a Plugin_hook task. We don't need any
1434 Plugin_hook::locks(Task_locker
*)
1438 // Run the "all symbols read" plugin hook.
1441 Plugin_hook::run(Workqueue
* workqueue
)
1443 gold_assert(this->options_
.has_plugins());
1444 Symbol
* start_sym
= this->symtab_
->lookup(parameters
->entry());
1445 if (start_sym
!= NULL
)
1446 start_sym
->set_in_real_elf();
1448 this->options_
.plugins()->all_symbols_read(workqueue
,
1450 this->input_objects_
,
1454 &this->this_blocker_
);
1455 workqueue
->queue_soon(new Plugin_finish(this->this_blocker_
,
1456 this->next_blocker_
));
1459 // The C interface routines called by the plugins.
1461 #ifdef ENABLE_PLUGINS
1463 // Register a claim-file handler.
1465 static enum ld_plugin_status
1466 register_claim_file(ld_plugin_claim_file_handler handler
)
1468 gold_assert(parameters
->options().has_plugins());
1469 parameters
->options().plugins()->set_claim_file_handler(handler
);
1473 // Register an all-symbols-read handler.
1475 static enum ld_plugin_status
1476 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
)
1478 gold_assert(parameters
->options().has_plugins());
1479 parameters
->options().plugins()->set_all_symbols_read_handler(handler
);
1483 // Register a cleanup handler.
1485 static enum ld_plugin_status
1486 register_cleanup(ld_plugin_cleanup_handler handler
)
1488 gold_assert(parameters
->options().has_plugins());
1489 parameters
->options().plugins()->set_cleanup_handler(handler
);
1493 // Add symbols from a plugin-claimed input file.
1495 static enum ld_plugin_status
1496 add_symbols(void* handle
, int nsyms
, const ld_plugin_symbol
* syms
)
1498 gold_assert(parameters
->options().has_plugins());
1499 Pluginobj
* obj
= parameters
->options().plugins()->make_plugin_object(
1500 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1503 obj
->store_incoming_symbols(nsyms
, syms
);
1507 // Get the input file information with an open (possibly re-opened)
1510 static enum ld_plugin_status
1511 get_input_file(const void* handle
, struct ld_plugin_input_file
* file
)
1513 gold_assert(parameters
->options().has_plugins());
1514 unsigned int obj_index
=
1515 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1516 return parameters
->options().plugins()->get_input_file(obj_index
, file
);
1519 // Release the input file.
1521 static enum ld_plugin_status
1522 release_input_file(const void* handle
)
1524 gold_assert(parameters
->options().has_plugins());
1525 unsigned int obj_index
=
1526 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1527 return parameters
->options().plugins()->release_input_file(obj_index
);
1530 static enum ld_plugin_status
1531 get_view(const void *handle
, const void **viewp
)
1533 gold_assert(parameters
->options().has_plugins());
1534 unsigned int obj_index
=
1535 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1536 return parameters
->options().plugins()->get_view(obj_index
, viewp
);
1539 // Get the symbol resolution info for a plugin-claimed input file.
1541 static enum ld_plugin_status
1542 get_symbols(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1544 gold_assert(parameters
->options().has_plugins());
1545 Plugin_manager
* plugins
= parameters
->options().plugins();
1546 Object
* obj
= plugins
->object(
1547 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1550 Pluginobj
* plugin_obj
= obj
->pluginobj();
1551 if (plugin_obj
== NULL
)
1553 Symbol_table
* symtab
= plugins
->symtab();
1554 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 1);
1557 // Version 2 of the above. The only difference is that this version
1558 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1560 static enum ld_plugin_status
1561 get_symbols_v2(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1563 gold_assert(parameters
->options().has_plugins());
1564 Plugin_manager
* plugins
= parameters
->options().plugins();
1565 Object
* obj
= plugins
->object(
1566 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1569 Pluginobj
* plugin_obj
= obj
->pluginobj();
1570 if (plugin_obj
== NULL
)
1572 Symbol_table
* symtab
= plugins
->symtab();
1573 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 2);
1576 // Version 3 of the above. The only difference from v2 is that it
1577 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1578 // decided to include.
1580 static enum ld_plugin_status
1581 get_symbols_v3(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1583 gold_assert(parameters
->options().has_plugins());
1584 Plugin_manager
* plugins
= parameters
->options().plugins();
1585 Object
* obj
= plugins
->object(
1586 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1589 Pluginobj
* plugin_obj
= obj
->pluginobj();
1590 if (plugin_obj
== NULL
)
1592 Symbol_table
* symtab
= plugins
->symtab();
1593 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 3);
1596 // Add a new (real) input file generated by a plugin.
1598 static enum ld_plugin_status
1599 add_input_file(const char* pathname
)
1601 gold_assert(parameters
->options().has_plugins());
1602 return parameters
->options().plugins()->add_input_file(pathname
, false);
1605 // Add a new (real) library required by a plugin.
1607 static enum ld_plugin_status
1608 add_input_library(const char* pathname
)
1610 gold_assert(parameters
->options().has_plugins());
1611 return parameters
->options().plugins()->add_input_file(pathname
, true);
1614 // Set the extra library path to be used by libraries added via
1615 // add_input_library
1617 static enum ld_plugin_status
1618 set_extra_library_path(const char* path
)
1620 gold_assert(parameters
->options().has_plugins());
1621 return parameters
->options().plugins()->set_extra_library_path(path
);
1624 // Issue a diagnostic message from a plugin.
1626 static enum ld_plugin_status
1627 message(int level
, const char* format
, ...)
1630 va_start(args
, format
);
1635 parameters
->errors()->info(format
, args
);
1638 parameters
->errors()->warning(format
, args
);
1642 parameters
->errors()->error(format
, args
);
1645 parameters
->errors()->fatal(format
, args
);
1653 // Get the section count of the object corresponding to the handle. This
1654 // plugin interface can only be called in the claim_file handler of the plugin.
1656 static enum ld_plugin_status
1657 get_input_section_count(const void* handle
, unsigned int* count
)
1659 gold_assert(parameters
->options().has_plugins());
1661 if (!parameters
->options().plugins()->in_claim_file_handler())
1664 Object
* obj
= parameters
->options().plugins()->get_elf_object(handle
);
1669 *count
= obj
->shnum();
1673 // Get the type of the specified section in the object corresponding
1674 // to the handle. This plugin interface can only be called in the
1675 // claim_file handler of the plugin.
1677 static enum ld_plugin_status
1678 get_input_section_type(const struct ld_plugin_section section
,
1681 gold_assert(parameters
->options().has_plugins());
1683 if (!parameters
->options().plugins()->in_claim_file_handler())
1687 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1690 return LDPS_BAD_HANDLE
;
1692 *type
= obj
->section_type(section
.shndx
);
1696 // Get the name of the specified section in the object corresponding
1697 // to the handle. This plugin interface can only be called in the
1698 // claim_file handler of the plugin.
1700 static enum ld_plugin_status
1701 get_input_section_name(const struct ld_plugin_section section
,
1702 char** section_name_ptr
)
1704 gold_assert(parameters
->options().has_plugins());
1706 if (!parameters
->options().plugins()->in_claim_file_handler())
1710 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1713 return LDPS_BAD_HANDLE
;
1715 // Check if the object is locked before getting the section name.
1716 gold_assert(obj
->is_locked());
1718 const std::string section_name
= obj
->section_name(section
.shndx
);
1719 *section_name_ptr
= static_cast<char*>(malloc(section_name
.length() + 1));
1720 memcpy(*section_name_ptr
, section_name
.c_str(), section_name
.length() + 1);
1724 // Get the contents of the specified section in the object corresponding
1725 // to the handle. This plugin interface can only be called in the
1726 // claim_file handler of the plugin.
1728 static enum ld_plugin_status
1729 get_input_section_contents(const struct ld_plugin_section section
,
1730 const unsigned char** section_contents_ptr
,
1733 gold_assert(parameters
->options().has_plugins());
1735 if (!parameters
->options().plugins()->in_claim_file_handler())
1739 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1742 return LDPS_BAD_HANDLE
;
1744 // Check if the object is locked before getting the section contents.
1745 gold_assert(obj
->is_locked());
1747 section_size_type plen
;
1748 *section_contents_ptr
1749 = obj
->section_contents(section
.shndx
, &plen
, false);
1754 // Get the alignment of the specified section in the object corresponding
1755 // to the handle. This plugin interface can only be called in the
1756 // claim_file handler of the plugin.
1758 static enum ld_plugin_status
1759 get_input_section_alignment(const struct ld_plugin_section section
,
1760 unsigned int* addralign
)
1762 gold_assert(parameters
->options().has_plugins());
1764 if (!parameters
->options().plugins()->in_claim_file_handler())
1768 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1771 return LDPS_BAD_HANDLE
;
1773 *addralign
= obj
->section_addralign(section
.shndx
);
1777 // Get the size of the specified section in the object corresponding
1778 // to the handle. This plugin interface can only be called in the
1779 // claim_file handler of the plugin.
1781 static enum ld_plugin_status
1782 get_input_section_size(const struct ld_plugin_section section
,
1785 gold_assert(parameters
->options().has_plugins());
1787 if (!parameters
->options().plugins()->in_claim_file_handler())
1791 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1794 return LDPS_BAD_HANDLE
;
1796 *secsize
= obj
->section_size(section
.shndx
);
1801 // Specify the ordering of sections in the final layout. The sections are
1802 // specified as (handle,shndx) pairs in the two arrays in the order in
1803 // which they should appear in the final layout.
1805 static enum ld_plugin_status
1806 update_section_order(const struct ld_plugin_section
* section_list
,
1807 unsigned int num_sections
)
1809 gold_assert(parameters
->options().has_plugins());
1811 if (num_sections
== 0)
1814 if (section_list
== NULL
)
1817 Layout
* layout
= parameters
->options().plugins()->layout();
1818 gold_assert (layout
!= NULL
);
1820 std::map
<Section_id
, unsigned int>* order_map
1821 = layout
->get_section_order_map();
1823 /* Store the mapping from Section_id to section position in layout's
1824 order_map to consult after output sections are added. */
1825 for (unsigned int i
= 0; i
< num_sections
; ++i
)
1827 Object
* obj
= parameters
->options().plugins()->get_elf_object(
1828 section_list
[i
].handle
);
1829 if (obj
== NULL
|| obj
->is_dynamic())
1830 return LDPS_BAD_HANDLE
;
1831 unsigned int shndx
= section_list
[i
].shndx
;
1832 Section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
1833 (*order_map
)[secn_id
] = i
+ 1;
1839 // Let the linker know that the sections could be reordered.
1841 static enum ld_plugin_status
1842 allow_section_ordering()
1844 gold_assert(parameters
->options().has_plugins());
1845 Layout
* layout
= parameters
->options().plugins()->layout();
1846 layout
->set_section_ordering_specified();
1850 // Let the linker know that a subset of sections could be mapped
1851 // to a unique segment.
1853 static enum ld_plugin_status
1854 allow_unique_segment_for_sections()
1856 gold_assert(parameters
->options().has_plugins());
1857 Layout
* layout
= parameters
->options().plugins()->layout();
1858 layout
->set_unique_segment_for_sections_specified();
1862 // This function should map the list of sections specified in the
1863 // SECTION_LIST to a unique segment. ELF segments do not have names
1864 // and the NAME is used to identify Output Section which should contain
1865 // the list of sections. This Output Section will then be mapped to
1866 // a unique segment. FLAGS is used to specify if any additional segment
1867 // flags need to be set. For instance, a specific segment flag can be
1868 // set to identify this segment. Unsetting segment flags is not possible.
1869 // ALIGN specifies the alignment of the segment.
1871 static enum ld_plugin_status
1872 unique_segment_for_sections(const char* segment_name
,
1875 const struct ld_plugin_section
* section_list
,
1876 unsigned int num_sections
)
1878 gold_assert(parameters
->options().has_plugins());
1880 if (num_sections
== 0)
1883 if (section_list
== NULL
)
1886 Layout
* layout
= parameters
->options().plugins()->layout();
1887 gold_assert (layout
!= NULL
);
1889 Layout::Unique_segment_info
* s
= new Layout::Unique_segment_info
;
1890 s
->name
= segment_name
;
1894 for (unsigned int i
= 0; i
< num_sections
; ++i
)
1896 Object
* obj
= parameters
->options().plugins()->get_elf_object(
1897 section_list
[i
].handle
);
1898 if (obj
== NULL
|| obj
->is_dynamic())
1899 return LDPS_BAD_HANDLE
;
1900 unsigned int shndx
= section_list
[i
].shndx
;
1901 Const_section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
1902 layout
->insert_section_segment_map(secn_id
, s
);
1908 #endif // ENABLE_PLUGINS
1910 // Allocate a Pluginobj object of the appropriate size and endianness.
1913 make_sized_plugin_object(Input_file
* input_file
, off_t offset
, off_t filesize
)
1915 Pluginobj
* obj
= NULL
;
1917 parameters_force_valid_target();
1918 const Target
& target(parameters
->target());
1920 if (target
.get_size() == 32)
1922 if (target
.is_big_endian())
1923 #ifdef HAVE_TARGET_32_BIG
1924 obj
= new Sized_pluginobj
<32, true>(input_file
->filename(),
1925 input_file
, offset
, filesize
);
1927 gold_error(_("%s: not configured to support "
1928 "32-bit big-endian object"),
1929 input_file
->filename().c_str());
1932 #ifdef HAVE_TARGET_32_LITTLE
1933 obj
= new Sized_pluginobj
<32, false>(input_file
->filename(),
1934 input_file
, offset
, filesize
);
1936 gold_error(_("%s: not configured to support "
1937 "32-bit little-endian object"),
1938 input_file
->filename().c_str());
1941 else if (target
.get_size() == 64)
1943 if (target
.is_big_endian())
1944 #ifdef HAVE_TARGET_64_BIG
1945 obj
= new Sized_pluginobj
<64, true>(input_file
->filename(),
1946 input_file
, offset
, filesize
);
1948 gold_error(_("%s: not configured to support "
1949 "64-bit big-endian object"),
1950 input_file
->filename().c_str());
1953 #ifdef HAVE_TARGET_64_LITTLE
1954 obj
= new Sized_pluginobj
<64, false>(input_file
->filename(),
1955 input_file
, offset
, filesize
);
1957 gold_error(_("%s: not configured to support "
1958 "64-bit little-endian object"),
1959 input_file
->filename().c_str());
1963 gold_assert(obj
!= NULL
);
1967 } // End namespace gold.