+/* Is the given elf program 32 or 64 bit?
+ *
+ * Note: This function aborts the current program if 'program' cannot
+ * be opened as a valid ELF file. */
+static int
+elf_bits (const char *program)
+{
+ Elf *elf;
+ GElf_Ehdr ehdr;
+ int fd, class;
+
+ fd = open (program, O_RDONLY, 0);
+ if (fd < 0) {
+ fprintf (stderr, "Failed to open %s: %s\n", program,
+ strerror (errno));
+ exit (1);
+ }
+
+ if (elf_version (EV_CURRENT ) == EV_NONE) {
+ fprintf (stderr, "Failed to initialize elf library: %s\n",
+ elf_errmsg (-1));
+ exit (1);
+ }
+
+ elf = elf_begin (fd, ELF_C_READ, NULL);
+ if (elf == NULL) {
+ fprintf (stderr, "Call to elf_begin on %s failed: %s\n",
+ program, elf_errmsg(-1));
+ exit (1);
+ }
+
+ if (elf_kind (elf) != ELF_K_ELF) {
+ fprintf (stderr, "Not an ELF object: %s\n", program);
+ exit (1);
+ }
+
+ if (gelf_getehdr (elf, &ehdr) == NULL) {
+ fprintf (stderr, "getehdr on %s failed: %s\n",
+ program, elf_errmsg (-1));
+ exit (1);
+ }
+
+ class = gelf_getclass (elf);
+
+ if (class == ELFCLASSNONE) {
+ fprintf (stderr, "getclass on %s failed: %s\n", program,
+ elf_errmsg (-1));
+ exit (1);
+ }
+
+ 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: