1 //===- EhFrame.cpp -------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 // This file contains a few utility functions to read .eh_frame contents.
17 //===----------------------------------------------------------------------===//
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/Endian.h"
27 using namespace llvm::ELF;
28 using namespace llvm::dwarf;
29 using namespace llvm::object;
30 using namespace llvm::support::endian;
35 // .eh_frame section is a sequence of records. Each record starts with
36 // a 4 byte length field. This function reads the length.
37 template <class ELFT> size_t readEhRecordSize(ArrayRef<uint8_t> D) {
38 const endianness E = ELFT::TargetEndianness;
40 fatal("CIE/FDE too small");
42 // First 4 bytes of CIE/FDE is the size of the record.
43 // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
44 // but we do not support that format yet.
45 uint64_t V = read32<E>(D.data());
47 fatal("CIE/FDE too large");
48 uint64_t Size = V + 4;
50 fatal("CIE/FIE ends past the end of the section");
54 // Read a byte and advance D by one byte.
55 static uint8_t readByte(ArrayRef<uint8_t> &D) {
57 fatal("corrupted or unsupported CIE information");
58 uint8_t B = D.front();
63 // Skip an integer encoded in the LEB128 format.
64 // Actual number is not of interest because only the runtime needs it.
65 // But we need to be at least able to skip it so that we can read
66 // the field that follows a LEB128 number.
67 static void skipLeb128(ArrayRef<uint8_t> &D) {
69 uint8_t Val = D.front();
71 if ((Val & 0x80) == 0)
74 fatal("corrupted or unsupported CIE information");
77 template <class ELFT> static size_t getAugPSize(unsigned Enc) {
81 return ELFT::Is64Bits ? 8 : 4;
92 fatal("unknown FDE encoding");
95 template <class ELFT> static void skipAugP(ArrayRef<uint8_t> &D) {
96 uint8_t Enc = readByte(D);
97 if ((Enc & 0xf0) == DW_EH_PE_aligned)
98 fatal("DW_EH_PE_aligned encoding is not supported");
99 size_t Size = getAugPSize<ELFT>(Enc);
100 if (Size >= D.size())
101 fatal("corrupted CIE");
105 template <class ELFT> uint8_t getFdeEncoding(ArrayRef<uint8_t> D) {
107 fatal("CIE too small");
110 uint8_t Version = readByte(D);
111 if (Version != 1 && Version != 3)
112 fatal("FDE version 1 or 3 expected, but got " + Twine((unsigned)Version));
114 const unsigned char *AugEnd = std::find(D.begin(), D.end(), '\0');
115 if (AugEnd == D.end())
116 fatal("corrupted CIE");
117 StringRef Aug(reinterpret_cast<const char *>(D.begin()), AugEnd - D.begin());
118 D = D.slice(Aug.size() + 1);
120 // Code alignment factor should always be 1 for .eh_frame.
121 if (readByte(D) != 1)
122 fatal("CIE code alignment must be 1");
124 // Skip data alignment factor.
127 // Skip the return address register. In CIE version 1 this is a single
128 // byte. In CIE version 3 this is an unsigned LEB128.
134 // We only care about an 'R' value, but other records may precede an 'R'
135 // record. Unfortunately records are not in TLV (type-length-value) format,
136 // so we need to teach the linker how to skip records for each type.
152 fatal("unknown .eh_frame augmentation string: " + Aug);
154 return DW_EH_PE_absptr;
157 template size_t readEhRecordSize<ELF32LE>(ArrayRef<uint8_t>);
158 template size_t readEhRecordSize<ELF32BE>(ArrayRef<uint8_t>);
159 template size_t readEhRecordSize<ELF64LE>(ArrayRef<uint8_t>);
160 template size_t readEhRecordSize<ELF64BE>(ArrayRef<uint8_t>);
162 template uint8_t getFdeEncoding<ELF32LE>(ArrayRef<uint8_t>);
163 template uint8_t getFdeEncoding<ELF32BE>(ArrayRef<uint8_t>);
164 template uint8_t getFdeEncoding<ELF64LE>(ArrayRef<uint8_t>);
165 template uint8_t getFdeEncoding<ELF64BE>(ArrayRef<uint8_t>);