Coverage Report

Created: 2018-01-17 21:32

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