?
Solved

practical dlsym and dlerror usage

Posted on 2003-03-13
3
Medium Priority
?
350 Views
Last Modified: 2013-12-26
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.

Questions:

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.
0
Comment
Question by:ewest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8135961
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));
  else
     printf("couldn't find bar: %s ...\n", dlerror());

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

0
 
LVL 2

Author Comment

by:ewest
ID: 8140319
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.

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 8140792
Points refunded and placed in PAQ

Computer101
E-E Admin
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question