Coverage Report

Created: 2019-07-24 05:18

/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
278
  EhReader(InputSectionBase *s, ArrayRef<uint8_t> d) : isec(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
          isec->getObjMsg((const uint8_t *)loc - isec->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 *isec;
56
  ArrayRef<uint8_t> d;
57
};
58
}
59
60
248
size_t elf::readEhRecordSize(InputSectionBase *s, size_t off) {
61
248
  return EhReader(s, s->data().slice(off)).readEhRecordSize();
62
248
}
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
248
size_t EhReader::readEhRecordSize() {
67
248
  if (d.size() < 4)
68
0
    failOn(d.data(), "CIE/FDE too small");
69
248
70
248
  // First 4 bytes of CIE/FDE is the size of the record.
71
248
  // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
72
248
  // but we do not support that format yet.
73
248
  uint64_t v = read32(d.data());
74
248
  if (v == UINT32_MAX)
75
248
    
failOn(d.data(), "CIE/FDE too large")0
;
76
248
  uint64_t size = v + 4;
77
248
  if (size > d.size())
78
0
    failOn(d.data(), "CIE/FDE ends past the end of the section");
79
248
  return size;
80
248
}
81
82
// Read a byte and advance D by one byte.
83
93
uint8_t EhReader::readByte() {
84
93
  if (d.empty())
85
0
    failOn(d.data(), "unexpected end of CIE");
86
93
  uint8_t b = d.front();
87
93
  d = d.slice(1);
88
93
  return b;
89
93
}
90
91
30
void EhReader::skipBytes(size_t count) {
92
30
  if (d.size() < count)
93
0
    failOn(d.data(), "CIE is too small");
94
30
  d = d.slice(count);
95
30
}
96
97
// Read a null-terminated string.
98
30
StringRef EhReader::readString() {
99
30
  const uint8_t *end = llvm::find(d, '\0');
100
30
  if (end == d.end())
101
0
    failOn(d.data(), "corrupted CIE (failed to read string)");
102
30
  StringRef s = toStringRef(d.slice(0, end - d.begin()));
103
30
  d = d.slice(s.size() + 1);
104
30
  return s;
105
30
}
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
80
void EhReader::skipLeb128() {
112
80
  const uint8_t *errPos = d.data();
113
80
  while (!d.empty()) {
114
80
    uint8_t val = d.front();
115
80
    d = d.slice(1);
116
80
    if ((val & 0x80) == 0)
117
80
      return;
118
80
  }
119
80
  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
30
uint8_t elf::getFdeEncoding(EhSectionPiece *p) {
153
30
  return EhReader(p->sec, p->data()).getFdeEncoding();
154
30
}
155
156
30
uint8_t EhReader::getFdeEncoding() {
157
30
  skipBytes(8);
158
30
  int version = readByte();
159
30
  if (version != 1 && 
version != 30
)
160
0
    failOn(d.data() - 1,
161
0
           "FDE version 1 or 3 expected, but got " + Twine(version));
162
30
163
30
  StringRef aug = readString();
164
30
165
30
  // Skip code and data alignment factors.
166
30
  skipLeb128();
167
30
  skipLeb128();
168
30
169
30
  // Skip the return address register. In CIE version 1 this is a single
170
30
  // byte. In CIE version 3 this is an unsigned LEB128.
171
30
  if (version == 1)
172
30
    readByte();
173
0
  else
174
0
    skipLeb128();
175
30
176
30
  // We only care about an 'R' value, but other records may precede an 'R'
177
30
  // record. Unfortunately records are not in TLV (type-length-value) format,
178
30
  // so we need to teach the linker how to skip records for each type.
179
53
  for (char c : aug) {
180
53
    if (c == 'R')
181
23
      return readByte();
182
30
    if (c == 'z') {
183
20
      skipLeb128();
184
20
      continue;
185
20
    }
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
30
  
return DW_EH_PE_absptr7
;
197
30
}