Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmELF.cxx
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst or https://cmake.org/licensing for details.  */
3
#include "cmELF.h"
4
5
#include <algorithm>
6
#include <cstddef>
7
#include <cstdint>
8
#include <map>
9
#include <memory>
10
#include <sstream>
11
#include <utility>
12
#include <vector>
13
14
#include <cm/memory>
15
#include <cmext/algorithm>
16
17
#include <cm3p/kwiml/abi.h>
18
19
#include "cmsys/FStream.hxx"
20
21
#include "cmelf/elf32.h"
22
#include "cmelf/elf64.h"
23
#include "cmelf/elf_common.h"
24
25
// Low-level byte swapping implementation.
26
template <size_t s>
27
struct cmELFByteSwapSize
28
{
29
};
30
static void cmELFByteSwap(char* data, cmELFByteSwapSize<2> /*unused*/)
31
3.12k
{
32
3.12k
  char one_byte;
33
3.12k
  one_byte = data[0];
34
3.12k
  data[0] = data[1];
35
3.12k
  data[1] = one_byte;
36
3.12k
}
37
static void cmELFByteSwap(char* data, cmELFByteSwapSize<4> /*unused*/)
38
27.1M
{
39
27.1M
  char one_byte;
40
27.1M
  one_byte = data[0];
41
27.1M
  data[0] = data[3];
42
27.1M
  data[3] = one_byte;
43
27.1M
  one_byte = data[1];
44
27.1M
  data[1] = data[2];
45
27.1M
  data[2] = one_byte;
46
27.1M
}
47
static void cmELFByteSwap(char* data, cmELFByteSwapSize<8> /*unused*/)
48
2.73M
{
49
2.73M
  char one_byte;
50
2.73M
  one_byte = data[0];
51
2.73M
  data[0] = data[7];
52
2.73M
  data[7] = one_byte;
53
2.73M
  one_byte = data[1];
54
2.73M
  data[1] = data[6];
55
2.73M
  data[6] = one_byte;
56
2.73M
  one_byte = data[2];
57
2.73M
  data[2] = data[5];
58
2.73M
  data[5] = one_byte;
59
2.73M
  one_byte = data[3];
60
2.73M
  data[3] = data[4];
61
2.73M
  data[4] = one_byte;
62
2.73M
}
63
64
// Low-level byte swapping interface.
65
template <typename T>
66
void cmELFByteSwap(T& x)
67
29.9M
{
68
29.9M
  cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>());
69
29.9M
}
void cmELFByteSwap<unsigned short>(unsigned short&)
Line
Count
Source
67
3.12k
{
68
3.12k
  cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>());
69
3.12k
}
void cmELFByteSwap<unsigned int>(unsigned int&)
Line
Count
Source
67
27.1M
{
68
27.1M
  cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>());
69
27.1M
}
void cmELFByteSwap<int>(int&)
Line
Count
Source
67
14.5k
{
68
14.5k
  cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>());
69
14.5k
}
void cmELFByteSwap<unsigned long>(unsigned long&)
Line
Count
Source
67
2.73M
{
68
2.73M
  cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>());
