OSDN Git Service

Backported from mainline
[pf3gnuchains/gcc-fork.git] / lto-plugin / lto-plugin.c
index dc51b48..8d55cb8 100644 (file)
@@ -1,5 +1,5 @@
-/* LTO plugin for gold.
-   Copyright (C) 2009 Free Software Foundation, Inc.
+/* LTO plugin for gold and/or GNU ld.
+   Copyright (C) 2009, 2010 Free Software Foundation, Inc.
    Contributed by Rafael Avila de Espindola (espindola@google.com).
 
 This program is free software; you can redistribute it and/or modify
@@ -32,6 +32,12 @@ along with this program; see the file COPYING3.  If not see
    -nop: Instead of running lto-wrapper, pass the original to the plugin. This
    only works if the input files are hybrid.  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#if HAVE_STDINT_H
+#include <stdint.h>
+#endif
 #include <assert.h>
 #include <string.h>
 #include <stdlib.h>
@@ -41,25 +47,54 @@ along with this program; see the file COPYING3.  If not see
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/types.h>
+#ifdef HAVE_SYS_WAIT_H
 #include <sys/wait.h>
-#include <stdbool.h>
+#endif
+#ifndef WIFEXITED
+#define WIFEXITED(S) (((S) & 0xff) == 0)
+#endif
+#ifndef WEXITSTATUS
+#define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
+#endif
 #include <libiberty.h>
 #include <hashtab.h>
+#include "../gcc/lto/common.h"
+#include "simple-object.h"
+#include "plugin-api.h"
 
-/* The presence of gelf.h is checked by the toplevel configure script.  */
-#include <gelf.h>
+/* We need to use I64 instead of ll width-specifier on native Windows.
+   The reason for this is that older MS-runtimes don't support the ll.  */
+#ifdef __MINGW32__
+#define PRI_LL "I64"
+#else
+#define PRI_LL "ll"
+#endif
 
-#include "plugin-api.h"
-#include "../gcc/lto/common.h"
+/* Handle opening elf files on hosts, such as Windows, that may use
+   text file handling that will break binary access.  */
+#ifndef O_BINARY
+# define O_BINARY 0
+#endif
+
+/* Segment name for LTO sections.  This is only used for Mach-O.
+   FIXME: This needs to be kept in sync with darwin.c.  */
+
+#define LTO_SEGMENT_NAME "__GNU_LTO"
+
+/* LTO magic section name.  */
+
+#define LTO_SECTION_PREFIX     ".gnu.lto_.symtab"
+#define LTO_SECTION_PREFIX_LEN (sizeof (LTO_SECTION_PREFIX) - 1)
 
 /* The part of the symbol table the plugin has to keep track of. Note that we
    must keep SYMS until all_symbols_read is called to give the linker time to
-   copy the symbol information. */
+   copy the symbol information. 
+   The id must be 64bit to minimze collisions. */
 
 struct sym_aux
 {
   uint32_t slot;
-  unsigned id;
+  unsigned long long id;
   unsigned next_conflict;
 };
 
@@ -68,7 +103,16 @@ struct plugin_symtab
   int nsyms;
   struct sym_aux *aux;
   struct ld_plugin_symbol *syms;
-  unsigned id;
+  unsigned long long id;
+};
+
+/* Encapsulates object file data during symbol scan.  */
+struct plugin_objfile
+{
+  int found;
+  simple_object_read *objfile;
+  struct plugin_symtab *out;
+  const struct ld_plugin_input_file *file;
 };
 
 /* All that we have to remember about a file. */
@@ -81,16 +125,25 @@ struct plugin_file_info
   struct plugin_symtab conflicts;
 };
 
+/* Until ASM_OUTPUT_LABELREF can be hookized and decoupled from
+   stdio file streams, we do simple label translation here.  */
+
+enum symbol_style
+{
+  ss_none,     /* No underscore prefix. */
+  ss_win32,    /* Underscore prefix any symbol not beginning with '@'.  */
+  ss_uscore,   /* Underscore prefix all symbols.  */
+};
 
 static char *arguments_file_name;
 static ld_plugin_register_claim_file register_claim_file;
