Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Object/ELF.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ELF.cpp - ELF object file implementation ---------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/Object/ELF.h"
10
#include "llvm/BinaryFormat/ELF.h"
11
#include "llvm/Support/LEB128.h"
12
13
using namespace llvm;
14
using namespace object;
15
16
#define STRINGIFY_ENUM_CASE(ns, name)                                          \
17
407k
  case ns::name:                                                               \
18
407k
    return #name;
19
20
8.06k
#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
21
22
StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
23
8.06k
                                                 uint32_t Type) {
24
8.06k
  switch (Machine) {
25
8.06k
  case ELF::EM_X86_64:
26
1.18k
    switch (Type) {
27
1.18k
#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
28
1.18k
    default:
29
0
      break;
30
0
    }
31
0
    break;
32
358
  case ELF::EM_386:
33
358
  case ELF::EM_IAMCU:
34
358
    switch (Type) {
35
358
#include "llvm/BinaryFormat/ELFRelocs/i386.def"
36
358
    default:
37
1
      break;
38
1
    }
39
1
    break;
40
2.98k
  case ELF::EM_MIPS:
41
2.98k
    switch (Type) {
42
2.98k
#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
43
2.98k
    default:
44
0
      break;
45
0
    }
46
0
    break;
47
846
  case ELF::EM_AARCH64:
48
846
    switch (Type) {
49
846
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
50
846
    default:
51
0
      break;
52
0
    }
53
0
    break;
54
629
  case ELF::EM_ARM:
55
629
    switch (Type) {
56
629
#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
57
629
    default:
58
0
      break;
59
0
    }
60
0
    break;
61
0
  case ELF::EM_ARC_COMPACT:
62
0
  case ELF::EM_ARC_COMPACT2:
63
0
    switch (Type) {
64
0
#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
65
0
    default:
66
0
      break;
67
0
    }
68
0
    break;
69
0
  case ELF::EM_AVR:
70
0
    switch (Type) {
71
0
#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
72
0
    default:
73
0
      break;
74
0
    }
75
0
    break;
76
117
  case ELF::EM_HEXAGON:
77
117
    switch (Type) {
78
117
#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
79
117
    default:
80
0
      break;
81
0
    }
82
0
    break;
83
14
  case ELF::EM_LANAI:
84
14
    switch (Type) {
85
14
#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
86
14
    default:
87
0
      break;
88
0
    }
89
0
    break;
90
38
  case ELF::EM_PPC:
91
38
    switch (Type) {
92
38
#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
93
38
    default:
94
0
      break;
95
0
    }
96
0
    break;
97
934
  case ELF::EM_PPC64:
98
934
    switch (Type) {
99
934
#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
100
934
    default:
101
0
      break;
102
0
    }
103
0
    break;
104
603
  case ELF::EM_RISCV:
105
603
    switch (Type) {
106
603
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
107
603
    default:
108
0
      break;
109
0
    }
110
0
    break;
111
31
  case ELF::EM_S390:
112
31
    switch (Type) {
113
31
#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
114
31
    default:
115
0
      break;
116
0
    }
117
0
    break;
118
183
  case ELF::EM_SPARC:
119
183
  case ELF::EM_SPARC32PLUS:
120
183
  case ELF::EM_SPARCV9:
121
183
    switch (Type) {
122
183
#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
123
183
    default:
124
0
      break;
125
0
    }
126
0
    break;
127
71
  case ELF::EM_AMDGPU:
128
71
    switch (Type) {
129
71
#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
130
71
    default:
131
0
      break;
132
0
    }
133
0
    break;
134
67
  case ELF::EM_BPF:
135
67
    switch (Type) {
136
67
#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
137
67
    default:
138
0
      break;
139
0
    }
140
0
    break;
141
7
  case ELF::EM_MSP430:
142
7
    switch (Type) {
143
7
#include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
144
7
    default:
145
0
      break;
146
0
    }
147
0
    break;
148
0
  default:
149
0
    break;
150
1
  }
151
1
  return "Unknown";
152
1
}
153
154
#undef ELF_RELOC
155
156
16
uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
157
16
  switch (Machine) {
158
16
  case ELF::EM_X86_64:
159
11
    return ELF::R_X86_64_RELATIVE;
160
16
  case ELF::EM_386:
161
2
  case ELF::EM_IAMCU:
162
2
    return ELF::R_386_RELATIVE;
163
2
  case ELF::EM_MIPS:
164
0
    break;
165
2
  case ELF::EM_AARCH64:
166
1
    return ELF::R_AARCH64_RELATIVE;
167
2
  case ELF::EM_ARM:
168
2
    return ELF::R_ARM_RELATIVE;
169
2
  case ELF::EM_ARC_COMPACT:
170
0
  case ELF::EM_ARC_COMPACT2:
171
0
    return ELF::R_ARC_RELATIVE;
172
0
  case ELF::EM_AVR:
173
0
    break;
174
0
  case ELF::EM_HEXAGON:
175
0
    return ELF::R_HEX_RELATIVE;
176
0
  case ELF::EM_LANAI:
177
0
    break;
178
0
  case ELF::EM_PPC:
179
0
    break;
180
0
  case ELF::EM_PPC64:
181
0
    return ELF::R_PPC64_RELATIVE;
182
0
  case ELF::EM_RISCV:
183
0
    return ELF::R_RISCV_RELATIVE;
184
0
  case ELF::EM_S390:
185
0
    return ELF::R_390_RELATIVE;
186
0
  case ELF::EM_SPARC:
187
0
  case ELF::EM_SPARC32PLUS:
188
0
  case ELF::EM_SPARCV9:
189
0
    return ELF::R_SPARC_RELATIVE;
190
0
  case ELF::EM_AMDGPU:
191
0
    break;
192
0
  case ELF::EM_BPF:
193
0
    break;
194
0
  default:
195
0
    break;
196
0
  }
197
0
  return 0;
198
0
}
199
200
399k
StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
201
399k
  switch (Machine) {
202
399k
  case ELF::EM_ARM:
203
846
    switch (Type) {
204
846
      
STRINGIFY_ENUM_CASE74
(ELF, SHT_ARM_EXIDX);
205
1
      STRINGIFY_ENUM_CASE
(ELF, SHT_ARM_PREEMPTMAP)0
;
206
17
      STRINGIFY_ENUM_CASE
(ELF, SHT_ARM_ATTRIBUTES)0
;
207
1
      STRINGIFY_ENUM_CASE
(ELF, SHT_ARM_DEBUGOVERLAY)0
;
208
1
      STRINGIFY_ENUM_CASE
(ELF, SHT_ARM_OVERLAYSECTION)0
;0
209
846
    }
210
846
    
break752
;
211
846
  case ELF::EM_HEXAGON:
212
10
    switch (Type) { 
STRINGIFY_ENUM_CASE0
(ELF, SHT_HEX_ORDERED)
;0
}
213
10
    break;
214
396k
  case ELF::EM_X86_64:
215
396k
    switch (Type) { 
STRINGIFY_ENUM_CASE37
(ELF, SHT_X86_64_UNWIND)
;0
}
216
396k
    
break396k
;
217
396k
  case ELF::EM_MIPS:
218
769
  case ELF::EM_MIPS_RS3_LE:
219
769
    switch (Type) {
220
769
      
STRINGIFY_ENUM_CASE49
(ELF, SHT_MIPS_REGINFO);
221
20
      STRINGIFY_ENUM_CASE
(ELF, SHT_MIPS_OPTIONS)0
;
222
2
      STRINGIFY_ENUM_CASE
(ELF, SHT_MIPS_DWARF)0
;
223
68
      STRINGIFY_ENUM_CASE
(ELF, SHT_MIPS_ABIFLAGS)0
;0
224
769
    }
225
769
    
break630
;
226
1.36k
  default:
227
1.36k
    break;
228
398k
  }
229
398k
230
398k
  switch (Type) {
231
398k
    
STRINGIFY_ENUM_CASE869
(ELF, SHT_NULL);
232
132k
    STRINGIFY_ENUM_CASE
(ELF, SHT_PROGBITS)0
;
233
820
    STRINGIFY_ENUM_CASE
(ELF, SHT_SYMTAB)0
;
234
1.60k
    STRINGIFY_ENUM_CASE
(ELF, SHT_STRTAB)0
;
235
304
    STRINGIFY_ENUM_CASE
(ELF, SHT_RELA)0
;
236
175
    STRINGIFY_ENUM_CASE
(ELF, SHT_HASH)0
;
237
184
    STRINGIFY_ENUM_CASE
(ELF, SHT_DYNAMIC)0
;
238
41
    STRINGIFY_ENUM_CASE
(ELF, SHT_NOTE)0
;
239
261k
    STRINGIFY_ENUM_CASE
(ELF, SHT_NOBITS)0
;
240
198
    STRINGIFY_ENUM_CASE
(ELF, SHT_REL)0
;
241
1
    STRINGIFY_ENUM_CASE
(ELF, SHT_SHLIB)0
;
242
198
    STRINGIFY_ENUM_CASE
(ELF, SHT_DYNSYM)0
;
243
12
    STRINGIFY_ENUM_CASE
(ELF, SHT_INIT_ARRAY)0
;
244
8
    STRINGIFY_ENUM_CASE
(ELF, SHT_FINI_ARRAY)0
;
245
6
    STRINGIFY_ENUM_CASE
(ELF, SHT_PREINIT_ARRAY)0
;
246
51
    STRINGIFY_ENUM_CASE
(ELF, SHT_GROUP)0
;
247
4
    STRINGIFY_ENUM_CASE
(ELF, SHT_SYMTAB_SHNDX)0
;
248
4
    STRINGIFY_ENUM_CASE
(ELF, SHT_RELR)0
;
249
2
    STRINGIFY_ENUM_CASE
(ELF, SHT_ANDROID_REL)0
;
250
3
    STRINGIFY_ENUM_CASE
(ELF, SHT_ANDROID_RELA)0
;
251
1
    STRINGIFY_ENUM_CASE
(ELF, SHT_ANDROID_RELR)0
;
252
2
    STRINGIFY_ENUM_CASE
(ELF, SHT_LLVM_ODRTAB)0
;
253
2
    STRINGIFY_ENUM_CASE
(ELF, SHT_LLVM_LINKER_OPTIONS)0
;
254
4
    STRINGIFY_ENUM_CASE
(ELF, SHT_LLVM_CALL_GRAPH_PROFILE)0
;
255
5
    STRINGIFY_ENUM_CASE
(ELF, SHT_LLVM_ADDRSIG)0
;
256
1
    STRINGIFY_ENUM_CASE
(ELF, SHT_LLVM_DEPENDENT_LIBRARIES)0
;
257
1
    STRINGIFY_ENUM_CASE
(ELF, SHT_LLVM_SYMPART)0
;
258
2
    STRINGIFY_ENUM_CASE
(ELF, SHT_GNU_ATTRIBUTES)0
;
259
111
    STRINGIFY_ENUM_CASE
(ELF, SHT_GNU_HASH)0
;
260
4
    STRINGIFY_ENUM_CASE
(ELF, SHT_GNU_verdef)0
;
261
5
    STRINGIFY_ENUM_CASE
(ELF, SHT_GNU_verneed)0
;
262
6
    STRINGIFY_ENUM_CASE
(ELF, SHT_GNU_versym)0
;
263
13
  default:
264
13
    return "Unknown";
265
398k
  }
266
398k
}
267
268
template <class ELFT>
269
Expected<std::vector<typename ELFT::Rela>>
270
9
ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
271
9
  // This function decodes the contents of an SHT_RELR packed relocation
272
9
  // section.
273
9
  //
274
9
  // Proposal for adding SHT_RELR sections to generic-abi is here:
275
9
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
276
9
  //
277
9
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
278
9
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
279
9
  //
280
9
  // i.e. start with an address, followed by any number of bitmaps. The address
281
9
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
282
9
  // relocations each, at subsequent offsets following the last address entry.
283
9
  //
284
9
  // The bitmap entries must have 1 in the least significant bit. The assumption
285
9
  // here is that an address cannot have 1 in lsb. Odd addresses are not
286
9
  // supported.
287
9
  //
288
9
  // Excluding the least significant bit in the bitmap, each non-zero bit in
289
9
  // the bitmap represents a relocation to be applied to a corresponding machine
290
9
  // word that follows the base address word. The second least significant bit
291
9
  // represents the machine word immediately following the initial address, and
292
9
  // each bit that follows represents the next word, in linear order. As such,
293
9
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
294
9
  // 63 relocations in a 64-bit object.
295
9
  //
296
9
  // This encoding has a couple of interesting properties:
297
9
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
298
9
  //    even means address, odd means bitmap.
299
9
  // 2. Just a simple list of addresses is a valid encoding.
300
9
301
9
  Elf_Rela Rela;
302
9
  Rela.r_info = 0;
303
9
  Rela.r_addend = 0;
304
9
  Rela.setType(getRelativeRelocationType(), false);
305
9
  std::vector<Elf_Rela> Relocs;
306
9
307
9
  // Word type: uint32_t for Elf32, and uint64_t for Elf64.
308
9
  typedef typename ELFT::uint Word;
309
9
310
9
  // Word size in number of bytes.
311
9
  const size_t WordSize = sizeof(Word);
312
9
313
9
  // Number of bits used for the relocation offsets bitmap.
314
9
  // These many relative relocations can be encoded in a single entry.
315
9
  const size_t NBits = 8*WordSize - 1;
316
9
317
9
  Word Base = 0;
318
29
  for (const Elf_Relr &R : relrs) {
319
29
    Word Entry = R;
320
29
    if ((Entry&1) == 0) {
321
13
      // Even entry: encodes the offset for next relocation.
322
13
      Rela.r_offset = Entry;
323
13
      Relocs.push_back(Rela);
324
13
      // Set base offset for subsequent bitmap entries.
325
13
      Base = Entry + WordSize;
326
13
      continue;
327
13
    }
328
16
329
16
    // Odd entry: encodes bitmap for relocations starting at base.
330
16
    Word Offset = Base;
331
385
    while (Entry != 0) {
332
369
      Entry >>= 1;
333
369
      if ((Entry&1) != 0) {
334
141
        Rela.r_offset = Offset;
335
141
        Relocs.push_back(Rela);
336
141
      }
337
369
      Offset += WordSize;
338
369
    }
339
16
340
16
    // Advance base offset by NBits words.
341
16
    Base += NBits * WordSize;
342
16
  }
