1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qelfparser_p.h"
41
42#if defined (Q_OF_ELF) && defined(Q_CC_GNU)
43
44#include "qlibrary_p.h"
45#include <qdebug.h>
46
47QT_BEGIN_NAMESPACE
48
49// #define QELFPARSER_DEBUG 1
50
51const char *QElfParser::parseSectionHeader(const char *data, ElfSectionHeader *sh)
52{
53 sh->name = read<qelfword_t>(data);
54 data += sizeof(qelfword_t); // sh_name
55 sh->type = read<qelfword_t>(data);
56 data += sizeof(qelfword_t) // sh_type
57 + sizeof(qelfaddr_t) // sh_flags
58 + sizeof(qelfaddr_t); // sh_addr
59 sh->offset = read<qelfoff_t>(data);
60 data += sizeof(qelfoff_t); // sh_offset
61 sh->size = read<qelfoff_t>(data);
62 data += sizeof(qelfoff_t); // sh_size
63 return data;
64}
65
66int QElfParser::parse(const char *dataStart, ulong fdlen, const QString &library, QLibraryPrivate *lib, qsizetype *pos, qsizetype *sectionlen)
67{
68#if defined(QELFPARSER_DEBUG)
69 qDebug() << "QElfParser::parse " << library;
70#endif
71
72 if (fdlen < 64) {
73 if (lib)
74 lib->errorString = QLibrary::tr("'%1' is not an ELF object (%2)").arg(library, QLibrary::tr("file too small"));
75 return NotElf;
76 }
77 const char *data = dataStart;
78 if (qstrncmp(data, "\177ELF", 4) != 0) {
79 if (lib)
80 lib->errorString = QLibrary::tr("'%1' is not an ELF object").arg(library);
81 return NotElf;
82 }
83 // 32 or 64 bit
84 if (data[4] != 1 && data[4] != 2) {
85 if (lib)
86 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library, QLibrary::tr("odd cpu architecture"));
87 return Corrupt;
88 }
89 m_bits = (data[4] << 5);
90
91 /* If you remove this check, to read ELF objects of a different arch, please make sure you modify the typedefs
92 to match the _plugin_ architecture.
93 */
94 if ((sizeof(void*) == 4 && m_bits != 32) || (sizeof(void*) == 8 && m_bits != 64)) {
95 if (lib)
96 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library, QLibrary::tr("wrong cpu architecture"));
97 return Corrupt;
98 }
99 // endian
100 if (data[5] == 0) {
101 if (lib)
102 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library, QLibrary::tr("odd endianness"));
103 return Corrupt;
104 }
105 m_endian = (data[5] == 1 ? ElfLittleEndian : ElfBigEndian);
106
107 data += 16 // e_ident
108 + sizeof(qelfhalf_t) // e_type
109 + sizeof(qelfhalf_t) // e_machine
110 + sizeof(qelfword_t) // e_version
111 + sizeof(qelfaddr_t) // e_entry
112 + sizeof(qelfoff_t); // e_phoff
113
114 qelfoff_t e_shoff = read<qelfoff_t> (data);
115 data += sizeof(qelfoff_t) // e_shoff
116 + sizeof(qelfword_t); // e_flags
117
118 qelfhalf_t e_shsize = read<qelfhalf_t> (data);
119
120 if (e_shsize > fdlen) {
121 if (lib)
122 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library, QLibrary::tr("unexpected e_shsize"));
123 return Corrupt;
124 }
125
126 data += sizeof(qelfhalf_t) // e_ehsize
127 + sizeof(qelfhalf_t) // e_phentsize
128 + sizeof(qelfhalf_t); // e_phnum
129
130 qelfhalf_t e_shentsize = read<qelfhalf_t> (data);
131
132 if (e_shentsize % 4) {
133 if (lib)
134 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library, QLibrary::tr("unexpected e_shentsize"));
135 return Corrupt;
136 }
137 data += sizeof(qelfhalf_t); // e_shentsize
138 qelfhalf_t e_shnum = read<qelfhalf_t> (data);
139 data += sizeof(qelfhalf_t); // e_shnum
140 qelfhalf_t e_shtrndx = read<qelfhalf_t> (data);
141 data += sizeof(qelfhalf_t); // e_shtrndx
142
143 if ((quint32)(e_shnum * e_shentsize) > fdlen) {
144 if (lib) {
145 const QString message =
146 QLibrary::tr("announced %n section(s), each %1 byte(s), exceed file size",
147 nullptr, int(e_shnum)).arg(e_shentsize);
148 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)").arg(library, message);
149 }
150 return Corrupt;
151 }
152
153#if defined(QELFPARSER_DEBUG)
154 qDebug() << e_shnum << "sections starting at " << ("0x" + QByteArray::number(e_shoff, 16)).data() << "each" << e_shentsize << "bytes";
155#endif
156
157 ElfSectionHeader strtab;
158 qulonglong soff = e_shoff + qelfword_t(e_shentsize) * qelfword_t(e_shtrndx);
159
160 if ((soff + e_shentsize) > fdlen || soff % 4 || soff == 0) {
161 if (lib)
162 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)")
163 .arg(library, QLibrary::tr("shstrtab section header seems to be at %1")
164 .arg(QString::number(soff, 16)));
165 return Corrupt;
166 }
167
168 parseSectionHeader(dataStart + soff, &strtab);
169 m_stringTableFileOffset = strtab.offset;
170
171 if ((quint32)(strtab.offset + strtab.size) > fdlen || strtab.offset == 0) {
172 if (lib)
173 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)")
174 .arg(library, QLibrary::tr("string table seems to be at %1")
175 .arg(QString::number(strtab.offset, 16)));
176 return Corrupt;
177 }
178
179#if defined(QELFPARSER_DEBUG)
180 qDebug(".shstrtab at 0x%s", QByteArray::number(m_stringTableFileOffset, 16).data());
181#endif
182
183 const char *s = dataStart + e_shoff;
184 for (int i = 0; i < e_shnum; ++i) {
185 ElfSectionHeader sh;
186 parseSectionHeader(s, &sh);
187 if (sh.name == 0) {
188 s += e_shentsize;
189 continue;
190 }
191 const char *shnam = dataStart + m_stringTableFileOffset + sh.name;
192
193 if (m_stringTableFileOffset + sh.name > fdlen) {
194 if (lib)
195 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)")
196 .arg(library, QLibrary::tr("section name %1 of %2 behind end of file")
197 .arg(i).arg(e_shnum));
198 return Corrupt;
199 }
200
201#if defined(QELFPARSER_DEBUG)
202 qDebug() << "++++" << i << shnam;
203#endif
204
205 if (qstrcmp(shnam, ".qtmetadata") == 0 || qstrcmp(shnam, ".rodata") == 0) {
206 if (!(sh.type & 0x1)) {
207 if (shnam[1] == 'r') {
208 if (lib)
209 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)")
210 .arg(library, QLibrary::tr("empty .rodata. not a library."));
211 return Corrupt;
212 }
213#if defined(QELFPARSER_DEBUG)
214 qDebug()<<"section is not program data. skipped.";
215#endif
216 s += e_shentsize;
217 continue;
218 }
219
220 if (sh.offset == 0 || (sh.offset + sh.size) > fdlen || sh.size < 1) {
221 if (lib)
222 lib->errorString = QLibrary::tr("'%1' is an invalid ELF object (%2)")
223 .arg(library, QLibrary::tr("missing section data. This is not a library."));
224 return Corrupt;
225 }
226 *pos = sh.offset;
227 *sectionlen = sh.size;
228 if (shnam[1] == 'q')
229 return QtMetaDataSection;
230 }
231 s += e_shentsize;
232 }
233 return NoQtSection;
234}
235
236QT_END_NAMESPACE
237
238#endif // defined(Q_OF_ELF) && defined(Q_CC_GNU)
239