-static ld_plugin_add_symbols add_symbols;
 static ld_plugin_register_all_symbols_read register_all_symbols_read;
-static ld_plugin_get_symbols get_symbols;
+static ld_plugin_get_symbols get_symbols, get_symbols_v2;
 static ld_plugin_register_cleanup register_cleanup;
 static ld_plugin_add_input_file add_input_file;
 static ld_plugin_add_input_library add_input_library;
 static ld_plugin_message message;
+static ld_plugin_add_symbols add_symbols;
 
 static struct plugin_file_info *claimed_files = NULL;
 static unsigned int num_claimed_files = 0;
@@ -104,12 +157,21 @@ static int lto_wrapper_num_args;
 static char **pass_through_items = NULL;
 static unsigned int num_pass_through_items;
 
-static bool debug;
-static bool nop;
+static char debug;
+static char nop;
 static char *resolution_file = NULL;
 
+/* The version of gold being used, or -1 if not gold.  The number is
+   MAJOR * 100 + MINOR.  */
+static int gold_version = -1;
+
+/* Not used by default, but can be overridden at runtime
+   by using -plugin-opt=-sym-style={none,win32,underscore|uscore}
+   (in fact, only first letter of style arg is checked.)  */
+static enum symbol_style sym_style = ss_none;
+
 static void
-check (bool gate, enum ld_plugin_level level, const char *text)
+check_1 (int gate, enum ld_plugin_level level, const char *text)
 {
   if (gate)
     return;
@@ -125,6 +187,11 @@ check (bool gate, enum ld_plugin_level level, const char *text)
     }
 }
 
+/* This little wrapper allows check to be called with a non-integer
+   first argument, such as a pointer that must be non-NULL.  We can't
+   use c99 bool type to coerce it into range, so we explicitly test.  */
+#define check(GATE, LEVEL, TEXT) check_1 (((GATE) != 0), (LEVEL), (TEXT))
+
 /* Parse an entry of the IL symbol table. The data to be parsed is pointed
    by P and the result is written in ENTRY. The slot number is stored in SLOT.
    Returns the address of the next entry. */
@@ -151,7 +218,24 @@ parse_table_entry (char *p, struct ld_plugin_symbol *entry,
       LDPV_HIDDEN
     };
 
-  entry->name = xstrdup (p);
+  switch (sym_style)
+    {
+    case ss_win32:
+      if (p[0] == '@')
+       {
+    /* cf. Duff's device.  */
+    case ss_none:
+         entry->name = xstrdup (p);
+         break;
+       }
+    /* FALL-THROUGH.  */
+    case ss_uscore:
+      entry->name = concat ("_", p, NULL);
+      break;
+    default:
+      check (0, LDPL_FATAL, "invalid symbol style requested");
+      break;
+    }
   while (*p)
     p++;
   p++;
