This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

BFD_INIT_MAGIC


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.

diff --git a/bfd/init.c b/bfd/init.c
index ec6354e8c9..ad10981931 100644
--- a/bfd/init.c
+++ b/bfd/init.c
@@ -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;
 }
diff --git a/binutils/addr2line.c b/binutils/addr2line.c
index 64454f54b9..008e62026e 100644
--- a/binutils/addr2line.c
+++ b/binutils/addr2line.c
@@ -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;
diff --git a/binutils/ar.c b/binutils/ar.c
index 28a6789175..b09efa0251 100644
--- a/binutils/ar.c
+++ b/binutils/ar.c
@@ -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);
diff --git a/binutils/dlltool.c b/binutils/dlltool.c
index 21c95ef2e7..2c751241f1 100644
--- a/binutils/dlltool.c
+++ b/binutils/dlltool.c
@@ -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)
diff --git a/binutils/nm.c b/binutils/nm.c
index e46fffc796..bc4fccb5fc 100644
--- a/binutils/nm.c
+++ b/binutils/nm.c
@@ -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:",
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index d8ae1f98a1..1d29059b8d 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -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)
diff --git a/binutils/objdump.c b/binutils/objdump.c
index 4368fc0666..9c3bce8b75 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -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,
diff --git a/binutils/size.c b/binutils/size.c
index 47f14fce33..3c72e48475 100644
--- a/binutils/size.c
+++ b/binutils/size.c
@@ -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,
diff --git a/binutils/strings.c b/binutils/strings.c
index 7655ab6152..74545dbbdc 100644
--- a/binutils/strings.c
+++ b/binutils/strings.c
@@ -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)
diff --git a/binutils/windmc.c b/binutils/windmc.c
index f6171e4757..c8771cd920 100644
--- a/binutils/windmc.c
+++ b/binutils/windmc.c
@@ -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;
diff --git a/binutils/windres.c b/binutils/windres.c
index 76558484b8..9f4555bfaf 100644
--- a/binutils/windres.c
+++ b/binutils/windres.c
@@ -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 ();
diff --git a/gas/as.c b/gas/as.c
index 3105d068c4..c4de060c9d 100644
--- 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
diff --git a/ld/ldmain.c b/ld/ldmain.c
index aca9b1bb5d..464192049a 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -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);
 

-- 
Alan Modra
Australia Development Lab, IBM


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]