diff options
Diffstat (limited to 'ELF/EhFrame.cpp')
-rw-r--r-- | ELF/EhFrame.cpp | 137 |
1 files changed, 68 insertions, 69 deletions
diff --git a/ELF/EhFrame.cpp b/ELF/EhFrame.cpp index 95d444bdc2a1f..b3245dd01669e 100644 --- a/ELF/EhFrame.cpp +++ b/ELF/EhFrame.cpp @@ -1,9 +1,8 @@ //===- EhFrame.cpp -------------------------------------------------------===// // -// The LLVM Linker -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -37,72 +36,72 @@ using namespace lld::elf; namespace { class EhReader { public: - EhReader(InputSectionBase *S, ArrayRef<uint8_t> D) : IS(S), D(D) {} + EhReader(InputSectionBase *s, ArrayRef<uint8_t> d) : isec(s), d(d) {} size_t readEhRecordSize(); uint8_t getFdeEncoding(); private: - template <class P> void failOn(const P *Loc, const Twine &Msg) { - fatal("corrupted .eh_frame: " + Msg + "\n>>> defined in " + - IS->getObjMsg((const uint8_t *)Loc - IS->data().data())); + template <class P> void failOn(const P *loc, const Twine &msg) { + fatal("corrupted .eh_frame: " + msg + "\n>>> defined in " + + isec->getObjMsg((const uint8_t *)loc - isec->data().data())); } uint8_t readByte(); - void skipBytes(size_t Count); + void skipBytes(size_t count); StringRef readString(); void skipLeb128(); void skipAugP(); - InputSectionBase *IS; - ArrayRef<uint8_t> D; + InputSectionBase *isec; + ArrayRef<uint8_t> d; }; } -size_t elf::readEhRecordSize(InputSectionBase *S, size_t Off) { - return EhReader(S, S->data().slice(Off)).readEhRecordSize(); +size_t elf::readEhRecordSize(InputSectionBase *s, size_t off) { + return EhReader(s, s->data().slice(off)).readEhRecordSize(); } // .eh_frame section is a sequence of records. Each record starts with // a 4 byte length field. This function reads the length. size_t EhReader::readEhRecordSize() { - if (D.size() < 4) - failOn(D.data(), "CIE/FDE too small"); + if (d.size() < 4) + failOn(d.data(), "CIE/FDE too small"); // First 4 bytes of CIE/FDE is the size of the record. // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead, // but we do not support that format yet. - uint64_t V = read32(D.data()); - if (V == UINT32_MAX) - failOn(D.data(), "CIE/FDE too large"); - uint64_t Size = V + 4; - if (Size > D.size()) - failOn(D.data(), "CIE/FDE ends past the end of the section"); - return Size; + uint64_t v = read32(d.data()); + if (v == UINT32_MAX) + failOn(d.data(), "CIE/FDE too large"); + uint64_t size = v + 4; + if (size > d.size()) + failOn(d.data(), "CIE/FDE ends past the end of the section"); + return size; } // Read a byte and advance D by one byte. uint8_t EhReader::readByte() { - if (D.empty()) - failOn(D.data(), "unexpected end of CIE"); - uint8_t B = D.front(); - D = D.slice(1); - return B; + if (d.empty()) + failOn(d.data(), "unexpected end of CIE"); + uint8_t b = d.front(); + d = d.slice(1); + return b; } -void EhReader::skipBytes(size_t Count) { - if (D.size() < Count) - failOn(D.data(), "CIE is too small"); - D = D.slice(Count); +void EhReader::skipBytes(size_t count) { + if (d.size() < count) + failOn(d.data(), "CIE is too small"); + d = d.slice(count); } // Read a null-terminated string. StringRef EhReader::readString() { - const uint8_t *End = std::find(D.begin(), D.end(), '\0'); - if (End == D.end()) - failOn(D.data(), "corrupted CIE (failed to read string)"); - StringRef S = toStringRef(D.slice(0, End - D.begin())); - D = D.slice(S.size() + 1); - return S; + const uint8_t *end = llvm::find(d, '\0'); + if (end == d.end()) + failOn(d.data(), "corrupted CIE (failed to read string)"); + StringRef s = toStringRef(d.slice(0, end - d.begin())); + d = d.slice(s.size() + 1); + return s; } // Skip an integer encoded in the LEB128 format. @@ -110,21 +109,21 @@ StringRef EhReader::readString() { // But we need to be at least able to skip it so that we can read // the field that follows a LEB128 number. void EhReader::skipLeb128() { - const uint8_t *ErrPos = D.data(); - while (!D.empty()) { - uint8_t Val = D.front(); - D = D.slice(1); - if ((Val & 0x80) == 0) + const uint8_t *errPos = d.data(); + while (!d.empty()) { + uint8_t val = d.front(); + d = d.slice(1); + if ((val & 0x80) == 0) return; } - failOn(ErrPos, "corrupted CIE (failed to read LEB128)"); + failOn(errPos, "corrupted CIE (failed to read LEB128)"); } -static size_t getAugPSize(unsigned Enc) { - switch (Enc & 0x0f) { +static size_t getAugPSize(unsigned enc) { + switch (enc & 0x0f) { case DW_EH_PE_absptr: case DW_EH_PE_signed: - return Config->Wordsize; + return config->wordsize; case DW_EH_PE_udata2: case DW_EH_PE_sdata2: return 2; @@ -139,29 +138,29 @@ static size_t getAugPSize(unsigned Enc) { } void EhReader::skipAugP() { - uint8_t Enc = readByte(); - if ((Enc & 0xf0) == DW_EH_PE_aligned) - failOn(D.data() - 1, "DW_EH_PE_aligned encoding is not supported"); - size_t Size = getAugPSize(Enc); - if (Size == 0) - failOn(D.data() - 1, "unknown FDE encoding"); - if (Size >= D.size()) - failOn(D.data() - 1, "corrupted CIE"); - D = D.slice(Size); + uint8_t enc = readByte(); + if ((enc & 0xf0) == DW_EH_PE_aligned) + failOn(d.data() - 1, "DW_EH_PE_aligned encoding is not supported"); + size_t size = getAugPSize(enc); + if (size == 0) + failOn(d.data() - 1, "unknown FDE encoding"); + if (size >= d.size()) + failOn(d.data() - 1, "corrupted CIE"); + d = d.slice(size); } -uint8_t elf::getFdeEncoding(EhSectionPiece *P) { - return EhReader(P->Sec, P->data()).getFdeEncoding(); +uint8_t elf::getFdeEncoding(EhSectionPiece *p) { + return EhReader(p->sec, p->data()).getFdeEncoding(); } uint8_t EhReader::getFdeEncoding() { skipBytes(8); - int Version = readByte(); - if (Version != 1 && Version != 3) - failOn(D.data() - 1, - "FDE version 1 or 3 expected, but got " + Twine(Version)); + int version = readByte(); + if (version != 1 && version != 3) + failOn(d.data() - 1, + "FDE version 1 or 3 expected, but got " + Twine(version)); - StringRef Aug = readString(); + StringRef aug = readString(); // Skip code and data alignment factors. skipLeb128(); @@ -169,7 +168,7 @@ uint8_t EhReader::getFdeEncoding() { // Skip the return address register. In CIE version 1 this is a single // byte. In CIE version 3 this is an unsigned LEB128. - if (Version == 1) + if (version == 1) readByte(); else skipLeb128(); @@ -177,22 +176,22 @@ uint8_t EhReader::getFdeEncoding() { // We only care about an 'R' value, but other records may precede an 'R' // record. Unfortunately records are not in TLV (type-length-value) format, // so we need to teach the linker how to skip records for each type. - for (char C : Aug) { - if (C == 'R') + for (char c : aug) { + if (c == 'R') return readByte(); - if (C == 'z') { + if (c == 'z') { skipLeb128(); continue; } - if (C == 'P') { + if (c == 'P') { skipAugP(); continue; } - if (C == 'L') { + if (c == 'L') { readByte(); continue; } - failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug); + failOn(aug.data(), "unknown .eh_frame augmentation string: " + aug); } return DW_EH_PE_absptr; } |