#include "DWARF.h"
#include "InputSection.h"
#include "Symbols.h"
#include "lld/Common/Memory.h"
#include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
#include "llvm/Object/ELFObjectFile.h"
using namespace llvm;
using namespace llvm::object;
using namespace lld;
using namespace lld::elf;
template <class ELFT> LLDDwarfObj<ELFT>::LLDDwarfObj(ObjFile<ELFT> *obj) {
ArrayRef<typename ELFT::Shdr> objSections = obj->template getELFShdrs<ELFT>();
assert(objSections.size() == obj->getSections().size());
for (auto [i, sec] : llvm::enumerate(obj->getSections())) {
if (!sec)
continue;
if (LLDDWARFSection *m =
StringSwitch<LLDDWARFSection *>(sec->name)
.Case(".debug_addr", &addrSection)
.Case(".debug_gnu_pubnames", &gnuPubnamesSection)
.Case(".debug_gnu_pubtypes", &gnuPubtypesSection)
.Case(".debug_line", &lineSection)
.Case(".debug_loclists", &loclistsSection)
.Case(".debug_names", &namesSection)
.Case(".debug_ranges", &rangesSection)
.Case(".debug_rnglists", &rnglistsSection)
.Case(".debug_str_offsets", &strOffsetsSection)
.Default(nullptr)) {
m->Data = toStringRef(sec->contentMaybeDecompress());
m->sec = sec;
continue;
}
if (sec->name == ".debug_abbrev")
abbrevSection = toStringRef(sec->contentMaybeDecompress());
else if (sec->name == ".debug_str")
strSection = toStringRef(sec->contentMaybeDecompress());
else if (sec->name == ".debug_line_str")
lineStrSection = toStringRef(sec->contentMaybeDecompress());
else if (sec->name == ".debug_info" &&
!(objSections[i].sh_flags & ELF::SHF_GROUP)) {
infoSection.Data = toStringRef(sec->contentMaybeDecompress());
infoSection.sec = sec;
}
}
}
namespace {
template <class RelTy> struct LLDRelocationResolver {
static uint64_t resolve(uint64_t , uint64_t , uint64_t s,
uint64_t , int64_t addend) {
return s + addend;
}
};
template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
static uint64_t resolve(uint64_t , uint64_t , uint64_t s,
uint64_t locData, int64_t ) {
return s + locData;
}
};
}
template <class ELFT>
template <class RelTy>
std::optional<RelocAddrEntry>
LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
ArrayRef<RelTy> rels) const {
auto it =
partition_point(rels, [=](const RelTy &a) { return a.r_offset < pos; });
if (it == rels.end() || it->r_offset != pos)
return std::nullopt;
const RelTy &rel = *it;
const ObjFile<ELFT> *file = sec.getFile<ELFT>();
uint32_t symIndex = rel.getSymbol(config->isMips64EL);
const typename ELFT::Sym &sym = file->template getELFSyms<ELFT>()[symIndex];
uint32_t secIndex = file->getSectionIndex(sym);
Symbol &s = file->getRelocTargetSym(rel);
uint64_t val = 0;
if (auto *dr = dyn_cast<Defined>(&s))
val = dr->value;
DataRefImpl d;
d.p = getAddend<ELFT>(rel);
return RelocAddrEntry{secIndex, RelocationRef(d, nullptr),
val, std::optional<object::RelocationRef>(),
0, LLDRelocationResolver<RelTy>::resolve};
}
template <class ELFT>
std::optional<RelocAddrEntry>
LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {
auto &sec = static_cast<const LLDDWARFSection &>(s);
const RelsOrRelas<ELFT> rels =
sec.sec->template relsOrRelas<ELFT>(false);
if (rels.areRelocsRel())
return findAux(*sec.sec, pos, rels.rels);
return findAux(*sec.sec, pos, rels.relas);
}
template class elf::LLDDwarfObj<ELF32LE>;
template class elf::LLDDwarfObj<ELF32BE>;
template class elf::LLDDwarfObj<ELF64LE>;
template class elf::LLDDwarfObj<ELF64BE>;