Build the libebl_* backends as static libraries instead of dynamic ones. 

openbackend was converted to use the statically linked init functions instead
of dynamically loading the libebl_*.so files.

In the .c files:
  dlhandle was removed from struct Ebl, since it's not needed any more.
  
  The solution with get_init_functions is a bit fragile vs. changes to the
  order of entires in machines[]. Directly adding the info to machines (via an
  additional struct member .init) doesn't work well however, since the *_init
  functions can't be used in the initializer (since they are no compiletime
  constant).
  
  I was thinking about a small script under debian/ (or inside of debian/rules)
  to check for such kind of stuff (and also for the consistency backends <->
  extern declaration in libebl).

In libebl/Makefile.am
  Two flavors are built, one ment to be compiled with -fPIC and one without that
  flag. We need both, because we need 2 libebl.a too. A non-PIC one to go into
  libebl-dev, and a PIC one for use in other elfutils shared libs.
  
  The _LIBADD stuff can't use the backend .a files directly, as they would end
  up as .a inside the .a. So it looks at the *backend*.a and manually includes
  those files.



Index: elfutils-0.123/backends/Makefile.am
===================================================================
--- elfutils-0.123.orig/backends/Makefile.am	2006-08-15 21:44:19.000000000 +0000
+++ elfutils-0.123/backends/Makefile.am	2006-08-15 21:48:43.000000000 +0000
@@ -40,12 +40,10 @@
 
 
 modules = i386 sh x86_64 ia64 alpha arm sparc ppc ppc64 s390
-libebl_pic = libebl_i386_pic.a libebl_sh_pic.a libebl_x86_64_pic.a \
-	     libebl_ia64_pic.a libebl_alpha_pic.a libebl_arm_pic.a \
-	     libebl_sparc_pic.a libebl_ppc_pic.a libebl_ppc64_pic.a \
-	     libebl_s390_pic.a
-noinst_LIBRARIES = $(libebl_pic)
-noinst_DATA = $(libebl_pic:_pic.a=.so)
+libebl_pic = libebl_backend_pic.a
+libebl_nopic = libebl_backend_nopic.a
+
+noinst_LIBRARIES = $(libebl_pic) $(libebl_nopic)
 
 
 if MUDFLAP
@@ -58,87 +56,39 @@
 libmudflap =
 endif
 
-
-textrel_check = if readelf -d $@ | fgrep -q TEXTREL; then exit 1; fi
-
-libebl_%.so: libebl_%_pic.a libebl_%.map $(libelf) $(libdw)
-	$(LINK) -shared -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
-		-Wl,--version-script,$(word 2,$^) \
-		-Wl,-z,defs $(LD_AS_NEEDED) $(libelf) $(libdw) $(libmudflap)
-	$(textrel_check)
-
-libebl_%.map: Makefile
-	echo 'ELFUTILS_$(PACKAGE_VERSION) { global: $*_init; local: *; };' > $@
-
-
 i386_SRCS = i386_init.c i386_symbol.c i386_corenote.c \
 	    i386_retval.c i386_regs.c
-libebl_i386_pic_a_SOURCES = $(i386_SRCS)
-am_libebl_i386_pic_a_OBJECTS = $(i386_SRCS:.c=.os)
-
 sh_SRCS = sh_init.c sh_symbol.c
-libebl_sh_pic_a_SOURCES = $(sh_SRCS)
-am_libebl_sh_pic_a_OBJECTS = $(sh_SRCS:.c=.os)
-
 x86_64_SRCS = x86_64_init.c x86_64_symbol.c x86_64_corenote.c \
 	      x86_64_retval.c x86_64_regs.c
-libebl_x86_64_pic_a_SOURCES = $(x86_64_SRCS)
-am_libebl_x86_64_pic_a_OBJECTS = $(x86_64_SRCS:.c=.os)
-
 ia64_SRCS = ia64_init.c ia64_symbol.c ia64_regs.c ia64_retval.c
-libebl_ia64_pic_a_SOURCES = $(ia64_SRCS)
-am_libebl_ia64_pic_a_OBJECTS = $(ia64_SRCS:.c=.os)
-
 alpha_SRCS = alpha_init.c alpha_symbol.c alpha_retval.c
-libebl_alpha_pic_a_SOURCES = $(alpha_SRCS)
-am_libebl_alpha_pic_a_OBJECTS = $(alpha_SRCS:.c=.os)
-
 arm_SRCS = arm_init.c arm_symbol.c
-libebl_arm_pic_a_SOURCES = $(arm_SRCS)
-am_libebl_arm_pic_a_OBJECTS = $(arm_SRCS:.c=.os)
-
 sparc_SRCS = sparc_init.c sparc_symbol.c sparc_regs.c
-libebl_sparc_pic_a_SOURCES = $(sparc_SRCS)
-am_libebl_sparc_pic_a_OBJECTS = $(sparc_SRCS:.c=.os)
-
 ppc_SRCS = ppc_init.c ppc_symbol.c ppc_retval.c ppc_regs.c
-libebl_ppc_pic_a_SOURCES = $(ppc_SRCS)
-am_libebl_ppc_pic_a_OBJECTS = $(ppc_SRCS:.c=.os)
-
 ppc64_SRCS = ppc64_init.c ppc64_symbol.c ppc64_retval.c ppc_regs.c
-libebl_ppc64_pic_a_SOURCES = $(ppc64_SRCS)
-am_libebl_ppc64_pic_a_OBJECTS = $(ppc64_SRCS:.c=.os)
-
 s390_SRCS = s390_init.c s390_symbol.c s390_regs.c s390_retval.c
-libebl_s390_pic_a_SOURCES = $(s390_SRCS)
-am_libebl_s390_pic_a_OBJECTS = $(s390_SRCS:.c=.os)
 
+all_SRCS = $(shell echo \
+	   $(i386_SRCS) $(sh_SRCS) $(x86_64_SRCS) $(ia64_SRCS) $(alpha_SRCS) \
+	   $(arm_SRCS) $(sparc_SRCS) $(ppc_SRCS) $(ppc64_SRCS) $(s390_SRCS) \
+	   | tr -s ' ' '\n' | sort -u)
+
+libebl_backend_pic_a_SOURCES = $(all_SRCS)
+am_libebl_backend_pic_a_OBJECTS = $(all_SRCS:.c=.os)
 
-%.os: %.c
+libebl_backend_nopic_a_SOURCES = $(all_SRCS)
+am_libebl_backend_nopic_a_OBJECTS = $(all_SRCS:.c=.o)
+
+%.os: %.c %.o
 	if $(COMPILE) -c -o $@ -fpic -DPIC -DSHARED -MT $@ -MD -MP \
 	  -MF "$(DEPDIR)/$*.Tpo" `test -f '$<' || echo '$(srcdir)/'`$<; \
 	then mv -f "$(DEPDIR)/$*.Tpo" "$(DEPDIR)/$*.Po"; \
 	else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; \
 	fi
 
-install: install-am install-ebl-modules
-install-ebl-modules:
-	$(mkinstalldirs) $(DESTDIR)$(libdir)/$(LIBEBL_SUBDIR)
-	for m in $(modules); do \
-	  $(INSTALL_PROGRAM) libebl_$${m}.so $(DESTDIR)$(libdir)/$(LIBEBL_SUBDIR)/libebl_$${m}-$(PACKAGE_VERSION).so; \
-	  ln -fs libebl_$${m}-$(PACKAGE_VERSION).so $(DESTDIR)$(libdir)/$(LIBEBL_SUBDIR)/libebl_$${m}.so; \
-	done
-
-uninstall: uninstall-am
-	for m in $(modules); do \
-	  rm -f $(DESTDIR)$(libdir)/$(LIBEBL_SUBDIR)/libebl_$${m}-$(PACKAGE_VERSION).so; \
-	  rm -f $(DESTDIR)$(libdir)/$(LIBEBL_SUBDIR)/libebl_$${m}.so; \
-	done
-	rmdir --ignore-fail-on-non-empty $(DESTDIR)$(libdir)/$(LIBEBL_SUBDIR)
-
 noinst_HEADERS = libebl_CPU.h common-reloc.c
 EXTRA_DIST = $(foreach m,$(modules),$($(m)_SRCS)) $(modules:=_reloc.def)
 
-CLEANFILES = *.gcno *.gcda \
-	     $(foreach m,$(modules),\
-		       libebl_$(m).so $(am_libebl_$(m)_pic_a_OBJECTS))
+CLEANFILES = *.gcno *.gcda $(am_libebl_backend_nopic_a_OBJECTS) \
+	     $(am_libebl_backend_pic_a_OBJECTS)
Index: elfutils-0.123/Makefile.am
===================================================================
--- elfutils-0.123.orig/Makefile.am	2006-08-15 21:44:14.000000000 +0000
+++ elfutils-0.123/Makefile.am	2006-08-15 21:44:19.000000000 +0000
@@ -29,7 +29,7 @@
 
 mini_SUBDIRS = config m4 lib libelf libelf-po
 # Add doc back when we have some real content.
-all_SUBDIRS = libebl libdwfl libdw libcpu libasm backends src po tests
+all_SUBDIRS = backends libebl libdwfl libdw libcpu libasm src po tests
 SUBDIRS = $(mini_SUBDIRS) $(all_SUBDIRS)
 
 EXTRA_DIST = elfutils.spec GPG-KEY NOTES
Index: elfutils-0.123/libebl/Makefile.am
===================================================================
--- elfutils-0.123.orig/libebl/Makefile.am	2006-08-15 21:44:19.000000000 +0000
+++ elfutils-0.123/libebl/Makefile.am	2006-08-15 21:48:33.000000000 +0000
@@ -40,7 +40,7 @@
 PACKAGE_VERSION = @PACKAGE_VERSION@
 LIBEBL_SUBDIR = @LIBEBL_SUBDIR@
 
-lib_LIBRARIES = libebl.a
+noinst_LIBRARIES = libebl.a libebl_nopic.a
 
 pkginclude_HEADERS = libebl.h
 
@@ -61,6 +61,14 @@
 	      eblsysvhashentrysize.c
 
 libebl_a_SOURCES = $(gen_SOURCES)
+am_libebl_a_OBJECTS = $(gen_SOURCES:.c=.os)
+libebl_libadd_pic = $(shell ar t $(top_srcdir)/backends/libebl_backend_pic.a)
+libebl_a_LIBADD = $(foreach file,$(libebl_libadd_pic),$(top_srcdir)/backends/$(file))
+
+libebl_nopic_a_SOURCES = $(gen_SOURCES)
+am_libebl_nopic_a_OBJECTS = $(gen_SOURCES:.c=.o)
+libebl_libadd_nopic = $(shell ar t $(top_srcdir)/backends/libebl_backend_nopic.a)
+libebl_nopic_a_LIBADD = $(foreach file,$(libebl_libadd_nopic),$(top_srcdir)/backends/$(file))
 
 
 %.os: %.c %.o
@@ -71,6 +79,14 @@
 	else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; \
 	fi
 
+install: install-am
+	$(mkinstalldirs) $(DESTDIR)$(libdir)
+	$(INSTALL_PROGRAM) libebl_nopic.a $(DESTDIR)$(libdir)/libebl.a
+
+uninstall: uninstall-am
+	rm -f $(DESTDIR)$(libdir)/libebl.a
+	rmdir --ignore-fail-on-non-empty $(DESTDIR)$(libdir)
+
 noinst_HEADERS = libeblP.h ebl-hooks.h
 
-CLEANFILES = $(am_libebl_pic_a_OBJECTS) *.gcno *.gcda
+CLEANFILES = $(am_libebl_a_OBJECTS) $(am_libebl_pic_a_OBJECTS) *.gcno *.gcda
Index: elfutils-0.123/libebl/eblclosebackend.c
===================================================================
--- elfutils-0.123.orig/libebl/eblclosebackend.c	2006-08-15 21:44:14.000000000 +0000
+++ elfutils-0.123/libebl/eblclosebackend.c	2006-08-15 21:44:19.000000000 +0000
@@ -65,10 +65,6 @@
       /* Run the destructor.  */
       ebl->destr (ebl);
 
