xref: /llvm-project-15.0.7/lld/ELF/EhFrame.cpp (revision 3dee12e4)
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   EhReader(InputSectionBase *S, ArrayRef<uint8_t> D) : IS(S), D(D) {}
40   size_t readEhRecordSize();
41   uint8_t getFdeEncoding();
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           IS->getObjMsg((const uint8_t *)Loc - IS->data().data()));
47   }
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 size_t elf::readEhRecordSize(InputSectionBase *S, size_t Off) {
61   return EhReader(S, S->data().slice(Off)).readEhRecordSize();
62 }
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 size_t EhReader::readEhRecordSize() {
67   if (D.size() < 4)
68     failOn(D.data(), "CIE/FDE too small");
69 
70   // First 4 bytes of CIE/FDE is the size of the record.
71   // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
72   // but we do not support that format yet.
73   uint64_t V = read32(D.data());
74   if (V == UINT32_MAX)
75     failOn(D.data(), "CIE/FDE too large");
76   uint64_t Size = V + 4;
77   if (Size > D.size())
78     failOn(D.data(), "CIE/FDE ends past the end of the section");
79   return Size;
80 }
81 
82 // Read a byte and advance D by one byte.
83 uint8_t EhReader::readByte() {
84   if (D.empty())
85     failOn(D.data(), "unexpected end of CIE");
86   uint8_t B = D.front();
87   D = D.slice(1);
88   return B;
89 }
90 
91 void EhReader::skipBytes(size_t Count) {
92   if (D.size() < Count)
93     failOn(D.data(), "CIE is too small");
94   D = D.slice(Count);
95 }
96 
97 // Read a null-terminated string.
98 StringRef EhReader::readString() {
99   const uint8_t *End = llvm::find(D, '\0');
100   if (End == D.end())
101     failOn(D.data(), "corrupted CIE (failed to read string)");
102   StringRef S = toStringRef(D.slice(0, End - D.begin()));
103   D = D.slice(S.size() + 1);
104   return S;
105 }
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 void EhReader::skipLeb128() {
112   const uint8_t *ErrPos = D.data();
113   while (!D.empty()) {
114     uint8_t Val = D.front();
115     D = D.slice(1);
116     if ((Val & 0x80) == 0)
117       return;
118   }
119   failOn(ErrPos, "corrupted CIE (failed to read LEB128)");
120 }
121 
122 static size_t getAugPSize(unsigned Enc) {
123   switch (Enc & 0x0f) {
124   case DW_EH_PE_absptr:
125   case DW_EH_PE_signed:
126     return Config->Wordsize;
127   case DW_EH_PE_udata2:
128   case DW_EH_PE_sdata2:
129     return 2;
130   case DW_EH_PE_udata4:
131   case DW_EH_PE_sdata4:
132     return 4;
133   case DW_EH_PE_udata8:
134   case DW_EH_PE_sdata8:
135     return 8;
136   }
137   return 0;
138 }
139 
140 void EhReader::skipAugP() {
141   uint8_t Enc = readByte();
142   if ((Enc & 0xf0) == DW_EH_PE_aligned)
143     failOn(D.data() - 1, "DW_EH_PE_aligned encoding is not supported");
144   size_t Size = getAugPSize(Enc);
145   if (Size == 0)
146     failOn(D.data() - 1, "unknown FDE encoding");
147   if (Size >= D.size())
148     failOn(D.data() - 1, "corrupted CIE");
149   D = D.slice(Size);
150 }
151 
152 uint8_t elf::getFdeEncoding(EhSectionPiece *P) {
153   return EhReader(P->Sec, P->data()).getFdeEncoding();
154 }
155 
156 uint8_t EhReader::getFdeEncoding() {
157   skipBytes(8);
158   int Version = readByte();
159   if (Version != 1 && Version != 3)
160     failOn(D.data() - 1,
161            "FDE version 1 or 3 expected, but got " + Twine(Version));
162 
163   StringRef Aug = readString();
164 
165   // Skip code and data alignment factors.
166   skipLeb128();
167   skipLeb128();
168 
169   // Skip the return address register. In CIE version 1 this is a single
170   // byte. In CIE version 3 this is an unsigned LEB128.
171   if (Version == 1)
172     readByte();
173   else
174     skipLeb128();
175 
176   // We only care about an 'R' value, but other records may precede an 'R'
177   // record. Unfortunately records are not in TLV (type-length-value) format,
178   // so we need to teach the linker how to skip records for each type.
179   for (char C : Aug) {
180     if (C == 'R')
181       return readByte();
182     if (C == 'z') {
183       skipLeb128();
184       continue;
185     }
186     if (C == 'P') {
187       skipAugP();
188       continue;
189     }
190     if (C == 'L') {
191       readByte();
192       continue;
193     }
194     failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug);
195   }
196   return DW_EH_PE_absptr;
197 }
198