Coverage Report

Created: 2018-10-20 06:24

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/EhFrame.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- EhFrame.cpp -------------------------------------------------------===//
2
//
3
//                             The LLVM Linker
4
//
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//
8
//===----------------------------------------------------------------------===//
9
//
10
// .eh_frame section contains information on how to unwind the stack when
11
// an exception is thrown. The section consists of sequence of CIE and FDE
12
// records. The linker needs to merge CIEs and associate FDEs to CIEs.
13
// That means the linker has to understand the format of the section.
14
//
15
// This file contains a few utility functions to read .eh_frame contents.
16
//
17
//===----------------------------------------------------------------------===//
18
19
#include "EhFrame.h"
20
#include "Config.h"
21
#include "InputSection.h"
22
#include "Relocations.h"
23
#include "Target.h"
24
#include "lld/Common/ErrorHandler.h"
25
#include "lld/Common/Strings.h"
26
#include "llvm/BinaryFormat/Dwarf.h"
27
#include "llvm/Object/ELF.h"
28
29
using namespace llvm;
30
using namespace llvm::ELF;
31
using namespace llvm::dwarf;
32
using namespace llvm::object;
33
34
using namespace lld;
35
using namespace lld::elf;
36
37
namespace {
38
class EhReader {
39
public:
40
265
  EhReader(InputSectionBase *S, ArrayRef<uint8_t> D) : IS(S), D(D) {}
41
  size_t readEhRecordSize();
42
  uint8_t getFdeEncoding();
43
44
private:
45
0
  template <class P> void failOn(const P *Loc, const Twine &Msg) {
46
0
    fatal("corrupted .eh_frame: " + Msg + "\n>>> defined in " +
47
0
          IS->getObjMsg((const uint8_t *)Loc - IS->data().data()));
48
0
  }
Unexecuted instantiation: EhFrame.cpp:void (anonymous namespace)::EhReader::failOn<unsigned char>(unsigned char const*, llvm::Twine const&)
Unexecuted instantiation: EhFrame.cpp:void (anonymous namespace)::EhReader::failOn<char>(char const*, llvm::Twine const&)
49
50
  uint8_t readByte();
51
  void skipBytes(size_t Count);
52
  StringRef readString();
53
  void skipLeb128();
54
  void skipAugP();
55
56
  InputSectionBase *IS;
57
  ArrayRef<uint8_t> D;
58
};
59
}
60
61
238
size_t elf::readEhRecordSize(InputSectionBase *S, size_t Off) {
62
238
  return EhReader(S, S->data().slice(Off)).readEhRecordSize();
63
238
}
64
65
// .eh_frame section is a sequence of records. Each record starts with
66
// a 4 byte length field. This function reads the length.
67
238
size_t EhReader::readEhRecordSize() {
68
238
  if (D.size() < 4)
69
0
    failOn(D.data(), "CIE/FDE too small");
70
238
71
238
  // First 4 bytes of CIE/FDE is the size of the record.
72
238
  // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
73
238
  // but we do not support that format yet.
74
238
  uint64_t V = read32(D.data());
75
238
  if (V == UINT32_MAX)
76
238
    
failOn(D.data(), "CIE/FDE too large")0
;
77
238
  uint64_t Size = V + 4;
78
238
  if (Size > D.size())
79
0
    failOn(D.data(), "CIE/FDE ends past the end of the section");
80
238
  return Size;
81
238
}
82
83
// Read a byte and advance D by one byte.
84
84
uint8_t EhReader::readByte() {
85
84
  if (D.empty())
86
0
    failOn(D.data(), "unexpected end of CIE");
87
84
  uint8_t B = D.front();
88
84
  D = D.slice(1);
89
84
  return B;
90
84
}
91
92
27
void EhReader::skipBytes(size_t Count) {
93
27
  if (D.size() < Count)
94
0
    failOn(D.data(), "CIE is too small");
95
27
  D = D.slice(Count);
96
27
}
97
98
// Read a null-terminated string.
99
27
StringRef EhReader::readString() {
100
27
  const uint8_t *End = std::find(D.begin(), D.end(), '\0');
101
27
  if (End == D.end())
102
0
    failOn(D.data(), "corrupted CIE (failed to read string)");
103
27
  StringRef S = toStringRef(D.slice(0, End - D.begin()));
104
27
  D = D.slice(S.size() + 1);
105
27
  return S;
106
27
}
107
108
// Skip an integer encoded in the LEB128 format.
109
// Actual number is not of interest because only the runtime needs it.
110
// But we need to be at least able to skip it so that we can read
111
// the field that follows a LEB128 number.
112
71
void EhReader::skipLeb128() {
113
71
  const uint8_t *ErrPos = D.data();
114
71
  while (!D.empty()) {
115
71
    uint8_t Val = D.front();
116
71
    D = D.slice(1);
117
71
    if ((Val & 0x80) == 0)
118
71
      return;
119
71
  }
120
71
  failOn(ErrPos, "corrupted CIE (failed to read LEB128)");
121
0
}
122
123
8
static size_t getAugPSize(unsigned Enc) {
124
8
  switch (Enc & 0x0f) {
125
8
  case DW_EH_PE_absptr:
126
2
  case DW_EH_PE_signed:
127
2
    return Config->Wordsize;
128
2
  case DW_EH_PE_udata2:
129
2
  case DW_EH_PE_sdata2:
130
2
    return 2;
131
2
  case DW_EH_PE_udata4:
132
2
  case DW_EH_PE_sdata4:
133
2
    return 4;
134
2
  case DW_EH_PE_udata8:
135
2
  case DW_EH_PE_sdata8:
136
2
    return 8;
137
0
  }
138
0
  return 0;
139
0
}
140
141
8
void EhReader::skipAugP() {
142
8
  uint8_t Enc = readByte();
143
8
  if ((Enc & 0xf0) == DW_EH_PE_aligned)
144
0
    failOn(D.data() - 1, "DW_EH_PE_aligned encoding is not supported");
145
8
  size_t Size = getAugPSize(Enc);
146
8
  if (Size == 0)
147
0
    failOn(D.data() - 1, "unknown FDE encoding");
148
8
  if (Size >= D.size())
149
0
    failOn(D.data() - 1, "corrupted CIE");
150
8
  D = D.slice(Size);
151
8
}
152
153
27
uint8_t elf::getFdeEncoding(EhSectionPiece *P) {
154
27
  return EhReader(P->Sec, P->data()).getFdeEncoding();
155
27
}
156
157
27
uint8_t EhReader::getFdeEncoding() {
158
27
  skipBytes(8);
159
27
  int Version = readByte();
160
27
  if (Version != 1 && 
Version != 30
)
161
0
    failOn(D.data() - 1,
162
0
           "FDE version 1 or 3 expected, but got " + Twine(Version));
163
27
164
27
  StringRef Aug = readString();
165
27
166
27
  // Skip code and data alignment factors.
167
27
  skipLeb128();
168
27
  skipLeb128();
169
27
170
27
  // Skip the return address register. In CIE version 1 this is a single
171
27
  // byte. In CIE version 3 this is an unsigned LEB128.
172
27
  if (Version == 1)
173
27
    readByte();
174
0
  else
175
0
    skipLeb128();
176
27
177
27
  // We only care about an 'R' value, but other records may precede an 'R'
178
27
  // record. Unfortunately records are not in TLV (type-length-value) format,
179
27
  // so we need to teach the linker how to skip records for each type.
180
47
  for (char C : Aug) {
181
47
    if (C == 'R')
182
20
      return readByte();
183
27
    if (C == 'z') {
184
17
      skipLeb128();
185
17
      continue;
186
17
    }
187
10
    if (C == 'P') {
188
8
      skipAugP();
189
8
      continue;
190
8
    }
191
2
    if (C == 'L') {
192
2
      readByte();
193
2
      continue;
194
2
    }
195
0
    failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug);
196
0
  }
197
27
  
return DW_EH_PE_absptr7
;
198
27
}