343
9
344
9
  return Relocs;
345
9
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::decode_relrs(llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<unsigned int, (llvm::support::endianness)1, 1ul> >) const
Line
Count
Source
270
4
ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
271
4
  // This function decodes the contents of an SHT_RELR packed relocation
272
4
  // section.
273
4
  //
274
4
  // Proposal for adding SHT_RELR sections to generic-abi is here:
275
4
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
276
4
  //
277
4
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
278
4
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
279
4
  //
280
4
  // i.e. start with an address, followed by any number of bitmaps. The address
281
4
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
282
4
  // relocations each, at subsequent offsets following the last address entry.
283
4
  //
284
4
  // The bitmap entries must have 1 in the least significant bit. The assumption
285
4
  // here is that an address cannot have 1 in lsb. Odd addresses are not
286
4
  // supported.
287
4
  //
288
4
  // Excluding the least significant bit in the bitmap, each non-zero bit in
289
4
  // the bitmap represents a relocation to be applied to a corresponding machine
290
4
  // word that follows the base address word. The second least significant bit
291
4
  // represents the machine word immediately following the initial address, and
292
4
  // each bit that follows represents the next word, in linear order. As such,
293
4
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
294
4
  // 63 relocations in a 64-bit object.
295
4
  //
296
4
  // This encoding has a couple of interesting properties:
297
4
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
298
4
  //    even means address, odd means bitmap.
299
4
  // 2. Just a simple list of addresses is a valid encoding.
300
4
301
4
  Elf_Rela Rela;
302
4
  Rela.r_info = 0;
303
4
  Rela.r_addend = 0;
304
4
  Rela.setType(getRelativeRelocationType(), false);
305
4
  std::vector<Elf_Rela> Relocs;
306
4
307
4
  // Word type: uint32_t for Elf32, and uint64_t for Elf64.
308
4
  typedef typename ELFT::uint Word;
309
4
310
4
  // Word size in number of bytes.
311
4
  const size_t WordSize = sizeof(Word);
312
4
313
4
  // Number of bits used for the relocation offsets bitmap.
314
4
  // These many relative relocations can be encoded in a single entry.
315
4
  const size_t NBits = 8*WordSize - 1;
316
4
317
4
  Word Base = 0;
318
15
  for (const Elf_Relr &R : relrs) {
319
15
    Word Entry = R;
320
15
    if ((Entry&1) == 0) {
321
6
      // Even entry: encodes the offset for next relocation.
322
6
      Rela.r_offset = Entry;
323
6
      Relocs.push_back(Rela);
324
6
      // Set base offset for subsequent bitmap entries.
325
6
      Base = Entry + WordSize;
326
6
      continue;
327
6
    }
328
9
329
9
    // Odd entry: encodes bitmap for relocations starting at base.
330
9
    Word Offset = Base;
331
190
    while (Entry != 0) {
332
181
      Entry >>= 1;
333
181
      if ((Entry&1) != 0) {
334
80
        Rela.r_offset = Offset;
335
80
        Relocs.push_back(Rela);
336
80
      }
337
181
      Offset += WordSize;
338
181
    }
339
9
340
9
    // Advance base offset by NBits words.
341
9
    Base += NBits * WordSize;
342
9
  }
343
4
344
4
  return Relocs;
345
4
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::decode_relrs(llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<unsigned int, (llvm::support::endianness)0, 1ul> >) const
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::decode_relrs(llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<unsigned long long, (llvm::support::endianness)1, 1ul> >) const
Line
Count
Source
270
5
ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
271
5
  // This function decodes the contents of an SHT_RELR packed relocation
272
5
  // section.
273
5
  //
274
5
  // Proposal for adding SHT_RELR sections to generic-abi is here:
275
5
  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
276
5
  //
277
5
  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
278
5
  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
279
5
  //
280
5
  // i.e. start with an address, followed by any number of bitmaps. The address
281
5
  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
282
5
  // relocations each, at subsequent offsets following the last address entry.
283
5
  //
284
5
  // The bitmap entries must have 1 in the least significant bit. The assumption
285
5
  // here is that an address cannot have 1 in lsb. Odd addresses are not
286
5
  // supported.
287
5
  //
288
5
  // Excluding the least significant bit in the bitmap, each non-zero bit in
289
5
  // the bitmap represents a relocation to be applied to a corresponding machine
290
5
  // word that follows the base address word. The second least significant bit
291
5
  // represents the machine word immediately following the initial address, and
292
5
  // each bit that follows represents the next word, in linear order. As such,
293
5
  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
294
5
  // 63 relocations in a 64-bit object.
295
5
  //
296
5
  // This encoding has a couple of interesting properties:
297
5
  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
298
5
  //    even means address, odd means bitmap.
299
5
  // 2. Just a simple list of addresses is a valid encoding.
300
5
301
5
  Elf_Rela Rela;
302
5
  Rela.r_info = 0;
303
5
  Rela.r_addend = 0;
304
5
  Rela.setType(getRelativeRelocationType(), false);
305
5
  std::vector<Elf_Rela> Relocs;
306
5
307
5
  // Word type: uint32_t for Elf32, and uint64_t for Elf64.
308
5
  typedef typename ELFT::uint Word;
309
5
310
5
  // Word size in number of bytes.
311
5
  const size_t WordSize = sizeof(Word);
312
5
313
5
  // Number of bits used for the relocation offsets bitmap.
314
5
  // These many relative relocations can be encoded in a single entry.
315
5
  const size_t NBits = 8*WordSize - 1;
316
5
317
5
  Word Base = 0;
318
14
  for (const Elf_Relr &R : relrs) {
319
14
    Word Entry = R;
320
14
    if ((Entry&1) == 0) {
321
7
      // Even entry: encodes the offset for next relocation.
322
7
      Rela.r_offset = Entry;
323
7
      Relocs.push_back(Rela);
324
7
      // Set base offset for subsequent bitmap entries.
325
7
      Base = Entry + WordSize;
326
7
      continue;
327
7
    }
328
7
329
7
    // Odd entry: encodes bitmap for relocations starting at base.
330
7
    Word Offset = Base;
331
195
    while (Entry != 0) {
332
188
      Entry >>= 1;
333
188
      if ((Entry&1) != 0) {
334
61
        Rela.r_offset = Offset;
335
61
        Relocs.push_back(Rela);
336
61
      }
337
188
      Offset += WordSize;
338
188
    }
339
7
340
7
    // Advance base offset by NBits words.
341
7
    Base += NBits * WordSize;
342
7
  }
343
5
344
5
  return Relocs;
345
5
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::decode_relrs(llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<unsigned long long, (llvm::support::endianness)0, 1ul> >) const
346
347
template <class ELFT>
348
Expected<std::vector<typename ELFT::Rela>>
349
18
ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
350
18
  // This function reads relocations in Android's packed relocation format,
351
18
  // which is based on SLEB128 and delta encoding.
352
18
  Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
353
18
  if (!ContentsOrErr)
354
0
    return ContentsOrErr.takeError();
355
18
  const uint8_t *Cur = ContentsOrErr->begin();
