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)\n"), inname
, strerror(errno
));
574 int out
= ::open(outname
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
577 gold_warning(_("%s: can't create (%s)\n"), outname
, strerror(errno
));
582 while ((len
= ::read(in
, buf
, sizeof(buf
))) > 0)
583 static_cast<void>(::write(out
, buf
, len
));
590 Plugin_recorder::replacement_file(const char* name
, bool is_lib
)
592 fprintf(this->logfile_
, "REPLACEMENT: %s", name
);
594 fprintf(this->logfile_
, "(lib)");
598 const char* basename
= lbasename(name
);
599 snprintf(counter
, sizeof(counter
), "%05d", this->file_count_
);
601 std::string
outname(this->tempdir_
);
603 outname
.append(counter
);
605 outname
.append(basename
);
606 if (link_or_copy_file(name
, outname
.c_str()))
607 fprintf(this->logfile_
, " -> %s", outname
.c_str());
609 fprintf(this->logfile_
, "\n");
613 Plugin_recorder::record_symbols(const Object
* obj
, int nsyms
,
614 const struct ld_plugin_symbol
* syms
)
616 fprintf(this->logfile_
, "SYMBOLS: %d %s\n", nsyms
, obj
->name().c_str());
617 for (int i
= 0; i
< nsyms
; ++i
)
619 const struct ld_plugin_symbol
* isym
= &syms
[i
];
645 switch (isym
->visibility
)
664 fprintf(this->logfile_
, " %5d: %-2s %c %s", i
, def
, vis
, isym
->name
);
665 if (isym
->version
!= NULL
&& isym
->version
[0] != '\0')
666 fprintf(this->logfile_
, "@%s", isym
->version
);
667 if (isym
->comdat_key
!= NULL
&& isym
->comdat_key
[0] != '\0')
669 if (strcmp(isym
->name
, isym
->comdat_key
) == 0)
670 fprintf(this->logfile_
, " [comdat]");
672 fprintf(this->logfile_
, " [comdat: %s]", isym
->comdat_key
);
674 fprintf(this->logfile_
, "\n");
678 // Plugin_manager methods.
680 Plugin_manager::~Plugin_manager()
682 for (Plugin_list::iterator p
= this->plugins_
.begin();
683 p
!= this->plugins_
.end();
686 this->plugins_
.clear();
687 for (Object_list::iterator obj
= this->objects_
.begin();
688 obj
!= this->objects_
.end();
691 this->objects_
.clear();
693 delete this->recorder_
;
696 // Load all plugin libraries.
699 Plugin_manager::load_plugins(Layout
* layout
)
701 this->layout_
= layout
;
703 if (is_debugging_enabled(DEBUG_PLUGIN
))
705 this->recorder_
= new Plugin_recorder();
706 this->recorder_
->init();
709 for (this->current_
= this->plugins_
.begin();
710 this->current_
!= this->plugins_
.end();
712 (*this->current_
)->load();
715 // Call the plugin claim-file handlers in turn to see if any claim the file.
718 Plugin_manager::claim_file(Input_file
* input_file
, off_t offset
,
719 off_t filesize
, Object
* elf_object
)
721 bool lock_initialized
= this->initialize_lock_
.initialize();
723 gold_assert(lock_initialized
);
724 Hold_lock
hl(*this->lock_
);
726 unsigned int handle
= this->objects_
.size();
727 this->input_file_
= input_file
;
728 this->plugin_input_file_
.name
= input_file
->filename().c_str();
729 this->plugin_input_file_
.fd
= input_file
->file().descriptor();
730 this->plugin_input_file_
.offset
= offset
;
731 this->plugin_input_file_
.filesize
= filesize
;
732 this->plugin_input_file_
.handle
= reinterpret_cast<void*>(handle
);
733 if (elf_object
!= NULL
)
734 this->objects_
.push_back(elf_object
);
735 this->in_claim_file_handler_
= true;
737 for (this->current_
= this->plugins_
.begin();
738 this->current_
!= this->plugins_
.end();
741 // If we aren't yet in replacement phase, allow plugins to claim input
742 // files, otherwise notify the plugin of the new input file, if needed.
743 if (!this->in_replacement_phase_
)
745 if ((*this->current_
)->claim_file(&this->plugin_input_file_
))
747 this->any_claimed_
= true;
748 this->in_claim_file_handler_
= false;
750 if (this->recorder_
!= NULL
)
752 const std::string
& objname
= (elf_object
== NULL
753 ? input_file
->filename()
754 : elf_object
->name());
755 this->recorder_
->claimed_file(objname
,
757 (*this->current_
)->filename());
760 if (this->objects_
.size() > handle
761 && this->objects_
[handle
]->pluginobj() != NULL
)
762 return this->objects_
[handle
]->pluginobj();
764 // If the plugin claimed the file but did not call the
765 // add_symbols callback, we need to create the Pluginobj now.
766 Pluginobj
* obj
= this->make_plugin_object(handle
);
772 (*this->current_
)->new_input(&this->plugin_input_file_
);
776 this->in_claim_file_handler_
= false;
778 if (this->recorder_
!= NULL
)
779 this->recorder_
->unclaimed_file(input_file
->filename(), offset
, filesize
);
784 // Save an archive. This is used so that a plugin can add a file
785 // which refers to a symbol which was not previously referenced. In
786 // that case we want to pretend that the symbol was referenced before,
787 // and pull in the archive object.
790 Plugin_manager::save_archive(Archive
* archive
)
792 if (this->in_replacement_phase_
|| !this->any_claimed_
)
795 this->rescannable_
.push_back(Rescannable(archive
));
798 // Save an Input_group. This is like save_archive.
801 Plugin_manager::save_input_group(Input_group
* input_group
)
803 if (this->in_replacement_phase_
|| !this->any_claimed_
)
806 this->rescannable_
.push_back(Rescannable(input_group
));
809 // Call the all-symbols-read handlers.
812 Plugin_manager::all_symbols_read(Workqueue
* workqueue
, Task
* task
,
813 Input_objects
* input_objects
,
814 Symbol_table
* symtab
,
815 Dirsearch
* dirpath
, Mapfile
* mapfile
,
816 Task_token
** last_blocker
)
818 this->in_replacement_phase_
= true;
819 this->workqueue_
= workqueue
;
821 this->input_objects_
= input_objects
;
822 this->symtab_
= symtab
;
823 this->dirpath_
= dirpath
;
824 this->mapfile_
= mapfile
;
825 this->this_blocker_
= NULL
;
827 // Set symbols used in defsym expressions as seen in real ELF.
828 Layout
*layout
= parameters
->options().plugins()->layout();
829 layout
->script_options()->set_defsym_uses_in_real_elf(symtab
);
830 layout
->script_options()->find_defsym_defs(this->defsym_defines_set_
);
832 for (this->current_
= this->plugins_
.begin();
833 this->current_
!= this->plugins_
.end();
835 (*this->current_
)->all_symbols_read();
837 if (this->any_added_
)
839 Task_token
* next_blocker
= new Task_token(true);
840 next_blocker
->add_blocker();
841 workqueue
->queue(new Plugin_rescan(this->this_blocker_
, next_blocker
));
842 this->this_blocker_
= next_blocker
;
845 *last_blocker
= this->this_blocker_
;
848 // This is called when we see a new undefined symbol. If we are in
849 // the replacement phase, this means that we may need to rescan some
850 // archives we have previously seen.
853 Plugin_manager::new_undefined_symbol(Symbol
* sym
)
855 if (this->in_replacement_phase_
)
856 this->undefined_symbols_
.push_back(sym
);
859 // Rescan archives as needed. This handles the case where a new
860 // object file added by a plugin has an undefined reference to some
861 // symbol defined in an archive.
864 Plugin_manager::rescan(Task
* task
)
866 size_t rescan_pos
= 0;
867 size_t rescan_size
= this->rescannable_
.size();
868 while (!this->undefined_symbols_
.empty())
870 if (rescan_pos
>= rescan_size
)
872 this->undefined_symbols_
.clear();
876 Undefined_symbol_list undefs
;
877 undefs
.reserve(this->undefined_symbols_
.size());
878 this->undefined_symbols_
.swap(undefs
);
880 size_t min_rescan_pos
= rescan_size
;
882 for (Undefined_symbol_list::const_iterator p
= undefs
.begin();
886 if (!(*p
)->is_undefined())
889 this->undefined_symbols_
.push_back(*p
);
891 // Find the first rescan archive which defines this symbol,
892 // starting at the current rescan position. The rescan position
893 // exists so that given -la -lb -lc we don't look for undefined
894 // symbols in -lb back in -la, but instead get the definition
895 // from -lc. Don't bother to look past the current minimum
897 for (size_t i
= rescan_pos
; i
< min_rescan_pos
; ++i
)
899 if (this->rescannable_defines(i
, *p
))
907 if (min_rescan_pos
>= rescan_size
)
909 // We didn't find any rescannable archives which define any
910 // undefined symbols.
914 const Rescannable
& r(this->rescannable_
[min_rescan_pos
]);
917 Task_lock_obj
<Archive
> tl(task
, r
.u
.archive
);
918 r
.u
.archive
->add_symbols(this->symtab_
, this->layout_
,
919 this->input_objects_
, this->mapfile_
);
923 size_t next_saw_undefined
= this->symtab_
->saw_undefined();
924 size_t saw_undefined
;
927 saw_undefined
= next_saw_undefined
;
929 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
930 p
!= r
.u
.input_group
->end();
933 Task_lock_obj
<Archive
> tl(task
, *p
);
935 (*p
)->add_symbols(this->symtab_
, this->layout_
,
936 this->input_objects_
, this->mapfile_
);
939 next_saw_undefined
= this->symtab_
->saw_undefined();
941 while (saw_undefined
!= next_saw_undefined
);
944 for (size_t i
= rescan_pos
; i
< min_rescan_pos
+ 1; ++i
)
946 if (this->rescannable_
[i
].is_archive
)
947 delete this->rescannable_
[i
].u
.archive
;
949 delete this->rescannable_
[i
].u
.input_group
;
952 rescan_pos
= min_rescan_pos
+ 1;
956 // Return whether the rescannable at index I defines SYM.
959 Plugin_manager::rescannable_defines(size_t i
, Symbol
* sym
)
961 const Rescannable
& r(this->rescannable_
[i
]);
963 return r
.u
.archive
->defines_symbol(sym
);
966 for (Input_group::const_iterator p
= r
.u
.input_group
->begin();
967 p
!= r
.u
.input_group
->end();
970 if ((*p
)->defines_symbol(sym
))
977 // Layout deferred objects.
980 Plugin_manager::layout_deferred_objects()
982 Deferred_layout_list::iterator obj
;
984 for (obj
= this->deferred_layout_objects_
.begin();
985 obj
!= this->deferred_layout_objects_
.end();
988 // Lock the object so we can read from it. This is only called
989 // single-threaded from queue_middle_tasks, so it is OK to lock.
990 // Unfortunately we have no way to pass in a Task token.
991 const Task
* dummy_task
= reinterpret_cast<const Task
*>(-1);
992 Task_lock_obj
<Object
> tl(dummy_task
, *obj
);
993 (*obj
)->layout_deferred_sections(this->layout_
);
997 // Call the cleanup handlers.
1000 Plugin_manager::cleanup()
1002 if (this->any_added_
)
1004 // If any input files were added, close all the input files.
1005 // This is because the plugin may want to remove them, and on
1006 // Windows you are not allowed to remove an open file.
1007 close_all_descriptors();
1010 for (this->current_
= this->plugins_
.begin();
1011 this->current_
!= this->plugins_
.end();
1013 (*this->current_
)->cleanup();
1016 // Make a new Pluginobj object. This is called when the plugin calls
1017 // the add_symbols API.
1020 Plugin_manager::make_plugin_object(unsigned int handle
)
1022 // Make sure we aren't asked to make an object for the same handle twice.
1023 if (this->objects_
.size() != handle
1024 && this->objects_
[handle
]->pluginobj() != NULL
)
1027 const std::string
* filename
= &this->input_file_
->filename();
1029 // If the elf object for this file was pushed into the objects_ vector,
1030 // use its filename, then delete it to make room for the Pluginobj as
1031 // this file is claimed.
1032 if (this->objects_
.size() != handle
)
1034 filename
= &this->objects_
.back()->name();
1035 this->objects_
.pop_back();
1038 Pluginobj
* obj
= make_sized_plugin_object(*filename
,
1040 this->plugin_input_file_
.offset
,
1041 this->plugin_input_file_
.filesize
);
1045 this->objects_
.push_back(obj
);
1049 // Get the input file information with an open (possibly re-opened)
1053 Plugin_manager::get_input_file(unsigned int handle
,
1054 struct ld_plugin_input_file
* file
)
1056 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1058 return LDPS_BAD_HANDLE
;
1060 obj
->lock(this->task_
);
1061 file
->name
= obj
->filename().c_str();
1062 file
->fd
= obj
->descriptor();
1063 file
->offset
= obj
->offset();
1064 file
->filesize
= obj
->filesize();
1065 file
->handle
= reinterpret_cast<void*>(handle
);
1069 // Release the input file.
1072 Plugin_manager::release_input_file(unsigned int handle
)
1074 if (this->object(handle
) == NULL
)
1075 return LDPS_BAD_HANDLE
;
1077 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1080 return LDPS_BAD_HANDLE
;
1082 obj
->unlock(this->task_
);
1086 // Get the elf object corresponding to the handle. Return NULL if we
1087 // found a Pluginobj instead.
1090 Plugin_manager::get_elf_object(const void* handle
)
1092 Object
* obj
= this->object(
1093 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1095 // The object should not be a Pluginobj.
1097 || obj
->pluginobj() != NULL
)
1104 Plugin_manager::get_view(unsigned int handle
, const void **viewp
)
1108 Input_file
*input_file
;
1109 if (this->in_claim_file_handler_
)
1111 // We are being called from the claim_file hook.
1112 const struct ld_plugin_input_file
&f
= this->plugin_input_file_
;
1114 filesize
= f
.filesize
;
1115 input_file
= this->input_file_
;
1119 // An already claimed file.
1120 if (this->object(handle
) == NULL
)
1121 return LDPS_BAD_HANDLE
;
1122 Pluginobj
* obj
= this->object(handle
)->pluginobj();
1124 return LDPS_BAD_HANDLE
;
1125 offset
= obj
->offset();
1126 filesize
= obj
->filesize();
1127 input_file
= obj
->input_file();
1129 *viewp
= (void*) input_file
->file().get_view(offset
, 0, filesize
, false,
1134 // Add a new library path.
1137 Plugin_manager::set_extra_library_path(const char* path
)
1139 this->extra_search_path_
= std::string(path
);
1143 // Add a new input file.
1146 Plugin_manager::add_input_file(const char* pathname
, bool is_lib
)
1148 Input_file_argument
file(pathname
,
1150 ? Input_file_argument::INPUT_FILE_TYPE_LIBRARY
1151 : Input_file_argument::INPUT_FILE_TYPE_FILE
),
1153 ? this->extra_search_path_
.c_str()
1157 Input_argument
* input_argument
= new Input_argument(file
);
1158 Task_token
* next_blocker
= new Task_token(true);
1159 next_blocker
->add_blocker();
1160 if (parameters
->incremental())
1161 gold_error(_("input files added by plug-ins in --incremental mode not "
1164 if (this->recorder_
!= NULL
)
1165 this->recorder_
->replacement_file(pathname
, is_lib
);
1167 this->workqueue_
->queue_soon(new Read_symbols(this->input_objects_
,
1176 this->this_blocker_
,
1178 this->this_blocker_
= next_blocker
;
1179 this->any_added_
= true;
1185 Pluginobj::Pluginobj(const std::string
& name
, Input_file
* input_file
,
1186 off_t offset
, off_t filesize
)
1187 : Object(name
, input_file
, false, offset
),
1188 nsyms_(0), syms_(NULL
), symbols_(), filesize_(filesize
), comdat_map_()
1192 // Return TRUE if a defined symbol is referenced from outside the
1193 // universe of claimed objects. Only references from relocatable,
1194 // non-IR (unclaimed) objects count as a reference. References from
1195 // dynamic objects count only as "visible".
1198 is_referenced_from_outside(Symbol
* lsym
)
1200 if (lsym
->in_real_elf())
1202 if (parameters
->options().relocatable())
1204 if (parameters
->options().is_undefined(lsym
->name()))
1209 // Return TRUE if a defined symbol might be reachable from outside the
1213 is_visible_from_outside(Symbol
* lsym
)
1217 if (parameters
->options().export_dynamic() || parameters
->options().shared()
1218 || parameters
->options().in_dynamic_list(lsym
->name())
1219 || parameters
->options().is_export_dynamic_symbol(lsym
->name()))
1220 return lsym
->is_externally_visible();
1224 // Get symbol resolution info.
1227 Pluginobj::get_symbol_resolution_info(Symbol_table
* symtab
,
1229 ld_plugin_symbol
* syms
,
1232 // For version 1 of this interface, we cannot use
1233 // LDPR_PREVAILING_DEF_IRONLY_EXP, so we return LDPR_PREVAILING_DEF
1235 const ld_plugin_symbol_resolution ldpr_prevailing_def_ironly_exp
1237 ? LDPR_PREVAILING_DEF_IRONLY_EXP
1238 : LDPR_PREVAILING_DEF
);
1240 if (nsyms
> this->nsyms_
)
1241 return LDPS_NO_SYMS
;
1243 if (static_cast<size_t>(nsyms
) > this->symbols_
.size())
1245 // We never decided to include this object. We mark all symbols as
1247 gold_assert(this->symbols_
.size() == 0);
1248 for (int i
= 0; i
< nsyms
; i
++)
1249 syms
[i
].resolution
= LDPR_PREEMPTED_REG
;
1250 return version
> 2 ? LDPS_NO_SYMS
: LDPS_OK
;
1253 Plugin_manager
* plugins
= parameters
->options().plugins();
1254 for (int i
= 0; i
< nsyms
; i
++)
1256 ld_plugin_symbol
* isym
= &syms
[i
];
1257 Symbol
* lsym
= this->symbols_
[i
];
1258 if (lsym
->is_forwarder())
1259 lsym
= symtab
->resolve_forwards(lsym
);
1260 ld_plugin_symbol_resolution res
= LDPR_UNKNOWN
;
1262 if (plugins
->is_defsym_def(lsym
->name()))
1264 // The symbol is redefined via defsym.
1265 res
= LDPR_PREEMPTED_REG
;
1267 else if (lsym
->is_undefined())
1269 // The symbol remains undefined.
1272 else if (isym
->def
== LDPK_UNDEF
1273 || isym
->def
== LDPK_WEAKUNDEF
1274 || isym
->def
== LDPK_COMMON
)
1276 // The original symbol was undefined or common.
1277 if (lsym
->source() != Symbol::FROM_OBJECT
)
1278 res
= LDPR_RESOLVED_EXEC
;
1279 else if (lsym
->object()->pluginobj() == this)
1281 if (is_referenced_from_outside(lsym
))
1282 res
= LDPR_PREVAILING_DEF
;
1283 else if (is_visible_from_outside(lsym
))
1284 res
= ldpr_prevailing_def_ironly_exp
;
1286 res
= LDPR_PREVAILING_DEF_IRONLY
;
1288 else if (lsym
->object()->pluginobj() != NULL
)
1289 res
= LDPR_RESOLVED_IR
;
1290 else if (lsym
->object()->is_dynamic())
1291 res
= LDPR_RESOLVED_DYN
;
1293 res
= LDPR_RESOLVED_EXEC
;
1297 // The original symbol was a definition.
1298 if (lsym
->source() != Symbol::FROM_OBJECT
)
1299 res
= LDPR_PREEMPTED_REG
;
1300 else if (lsym
->object() == static_cast<const Object
*>(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
;
1310 res
= (lsym
->object()->pluginobj() != NULL
1312 : LDPR_PREEMPTED_REG
);
1314 isym
->resolution
= res
;
1319 // Return TRUE if the comdat group with key COMDAT_KEY from this object
1323 Pluginobj::include_comdat_group(std::string comdat_key
, Layout
* layout
)
1325 std::pair
<Comdat_map::iterator
, bool> ins
=
1326 this->comdat_map_
.insert(std::make_pair(comdat_key
, false));
1328 // If this is the first time we've seen this comdat key, ask the
1329 // layout object whether it should be included.
1331 ins
.first
->second
= layout
->find_or_add_kept_section(comdat_key
,
1335 return ins
.first
->second
;
1338 // Class Sized_pluginobj.
1340 template<int size
, bool big_endian
>
1341 Sized_pluginobj
<size
, big_endian
>::Sized_pluginobj(
1342 const std::string
& name
,
1343 Input_file
* input_file
,
1346 : Pluginobj(name
, input_file
, offset
, filesize
)
1350 // Read the symbols. Not used for plugin objects.
1352 template<int size
, bool big_endian
>
1354 Sized_pluginobj
<size
, big_endian
>::do_read_symbols(Read_symbols_data
*)
1359 // Lay out the input sections. Not used for plugin objects.
1361 template<int size
, bool big_endian
>
1363 Sized_pluginobj
<size
, big_endian
>::do_layout(Symbol_table
*, Layout
*,
1369 // Add the symbols to the symbol table.
1371 template<int size
, bool big_endian
>
1373 Sized_pluginobj
<size
, big_endian
>::do_add_symbols(Symbol_table
* symtab
,
1377 const int sym_size
= elfcpp::Elf_sizes
<size
>::sym_size
;
1378 unsigned char symbuf
[sym_size
];
1379 elfcpp::Sym
<size
, big_endian
> sym(symbuf
);
1380 elfcpp::Sym_write
<size
, big_endian
> osym(symbuf
);
1382 Plugin_recorder
* recorder
= parameters
->options().plugins()->recorder();
1383 if (recorder
!= NULL
)
1384 recorder
->record_symbols(this, this->nsyms_
, this->syms_
);
1386 this->symbols_
.resize(this->nsyms_
);
1388 for (int i
= 0; i
< this->nsyms_
; ++i
)
1390 const struct ld_plugin_symbol
* isym
= &this->syms_
[i
];
1391 const char* name
= isym
->name
;
1392 const char* ver
= isym
->version
;
1393 elfcpp::Elf_Half shndx
;
1397 if (name
!= NULL
&& name
[0] == '\0')
1399 if (ver
!= NULL
&& ver
[0] == '\0')
1405 case LDPK_WEAKUNDEF
:
1406 bind
= elfcpp::STB_WEAK
;
1412 bind
= elfcpp::STB_GLOBAL
;
1420 shndx
= elfcpp::SHN_ABS
;
1423 shndx
= elfcpp::SHN_COMMON
;
1426 case LDPK_WEAKUNDEF
:
1428 shndx
= elfcpp::SHN_UNDEF
;
1432 switch (isym
->visibility
)
1434 case LDPV_PROTECTED
:
1435 vis
= elfcpp::STV_PROTECTED
;
1438 vis
= elfcpp::STV_INTERNAL
;
1441 vis
= elfcpp::STV_HIDDEN
;
1445 vis
= elfcpp::STV_DEFAULT
;
1449 if (isym
->comdat_key
!= NULL
1450 && isym
->comdat_key
[0] != '\0'
1451 && !this->include_comdat_group(isym
->comdat_key
, layout
))
1452 shndx
= elfcpp::SHN_UNDEF
;
1454 osym
.put_st_name(0);
1455 osym
.put_st_value(0);
1456 osym
.put_st_size(0);
1457 osym
.put_st_info(bind
, elfcpp::STT_NOTYPE
);
1458 osym
.put_st_other(vis
, 0);
1459 osym
.put_st_shndx(shndx
);
1462 symtab
->add_from_pluginobj
<size
, big_endian
>(this, name
, ver
, &sym
);
1466 template<int size
, bool big_endian
>
1467 Archive::Should_include
1468 Sized_pluginobj
<size
, big_endian
>::do_should_include_member(
1469 Symbol_table
* symtab
,
1474 char* tmpbuf
= NULL
;
1475 size_t tmpbuflen
= 0;
1477 for (int i
= 0; i
< this->nsyms_
; ++i
)
1479 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1480 if (sym
.def
== LDPK_UNDEF
|| sym
.def
== LDPK_WEAKUNDEF
)
1482 const char* name
= sym
.name
;
1484 Archive::Should_include t
= Archive::should_include_member(symtab
,
1490 if (t
== Archive::SHOULD_INCLUDE_YES
)
1499 return Archive::SHOULD_INCLUDE_UNKNOWN
;
1502 // Iterate over global symbols, calling a visitor class V for each.
1504 template<int size
, bool big_endian
>
1506 Sized_pluginobj
<size
, big_endian
>::do_for_all_global_symbols(
1508 Library_base::Symbol_visitor_base
* v
)
1510 for (int i
= 0; i
< this->nsyms_
; ++i
)
1512 const struct ld_plugin_symbol
& sym
= this->syms_
[i
];
1513 if (sym
.def
!= LDPK_UNDEF
)
1518 // Iterate over local symbols, calling a visitor class V for each GOT offset
1519 // associated with a local symbol.
1520 template<int size
, bool big_endian
>
1522 Sized_pluginobj
<size
, big_endian
>::do_for_all_local_got_entries(
1523 Got_offset_list::Visitor
*) const
1528 // Get the size of a section. Not used for plugin objects.
1530 template<int size
, bool big_endian
>
1532 Sized_pluginobj
<size
, big_endian
>::do_section_size(unsigned int)
1538 // Get the name of a section. Not used for plugin objects.
1540 template<int size
, bool big_endian
>
1542 Sized_pluginobj
<size
, big_endian
>::do_section_name(unsigned int) const
1545 return std::string();
1548 // Return a view of the contents of a section. Not used for plugin objects.
1550 template<int size
, bool big_endian
>
1551 const unsigned char*
1552 Sized_pluginobj
<size
, big_endian
>::do_section_contents(
1561 // Return section flags. Not used for plugin objects.
1563 template<int size
, bool big_endian
>
1565 Sized_pluginobj
<size
, big_endian
>::do_section_flags(unsigned int)
1571 // Return section entsize. Not used for plugin objects.
1573 template<int size
, bool big_endian
>
1575 Sized_pluginobj
<size
, big_endian
>::do_section_entsize(unsigned int)
1581 // Return section address. Not used for plugin objects.
1583 template<int size
, bool big_endian
>
1585 Sized_pluginobj
<size
, big_endian
>::do_section_address(unsigned int)
1591 // Return section type. Not used for plugin objects.
1593 template<int size
, bool big_endian
>
1595 Sized_pluginobj
<size
, big_endian
>::do_section_type(unsigned int)
1601 // Return the section link field. Not used for plugin objects.
1603 template<int size
, bool big_endian
>
1605 Sized_pluginobj
<size
, big_endian
>::do_section_link(unsigned int)
1611 // Return the section link field. Not used for plugin objects.
1613 template<int size
, bool big_endian
>
1615 Sized_pluginobj
<size
, big_endian
>::do_section_info(unsigned int)
1621 // Return the section alignment. Not used for plugin objects.
1623 template<int size
, bool big_endian
>
1625 Sized_pluginobj
<size
, big_endian
>::do_section_addralign(unsigned int)
1631 // Return the Xindex structure to use. Not used for plugin objects.
1633 template<int size
, bool big_endian
>
1635 Sized_pluginobj
<size
, big_endian
>::do_initialize_xindex()
1641 // Get symbol counts. Don't count plugin objects; the replacement
1642 // files will provide the counts.
1644 template<int size
, bool big_endian
>
1646 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbol_counts(
1647 const Symbol_table
*,
1655 // Get symbols. Not used for plugin objects.
1657 template<int size
, bool big_endian
>
1658 const Object::Symbols
*
1659 Sized_pluginobj
<size
, big_endian
>::do_get_global_symbols() const
1664 // Class Plugin_finish. This task runs after all replacement files have
1665 // been added. For now, it's a placeholder for a possible plugin API
1666 // to allow the plugin to release most of its resources. The cleanup
1667 // handlers must be called later, because they can remove the temporary
1668 // object files that are needed until the end of the link.
1670 class Plugin_finish
: public Task
1673 Plugin_finish(Task_token
* this_blocker
, Task_token
* next_blocker
)
1674 : this_blocker_(this_blocker
), next_blocker_(next_blocker
)
1679 if (this->this_blocker_
!= NULL
)
1680 delete this->this_blocker_
;
1686 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1687 return this->this_blocker_
;
1692 locks(Task_locker
* tl
)
1693 { tl
->add(this, this->next_blocker_
); }
1698 Plugin_manager
* plugins
= parameters
->options().plugins();
1699 gold_assert(plugins
!= NULL
);
1700 // We could call early cleanup handlers here.
1701 if (plugins
->recorder())
1702 plugins
->recorder()->finish();
1707 { return "Plugin_finish"; }
1710 Task_token
* this_blocker_
;
1711 Task_token
* next_blocker_
;
1714 // Class Plugin_hook.
1716 Plugin_hook::~Plugin_hook()
1720 // Return whether a Plugin_hook task is runnable.
1723 Plugin_hook::is_runnable()
1725 if (this->this_blocker_
!= NULL
&& this->this_blocker_
->is_blocked())
1726 return this->this_blocker_
;
1730 // Return a Task_locker for a Plugin_hook task. We don't need any
1734 Plugin_hook::locks(Task_locker
*)
1738 // Run the "all symbols read" plugin hook.
1741 Plugin_hook::run(Workqueue
* workqueue
)
1743 gold_assert(this->options_
.has_plugins());
1744 Symbol
* start_sym
= this->symtab_
->lookup(parameters
->entry());
1745 if (start_sym
!= NULL
)
1746 start_sym
->set_in_real_elf();
1748 this->options_
.plugins()->all_symbols_read(workqueue
,
1750 this->input_objects_
,
1754 &this->this_blocker_
);
1755 workqueue
->queue_soon(new Plugin_finish(this->this_blocker_
,
1756 this->next_blocker_
));
1759 // The C interface routines called by the plugins.
1761 #ifdef ENABLE_PLUGINS
1763 // Register a claim-file handler.
1765 static enum ld_plugin_status
1766 register_claim_file(ld_plugin_claim_file_handler handler
)
1768 gold_assert(parameters
->options().has_plugins());
1769 parameters
->options().plugins()->set_claim_file_handler(handler
);
1773 // Register an all-symbols-read handler.
1775 static enum ld_plugin_status
1776 register_all_symbols_read(ld_plugin_all_symbols_read_handler handler
)
1778 gold_assert(parameters
->options().has_plugins());
1779 parameters
->options().plugins()->set_all_symbols_read_handler(handler
);
1783 // Register a cleanup handler.
1785 static enum ld_plugin_status
1786 register_cleanup(ld_plugin_cleanup_handler handler
)
1788 gold_assert(parameters
->options().has_plugins());
1789 parameters
->options().plugins()->set_cleanup_handler(handler
);
1793 // Add symbols from a plugin-claimed input file.
1795 static enum ld_plugin_status
1796 add_symbols(void* handle
, int nsyms
, const ld_plugin_symbol
* syms
)
1798 gold_assert(parameters
->options().has_plugins());
1799 Pluginobj
* obj
= parameters
->options().plugins()->make_plugin_object(
1800 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1803 obj
->store_incoming_symbols(nsyms
, syms
);
1807 // Get the input file information with an open (possibly re-opened)
1810 static enum ld_plugin_status
1811 get_input_file(const void* handle
, struct ld_plugin_input_file
* file
)
1813 gold_assert(parameters
->options().has_plugins());
1814 unsigned int obj_index
=
1815 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1816 return parameters
->options().plugins()->get_input_file(obj_index
, file
);
1819 // Release the input file.
1821 static enum ld_plugin_status
1822 release_input_file(const void* handle
)
1824 gold_assert(parameters
->options().has_plugins());
1825 unsigned int obj_index
=
1826 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1827 return parameters
->options().plugins()->release_input_file(obj_index
);
1830 static enum ld_plugin_status
1831 get_view(const void *handle
, const void **viewp
)
1833 gold_assert(parameters
->options().has_plugins());
1834 unsigned int obj_index
=
1835 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
));
1836 return parameters
->options().plugins()->get_view(obj_index
, viewp
);
1839 // Get the symbol resolution info for a plugin-claimed input file.
1841 static enum ld_plugin_status
1842 get_symbols(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1844 gold_assert(parameters
->options().has_plugins());
1845 Plugin_manager
* plugins
= parameters
->options().plugins();
1846 Object
* obj
= plugins
->object(
1847 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1850 Pluginobj
* plugin_obj
= obj
->pluginobj();
1851 if (plugin_obj
== NULL
)
1853 Symbol_table
* symtab
= plugins
->symtab();
1854 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 1);
1857 // Version 2 of the above. The only difference is that this version
1858 // is allowed to return the resolution code LDPR_PREVAILING_DEF_IRONLY_EXP.
1860 static enum ld_plugin_status
1861 get_symbols_v2(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1863 gold_assert(parameters
->options().has_plugins());
1864 Plugin_manager
* plugins
= parameters
->options().plugins();
1865 Object
* obj
= plugins
->object(
1866 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1869 Pluginobj
* plugin_obj
= obj
->pluginobj();
1870 if (plugin_obj
== NULL
)
1872 Symbol_table
* symtab
= plugins
->symtab();
1873 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 2);
1876 // Version 3 of the above. The only difference from v2 is that it
1877 // returns LDPS_NO_SYMS instead of LDPS_OK for the objects we never
1878 // decided to include.
1880 static enum ld_plugin_status
1881 get_symbols_v3(const void* handle
, int nsyms
, ld_plugin_symbol
* syms
)
1883 gold_assert(parameters
->options().has_plugins());
1884 Plugin_manager
* plugins
= parameters
->options().plugins();
1885 Object
* obj
= plugins
->object(
1886 static_cast<unsigned int>(reinterpret_cast<intptr_t>(handle
)));
1889 Pluginobj
* plugin_obj
= obj
->pluginobj();
1890 if (plugin_obj
== NULL
)
1892 Symbol_table
* symtab
= plugins
->symtab();
1893 return plugin_obj
->get_symbol_resolution_info(symtab
, nsyms
, syms
, 3);
1896 // Add a new (real) input file generated by a plugin.
1898 static enum ld_plugin_status
1899 add_input_file(const char* pathname
)
1901 gold_assert(parameters
->options().has_plugins());
1902 return parameters
->options().plugins()->add_input_file(pathname
, false);
1905 // Add a new (real) library required by a plugin.
1907 static enum ld_plugin_status
1908 add_input_library(const char* pathname
)
1910 gold_assert(parameters
->options().has_plugins());
1911 return parameters
->options().plugins()->add_input_file(pathname
, true);
1914 // Set the extra library path to be used by libraries added via
1915 // add_input_library
1917 static enum ld_plugin_status
1918 set_extra_library_path(const char* path
)
1920 gold_assert(parameters
->options().has_plugins());
1921 return parameters
->options().plugins()->set_extra_library_path(path
);
1924 // Issue a diagnostic message from a plugin.
1926 static enum ld_plugin_status
1927 message(int level
, const char* format
, ...)
1930 va_start(args
, format
);
1935 parameters
->errors()->info(format
, args
);
1938 parameters
->errors()->warning(format
, args
);
1942 parameters
->errors()->error(format
, args
);
1945 parameters
->errors()->fatal(format
, args
);
1953 // Get the section count of the object corresponding to the handle. This
1954 // plugin interface can only be called in the claim_file handler of the plugin.
1956 static enum ld_plugin_status
1957 get_input_section_count(const void* handle
, unsigned int* count
)
1959 gold_assert(parameters
->options().has_plugins());
1961 if (!parameters
->options().plugins()->in_claim_file_handler())
1964 Object
* obj
= parameters
->options().plugins()->get_elf_object(handle
);
1969 *count
= obj
->shnum();
1973 // Get the type of the specified section in the object corresponding
1974 // to the handle. This plugin interface can only be called in the
1975 // claim_file handler of the plugin.
1977 static enum ld_plugin_status
1978 get_input_section_type(const struct ld_plugin_section section
,
1981 gold_assert(parameters
->options().has_plugins());
1983 if (!parameters
->options().plugins()->in_claim_file_handler())
1987 = parameters
->options().plugins()->get_elf_object(section
.handle
);
1990 return LDPS_BAD_HANDLE
;
1992 *type
= obj
->section_type(section
.shndx
);
1996 // Get the name of the specified section in the object corresponding
1997 // to the handle. This plugin interface can only be called in the
1998 // claim_file handler of the plugin.
2000 static enum ld_plugin_status
2001 get_input_section_name(const struct ld_plugin_section section
,
2002 char** section_name_ptr
)
2004 gold_assert(parameters
->options().has_plugins());
2006 if (!parameters
->options().plugins()->in_claim_file_handler())
2010 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2013 return LDPS_BAD_HANDLE
;
2015 // Check if the object is locked before getting the section name.
2016 gold_assert(obj
->is_locked());
2018 const std::string section_name
= obj
->section_name(section
.shndx
);
2019 *section_name_ptr
= static_cast<char*>(malloc(section_name
.length() + 1));
2020 memcpy(*section_name_ptr
, section_name
.c_str(), section_name
.length() + 1);
2024 // Get the contents of the specified section in the object corresponding
2025 // to the handle. This plugin interface can only be called in the
2026 // claim_file handler of the plugin.
2028 static enum ld_plugin_status
2029 get_input_section_contents(const struct ld_plugin_section section
,
2030 const unsigned char** section_contents_ptr
,
2033 gold_assert(parameters
->options().has_plugins());
2035 if (!parameters
->options().plugins()->in_claim_file_handler())
2039 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2042 return LDPS_BAD_HANDLE
;
2044 // Check if the object is locked before getting the section contents.
2045 gold_assert(obj
->is_locked());
2047 section_size_type plen
;
2048 *section_contents_ptr
2049 = obj
->section_contents(section
.shndx
, &plen
, false);
2054 // Get the alignment of the specified section in the object corresponding
2055 // to the handle. This plugin interface can only be called in the
2056 // claim_file handler of the plugin.
2058 static enum ld_plugin_status
2059 get_input_section_alignment(const struct ld_plugin_section section
,
2060 unsigned int* addralign
)
2062 gold_assert(parameters
->options().has_plugins());
2064 if (!parameters
->options().plugins()->in_claim_file_handler())
2068 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2071 return LDPS_BAD_HANDLE
;
2073 *addralign
= obj
->section_addralign(section
.shndx
);
2077 // Get the size of the specified section in the object corresponding
2078 // to the handle. This plugin interface can only be called in the
2079 // claim_file handler of the plugin.
2081 static enum ld_plugin_status
2082 get_input_section_size(const struct ld_plugin_section section
,
2085 gold_assert(parameters
->options().has_plugins());
2087 if (!parameters
->options().plugins()->in_claim_file_handler())
2091 = parameters
->options().plugins()->get_elf_object(section
.handle
);
2094 return LDPS_BAD_HANDLE
;
2096 *secsize
= obj
->section_size(section
.shndx
);
2100 static enum ld_plugin_status
2101 get_wrap_symbols(uint64_t *count
, const char ***wrap_symbols
)
2103 gold_assert(parameters
->options().has_plugins());
2104 *count
= parameters
->options().wrap_size();
2109 *wrap_symbols
= new const char *[*count
];
2111 for (options::String_set::const_iterator
2112 it
= parameters
->options().wrap_begin();
2113 it
!= parameters
->options().wrap_end(); ++it
, ++i
) {
2114 (*wrap_symbols
)[i
] = it
->c_str();
2120 // Specify the ordering of sections in the final layout. The sections are
2121 // specified as (handle,shndx) pairs in the two arrays in the order in
2122 // which they should appear in the final layout.
2124 static enum ld_plugin_status
2125 update_section_order(const struct ld_plugin_section
* section_list
,
2126 unsigned int num_sections
)
2128 gold_assert(parameters
->options().has_plugins());
2130 if (num_sections
== 0)
2133 if (section_list
== NULL
)
2136 Layout
* layout
= parameters
->options().plugins()->layout();
2137 gold_assert (layout
!= NULL
);
2139 std::map
<Section_id
, unsigned int>* order_map
2140 = layout
->get_section_order_map();
2142 /* Store the mapping from Section_id to section position in layout's
2143 order_map to consult after output sections are added. */
2144 for (unsigned int i
= 0; i
< num_sections
; ++i
)
2146 Object
* obj
= parameters
->options().plugins()->get_elf_object(
2147 section_list
[i
].handle
);
2148 if (obj
== NULL
|| obj
->is_dynamic())
2149 return LDPS_BAD_HANDLE
;
2150 unsigned int shndx
= section_list
[i
].shndx
;
2151 Section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
2152 (*order_map
)[secn_id
] = i
+ 1;
2158 // Let the linker know that the sections could be reordered.
2160 static enum ld_plugin_status
2161 allow_section_ordering()
2163 gold_assert(parameters
->options().has_plugins());
2164 Layout
* layout
= parameters
->options().plugins()->layout();
2165 layout
->set_section_ordering_specified();
2169 // Let the linker know that a subset of sections could be mapped
2170 // to a unique segment.
2172 static enum ld_plugin_status
2173 allow_unique_segment_for_sections()
2175 gold_assert(parameters
->options().has_plugins());
2176 Layout
* layout
= parameters
->options().plugins()->layout();
2177 layout
->set_unique_segment_for_sections_specified();
2181 // This function should map the list of sections specified in the
2182 // SECTION_LIST to a unique segment. ELF segments do not have names
2183 // and the NAME is used to identify Output Section which should contain
2184 // the list of sections. This Output Section will then be mapped to
2185 // a unique segment. FLAGS is used to specify if any additional segment
2186 // flags need to be set. For instance, a specific segment flag can be
2187 // set to identify this segment. Unsetting segment flags is not possible.
2188 // ALIGN specifies the alignment of the segment.
2190 static enum ld_plugin_status
2191 unique_segment_for_sections(const char* segment_name
,
2194 const struct ld_plugin_section
* section_list
,
2195 unsigned int num_sections
)
2197 gold_assert(parameters
->options().has_plugins());
2199 if (num_sections
== 0)
2202 if (section_list
== NULL
)
2205 Layout
* layout
= parameters
->options().plugins()->layout();
2206 gold_assert (layout
!= NULL
);
2208 Layout::Unique_segment_info
* s
= new Layout::Unique_segment_info
;
2209 s
->name
= segment_name
;
2213 for (unsigned int i
= 0; i
< num_sections
; ++i
)
2215 Object
* obj
= parameters
->options().plugins()->get_elf_object(
2216 section_list
[i
].handle
);
2217 if (obj
== NULL
|| obj
->is_dynamic())
2218 return LDPS_BAD_HANDLE
;
2219 unsigned int shndx
= section_list
[i
].shndx
;
2220 Const_section_id
secn_id(static_cast<Relobj
*>(obj
), shndx
);
2221 layout
->insert_section_segment_map(secn_id
, s
);
2227 // Register a new_input handler.
2229 static enum ld_plugin_status
2230 register_new_input(ld_plugin_new_input_handler handler
)
2232 gold_assert(parameters
->options().has_plugins());
2233 parameters
->options().plugins()->set_new_input_handler(handler
);
2237 #endif // ENABLE_PLUGINS
2239 // Allocate a Pluginobj object of the appropriate size and endianness.
2242 make_sized_plugin_object(const std::string
& filename
,
2243 Input_file
* input_file
, off_t offset
, off_t filesize
)
2245 Pluginobj
* obj
= NULL
;
2247 parameters_force_valid_target();
2248 const Target
& target(parameters
->target());
2250 if (target
.get_size() == 32)
2252 if (target
.is_big_endian())
2253 #ifdef HAVE_TARGET_32_BIG
2254 obj
= new Sized_pluginobj
<32, true>(filename
, input_file
,
2257 gold_error(_("%s: not configured to support "
2258 "32-bit big-endian object"),
2262 #ifdef HAVE_TARGET_32_LITTLE
2263 obj
= new Sized_pluginobj
<32, false>(filename
, input_file
,
2266 gold_error(_("%s: not configured to support "
2267 "32-bit little-endian object"),
2271 else if (target
.get_size() == 64)
2273 if (target
.is_big_endian())
2274 #ifdef HAVE_TARGET_64_BIG
2275 obj
= new Sized_pluginobj
<64, true>(filename
, input_file
,
2278 gold_error(_("%s: not configured to support "
2279 "64-bit big-endian object"),
2283 #ifdef HAVE_TARGET_64_LITTLE
2284 obj
= new Sized_pluginobj
<64, false>(filename
, input_file
,
2287 gold_error(_("%s: not configured to support "
2288 "64-bit little-endian object"),
2293 gold_assert(obj
!= NULL
);
2297 } // End namespace gold.