BFD_INIT_MAGIC
authorAlan Modra <amodra@gmail.com>
Mon, 15 Oct 2018 05:40:27 +0000 (16:10 +1030)
committerAlan Modra <amodra@gmail.com>
Mon, 15 Oct 2018 11:41:58 +0000 (22:11 +1030)
This patch performs a run-time test that a shared libbfd.so has been
compiled with the same size bfd_vma as that of apps using the library.
On a 32-bit host it is easily possible to have one libbfd.so compiled
to support 64-bit targets (or configured with --enable-64-bit-bfd)
while another only supports 32-bit targets.  The two libraries will
have differently sized bfd_vma types, and if the wrong one is loaded
all sorts of weird behaviour might be seen.

bfd/
PR 23534
* init.c (BFD_INIT_MAGIC): Define.
(bfd_init): Return BFD_INIT_MAGIC.
bfd-in2.h: Regenerate.
binutils/
PR 23534
* addr2line.c (main): Exit with fatal error if bfd_init
returns an unexpected value.
* ar.c (main): Likewise.
* dlltool.c (identify_dll_for_implib): Likewise.
* nm.c (main): Likewise.
* objcopy.c (main): Likewise.
* objdump.c (main): Likewise.
* size.c (main): Likewise.
* strings.c (main): Likewise.
* windmc.c (main): Likewise.
* windres.c (main): Likewise.
gas/
PR 23534
* as.c (main): Exit with fatal error if bfd_init returns an
unexpected value.
ld/
PR 23534
* ldmain.c (main): Exit with fatal error if bfd_init returns
an unexpected value.

18 files changed:
bfd/ChangeLog
bfd/bfd-in2.h
bfd/init.c
binutils/ChangeLog
binutils/addr2line.c
binutils/ar.c
binutils/dlltool.c
binutils/nm.c
binutils/objcopy.c
binutils/objdump.c
binutils/size.c
binutils/strings.c
binutils/windmc.c
binutils/windres.c
gas/ChangeLog
gas/as.c
ld/ChangeLog
ld/ldmain.c

index e9696ee314307d63fa11b525a9270cf8b8005758..9e0f1242651bc80af045eb6921379b97c84916f3 100644 (file)
@@ -1,3 +1,10 @@
+2018-10-15  Alan Modra  <amodra@gmail.com>
+
+       PR 23534
+       * init.c (BFD_INIT_MAGIC): Define.
+       (bfd_init): Return BFD_INIT_MAGIC.
+       bfd-in2.h: Regenerate.
+
 2018-10-13  Alan Modra  <amodra@gmail.com>
 
        PR 23770
index 662ef41a4713e57cd209c3498e4b18ba60c6d5f2..beb695a18bc56d9c73991c3b276268902315c0b2 100644 (file)
@@ -1080,8 +1080,12 @@ extern void elf32_csky_next_input_section
 extern int elf32_csky_setup_section_lists
   (bfd *, struct bfd_link_info *);
 /* Extracted from init.c.  */
-void bfd_init (void);
+size_t bfd_init (void);
 
+
+/* Value returned by bfd_init.  */
+
+#define BFD_INIT_MAGIC (sizeof (struct bfd_section))
 /* Extracted from opncls.c.  */
 /* Set to N to open the next N BFDs using an alternate id space.  */
 extern unsigned int bfd_use_reserved_id;
index ec6354e8c9ed3d05d9a06b4e00532d33d2068234..ad109819314e00d83debd21735fdfa85f675211a 100644 (file)
@@ -38,17 +38,24 @@ FUNCTION
        bfd_init
 
 SYNOPSIS
-       void bfd_init (void);
+       size_t bfd_init (void);
 
 DESCRIPTION
        This routine must be called before any other BFD function to
        initialize magical internal data structures.
+       Returns a magic number, which may be used to check
+       that the bfd library is configured as expected by users.
+.
+.{* Value returned by bfd_init.  *}
+.
+.#define BFD_INIT_MAGIC (sizeof (struct bfd_section))
 */
 
 /* Actually, there is currently nothing for this function to do.
    However, someday it may be needed, so keep it around.  */
 
-void
+size_t
 bfd_init (void)
 {
+  return BFD_INIT_MAGIC;
 }
index 8b84df5d5720cae089926d7f1f76aa0e5feaf5a8..d41894fa210ba4939228c29df411c05658dbad72 100644 (file)
@@ -1,3 +1,18 @@
+2018-10-15  Alan Modra  <amodra@gmail.com>
+
+       PR 23534
+       * addr2line.c (main): Exit with fatal error if bfd_init
+       returns an unexpected value.
+       * ar.c (main): Likewise.
+       * dlltool.c (identify_dll_for_implib): Likewise.
+       * nm.c (main): Likewise.
+       * objcopy.c (main): Likewise.
+       * objdump.c (main): Likewise.
+       * size.c (main): Likewise.
+       * strings.c (main): Likewise.
+       * windmc.c (main): Likewise.
+       * windres.c (main): Likewise.
+
 2018-10-11  Jordan Rupprecht  <rupprecht@google.com>
            Nick Clifton  <nickc@redhat.com>
 
index 64454f54b9cad9dd873e187d20e8fb7f741a2365..008e62026e80bf5f961ca101c1a0ea9e67793f5d 100644 (file)
@@ -435,7 +435,8 @@ main (int argc, char **argv)
 
   expandargv (&argc, &argv);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   file_name = NULL;
