-
Notifications
You must be signed in to change notification settings - Fork 2
/
LibrariesInfo.cpp
268 lines (228 loc) · 7.62 KB
/
LibrariesInfo.cpp
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* Copyright © 2007 Michael Pyne <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "LibrariesInfo.h"
#include <QFile>
#include <QFileInfo>
#include <QProcess>
#include <QTextStream>
#include <QDir>
LibrariesInfo::LibrariesInfo()
: elfClass(Unknown)
{
splitPaths(getBasePaths(), basePaths32, basePaths64);
splitPaths(getSystemEnvPaths(), envPaths32, envPaths64);
}
void LibrariesInfo::loadFile(const QString &path)
{
LibPtr executable = Lib::create(path);
cache[path] = executable;
loadFileRecursive(path, *executable);
}
LibrariesStatistics LibrariesInfo::getStatistics() const
{
LibrariesStatistics ret;
ret.depsCount = cache.size();
ret.depsSizeInBytes = 0;
foreach (const QString &key, cache.keys()) {
LibPtr lib = cache.value(key);
if (!lib.isNull()) {
QFileInfo fi(lib->path);
ret.depsSizeInBytes += fi.size();
}
}
return ret;
}
void LibrariesInfo::clear()
{
elfClass = Unknown;
cache.clear();
}
QString LibrariesInfo::getLibraryPath(const QString &name)
{
LibPtr lib = cache.value(name);
if (!lib.isNull())
return lib->path;
return QString();
}
QStringList LibrariesInfo::getLibraryChildren(const QString &name)
{
LibPtr lib = cache.value(name);
if (!lib.isNull())
return lib->children;
return QStringList();
}
QString LibrariesInfo::resolveLibraryPath(const QString &library, const LibSearchInfo &info)
{
if (cache.contains(library))
return cache[library]->path;
// Create search path
QStringList paths;
// RPATH was set, RUNPATH was not, look in RPATH first.
if(info.runPath.isEmpty() && !info.rPath.isEmpty())
paths << info.rPath.split(":");
if (elfClass == ELF64)
paths << envPaths64;
else
paths << envPaths32;
if(!info.runPath.isEmpty())
paths << info.runPath.split(":");
if (elfClass == ELF64)
paths << basePaths64;
else
paths << basePaths32;
paths << "/lib" << "/usr/lib";
foreach(QString path, paths) {
QFileInfo qfi(path + "/" + library);
if (qfi.exists()) {
// Resolve symlink.
cache[library] = Lib::create(qfi.canonicalFilePath());
return qfi.canonicalFilePath();
}
}
return QString();
}
void LibrariesInfo::loadFileRecursive(const QString &path, Lib &parent)
{
QProcess readelf;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert(QLatin1String("LC_ALL"), QLatin1String("en_US"));
readelf.setProcessEnvironment(env);
readelf.start("readelf", QStringList() << "-h" << "-d" << path);
if (!readelf.waitForFinished())
{
return;
}
QTextStream ts(&readelf);
QRegExp soPat("\\[(.*)\\]$");
QRegExp classElf64("\\s*Class\\:\\s+ELF64\\s*");
QRegExp classElfAny("\\s*Class\\:\\s+ELF\\w*\\s*");
LibSearchInfo info;
// List of libraries to search for. We cannot search as soon as its
// encountered because we must wait until we've seen rPath and runPath.
QStringList libs;
while(!ts.atEnd()) {
QString str = ts.readLine();
if (elfClass == Unknown) {
if (classElf64.exactMatch(str))
elfClass = ELF64;
else if (classElfAny.exactMatch(str))
elfClass = ELF32;
}
if(str.contains("(RPATH)") && soPat.indexIn(str) != -1) {
info.rPath = soPat.cap(1);
} else if(str.contains("(RUNPATH)") && soPat.indexIn(str) != -1) {
info.runPath = soPat.cap(1);
} else if(str.contains("(NEEDED)") && soPat.indexIn(str) != -1) {
libs << soPat.cap(1);
}
}
parent.children = libs;
parent.loadedChildren = true;
foreach (const QString &soname, libs) {
QString sopath = resolveLibraryPath(soname, info);
if (!sopath.isEmpty())
{
LibPtr lib = cache[soname];
if (lib && !lib->loadedChildren)
{
loadFileRecursive(sopath, *lib);
}
}
}
}
QStringList LibrariesInfo::getBasePaths() const
{
QStringList basePaths = readLdConfig(QLatin1String("/etc/ld.so.conf"));
basePaths.removeDuplicates();
return basePaths;
}
/// Reads /etc/ld.so.conf or included file
QStringList LibrariesInfo::readLdConfig(const QString &path) const
{
// Open system file defining standard library paths.
QFile ldConf(path);
if(!ldConf.open(QIODevice::ReadOnly))
return QStringList();
QTextStream in(&ldConf);
QStringList paths;
// Loop through each path. Resolve it to the actual file, and if not
// already in the path, add it at the end.
while(!in.atEnd()) {
QString lib = in.readLine();
if (lib.isEmpty())
continue;
if (lib.startsWith(QLatin1String("include "))) {
QStringList parts = lib.split(QLatin1Char(' '), QString::SkipEmptyParts);
if (parts.size() > 1)
paths << readLdConfigsByWildcard(parts[1]);
} else {
QFileInfo libInfo(lib);
if (!libInfo.exists())
continue;
QString realPath = libInfo.canonicalFilePath();
paths << realPath;
}
}
return paths;
}
/// Reads all ld.so.conf-like files matching wildcard
/// Example - /etc/ld.so.conf.d/*.conf
QStringList LibrariesInfo::readLdConfigsByWildcard(const QString &wildcardPath) const
{
int pos = wildcardPath.indexOf(QLatin1Char('*'));
if (pos == -1)
return readLdConfig(wildcardPath);
const QString dirPath = wildcardPath.left(pos);
const QString namesFilter = wildcardPath.right(wildcardPath.size() - pos);
QDir dir(dirPath);
if (!dir.exists())
return QStringList();
dir.setNameFilters(QStringList() << namesFilter);
QStringList files = dir.entryList(QDir::Files);
QStringList paths;
foreach (const QString &configFile, files)
paths << readLdConfig(dirPath + configFile);
return paths;
}
void LibrariesInfo::splitPaths(const QStringList &all, QStringList &elf32, QStringList &elf64)
{
foreach (const QString &path, all)
{
if (path.contains("i386") || path.contains("i686")
|| path.endsWith("lib32") || path.endsWith("libx32")) {
elf32 << path;
} else if (path.contains("x86_64") || path.endsWith("lib64")) {
elf64 << path;
} else {
elf32 << path;
elf64 << path;
}
}
}
QStringList LibrariesInfo::getSystemEnvPaths() const
{
QStringList envPath = QProcess::systemEnvironment();
foreach(QString s, envPath) {
if(s.startsWith("LD_LIBRARY_PATH=")) {
s.remove("LD_LIBRARY_PATH=");
return s.split(":");
}
}
return QStringList();
}
QSharedPointer<LibrariesInfo::Lib> LibrariesInfo::Lib::create(const QString &path)
{
return QSharedPointer<LibrariesInfo::Lib>(new Lib(path));
}