356
18
  const uint8_t *End = ContentsOrErr->end();
357
18
  if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
358
18
      Cur[2] != 'S' || Cur[3] != '2')
359
1
    return createError("invalid packed relocation header");
360
17
  Cur += 4;
361
17
362
17
  const char *ErrStr = nullptr;
363
269
  auto ReadSLEB = [&]() -> int64_t {
364
269
    if (ErrStr)
365
2
      return 0;
366
267
    unsigned Len;
367
267
    int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
368
267
    Cur += Len;
369
267
    return Result;
370
267
  };
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*) const::'lambda'()::operator()() const
Line
Count
Source
363
57
  auto ReadSLEB = [&]() -> int64_t {
364
57
    if (ErrStr)
365
0
      return 0;
366
57
    unsigned Len;
367
57
    int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
368
57
    Cur += Len;
369
57
    return Result;
370
57
  };
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*) const::'lambda'()::operator()() const
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*) const::'lambda'()::operator()() const
Line
Count
Source
363
212
  auto ReadSLEB = [&]() -> int64_t {
364
212
    if (ErrStr)
365
2
      return 0;
366
210
    unsigned Len;
367
210
    int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
368
210
    Cur += Len;
369
210
    return Result;
370
210
  };
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*) const::'lambda'()::operator()() const
371
17
372
17
  uint64_t NumRelocs = ReadSLEB();
373
17
  uint64_t Offset = ReadSLEB();
374
17
  uint64_t Addend = 0;
375
17
376
17
  if (ErrStr)
377
1
    return createError(ErrStr);
378
16
379
16
  std::vector<Elf_Rela> Relocs;
380
16
  Relocs.reserve(NumRelocs);
381
50
  while (NumRelocs) {
382
37
    uint64_t NumRelocsInGroup = ReadSLEB();
383
37
    if (NumRelocsInGroup > NumRelocs)
384
1
      return createError("relocation group unexpectedly large");
385
36
    NumRelocs -= NumRelocsInGroup;
386
36
387
36
    uint64_t GroupFlags = ReadSLEB();
388
36
    bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
389
36
    bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
390
36
    bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
391
36
    bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
392
36
393
36
    uint64_t GroupOffsetDelta;
394
36
    if (GroupedByOffsetDelta)
395
15
      GroupOffsetDelta = ReadSLEB();
396
36
397
36
    uint64_t GroupRInfo;
398
36
    if (GroupedByInfo)
399
14
      GroupRInfo = ReadSLEB();
400
36
401
36
    if (GroupedByAddend && 
GroupHasAddend2
)
402
2
      Addend += ReadSLEB();
403
36
404
36
    if (!GroupHasAddend)
405
18
      Addend = 0;
406
36
407
143
    for (uint64_t I = 0; I != NumRelocsInGroup; 
++I107
) {
408
108
      Elf_Rela R;
409
108
      Offset += GroupedByOffsetDelta ? 
GroupOffsetDelta59
:
ReadSLEB()49
;
410
108
      R.r_offset = Offset;
411
108
      R.r_info = GroupedByInfo ? 
GroupRInfo70
:
ReadSLEB()38
;
412
108
      if (GroupHasAddend && 
!GroupedByAddend48
)
413
44
        Addend += ReadSLEB();
414
108
      R.r_addend = Addend;
415
108
      Relocs.push_back(R);
416
108
417
108
      if (ErrStr)
418
1
        return createError(ErrStr);
419
108
    }
420
36
421
36
    
if (35
ErrStr35
)
422
1
      return createError(ErrStr);
423
35
  }
424
16
425
16
  
return Relocs13
;
426
16
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*) const
Line
Count
Source
349
3
ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
350
3
  // This function reads relocations in Android's packed relocation format,
351
3
  // which is based on SLEB128 and delta encoding.
352
3
  Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
353
3
  if (!ContentsOrErr)
354
0
    return ContentsOrErr.takeError();
355
3
  const uint8_t *Cur = ContentsOrErr->begin();
356
3
  const uint8_t *End = ContentsOrErr->end();
357
3
  if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
358
3
      Cur[2] != 'S' || Cur[3] != '2')
359
0
    return createError("invalid packed relocation header");
360
3
  Cur += 4;
361
3
362
3
  const char *ErrStr = nullptr;
363
3
  auto ReadSLEB = [&]() -> int64_t {
364
3
    if (ErrStr)
365
3
      return 0;
366
3
    unsigned Len;
367
3
    int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
368
3
    Cur += Len;
369
3
    return Result;
370
3
  };
371
3
372
3
  uint64_t NumRelocs = ReadSLEB();
373
3
  uint64_t Offset = ReadSLEB();
374
3
  uint64_t Addend = 0;
375
3
376
3
  if (ErrStr)
377
0
    return createError(ErrStr);
378
3
379
3
  std::vector<Elf_Rela> Relocs;
380
3
  Relocs.reserve(NumRelocs);
381
13
  while (NumRelocs) {
382
10
    uint64_t NumRelocsInGroup = ReadSLEB();
383
10
    if (NumRelocsInGroup > NumRelocs)
384
0
      return createError("relocation group unexpectedly large");
385
10
    NumRelocs -= NumRelocsInGroup;
386
10
387
10
    uint64_t GroupFlags = ReadSLEB();
388
10
    bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
389
10
    bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
390
10
    bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
391
10
    bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
392
10
393
10
    uint64_t GroupOffsetDelta;
394
10
    if (GroupedByOffsetDelta)
395
8
      GroupOffsetDelta = ReadSLEB();
396
10
397
10
    uint64_t GroupRInfo;
398
10
    if (GroupedByInfo)
399
7
      GroupRInfo = ReadSLEB();
400
10
401
10
    if (GroupedByAddend && 
GroupHasAddend0
)
402
0
      Addend += ReadSLEB();
403
10
404
10
    if (!GroupHasAddend)
405
10
      Addend = 0;
406
10
407
57
    for (uint64_t I = 0; I != NumRelocsInGroup; 
++I47
) {
408
47
      Elf_Rela R;
409
47
      Offset += GroupedByOffsetDelta ? 
GroupOffsetDelta37
:
ReadSLEB()10
;
410
47
      R.r_offset = Offset;
411
47
      R.r_info = GroupedByInfo ? 
GroupRInfo41
:
ReadSLEB()6
;
412
47
      if (GroupHasAddend && 
!GroupedByAddend0
)
413
0
        Addend += ReadSLEB();
414
47
      R.r_addend = Addend;
415
47
      Relocs.push_back(R);
416
47
417
47
      if (ErrStr)
418
0
        return createError(ErrStr);
419
47
    }
420
10
421
10
    if (ErrStr)
422
0
      return createError(ErrStr);
423
10
  }
424
3
425
3
  return Relocs;
426
3
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*) const
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*) const
Line
Count
Source
349
15
ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
350
15
  // This function reads relocations in Android's packed relocation format,
351
15
  // which is based on SLEB128 and delta encoding.
352
15
  Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
353
15
  if (!ContentsOrErr)
354
0
    return ContentsOrErr.takeError();
355
15
  const uint8_t *Cur = ContentsOrErr->begin();
356
15
  const uint8_t *End = ContentsOrErr->end();
357
15
  if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
358
15
      Cur[2] != 'S' || Cur[3] != '2')