@@ -178,10 +262,10 @@ parse_table_entry (char *p, struct ld_plugin_symbol *entry,
   entry->visibility = translate_visibility[t];
   p++;
 
-  entry->size = *(uint64_t *) p;
+  memcpy (&entry->size, p, sizeof (uint64_t));
   p += 8;
 
-  aux->slot = *(uint32_t *) p;
+  memcpy (&aux->slot, p, sizeof (uint32_t));
   p += 4;
 
   entry->resolution = LDPR_UNKNOWN;
@@ -191,16 +275,13 @@ parse_table_entry (char *p, struct ld_plugin_symbol *entry,
   return p;
 }
 
-#define LTO_SECTION_PREFIX ".gnu.lto_.symtab"
-
-/* Translate the IL symbol table SYMTAB. Append the slots and symbols to OUT. */
+/* Translate the IL symbol table located between DATA and END. Append the
+   slots and symbols to OUT. */
 
 static void
-translate (Elf_Data *symtab, struct plugin_symtab *out)
+translate (char *data, char *end, struct plugin_symtab *out)
 {
   struct sym_aux *aux;
-  char *data = symtab->d_buf;
-  char *end = data + symtab->d_size;
   struct ld_plugin_symbol *syms = NULL;
   int n, len;
 
@@ -224,39 +305,6 @@ translate (Elf_Data *symtab, struct plugin_symtab *out)
   out->aux = aux;
 }
 
-/* Process all lto symtabs of file ELF. */
-
-static int
-process_symtab (Elf *elf, struct plugin_symtab *out)
-{
-  int found = 0;
-  Elf_Scn *section = 0;
-  GElf_Ehdr header;
-  GElf_Ehdr *t = gelf_getehdr (elf, &header);
-  if (t == NULL)
-    return 0;
-  assert (t == &header);
-
-  while ((section = elf_nextscn(elf, section)) != 0)
-    {
-      GElf_Shdr shdr;
-      GElf_Shdr *tshdr = gelf_getshdr (section, &shdr);
-      const char *t;
-      assert (tshdr == &shdr);
-      t = elf_strptr (elf, header.e_shstrndx, shdr.sh_name);
-      assert (t != NULL);
-      if (strncmp (t, LTO_SECTION_PREFIX, strlen (LTO_SECTION_PREFIX)) == 0) 
-       {
-         char *s = strrchr (t, '.');
-         if (s)
-             sscanf (s, ".%x", &out->id);
-         translate (elf_getdata (section, NULL), out);
-         found++;
-       }
-    }
-  return found;
-}
-
 /* Free all memory that is no longer needed after writing the symbol
    resolution. */
 
@@ -273,8 +321,7 @@ free_1 (void)
        {
          struct ld_plugin_symbol *s = &symtab->syms[j];
          free (s->name);
-         if (s->comdat_key)
-           free (s->comdat_key);
+         free (s->comdat_key);
        }
       free (symtab->syms);
       symtab->syms = NULL;
@@ -303,8 +350,7 @@ free_2 (void)
   claimed_files = NULL;
   num_claimed_files = 0;
 
-  if (arguments_file_name)
-    free (arguments_file_name);
+  free (arguments_file_name);
   arguments_file_name = NULL;
 }
 
@@ -322,7 +368,8 @@ dump_symtab (FILE *f, struct plugin_symtab *symtab)
       
       assert (resolution != LDPR_UNKNOWN);
 
-      fprintf (f, "%d %x %s %s\n", slot, symtab->aux[j].id,
+      fprintf (f, "%u %" PRI_LL "x %s %s\n",
+               (unsigned int) slot, symtab->aux[j].id,
               lto_resolution_str[resolution], 
               symtab->syms[j].name);
     }
