2016-05-24 10:55:45 +08:00
|
|
|
//===- EhFrame.cpp -------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// .eh_frame section contains information on how to unwind the stack when
|
|
|
|
// an exception is thrown. The section consists of sequence of CIE and FDE
|
|
|
|
// records. The linker needs to merge CIEs and associate FDEs to CIEs.
|
|
|
|
// That means the linker has to understand the format of the section.
|
|
|
|
//
|
|
|
|
// This file contains a few utility functions to read .eh_frame contents.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "EhFrame.h"
|
|
|
|
#include "Error.h"
|
2016-11-23 17:45:17 +08:00
|
|
|
#include "InputSection.h"
|
|
|
|
#include "Relocations.h"
|
2016-11-20 02:44:09 +08:00
|
|
|
#include "Strings.h"
|
2016-05-24 10:55:45 +08:00
|
|
|
|
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
#include "llvm/Support/Dwarf.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::ELF;
|
|
|
|
using namespace llvm::dwarf;
|
|
|
|
using namespace llvm::object;
|
|
|
|
using namespace llvm::support::endian;
|
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
using namespace lld;
|
|
|
|
using namespace lld::elf;
|
2016-05-24 10:55:45 +08:00
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
namespace {
|
|
|
|
template <class ELFT> class EhReader {
|
|
|
|
public:
|
|
|
|
EhReader(InputSectionBase<ELFT> *S, ArrayRef<uint8_t> D) : IS(S), D(D) {}
|
|
|
|
size_t readEhRecordSize();
|
|
|
|
uint8_t getFdeEncoding();
|
|
|
|
|
|
|
|
private:
|
|
|
|
template <class P> void failOn(const P *Loc, const Twine &Msg) {
|
2016-11-26 02:51:53 +08:00
|
|
|
fatal(IS->getLocation((const uint8_t *)Loc - IS->Data.data()) + ": " + Msg);
|
2016-11-23 17:45:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t readByte();
|
|
|
|
void skipBytes(size_t Count);
|
|
|
|
StringRef readString();
|
|
|
|
void skipLeb128();
|
|
|
|
void skipAugP();
|
|
|
|
|
|
|
|
InputSectionBase<ELFT> *IS;
|
|
|
|
ArrayRef<uint8_t> D;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT>
|
|
|
|
size_t elf::readEhRecordSize(InputSectionBase<ELFT> *S, size_t Off) {
|
|
|
|
return EhReader<ELFT>(S, S->Data.slice(Off)).readEhRecordSize();
|
|
|
|
}
|
2016-05-24 10:55:45 +08:00
|
|
|
// .eh_frame section is a sequence of records. Each record starts with
|
|
|
|
// a 4 byte length field. This function reads the length.
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> size_t EhReader<ELFT>::readEhRecordSize() {
|
2016-05-24 10:55:45 +08:00
|
|
|
const endianness E = ELFT::TargetEndianness;
|
|
|
|
if (D.size() < 4)
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data(), "CIE/FDE too small");
|
2016-05-24 10:55:45 +08:00
|
|
|
|
|
|
|
// 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<E>(D.data());
|
|
|
|
if (V == UINT32_MAX)
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data(), "CIE/FDE too large");
|
2016-05-24 10:55:45 +08:00
|
|
|
uint64_t Size = V + 4;
|
|
|
|
if (Size > D.size())
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data(), "CIE/FDE ends past the end of the section");
|
2016-05-24 10:55:45 +08:00
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a byte and advance D by one byte.
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> uint8_t EhReader<ELFT>::readByte() {
|
2016-05-24 10:55:45 +08:00
|
|
|
if (D.empty())
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data(), "unexpected end of CIE");
|
2016-05-24 10:55:45 +08:00
|
|
|
uint8_t B = D.front();
|
|
|
|
D = D.slice(1);
|
|
|
|
return B;
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> void EhReader<ELFT>::skipBytes(size_t Count) {
|
|
|
|
if (D.size() < Count)
|
|
|
|
failOn(D.data(), "CIE is too small");
|
|
|
|
D = D.slice(Count);
|
|
|
|
}
|
|
|
|
|
2016-11-20 02:44:09 +08:00
|
|
|
// Read a null-terminated string.
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> StringRef EhReader<ELFT>::readString() {
|
2016-11-20 02:44:09 +08:00
|
|
|
const uint8_t *End = std::find(D.begin(), D.end(), '\0');
|
|
|
|
if (End == D.end())
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data(), "corrupted CIE (failed to read string)");
|
2016-11-20 02:44:09 +08:00
|
|
|
StringRef S = toStringRef(D.slice(0, End - D.begin()));
|
|
|
|
D = D.slice(S.size() + 1);
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2016-05-24 10:55:45 +08:00
|
|
|
// Skip an integer encoded in the LEB128 format.
|
|
|
|
// 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.
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> void EhReader<ELFT>::skipLeb128() {
|
|
|
|
const uint8_t *ErrPos = D.data();
|
2016-05-24 10:55:45 +08:00
|
|
|
while (!D.empty()) {
|
|
|
|
uint8_t Val = D.front();
|
|
|
|
D = D.slice(1);
|
|
|
|
if ((Val & 0x80) == 0)
|
|
|
|
return;
|
|
|
|
}
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(ErrPos, "corrupted CIE (failed to read LEB128)");
|
2016-05-24 10:55:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class ELFT> static size_t getAugPSize(unsigned Enc) {
|
|
|
|
switch (Enc & 0x0f) {
|
|
|
|
case DW_EH_PE_absptr:
|
|
|
|
case DW_EH_PE_signed:
|
|
|
|
return ELFT::Is64Bits ? 8 : 4;
|
|
|
|
case DW_EH_PE_udata2:
|
|
|
|
case DW_EH_PE_sdata2:
|
|
|
|
return 2;
|
|
|
|
case DW_EH_PE_udata4:
|
|
|
|
case DW_EH_PE_sdata4:
|
|
|
|
return 4;
|
|
|
|
case DW_EH_PE_udata8:
|
|
|
|
case DW_EH_PE_sdata8:
|
|
|
|
return 8;
|
|
|
|
}
|
2016-11-23 17:45:17 +08:00
|
|
|
return 0;
|
2016-05-24 10:55:45 +08:00
|
|
|
}
|
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> void EhReader<ELFT>::skipAugP() {
|
|
|
|
uint8_t Enc = readByte();
|
2016-05-24 10:55:45 +08:00
|
|
|
if ((Enc & 0xf0) == DW_EH_PE_aligned)
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data() - 1, "DW_EH_PE_aligned encoding is not supported");
|
2016-05-24 10:55:45 +08:00
|
|
|
size_t Size = getAugPSize<ELFT>(Enc);
|
2016-11-23 17:45:17 +08:00
|
|
|
if (Size == 0)
|
|
|
|
failOn(D.data() - 1, "unknown FDE encoding");
|
2016-05-24 10:55:45 +08:00
|
|
|
if (Size >= D.size())
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data() - 1, "corrupted CIE");
|
2016-05-24 10:55:45 +08:00
|
|
|
D = D.slice(Size);
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> uint8_t elf::getFdeEncoding(EhSectionPiece *P) {
|
|
|
|
auto *IS = static_cast<InputSectionBase<ELFT> *>(P->ID);
|
|
|
|
return EhReader<ELFT>(IS, P->data()).getFdeEncoding();
|
|
|
|
}
|
2016-05-24 10:55:45 +08:00
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
template <class ELFT> uint8_t EhReader<ELFT>::getFdeEncoding() {
|
|
|
|
skipBytes(8);
|
|
|
|
int Version = readByte();
|
2016-05-24 10:55:45 +08:00
|
|
|
if (Version != 1 && Version != 3)
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(D.data() - 1,
|
|
|
|
"FDE version 1 or 3 expected, but got " + Twine(Version));
|
2016-05-24 10:55:45 +08:00
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
StringRef Aug = readString();
|
2016-05-24 10:55:45 +08:00
|
|
|
|
2016-11-20 02:44:09 +08:00
|
|
|
// Skip code and data alignment factors.
|
2016-11-23 17:45:17 +08:00
|
|
|
skipLeb128();
|
|
|
|
skipLeb128();
|
2016-05-24 10:55:45 +08:00
|
|
|
|
|
|
|
// 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)
|
2016-11-23 17:45:17 +08:00
|
|
|
readByte();
|
2016-05-24 10:55:45 +08:00
|
|
|
else
|
2016-11-23 17:45:17 +08:00
|
|
|
skipLeb128();
|
2016-05-24 10:55:45 +08:00
|
|
|
|
|
|
|
// 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')
|
2016-11-23 17:45:17 +08:00
|
|
|
return readByte();
|
2016-05-24 10:55:45 +08:00
|
|
|
if (C == 'z') {
|
2016-11-23 17:45:17 +08:00
|
|
|
skipLeb128();
|
2016-05-24 10:55:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (C == 'P') {
|
2016-11-23 17:45:17 +08:00
|
|
|
skipAugP();
|
2016-05-24 10:55:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (C == 'L') {
|
2016-11-23 17:45:17 +08:00
|
|
|
readByte();
|
2016-05-24 10:55:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-11-23 17:45:17 +08:00
|
|
|
failOn(Aug.data(), "unknown .eh_frame augmentation string: " + Aug);
|
2016-05-24 10:55:45 +08:00
|
|
|
}
|
|
|
|
return DW_EH_PE_absptr;
|
|
|
|
}
|
|
|
|
|
2016-11-23 17:45:17 +08:00
|
|
|
template size_t elf::readEhRecordSize<ELF32LE>(InputSectionBase<ELF32LE> *S,
|
|
|
|
size_t Off);
|
|
|
|
template size_t elf::readEhRecordSize<ELF32BE>(InputSectionBase<ELF32BE> *S,
|
|
|
|
size_t Off);
|
|
|
|
template size_t elf::readEhRecordSize<ELF64LE>(InputSectionBase<ELF64LE> *S,
|
|
|
|
size_t Off);
|
|
|
|
template size_t elf::readEhRecordSize<ELF64BE>(InputSectionBase<ELF64BE> *S,
|
|
|
|
size_t Off);
|
|
|
|
|
|
|
|
template uint8_t elf::getFdeEncoding<ELF32LE>(EhSectionPiece *P);
|
|
|
|
template uint8_t elf::getFdeEncoding<ELF32BE>(EhSectionPiece *P);
|
|
|
|
template uint8_t elf::getFdeEncoding<ELF64LE>(EhSectionPiece *P);
|
|
|
|
template uint8_t elf::getFdeEncoding<ELF64BE>(EhSectionPiece *P);
|