-      /* Close the dynamically loaded object.  */
-      if (ebl->dlhandle != NULL)
-	(void) dlclose (ebl->dlhandle);
-
       /* Free the resources.  */
       free (ebl);
     }
Index: elfutils-0.123/libebl/eblopenbackend.c
===================================================================
--- elfutils-0.123.orig/libebl/eblopenbackend.c	2006-08-15 21:44:14.000000000 +0000
+++ elfutils-0.123/libebl/eblopenbackend.c	2006-08-15 21:44:19.000000000 +0000
@@ -241,6 +241,31 @@
   result->sysvhash_entrysize = sizeof (Elf32_Word);
 }
 
+typedef const char *(fn_bhinit_t) (Elf *, GElf_Half, Ebl *, size_t);
+extern fn_bhinit_t i386_init, ia64_init, alpha_init, x86_64_init, ppc_init,
+	ppc64_init, sh_init, arm_init, sparc_init, sparc_init, sparc_init,
+	s390_init;
+
+static ebl_bhinit_t
+get_init_function(cnt)
+    size_t cnt;
+{
+  switch(cnt) {
+      case  0: return i386_init;
+      case  1: return ia64_init;
+      case  2: return alpha_init;
+      case  3: return x86_64_init;
+      case  4: return ppc_init;
+      case  5: return ppc64_init;
+      case  6: return sh_init;
+      case  7: return arm_init;
+      case  8: return sparc_init;
+      case  9: return sparc_init;
+      case 10: return sparc_init;
+      case 11: return s390_init;
+      default: return (ebl_bhinit_t) NULL;
+  }
+}
 
 /* Find an appropriate backend for the file associated with ELF.  */
 static Ebl *
@@ -306,63 +331,28 @@
 	    result->data = elf->state.elf32.ehdr->e_ident[EI_DATA];
 	  }
 
-#ifndef LIBEBL_SUBDIR
-# define LIBEBL_SUBDIR PACKAGE
-#endif
-#define ORIGINDIR "$ORIGIN/../$LIB/" LIBEBL_SUBDIR "/"
-
-	/* Give it a try.  At least the machine type matches.  First
-           try to load the module.  */
-	char dsoname[100];
-	strcpy (stpcpy (stpcpy (dsoname, ORIGINDIR "libebl_"),
-			machines[cnt].dsoname),
-		".so");
-
-	void *h = dlopen (dsoname, RTLD_LAZY);
-	if (h == NULL)
-	  {
-	    strcpy (stpcpy (stpcpy (dsoname, "libebl_"),
-			    machines[cnt].dsoname),
-		    ".so");
-	    h = dlopen (dsoname, RTLD_LAZY);
-	  }
-
-	  /* Try without an explicit path.  */
-	if (h != NULL)
-	  {
-	    /* We managed to load the object.  Now see whether the
-	       initialization function likes our file.  */
-	    static const char version[] = MODVERSION;
-	    const char *modversion;
-	    ebl_bhinit_t initp;
-	    char symname[machines[cnt].prefix_len + sizeof "_init"];
-
-	    strcpy (mempcpy (symname, machines[cnt].prefix,
-			     machines[cnt].prefix_len), "_init");
-
-	    initp = (ebl_bhinit_t) dlsym (h, symname);
-	    if (initp != NULL
-		&& (modversion = initp (elf, machine, result, sizeof (Ebl)))
-		&& strcmp (version, modversion) == 0)
-	      {
-		/* We found a module to handle our file.  */
-		result->dlhandle = h;
-		result->elf = elf;
-
-		/* A few entries are mandatory.  */
-		assert (result->name != NULL);
-		assert (result->destr != NULL);
-
-		return result;
-	      }
+        /* Try the init function */
+        static const char version[] = MODVERSION;
+        const char *modversion;
+        ebl_bhinit_t initp;
+
+        initp = get_init_function(cnt);
+        if (initp != NULL
+            && (modversion = initp (elf, machine, result, sizeof (Ebl)))
+            && strcmp (version, modversion) == 0)
+          {
+            /* We found a module to handle our file.  */
+            result->elf = elf;
+
+            /* A few entries are mandatory.  */
+            assert (result->name != NULL);
+            assert (result->destr != NULL);
 
-	    /* Not the module we need.  */
-	    (void) dlclose (h);
-	  }
+            return result;
+          }
 
 	/* We cannot find a DSO but the emulation/machine ID matches.
 	   Return that information.  */
