Subject: Re: Problem with dlopen
From: "Frank Motta" <[email-masked]>
To: <[email-masked]>
Cc: <[email-masked]>
Date: Mon, 8 Apr 2002 15:32:03 -0700 (PDT)

OOPS!!!!
Thanks - I actually caught that before sending the email - that didn't
seem to fix it either...

below is a revised file set - :/

Hi,
Maybe I hadn't stated this well --

I need some help creating a dll which I can open and use dlopen and dlsym.

The description is below...

Could someone give me a pointer?

thanks!

      The problem seems to be in the -creation- of the dll.

      the instructions at http://cygwin.com/cygwin-ug-net/dll.html result in
  many link-time errors and the method I -was- using (see below) does not
  export the symbol I need.

      Any ideas?

from testdlopen.c clip *<
---------------------------------------------------------------------

  #include <dlfcn.h> /* dll support */
  #include <stdlib.h> // NULL

  #define MODULE_IMPORT_STR "mod_import"

  int main(int argc, char **argv) {
  char *mod_file_name = "test.dll";
  void *handle;
  void *mod;
  // void *import;
  char *err;
  void *(*import)();

   handle = dlopen(mod_file_name, (RTLD_NOW | RTLD_GLOBAL));
   if ((err = dlerror()) != NULL) {
    printf("Error [%s] in dlopen(%s, %d)\n", err, mod_file_name, (RTLD_NOW |
  RTLD_GLOBAL));
    exit(1);
   }

   if (handle != NULL) {
    /* handle now points to a valid lib */
    dlerror(); /* clear errors */
    import = dlsym(handle, MODULE_IMPORT_STR);

    if ((err = dlerror()) == NULL) {
     mod = (void *)(*import)(argv);
     if (mod != NULL) { /* import it */
     } else {
      printf("Error: execution of imported method failed\n");
      exit(0);
     }
    } else {
     printf("Error [%s] in dlsym(%ul, %s)\n", err, (unsigned int) handle,
  MODULE_IMPORT_STR);
    }
   }

   exit(0);
  }
  ------------------------------------- >8 clip testdlopen.c

from makefile.exe 8<
--------------------------------------------------------------------------

  CC=gcc
  INCLUDE=-I.
  CFLAGS=-Wall
  ifdef debug
  CFLAGS=-g -Wall -DDEBUG -rdynamic
  endif
  #LDFLAGS=-ldl -Wl,-export-dynamic
  LDFLAGS= -Wl,-export-dynamic
  OBJS=util.o output.o types.o hooks.o tsh.o basename.o
  SRCS=output.c types.c tsh.c hooks.c util.c modules.h types.h basename.c

  tsh: $(OBJS)
          $(CC) $(LDFLAGS) -o tsh $(OBJS)

  testdlopen: testdlopen.o
          $(CC) $(LDFLAGS) -o testdlopen testdlopen.o

  testdlopen.o: testdlopen.c
          $(CC) $(INCLUDE) $(CFLAGS) -c testdlopen.c
  ---------------------------------------- >8 clip makefile.exe

The run of testdlopen.exe
Error [dlsym: Win32 error 127] in dlsym(438501376l, mod_import)

  from makefile.dll clip 8<-----------------------------
  # This creates a dll without the exports I need - see def below
  TARGET=simpletest
  DLL_SUFF=.dll
  LIB_SUFF=.a
  DLL_EXP_LIB =$(TARGET)$(DLL_SUFF)$(LIB_SUFF)
  DLL_NAME=$(TARGET)$(DLL_SUFF)
  DLL_LDFLAGS=-Wl,--export-all-symbols
  INCLUDE=

  simpletest.dll: Makefile simpletest.o
          gcc -shared -Wl,--out-implib=$(DLL_EXP_LIB) -o $(DLL_NAME)
  $(DLL_OBJS) $
  (DLL_LDFLAGS) $(DLL_LDLIBS) -Wl,--export-all-symbols
          dlltool --def simpletest.def --dllname simpletest.dll --output-lib
simpl
  etest.a

  #-Wl,simpletest.def
  simpletest.o: simpletest.c ../../modules.h ../../global.h ../../types.h
          $(CC) $(INCLUDE) -c simpletest.c
  ------------------------------------- >8 clip makefile

  from test.def clip 8< ----------------------------------------
  EXPORTS
  mod_import
  ------------------------------------ >8 clip test.def

  from simpltest.c 8< ---------------------------------------------------

  void *foo();
  void *(*export_foo)() = foo;
  void *mod_import(char **argv) {
     return export_foo;
  }

  char *hello = "hello world";
  void *foo() {
          return (void *) hello;
  }
  ------------------------------------ >8 clip test.c
  from nm simpletest.dll | grep T clip 8< ------------------------------
  100010f4 T _DllMain@12
  100012a4 T _GetModuleHandleA@4
  100012ac T __CTOR_LIST__
  100012b4 T __DTOR_LIST__
  100012ac T ___CTOR_LIST__
  100012b4 T ___DTOR_LIST__
  1000113c T __cygwin_crt0_common@8
  10001024 T __cygwin_dll_entry@12
  100010d4 T __cygwin_noncygwin_dll_entry@12
  10001000 T __end__
  1000125c T _calloc
  1000110c T _cygwin_attach_dll
  10001104 T _cygwin_detach_dll
  1000129c T _cygwin_internal
  10001294 T _cygwin_premain0
  1000128c T _cygwin_premain1
  10001284 T _cygwin_premain2
  1000127c T _cygwin_premain3
  10001254 T _dll_dllcrt0
  10001000 T _end
  1000126c T _free
  10001274 T _malloc
  10001264 T _realloc
  10001000 T end
  100012bc T etext
  --------------------------------------------- >8 nm output

  Notice the -lack- of mod_import!!!

-- 
Frank Motta
[email-masked]

If you cannot recognize divinity in your fellow man, then you will not
realize the devine within yourself...

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/