dlsym.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 yi.qiu first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtm.h>
  12. #include "dlmodule.h"
  13. /**
  14. * @brief look up the address of a symbol in a dynamically loaded shared library.
  15. *
  16. * @param handle the handle returned by dlopen() when the library was previously loaded.
  17. * @param symbol A string containing the name of the symbol to locate.
  18. * @return void* On success, it returns a pointer to the symbol. Otherwise, it returns RT_NULL.
  19. *
  20. * @note This function is an API of POSIX standard, which is commonly used in conjunction with dlopen() to retrieve function pointers from shared libraries.
  21. * the input symbol name, which can be the name of a function or variable, is compared with each symbol
  22. * in the module symbol table. if the same symbol is found, return its address.
  23. */
  24. void* dlsym(void *handle, const char* symbol)
  25. {
  26. int i;
  27. struct rt_dlmodule *module;
  28. RT_ASSERT(handle != RT_NULL);
  29. module = (struct rt_dlmodule *)handle;
  30. for(i=0; i<module->nsym; i++)
  31. {
  32. if (rt_strcmp(module->symtab[i].name, symbol) == 0)
  33. return (void*)module->symtab[i].addr;
  34. }
  35. return RT_NULL;
  36. }
  37. RTM_EXPORT(dlsym)