Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: it's legal for dlsym() to return NULL
Browse files Browse the repository at this point in the history
A symbol name can map to NULL. Check dlerror() to see if a real error happened.
  • Loading branch information
bnoordhuis committed Dec 18, 2011
1 parent e9235a3 commit feb267e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion include/uv.h
Expand Up @@ -1250,7 +1250,8 @@ UV_EXTERN uv_err_t uv_dlopen(const char* filename, uv_lib_t* library);
UV_EXTERN uv_err_t uv_dlclose(uv_lib_t library);

/*
* Retrieves a data pointer from a dynamic library.
* Retrieves a data pointer from a dynamic library. It is legal for a symbol to
* map to NULL.
*/
UV_EXTERN uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr);

Expand Down
10 changes: 8 additions & 2 deletions src/unix/dl.c
Expand Up @@ -53,8 +53,14 @@ uv_err_t uv_dlclose(uv_lib_t library) {


uv_err_t uv_dlsym(uv_lib_t library, const char* name, void** ptr) {
void* address = dlsym(library, name);
if (address == NULL) {
void* address;

/* Reset error status. */
dlerror();

address = dlsym(library, name);

if (dlerror()) {
return uv_inval_;
}

Expand Down

4 comments on commit feb267e

@wereHamster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purely out of interest. Do you know how to create a symbol so that dlsym() returns NULL when you try to resolve it?

@bnoordhuis
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It depends on your toolchain. With gcc, you compile with -Wl,--defsym=foo=0x0 or -Wl,--just-symbols=symbols.txt (where symbols.txt contains one or more symbol = address mappings).

@wereHamster
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. Though I can't imagine why anyone would use foo=0x0 in a real linux application or library.

@bnoordhuis
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sometimes used by application plugins to signal that they're compatible with a particular API version but don't support all the functions.

Please sign in to comment.