1 /* LTO plugin for gold and/or GNU ld.
2 Copyright (C) 2009, 2010 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 2 options of its 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. */
50 #include <sys/types.h>
51 #ifdef HAVE_SYS_WAIT_H
55 #define WIFEXITED(S) (((S) & 0xff) == 0)
58 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
60 #include <libiberty.h>
62 #include "../gcc/lto/common.h"
63 #include "simple-object.h"
64 #include "plugin-api.h"
66 /* We need to use I64 instead of ll width-specifier on native Windows.
67 The reason for this is that older MS-runtimes don't support the ll. */
74 /* Handle opening elf files on hosts, such as Windows, that may use
75 text file handling that will break binary access. */
80 /* Segment name for LTO sections. This is only used for Mach-O.
81 FIXME: This needs to be kept in sync with darwin.c. */
83 #define LTO_SEGMENT_NAME "__GNU_LTO"
85 /* LTO magic section name. */
87 #define LTO_SECTION_PREFIX ".gnu.lto_.symtab"
88 #define LTO_SECTION_PREFIX_LEN (sizeof (LTO_SECTION_PREFIX) - 1)
89 #define OFFLOAD_SECTION ".gnu.offload_lto_.opts"
90 #define OFFLOAD_SECTION_LEN (sizeof (OFFLOAD_SECTION) - 1)
92 /* The part of the symbol table the plugin has to keep track of. Note that we
93 must keep SYMS until all_symbols_read is called to give the linker time to
94 copy the symbol information.
95 The id must be 64bit to minimze collisions. */
100 unsigned long long id
;
101 unsigned next_conflict
;
108 struct ld_plugin_symbol
*syms
;
109 unsigned long long id
;
112 /* Encapsulates object file data during symbol scan. */
113 struct plugin_objfile
117 simple_object_read
*objfile
;
118 struct plugin_symtab
*out
;
119 const struct ld_plugin_input_file
*file
;
122 /* All that we have to remember about a file. */
124 struct plugin_file_info
128 struct plugin_symtab symtab
;
129 struct plugin_symtab conflicts
;
132 /* List item with name of the file with offloading. */
134 struct plugin_offload_file
137 struct plugin_offload_file
*next
;
140 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
141 stdio file streams, we do simple label translation here. */
145 ss_none
, /* No underscore prefix. */
146 ss_win32
, /* Underscore prefix any symbol not beginning with '@'. */
147 ss_uscore
, /* Underscore prefix all symbols. */
150 static char *arguments_file_name
;
151 static ld_plugin_register_claim_file register_claim_file
;
152 static ld_plugin_register_all_symbols_read register_all_symbols_read
;
153 static ld_plugin_get_symbols get_symbols
, get_symbols_v2
;
154 static ld_plugin_register_cleanup register_cleanup
;
155 static ld_plugin_add_input_file add_input_file
;
156 static ld_plugin_add_input_library add_input_library
;
157 static ld_plugin_message message
;
158 static ld_plugin_add_symbols add_symbols
;
160 static struct plugin_file_info
*claimed_files
= NULL
;
161 static unsigned int num_claimed_files
= 0;
163 /* List of files with offloading. */
164 static struct plugin_offload_file
*offload_files
;
165 /* Last file in the list. */
166 static struct plugin_offload_file
*offload_files_last
;
167 /* Last non-archive file in the list. */
168 static struct plugin_offload_file
*offload_files_last_obj
;
169 /* Last LTO file in the list. */
170 static struct plugin_offload_file
*offload_files_last_lto
;
171 /* Total number of files with offloading. */
172 static unsigned num_offload_files
;
174 static char **output_files
= NULL
;
175 static unsigned int num_output_files
= 0;
177 static char **lto_wrapper_argv
;
178 static int lto_wrapper_num_args
;
180 static char **pass_through_items
= NULL
;
181 static unsigned int num_pass_through_items
;
185 static char *resolution_file
= NULL
;
186 static enum ld_plugin_output_file_type linker_output
;
187 static int linker_output_set
;
189 /* The version of gold being used, or -1 if not gold. The number is
190 MAJOR * 100 + MINOR. */
191 static int gold_version
= -1;
193 /* Not used by default, but can be overridden at runtime
194 by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
195 (in fact, only first letter of style arg is checked.) */
196 static enum symbol_style sym_style
= ss_none
;
199 check_1 (int gate
, enum ld_plugin_level level
, const char *text
)
205 message (level
, text
);
208 /* If there is no nicer way to inform the user, fallback to stderr. */
209 fprintf (stderr
, "%s\n", text
);
210 if (level
== LDPL_FATAL
)
215 /* This little wrapper allows check to be called with a non-integer
216 first argument, such as a pointer that must be non-NULL. We can't
217 use c99 bool type to coerce it into range, so we explicitly test. */
218 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
220 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
221 by P and the result is written in ENTRY. The slot number is stored in SLOT.
222 Returns the address of the next entry. */
225 parse_table_entry (char *p
, struct ld_plugin_symbol
*entry
,
229 enum ld_plugin_symbol_kind translate_kind
[] =
238 enum ld_plugin_symbol_visibility translate_visibility
[] =
251 /* cf. Duff's device. */
253 entry
->name
= xstrdup (p
);
258 entry
->name
= concat ("_", p
, NULL
);
261 check (0, LDPL_FATAL
, "invalid symbol style requested");
268 entry
->version
= NULL
;
270 entry
->comdat_key
= p
;
275 if (strlen (entry
->comdat_key
) == 0)
276 entry
->comdat_key
= NULL
;
278 entry
->comdat_key
= xstrdup (entry
->comdat_key
);
281 check (t
<= 4, LDPL_FATAL
, "invalid symbol kind found");
282 entry
->def
= translate_kind
[t
];
286 check (t
<= 3, LDPL_FATAL
, "invalid symbol visibility found");
287 entry
->visibility
= translate_visibility
[t
];
290 memcpy (&entry
->size
, p
, sizeof (uint64_t));
293 memcpy (&aux
->slot
, p
, sizeof (uint32_t));
296 entry
->resolution
= LDPR_UNKNOWN
;
298 aux
->next_conflict
= -1;
303 /* Translate the IL symbol table located between DATA and END. Append the
304 slots and symbols to OUT. */
307 translate (char *data
, char *end
, struct plugin_symtab
*out
)
310 struct ld_plugin_symbol
*syms
= NULL
;
313 /* This overestimates the output buffer sizes, but at least
314 the algorithm is O(1) now. */
316 len
= (end
- data
)/8 + out
->nsyms
+ 1;
317 syms
= xrealloc (out
->syms
, len
* sizeof (struct ld_plugin_symbol
));
318 aux
= xrealloc (out
->aux
, len
* sizeof (struct sym_aux
));
320 for (n
= out
->nsyms
; data
< end
; n
++)
323 data
= parse_table_entry (data
, &syms
[n
], &aux
[n
]);
333 /* Free all memory that is no longer needed after writing the symbol
337 free_1 (struct plugin_file_info
*files
, unsigned num_files
)
340 for (i
= 0; i
< num_files
; i
++)
342 struct plugin_file_info
*info
= &files
[i
];
343 struct plugin_symtab
*symtab
= &info
->symtab
;
345 for (j
= 0; j
< symtab
->nsyms
; j
++)
347 struct ld_plugin_symbol
*s
= &symtab
->syms
[j
];
349 free (s
->comdat_key
);
356 /* Free all remaining memory. */
362 for (i
= 0; i
< num_claimed_files
; i
++)
364 struct plugin_file_info
*info
= &claimed_files
[i
];
365 struct plugin_symtab
*symtab
= &info
->symtab
;
370 for (i
= 0; i
< num_output_files
; i
++)
371 free (output_files
[i
]);
374 free (claimed_files
);
375 claimed_files
= NULL
;
376 num_claimed_files
= 0;
378 while (offload_files
)
380 struct plugin_offload_file
*ofld
= offload_files
;
381 offload_files
= offload_files
->next
;
384 num_offload_files
= 0;
386 free (arguments_file_name
);
387 arguments_file_name
= NULL
;
390 /* Dump SYMTAB to resolution file F. */
393 dump_symtab (FILE *f
, struct plugin_symtab
*symtab
)
397 for (j
= 0; j
< symtab
->nsyms
; j
++)
399 uint32_t slot
= symtab
->aux
[j
].slot
;
400 unsigned int resolution
= symtab
->syms
[j
].resolution
;
402 assert (resolution
!= LDPR_UNKNOWN
);
404 fprintf (f
, "%u %" PRI_LL
"x %s %s\n",
405 (unsigned int) slot
, symtab
->aux
[j
].id
,
406 lto_resolution_str
[resolution
],
407 symtab
->syms
[j
].name
);
411 /* Finish the conflicts' resolution information after the linker resolved
412 the original symbols */
415 finish_conflict_resolution (struct plugin_symtab
*symtab
,
416 struct plugin_symtab
*conflicts
)
420 if (conflicts
->nsyms
== 0)
423 for (i
= 0; i
< symtab
->nsyms
; i
++)
425 int resolution
= LDPR_UNKNOWN
;
427 if (symtab
->aux
[i
].next_conflict
== -1)
430 switch (symtab
->syms
[i
].def
)
433 case LDPK_COMMON
: /* ??? */
434 resolution
= LDPR_RESOLVED_IR
;
437 resolution
= LDPR_PREEMPTED_IR
;
441 resolution
= symtab
->syms
[i
].resolution
;
447 assert (resolution
!= LDPR_UNKNOWN
);
449 for (j
= symtab
->aux
[i
].next_conflict
;
451 j
= conflicts
->aux
[j
].next_conflict
)
452 conflicts
->syms
[j
].resolution
= resolution
;
456 /* Free symbol table SYMTAB. */
459 free_symtab (struct plugin_symtab
*symtab
)
467 /* Writes the relocations to disk. */
470 write_resolution (void)
475 check (resolution_file
, LDPL_FATAL
, "resolution file not specified");
476 f
= fopen (resolution_file
, "w");
477 check (f
, LDPL_FATAL
, "could not open file");
479 fprintf (f
, "%d\n", num_claimed_files
);
481 for (i
= 0; i
< num_claimed_files
; i
++)
483 struct plugin_file_info
*info
= &claimed_files
[i
];
484 struct plugin_symtab
*symtab
= &info
->symtab
;
485 struct ld_plugin_symbol
*syms
= symtab
->syms
;
487 /* Version 2 of API supports IRONLY_EXP resolution that is
488 accepted by GCC-4.7 and newer. */
490 get_symbols_v2 (info
->handle
, symtab
->nsyms
, syms
);
492 get_symbols (info
->handle
, symtab
->nsyms
, syms
);
494 finish_conflict_resolution (symtab
, &info
->conflicts
);
496 fprintf (f
, "%s %d\n", info
->name
, symtab
->nsyms
+ info
->conflicts
.nsyms
);
497 dump_symtab (f
, symtab
);
498 if (info
->conflicts
.nsyms
)
500 dump_symtab (f
, &info
->conflicts
);
501 free_symtab (&info
->conflicts
);
507 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
511 add_output_files (FILE *f
)
515 const unsigned piece
= 32;
516 char *buf
, *s
= xmalloc (piece
);
521 if (!fgets (buf
, piece
, f
))
527 if (s
[len
- 1] != '\n')
529 s
= xrealloc (s
, len
+ piece
);
537 = xrealloc (output_files
, num_output_files
* sizeof (char *));
538 output_files
[num_output_files
- 1] = s
;
539 add_input_file (output_files
[num_output_files
- 1]);
543 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
547 exec_lto_wrapper (char *argv
[])
553 FILE *wrapper_output
;
558 /* Write argv to a file to avoid a command line that is too long. */
559 arguments_file_name
= make_temp_file ("");
560 check (arguments_file_name
, LDPL_FATAL
,
561 "Failed to generate a temorary file name");
563 args
= fopen (arguments_file_name
, "w");
564 check (args
, LDPL_FATAL
, "could not open arguments file");
566 t
= writeargv (&argv
[1], args
);
567 check (t
== 0, LDPL_FATAL
, "could not write arguments");
569 check (t
== 0, LDPL_FATAL
, "could not close arguments file");
571 at_args
= concat ("@", arguments_file_name
, NULL
);
572 check (at_args
, LDPL_FATAL
, "could not allocate");
574 for (i
= 1; argv
[i
]; i
++)
577 if (a
[0] == '-' && a
[1] == 'v' && a
[2] == '\0')
579 for (i
= 0; argv
[i
]; i
++)
580 fprintf (stderr
, "%s ", argv
[i
]);
581 fprintf (stderr
, "\n");
586 new_argv
[0] = argv
[0];
587 new_argv
[1] = at_args
;
592 for (i
= 0; new_argv
[i
]; i
++)
593 fprintf (stderr
, "%s ", new_argv
[i
]);
594 fprintf (stderr
, "\n");
598 pex
= pex_init (PEX_USE_PIPES
, "lto-wrapper", NULL
);
599 check (pex
!= NULL
, LDPL_FATAL
, "could not pex_init lto-wrapper");
601 errmsg
= pex_run (pex
, 0, new_argv
[0], new_argv
, NULL
, NULL
, &t
);
602 check (errmsg
== NULL
, LDPL_FATAL
, "could not run lto-wrapper");
603 check (t
== 0, LDPL_FATAL
, "could not run lto-wrapper");
605 wrapper_output
= pex_read_output (pex
, 0);
606 check (wrapper_output
, LDPL_FATAL
, "could not read lto-wrapper output");
608 add_output_files (wrapper_output
);
610 t
= pex_get_status (pex
, 1, &status
);
611 check (t
== 1, LDPL_FATAL
, "could not get lto-wrapper exit status");
612 check (WIFEXITED (status
) && WEXITSTATUS (status
) == 0, LDPL_FATAL
,
613 "lto-wrapper failed");
620 /* Pass the original files back to the linker. */
623 use_original_files (void)
626 for (i
= 0; i
< num_claimed_files
; i
++)
628 struct plugin_file_info
*info
= &claimed_files
[i
];
629 add_input_file (info
->name
);
634 /* Called by the linker once all symbols have been read. */
636 static enum ld_plugin_status
637 all_symbols_read_handler (void)
640 unsigned num_lto_args
= num_claimed_files
+ lto_wrapper_num_args
+ 3;
642 const char *linker_output_str
= NULL
;
643 const char **lto_arg_ptr
;
644 if (num_claimed_files
+ num_offload_files
== 0)
649 use_original_files ();
653 lto_argv
= (char **) xcalloc (sizeof (char *), num_lto_args
);
654 lto_arg_ptr
= (const char **) lto_argv
;
655 assert (lto_wrapper_argv
);
659 free_1 (claimed_files
, num_claimed_files
);
661 for (i
= 0; i
< lto_wrapper_num_args
; i
++)
662 *lto_arg_ptr
++ = lto_wrapper_argv
[i
];
664 assert (linker_output_set
);
665 switch (linker_output
)
668 linker_output_str
= "-flinker-output=rel";
671 linker_output_str
= "-flinker-output=dyn";
674 linker_output_str
= "-flinker-output=pie";
677 linker_output_str
= "-flinker-output=exec";
680 message (LDPL_FATAL
, "unsupported linker output %i", linker_output
);
683 *lto_arg_ptr
++ = xstrdup (linker_output_str
);
685 if (num_offload_files
> 0)
689 char *offload_objects_file_name
;
690 struct plugin_offload_file
*ofld
;
692 offload_objects_file_name
= make_temp_file (".ofldlist");
693 check (offload_objects_file_name
, LDPL_FATAL
,
694 "Failed to generate a temporary file name");
695 f
= fopen (offload_objects_file_name
, "w");
696 check (f
, LDPL_FATAL
, "could not open file with offload objects");
697 fprintf (f
, "%u\n", num_offload_files
);
699 /* Skip the dummy item at the start of the list. */
700 ofld
= offload_files
->next
;
703 fprintf (f
, "%s\n", ofld
->name
);
708 arg
= concat ("-foffload-objects=", offload_objects_file_name
, NULL
);
709 check (arg
, LDPL_FATAL
, "could not allocate");
710 *lto_arg_ptr
++ = arg
;
713 for (i
= 0; i
< num_claimed_files
; i
++)
715 struct plugin_file_info
*info
= &claimed_files
[i
];
717 *lto_arg_ptr
++ = info
->name
;
720 *lto_arg_ptr
++ = NULL
;
721 exec_lto_wrapper (lto_argv
);
725 /* --pass-through is not needed when using gold 1.11 or later. */
726 if (pass_through_items
&& gold_version
< 111)
729 for (i
= 0; i
< num_pass_through_items
; i
++)
731 if (strncmp (pass_through_items
[i
], "-l", 2) == 0)
732 add_input_library (pass_through_items
[i
] + 2);
734 add_input_file (pass_through_items
[i
]);
735 free (pass_through_items
[i
]);
736 pass_through_items
[i
] = NULL
;
738 free (pass_through_items
);
739 pass_through_items
= NULL
;
745 /* Remove temporary files at the end of the link. */
747 static enum ld_plugin_status
748 cleanup_handler (void)
756 if (arguments_file_name
)
758 t
= unlink (arguments_file_name
);
759 check (t
== 0, LDPL_FATAL
, "could not unlink arguments file");
762 for (i
= 0; i
< num_output_files
; i
++)
764 t
= unlink (output_files
[i
]);
765 check (t
== 0, LDPL_FATAL
, "could not unlink output file");
772 #define SWAP(type, a, b) \
773 do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
775 /* Compare two hash table entries */
777 static int eq_sym (const void *a
, const void *b
)
779 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
780 const struct ld_plugin_symbol
*bs
= (const struct ld_plugin_symbol
*)b
;
782 return !strcmp (as
->name
, bs
->name
);
787 static hashval_t
hash_sym (const void *a
)
789 const struct ld_plugin_symbol
*as
= (const struct ld_plugin_symbol
*)a
;
791 return htab_hash_string (as
->name
);
794 /* Determine how strong a symbol is */
796 static int symbol_strength (struct ld_plugin_symbol
*s
)
810 /* In the ld -r case we can get dups in the LTO symbol tables, where
811 the same symbol can have different resolutions (e.g. undefined and defined).
813 We have to keep that in the LTO symbol tables, but the dups confuse
814 gold and then finally gcc by supplying incorrect resolutions.
816 Problem is that the main gold symbol table doesn't know about subids
817 and does not distingush the same symbols in different states.
819 So we drop duplicates from the linker visible symbol table
820 and keep them in a private table. Then later do own symbol
821 resolution for the duplicated based on the results for the
824 Then when writing out the resolution file readd the dropped symbols.
826 XXX how to handle common? */
829 resolve_conflicts (struct plugin_symtab
*t
, struct plugin_symtab
*conflicts
)
831 htab_t symtab
= htab_create (t
->nsyms
, hash_sym
, eq_sym
, NULL
);
837 conflicts
->syms
= xmalloc (sizeof (struct ld_plugin_symbol
) * outlen
);
838 conflicts
->aux
= xmalloc (sizeof (struct sym_aux
) * outlen
);
840 /* Move all duplicate symbols into the auxiliary conflicts table. */
842 for (i
= 0; i
< t
->nsyms
; i
++)
844 struct ld_plugin_symbol
*s
= &t
->syms
[i
];
845 struct sym_aux
*aux
= &t
->aux
[i
];
848 slot
= htab_find_slot (symtab
, s
, INSERT
);
852 struct ld_plugin_symbol
*orig
= (struct ld_plugin_symbol
*)*slot
;
853 struct sym_aux
*orig_aux
= &t
->aux
[orig
- t
->syms
];
855 /* Always let the linker resolve the strongest symbol */
856 if (symbol_strength (orig
) < symbol_strength (s
))
858 SWAP (struct ld_plugin_symbol
, *orig
, *s
);
859 SWAP (uint32_t, orig_aux
->slot
, aux
->slot
);
860 SWAP (unsigned long long, orig_aux
->id
, aux
->id
);
861 /* Don't swap conflict chain pointer */
864 /* Move current symbol into the conflicts table */
865 cnf
= conflicts
->nsyms
++;
866 conflicts
->syms
[cnf
] = *s
;
867 conflicts
->aux
[cnf
] = *aux
;
868 aux
= &conflicts
->aux
[cnf
];
870 /* Update conflicts chain of the original symbol */
871 aux
->next_conflict
= orig_aux
->next_conflict
;
872 orig_aux
->next_conflict
= cnf
;
877 /* Remove previous duplicates in the main table */
884 /* Put original into the hash table */
885 *slot
= &t
->syms
[out
];
889 assert (conflicts
->nsyms
<= outlen
);
890 assert (conflicts
->nsyms
+ out
== t
->nsyms
);
893 htab_delete (symtab
);
896 /* Process one section of an object file. */
899 process_symtab (void *data
, const char *name
, off_t offset
, off_t length
)
901 struct plugin_objfile
*obj
= (struct plugin_objfile
*)data
;
903 char *secdatastart
, *secdata
;
905 if (strncmp (name
, LTO_SECTION_PREFIX
, LTO_SECTION_PREFIX_LEN
) != 0)
908 s
= strrchr (name
, '.');
910 sscanf (s
, ".%" PRI_LL
"x", &obj
->out
->id
);
911 secdata
= secdatastart
= xmalloc (length
);
912 offset
+= obj
->file
->offset
;
913 if (offset
!= lseek (obj
->file
->fd
, offset
, SEEK_SET
))
918 ssize_t got
= read (obj
->file
->fd
, secdata
, length
);
926 else if (errno
!= EINTR
)
933 translate (secdatastart
, secdata
, obj
->out
);
940 message (LDPL_FATAL
, "%s: corrupt object file", obj
->file
->name
);
941 /* Force claim_file_handler to abandon this file. */
947 /* Find an offload section of an object file. */
950 process_offload_section (void *data
, const char *name
, off_t offset
, off_t len
)
952 if (!strncmp (name
, OFFLOAD_SECTION
, OFFLOAD_SECTION_LEN
))
954 struct plugin_objfile
*obj
= (struct plugin_objfile
*) data
;
962 /* Callback used by gold to check if the plugin will claim FILE. Writes
963 the result in CLAIMED. */
965 static enum ld_plugin_status
966 claim_file_handler (const struct ld_plugin_input_file
*file
, int *claimed
)
968 enum ld_plugin_status status
;
969 struct plugin_objfile obj
;
970 struct plugin_file_info lto_file
;
974 memset (<o_file
, 0, sizeof (struct plugin_file_info
));
976 if (file
->offset
!= 0)
979 /* We pass the offset of the actual file, not the archive header.
980 Can't use PRIx64, because that's C99, so we have to print the
981 64-bit hex int as two 32-bit ones. */
983 lo
= file
->offset
& 0xffffffff;
984 hi
= ((int64_t)file
->offset
>> 32) & 0xffffffff;
985 t
= hi
? asprintf (&objname
, "%s@0x%x%08x", file
->name
, lo
, hi
)
986 : asprintf (&objname
, "%s@0x%x", file
->name
, lo
);
987 check (t
>= 0, LDPL_FATAL
, "asprintf failed");
988 lto_file
.name
= objname
;
992 lto_file
.name
= xstrdup (file
->name
);
994 lto_file
.handle
= file
->handle
;
1000 obj
.out
= <o_file
.symtab
;
1002 obj
.objfile
= simple_object_start_read (file
->fd
, file
->offset
, LTO_SEGMENT_NAME
,
1004 /* No file, but also no error code means unrecognized format; just skip it. */
1005 if (!obj
.objfile
&& !err
)
1009 errmsg
= simple_object_find_sections (obj
.objfile
, process_symtab
, &obj
, &err
);
1011 if (!obj
.objfile
|| errmsg
)
1014 message (LDPL_FATAL
, "%s: %s: %s", file
->name
, errmsg
,
1017 message (LDPL_FATAL
, "%s: %s", file
->name
, errmsg
);
1022 simple_object_find_sections (obj
.objfile
, process_offload_section
,
1025 if (obj
.found
== 0 && obj
.offload
== 0)
1029 resolve_conflicts (<o_file
.symtab
, <o_file
.conflicts
);
1033 status
= add_symbols (file
->handle
, lto_file
.symtab
.nsyms
,
1034 lto_file
.symtab
.syms
);
1035 check (status
== LDPS_OK
, LDPL_FATAL
, "could not add symbols");
1037 num_claimed_files
++;
1039 xrealloc (claimed_files
,
1040 num_claimed_files
* sizeof (struct plugin_file_info
));
1041 claimed_files
[num_claimed_files
- 1] = lto_file
;
1046 if (offload_files
== NULL
)
1048 /* Add dummy item to the start of the list. */
1049 offload_files
= xmalloc (sizeof (struct plugin_offload_file
));
1050 offload_files
->name
= NULL
;
1051 offload_files
->next
= NULL
;
1052 offload_files_last
= offload_files
;
1055 /* If this is an LTO file without offload, and it is the first LTO file, save
1056 the pointer to the last offload file in the list. Further offload LTO
1057 files will be inserted after it, if any. */
1058 if (*claimed
&& obj
.offload
== 0 && offload_files_last_lto
== NULL
)
1059 offload_files_last_lto
= offload_files_last
;
1061 if (obj
.offload
== 1)
1063 /* Add file to the list. The order must be exactly the same as the final
1064 order after recompilation and linking, otherwise host and target tables
1065 with addresses wouldn't match. If a static library contains both LTO
1066 and non-LTO objects, ld and gold link them in a different order. */
1067 struct plugin_offload_file
*ofld
1068 = xmalloc (sizeof (struct plugin_offload_file
));
1069 ofld
->name
= lto_file
.name
;
1072 if (*claimed
&& offload_files_last_lto
== NULL
&& file
->offset
!= 0
1073 && gold_version
== -1)
1075 /* ld only: insert first LTO file from the archive after the last real
1076 object file immediately preceding the archive, or at the begin of
1077 the list if there was no real objects before archives. */
1078 if (offload_files_last_obj
!= NULL
)
1080 ofld
->next
= offload_files_last_obj
->next
;
1081 offload_files_last_obj
->next
= ofld
;
1085 ofld
->next
= offload_files
->next
;
1086 offload_files
->next
= ofld
;
1089 else if (*claimed
&& offload_files_last_lto
!= NULL
)
1091 /* Insert LTO file after the last LTO file in the list. */
1092 ofld
->next
= offload_files_last_lto
->next
;
1093 offload_files_last_lto
->next
= ofld
;
1096 /* Add non-LTO file or first non-archive LTO file to the end of the
1098 offload_files_last
->next
= ofld
;
1100 if (ofld
->next
== NULL
)
1101 offload_files_last
= ofld
;
1102 if (file
->offset
== 0)
1103 offload_files_last_obj
= ofld
;
1105 offload_files_last_lto
= ofld
;
1106 num_offload_files
++;
1112 free (lto_file
.name
);
1116 simple_object_release_read (obj
.objfile
);
1121 /* Parse the plugin options. */
1124 process_option (const char *option
)
1126 if (strcmp (option
, "-debug") == 0)
1128 else if (strcmp (option
, "-nop") == 0)
1130 else if (!strncmp (option
, "-pass-through=", strlen("-pass-through=")))
1132 num_pass_through_items
++;
1133 pass_through_items
= xrealloc (pass_through_items
,
1134 num_pass_through_items
* sizeof (char *));
1135 pass_through_items
[num_pass_through_items
- 1] =
1136 xstrdup (option
+ strlen ("-pass-through="));
1138 else if (!strncmp (option
, "-sym-style=", sizeof ("-sym-style=") - 1))
1140 switch (option
[sizeof ("-sym-style=") - 1])
1143 sym_style
= ss_win32
;
1146 sym_style
= ss_uscore
;
1149 sym_style
= ss_none
;
1156 char *opt
= xstrdup (option
);
1157 lto_wrapper_num_args
+= 1;
1158 size
= lto_wrapper_num_args
* sizeof (char *);
1159 lto_wrapper_argv
= (char **) xrealloc (lto_wrapper_argv
, size
);
1160 lto_wrapper_argv
[lto_wrapper_num_args
- 1] = opt
;
1161 if (strncmp (option
, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
1162 resolution_file
= opt
+ sizeof ("-fresolution=") - 1;
1166 /* Called by gold after loading the plugin. TV is the transfer vector. */
1168 enum ld_plugin_status
1169 onload (struct ld_plugin_tv
*tv
)
1171 struct ld_plugin_tv
*p
;
1172 enum ld_plugin_status status
;
1180 message
= p
->tv_u
.tv_message
;
1182 case LDPT_REGISTER_CLAIM_FILE_HOOK
:
1183 register_claim_file
= p
->tv_u
.tv_register_claim_file
;
1185 case LDPT_ADD_SYMBOLS
:
1186 add_symbols
= p
->tv_u
.tv_add_symbols
;
1188 case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK
:
1189 register_all_symbols_read
= p
->tv_u
.tv_register_all_symbols_read
;
1191 case LDPT_GET_SYMBOLS_V2
:
1192 get_symbols_v2
= p
->tv_u
.tv_get_symbols
;
1194 case LDPT_GET_SYMBOLS
:
1195 get_symbols
= p
->tv_u
.tv_get_symbols
;
1197 case LDPT_REGISTER_CLEANUP_HOOK
:
1198 register_cleanup
= p
->tv_u
.tv_register_cleanup
;
1200 case LDPT_ADD_INPUT_FILE
:
1201 add_input_file
= p
->tv_u
.tv_add_input_file
;
1203 case LDPT_ADD_INPUT_LIBRARY
:
1204 add_input_library
= p
->tv_u
.tv_add_input_library
;
1207 process_option (p
->tv_u
.tv_string
);
1209 case LDPT_GOLD_VERSION
:
1210 gold_version
= p
->tv_u
.tv_val
;
1212 case LDPT_LINKER_OUTPUT
:
1213 linker_output
= (enum ld_plugin_output_file_type
) p
->tv_u
.tv_val
;
1214 linker_output_set
= 1;
1222 check (register_claim_file
, LDPL_FATAL
, "register_claim_file not found");
1223 check (add_symbols
, LDPL_FATAL
, "add_symbols not found");
1224 status
= register_claim_file (claim_file_handler
);
1225 check (status
== LDPS_OK
, LDPL_FATAL
,
1226 "could not register the claim_file callback");
1228 if (register_cleanup
)
1230 status
= register_cleanup (cleanup_handler
);
1231 check (status
== LDPS_OK
, LDPL_FATAL
,
1232 "could not register the cleanup callback");
1235 if (register_all_symbols_read
)
1237 check (get_symbols
, LDPL_FATAL
, "get_symbols not found");
1238 status
= register_all_symbols_read (all_symbols_read_handler
);
1239 check (status
== LDPS_OK
, LDPL_FATAL
,
1240 "could not register the all_symbols_read callback");
1243 /* Support -fno-use-linker-plugin by failing to load the plugin
1244 for the case where it is auto-loaded by BFD. */
1245 char *collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1246 if (collect_gcc_options
1247 && strstr (collect_gcc_options
, "'-fno-use-linker-plugin'"))