• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

practical dlsym and dlerror usage

The man pages for dlsym state that dlsym will return NULL on error. ...and dlerror() will return non-NULL if an error has occured between initialization and the last invocation of the dlerror.

I'm running into a case where dlsym returns a valid pointer, but dlerror reports an error.

My code is basically:

        void *ptr;
        char *str;

        ptr = dlsym((void *)*libHandle, symName);
        if (str = dlerror()) {
            TRACE_MSG(( "dlsym: %s: %s\n", symName, str));
            *symPtr = NULL;
            *status = ENOTSUPP;
            err = StatusFailure;
        } else {
            *symPtr = ptr;

Note dlopen is called with RTLD_LAZY, although there are some on the 'net who are suggesting only RTLD_NOW should be used. Seems to me that if RTLD_LAZY works inconsistently for a given implementation of libld, complaints should be directed at the vendor of that implementation.

Back on topic...

The error reported by dlerror() is that a symbol foo can't be found. However, dlsym() returned a valid function pointer. And "foo" is a symbol from some other library.


Do I just depend on dlsym != NULL as a success indicator?
Do I call dlerror() before dlsym() to "flush" a stale error mesg?

...and of course there is the ever popular approach of demanding that the developers of the other library deal with the missing symbol.
1 Solution
I found this code, may be it wil help

typedef int (*fp_t)(int); /* a function poiner */

void* so_object;
fp_t  func;

if (so_object= dlopen("foo.so", 1)) { /* open shared object */
  if (func= dlsym(so_object, "bar") {  /* get address of bar */
     printf("bar42 returned %d\n", func(42));
     printf("couldn't find bar: %s ...\n", dlerror());

  dlclose(so_object); /* close shared object */
  printf("couldn't open foo.so: %s\n", dlerror());

ewestAuthor Commented:
Ok. I did more 'net searching. The man pages in linux are really somewhat misleading. In those pages, a code snippet akin to what I posted is given as the "correct" way to use dlsym and dlerror. In particular to deal with the case that it may be valid for a symbol to be NULL. Solaris documentation and many of the other UNIXes I've checked pretty much agree, that you check the return value of dlsym *before* calling dlerror. Personally, I have a hard time coming up with a reason for dlsym returning NULL where it is not an error (e.g. symbol not found).

I have also found on the 'net suggestions to call dlerror, before dlsym to flush stale errors. Again I think this is not how dlsym, dlerror are intended to be used.

Accordingly, I'm going with

     pFunc = dlsym(fdes, "foo");
     if (NULL == pFunc) {
         printf("error: %s\n", dlerror());

For the case (e.g. loading an older library) where pFunc is NULL, it is easy to special case the error handling.

HamdyHassan, thanks for making the attempt, but that code snippet is what I was describing in my first question.

Points refunded and placed in PAQ

E-E Admin

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now