DLSYM(3POSIX)
π DLSYM β get the address of a symbol from a symbol table handle π
okay bestie, welcome to the POSIX Programmer's Manual page for dlsym(). π«Ά
πΈ PROLOG πΈ
so like, real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β so if you're on Linux, go peek at the corresponding Linux manual page for the actual Linux behavior, mkay? Or, plot twist, the interface might not even be implemented on Linux at all. π Check before you ship, fr fr.
β¨ NAME β¨
dlsym β get the address of a symbol from a symbol table handle
π SYNOPSIS π
#include <dlfcn.h>
void *dlsym(void *restrict handle, const char *restrict name);
π¦ DESCRIPTION π¦
So what does dlsym() actually do? It shall obtain the address of a symbol β and a symbol here can be a function identifier or a data object identifier β that's defined in the symbol table identified by the handle argument. Iconic behavior honestly.
The handle argument is a symbol table handle returned from a call to dlopen() β and, key detail, one that has not since been released by a call to dlclose(). π¨ (Released it already? Then don't come crying to dlsym().) And name is the symbol's name as a character string.
The return value from dlsym(), cast to a pointer to the type of the named symbol, can be used to call the named symbol (when it's a function) or access the contents of the named symbol (when it's a data object). slay.
dlsym() shall search for the named symbol in the symbol table referenced by handle. Now if that symbol table was created with lazy loading (see RTLD_LAZY in dlopen()), then load ordering shall be used in dlsym() operations to relocate executable object files needed to resolve the symbol. And the symbol resolution algorithm used shall be dependency order, as described in dlopen().
Also, heads up: the RTLD_DEFAULT and RTLD_NEXT symbolic constants (which may be defined in <dlfcn.h>) are reserved for future use as special values that applications may be allowed to use for handle. So like... they're the VIP section. Reserved. For future use. Noted. π
π RETURN VALUE π
Upon successful completion:
- if
namenames a function identifier,dlsym()shall return the address of the function converted from type pointer to function to type pointer tovoid; - otherwise,
dlsym()shall return the address of the data object associated with the data object identifier named byname, converted from a pointer to the type of the data object to a pointer tovoid.
And if things go sideways β specifically, if handle does not refer to a valid symbol table handle, or if the symbol named by name cannot be found in the symbol table associated with handle β then dlsym() shall return a null pointer. π
More detailed diagnostic information shall be available through dlerror(). She's the one with the receipts. π§Ύ
π ERRORS π
No errors are defined.
The following sections are informative. (i.e. the vibe is chill, the content is not normative, iykyk)
π EXAMPLES π
okay here's a lil demo showing how dlopen() and dlsym() can be used to access either a function or a data object. For simplicity, error checking has been omitted. (bestie. omitted. π)
void *handle;
int (*fptr)(int), *iptr, result;
/* open the needed symbol table */
handle = dlopen("/usr/home/me/libfoo.so", RTLD_LOCAL | RTLD_LAZY);
/* find the address of the function my_function */
fptr = (int (*)(int))dlsym(handle, "my_function");
/* find the address of the data object my_object */
iptr = (int *)dlsym(handle, "my_OBJ");
/* invoke my_function, passing the value of my_OBJ as the parameter */
result = (*fptr)(*iptr);
π APPLICATION USAGE π
The following special purpose values for handle are reserved for future use and have the indicated meanings:
RTLD_DEFAULTβ The identifier lookup happens in the normal global scope; that is, a search for an identifier usinghandlewould find the same definition as a direct use of this identifier in the program code.RTLD_NEXTβ Specifies the next executable object file after this one that definesname. "This one" refers to the executable object file containing the invocation ofdlsym(). The next executable object file is the one found upon the application of a load order symbol resolution algorithm (seedlopen()). The next symbol is either one of global scope (because it was introduced as part of the original process image or because it was added with adlopen()operation including theRTLD_GLOBALflag), or is in an executable object file that was included in the samedlopen()operation that loaded this one.
The RTLD_NEXT flag is useful to navigate an intentionally created hierarchy of multiply-defined symbols created through interposition. For example, if a program wished to create an implementation of malloc() that embedded some statistics gathering about memory allocations, such an implementation could use the real malloc() definition to perform the memory allocation β and itself only embed the necessary logic to implement the statistics gathering function. main character energy, honestly. π
β οΈ Note that conversion from a void * pointer to a function pointer as in:
fptr = (int (*)(int))dlsym(handle, "my_function");
is not defined by the ISO C standard. This standard requires this conversion to work correctly on conforming implementations. So like, POSIX said "we got you", but ISO C said "not my problem". π«
π§ RATIONALE π§
None.
β FUTURE DIRECTIONS β
None.
π₯ SEE ALSO π₯
dlclose()dlerror()dlopen()
The Base Definitions volume of POSIX.1β2017, <dlfcn.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! πβ¨ this one came from IEEE/The Open Group, 2017 β go forth and dlsym() responsibly, ily, bye ππ¦πΈ
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms