Coverage Report

Created: 2019-05-19 14:56

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