69
2.73M
}
void cmELFByteSwap<long>(long&)
Line
Count
Source
67
3
{
68
3
  cmELFByteSwap(reinterpret_cast<char*>(&x), cmELFByteSwapSize<sizeof(T)>());
69
3
}
70
71
class cmELFInternal
72
{
73
public:
74
  using StringEntry = cmELF::StringEntry;
75
  enum ByteOrderType
76
  {
77
    ByteOrderMSB,
78
    ByteOrderLSB
79
  };
80
81
  // Construct and take ownership of the file stream object.
82
  cmELFInternal(cmELF* external, std::unique_ptr<std::istream> fin,
83
                ByteOrderType order)
84
899
    : External(external)
85
899
    , Stream(std::move(fin))
86
899
    , ByteOrder(order)
87
899
  {
88
// In most cases the processor-specific byte order will match that
89
// of the target execution environment.  If we choose wrong here
90
// it is fixed when the header is read.
91
899
#if KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_LITTLE
92
899
    this->NeedSwap = (this->ByteOrder == ByteOrderMSB);
93
#elif KWIML_ABI_ENDIAN_ID == KWIML_ABI_ENDIAN_ID_BIG
94
    this->NeedSwap = (this->ByteOrder == ByteOrderLSB);
95
#else
96
    this->NeedSwap = false; // Final decision is at runtime anyway.
97
#endif
98
99
    // We have not yet loaded the section info.
100
899
    this->DynamicSectionIndex = -1;
101
899
  }
102
103
  // Destruct and delete the file stream object.
104
899
  virtual ~cmELFInternal() = default;
105
106
  // Forward to the per-class implementation.
107
  virtual std::size_t GetNumberOfSections() const = 0;
108
  virtual unsigned long GetDynamicEntryPosition(int j) = 0;
109
  virtual cmELF::DynamicEntryList GetDynamicEntries() = 0;
110
  virtual std::vector<char> EncodeDynamicEntries(
111
    cmELF::DynamicEntryList const&) = 0;
112
  virtual StringEntry const* GetDynamicSectionString(unsigned int tag) = 0;
113
  virtual bool IsMips() const = 0;
114
  virtual void PrintInfo(std::ostream& os) const = 0;
115
116
  /** Returns true if the ELF file has a dynamic section **/
117
8.31k
  bool HasDynamicSection() const { return this->DynamicSectionIndex >= 0; }
118
119
  // Lookup the SONAME in the DYNAMIC section.
120
  StringEntry const* GetSOName()
121
446
  {
122
446
    return this->GetDynamicSectionString(DT_SONAME);
123
446
  }
124
125
  // Lookup the RPATH in the DYNAMIC section.
126
  StringEntry const* GetRPath()
127
351
  {
128
351
    return this->GetDynamicSectionString(DT_RPATH);
129
351
  }
130
131
  // Lookup the RUNPATH in the DYNAMIC section.
132
  StringEntry const* GetRunPath()
133
237
  {
134
237
    return this->GetDynamicSectionString(DT_RUNPATH);
135
237
  }
136
137
  // Return the recorded ELF type.
138
17.2k
  cmELF::FileType GetFileType() const { return this->ELFType; }
139
140
  // Return the recorded machine.
141
611
  std::uint16_t GetMachine() const { return this->Machine; }
142
143
protected:
144
  // Data common to all ELF class implementations.
145
146
  // The external cmELF object.
147
  cmELF* External;
148
149
  // The stream from which to read.
150
  std::unique_ptr<std::istream> Stream;
151
152
  // The byte order of the ELF file.
153
  ByteOrderType ByteOrder;
154
155
  // The ELF file type.
156
  cmELF::FileType ELFType = cmELF::FileTypeInvalid;
157
158
  // The ELF architecture.
159
  std::uint16_t Machine;
160
161
  // Whether we need to byte-swap structures read from the stream.
162
  bool NeedSwap;
163
164
  // The section header index of the DYNAMIC section (-1 if none).
165
  int DynamicSectionIndex;
166
167
  // Helper methods for subclasses.
168
  void SetErrorMessage(char const* msg)
169
608
  {
170
608
    this->External->ErrorMessage = msg;
171
608
    this->ELFType = cmELF::FileTypeInvalid;
172
608
  }
173
174
  // Store string table entry states.
175
  std::map<unsigned int, StringEntry> DynamicSectionStrings;
176
};
177
178
// Configure the implementation template for 32-bit ELF files.
179
struct cmELFTypes32
180
{
181
  using ELF_Ehdr = Elf32_Ehdr;
182
  using ELF_Shdr = Elf32_Shdr;
183
  using ELF_Dyn = Elf32_Dyn;
184
  using ELF_Half = Elf32_Half;
185
  using tagtype = ::uint32_t;
186
165
  static char const* GetName() { return "32-bit"; }
187
};
188
189
// Configure the implementation template for 64-bit ELF files.
190
struct cmELFTypes64
191
{
192
  using ELF_Ehdr = Elf64_Ehdr;
193
  using ELF_Shdr = Elf64_Shdr;
194
  using ELF_Dyn = Elf64_Dyn;
195
  using ELF_Half = Elf64_Half;
196
  using tagtype = ::uint64_t;
197
126
  static char const* GetName() { return "64-bit"; }
198
};
199
200
// Parser implementation template.
201
template <class Types>
202
class cmELFInternalImpl : public cmELFInternal
203
{
204
public:
205
  // Copy the ELF file format types from our configuration parameter.
206
  using ELF_Ehdr = typename Types::ELF_Ehdr;
207
  using ELF_Shdr = typename Types::ELF_Shdr;
208
  using ELF_Dyn = typename Types::ELF_Dyn;
209
  using ELF_Half = typename Types::ELF_Half;
210
  using tagtype = typename Types::tagtype;
211
212
  // Construct with a stream and byte swap indicator.
213
  cmELFInternalImpl(cmELF* external, std::unique_ptr<std::istream> fin,
214
                    ByteOrderType order);
215
216
  // Return the number of sections as specified by the ELF header.
217
  std::size_t GetNumberOfSections() const override
218
1.41k
  {
219
1.41k
    return static_cast<unsigned int>(this->ELFHeader.e_shnum +
220
1.41k
                                     this->SectionHeaders[0].sh_size);
221
1.41k
  }
cmELFInternalImpl<cmELFTypes32>::GetNumberOfSections() const
Line
Count
Source
218
693
  {
219
693
    return static_cast<unsigned int>(this->ELFHeader.e_shnum +
220
693
                                     this->SectionHeaders[0].sh_size);
221
693
  }
cmELFInternalImpl<cmELFTypes64>::GetNumberOfSections() const
Line
Count
Source
218
717
  {
219
717
    return static_cast<unsigned int>(this->ELFHeader.e_shnum +
220
717
                                     this->SectionHeaders[0].sh_size);
221
717
  }
222
223
  // Get the file position of a dynamic section entry.
224
  unsigned long GetDynamicEntryPosition(int j) override;
225
226
  cmELF::DynamicEntryList GetDynamicEntries() override;
227
  std::vector<char> EncodeDynamicEntries(
228
    cmELF::DynamicEntryList const&) override;
229
230
  // Lookup a string from the dynamic section with the given tag.
231
  StringEntry const* GetDynamicSectionString(unsigned int tag) override;
232
233
611
  bool IsMips() const override { return this->ELFHeader.e_machine == EM_MIPS; }
cmELFInternalImpl<cmELFTypes32>::IsMips() const
Line
Count
Source
233
295
  bool IsMips() const override { return this->ELFHeader.e_machine == EM_MIPS; }
cmELFInternalImpl<cmELFTypes64>::IsMips() const
Line
Count
Source
233
316
  bool IsMips() const override { return this->ELFHeader.e_machine == EM_MIPS; }
234
235
  // Print information about the ELF file.
236
  void PrintInfo(std::ostream& os) const override
237
291
  {
238
291
    os << "ELF " << Types::GetName();
239
291
    if (this->ByteOrder == ByteOrderMSB) {
240
122
      os << " MSB";
241
169
    } else if (this->ByteOrder == ByteOrderLSB) {
242
169
      os << " LSB";
243
169
    }
244
291
    switch (this->ELFType) {
245
0
      case cmELF::FileTypeInvalid:
246
0
        os << " invalid file";
247
0
        break;
248
8
      case cmELF::FileTypeRelocatableObject:
249
8
        os << " relocatable object";
250
8
        break;
251
73
      case cmELF::FileTypeExecutable:
252
73
        os << " executable";
253
73
        break;
254
124
      case cmELF::FileTypeSharedLibrary:
255
124
        os << " shared library";
256
124
        break;
257
13
      case cmELF::FileTypeCore:
258
13
        os << " core file";
259
13
        break;
260
35
      case cmELF::FileTypeSpecificOS:
261
35
        os << " os-specific type";
262
35
        break;
263
38
      case cmELF::FileTypeSpecificProc:
264
38
        os << " processor-specific type";
265
38
        break;
266
291
    }
267
291
    os << "\n";
268
291
  }
cmELFInternalImpl<cmELFTypes32>::PrintInfo(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const
Line
Count
Source
237
165
  {
238
165
    os << "ELF " << Types::GetName();
239
165
    if (this->ByteOrder == ByteOrderMSB) {
240
113
      os << " MSB";
241
113
    } else if (this->ByteOrder == ByteOrderLSB) {
242
52
      os << " LSB";
243
52
    }
244
165
    switch (this->ELFType) {
245
0
      case cmELF::FileTypeInvalid:
246
0
        os << " invalid file";
247
0
        break;
248
5
      case cmELF::FileTypeRelocatableObject:
249
5
        os << " relocatable object";
250
5
        break;
251
25
      case cmELF::FileTypeExecutable:
252
25
        os << " executable";
253
25
        break;
254
81
      case cmELF::FileTypeSharedLibrary:
255
81
        os << " shared library";
256
81
        break;
257
9
      case cmELF::FileTypeCore:
258
9
        os << " core file";
259
9
        break;
260
25
      case cmELF::FileTypeSpecificOS:
261
25
        os << " os-specific type";
262
25
        break;
263
20
      case cmELF::FileTypeSpecificProc:
264
20
        os << " processor-specific type";
265
20
        break;
266
165
    }
267
165
    os << "\n";
268
165
  }
cmELFInternalImpl<cmELFTypes64>::PrintInfo(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) const
Line
Count
Source
237
126
  {
238
126
    os << "ELF " << Types::GetName();
239
126
    if (this->ByteOrder == ByteOrderMSB) {
240
9
      os << " MSB";
241
117
    } else if (this->ByteOrder == ByteOrderLSB) {
242
117
      os << " LSB";
243
117
    }
244
126
    switch (this->ELFType) {
245
0
      case cmELF::FileTypeInvalid:
246
0
        os << " invalid file";
247
0
        break;
248
3
      case cmELF::FileTypeRelocatableObject:
249
3
        os << " relocatable object";
250
3
        break;
251
48
      case cmELF::FileTypeExecutable:
252
48
        os << " executable";
253
48
        break;
254
43
      case cmELF::FileTypeSharedLibrary:
255
43
        os << " shared library";
256
43
        break;
257
4
      case cmELF::FileTypeCore:
258
4
        os << " core file";
259
4
        break;
260
10
      case cmELF::FileTypeSpecificOS:
261
10
        os << " os-specific type";
262
10
        break;
263
18
      case cmELF::FileTypeSpecificProc:
264
18
        os << " processor-specific type";
265
18
        break;
266
126
    }
267
126
    os << "\n";
268
126
  }
269
270
private:
271
  static_assert(sizeof(ELF_Dyn().d_un.d_val) == sizeof(ELF_Dyn().d_un.d_ptr),
272
                "ByteSwap(ELF_Dyn) assumes d_val and d_ptr are the same size");
273
274
  void ByteSwap(ELF_Ehdr& elf_header)
275
329
  {
276
329
    cmELFByteSwap(elf_header.e_type);
277
329
    cmELFByteSwap(elf_header.e_machine);
278
329
    cmELFByteSwap(elf_header.e_version);
279
329
    cmELFByteSwap(elf_header.e_entry);
280
329
    cmELFByteSwap(elf_header.e_phoff);
281
329
    cmELFByteSwap(elf_header.e_shoff);
282
329
    cmELFByteSwap(elf_header.e_flags);
283
329
    cmELFByteSwap(elf_header.e_ehsize);
284
329
    cmELFByteSwap(elf_header.e_phentsize);
285
329
    cmELFByteSwap(elf_header.e_phnum);
286
329
    cmELFByteSwap(elf_header.e_shentsize);
287
329
    cmELFByteSwap(elf_header.e_shnum);
288
329
    cmELFByteSwap(elf_header.e_shstrndx);
289
329
  }
cmELFInternalImpl<cmELFTypes32>::ByteSwap(Elf32_Ehdr&)
Line
Count
Source
275
268
  {
276
268
    cmELFByteSwap(elf_header.e_type);
277
268
    cmELFByteSwap(elf_header.e_machine);
278
268
    cmELFByteSwap(elf_header.e_version);
279
268
    cmELFByteSwap(elf_header.e_entry);
280
268
    cmELFByteSwap(elf_header.e_phoff);
281
268
    cmELFByteSwap(elf_header.e_shoff);
282
268
    cmELFByteSwap(elf_header.e_flags);
283
268
    cmELFByteSwap(elf_header.e_ehsize);
284
268
    cmELFByteSwap(elf_header.e_phentsize);
285
268
    cmELFByteSwap(elf_header.e_phnum);
286
268
    cmELFByteSwap(elf_header.e_shentsize);
287
268
    cmELFByteSwap(elf_header.e_shnum);
288
268
    cmELFByteSwap(elf_header.e_shstrndx);
289
268
  }
cmELFInternalImpl<cmELFTypes64>::ByteSwap(Elf64_Ehdr&)
Line
Count
Source
275
61
  {
276
61
    cmELFByteSwap(elf_header.e_type);
277
61
    cmELFByteSwap(elf_header.e_machine);
278
61
    cmELFByteSwap(elf_header.e_version);
279
61
    cmELFByteSwap(elf_header.e_entry);
280
61
    cmELFByteSwap(elf_header.e_phoff);
281
61
    cmELFByteSwap(elf_header.e_shoff);
282
61
    cmELFByteSwap(elf_header.e_flags);
283
61
    cmELFByteSwap(elf_header.e_ehsize);
284
61
    cmELFByteSwap(elf_header.e_phentsize);
285
61
    cmELFByteSwap(elf_header.e_phnum);
286
61
    cmELFByteSwap(elf_header.e_shentsize);
287
61
    cmELFByteSwap(elf_header.e_shnum);
288
61
    cmELFByteSwap(elf_header.e_shstrndx);
289
61
  }
290
291
  void ByteSwap(ELF_Shdr& sec_header)
292
2.98M
  {
293
2.98M
    cmELFByteSwap(sec_header.sh_name);
294
2.98M
    cmELFByteSwap(sec_header.sh_type);
295
2.98M
    cmELFByteSwap(sec_header.sh_flags);
296
2.98M
    cmELFByteSwap(sec_header.sh_addr);
297
2.98M
    cmELFByteSwap(sec_header.sh_offset);
298
2.98M
    cmELFByteSwap(sec_header.sh_size);
299
2.98M
    cmELFByteSwap(sec_header.sh_link);
300
2.98M
    cmELFByteSwap(sec_header.sh_info);
301
2.98M
    cmELFByteSwap(sec_header.sh_addralign);
302
2.98M
    cmELFByteSwap(sec_header.sh_entsize);
303
2.98M
  }
cmELFInternalImpl<cmELFTypes32>::ByteSwap(Elf32_Shdr&)
Line
Count
Source
292
2.53M
  {
293
2.53M
    cmELFByteSwap(sec_header.sh_name);
294
2.53M
    cmELFByteSwap(sec_header.sh_type);
295
2.53M
    cmELFByteSwap(sec_header.sh_flags);
296
2.53M
    cmELFByteSwap(sec_header.sh_addr);
297
2.53M
    cmELFByteSwap(sec_header.sh_offset);
298
2.53M
    cmELFByteSwap(sec_header.sh_size);
299
2.53M
    cmELFByteSwap(sec_header.sh_link);
300
2.53M
    cmELFByteSwap(sec_header.sh_info);
301
2.53M
    cmELFByteSwap(sec_header.sh_addralign);
302
2.53M
    cmELFByteSwap(sec_header.sh_entsize);
303
2.53M
  }
cmELFInternalImpl<cmELFTypes64>::ByteSwap(Elf64_Shdr&)
Line
Count
Source
292
455k
  {
293
455k
    cmELFByteSwap(sec_header.sh_name);
294
455k
    cmELFByteSwap(sec_header.sh_type);
295
455k
    cmELFByteSwap(sec_header.sh_flags);
296
455k
    cmELFByteSwap(sec_header.sh_addr);
297
455k
    cmELFByteSwap(sec_header.sh_offset);
298
455k
    cmELFByteSwap(sec_header.sh_size);
299
455k
    cmELFByteSwap(sec_header.sh_link);
300
455k
    cmELFByteSwap(sec_header.sh_info);
301
455k
    cmELFByteSwap(sec_header.sh_addralign);
302
455k
    cmELFByteSwap(sec_header.sh_entsize);
303
455k
  }
304
305
  void ByteSwap(ELF_Dyn& dyn)
306
14.5k
  {
307
14.5k
    cmELFByteSwap(dyn.d_tag);
308
14.5k
    cmELFByteSwap(dyn.d_un.d_val);
309
14.5k
  }
cmELFInternalImpl<cmELFTypes32>::ByteSwap(Elf32_Dyn&)
Line
Count
Source
306
14.5k
  {
307
14.5k
    cmELFByteSwap(dyn.d_tag);
308
14.5k
    cmELFByteSwap(dyn.d_un.d_val);
309
14.5k
  }
cmELFInternalImpl<cmELFTypes64>::ByteSwap(Elf64_Dyn&)
Line
Count
Source
306
3
  {
307
3
    cmELFByteSwap(dyn.d_tag);
308
3
    cmELFByteSwap(dyn.d_un.d_val);
309
3
  }
310
311
  bool FileTypeValid(ELF_Half et)
312
1.06k
  {
313
1.06k
    unsigned int eti = static_cast<unsigned int>(et);
314
1.06k
    if (eti == ET_NONE || eti == ET_REL || eti == ET_EXEC || eti == ET_DYN ||
315
642
        eti == ET_CORE) {
316
642
      return true;
317
642
    }
318
421
    if (eti >= ET_LOOS && eti <= ET_HIOS) {
319
69
      return true;
320
69
    }
321
352
    if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
322
131
      return true;
323
131
    }
324
221
    return false;
325
352
  }
cmELFInternalImpl<cmELFTypes32>::FileTypeValid(unsigned short)
Line
Count
Source
312
542
  {
313
542
    unsigned int eti = static_cast<unsigned int>(et);
314
542
    if (eti == ET_NONE || eti == ET_REL || eti == ET_EXEC || eti == ET_DYN ||
315
299
        eti == ET_CORE) {
316
299
      return true;
317
299
    }
318
243
    if (eti >= ET_LOOS && eti <= ET_HIOS) {
319
51
      return true;
320
51
    }
321
192
    if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
322
75
      return true;
323
75
    }
324
117
    return false;
325
192
  }
