1 // plugin.cc -- plugin manager for gold -*- C++ -*-
3 // Copyright (C) 2008-2024 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";
512 if (mkdtemp(dir_template
) == NULL
)
515 if (mktemp(dir_template
) == NULL
)
517 #if defined (_WIN32) && !defined (__CYGWIN32__)
518 if (mkdir(dir_template
) != 0)
521 if (mkdir(dir_template
, 0700) != 0)
526 size_t len
= strlen(dir_template
) + 1;
527 char* tempdir
= new char[len
];
528 memcpy(tempdir
, dir_template
, len
);
530 // Create the log file.
531 std::string
logname(tempdir
);
532 logname
.append("/log");
533 FILE* logfile
= ::fopen(logname
.c_str(), "w");
537 this->tempdir_
= tempdir
;
538 this->logfile_
= logfile
;
540 gold_info(_("%s: recording to %s"), program_name
, this->tempdir_
);
546 Plugin_recorder::claimed_file(const std::string
& obj_name
,
549 const std::string
& plugin_name
)
551 fprintf(this->logfile_
, "PLUGIN: %s\n", plugin_name
.c_str());
552 fprintf(this->logfile_
, "CLAIMED: %s", obj_name
.c_str());
554 fprintf(this->logfile_
, " @%ld", static_cast<long>(offset
));
555 fprintf(this->logfile_
, " %ld\n", static_cast<long>(filesize
));
559 Plugin_recorder::unclaimed_file(const std::string
& obj_name
,
563 fprintf(this->logfile_
, "UNCLAIMED: %s", obj_name
.c_str());
565 fprintf(this->logfile_
, " @%ld", static_cast<long>(offset
));
566 fprintf(this->logfile_
, " %ld\n", static_cast<long>(filesize
));
569 // Make a hard link to INNAME from OUTNAME, if possible.
570 // If not, copy the file.
573 link_or_copy_file(const char* inname
, const char* outname
)
575 static char buf
[4096];
578 if (::link(inname
, outname
) == 0)
582 int in
= ::open(inname
, O_RDONLY
);
585 gold_warning(_("%s: can't open (%s)"), inname
, strerror(errno
));
588 int out
= ::open(outname
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
591 gold_warning(_("%s: can't create (%s)"), outname
, strerror(errno
));
596 while ((len
= ::read(in
, buf
, sizeof(buf
))) > 0)
598 if (::write(out
, buf
, len
) != len
)
600 gold_warning(_("%s: write error while making copy of file (%s)"),
601 inname
, strerror(errno
));
611 Plugin_recorder::replacement_file(const char* name
, bool is_lib
)
613 fprintf(this->logfile_
, "REPLACEMENT: %s", name
);
615 fprintf(this->logfile_
, "(lib)");
619 const char* basename
= lbasename(name
);
620 snprintf(counter
, sizeof(counter
), "%05d", this->file_count_
);
622 std::string
outname(this->tempdir_
);
624 outname
.append(counter
);
626 outname
.append(basename
);
627 if (link_or_copy_file(name
, outname
.c_str()))
628 fprintf(this->logfile_
, " -> %s", outname
.c_str());
630 fprintf(this->logfile_
, "\n");
634 Plugin_recorder::record_symbols(const Object
* obj
, int nsyms
,
635 const struct ld_plugin_symbol
* syms
)
637 fprintf(this->logfile_
, "SYMBOLS: %d %s\n", nsyms
, obj
->name().c_str());
638 for (int i
= 0; i
< nsyms
; ++i
)
640 const struct ld_plugin_symbol
* isym
= &syms
[i
];
666 switch (isym
->visibility
)
685 fprintf(this->logfile_
, " %5d: %-2s %c %s", i
, def
, vis
, isym
->name
);
686 if (isym
->version
!= NULL
&& isym
->version
[0] != '\0')
687 fprintf(this->logfile_
, "@%s", isym
->version
);
688 if (isym
->comdat_key
!= NULL
&& isym
->comdat_key
[0] != '\0')
690 if (strcmp(isym
->name
, isym
->comdat_key
) == 0)
691 fprintf(this->logfile_
, " [comdat]");
693 fprintf(this->logfile_
, " [comdat: %s]", isym
->comdat_key
);
695 fprintf(this->logfile_
, "\n");
699 // Plugin_manager methods.
701 Plugin_manager::~Plugin_manager()
703 for (Plugin_list::iterator p
= this->plugins_
.begin();
704 p
!= this->plugins_
.end();
707 this->plugins_
.clear();
708 for (Object_list::iterator obj
= this->objects_
.begin();
709 obj
!= this->objects_
.end();
712 this->objects_
.clear();
714 delete this->recorder_
;
717 // Load all plugin libraries.
720 Plugin_manager::load_plugins(Layout
* layout
)
722 this->layout_
= layout
;
724 if (is_debugging_enabled(DEBUG_PLUGIN
))
726 this->recorder_
= new Plugin_recorder();
727 this->recorder_
->init();
730 for (this->current_
= this->plugins_
.begin();
731 this->current_
!= this->plugins_
.end();
733 (*this->current_
)->load();
736 // Call the plugin claim-file handlers in turn to see if any claim the file.
739 Plugin_manager::claim_file(Input_file
* input_file
, off_t offset
,
740 off_t filesize
, Object
* elf_object
)
742 bool lock_initialized
= this->initialize_lock_
.initialize();
744 gold_assert(lock_initialized
);
745 Hold_lock
hl(*this->lock_
);
747 unsigned int handle
= this->objects_
.size();
748 this->input_file_
= input_file
;
749 this->plugin_input_file_
.name
= input_file
->filename().c_str();
750 this->plugin_input_file_
.fd
= input_file
->file().descriptor();
751 this->plugin_input_file_
.offset
= offset
;
752 this->plugin_input_file_
.filesize
= filesize
;
753 this->plugin_input_file_
.handle
= reinterpret_cast<void*>(handle
);
754 if (elf_object
!= NULL
)
755 this->objects_
.push_back(elf_object
);
756 this->in_claim_file_handler_
= true;
758 for (Plugin_list::iterator p
= this->plugins_
.begin();
759 p
!= this->plugins_
.end();
762 // If we aren't yet in replacement phase, allow plugins to claim input
763 // files, otherwise notify the plugin of the new input file, if needed.
764 if (!this->in_replacement_phase_
)
766 if ((*p
)->claim_file(&this->plugin_input_file_
))
768 this->any_claimed_
= true;
769 this->in_claim_file_handler_
= false;
771 if (this->recorder_
!= NULL
)
773 const std::string
& objname
= (elf_object
== NULL
774 ? input_file
->filename()
775 : elf_object
->name());
776 this->recorder_
->claimed_file(objname
,
781 if (this->objects_
.size() > handle
782 && this->objects_
[handle
]->pluginobj() != NULL
)
783 return this->objects_
[handle
]->pluginobj();
785 // If the plugin claimed the file but did not call the
786 // add_symbols callback, we need to create the Pluginobj now.
787 Pluginobj
* obj
= this->make_plugin_object(handle
);
793 (*p
)->new_input(&this->plugin_input_file_
);
797 this->in_claim_file_handler_
= false;
799 if (this->recorder_
!= NULL
)
800 this->recorder_
->unclaimed_file(input_file
->filename(), offset
, filesize
);
805 // Save an archive. This is used so that a plugin can add a file
806 // which refers to a symbol which was not previously referenced. In
807 // that case we want to pretend that the symbol was referenced before,
808 // and pull in the archive object.
811 Plugin_manager::save_archive(Archive
* archive
)
813 if (this->in_replacement_phase_
|| !this->any_claimed_
)
816 this->rescannable_
.push_back(Rescannable(archive
));
819 // Save an Input_group. This is like save_archive.
822 Plugin_manager::save_input_group(Input_group
* input_group
)
824 if (this->in_replacement_phase_
|| !this->any_claimed_
)
827 this->rescannable_
.push_back(Rescannable(input_group
));
830 // Call the all-symbols-read handlers.
833 Plugin_manager::all_symbols_read(Workqueue
* workqueue
, Task
* task
,
834 Input_objects
* input_objects
,
835 Symbol_table
* symtab
,
836 Dirsearch
* dirpath
, Mapfile
* mapfile
,
837 Task_token
** last_blocker
)
839 this->in_replacement_phase_
= true;
840 this->workqueue_
= workqueue
;
842 this->input_objects_
= input_objects
;
843 this->symtab_
= symtab
;
844 this->dirpath_
= dirpath
;
845 this->mapfile_
= mapfile
;
846 this->this_blocker_
= NULL
;
848 // Set symbols used in defsym expressions as seen in real ELF.
849 Layout
*layout
= parameters
->options().plugins()->layout();
850 layout
->script_options()->set_defsym_uses_in_real_elf(symtab
);
851 layout
->script_options()->find_defsym_defs(this->defsym_defines_set_
);
853 for (Plugin_list::iterator p
= this->plugins_
.begin();
854 p
!= this->plugins_
.end();
856 (*p
)->all_symbols_read();
858 if (this->any_added_
)
860 Task_token
* next_blocker
= new Task_token(true);
861 next_blocker
->add_blocker();
862 workqueue
->queue(new Plugin_rescan(this->this_blocker_
, next_blocker
));
863 this->this_blocker_
= next_blocker
;
866 *last_blocker
= this->this_blocker_
;
869 // This is called when we see a new undefined symbol. If we are in
870 // the replacement phase, this means that we may need to rescan some
871 // archives we have previously seen.
874 Plugin_manager::new_undefined_symbol(Symbol
* sym
)
876 if (this->in_replacement_phase_
)
877 this->undefined_symbols_
.push_back(sym
);
880 // Rescan archives as needed. This handles the case where a new
881 // object file added by a plugin has an undefined reference to some
882 // symbol defined in an archive.
885 Plugin_manager::rescan(Task
* task
)
887 size_t rescan_pos
= 0;
888 size_t rescan_size
= this->rescannable_
.size();
889 while (!this->undefined_symbols_
.empty())
891 if (rescan_pos
>= rescan_size
)
893 this->undefined_symbols_
.clear();
897 Undefined_symbol_list undefs
;
898 undefs
.reserve(this->undefined_symbols_
.size());
899 this->undefined_symbols_
.swap(undefs
);
901 size_t min_rescan_pos
= rescan_size
;
903 for (Undefined_symbol_list::const_iterator p
= undefs
.begin();
907 if (!(*p
)->is_undefined())
910 this->undefined_symbols_
.push_back(*p
);
912 // Find the first rescan archive which defines this symbol,
913 // starting at the current rescan position. The rescan position
914 // exists so that given -la -lb -lc we don't look for undefined
915 // symbols in -lb back in -la, but instead get the definition
916 // from -lc. Don't bother to look past the current minimum
918 for (size_t i
= rescan_pos
; i
< min_rescan_pos
; ++i
)
920 if (this->rescannable_defines(i
, *p
))
928 if (min_rescan_pos
>= rescan_size
)
930 // We didn't find any rescannable archives which define any
931 // undefined symbols.
935 const Rescannable
& r(this->rescannable_
[min_rescan_pos
]);
938 Task_lock_obj
<Archive
> tl(task
, r
.u
.archive
);
939 r
.u
.archive
->add_symbols(this->symtab_
, this->layout_
,
940 this->input_objects_
, this->mapfile_
);
944 size_t next_saw_undefined
= this->symtab_
->saw_undefined();
945 size_t saw_undefined
;
948 saw_undefined
= next_saw_undefined
;
950 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
951 p
!= r
.u
.input_group
->end();
954 Task_lock_obj
<Archive
> tl(task
, *p
);
956 (*p
)->add_symbols(this->symtab_
, this->layout_
,
957 this->input_objects_
, this->mapfile_
);
960 next_saw_undefined
= this->symtab_
->saw_undefined();
962 while (saw_undefined
!= next_saw_undefined
);
965 for (size_t i
= rescan_pos
; i
< min_rescan_pos
+ 1; ++i
)
967 if (this->rescannable_
[i
].is_archive
)
968 delete this->rescannable_
[i
].u
.archive
;
970 delete this->rescannable_
[i
].u
.input_group
;
973 rescan_pos
= min_rescan_pos
+ 1;
977 // Return whether the rescannable at index I defines SYM.
980 Plugin_manager::rescannable_defines(size_t i
, Symbol
* sym
)
982 const Rescannable
& r(this->rescannable_
[i
]);
984 return r
.u
.archive
->defines_symbol(sym
);
987 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
988 p
!= r
.u
.input_group
->end();
991 if ((*p
)->defines_symbol(sym
))
998 // Layout deferred objects.
1001 Plugin_manager::layout_deferred_objects()
1003 Deferred_layout_list::iterator obj
;
1005 for (obj
= this->deferred_layout_objects_
.begin();
1006 obj
!= this->deferred_layout_objects_
.end();
1009 // Lock the object so we can read from it. This is only called
1010 // single-threaded from queue_middle_tasks, so it is OK to lock.
1011 // Unfortunately we have no way to pass in a Task token.
1012 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
1013 Task_lock_obj
<Object
> tl(dummy_task
, *obj
);
1014 (*obj
)->layout_deferred_sections(this->layout_
);
1018 // Call the cleanup handlers.
1021 Plugin_manager::cleanup()
1023 if (this->any_added_
)
1025 // If any input files were added, close all the input files.
1026 // This is because the plugin may want to remove them, and on
1027 // Windows you are not allowed to remove an open file.
1028 close_all_descriptors();
1031 for (Plugin_list::iterator p
= this->plugins_
.begin();
1032 p
!= this->plugins_
.end();
1037 // Make a new Pluginobj object. This is called when the plugin calls
1038 // the add_symbols API.
1041 Plugin_manager::make_plugin_object(unsigned int handle
)
1043 // Make sure we aren't asked to make an object for the same handle twice.
1044 if (this->objects_
.size() != handle
1045 && this->objects_
[handle
]->pluginobj() != NULL
)
1048 const std::string
* filename
= &this->input_file_
->filename();
1050 // If the elf object for this file was pushed into the objects_ vector,
1051 // use its filename, then delete it to make room for the Pluginobj as
1052 // this file is claimed.
1053 if (this->objects_
.size() != handle
)
1055 filename
= &this->objects_
.back()->name();
1056 this->objects_
.pop_back();
1059 Pluginobj
* obj
= make_sized_plugin_object(*filename
,
1061 this->plugin_input_file_
.offset
,
1062 this->plugin_input_file_
.filesize
);
1066 this->objects_
.push_back(obj
);
1070 // Get the input file information with an open (possibly re-opened)
1074 Plugin_manager::get_input_file(unsigned int handle
,
1075 struct ld_plugin_input_file
* file
)
1077 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1079 return LDPS_BAD_HANDLE
;
1081 obj
->lock(this->task_
);
1082 file
->name
= obj
->filename().c_str();
1083 file
->fd
= obj
->descriptor();
1084 file
->offset
= obj
->offset();
1085 file
->filesize
= obj
->filesize();
1086 file
->handle
= reinterpret_cast<void*>(handle
);
1090 // Release the input file.
1093 Plugin_manager::release_input_file(unsigned int handle
)
1095 if (this->object(handle
) == NULL
)
1096 return LDPS_BAD_HANDLE
;
1098 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1101 return LDPS_BAD_HANDLE
;
1103 obj
->unlock(this->task_
);
1107 // Get the elf object corresponding to the handle. Return NULL if we
1108 // found a Pluginobj instead.
1111 Plugin_manager::get_elf_object(const void* handle
)
1113 Object
* obj
= this->object(
1114 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1116 // The object should not be a Pluginobj.
1118 || obj
->pluginobj() != NULL
)
1125 Plugin_manager::get_view(unsigned int handle
, const void **viewp
)
1129 Input_file
*input_file
;
1130 if (this->in_claim_file_handler_
)
1132 // We are being called from the claim_file hook.
1133 const struct ld_plugin_input_file
&f
= this->plugin_input_file_
;
1135 filesize
= f
.filesize
;
1136 input_file
= this->input_file_
;
1140 // An already claimed file.
1141 if (this->object(handle
) == NULL
)
1142 return LDPS_BAD_HANDLE
;
1143 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1145 return LDPS_BAD_HANDLE
;
1146 offset
= obj
->offset();
1147 filesize
= obj
->filesize();
1148 input_file
= obj
->input_file();
1150 *viewp
= (void*) input_file
->file().get_view(offset
, 0, filesize
, false,
1155 // Add a new library path.
1158 Plugin_manager::set_extra_library_path(const char* path
)
1160 this->extra_search_path_
= std::string(path
);
1164 // Add a new input file.
1167 Plugin_manager::add_input_file(const char* pathname
, bool is_lib
)
1169 Input_file_argument
file(pathname
,
1171 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
1172 : Input_file_argument::INPUT_FILE_TYPE_FILE
),
1174 ? this->extra_search_path_
.c_str()
1178 Input_argument
* input_argument
= new Input_argument(file
);
1179 Task_token
* next_blocker
= new Task_token(true);
1180 next_blocker
->add_blocker();
1181 if (parameters
->incremental())
1182 gold_error(_("input files added by plug-ins in --incremental mode not "
1185 if (this->recorder_
!= NULL
)
1186 this->recorder_
->replacement_file(pathname
, is_lib
);
1188 this->workqueue_
->queue_soon(new Read_symbols(this->input_objects_
,
1197 this->this_blocker_
,
1199 this->this_blocker_
= next_blocker
;
1200 this->any_added_
= true;
1206 Pluginobj::Pluginobj(const std::string
& name
, Input_file
* input_file
,
1207 off_t offset
, off_t filesize
)
1208 : Object(name
, input_file
, false, offset
),
1209 nsyms_(0), syms_(NULL
), symbols_(), filesize_(filesize
), comdat_map_()
1213 // Return TRUE if a defined symbol is referenced from outside the
1214 // universe of claimed objects. Only references from relocatable,
1215 // non-IR (unclaimed) objects count as a reference. References from
1216 // dynamic objects count only as "visible".
1219 is_referenced_from_outside(Symbol
* lsym
)
1221 if (lsym
->in_real_elf())
1223 if (parameters
->options().relocatable())
1225 if (parameters
->options().is_undefined(lsym
->name()))
1230 // Return TRUE if a defined symbol might be reachable from outside the
1234 is_visible_from_outside(Symbol
* lsym
)
1238 if (parameters
->options().export_dynamic() || parameters
->options().shared()
1239 || parameters
->options().in_dynamic_list(lsym
->name())
1240 || parameters
->options().is_export_dynamic_symbol(lsym
->name()))
1241 return lsym
->is_externally_visible();
1245 // Get symbol resolution info.
1248 Pluginobj::get_symbol_resolution_info(Symbol_table
* symtab
,
1250 ld_plugin_symbol
* syms
,
1253 // For version 1 of this interface, we cannot use
1254 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
1256 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
1258 ? LDPR_PREVAILING_DEF_IRONLY_EXP
1259 : LDPR_PREVAILING_DEF
);
1261 if (nsyms
> this->nsyms_
)
1262 return LDPS_NO_SYMS
;
1264 if (static_cast<size_t>(nsyms
) > this->symbols_
.size())
1266 // We never decided to include this object. We mark all symbols as
1268 gold_assert(this->symbols_
.size() == 0);
1269 for (int i
= 0; i
< nsyms
; i
++)
1270 syms
[i
].resolution
= LDPR_PREEMPTED_REG
;
1271 return version
> 2 ? LDPS_NO_SYMS
: LDPS_OK
;
1274 Plugin_manager
* plugins
= parameters
->options().plugins();
1275 for (int i
= 0; i
< nsyms
; i
++)
1277 ld_plugin_symbol
* isym
= &syms
[i
];
1278 Symbol
* lsym
= this->symbols_
[i
];
1279 if (lsym
->is_forwarder())
1280 lsym
= symtab
->resolve_forwards(lsym
);
1281 ld_plugin_symbol_resolution res
= LDPR_UNKNOWN
;
1283 if (plugins
->is_defsym_def(lsym
->name()))
1285 // The symbol is redefined via defsym.
1286 res
= LDPR_PREEMPTED_REG
;
1288 else if (lsym
->is_undefined())
1290 // The symbol remains undefined.
1293 else if (isym
->def
== LDPK_UNDEF
1294 || isym
->def
== LDPK_WEAKUNDEF
1295 || isym
->def
== LDPK_COMMON
)
1297 // The original symbol was undefined or common.
1298 if (lsym
->source() != Symbol::FROM_OBJECT
)
1299 res
= LDPR_RESOLVED_EXEC
;
1300 else if (lsym
->object()->pluginobj() == this)
1302 if (is_referenced_from_outside(lsym
))
1303 res
= LDPR_PREVAILING_DEF
;
1304 else if (is_visible_from_outside(lsym
))
1305 res
= ldpr_prevailing_def_ironly_exp
;
1307 res
= LDPR_PREVAILING_DEF_IRONLY
;
1309 else if (lsym
->object()->pluginobj() != NULL
)
1310 res
= LDPR_RESOLVED_IR
;
1311 else if (lsym
->object()->is_dynamic())
1312 res
= LDPR_RESOLVED_DYN
;
1314 res
= LDPR_RESOLVED_EXEC
;
1318 // The original symbol was a definition.
1319 if (lsym
->source() != Symbol::FROM_OBJECT
)
1320 res
= LDPR_PREEMPTED_REG
;
1321 else if (lsym
->object() == static_cast<const Object
*>(this))
1323 if (is_referenced_from_outside(lsym
))
1324 res
= LDPR_PREVAILING_DEF
;
1325 else if (is_visible_from_outside(lsym
))
1326 res
= ldpr_prevailing_def_ironly_exp
;
1328 res
= LDPR_PREVAILING_DEF_IRONLY
;
1331 res
= (lsym
->object()->pluginobj() != NULL
1333 : LDPR_PREEMPTED_REG
);
1335 isym
->resolution
= res
;
1340 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1344 Pluginobj::include_comdat_group(std::string comdat_key
, Layout
* layout
)
1346 std::pair
<Comdat_map::iterator
, bool> ins
=
1347 this->comdat_map_
.insert(std::make_pair(comdat_key
, false));
1349 // If this is the first time we've seen this comdat key, ask the
1350 // layout object whether it should be included.
1352 ins
.first
->second
= layout
->find_or_add_kept_section(comdat_key
,
1356 return ins
.first
->second
;
1359 // Class Sized_pluginobj.
1361 template<int size
, bool big_endian
>
1362 Sized_pluginobj
<size
, big_endian
>::Sized_pluginobj(
1363 const std::string
& name
,
1364 Input_file
* input_file
,
1367 : Pluginobj(name
, input_file
, offset
, filesize
)
1371 // Read the symbols. Not used for plugin objects.
1373 template<int size
, bool big_endian
>
1375 Sized_pluginobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1380 // Lay out the input sections. Not used for plugin objects.
1382 template<int size
, bool big_endian
>
1384 Sized_pluginobj
<size
, big_endian
>::do_layout(Symbol_table
*, Layout
*,
1390 // Add the symbols to the symbol table.
1392 template<int size
, bool big_endian
>
1394 Sized_pluginobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1398 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1399 unsigned char symbuf
[sym_size
];
1400 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
1402 Plugin_recorder
* recorder
= parameters
->options().plugins()->recorder();
1403 if (recorder
!= NULL
)
1404 recorder
->record_symbols(this, this->nsyms_
, this->syms_
);
1406 this->symbols_
.resize(this->nsyms_
);
1408 for (int i
= 0; i
< this->nsyms_
; ++i
)
1410 const struct ld_plugin_symbol
* isym
= &this->syms_
[i
];
1411 const char* name
= isym
->name
;
1412 const char* ver
= isym
->version
;
1413 elfcpp::Elf_Half shndx
;
1417 if (name
!= NULL
&& name
[0] == '\0')
1419 if (ver
!= NULL
&& ver
[0] == '\0')
1425 case LDPK_WEAKUNDEF
:
1426 bind
= elfcpp::STB_WEAK
;
1432 bind
= elfcpp::STB_GLOBAL
;
1440 // We use an arbitrary section number for a defined symbol.
1444 shndx
= elfcpp::SHN_COMMON
;
1447 case LDPK_WEAKUNDEF
:
1449 shndx
= elfcpp::SHN_UNDEF
;
1453 switch (isym
->visibility
)
1455 case LDPV_PROTECTED
:
1456 vis
= elfcpp::STV_PROTECTED
;
1459 vis
= elfcpp::STV_INTERNAL
;
1462 vis
= elfcpp::STV_HIDDEN
;
1466 vis
= elfcpp::STV_DEFAULT
;
1470 if (isym
->comdat_key
!= NULL
1471 && isym
->comdat_key
[0] != '\0'
1472 && !this->include_comdat_group(isym
->comdat_key
, layout
))
1473 shndx
= elfcpp::SHN_UNDEF
;
1475 osym
.put_st_name(0);
1476 osym
.put_st_value(0);
1477 osym
.put_st_size(0);
1478 osym
.put_st_info(bind
, elfcpp::STT_NOTYPE
);
1479 osym
.put_st_other(vis
, 0);
1480 osym
.put_st_shndx(shndx
);
1482 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
1484 symtab
->add_from_pluginobj
<size
, big_endian
>(this, name
, ver
, &sym
);
1488 template<int size
, bool big_endian
>
1489 Archive::Should_include
1490 Sized_pluginobj
<size
, big_endian
>::do_should_include_member(
1491 Symbol_table
* symtab
,
1496 char* tmpbuf
= NULL
;
1497 size_t tmpbuflen
= 0;
1499 for (int i
= 0; i
< this->nsyms_
; ++i
)
1501 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1502 if (sym
.def
== LDPK_UNDEF
|| sym
.def
== LDPK_WEAKUNDEF
)
1504 const char* name
= sym
.name
;
1506 Archive::Should_include t
= Archive::should_include_member(symtab
,
1512 if (t
== Archive::SHOULD_INCLUDE_YES
)
1521 return Archive::SHOULD_INCLUDE_UNKNOWN
;
1524 // Iterate over global symbols, calling a visitor class V for each.
1526 template<int size
, bool big_endian
>
1528 Sized_pluginobj
<size
, big_endian
>::do_for_all_global_symbols(
1530 Library_base::Symbol_visitor_base
* v
)
1532 for (int i
= 0; i
< this->nsyms_
; ++i
)
1534 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1535 if (sym
.def
!= LDPK_UNDEF
)
1540 // Iterate over local symbols, calling a visitor class V for each GOT offset
1541 // associated with a local symbol.
1542 template<int size
, bool big_endian
>
1544 Sized_pluginobj
<size
, big_endian
>::do_for_all_local_got_entries(
1545 Got_offset_list::Visitor
*) const
1550 // Get the size of a section. Not used for plugin objects.
1552 template<int size
, bool big_endian
>
1554 Sized_pluginobj
<size
, big_endian
>::do_section_size(unsigned int)
1560 // Get the name of a section. Not used for plugin objects.
1562 template<int size
, bool big_endian
>
1564 Sized_pluginobj
<size
, big_endian
>::do_section_name(unsigned int) const
1567 return std::string();
1570 // Return a view of the contents of a section. Not used for plugin objects.
1572 template<int size
, bool big_endian
>
1573 const unsigned char*
1574 Sized_pluginobj
<size
, big_endian
>::do_section_contents(
1583 // Return section flags. Not used for plugin objects.
1585 template<int size
, bool big_endian
>
1587 Sized_pluginobj
<size
, big_endian
>::do_section_flags(unsigned int)
1593 // Return section entsize. Not used for plugin objects.
1595 template<int size
, bool big_endian
>
1597 Sized_pluginobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1603 // Return section address. Not used for plugin objects.
1605 template<int size
, bool big_endian
>
1607 Sized_pluginobj
<size
, big_endian
>::do_section_address(unsigned int)
1613 // Return section type. Not used for plugin objects.
1615 template<int size
, bool big_endian
>
1617 Sized_pluginobj
<size
, big_endian
>::do_section_type(unsigned int)
1623 // Return the section link field. Not used for plugin objects.
1625 template<int size
, bool big_endian
>
1627 Sized_pluginobj
<size
, big_endian
>::do_section_link(unsigned int)
1633 // Return the section link field. Not used for plugin objects.
1635 template<int size
, bool big_endian
>
1637 Sized_pluginobj
<size
, big_endian
>::do_section_info(unsigned int)
1643 // Return the section alignment. Not used for plugin objects.
1645 template<int size
, bool big_endian
>
1647 Sized_pluginobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1653 // Return the Xindex structure to use. Not used for plugin objects.
1655 template<int size
, bool big_endian
>
1657 Sized_pluginobj
<size
, big_endian
>::do_initialize_xindex()
1663 // Get symbol counts. Don't count plugin objects; the replacement
1664 // files will provide the counts.
1666 template<int size
, bool big_endian
>
1668 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbol_counts(
1669 const Symbol_table
*,
1677 // Get symbols. Not used for plugin objects.
1679 template<int size
, bool big_endian
>
1680 const Object::Symbols
*
1681 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbols() const
1686 // Class Plugin_finish. This task runs after all replacement files have
1687 // been added. For now, it's a placeholder for a possible plugin API
1688 // to allow the plugin to release most of its resources. The cleanup
1689 // handlers must be called later, because they can remove the temporary
1690 // object files that are needed until the end of the link.
1692 class Plugin_finish
: public Task
1695 Plugin_finish(Task_token
* this_blocker
, Task_token
* next_blocker
)
1696 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
1701 if (this->this_blocker_
!= NULL
)
1702 delete this->this_blocker_
;
1708 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1709 return this->this_blocker_
;
1714 locks(Task_locker
* tl
)
1715 { tl
->add(this, this->next_blocker_
); }
1720 Plugin_manager
* plugins
= parameters
->options().plugins();
1721 gold_assert(plugins
!= NULL
);
1722 // We could call early cleanup handlers here.
1723 if (plugins
->recorder())
1724 plugins
->recorder()->finish();
1729 { return "Plugin_finish"; }
1732 Task_token
* this_blocker_
;
1733 Task_token
* next_blocker_
;
1736 // Class Plugin_hook.
1738 Plugin_hook::~Plugin_hook()
1742 // Return whether a Plugin_hook task is runnable.
1745 Plugin_hook::is_runnable()
1747 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1748 return this->this_blocker_
;
1752 // Return a Task_locker for a Plugin_hook task. We don't need any
1756 Plugin_hook::locks(Task_locker
*)
1760 // Run the "all symbols read" plugin hook.
1763 Plugin_hook::run(Workqueue
* workqueue
)
1765 gold_assert(this->options_
.has_plugins());
1766 Symbol
* start_sym
= this->symtab_
->lookup(parameters
->entry());
1767 if (start_sym
!= NULL
)
1768 start_sym
->set_in_real_elf();
1770 this->options_
.plugins()->all_symbols_read(workqueue
,
1772 this->input_objects_
,
1776 &this->this_blocker_
);
1777 workqueue
->queue_soon(new Plugin_finish(this->this_blocker_
,
1778 this->next_blocker_
));
1781 // The C interface routines called by the plugins.
1783 #ifdef ENABLE_PLUGINS
1785 // Register a claim-file handler.
1787 static enum ld_plugin_status
1788 register_claim_file(ld_plugin_claim_file_handler handler
)
1790 gold_assert(parameters
->options().has_plugins());
1791 parameters
->options().plugins()->set_claim_file_handler(handler
);
1795 // Register an all-symbols-read handler.
1797 static enum ld_plugin_status
1798 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
)
1800 gold_assert(parameters
->options().has_plugins());
1801 parameters
->options().plugins()->set_all_symbols_read_handler(handler
);
1805 // Register a cleanup handler.
1807 static enum ld_plugin_status
1808 register_cleanup(ld_plugin_cleanup_handler handler
)
1810 gold_assert(parameters
->options().has_plugins());
1811 parameters
->options().plugins()->set_cleanup_handler(handler
);
1815 // Add symbols from a plugin-claimed input file.
1817 static enum ld_plugin_status
1818 add_symbols(void* handle
, int nsyms
, const ld_plugin_symbol
* syms
)
1820 gold_assert(parameters
->options().has_plugins());
1821 Pluginobj
* obj
= parameters
->options().plugins()->make_plugin_object(
1822 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1825 obj
->store_incoming_symbols(nsyms
, syms
);
1829 // Get the input file information with an open (possibly re-opened)
1832 static enum ld_plugin_status
1833 get_input_file(const void* handle
, struct ld_plugin_input_file
* file
)
1835 gold_assert(parameters
->options().has_plugins());
1836 unsigned int obj_index
=
1837 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1838 return parameters
->options().plugins()->get_input_file(obj_index
, file
);
1841 // Release the input file.
1843 static enum ld_plugin_status
1844 release_input_file(const void* handle
)
1846 gold_assert(parameters
->options().has_plugins());
1847 unsigned int obj_index
=
1848 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1849 return parameters
->options().plugins()->release_input_file(obj_index
);
1852 static enum ld_plugin_status
1853 get_view(const void *handle
, const void **viewp
)
1855 gold_assert(parameters
->options().has_plugins());
1856 unsigned int obj_index
=
1857 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1858 return parameters
->options().plugins()->get_view(obj_index
, viewp
);
1861 // Get the symbol resolution info for a plugin-claimed input file.
1863 static enum ld_plugin_status
1864 get_symbols(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1866 gold_assert(parameters
->options().has_plugins());
1867 Plugin_manager
* plugins
= parameters
->options().plugins();
1868 Object
* obj
= plugins
->object(
1869 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1872 Pluginobj
* plugin_obj
= obj
->pluginobj();
1873 if (plugin_obj
== NULL
)
1875 Symbol_table
* symtab
= plugins
->symtab();
1876 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 1);
1879 // Version 2 of the above. The only difference is that this version
1880 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1882 static enum ld_plugin_status
1883 get_symbols_v2(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1885 gold_assert(parameters
->options().has_plugins());
1886 Plugin_manager
* plugins
= parameters
->options().plugins();
1887 Object
* obj
= plugins
->object(
1888 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1891 Pluginobj
* plugin_obj
= obj
->pluginobj();
1892 if (plugin_obj
== NULL
)
1894 Symbol_table
* symtab
= plugins
->symtab();
1895 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 2);
1898 // Version 3 of the above. The only difference from v2 is that it
1899 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1900 // decided to include.
1902 static enum ld_plugin_status
1903 get_symbols_v3(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1905 gold_assert(parameters
->options().has_plugins());
1906 Plugin_manager
* plugins
= parameters
->options().plugins();
1907 Object
* obj
= plugins
->object(
1908 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1911 Pluginobj
* plugin_obj
= obj
->pluginobj();
1912 if (plugin_obj
== NULL
)
1914 Symbol_table
* symtab
= plugins
->symtab();
1915 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 3);
1918 // Add a new (real) input file generated by a plugin.
1920 static enum ld_plugin_status
1921 add_input_file(const char* pathname
)
1923 gold_assert(parameters
->options().has_plugins());
1924 return parameters
->options().plugins()->add_input_file(pathname
, false);
1927 // Add a new (real) library required by a plugin.
1929 static enum ld_plugin_status
1930 add_input_library(const char* pathname
)
1932 gold_assert(parameters
->options().has_plugins());
1933 return parameters
->options().plugins()->add_input_file(pathname
, true);
1936 // Set the extra library path to be used by libraries added via
1937 // add_input_library
1939 static enum ld_plugin_status
1940 set_extra_library_path(const char* path
)
1942 gold_assert(parameters
->options().has_plugins());
1943 return parameters
->options().plugins()->set_extra_library_path(path
);
1946 // Issue a diagnostic message from a plugin.
1948 static enum ld_plugin_status
1949 message(int level
, const char* format
, ...)
1952 va_start(args
, format
);
1957 parameters
->errors()->info(format
, args
);
1960 parameters
->errors()->warning(format
, args
);
1964 parameters
->errors()->error(format
, args
);
1967 parameters
->errors()->fatal(format
, args
);
1975 // Get the section count of the object corresponding to the handle. This
1976 // plugin interface can only be called in the claim_file handler of the plugin.
1978 static enum ld_plugin_status
1979 get_input_section_count(const void* handle
, unsigned int* count
)
1981 gold_assert(parameters
->options().has_plugins());
1983 if (!parameters
->options().plugins()->in_claim_file_handler())
1986 Object
* obj
= parameters
->options().plugins()->get_elf_object(handle
);
1991 *count
= obj
->shnum();
1995 // Get the type of the specified section in the object corresponding
1996 // to the handle. This plugin interface can only be called in the
1997 // claim_file handler of the plugin.
1999 static enum ld_plugin_status
2000 get_input_section_type(const struct ld_plugin_section section
,
2003 gold_assert(parameters
->options().has_plugins());
2005 if (!parameters
->options().plugins()->in_claim_file_handler())
2009 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2012 return LDPS_BAD_HANDLE
;
2014 *type
= obj
->section_type(section
.shndx
);
2018 // Get the name of the specified section in the object corresponding
2019 // to the handle. This plugin interface can only be called in the
2020 // claim_file handler of the plugin.
2022 static enum ld_plugin_status
2023 get_input_section_name(const struct ld_plugin_section section
,
2024 char** section_name_ptr
)
2026 gold_assert(parameters
->options().has_plugins());
2028 if (!parameters
->options().plugins()->in_claim_file_handler())
2032 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2035 return LDPS_BAD_HANDLE
;
2037 // Check if the object is locked before getting the section name.
2038 gold_assert(obj
->is_locked());
2040 const std::string section_name
= obj
->section_name(section
.shndx
);
2041 *section_name_ptr
= static_cast<char*>(malloc(section_name
.length() + 1));
2042 memcpy(*section_name_ptr
, section_name
.c_str(), section_name
.length() + 1);
2046 // Get the contents of the specified section in the object corresponding
2047 // to the handle. This plugin interface can only be called in the
2048 // claim_file handler of the plugin.
2050 static enum ld_plugin_status
2051 get_input_section_contents(const struct ld_plugin_section section
,
2052 const unsigned char** section_contents_ptr
,
2055 gold_assert(parameters
->options().has_plugins());
2057 if (!parameters
->options().plugins()->in_claim_file_handler())
2061 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2064 return LDPS_BAD_HANDLE
;
2066 // Check if the object is locked before getting the section contents.
2067 gold_assert(obj
->is_locked());
2069 section_size_type plen
;
2070 *section_contents_ptr
2071 = obj
->section_contents(section
.shndx
, &plen
, false);
2076 // Get the alignment of the specified section in the object corresponding
2077 // to the handle. This plugin interface can only be called in the
2078 // claim_file handler of the plugin.
2080 static enum ld_plugin_status
2081 get_input_section_alignment(const struct ld_plugin_section section
,
2082 unsigned int* addralign
)
2084 gold_assert(parameters
->options().has_plugins());
2086 if (!parameters
->options().plugins()->in_claim_file_handler())
2090 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2093 return LDPS_BAD_HANDLE
;
2095 *addralign
= obj
->section_addralign(section
.shndx
);
2099 // Get the size of the specified section in the object corresponding
2100 // to the handle. This plugin interface can only be called in the
2101 // claim_file handler of the plugin.
2103 static enum ld_plugin_status
2104 get_input_section_size(const struct ld_plugin_section section
,
2107 gold_assert(parameters
->options().has_plugins());
2109 if (!parameters
->options().plugins()->in_claim_file_handler())
2113 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2116 return LDPS_BAD_HANDLE
;
2118 *secsize
= obj
->section_size(section
.shndx
);
2122 static enum ld_plugin_status
2123 get_wrap_symbols(uint64_t *count
, const char ***wrap_symbols
)
2125 gold_assert(parameters
->options().has_plugins());
2126 *count
= parameters
->options().wrap_size();
2131 *wrap_symbols
= new const char *[*count
];
2133 for (options::String_set::const_iterator
2134 it
= parameters
->options().wrap_begin();
2135 it
!= parameters
->options().wrap_end(); ++it
, ++i
) {
2136 (*wrap_symbols
)[i
] = it
->c_str();
2142 // Specify the ordering of sections in the final layout. The sections are
2143 // specified as (handle,shndx) pairs in the two arrays in the order in
2144 // which they should appear in the final layout.
2146 static enum ld_plugin_status
2147 update_section_order(const struct ld_plugin_section
* section_list
,
2148 unsigned int num_sections
)
2150 gold_assert(parameters
->options().has_plugins());
2152 if (num_sections
== 0)
2155 if (section_list
== NULL
)
2158 Layout
* layout
= parameters
->options().plugins()->layout();
2159 gold_assert (layout
!= NULL
);
2161 std::map
<Section_id
, unsigned int>* order_map
2162 = layout
->get_section_order_map();
2164 /* Store the mapping from Section_id to section position in layout's
2165 order_map to consult after output sections are added. */
2166 for (unsigned int i
= 0; i
< num_sections
; ++i
)
2168 Object
* obj
= parameters
->options().plugins()->get_elf_object(
2169 section_list
[i
].handle
);
2170 if (obj
== NULL
|| obj
->is_dynamic())
2171 return LDPS_BAD_HANDLE
;
2172 unsigned int shndx
= section_list
[i
].shndx
;
2173 Section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
2174 (*order_map
)[secn_id
] = i
+ 1;
2180 // Let the linker know that the sections could be reordered.
2182 static enum ld_plugin_status
2183 allow_section_ordering()
2185 gold_assert(parameters
->options().has_plugins());
2186 Layout
* layout
= parameters
->options().plugins()->layout();
2187 layout
->set_section_ordering_specified();
2191 // Let the linker know that a subset of sections could be mapped
2192 // to a unique segment.
2194 static enum ld_plugin_status
2195 allow_unique_segment_for_sections()
2197 gold_assert(parameters
->options().has_plugins());
2198 Layout
* layout
= parameters
->options().plugins()->layout();
2199 layout
->set_unique_segment_for_sections_specified();
2203 // This function should map the list of sections specified in the
2204 // SECTION_LIST to a unique segment. ELF segments do not have names
2205 // and the NAME is used to identify Output Section which should contain
2206 // the list of sections. This Output Section will then be mapped to
2207 // a unique segment. FLAGS is used to specify if any additional segment
2208 // flags need to be set. For instance, a specific segment flag can be
2209 // set to identify this segment. Unsetting segment flags is not possible.
2210 // ALIGN specifies the alignment of the segment.
2212 static enum ld_plugin_status
2213 unique_segment_for_sections(const char* segment_name
,
2216 const struct ld_plugin_section
* section_list
,
2217 unsigned int num_sections
)
2219 gold_assert(parameters
->options().has_plugins());
2221 if (num_sections
== 0)
2224 if (section_list
== NULL
)
2227 Layout
* layout
= parameters
->options().plugins()->layout();
2228 gold_assert (layout
!= NULL
);
2230 Layout::Unique_segment_info
* s
= new Layout::Unique_segment_info
;
2231 s
->name
= segment_name
;
2235 for (unsigned int i
= 0; i
< num_sections
; ++i
)
2237 Object
* obj
= parameters
->options().plugins()->get_elf_object(
2238 section_list
[i
].handle
);
2239 if (obj
== NULL
|| obj
->is_dynamic())
2240 return LDPS_BAD_HANDLE
;
2241 unsigned int shndx
= section_list
[i
].shndx
;
2242 Const_section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
2243 layout
->insert_section_segment_map(secn_id
, s
);
2249 // Register a new_input handler.
2251 static enum ld_plugin_status
2252 register_new_input(ld_plugin_new_input_handler handler
)
2254 gold_assert(parameters
->options().has_plugins());
2255 parameters
->options().plugins()->set_new_input_handler(handler
);
2259 #endif // ENABLE_PLUGINS
2261 // Allocate a Pluginobj object of the appropriate size and endianness.
2264 make_sized_plugin_object(const std::string
& filename
,
2265 Input_file
* input_file
, off_t offset
, off_t filesize
)
2267 Pluginobj
* obj
= NULL
;
2269 parameters_force_valid_target();
2270 const Target
& target(parameters
->target());
2272 if (target
.get_size() == 32)
2274 if (target
.is_big_endian())
2275 #ifdef HAVE_TARGET_32_BIG
2276 obj
= new Sized_pluginobj
<32, true>(filename
, input_file
,
2279 gold_error(_("%s: not configured to support "
2280 "32-bit big-endian object"),
2284 #ifdef HAVE_TARGET_32_LITTLE
2285 obj
= new Sized_pluginobj
<32, false>(filename
, input_file
,
2288 gold_error(_("%s: not configured to support "
2289 "32-bit little-endian object"),
2293 else if (target
.get_size() == 64)
2295 if (target
.is_big_endian())
2296 #ifdef HAVE_TARGET_64_BIG
2297 obj
= new Sized_pluginobj
<64, true>(filename
, input_file
,
2300 gold_error(_("%s: not configured to support "
2301 "64-bit big-endian object"),
2305 #ifdef HAVE_TARGET_64_LITTLE
2306 obj
= new Sized_pluginobj
<64, false>(filename
, input_file
,
2309 gold_error(_("%s: not configured to support "
2310 "64-bit little-endian object"),
2315 gold_assert(obj
!= NULL
);
2319 } // End namespace gold.