diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 00830ba7836..e12b670c7d3 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -210,6 +210,8 @@ public: } ErrorOr getSectionName(const Elf_Shdr *Section) const; + template + ErrorOr> getSectionContentsAsArray(const Elf_Shdr *Sec) const; ErrorOr > getSectionContents(const Elf_Shdr *Sec) const; }; @@ -243,12 +245,25 @@ ELFFile::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, } template -ErrorOr > -ELFFile::getSectionContents(const Elf_Shdr *Sec) const { - if (Sec->sh_offset + Sec->sh_size > Buf.size()) +template +ErrorOr> +ELFFile::getSectionContentsAsArray(const Elf_Shdr *Sec) const { + uintX_t Offset = Sec->sh_offset; + uintX_t Size = Sec->sh_size; + + if (Size % sizeof(T)) return object_error::parse_failed; - const uint8_t *Start = base() + Sec->sh_offset; - return makeArrayRef(Start, Sec->sh_size); + if (Offset + Size > Buf.size()) + return object_error::parse_failed; + + const T *Start = reinterpret_cast(base() + Offset); + return makeArrayRef(Start, Size / sizeof(T)); +} + +template +ErrorOr> +ELFFile::getSectionContents(const Elf_Shdr *Sec) const { + return getSectionContentsAsArray(Sec); } template