cmELFInternalImpl<cmELFTypes64>::FileTypeValid(unsigned short)
Line
Count
Source
312
521
  {
313
521
    unsigned int eti = static_cast<unsigned int>(et);
314
521
    if (eti == ET_NONE || eti == ET_REL || eti == ET_EXEC || eti == ET_DYN ||
315
343
        eti == ET_CORE) {
316
343
      return true;
317
343
    }
318
178
    if (eti >= ET_LOOS && eti <= ET_HIOS) {
319
18
      return true;
320
18
    }
321
160
    if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
322
56
      return true;
323
56
    }
324
104
    return false;
325
160
  }
326
327
  bool Read(ELF_Ehdr& x)
328
899
  {
329
    // Read the header from the file.
330
899
    if (!this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x))) {
331
5
      return false;
332
5
    }
333
334
    // The byte order of ELF header fields may not match that of the
335
    // processor-specific data.  The header fields are ordered to
336
    // match the target execution environment, so we may need to
337
    // memorize the order of all platforms based on the e_machine
338
    // value.  As a heuristic, if the type is invalid but its
339
    // swapped value is okay then flip our swap mode.
340
894
    ELF_Half et = x.e_type;
341
894
    if (this->NeedSwap) {
342
322
      cmELFByteSwap(et);
343
322
    }
344
894
    if (!this->FileTypeValid(et)) {
345
169
      cmELFByteSwap(et);
346
169
      if (this->FileTypeValid(et)) {
347
        // The previous byte order guess was wrong.  Flip it.
348
117
        this->NeedSwap = !this->NeedSwap;
349
117
      }
350
169
    }
351
352
    // Fix the byte order of the header.
353
894
    if (this->NeedSwap) {
354
329
      this->ByteSwap(x);
355
329
    }
356
894
    return true;
357
899
  }
cmELFInternalImpl<cmELFTypes32>::Read(Elf32_Ehdr&)
Line
Count
Source
328
456
  {
329
    // Read the header from the file.
330
456
    if (!this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x))) {
331
3
      return false;
332
3
    }
333
334
    // The byte order of ELF header fields may not match that of the
335
    // processor-specific data.  The header fields are ordered to
336
    // match the target execution environment, so we may need to
337
    // memorize the order of all platforms based on the e_machine
338
    // value.  As a heuristic, if the type is invalid but its
339
    // swapped value is okay then flip our swap mode.
340
453
    ELF_Half et = x.e_type;
341
453
    if (this->NeedSwap) {
342
295
      cmELFByteSwap(et);
343
295
    }
344
453
    if (!this->FileTypeValid(et)) {
345
89
      cmELFByteSwap(et);
346
89
      if (this->FileTypeValid(et)) {
347
        // The previous byte order guess was wrong.  Flip it.
348
61
        this->NeedSwap = !this->NeedSwap;
349
61
      }
350
89
    }
351
352
    // Fix the byte order of the header.
353
453
    if (this->NeedSwap) {
354
268
      this->ByteSwap(x);
355
268
    }
356
453
    return true;
357
456
  }
cmELFInternalImpl<cmELFTypes64>::Read(Elf64_Ehdr&)
Line
Count
Source
328
443
  {
329
    // Read the header from the file.
330
443
    if (!this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x))) {
331
2
      return false;
332
2
    }
333
334
    // The byte order of ELF header fields may not match that of the
335
    // processor-specific data.  The header fields are ordered to
336
    // match the target execution environment, so we may need to
337
    // memorize the order of all platforms based on the e_machine
338
    // value.  As a heuristic, if the type is invalid but its
339
    // swapped value is okay then flip our swap mode.
340
441
    ELF_Half et = x.e_type;
341
441
    if (this->NeedSwap) {
342
27
      cmELFByteSwap(et);
343
27
    }
