1 /* LTO plugin for gold and/or GNU ld.
2 Copyright (C) 2009-2020 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. Gold passes it an array of
20 function that the plugin uses to communicate back to gold.
22 With the functions provided by gold, the plugin can be notified when
23 gold first analyzes a file and pass a symbol table back to gold. 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-notlo-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 #ifdef HAVE_SYS_WAIT_H
62 #define WIFEXITED(S) (((S) & 0xff) == 0)
65 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
67 #include <libiberty.h>
69 #include "../gcc/lto/common.h"
70 #include "simple-object.h"
71 #include "plugin-api.h"
73 /* We need to use I64 instead of ll width-specifier on native Windows.
74 The reason for this is that older MS-runtimes don't support the ll. */
81 /* Handle opening elf files on hosts, such as Windows, that may use
82 text file handling that will break binary access. */
87 /* Segment name for LTO sections. This is only used for Mach-O.
88 FIXME: This needs to be kept in sync with darwin.c. */
90 #define LTO_SEGMENT_NAME "__GNU_LTO"
92 /* LTO magic section name. */
94 #define LTO_SYMTAB_PREFIX ".gnu.lto_.symtab"
95 #define LTO_SYMTAB_PREFIX_LEN (sizeof (LTO_SYMTAB_PREFIX) - 1)
96 #define LTO_SYMTAB_EXT_PREFIX ".gnu.lto_.ext_symtab"
97 #define LTO_SYMTAB_EXT_PREFIX_LEN (sizeof (LTO_SYMTAB_EXT_PREFIX) - 1)
98 #define LTO_LTO_PREFIX ".gnu.lto_.lto"
99 #define LTO_LTO_PREFIX_LEN (sizeof (LTO_LTO_PREFIX) - 1)
100 #define OFFLOAD_SECTION ".gnu.offload_lto_.opts"
101 #define OFFLOAD_SECTION_LEN (sizeof (OFFLOAD_SECTION) - 1)
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
;
144 /* List item with name of the file with offloading. */
146 struct plugin_offload_file
149 struct plugin_offload_file
*next
;
152 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
153 stdio file streams, we do simple label translation here. */
157 ss_none
, /* No underscore prefix. */
158 ss_win32
, /* Underscore prefix any symbol not beginning with '@'. */
159 ss_uscore
, /* Underscore prefix all symbols. */
162 static char *arguments_file_name
;
163 static ld_plugin_register_claim_file register_claim_file
;
164 static ld_plugin_register_all_symbols_read register_all_symbols_read
;
165 static ld_plugin_get_symbols get_symbols
, get_symbols_v2
;
166 static ld_plugin_register_cleanup register_cleanup
;
167 static ld_plugin_add_input_file add_input_file
;
168 static ld_plugin_add_input_library add_input_library
;
169 static ld_plugin_message message
;
170 static ld_plugin_add_symbols add_symbols
, add_symbols_v2
;
172 static struct plugin_file_info
*claimed_files
= NULL
;
173 static unsigned int num_claimed_files
= 0;
174 static unsigned int non_claimed_files
= 0;
176 /* List of files with offloading. */
177 static struct plugin_offload_file
*offload_files
;
178 /* Last file in the list. */
179 static struct plugin_offload_file
*offload_files_last
;
180 /* Last non-archive file in the list. */
181 static struct plugin_offload_file
*offload_files_last_obj
;
182 /* Last LTO file in the list. */
183 static struct plugin_offload_file
*offload_files_last_lto
;
184 /* Total number of files with offloading. */
185 static unsigned num_offload_files
;
187 static char **output_files
= NULL
;
188 static unsigned int num_output_files
= 0;
190 static char **lto_wrapper_argv
;
191 static int lto_wrapper_num_args
;
193 static char **pass_through_items
= NULL
;
194 static unsigned int num_pass_through_items
;
197 static bool save_temps
;
200 static char *resolution_file
= NULL
;
201 static enum ld_plugin_output_file_type linker_output
;
202 static bool linker_output_set
;
203 static bool linker_output_known
;
204 static bool linker_output_auto_nolto_rel
;
205 static const char *link_output_name
= NULL
;
207 /* This indicates link_output_name already contains the dot of the
208 suffix, so we can skip it in extensions. */
209 static int skip_in_suffix
= 0;
211 /* The version of gold being used, or -1 if not gold. The number is
212 MAJOR * 100 + MINOR. */
213 static int gold_version
= -1;
215 /* Not used by default, but can be overridden at runtime
216 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
217 (in fact, only first letter of style arg is checked.) */
218 static enum symbol_style sym_style
= ss_none
;
221 check_1 (int gate
, enum ld_plugin_level level
, const char *text
)
227 message (level
, text
);
230 /* If there is no nicer way to inform the user, fallback to stderr. */
231 fprintf (stderr
, "%s\n", text
);
232 if (level
== LDPL_FATAL
)
237 /* This little wrapper allows check to be called with a non-integer
238 first argument, such as a pointer that must be non-NULL. We can't
239 use c99 bool type to coerce it into range, so we explicitly test. */
240 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
242 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
243 by P and the result is written in ENTRY. The slot number is stored in SLOT.
244 Returns the address of the next entry. */
247 parse_table_entry (char *p
, struct ld_plugin_symbol
*entry
,
251 enum ld_plugin_symbol_kind translate_kind
[] =
260 enum ld_plugin_symbol_visibility translate_visibility
[] =
273 /* cf. Duff's device. */
275 entry
->name
= xstrdup (p
);
280 entry
->name
= concat ("_", p
, NULL
);
283 check (0, LDPL_FATAL
, "invalid symbol style requested");
290 entry
->version
= NULL
;
292 entry
->comdat_key
= p
;
297 if (strlen (entry
->comdat_key
) == 0)
298 entry
->comdat_key
= NULL
;
300 entry
->comdat_key
= xstrdup (entry
->comdat_key
);
302 entry
->unused
= entry
->section_kind
= entry
->symbol_type
= 0;
305 check (t
<= 4, LDPL_FATAL
, "invalid symbol kind found");
306 entry
->def
= translate_kind
[t
];
310 check (t
<= 3, LDPL_FATAL
, "invalid symbol visibility found");
311 entry
->visibility
= translate_visibility
[t
];
314 memcpy (&entry
->size
, p
, sizeof (uint64_t));
317 memcpy (&aux
->slot
, p
, sizeof (uint32_t));
320 entry
->resolution
= LDPR_UNKNOWN
;
322 aux
->next_conflict
= -1;
327 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
328 by P and the result is written in ENTRY. The slot number is stored in SLOT.
329 Returns the address of the next entry. */
332 parse_table_entry_extension (char *p
, struct ld_plugin_symbol
*entry
)
335 enum ld_plugin_symbol_type symbol_types
[] =
343 check (t
<= 2, LDPL_FATAL
, "invalid symbol type found");
344 entry
->symbol_type
= symbol_types
[t
];
346 entry
->section_kind
= *p
;
353 /* Translate the IL symbol table located between DATA and END. Append the
354 slots and symbols to OUT. */
357 translate (char *data
, char *end
, struct plugin_symtab
*out
)
360 struct ld_plugin_symbol
*syms
= NULL
;
363 /* This overestimates the output buffer sizes, but at least
364 the algorithm is O(1) now. */
366 len
= (end
- data
)/8 + out
->nsyms
+ 1;
367 syms
= xrealloc (out
->syms
, len
* sizeof (struct ld_plugin_symbol
));
368 aux
= xrealloc (out
->aux
, len
* sizeof (struct sym_aux
));
370 for (n
= out
->nsyms
; data
< end
; n
++)
373 data
= parse_table_entry (data
, &syms
[n
], &aux
[n
]);
384 parse_symtab_extension (char *data
, char *end
, struct plugin_symtab
*out
)
387 unsigned char version
;
390 /* FIXME: Issue an error ? */
399 /* Version 1 contains the following data per entry:
404 unsigned long nsyms
= (end
- data
) / 2;
406 for (i
= 0; i
< nsyms
; i
++)
407 data
= parse_table_entry_extension (data
, out
->syms
+ i
+ out
->last_sym
);
409 out
->last_sym
+= nsyms
;
412 /* Free all memory that is no longer needed after writing the symbol
416 free_1 (struct plugin_file_info
*files
, unsigned num_files
)
419 for (i
= 0; i
< num_files
; i
++)
421 struct plugin_file_info
*info
= &files
[i
];
422 struct plugin_symtab
*symtab
= &info
->symtab
;
424 for (j
= 0; j
< symtab
->nsyms
; j
++)
426 struct ld_plugin_symbol
*s
= &symtab
->syms
[j
];
428 free (s
->comdat_key
);
435 /* Free all remaining memory. */
441 for (i
= 0; i
< num_claimed_files
; i
++)
443 struct plugin_file_info
*info
= &claimed_files
[i
];
444 struct plugin_symtab
*symtab
= &info
->symtab
;
449 for (i
= 0; i
< num_output_files
; i
++)
450 free (output_files
[i
]);
453 free (claimed_files
);
454 claimed_files
= NULL
;
455 num_claimed_files
= 0;
457 while (offload_files
)
459 struct plugin_offload_file
*ofld
= offload_files
;
460 offload_files
= offload_files
->next
;
463 num_offload_files
= 0;
465 free (arguments_file_name
);
466 arguments_file_name
= NULL
;
469 /* Dump SYMTAB to resolution file F. */
472 dump_symtab (FILE *f
, struct plugin_symtab
*symtab
)
476 for (j
= 0; j
< symtab
->nsyms
; j
++)
478 uint32_t slot
= symtab
->aux
[j
].slot
;
479 unsigned int resolution
= symtab
->syms
[j
].resolution
;
481 assert (resolution
!= LDPR_UNKNOWN
);
483 fprintf (f
, "%u %" PRI_LL
"x %s %s\n",
484 (unsigned int) slot
, symtab
->aux
[j
].id
,
485 lto_resolution_str
[resolution
],
486 symtab
->syms
[j
].name
);
490 /* Finish the conflicts' resolution information after the linker resolved
491 the original symbols */
494 finish_conflict_resolution (struct plugin_symtab
*symtab
,
495 struct plugin_symtab
*conflicts
)
499 if (conflicts
->nsyms
== 0)
502 for (i
= 0; i
< symtab
->nsyms
; i
++)
504 char resolution
= LDPR_UNKNOWN
;
506 if (symtab
->aux
[i
].next_conflict
== -1)
509 switch (symtab
->syms
[i
].def
)
512 case LDPK_COMMON
: /* ??? */
513 resolution
= LDPR_RESOLVED_IR
;
516 resolution
= LDPR_PREEMPTED_IR
;
520 resolution
= symtab
->syms
[i
].resolution
;
526 assert (resolution
!= LDPR_UNKNOWN
);
528 for (j
= symtab
->aux
[i
].next_conflict
;
530 j
= conflicts
->aux
[j
].next_conflict
)
531 conflicts
->syms
[j
].resolution
= resolution
;
535 /* Free symbol table SYMTAB. */
538 free_symtab (struct plugin_symtab
*symtab
)
546 /* Writes the relocations to disk. */
549 write_resolution (void)
554 check (resolution_file
, LDPL_FATAL
, "resolution file not specified");
555 f
= fopen (resolution_file
, "w");
556 check (f
, LDPL_FATAL
, "could not open file");
558 fprintf (f
, "%d\n", num_claimed_files
);
560 for (i
= 0; i
< num_claimed_files
; i
++)
562 struct plugin_file_info
*info
= &claimed_files
[i
];
563 struct plugin_symtab
*symtab
= &info
->symtab
;
564 struct ld_plugin_symbol
*syms
= symtab
->syms
;
566 /* Version 2 of API supports IRONLY_EXP resolution that is
567 accepted by GCC-4.7 and newer. */
569 get_symbols_v2 (info
->handle
, symtab
->nsyms
, syms
);
571 get_symbols (info
->handle
, symtab
->nsyms
, syms
);
573 finish_conflict_resolution (symtab
, &info
->conflicts
);
575 fprintf (f
, "%s %d\n", info
->name
, symtab
->nsyms
+ info
->conflicts
.nsyms
);
576 dump_symtab (f
, symtab
);
577 if (info
->conflicts
.nsyms
)
579 dump_symtab (f
, &info
->conflicts
);
580 free_symtab (&info
->conflicts
);
586 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
590 add_output_files (FILE *f
)
594 const unsigned piece
= 32;
595 char *buf
, *s
= xmalloc (piece
);
600 if (!fgets (buf
, piece
, f
))
606 if (s
[len
- 1] != '\n')
608 s
= xrealloc (s
, len
+ piece
);
616 = xrealloc (output_files
, num_output_files
* sizeof (char *));
617 output_files
[num_output_files
- 1] = s
;
618 add_input_file (output_files
[num_output_files
- 1]);
622 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
626 exec_lto_wrapper (char *argv
[])
632 FILE *wrapper_output
;
637 /* Write argv to a file to avoid a command line that is too long
638 Save the file locally on save-temps. */
639 if (save_temps
&& link_output_name
)
640 arguments_file_name
= concat (link_output_name
,
642 + skip_in_suffix
, NULL
);
644 arguments_file_name
= make_temp_file (".lto_wrapper_args");
645 check (arguments_file_name
, LDPL_FATAL
,
646 "Failed to generate a temorary file name");
648 args
= fopen (arguments_file_name
, "w");
649 check (args
, LDPL_FATAL
, "could not open arguments file");
651 t
= writeargv (&argv
[1], args
);
652 check (t
== 0, LDPL_FATAL
, "could not write arguments");
654 check (t
== 0, LDPL_FATAL
, "could not close arguments file");
656 at_args
= concat ("@", arguments_file_name
, NULL
);
657 check (at_args
, LDPL_FATAL
, "could not allocate");
659 for (i
= 1; argv
[i
]; i
++)
662 /* Check the input argument list for a verbose marker too. */
663 if (a
[0] == '-' && a
[1] == 'v' && a
[2] == '\0')
672 for (i
= 0; argv
[i
]; i
++)
673 fprintf (stderr
, "%s ", argv
[i
]);
674 fprintf (stderr
, "\n");
677 new_argv
[0] = argv
[0];
678 new_argv
[1] = at_args
;
683 for (i
= 0; new_argv
[i
]; i
++)
684 fprintf (stderr
, "%s ", new_argv
[i
]);
685 fprintf (stderr
, "\n");
688 pex
= pex_init (PEX_USE_PIPES
, "lto-wrapper", NULL
);
689 check (pex
!= NULL
, LDPL_FATAL
, "could not pex_init lto-wrapper");
691 errmsg
= pex_run (pex
, 0, new_argv
[0], new_argv
, NULL
, NULL
, &t
);
692 check (errmsg
== NULL
, LDPL_FATAL
, "could not run lto-wrapper");
693 check (t
== 0, LDPL_FATAL
, "could not run lto-wrapper");
695 wrapper_output
= pex_read_output (pex
, 0);
696 check (wrapper_output
, LDPL_FATAL
, "could not read lto-wrapper output");
698 add_output_files (wrapper_output
);
700 t
= pex_get_status (pex
, 1, &status
);
701 check (t
== 1, LDPL_FATAL
, "could not get lto-wrapper exit status");
702 check (WIFEXITED (status
) && WEXITSTATUS (status
) == 0, LDPL_FATAL
,
703 "lto-wrapper failed");
710 /* Pass the original files back to the linker. */
713 use_original_files (void)
716 for (i
= 0; i
< num_claimed_files
; i
++)
718 struct plugin_file_info
*info
= &claimed_files
[i
];
719 add_input_file (info
->name
);
724 /* Called by the linker once all symbols have been read. */
726 static enum ld_plugin_status
727 all_symbols_read_handler (void)
729 const unsigned num_lto_args
730 = num_claimed_files
+ lto_wrapper_num_args
+ 2
731 + !linker_output_known
+ !linker_output_auto_nolto_rel
;
734 const char *linker_output_str
= NULL
;
735 const char **lto_arg_ptr
;
736 if (num_claimed_files
+ num_offload_files
== 0)
741 use_original_files ();
745 lto_argv
= (char **) xcalloc (sizeof (char *), num_lto_args
);
746 lto_arg_ptr
= (const char **) lto_argv
;
747 assert (lto_wrapper_argv
);
751 free_1 (claimed_files
, num_claimed_files
);
753 for (i
= 0; i
< lto_wrapper_num_args
; i
++)
754 *lto_arg_ptr
++ = lto_wrapper_argv
[i
];
756 if (!linker_output_known
)
758 assert (linker_output_set
);
759 switch (linker_output
)
762 if (non_claimed_files
)
764 if (!linker_output_auto_nolto_rel
)
765 message (LDPL_WARNING
, "incremental linking of LTO and non-LTO"
766 " objects; using -flinker-output=nolto-rel which will"
767 " bypass whole program optimization");
768 linker_output_str
= "-flinker-output=nolto-rel";
771 linker_output_str
= "-flinker-output=rel";
774 linker_output_str
= "-flinker-output=dyn";
777 linker_output_str
= "-flinker-output=pie";
780 linker_output_str
= "-flinker-output=exec";
783 message (LDPL_FATAL
, "unsupported linker output %i", linker_output
);
786 *lto_arg_ptr
++ = xstrdup (linker_output_str
);
789 if (num_offload_files
> 0)
793 char *offload_objects_file_name
;
794 struct plugin_offload_file
*ofld
;
796 offload_objects_file_name
= make_temp_file (".ofldlist");
797 check (offload_objects_file_name
, LDPL_FATAL
,
798 "Failed to generate a temporary file name");
799 f
= fopen (offload_objects_file_name
, "w");
800 check (f
, LDPL_FATAL
, "could not open file with offload objects");
801 fprintf (f
, "%u\n", num_offload_files
);
803 /* Skip the dummy item at the start of the list. */
804 ofld
= offload_files
->next
;
807 fprintf (f
, "%s\n", ofld
->name
);
812 arg
= concat ("-foffload-objects=", offload_objects_file_name
, NULL
);
813 check (arg
, LDPL_FATAL
, "could not allocate");
814 *lto_arg_ptr
++ = arg
;
817 for (i
= 0; i
< num_claimed_files
; i
++)
819 struct plugin_file_info
*info
= &claimed_files
[i
];
821 *lto_arg_ptr
++ = info
->name
;
824 *lto_arg_ptr
++ = NULL
;
825 exec_lto_wrapper (lto_argv
);
829 /* --pass-through is not needed when using gold 1.11 or later. */
830 if (pass_through_items
&& gold_version
< 111)
833 for (i
= 0; i
< num_pass_through_items
; i
++)
835 if (strncmp (pass_through_items
[i
], "-l", 2) == 0)
836 add_input_library (pass_through_items
[i
] + 2);
838 add_input_file (pass_through_items
[i
]);
839 free (pass_through_items
[i
]);
840 pass_through_items
[i
] = NULL
;
842 free (pass_through_items
);
843 pass_through_items
= NULL
;
849 /* Helper, as used in collect2. */
851 file_exists (const char *name
)
853 return access (name
, R_OK
) == 0;
856 /* Unlink FILE unless we have save-temps set.
857 Note that we're saving files if verbose output is set. */
860 maybe_unlink (const char *file
)
862 if (save_temps
&& file_exists (file
))
865 fprintf (stderr
, "[Leaving %s]\n", file
);
869 unlink_if_ordinary (file
);
872 /* Remove temporary files at the end of the link. */
874 static enum ld_plugin_status
875 cleanup_handler (void)
882 if (arguments_file_name
)
883 maybe_unlink (arguments_file_name
);
885 for (i
= 0; i
< num_output_files
; i
++)
886 maybe_unlink (output_files
[i
]);
892 #define SWAP(type, a, b) \
893 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
895 /* Compare two hash table entries */
897 static int eq_sym (const void *a
, const void *b
)
899 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
900 const struct ld_plugin_symbol
*bs
= (const struct ld_plugin_symbol
*)b
;
902 return !strcmp (as
->name
, bs
->name
);
907 static hashval_t
hash_sym (const void *a
)
909 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
911 return htab_hash_string (as
->name
);
914 /* Determine how strong a symbol is */
916 static int symbol_strength (struct ld_plugin_symbol
*s
)
930 /* In the ld -r case we can get dups in the LTO symbol tables, where
931 the same symbol can have different resolutions (e.g. undefined and defined).
933 We have to keep that in the LTO symbol tables, but the dups confuse
934 gold and then finally gcc by supplying incorrect resolutions.
936 Problem is that the main gold symbol table doesn't know about subids
937 and does not distingush the same symbols in different states.
939 So we drop duplicates from the linker visible symbol table
940 and keep them in a private table. Then later do own symbol
941 resolution for the duplicated based on the results for the
944 Then when writing out the resolution file readd the dropped symbols.
946 XXX how to handle common? */
949 resolve_conflicts (struct plugin_symtab
*t
, struct plugin_symtab
*conflicts
)
951 htab_t symtab
= htab_create (t
->nsyms
, hash_sym
, eq_sym
, NULL
);
957 conflicts
->syms
= xmalloc (sizeof (struct ld_plugin_symbol
) * outlen
);
958 conflicts
->aux
= xmalloc (sizeof (struct sym_aux
) * outlen
);
960 /* Move all duplicate symbols into the auxiliary conflicts table. */
962 for (i
= 0; i
< t
->nsyms
; i
++)
964 struct ld_plugin_symbol
*s
= &t
->syms
[i
];
965 struct sym_aux
*aux
= &t
->aux
[i
];
968 slot
= htab_find_slot (symtab
, s
, INSERT
);
972 struct ld_plugin_symbol
*orig
= (struct ld_plugin_symbol
*)*slot
;
973 struct sym_aux
*orig_aux
= &t
->aux
[orig
- t
->syms
];
975 /* Always let the linker resolve the strongest symbol */
976 if (symbol_strength (orig
) < symbol_strength (s
))
978 SWAP (struct ld_plugin_symbol
, *orig
, *s
);
979 SWAP (uint32_t, orig_aux
->slot
, aux
->slot
);
980 SWAP (unsigned long long, orig_aux
->id
, aux
->id
);
981 /* Don't swap conflict chain pointer */
984 /* Move current symbol into the conflicts table */
985 cnf
= conflicts
->nsyms
++;
986 conflicts
->syms
[cnf
] = *s
;
987 conflicts
->aux
[cnf
] = *aux
;
988 aux
= &conflicts
->aux
[cnf
];
990 /* Update conflicts chain of the original symbol */
991 aux
->next_conflict
= orig_aux
->next_conflict
;
992 orig_aux
->next_conflict
= cnf
;
997 /* Remove previous duplicates in the main table */
1004 /* Put original into the hash table */
1005 *slot
= &t
->syms
[out
];
1009 assert (conflicts
->nsyms
<= outlen
);
1010 assert (conflicts
->nsyms
+ out
== t
->nsyms
);
1013 htab_delete (symtab
);
1016 /* Process one section of an object file. */
1019 process_symtab (void *data
, const char *name
, off_t offset
, off_t length
)
1021 struct plugin_objfile
*obj
= (struct plugin_objfile
*)data
;
1023 char *secdatastart
, *secdata
;
1025 if (strncmp (name
, LTO_SYMTAB_PREFIX
, LTO_SYMTAB_PREFIX_LEN
) != 0)
1028 s
= strrchr (name
, '.');
1030 sscanf (s
, ".%" PRI_LL
"x", &obj
->out
->id
);
1031 secdata
= secdatastart
= xmalloc (length
);
1032 offset
+= obj
->file
->offset
;
1033 if (offset
!= lseek (obj
->file
->fd
, offset
, SEEK_SET
))
1038 ssize_t got
= read (obj
->file
->fd
, secdata
, length
);
1046 else if (errno
!= EINTR
)
1053 translate (secdatastart
, secdata
, obj
->out
);
1055 free (secdatastart
);
1060 message (LDPL_FATAL
, "%s: corrupt object file", obj
->file
->name
);
1061 /* Force claim_file_handler to abandon this file. */
1063 free (secdatastart
);
1067 /* Process one section of an object file. */
1070 process_symtab_extension (void *data
, const char *name
, off_t offset
,
1073 struct plugin_objfile
*obj
= (struct plugin_objfile
*)data
;
1075 char *secdatastart
, *secdata
;
1077 if (strncmp (name
, LTO_SYMTAB_EXT_PREFIX
, LTO_SYMTAB_EXT_PREFIX_LEN
) != 0)
1080 s
= strrchr (name
, '.');
1082 sscanf (s
, ".%" PRI_LL
"x", &obj
->out
->id
);
1083 secdata
= secdatastart
= xmalloc (length
);
1084 offset
+= obj
->file
->offset
;
1085 if (offset
!= lseek (obj
->file
->fd
, offset
, SEEK_SET
))
1090 ssize_t got
= read (obj
->file
->fd
, secdata
, length
);
1098 else if (errno
!= EINTR
)
1105 parse_symtab_extension (secdatastart
, secdata
, obj
->out
);
1107 free (secdatastart
);
1112 message (LDPL_FATAL
, "%s: corrupt object file", obj
->file
->name
);
1113 /* Force claim_file_handler to abandon this file. */
1115 free (secdatastart
);
1120 /* Find an offload section of an object file. */
1123 process_offload_section (void *data
, const char *name
, off_t offset
, off_t len
)
1125 if (!strncmp (name
, OFFLOAD_SECTION
, OFFLOAD_SECTION_LEN
))
1127 struct plugin_objfile
*obj
= (struct plugin_objfile
*) data
;
1135 /* Callback used by gold to check if the plugin will claim FILE. Writes
1136 the result in CLAIMED. */
1138 static enum ld_plugin_status
1139 claim_file_handler (const struct ld_plugin_input_file
*file
, int *claimed
)
1141 enum ld_plugin_status status
;
1142 struct plugin_objfile obj
;
1143 struct plugin_file_info lto_file
;
1147 memset (<o_file
, 0, sizeof (struct plugin_file_info
));
1149 if (file
->offset
!= 0)
1151 /* We pass the offset of the actual file, not the archive header.
1152 Can't use PRIx64, because that's C99, so we have to print the
1153 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1154 asprintf because asprintf doesn't work as expected on some older
1157 lo
= file
->offset
& 0xffffffff;
1158 hi
= ((int64_t)file
->offset
>> 32) & 0xffffffff;
1159 lto_file
.name
= hi
? xasprintf ("%s@0x%x%08x", file
->name
, hi
, lo
)
1160 : xasprintf ("%s@0x%x", file
->name
, lo
);
1164 lto_file
.name
= xstrdup (file
->name
);
1166 lto_file
.handle
= file
->handle
;
1172 obj
.out
= <o_file
.symtab
;
1174 obj
.objfile
= simple_object_start_read (file
->fd
, file
->offset
, LTO_SEGMENT_NAME
,
1176 /* No file, but also no error code means unrecognized format; just skip it. */
1177 if (!obj
.objfile
&& !err
)
1182 errmsg
= simple_object_find_sections (obj
.objfile
, process_symtab
, &obj
,
1184 /* Parsing symtab extension should be done only for add_symbols_v2 and
1186 if (!errmsg
&& add_symbols_v2
!= NULL
)
1188 obj
.out
->last_sym
= 0;
1189 errmsg
= simple_object_find_sections (obj
.objfile
,
1190 process_symtab_extension
,
1195 if (!obj
.objfile
|| errmsg
)
1198 message (LDPL_FATAL
, "%s: %s: %s", file
->name
, errmsg
,
1201 message (LDPL_FATAL
, "%s: %s", file
->name
, errmsg
);
1206 simple_object_find_sections (obj
.objfile
, process_offload_section
,
1209 if (obj
.found
== 0 && obj
.offload
== 0)
1213 resolve_conflicts (<o_file
.symtab
, <o_file
.conflicts
);
1218 status
= add_symbols_v2 (file
->handle
, lto_file
.symtab
.nsyms
,
1219 lto_file
.symtab
.syms
);
1221 status
= add_symbols (file
->handle
, lto_file
.symtab
.nsyms
,
1222 lto_file
.symtab
.syms
);
1223 check (status
== LDPS_OK
, LDPL_FATAL
, "could not add symbols");
1225 num_claimed_files
++;
1227 xrealloc (claimed_files
,
1228 num_claimed_files
* sizeof (struct plugin_file_info
));
1229 claimed_files
[num_claimed_files
- 1] = lto_file
;
1234 if (offload_files
== NULL
)
1236 /* Add dummy item to the start of the list. */
1237 offload_files
= xmalloc (sizeof (struct plugin_offload_file
));
1238 offload_files
->name
= NULL
;
1239 offload_files
->next
= NULL
;
1240 offload_files_last
= offload_files
;
1243 /* If this is an LTO file without offload, and it is the first LTO file, save
1244 the pointer to the last offload file in the list. Further offload LTO
1245 files will be inserted after it, if any. */
1246 if (*claimed
&& obj
.offload
== 0 && offload_files_last_lto
== NULL
)
1247 offload_files_last_lto
= offload_files_last
;
1249 if (obj
.offload
== 1)
1251 /* Add file to the list. The order must be exactly the same as the final
1252 order after recompilation and linking, otherwise host and target tables
1253 with addresses wouldn't match. If a static library contains both LTO
1254 and non-LTO objects, ld and gold link them in a different order. */
1255 struct plugin_offload_file
*ofld
1256 = xmalloc (sizeof (struct plugin_offload_file
));
1257 ofld
->name
= lto_file
.name
;
1260 if (*claimed
&& offload_files_last_lto
== NULL
&& file
->offset
!= 0
1261 && gold_version
== -1)
1263 /* ld only: insert first LTO file from the archive after the last real
1264 object file immediately preceding the archive, or at the begin of
1265 the list if there was no real objects before archives. */
1266 if (offload_files_last_obj
!= NULL
)
1268 ofld
->next
= offload_files_last_obj
->next
;
1269 offload_files_last_obj
->next
= ofld
;
1273 ofld
->next
= offload_files
->next
;
1274 offload_files
->next
= ofld
;
1277 else if (*claimed
&& offload_files_last_lto
!= NULL
)
1279 /* Insert LTO file after the last LTO file in the list. */
1280 ofld
->next
= offload_files_last_lto
->next
;
1281 offload_files_last_lto
->next
= ofld
;
1284 /* Add non-LTO file or first non-archive LTO file to the end of the
1286 offload_files_last
->next
= ofld
;
1288 if (ofld
->next
== NULL
)
1289 offload_files_last
= ofld
;
1290 if (file
->offset
== 0)
1291 offload_files_last_obj
= ofld
;
1293 offload_files_last_lto
= ofld
;
1294 num_offload_files
++;
1300 non_claimed_files
++;
1301 free (lto_file
.name
);
1305 simple_object_release_read (obj
.objfile
);
1310 /* Parse the plugin options. */
1313 process_option (const char *option
)
1315 if (strcmp (option
, "-linker-output-known") == 0)
1316 linker_output_known
= true;
1317 else if (strcmp (option
, "-linker-output-auto-notlo-rel") == 0)
1318 linker_output_auto_nolto_rel
= true;
1319 else if (strcmp (option
, "-debug") == 0)
1321 else if ((strcmp (option
, "-v") == 0)
1322 || (strcmp (option
, "--verbose") == 0))
1324 else if (strcmp (option
, "-save-temps") == 0)
1326 else if (strcmp (option
, "-nop") == 0)
1328 else if (!strncmp (option
, "-pass-through=", strlen("-pass-through=")))
1330 num_pass_through_items
++;
1331 pass_through_items
= xrealloc (pass_through_items
,
1332 num_pass_through_items
* sizeof (char *));
1333 pass_through_items
[num_pass_through_items
- 1] =
1334 xstrdup (option
+ strlen ("-pass-through="));
1336 else if (!strncmp (option
, "-sym-style=", sizeof ("-sym-style=") - 1))
1338 switch (option
[sizeof ("-sym-style=") - 1])
1341 sym_style
= ss_win32
;
1344 sym_style
= ss_uscore
;
1347 sym_style
= ss_none
;
1354 char *opt
= xstrdup (option
);
1355 lto_wrapper_num_args
+= 1;
1356 size
= lto_wrapper_num_args
* sizeof (char *);
1357 lto_wrapper_argv
= (char **) xrealloc (lto_wrapper_argv
, size
);
1358 lto_wrapper_argv
[lto_wrapper_num_args
- 1] = opt
;
1359 if (strncmp (option
, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1360 resolution_file
= opt
+ sizeof ("-fresolution=") - 1;
1362 save_temps
= save_temps
|| debug
;
1363 verbose
= verbose
|| debug
;
1366 /* Called by gold after loading the plugin. TV is the transfer vector. */
1368 enum ld_plugin_status
1369 onload (struct ld_plugin_tv
*tv
)
1371 struct ld_plugin_tv
*p
;
1372 enum ld_plugin_status status
;
1380 message
= p
->tv_u
.tv_message
;
1382 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
1383 register_claim_file
= p
->tv_u
.tv_register_claim_file
;
1385 case LDPT_ADD_SYMBOLS_V2
:
1386 add_symbols_v2
= p
->tv_u
.tv_add_symbols
;
1388 case LDPT_ADD_SYMBOLS
:
1389 add_symbols
= p
->tv_u
.tv_add_symbols
;
1391 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
1392 register_all_symbols_read
= p
->tv_u
.tv_register_all_symbols_read
;
1394 case LDPT_GET_SYMBOLS_V2
:
1395 get_symbols_v2
= p
->tv_u
.tv_get_symbols
;
1397 case LDPT_GET_SYMBOLS
:
1398 get_symbols
= p
->tv_u
.tv_get_symbols
;
1400 case LDPT_REGISTER_CLEANUP_HOOK
:
1401 register_cleanup
= p
->tv_u
.tv_register_cleanup
;
1403 case LDPT_ADD_INPUT_FILE
:
1404 add_input_file
= p
->tv_u
.tv_add_input_file
;
1406 case LDPT_ADD_INPUT_LIBRARY
:
1407 add_input_library
= p
->tv_u
.tv_add_input_library
;
1410 process_option (p
->tv_u
.tv_string
);
1412 case LDPT_GOLD_VERSION
:
1413 gold_version
= p
->tv_u
.tv_val
;
1415 case LDPT_LINKER_OUTPUT
:
1416 linker_output
= (enum ld_plugin_output_file_type
) p
->tv_u
.tv_val
;
1417 linker_output_set
= true;
1419 case LDPT_OUTPUT_NAME
:
1420 /* We only use this to make user-friendly temp file names. */
1421 link_output_name
= p
->tv_u
.tv_string
;
1429 check (register_claim_file
, LDPL_FATAL
, "register_claim_file not found");
1430 check (add_symbols
, LDPL_FATAL
, "add_symbols not found");
1431 status
= register_claim_file (claim_file_handler
);
1432 check (status
== LDPS_OK
, LDPL_FATAL
,
1433 "could not register the claim_file callback");
1435 if (register_cleanup
)
1437 status
= register_cleanup (cleanup_handler
);
1438 check (status
== LDPS_OK
, LDPL_FATAL
,
1439 "could not register the cleanup callback");
1442 if (register_all_symbols_read
)
1444 check (get_symbols
, LDPL_FATAL
, "get_symbols not found");
1445 status
= register_all_symbols_read (all_symbols_read_handler
);
1446 check (status
== LDPS_OK
, LDPL_FATAL
,
1447 "could not register the all_symbols_read callback");
1450 char *collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1451 if (collect_gcc_options
)
1453 /* Support -fno-use-linker-plugin by failing to load the plugin
1454 for the case where it is auto-loaded by BFD. */
1455 if (strstr (collect_gcc_options
, "'-fno-use-linker-plugin'"))
1458 if (strstr (collect_gcc_options
, "'-save-temps'"))
1461 if (strstr (collect_gcc_options
, "'-v'")
1462 || strstr (collect_gcc_options
, "'--verbose'"))
1466 if ((p
= strstr (collect_gcc_options
, "'-dumpdir'")))
1468 p
+= sizeof ("'-dumpdir'");
1471 const char *start
= p
;
1472 int ticks
= 0, escapes
= 0;
1473 /* Count ticks (') and escaped (\.) characters. Stop at the
1474 end of the options or at a blank after an even number of
1475 ticks (not counting escaped ones. */
1476 for (p
= start
; *p
; p
++)
1483 else if ((ticks
% 2) != 0)
1497 /* Now allocate a new link_output_name and decode dumpdir
1498 into it. The loop uses the same logic, except it counts
1499 ticks and escapes backwards (so ticks is adjusted if we
1500 find an odd number of them), and it copies characters
1501 that are escaped or not otherwise skipped. */
1502 int len
= p
- start
- ticks
- escapes
+ 1;
1503 char *q
= xmalloc (len
);
1504 link_output_name
= q
;
1505 int oddticks
= (ticks
% 2);
1507 for (p
= start
; *p
; p
++)
1514 else if ((ticks
% 2) != 0)
1529 assert (escapes
== 0);
1530 assert (ticks
== oddticks
);
1531 assert (q
- link_output_name
== len
- 1);