From b21318bd2c29fcca8f99c1de7facdaa5cb2e66e2 Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Thu, 11 May 2023 14:31:09 +0000 Subject: 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. --- bfd/plugin.c | 25 ++++++++++++++++++++----- bfd/plugin.h | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) (limited to 'bfd') diff --git a/bfd/plugin.c b/bfd/plugin.c index 66d286a..b798d34 100644 --- a/bfd/plugin.c +++ b/bfd/plugin.c @@ -129,6 +129,7 @@ struct plugin_list_entry { /* These must be initialized for each IR object with LTO wrapper. */ ld_plugin_claim_file_handler claim_file; + ld_plugin_claim_file_handler_v2 claim_file_v2; ld_plugin_all_symbols_read_handler all_symbols_read; ld_plugin_all_symbols_read_handler cleanup_handler; bool has_symbol_type; @@ -159,6 +160,16 @@ register_claim_file (ld_plugin_claim_file_handler handler) return LDPS_OK; } + +/* Register a claim-file handler, version 2. */ + +static enum ld_plugin_status +register_claim_file_v2 (ld_plugin_claim_file_handler_v2 handler) +{ + current_plugin->claim_file_v2 = handler; + return LDPS_OK; +} + static enum ld_plugin_status add_symbols (void * handle, int nsyms, @@ -337,7 +348,7 @@ try_load_plugin (const char *pname, bool build_list_p) { void *plugin_handle; - struct ld_plugin_tv tv[5]; + struct ld_plugin_tv tv[6]; int i; ld_plugin_onload onload; enum ld_plugin_status status; @@ -403,6 +414,10 @@ try_load_plugin (const char *pname, tv[i].tv_u.tv_register_claim_file = register_claim_file; ++i; + tv[i].tv_tag = LDPT_REGISTER_CLAIM_FILE_HOOK_V2; + tv[i].tv_u.tv_register_claim_file_v2 = register_claim_file_v2; + + ++i; tv[i].tv_tag = LDPT_ADD_SYMBOLS; tv[i].tv_u.tv_add_symbols = add_symbols; @@ -439,7 +454,7 @@ try_load_plugin (const char *pname, /* There may be plugin libraries in lib/bfd-plugins. */ static int has_plugin_list = -1; -static bfd_cleanup (*ld_plugin_object_p) (bfd *); +static bfd_cleanup (*ld_plugin_object_p) (bfd *, bool); static const char *plugin_name; @@ -463,7 +478,7 @@ bool bfd_link_plugin_object_p (bfd *abfd) { if (ld_plugin_object_p) - return ld_plugin_object_p (abfd) != NULL; + return ld_plugin_object_p (abfd, false) != NULL; return false; } @@ -480,7 +495,7 @@ bfd_plugin_target_p (const bfd_target *target) /* Register OBJECT_P to be used by bfd_plugin_object_p. */ void -register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *)) +register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *, bool)) { ld_plugin_object_p = object_p; } @@ -572,7 +587,7 @@ static bfd_cleanup bfd_plugin_object_p (bfd *abfd) { if (ld_plugin_object_p) - return ld_plugin_object_p (abfd); + return ld_plugin_object_p (abfd, false); if (abfd->plugin_format == bfd_plugin_unknown && !load_plugin (abfd)) return NULL; diff --git a/bfd/plugin.h b/bfd/plugin.h index 8da436a..6a6f0f1 100644 --- a/bfd/plugin.h +++ b/bfd/plugin.h @@ -27,7 +27,7 @@ void bfd_plugin_set_plugin (const char *); bool bfd_plugin_target_p (const bfd_target *); bool bfd_plugin_specified_p (void); bool bfd_link_plugin_object_p (bfd *); -void register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *)); +void register_ld_plugin_object_p (bfd_cleanup (*object_p) (bfd *, bool)); void bfd_plugin_close_file_descriptor (bfd *, int); typedef struct plugin_data_struct -- cgit v1.1