sim: unify various library testing logic
[binutils-gdb.git] / ld / testplug2.c
index 5e17b68eb3fa72d24ceddf16a7490551645c2d24..ddf2ea3d92e8a666e1f3e68b0610a53c08a3a391 100644 (file)
@@ -1,6 +1,6 @@
 /* Test plugin for the GNU linker.  Check non-object IR file as well as
    get_input_file, get_view and release_input_file interfaces.
-   Copyright (C) 2015 Free Software Foundation, Inc.
+   Copyright (C) 2015-2021 Free Software Foundation, Inc.
 
    This file is part of the GNU Binutils.
 
@@ -21,6 +21,7 @@
 
 #include "sysdep.h"
 #include "bfd.h"
+#if BFD_SUPPORTS_PLUGINS
 #include "plugin-api.h"
 #include "filenames.h"
 /* For ARRAY_SIZE macro only - we don't link the library itself.  */
@@ -40,7 +41,7 @@ typedef struct claim_file
 {
   struct claim_file *next;
   struct ld_plugin_input_file file;
-  bfd_boolean claimed;
+  bool claimed;
   struct ld_plugin_symbol *symbols;
   int n_syms_allocated;
   int n_syms_used;
@@ -121,10 +122,11 @@ static enum ld_plugin_status onload_ret = LDPS_OK;
 static enum ld_plugin_status claim_file_ret = LDPS_OK;
 static enum ld_plugin_status all_symbols_read_ret = LDPS_OK;
 static enum ld_plugin_status cleanup_ret = LDPS_OK;
-static bfd_boolean register_claimfile_hook = TRUE;
-static bfd_boolean register_allsymbolsread_hook = FALSE;
-static bfd_boolean register_cleanup_hook = FALSE;
-static bfd_boolean dumpresolutions = FALSE;
+static bool register_claimfile_hook = true;
+static bool register_allsymbolsread_hook = false;
+static bool register_cleanup_hook = false;
+static bool dumpresolutions = false;
+static bool allsymbolsread_silent = false;
 
 /* The master list of all claimable/claimed files.  */
 static claim_file_t *claimfiles_list = NULL;
@@ -217,12 +219,15 @@ parse_symdefstr (const char *str, struct ld_plugin_symbol *sym)
   /* Finally we'll use sscanf to parse the numeric fields, then
      we'll split out the strings which we need to allocate separate
      storage for anyway so that we can add nul termination.  */
-  n = sscanf (colon2 + 1, "%i:%i:%lli", &sym->def, &sym->visibility, &size);
+  n = sscanf (colon2 + 1, "%hhi:%i:%lli", &sym->def, &sym->visibility, &size);
   if (n != 3)
     return LDPS_ERR;
 
   /* Parsed successfully, so allocate strings and fill out fields.  */
   sym->size = size;
+  sym->unused = 0;
+  sym->section_kind = 0;
+  sym->symbol_type = 0;
   sym->resolution = LDPR_UNKNOWN;
   sym->name = malloc (colon1 - str + 1);
   if (!sym->name)
@@ -301,12 +306,17 @@ set_ret_val (const char *whichval, enum ld_plugin_status retval)
 
 /* Records hooks which should be registered.  */
 static enum ld_plugin_status
-set_register_hook (const char *whichhook, bfd_boolean yesno)
+set_register_hook (const char *whichhook, bool yesno)
 {
   if (!strcmp ("claimfile", whichhook))
     register_claimfile_hook = yesno;
   else if (!strcmp ("allsymbolsread", whichhook))
     register_allsymbolsread_hook = yesno;
+  else if (!strcmp ("allsymbolsreadsilent", whichhook))
+    {
+      register_allsymbolsread_hook = yesno;
+      allsymbolsread_silent = true;
+    }
   else if (!strcmp ("cleanup", whichhook))
     register_cleanup_hook = yesno;
   else
@@ -318,14 +328,29 @@ set_register_hook (const char *whichhook, bfd_boolean yesno)
 static enum ld_plugin_status
 parse_option (const char *opt)
 {
-  if (!strncmp ("fail", opt, 4))
+  if (!strncmp ("fatal", opt, 5))
+    {
+      TV_MESSAGE (LDPL_FATAL, "Fatal error");
+      fflush (NULL);
+    }
+  else if (!strncmp ("error", opt, 5))
+    {
+      TV_MESSAGE (LDPL_ERROR, "Error");
+      fflush (NULL);
+    }
+  else if (!strncmp ("warning", opt, 7))
+    {
+      TV_MESSAGE (LDPL_WARNING, "Warning");
+      fflush (NULL);
+    }
+  else if (!strncmp ("fail", opt, 4))
     return set_ret_val (opt + 4, LDPS_ERR);
   else if (!strncmp ("pass", opt, 4))
     return set_ret_val (opt + 4, LDPS_OK);
   else if (!strncmp ("register", opt, 8))
-    return set_register_hook (opt + 8, TRUE);
+    return set_register_hook (opt + 8, true);
   else if (!strncmp ("noregister", opt, 10))
-    return set_register_hook (opt + 10, FALSE);
+    return set_register_hook (opt + 10, false);
   else if (!strncmp ("claim:", opt, 6))
     return record_claim_file (opt + 6, 0);
   else if (!strncmp ("sym:", opt, 4))
@@ -337,60 +362,12 @@ parse_option (const char *opt)
   else if (!strncmp ("dir:", opt, 4))
     return record_add_file (opt + 4, ADD_DIR);
   else if (!strcmp ("dumpresolutions", opt))
-    dumpresolutions = TRUE;
+    dumpresolutions = true;
   else
     return LDPS_ERR;
   return LDPS_OK;
 }
 
-/* Output contents of transfer vector array entry in human-readable form.  */
-static void
-dump_tv_tag (size_t n, struct ld_plugin_tv *tv)
-{
-  size_t tag;
-  char unknownbuf[40];
-  const char *name;
-
-  for (tag = 0; tag < ARRAY_SIZE (tag_names); tag++)
-    if (tag_names[tag].tag == tv->tv_tag)
-      break;
-  sprintf (unknownbuf, "unknown tag #%d", tv->tv_tag);
-  name = (tag < ARRAY_SIZE (tag_names)) ? tag_names[tag].name : unknownbuf;
-  switch (tv->tv_tag)
-    {
-      case LDPT_OPTION:
-      case LDPT_OUTPUT_NAME:
-       TV_MESSAGE (LDPL_INFO, "tv[%d]: %s '%s'", n, name,
-                   tv->tv_u.tv_string);
-        break;
-      case LDPT_REGISTER_CLAIM_FILE_HOOK:
-      case LDPT_REGISTER_ALL_SYMBOLS_READ_HOOK:
-      case LDPT_REGISTER_CLEANUP_HOOK:
-      case LDPT_ADD_SYMBOLS:
-      case LDPT_GET_SYMBOLS:
-      case LDPT_GET_SYMBOLS_V2:
-      case LDPT_ADD_INPUT_FILE:
-      case LDPT_MESSAGE:
-      case LDPT_GET_INPUT_FILE:
-      case LDPT_GET_VIEW:
-      case LDPT_RELEASE_INPUT_FILE:
-      case LDPT_ADD_INPUT_LIBRARY:
-      case LDPT_SET_EXTRA_LIBRARY_PATH:
-       TV_MESSAGE (LDPL_INFO, "tv[%d]: %s func@0x%p", n, name,
-                   (void *)(tv->tv_u.tv_message));
-        break;
-      case LDPT_NULL:
-      case LDPT_API_VERSION:
-      case LDPT_GOLD_VERSION:
-      case LDPT_LINKER_OUTPUT:
-      case LDPT_GNU_LD_VERSION:
-      default:
-       TV_MESSAGE (LDPL_INFO, "tv[%d]: %s value %W (%d)", n, name,
-                   (bfd_vma)tv->tv_u.tv_val, tv->tv_u.tv_val);
-       break;
-    }
-}
-
 /* Handle/record information received in a transfer vector entry.  */
 static enum ld_plugin_status
 parse_tv_tag (struct ld_plugin_tv *tv)
@@ -541,7 +518,7 @@ onclaim_file (const struct ld_plugin_input_file *file, int *claimed)
   size_t len = strlen (file->name);
   char *name = xstrdup (file->name);
   char *p = name + len;
-  bfd_boolean islib;
+  bool islib;
 
   /* Only match the file name without the directory part.  */
   islib = *p == 'a' && *(p - 1) == '.';
@@ -570,7 +547,7 @@ onclaim_file (const struct ld_plugin_input_file *file, int *claimed)
   *claimed = (claimfile != 0);
   if (claimfile)
     {
-      claimfile->claimed = TRUE;
+      claimfile->claimed = true;
       claimfile->file = *file;
       if (claimfile->n_syms_used && !tv_add_symbols)
        return LDPS_ERR;
@@ -606,7 +583,8 @@ onall_symbols_read (void)
   char buffer[30];
   int fd;
   char *filename;
-  TV_MESSAGE (LDPL_INFO, "hook called: all symbols read.");
+  if (! allsymbolsread_silent)
+    TV_MESSAGE (LDPL_INFO, "hook called: all symbols read.");
   for ( ; claimfile; claimfile = claimfile->next)
     {
       enum ld_plugin_status rv;
@@ -628,7 +606,7 @@ onall_symbols_read (void)
 #define EXPECTED_VIEW "/* The first line of this file must match the expectation of"
 #define EXPECTED_VIEW_LENGTH (sizeof (EXPECTED_VIEW) - 1)
       if (file.filesize != SIZE_OF_FUNC_C
-         || SIZE_OF_FUNC_C < sizeof EXPECTED_VIEW_LENGTH
+         || SIZE_OF_FUNC_C < EXPECTED_VIEW_LENGTH
          || memcmp (view, EXPECTED_VIEW, EXPECTED_VIEW_LENGTH) != 0)
        {
          char result[EXPECTED_VIEW_LENGTH + 1];
@@ -659,9 +637,10 @@ onall_symbols_read (void)
        }
       if (read (fd, buffer, sizeof (buffer)) >= 0)
        {
-         rv == LDPS_ERR;
          TV_MESSAGE (LDPL_FATAL, "Unreleased file descriptor on: %s",
                      claimfile->file.name);
+         free (filename);
+         return LDPS_ERR;
        }
       free (filename);
     }
@@ -691,3 +670,4 @@ oncleanup (void)
   fflush (NULL);
   return cleanup_ret;
 }
+#endif /* BFD_SUPPORTS_PLUGINS */