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

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 #include "elfsectionheader.h"
0019 #include "elffile.h"
0020 #include "elfheader.h"
0021 #include "elfstringtablesection.h"
0022 
0023 #include <cassert>
0024 
0025 ElfSectionHeader::ElfSectionHeader(ElfFile* file, uint16_t sectionIndex) :
0026     m_file(file), m_sectionIndex(sectionIndex)
0027 {
0028     assert(file);
0029 }
0030 
0031 ElfSectionHeader::~ElfSectionHeader() = default;
0032 
0033 uint16_t ElfSectionHeader::sectionIndex() const
0034 {
0035     return m_sectionIndex;
0036 }
0037 
0038 uint64_t ElfSectionHeader::headerOffset() const
0039 {
0040     return m_file->header()->sectionHeaderTableOffset() + sectionIndex() * m_file->header()->sectionHeaderEntrySize();
0041 }
0042 
0043 const char* ElfSectionHeader::name() const
0044 {
0045     auto strTab = m_file->section<ElfStringTableSection>(m_file->header()->stringTableSectionHeader());
0046     if (!strTab)
0047         return nullptr;
0048     return strTab->string(nameIndex());
0049 }
0050 
0051 bool ElfSectionHeader::isDebugInformation() const
0052 {
0053     return strncmp(name(), ".debug", 6) == 0 || strncmp(name(), ".gdb", 4) == 0;
0054 }
0055 
0056 uint64_t ElfSectionHeader::entryCount() const
0057 {
0058     if (entrySize() <= 0)
0059         return 0;
0060     return size() / entrySize();
0061 }
0062 
0063 ElfFile* ElfSectionHeader::file() const
0064 {
0065     return m_file;
0066 }