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
;
119 struct ld_plugin_symbol
*syms
;
120 unsigned long long id
;
123 /* Encapsulates object file data during symbol scan. */
124 struct plugin_objfile
128 simple_object_read
*objfile
;
129 struct plugin_symtab
*out
;
130 const struct ld_plugin_input_file
*file
;
133 /* All that we have to remember about a file. */
135 struct plugin_file_info
139 struct plugin_symtab symtab
;
140 struct plugin_symtab conflicts
;
143 /* List item with name of the file with offloading. */
145 struct plugin_offload_file
148 struct plugin_offload_file
*next
;
151 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
152 stdio file streams, we do simple label translation here. */
156 ss_none
, /* No underscore prefix. */
157 ss_win32
, /* Underscore prefix any symbol not beginning with '@'. */
158 ss_uscore
, /* Underscore prefix all symbols. */
161 static char *arguments_file_name
;
162 static ld_plugin_register_claim_file register_claim_file
;
163 static ld_plugin_register_all_symbols_read register_all_symbols_read
;
164 static ld_plugin_get_symbols get_symbols
, get_symbols_v2
;
165 static ld_plugin_register_cleanup register_cleanup
;
166 static ld_plugin_add_input_file add_input_file
;
167 static ld_plugin_add_input_library add_input_library
;
168 static ld_plugin_message message
;
169 static ld_plugin_add_symbols add_symbols
, add_symbols_v2
;
171 static struct plugin_file_info
*claimed_files
= NULL
;
172 static unsigned int num_claimed_files
= 0;
173 static unsigned int non_claimed_files
= 0;
175 /* List of files with offloading. */
176 static struct plugin_offload_file
*offload_files
;
177 /* Last file in the list. */
178 static struct plugin_offload_file
*offload_files_last
;
179 /* Last non-archive file in the list. */
180 static struct plugin_offload_file
*offload_files_last_obj
;
181 /* Last LTO file in the list. */
182 static struct plugin_offload_file
*offload_files_last_lto
;
183 /* Total number of files with offloading. */
184 static unsigned num_offload_files
;
186 static char **output_files
= NULL
;
187 static unsigned int num_output_files
= 0;
189 static char **lto_wrapper_argv
;
190 static int lto_wrapper_num_args
;
192 static char **pass_through_items
= NULL
;
193 static unsigned int num_pass_through_items
;
196 static bool save_temps
;
199 static char *resolution_file
= NULL
;
200 static enum ld_plugin_output_file_type linker_output
;
201 static bool linker_output_set
;
202 static bool linker_output_known
;
203 static bool linker_output_auto_nolto_rel
;
204 static const char *link_output_name
= NULL
;
206 /* The version of gold being used, or -1 if not gold. The number is
207 MAJOR * 100 + MINOR. */
208 static int gold_version
= -1;
210 /* Not used by default, but can be overridden at runtime
211 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
212 (in fact, only first letter of style arg is checked.) */
213 static enum symbol_style sym_style
= ss_none
;
216 check_1 (int gate
, enum ld_plugin_level level
, const char *text
)
222 message (level
, text
);
225 /* If there is no nicer way to inform the user, fallback to stderr. */
226 fprintf (stderr
, "%s\n", text
);
227 if (level
== LDPL_FATAL
)
232 /* This little wrapper allows check to be called with a non-integer
233 first argument, such as a pointer that must be non-NULL. We can't
234 use c99 bool type to coerce it into range, so we explicitly test. */
235 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
237 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
238 by P and the result is written in ENTRY. The slot number is stored in SLOT.
239 Returns the address of the next entry. */
242 parse_table_entry (char *p
, struct ld_plugin_symbol
*entry
,
246 enum ld_plugin_symbol_kind translate_kind
[] =
255 enum ld_plugin_symbol_visibility translate_visibility
[] =
268 /* cf. Duff's device. */
270 entry
->name
= xstrdup (p
);
275 entry
->name
= concat ("_", p
, NULL
);
278 check (0, LDPL_FATAL
, "invalid symbol style requested");
285 entry
->version
= NULL
;
287 entry
->comdat_key
= p
;
292 if (strlen (entry
->comdat_key
) == 0)
293 entry
->comdat_key
= NULL
;
295 entry
->comdat_key
= xstrdup (entry
->comdat_key
);
297 entry
->unused
= entry
->section_kind
= entry
->symbol_type
= 0;
300 check (t
<= 4, LDPL_FATAL
, "invalid symbol kind found");
301 entry
->def
= translate_kind
[t
];
305 check (t
<= 3, LDPL_FATAL
, "invalid symbol visibility found");
306 entry
->visibility
= translate_visibility
[t
];
309 memcpy (&entry
->size
, p
, sizeof (uint64_t));
312 memcpy (&aux
->slot
, p
, sizeof (uint32_t));
315 entry
->resolution
= LDPR_UNKNOWN
;
317 aux
->next_conflict
= -1;
322 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
323 by P and the result is written in ENTRY. The slot number is stored in SLOT.
324 Returns the address of the next entry. */
327 parse_table_entry_extension (char *p
, struct ld_plugin_symbol
*entry
)
330 enum ld_plugin_symbol_type symbol_types
[] =
338 check (t
<= 2, LDPL_FATAL
, "invalid symbol type found");
339 entry
->symbol_type
= symbol_types
[t
];
341 entry
->section_kind
= *p
;
348 /* Translate the IL symbol table located between DATA and END. Append the
349 slots and symbols to OUT. */
352 translate (char *data
, char *end
, struct plugin_symtab
*out
)
355 struct ld_plugin_symbol
*syms
= NULL
;
358 /* This overestimates the output buffer sizes, but at least
359 the algorithm is O(1) now. */
361 len
= (end
- data
)/8 + out
->nsyms
+ 1;
362 syms
= xrealloc (out
->syms
, len
* sizeof (struct ld_plugin_symbol
));
363 aux
= xrealloc (out
->aux
, len
* sizeof (struct sym_aux
));
365 for (n
= out
->nsyms
; data
< end
; n
++)
368 data
= parse_table_entry (data
, &syms
[n
], &aux
[n
]);
379 parse_symtab_extension (char *data
, char *end
, struct plugin_symtab
*out
)
383 unsigned char version
= *data
;
386 /* Version 1 contains the following data per entry:
392 for (i
= 0; i
< out
->nsyms
; i
++)
393 data
= parse_table_entry_extension (data
, &out
->syms
[i
]);
396 /* Free all memory that is no longer needed after writing the symbol
400 free_1 (struct plugin_file_info
*files
, unsigned num_files
)
403 for (i
= 0; i
< num_files
; i
++)
405 struct plugin_file_info
*info
= &files
[i
];
406 struct plugin_symtab
*symtab
= &info
->symtab
;
408 for (j
= 0; j
< symtab
->nsyms
; j
++)
410 struct ld_plugin_symbol
*s
= &symtab
->syms
[j
];
412 free (s
->comdat_key
);
419 /* Free all remaining memory. */
425 for (i
= 0; i
< num_claimed_files
; i
++)
427 struct plugin_file_info
*info
= &claimed_files
[i
];
428 struct plugin_symtab
*symtab
= &info
->symtab
;
433 for (i
= 0; i
< num_output_files
; i
++)
434 free (output_files
[i
]);
437 free (claimed_files
);
438 claimed_files
= NULL
;
439 num_claimed_files
= 0;
441 while (offload_files
)
443 struct plugin_offload_file
*ofld
= offload_files
;
444 offload_files
= offload_files
->next
;
447 num_offload_files
= 0;
449 free (arguments_file_name
);
450 arguments_file_name
= NULL
;
453 /* Dump SYMTAB to resolution file F. */
456 dump_symtab (FILE *f
, struct plugin_symtab
*symtab
)
460 for (j
= 0; j
< symtab
->nsyms
; j
++)
462 uint32_t slot
= symtab
->aux
[j
].slot
;
463 unsigned int resolution
= symtab
->syms
[j
].resolution
;
465 assert (resolution
!= LDPR_UNKNOWN
);
467 fprintf (f
, "%u %" PRI_LL
"x %s %s\n",
468 (unsigned int) slot
, symtab
->aux
[j
].id
,
469 lto_resolution_str
[resolution
],
470 symtab
->syms
[j
].name
);
474 /* Finish the conflicts' resolution information after the linker resolved
475 the original symbols */
478 finish_conflict_resolution (struct plugin_symtab
*symtab
,
479 struct plugin_symtab
*conflicts
)
483 if (conflicts
->nsyms
== 0)
486 for (i
= 0; i
< symtab
->nsyms
; i
++)
488 char resolution
= LDPR_UNKNOWN
;
490 if (symtab
->aux
[i
].next_conflict
== -1)
493 switch (symtab
->syms
[i
].def
)
496 case LDPK_COMMON
: /* ??? */
497 resolution
= LDPR_RESOLVED_IR
;
500 resolution
= LDPR_PREEMPTED_IR
;
504 resolution
= symtab
->syms
[i
].resolution
;
510 assert (resolution
!= LDPR_UNKNOWN
);
512 for (j
= symtab
->aux
[i
].next_conflict
;
514 j
= conflicts
->aux
[j
].next_conflict
)
515 conflicts
->syms
[j
].resolution
= resolution
;
519 /* Free symbol table SYMTAB. */
522 free_symtab (struct plugin_symtab
*symtab
)
530 /* Writes the relocations to disk. */
533 write_resolution (void)
538 check (resolution_file
, LDPL_FATAL
, "resolution file not specified");
539 f
= fopen (resolution_file
, "w");
540 check (f
, LDPL_FATAL
, "could not open file");
542 fprintf (f
, "%d\n", num_claimed_files
);
544 for (i
= 0; i
< num_claimed_files
; i
++)
546 struct plugin_file_info
*info
= &claimed_files
[i
];
547 struct plugin_symtab
*symtab
= &info
->symtab
;
548 struct ld_plugin_symbol
*syms
= symtab
->syms
;
550 /* Version 2 of API supports IRONLY_EXP resolution that is
551 accepted by GCC-4.7 and newer. */
553 get_symbols_v2 (info
->handle
, symtab
->nsyms
, syms
);
555 get_symbols (info
->handle
, symtab
->nsyms
, syms
);
557 finish_conflict_resolution (symtab
, &info
->conflicts
);
559 fprintf (f
, "%s %d\n", info
->name
, symtab
->nsyms
+ info
->conflicts
.nsyms
);
560 dump_symtab (f
, symtab
);
561 if (info
->conflicts
.nsyms
)
563 dump_symtab (f
, &info
->conflicts
);
564 free_symtab (&info
->conflicts
);
570 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
574 add_output_files (FILE *f
)
578 const unsigned piece
= 32;
579 char *buf
, *s
= xmalloc (piece
);
584 if (!fgets (buf
, piece
, f
))
590 if (s
[len
- 1] != '\n')
592 s
= xrealloc (s
, len
+ piece
);
600 = xrealloc (output_files
, num_output_files
* sizeof (char *));
601 output_files
[num_output_files
- 1] = s
;
602 add_input_file (output_files
[num_output_files
- 1]);
606 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
610 exec_lto_wrapper (char *argv
[])
616 FILE *wrapper_output
;
621 /* Write argv to a file to avoid a command line that is too long
622 Save the file locally on save-temps. */
623 if (save_temps
&& link_output_name
)
625 arguments_file_name
= (char *) xmalloc (strlen (link_output_name
)
626 + sizeof (".lto_wrapper_args") + 1);
627 strcpy (arguments_file_name
, link_output_name
);
628 strcat (arguments_file_name
, ".lto_wrapper_args");
631 arguments_file_name
= make_temp_file (".lto_wrapper_args");
632 check (arguments_file_name
, LDPL_FATAL
,
633 "Failed to generate a temorary file name");
635 args
= fopen (arguments_file_name
, "w");
636 check (args
, LDPL_FATAL
, "could not open arguments file");
638 t
= writeargv (&argv
[1], args
);
639 check (t
== 0, LDPL_FATAL
, "could not write arguments");
641 check (t
== 0, LDPL_FATAL
, "could not close arguments file");
643 at_args
= concat ("@", arguments_file_name
, NULL
);
644 check (at_args
, LDPL_FATAL
, "could not allocate");
646 for (i
= 1; argv
[i
]; i
++)
649 /* Check the input argument list for a verbose marker too. */
650 if (a
[0] == '-' && a
[1] == 'v' && a
[2] == '\0')
659 for (i
= 0; argv
[i
]; i
++)
660 fprintf (stderr
, "%s ", argv
[i
]);
661 fprintf (stderr
, "\n");
664 new_argv
[0] = argv
[0];
665 new_argv
[1] = at_args
;
670 for (i
= 0; new_argv
[i
]; i
++)
671 fprintf (stderr
, "%s ", new_argv
[i
]);
672 fprintf (stderr
, "\n");
675 pex
= pex_init (PEX_USE_PIPES
, "lto-wrapper", NULL
);
676 check (pex
!= NULL
, LDPL_FATAL
, "could not pex_init lto-wrapper");
678 errmsg
= pex_run (pex
, 0, new_argv
[0], new_argv
, NULL
, NULL
, &t
);
679 check (errmsg
== NULL
, LDPL_FATAL
, "could not run lto-wrapper");
680 check (t
== 0, LDPL_FATAL
, "could not run lto-wrapper");
682 wrapper_output
= pex_read_output (pex
, 0);
683 check (wrapper_output
, LDPL_FATAL
, "could not read lto-wrapper output");
685 add_output_files (wrapper_output
);
687 t
= pex_get_status (pex
, 1, &status
);
688 check (t
== 1, LDPL_FATAL
, "could not get lto-wrapper exit status");
689 check (WIFEXITED (status
) && WEXITSTATUS (status
) == 0, LDPL_FATAL
,
690 "lto-wrapper failed");
697 /* Pass the original files back to the linker. */
700 use_original_files (void)
703 for (i
= 0; i
< num_claimed_files
; i
++)
705 struct plugin_file_info
*info
= &claimed_files
[i
];
706 add_input_file (info
->name
);
711 /* Called by the linker once all symbols have been read. */
713 static enum ld_plugin_status
714 all_symbols_read_handler (void)
716 const unsigned num_lto_args
717 = num_claimed_files
+ lto_wrapper_num_args
+ 2
718 + !linker_output_known
+ !linker_output_auto_nolto_rel
;
721 const char *linker_output_str
= NULL
;
722 const char **lto_arg_ptr
;
723 if (num_claimed_files
+ num_offload_files
== 0)
728 use_original_files ();
732 lto_argv
= (char **) xcalloc (sizeof (char *), num_lto_args
);
733 lto_arg_ptr
= (const char **) lto_argv
;
734 assert (lto_wrapper_argv
);
738 free_1 (claimed_files
, num_claimed_files
);
740 for (i
= 0; i
< lto_wrapper_num_args
; i
++)
741 *lto_arg_ptr
++ = lto_wrapper_argv
[i
];
743 if (!linker_output_known
)
745 assert (linker_output_set
);
746 switch (linker_output
)
749 if (non_claimed_files
)
751 if (!linker_output_auto_nolto_rel
)
752 message (LDPL_WARNING
, "incremental linking of LTO and non-LTO"
753 " objects; using -flinker-output=nolto-rel which will"
754 " bypass whole program optimization");
755 linker_output_str
= "-flinker-output=nolto-rel";
758 linker_output_str
= "-flinker-output=rel";
761 linker_output_str
= "-flinker-output=dyn";
764 linker_output_str
= "-flinker-output=pie";
767 linker_output_str
= "-flinker-output=exec";
770 message (LDPL_FATAL
, "unsupported linker output %i", linker_output
);
773 *lto_arg_ptr
++ = xstrdup (linker_output_str
);
776 if (num_offload_files
> 0)
780 char *offload_objects_file_name
;
781 struct plugin_offload_file
*ofld
;
783 offload_objects_file_name
= make_temp_file (".ofldlist");
784 check (offload_objects_file_name
, LDPL_FATAL
,
785 "Failed to generate a temporary file name");
786 f
= fopen (offload_objects_file_name
, "w");
787 check (f
, LDPL_FATAL
, "could not open file with offload objects");
788 fprintf (f
, "%u\n", num_offload_files
);
790 /* Skip the dummy item at the start of the list. */
791 ofld
= offload_files
->next
;
794 fprintf (f
, "%s\n", ofld
->name
);
799 arg
= concat ("-foffload-objects=", offload_objects_file_name
, NULL
);
800 check (arg
, LDPL_FATAL
, "could not allocate");
801 *lto_arg_ptr
++ = arg
;
804 for (i
= 0; i
< num_claimed_files
; i
++)
806 struct plugin_file_info
*info
= &claimed_files
[i
];
808 *lto_arg_ptr
++ = info
->name
;
811 *lto_arg_ptr
++ = NULL
;
812 exec_lto_wrapper (lto_argv
);
816 /* --pass-through is not needed when using gold 1.11 or later. */
817 if (pass_through_items
&& gold_version
< 111)
820 for (i
= 0; i
< num_pass_through_items
; i
++)
822 if (strncmp (pass_through_items
[i
], "-l", 2) == 0)
823 add_input_library (pass_through_items
[i
] + 2);
825 add_input_file (pass_through_items
[i
]);
826 free (pass_through_items
[i
]);
827 pass_through_items
[i
] = NULL
;
829 free (pass_through_items
);
830 pass_through_items
= NULL
;
836 /* Helper, as used in collect2. */
838 file_exists (const char *name
)
840 return access (name
, R_OK
) == 0;
843 /* Unlink FILE unless we have save-temps set.
844 Note that we're saving files if verbose output is set. */
847 maybe_unlink (const char *file
)
849 if (save_temps
&& file_exists (file
))
852 fprintf (stderr
, "[Leaving %s]\n", file
);
856 unlink_if_ordinary (file
);
859 /* Remove temporary files at the end of the link. */
861 static enum ld_plugin_status
862 cleanup_handler (void)
869 if (arguments_file_name
)
870 maybe_unlink (arguments_file_name
);
872 for (i
= 0; i
< num_output_files
; i
++)
873 maybe_unlink (output_files
[i
]);
879 #define SWAP(type, a, b) \
880 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
882 /* Compare two hash table entries */
884 static int eq_sym (const void *a
, const void *b
)
886 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
887 const struct ld_plugin_symbol
*bs
= (const struct ld_plugin_symbol
*)b
;
889 return !strcmp (as
->name
, bs
->name
);
894 static hashval_t
hash_sym (const void *a
)
896 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
898 return htab_hash_string (as
->name
);
901 /* Determine how strong a symbol is */
903 static int symbol_strength (struct ld_plugin_symbol
*s
)
917 /* In the ld -r case we can get dups in the LTO symbol tables, where
918 the same symbol can have different resolutions (e.g. undefined and defined).
920 We have to keep that in the LTO symbol tables, but the dups confuse
921 gold and then finally gcc by supplying incorrect resolutions.
923 Problem is that the main gold symbol table doesn't know about subids
924 and does not distingush the same symbols in different states.
926 So we drop duplicates from the linker visible symbol table
927 and keep them in a private table. Then later do own symbol
928 resolution for the duplicated based on the results for the
931 Then when writing out the resolution file readd the dropped symbols.
933 XXX how to handle common? */
936 resolve_conflicts (struct plugin_symtab
*t
, struct plugin_symtab
*conflicts
)
938 htab_t symtab
= htab_create (t
->nsyms
, hash_sym
, eq_sym
, NULL
);
944 conflicts
->syms
= xmalloc (sizeof (struct ld_plugin_symbol
) * outlen
);
945 conflicts
->aux
= xmalloc (sizeof (struct sym_aux
) * outlen
);
947 /* Move all duplicate symbols into the auxiliary conflicts table. */
949 for (i
= 0; i
< t
->nsyms
; i
++)
951 struct ld_plugin_symbol
*s
= &t
->syms
[i
];
952 struct sym_aux
*aux
= &t
->aux
[i
];
955 slot
= htab_find_slot (symtab
, s
, INSERT
);
959 struct ld_plugin_symbol
*orig
= (struct ld_plugin_symbol
*)*slot
;
960 struct sym_aux
*orig_aux
= &t
->aux
[orig
- t
->syms
];
962 /* Always let the linker resolve the strongest symbol */
963 if (symbol_strength (orig
) < symbol_strength (s
))
965 SWAP (struct ld_plugin_symbol
, *orig
, *s
);
966 SWAP (uint32_t, orig_aux
->slot
, aux
->slot
);
967 SWAP (unsigned long long, orig_aux
->id
, aux
->id
);
968 /* Don't swap conflict chain pointer */
971 /* Move current symbol into the conflicts table */
972 cnf
= conflicts
->nsyms
++;
973 conflicts
->syms
[cnf
] = *s
;
974 conflicts
->aux
[cnf
] = *aux
;
975 aux
= &conflicts
->aux
[cnf
];
977 /* Update conflicts chain of the original symbol */
978 aux
->next_conflict
= orig_aux
->next_conflict
;
979 orig_aux
->next_conflict
= cnf
;
984 /* Remove previous duplicates in the main table */
991 /* Put original into the hash table */
992 *slot
= &t
->syms
[out
];
996 assert (conflicts
->nsyms
<= outlen
);
997 assert (conflicts
->nsyms
+ out
== t
->nsyms
);
1000 htab_delete (symtab
);
1003 /* Process one section of an object file. */
1006 process_symtab (void *data
, const char *name
, off_t offset
, off_t length
)
1008 struct plugin_objfile
*obj
= (struct plugin_objfile
*)data
;
1010 char *secdatastart
, *secdata
;
1012 if (strncmp (name
, LTO_SYMTAB_PREFIX
, LTO_SYMTAB_PREFIX_LEN
) != 0)
1015 s
= strrchr (name
, '.');
1017 sscanf (s
, ".%" PRI_LL
"x", &obj
->out
->id
);
1018 secdata
= secdatastart
= xmalloc (length
);
1019 offset
+= obj
->file
->offset
;
1020 if (offset
!= lseek (obj
->file
->fd
, offset
, SEEK_SET
))
1025 ssize_t got
= read (obj
->file
->fd
, secdata
, length
);
1033 else if (errno
!= EINTR
)
1040 translate (secdatastart
, secdata
, obj
->out
);
1042 free (secdatastart
);
1047 message (LDPL_FATAL
, "%s: corrupt object file", obj
->file
->name
);
1048 /* Force claim_file_handler to abandon this file. */
1050 free (secdatastart
);
1054 /* Process one section of an object file. */
1057 process_symtab_extension (void *data
, const char *name
, off_t offset
,
1060 struct plugin_objfile
*obj
= (struct plugin_objfile
*)data
;
1062 char *secdatastart
, *secdata
;
1064 if (strncmp (name
, LTO_SYMTAB_EXT_PREFIX
, LTO_SYMTAB_EXT_PREFIX_LEN
) != 0)
1067 s
= strrchr (name
, '.');
1069 sscanf (s
, ".%" PRI_LL
"x", &obj
->out
->id
);
1070 secdata
= secdatastart
= xmalloc (length
);
1071 offset
+= obj
->file
->offset
;
1072 if (offset
!= lseek (obj
->file
->fd
, offset
, SEEK_SET
))
1077 ssize_t got
= read (obj
->file
->fd
, secdata
, length
);
1085 else if (errno
!= EINTR
)
1092 parse_symtab_extension (secdatastart
, secdata
, obj
->out
);
1094 free (secdatastart
);
1099 message (LDPL_FATAL
, "%s: corrupt object file", obj
->file
->name
);
1100 /* Force claim_file_handler to abandon this file. */
1102 free (secdatastart
);
1107 /* Find an offload section of an object file. */
1110 process_offload_section (void *data
, const char *name
, off_t offset
, off_t len
)
1112 if (!strncmp (name
, OFFLOAD_SECTION
, OFFLOAD_SECTION_LEN
))
1114 struct plugin_objfile
*obj
= (struct plugin_objfile
*) data
;
1122 /* Callback used by gold to check if the plugin will claim FILE. Writes
1123 the result in CLAIMED. */
1125 static enum ld_plugin_status
1126 claim_file_handler (const struct ld_plugin_input_file
*file
, int *claimed
)
1128 enum ld_plugin_status status
;
1129 struct plugin_objfile obj
;
1130 struct plugin_file_info lto_file
;
1134 memset (<o_file
, 0, sizeof (struct plugin_file_info
));
1136 if (file
->offset
!= 0)
1138 /* We pass the offset of the actual file, not the archive header.
1139 Can't use PRIx64, because that's C99, so we have to print the
1140 64-bit hex int as two 32-bit ones. Use xasprintf instead of
1141 asprintf because asprintf doesn't work as expected on some older
1144 lo
= file
->offset
& 0xffffffff;
1145 hi
= ((int64_t)file
->offset
>> 32) & 0xffffffff;
1146 lto_file
.name
= hi
? xasprintf ("%s@0x%x%08x", file
->name
, hi
, lo
)
1147 : xasprintf ("%s@0x%x", file
->name
, lo
);
1151 lto_file
.name
= xstrdup (file
->name
);
1153 lto_file
.handle
= file
->handle
;
1159 obj
.out
= <o_file
.symtab
;
1161 obj
.objfile
= simple_object_start_read (file
->fd
, file
->offset
, LTO_SEGMENT_NAME
,
1163 /* No file, but also no error code means unrecognized format; just skip it. */
1164 if (!obj
.objfile
&& !err
)
1169 errmsg
= simple_object_find_sections (obj
.objfile
, process_symtab
, &obj
,
1171 /* Parsing symtab extension should be done only for add_symbols_v2 and
1173 if (!errmsg
&& add_symbols_v2
!= NULL
)
1174 errmsg
= simple_object_find_sections (obj
.objfile
,
1175 process_symtab_extension
,
1179 if (!obj
.objfile
|| errmsg
)
1182 message (LDPL_FATAL
, "%s: %s: %s", file
->name
, errmsg
,
1185 message (LDPL_FATAL
, "%s: %s", file
->name
, errmsg
);
1190 simple_object_find_sections (obj
.objfile
, process_offload_section
,
1193 if (obj
.found
== 0 && obj
.offload
== 0)
1197 resolve_conflicts (<o_file
.symtab
, <o_file
.conflicts
);
1202 status
= add_symbols_v2 (file
->handle
, lto_file
.symtab
.nsyms
,
1203 lto_file
.symtab
.syms
);
1205 status
= add_symbols (file
->handle
, lto_file
.symtab
.nsyms
,
1206 lto_file
.symtab
.syms
);
1207 check (status
== LDPS_OK
, LDPL_FATAL
, "could not add symbols");
1209 num_claimed_files
++;
1211 xrealloc (claimed_files
,
1212 num_claimed_files
* sizeof (struct plugin_file_info
));
1213 claimed_files
[num_claimed_files
- 1] = lto_file
;
1218 if (offload_files
== NULL
)
1220 /* Add dummy item to the start of the list. */
1221 offload_files
= xmalloc (sizeof (struct plugin_offload_file
));
1222 offload_files
->name
= NULL
;
1223 offload_files
->next
= NULL
;
1224 offload_files_last
= offload_files
;
1227 /* If this is an LTO file without offload, and it is the first LTO file, save
1228 the pointer to the last offload file in the list. Further offload LTO
1229 files will be inserted after it, if any. */
1230 if (*claimed
&& obj
.offload
== 0 && offload_files_last_lto
== NULL
)
1231 offload_files_last_lto
= offload_files_last
;
1233 if (obj
.offload
== 1)
1235 /* Add file to the list. The order must be exactly the same as the final
1236 order after recompilation and linking, otherwise host and target tables
1237 with addresses wouldn't match. If a static library contains both LTO
1238 and non-LTO objects, ld and gold link them in a different order. */
1239 struct plugin_offload_file
*ofld
1240 = xmalloc (sizeof (struct plugin_offload_file
));
1241 ofld
->name
= lto_file
.name
;
1244 if (*claimed
&& offload_files_last_lto
== NULL
&& file
->offset
!= 0
1245 && gold_version
== -1)
1247 /* ld only: insert first LTO file from the archive after the last real
1248 object file immediately preceding the archive, or at the begin of
1249 the list if there was no real objects before archives. */
1250 if (offload_files_last_obj
!= NULL
)
1252 ofld
->next
= offload_files_last_obj
->next
;
1253 offload_files_last_obj
->next
= ofld
;
1257 ofld
->next
= offload_files
->next
;
1258 offload_files
->next
= ofld
;
1261 else if (*claimed
&& offload_files_last_lto
!= NULL
)
1263 /* Insert LTO file after the last LTO file in the list. */
1264 ofld
->next
= offload_files_last_lto
->next
;
1265 offload_files_last_lto
->next
= ofld
;
1268 /* Add non-LTO file or first non-archive LTO file to the end of the
1270 offload_files_last
->next
= ofld
;
1272 if (ofld
->next
== NULL
)
1273 offload_files_last
= ofld
;
1274 if (file
->offset
== 0)
1275 offload_files_last_obj
= ofld
;
1277 offload_files_last_lto
= ofld
;
1278 num_offload_files
++;
1284 non_claimed_files
++;
1285 free (lto_file
.name
);
1289 simple_object_release_read (obj
.objfile
);
1294 /* Parse the plugin options. */
1297 process_option (const char *option
)
1299 if (strcmp (option
, "-linker-output-known") == 0)
1300 linker_output_known
= true;
1301 else if (strcmp (option
, "-linker-output-auto-notlo-rel") == 0)
1302 linker_output_auto_nolto_rel
= true;
1303 else if (strcmp (option
, "-debug") == 0)
1305 else if ((strcmp (option
, "-v") == 0)
1306 || (strcmp (option
, "--verbose") == 0))
1308 else if (strcmp (option
, "-save-temps") == 0)
1310 else if (strcmp (option
, "-nop") == 0)
1312 else if (!strncmp (option
, "-pass-through=", strlen("-pass-through=")))
1314 num_pass_through_items
++;
1315 pass_through_items
= xrealloc (pass_through_items
,
1316 num_pass_through_items
* sizeof (char *));
1317 pass_through_items
[num_pass_through_items
- 1] =
1318 xstrdup (option
+ strlen ("-pass-through="));
1320 else if (!strncmp (option
, "-sym-style=", sizeof ("-sym-style=") - 1))
1322 switch (option
[sizeof ("-sym-style=") - 1])
1325 sym_style
= ss_win32
;
1328 sym_style
= ss_uscore
;
1331 sym_style
= ss_none
;
1338 char *opt
= xstrdup (option
);
1339 lto_wrapper_num_args
+= 1;
1340 size
= lto_wrapper_num_args
* sizeof (char *);
1341 lto_wrapper_argv
= (char **) xrealloc (lto_wrapper_argv
, size
);
1342 lto_wrapper_argv
[lto_wrapper_num_args
- 1] = opt
;
1343 if (strncmp (option
, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1344 resolution_file
= opt
+ sizeof ("-fresolution=") - 1;
1346 save_temps
= save_temps
|| debug
;
1347 verbose
= verbose
|| debug
;
1350 /* Called by gold after loading the plugin. TV is the transfer vector. */
1352 enum ld_plugin_status
1353 onload (struct ld_plugin_tv
*tv
)
1355 struct ld_plugin_tv
*p
;
1356 enum ld_plugin_status status
;
1364 message
= p
->tv_u
.tv_message
;
1366 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
1367 register_claim_file
= p
->tv_u
.tv_register_claim_file
;
1369 case LDPT_ADD_SYMBOLS_V2
:
1370 add_symbols_v2
= p
->tv_u
.tv_add_symbols
;
1372 case LDPT_ADD_SYMBOLS
:
1373 add_symbols
= p
->tv_u
.tv_add_symbols
;
1375 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
1376 register_all_symbols_read
= p
->tv_u
.tv_register_all_symbols_read
;
1378 case LDPT_GET_SYMBOLS_V2
:
1379 get_symbols_v2
= p
->tv_u
.tv_get_symbols
;
1381 case LDPT_GET_SYMBOLS
:
1382 get_symbols
= p
->tv_u
.tv_get_symbols
;
1384 case LDPT_REGISTER_CLEANUP_HOOK
:
1385 register_cleanup
= p
->tv_u
.tv_register_cleanup
;
1387 case LDPT_ADD_INPUT_FILE
:
1388 add_input_file
= p
->tv_u
.tv_add_input_file
;
1390 case LDPT_ADD_INPUT_LIBRARY
:
1391 add_input_library
= p
->tv_u
.tv_add_input_library
;
1394 process_option (p
->tv_u
.tv_string
);
1396 case LDPT_GOLD_VERSION
:
1397 gold_version
= p
->tv_u
.tv_val
;
1399 case LDPT_LINKER_OUTPUT
:
1400 linker_output
= (enum ld_plugin_output_file_type
) p
->tv_u
.tv_val
;
1401 linker_output_set
= true;
1403 case LDPT_OUTPUT_NAME
:
1404 /* We only use this to make user-friendly temp file names. */
1405 link_output_name
= p
->tv_u
.tv_string
;
1413 check (register_claim_file
, LDPL_FATAL
, "register_claim_file not found");
1414 check (add_symbols
, LDPL_FATAL
, "add_symbols not found");
1415 status
= register_claim_file (claim_file_handler
);
1416 check (status
== LDPS_OK
, LDPL_FATAL
,
1417 "could not register the claim_file callback");
1419 if (register_cleanup
)
1421 status
= register_cleanup (cleanup_handler
);
1422 check (status
== LDPS_OK
, LDPL_FATAL
,
1423 "could not register the cleanup callback");
1426 if (register_all_symbols_read
)
1428 check (get_symbols
, LDPL_FATAL
, "get_symbols not found");
1429 status
= register_all_symbols_read (all_symbols_read_handler
);
1430 check (status
== LDPS_OK
, LDPL_FATAL
,
1431 "could not register the all_symbols_read callback");
1434 char *collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1435 if (collect_gcc_options
)
1437 /* Support -fno-use-linker-plugin by failing to load the plugin
1438 for the case where it is auto-loaded by BFD. */
1439 if (strstr (collect_gcc_options
, "'-fno-use-linker-plugin'"))
1442 if ( strstr (collect_gcc_options
, "'-save-temps'"))
1445 if (strstr (collect_gcc_options
, "'-v'")
1446 || strstr (collect_gcc_options
, "'--verbose'"))