344
441
    if (!this->FileTypeValid(et)) {
345
80
      cmELFByteSwap(et);
346
80
      if (this->FileTypeValid(et)) {
347
        // The previous byte order guess was wrong.  Flip it.
348
56
        this->NeedSwap = !this->NeedSwap;
349
56
      }
350
80
    }
351
352
    // Fix the byte order of the header.
353
441
    if (this->NeedSwap) {
354
61
      this->ByteSwap(x);
355
61
    }
356
441
    return true;
357
443
  }
358
  bool Read(ELF_Shdr& x)
359
12.1M
  {
360
12.1M
    if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) &&
361
12.1M
        this->NeedSwap) {
362
2.98M
      this->ByteSwap(x);
363
2.98M
    }
364
12.1M
    return !this->Stream->fail();
365
12.1M
  }
cmELFInternalImpl<cmELFTypes32>::Read(Elf32_Shdr&)
Line
Count
Source
359
10.3M
  {
360
10.3M
    if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) &&
361
10.3M
        this->NeedSwap) {
362
2.53M
      this->ByteSwap(x);
363
2.53M
    }
364
10.3M
    return !this->Stream->fail();
365
10.3M
  }
cmELFInternalImpl<cmELFTypes64>::Read(Elf64_Shdr&)
Line
Count
Source
359
1.81M
  {
360
1.81M
    if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) &&
361
1.81M
        this->NeedSwap) {
362
455k
      this->ByteSwap(x);
363
455k
    }
364
1.81M
    return !this->Stream->fail();
365
1.81M
  }
366
  bool Read(ELF_Dyn& x)
367
83.2k
  {
368
83.2k
    if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) &&
369
83.2k
        this->NeedSwap) {
370
9.33k
      this->ByteSwap(x);
371
9.33k
    }
372
83.2k
    return !this->Stream->fail();
373
83.2k
  }
cmELFInternalImpl<cmELFTypes32>::Read(Elf32_Dyn&)
Line
Count
Source
367
77.2k
  {
368
77.2k
    if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) &&
369
77.2k
        this->NeedSwap) {
370
9.33k
      this->ByteSwap(x);
371
9.33k
    }
372
77.2k
    return !this->Stream->fail();
373
77.2k
  }
cmELFInternalImpl<cmELFTypes64>::Read(Elf64_Dyn&)
Line
Count
Source
367
6.01k
  {
368
6.01k
    if (this->Stream->read(reinterpret_cast<char*>(&x), sizeof(x)) &&
369
5.97k
        this->NeedSwap) {
370
2
      this->ByteSwap(x);
371
2
    }
372
6.01k
    return !this->Stream->fail();
373
6.01k
  }
374
375
  bool LoadSectionHeader(std::size_t i)
376
12.1M
  {
377
    // Read the section header from the file.
378
12.1M
    this->Stream->seekg(this->ELFHeader.e_shoff +
379
12.1M
                        this->ELFHeader.e_shentsize * i);
380
12.1M
    if (!this->Read(this->SectionHeaders[i])) {
381
229
      this->SetErrorMessage("Failed to load section headers.");
382
229
      return false;
383
229
    }
384
385
    // Identify some important sections.
386
12.1M
    if (this->SectionHeaders[i].sh_type == SHT_DYNAMIC) {
387
5.61M
      this->DynamicSectionIndex = static_cast<int>(i);
388
5.61M
    }
389
12.1M
    return true;
390
12.1M
  }
cmELFInternalImpl<cmELFTypes32>::LoadSectionHeader(unsigned long)
Line
Count
Source
376
10.3M
  {
377
    // Read the section header from the file.
378
10.3M
    this->Stream->seekg(this->ELFHeader.e_shoff +
379
10.3M
                        this->ELFHeader.e_shentsize * i);
380
10.3M
    if (!this->Read(this->SectionHeaders[i])) {
381
129
      this->SetErrorMessage("Failed to load section headers.");
382
129
      return false;
383
129
    }
384
385
    // Identify some important sections.
386
10.3M
    if (this->SectionHeaders[i].sh_type == SHT_DYNAMIC) {
387
5.61M
      this->DynamicSectionIndex = static_cast<int>(i);
388
5.61M
    }
389
10.3M
    return true;
390
10.3M
  }
cmELFInternalImpl<cmELFTypes64>::LoadSectionHeader(unsigned long)
Line
Count
Source
376
1.81M
  {
377
    // Read the section header from the file.
378
1.81M
    this->Stream->seekg(this->ELFHeader.e_shoff +
379
1.81M
                        this->ELFHeader.e_shentsize * i);
380
1.81M
    if (!this->Read(this->SectionHeaders[i])) {
381
100
      this->SetErrorMessage("Failed to load section headers.");
382
100
      return false;
383
100
    }
384
385
    // Identify some important sections.
386
1.81M
    if (this->SectionHeaders[i].sh_type == SHT_DYNAMIC) {
387
587
      this->DynamicSectionIndex = static_cast<int>(i);
388
587
    }
389
1.81M
    return true;
390
1.81M
  }
391
392
  bool LoadDynamicSection();
393
394
  // Store the main ELF header.
395
  ELF_Ehdr ELFHeader;
396
397
  // Store all the section headers.
398
  std::vector<ELF_Shdr> SectionHeaders;
399
400
  // Store all entries of the DYNAMIC section.
401
  std::vector<ELF_Dyn> DynamicSectionEntries;
402
};
403
404
template <class Types>
405
cmELFInternalImpl<Types>::cmELFInternalImpl(cmELF* external,
406
                                            std::unique_ptr<std::istream> fin,
407
                                            ByteOrderType order)
408
899
  : cmELFInternal(external, std::move(fin), order)
409
899
{
410
  // Read the main header.
411
899
  if (!this->Read(this->ELFHeader)) {
412
5
    this->SetErrorMessage("Failed to read main ELF header.");
413
5
    return;
414
5
  }
415
416
  // Determine the ELF file type.
417
894
  switch (this->ELFHeader.e_type) {
418
2
    case ET_NONE:
419
2
      this->SetErrorMessage("ELF file type is NONE.");
420
2
      return;
421
24
    case ET_REL:
422
24
      this->ELFType = cmELF::FileTypeRelocatableObject;
423
24
      break;
424
245
    case ET_EXEC:
425
245
      this->ELFType = cmELF::FileTypeExecutable;
426
245
      break;
427
321
    case ET_DYN:
428
321
      this->ELFType = cmELF::FileTypeSharedLibrary;
429
321
      break;
430
50
    case ET_CORE:
431
50
      this->ELFType = cmELF::FileTypeCore;
432
50
      break;
433
252
    default: {
434
252
      unsigned int eti = static_cast<unsigned int>(this->ELFHeader.e_type);
435
252
      if (eti >= ET_LOOS && eti <= ET_HIOS) {
436
69
        this->ELFType = cmELF::FileTypeSpecificOS;
437
69
        break;
438
69
      }
439
183
      if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
440
131
        this->ELFType = cmELF::FileTypeSpecificProc;
441
131
        break;
442
131
      }
443
52
      std::ostringstream e;
444
52
      e << "Unknown ELF file type " << eti;
445
52
      this->SetErrorMessage(e.str().c_str());
446
52
      return;
447
183
    }
448
894
  }
449
450
840
  this->Machine = this->ELFHeader.e_machine;
451
452
  // Load the section headers.
453
840
  std::size_t const minSections = 1;
454
840
  std::size_t numSections = this->ELFHeader.e_shnum;
455
840
  this->SectionHeaders.resize(std::max(numSections, minSections));
456
840
  if (!this->LoadSectionHeader(0)) {
457
41
    return;
458
41
  }
459
799
  numSections = this->GetNumberOfSections();
460
799
  this->SectionHeaders.resize(std::max(numSections, minSections));
461
12.1M
  for (std::size_t i = 1; i < numSections; ++i) {
462
12.1M
    if (!this->LoadSectionHeader(i)) {
463
188
      return;
464
188
    }
465
12.1M
  }
466
799
}
cmELFInternalImpl<cmELFTypes32>::cmELFInternalImpl(cmELF*, std::__1::unique_ptr<std::__1::basic_istream<char, std::__1::char_traits<char> >, std::__1::default_delete<std::__1::basic_istream<char, std::__1::char_traits<char> > > >, cmELFInternal::ByteOrderType)
Line
Count
Source
408
456
  : cmELFInternal(external, std::move(fin), order)
