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 "elfgnusymbolversiondefinitionssection.h"
0019 #include "elfgnusymbolversiondefinition.h"
0020 #include "elffile.h"
0021 
0022 #include <elf.h>
0023 
0024 ElfGNUSymbolVersionDefinitionsSection::ElfGNUSymbolVersionDefinitionsSection(ElfFile* file, ElfSectionHeader* shdr):
0025     ElfSection(file, shdr)
0026 {
0027 }
0028 
0029 ElfGNUSymbolVersionDefinitionsSection::~ElfGNUSymbolVersionDefinitionsSection()
0030 {
0031     qDeleteAll(m_versionDefinitions);
0032 }
0033 
0034 uint32_t ElfGNUSymbolVersionDefinitionsSection::entryCount() const
0035 {
0036     return m_versionDefinitions.size();
0037 }
0038 
0039 ElfGNUSymbolVersionDefinition* ElfGNUSymbolVersionDefinitionsSection::definition(uint32_t index) const
0040 {
0041     return m_versionDefinitions.at(index);
0042 }
0043 
0044 ElfGNUSymbolVersionDefinition* ElfGNUSymbolVersionDefinitionsSection::definitionForVersionIndex(uint16_t index) const
0045 {
0046     foreach (auto def, m_versionDefinitions) {
0047         if ((def->versionIndex() & 0x7FFF) == (index & 0x7FFF))
0048             return def;
0049     }
0050     return nullptr;
0051 }
0052 
0053 void ElfGNUSymbolVersionDefinitionsSection::parse()
0054 {
0055     // TODO parse until nextOffset() is 0 might be an alternative, removes dependency on dynamicSection() being available here
0056     const auto verDefNum = file()->dynamicSection()->entryWithTag(DT_VERDEFNUM);
0057     if (!verDefNum)
0058         return;
0059 
0060     uint32_t offset = 0;
0061     m_versionDefinitions.reserve(verDefNum->value());
0062     for (uint i = 0; i < verDefNum->value(); ++i) {
0063         const auto entry = new ElfGNUSymbolVersionDefinition(this, offset);
0064         m_versionDefinitions.push_back(entry);
0065         offset += entry->nextOffset();
0066     }
0067 }