1 //===- EhFrame.cpp -------------------------------------------------------===//
2 //
3 // The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // .eh_frame section contains information on how to unwind the stack when
11 // an exception is thrown. The section consists of sequence of CIE and FDE
12 // records. The linker needs to merge CIEs and associate FDEs to CIEs.
13 // That means the linker has to understand the format of the section.
14 //
15 // This file contains a few utility functions to read .eh_frame contents.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "EhFrame.h"
20 #include "Config.h"
21 #include "InputSection.h"
22 #include "Relocations.h"
23 #include "Target.h"
24 #include "lld/Common/ErrorHandler.h"
25 #include "lld/Common/Strings.h"
26 #include "llvm/BinaryFormat/Dwarf.h"
27 #include "llvm/Object/ELF.h"
28
29 using namespace llvm;
30 using namespace llvm::ELF;
31 using namespace llvm::dwarf;
32 using namespace llvm::object;
33
34 using namespace lld;
35 using namespace lld::elf;
36
37 namespace {
38 class EhReader {
39 public:
EhReader(InputSectionBase * S,ArrayRef<uint8_t> D)40 EhReader(InputSectionBase *S, ArrayRef<uint8_t> D) : IS(S), D(D) {}
41 size_t readEhRecordSize();
42 uint8_t getFdeEncoding();
43
44 private:
failOn(const P * Loc,const Twine & Msg)45 template <class P> void failOn(const P *Loc, const Twine &Msg) {
46 fatal("corrupted .eh_frame: " + Msg + "\n>>> defined in " +
47 IS->getObjMsg((const uint8_t *)Loc - IS->data().data()));
48 }
49
50 uint8_t readByte();
51 void skipBytes(size_t Count);
52 StringRef readString();
53 void skipLeb128();
54 void skipAugP();
55
56 InputSectionBase *IS;
57 ArrayRef<uint8_t> D;
58 };
59 }
60
readEhRecordSize(InputSectionBase * S,size_t Off)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.
readEhRecordSize()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.
readByte()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
skipBytes(size_t Count)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.
readString()99 StringRef EhReader::readString() {
100 const uint8_t *End = std::find(D.begin(), D.end(), '\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.
skipLeb128()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
getAugPSize(unsigned Enc)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
skipAugP()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
getFdeEncoding(EhSectionPiece * P)153 uint8_t elf::getFdeEncoding(EhSectionPiece *P) {
154 return EhReader(P->Sec, P->data()).getFdeEncoding();
155 }
156
getFdeEncoding()157 uint8_t EhReader::getFdeEncoding() {
158 skipBytes(8);
159 int Version = readByte();
160 if (Version != 1 && Version != 3)
161 failOn(D.data() - 1,
162 "FDE version 1 or 3 expected, but got " + Twine(Version));
163
164 StringRef Aug = readString();
165
166 // Skip code and data alignment factors.
167 skipLeb128();
168 skipLeb128();
169
170 // Skip the return address register. In CIE version 1 this is a single
171 // byte. In CIE version 3 this is an unsigned LEB128.
172 if (Version == 1)
173 readByte();
174 else
175 skipLeb128();
176
177 // We only care about an 'R' value, but other records may precede an 'R'
178 // record. Unfortunately records are not in TLV (type-length-value) format,
179 // so we need to teach the linker how to skip records for each type.
180 for (char C : Aug) {
181 if (C == 'R')
182 return readByte();
183 if (C == 'z') {
184 skipLeb128();
185 continue;
186 }
187 if (C == 'P') {
188 skipAugP();
189 continue;
190 }
191 if (C == 'L') {
192 readByte();
193 continue;
194 }
195 failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug);
196 }
197 return DW_EH_PE_absptr;
198 }
199