409
456
{
410
  // Read the main header.
411
456
  if (!this->Read(this->ELFHeader)) {
412
3
    this->SetErrorMessage("Failed to read main ELF header.");
413
3
    return;
414
3
  }
415
416
  // Determine the ELF file type.
417
453
  switch (this->ELFHeader.e_type) {
418
1
    case ET_NONE:
419
1
      this->SetErrorMessage("ELF file type is NONE.");
420
1
      return;
421
18
    case ET_REL:
422
18
      this->ELFType = cmELF::FileTypeRelocatableObject;
423
18
      break;
424
54
    case ET_EXEC:
425
54
      this->ELFType = cmELF::FileTypeExecutable;
426
54
      break;
427
185
    case ET_DYN:
428
185
      this->ELFType = cmELF::FileTypeSharedLibrary;
429
185
      break;
430
41
    case ET_CORE:
431
41
      this->ELFType = cmELF::FileTypeCore;
432
41
      break;
433
154
    default: {
434
154
      unsigned int eti = static_cast<unsigned int>(this->ELFHeader.e_type);
435
154
      if (eti >= ET_LOOS && eti <= ET_HIOS) {
436
51
        this->ELFType = cmELF::FileTypeSpecificOS;
437
51
        break;
438
51
      }
439
103
      if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
440
75
        this->ELFType = cmELF::FileTypeSpecificProc;
441
75
        break;
442
75
      }
443
28
      std::ostringstream e;
444
28
      e << "Unknown ELF file type " << eti;
445
28
      this->SetErrorMessage(e.str().c_str());
446
28
      return;
447
103
    }
448
453
  }
449
450
424
  this->Machine = this->ELFHeader.e_machine;
451
452
  // Load the section headers.
453
424
  std::size_t const minSections = 1;
454
424
  std::size_t numSections = this->ELFHeader.e_shnum;
455
424
  this->SectionHeaders.resize(std::max(numSections, minSections));
456
424
  if (!this->LoadSectionHeader(0)) {
457
26
    return;
458
26
  }
459
398
  numSections = this->GetNumberOfSections();
460
398
  this->SectionHeaders.resize(std::max(numSections, minSections));
461
10.3M
  for (std::size_t i = 1; i < numSections; ++i) {
462
10.3M
    if (!this->LoadSectionHeader(i)) {
463
103
      return;
464
103
    }
465
10.3M
  }
466
398
}
cmELFInternalImpl<cmELFTypes64>::cmELFInternalImpl(cmELF*, std::__1::unique_ptr<std::__1::basic_istream<char, std::__1::char_traits<char> >, std::__1::default_delete<std::__1::basic_istream<char, std::__1::char_traits<char> > > >, cmELFInternal::ByteOrderType)
Line
Count
Source
408
443
  : cmELFInternal(external, std::move(fin), order)
409
443
{
410
  // Read the main header.
411
443
  if (!this->Read(this->ELFHeader)) {
412
2
    this->SetErrorMessage("Failed to read main ELF header.");
413
2
    return;
414
2
  }
415
416
  // Determine the ELF file type.
417
441
  switch (this->ELFHeader.e_type) {
418
1
    case ET_NONE:
419
1
      this->SetErrorMessage("ELF file type is NONE.");
420
1
      return;
421
6
    case ET_REL:
422
6
      this->ELFType = cmELF::FileTypeRelocatableObject;
423
6
      break;
424
191
    case ET_EXEC:
425
191
      this->ELFType = cmELF::FileTypeExecutable;
426
191
      break;
427
136
    case ET_DYN:
428
136
      this->ELFType = cmELF::FileTypeSharedLibrary;
429
136
      break;
430
9
    case ET_CORE:
431
9
      this->ELFType = cmELF::FileTypeCore;
432
9
      break;
433
98
    default: {
434
98
      unsigned int eti = static_cast<unsigned int>(this->ELFHeader.e_type);
435
98
      if (eti >= ET_LOOS && eti <= ET_HIOS) {
436
18
        this->ELFType = cmELF::FileTypeSpecificOS;
437
18
        break;
438
18
      }
439
80
      if (eti >= ET_LOPROC && eti <= ET_HIPROC) {
440
56
        this->ELFType = cmELF::FileTypeSpecificProc;
441
56
        break;
442
56
      }
443
24
      std::ostringstream e;
444
24
      e << "Unknown ELF file type " << eti;
445
24
      this->SetErrorMessage(e.str().c_str());
446
24
      return;
447
80
    }
448
441
  }
449
450
416
  this->Machine = this->ELFHeader.e_machine;
451
452
  // Load the section headers.
453
416
  std::size_t const minSections = 1;
454
416
  std::size_t numSections = this->ELFHeader.e_shnum;
455
416
  this->SectionHeaders.resize(std::max(numSections, minSections));
456
416
  if (!this->LoadSectionHeader(0)) {
457
15
    return;
458
15
  }
459
401
  numSections = this->GetNumberOfSections();
460
401
  this->SectionHeaders.resize(std::max(numSections, minSections));
461
1.81M
  for (std::size_t i = 1; i < numSections; ++i) {
462
1.81M
    if (!this->LoadSectionHeader(i)) {
463
85
      return;
464
85
    }
465
1.81M
  }
466
401
}
467
468
template <class Types>
469
bool cmELFInternalImpl<Types>::LoadDynamicSection()
470
7.70k
{
471
  // If there is no dynamic section we are done.
472
7.70k
  if (!this->HasDynamicSection()) {
473
182
    return false;
474
182
  }
475
476
  // If the section was already loaded we are done.
477
7.52k
  if (!this->DynamicSectionEntries.empty()) {
478
6.84k
    return true;
479
6.84k
  }
480
481
  // If there are no entries we are done.
482
686
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
483
686
  if (sec.sh_entsize == 0) {
484
19
    return false;
485
19
  }
486
487
  // Allocate the dynamic section entries.
488
667
  int n = static_cast<int>(sec.sh_size / sec.sh_entsize);
489
667
  this->DynamicSectionEntries.resize(n);
490
491
  // Read each entry.
492
83.8k
  for (int j = 0; j < n; ++j) {
493
    // Seek to the beginning of the section entry.
494
83.2k
    this->Stream->seekg(sec.sh_offset + sec.sh_entsize * j);
495
83.2k
    ELF_Dyn& dyn = this->DynamicSectionEntries[j];
496
497
    // Try reading the entry.
498
83.2k
    if (!this->Read(dyn)) {
499
84
      this->SetErrorMessage("Error reading entry from DYNAMIC section.");
500
84
      this->DynamicSectionIndex = -1;
501
84
      return false;
502
84
    }
503
83.2k
  }
504
583
  return true;
505
667
}
cmELFInternalImpl<cmELFTypes32>::LoadDynamicSection()
Line
Count
Source
470
5.16k
{
471
  // If there is no dynamic section we are done.
472
5.16k
  if (!this->HasDynamicSection()) {
473
89
    return false;
474
89
  }
475
476
  // If the section was already loaded we are done.
477
5.08k
  if (!this->DynamicSectionEntries.empty()) {
478
4.73k
    return true;
479
4.73k
  }
480
481
  // If there are no entries we are done.
482
345
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
483
345
  if (sec.sh_entsize == 0) {
484
11
    return false;
485
11
  }
486
487
  // Allocate the dynamic section entries.
488
334
  int n = static_cast<int>(sec.sh_size / sec.sh_entsize);
489
334
  this->DynamicSectionEntries.resize(n);
490
491
  // Read each entry.
492
77.5k
  for (int j = 0; j < n; ++j) {
493
    // Seek to the beginning of the section entry.
494
77.2k
    this->Stream->seekg(sec.sh_offset + sec.sh_entsize * j);
495
77.2k
    ELF_Dyn& dyn = this->DynamicSectionEntries[j];
496
497
    // Try reading the entry.
498
77.2k
    if (!this->Read(dyn)) {
499
43
      this->SetErrorMessage("Error reading entry from DYNAMIC section.");
500
43
      this->DynamicSectionIndex = -1;
501
43
      return false;
502
43
    }
503
77.2k
  }
504
291
  return true;
505
334
}
cmELFInternalImpl<cmELFTypes64>::LoadDynamicSection()
Line
Count
Source
470
2.53k
{
471
  // If there is no dynamic section we are done.
472
2.53k
  if (!this->HasDynamicSection()) {
473
93
    return false;
474
93
  }
475
476
  // If the section was already loaded we are done.
477
2.44k
  if (!this->DynamicSectionEntries.empty()) {
478
2.10k
    return true;
479
2.10k
  }
480
481
  // If there are no entries we are done.
482
341
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
483
341
  if (sec.sh_entsize == 0) {
484
8
    return false;
485
8
  }
486
487
  // Allocate the dynamic section entries.
488
333
  int n = static_cast<int>(sec.sh_size / sec.sh_entsize);
489
333
  this->DynamicSectionEntries.resize(n);
490
491
  // Read each entry.
492
6.30k
  for (int j = 0; j < n; ++j) {
493
    // Seek to the beginning of the section entry.
494
6.01k
    this->Stream->seekg(sec.sh_offset + sec.sh_entsize * j);
495
6.01k
    ELF_Dyn& dyn = this->DynamicSectionEntries[j];
496
497
    // Try reading the entry.
498
6.01k
    if (!this->Read(dyn)) {
499
41
      this->SetErrorMessage("Error reading entry from DYNAMIC section.");
500
41
      this->DynamicSectionIndex = -1;
501
41
      return false;
502
41
    }
503
6.01k
  }
504
292
  return true;
505
333
}
506
507
template <class Types>
508
unsigned long cmELFInternalImpl<Types>::GetDynamicEntryPosition(int j)
509
6.51k
{
510
6.51k
  if (!this->LoadDynamicSection()) {
511
0
    return 0;
512
0
  }
513
6.51k
  if (j < 0 || j >= static_cast<int>(this->DynamicSectionEntries.size())) {
514
0
    return 0;
515
0
  }
516
6.51k
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
517
6.51k
  return sec.sh_offset + sec.sh_entsize * static_cast<unsigned long>(j);
518
6.51k
}
cmELFInternalImpl<cmELFTypes32>::GetDynamicEntryPosition(int)
Line
Count
Source
509
4.57k
{
510
4.57k
  if (!this->LoadDynamicSection()) {
511
0
    return 0;
512
0
  }
513
4.57k
  if (j < 0 || j >= static_cast<int>(this->DynamicSectionEntries.size())) {
514
0
    return 0;
515
0
  }
516
4.57k
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
517
4.57k
  return sec.sh_offset + sec.sh_entsize * static_cast<unsigned long>(j);
518
4.57k
}
cmELFInternalImpl<cmELFTypes64>::GetDynamicEntryPosition(int)
Line
Count
Source
509
1.94k
{
510
1.94k
  if (!this->LoadDynamicSection()) {
511
0
    return 0;
512
0
  }
513
1.94k
  if (j < 0 || j >= static_cast<int>(this->DynamicSectionEntries.size())) {
514
0
    return 0;
515
0
  }
516
1.94k
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
517
1.94k
  return sec.sh_offset + sec.sh_entsize * static_cast<unsigned long>(j);
518
1.94k
}
519
520
template <class Types>
521
cmELF::DynamicEntryList cmELFInternalImpl<Types>::GetDynamicEntries()
522
309
{
523
309
  cmELF::DynamicEntryList result;
524
525
  // Ensure entries have been read from file
526
309
  if (!this->LoadDynamicSection()) {
527
128
    return result;
528
128
  }
529
530
  // Copy into public array
531
181
  result.reserve(this->DynamicSectionEntries.size());
532
74.3k
  for (ELF_Dyn& dyn : this->DynamicSectionEntries) {
533
74.3k
    result.emplace_back(dyn.d_tag, dyn.d_un.d_val);
534
74.3k
  }
535
536
181
  return result;
537
309
}
cmELFInternalImpl<cmELFTypes32>::GetDynamicEntries()
Line
Count
Source
522
183
{
523
183
  cmELF::DynamicEntryList result;
524
525
  // Ensure entries have been read from file
526
183
  if (!this->LoadDynamicSection()) {
527
73
    return result;
528
73
  }
529
530
  // Copy into public array
531
110
  result.reserve(this->DynamicSectionEntries.size());
532
72.3k
  for (ELF_Dyn& dyn : this->DynamicSectionEntries) {
533
72.3k
    result.emplace_back(dyn.d_tag, dyn.d_un.d_val);
534
72.3k
  }
535
536
110
  return result;
537
183
}
cmELFInternalImpl<cmELFTypes64>::GetDynamicEntries()
Line
Count
Source
522
126
{
523
126
  cmELF::DynamicEntryList result;
524
525
  // Ensure entries have been read from file
526
126
  if (!this->LoadDynamicSection()) {
527
55
    return result;
528
55
  }
529
530
  // Copy into public array
531
71
  result.reserve(this->DynamicSectionEntries.size());
532
1.97k
  for (ELF_Dyn& dyn : this->DynamicSectionEntries) {
533
1.97k
    result.emplace_back(dyn.d_tag, dyn.d_un.d_val);
534
1.97k
  }
535
536
71
  return result;
537
126
}
538
539
template <class Types>
540
std::vector<char> cmELFInternalImpl<Types>::EncodeDynamicEntries(
541
  cmELF::DynamicEntryList const& entries)