index 28a67891753f3608fa54679b48a4e021cba04ccc..b09efa02510fa67f25a1b18f676657012aa5756f 100644 (file)
@@ -718,7 +718,8 @@ main (int argc, char **argv)
 
   START_PROGRESS (program_name, 0);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   xatexit (remove_output);
index 21c95ef2e70092e2cf782f4dddaab0ac65ec4de3..2c751241f14d2d9fb9a59cbb1591e48288b03208 100644 (file)
@@ -3504,7 +3504,8 @@ identify_dll_for_implib (void)
   search_data.symname = "__NULL_IMPORT_DESCRIPTOR";
   search_data.found = FALSE;
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
 
   abfd = bfd_openr (identify_imp_name, 0);
   if (abfd == NULL)
index e46fffc796f772141e9ad8ea4515be2dc36d49cd..bc4fccb5fcb80db9d4f571c9367a0876f0b429e7 100644 (file)
@@ -1654,7 +1654,8 @@ main (int argc, char **argv)
 
   expandargv (&argc, &argv);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   while ((c = getopt_long (argc, argv, "aABCDef:gHhlnopPrSst:uvVvX:",
index d8ae1f98a10a50f640c3a40b67a6da9ed0db8b5c..1d29059b8df90eebab2dc9ec99ff1796f7ff2a3b 100644 (file)
@@ -5659,7 +5659,8 @@ main (int argc, char *argv[])
   strip_symbols = STRIP_UNDEF;
   discard_locals = LOCALS_UNDEF;
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   if (is_strip < 0)
index 4368fc06664d2de0bd803d7be3412435fa5352a0..9c3bce8b75381c6620e0f5fc98a4227c44192495 100644 (file)
@@ -3842,7 +3842,8 @@ main (int argc, char **argv)
 
   expandargv (&argc, &argv);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   while ((c = getopt_long (argc, argv,
index 47f14fce333750c1f9a80efa51efe33e31a21bc4..3c72e484752d0272a24ac628f497f89ecf36d547 100644 (file)
@@ -137,7 +137,8 @@ main (int argc, char **argv)
 
   expandargv (&argc, &argv);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   while ((c = getopt_long (argc, argv, "ABHhVvdfotx", long_options,
index 7655ab6152b18af557b70f62666839e48aaa8570..74545dbbdcb5b65e858958bbe219713f54f6b0fa 100644 (file)
@@ -287,7 +287,8 @@ main (int argc, char **argv)
       usage (stderr, 1);
     }
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   if (optind >= argc)
index f6171e475796d249f86dad61e93f38ac1a9da068..c8771cd92037adf495efb305432916979de50171 100644 (file)
@@ -956,7 +956,8 @@ main (int argc, char **argv)
 
   expandargv (&argc, &argv);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   target = NULL;
index 76558484b8cfc3ed4708ec9fb65fb2e866abe085..9f4555bfaf6919f58bf6ce1c45f1fc4bcaa416d1 100644 (file)
@@ -812,7 +812,8 @@ main (int argc, char **argv)
 
   expandargv (&argc, &argv);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   res_init ();
index 2538da246cde204c8d5dc99c72b88782dd888bb7..69187bb2fe3f39ce2b06af807546be0795f2a2a9 100644 (file)
@@ -1,3 +1,9 @@
+2018-10-15  Alan Modra  <amodra@gmail.com>
+
+       PR 23534
+       * as.c (main): Exit with fatal error if bfd_init returns an
+       unexpected value.
+
 2018-10-11  Jan Beulich  <jbeulich@suse.com>
 
        * testsuite/gas/i386/xmmword.s: Add MOVQ / VMOVQ cases.
index 3105d068c48c1fb0bea4c0859f8190e333727ed3..c4de060c9df7ed5ca7dda73fe382f764b437a2cf 100644 (file)
--- a/gas/as.c
+++ b/gas/as.c
@@ -1239,7 +1239,8 @@ main (int argc, char ** argv)
   out_file_name = OBJ_DEFAULT_OUTPUT_FILE_NAME;
 
   hex_init ();
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    as_fatal (_("libbfd ABI mismatch"));
   bfd_set_error_program_name (myname);
 
 #ifdef USE_EMULATIONS
index 0f28a2eeef27aecb3a790844f00fa4273351e9ab..3043096e36a777979acade340b3936101fc4d8bc 100644 (file)
@@ -1,3 +1,9 @@
+2018-10-15  Alan Modra  <amodra@gmail.com>
+
+       PR 23534
+       * ldmain.c (main): Exit with fatal error if bfd_init returns
+       an unexpected value.
+
 2018-10-13  Alan Modra  <amodra@gmail.com>
 
        * Makefile.am (eelf32btsmip_fbsd.c, eelf32btsmipn32_fbsd.c),
index aca9b1bb5d00320a9adaab4be67a19fa1d6ecad0..464192049ab6720f15f09df72816c190dc063e3a 100644 (file)
@@ -209,7 +209,8 @@ main (int argc, char **argv)
 
   expandargv (&argc, &argv);
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    einfo (_("%F%P: fatal error: libbfd ABI mismatch\n"));
 
   bfd_set_error_program_name (program_name);
 
This page took 0.039425 seconds and 4 git commands to generate.