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.
33 #include "libiberty.h"
38 #elif defined (HAVE_WINDOWS_H)
41 #error Unknown how to handle dynamic-load-libraries.
44 #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H)
46 #define RTLD_NOW 0 /* Dummy value. */
48 dlopen(const char *file
, int mode ATTRIBUTE_UNUSED
)
50 return LoadLibrary(file
);
54 dlsym(void *handle
, const char *name
)
56 return reinterpret_cast<void *>(
57 GetProcAddress(static_cast<HMODULE
>(handle
),name
));
63 return "unable to load dll";
66 #endif /* !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) */
67 #endif /* ENABLE_PLUGINS */
69 #include "parameters.h"
79 #include "descriptors.h"
87 // The linker's exported interfaces.
92 static enum ld_plugin_status
93 register_claim_file(ld_plugin_claim_file_handler handler
);
95 static enum ld_plugin_status
96 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
);
98 static enum ld_plugin_status
99 register_cleanup(ld_plugin_cleanup_handler handler
);
101 static enum ld_plugin_status
102 add_symbols(void *handle
, int nsyms
, const struct ld_plugin_symbol
*syms
);
104 static enum ld_plugin_status
105 get_input_file(const void *handle
, struct ld_plugin_input_file
*file
);
107 static enum ld_plugin_status
108 get_view(const void *handle
, const void **viewp
);
110 static enum ld_plugin_status
111 release_input_file(const void *handle
);
113 static enum ld_plugin_status
114 get_symbols(const void *handle
, int nsyms
, struct ld_plugin_symbol
*syms
);
116 static enum ld_plugin_status
117 get_symbols_v2(const void *handle
, int nsyms
, struct ld_plugin_symbol
*syms
);
119 static enum ld_plugin_status
120 get_symbols_v3(const void *handle
, int nsyms
, struct ld_plugin_symbol
*syms
);
122 static enum ld_plugin_status
123 add_input_file(const char *pathname
);
125 static enum ld_plugin_status
126 add_input_library(const char *pathname
);
128 static enum ld_plugin_status
129 set_extra_library_path(const char *path
);
131 static enum ld_plugin_status
132 message(int level
, const char *format
, ...);
134 static enum ld_plugin_status
135 get_input_section_count(const void* handle
, unsigned int* count
);
137 static enum ld_plugin_status
138 get_input_section_type(const struct ld_plugin_section section
,
141 static enum ld_plugin_status
142 get_input_section_name(const struct ld_plugin_section section
,
143 char** section_name_ptr
);
145 static enum ld_plugin_status
146 get_input_section_contents(const struct ld_plugin_section section
,
147 const unsigned char** section_contents
,
150 static enum ld_plugin_status
151 update_section_order(const struct ld_plugin_section
*section_list
,
152 unsigned int num_sections
);
154 static enum ld_plugin_status
155 allow_section_ordering();
157 static enum ld_plugin_status
158 allow_unique_segment_for_sections();
160 static enum ld_plugin_status
161 unique_segment_for_sections(const char* segment_name
,
164 const struct ld_plugin_section
*section_list
,
165 unsigned int num_sections
);
167 static enum ld_plugin_status
168 get_input_section_alignment(const struct ld_plugin_section section
,
169 unsigned int* addralign
);
171 static enum ld_plugin_status
172 get_input_section_size(const struct ld_plugin_section section
,
175 static enum ld_plugin_status
176 register_new_input(ld_plugin_new_input_handler handler
);
178 static enum ld_plugin_status
179 get_wrap_symbols(uint64_t *num_symbols
, const char ***wrap_symbol_list
);
183 #endif // ENABLE_PLUGINS
185 static Pluginobj
* make_sized_plugin_object(const std::string
& filename
,
186 Input_file
* input_file
,
187 off_t offset
, off_t filesize
);
191 // Load one plugin library.
196 #ifdef ENABLE_PLUGINS
197 // Load the plugin library.
198 // FIXME: Look for the library in standard locations.
199 this->handle_
= dlopen(this->filename_
.c_str(), RTLD_NOW
);
200 if (this->handle_
== NULL
)
202 gold_error(_("%s: could not load plugin library: %s"),
203 this->filename_
.c_str(), dlerror());
207 // Find the plugin's onload entry point.
208 void* ptr
= dlsym(this->handle_
, "onload");
211 gold_error(_("%s: could not find onload entry point"),
212 this->filename_
.c_str());
215 ld_plugin_onload onload
;
216 gold_assert(sizeof(onload
) == sizeof(ptr
));
217 memcpy(&onload
, &ptr
, sizeof(ptr
));
219 // Get the linker's version number.
220 const char* ver
= get_version_string();
223 sscanf(ver
, "%d.%d", &major
, &minor
);
225 // Allocate and populate a transfer vector.
226 const int tv_fixed_size
= 31;
228 int tv_size
= this->args_
.size() + tv_fixed_size
;
229 ld_plugin_tv
* tv
= new ld_plugin_tv
[tv_size
];
231 // Put LDPT_MESSAGE at the front of the list so the plugin can use it
232 // while processing subsequent entries.
234 tv
[i
].tv_tag
= LDPT_MESSAGE
;
235 tv
[i
].tv_u
.tv_message
= message
;
238 tv
[i
].tv_tag
= LDPT_API_VERSION
;
239 tv
[i
].tv_u
.tv_val
= LD_PLUGIN_API_VERSION
;
242 tv
[i
].tv_tag
= LDPT_GOLD_VERSION
;
243 tv
[i
].tv_u
.tv_val
= major
* 100 + minor
;
246 tv
[i
].tv_tag
= LDPT_LINKER_OUTPUT
;
247 if (parameters
->options().relocatable())
248 tv
[i
].tv_u
.tv_val
= LDPO_REL
;
249 else if (parameters
->options().shared())
250 tv
[i
].tv_u
.tv_val
= LDPO_DYN
;
251 else if (parameters
->options().pie())
252 tv
[i
].tv_u
.tv_val
= LDPO_PIE
;
254 tv
[i
].tv_u
.tv_val
= LDPO_EXEC
;
257 tv
[i
].tv_tag
= LDPT_OUTPUT_NAME
;
258 tv
[i
].tv_u
.tv_string
= parameters
->options().output();
260 for (unsigned int j
= 0; j
< this->args_
.size(); ++j
)
263 tv
[i
].tv_tag
= LDPT_OPTION
;
264 tv
[i
].tv_u
.tv_string
= this->args_
[j
].c_str();
268 tv
[i
].tv_tag
= LDPT_REGISTER_CLAIM_FILE_HOOK
;
269 tv
[i
].tv_u
.tv_register_claim_file
= register_claim_file
;
272 tv
[i
].tv_tag
= LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
;
273 tv
[i
].tv_u
.tv_register_all_symbols_read
= register_all_symbols_read
;
276 tv
[i
].tv_tag
= LDPT_REGISTER_CLEANUP_HOOK
;
277 tv
[i
].tv_u
.tv_register_cleanup
= register_cleanup
;
280 tv
[i
].tv_tag
= LDPT_ADD_SYMBOLS
;
281 tv
[i
].tv_u
.tv_add_symbols
= add_symbols
;
284 tv
[i
].tv_tag
= LDPT_GET_INPUT_FILE
;
285 tv
[i
].tv_u
.tv_get_input_file
= get_input_file
;
288 tv
[i
].tv_tag
= LDPT_GET_VIEW
;
289 tv
[i
].tv_u
.tv_get_view
= get_view
;
292 tv
[i
].tv_tag
= LDPT_RELEASE_INPUT_FILE
;
293 tv
[i
].tv_u
.tv_release_input_file
= release_input_file
;
296 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS
;
297 tv
[i
].tv_u
.tv_get_symbols
= get_symbols
;
300 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V2
;
301 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v2
;
304 tv
[i
].tv_tag
= LDPT_GET_SYMBOLS_V3
;
305 tv
[i
].tv_u
.tv_get_symbols
= get_symbols_v3
;
308 tv
[i
].tv_tag
= LDPT_ADD_INPUT_FILE
;
309 tv
[i
].tv_u
.tv_add_input_file
= add_input_file
;
312 tv
[i
].tv_tag
= LDPT_ADD_INPUT_LIBRARY
;
313 tv
[i
].tv_u
.tv_add_input_library
= add_input_library
;
316 tv
[i
].tv_tag
= LDPT_SET_EXTRA_LIBRARY_PATH
;
317 tv
[i
].tv_u
.tv_set_extra_library_path
= set_extra_library_path
;
320 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_COUNT
;
321 tv
[i
].tv_u
.tv_get_input_section_count
= get_input_section_count
;
324 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_TYPE
;
325 tv
[i
].tv_u
.tv_get_input_section_type
= get_input_section_type
;
328 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_NAME
;
329 tv
[i
].tv_u
.tv_get_input_section_name
= get_input_section_name
;
332 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_CONTENTS
;
333 tv
[i
].tv_u
.tv_get_input_section_contents
= get_input_section_contents
;
336 tv
[i
].tv_tag
= LDPT_UPDATE_SECTION_ORDER
;
337 tv
[i
].tv_u
.tv_update_section_order
= update_section_order
;
340 tv
[i
].tv_tag
= LDPT_ALLOW_SECTION_ORDERING
;
341 tv
[i
].tv_u
.tv_allow_section_ordering
= allow_section_ordering
;
344 tv
[i
].tv_tag
= LDPT_ALLOW_UNIQUE_SEGMENT_FOR_SECTIONS
;
345 tv
[i
].tv_u
.tv_allow_unique_segment_for_sections
346 = allow_unique_segment_for_sections
;
349 tv
[i
].tv_tag
= LDPT_UNIQUE_SEGMENT_FOR_SECTIONS
;
350 tv
[i
].tv_u
.tv_unique_segment_for_sections
= unique_segment_for_sections
;
353 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_ALIGNMENT
;
354 tv
[i
].tv_u
.tv_get_input_section_alignment
= get_input_section_alignment
;
357 tv
[i
].tv_tag
= LDPT_GET_INPUT_SECTION_SIZE
;
358 tv
[i
].tv_u
.tv_get_input_section_size
= get_input_section_size
;
361 tv
[i
].tv_tag
= LDPT_REGISTER_NEW_INPUT_HOOK
;
362 tv
[i
].tv_u
.tv_register_new_input
= register_new_input
;
365 tv
[i
].tv_tag
= LDPT_GET_WRAP_SYMBOLS
;
366 tv
[i
].tv_u
.tv_get_wrap_symbols
= get_wrap_symbols
;
369 tv
[i
].tv_tag
= LDPT_NULL
;
370 tv
[i
].tv_u
.tv_val
= 0;
372 gold_assert(i
== tv_size
- 1);
374 // Call the onload entry point.
378 #endif // ENABLE_PLUGINS
381 // Call the plugin claim-file handler.
384 Plugin::claim_file(struct ld_plugin_input_file
* plugin_input_file
)
388 if (this->claim_file_handler_
!= NULL
)
390 (*this->claim_file_handler_
)(plugin_input_file
, &claimed
);
397 // Call the all-symbols-read handler.
400 Plugin::all_symbols_read()
402 if (this->all_symbols_read_handler_
!= NULL
)
403 (*this->all_symbols_read_handler_
)();
406 // Call the new_input handler.
409 Plugin::new_input(struct ld_plugin_input_file
* plugin_input_file
)
411 if (this->new_input_handler_
!= NULL
)
412 (*this->new_input_handler_
)(plugin_input_file
);
415 // Call the cleanup handler.
420 if (this->cleanup_handler_
!= NULL
&& !this->cleanup_done_
)
422 // Set this flag before calling to prevent a recursive plunge
423 // in the event that a plugin's cleanup handler issues a
425 this->cleanup_done_
= true;
426 (*this->cleanup_handler_
)();
430 // This task is used to rescan archives as needed.
432 class Plugin_rescan
: public Task
435 Plugin_rescan(Task_token
* this_blocker
, Task_token
* next_blocker
)
436 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
441 delete this->this_blocker_
;
447 if (this->this_blocker_
->is_blocked())
448 return this->this_blocker_
;
453 locks(Task_locker
* tl
)
454 { tl
->add(this, this->next_blocker_
); }
458 { parameters
->options().plugins()->rescan(this); }
462 { return "Plugin_rescan"; }
465 Task_token
* this_blocker_
;
466 Task_token
* next_blocker_
;
469 // Plugin_recorder logs plugin actions and saves intermediate files
472 class Plugin_recorder
475 Plugin_recorder() : file_count_(0), tempdir_(NULL
), logfile_(NULL
)
482 claimed_file(const std::string
& obj_name
, off_t offset
, off_t filesize
,
483 const std::string
& plugin_name
);
486 unclaimed_file(const std::string
& obj_name
, off_t offset
, off_t filesize
);
489 replacement_file(const char* name
, bool is_lib
);
492 record_symbols(const Object
* obj
, int nsyms
,
493 const struct ld_plugin_symbol
* syms
);
497 { ::fclose(this->logfile_
); }
500 unsigned int file_count_
;
501 const char* tempdir_
;
506 Plugin_recorder::init()
508 // Create a temporary directory where we can stash the log and
509 // copies of replacement files.
510 char dir_template
[] = "gold-recording-XXXXXX";
511 if (mkdtemp(dir_template
) == NULL
)
514 size_t len
= strlen(dir_template
) + 1;
515 char* tempdir
= new char[len
];
516 strncpy(tempdir
, dir_template
, len
);
518 // Create the log file.
519 std::string
logname(tempdir
);
520 logname
.append("/log");
521 FILE* logfile
= ::fopen(logname
.c_str(), "w");
525 this->tempdir_
= tempdir
;
526 this->logfile_
= logfile
;
528 gold_info(_("%s: recording to %s"), program_name
, this->tempdir_
);
534 Plugin_recorder::claimed_file(const std::string
& obj_name
,
537 const std::string
& plugin_name
)
539 fprintf(this->logfile_
, "PLUGIN: %s\n", plugin_name
.c_str());
540 fprintf(this->logfile_
, "CLAIMED: %s", obj_name
.c_str());
542 fprintf(this->logfile_
, " @%ld", static_cast<long>(offset
));
543 fprintf(this->logfile_
, " %ld\n", static_cast<long>(filesize
));
547 Plugin_recorder::unclaimed_file(const std::string
& obj_name
,
551 fprintf(this->logfile_
, "UNCLAIMED: %s", obj_name
.c_str());
553 fprintf(this->logfile_
, " @%ld", static_cast<long>(offset
));
554 fprintf(this->logfile_
, " %ld\n", static_cast<long>(filesize
));
557 // Make a hard link to INNAME from OUTNAME, if possible.
558 // If not, copy the file.
561 link_or_copy_file(const char* inname
, const char* outname
)
563 static char buf
[4096];
565 if (::link(inname
, outname
) == 0)
568 int in
= ::open(inname
, O_RDONLY
);
571 gold_warning(_("%s: can't open (%s)"), inname
, strerror(errno
));
574 int out
= ::open(outname
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
577 gold_warning(_("%s: can't create (%s)"), outname
, strerror(errno
));
582 while ((len
= ::read(in
, buf
, sizeof(buf
))) > 0)
584 if (::write(out
, buf
, len
) != len
)
586 gold_warning(_("%s: write error while making copy of file (%s)"),
587 inname
, strerror(errno
));
597 Plugin_recorder::replacement_file(const char* name
, bool is_lib
)
599 fprintf(this->logfile_
, "REPLACEMENT: %s", name
);
601 fprintf(this->logfile_
, "(lib)");
605 const char* basename
= lbasename(name
);
606 snprintf(counter
, sizeof(counter
), "%05d", this->file_count_
);
608 std::string
outname(this->tempdir_
);
610 outname
.append(counter
);
612 outname
.append(basename
);
613 if (link_or_copy_file(name
, outname
.c_str()))
614 fprintf(this->logfile_
, " -> %s", outname
.c_str());
616 fprintf(this->logfile_
, "\n");
620 Plugin_recorder::record_symbols(const Object
* obj
, int nsyms
,
621 const struct ld_plugin_symbol
* syms
)
623 fprintf(this->logfile_
, "SYMBOLS: %d %s\n", nsyms
, obj
->name().c_str());
624 for (int i
= 0; i
< nsyms
; ++i
)
626 const struct ld_plugin_symbol
* isym
= &syms
[i
];
652 switch (isym
->visibility
)
671 fprintf(this->logfile_
, " %5d: %-2s %c %s", i
, def
, vis
, isym
->name
);
672 if (isym
->version
!= NULL
&& isym
->version
[0] != '\0')
673 fprintf(this->logfile_
, "@%s", isym
->version
);
674 if (isym
->comdat_key
!= NULL
&& isym
->comdat_key
[0] != '\0')
676 if (strcmp(isym
->name
, isym
->comdat_key
) == 0)
677 fprintf(this->logfile_
, " [comdat]");
679 fprintf(this->logfile_
, " [comdat: %s]", isym
->comdat_key
);
681 fprintf(this->logfile_
, "\n");
685 // Plugin_manager methods.
687 Plugin_manager::~Plugin_manager()
689 for (Plugin_list::iterator p
= this->plugins_
.begin();
690 p
!= this->plugins_
.end();
693 this->plugins_
.clear();
694 for (Object_list::iterator obj
= this->objects_
.begin();
695 obj
!= this->objects_
.end();
698 this->objects_
.clear();
700 delete this->recorder_
;
703 // Load all plugin libraries.
706 Plugin_manager::load_plugins(Layout
* layout
)
708 this->layout_
= layout
;
710 if (is_debugging_enabled(DEBUG_PLUGIN
))
712 this->recorder_
= new Plugin_recorder();
713 this->recorder_
->init();
716 for (this->current_
= this->plugins_
.begin();
717 this->current_
!= this->plugins_
.end();
719 (*this->current_
)->load();
722 // Call the plugin claim-file handlers in turn to see if any claim the file.
725 Plugin_manager::claim_file(Input_file
* input_file
, off_t offset
,
726 off_t filesize
, Object
* elf_object
)
728 bool lock_initialized
= this->initialize_lock_
.initialize();
730 gold_assert(lock_initialized
);
731 Hold_lock
hl(*this->lock_
);
733 unsigned int handle
= this->objects_
.size();
734 this->input_file_
= input_file
;
735 this->plugin_input_file_
.name
= input_file
->filename().c_str();
736 this->plugin_input_file_
.fd
= input_file
->file().descriptor();
737 this->plugin_input_file_
.offset
= offset
;
738 this->plugin_input_file_
.filesize
= filesize
;
739 this->plugin_input_file_
.handle
= reinterpret_cast<void*>(handle
);
740 if (elf_object
!= NULL
)
741 this->objects_
.push_back(elf_object
);
742 this->in_claim_file_handler_
= true;
744 for (this->current_
= this->plugins_
.begin();
745 this->current_
!= this->plugins_
.end();
748 // If we aren't yet in replacement phase, allow plugins to claim input
749 // files, otherwise notify the plugin of the new input file, if needed.
750 if (!this->in_replacement_phase_
)
752 if ((*this->current_
)->claim_file(&this->plugin_input_file_
))
754 this->any_claimed_
= true;
755 this->in_claim_file_handler_
= false;
757 if (this->recorder_
!= NULL
)
759 const std::string
& objname
= (elf_object
== NULL
760 ? input_file
->filename()
761 : elf_object
->name());
762 this->recorder_
->claimed_file(objname
,
764 (*this->current_
)->filename());
767 if (this->objects_
.size() > handle
768 && this->objects_
[handle
]->pluginobj() != NULL
)
769 return this->objects_
[handle
]->pluginobj();
771 // If the plugin claimed the file but did not call the
772 // add_symbols callback, we need to create the Pluginobj now.
773 Pluginobj
* obj
= this->make_plugin_object(handle
);
779 (*this->current_
)->new_input(&this->plugin_input_file_
);
783 this->in_claim_file_handler_
= false;
785 if (this->recorder_
!= NULL
)
786 this->recorder_
->unclaimed_file(input_file
->filename(), offset
, filesize
);
791 // Save an archive. This is used so that a plugin can add a file
792 // which refers to a symbol which was not previously referenced. In
793 // that case we want to pretend that the symbol was referenced before,
794 // and pull in the archive object.
797 Plugin_manager::save_archive(Archive
* archive
)
799 if (this->in_replacement_phase_
|| !this->any_claimed_
)
802 this->rescannable_
.push_back(Rescannable(archive
));
805 // Save an Input_group. This is like save_archive.
808 Plugin_manager::save_input_group(Input_group
* input_group
)
810 if (this->in_replacement_phase_
|| !this->any_claimed_
)
813 this->rescannable_
.push_back(Rescannable(input_group
));
816 // Call the all-symbols-read handlers.
819 Plugin_manager::all_symbols_read(Workqueue
* workqueue
, Task
* task
,
820 Input_objects
* input_objects
,
821 Symbol_table
* symtab
,
822 Dirsearch
* dirpath
, Mapfile
* mapfile
,
823 Task_token
** last_blocker
)
825 this->in_replacement_phase_
= true;
826 this->workqueue_
= workqueue
;
828 this->input_objects_
= input_objects
;
829 this->symtab_
= symtab
;
830 this->dirpath_
= dirpath
;
831 this->mapfile_
= mapfile
;
832 this->this_blocker_
= NULL
;
834 // Set symbols used in defsym expressions as seen in real ELF.
835 Layout
*layout
= parameters
->options().plugins()->layout();
836 layout
->script_options()->set_defsym_uses_in_real_elf(symtab
);
837 layout
->script_options()->find_defsym_defs(this->defsym_defines_set_
);
839 for (this->current_
= this->plugins_
.begin();
840 this->current_
!= this->plugins_
.end();
842 (*this->current_
)->all_symbols_read();
844 if (this->any_added_
)
846 Task_token
* next_blocker
= new Task_token(true);
847 next_blocker
->add_blocker();
848 workqueue
->queue(new Plugin_rescan(this->this_blocker_
, next_blocker
));
849 this->this_blocker_
= next_blocker
;
852 *last_blocker
= this->this_blocker_
;
855 // This is called when we see a new undefined symbol. If we are in
856 // the replacement phase, this means that we may need to rescan some
857 // archives we have previously seen.
860 Plugin_manager::new_undefined_symbol(Symbol
* sym
)
862 if (this->in_replacement_phase_
)
863 this->undefined_symbols_
.push_back(sym
);
866 // Rescan archives as needed. This handles the case where a new
867 // object file added by a plugin has an undefined reference to some
868 // symbol defined in an archive.
871 Plugin_manager::rescan(Task
* task
)
873 size_t rescan_pos
= 0;
874 size_t rescan_size
= this->rescannable_
.size();
875 while (!this->undefined_symbols_
.empty())
877 if (rescan_pos
>= rescan_size
)
879 this->undefined_symbols_
.clear();
883 Undefined_symbol_list undefs
;
884 undefs
.reserve(this->undefined_symbols_
.size());
885 this->undefined_symbols_
.swap(undefs
);
887 size_t min_rescan_pos
= rescan_size
;
889 for (Undefined_symbol_list::const_iterator p
= undefs
.begin();
893 if (!(*p
)->is_undefined())
896 this->undefined_symbols_
.push_back(*p
);
898 // Find the first rescan archive which defines this symbol,
899 // starting at the current rescan position. The rescan position
900 // exists so that given -la -lb -lc we don't look for undefined
901 // symbols in -lb back in -la, but instead get the definition
902 // from -lc. Don't bother to look past the current minimum
904 for (size_t i
= rescan_pos
; i
< min_rescan_pos
; ++i
)
906 if (this->rescannable_defines(i
, *p
))
914 if (min_rescan_pos
>= rescan_size
)
916 // We didn't find any rescannable archives which define any
917 // undefined symbols.
921 const Rescannable
& r(this->rescannable_
[min_rescan_pos
]);
924 Task_lock_obj
<Archive
> tl(task
, r
.u
.archive
);
925 r
.u
.archive
->add_symbols(this->symtab_
, this->layout_
,
926 this->input_objects_
, this->mapfile_
);
930 size_t next_saw_undefined
= this->symtab_
->saw_undefined();
931 size_t saw_undefined
;
934 saw_undefined
= next_saw_undefined
;
936 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
937 p
!= r
.u
.input_group
->end();
940 Task_lock_obj
<Archive
> tl(task
, *p
);
942 (*p
)->add_symbols(this->symtab_
, this->layout_
,
943 this->input_objects_
, this->mapfile_
);
946 next_saw_undefined
= this->symtab_
->saw_undefined();
948 while (saw_undefined
!= next_saw_undefined
);
951 for (size_t i
= rescan_pos
; i
< min_rescan_pos
+ 1; ++i
)
953 if (this->rescannable_
[i
].is_archive
)
954 delete this->rescannable_
[i
].u
.archive
;
956 delete this->rescannable_
[i
].u
.input_group
;
959 rescan_pos
= min_rescan_pos
+ 1;
963 // Return whether the rescannable at index I defines SYM.
966 Plugin_manager::rescannable_defines(size_t i
, Symbol
* sym
)
968 const Rescannable
& r(this->rescannable_
[i
]);
970 return r
.u
.archive
->defines_symbol(sym
);
973 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
974 p
!= r
.u
.input_group
->end();
977 if ((*p
)->defines_symbol(sym
))
984 // Layout deferred objects.
987 Plugin_manager::layout_deferred_objects()
989 Deferred_layout_list::iterator obj
;
991 for (obj
= this->deferred_layout_objects_
.begin();
992 obj
!= this->deferred_layout_objects_
.end();
995 // Lock the object so we can read from it. This is only called
996 // single-threaded from queue_middle_tasks, so it is OK to lock.
997 // Unfortunately we have no way to pass in a Task token.
998 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
999 Task_lock_obj
<Object
> tl(dummy_task
, *obj
);
1000 (*obj
)->layout_deferred_sections(this->layout_
);
1004 // Call the cleanup handlers.
1007 Plugin_manager::cleanup()
1009 if (this->any_added_
)
1011 // If any input files were added, close all the input files.
1012 // This is because the plugin may want to remove them, and on
1013 // Windows you are not allowed to remove an open file.
1014 close_all_descriptors();
1017 for (this->current_
= this->plugins_
.begin();
1018 this->current_
!= this->plugins_
.end();
1020 (*this->current_
)->cleanup();
1023 // Make a new Pluginobj object. This is called when the plugin calls
1024 // the add_symbols API.
1027 Plugin_manager::make_plugin_object(unsigned int handle
)
1029 // Make sure we aren't asked to make an object for the same handle twice.
1030 if (this->objects_
.size() != handle
1031 && this->objects_
[handle
]->pluginobj() != NULL
)
1034 const std::string
* filename
= &this->input_file_
->filename();
1036 // If the elf object for this file was pushed into the objects_ vector,
1037 // use its filename, then delete it to make room for the Pluginobj as
1038 // this file is claimed.
1039 if (this->objects_
.size() != handle
)
1041 filename
= &this->objects_
.back()->name();
1042 this->objects_
.pop_back();
1045 Pluginobj
* obj
= make_sized_plugin_object(*filename
,
1047 this->plugin_input_file_
.offset
,
1048 this->plugin_input_file_
.filesize
);
1052 this->objects_
.push_back(obj
);
1056 // Get the input file information with an open (possibly re-opened)
1060 Plugin_manager::get_input_file(unsigned int handle
,
1061 struct ld_plugin_input_file
* file
)
1063 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1065 return LDPS_BAD_HANDLE
;
1067 obj
->lock(this->task_
);
1068 file
->name
= obj
->filename().c_str();
1069 file
->fd
= obj
->descriptor();
1070 file
->offset
= obj
->offset();
1071 file
->filesize
= obj
->filesize();
1072 file
->handle
= reinterpret_cast<void*>(handle
);
1076 // Release the input file.
1079 Plugin_manager::release_input_file(unsigned int handle
)
1081 if (this->object(handle
) == NULL
)
1082 return LDPS_BAD_HANDLE
;
1084 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1087 return LDPS_BAD_HANDLE
;
1089 obj
->unlock(this->task_
);
1093 // Get the elf object corresponding to the handle. Return NULL if we
1094 // found a Pluginobj instead.
1097 Plugin_manager::get_elf_object(const void* handle
)
1099 Object
* obj
= this->object(
1100 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1102 // The object should not be a Pluginobj.
1104 || obj
->pluginobj() != NULL
)
1111 Plugin_manager::get_view(unsigned int handle
, const void **viewp
)
1115 Input_file
*input_file
;
1116 if (this->in_claim_file_handler_
)
1118 // We are being called from the claim_file hook.
1119 const struct ld_plugin_input_file
&f
= this->plugin_input_file_
;
1121 filesize
= f
.filesize
;
1122 input_file
= this->input_file_
;
1126 // An already claimed file.
1127 if (this->object(handle
) == NULL
)
1128 return LDPS_BAD_HANDLE
;
1129 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1131 return LDPS_BAD_HANDLE
;
1132 offset
= obj
->offset();
1133 filesize
= obj
->filesize();
1134 input_file
= obj
->input_file();
1136 *viewp
= (void*) input_file
->file().get_view(offset
, 0, filesize
, false,
1141 // Add a new library path.
1144 Plugin_manager::set_extra_library_path(const char* path
)
1146 this->extra_search_path_
= std::string(path
);
1150 // Add a new input file.
1153 Plugin_manager::add_input_file(const char* pathname
, bool is_lib
)
1155 Input_file_argument
file(pathname
,
1157 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
1158 : Input_file_argument::INPUT_FILE_TYPE_FILE
),
1160 ? this->extra_search_path_
.c_str()
1164 Input_argument
* input_argument
= new Input_argument(file
);
1165 Task_token
* next_blocker
= new Task_token(true);
1166 next_blocker
->add_blocker();
1167 if (parameters
->incremental())
1168 gold_error(_("input files added by plug-ins in --incremental mode not "
1171 if (this->recorder_
!= NULL
)
1172 this->recorder_
->replacement_file(pathname
, is_lib
);
1174 this->workqueue_
->queue_soon(new Read_symbols(this->input_objects_
,
1183 this->this_blocker_
,
1185 this->this_blocker_
= next_blocker
;
1186 this->any_added_
= true;
1192 Pluginobj::Pluginobj(const std::string
& name
, Input_file
* input_file
,
1193 off_t offset
, off_t filesize
)
1194 : Object(name
, input_file
, false, offset
),
1195 nsyms_(0), syms_(NULL
), symbols_(), filesize_(filesize
), comdat_map_()
1199 // Return TRUE if a defined symbol is referenced from outside the
1200 // universe of claimed objects. Only references from relocatable,
1201 // non-IR (unclaimed) objects count as a reference. References from
1202 // dynamic objects count only as "visible".
1205 is_referenced_from_outside(Symbol
* lsym
)
1207 if (lsym
->in_real_elf())
1209 if (parameters
->options().relocatable())
1211 if (parameters
->options().is_undefined(lsym
->name()))
1216 // Return TRUE if a defined symbol might be reachable from outside the
1220 is_visible_from_outside(Symbol
* lsym
)
1224 if (parameters
->options().export_dynamic() || parameters
->options().shared()
1225 || parameters
->options().in_dynamic_list(lsym
->name())
1226 || parameters
->options().is_export_dynamic_symbol(lsym
->name()))
1227 return lsym
->is_externally_visible();
1231 // Get symbol resolution info.
1234 Pluginobj::get_symbol_resolution_info(Symbol_table
* symtab
,
1236 ld_plugin_symbol
* syms
,
1239 // For version 1 of this interface, we cannot use
1240 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
1242 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
1244 ? LDPR_PREVAILING_DEF_IRONLY_EXP
1245 : LDPR_PREVAILING_DEF
);
1247 if (nsyms
> this->nsyms_
)
1248 return LDPS_NO_SYMS
;
1250 if (static_cast<size_t>(nsyms
) > this->symbols_
.size())
1252 // We never decided to include this object. We mark all symbols as
1254 gold_assert(this->symbols_
.size() == 0);
1255 for (int i
= 0; i
< nsyms
; i
++)
1256 syms
[i
].resolution
= LDPR_PREEMPTED_REG
;
1257 return version
> 2 ? LDPS_NO_SYMS
: LDPS_OK
;
1260 Plugin_manager
* plugins
= parameters
->options().plugins();
1261 for (int i
= 0; i
< nsyms
; i
++)
1263 ld_plugin_symbol
* isym
= &syms
[i
];
1264 Symbol
* lsym
= this->symbols_
[i
];
1265 if (lsym
->is_forwarder())
1266 lsym
= symtab
->resolve_forwards(lsym
);
1267 ld_plugin_symbol_resolution res
= LDPR_UNKNOWN
;
1269 if (plugins
->is_defsym_def(lsym
->name()))
1271 // The symbol is redefined via defsym.
1272 res
= LDPR_PREEMPTED_REG
;
1274 else if (lsym
->is_undefined())
1276 // The symbol remains undefined.
1279 else if (isym
->def
== LDPK_UNDEF
1280 || isym
->def
== LDPK_WEAKUNDEF
1281 || isym
->def
== LDPK_COMMON
)
1283 // The original symbol was undefined or common.
1284 if (lsym
->source() != Symbol::FROM_OBJECT
)
1285 res
= LDPR_RESOLVED_EXEC
;
1286 else if (lsym
->object()->pluginobj() == this)
1288 if (is_referenced_from_outside(lsym
))
1289 res
= LDPR_PREVAILING_DEF
;
1290 else if (is_visible_from_outside(lsym
))
1291 res
= ldpr_prevailing_def_ironly_exp
;
1293 res
= LDPR_PREVAILING_DEF_IRONLY
;
1295 else if (lsym
->object()->pluginobj() != NULL
)
1296 res
= LDPR_RESOLVED_IR
;
1297 else if (lsym
->object()->is_dynamic())
1298 res
= LDPR_RESOLVED_DYN
;
1300 res
= LDPR_RESOLVED_EXEC
;
1304 // The original symbol was a definition.
1305 if (lsym
->source() != Symbol::FROM_OBJECT
)
1306 res
= LDPR_PREEMPTED_REG
;
1307 else if (lsym
->object() == static_cast<const Object
*>(this))
1309 if (is_referenced_from_outside(lsym
))
1310 res
= LDPR_PREVAILING_DEF
;
1311 else if (is_visible_from_outside(lsym
))
1312 res
= ldpr_prevailing_def_ironly_exp
;
1314 res
= LDPR_PREVAILING_DEF_IRONLY
;
1317 res
= (lsym
->object()->pluginobj() != NULL
1319 : LDPR_PREEMPTED_REG
);
1321 isym
->resolution
= res
;
1326 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1330 Pluginobj::include_comdat_group(std::string comdat_key
, Layout
* layout
)
1332 std::pair
<Comdat_map::iterator
, bool> ins
=
1333 this->comdat_map_
.insert(std::make_pair(comdat_key
, false));
1335 // If this is the first time we've seen this comdat key, ask the
1336 // layout object whether it should be included.
1338 ins
.first
->second
= layout
->find_or_add_kept_section(comdat_key
,
1342 return ins
.first
->second
;
1345 // Class Sized_pluginobj.
1347 template<int size
, bool big_endian
>
1348 Sized_pluginobj
<size
, big_endian
>::Sized_pluginobj(
1349 const std::string
& name
,
1350 Input_file
* input_file
,
1353 : Pluginobj(name
, input_file
, offset
, filesize
)
1357 // Read the symbols. Not used for plugin objects.
1359 template<int size
, bool big_endian
>
1361 Sized_pluginobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1366 // Lay out the input sections. Not used for plugin objects.
1368 template<int size
, bool big_endian
>
1370 Sized_pluginobj
<size
, big_endian
>::do_layout(Symbol_table
*, Layout
*,
1376 // Add the symbols to the symbol table.
1378 template<int size
, bool big_endian
>
1380 Sized_pluginobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1384 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1385 unsigned char symbuf
[sym_size
];
1386 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
1387 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
1389 Plugin_recorder
* recorder
= parameters
->options().plugins()->recorder();
1390 if (recorder
!= NULL
)
1391 recorder
->record_symbols(this, this->nsyms_
, this->syms_
);
1393 this->symbols_
.resize(this->nsyms_
);
1395 for (int i
= 0; i
< this->nsyms_
; ++i
)
1397 const struct ld_plugin_symbol
* isym
= &this->syms_
[i
];
1398 const char* name
= isym
->name
;
1399 const char* ver
= isym
->version
;
1400 elfcpp::Elf_Half shndx
;
1404 if (name
!= NULL
&& name
[0] == '\0')
1406 if (ver
!= NULL
&& ver
[0] == '\0')
1412 case LDPK_WEAKUNDEF
:
1413 bind
= elfcpp::STB_WEAK
;
1419 bind
= elfcpp::STB_GLOBAL
;
1427 // We use an arbitrary section number for a defined symbol.
1431 shndx
= elfcpp::SHN_COMMON
;
1434 case LDPK_WEAKUNDEF
:
1436 shndx
= elfcpp::SHN_UNDEF
;
1440 switch (isym
->visibility
)
1442 case LDPV_PROTECTED
:
1443 vis
= elfcpp::STV_PROTECTED
;
1446 vis
= elfcpp::STV_INTERNAL
;
1449 vis
= elfcpp::STV_HIDDEN
;
1453 vis
= elfcpp::STV_DEFAULT
;
1457 if (isym
->comdat_key
!= NULL
1458 && isym
->comdat_key
[0] != '\0'
1459 && !this->include_comdat_group(isym
->comdat_key
, layout
))
1460 shndx
= elfcpp::SHN_UNDEF
;
1462 osym
.put_st_name(0);
1463 osym
.put_st_value(0);
1464 osym
.put_st_size(0);
1465 osym
.put_st_info(bind
, elfcpp::STT_NOTYPE
);
1466 osym
.put_st_other(vis
, 0);
1467 osym
.put_st_shndx(shndx
);
1470 symtab
->add_from_pluginobj
<size
, big_endian
>(this, name
, ver
, &sym
);
1474 template<int size
, bool big_endian
>
1475 Archive::Should_include
1476 Sized_pluginobj
<size
, big_endian
>::do_should_include_member(
1477 Symbol_table
* symtab
,
1482 char* tmpbuf
= NULL
;
1483 size_t tmpbuflen
= 0;
1485 for (int i
= 0; i
< this->nsyms_
; ++i
)
1487 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1488 if (sym
.def
== LDPK_UNDEF
|| sym
.def
== LDPK_WEAKUNDEF
)
1490 const char* name
= sym
.name
;
1492 Archive::Should_include t
= Archive::should_include_member(symtab
,
1498 if (t
== Archive::SHOULD_INCLUDE_YES
)
1507 return Archive::SHOULD_INCLUDE_UNKNOWN
;
1510 // Iterate over global symbols, calling a visitor class V for each.
1512 template<int size
, bool big_endian
>
1514 Sized_pluginobj
<size
, big_endian
>::do_for_all_global_symbols(
1516 Library_base::Symbol_visitor_base
* v
)
1518 for (int i
= 0; i
< this->nsyms_
; ++i
)
1520 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1521 if (sym
.def
!= LDPK_UNDEF
)
1526 // Iterate over local symbols, calling a visitor class V for each GOT offset
1527 // associated with a local symbol.
1528 template<int size
, bool big_endian
>
1530 Sized_pluginobj
<size
, big_endian
>::do_for_all_local_got_entries(
1531 Got_offset_list::Visitor
*) const
1536 // Get the size of a section. Not used for plugin objects.
1538 template<int size
, bool big_endian
>
1540 Sized_pluginobj
<size
, big_endian
>::do_section_size(unsigned int)
1546 // Get the name of a section. Not used for plugin objects.
1548 template<int size
, bool big_endian
>
1550 Sized_pluginobj
<size
, big_endian
>::do_section_name(unsigned int) const
1553 return std::string();
1556 // Return a view of the contents of a section. Not used for plugin objects.
1558 template<int size
, bool big_endian
>
1559 const unsigned char*
1560 Sized_pluginobj
<size
, big_endian
>::do_section_contents(
1569 // Return section flags. Not used for plugin objects.
1571 template<int size
, bool big_endian
>
1573 Sized_pluginobj
<size
, big_endian
>::do_section_flags(unsigned int)
1579 // Return section entsize. Not used for plugin objects.
1581 template<int size
, bool big_endian
>
1583 Sized_pluginobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1589 // Return section address. Not used for plugin objects.
1591 template<int size
, bool big_endian
>
1593 Sized_pluginobj
<size
, big_endian
>::do_section_address(unsigned int)
1599 // Return section type. Not used for plugin objects.
1601 template<int size
, bool big_endian
>
1603 Sized_pluginobj
<size
, big_endian
>::do_section_type(unsigned int)
1609 // Return the section link field. Not used for plugin objects.
1611 template<int size
, bool big_endian
>
1613 Sized_pluginobj
<size
, big_endian
>::do_section_link(unsigned int)
1619 // Return the section link field. Not used for plugin objects.
1621 template<int size
, bool big_endian
>
1623 Sized_pluginobj
<size
, big_endian
>::do_section_info(unsigned int)
1629 // Return the section alignment. Not used for plugin objects.
1631 template<int size
, bool big_endian
>
1633 Sized_pluginobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1639 // Return the Xindex structure to use. Not used for plugin objects.
1641 template<int size
, bool big_endian
>
1643 Sized_pluginobj
<size
, big_endian
>::do_initialize_xindex()
1649 // Get symbol counts. Don't count plugin objects; the replacement
1650 // files will provide the counts.
1652 template<int size
, bool big_endian
>
1654 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbol_counts(
1655 const Symbol_table
*,
1663 // Get symbols. Not used for plugin objects.
1665 template<int size
, bool big_endian
>
1666 const Object::Symbols
*
1667 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbols() const
1672 // Class Plugin_finish. This task runs after all replacement files have
1673 // been added. For now, it's a placeholder for a possible plugin API
1674 // to allow the plugin to release most of its resources. The cleanup
1675 // handlers must be called later, because they can remove the temporary
1676 // object files that are needed until the end of the link.
1678 class Plugin_finish
: public Task
1681 Plugin_finish(Task_token
* this_blocker
, Task_token
* next_blocker
)
1682 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
1687 if (this->this_blocker_
!= NULL
)
1688 delete this->this_blocker_
;
1694 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1695 return this->this_blocker_
;
1700 locks(Task_locker
* tl
)
1701 { tl
->add(this, this->next_blocker_
); }
1706 Plugin_manager
* plugins
= parameters
->options().plugins();
1707 gold_assert(plugins
!= NULL
);
1708 // We could call early cleanup handlers here.
1709 if (plugins
->recorder())
1710 plugins
->recorder()->finish();
1715 { return "Plugin_finish"; }
1718 Task_token
* this_blocker_
;
1719 Task_token
* next_blocker_
;
1722 // Class Plugin_hook.
1724 Plugin_hook::~Plugin_hook()
1728 // Return whether a Plugin_hook task is runnable.
1731 Plugin_hook::is_runnable()
1733 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1734 return this->this_blocker_
;
1738 // Return a Task_locker for a Plugin_hook task. We don't need any
1742 Plugin_hook::locks(Task_locker
*)
1746 // Run the "all symbols read" plugin hook.
1749 Plugin_hook::run(Workqueue
* workqueue
)
1751 gold_assert(this->options_
.has_plugins());
1752 Symbol
* start_sym
= this->symtab_
->lookup(parameters
->entry());
1753 if (start_sym
!= NULL
)
1754 start_sym
->set_in_real_elf();
1756 this->options_
.plugins()->all_symbols_read(workqueue
,
1758 this->input_objects_
,
1762 &this->this_blocker_
);
1763 workqueue
->queue_soon(new Plugin_finish(this->this_blocker_
,
1764 this->next_blocker_
));
1767 // The C interface routines called by the plugins.
1769 #ifdef ENABLE_PLUGINS
1771 // Register a claim-file handler.
1773 static enum ld_plugin_status
1774 register_claim_file(ld_plugin_claim_file_handler handler
)
1776 gold_assert(parameters
->options().has_plugins());
1777 parameters
->options().plugins()->set_claim_file_handler(handler
);
1781 // Register an all-symbols-read handler.
1783 static enum ld_plugin_status
1784 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
)
1786 gold_assert(parameters
->options().has_plugins());
1787 parameters
->options().plugins()->set_all_symbols_read_handler(handler
);
1791 // Register a cleanup handler.
1793 static enum ld_plugin_status
1794 register_cleanup(ld_plugin_cleanup_handler handler
)
1796 gold_assert(parameters
->options().has_plugins());
1797 parameters
->options().plugins()->set_cleanup_handler(handler
);
1801 // Add symbols from a plugin-claimed input file.
1803 static enum ld_plugin_status
1804 add_symbols(void* handle
, int nsyms
, const ld_plugin_symbol
* syms
)
1806 gold_assert(parameters
->options().has_plugins());
1807 Pluginobj
* obj
= parameters
->options().plugins()->make_plugin_object(
1808 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1811 obj
->store_incoming_symbols(nsyms
, syms
);
1815 // Get the input file information with an open (possibly re-opened)
1818 static enum ld_plugin_status
1819 get_input_file(const void* handle
, struct ld_plugin_input_file
* file
)
1821 gold_assert(parameters
->options().has_plugins());
1822 unsigned int obj_index
=
1823 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1824 return parameters
->options().plugins()->get_input_file(obj_index
, file
);
1827 // Release the input file.
1829 static enum ld_plugin_status
1830 release_input_file(const void* handle
)
1832 gold_assert(parameters
->options().has_plugins());
1833 unsigned int obj_index
=
1834 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1835 return parameters
->options().plugins()->release_input_file(obj_index
);
1838 static enum ld_plugin_status
1839 get_view(const void *handle
, const void **viewp
)
1841 gold_assert(parameters
->options().has_plugins());
1842 unsigned int obj_index
=
1843 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1844 return parameters
->options().plugins()->get_view(obj_index
, viewp
);
1847 // Get the symbol resolution info for a plugin-claimed input file.
1849 static enum ld_plugin_status
1850 get_symbols(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1852 gold_assert(parameters
->options().has_plugins());
1853 Plugin_manager
* plugins
= parameters
->options().plugins();
1854 Object
* obj
= plugins
->object(
1855 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1858 Pluginobj
* plugin_obj
= obj
->pluginobj();
1859 if (plugin_obj
== NULL
)
1861 Symbol_table
* symtab
= plugins
->symtab();
1862 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 1);
1865 // Version 2 of the above. The only difference is that this version
1866 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1868 static enum ld_plugin_status
1869 get_symbols_v2(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1871 gold_assert(parameters
->options().has_plugins());
1872 Plugin_manager
* plugins
= parameters
->options().plugins();
1873 Object
* obj
= plugins
->object(
1874 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1877 Pluginobj
* plugin_obj
= obj
->pluginobj();
1878 if (plugin_obj
== NULL
)
1880 Symbol_table
* symtab
= plugins
->symtab();
1881 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 2);
1884 // Version 3 of the above. The only difference from v2 is that it
1885 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1886 // decided to include.
1888 static enum ld_plugin_status
1889 get_symbols_v3(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1891 gold_assert(parameters
->options().has_plugins());
1892 Plugin_manager
* plugins
= parameters
->options().plugins();
1893 Object
* obj
= plugins
->object(
1894 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1897 Pluginobj
* plugin_obj
= obj
->pluginobj();
1898 if (plugin_obj
== NULL
)
1900 Symbol_table
* symtab
= plugins
->symtab();
1901 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 3);
1904 // Add a new (real) input file generated by a plugin.
1906 static enum ld_plugin_status
1907 add_input_file(const char* pathname
)
1909 gold_assert(parameters
->options().has_plugins());
1910 return parameters
->options().plugins()->add_input_file(pathname
, false);
1913 // Add a new (real) library required by a plugin.
1915 static enum ld_plugin_status
1916 add_input_library(const char* pathname
)
1918 gold_assert(parameters
->options().has_plugins());
1919 return parameters
->options().plugins()->add_input_file(pathname
, true);
1922 // Set the extra library path to be used by libraries added via
1923 // add_input_library
1925 static enum ld_plugin_status
1926 set_extra_library_path(const char* path
)
1928 gold_assert(parameters
->options().has_plugins());
1929 return parameters
->options().plugins()->set_extra_library_path(path
);
1932 // Issue a diagnostic message from a plugin.
1934 static enum ld_plugin_status
1935 message(int level
, const char* format
, ...)
1938 va_start(args
, format
);
1943 parameters
->errors()->info(format
, args
);
1946 parameters
->errors()->warning(format
, args
);
1950 parameters
->errors()->error(format
, args
);
1953 parameters
->errors()->fatal(format
, args
);
1961 // Get the section count of the object corresponding to the handle. This
1962 // plugin interface can only be called in the claim_file handler of the plugin.
1964 static enum ld_plugin_status
1965 get_input_section_count(const void* handle
, unsigned int* count
)
1967 gold_assert(parameters
->options().has_plugins());
1969 if (!parameters
->options().plugins()->in_claim_file_handler())
1972 Object
* obj
= parameters
->options().plugins()->get_elf_object(handle
);
1977 *count
= obj
->shnum();
1981 // Get the type of the specified section in the object corresponding
1982 // to the handle. This plugin interface can only be called in the
1983 // claim_file handler of the plugin.
1985 static enum ld_plugin_status
1986 get_input_section_type(const struct ld_plugin_section section
,
1989 gold_assert(parameters
->options().has_plugins());
1991 if (!parameters
->options().plugins()->in_claim_file_handler())
1995 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1998 return LDPS_BAD_HANDLE
;
2000 *type
= obj
->section_type(section
.shndx
);
2004 // Get the name of the specified section in the object corresponding
2005 // to the handle. This plugin interface can only be called in the
2006 // claim_file handler of the plugin.
2008 static enum ld_plugin_status
2009 get_input_section_name(const struct ld_plugin_section section
,
2010 char** section_name_ptr
)
2012 gold_assert(parameters
->options().has_plugins());
2014 if (!parameters
->options().plugins()->in_claim_file_handler())
2018 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2021 return LDPS_BAD_HANDLE
;
2023 // Check if the object is locked before getting the section name.
2024 gold_assert(obj
->is_locked());
2026 const std::string section_name
= obj
->section_name(section
.shndx
);
2027 *section_name_ptr
= static_cast<char*>(malloc(section_name
.length() + 1));
2028 memcpy(*section_name_ptr
, section_name
.c_str(), section_name
.length() + 1);
2032 // Get the contents of the specified section in the object corresponding
2033 // to the handle. This plugin interface can only be called in the
2034 // claim_file handler of the plugin.
2036 static enum ld_plugin_status
2037 get_input_section_contents(const struct ld_plugin_section section
,
2038 const unsigned char** section_contents_ptr
,
2041 gold_assert(parameters
->options().has_plugins());
2043 if (!parameters
->options().plugins()->in_claim_file_handler())
2047 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2050 return LDPS_BAD_HANDLE
;
2052 // Check if the object is locked before getting the section contents.
2053 gold_assert(obj
->is_locked());
2055 section_size_type plen
;
2056 *section_contents_ptr
2057 = obj
->section_contents(section
.shndx
, &plen
, false);
2062 // Get the alignment of the specified section in the object corresponding
2063 // to the handle. This plugin interface can only be called in the
2064 // claim_file handler of the plugin.
2066 static enum ld_plugin_status
2067 get_input_section_alignment(const struct ld_plugin_section section
,
2068 unsigned int* addralign
)
2070 gold_assert(parameters
->options().has_plugins());
2072 if (!parameters
->options().plugins()->in_claim_file_handler())
2076 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2079 return LDPS_BAD_HANDLE
;
2081 *addralign
= obj
->section_addralign(section
.shndx
);
2085 // Get the size of the specified section in the object corresponding
2086 // to the handle. This plugin interface can only be called in the
2087 // claim_file handler of the plugin.
2089 static enum ld_plugin_status
2090 get_input_section_size(const struct ld_plugin_section section
,
2093 gold_assert(parameters
->options().has_plugins());
2095 if (!parameters
->options().plugins()->in_claim_file_handler())
2099 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2102 return LDPS_BAD_HANDLE
;
2104 *secsize
= obj
->section_size(section
.shndx
);
2108 static enum ld_plugin_status
2109 get_wrap_symbols(uint64_t *count
, const char ***wrap_symbols
)
2111 gold_assert(parameters
->options().has_plugins());
2112 *count
= parameters
->options().wrap_size();
2117 *wrap_symbols
= new const char *[*count
];
2119 for (options::String_set::const_iterator
2120 it
= parameters
->options().wrap_begin();
2121 it
!= parameters
->options().wrap_end(); ++it
, ++i
) {
2122 (*wrap_symbols
)[i
] = it
->c_str();
2128 // Specify the ordering of sections in the final layout. The sections are
2129 // specified as (handle,shndx) pairs in the two arrays in the order in
2130 // which they should appear in the final layout.
2132 static enum ld_plugin_status
2133 update_section_order(const struct ld_plugin_section
* section_list
,
2134 unsigned int num_sections
)
2136 gold_assert(parameters
->options().has_plugins());
2138 if (num_sections
== 0)
2141 if (section_list
== NULL
)
2144 Layout
* layout
= parameters
->options().plugins()->layout();
2145 gold_assert (layout
!= NULL
);
2147 std::map
<Section_id
, unsigned int>* order_map
2148 = layout
->get_section_order_map();
2150 /* Store the mapping from Section_id to section position in layout's
2151 order_map to consult after output sections are added. */
2152 for (unsigned int i
= 0; i
< num_sections
; ++i
)
2154 Object
* obj
= parameters
->options().plugins()->get_elf_object(
2155 section_list
[i
].handle
);
2156 if (obj
== NULL
|| obj
->is_dynamic())
2157 return LDPS_BAD_HANDLE
;
2158 unsigned int shndx
= section_list
[i
].shndx
;
2159 Section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
2160 (*order_map
)[secn_id
] = i
+ 1;
2166 // Let the linker know that the sections could be reordered.
2168 static enum ld_plugin_status
2169 allow_section_ordering()
2171 gold_assert(parameters
->options().has_plugins());
2172 Layout
* layout
= parameters
->options().plugins()->layout();
2173 layout
->set_section_ordering_specified();
2177 // Let the linker know that a subset of sections could be mapped
2178 // to a unique segment.
2180 static enum ld_plugin_status
2181 allow_unique_segment_for_sections()
2183 gold_assert(parameters
->options().has_plugins());
2184 Layout
* layout
= parameters
->options().plugins()->layout();
2185 layout
->set_unique_segment_for_sections_specified();
2189 // This function should map the list of sections specified in the
2190 // SECTION_LIST to a unique segment. ELF segments do not have names
2191 // and the NAME is used to identify Output Section which should contain
2192 // the list of sections. This Output Section will then be mapped to
2193 // a unique segment. FLAGS is used to specify if any additional segment
2194 // flags need to be set. For instance, a specific segment flag can be
2195 // set to identify this segment. Unsetting segment flags is not possible.
2196 // ALIGN specifies the alignment of the segment.
2198 static enum ld_plugin_status
2199 unique_segment_for_sections(const char* segment_name
,
2202 const struct ld_plugin_section
* section_list
,
2203 unsigned int num_sections
)
2205 gold_assert(parameters
->options().has_plugins());
2207 if (num_sections
== 0)
2210 if (section_list
== NULL
)
2213 Layout
* layout
= parameters
->options().plugins()->layout();
2214 gold_assert (layout
!= NULL
);
2216 Layout::Unique_segment_info
* s
= new Layout::Unique_segment_info
;
2217 s
->name
= segment_name
;
2221 for (unsigned int i
= 0; i
< num_sections
; ++i
)
2223 Object
* obj
= parameters
->options().plugins()->get_elf_object(
2224 section_list
[i
].handle
);
2225 if (obj
== NULL
|| obj
->is_dynamic())
2226 return LDPS_BAD_HANDLE
;
2227 unsigned int shndx
= section_list
[i
].shndx
;
2228 Const_section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
2229 layout
->insert_section_segment_map(secn_id
, s
);
2235 // Register a new_input handler.
2237 static enum ld_plugin_status
2238 register_new_input(ld_plugin_new_input_handler handler
)
2240 gold_assert(parameters
->options().has_plugins());
2241 parameters
->options().plugins()->set_new_input_handler(handler
);
2245 #endif // ENABLE_PLUGINS
2247 // Allocate a Pluginobj object of the appropriate size and endianness.
2250 make_sized_plugin_object(const std::string
& filename
,
2251 Input_file
* input_file
, off_t offset
, off_t filesize
)
2253 Pluginobj
* obj
= NULL
;
2255 parameters_force_valid_target();
2256 const Target
& target(parameters
->target());
2258 if (target
.get_size() == 32)
2260 if (target
.is_big_endian())
2261 #ifdef HAVE_TARGET_32_BIG
2262 obj
= new Sized_pluginobj
<32, true>(filename
, input_file
,
2265 gold_error(_("%s: not configured to support "
2266 "32-bit big-endian object"),
2270 #ifdef HAVE_TARGET_32_LITTLE
2271 obj
= new Sized_pluginobj
<32, false>(filename
, input_file
,
2274 gold_error(_("%s: not configured to support "
2275 "32-bit little-endian object"),
2279 else if (target
.get_size() == 64)
2281 if (target
.is_big_endian())
2282 #ifdef HAVE_TARGET_64_BIG
2283 obj
= new Sized_pluginobj
<64, true>(filename
, input_file
,
2286 gold_error(_("%s: not configured to support "
2287 "64-bit big-endian object"),
2291 #ifdef HAVE_TARGET_64_LITTLE
2292 obj
= new Sized_pluginobj
<64, false>(filename
, input_file
,
2295 gold_error(_("%s: not configured to support "
2296 "64-bit little-endian object"),
2301 gold_assert(obj
!= NULL
);
2305 } // End namespace gold.