EhFrame.cpp revision 303239
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 22#include "llvm/Object/ELF.h" 23#include "llvm/Support/Dwarf.h" 24#include "llvm/Support/Endian.h" 25 26using namespace llvm; 27using namespace llvm::ELF; 28using namespace llvm::dwarf; 29using namespace llvm::object; 30using namespace llvm::support::endian; 31 32namespace lld { 33namespace elf { 34 35// .eh_frame section is a sequence of records. Each record starts with 36// a 4 byte length field. This function reads the length. 37template <class ELFT> size_t readEhRecordSize(ArrayRef<uint8_t> D) { 38 const endianness E = ELFT::TargetEndianness; 39 if (D.size() < 4) 40 fatal("CIE/FDE too small"); 41 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()); 46 if (V == UINT32_MAX) 47 fatal("CIE/FDE too large"); 48 uint64_t Size = V + 4; 49 if (Size > D.size()) 50 fatal("CIE/FIE ends past the end of the section"); 51 return Size; 52} 53 54// Read a byte and advance D by one byte. 55static uint8_t readByte(ArrayRef<uint8_t> &D) { 56 if (D.empty()) 57 fatal("corrupted or unsupported CIE information"); 58 uint8_t B = D.front(); 59 D = D.slice(1); 60 return B; 61} 62 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. 67static void skipLeb128(ArrayRef<uint8_t> &D) { 68 while (!D.empty()) { 69 uint8_t Val = D.front(); 70 D = D.slice(1); 71 if ((Val & 0x80) == 0) 72 return; 73 } 74 fatal("corrupted or unsupported CIE information"); 75} 76 77template <class ELFT> static size_t getAugPSize(unsigned Enc) { 78 switch (Enc & 0x0f) { 79 case DW_EH_PE_absptr: 80 case DW_EH_PE_signed: 81 return ELFT::Is64Bits ? 8 : 4; 82 case DW_EH_PE_udata2: 83 case DW_EH_PE_sdata2: 84 return 2; 85 case DW_EH_PE_udata4: 86 case DW_EH_PE_sdata4: 87 return 4; 88 case DW_EH_PE_udata8: 89 case DW_EH_PE_sdata8: 90 return 8; 91 } 92 fatal("unknown FDE encoding"); 93} 94 95template <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"); 102 D = D.slice(Size); 103} 104 105template <class ELFT> uint8_t getFdeEncoding(ArrayRef<uint8_t> D) { 106 if (D.size() < 8) 107 fatal("CIE too small"); 108 D = D.slice(8); 109 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)); 113 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); 119 120 // Code alignment factor should always be 1 for .eh_frame. 121 if (readByte(D) != 1) 122 fatal("CIE code alignment must be 1"); 123 124 // Skip data alignment factor. 125 skipLeb128(D); 126 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. 129 if (Version == 1) 130 readByte(D); 131 else 132 skipLeb128(D); 133 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. 137 for (char C : Aug) { 138 if (C == 'R') 139 return readByte(D); 140 if (C == 'z') { 141 skipLeb128(D); 142 continue; 143 } 144 if (C == 'P') { 145 skipAugP<ELFT>(D); 146 continue; 147 } 148 if (C == 'L') { 149 readByte(D); 150 continue; 151 } 152 fatal("unknown .eh_frame augmentation string: " + Aug); 153 } 154 return DW_EH_PE_absptr; 155} 156 157template size_t readEhRecordSize<ELF32LE>(ArrayRef<uint8_t>); 158template size_t readEhRecordSize<ELF32BE>(ArrayRef<uint8_t>); 159template size_t readEhRecordSize<ELF64LE>(ArrayRef<uint8_t>); 160template size_t readEhRecordSize<ELF64BE>(ArrayRef<uint8_t>); 161 162template uint8_t getFdeEncoding<ELF32LE>(ArrayRef<uint8_t>); 163template uint8_t getFdeEncoding<ELF32BE>(ArrayRef<uint8_t>); 164template uint8_t getFdeEncoding<ELF64LE>(ArrayRef<uint8_t>); 165template uint8_t getFdeEncoding<ELF64BE>(ArrayRef<uint8_t>); 166} 167} 168