542
100
{
543
100
  std::vector<char> result;
544
100
  result.reserve(sizeof(ELF_Dyn) * entries.size());
545
546
74.3k
  for (auto const& entry : entries) {
547
    // Store the entry in an ELF_Dyn, byteswap it, then serialize to chars
548
74.3k
    ELF_Dyn dyn;
549
74.3k
    dyn.d_tag = static_cast<tagtype>(entry.first);
550
74.3k
    dyn.d_un.d_val = static_cast<tagtype>(entry.second);
551
552
74.3k
    if (this->NeedSwap) {
553
5.17k
      this->ByteSwap(dyn);
554
5.17k
    }
555
556
74.3k
    char* pdyn = reinterpret_cast<char*>(&dyn);
557
74.3k
    cm::append(result, pdyn, pdyn + sizeof(ELF_Dyn));
558
74.3k
  }
559
560
100
  return result;
561
100
}
cmELFInternalImpl<cmELFTypes32>::EncodeDynamicEntries(std::__1::vector<std::__1::pair<long, unsigned long>, std::__1::allocator<std::__1::pair<long, unsigned long> > > const&)
Line
Count
Source
542
60
{
543
60
  std::vector<char> result;
544
60
  result.reserve(sizeof(ELF_Dyn) * entries.size());
545
546
72.3k
  for (auto const& entry : entries) {
547
    // Store the entry in an ELF_Dyn, byteswap it, then serialize to chars
548
72.3k
    ELF_Dyn dyn;
549
72.3k
    dyn.d_tag = static_cast<tagtype>(entry.first);
550
72.3k
    dyn.d_un.d_val = static_cast<tagtype>(entry.second);
551
552
72.3k
    if (this->NeedSwap) {
553
5.17k
      this->ByteSwap(dyn);
554
5.17k
    }
555
556
72.3k
    char* pdyn = reinterpret_cast<char*>(&dyn);
557
72.3k
    cm::append(result, pdyn, pdyn + sizeof(ELF_Dyn));
558
72.3k
  }
559
560
60
  return result;
561
60
}
cmELFInternalImpl<cmELFTypes64>::EncodeDynamicEntries(std::__1::vector<std::__1::pair<long, unsigned long>, std::__1::allocator<std::__1::pair<long, unsigned long> > > const&)
Line
Count
Source
542
40
{
543
40
  std::vector<char> result;
544
40
  result.reserve(sizeof(ELF_Dyn) * entries.size());
545
546
1.97k
  for (auto const& entry : entries) {
547
    // Store the entry in an ELF_Dyn, byteswap it, then serialize to chars
548
1.97k
    ELF_Dyn dyn;
549
1.97k
    dyn.d_tag = static_cast<tagtype>(entry.first);
550
1.97k
    dyn.d_un.d_val = static_cast<tagtype>(entry.second);
551
552
1.97k
    if (this->NeedSwap) {
553
1
      this->ByteSwap(dyn);
554
1
    }
555
556
1.97k
    char* pdyn = reinterpret_cast<char*>(&dyn);
557
1.97k
    cm::append(result, pdyn, pdyn + sizeof(ELF_Dyn));
558
1.97k
  }
559
560
40
  return result;
561
40
}
562
563
template <class Types>
564
cmELF::StringEntry const* cmELFInternalImpl<Types>::GetDynamicSectionString(
565
  unsigned int tag)
