SharedLibrary_VX.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // SharedLibrary_VX.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/SharedLibrary_VX.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: SharedLibrary
  8. // Module: SharedLibrary
  9. //
  10. // Copyright (c) 2004-2011, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/SharedLibrary_VX.h"
  16. #include "Poco/Exception.h"
  17. #include "Poco/Format.h"
  18. #include <loadLib.h>
  19. #include <unldLib.h>
  20. #include <ioLib.h>
  21. #include <symLib.h>
  22. #include <sysSymTbl.h>
  23. #include <cstring>
  24. struct SymLookup
  25. {
  26. const char* name;
  27. int group;
  28. void* addr;
  29. };
  30. extern "C" bool lookupFunc(char* name, int val, SYM_TYPE type, int arg, UINT16 group)
  31. {
  32. SymLookup* symLookup = reinterpret_cast<SymLookup*>(arg);
  33. if (group == symLookup->group && std::strcmp(name, symLookup->name) == 0)
  34. {
  35. symLookup->addr = reinterpret_cast<void*>(val);
  36. return TRUE;
  37. }
  38. else return FALSE;
  39. }
  40. namespace Poco {
  41. FastMutex SharedLibraryImpl::_mutex;
  42. SharedLibraryImpl::SharedLibraryImpl():
  43. _moduleId(0)
  44. {
  45. }
  46. SharedLibraryImpl::~SharedLibraryImpl()
  47. {
  48. }
  49. void SharedLibraryImpl::loadImpl(const std::string& path, int /*flags*/)
  50. {
  51. FastMutex::ScopedLock lock(_mutex);
  52. if (_moduleId) throw LibraryAlreadyLoadedException(path);
  53. int fd = open(const_cast<char*>(path.c_str()), O_RDONLY, 0);
  54. if (fd)
  55. {
  56. _moduleId = loadModule(fd, LOAD_GLOBAL_SYMBOLS);
  57. if (!_moduleId)
  58. {
  59. int err = errno;
  60. close(fd);
  61. throw LibraryLoadException(Poco::format("error %d", err));
  62. }
  63. }
  64. else
  65. {
  66. int err = errno;
  67. throw LibraryLoadException(Poco::format("cannot open library (error %d)", err));
  68. }
  69. _path = path;
  70. }
  71. void SharedLibraryImpl::unloadImpl()
  72. {
  73. FastMutex::ScopedLock lock(_mutex);
  74. if (_moduleId)
  75. {
  76. unldByModuleId(_moduleId, 0);
  77. _moduleId = 0;
  78. }
  79. }
  80. bool SharedLibraryImpl::isLoadedImpl() const
  81. {
  82. return _moduleId != 0;
  83. }
  84. void* SharedLibraryImpl::findSymbolImpl(const std::string& name)
  85. {
  86. poco_assert (_moduleId != 0);
  87. FastMutex::ScopedLock lock(_mutex);
  88. MODULE_INFO mi;
  89. if (!moduleInfoGet(_moduleId, &mi)) return 0;
  90. SymLookup symLookup;
  91. symLookup.name = name.c_str();
  92. symLookup.group = mi.group;
  93. symLookup.addr = 0;
  94. symEach(sysSymTbl, reinterpret_cast<FUNCPTR>(lookupFunc), reinterpret_cast<int>(&symLookup));
  95. return symLookup.addr;
  96. }
  97. const std::string& SharedLibraryImpl::getPathImpl() const
  98. {
  99. return _path;
  100. }
  101. std::string SharedLibraryImpl::suffixImpl()
  102. {
  103. return ".out";
  104. }
  105. } // namespace Poco