1 /* LTO plugin for linkers like gold, GNU ld or mold.
2 Copyright (C) 2009-2024 Free Software Foundation, Inc.
3 Contributed by Rafael Avila de Espindola (espindola@google.com).
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; see the file COPYING3. If not see
17 <http://www.gnu.org/licenses/>. */
19 /* The plugin has only one external function: onload. A linker passes it an array of
20 function that the plugin uses to communicate back to the linker.
22 With the functions provided by the linker, the plugin can be notified when
23 the linker first analyzes a file and pass a symbol table back to the linker. The plugin
24 is also notified when all symbols have been read and it is time to generate
25 machine code for the necessary symbols.
27 More information at http://gcc.gnu.org/wiki/whopr/driver.
29 This plugin should be passed the lto-wrapper options and will forward them.
30 It also has options at his own:
31 -debug: Print the command line used to run lto-wrapper.
32 -nop: Instead of running lto-wrapper, pass the original to the plugin. This
33 only works if the input files are hybrid.
34 -linker-output-known: Do not determine linker output
35 -linker-output-auto-nolto-rel: Switch from rel to nolto-rel mode without
36 warning. This is used on systems like VxWorks (kernel) where the link is
37 always partial and repeated incremental linking is generally not used.
38 -sym-style={none,win32,underscore|uscore}
57 #include <sys/types.h>
58 #if HAVE_PTHREAD_LOCKING
61 #ifdef HAVE_SYS_WAIT_H
65 #define WIFEXITED(S) (((S) & 0xff) == 0)
68 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
70 #include <libiberty.h>
72 #include "../gcc/lto/common.h"
73 #include "simple-object.h"
74 #include "plugin-api.h"
76 /* We need to use I64 instead of ll width-specifier on native Windows.
77 The reason for this is that older MS-runtimes don't support the ll. */
84 /* Handle opening elf files on hosts, such as Windows, that may use
85 text file handling that will break binary access. */
90 /* Segment name for LTO sections. This is only used for Mach-O.
91 FIXME: This needs to be kept in sync with darwin.c. */
93 #define LTO_SEGMENT_NAME "__GNU_LTO"
95 /* Return true if STR string starts with PREFIX. */
98 startswith (const char *str
, const char *prefix
)
100 return strncmp (str
, prefix
, strlen (prefix
)) == 0;
103 /* The part of the symbol table the plugin has to keep track of. Note that we
104 must keep SYMS until all_symbols_read is called to give the linker time to
105 copy the symbol information.
106 The id must be 64bit to minimze collisions. */
111 unsigned long long id
;
112 unsigned next_conflict
;
120 struct ld_plugin_symbol
*syms
;
121 unsigned long long id
;
124 /* Encapsulates object file data during symbol scan. */
125 struct plugin_objfile
129 simple_object_read
*objfile
;
130 struct plugin_symtab
*out
;
131 const struct ld_plugin_input_file
*file
;
134 /* All that we have to remember about a file. */
136 struct plugin_file_info
140 struct plugin_symtab symtab
;
141 struct plugin_symtab conflicts
;
145 /* List item with name of the file with offloading. */
147 struct plugin_offload_file
150 struct plugin_offload_file
*next
;
153 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
154 stdio file streams, we do simple label translation here. */
158 ss_none
, /* No underscore prefix. */
159 ss_win32
, /* Underscore prefix any symbol not beginning with '@'. */
160 ss_uscore
, /* Underscore prefix all symbols. */
163 #if HAVE_PTHREAD_LOCKING
165 static pthread_mutex_t plugin_lock
;
167 #define LOCK_SECTION pthread_mutex_lock (&plugin_lock)
168 #define UNLOCK_SECTION pthread_mutex_unlock (&plugin_lock)
171 #define UNLOCK_SECTION
174 static char *arguments_file_name
;
175 static ld_plugin_register_claim_file register_claim_file
;
176 static ld_plugin_register_claim_file_v2 register_claim_file_v2
;
177 static ld_plugin_register_all_symbols_read register_all_symbols_read
;
178 static ld_plugin_get_symbols get_symbols
, get_symbols_v2
, get_symbols_v3
;
179 static ld_plugin_register_cleanup register_cleanup
;
180 static ld_plugin_add_input_file add_input_file
;
181 static ld_plugin_add_input_library add_input_library
;
182 static ld_plugin_message message
;
183 static ld_plugin_add_symbols add_symbols
, add_symbols_v2
;
184 static ld_plugin_get_api_version get_api_version
;
186 /* By default, use version LAPI_V0 if there is not negotiation. */
187 static enum linker_api_version api_version
= LAPI_V0
;
189 static struct plugin_file_info
*claimed_files
= NULL
;
190 static unsigned int num_claimed_files
= 0;
191 static unsigned int non_claimed_files
= 0;
193 /* List of files with offloading. */
194 static struct plugin_offload_file
*offload_files
;
195 /* Last file in the list. */
196 static struct plugin_offload_file
*offload_files_last
;
197 /* Last non-archive file in the list. */
198 static struct plugin_offload_file
*offload_files_last_obj
;
199 /* Last LTO file in the list. */
200 static struct plugin_offload_file
*offload_files_last_lto
;
201 /* Total number of files with offloading. */
202 static unsigned num_offload_files
;
204 static char **output_files
= NULL
;
205 static unsigned int num_output_files
= 0;
207 static char **lto_wrapper_argv
;
208 static int lto_wrapper_num_args
;
210 static char **pass_through_items
= NULL
;
211 static unsigned int num_pass_through_items
;
213 static char *ltrans_objects
= NULL
;
216 static bool save_temps
;
219 static char *resolution_file
= NULL
;
220 static enum ld_plugin_output_file_type linker_output
;
221 static bool linker_output_set
;
222 static bool linker_output_known
;
223 static bool linker_output_auto_nolto_rel
;
224 static const char *link_output_name
= NULL
;
226 /* This indicates link_output_name already contains the dot of the
227 suffix, so we can skip it in extensions. */
228 static bool skip_in_suffix
= false;
230 /* The version of gold being used, or -1 if not gold. The number is
231 MAJOR * 100 + MINOR. */
232 static int gold_version
= -1;
234 /* Not used by default, but can be overridden at runtime
235 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
236 (in fact, only first letter of style arg is checked.) */
237 static enum symbol_style sym_style
= ss_none
;
240 check_1 (int gate
, enum ld_plugin_level level
, const char *text
)
246 message (level
, text
);
249 /* If there is no nicer way to inform the user, fallback to stderr. */
250 fprintf (stderr
, "%s\n", text
);
251 if (level
== LDPL_FATAL
)
256 /* This little wrapper allows check to be called with a non-integer
257 first argument, such as a pointer that must be non-NULL. We can't
258 use c99 bool type to coerce it into range, so we explicitly test. */
259 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
261 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
262 by P and the result is written in ENTRY. The slot number is stored in SLOT.
263 Returns the address of the next entry. */
266 parse_table_entry (char *p
, struct ld_plugin_symbol
*entry
,
270 enum ld_plugin_symbol_kind translate_kind
[] =
279 enum ld_plugin_symbol_visibility translate_visibility
[] =
292 /* cf. Duff's device. */
294 entry
->name
= xstrdup (p
);
299 entry
->name
= concat ("_", p
, NULL
);
302 check (0, LDPL_FATAL
, "invalid symbol style requested");
309 entry
->version
= NULL
;
311 entry
->comdat_key
= p
;
316 if (strlen (entry
->comdat_key
) == 0)
317 entry
->comdat_key
= NULL
;
319 entry
->comdat_key
= xstrdup (entry
->comdat_key
);
321 entry
->unused
= entry
->section_kind
= entry
->symbol_type
= 0;
324 check (t
<= 4, LDPL_FATAL
, "invalid symbol kind found");
325 entry
->def
= translate_kind
[t
];
329 check (t
<= 3, LDPL_FATAL
, "invalid symbol visibility found");
330 entry
->visibility
= translate_visibility
[t
];
333 memcpy (&entry
->size
, p
, sizeof (uint64_t));
336 memcpy (&aux
->slot
, p
, sizeof (uint32_t));
339 entry
->resolution
= LDPR_UNKNOWN
;
341 aux
->next_conflict
= -1;
346 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
347 by P and the result is written in ENTRY. The slot number is stored in SLOT.
348 Returns the address of the next entry. */
351 parse_table_entry_extension (char *p
, struct ld_plugin_symbol
*entry
)
354 enum ld_plugin_symbol_type symbol_types
[] =
362 check (t
<= 2, LDPL_FATAL
, "invalid symbol type found");
363 entry
->symbol_type
= symbol_types
[t
];
365 entry
->section_kind
= *p
;
372 /* Translate the IL symbol table located between DATA and END. Append the
373 slots and symbols to OUT. */
376 translate (char *data
, char *end
, struct plugin_symtab
*out
)
379 struct ld_plugin_symbol
*syms
= NULL
;
382 /* This overestimates the output buffer sizes, but at least
383 the algorithm is O(1) now. */
385 len
= (end
- data
)/8 + out
->nsyms
+ 1;
386 syms
= xrealloc (out
->syms
, len
* sizeof (struct ld_plugin_symbol
));
387 aux
= xrealloc (out
->aux
, len
* sizeof (struct sym_aux
));
389 for (n
= out
->nsyms
; data
< end
; n
++)
392 data
= parse_table_entry (data
, &syms
[n
], &aux
[n
]);
403 parse_symtab_extension (char *data
, char *end
, struct plugin_symtab
*out
)
406 unsigned char version
;
409 /* FIXME: Issue an error ? */
418 /* Version 1 contains the following data per entry:
423 unsigned long nsyms
= (end
- data
) / 2;
425 for (i
= 0; i
< nsyms
; i
++)
426 data
= parse_table_entry_extension (data
, out
->syms
+ i
+ out
->last_sym
);
428 out
->last_sym
+= nsyms
;
431 /* Free all memory that is no longer needed after writing the symbol
435 free_1 (struct plugin_file_info
*files
, unsigned num_files
)
438 for (i
= 0; i
< num_files
; i
++)
440 struct plugin_file_info
*info
= &files
[i
];
441 struct plugin_symtab
*symtab
= &info
->symtab
;
443 for (j
= 0; j
< symtab
->nsyms
; j
++)
445 struct ld_plugin_symbol
*s
= &symtab
->syms
[j
];
447 free (s
->comdat_key
);
454 /* Free all remaining memory. */
460 for (i
= 0; i
< num_claimed_files
; i
++)
462 struct plugin_file_info
*info
= &claimed_files
[i
];
463 struct plugin_symtab
*symtab
= &info
->symtab
;
468 for (i
= 0; i
< num_output_files
; i
++)
469 free (output_files
[i
]);
472 free (claimed_files
);
473 claimed_files
= NULL
;
474 num_claimed_files
= 0;
476 while (offload_files
)
478 struct plugin_offload_file
*ofld
= offload_files
;
479 offload_files
= offload_files
->next
;
482 num_offload_files
= 0;
484 free (arguments_file_name
);
485 arguments_file_name
= NULL
;
488 /* Dump SYMTAB to resolution file F. */
491 dump_symtab (FILE *f
, struct plugin_symtab
*symtab
)
495 for (j
= 0; j
< symtab
->nsyms
; j
++)
497 uint32_t slot
= symtab
->aux
[j
].slot
;
498 unsigned int resolution
= symtab
->syms
[j
].resolution
;
500 assert (resolution
!= LDPR_UNKNOWN
);
502 fprintf (f
, "%u %" PRI_LL
"x %s %s\n",
503 (unsigned int) slot
, symtab
->aux
[j
].id
,
504 lto_resolution_str
[resolution
],
505 symtab
->syms
[j
].name
);
509 /* Finish the conflicts' resolution information after the linker resolved
510 the original symbols */
513 finish_conflict_resolution (struct plugin_symtab
*symtab
,
514 struct plugin_symtab
*conflicts
)
518 if (conflicts
->nsyms
== 0)
521 for (i
= 0; i
< symtab
->nsyms
; i
++)
523 char resolution
= LDPR_UNKNOWN
;
525 if (symtab
->aux
[i
].next_conflict
== -1)
528 switch (symtab
->syms
[i
].def
)
531 case LDPK_COMMON
: /* ??? */
532 resolution
= LDPR_RESOLVED_IR
;
535 resolution
= LDPR_PREEMPTED_IR
;
539 resolution
= symtab
->syms
[i
].resolution
;
545 assert (resolution
!= LDPR_UNKNOWN
);
547 for (j
= symtab
->aux
[i
].next_conflict
;
549 j
= conflicts
->aux
[j
].next_conflict
)
550 conflicts
->syms
[j
].resolution
= resolution
;
554 /* Free symbol table SYMTAB. */
557 free_symtab (struct plugin_symtab
*symtab
)
565 /* Writes the relocations to disk. */
568 write_resolution (void)
570 unsigned int i
, included_files
= 0;
573 check (resolution_file
, LDPL_FATAL
, "resolution file not specified");
574 f
= fopen (resolution_file
, "w");
575 check (f
, LDPL_FATAL
, "could not open file");
577 for (i
= 0; i
< num_claimed_files
; i
++)
579 struct plugin_file_info
*info
= &claimed_files
[i
];
580 struct plugin_symtab
*symtab
= &info
->symtab
;
581 struct ld_plugin_symbol
*syms
= symtab
->syms
;
583 /* Version 2 of API supports IRONLY_EXP resolution that is
584 accepted by GCC-4.7 and newer.
585 Version 3 can return LDPS_NO_SYMS that means the object
586 will not be used at all. */
589 enum ld_plugin_status status
590 = get_symbols_v3 (info
->handle
, symtab
->nsyms
, syms
);
591 if (status
== LDPS_NO_SYMS
)
593 info
->skip_file
= true;
597 else if (get_symbols_v2
)
598 get_symbols_v2 (info
->handle
, symtab
->nsyms
, syms
);
600 get_symbols (info
->handle
, symtab
->nsyms
, syms
);
604 finish_conflict_resolution (symtab
, &info
->conflicts
);
607 fprintf (f
, "%d\n", included_files
);
609 for (i
= 0; i
< num_claimed_files
; i
++)
611 struct plugin_file_info
*info
= &claimed_files
[i
];
612 struct plugin_symtab
*symtab
= &info
->symtab
;
617 fprintf (f
, "%s %d\n", info
->name
, symtab
->nsyms
+ info
->conflicts
.nsyms
);
618 dump_symtab (f
, symtab
);
619 if (info
->conflicts
.nsyms
)
621 dump_symtab (f
, &info
->conflicts
);
622 free_symtab (&info
->conflicts
);
628 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
632 add_output_files (FILE *f
)
636 const unsigned piece
= 32;
637 char *buf
, *s
= xmalloc (piece
);
642 if (!fgets (buf
, piece
, f
))
648 if (s
[len
- 1] != '\n')
650 s
= xrealloc (s
, len
+ piece
);
658 = xrealloc (output_files
, num_output_files
* sizeof (char *));
659 output_files
[num_output_files
- 1] = s
;
660 add_input_file (output_files
[num_output_files
- 1]);
664 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
668 exec_lto_wrapper (char *argv
[])
674 FILE *wrapper_output
;
679 /* Write argv to a file to avoid a command line that is too long
680 Save the file locally on save-temps. */
681 const char *suffix
= ".lto_wrapper_args";
684 if (save_temps
&& link_output_name
)
685 arguments_file_name
= concat (link_output_name
, suffix
, NULL
);
687 arguments_file_name
= make_temp_file (".lto_wrapper_args");
688 check (arguments_file_name
, LDPL_FATAL
,
689 "Failed to generate a temorary file name");
691 args
= fopen (arguments_file_name
, "w");
692 check (args
, LDPL_FATAL
, "could not open arguments file");
694 t
= writeargv (&argv
[1], args
);
695 check (t
== 0, LDPL_FATAL
, "could not write arguments");
697 check (t
== 0, LDPL_FATAL
, "could not close arguments file");
699 at_args
= concat ("@", arguments_file_name
, NULL
);
700 check (at_args
, LDPL_FATAL
, "could not allocate");
702 for (i
= 1; argv
[i
]; i
++)
705 /* Check the input argument list for a verbose marker too. */
706 if (a
[0] == '-' && a
[1] == 'v' && a
[2] == '\0')
715 for (i
= 0; argv
[i
]; i
++)
716 fprintf (stderr
, "%s ", argv
[i
]);
717 fprintf (stderr
, "\n");
720 new_argv
[0] = argv
[0];
721 new_argv
[1] = at_args
;
726 for (i
= 0; new_argv
[i
]; i
++)
727 fprintf (stderr
, "%s ", new_argv
[i
]);
728 fprintf (stderr
, "\n");
731 pex
= pex_init (PEX_USE_PIPES
, "lto-wrapper", NULL
);
732 check (pex
!= NULL
, LDPL_FATAL
, "could not pex_init lto-wrapper");
734 errmsg
= pex_run (pex
, 0, new_argv
[0], new_argv
, NULL
, NULL
, &t
);
735 check (errmsg
== NULL
, LDPL_FATAL
, "could not run lto-wrapper");
736 check (t
== 0, LDPL_FATAL
, "could not run lto-wrapper");
738 wrapper_output
= pex_read_output (pex
, 0);
739 check (wrapper_output
, LDPL_FATAL
, "could not read lto-wrapper output");
741 add_output_files (wrapper_output
);
743 t
= pex_get_status (pex
, 1, &status
);
744 check (t
== 1, LDPL_FATAL
, "could not get lto-wrapper exit status");
745 check (WIFEXITED (status
) && WEXITSTATUS (status
) == 0, LDPL_FATAL
,
746 "lto-wrapper failed");
753 /* Pass the original files back to the linker. */
756 use_original_files (void)
759 for (i
= 0; i
< num_claimed_files
; i
++)
761 struct plugin_file_info
*info
= &claimed_files
[i
];
762 add_input_file (info
->name
);
767 /* Called by the linker once all symbols have been read. */
769 static enum ld_plugin_status
770 all_symbols_read_handler (void)
772 const unsigned num_lto_args
773 = num_claimed_files
+ lto_wrapper_num_args
+ 2
774 + !linker_output_known
+ !linker_output_auto_nolto_rel
;
777 const char *linker_output_str
= NULL
;
778 const char **lto_arg_ptr
;
779 if (num_claimed_files
+ num_offload_files
== 0)
784 use_original_files ();
790 FILE *objs
= fopen (ltrans_objects
, "r");
791 add_output_files (objs
);
796 lto_argv
= (char **) xcalloc (sizeof (char *), num_lto_args
);
797 lto_arg_ptr
= (const char **) lto_argv
;
798 assert (lto_wrapper_argv
);
802 free_1 (claimed_files
, num_claimed_files
);
804 for (i
= 0; i
< lto_wrapper_num_args
; i
++)
805 *lto_arg_ptr
++ = lto_wrapper_argv
[i
];
807 if (!linker_output_known
)
809 assert (linker_output_set
);
810 switch (linker_output
)
813 if (non_claimed_files
)
815 if (!linker_output_auto_nolto_rel
)
816 message (LDPL_WARNING
, "incremental linking of LTO and non-LTO"
817 " objects; using -flinker-output=nolto-rel which will"
818 " bypass whole program optimization");
819 linker_output_str
= "-flinker-output=nolto-rel";
822 linker_output_str
= "-flinker-output=rel";
825 linker_output_str
= "-flinker-output=dyn";
828 linker_output_str
= "-flinker-output=pie";
831 linker_output_str
= "-flinker-output=exec";
834 message (LDPL_FATAL
, "unsupported linker output %i", linker_output
);
837 *lto_arg_ptr
++ = xstrdup (linker_output_str
);
840 if (num_offload_files
> 0)
844 char *offload_objects_file_name
;
845 struct plugin_offload_file
*ofld
;
846 const char *suffix
= ".ofldlist";
848 if (save_temps
&& link_output_name
)
850 suffix
+= skip_in_suffix
;
851 offload_objects_file_name
= concat (link_output_name
, suffix
, NULL
);
854 offload_objects_file_name
= make_temp_file (suffix
);
855 check (offload_objects_file_name
, LDPL_FATAL
,
856 "Failed to generate a temporary file name");
857 f
= fopen (offload_objects_file_name
, "w");
858 check (f
, LDPL_FATAL
, "could not open file with offload objects");
859 fprintf (f
, "%u\n", num_offload_files
);
861 /* Skip the dummy item at the start of the list. */
862 ofld
= offload_files
->next
;
865 fprintf (f
, "%s\n", ofld
->name
);
870 arg
= concat ("-foffload-objects=", offload_objects_file_name
, NULL
);
871 check (arg
, LDPL_FATAL
, "could not allocate");
872 *lto_arg_ptr
++ = arg
;
875 for (i
= 0; i
< num_claimed_files
; i
++)
877 struct plugin_file_info
*info
= &claimed_files
[i
];
879 if (!info
->skip_file
)
880 *lto_arg_ptr
++ = info
->name
;
883 *lto_arg_ptr
++ = NULL
;
884 exec_lto_wrapper (lto_argv
);
888 /* --pass-through is not needed when using gold 1.11 or later. */
889 if (pass_through_items
&& gold_version
< 111)
892 for (i
= 0; i
< num_pass_through_items
; i
++)
894 if (startswith (pass_through_items
[i
], "-l"))
895 add_input_library (pass_through_items
[i
] + 2);
897 add_input_file (pass_through_items
[i
]);
898 free (pass_through_items
[i
]);
899 pass_through_items
[i
] = NULL
;
901 free (pass_through_items
);
902 pass_through_items
= NULL
;
908 /* Helper, as used in collect2. */
910 file_exists (const char *name
)
912 return access (name
, R_OK
) == 0;
915 /* Unlink FILE unless we have save-temps set.
916 Note that we're saving files if verbose output is set. */
919 maybe_unlink (const char *file
)
921 if (save_temps
&& file_exists (file
))
924 fprintf (stderr
, "[Leaving %s]\n", file
);
928 unlink_if_ordinary (file
);
931 /* Remove temporary files at the end of the link. */
933 static enum ld_plugin_status
934 cleanup_handler (void)
941 if (arguments_file_name
)
942 maybe_unlink (arguments_file_name
);
944 for (i
= 0; i
< num_output_files
; i
++)
945 maybe_unlink (output_files
[i
]);
951 #define SWAP(type, a, b) \
952 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
954 /* Compare two hash table entries */
956 static int eq_sym (const void *a
, const void *b
)
958 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
959 const struct ld_plugin_symbol
*bs
= (const struct ld_plugin_symbol
*)b
;
961 return !strcmp (as
->name
, bs
->name
);
966 static hashval_t
hash_sym (const void *a
)
968 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
970 return htab_hash_string (as
->name
);
973 /* Determine how strong a symbol is */
975 static int symbol_strength (struct ld_plugin_symbol
*s
)
989 /* In the ld -r case we can get dups in the LTO symbol tables, where
990 the same symbol can have different resolutions (e.g. undefined and defined).
992 We have to keep that in the LTO symbol tables, but the dups confuse
993 gold and then finally gcc by supplying incorrect resolutions.
995 Problem is that the main gold symbol table doesn't know about subids
996 and does not distingush the same symbols in different states.
998 So we drop duplicates from the linker visible symbol table
999 and keep them in a private table. Then later do own symbol
1000 resolution for the duplicated based on the results for the
1003 Then when writing out the resolution file readd the dropped symbols.
1005 XXX how to handle common? */
1008 resolve_conflicts (struct plugin_symtab
*t
, struct plugin_symtab
*conflicts
)
1010 htab_t symtab
= htab_create (t
->nsyms
, hash_sym
, eq_sym
, NULL
);
1016 conflicts
->syms
= xmalloc (sizeof (struct ld_plugin_symbol
) * outlen
);
1017 conflicts
->aux
= xmalloc (sizeof (struct sym_aux
) * outlen
);
1019 /* Move all duplicate symbols into the auxiliary conflicts table. */
1021 for (i
= 0; i
< t
->nsyms
; i
++)
1023 struct ld_plugin_symbol
*s
= &t
->syms
[i
];
1024 struct sym_aux
*aux
= &t
->aux
[i
];
1027 slot
= htab_find_slot (symtab
, s
, INSERT
);
1031 struct ld_plugin_symbol
*orig
= (struct ld_plugin_symbol
*)*slot
;
1032 struct sym_aux
*orig_aux
= &t
->aux
[orig
- t
->syms
];
1034 /* Always let the linker resolve the strongest symbol */
1035 if (symbol_strength (orig
) < symbol_strength (s
))
1037 SWAP (struct ld_plugin_symbol
, *orig
, *s
);
1038 SWAP (uint32_t, orig_aux
->slot
, aux
->slot
);
1039 SWAP (unsigned long long, orig_aux
->id
, aux
->id
);
1040 /* Don't swap conflict chain pointer */
1043 /* Move current symbol into the conflicts table */
1044 cnf
= conflicts
->nsyms
++;
1045 conflicts
->syms
[cnf
] = *s
;
1046 conflicts
->aux
[cnf
] = *aux
;
1047 aux
= &conflicts
->aux
[cnf
];
1049 /* Update conflicts chain of the original symbol */
1050 aux
->next_conflict
= orig_aux
->next_conflict
;
1051 orig_aux
->next_conflict
= cnf
;
1056 /* Remove previous duplicates in the main table */
1063 /* Put original into the hash table */
1064 *slot
= &t
->syms
[out
];
1068 assert (conflicts
->nsyms
<= outlen
);
1069 assert (conflicts
->nsyms
+ out
== t
->nsyms
);
1072 htab_delete (symtab
);
1075 /* Process one section of an object file. */
1078 process_symtab (void *data
, const char *name
, off_t offset
, off_t length
)
1080 struct plugin_objfile
*obj
= (struct plugin_objfile
*)data
;
1082 char *secdatastart
, *secdata
;
1084 if (!startswith (name
, ".gnu.lto_.symtab"))
1087 s
= strrchr (name
, '.');
1089 sscanf (s
, ".%" PRI_LL
"x", &obj
->out
->id
);
1090 secdata
= secdatastart
= xmalloc (length
);
1091 offset
+= obj
->file
->offset
;
1092 if (offset
!= lseek (obj
->file
->fd
, offset
, SEEK_SET
))
1097 ssize_t got
= read (obj
->file
->fd
, secdata
, length
);
1105 else if (errno
!= EINTR
)
1112 translate (secdatastart
, secdata
, obj
->out
);
1114 free (secdatastart
);
1119 message (LDPL_FATAL
, "%s: corrupt object file", obj
->file
->name
);
1120 /* Force claim_file_handler to abandon this file. */
1122 free (secdatastart
);
1126 /* Process one section of an object file. */
1129 process_symtab_extension (void *data
, const char *name
, off_t offset
,
1132 struct plugin_objfile
*obj
= (struct plugin_objfile
*)data
;
1134 char *secdatastart
, *secdata
;
1136 if (!startswith (name
, ".gnu.lto_.ext_symtab"))
1139 s
= strrchr (name
, '.');
1141 sscanf (s
, ".%" PRI_LL
"x", &obj
->out
->id
);
1142 secdata
= secdatastart
= xmalloc (length
);
1143 offset
+= obj
->file
->offset
;
1144 if (offset
!= lseek (obj
->file
->fd
, offset
, SEEK_SET
))
1149 ssize_t got
= read (obj
->file
->fd
, secdata
, length
);
1157 else if (errno
!= EINTR
)
1164 parse_symtab_extension (secdatastart
, secdata
, obj
->out
);
1166 free (secdatastart
);
1171 message (LDPL_FATAL
, "%s: corrupt object file", obj
->file
->name
);
1172 /* Force claim_file_handler to abandon this file. */
1174 free (secdatastart
);
1179 /* Find an offload section of an object file. */
1182 process_offload_section (void *data
, const char *name
, off_t offset
, off_t len
)
1184 if (startswith (name
, ".gnu.offload_lto_.opts"))
1186 struct plugin_objfile
*obj
= (struct plugin_objfile
*) data
;
1187 obj
->offload
= true;
1194 /* Callback used by a linker to check if the plugin can claim FILE.
1195 Writes the result in CAN_BE_CLAIMED. If KNOWN_USED != 0, the object
1196 is known by the linker to be included in link output, or an older API
1197 version is in use that does not provide that information. Otherwise,
1198 the linker is only determining whether this is a plugin object and
1199 only the symbol table is needed by the linker. In this case, the
1200 object should not be included in link output and this function will
1201 be called by the linker again with KNOWN_USED != 0 after the linker
1202 decides the object should be included in link output. */
1204 static enum ld_plugin_status
1205 claim_file_handler_v2 (const struct ld_plugin_input_file
*file
,
1206 int *can_be_claimed
, int known_used
)
1208 enum ld_plugin_status status
;
1209 struct plugin_objfile obj
;
1210 struct plugin_file_info lto_file
;
1214 memset (<o_file
, 0, sizeof (struct plugin_file_info
));
1216 if (file
->offset
!= 0)
1218 /* We pass the offset of the actual file, not the archive header.
1219 Can't use PRIx64, because that's C99, so we have to print the
1220 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1221 asprintf because asprintf doesn't work as expected on some older
1224 lo
= file
->offset
& 0xffffffff;
1225 hi
= ((int64_t)file
->offset
>> 32) & 0xffffffff;
1226 lto_file
.name
= hi
? xasprintf ("%s@0x%x%08x", file
->name
, hi
, lo
)
1227 : xasprintf ("%s@0x%x", file
->name
, lo
);
1231 lto_file
.name
= xstrdup (file
->name
);
1233 lto_file
.handle
= file
->handle
;
1235 *can_be_claimed
= 0;
1238 obj
.offload
= false;
1239 obj
.out
= <o_file
.symtab
;
1241 obj
.objfile
= simple_object_start_read (file
->fd
, file
->offset
, LTO_SEGMENT_NAME
,
1243 /* No file, but also no error code means unrecognized format; just skip it. */
1244 if (!obj
.objfile
&& !err
)
1249 errmsg
= simple_object_find_sections (obj
.objfile
, process_symtab
, &obj
,
1251 /* Parsing symtab extension should be done only for add_symbols_v2 and
1253 if (!errmsg
&& add_symbols_v2
!= NULL
)
1255 obj
.out
->last_sym
= 0;
1256 errmsg
= simple_object_find_sections (obj
.objfile
,
1257 process_symtab_extension
,
1262 if (!obj
.objfile
|| errmsg
)
1265 message (LDPL_FATAL
, "%s: %s: %s", file
->name
, errmsg
,
1268 message (LDPL_FATAL
, "%s: %s", file
->name
, errmsg
);
1273 simple_object_find_sections (obj
.objfile
, process_offload_section
,
1276 if (obj
.found
== 0 && !obj
.offload
)
1280 resolve_conflicts (<o_file
.symtab
, <o_file
.conflicts
);
1285 status
= add_symbols_v2 (file
->handle
, lto_file
.symtab
.nsyms
,
1286 lto_file
.symtab
.syms
);
1288 status
= add_symbols (file
->handle
, lto_file
.symtab
.nsyms
,
1289 lto_file
.symtab
.syms
);
1290 check (status
== LDPS_OK
, LDPL_FATAL
, "could not add symbols");
1292 /* Include it only if it is known to be used for link output. */
1296 num_claimed_files
++;
1298 xrealloc (claimed_files
,
1299 num_claimed_files
* sizeof (struct plugin_file_info
));
1300 claimed_files
[num_claimed_files
- 1] = lto_file
;
1304 *can_be_claimed
= 1;
1308 if (offload_files
== NULL
)
1310 /* Add dummy item to the start of the list. */
1311 offload_files
= xmalloc (sizeof (struct plugin_offload_file
));
1312 offload_files
->name
= NULL
;
1313 offload_files
->next
= NULL
;
1314 offload_files_last
= offload_files
;
1317 /* If this is an LTO file without offload, and it is the first LTO file, save
1318 the pointer to the last offload file in the list. Further offload LTO
1319 files will be inserted after it, if any. */
1320 if (*can_be_claimed
&& !obj
.offload
&& offload_files_last_lto
== NULL
)
1321 offload_files_last_lto
= offload_files_last
;
1323 if (obj
.offload
&& known_used
)
1325 /* Add file to the list. The order must be exactly the same as the final
1326 order after recompilation and linking, otherwise host and target tables
1327 with addresses wouldn't match. If a static library contains both LTO
1328 and non-LTO objects, ld and gold link them in a different order. */
1329 struct plugin_offload_file
*ofld
1330 = xmalloc (sizeof (struct plugin_offload_file
));
1331 ofld
->name
= lto_file
.name
;
1335 && offload_files_last_lto
== NULL
1336 && file
->offset
!= 0
1337 && gold_version
== -1)
1339 /* ld only: insert first LTO file from the archive after the last real
1340 object file immediately preceding the archive, or at the begin of
1341 the list if there was no real objects before archives. */
1342 if (offload_files_last_obj
!= NULL
)
1344 ofld
->next
= offload_files_last_obj
->next
;
1345 offload_files_last_obj
->next
= ofld
;
1349 ofld
->next
= offload_files
->next
;
1350 offload_files
->next
= ofld
;
1353 else if (*can_be_claimed
&& offload_files_last_lto
!= NULL
)
1355 /* Insert LTO file after the last LTO file in the list. */
1356 ofld
->next
= offload_files_last_lto
->next
;
1357 offload_files_last_lto
->next
= ofld
;
1360 /* Add non-LTO file or first non-archive LTO file to the end of the
1362 offload_files_last
->next
= ofld
;
1364 if (ofld
->next
== NULL
)
1365 offload_files_last
= ofld
;
1366 if (file
->offset
== 0)
1367 offload_files_last_obj
= ofld
;
1368 if (*can_be_claimed
)
1369 offload_files_last_lto
= ofld
;
1370 num_offload_files
++;
1379 non_claimed_files
++;
1381 free (lto_file
.name
);
1385 simple_object_release_read (obj
.objfile
);
1390 /* Callback used by a linker to check if the plugin will claim FILE. Writes
1391 the result in CLAIMED. */
1393 static enum ld_plugin_status
1394 claim_file_handler (const struct ld_plugin_input_file
*file
, int *claimed
)
1396 return claim_file_handler_v2 (file
, claimed
, true);
1399 /* Parse the plugin options. */
1402 process_option (const char *option
)
1404 if (strcmp (option
, "-linker-output-known") == 0)
1405 linker_output_known
= true;
1406 /* Also accept "notlo" for backwards compatibility. */
1407 else if ((strcmp (option
, "-linker-output-auto-nolto-rel") == 0)
1408 || (strcmp (option
, "-linker-output-auto-notlo-rel") == 0))
1409 linker_output_auto_nolto_rel
= true;
1410 else if (strcmp (option
, "-debug") == 0)
1412 else if ((strcmp (option
, "-v") == 0)
1413 || (strcmp (option
, "--verbose") == 0))
1415 else if (strcmp (option
, "-save-temps") == 0)
1417 else if (strcmp (option
, "-nop") == 0)
1419 else if (startswith (option
, "-pass-through="))
1421 num_pass_through_items
++;
1422 pass_through_items
= xrealloc (pass_through_items
,
1423 num_pass_through_items
* sizeof (char *));
1424 pass_through_items
[num_pass_through_items
- 1] =
1425 xstrdup (option
+ strlen ("-pass-through="));
1427 else if (startswith (option
, "-sym-style="))
1429 switch (option
[sizeof ("-sym-style=") - 1])
1432 sym_style
= ss_win32
;
1435 sym_style
= ss_uscore
;
1438 sym_style
= ss_none
;
1442 else if (startswith (option
, "-ltrans-objects="))
1443 ltrans_objects
= xstrdup (option
+ strlen ("-ltrans-objects="));
1447 char *opt
= xstrdup (option
);
1448 lto_wrapper_num_args
+= 1;
1449 size
= lto_wrapper_num_args
* sizeof (char *);
1450 lto_wrapper_argv
= (char **) xrealloc (lto_wrapper_argv
, size
);
1451 lto_wrapper_argv
[lto_wrapper_num_args
- 1] = opt
;
1452 if (startswith (option
, "-fresolution="))
1453 resolution_file
= opt
+ sizeof ("-fresolution=") - 1;
1455 save_temps
= save_temps
|| debug
;
1456 verbose
= verbose
|| debug
;
1459 /* Negotiate linker API version. */
1462 negotiate_api_version (void)
1464 const char *linker_identifier
;
1465 const char *linker_version
;
1467 enum linker_api_version supported_api
= LAPI_V0
;
1468 #if HAVE_PTHREAD_LOCKING
1469 supported_api
= LAPI_V1
;
1472 api_version
= get_api_version ("GCC", BASE_VERSION
, LAPI_V0
,
1473 supported_api
, &linker_identifier
, &linker_version
);
1474 if (api_version
> supported_api
)
1476 fprintf (stderr
, "requested an unsupported API version (%d)\n", api_version
);
1480 switch (api_version
)
1485 check (get_symbols_v3
, LDPL_FATAL
,
1486 "get_symbols_v3 required for API version 1");
1487 check (add_symbols_v2
, LDPL_FATAL
,
1488 "add_symbols_v2 required for API version 1");
1491 fprintf (stderr
, "unsupported API version (%d)\n", api_version
);
1496 /* Called by a linker after loading the plugin. TV is the transfer vector. */
1498 enum ld_plugin_status
1499 onload (struct ld_plugin_tv
*tv
)
1501 struct ld_plugin_tv
*p
;
1502 enum ld_plugin_status status
;
1504 #if HAVE_PTHREAD_LOCKING
1505 if (pthread_mutex_init (&plugin_lock
, NULL
) != 0)
1507 fprintf (stderr
, "mutex init failed\n");
1518 message
= p
->tv_u
.tv_message
;
1520 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
1521 register_claim_file
= p
->tv_u
.tv_register_claim_file
;
1523 case LDPT_REGISTER_CLAIM_FILE_HOOK_V2
:
1524 register_claim_file_v2
= p
->tv_u
.tv_register_claim_file_v2
;
1526 case LDPT_ADD_SYMBOLS_V2
:
1527 add_symbols_v2
= p
->tv_u
.tv_add_symbols
;
1529 case LDPT_ADD_SYMBOLS
:
1530 add_symbols
= p
->tv_u
.tv_add_symbols
;
1532 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
1533 register_all_symbols_read
= p
->tv_u
.tv_register_all_symbols_read
;
1535 case LDPT_GET_SYMBOLS_V3
:
1536 get_symbols_v3
= p
->tv_u
.tv_get_symbols
;
1538 case LDPT_GET_SYMBOLS_V2
:
1539 get_symbols_v2
= p
->tv_u
.tv_get_symbols
;
1541 case LDPT_GET_SYMBOLS
:
1542 get_symbols
= p
->tv_u
.tv_get_symbols
;
1544 case LDPT_REGISTER_CLEANUP_HOOK
:
1545 register_cleanup
= p
->tv_u
.tv_register_cleanup
;
1547 case LDPT_ADD_INPUT_FILE
:
1548 add_input_file
= p
->tv_u
.tv_add_input_file
;
1550 case LDPT_ADD_INPUT_LIBRARY
:
1551 add_input_library
= p
->tv_u
.tv_add_input_library
;
1554 process_option (p
->tv_u
.tv_string
);
1556 case LDPT_GOLD_VERSION
:
1557 gold_version
= p
->tv_u
.tv_val
;
1559 case LDPT_LINKER_OUTPUT
:
1560 linker_output
= (enum ld_plugin_output_file_type
) p
->tv_u
.tv_val
;
1561 linker_output_set
= true;
1563 case LDPT_OUTPUT_NAME
:
1564 /* We only use this to make user-friendly temp file names. */
1565 link_output_name
= p
->tv_u
.tv_string
;
1567 case LDPT_GET_API_VERSION
:
1568 get_api_version
= p
->tv_u
.tv_get_api_version
;
1576 if (get_api_version
)
1577 negotiate_api_version ();
1579 check (register_claim_file
, LDPL_FATAL
, "register_claim_file not found");
1580 check (add_symbols
, LDPL_FATAL
, "add_symbols not found");
1581 status
= register_claim_file (claim_file_handler
);
1582 check (status
== LDPS_OK
, LDPL_FATAL
,
1583 "could not register the claim_file callback");
1585 if (register_claim_file_v2
)
1587 status
= register_claim_file_v2 (claim_file_handler_v2
);
1588 check (status
== LDPS_OK
, LDPL_FATAL
,
1589 "could not register the claim_file_v2 callback");
1592 if (register_cleanup
)
1594 status
= register_cleanup (cleanup_handler
);
1595 check (status
== LDPS_OK
, LDPL_FATAL
,
1596 "could not register the cleanup callback");
1599 if (register_all_symbols_read
)
1601 check (get_symbols
, LDPL_FATAL
, "get_symbols not found");
1602 status
= register_all_symbols_read (all_symbols_read_handler
);
1603 check (status
== LDPS_OK
, LDPL_FATAL
,
1604 "could not register the all_symbols_read callback");
1607 char *collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1608 if (collect_gcc_options
)
1610 /* Support -fno-use-linker-plugin by failing to load the plugin
1611 for the case where it is auto-loaded by BFD. */
1612 if (strstr (collect_gcc_options
, "'-fno-use-linker-plugin'"))
1615 if (strstr (collect_gcc_options
, "'-save-temps'"))
1618 if (strstr (collect_gcc_options
, "'-v'")
1619 || strstr (collect_gcc_options
, "'--verbose'"))
1623 if ((p
= strstr (collect_gcc_options
, "'-dumpdir'")))
1625 p
+= sizeof ("'-dumpdir'");
1628 const char *start
= p
;
1629 int ticks
= 0, escapes
= 0;
1630 /* Count ticks (') and escaped (\.) characters. Stop at the
1631 end of the options or at a blank after an even number of
1632 ticks (not counting escaped ones. */
1633 for (p
= start
; *p
; p
++)
1640 else if ((ticks
% 2) != 0)
1654 /* Now allocate a new link_output_name and decode dumpdir
1655 into it. The loop uses the same logic, except it counts
1656 ticks and escapes backwards (so ticks is adjusted if we
1657 find an odd number of them), and it copies characters
1658 that are escaped or not otherwise skipped. */
1659 int len
= p
- start
- ticks
- escapes
+ 1;
1660 char *q
= xmalloc (len
);
1661 link_output_name
= q
;
1662 int oddticks
= (ticks
% 2);
1664 for (p
= start
; *p
; p
++)
1671 else if ((ticks
% 2) != 0)
1686 assert (escapes
== 0);
1687 assert (ticks
== oddticks
);
1688 assert (q
- link_output_name
== len
- 1);
1689 skip_in_suffix
= true;