Header->getDataEncoding() == ELF::ELFDATA2LSB;
}
- const Elf_Shdr *section_begin() const;
- const Elf_Shdr *section_end() const;
- Elf_Shdr_Range sections() const {
- return makeArrayRef(section_begin(), section_end());
- }
+ ErrorOr<Elf_Shdr_Range> sections() const;
Elf_Sym_Range symbols(const Elf_Shdr *Sec) const {
if (!Sec)
}
template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_begin() const {
+ErrorOr<typename ELFT::ShdrRange> ELFFile<ELFT>::sections() const {
+ // Invalid section header entry size (e_shentsize) in ELF header
if (Header->e_shentsize != sizeof(Elf_Shdr))
- report_fatal_error(
- "Invalid section header entry size (e_shentsize) in ELF header");
- return reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
-}
-
-template <class ELFT>
-const typename ELFFile<ELFT>::Elf_Shdr *ELFFile<ELFT>::section_end() const {
- return section_begin() + getNumSections();
+ return object_error::parse_failed;
+ auto *Begin = reinterpret_cast<const Elf_Shdr *>(base() + Header->e_shoff);
+ return makeArrayRef(Begin, Begin + getNumSections());
}
template <class ELFT>
assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
SymTable->sh_type == ELF::SHT_DYNSYM);
- uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
+ uintptr_t SHT = reinterpret_cast<uintptr_t>((*EF.sections()).begin());
unsigned SymTableIndex =
(reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
relocation_iterator
ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
DataRefImpl RelData;
- uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
+ uintptr_t SHT = reinterpret_cast<uintptr_t>((*EF.sections()).begin());
RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
RelData.d.b = 0;
return relocation_iterator(RelocationRef(RelData, this));
EF(Data.getBuffer(), EC) {
if (EC)
return;
- for (const Elf_Shdr &Sec : EF.sections()) {
+ auto SectionsOrErr = EF.sections();
+ if ((EC = SectionsOrErr.getError()))
+ return;
+ for (const Elf_Shdr &Sec : *SectionsOrErr) {
switch (Sec.sh_type) {
case ELF::SHT_DYNSYM: {
if (DotDynSymSec) {
template <class ELFT>
section_iterator ELFObjectFile<ELFT>::section_begin() const {
- return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
+ return section_iterator(SectionRef(toDRI((*EF.sections()).begin()), this));
}
template <class ELFT>
section_iterator ELFObjectFile<ELFT>::section_end() const {
- return section_iterator(SectionRef(toDRI(EF.section_end()), this));
+ return section_iterator(SectionRef(toDRI((*EF.sections()).end()), this));
}
template <class ELFT>
template <class ELFO>
static const typename ELFO::Elf_Shdr *
findNotEmptySectionByAddress(const ELFO *Obj, uint64_t Addr) {
- for (const auto &Shdr : Obj->sections())
+ for (const auto &Shdr : unwrapOrError(Obj->sections()))
if (Shdr.sh_addr == Addr && Shdr.sh_size > 0)
return &Shdr;
return nullptr;
template <class ELFO>
static const typename ELFO::Elf_Shdr *findSectionByName(const ELFO &Obj,
StringRef Name) {
- for (const auto &Shdr : Obj.sections()) {
+ for (const auto &Shdr : unwrapOrError(Obj.sections())) {
if (Name == unwrapOrError(Obj.getSectionName(&Shdr)))
return &Shdr;
}
LoadSegments.push_back(&Phdr);
}
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
switch (Sec.sh_type) {
case ELF::SHT_SYMTAB:
if (DotSymtabSec != nullptr)
}
DictScope BA(W, "BuildAttributes");
- for (const ELFO::Elf_Shdr &Sec : Obj->sections()) {
+ for (const ELFO::Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
if (Sec.sh_type != ELF::SHT_ARM_ATTRIBUTES)
continue;
template <class ELFT> void ELFDumper<ELFT>::printStackMap() const {
const Elf_Shdr *StackMapSection = nullptr;
- for (const auto &Sec : Obj->sections()) {
+ for (const auto &Sec : unwrapOrError(Obj->sections())) {
StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
if (Name == ".llvm_stackmaps") {
StackMapSection = &Sec;
template <class ELFT> void GNUStyle<ELFT>::printGroupSections(const ELFO *Obj) {
uint32_t SectionIndex = 0;
bool HasGroups = false;
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
if (Sec.sh_type == ELF::SHT_GROUP) {
HasGroups = true;
const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
template <class ELFT> void GNUStyle<ELFT>::printRelocations(const ELFO *Obj) {
bool HasRelocSections = false;
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA)
continue;
HasRelocSections = true;
printField(f);
OS << "\n";
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
Number = to_string(SectionIndex);
Fields[0].Str = Number;
Fields[1].Str = unwrapOrError(Obj->getSectionName(&Sec));
for (const Elf_Phdr &Phdr : Obj->program_headers()) {
std::string Sections;
OS << format(" %2.2d ", Phnum++);
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
// Check if each section is in a segment and then print mapping.
// readelf additionally makes sure it does not print zero sized sections
// at end of segments and for PT_DYNAMIC both start and end of section
if (P.p_type == PT_NOTE)
process(P.p_offset, P.p_filesz);
} else {
- for (const auto &S : Obj->sections())
+ for (const auto &S : unwrapOrError(Obj->sections()))
if (S.sh_type == SHT_NOTE)
process(S.sh_offset, S.sh_size);
}
DictScope Lists(W, "Groups");
uint32_t SectionIndex = 0;
bool HasGroups = false;
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
if (Sec.sh_type == ELF::SHT_GROUP) {
HasGroups = true;
const Elf_Shdr *Symtab = unwrapOrError(Obj->getSection(Sec.sh_link));
ListScope D(W, "Relocations");
int SectionNumber = -1;
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
++SectionNumber;
if (Sec.sh_type != ELF::SHT_REL && Sec.sh_type != ELF::SHT_RELA)
ListScope SectionsD(W, "Sections");
int SectionIndex = -1;
- for (const Elf_Shdr &Sec : Obj->sections()) {
+ for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
++SectionIndex;
StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));