566
1.03k
{
567
  // Short-circuit if already checked.
568
1.03k
  auto dssi = this->DynamicSectionStrings.find(tag);
569
1.03k
  if (dssi != this->DynamicSectionStrings.end()) {
570
149
    if (dssi->second.Position > 0) {
571
36
      return &dssi->second;
572
36
    }
573
113
    return nullptr;
574
149
  }
575
576
  // Create an entry for this tag.  Assume it is missing until found.
577
885
  StringEntry& se = this->DynamicSectionStrings[tag];
578
885
  se.Position = 0;
579
885
  se.Size = 0;
580
885
  se.IndexInSection = -1;
581
582
  // Try reading the dynamic section.
583
885
  if (!this->LoadDynamicSection()) {
584
157
    return nullptr;
585
157
  }
586
587
  // Get the string table referenced by the DYNAMIC section.
588
728
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
589
728
  if (sec.sh_link >= this->SectionHeaders.size()) {
590
154
    this->SetErrorMessage("Section DYNAMIC has invalid string table index.");
591
154
    return nullptr;
592
154
  }
593
574
  ELF_Shdr const& strtab = this->SectionHeaders[sec.sh_link];
594
595
  // Look for the requested entry.
596
574
  for (auto di = this->DynamicSectionEntries.begin();
597
224k
       di != this->DynamicSectionEntries.end(); ++di) {
598
223k
    ELF_Dyn& dyn = *di;
599
223k
    if (static_cast<tagtype>(dyn.d_tag) == static_cast<tagtype>(tag)) {
600
      // We found the tag requested.
601
      // Make sure the position given is within the string section.
602
143
      if (dyn.d_un.d_val >= strtab.sh_size) {
603
64
        this->SetErrorMessage("Section DYNAMIC references string beyond "
604
64
                              "the end of its string section.");
605
64
        return nullptr;
606
64
      }
607
608
      // Seek to the position reported by the entry.
609
79
      unsigned long first = static_cast<unsigned long>(dyn.d_un.d_val);
610
79
      unsigned long last = first;
611
79
      unsigned long end = static_cast<unsigned long>(strtab.sh_size);
612
79
      this->Stream->seekg(strtab.sh_offset + first);
613
614
      // Read the string.  It may be followed by more than one NULL
615
      // terminator.  Count the total size of the region allocated to
616
      // the string.  This assumes that the next string in the table
617
      // is non-empty, but the "chrpath" tool makes the same
618
      // assumption.
619
79
      bool terminated = false;
620
79
      char c;
621
740
      while (last != end && this->Stream->get(c) && !(terminated && c)) {
622
661
        ++last;
623
661
        if (c) {
624
288
          se.Value += c;
625
373
        } else {
626
373
          terminated = true;
627
373
        }
628
661
      }
629
630
      // Make sure the whole value was read.
631
79
      if (!(*this->Stream)) {
632
18
        if (tag == cmELF::TagRPath) {
633
5
          this->SetErrorMessage(
634
5
            "Dynamic section specifies unreadable DT_RPATH");
635
13
        } else if (tag == cmELF::TagRunPath) {
636
8
          this->SetErrorMessage(
637
8
            "Dynamic section specifies unreadable DT_RUNPATH");
638
8
        } else if (tag == cmELF::TagMipsRldMapRel) {
639
0
          this->SetErrorMessage(
640
0
            "Dynamic section specifies unreadable DT_MIPS_RLD_MAP_REL");
641
5
        } else {
642
5
          this->SetErrorMessage("Dynamic section specifies unreadable value"
643
5
                                " for unexpected attribute");
644
5
        }
645
18
        se.Value = "";
646
18
        return nullptr;
647
18
      }
648
649
      // The value has been read successfully.  Report it.
650
61
      se.Position = static_cast<unsigned long>(strtab.sh_offset + first);
651
61
      se.Size = last - first;
652
61
      se.IndexInSection =
653
61
        static_cast<int>(di - this->DynamicSectionEntries.begin());
654
61
      return &se;
655
79
    }
656
223k
  }
657
431
  return nullptr;
658
574
}
cmELFInternalImpl<cmELFTypes32>::GetDynamicSectionString(unsigned int)
Line
Count
Source
566
503
{
567
  // Short-circuit if already checked.
568
503
  auto dssi = this->DynamicSectionStrings.find(tag);
569
503
  if (dssi != this->DynamicSectionStrings.end()) {
570
88
    if (dssi->second.Position > 0) {
571
36
      return &dssi->second;
572
36
    }
573
52
    return nullptr;
574
88
  }
575
576
  // Create an entry for this tag.  Assume it is missing until found.
577
415
  StringEntry& se = this->DynamicSectionStrings[tag];
578
415
  se.Position = 0;
579
415
  se.Size = 0;
580
415
  se.IndexInSection = -1;
581
582
  // Try reading the dynamic section.
583
415
  if (!this->LoadDynamicSection()) {
584
70
    return nullptr;
585
70
  }
586
587
  // Get the string table referenced by the DYNAMIC section.
588
345
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
589
345
  if (sec.sh_link >= this->SectionHeaders.size()) {
590
58
    this->SetErrorMessage("Section DYNAMIC has invalid string table index.");
591
58
    return nullptr;
592
58
  }
593
287
  ELF_Shdr const& strtab = this->SectionHeaders[sec.sh_link];
594
595
  // Look for the requested entry.
596
287
  for (auto di = this->DynamicSectionEntries.begin();
597
211k
       di != this->DynamicSectionEntries.end(); ++di) {
598
211k
    ELF_Dyn& dyn = *di;
599
211k
    if (static_cast<tagtype>(dyn.d_tag) == static_cast<tagtype>(tag)) {
600
      // We found the tag requested.
601
      // Make sure the position given is within the string section.
602
75
      if (dyn.d_un.d_val >= strtab.sh_size) {
603
20
        this->SetErrorMessage("Section DYNAMIC references string beyond "
604
20
                              "the end of its string section.");
605
20
        return nullptr;
606
20
      }
607
608
      // Seek to the position reported by the entry.
609
55
      unsigned long first = static_cast<unsigned long>(dyn.d_un.d_val);
610
55
      unsigned long last = first;
611
55
      unsigned long end = static_cast<unsigned long>(strtab.sh_size);
612
55
      this->Stream->seekg(strtab.sh_offset + first);
613
614
      // Read the string.  It may be followed by more than one NULL
615
      // terminator.  Count the total size of the region allocated to
616
      // the string.  This assumes that the next string in the table
617
      // is non-empty, but the "chrpath" tool makes the same
618
      // assumption.
619
55
      bool terminated = false;
620
55
      char c;
621
626
      while (last != end && this->Stream->get(c) && !(terminated && c)) {
622
571
        ++last;
623
571
        if (c) {
624
262
          se.Value += c;
625
309
        } else {
626
309
          terminated = true;
627
309
        }
628
571
      }
629
630
      // Make sure the whole value was read.
631
55
      if (!(*this->Stream)) {
632
9
        if (tag == cmELF::TagRPath) {
633
4
          this->SetErrorMessage(
634
4
            "Dynamic section specifies unreadable DT_RPATH");
635
5
        } else if (tag == cmELF::TagRunPath) {
636
1
          this->SetErrorMessage(
637
1
            "Dynamic section specifies unreadable DT_RUNPATH");
638
4
        } else if (tag == cmELF::TagMipsRldMapRel) {
639
0
          this->SetErrorMessage(
640
0
            "Dynamic section specifies unreadable DT_MIPS_RLD_MAP_REL");
641
4
        } else {
642
4
          this->SetErrorMessage("Dynamic section specifies unreadable value"
643
4
                                " for unexpected attribute");
644
4
        }
645
9
        se.Value = "";
646
9
        return nullptr;
647
9
      }
648
649
      // The value has been read successfully.  Report it.
650
46
      se.Position = static_cast<unsigned long>(strtab.sh_offset + first);
651
46
      se.Size = last - first;
652
46
      se.IndexInSection =
653
46
        static_cast<int>(di - this->DynamicSectionEntries.begin());
654
46
      return &se;
655
55
    }
656
211k
  }
657
212
  return nullptr;
658
287
}
cmELFInternalImpl<cmELFTypes64>::GetDynamicSectionString(unsigned int)
Line
Count
Source
566
531
{
567
  // Short-circuit if already checked.
568
531
  auto dssi = this->DynamicSectionStrings.find(tag);
569
531
  if (dssi != this->DynamicSectionStrings.end()) {
570
61
    if (dssi->second.Position > 0) {
571
0
      return &dssi->second;
572
0
    }
573
61
    return nullptr;
574
61
  }
575
576
  // Create an entry for this tag.  Assume it is missing until found.
577
470
  StringEntry& se = this->DynamicSectionStrings[tag];
578
470
  se.Position = 0;
579
470
  se.Size = 0;
580
470
  se.IndexInSection = -1;
581
582
  // Try reading the dynamic section.
583
470
  if (!this->LoadDynamicSection()) {
584
87
    return nullptr;
585
87
  }
586
587
  // Get the string table referenced by the DYNAMIC section.
588
383
  ELF_Shdr const& sec = this->SectionHeaders[this->DynamicSectionIndex];
589
383
  if (sec.sh_link >= this->SectionHeaders.size()) {
590
96
    this->SetErrorMessage("Section DYNAMIC has invalid string table index.");
591
96
    return nullptr;
592
96
  }
593
287
  ELF_Shdr const& strtab = this->SectionHeaders[sec.sh_link];
594
595
  // Look for the requested entry.
596
287
  for (auto di = this->DynamicSectionEntries.begin();
597
12.7k
       di != this->DynamicSectionEntries.end(); ++di) {
598
12.4k
    ELF_Dyn& dyn = *di;
599
12.4k
    if (static_cast<tagtype>(dyn.d_tag) == static_cast<tagtype>(tag)) {
600
      // We found the tag requested.
601
      // Make sure the position given is within the string section.
602
68
      if (dyn.d_un.d_val >= strtab.sh_size) {
603
44
        this->SetErrorMessage("Section DYNAMIC references string beyond "
604
44
                              "the end of its string section.");
605
44
        return nullptr;
606
44
      }
607
608
      // Seek to the position reported by the entry.
609
24
      unsigned long first = static_cast<unsigned long>(dyn.d_un.d_val);
610
24
      unsigned long last = first;
611
24
      unsigned long end = static_cast<unsigned long>(strtab.sh_size);
612
24
      this->Stream->seekg(strtab.sh_offset + first);
613
614
      // Read the string.  It may be followed by more than one NULL
615
      // terminator.  Count the total size of the region allocated to
616
      // the string.  This assumes that the next string in the table
617
      // is non-empty, but the "chrpath" tool makes the same
618
      // assumption.
619
24
      bool terminated = false;
620
24
      char c;
621
114
      while (last != end && this->Stream->get(c) && !(terminated && c)) {
622
90
        ++last;
623
90
        if (c) {
624
26
          se.Value += c;
625
64
        } else {
626
64
          terminated = true;
627
64
        }
628
90
      }
629
630
      // Make sure the whole value was read.
631
24
      if (!(*this->Stream)) {
632
9
        if (tag == cmELF::TagRPath) {
633
1
          this->SetErrorMessage(
634
1
            "Dynamic section specifies unreadable DT_RPATH");
635
8
        } else if (tag == cmELF::TagRunPath) {
636
7
          this->SetErrorMessage(
637
7
            "Dynamic section specifies unreadable DT_RUNPATH");
638
7
        } else if (tag == cmELF::TagMipsRldMapRel) {
639
0
          this->SetErrorMessage(
640
0
            "Dynamic section specifies unreadable DT_MIPS_RLD_MAP_REL");
641
1
        } else {
642
1
          this->SetErrorMessage("Dynamic section specifies unreadable value"
643
1
                                " for unexpected attribute");
644
1
        }
645
9
        se.Value = "";
646
9
        return nullptr;
647
9
      }
648
649
      // The value has been read successfully.  Report it.
650
15
      se.Position = static_cast<unsigned long>(strtab.sh_offset + first);
651
15
      se.Size = last - first;
652
15
      se.IndexInSection =
653
15
        static_cast<int>(di - this->DynamicSectionEntries.begin());
654
15
      return &se;
655
24
    }
656
12.4k
  }
657
219
  return nullptr;
658
287
}
659
660
//============================================================================
661
// External class implementation.
662
663
long const cmELF::TagRPath = DT_RPATH;
664
long const cmELF::TagRunPath = DT_RUNPATH;
665
long const cmELF::TagMipsRldMapRel = DT_MIPS_RLD_MAP_REL;
666
667
cmELF::cmELF(char const* fname)
668
998
{
669
  // Try to open the file.
670
998
  auto fin = cm::make_unique<cmsys::ifstream>(fname, std::ios::binary);
671
672
  // Quit now if the file could not be opened.
673
998
  if (!fin || !*fin) {
674
0
    this->ErrorMessage = "Error opening input file.";
675
0
    return;
676
0
  }
677
678
  // Read the ELF identification block.
679
998
  char ident[EI_NIDENT];
680
998
  if (!fin->read(ident, EI_NIDENT)) {
681
0
    this->ErrorMessage = "Error reading ELF identification.";
682
0
    return;
683
0
  }
684
998
  if (!fin->seekg(0)) {
685
0
    this->ErrorMessage = "Error seeking to beginning of file.";
686
0
    return;
687
0
  }
688
689
  // Verify the ELF identification.
690
998
  if (!(ident[EI_MAG0] == ELFMAG0 && ident[EI_MAG1] == ELFMAG1 &&
691
944
        ident[EI_MAG2] == ELFMAG2 && ident[EI_MAG3] == ELFMAG3)) {
692
76
    this->ErrorMessage = "File does not have a valid ELF identification.";
693
76
    return;
694
76
  }
695
696
  // Check the byte order in which the rest of the file is encoded.
697
922
  cmELFInternal::ByteOrderType order;
698
922
  if (ident[EI_DATA] == ELFDATA2LSB) {
699
    // File is LSB.
700
578
    order = cmELFInternal::ByteOrderLSB;
701
578
  } else if (ident[EI_DATA] == ELFDATA2MSB) {
702
    // File is MSB.
703
331
    order = cmELFInternal::ByteOrderMSB;
704
331
  } else {
705
13
    this->ErrorMessage = "ELF file is not LSB or MSB encoded.";
706
13
    return;
707
13
  }
708
709
  // Check the class of the file and construct the corresponding
710
  // parser implementation.
711
909
  if (ident[EI_CLASS] == ELFCLASS32) {
712
    // 32-bit ELF
713
456
    this->Internal = cm::make_unique<cmELFInternalImpl<cmELFTypes32>>(
714
456
      this, std::move(fin), order);
715
456
  } else if (ident[EI_CLASS] == ELFCLASS64) {
716
    // 64-bit ELF
717
443
    this->Internal = cm::make_unique<cmELFInternalImpl<cmELFTypes64>>(
718
443
      this, std::move(fin), order);
719
443
  } else {
720
10
    this->ErrorMessage = "ELF file class is not 32-bit or 64-bit.";
721
10
    return;
722
10
  }
723
909
}
724
725
998
cmELF::~cmELF() = default;
726
727
bool cmELF::Valid() const
728
14.3k
{
729
14.3k
  return this->Internal && this->Internal->GetFileType() != FileTypeInvalid;
730
14.3k
}
731
732
cmELF::FileType cmELF::GetFileType() const
733
611
{
734
611
  if (this->Valid()) {
735
611
    return this->Internal->GetFileType();
736
611
  }
737
0
  return FileTypeInvalid;
738
611
}
739
740
std::uint16_t cmELF::GetMachine() const
741
611
{
742
611
  if (this->Valid()) {
743
611
    return this->Internal->GetMachine();
744
611
  }
745
0
  return 0;
746
611
}
747
748
std::size_t cmELF::GetNumberOfSections() const
749
611
{
750
611
  if (this->Valid()) {
751
611
    return this->Internal->GetNumberOfSections();
752
611
  }
753
0
  return 0;
754
611
}
755
756
unsigned long cmELF::GetDynamicEntryPosition(int index) const
757
6.51k
{
758
6.51k
  if (this->Valid()) {
759
6.51k
    return this->Internal->GetDynamicEntryPosition(index);
760
6.51k
  }
761
0
  return 0;
762
6.51k
}
763
764
cmELF::DynamicEntryList cmELF::GetDynamicEntries() const
765
611
{
766
611
  if (this->Valid()) {
767
309
    return this->Internal->GetDynamicEntries();
768
309
  }
769
770
302
  return cmELF::DynamicEntryList();
771
611
}
772
773
std::vector<char> cmELF::EncodeDynamicEntries(
774
  cmELF::DynamicEntryList const& dentries) const
