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