Downgrade projects to C++17.

This commit is contained in:
Skyth
2024-12-13 18:31:55 +03:00
parent 02d23b3463
commit 847842cd28
20 changed files with 250 additions and 189 deletions

View File

@@ -1,5 +1,5 @@
project("PowerUtils")
add_library(PowerUtils "disasm.h" "disasm.cpp" "file.h" "xex.cpp" "image.h" "image.cpp" "elf.h" "ppc_context.h" "symbol.h" "symbol_table.h" "section.h" "xdbf_wrapper.cpp")
add_library(PowerUtils "disasm.h" "disasm.cpp" "file.h" "xex.cpp" "image.h" "image.cpp" "elf.h" "ppc_context.h" "symbol.h" "symbol_table.h" "section.h" "xdbf_wrapper.cpp" "byteswap.h")
target_include_directories(PowerUtils PUBLIC .)
target_link_libraries(PowerUtils PUBLIC disasm)

25
PowerUtils/byteswap.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <cassert>
template<typename T>
inline T ByteSwap(T value)
{
if constexpr (sizeof(T) == 1)
return value;
else if constexpr (sizeof(T) == 2)
return static_cast<T>(__builtin_bswap16(static_cast<uint16_t>(value)));
else if constexpr (sizeof(T) == 4)
return static_cast<T>(__builtin_bswap32(static_cast<uint32_t>(value)));
else if constexpr (sizeof(T) == 8)
return static_cast<T>(__builtin_bswap64(static_cast<uint64_t>(value)));
assert(false && "Unexpected byte size.");
return value;
}
template<typename T>
inline void ByteSwapInplace(T& value)
{
value = ByteSwap(value);
}

View File

@@ -1,14 +1,14 @@
#pragma once
#include <expected>
#include <vector>
inline static std::expected<std::vector<uint8_t>, int> LoadFile(const char* path)
inline std::vector<uint8_t> LoadFile(const char* path)
{
std::vector<uint8_t> data{};
auto* stream = fopen(path, "rb");
if (stream == nullptr)
{
return std::unexpected(1);
return {};
}
fseek(stream, 0, SEEK_END);

View File

@@ -5,8 +5,8 @@
void Image::Map(const std::string_view& name, size_t base, uint32_t size, uint8_t flags, uint8_t* data)
{
sections.emplace(std::string(name), this->base + base,
size, static_cast<SectionFlags>(flags), data);
sections.insert({ std::string(name), this->base + base,
size, static_cast<SectionFlags>(flags), data });
}
const void* Image::Find(size_t address) const
@@ -28,7 +28,7 @@ const Section* Image::Find(const std::string_view& name) const
return nullptr;
}
std::expected<Image, int> Image::ParseImage(const uint8_t* data, size_t size)
Image Image::ParseImage(const uint8_t* data, size_t size)
{
if (data[0] == ELFMAG0 && data[1] == ELFMAG1 && data[2] == ELFMAG2 && data[3] == ELFMAG3)
{
@@ -39,7 +39,7 @@ std::expected<Image, int> Image::ParseImage(const uint8_t* data, size_t size)
return Xex2LoadImage(data);
}
return std::unexpected(1);
return {};
}
Image ElfLoadImage(const uint8_t* data, size_t size)
@@ -50,27 +50,27 @@ Image ElfLoadImage(const uint8_t* data, size_t size)
Image image{};
image.size = size;
image.data = std::make_unique<uint8_t[]>(size);
image.entry_point = std::byteswap(header->e_entry);
image.entry_point = ByteSwap(header->e_entry);
memcpy(image.data.get(), data, size);
auto stringTableIndex = std::byteswap(header->e_shstrndx);
auto stringTableIndex = ByteSwap(header->e_shstrndx);
const auto numSections = std::byteswap(header->e_shnum);
const auto numpSections = std::byteswap(header->e_phnum);
const auto numSections = ByteSwap(header->e_shnum);
const auto numpSections = ByteSwap(header->e_phnum);
const auto* sections = (elf32_shdr*)(data + std::byteswap(header->e_shoff));
const auto* psections = (elf32_phdr*)(data + std::byteswap(header->e_phoff));
const auto* sections = (elf32_shdr*)(data + ByteSwap(header->e_shoff));
const auto* psections = (elf32_phdr*)(data + ByteSwap(header->e_phoff));
for (size_t i = 0; i < numpSections; i++)
{
if (psections[i].p_type == std::byteswap((Elf32_Word)PT_LOAD))
if (psections[i].p_type == ByteSwap((Elf32_Word)PT_LOAD))
{
image.base = std::byteswap(psections[i].p_vaddr);
image.base = ByteSwap(psections[i].p_vaddr);
break;
}
}
auto* stringTable = reinterpret_cast<const char*>(data + std::byteswap(sections[stringTableIndex].sh_offset));
auto* stringTable = reinterpret_cast<const char*>(data + ByteSwap(sections[stringTableIndex].sh_offset));
for (size_t i = 0; i < numSections; i++)
{
@@ -82,16 +82,16 @@ Image ElfLoadImage(const uint8_t* data, size_t size)
uint8_t flags{};
if (section.sh_flags & std::byteswap(SHF_EXECINSTR))
if (section.sh_flags & ByteSwap(SHF_EXECINSTR))
{
flags |= SectionFlags_Code;
}
auto* name = section.sh_name != 0 ? stringTable + std::byteswap(section.sh_name) : nullptr;
const auto rva = std::byteswap(section.sh_addr) - image.base;
const auto size = std::byteswap(section.sh_size);
auto* name = section.sh_name != 0 ? stringTable + ByteSwap(section.sh_name) : nullptr;
const auto rva = ByteSwap(section.sh_addr) - image.base;
const auto size = ByteSwap(section.sh_size);
image.Map(name, rva, size, flags, image.data.get() + std::byteswap(section.sh_offset));
image.Map(name, rva, size, flags, image.data.get() + ByteSwap(section.sh_offset));
}
return image;