775
100
{
776
100
  if (this->Valid()) {
777
100
    return this->Internal->EncodeDynamicEntries(dentries);
778
100
  }
779
780
0
  return std::vector<char>();
781
100
}
782
783
bool cmELF::HasDynamicSection() const
784
611
{
785
611
  return this->Valid() && this->Internal->HasDynamicSection();
786
611
}
787
788
bool cmELF::GetSOName(std::string& soname)
789
611
{
790
611
  if (StringEntry const* se = this->GetSOName()) {
791
36
    soname = se->Value;
792
36
    return true;
793
36
  }
794
575
  return false;
795
611
}
796
797
cmELF::StringEntry const* cmELF::GetSOName()
798
1.22k
{
799
1.22k
  if (this->Valid() &&
800
1.07k
      this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary) {
801
446
    return this->Internal->GetSOName();
802
446
  }
803
776
  return nullptr;
804
1.22k
}
805
806
cmELF::StringEntry const* cmELF::GetRPath()
807
611
{
808
611
  if (this->Valid() &&
809
463
      (this->Internal->GetFileType() == cmELF::FileTypeExecutable ||
810
351
       this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary)) {
811
351
    return this->Internal->GetRPath();
812
351
  }
813
260
  return nullptr;
814
611
}
815
816
cmELF::StringEntry const* cmELF::GetRunPath()
817
611
{
818
611
  if (this->Valid() &&
819
349
      (this->Internal->GetFileType() == cmELF::FileTypeExecutable ||
820
251
       this->Internal->GetFileType() == cmELF::FileTypeSharedLibrary)) {
821
237
    return this->Internal->GetRunPath();
822
237
  }
823
374
  return nullptr;
824
611
}
825
826
bool cmELF::IsMIPS() const
827
611
{
828
611
  if (this->Valid()) {
829
611
    return this->Internal->IsMips();
830
611
  }
831
0
  return false;
832
611
}
833
834
void cmELF::PrintInfo(std::ostream& os) const
835
611
{
836
611
  if (this->Valid()) {
837
291
    this->Internal->PrintInfo(os);
838
320
  } else {
839
320
    os << "Not a valid ELF file.\n";
840
320
  }
841
611
}