Update LDPT_REGISTER_CLAIM_FILE_HOOK_V2 linker plugin hook

This hook allows the BFD 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 including the unused LTO archive
members in link output.  To get the proper support for archives with LTO
common symbols, the linker fix

commit a6f8fe0a9e9cbe871652e46ba7c22d5e9fb86208
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Aug 14 20:50:02 2024 -0700

    lto: Don't include unused LTO archive members in output

is required.

	PR lto/116361
	* lto-plugin.c (claim_file_handler_v2): Rename claimed to
	can_be_claimed.  Include the LTO object only if it is known to
	be included in link output.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
This commit is contained in:
H.J. Lu 2024-08-21 07:25:25 -07:00
parent b07f8a3011
commit a98dd536b1
1 changed files with 31 additions and 22 deletions

View File

@ -1191,16 +1191,19 @@ process_offload_section (void *data, const char *name, off_t offset, off_t len)
return 1; return 1;
} }
/* Callback used by a linker to check if the plugin will claim FILE. Writes /* Callback used by a linker to check if the plugin can claim FILE.
the result in CLAIMED. If KNOWN_USED, the object is known by the linker Writes the result in CAN_BE_CLAIMED. If KNOWN_USED != 0, the object
to be used, or an older API version is in use that does not provide that is known by the linker to be included in link output, or an older API
information; otherwise, the linker is only determining whether this is version is in use that does not provide that information. Otherwise,
a plugin object and it should not be registered as having offload data if the linker is only determining whether this is a plugin object and
not claimed by the plugin. */ only the symbol table is needed by the linker. In this case, the
object should not be included in link output and this function will
be called by the linker again with KNOWN_USED != 0 after the linker
decides the object should be included in link output. */
static enum ld_plugin_status static enum ld_plugin_status
claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed, claim_file_handler_v2 (const struct ld_plugin_input_file *file,
int known_used) int *can_be_claimed, int known_used)
{ {
enum ld_plugin_status status; enum ld_plugin_status status;
struct plugin_objfile obj; struct plugin_objfile obj;
@ -1229,7 +1232,7 @@ claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
} }
lto_file.handle = file->handle; lto_file.handle = file->handle;
*claimed = 0; *can_be_claimed = 0;
obj.file = file; obj.file = file;
obj.found = 0; obj.found = 0;
obj.offload = false; obj.offload = false;
@ -1286,6 +1289,9 @@ claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
lto_file.symtab.syms); lto_file.symtab.syms);
check (status == LDPS_OK, LDPL_FATAL, "could not add symbols"); check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
/* Include it only if it is known to be used for link output. */
if (known_used)
{
LOCK_SECTION; LOCK_SECTION;
num_claimed_files++; num_claimed_files++;
claimed_files = claimed_files =
@ -1293,8 +1299,9 @@ claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
num_claimed_files * sizeof (struct plugin_file_info)); num_claimed_files * sizeof (struct plugin_file_info));
claimed_files[num_claimed_files - 1] = lto_file; claimed_files[num_claimed_files - 1] = lto_file;
UNLOCK_SECTION; UNLOCK_SECTION;
}
*claimed = 1; *can_be_claimed = 1;
} }
LOCK_SECTION; LOCK_SECTION;
@ -1310,10 +1317,10 @@ claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
/* If this is an LTO file without offload, and it is the first LTO file, save /* If this is an LTO file without offload, and it is the first LTO file, save
the pointer to the last offload file in the list. Further offload LTO the pointer to the last offload file in the list. Further offload LTO
files will be inserted after it, if any. */ files will be inserted after it, if any. */
if (*claimed && !obj.offload && offload_files_last_lto == NULL) if (*can_be_claimed && !obj.offload && offload_files_last_lto == NULL)
offload_files_last_lto = offload_files_last; offload_files_last_lto = offload_files_last;
if (obj.offload && (known_used || obj.found > 0)) if (obj.offload && known_used && obj.found > 0)
{ {
/* Add file to the list. The order must be exactly the same as the final /* Add file to the list. The order must be exactly the same as the final
order after recompilation and linking, otherwise host and target tables order after recompilation and linking, otherwise host and target tables
@ -1324,7 +1331,9 @@ claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
ofld->name = lto_file.name; ofld->name = lto_file.name;
ofld->next = NULL; ofld->next = NULL;
if (*claimed && offload_files_last_lto == NULL && file->offset != 0 if (*can_be_claimed
&& offload_files_last_lto == NULL
&& file->offset != 0
&& gold_version == -1) && gold_version == -1)
{ {
/* ld only: insert first LTO file from the archive after the last real /* ld only: insert first LTO file from the archive after the last real
@ -1341,7 +1350,7 @@ claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
offload_files->next = ofld; offload_files->next = ofld;
} }
} }
else if (*claimed && offload_files_last_lto != NULL) else if (*can_be_claimed && offload_files_last_lto != NULL)
{ {
/* Insert LTO file after the last LTO file in the list. */ /* Insert LTO file after the last LTO file in the list. */
ofld->next = offload_files_last_lto->next; ofld->next = offload_files_last_lto->next;
@ -1356,7 +1365,7 @@ claim_file_handler_v2 (const struct ld_plugin_input_file *file, int *claimed,
offload_files_last = ofld; offload_files_last = ofld;
if (file->offset == 0) if (file->offset == 0)
offload_files_last_obj = ofld; offload_files_last_obj = ofld;
if (*claimed) if (*can_be_claimed)
offload_files_last_lto = ofld; offload_files_last_lto = ofld;
num_offload_files++; num_offload_files++;
} }