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 "InputFiles.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 using namespace lld; 34 using namespace lld::elf; 35 36 namespace { 37 class EhReader { 38 public: 39 EhReader(InputSectionBase *s, ArrayRef<uint8_t> d) : isec(s), d(d) {} 40 uint8_t getFdeEncoding(); 41 bool hasLSDA(); 42 43 private: 44 template <class P> void errOn(const P *loc, const Twine &msg) { 45 Ctx &ctx = isec->file->ctx; 46 Err(ctx) << "corrupted .eh_frame: " << msg << "\n>>> defined in " 47 << isec->getObjMsg((const uint8_t *)loc - isec->content().data()); 48 } 49 50 uint8_t readByte(); 51 void skipBytes(size_t count); 52 StringRef readString(); 53 void skipLeb128(); 54 void skipAugP(); 55 StringRef getAugmentation(); 56 57 InputSectionBase *isec; 58 ArrayRef<uint8_t> d; 59 }; 60 } 61 62 // Read a byte and advance D by one byte. 63 uint8_t EhReader::readByte() { 64 if (d.empty()) { 65 errOn(d.data(), "unexpected end of CIE"); 66 return 0; 67 } 68 uint8_t b = d.front(); 69 d = d.slice(1); 70 return b; 71 } 72 73 void EhReader::skipBytes(size_t count) { 74 if (d.size() < count) 75 errOn(d.data(), "CIE is too small"); 76 else 77 d = d.slice(count); 78 } 79 80 // Read a null-terminated string. 81 StringRef EhReader::readString() { 82 const uint8_t *end = llvm::find(d, '\0'); 83 if (end == d.end()) { 84 errOn(d.data(), "corrupted CIE (failed to read string)"); 85 return {}; 86 } 87 StringRef s = toStringRef(d.slice(0, end - d.begin())); 88 d = d.slice(s.size() + 1); 89 return s; 90 } 91 92 // Skip an integer encoded in the LEB128 format. 93 // Actual number is not of interest because only the runtime needs it. 94 // But we need to be at least able to skip it so that we can read 95 // the field that follows a LEB128 number. 96 void EhReader::skipLeb128() { 97 const uint8_t *errPos = d.data(); 98 while (!d.empty()) { 99 uint8_t val = d.front(); 100 d = d.slice(1); 101 if ((val & 0x80) == 0) 102 return; 103 } 104 errOn(errPos, "corrupted CIE (failed to read LEB128)"); 105 } 106 107 static size_t getAugPSize(Ctx &ctx, unsigned enc) { 108 switch (enc & 0x0f) { 109 case DW_EH_PE_absptr: 110 case DW_EH_PE_signed: 111 return ctx.arg.wordsize; 112 case DW_EH_PE_udata2: 113 case DW_EH_PE_sdata2: 114 return 2; 115 case DW_EH_PE_udata4: 116 case DW_EH_PE_sdata4: 117 return 4; 118 case DW_EH_PE_udata8: 119 case DW_EH_PE_sdata8: 120 return 8; 121 } 122 return 0; 123 } 124 125 void EhReader::skipAugP() { 126 uint8_t enc = readByte(); 127 if ((enc & 0xf0) == DW_EH_PE_aligned) 128 return errOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported"); 129 size_t size = getAugPSize(isec->getCtx(), enc); 130 if (size == 0) 131 return errOn(d.data() - 1, "unknown FDE encoding"); 132 if (size >= d.size()) 133 return errOn(d.data() - 1, "corrupted CIE"); 134 d = d.slice(size); 135 } 136 137 uint8_t elf::getFdeEncoding(EhSectionPiece *p) { 138 return EhReader(p->sec, p->data()).getFdeEncoding(); 139 } 140 141 bool elf::hasLSDA(const EhSectionPiece &p) { 142 return EhReader(p.sec, p.data()).hasLSDA(); 143 } 144 145 StringRef EhReader::getAugmentation() { 146 skipBytes(8); 147 int version = readByte(); 148 if (version != 1 && version != 3) { 149 errOn(d.data() - 1, 150 "FDE version 1 or 3 expected, but got " + Twine(version)); 151 return {}; 152 } 153 154 StringRef aug = readString(); 155 156 // Skip code and data alignment factors. 157 skipLeb128(); 158 skipLeb128(); 159 160 // Skip the return address register. In CIE version 1 this is a single 161 // byte. In CIE version 3 this is an unsigned LEB128. 162 if (version == 1) 163 readByte(); 164 else 165 skipLeb128(); 166 return aug; 167 } 168 169 uint8_t EhReader::getFdeEncoding() { 170 // We only care about an 'R' value, but other records may precede an 'R' 171 // record. Unfortunately records are not in TLV (type-length-value) format, 172 // so we need to teach the linker how to skip records for each type. 173 StringRef aug = getAugmentation(); 174 for (char c : aug) { 175 if (c == 'R') 176 return readByte(); 177 if (c == 'z') 178 skipLeb128(); 179 else if (c == 'L') 180 readByte(); 181 else if (c == 'P') 182 skipAugP(); 183 else if (c != 'B' && c != 'S' && c != 'G') { 184 errOn(aug.data(), "unknown .eh_frame augmentation string: " + aug); 185 break; 186 } 187 } 188 return DW_EH_PE_absptr; 189 } 190 191 bool EhReader::hasLSDA() { 192 StringRef aug = getAugmentation(); 193 for (char c : aug) { 194 if (c == 'L') 195 return true; 196 if (c == 'z') 197 skipLeb128(); 198 else if (c == 'P') 199 skipAugP(); 200 else if (c == 'R') 201 readByte(); 202 else if (c != 'B' && c != 'S' && c != 'G') { 203 errOn(aug.data(), "unknown .eh_frame augmentation string: " + aug); 204 break; 205 } 206 } 207 return false; 208 } 209