1 //===- EhFrame.cpp -------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
14 // This file contains a few utility functions to read .eh_frame contents.
16 //===----------------------------------------------------------------------===//
20 #include "InputSection.h"
21 #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"
29 using namespace llvm::ELF;
30 using namespace llvm::dwarf;
31 using namespace llvm::object;
34 using namespace lld::elf;
39 EhReader(InputSectionBase *s, ArrayRef<uint8_t> d) : isec(s), d(d) {}
40 size_t readEhRecordSize();
41 uint8_t getFdeEncoding();
44 template <class P> void failOn(const P *loc, const Twine &msg) {
45 fatal("corrupted .eh_frame: " + msg + "\n>>> defined in " +
46 isec->getObjMsg((const uint8_t *)loc - isec->data().data()));
50 void skipBytes(size_t count);
51 StringRef readString();
55 InputSectionBase *isec;
60 size_t elf::readEhRecordSize(InputSectionBase *s, size_t off) {
61 return EhReader(s, s->data().slice(off)).readEhRecordSize();
64 // .eh_frame section is a sequence of records. Each record starts with
65 // a 4 byte length field. This function reads the length.
66 size_t EhReader::readEhRecordSize() {
68 failOn(d.data(), "CIE/FDE too small");
70 // First 4 bytes of CIE/FDE is the size of the record.
71 // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead,
72 // but we do not support that format yet.
73 uint64_t v = read32(d.data());
75 failOn(d.data(), "CIE/FDE too large");
76 uint64_t size = v + 4;
78 failOn(d.data(), "CIE/FDE ends past the end of the section");
82 // Read a byte and advance D by one byte.
83 uint8_t EhReader::readByte() {
85 failOn(d.data(), "unexpected end of CIE");
86 uint8_t b = d.front();
91 void EhReader::skipBytes(size_t count) {
93 failOn(d.data(), "CIE is too small");
97 // Read a null-terminated string.
98 StringRef EhReader::readString() {
99 const uint8_t *end = llvm::find(d, '\0');
101 failOn(d.data(), "corrupted CIE (failed to read string)");
102 StringRef s = toStringRef(d.slice(0, end - d.begin()));
103 d = d.slice(s.size() + 1);
107 // Skip an integer encoded in the LEB128 format.
108 // Actual number is not of interest because only the runtime needs it.
109 // But we need to be at least able to skip it so that we can read
110 // the field that follows a LEB128 number.
111 void EhReader::skipLeb128() {
112 const uint8_t *errPos = d.data();
114 uint8_t val = d.front();
116 if ((val & 0x80) == 0)
119 failOn(errPos, "corrupted CIE (failed to read LEB128)");
122 static size_t getAugPSize(unsigned enc) {
123 switch (enc & 0x0f) {
124 case DW_EH_PE_absptr:
125 case DW_EH_PE_signed:
126 return config->wordsize;
127 case DW_EH_PE_udata2:
128 case DW_EH_PE_sdata2:
130 case DW_EH_PE_udata4:
131 case DW_EH_PE_sdata4:
133 case DW_EH_PE_udata8:
134 case DW_EH_PE_sdata8:
140 void EhReader::skipAugP() {
141 uint8_t enc = readByte();
142 if ((enc & 0xf0) == DW_EH_PE_aligned)
143 failOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported");
144 size_t size = getAugPSize(enc);
146 failOn(d.data() - 1, "unknown FDE encoding");
147 if (size >= d.size())
148 failOn(d.data() - 1, "corrupted CIE");
152 uint8_t elf::getFdeEncoding(EhSectionPiece *p) {
153 return EhReader(p->sec, p->data()).getFdeEncoding();
156 uint8_t EhReader::getFdeEncoding() {
158 int version = readByte();
159 if (version != 1 && version != 3)
161 "FDE version 1 or 3 expected, but got " + Twine(version));
163 StringRef aug = readString();
165 // Skip code and data alignment factors.
169 // Skip the return address register. In CIE version 1 this is a single
170 // byte. In CIE version 3 this is an unsigned LEB128.
176 // We only care about an 'R' value, but other records may precede an 'R'
177 // record. Unfortunately records are not in TLV (type-length-value) format,
178 // so we need to teach the linker how to skip records for each type.
194 failOn(aug.data(), "unknown .eh_frame augmentation string: " + aug);
196 return DW_EH_PE_absptr;