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 "InputFiles.h"
21#include "InputSection.h"
22#include "Relocations.h"
23#include "Symbols.h"
24#include "Target.h"
25#include "llvm/BinaryFormat/Dwarf.h"
26
27using namespace llvm;
28using namespace llvm::ELF;
29using namespace llvm::dwarf;
30using namespace llvm::object;
31using namespace lld;
32using namespace lld::elf;
33
34namespace {
35class EhReader {
36public:
37 EhReader(const InputSectionBase *s, ArrayRef<uint8_t> d) : isec(s), d(d) {}
38 uint8_t getFdeEncoding();
39 bool hasLSDA();
40
41private:
42 template <class P> void errOn(const P *loc, const Twine &msg) {
43 Ctx &ctx = isec->file->ctx;
44 Err(ctx) << "corrupted .eh_frame: " << msg << "\n>>> defined in "
45 << isec->getObjMsg(offset: (const uint8_t *)loc - isec->content().data());
46 }
47
48 uint8_t readByte();
49 void skipBytes(size_t count);
50 StringRef readString();
51 void skipLeb128();
52 void skipAugP();
53 StringRef getAugmentation();
54
55 const InputSectionBase *isec;
56 ArrayRef<uint8_t> d;
57};
58}
59
60// Read a byte and advance D by one byte.
61uint8_t EhReader::readByte() {
62 if (d.empty()) {
63 errOn(loc: d.data(), msg: "unexpected end of CIE");
64 return 0;
65 }
66 uint8_t b = d.front();
67 d = d.slice(N: 1);
68 return b;
69}
70
71void EhReader::skipBytes(size_t count) {
72 if (d.size() < count)
73 errOn(loc: d.data(), msg: "CIE is too small");
74 else
75 d = d.slice(N: count);
76}
77
78// Read a null-terminated string.
79StringRef EhReader::readString() {
80 const uint8_t *end = llvm::find(Range&: d, Val: '\0');
81 if (end == d.end()) {
82 errOn(loc: d.data(), msg: "corrupted CIE (failed to read string)");
83 return {};
84 }
85 StringRef s = toStringRef(Input: d.slice(N: 0, M: end - d.begin()));
86 d = d.slice(N: s.size() + 1);
87 return s;
88}
89
90// Skip an integer encoded in the LEB128 format.
91// Actual number is not of interest because only the runtime needs it.
92// But we need to be at least able to skip it so that we can read
93// the field that follows a LEB128 number.
94void EhReader::skipLeb128() {
95 const uint8_t *errPos = d.data();
96 while (!d.empty()) {
97 uint8_t val = d.front();
98 d = d.slice(N: 1);
99 if ((val & 0x80) == 0)
100 return;
101 }
102 errOn(loc: errPos, msg: "corrupted CIE (failed to read LEB128)");
103}
104
105static size_t getAugPSize(Ctx &ctx, unsigned enc) {
106 switch (enc & 0x0f) {
107 case DW_EH_PE_absptr:
108 case DW_EH_PE_signed:
109 return ctx.arg.wordsize;
110 case DW_EH_PE_udata2:
111 case DW_EH_PE_sdata2:
112 return 2;
113 case DW_EH_PE_udata4:
114 case DW_EH_PE_sdata4:
115 return 4;
116 case DW_EH_PE_udata8:
117 case DW_EH_PE_sdata8:
118 return 8;
119 }
120 return 0;
121}
122
123void EhReader::skipAugP() {
124 uint8_t enc = readByte();
125 if ((enc & 0xf0) == DW_EH_PE_aligned)
126 return errOn(loc: d.data() - 1, msg: "DW_EH_PE_aligned encoding is not supported");
127 size_t size = getAugPSize(ctx&: isec->getCtx(), enc);
128 if (size == 0)
129 return errOn(loc: d.data() - 1, msg: "unknown FDE encoding");
130 if (size >= d.size())
131 return errOn(loc: d.data() - 1, msg: "corrupted CIE");
132 d = d.slice(N: size);
133}
134
135uint8_t elf::getFdeEncoding(const EhInputSection &sec,
136 const EhSectionPiece &p) {
137 return EhReader(&sec, p.data(sec)).getFdeEncoding();
138}
139
140bool elf::hasLSDA(const EhInputSection &sec, const EhSectionPiece &p) {
141 return EhReader(&sec, p.data(sec)).hasLSDA();
142}
143
144// pc_range follows the 4-byte length, 4-byte CIE pointer, and pc_begin, and
145// uses pc_begin's encoding.
146bool elf::hasZeroPcRange(const EhInputSection &sec, const EhSectionPiece &p,
147 uint8_t enc) {
148 Ctx &ctx = sec.getCtx();
149 size_t pSize = getAugPSize(ctx, enc);
150 ArrayRef<uint8_t> d = p.data(sec);
151 if (pSize == 0 || d.size() < 8 + 2 * pSize ||
152 !llvm::all_of(Range: d.slice(N: 8 + pSize, M: pSize), P: [](uint8_t c) { return !c; }))
153 return false;
154 // With linker relaxation, pc_range is computed by an ADD/SUB relocation pair,
155 // which GNU as emits even for an empty function. The range is zero if both
156 // relocations resolve to the same address.
157 if (ctx.arg.emachine == EM_LOONGARCH || ctx.arg.emachine == EM_RISCV) {
158 uint64_t off = p.inputOff + 8 + pSize;
159 SmallVector<uint64_t, 2> vals;
160 for (size_t i = p.firstRelocation;
161 i < sec.rels.size() && sec.rels[i].offset < off + pSize; ++i)
162 if (const Relocation &r = sec.rels[i]; r.offset >= off)
163 vals.push_back(Elt: r.sym->getVA(ctx, addend: r.addend));
164 return vals.empty() || (vals.size() == 2 && vals[0] == vals[1]);
165 }
166 return true;
167}
168
169StringRef EhReader::getAugmentation() {
170 skipBytes(count: 8);
171 int version = readByte();
172 if (version != 1 && version != 3) {
173 errOn(loc: d.data() - 1,
174 msg: "FDE version 1 or 3 expected, but got " + Twine(version));
175 return {};
176 }
177
178 StringRef aug = readString();
179
180 // Skip code and data alignment factors.
181 skipLeb128();
182 skipLeb128();
183
184 // Skip the return address register. In CIE version 1 this is a single
185 // byte. In CIE version 3 this is an unsigned LEB128.
186 if (version == 1)
187 readByte();
188 else
189 skipLeb128();
190 return aug;
191}
192
193uint8_t EhReader::getFdeEncoding() {
194 // We only care about an 'R' value, but other records may precede an 'R'
195 // record. Unfortunately records are not in TLV (type-length-value) format,
196 // so we need to teach the linker how to skip records for each type.
197 StringRef aug = getAugmentation();
198 for (char c : aug) {
199 if (c == 'R')
200 return readByte();
201 if (c == 'z')
202 skipLeb128();
203 else if (c == 'L')
204 readByte();
205 else if (c == 'P')
206 skipAugP();
207 else if (c != 'B' && c != 'S' && c != 'G') {
208 errOn(loc: aug.data(), msg: "unknown .eh_frame augmentation string: " + aug);
209 break;
210 }
211 }
212 return DW_EH_PE_absptr;
213}
214
215bool EhReader::hasLSDA() {
216 StringRef aug = getAugmentation();
217 for (char c : aug) {
218 if (c == 'L')
219 return true;
220 if (c == 'z')
221 skipLeb128();
222 else if (c == 'P')
223 skipAugP();
224 else if (c == 'R')
225 readByte();
226 else if (c != 'B' && c != 'S' && c != 'G') {
227 errOn(loc: aug.data(), msg: "unknown .eh_frame augmentation string: " + aug);
228 break;
229 }
230 }
231 return false;
232}
233