diff options
author | somov <somov@yandex-team.ru> | 2022-02-10 16:45:49 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:49 +0300 |
commit | 7489e4682331202b9c7d863c0898eb83d7b12c2b (patch) | |
tree | 9142afc54d335ea52910662635b898e79e192e49 /tools/fix_elf/patch.h | |
parent | a5950576e397b1909261050b8c7da16db58f10b1 (diff) | |
download | ydb-7489e4682331202b9c7d863c0898eb83d7b12c2b.tar.gz |
Restoring authorship annotation for <somov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'tools/fix_elf/patch.h')
-rw-r--r-- | tools/fix_elf/patch.h | 294 |
1 files changed, 147 insertions, 147 deletions
diff --git a/tools/fix_elf/patch.h b/tools/fix_elf/patch.h index 6a30aba4ea..c3dcd24224 100644 --- a/tools/fix_elf/patch.h +++ b/tools/fix_elf/patch.h @@ -1,175 +1,175 @@ -#pragma once - +#pragma once + #include "elf.h" - + #include <util/generic/string.h> -#include <util/stream/file.h> -#include <util/system/filemap.h> - -namespace NElf { - -template<typename TTo, typename TFrom> -inline TTo Offset(TFrom from, size_t off) { - return reinterpret_cast<TTo>(reinterpret_cast<char*>(from) + off); -} - +#include <util/stream/file.h> +#include <util/system/filemap.h> + +namespace NElf { + +template<typename TTo, typename TFrom> +inline TTo Offset(TFrom from, size_t off) { + return reinterpret_cast<TTo>(reinterpret_cast<char*>(from) + off); +} + bool IsElf(const TString& path); - -class TElf { -public: + +class TElf { +public: TElf(const TString& path) - : Map(path, TFileMap::oRdWr) - { - Map.Map(0, Map.Length()); - Begin = reinterpret_cast<char*>(Map.Ptr()); - + : Map(path, TFileMap::oRdWr) + { + Map.Map(0, Map.Length()); + Begin = reinterpret_cast<char*>(Map.Ptr()); + if (Map.Length() < static_cast<i64>(sizeof(Elf64_Ehdr)) || TStringBuf(Begin, SELFMAG) != ELFMAG) { - ythrow yexception() << path << " is not an ELF file"; - } - } - + ythrow yexception() << path << " is not an ELF file"; + } + } + Elf64_Ehdr* GetHeader() const noexcept { - return reinterpret_cast<Elf64_Ehdr*>(Begin); - } - + return reinterpret_cast<Elf64_Ehdr*>(Begin); + } + char* GetPtr(size_t offset = 0) const noexcept { - return Begin + offset; - } - - Elf64_Shdr* GetSectionByType(Elf64_Word type) const { + return Begin + offset; + } + + Elf64_Shdr* GetSectionByType(Elf64_Word type) const { Elf64_Shdr* r = nullptr; - - for (Elf64_Shdr* p = GetSectionBegin(), *end = GetSectionEnd(); p != end; ++p) { - if (p->sh_type == type) { - if (r) { - ythrow yexception() << "More than one section of type " << type << Endl; - } - - r = p; - } - } - - return r; - } - + + for (Elf64_Shdr* p = GetSectionBegin(), *end = GetSectionEnd(); p != end; ++p) { + if (p->sh_type == type) { + if (r) { + ythrow yexception() << "More than one section of type " << type << Endl; + } + + r = p; + } + } + + return r; + } + size_t GetSectionCount() const noexcept { - size_t count = GetHeader()->e_shnum; - if (count == 0) { - count = GetSection(0)->sh_size; - } - - return count; - } - + size_t count = GetHeader()->e_shnum; + if (count == 0) { + count = GetSection(0)->sh_size; + } + + return count; + } + Elf64_Shdr* GetSectionBegin() const noexcept { - return reinterpret_cast<Elf64_Shdr*>(Begin + GetHeader()->e_shoff); - } - + return reinterpret_cast<Elf64_Shdr*>(Begin + GetHeader()->e_shoff); + } + Elf64_Shdr* GetSectionEnd() const noexcept { - return reinterpret_cast<Elf64_Shdr*>(Begin + GetHeader()->e_shoff) + GetSectionCount(); - } - + return reinterpret_cast<Elf64_Shdr*>(Begin + GetHeader()->e_shoff) + GetSectionCount(); + } + Elf64_Shdr* GetSection(size_t i) const noexcept { - return GetSectionBegin() + i; - } - - Elf64_Shdr* GetSectionsNameSection() const noexcept { - size_t index = GetHeader()->e_shstrndx; - if (index == SHN_XINDEX) { - index = GetSection(0)->sh_link; - } - return GetSection(index); - } - -private: - TFileMap Map; - char* Begin; -}; - -class TSection { -public: - TSection(TElf* elf, Elf64_Shdr* this_) - : Elf(elf) - , This(this_) - { - } - + return GetSectionBegin() + i; + } + + Elf64_Shdr* GetSectionsNameSection() const noexcept { + size_t index = GetHeader()->e_shstrndx; + if (index == SHN_XINDEX) { + index = GetSection(0)->sh_link; + } + return GetSection(index); + } + +private: + TFileMap Map; + char* Begin; +}; + +class TSection { +public: + TSection(TElf* elf, Elf64_Shdr* this_) + : Elf(elf) + , This(this_) + { + } + bool IsNull() const noexcept { - return !This; - } - + return !This; + } + char* GetPtr(size_t offset = 0) const noexcept { - return Elf->GetPtr(This->sh_offset) + offset; - } - + return Elf->GetPtr(This->sh_offset) + offset; + } + TStringBuf GetStr(size_t offset) const noexcept { - return GetPtr(offset); - } - + return GetPtr(offset); + } + TStringBuf GetName() const noexcept { - return TSection{Elf, Elf->GetSectionsNameSection()}.GetPtr(This->sh_name); - } - + return TSection{Elf, Elf->GetSectionsNameSection()}.GetPtr(This->sh_name); + } + size_t GetLink() const noexcept { - return This->sh_link; - } - + return This->sh_link; + } + size_t GetSize() const noexcept { - return This->sh_size; - } - + return This->sh_size; + } + size_t GetEntryCount() const noexcept { - return GetSize() / This->sh_entsize; - } - - template<typename TTo = char> + return GetSize() / This->sh_entsize; + } + + template<typename TTo = char> TTo* GetEntry(size_t i) const noexcept { - return reinterpret_cast<TTo*>(GetPtr(i * This->sh_entsize)); - } - -private: - TElf* Elf; - Elf64_Shdr* This; -}; - -class TVerneedSection : public TSection { -public: - TVerneedSection(TElf* elf) - : TSection(elf, elf->GetSectionByType(SHT_GNU_verneed)) - { - } - + return reinterpret_cast<TTo*>(GetPtr(i * This->sh_entsize)); + } + +private: + TElf* Elf; + Elf64_Shdr* This; +}; + +class TVerneedSection : public TSection { +public: + TVerneedSection(TElf* elf) + : TSection(elf, elf->GetSectionByType(SHT_GNU_verneed)) + { + } + Elf64_Verneed* GetFirstVerneed() const noexcept { - if (!GetSize()) { + if (!GetSize()) { return nullptr; - } - - return reinterpret_cast<Elf64_Verneed*>(GetPtr()); - } - + } + + return reinterpret_cast<Elf64_Verneed*>(GetPtr()); + } + Elf64_Verneed* GetNextVerneed(Elf64_Verneed* v) const noexcept { - if (!v->vn_next) { + if (!v->vn_next) { return nullptr; - } - - return Offset<Elf64_Verneed*>(v, v->vn_next); - } - + } + + return Offset<Elf64_Verneed*>(v, v->vn_next); + } + Elf64_Vernaux* GetFirstVernaux(Elf64_Verneed* v) const noexcept { - if (!v->vn_cnt) { + if (!v->vn_cnt) { return nullptr; - } - - return Offset<Elf64_Vernaux*>(v, v->vn_aux); - } - + } + + return Offset<Elf64_Vernaux*>(v, v->vn_aux); + } + Elf64_Vernaux* GetNextVernaux(Elf64_Vernaux* v) const noexcept { - if (!v->vna_next) { + if (!v->vna_next) { return nullptr; - } - - return Offset<Elf64_Vernaux*>(v, v->vna_next); - } -}; - -} + } + + return Offset<Elf64_Vernaux*>(v, v->vna_next); + } +}; + +} |