View File

@@ -44,7 +44,7 @@ struct Image
* \param size Size of data
* \return Parsed image
*/
static std::expected<Image, int> ParseImage(const uint8_t* data, size_t size);
static Image ParseImage(const uint8_t* data, size_t size);
};
Image ElfLoadImage(const uint8_t* data, size_t size);

View File

@@ -13,9 +13,18 @@ enum SymbolType
struct Symbol
{
mutable std::string name{};
uint32_t address{};
uint32_t size{};
size_t address{};
size_t size{};
mutable SymbolType type{};
Symbol()
{
}
Symbol(std::string name, size_t address, size_t size, SymbolType type)
: name(std::move(name)), address(address), size(size), type(type)
{
}
};
struct SymbolComparer

View File

@@ -3,7 +3,7 @@
#include <type_traits>
#include <bit>
#include <string>
#include "byteswap.h"
#ifdef _WIN32
#include <windows.h>
@@ -81,22 +81,22 @@ struct be
{
if constexpr (std::is_same_v<T, double>)
{
const uint64_t swapped = std::byteswap(*reinterpret_cast<uint64_t*>(&value));
const uint64_t swapped = ByteSwap(*reinterpret_cast<uint64_t*>(&value));
return *reinterpret_cast<const T*>(&swapped);
}
else if constexpr (std::is_same_v<T, float>)
{
const uint32_t swapped = std::byteswap(*reinterpret_cast<uint32_t*>(&value));
const uint32_t swapped = ByteSwap(*reinterpret_cast<uint32_t*>(&value));
return *reinterpret_cast<const T*>(&swapped);
}
else if constexpr (std::is_enum_v<T>)
{
const std::underlying_type_t<T> swapped = std::byteswap(*reinterpret_cast<std::underlying_type_t<T>*>(&value));
const std::underlying_type_t<T> swapped = ByteSwap(*reinterpret_cast<std::underlying_type_t<T>*>(&value));
return *reinterpret_cast<const T*>(&swapped);
}
else
{
return std::byteswap(value);
return ByteSwap(value);
}
}

View File

@@ -126,7 +126,7 @@ Image Xex2LoadImage(const uint8_t* data)
{
auto originalThunk = (XEX_THUNK_DATA*)image.Find(descriptors[im].FirstThunk);
auto originalData = originalThunk;
originalData->Data = std::byteswap(originalData->Data);
originalData->Data = ByteSwap(originalData->Data);
if (originalData->OriginalData.Type != 0)
{
@@ -134,7 +134,7 @@ Image Xex2LoadImage(const uint8_t* data)
auto name = names->find(originalData->OriginalData.Ordinal);
if (name != names->end())
{
image.symbols.emplace(name->second, descriptors[im].FirstThunk, sizeof(thunk), Symbol_Function);
image.symbols.insert({ name->second, descriptors[im].FirstThunk, sizeof(thunk), Symbol_Function });
}
memcpy(originalThunk, thunk, sizeof(thunk));