Index: ELF/EhFrame.h =================================================================== --- ELF/EhFrame.h +++ ELF/EhFrame.h @@ -10,12 +10,14 @@ #ifndef LLD_ELF_EHFRAME_H #define LLD_ELF_EHFRAME_H +#include "InputSection.h" #include "lld/Core/LLVM.h" namespace lld { namespace elf { -template size_t readEhRecordSize(ArrayRef Data); -template uint8_t getFdeEncoding(ArrayRef Data); +template +size_t readEhRecordSize(InputSectionBase *S, size_t Off); +template uint8_t getFdeEncoding(EhSectionPiece *P); } } Index: ELF/EhFrame.cpp =================================================================== --- ELF/EhFrame.cpp +++ ELF/EhFrame.cpp @@ -18,6 +18,7 @@ #include "EhFrame.h" #include "Error.h" +#include "Relocations.h" #include "Strings.h" #include "llvm/Object/ELF.h" @@ -30,42 +31,65 @@ using namespace llvm::object; using namespace llvm::support::endian; -namespace lld { -namespace elf { +using namespace lld; +using namespace lld::elf; + +namespace { +template class ReadHelper { +public: + ReadHelper(InputSectionBase *S) : IS(S) {} + template void fatal(const P *Loc, const Twine &Msg) { + ::fatal(getLocation(*IS, (const uint8_t *)Loc - IS->Data.data()) + ": " + + Msg); + } + + uint8_t readByte(ArrayRef &D); + StringRef readString(ArrayRef &D); + void skipLeb128(ArrayRef &D); + void skipAugP(ArrayRef &D); + +private: + InputSectionBase *IS; +}; +} // .eh_frame section is a sequence of records. Each record starts with // a 4 byte length field. This function reads the length. -template size_t readEhRecordSize(ArrayRef D) { +template +size_t elf::readEhRecordSize(InputSectionBase *S, size_t Off) { + ArrayRef D = S->Data.slice(Off); const endianness E = ELFT::TargetEndianness; + ReadHelper H(S); if (D.size() < 4) - fatal("CIE/FDE too small"); + H.fatal(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) - fatal("CIE/FDE too large"); + H.fatal(D.data(), "CIE/FDE too large"); uint64_t Size = V + 4; if (Size > D.size()) - fatal("CIE/FDE ends past the end of the section"); + H.fatal(D.data(), "CIE/FDE ends past the end of the section"); return Size; } // Read a byte and advance D by one byte. -static uint8_t readByte(ArrayRef &D) { +template uint8_t ReadHelper::readByte(ArrayRef &D) { if (D.empty()) - fatal("corrupted or unsupported CIE information"); + fatal(D.data(), "unexpected end of CIE"); uint8_t B = D.front(); D = D.slice(1); return B; } // Read a null-terminated string. -static StringRef readString(ArrayRef &D) { +template +StringRef ReadHelper::readString(ArrayRef &D) { const uint8_t *End = std::find(D.begin(), D.end(), '\0'); if (End == D.end()) - fatal("corrupted CIE"); + fatal(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; @@ -75,14 +99,15 @@ // Actual number is not of interest because only the runtime needs it. // But we need to be at least able to skip it so that we can read // the field that follows a LEB128 number. -static void skipLeb128(ArrayRef &D) { +template void ReadHelper::skipLeb128(ArrayRef &D) { + const uint8_t *ErrPos = D.data(); while (!D.empty()) { uint8_t Val = D.front(); D = D.slice(1); if ((Val & 0x80) == 0) return; } - fatal("corrupted or unsupported CIE information"); + fatal(ErrPos, "corrupted CIE (failed to read LEB128)"); } template static size_t getAugPSize(unsigned Enc) { @@ -100,72 +125,81 @@ case DW_EH_PE_sdata8: return 8; } - fatal("unknown FDE encoding"); + return 0; } -template static void skipAugP(ArrayRef &D) { +template void ReadHelper::skipAugP(ArrayRef &D) { uint8_t Enc = readByte(D); if ((Enc & 0xf0) == DW_EH_PE_aligned) - fatal("DW_EH_PE_aligned encoding is not supported"); + fatal(D.data() - 1, "DW_EH_PE_aligned encoding is not supported"); size_t Size = getAugPSize(Enc); + if (Size == 0) + fatal(D.data() - 1, "unknown FDE encoding"); if (Size >= D.size()) - fatal("corrupted CIE"); + fatal(D.data() - 1, "corrupted CIE"); D = D.slice(Size); } -template uint8_t getFdeEncoding(ArrayRef D) { +template uint8_t elf::getFdeEncoding(EhSectionPiece *P) { + ArrayRef D = P->data(); + InputSectionBase *IS = static_cast *>(P->ID); + + ReadHelper H(IS); if (D.size() < 8) - fatal("CIE too small"); + H.fatal(D.data(), "CIE too small"); D = D.slice(8); - int Version = readByte(D); + int Version = H.readByte(D); if (Version != 1 && Version != 3) - fatal("FDE version 1 or 3 expected, but got " + Twine(Version)); + H.fatal(D.data() - 1, + "FDE version 1 or 3 expected, but got " + Twine(Version)); - StringRef Aug = readString(D); + StringRef Aug = H.readString(D); // Skip code and data alignment factors. - skipLeb128(D); - skipLeb128(D); + H.skipLeb128(D); + H.skipLeb128(D); // 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) - readByte(D); + H.readByte(D); else - skipLeb128(D); + H.skipLeb128(D); // 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') - return readByte(D); + return H.readByte(D); if (C == 'z') { - skipLeb128(D); + H.skipLeb128(D); continue; } if (C == 'P') { - skipAugP(D); + H.skipAugP(D); continue; } if (C == 'L') { - readByte(D); + H.readByte(D); continue; } - fatal("unknown .eh_frame augmentation string: " + Aug); + H.fatal(Aug.data(), "unknown .eh_frame augmentation string: " + Aug); } return DW_EH_PE_absptr; } -template size_t readEhRecordSize(ArrayRef); -template size_t readEhRecordSize(ArrayRef); -template size_t readEhRecordSize(ArrayRef); -template size_t readEhRecordSize(ArrayRef); - -template uint8_t getFdeEncoding(ArrayRef); -template uint8_t getFdeEncoding(ArrayRef); -template uint8_t getFdeEncoding(ArrayRef); -template uint8_t getFdeEncoding(ArrayRef); -} -} +template size_t elf::readEhRecordSize(InputSectionBase *S, + size_t Off); +template size_t elf::readEhRecordSize(InputSectionBase *S, + size_t Off); +template size_t elf::readEhRecordSize(InputSectionBase *S, + size_t Off); +template size_t elf::readEhRecordSize(InputSectionBase *S, + size_t Off); + +template uint8_t elf::getFdeEncoding(EhSectionPiece *P); +template uint8_t elf::getFdeEncoding(EhSectionPiece *P); +template uint8_t elf::getFdeEncoding(EhSectionPiece *P); +template uint8_t elf::getFdeEncoding(EhSectionPiece *P); Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -210,14 +210,15 @@ }; struct EhSectionPiece : public SectionPiece { - EhSectionPiece(size_t Off, ArrayRef Data, unsigned FirstRelocation) - : SectionPiece(Off, false), Data(Data.data()), Size(Data.size()), + EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size, + unsigned FirstRelocation) + : SectionPiece(Off, false), ID(ID), Size(Size), FirstRelocation(FirstRelocation) {} - const uint8_t *Data; + InputSectionData *ID; uint32_t Size; uint32_t size() const { return Size; } - ArrayRef data() { return {Data, Size}; } + ArrayRef data() { return {ID->Data.data() + this->InputOff, Size}; } unsigned FirstRelocation; }; Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -632,9 +632,8 @@ ArrayRef Data = this->Data; unsigned RelI = 0; for (size_t Off = 0, End = Data.size(); Off != End;) { - size_t Size = readEhRecordSize(Data.slice(Off)); - this->Pieces.emplace_back(Off, Data.slice(Off, Size), - getReloc(Off, Size, Rels, RelI)); + size_t Size = readEhRecordSize(this, Off); + this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI)); // The empty record is the end marker. if (Size == 4) break; Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -502,7 +502,7 @@ // we obtain two addresses and pass them to EhFrameHdr object. if (Out::EhFrameHdr) { for (CieRecord *Cie : Cies) { - uint8_t Enc = getFdeEncoding(Cie->Piece->data()); + uint8_t Enc = getFdeEncoding(Cie->Piece); for (SectionPiece *Fde : Cie->FdePieces) { uintX_t Pc = getFdePc(Buf, Fde->OutputOff, Enc); uintX_t FdeVA = this->Addr + Fde->OutputOff; Index: test/ELF/invalid-cie-length.s =================================================================== --- test/ELF/invalid-cie-length.s +++ test/ELF/invalid-cie-length.s @@ -6,4 +6,4 @@ .section .eh_frame .byte 0 -// CHECK: CIE/FDE too small +// CHECK: {{.*}}:(.eh_frame+0x0): CIE/FDE too small Index: test/ELF/invalid-cie-length2.s =================================================================== --- test/ELF/invalid-cie-length2.s +++ test/ELF/invalid-cie-length2.s @@ -6,4 +6,4 @@ .section .eh_frame .long 42 -// CHECK: CIE/FDE ends past the end of the section +// CHECK: {{.*}}:(.eh_frame+0x0): CIE/FDE ends past the end of the section Index: test/ELF/invalid-cie-length3.s =================================================================== --- test/ELF/invalid-cie-length3.s +++ test/ELF/invalid-cie-length3.s @@ -6,4 +6,4 @@ .section .eh_frame .long 0xFFFFFFFC -// CHECK: CIE/FDE ends past the end of the section +// CHECK: {{.*}}:(.eh_frame+0x0): CIE/FDE ends past the end of the section Index: test/ELF/invalid-cie-length4.s =================================================================== --- test/ELF/invalid-cie-length4.s +++ test/ELF/invalid-cie-length4.s @@ -7,4 +7,4 @@ .long 0xFFFFFFFF .byte 0 -// CHECK: CIE/FDE too large +// CHECK: {{.*}}:(.eh_frame+0x0): CIE/FDE too large