-	result->dlhandle = NULL;
 	result->elf = elf;
 	result->name = machines[cnt].prefix;
 	fill_defaults (result);
@@ -371,7 +361,6 @@
       }
 
   /* Nothing matched.  We use only the default callbacks.   */
-  result->dlhandle = NULL;
   result->elf = elf;
   result->emulation = "<unknown>";
   result->name = "<unknown>";
Index: elfutils-0.123/libebl/libeblP.h
===================================================================
--- elfutils-0.123.orig/libebl/libeblP.h	2006-08-15 21:44:14.000000000 +0000
+++ elfutils-0.123/libebl/libeblP.h	2006-08-15 21:44:19.000000000 +0000
@@ -80,8 +80,6 @@
   /* Size of entry in Sysv-style hash table.  */
   int sysvhash_entrysize;
 
-  /* Internal data.  */
-  void *dlhandle;
 };
 
 
Index: elfutils-0.123/src/Makefile.am
===================================================================
--- elfutils-0.123.orig/src/Makefile.am	2006-08-15 21:44:19.000000000 +0000
+++ elfutils-0.123/src/Makefile.am	2006-08-15 21:48:33.000000000 +0000
@@ -106,18 +106,18 @@
 readelf_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
 nm_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
 size_LDADD = $(libelf) $(libeu) $(libmudflap)
-strip_LDADD = $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
-ld_LDADD = $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
+strip_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
+ld_LDADD = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
 if NATIVE_LD
 # -ldl is always needed for libebl.
 ld_LDADD += libld_elf.a
 endif
 ld_LDFLAGS = -rdynamic
-elflint_LDADD  = $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
+elflint_LDADD  = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
 findtextrel_LDADD = $(libdw) $(libelf) $(libmudflap)
 addr2line_LDADD = $(libdw) $(libmudflap)
-elfcmp_LDADD = $(libebl) $(libelf) $(libmudflap) -ldl
-objdump_LDADD  = $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
+elfcmp_LDADD = $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+objdump_LDADD  = $(libdw) $(libebl) $(libelf) $(libeu) $(libmudflap) -ldl
 ranlib_LDADD = $(libelf) $(libeu) $(libmudflap)
 strings_LDADD = $(libelf) $(libeu) $(libmudflap)
 
Index: elfutils-0.123/tests/Makefile.am
===================================================================
--- elfutils-0.123.orig/tests/Makefile.am	2006-08-15 21:44:19.000000000 +0000
+++ elfutils-0.123/tests/Makefile.am	2006-08-15 21:44:19.000000000 +0000
@@ -187,15 +187,15 @@
 allregs_LDADD = $(libdw) $(libmudflap)
 find_prologues_LDADD = $(libdw) $(libmudflap)
 #show_ciefde_LDADD = ../libdwarf/libdwarf.so $(libelf) $(libmudflap)
-asm_tst1_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst2_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst3_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst4_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst5_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst6_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst7_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst8_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
-asm_tst9_LDADD = $(libasm) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst1_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst2_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst3_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst4_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst5_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst6_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst7_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst8_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
+asm_tst9_LDADD = $(libasm) $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
 dwflmodtest_LDADD = $(libdw) $(libebl) $(libelf) $(libmudflap) -ldl
 rdwrmmap_LDADD = $(libelf)
 