@@ -342,7 +389,7 @@ finish_conflict_resolution (struct plugin_symtab *symtab,
 
   for (i = 0; i < symtab->nsyms; i++)
     { 
-      int resolution;
+      int resolution = LDPR_UNKNOWN;
 
       if (symtab->aux[i].next_conflict == -1)
        continue;
@@ -404,7 +451,12 @@ write_resolution (void)
       struct plugin_symtab *symtab = &info->symtab;
       struct ld_plugin_symbol *syms = symtab->syms;
 
-      get_symbols (info->handle, symtab->nsyms, syms);
+      /* Version 2 of API supports IRONLY_EXP resolution that is
+         accepted by GCC-4.7 and newer.  */
+      if (get_symbols_v2)
+        get_symbols_v2 (info->handle, symtab->nsyms, syms);
+      else
+        get_symbols (info->handle, symtab->nsyms, syms);
 
       finish_conflict_resolution (symtab, &info->conflicts);
 
@@ -434,7 +486,10 @@ add_output_files (FILE *f)
       buf = s;
 cont:
       if (!fgets (buf, piece, f))
-       break;
+       {
+         free (s);
+         break;
+       }
       len = strlen (s);
       if (s[len - 1] != '\n')
        {
@@ -584,7 +639,8 @@ all_symbols_read_handler (void)
 
   free (lto_argv);
 
-  if (pass_through_items)
+  /* --pass-through is not needed when using gold 1.11 or later.  */
+  if (pass_through_items && gold_version < 111)
     {
       unsigned int i;
       for (i = 0; i < num_pass_through_items; i++)
@@ -718,7 +774,7 @@ resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
            {
              SWAP (struct ld_plugin_symbol, *orig, *s);
              SWAP (uint32_t, orig_aux->slot, aux->slot);
-             SWAP (unsigned, orig_aux->id, aux->id);
+             SWAP (unsigned long long, orig_aux->id, aux->id);
              /* Don't swap conflict chain pointer */
            } 
 
@@ -754,6 +810,40 @@ resolve_conflicts (struct plugin_symtab *t, struct plugin_symtab *conflicts)
   htab_delete (symtab);
 }
 
+/* Process one section of an object file.  */
+
+static int 
+process_symtab (void *data, const char *name, off_t offset, off_t length)
+{
+  struct plugin_objfile *obj = (struct plugin_objfile *)data;
+  char *s;
+  char *secdata;
+
+  if (strncmp (name, LTO_SECTION_PREFIX, LTO_SECTION_PREFIX_LEN) != 0)
+    return 1;
+
+  s = strrchr (name, '.');
+  if (s)
+    sscanf (s, ".%" PRI_LL "x", &obj->out->id);
+  secdata = xmalloc (length);
+  offset += obj->file->offset;
+  if (offset != lseek (obj->file->fd, offset, SEEK_SET)
+       || length != read (obj->file->fd, secdata, length))
+    {
+      if (message)
+       message (LDPL_FATAL, "%s: corrupt object file", obj->file->name);
+      /* Force claim_file_handler to abandon this file.  */
+      obj->found = 0;
+      free (secdata);
+      return 0;
+    }
+
+  translate (secdata, secdata + length, obj->out);
+  obj->found++;
+  free (secdata);
+  return 1;
+}
+
 /* Callback used by gold to check if the plugin will claim FILE. Writes
    the result in CLAIMED. */
 
@@ -761,55 +851,61 @@ static enum ld_plugin_status
 claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
 {
   enum ld_plugin_status status;
-  Elf *elf;
+  struct plugin_objfile obj;
   struct plugin_file_info lto_file;
-  int n;
+  int err;
+  const char *errmsg;
 
   memset (&lto_file, 0, sizeof (struct plugin_file_info));
 
   if (file->offset != 0)
     {
       char *objname;
-      Elf *archive;
-      off_t offset;
-      /* We pass the offset of the actual file, not the archive header. */
-      int t = asprintf (&objname, "%s@0x%" PRIx64, file->name,
-                        (int64_t) file->offset);
+      /* We pass the offset of the actual file, not the archive header.
+         Can't use PRIx64, because that's C99, so we have to print the
+        64-bit hex int as two 32-bit ones. */
+      int lo, hi, t;
+      lo = file->offset & 0xffffffff;
+      hi = ((int64_t)file->offset >> 32) & 0xffffffff;
+      t = hi ? asprintf (&objname, "%s@0x%x%08x", file->name, lo, hi)
+            : asprintf (&objname, "%s@0x%x", file->name, lo);
       check (t >= 0, LDPL_FATAL, "asprintf failed");
       lto_file.name = objname;
-
-      archive = elf_begin (file->fd, ELF_C_READ, NULL);
-      check (elf_kind (archive) == ELF_K_AR, LDPL_FATAL,
-             "Not an archive and offset not 0");
-
-      /* elf_rand expects the offset to point to the ar header, not the
-         object itself. Subtract the size of the ar header (60 bytes).
-         We don't uses sizeof (struct ar_hd) to avoid including ar.h */
-
-      offset = file->offset - 60;
-      check (offset == elf_rand (archive, offset), LDPL_FATAL,
-             "could not seek in archive");
-      elf = elf_begin (file->fd, ELF_C_READ, archive);
-      check (elf != NULL, LDPL_FATAL, "could not find archive member");
-      elf_end (archive);
     }
   else
     {
       lto_file.name = xstrdup (file->name);
-      elf = elf_begin (file->fd, ELF_C_READ, NULL);
     }
   lto_file.handle = file->handle;
 
   *claimed = 0;
-
-  if (!elf)
+  obj.file = file;
+  obj.found = 0;
+  obj.out = &lto_file.symtab;
+  errmsg = NULL;
+  obj.objfile = simple_object_start_read (file->fd, file->offset, LTO_SEGMENT_NAME,
+                       &errmsg, &err);
+  /* No file, but also no error code means unrecognized format; just skip it.  */
+  if (!obj.objfile && !err)
     goto err;
 
-  n = process_symtab (elf, &lto_file.symtab);
-  if (n == 0)
+  if (obj.objfile)
+    errmsg = simple_object_find_sections (obj.objfile, process_symtab, &obj, &err);
+
+  if (!obj.objfile || errmsg)
+    {
+      if (err && message)
+       message (LDPL_FATAL, "%s: %s: %s", file->name, errmsg,
+               xstrerror (err));
+      else if (message)
+       message (LDPL_FATAL, "%s: %s", file->name, errmsg);
+      goto err;
+    }
+
+  if (obj.found == 0)
     goto err;
 
-  if (n > 1)
+  if (obj.found > 1)
     resolve_conflicts (&lto_file.symtab, &lto_file.conflicts);
 
   status = add_symbols (file->handle, lto_file.symtab.nsyms,
@@ -829,8 +925,8 @@ claim_file_handler (const struct ld_plugin_input_file *file, int *claimed)
   free (lto_file.name);
 
  cleanup:
-  if (elf)
-    elf_end (elf);
+  if (obj.objfile)
+    simple_object_release_read (obj.objfile);
 
   return LDPS_OK;
 }
@@ -852,6 +948,21 @@ process_option (const char *option)
       pass_through_items[num_pass_through_items - 1] =
           xstrdup (option + strlen ("-pass-through="));
     }
+  else if (!strncmp (option, "-sym-style=", sizeof ("-sym-style=") - 1))
+    {
+      switch (option[sizeof ("-sym-style=") - 1])
+       {
+       case 'w':
+         sym_style = ss_win32;
+         break;
+       case 'u':
+         sym_style = ss_uscore;
+         break;
+       default:
+         sym_style = ss_none;
+         break;
+       }
+    }
   else
     {
       int size;
@@ -873,9 +984,6 @@ onload (struct ld_plugin_tv *tv)
   struct ld_plugin_tv *p;
   enum ld_plugin_status status;
 
-  unsigned version = elf_version (EV_CURRENT);
-  check (version != EV_NONE, LDPL_FATAL, "invalid ELF version");
-
   p = tv;
   while (p->tv_tag)
     {
@@ -893,6 +1001,9 @@ onload (struct ld_plugin_tv *tv)
        case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
          register_all_symbols_read = p->tv_u.tv_register_all_symbols_read;
          break;
+       case LDPT_GET_SYMBOLS_V2:
+         get_symbols_v2 = p->tv_u.tv_get_symbols;
+         break;
        case LDPT_GET_SYMBOLS:
          get_symbols = p->tv_u.tv_get_symbols;
          break;
@@ -908,6 +1019,9 @@ onload (struct ld_plugin_tv *tv)
        case LDPT_OPTION:
          process_option (p->tv_u.tv_string);
          break;
+       case LDPT_GOLD_VERSION:
+         gold_version = p->tv_u.tv_val;
+         break;
        default:
          break;
        }