]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/EhFrame.cpp
MFV r326007: less v529.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / EhFrame.cpp
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 "Error.h"
21 #include "InputSection.h"
22 #include "Relocations.h"
23 #include "Strings.h"
24
25 #include "llvm/BinaryFormat/Dwarf.h"
26 #include "llvm/Object/ELF.h"
27 #include "llvm/Support/Endian.h"
28
29 using namespace llvm;
30 using namespace llvm::ELF;
31 using namespace llvm::dwarf;
32 using namespace llvm::object;
33 using namespace llvm::support::endian;
34
35 using namespace lld;
36 using namespace lld::elf;
37
38 namespace {
39 template <class ELFT> class EhReader {
40 public:
41   EhReader(InputSectionBase *S, ArrayRef<uint8_t> D) : IS(S), D(D) {}
42   size_t readEhRecordSize();
43   uint8_t getFdeEncoding();
44
45 private:
46   template <class P> void failOn(const P *Loc, const Twine &Msg) {
47     fatal("corrupted .eh_frame: " + Msg + "\n>>> defined in " +
48           IS->getObjMsg<ELFT>((const uint8_t *)Loc - IS->Data.data()));
49   }
50
51   uint8_t readByte();
52   void skipBytes(size_t Count);
53   StringRef readString();
54   void skipLeb128();
55   void skipAugP();
56
57   InputSectionBase *IS;
58   ArrayRef<uint8_t> D;
59 };
60 }
61
62 template <class ELFT>
63 size_t elf::readEhRecordSize(InputSectionBase *S, size_t Off) {
64   return EhReader<ELFT>(S, S->Data.slice(Off)).readEhRecordSize();
65 }
66
67 // .eh_frame section is a sequence of records. Each record starts with
68 // a 4 byte length field. This function reads the length.
69 template <class ELFT> size_t EhReader<ELFT>::readEhRecordSize() {
70   const endianness E = ELFT::TargetEndianness;
71   if (D.size() < 4)
72     failOn(D.data(), "CIE/FDE too small");
73
74   // First 4 bytes of CIE/FDE is the size of the record.
75   // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
76   // but we do not support that format yet.
77   uint64_t V = read32<E>(D.data());
78   if (V == UINT32_MAX)
79     failOn(D.data(), "CIE/FDE too large");
80   uint64_t Size = V + 4;
81   if (Size > D.size())
82     failOn(D.data(), "CIE/FDE ends past the end of the section");
83   return Size;
84 }
85
86 // Read a byte and advance D by one byte.
87 template <class ELFT> uint8_t EhReader<ELFT>::readByte() {
88   if (D.empty())
89     failOn(D.data(), "unexpected end of CIE");
90   uint8_t B = D.front();
91   D = D.slice(1);
92   return B;
93 }
94
95 template <class ELFT> void EhReader<ELFT>::skipBytes(size_t Count) {
96   if (D.size() < Count)
97     failOn(D.data(), "CIE is too small");
98   D = D.slice(Count);
99 }
100
101 // Read a null-terminated string.
102 template <class ELFT> StringRef EhReader<ELFT>::readString() {
103   const uint8_t *End = std::find(D.begin(), D.end(), '\0');
104   if (End == D.end())
105     failOn(D.data(), "corrupted CIE (failed to read string)");
106   StringRef S = toStringRef(D.slice(0, End - D.begin()));
107   D = D.slice(S.size() + 1);
108   return S;
109 }
110
111 // Skip an integer encoded in the LEB128 format.
112 // Actual number is not of interest because only the runtime needs it.
113 // But we need to be at least able to skip it so that we can read
114 // the field that follows a LEB128 number.
115 template <class ELFT> void EhReader<ELFT>::skipLeb128() {
116   const uint8_t *ErrPos = D.data();
117   while (!D.empty()) {
118     uint8_t Val = D.front();
119     D = D.slice(1);
120     if ((Val & 0x80) == 0)
121       return;
122   }
123   failOn(ErrPos, "corrupted CIE (failed to read LEB128)");
124 }
125
126 static size_t getAugPSize(unsigned Enc) {
127   switch (Enc & 0x0f) {
128   case DW_EH_PE_absptr:
129   case DW_EH_PE_signed:
130     return Config->Wordsize;
131   case DW_EH_PE_udata2:
132   case DW_EH_PE_sdata2:
133     return 2;
134   case DW_EH_PE_udata4:
135   case DW_EH_PE_sdata4:
136     return 4;
137   case DW_EH_PE_udata8:
138   case DW_EH_PE_sdata8:
139     return 8;
140   }
141   return 0;
142 }
143
144 template <class ELFT> void EhReader<ELFT>::skipAugP() {
145   uint8_t Enc = readByte();
146   if ((Enc & 0xf0) == DW_EH_PE_aligned)
147     failOn(D.data() - 1, "DW_EH_PE_aligned encoding is not supported");
148   size_t Size = getAugPSize(Enc);
149   if (Size == 0)
150     failOn(D.data() - 1, "unknown FDE encoding");
151   if (Size >= D.size())
152     failOn(D.data() - 1, "corrupted CIE");
153   D = D.slice(Size);
154 }
155
156 template <class ELFT> uint8_t elf::getFdeEncoding(EhSectionPiece *P) {
157   auto *IS = static_cast<InputSectionBase *>(P->ID);
158   return EhReader<ELFT>(IS, P->data()).getFdeEncoding();
159 }
160
161 template <class ELFT> uint8_t EhReader<ELFT>::getFdeEncoding() {
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
181   // We only care about an 'R' value, but other records may precede an 'R'
182   // record. Unfortunately records are not in TLV (type-length-value) format,
183   // so we need to teach the linker how to skip records for each type.
184   for (char C : Aug) {
185     if (C == 'R')
186       return readByte();
187     if (C == 'z') {
188       skipLeb128();
189       continue;
190     }
191     if (C == 'P') {
192       skipAugP();
193       continue;
194     }
195     if (C == 'L') {
196       readByte();
197       continue;
198     }
199     failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug);
200   }
201   return DW_EH_PE_absptr;
202 }
203
204 template size_t elf::readEhRecordSize<ELF32LE>(InputSectionBase *S, size_t Off);
205 template size_t elf::readEhRecordSize<ELF32BE>(InputSectionBase *S, size_t Off);
206 template size_t elf::readEhRecordSize<ELF64LE>(InputSectionBase *S, size_t Off);
207 template size_t elf::readEhRecordSize<ELF64BE>(InputSectionBase *S, size_t Off);
208
209 template uint8_t elf::getFdeEncoding<ELF32LE>(EhSectionPiece *P);
210 template uint8_t elf::getFdeEncoding<ELF32BE>(EhSectionPiece *P);
211 template uint8_t elf::getFdeEncoding<ELF64LE>(EhSectionPiece *P);
212 template uint8_t elf::getFdeEncoding<ELF64BE>(EhSectionPiece *P);