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 #ifndef ELFNOTEENTRY_IMPL_H
0019 #define ELFNOTEENTRY_IMPL_H
0020 
0021 #include "elfnoteentry.h"
0022 
0023 template <typename T>
0024 class ElfNoteEntryImpl : public ElfNoteEntry
0025 {
0026 public:
0027     explicit ElfNoteEntryImpl(const ElfNoteSection* section, uint64_t offset) :
0028         ElfNoteEntry(section),
0029         m_note(reinterpret_cast<const T*>(section->rawData() + offset))
0030     {
0031     }
0032 
0033     uint64_t type() const final override
0034     {
0035         return m_note->n_type;
0036     }
0037 
0038     uint64_t size() const final override
0039     {
0040         const uint64_t nameszSize = sizeof(((T*)nullptr)->n_namesz);
0041         return alignTo(nameSize(), nameszSize) + alignTo(descriptionSize(), nameszSize) + sizeof(T);
0042     }
0043 
0044     const char* name() const final override
0045     {
0046         return reinterpret_cast<const char*>(m_note) + sizeof(T);
0047     }
0048 
0049     uint64_t descriptionSize() const final override
0050     {
0051         return m_note->n_descsz;
0052     }
0053 
0054     const char* descriptionData() const final override
0055     {
0056         return reinterpret_cast<const char*>(m_note) + sizeof(T) + alignTo(nameSize(), sizeof(((T*)nullptr)->n_namesz));
0057     }
0058 
0059 protected:
0060     uint64_t nameSize() const final override
0061     {
0062         return m_note->n_namesz;
0063     }
0064 
0065 private:
0066     static uint64_t alignTo(uint64_t value, uint64_t size)
0067     {
0068         const auto r = value % size;
0069         if (!r)
0070             return value;
0071         return value + size - r;
0072     }
0073 
0074     const T * const m_note;
0075 };
0076 
0077 #endif