From a6f8fe0a9e9cbe871652e46ba7c22d5e9fb86208 Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Wed, 14 Aug 2024 20:50:02 -0700 Subject: lto: Don't include unused LTO archive members in output When plugin_object_p is called by elf_link_is_defined_archive_symbol to check if a symbol in archive is a real definition, set archive member plugin_format to bfd_plugin_yes_unused to avoid including the unused LTO archive members in linker output. When plugin_object_p is called as known used, call plugin claim_file if plugin_format is bfd_plugin_unknown or bfd_plugin_yes_unused. To get the proper support for archives with LTO common symbols with GCC, the GCC fix for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116361 is needed. bfd/ PR ld/32083 * archures.c (bfd_arch_get_compatible): Treat bfd_plugin_yes_unused the same as bfd_plugin_yes. * elflink.c (elf_link_is_defined_archive_symbol): Likewise. * bfd.c (bfd_plugin_format): Add bfd_plugin_yes_unused. * plugin.c (try_claim): Try claim_file_v2 first. * bfd-in2.h: Regenerated. ld/ PR ld/32083 * plugin.c (plugin_call_claim_file): Add an argument to return if LDPT_REGISTER_CLAIM_FILE_HOOK_V2 is used. (plugin_object_p): When KNOWN_USED is false, we call plugin claim_file if plugin_format is bfd_plugin_unknown and set plugin_format to bfd_plugin_yes_unused on LTO object. When KNOWN_USED is true, we call plugin claim_file if plugin_format is bfd_plugin_unknown or bfd_plugin_yes_unused. Signed-off-by: H.J. Lu --- bfd/archures.c | 1 + bfd/bfd-in2.h | 3 ++- bfd/bfd.c | 3 ++- bfd/elflink.c | 1 + bfd/plugin.c | 22 ++++++++++++++++------ 5 files changed, 22 insertions(+), 8 deletions(-) (limited to 'bfd') diff --git a/bfd/archures.c b/bfd/archures.c index 94118b8..c4decc5 100644 --- a/bfd/archures.c +++ b/bfd/archures.c @@ -947,6 +947,7 @@ bfd_arch_get_compatible (const bfd *abfd, to assume that they know what they are doing. */ if (accept_unknowns || ubfd->plugin_format == bfd_plugin_yes + || ubfd->plugin_format == bfd_plugin_yes_unused || strcmp (bfd_get_target (ubfd), "binary") == 0) return kbfd->arch_info; return NULL; diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h index e3b5a8b..40ec416 100644 --- a/bfd/bfd-in2.h +++ b/bfd/bfd-in2.h @@ -1947,7 +1947,8 @@ enum bfd_plugin_format { bfd_plugin_unknown = 0, bfd_plugin_yes = 1, - bfd_plugin_no = 2 + bfd_plugin_yes_unused = 2, + bfd_plugin_no = 3 }; struct bfd_build_id diff --git a/bfd/bfd.c b/bfd/bfd.c index ae79c64..a93be10 100644 --- a/bfd/bfd.c +++ b/bfd/bfd.c @@ -65,7 +65,8 @@ EXTERNAL . { . bfd_plugin_unknown = 0, . bfd_plugin_yes = 1, -. bfd_plugin_no = 2 +. bfd_plugin_yes_unused = 2, +. bfd_plugin_no = 3 . }; . .struct bfd_build_id diff --git a/bfd/elflink.c b/bfd/elflink.c index ed0bd71..9eb1122 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -3653,6 +3653,7 @@ elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) object file is an IR object, give linker LTO plugin a chance to get the correct symbol table. */ if (abfd->plugin_format == bfd_plugin_yes + || abfd->plugin_format == bfd_plugin_yes_unused #if BFD_SUPPORTS_PLUGINS || (abfd->plugin_format == bfd_plugin_unknown && bfd_link_plugin_object_p (abfd)) diff --git a/bfd/plugin.c b/bfd/plugin.c index 026654f..f6c6fdb 100644 --- a/bfd/plugin.c +++ b/bfd/plugin.c @@ -329,13 +329,23 @@ try_claim (bfd *abfd) struct ld_plugin_input_file file; file.handle = abfd; - if (bfd_plugin_open_input (abfd, &file) - && current_plugin->claim_file) + if (bfd_plugin_open_input (abfd, &file)) { - current_plugin->claim_file (&file, &claimed); - bfd_plugin_close_file_descriptor ((abfd->my_archive != NULL - ? abfd : NULL), - file.fd); + bool claim_file_called = false; + if (current_plugin->claim_file_v2) + { + current_plugin->claim_file_v2 (&file, &claimed, false); + claim_file_called = true; + } + else if (current_plugin->claim_file) + { + current_plugin->claim_file (&file, &claimed); + claim_file_called = true; + } + if (claim_file_called) + bfd_plugin_close_file_descriptor ((abfd->my_archive != NULL + ? abfd : NULL), + file.fd); } return claimed; -- cgit v1.1