Skip to content

Commit 4bd767d

Browse files
committed
Update Section_t structure
1 parent 12b899e commit 4bd767d

4 files changed

Lines changed: 14 additions & 18 deletions

File tree

include/dynlibutils/module.hpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,14 @@
3939

4040
namespace DynLibUtils {
4141

42-
struct Section_t
42+
struct Section_t : public CMemory // Start address of the section.
4343
{
4444
// Constructors.
45-
Section_t(size_t nSectionSize = 0, const std::string_view& svSectionName = {}, CMemory pSectionBase = nullptr) noexcept : m_nSectionSize(nSectionSize), m_svSectionName(svSectionName), m_pBase(pSectionBase) {} // Default one.
46-
Section_t(Section_t&& other) noexcept : m_nSectionSize(std::move(other.m_nSectionSize)), m_svSectionName(std::move(other.m_svSectionName)), m_pBase(std::move(other.m_pBase)) {}
47-
48-
[[nodiscard]]
49-
bool IsValid() const noexcept { return m_pBase.IsValid(); }
45+
Section_t(CMemory pSectionBase = nullptr, size_t nSectionSize = 0, const std::string_view& svSectionName = {}) noexcept : CMemory(pSectionBase), m_nSectionSize(nSectionSize), m_svSectionName(svSectionName) {} // Default one.
46+
Section_t(Section_t&& other) noexcept = default;
5047

5148
std::size_t m_nSectionSize; // Size of the section.
5249
std::string m_svSectionName; // Name of the section.
53-
CMemory m_pBase; // Start address of the section.
5450
}; // struct Section_t
5551

5652
static constexpr std::size_t s_nDefaultPatternSize = 128;
@@ -332,7 +328,7 @@ class CModule : public CMemory
332328
if (!pSection || !pSection->IsValid())
333329
return DYNLIB_INVALID_MEMORY;
334330

335-
const std::uintptr_t base = pSection->m_pBase;
331+
const std::uintptr_t base = pSection->GetAddr();
336332
const std::size_t sectionSize = pSection->m_nSectionSize;
337333
const std::size_t patternSize = svMask.size();
338334

@@ -429,7 +425,7 @@ class CModule : public CMemory
429425
if (!pSection || !pSection->IsValid())
430426
return 0;
431427

432-
const CMemory pBase = pSection->m_pBase;
428+
const CMemory pBase = *pSection;
433429
const std::size_t sectionSize = pSection->m_nSectionSize;
434430

435431
CMemory pIter = pStartAddress ? pStartAddress : pBase;

src/apple/module.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ bool CModule::LoadFromPath(const std::string_view svModelePath, int flags)
136136
for (uint32_t j = 0; j < seg->nsects; ++j) {
137137
const MachSection& section = sec[j];
138138
m_vecSections.emplace_back(
139+
GetAddr() + section.addr,
139140
section.size,
140-
section.sectname,
141-
GetAddr() + section.addr
141+
section.sectname
142142
);
143143
}
144144
}

