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