359
1
    return createError("invalid packed relocation header");
360
14
  Cur += 4;
361
14
362
14
  const char *ErrStr = nullptr;
363
14
  auto ReadSLEB = [&]() -> int64_t {
364
14
    if (ErrStr)
365
14
      return 0;
366
14
    unsigned Len;
367
14
    int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
368
14
    Cur += Len;
369
14
    return Result;
370
14
  };
371
14
372
14
  uint64_t NumRelocs = ReadSLEB();
373
14
  uint64_t Offset = ReadSLEB();
374
14
  uint64_t Addend = 0;
375
14
376
14
  if (ErrStr)
377
1
    return createError(ErrStr);
378
13
379
13
  std::vector<Elf_Rela> Relocs;
380
13
  Relocs.reserve(NumRelocs);
381
37
  while (NumRelocs) {
382
27
    uint64_t NumRelocsInGroup = ReadSLEB();
383
27
    if (NumRelocsInGroup > NumRelocs)
384
1
      return createError("relocation group unexpectedly large");
385
26
    NumRelocs -= NumRelocsInGroup;
386
26
387
26
    uint64_t GroupFlags = ReadSLEB();
388
26
    bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
389
26
    bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
390
26
    bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
391
26
    bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
392
26
393
26
    uint64_t GroupOffsetDelta;
394
26
    if (GroupedByOffsetDelta)
395
7
      GroupOffsetDelta = ReadSLEB();
396
26
397
26
    uint64_t GroupRInfo;
398
26
    if (GroupedByInfo)
399
7
      GroupRInfo = ReadSLEB();
400
26
401
26
    if (GroupedByAddend && 
GroupHasAddend2
)
402
2
      Addend += ReadSLEB();
403
26
404
26
    if (!GroupHasAddend)
405
8
      Addend = 0;
406
26
407
86
    for (uint64_t I = 0; I != NumRelocsInGroup; 
++I60
) {
408
61
      Elf_Rela R;
409
61
      Offset += GroupedByOffsetDelta ? 
GroupOffsetDelta22
:
ReadSLEB()39
;
410
61
      R.r_offset = Offset;
411
61
      R.r_info = GroupedByInfo ? 
GroupRInfo29
:
ReadSLEB()32
;
412
61
      if (GroupHasAddend && 
!GroupedByAddend48
)
413
44
        Addend += ReadSLEB();
414
61
      R.r_addend = Addend;
415
61
      Relocs.push_back(R);
416
61
417
61
      if (ErrStr)
418
1
        return createError(ErrStr);
419
61
    }
420
26
421
26
    
if (25
ErrStr25
)
422
1
      return createError(ErrStr);
423
25
  }
424
13
425
13
  
return Relocs10
;
426
13
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::android_relas(llvm::object::Elf_Shdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*) const
427
428
template <class ELFT>
429
std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
430
271
                                                 uint64_t Type) const {
431
271
#define DYNAMIC_STRINGIFY_ENUM(tag, value)                                     \
432
271
  case value:                                                                  \
433
266
    return #tag;
434
271
435
271
#define DYNAMIC_TAG(n, v)
436
271
  switch (Arch) {
437
271
  case ELF::EM_AARCH64:
438
0
    switch (Type) {
439
0
#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
440
0
#include "llvm/BinaryFormat/DynamicTags.def"
441
0
#undef AARCH64_DYNAMIC_TAG
442
0
    }
443
0
    break;
444
0
445
5
  case ELF::EM_HEXAGON:
446
5
    switch (Type) {
447
5
#define HEXAGON_DYNAMIC_TAG(name, value) 
DYNAMIC_STRINGIFY_ENUM2
(name, value)
448
5
#include 
"llvm/BinaryFormat/DynamicTags.def"1
449
5
#undef HEXAGON_DYNAMIC_TAG
450
5
    }
451
5
    
break2
;
452
5
453
47
  case ELF::EM_MIPS:
454
47
    switch (Type) {
455
47
#define MIPS_DYNAMIC_TAG(name, value) 
DYNAMIC_STRINGIFY_ENUM44
(name, value)
456
47
#include 
"llvm/BinaryFormat/DynamicTags.def"1
457
47
#undef MIPS_DYNAMIC_TAG
458
47
    }
459
47
    
break2
;
460
47
461
47
  case ELF::EM_PPC64:
462
3
    switch (Type) {
463
3
#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
464
3
#include "llvm/BinaryFormat/DynamicTags.def"
465
3
#undef PPC64_DYNAMIC_TAG
466
3
    }
467
3
    
break2
;
468
222
  }
469
222
#undef DYNAMIC_TAG
470
222
  switch (Type) {
471
222
// Now handle all dynamic tags except the architecture specific ones
472
222
#define AARCH64_DYNAMIC_TAG(name, value)
473
222
#define MIPS_DYNAMIC_TAG(name, value)
474
222
#define HEXAGON_DYNAMIC_TAG(name, value)
475
222
#define PPC64_DYNAMIC_TAG(name, value)
476
222
// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
477
222
#define DYNAMIC_TAG_MARKER(name, value)
478
222
#define DYNAMIC_TAG(name, value) 
DYNAMIC_STRINGIFY_ENUM217
(name, value)
479
222
#include "llvm/BinaryFormat/DynamicTags.def"
480
222
#undef DYNAMIC_TAG
481
222
#undef AARCH64_DYNAMIC_TAG
482
222
#undef MIPS_DYNAMIC_TAG
483
222
#undef HEXAGON_DYNAMIC_TAG
484
222
#undef PPC64_DYNAMIC_TAG
485
222
#undef DYNAMIC_TAG_MARKER
486
222
#undef DYNAMIC_STRINGIFY_ENUM
487
222
  default:
488
5
    return "<unknown:>0x" + utohexstr(Type, true);
489
222
  }
490
222
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDynamicTagAsString(unsigned int, unsigned long long) const
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDynamicTagAsString(unsigned int, unsigned long long) const
Line
Count
Source
430
3
                                                 uint64_t Type) const {
431
3
#define DYNAMIC_STRINGIFY_ENUM(tag, value)                                     \
432
3
  case value:                                                                  \
433
3
    return #tag;
434
3
435
3
#define DYNAMIC_TAG(n, v)
436
3
  switch (Arch) {
437
3
  case ELF::EM_AARCH64:
438
0
    switch (Type) {
439
0
#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
440
0
#include "llvm/BinaryFormat/DynamicTags.def"
441
0
#undef AARCH64_DYNAMIC_TAG
442
0
    }
443
0
    break;
444
0
445
0
  case ELF::EM_HEXAGON:
446
0
    switch (Type) {
447
0
#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
448
0
#include "llvm/BinaryFormat/DynamicTags.def"
449
0
#undef HEXAGON_DYNAMIC_TAG
450
0
    }
451
0
    break;
452
0
453
0
  case ELF::EM_MIPS:
454
0
    switch (Type) {
455
0
#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
456
0
#include "llvm/BinaryFormat/DynamicTags.def"
457
0
#undef MIPS_DYNAMIC_TAG
458
0
    }
459
0
    break;
460
0
461
0
  case ELF::EM_PPC64:
462
0
    switch (Type) {
463
0
#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
464
0
#include "llvm/BinaryFormat/DynamicTags.def"
465
0
#undef PPC64_DYNAMIC_TAG
466
0
    }
467
0
    break;
468
3
  }
469
3
#undef DYNAMIC_TAG
470
3
  switch (Type) {
471
3
// Now handle all dynamic tags except the architecture specific ones
472
3
#define AARCH64_DYNAMIC_TAG(name, value)
473
3
#define MIPS_DYNAMIC_TAG(name, value)
474
3
#define HEXAGON_DYNAMIC_TAG(name, value)
475
3
#define PPC64_DYNAMIC_TAG(name, value)
476
3
// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
477
3
#define DYNAMIC_TAG_MARKER(name, value)
478
3
#define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
479
3
#include "llvm/BinaryFormat/DynamicTags.def"
480
3
#undef DYNAMIC_TAG
481
3
#undef AARCH64_DYNAMIC_TAG
482
3
#undef MIPS_DYNAMIC_TAG
483
3
#undef HEXAGON_DYNAMIC_TAG
484
3
#undef PPC64_DYNAMIC_TAG
485
3
#undef DYNAMIC_TAG_MARKER
486
3
#undef DYNAMIC_STRINGIFY_ENUM
487
3
  default:
488
1
    return "<unknown:>0x" + utohexstr(Type, true);
489
3
  }
490
3
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDynamicTagAsString(unsigned int, unsigned long long) const
Line
Count
Source
430
268
                                                 uint64_t Type) const {
431
268
#define DYNAMIC_STRINGIFY_ENUM(tag, value)                                     \
432
268
  case value:                                                                  \
433
268
    return #tag;
434
268
435
268
#define DYNAMIC_TAG(n, v)
436
268
  switch (Arch) {
437
268
  case ELF::EM_AARCH64:
438
0
    switch (Type) {
439
0
#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
440
0
#include "llvm/BinaryFormat/DynamicTags.def"
441
0
#undef AARCH64_DYNAMIC_TAG
442
0
    }
443
0
    break;
444
0
445
5
  case ELF::EM_HEXAGON:
446
5
    switch (Type) {
447
5
#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
448
5
#include 
"llvm/BinaryFormat/DynamicTags.def"1
449
5
#undef HEXAGON_DYNAMIC_TAG
450
5
    }
451
5
    
break2
;
452
5
453
47
  case ELF::EM_MIPS:
454
47
    switch (Type) {
455
47
#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
456
47
#include 
"llvm/BinaryFormat/DynamicTags.def"1
457
47
#undef MIPS_DYNAMIC_TAG
458
47
    }
459
47
    
break2
;
460
47
461
47
  case ELF::EM_PPC64:
462
3
    switch (Type) {
463
3
#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
464
3
#include "llvm/BinaryFormat/DynamicTags.def"
465
3
#undef PPC64_DYNAMIC_TAG
466
3
    }
467
3
    
break2
;
468
219
  }
469
219
#undef DYNAMIC_TAG
470
219
  switch (Type) {
471
219
// Now handle all dynamic tags except the architecture specific ones
472
219
#define AARCH64_DYNAMIC_TAG(name, value)
473
219
#define MIPS_DYNAMIC_TAG(name, value)
474
219
#define HEXAGON_DYNAMIC_TAG(name, value)
475
219
#define PPC64_DYNAMIC_TAG(name, value)
476
219
// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
477
219
#define DYNAMIC_TAG_MARKER(name, value)
478
219
#define DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
479
219
#include "llvm/BinaryFormat/DynamicTags.def"
480
219
#undef DYNAMIC_TAG
481
219
#undef AARCH64_DYNAMIC_TAG
482
219
#undef MIPS_DYNAMIC_TAG
483
219
#undef HEXAGON_DYNAMIC_TAG
484
219
#undef PPC64_DYNAMIC_TAG
485
219
#undef DYNAMIC_TAG_MARKER
486
219
#undef DYNAMIC_STRINGIFY_ENUM
487
219
  default:
488
4
    return "<unknown:>0x" + utohexstr(Type, true);
489
219
  }
490
219
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDynamicTagAsString(unsigned int, unsigned long long) const
491
492
template <class ELFT>
493
271
std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
494
271
  return getDynamicTagAsString(getHeader()->e_machine, Type);
495
271
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::getDynamicTagAsString(unsigned long long) const
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::getDynamicTagAsString(unsigned long long) const
Line
Count
Source
493
3
std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
494
3
  return getDynamicTagAsString(getHeader()->e_machine, Type);
495
3
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::getDynamicTagAsString(unsigned long long) const
Line
Count
Source
493
268
std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
494
268
  return getDynamicTagAsString(getHeader()->e_machine, Type);
495
268
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::getDynamicTagAsString(unsigned long long) const
496
497
template <class ELFT>
498
63
Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
499
63
  ArrayRef<Elf_Dyn> Dyn;
500
63
  size_t DynSecSize = 0;
501
63
502
63
  auto ProgramHeadersOrError = program_headers();
503
63
  if (!ProgramHeadersOrError)
504
0
    return ProgramHeadersOrError.takeError();
505
63
506
180
  
for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)63
{
507
180
    if (Phdr.p_type == ELF::PT_DYNAMIC) {
508
46
      Dyn = makeArrayRef(
509
46
          reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
510
46
          Phdr.p_filesz / sizeof(Elf_Dyn));
511
46
      DynSecSize = Phdr.p_filesz;
512
46
      break;
513
46
    }
514
180
  }
515
63
516
63
  // If we can't find the dynamic section in the program headers, we just fall
517
63
  // back on the sections.
518
63
  if (Dyn.empty()) {
519
19
    auto SectionsOrError = sections();
520
19
    if (!SectionsOrError)
521
0
      return SectionsOrError.takeError();
522
19
523
109
    
for (const Elf_Shdr &Sec : *SectionsOrError)19
{
524
109
      if (Sec.sh_type == ELF::SHT_DYNAMIC) {
525
3
        Expected<ArrayRef<Elf_Dyn>> DynOrError =
526
3
            getSectionContentsAsArray<Elf_Dyn>(&Sec);
527
3
        if (!DynOrError)
528
0
          return DynOrError.takeError();
529
3
        Dyn = *DynOrError;
530
3
        DynSecSize = Sec.sh_size;
531
3
        break;
532
3
      }
533
109
    }
534
19
535
19
    if (!Dyn.data())
536
16
      return ArrayRef<Elf_Dyn>();
537
47
  }
538
47
539
47
  if (Dyn.empty())
540
0
    // TODO: this error is untested.
541
0
    return createError("invalid empty dynamic section");
542
47
543
47
  if (DynSecSize % sizeof(Elf_Dyn) != 0)
544
0
    // TODO: this error is untested.
545
0
    return createError("malformed dynamic section");
546
47
547
47
  if (Dyn.back().d_tag != ELF::DT_NULL)
548
0
    // TODO: this error is untested.
549
0
    return createError("dynamic sections must be DT_NULL terminated");
550
47
551
47
  return Dyn;
552
47
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::dynamicEntries() const
Line
Count
Source
498
1
Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
499
1
  ArrayRef<Elf_Dyn> Dyn;
500
1
  size_t DynSecSize = 0;
501
1
502
1
  auto ProgramHeadersOrError = program_headers();
503
1
  if (!ProgramHeadersOrError)
504
0
    return ProgramHeadersOrError.takeError();
505
1
506
2
  
for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)1
{
507
2
    if (Phdr.p_type == ELF::PT_DYNAMIC) {
508
0
      Dyn = makeArrayRef(
509
0
          reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
510
0
          Phdr.p_filesz / sizeof(Elf_Dyn));
511
0
      DynSecSize = Phdr.p_filesz;
512
0
      break;
513
0
    }
514
2
  }
515
1
516
1
  // If we can't find the dynamic section in the program headers, we just fall
517
1
  // back on the sections.
518
1
  if (Dyn.empty()) {
519
1
    auto SectionsOrError = sections();
520
1
    if (!SectionsOrError)
521
0
      return SectionsOrError.takeError();
522
1
523
7
    
for (const Elf_Shdr &Sec : *SectionsOrError)1
{
524
7
      if (Sec.sh_type == ELF::SHT_DYNAMIC) {
525
0
        Expected<ArrayRef<Elf_Dyn>> DynOrError =
526
0
            getSectionContentsAsArray<Elf_Dyn>(&Sec);
527
0
        if (!DynOrError)
528
0
          return DynOrError.takeError();
529
0
        Dyn = *DynOrError;
530
0
        DynSecSize = Sec.sh_size;
531
0
        break;
532
0
      }
533
7
    }
534
1
535
1
    if (!Dyn.data())
536
1
      return ArrayRef<Elf_Dyn>();
537
0
  }
538
0
539
0
  if (Dyn.empty())
540
0
    // TODO: this error is untested.
541
0
    return createError("invalid empty dynamic section");
542
0
543
0
  if (DynSecSize % sizeof(Elf_Dyn) != 0)
544
0
    // TODO: this error is untested.
545
0
    return createError("malformed dynamic section");
546
0
547
0
  if (Dyn.back().d_tag != ELF::DT_NULL)
548
0
    // TODO: this error is untested.
549
0
    return createError("dynamic sections must be DT_NULL terminated");
550
0
551
0
  return Dyn;
552
0
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::dynamicEntries() const
Line
Count
Source
498
1
Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
499
1
  ArrayRef<Elf_Dyn> Dyn;
500
1
  size_t DynSecSize = 0;
501
1
502
1
  auto ProgramHeadersOrError = program_headers();
503
1
  if (!ProgramHeadersOrError)
504
0
    return ProgramHeadersOrError.takeError();
505
1
506
2
  
for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)1
{
507
2
    if (Phdr.p_type == ELF::PT_DYNAMIC) {
508
1
      Dyn = makeArrayRef(
509
1
          reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
510
1
          Phdr.p_filesz / sizeof(Elf_Dyn));
511
1
      DynSecSize = Phdr.p_filesz;
512
1
      break;
513
1
    }
514
2
  }
515
1
516
1
  // If we can't find the dynamic section in the program headers, we just fall
517
1
  // back on the sections.
518
1
  if (Dyn.empty()) {
519
0
    auto SectionsOrError = sections();
520
0
    if (!SectionsOrError)
521
0
      return SectionsOrError.takeError();
522
0
523
0
    for (const Elf_Shdr &Sec : *SectionsOrError) {
524
0
      if (Sec.sh_type == ELF::SHT_DYNAMIC) {
525
0
        Expected<ArrayRef<Elf_Dyn>> DynOrError =
526
0
            getSectionContentsAsArray<Elf_Dyn>(&Sec);
527
0
        if (!DynOrError)
528
0
          return DynOrError.takeError();
529
0
        Dyn = *DynOrError;
530
0
        DynSecSize = Sec.sh_size;
531
0
        break;
532
0
      }
533
0
    }
534
0
535
0
    if (!Dyn.data())
536
0
      return ArrayRef<Elf_Dyn>();
537
1
  }
538
1
539
1
  if (Dyn.empty())
540
0
    // TODO: this error is untested.
541
0
    return createError("invalid empty dynamic section");
542
1
543
1
  if (DynSecSize % sizeof(Elf_Dyn) != 0)
544
0
    // TODO: this error is untested.
545
0
    return createError("malformed dynamic section");
546
1
547
1
  if (Dyn.back().d_tag != ELF::DT_NULL)
548
0
    // TODO: this error is untested.
549
0
    return createError("dynamic sections must be DT_NULL terminated");
550
1
551
1
  return Dyn;
552
1
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::dynamicEntries() const
Line
Count
Source
498
61
Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
499
61
  ArrayRef<Elf_Dyn> Dyn;
500
61
  size_t DynSecSize = 0;
501
61
502
61
  auto ProgramHeadersOrError = program_headers();
503
61
  if (!ProgramHeadersOrError)
504
0
    return ProgramHeadersOrError.takeError();
505
61
506
176
  
for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)61
{
507
176
    if (Phdr.p_type == ELF::PT_DYNAMIC) {
508
45
      Dyn = makeArrayRef(
509
45
          reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
510
45
          Phdr.p_filesz / sizeof(Elf_Dyn));
511
45
      DynSecSize = Phdr.p_filesz;
512
45
      break;
513
45
    }
514
176
  }
515
61
516
61
  // If we can't find the dynamic section in the program headers, we just fall
517
61
  // back on the sections.
518
61
  if (Dyn.empty()) {
519
18
    auto SectionsOrError = sections();
520
18
    if (!SectionsOrError)
521
0
      return SectionsOrError.takeError();
522
18
523
102
    
for (const Elf_Shdr &Sec : *SectionsOrError)18
{
524
102
      if (Sec.sh_type == ELF::SHT_DYNAMIC) {
525
3
        Expected<ArrayRef<Elf_Dyn>> DynOrError =
526
3
            getSectionContentsAsArray<Elf_Dyn>(&Sec);
527
3
        if (!DynOrError)
528
0
          return DynOrError.takeError();
529
3
        Dyn = *DynOrError;
530
3
        DynSecSize = Sec.sh_size;
531
3
        break;
532
3
      }
533
102
    }
534
18
535
18
    if (!Dyn.data())
536
15
      return ArrayRef<Elf_Dyn>();
537
46
  }
538
46
539
46
  if (Dyn.empty())
540
0
    // TODO: this error is untested.
541
0
    return createError("invalid empty dynamic section");
542
46
543
46
  if (DynSecSize % sizeof(Elf_Dyn) != 0)
544
0
    // TODO: this error is untested.
545
0
    return createError("malformed dynamic section");
546
46
547
46
  if (Dyn.back().d_tag != ELF::DT_NULL)
548
0
    // TODO: this error is untested.
549
0
    return createError("dynamic sections must be DT_NULL terminated");
550
46
551
46
  return Dyn;
552
46
}
Unexecuted instantiation: llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::dynamicEntries() const
553
554
template <class ELFT>
555
3.64k
Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
556
3.64k
  auto ProgramHeadersOrError = program_headers();
557
3.64k
  if (!ProgramHeadersOrError)
558
0
    return ProgramHeadersOrError.takeError();
559
3.64k
560
3.64k
  llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
561
3.64k
562
3.64k
  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
563
25.9k
    if (Phdr.p_type == ELF::PT_LOAD)
564
11.0k
      LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
565
3.64k
566
3.64k
  const Elf_Phdr *const *I =
567
3.64k
      std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
568
8.36k
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
8.36k
                         return VAddr < Phdr->p_vaddr;
570
8.36k
                       });
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::toMappedAddr(unsigned long long) const::'lambda'(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*)::operator()(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, false> > const*) const
Line
Count
Source
568
1.31k
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
1.31k
                         return VAddr < Phdr->p_vaddr;
570
1.31k
                       });
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::toMappedAddr(unsigned long long) const::'lambda'(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*)::operator()(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, false> > const*) const
Line
Count
Source
568
558
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
558
                         return VAddr < Phdr->p_vaddr;
570
558
                       });
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::toMappedAddr(unsigned long long) const::'lambda'(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*)::operator()(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)1, true> > const*) const
Line
Count
Source
568
6.21k
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
6.21k
                         return VAddr < Phdr->p_vaddr;
570
6.21k
                       });
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::toMappedAddr(unsigned long long) const::'lambda'(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*)::operator()(unsigned long long, llvm::object::Elf_Phdr_Impl<llvm::object::ELFType<(llvm::support::endianness)0, true> > const*) const
Line
Count
Source
568
267
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
267
                         return VAddr < Phdr->p_vaddr;
570
267
                       });
571
3.64k
572
3.64k
  if (I == LoadSegments.begin())
573
13
    return createError("virtual address is not in any segment: 0x" +
574
13
                       Twine::utohexstr(VAddr));
575
3.63k
  --I;
576
3.63k
  const Elf_Phdr &Phdr = **I;
577
3.63k
  uint64_t Delta = VAddr - Phdr.p_vaddr;
578
3.63k
  if (Delta >= Phdr.p_filesz)
579
7
    return createError("virtual address is not in any segment: 0x" +
580
7
                       Twine::utohexstr(VAddr));
581
3.62k
  return base() + Phdr.p_offset + Delta;
582
3.62k
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, false> >::toMappedAddr(unsigned long long) const
Line
Count
Source
555
567
Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
556
567
  auto ProgramHeadersOrError = program_headers();
557
567
  if (!ProgramHeadersOrError)
558
0
    return ProgramHeadersOrError.takeError();
559
567
560
567
  llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
561
567
562
567
  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
563
4.18k
    if (Phdr.p_type == ELF::PT_LOAD)
564
1.79k
      LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
565
567
566
567
  const Elf_Phdr *const *I =
567
567
      std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
568
567
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
567
                         return VAddr < Phdr->p_vaddr;
570
567
                       });
571
567
572
567
  if (I == LoadSegments.begin())
573
0
    return createError("virtual address is not in any segment: 0x" +
574
0
                       Twine::utohexstr(VAddr));
575
567
  --I;
576
567
  const Elf_Phdr &Phdr = **I;
577
567
  uint64_t Delta = VAddr - Phdr.p_vaddr;
578
567
  if (Delta >= Phdr.p_filesz)
579
0
    return createError("virtual address is not in any segment: 0x" +
580
0
                       Twine::utohexstr(VAddr));
581
567
  return base() + Phdr.p_offset + Delta;
582
567
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, false> >::toMappedAddr(unsigned long long) const
Line
Count
Source
555
235
Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
556
235
  auto ProgramHeadersOrError = program_headers();
557
235
  if (!ProgramHeadersOrError)
558
0
    return ProgramHeadersOrError.takeError();
559
235
560
235
  llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
561
235
562
235
  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
563
1.87k
    if (Phdr.p_type == ELF::PT_LOAD)
564
774
      LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
565
235
566
235
  const Elf_Phdr *const *I =
567
235
      std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
568
235
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
235
                         return VAddr < Phdr->p_vaddr;
570
235
                       });
571
235
572
235
  if (I == LoadSegments.begin())
573
0
    return createError("virtual address is not in any segment: 0x" +
574
0
                       Twine::utohexstr(VAddr));
575
235
  --I;
576
235
  const Elf_Phdr &Phdr = **I;
577
235
  uint64_t Delta = VAddr - Phdr.p_vaddr;
578
235
  if (Delta >= Phdr.p_filesz)
579
0
    return createError("virtual address is not in any segment: 0x" +
580
0
                       Twine::utohexstr(VAddr));
581
235
  return base() + Phdr.p_offset + Delta;
582
235
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)1, true> >::toMappedAddr(unsigned long long) const
Line
Count
Source
555
2.73k
Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
556
2.73k
  auto ProgramHeadersOrError = program_headers();
557
2.73k
  if (!ProgramHeadersOrError)
558
0
    return ProgramHeadersOrError.takeError();
559
2.73k
560
2.73k
  llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
561
2.73k
562
2.73k
  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
563
19.0k
    if (Phdr.p_type == ELF::PT_LOAD)
564
8.13k
      LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
565
2.73k
566
2.73k
  const Elf_Phdr *const *I =
567
2.73k
      std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
568
2.73k
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
2.73k
                         return VAddr < Phdr->p_vaddr;
570
2.73k
                       });
571
2.73k
572
2.73k
  if (I == LoadSegments.begin())
573
13
    return createError("virtual address is not in any segment: 0x" +
574
13
                       Twine::utohexstr(VAddr));
575
2.71k
  --I;
576
2.71k
  const Elf_Phdr &Phdr = **I;
577
2.71k
  uint64_t Delta = VAddr - Phdr.p_vaddr;
578
2.71k
  if (Delta >= Phdr.p_filesz)
579
7
    return createError("virtual address is not in any segment: 0x" +
580
7
                       Twine::utohexstr(VAddr));
581
2.71k
  return base() + Phdr.p_offset + Delta;
582
2.71k
}
llvm::object::ELFFile<llvm::object::ELFType<(llvm::support::endianness)0, true> >::toMappedAddr(unsigned long long) const
Line
Count
Source
555
115
Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
556
115
  auto ProgramHeadersOrError = program_headers();
557
115
  if (!ProgramHeadersOrError)
558
0
    return ProgramHeadersOrError.takeError();
559
115
560
115
  llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
561
115
562
115
  for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
563
911
    if (Phdr.p_type == ELF::PT_LOAD)
564
366
      LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
565
115
566
115
  const Elf_Phdr *const *I =
567
115
      std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
568
115
                       [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
569
115
                         return VAddr < Phdr->p_vaddr;
570
115
                       });
571
115
572
115
  if (I == LoadSegments.begin())
573
0
    return createError("virtual address is not in any segment: 0x" +
574
0
                       Twine::utohexstr(VAddr));
575
115
  --I;
576
115
  const Elf_Phdr &Phdr = **I;
577
115
  uint64_t Delta = VAddr - Phdr.p_vaddr;
578
115
  if (Delta >= Phdr.p_filesz)
579
0
    return createError("virtual address is not in any segment: 0x" +
580
0
                       Twine::utohexstr(VAddr));
581
115
  return base() + Phdr.p_offset + Delta;
582
115
}
583
584
template class llvm::object::ELFFile<ELF32LE>;
585
template class llvm::object::ELFFile<ELF32BE>;
586
template class llvm::object::ELFFile<ELF64LE>;
587
template class llvm::object::ELFFile<ELF64BE>;