src/linux/module.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ bool CModule::LoadFromPath(const std::string_view svModelePath, int flags)
123123
if (map != MAP_FAILED)
124124
{
125125
ElfW(Ehdr)* ehdr = static_cast<ElfW(Ehdr)*>(map);
126-
ElfW(Shdr)* shdrs = reinterpret_cast<ElfW(Shdr)*>(reinterpret_cast<uintptr_t>(ehdr) + ehdr->e_shoff);
127-
const char* strTab = reinterpret_cast<const char*>(reinterpret_cast<uintptr_t>(ehdr) + shdrs[ehdr->e_shstrndx].sh_offset);
126+
ElfW(Shdr)* shdrs = reinterpret_cast<ElfW(Shdr)*>(reinterpret_cast<std::uintptr_t>(ehdr) + ehdr->e_shoff);
127+
const char* strTab = reinterpret_cast<const char*>(reinterpret_cast<std::uintptr_t>(ehdr) + shdrs[ehdr->e_shstrndx].sh_offset);
128128

129129
for (auto i = 0; i < ehdr->e_shnum; ++i) // Loop through the sections.
130130
{
131-
ElfW(Shdr)* shdr = reinterpret_cast<ElfW(Shdr)*>(reinterpret_cast<uintptr_t>(shdrs) + i * ehdr->e_shentsize);
131+
ElfW(Shdr)* shdr = reinterpret_cast<ElfW(Shdr)*>(reinterpret_cast<std::uintptr_t>(shdrs) + i * ehdr->e_shentsize);
132132
if (*(strTab + shdr->sh_name) == '\0')
133133
continue;
134134

135-
m_vecSections.emplace_back(shdr->sh_size, strTab + shdr->sh_name, static_cast<uintptr_t>(lmap->l_addr + shdr->sh_addr));
135+
m_vecSections.emplace_back(static_cast<std::uintptr_t>(lmap->l_addr + shdr->sh_addr), shdr->sh_size, strTab + shdr->sh_name);
136136
}
137137

138138
munmap(map, st.st_size);

src/windows/module.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ bool CModule::LoadFromPath(const std::string_view svModelePath, int flags)
116116
}
117117

118118
IMAGE_DOS_HEADER* pDOSHeader = reinterpret_cast<IMAGE_DOS_HEADER*>(handle);
119-
IMAGE_NT_HEADERS64* pNTHeaders = reinterpret_cast<IMAGE_NT_HEADERS64*>(reinterpret_cast<uintptr_t>(handle) + pDOSHeader->e_lfanew);
119+
IMAGE_NT_HEADERS64* pNTHeaders = reinterpret_cast<IMAGE_NT_HEADERS64*>(reinterpret_cast<std::uintptr_t>(handle) + pDOSHeader->e_lfanew);
120120

121121
const IMAGE_SECTION_HEADER* hSection = IMAGE_FIRST_SECTION(pNTHeaders); // Get first image section.
122122

123123
for (WORD i = 0; i < pNTHeaders->FileHeader.NumberOfSections; ++i) // Loop through the sections.
124124
{
125125
const IMAGE_SECTION_HEADER& hCurrentSection = hSection[i]; // Get current section.
126-
m_vecSections.emplace_back(hCurrentSection.SizeOfRawData, reinterpret_cast<const char*>(hCurrentSection.Name), static_cast<uintptr_t>(reinterpret_cast<uintptr_t>(handle) + hCurrentSection.VirtualAddress)); // Push back a struct with the section data.
126+
m_vecSections.emplace_back(static_cast<std::uintptr_t>(reinterpret_cast<std::uintptr_t>(handle) + hCurrentSection.VirtualAddress), hCurrentSection.SizeOfRawData, reinterpret_cast<const char*>(hCurrentSection.Name)); // Push back a struct with the section data.
127127
}
128128

129129
SetPtr(static_cast<void *>(handle));
@@ -158,7 +158,7 @@ CMemory CModule::GetVirtualTableByName(const std::string_view svTableName, bool
158158
return DYNLIB_INVALID_MEMORY;
159159

160160
CMemory rttiTypeDescriptor = typeDescriptorName.Offset(-0x10);
161-
uintptr_t rttiTDRva = rttiTypeDescriptor.GetAddr() - GetBase().GetAddr(); // The RTTI gets referenced by a 4-Byte RVA address. We need to scan for that address.
161+
std::uintptr_t rttiTDRva = rttiTypeDescriptor.GetAddr() - GetBase().GetAddr(); // The RTTI gets referenced by a 4-Byte RVA address. We need to scan for that address.
162162

163163
CMemory reference;
164164
while ((reference = FindPattern(&rttiTDRva, "xxxx", reference, pReadOnlyData))) // Get reference typeinfo in vtable

0 commit comments

Comments
 (0)