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

0001 /*
0002     Copyright (C) 2015 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 #include "elfgnusymbolversiondefinition.h"
0019 #include "elfgnusymbolversiondefinitionauxiliaryentry.h"
0020 
0021 #include <elf.h>
0022 
0023 #include <cassert>
0024 
0025 ElfGNUSymbolVersionDefinition::ElfGNUSymbolVersionDefinition(ElfGNUSymbolVersionDefinitionsSection* section, uint32_t offset) :
0026     m_section(section),
0027     m_verDef(reinterpret_cast<const Elf64_Verdef*>(section->rawData() + offset))
0028 {
0029     // 32bit and 64bit have exactly the same memory layout
0030     static_assert(sizeof(Elf32_Verdef) == sizeof(Elf64_Verdef), "SHT_GNU_verdef memory layout changed");
0031 
0032     assert(m_verDef->vd_version == 1);
0033     static_assert(VER_DEF_CURRENT == 1, "SHT_GNU_verdef format changed!");
0034 
0035     uint32_t auxOffset = 0;
0036     m_auxEntries.reserve(auxiliarySize());
0037     for (int i = 0; i < auxiliarySize(); ++i) {
0038         const auto auxEntry = new ElfGNUSymbolVersionDefinitionAuxiliaryEntry(this, auxOffset);
0039         m_auxEntries.push_back(auxEntry);
0040         auxOffset += auxEntry->nextAuxiliaryEntryOffset();
0041     }
0042 
0043     assert(auxiliarySize() == m_auxEntries.size());
0044 }
0045 
0046 ElfGNUSymbolVersionDefinition::~ElfGNUSymbolVersionDefinition()
0047 {
0048     qDeleteAll(m_auxEntries);
0049 }
0050 
0051 ElfGNUSymbolVersionDefinitionsSection* ElfGNUSymbolVersionDefinition::section() const
0052 {
0053     return m_section;
0054 }
0055 
0056 uint16_t ElfGNUSymbolVersionDefinition::flags() const
0057 {
0058     return m_verDef->vd_flags;
0059 }
0060 
0061 uint16_t ElfGNUSymbolVersionDefinition::versionIndex() const
0062 {
0063     return m_verDef->vd_ndx;
0064 }
0065 
0066 uint16_t ElfGNUSymbolVersionDefinition::auxiliarySize() const
0067 {
0068     return m_verDef->vd_cnt;
0069 }
0070 
0071 uint32_t ElfGNUSymbolVersionDefinition::hash() const
0072 {
0073     return m_verDef->vd_hash;
0074 }
0075 
0076 uint32_t ElfGNUSymbolVersionDefinition::auxiliaryOffset() const
0077 {
0078     return m_verDef->vd_aux;
0079 }
0080 
0081 uint32_t ElfGNUSymbolVersionDefinition::nextOffset() const
0082 {
0083     return m_verDef->vd_next;
0084 }
0085 
0086 uint32_t ElfGNUSymbolVersionDefinition::size() const
0087 {
0088     if (nextOffset())
0089         return nextOffset();
0090     return section()->size() - (reinterpret_cast<const unsigned char*>(m_verDef) - section()->rawData());
0091 }
0092 
0093 ElfGNUSymbolVersionDefinitionAuxiliaryEntry* ElfGNUSymbolVersionDefinition::auxiliaryEntry(uint32_t index) const
0094 {
0095     return m_auxEntries.at(index);
0096 }
0097 
0098 const unsigned char* ElfGNUSymbolVersionDefinition::rawData() const
0099 {
0100     return reinterpret_cast<const unsigned char*>(m_verDef);
0101 }