+
+ if (elf_kind (elf) != ELF_K_ELF) {
+ fprintf (stderr, "fips: Not an ELF object: %s\n", absolute_program);
+ exit (1);
+ }
+
+ if (gelf_getehdr (elf, &ehdr) == NULL) {
+ fprintf (stderr, "fips: getehdr on %s failed: %s\n",
+ absolute_program, elf_errmsg (-1));
+ exit (1);
+ }
+
+ class = gelf_getclass (elf);
+
+ if (class == ELFCLASSNONE) {
+ fprintf (stderr, "fips: getclass on %s failed: %s\n",
+ absolute_program, elf_errmsg (-1));
+ exit (1);
+ }
+
+ talloc_free (local);
+
+ if (class == ELFCLASS32)
+ return 32;
+ else
+ return 64;
+
+}
+
+/* Find the appropriate path to the libfips wrapper.
+ *
+ * This involves, first, examining the elf header of the 'program'
+ * binary to be executed to know whether we should look for
+ * libfips-32.so or libfips-64.so.
+ *
+ * Next, we find the absolute patch containing the library as follows:
+ *
+ * 1. Look in same directory as current executable image
+ *
+ * This is to support running from the source directory, without
+ * having installed anything.
+ *
+ * 2. Look in relative path from $(foo)/$(bindir) to
+ * $(foo)/$(libdir)/fips based on $(foo) from current executable
+ * image and configured $(bindir) and $(libdir).
+ *
+ * We do this rather than looking directly at the configured
+ * $(libdir) to support cases where the application may have been
+ * moved after being installed, (in particular, we want to be
+ * particularly careful not to mix one program with a different
+ * wrapper---so this "nearest search" should most often be
+ * correct.
+ *
+ * Returns: a string talloc'ed to 'ctx'
+ */
+static char *
+find_libfips_path (void *ctx, const char *program)
+{
+ char *bin_path, *library, *lib_path;
+ int bits;
+
+ bits = elf_bits (program);
+
+ library = talloc_asprintf(ctx, "libfips-%d.so", bits);
+
+ bin_path = get_bin_name (ctx);
+
+ chop_trailing_path_component (bin_path);
+
+ lib_path = talloc_asprintf(ctx, "%s/%s", bin_path, library);
+
+ if (exists (lib_path))
+ return lib_path;
+
+ talloc_free (lib_path);
+
+ lib_path = talloc_asprintf(ctx, "%s/" BINDIR_TO_LIBFIPSDIR "/%s",
+ bin_path, library);
+
+ if (exists (lib_path))
+ return lib_path;
+
+ fprintf (stderr, "Error: Failed to find library %s.\n", library);
+ fprintf (stderr, "Looked in both:\n"
+ "\t%s\n"
+ "and\n"
+ "\t%s/" BINDIR_TO_LIBFIPSDIR "\n", bin_path, bin_path);
+
+ fprintf(stderr, "\nIt's possible fips was not compiled with support for %d-bit applications.\n", bits);
+ fprintf(stderr, "Perhaps you need to install gcc-multilib and re-compile fips?\n");
+ exit (1);