[elf] Add get_section_by_name

Add the get_section_by_name function to iterate sections and compare name strings.
This commit is contained in:
Justin C. Miller
2024-02-18 17:22:23 -08:00
parent ba6e8e1349
commit 55a485ee67
2 changed files with 31 additions and 3 deletions

View File

@@ -13,10 +13,11 @@ const T *convert(util::const_buffer data, size_t offset) {
} }
file::file(util::const_buffer data) : file::file(util::const_buffer data) :
m_segments(convert<segment_header>(data, fh(data)->ph_offset), fh(data)->ph_entsize, fh(data)->ph_num), m_segments {convert<segment_header>(data, fh(data)->ph_offset), fh(data)->ph_entsize, fh(data)->ph_num},
m_sections(convert<section_header>(data, fh(data)->sh_offset), fh(data)->sh_entsize, fh(data)->sh_num), m_sections {convert<section_header>(data, fh(data)->sh_offset), fh(data)->sh_entsize, fh(data)->sh_num},
m_data(data) m_data {data}
{ {
} }
bool bool
@@ -52,4 +53,29 @@ file::entrypoint() const
} }
const section_header *
file::get_section_by_name(const char *name)
{
if (!name)
return nullptr;
const section_header &shstrtab = m_sections[header()->sh_str_idx];
util::counted<const char> strings = {
reinterpret_cast<const char*>(m_data.pointer) + shstrtab.offset,
shstrtab.size,
};
for (auto &sec : m_sections) {
const char *section_name = &strings[sec.name_offset];
size_t i = 0;
// Can't depend on libc, do our own basic strcmp here
while (name[i] && section_name[i] && name[i] == section_name[i]) ++i;
if (name[i] == section_name[i])
return &sec;
}
return nullptr;
}
} // namespace elf } // namespace elf

View File

@@ -67,6 +67,8 @@ public:
return reinterpret_cast<const file_header *>(m_data.pointer); return reinterpret_cast<const file_header *>(m_data.pointer);
} }
const section_header * get_section_by_name(const char *);
/// Get the ELF file type from the header /// Get the ELF file type from the header
inline filetype type() const { return header()->file_type; } inline filetype type() const { return header()->file_type; }