File indexing completed on 2024-05-12 05:43:29

0001 /*
0002     Copyright (C) 2013-2014 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #ifndef ELFHEADER_H
0019 #define ELFHEADER_H
0020 
0021 #include <cstdint>
0022 
0023 /** Size-independent adaptor to ElfXX_Ehdr. */
0024 class ElfHeader
0025 {
0026 public:
0027     ElfHeader() = default;
0028     ElfHeader(const ElfHeader&) = delete;
0029     virtual ~ElfHeader();
0030 
0031     ElfHeader& operator=(const ElfHeader&) = delete;
0032 
0033     virtual uint16_t type() const = 0;
0034     virtual uint16_t machine() const = 0;
0035 //     virtual uint32_t version() const = 0;
0036     virtual uint64_t entryPoint() const = 0;
0037     virtual uint64_t programHeaderTableOffset() const = 0;
0038     virtual uint64_t sectionHeaderTableOffset() const = 0;
0039     virtual uint32_t flags() const = 0;
0040 //     virtual uint16_t headerSize() const = 0;
0041     virtual uint16_t programHeaderEntrySize() const = 0;
0042     virtual uint16_t programHeaderCount() const = 0;
0043     virtual uint16_t sectionHeaderEntrySize() const = 0;
0044     virtual uint16_t sectionHeaderCount() const = 0;
0045     virtual uint16_t stringTableSectionHeader() const = 0;
0046 };
0047 
0048 template <typename T>
0049 class ElfHeaderImpl : public ElfHeader
0050 {
0051 public:
0052     explicit inline ElfHeaderImpl(unsigned char *data) : m_hdr(reinterpret_cast<T*>(data)) {}
0053 
0054     inline uint16_t type() const override { return m_hdr->e_type; }
0055     inline uint16_t machine() const override { return m_hdr->e_machine; }
0056     inline uint64_t entryPoint() const override { return m_hdr->e_entry; }
0057     inline uint64_t programHeaderTableOffset() const override { return m_hdr->e_phoff; }
0058     inline uint64_t sectionHeaderTableOffset() const override { return m_hdr->e_shoff; }
0059     inline uint32_t flags() const override { return m_hdr->e_flags; }
0060     inline  uint16_t programHeaderCount() const override { return m_hdr->e_phnum; }
0061     inline uint16_t programHeaderEntrySize() const override { return m_hdr->e_phentsize; }
0062     inline uint16_t sectionHeaderEntrySize() const override { return m_hdr->e_shentsize; }
0063     inline uint16_t sectionHeaderCount() const override { return m_hdr->e_shnum; }
0064     inline uint16_t stringTableSectionHeader() const override { return m_hdr->e_shstrndx; }
0065 
0066 private:
0067     T* m_hdr;
0068 };
0069 
0070 #endif // ELFHEADER_H