xref: /freebsd/contrib/llvm-project/lld/ELF/EhFrame.cpp (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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 using namespace lld;
33 using namespace lld::elf;
34 
35 namespace {
36 class EhReader {
37 public:
38   EhReader(InputSectionBase *s, ArrayRef<uint8_t> d) : isec(s), d(d) {}
39   size_t readEhRecordSize();
40   uint8_t getFdeEncoding();
41   bool hasLSDA();
42 
43 private:
44   template <class P> void failOn(const P *loc, const Twine &msg) {
45     fatal("corrupted .eh_frame: " + msg + "\n>>> defined in " +
46           isec->getObjMsg((const uint8_t *)loc - isec->data().data()));
47   }
48 
49   uint8_t readByte();
50   void skipBytes(size_t count);
51   StringRef readString();
52   void skipLeb128();
53   void skipAugP();
54   StringRef getAugmentation();
55 
56   InputSectionBase *isec;
57   ArrayRef<uint8_t> d;
58 };
59 }
60 
61 size_t elf::readEhRecordSize(InputSectionBase *s, size_t off) {
62   return EhReader(s, s->data().slice(off)).readEhRecordSize();
63 }
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 size_t EhReader::readEhRecordSize() {
68   if (d.size() < 4)
69     failOn(d.data(), "CIE/FDE too small");
70 
71   // First 4 bytes of CIE/FDE is the size of the record.
72   // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
73   // but we do not support that format yet.
74   uint64_t v = read32(d.data());
75   if (v == UINT32_MAX)
76     failOn(d.data(), "CIE/FDE too large");
77   uint64_t size = v + 4;
78   if (size > d.size())
79     failOn(d.data(), "CIE/FDE ends past the end of the section");
80   return size;
81 }
82 
83 // Read a byte and advance D by one byte.
84 uint8_t EhReader::readByte() {
85   if (d.empty())
86     failOn(d.data(), "unexpected end of CIE");
87   uint8_t b = d.front();
88   d = d.slice(1);
89   return b;
90 }
91 
92 void EhReader::skipBytes(size_t count) {
93   if (d.size() < count)
94     failOn(d.data(), "CIE is too small");
95   d = d.slice(count);
96 }
97 
98 // Read a null-terminated string.
99 StringRef EhReader::readString() {
100   const uint8_t *end = llvm::find(d, '\0');
101   if (end == d.end())
102     failOn(d.data(), "corrupted CIE (failed to read string)");
103   StringRef s = toStringRef(d.slice(0, end - d.begin()));
104   d = d.slice(s.size() + 1);
105   return s;
106 }
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 void EhReader::skipLeb128() {
113   const uint8_t *errPos = d.data();
114   while (!d.empty()) {
115     uint8_t val = d.front();
116     d = d.slice(1);
117     if ((val & 0x80) == 0)
118       return;
119   }
120   failOn(errPos, "corrupted CIE (failed to read LEB128)");
121 }
122 
123 static size_t getAugPSize(unsigned enc) {
124   switch (enc & 0x0f) {
125   case DW_EH_PE_absptr:
126   case DW_EH_PE_signed:
127     return config->wordsize;
128   case DW_EH_PE_udata2:
129   case DW_EH_PE_sdata2:
130     return 2;
131   case DW_EH_PE_udata4:
132   case DW_EH_PE_sdata4:
133     return 4;
134   case DW_EH_PE_udata8:
135   case DW_EH_PE_sdata8:
136     return 8;
137   }
138   return 0;
139 }
140 
141 void EhReader::skipAugP() {
142   uint8_t enc = readByte();
143   if ((enc & 0xf0) == DW_EH_PE_aligned)
144     failOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported");
145   size_t size = getAugPSize(enc);
146   if (size == 0)
147     failOn(d.data() - 1, "unknown FDE encoding");
148   if (size >= d.size())
149     failOn(d.data() - 1, "corrupted CIE");
150   d = d.slice(size);
151 }
152 
153 uint8_t elf::getFdeEncoding(EhSectionPiece *p) {
154   return EhReader(p->sec, p->data()).getFdeEncoding();
155 }
156 
157 bool elf::hasLSDA(const EhSectionPiece &p) {
158   return EhReader(p.sec, p.data()).hasLSDA();
159 }
160 
161 StringRef EhReader::getAugmentation() {
162   skipBytes(8);
163   int version = readByte();
164   if (version != 1 && version != 3)
165     failOn(d.data() - 1,
166            "FDE version 1 or 3 expected, but got " + Twine(version));
167 
168   StringRef aug = readString();
169 
170   // Skip code and data alignment factors.
171   skipLeb128();
172   skipLeb128();
173 
174   // Skip the return address register. In CIE version 1 this is a single
175   // byte. In CIE version 3 this is an unsigned LEB128.
176   if (version == 1)
177     readByte();
178   else
179     skipLeb128();
180   return aug;
181 }
182 
183 uint8_t EhReader::getFdeEncoding() {
184   // We only care about an 'R' value, but other records may precede an 'R'
185   // record. Unfortunately records are not in TLV (type-length-value) format,
186   // so we need to teach the linker how to skip records for each type.
187   StringRef aug = getAugmentation();
188   for (char c : aug) {
189     if (c == 'R')
190       return readByte();
191     if (c == 'z')
192       skipLeb128();
193     else if (c == 'L')
194       readByte();
195     else if (c == 'P')
196       skipAugP();
197     else if (c != 'B' && c != 'S')
198       failOn(aug.data(), "unknown .eh_frame augmentation string: " + aug);
199   }
200   return DW_EH_PE_absptr;
201 }
202 
203 bool EhReader::hasLSDA() {
204   StringRef aug = getAugmentation();
205   for (char c : aug) {
206     if (c == 'L')
207       return true;
208     if (c == 'z')
209       skipLeb128();
210     else if (c == 'P')
211       skipAugP();
212     else if (c == 'R')
213       readByte();
214     else if (c != 'B' && c != 'S')
215       failOn(aug.data(), "unknown .eh_frame augmentation string: " + aug);
216   }
217   return false;
218 }
219