OSDN Git Service

2010-12-01 Kai Tietz <kai.tietz@onevision.com>
[pf3gnuchains/gcc-fork.git] / lto-plugin / lto-plugin.c
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).
4
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)
8 any later version.
9
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.
14
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/>.  */
18
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.
21
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.
26
27    More information at http://gcc.gnu.org/wiki/whopr/driver.
28
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.  */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 #if HAVE_STDINT_H
39 #include <stdint.h>
40 #endif
41 #include <assert.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <inttypes.h>
46 #include <sys/stat.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <sys/types.h>
50 #ifdef HAVE_SYS_WAIT_H
51 #include <sys/wait.h>
52 #endif
53 #include <libiberty.h>
54 #include <hashtab.h>
55 #include "../gcc/lto/common.h"
56 #include "simple-object.h"
57 #include "plugin-api.h"
58
59 /* Handle opening elf files on hosts, such as Windows, that may use
60    text file handling that will break binary access.  */
61 #ifndef O_BINARY
62 # define O_BINARY 0
63 #endif
64
65 /* Segment name for LTO sections.  This is only used for Mach-O.
66    FIXME: This needs to be kept in sync with darwin.c.  */
67
68 #define LTO_SEGMENT_NAME "__GNU_LTO"
69
70 /* LTO magic section name.  */
71
72 #define LTO_SECTION_PREFIX      ".gnu.lto_.symtab"
73 #define LTO_SECTION_PREFIX_LEN  (sizeof (LTO_SECTION_PREFIX) - 1)
74
75 /* The part of the symbol table the plugin has to keep track of. Note that we
76    must keep SYMS until all_symbols_read is called to give the linker time to
77    copy the symbol information. */
78
79 struct sym_aux
80 {
81   uint32_t slot;
82   unsigned id;
83   unsigned next_conflict;
84 };
85
86 struct plugin_symtab
87 {
88   int nsyms;
89   struct sym_aux *aux;
90   struct ld_plugin_symbol *syms;
91   unsigned id;
92 };
93
94 /* Encapsulates object file data during symbol scan.  */
95 struct plugin_objfile
96 {
97   int found;
98   simple_object_read *objfile;
99   struct plugin_symtab *out;
100   const struct ld_plugin_input_file *file;
101 };
102
103 /* All that we have to remember about a file. */
104
105 struct plugin_file_info
106 {
107   char *name;
108   void *handle;
109   struct plugin_symtab symtab;
110   struct plugin_symtab conflicts;
111 };
112
113 /* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
114    stdio file streams, we do simple label translation here.  */
115
116 enum symbol_style
117 {
118   ss_none,      /* No underscore prefix. */
119   ss_win32,     /* Underscore prefix any symbol not beginning with '@'.  */
120   ss_uscore,    /* Underscore prefix all symbols.  */
121 };
122
123 static char *arguments_file_name;
124 static ld_plugin_register_claim_file register_claim_file;
125 static ld_plugin_register_all_symbols_read register_all_symbols_read;
126 static ld_plugin_get_symbols get_symbols;
127 static ld_plugin_register_cleanup register_cleanup;
128 static ld_plugin_add_input_file add_input_file;
129 static ld_plugin_add_input_library add_input_library;
130 static ld_plugin_message message;
131 static ld_plugin_add_symbols add_symbols;
132
133 static struct plugin_file_info *claimed_files = NULL;
134 static unsigned int num_claimed_files = 0;
135
136 static char **output_files = NULL;
137 static unsigned int num_output_files = 0;
138
139 static char **lto_wrapper_argv;
140 static int lto_wrapper_num_args;
141
142 static char **pass_through_items = NULL;
143 static unsigned int num_pass_through_items;
144
145 static char debug;
146 static char nop;
147 static char *resolution_file = NULL;
148
149 /* Set by default from configure.ac, but can be overridden at runtime
150    by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
151    (in fact, only first letter of style arg is checked.)  */
152 static enum symbol_style sym_style = SYM_STYLE;
153
154 static void
155 check_1 (int gate, enum ld_plugin_level level, const char *text)
156 {
157   if (gate)
158     return;
159
160   if (message)
161     message (level, text);
162   else
163     {
164       /* If there is no nicer way to inform the user, fallback to stderr. */
165       fprintf (stderr, "%s\n", text);
166       if (level == LDPL_FATAL)
167         abort ();
168     }
169 }
170
171 /* This little wrapper allows check to be called with a non-integer
172    first argument, such as a pointer that must be non-NULL.  We can't
173    use c99 bool type to coerce it into range, so we explicitly test.  */
174 #define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
175
176 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
177    by P and the result is written in ENTRY. The slot number is stored in SLOT.
178    Returns the address of the next entry. */
179
180 static char *
181 parse_table_entry (char *p, struct ld_plugin_symbol *entry, 
182                    struct sym_aux *aux)
183 {
184   unsigned char t;
185   enum ld_plugin_symbol_kind translate_kind[] =
186     {
187       LDPK_DEF,
188       LDPK_WEAKDEF,
189       LDPK_UNDEF,
190       LDPK_WEAKUNDEF,
191       LDPK_COMMON
192     };
193
194   enum ld_plugin_symbol_visibility translate_visibility[] =
195     {
196       LDPV_DEFAULT,
197       LDPV_PROTECTED,
198       LDPV_INTERNAL,
199       LDPV_HIDDEN
200     };
201
202   switch (sym_style)
203     {
204     case ss_win32:
205       if (p[0] == '@')
206         {
207     /* cf. Duff's device.  */
208     case ss_none:
209           entry->name = xstrdup (p);
210           break;
211         }
212     /* FALL-THROUGH.  */
213     case ss_uscore:
214       entry->name = concat ("_", p, NULL);
215       break;
216     default:
217       check (0, LDPL_FATAL, "invalid symbol style requested");
218       break;
219     }
220   while (*p)
221     p++;
222   p++;
223
224   entry->version = NULL;
225
226   entry->comdat_key = p;
227   while (*p)
228     p++;
229   p++;
230
231   if (strlen (entry->comdat_key) == 0)
232     entry->comdat_key = NULL;
233   else
234     entry->comdat_key = xstrdup (entry->comdat_key);
235
236   t = *p;
237   check (t <= 4, LDPL_FATAL, "invalid symbol kind found");
238   entry->def = translate_kind[t];
239   p++;
240
241   t = *p;
242   check (t <= 3, LDPL_FATAL, "invalid symbol visibility found");
243   entry->visibility = translate_visibility[t];
244   p++;
245
246   entry->size = *(uint64_t *) p;
247   p += 8;
248
249   aux->slot = *(uint32_t *) p;
250   p += 4;
251
252   entry->resolution = LDPR_UNKNOWN;
253
254   aux->next_conflict = -1;
255
256   return p;
257 }
258
259 /* Translate the IL symbol table located between DATA and END. Append the
260    slots and symbols to OUT. */
261
262 static void
263 translate (char *data, char *end, struct plugin_symtab *out)
264 {
265   struct sym_aux *aux;
266   struct ld_plugin_symbol *syms = NULL;
267   int n, len;
268
269   /* This overestimates the output buffer sizes, but at least 
270      the algorithm is O(1) now. */
271
272   len = (end - data)/8 + out->nsyms + 1;
273   syms = xrealloc (out->syms, len * sizeof (struct ld_plugin_symbol));
274   aux = xrealloc (out->aux, len * sizeof (struct sym_aux));
275   
276   for (n = out->nsyms; data < end; n++) 
277     { 
278       aux[n].id = out->id; 
279       data = parse_table_entry (data, &syms[n], &aux[n]);
280     }
281
282   assert(n < len);
283
284   out->nsyms = n;
285   out->syms = syms;
286   out->aux = aux;
287 }
288
289 /* Free all memory that is no longer needed after writing the symbol
290    resolution. */
291
292 static void
293 free_1 (void)
294 {
295   unsigned int i;
296   for (i = 0; i < num_claimed_files; i++)
297     {
298       struct plugin_file_info *info = &claimed_files[i];
299       struct plugin_symtab *symtab = &info->symtab;
300       unsigned int j;
301       for (j = 0; j < symtab->nsyms; j++)
302         {
303           struct ld_plugin_symbol *s = &symtab->syms[j];
304           free (s->name);
305           if (s->comdat_key)
306             free (s->comdat_key);
307         }
308       free (symtab->syms);
309       symtab->syms = NULL;
310     }
311 }
312
313 /* Free all remaining memory. */
314
315 static void
316 free_2 (void)
317 {
318   unsigned int i;
319   for (i = 0; i < num_claimed_files; i++)
320     {
321       struct plugin_file_info *info = &claimed_files[i];
322       struct plugin_symtab *symtab = &info->symtab;
323       free (symtab->aux);
324       free (info->name);
325     }
326
327   for (i = 0; i < num_output_files; i++)
328     free (output_files[i]);
329   free (output_files);
330
331   free (claimed_files);
332   claimed_files = NULL;
333   num_claimed_files = 0;
334
335   if (arguments_file_name)
336     free (arguments_file_name);
337   arguments_file_name = NULL;
338 }
339
340 /* Dump SYMTAB to resolution file F. */
341
342 static void
343 dump_symtab (FILE *f, struct plugin_symtab *symtab)
344 {
345   unsigned j;
346
347   for (j = 0; j < symtab->nsyms; j++)
348     {
349       uint32_t slot = symtab->aux[j].slot;
350       unsigned int resolution = symtab->syms[j].resolution;
351       
352       assert (resolution != LDPR_UNKNOWN);
353
354       fprintf (f, "%u %x %s %s\n", (unsigned int) slot, symtab->aux[j].id,
355                lto_resolution_str[resolution], 
356                symtab->syms[j].name);
357     }
358 }
359
360 /* Finish the conflicts' resolution information after the linker resolved
361    the original symbols */
362
363 static void
364 finish_conflict_resolution (struct plugin_symtab *symtab, 
365                            struct plugin_symtab *conflicts)
366 {
367   int i, j;
368
369   if (conflicts->nsyms == 0)
370     return;
371
372   for (i = 0; i < symtab->nsyms; i++)
373     { 
374       int resolution = LDPR_UNKNOWN;
375
376       if (symtab->aux[i].next_conflict == -1)
377         continue;
378
379       switch (symtab->syms[i].def) 
380         {
381         case LDPK_DEF:
382         case LDPK_COMMON: /* ??? */
383           resolution = LDPR_RESOLVED_IR; 
384           break;
385         case LDPK_WEAKDEF:
386           resolution = LDPR_PREEMPTED_IR;
387           break;
388         case LDPK_UNDEF:
389         case LDPK_WEAKUNDEF:
390           resolution = symtab->syms[i].resolution;
391           break;
392         default:
393           assert (0);
394         }
395
396       assert (resolution != LDPR_UNKNOWN);
397
398       for (j = symtab->aux[i].next_conflict; 
399            j != -1; 
400            j = conflicts->aux[j].next_conflict)
401         conflicts->syms[j].resolution = resolution;
402     }
403 }
404
405 /* Free symbol table SYMTAB. */
406
407 static void
408 free_symtab (struct plugin_symtab *symtab)
409 {
410   free (symtab->syms);
411   symtab->syms = NULL;
412   free (symtab->aux);
413   symtab->aux = NULL;
414 }
415
416 /*  Writes the relocations to disk. */
417
418 static void
419 write_resolution (void)
420 {
421   unsigned int i;
422   FILE *f;
423
424   check (resolution_file, LDPL_FATAL, "resolution file not specified");
425   f = fopen (resolution_file, "w");
426   check (f, LDPL_FATAL, "could not open file");
427
428   fprintf (f, "%d\n", num_claimed_files);
429
430   for (i = 0; i < num_claimed_files; i++)
431     {
432       struct plugin_file_info *info = &claimed_files[i];
433       struct plugin_symtab *symtab = &info->symtab;
434       struct ld_plugin_symbol *syms = symtab->syms;
435
436       get_symbols (info->handle, symtab->nsyms, syms);
437
438       finish_conflict_resolution (symtab, &info->conflicts);
439
440       fprintf (f, "%s %d\n", info->name, symtab->nsyms + info->conflicts.nsyms);
441       dump_symtab (f, symtab);
442       if (info->conflicts.nsyms)
443         {
444           dump_symtab (f, &info->conflicts);
445           free_symtab (&info->conflicts);
446         }
447     }
448   fclose (f);
449 }
450
451 /* Pass files generated by the lto-wrapper to the linker. FD is lto-wrapper's
452    stdout. */
453
454 static void
455 add_output_files (FILE *f)
456 {
457   for (;;)
458     {
459       const unsigned piece = 32;
460       char *buf, *s = xmalloc (piece);
461       size_t len;
462
463       buf = s;
464 cont:
465       if (!fgets (buf, piece, f))
466         {
467           free (s);
468           break;
469         }
470       len = strlen (s);
471       if (s[len - 1] != '\n')
472         {
473           s = xrealloc (s, len + piece);
474           buf = s + len;
475           goto cont;
476         }
477       s[len - 1] = '\0';
478
479       num_output_files++;
480       output_files
481         = xrealloc (output_files, num_output_files * sizeof (char *));
482       output_files[num_output_files - 1] = s;
483       add_input_file (output_files[num_output_files - 1]);
484     }
485 }
486
487 /* Execute the lto-wrapper. ARGV[0] is the binary. The rest of ARGV is the
488    argument list. */
489
490 static void
491 exec_lto_wrapper (char *argv[])
492 {
493   int t, i;
494   int status;
495   char *at_args;
496   FILE *args;
497   FILE *wrapper_output;
498   char *new_argv[3];
499   struct pex_obj *pex;
500   const char *errmsg;
501
502   /* Write argv to a file to avoid a command line that is too long. */
503   arguments_file_name = make_temp_file ("");
504   check (arguments_file_name, LDPL_FATAL,
505          "Failed to generate a temorary file name");
506
507   args = fopen (arguments_file_name, "w");
508   check (args, LDPL_FATAL, "could not open arguments file");
509
510   t = writeargv (&argv[1], args);
511   check (t == 0, LDPL_FATAL, "could not write arguments");
512   t = fclose (args);
513   check (t == 0, LDPL_FATAL, "could not close arguments file");
514
515   at_args = concat ("@", arguments_file_name, NULL);
516   check (at_args, LDPL_FATAL, "could not allocate");
517
518   for (i = 1; argv[i]; i++)
519     {
520       char *a = argv[i];
521       if (a[0] == '-' && a[1] == 'v' && a[2] == '\0')
522         {
523           for (i = 0; argv[i]; i++)
524             fprintf (stderr, "%s ", argv[i]);
525           fprintf (stderr, "\n");
526           break;
527         }
528     }
529
530   new_argv[0] = argv[0];
531   new_argv[1] = at_args;
532   new_argv[2] = NULL;
533
534   if (debug)
535     {
536       for (i = 0; new_argv[i]; i++)
537         fprintf (stderr, "%s ", new_argv[i]);
538       fprintf (stderr, "\n");
539     }
540
541
542   pex = pex_init (PEX_USE_PIPES, "lto-wrapper", NULL);
543   check (pex != NULL, LDPL_FATAL, "could not pex_init lto-wrapper");
544
545   errmsg = pex_run (pex, 0, new_argv[0], new_argv, NULL, NULL, &t);
546   check (errmsg == NULL, LDPL_FATAL, "could not run lto-wrapper");
547   check (t == 0, LDPL_FATAL, "could not run lto-wrapper");
548
549   wrapper_output = pex_read_output (pex, 0);
550   check (wrapper_output, LDPL_FATAL, "could not read lto-wrapper output");
551
552   add_output_files (wrapper_output);
553
554   t = pex_get_status (pex, 1, &status);
555   check (t == 1, LDPL_FATAL, "could not get lto-wrapper exit status");
556   check (WIFEXITED (status) && WEXITSTATUS (status) == 0, LDPL_FATAL,
557          "lto-wrapper failed");
558
559   pex_free (pex);
560
561   free (at_args);
562 }
563
564 /* Pass the original files back to the linker. */
565
566 static void
567 use_original_files (void)
568 {
569   unsigned i;
570   for (i = 0; i < num_claimed_files; i++)
571     {
572       struct plugin_file_info *info = &claimed_files[i];
573       add_input_file (info->name);
574     }
575 }
576
577
578 /* Called by the linker once all symbols have been read. */
579
580 static enum ld_plugin_status
581 all_symbols_read_handler (void)
582 {
583   unsigned i;
584   unsigned num_lto_args = num_claimed_files + lto_wrapper_num_args + 1;
585   char **lto_argv;
586   const char **lto_arg_ptr;
587   if (num_claimed_files == 0)
588     return LDPS_OK;
589
590   if (nop)
591     {
592       use_original_files ();
593       return LDPS_OK;
594     }
595
596   lto_argv = (char **) xcalloc (sizeof (char *), num_lto_args);
597   lto_arg_ptr = (const char **) lto_argv;
598   assert (lto_wrapper_argv);
599
600   write_resolution ();
601
602   free_1 ();
603
604   for (i = 0; i < lto_wrapper_num_args; i++)
605     *lto_arg_ptr++ = lto_wrapper_argv[i];
606
607   for (i = 0; i < num_claimed_files; i++)
608     {
609       struct plugin_file_info *info = &claimed_files[i];
610
611       *lto_arg_ptr++ = info->name;
612     }
613
614   *lto_arg_ptr++ = NULL;
615   exec_lto_wrapper (lto_argv);
616
617   free (lto_argv);
618
619   if (pass_through_items)
620     {
621       unsigned int i;
622       for (i = 0; i < num_pass_through_items; i++)
623         {
624           if (strncmp (pass_through_items[i], "-l", 2) == 0)
625             add_input_library (pass_through_items[i] + 2);
626           else
627             add_input_file (pass_through_items[i]);
628           free (pass_through_items[i]);
629           pass_through_items[i] = NULL;
630         }
631       free (pass_through_items);
632       pass_through_items = NULL;
633     }
634
635   return LDPS_OK;
636 }
637
638 /* Remove temporary files at the end of the link. */
639
640 static enum ld_plugin_status
641 cleanup_handler (void)
642 {
643   unsigned int i;
644   int t;
645
646   if (debug)
647     return LDPS_OK;
648
649   if (arguments_file_name)
650     {
651       t = unlink (arguments_file_name);
652       check (t == 0, LDPL_FATAL, "could not unlink arguments file");
653     }
654
655   for (i = 0; i < num_output_files; i++)
656     {
657       t = unlink (output_files[i]);
658       check (t == 0, LDPL_FATAL, "could not unlink output file");
659     }
660
661   free_2 ();
662   return LDPS_OK;
663 }
664
665 #define SWAP(type, a, b) \
666   do { type tmp_; tmp_ = (a); (a) = (b); (b) = tmp_; } while(0)
667
668 /* Compare two hash table entries */
669
670 static int eq_sym (const void *a, const void *b)
671 {
672   const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
673   const struct ld_plugin_symbol *bs = (const struct ld_plugin_symbol *)b;
674
675   return !strcmp (as->name, bs->name);
676 }
677
678 /* Hash a symbol */
679
680 static hashval_t hash_sym (const void *a)
681 {
682   const struct ld_plugin_symbol *as = (const struct ld_plugin_symbol *)a;
683
684   return htab_hash_string (as->name);
685 }
686
687 /* Determine how strong a symbol is */
688
689 static int symbol_strength (struct ld_plugin_symbol *s)
690 {
691   switch (s->def) 
692     { 
693     case LDPK_UNDEF:
694     case LDPK_WEAKUNDEF:
695       return 0;
696     case LDPK_WEAKDEF:
697       return 1;
698     default:
699       return 2;
700     }
701 }
702
703 /* In the ld -r case we can get dups in the LTO symbol tables, where
704    the same symbol can have different resolutions (e.g. undefined and defined).
705
706    We have to keep that in the LTO symbol tables, but the dups confuse
707    gold and then finally gcc by supplying incorrect resolutions.
708
709    Problem is that the main gold symbol table doesn't know about subids
710    and does not distingush the same symbols in different states.
711
712    So we drop duplicates from the linker visible symbol table
713    and keep them in a private table. Then later do own symbol
714    resolution for the duplicated based on the results for the
715    originals.
716
717    Then when writing out the resolution file readd the dropped symbols.
718    
719    XXX how to handle common? */
720
721 static void
722 resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
723 {
724   htab_t symtab = htab_create (t->nsyms, hash_sym, eq_sym, NULL);
725   int i;
726   int out;
727   int outlen;
728
729   outlen = t->nsyms;
730   conflicts->syms = xmalloc (sizeof (struct ld_plugin_symbol) * outlen);
731   conflicts->aux = xmalloc (sizeof (struct sym_aux) * outlen);
732
733   /* Move all duplicate symbols into the auxillary conflicts table. */
734   out = 0;
735   for (i = 0; i < t->nsyms; i++) 
736     {
737       struct ld_plugin_symbol *s = &t->syms[i];
738       struct sym_aux *aux = &t->aux[i];
739       void **slot;
740
741       slot = htab_find_slot (symtab, s, INSERT);
742       if (*slot != NULL)
743         {
744           int cnf;
745           struct ld_plugin_symbol *orig = (struct ld_plugin_symbol *)*slot;
746           struct sym_aux *orig_aux = &t->aux[orig - t->syms];
747
748           /* Always let the linker resolve the strongest symbol */
749           if (symbol_strength (orig) < symbol_strength (s)) 
750             {
751               SWAP (struct ld_plugin_symbol, *orig, *s);
752               SWAP (uint32_t, orig_aux->slot, aux->slot);
753               SWAP (unsigned, orig_aux->id, aux->id);
754               /* Don't swap conflict chain pointer */
755             } 
756
757           /* Move current symbol into the conflicts table */
758           cnf = conflicts->nsyms++;
759           conflicts->syms[cnf] = *s;
760           conflicts->aux[cnf] = *aux;
761           aux = &conflicts->aux[cnf];
762
763           /* Update conflicts chain of the original symbol */
764           aux->next_conflict = orig_aux->next_conflict;
765           orig_aux->next_conflict = cnf;
766
767           continue;
768         }
769
770       /* Remove previous duplicates in the main table */
771       if (out < i)
772         {
773           t->syms[out] = *s;
774           t->aux[out] = *aux;
775         }
776
777       /* Put original into the hash table */
778       *slot = &t->syms[out];
779       out++;
780     }
781
782   assert (conflicts->nsyms <= outlen);
783   assert (conflicts->nsyms + out == t->nsyms);
784   
785   t->nsyms = out;
786   htab_delete (symtab);
787 }
788
789 /* Process one section of an object file.  */
790
791 static int 
792 process_symtab (void *data, const char *name, off_t offset, off_t length)
793 {
794   struct plugin_objfile *obj = (struct plugin_objfile *)data;
795   char *s;
796   char *secdata;
797
798   if (strncmp (name, LTO_SECTION_PREFIX, LTO_SECTION_PREFIX_LEN) != 0)
799     return 1;
800
801   s = strrchr (name, '.');
802   if (s)
803     sscanf (s, ".%x", &obj->out->id);
804   secdata = xmalloc (length);
805   offset += obj->file->offset;
806   if (offset != lseek (obj->file->fd, offset, SEEK_SET)
807         || length != read (obj->file->fd, secdata, length))
808     {
809       if (message)
810         message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
811       /* Force claim_file_handler to abandon this file.  */
812       obj->found = 0;
813       free (secdata);
814       return 0;
815     }
816
817   translate (secdata, secdata + length, obj->out);
818   obj->found++;
819   free (secdata);
820   return 1;
821 }
822
823 /* Callback used by gold to check if the plugin will claim FILE. Writes
824    the result in CLAIMED. */
825
826 static enum ld_plugin_status
827 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
828 {
829   enum ld_plugin_status status;
830   struct plugin_objfile obj;
831   struct plugin_file_info lto_file;
832   int err;
833   const char *errmsg;
834
835   memset (&lto_file, 0, sizeof (struct plugin_file_info));
836
837   if (file->offset != 0)
838     {
839       char *objname;
840       /* We pass the offset of the actual file, not the archive header.
841          Can't use PRIx64, because that's C99, so we have to print the
842          64-bit hex int as two 32-bit ones. */
843       int lo, hi;
844       lo = file->offset & 0xffffffff;
845       hi = ((int64_t)file->offset >> 32) & 0xffffffff;
846       int t = hi ? asprintf (&objname, "%s@0x%x%08x", file->name, lo, hi)
847                 : asprintf (&objname, "%s@0x%x", file->name, lo);
848       check (t >= 0, LDPL_FATAL, "asprintf failed");
849       lto_file.name = objname;
850     }
851   else
852     {
853       lto_file.name = xstrdup (file->name);
854     }
855   lto_file.handle = file->handle;
856
857   *claimed = 0;
858   obj.file = file;
859   obj.found = 0;
860   obj.out = &lto_file.symtab;
861   errmsg = NULL;
862   obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
863                         &errmsg, &err);
864   /* No file, but also no error code means unrecognized format; just skip it.  */
865   if (!obj.objfile && !err)
866     goto err;
867
868   if (obj.objfile)
869     errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj, &err);
870
871   if (!obj.objfile || errmsg)
872     {
873       if (err && message)
874         message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
875                 xstrerror (err));
876       else if (message)
877         message (LDPL_FATAL, "%s: %s", file->name, errmsg);
878       goto err;
879     }
880
881   if (obj.found == 0)
882     goto err;
883
884   if (obj.found > 1)
885     resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
886
887   status = add_symbols (file->handle, lto_file.symtab.nsyms,
888                         lto_file.symtab.syms);
889   check (status == LDPS_OK, LDPL_FATAL, "could not add symbols");
890
891   *claimed = 1;
892   num_claimed_files++;
893   claimed_files =
894     xrealloc (claimed_files,
895               num_claimed_files * sizeof (struct plugin_file_info));
896   claimed_files[num_claimed_files - 1] = lto_file;
897
898   goto cleanup;
899
900  err:
901   free (lto_file.name);
902
903  cleanup:
904   if (obj.objfile)
905     simple_object_release_read (obj.objfile);
906
907   return LDPS_OK;
908 }
909
910 /* Parse the plugin options. */
911
912 static void
913 process_option (const char *option)
914 {
915   if (strcmp (option, "-debug") == 0)
916     debug = 1;
917   else if (strcmp (option, "-nop") == 0)
918     nop = 1;
919   else if (!strncmp (option, "-pass-through=", strlen("-pass-through=")))
920     {
921       num_pass_through_items++;
922       pass_through_items = xrealloc (pass_through_items,
923                                      num_pass_through_items * sizeof (char *));
924       pass_through_items[num_pass_through_items - 1] =
925           xstrdup (option + strlen ("-pass-through="));
926     }
927   else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
928     {
929       switch (option[sizeof ("-sym-style=") - 1])
930         {
931         case 'w':
932           sym_style = ss_win32;
933           break;
934         case 'u':
935           sym_style = ss_uscore;
936           break;
937         default:
938           sym_style = ss_none;
939           break;
940         }
941     }
942   else
943     {
944       int size;
945       char *opt = xstrdup (option);
946       lto_wrapper_num_args += 1;
947       size = lto_wrapper_num_args * sizeof (char *);
948       lto_wrapper_argv = (char **) xrealloc (lto_wrapper_argv, size);
949       lto_wrapper_argv[lto_wrapper_num_args - 1] = opt;
950       if (strncmp (option, "-fresolution=", sizeof ("-fresolution=") - 1) == 0)
951         resolution_file = opt + sizeof ("-fresolution=") - 1;
952     }
953 }
954
955 /* Called by gold after loading the plugin. TV is the transfer vector. */
956
957 enum ld_plugin_status
958 onload (struct ld_plugin_tv *tv)
959 {
960   struct ld_plugin_tv *p;
961   enum ld_plugin_status status;
962
963   p = tv;
964   while (p->tv_tag)
965     {
966       switch (p->tv_tag)
967         {
968         case LDPT_MESSAGE:
969           message = p->tv_u.tv_message;
970           break;
971         case LDPT_REGISTER_CLAIM_FILE_HOOK:
972           register_claim_file = p->tv_u.tv_register_claim_file;
973           break;
974         case LDPT_ADD_SYMBOLS:
975           add_symbols = p->tv_u.tv_add_symbols;
976           break;
977         case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
978           register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
979           break;
980         case LDPT_GET_SYMBOLS:
981           get_symbols = p->tv_u.tv_get_symbols;
982           break;
983         case LDPT_REGISTER_CLEANUP_HOOK:
984           register_cleanup = p->tv_u.tv_register_cleanup;
985           break;
986         case LDPT_ADD_INPUT_FILE:
987           add_input_file = p->tv_u.tv_add_input_file;
988           break;
989         case LDPT_ADD_INPUT_LIBRARY:
990           add_input_library = p->tv_u.tv_add_input_library;
991           break;
992         case LDPT_OPTION:
993           process_option (p->tv_u.tv_string);
994           break;
995         default:
996           break;
997         }
998       p++;
999     }
1000
1001   check (register_claim_file, LDPL_FATAL, "register_claim_file not found");
1002   check (add_symbols, LDPL_FATAL, "add_symbols not found");
1003   status = register_claim_file (claim_file_handler);
1004   check (status == LDPS_OK, LDPL_FATAL,
1005          "could not register the claim_file callback");
1006
1007   if (register_cleanup)
1008     {
1009       status = register_cleanup (cleanup_handler);
1010       check (status == LDPS_OK, LDPL_FATAL,
1011              "could not register the cleanup callback");
1012     }
1013
1014   if (register_all_symbols_read)
1015     {
1016       check (get_symbols, LDPL_FATAL, "get_symbols not found");
1017       status = register_all_symbols_read (all_symbols_read_handler);
1018       check (status == LDPS_OK, LDPL_FATAL,
1019              "could not register the all_symbols_read callback");
1020     }
1021
1022   return LDPS_OK;
1023 }