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