summaryrefslogtreecommitdiff
path: root/src/elf_image.h
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2024-03-03 18:38:46 +0300
committerOxore <oxore@protonmail.com>2024-03-03 18:43:31 +0300
commitc993531d0678de5e29c943fdbb912e1f20957765 (patch)
tree04c827e3a4f7b739c7bb0c655790bfd0e3401e2b /src/elf_image.h
parent3ae20774096ddb42ea03142d0c55f9564da4ba50 (diff)
Impl ELF symbols extraction
Diffstat (limited to 'src/elf_image.h')
-rw-r--r--src/elf_image.h34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/elf_image.h b/src/elf_image.h
index b7c7123..b753008 100644
--- a/src/elf_image.h
+++ b/src/elf_image.h
@@ -13,7 +13,7 @@ namespace ELF {
struct ProgramHeader32Table {
const ProgramHeader32 *headers{};
size_t size{};
- static ProgramHeader32Table FromBytes(const DataView &, DataEncoding);
+ static ProgramHeader32Table FromView(const DataView &, DataEncoding);
};
struct Segment {
@@ -26,6 +26,7 @@ class Image {
char *const _error;
const Header32 _h;
const ProgramHeader32Table _pht;
+ const SectionHeader32 _shstrtab, _symtab, _strtab;
public:
explicit Image(DataBuffer&&);
~Image();
@@ -50,6 +51,37 @@ public:
return DataView{};
};
constexpr const char *Error() const { return _error; }
+ ELF::SectionHeader32 GetSectionHeaderByName(const char *name) const;
+ constexpr const ELF::SectionHeader32 GetSectionHeader(uint32_t index) const
+ {
+ if (index > _h.shnum) {
+ return SectionHeader32{};
+ }
+ const size_t offset = _h.shoff + kSectionHeaderSize * index;
+ if (offset + kSectionHeaderSize > _data.buffer_size) {
+ return SectionHeader32{};
+ }
+ return SectionHeader32::FromBytes(
+ _data.buffer + offset, _h.ident.data_encoding);
+ }
+ uint32_t GetSectionHeaderIndexByName(const char *name) const;
+ constexpr ELF::Symbol32 GetSymbolByIndex(uint32_t index) const
+ {
+ if (!IsValid()) {
+ return Symbol32{};
+ }
+ if (_symtab.entsize == 0 || index >= _symtab.size / _symtab.entsize) {
+ return Symbol32{};
+ }
+ auto symbol = Symbol32::FromBytes(
+ _data.buffer + _symtab.offset + _symtab.entsize * index,
+ _h.ident.data_encoding);
+ if (symbol.namendx < _strtab.size && _data.buffer[_strtab.offset + _strtab.size] == '\0') {
+ symbol.name = reinterpret_cast<const char *>(
+ _data.buffer + _strtab.offset + symbol.namendx);
+ }
+ return symbol;
+ }
};
}