diff options
author | Joseph Myers <joseph@codesourcery.com> | 2023-05-11 14:31:09 +0000 |
---|---|---|
committer | Joseph Myers <joseph@codesourcery.com> | 2023-05-11 14:31:09 +0000 |
commit | b21318bd2c29fcca8f99c1de7facdaa5cb2e66e2 (patch) | |
tree | e1c2baacc959a91de68e4625ec356c85ea929c5a /ld/plugin.c | |
parent | 38b95a529385e32adc39428231c6fc8b0e8d6f21 (diff) |
Add LDPT_REGISTER_CLAIM_FILE_HOOK_V2 linker plugin hook [GCC PR109128]
This is one part of the fix for GCC PR109128, along with a
corresponding GCC change. Without this patch, what happens in the
linker, when an unused object in a .a file has offload data, is that
elf_link_is_defined_archive_symbol calls bfd_link_plugin_object_p,
which ends up calling the plugin's claim_file_handler, which then
records the object as one with offload data. That is, the linker never
decides to use the object in the first place, but use of this _p
interface (called as part of trying to decide whether to use the
object) results in the plugin deciding to use its offload data (and a
consequent mismatch in the offload data present at runtime).
The new hook allows the linker plugin to distinguish calls to
claim_file_handler that know the object is being used by the linker
(from ldmain.c:add_archive_element), from calls that don't know it's
being used by the linker (from elf_link_is_defined_archive_symbol); in
the latter case, the plugin should avoid recording the object as one
with offload data.
bfd/
* plugin.c (struct plugin_list_entry): Add claim_file_v2.
(register_claim_file_v2): New.
(try_load_plugin): Use LDPT_REGISTER_CLAIM_FILE_HOOK_V2.
(ld_plugin_object_p): Take second argument.
(bfd_link_plugin_object_p): Update call to ld_plugin_object_p.
(register_ld_plugin_object_p): Update argument prototype.
(bfd_plugin_object_p): Update call to ld_plugin_object_p.
* plugin.h (register_ld_plugin_object_p): Update argument
prototype.
include/
* plugin.api.h (ld_plugin_claim_file_handler_v2)
(ld_plugin_register_claim_file_v2)
(LDPT_REGISTER_CLAIM_FILE_HOOK_V2): New.
(struct ld_plugin_tv): Add tv_register_claim_file_v2.
ld/
* plugin.c (struct plugin): Add claim_file_handler_v2.
(LDPT_REGISTER_CLAIM_FILE_HOOK_V2): New.
(plugin_object_p): Add second argument. Update call to
plugin_call_claim_file.
(register_claim_file_v2): New.
(set_tv_header): Handle LDPT_REGISTER_CLAIM_FILE_HOOK_V2.
(plugin_call_claim_file): Add argument known_used.
(plugin_maybe_claim): Update call to plugin_object_p.
* testplug.c, testplug2.c, testplug3.c, testplug4.c: Handle
LDPT_REGISTER_CLAIM_FILE_HOOK_V2.
* testsuite/ld-plugin/plugin-1.d, testsuite/ld-plugin/plugin-10.d,
testsuite/ld-plugin/plugin-11.d, testsuite/ld-plugin/plugin-13.d,
testsuite/ld-plugin/plugin-14.d, testsuite/ld-plugin/plugin-15.d,
testsuite/ld-plugin/plugin-16.d, testsuite/ld-plugin/plugin-17.d,
testsuite/ld-plugin/plugin-18.d, testsuite/ld-plugin/plugin-19.d,
testsuite/ld-plugin/plugin-2.d, testsuite/ld-plugin/plugin-26.d,
testsuite/ld-plugin/plugin-3.d, testsuite/ld-plugin/plugin-30.d,
testsuite/ld-plugin/plugin-4.d, testsuite/ld-plugin/plugin-5.d,
testsuite/ld-plugin/plugin-6.d, testsuite/ld-plugin/plugin-7.d,
testsuite/ld-plugin/plugin-8.d, testsuite/ld-plugin/plugin-9.d:
Update test expectations.
Diffstat (limited to 'ld/plugin.c')
-rw-r--r-- | ld/plugin.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/ld/plugin.c b/ld/plugin.c index 34aefc584cc..970cf566ffe 100644 --- a/ld/plugin.c +++ b/ld/plugin.c @@ -87,6 +87,7 @@ typedef struct plugin size_t n_args; /* The plugin's event handlers. */ ld_plugin_claim_file_handler claim_file_handler; + ld_plugin_claim_file_handler_v2 claim_file_handler_v2; ld_plugin_all_symbols_read_handler all_symbols_read_handler; ld_plugin_cleanup_handler cleanup_handler; /* TRUE if the cleanup handlers have been called. */ @@ -159,6 +160,7 @@ static const enum ld_plugin_tag tv_header_tags[] = LDPT_LINKER_OUTPUT, LDPT_OUTPUT_NAME, LDPT_REGISTER_CLAIM_FILE_HOOK, + LDPT_REGISTER_CLAIM_FILE_HOOK_V2, LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK, LDPT_REGISTER_CLEANUP_HOOK, LDPT_ADD_SYMBOLS, @@ -181,7 +183,7 @@ static bool plugin_notice (struct bfd_link_info *, struct bfd_link_hash_entry *, bfd *, asection *, bfd_vma, flagword); -static bfd_cleanup plugin_object_p (bfd *); +static bfd_cleanup plugin_object_p (bfd *, bool); #if !defined (HAVE_DLFCN_H) && defined (HAVE_WINDOWS_H) @@ -467,6 +469,15 @@ register_claim_file (ld_plugin_claim_file_handler handler) return LDPS_OK; } +/* Register a claim-file version 2 handler. */ +static enum ld_plugin_status +register_claim_file_v2 (ld_plugin_claim_file_handler_v2 handler) +{ + ASSERT (called_plugin); + called_plugin->claim_file_handler_v2 = handler; + return LDPS_OK; +} + /* Register an all-symbols-read handler. */ static enum ld_plugin_status register_all_symbols_read (ld_plugin_all_symbols_read_handler handler) @@ -1019,6 +1030,9 @@ set_tv_header (struct ld_plugin_tv *tv) case LDPT_REGISTER_CLAIM_FILE_HOOK: TVU(register_claim_file) = register_claim_file; break; + case LDPT_REGISTER_CLAIM_FILE_HOOK_V2: + TVU(register_claim_file_v2) = register_claim_file_v2; + break; case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK: TVU(register_all_symbols_read) = register_all_symbols_read; break; @@ -1144,7 +1158,8 @@ plugin_load_plugins (void) /* Call 'claim file' hook for all plugins. */ static int -plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed) +plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed, + bool known_used) { plugin_t *curplug = plugins_list; *claimed = false; @@ -1155,7 +1170,10 @@ plugin_call_claim_file (const struct ld_plugin_input_file *file, int *claimed) enum ld_plugin_status rv; called_plugin = curplug; - rv = (*curplug->claim_file_handler) (file, claimed); + if (curplug->claim_file_handler_v2) + rv = (*curplug->claim_file_handler_v2) (file, claimed, known_used); + else + rv = (*curplug->claim_file_handler) (file, claimed); called_plugin = NULL; if (rv != LDPS_OK) set_plugin_error (curplug->name); @@ -1187,7 +1205,7 @@ plugin_cleanup (bfd *abfd ATTRIBUTE_UNUSED) } static bfd_cleanup -plugin_object_p (bfd *ibfd) +plugin_object_p (bfd *ibfd, bool known_used) { int claimed; plugin_input_file_t *input; @@ -1239,7 +1257,7 @@ plugin_object_p (bfd *ibfd) claimed = 0; - if (plugin_call_claim_file (&file, &claimed)) + if (plugin_call_claim_file (&file, &claimed, known_used)) einfo (_("%F%P: %s: plugin reported error claiming file\n"), plugin_error_plugin ()); @@ -1294,7 +1312,7 @@ void plugin_maybe_claim (lang_input_statement_type *entry) { ASSERT (entry->header.type == lang_input_statement_enum); - if (plugin_object_p (entry->the_bfd)) + if (plugin_object_p (entry->the_bfd, true)) { bfd *abfd = entry->the_bfd->plugin_dummy_bfd; |