mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-18 20:11:16 +01:00
f7bc75ae6d
This allows avoiding two copies of the executable data being created in the following scenario (using pseudocode): some_function() { std::vector<u8> data = ...; DolReader reader{data}; ... } In this scenario, if we only use the data for passing it to DolReader, then we have to perform a copy, as the constructor takes the std::vector as a constant reference -- you cannot move from a constant reference, and so we copy data into the DolReader, and perform another copy in the constructor itself when assigning the data to the m_bytes member variable. However, we can do better. Now, the following is allowable as well: some_function() { std::vector<u8> data = ...; DolReader reader{std::move(data)}; ... } and now we perform no copy at any point in the reader's construction, as we just std::move the data all the way through to m_bytes. In the case where we *do* want to keep the executable data around after constructing the reader, then we can just pass the vector without std::move-ing it, and we only perform a copy once (as we'll std::move said copy into m_bytes). Therefore, we get a more flexible interface resource-wise out of it.
81 lines
2.4 KiB
C++
81 lines
2.4 KiB
C++
// Copyright 2008 Dolphin Emulator Project
|
|
// Licensed under GPLv2+
|
|
// Refer to the license.txt file included.
|
|
|
|
#pragma once
|
|
|
|
#include "Common/CommonTypes.h"
|
|
#include "Core/Boot/Boot.h"
|
|
#include "Core/Boot/ElfTypes.h"
|
|
|
|
namespace File
|
|
{
|
|
class IOFile;
|
|
}
|
|
|
|
enum KnownElfTypes
|
|
{
|
|
KNOWNELF_PSP = 0,
|
|
KNOWNELF_DS = 1,
|
|
KNOWNELF_GBA = 2,
|
|
KNOWNELF_GC = 3,
|
|
};
|
|
|
|
typedef int SectionID;
|
|
|
|
class ElfReader final : public BootExecutableReader
|
|
{
|
|
public:
|
|
explicit ElfReader(const std::string& filename);
|
|
explicit ElfReader(File::IOFile file);
|
|
explicit ElfReader(std::vector<u8> buffer);
|
|
~ElfReader();
|
|
u32 Read32(int off) const { return base32[off >> 2]; }
|
|
// Quick accessors
|
|
ElfType GetType() const { return (ElfType)(header->e_type); }
|
|
ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); }
|
|
u32 GetEntryPoint() const override { return entryPoint; }
|
|
u32 GetFlags() const { return (u32)(header->e_flags); }
|
|
bool LoadIntoMemory(bool only_in_mem1 = false) const override;
|
|
bool LoadSymbols() const override;
|
|
// TODO: actually check for validity.
|
|
bool IsValid() const override { return true; }
|
|
bool IsWii() const override;
|
|
|
|
int GetNumSegments() const { return (int)(header->e_phnum); }
|
|
int GetNumSections() const { return (int)(header->e_shnum); }
|
|
const u8* GetPtr(int offset) const { return (u8*)base + offset; }
|
|
const char* GetSectionName(int section) const;
|
|
const u8* GetSectionDataPtr(int section) const
|
|
{
|
|
if (section < 0 || section >= header->e_shnum)
|
|
return nullptr;
|
|
if (sections[section].sh_type != SHT_NOBITS)
|
|
return GetPtr(sections[section].sh_offset);
|
|
else
|
|
return nullptr;
|
|
}
|
|
bool IsCodeSegment(int segment) const { return segments[segment].p_flags & PF_X; }
|
|
const u8* GetSegmentPtr(int segment) const { return GetPtr(segments[segment].p_offset); }
|
|
int GetSegmentSize(int segment) const { return segments[segment].p_filesz; }
|
|
u32 GetSectionAddr(SectionID section) const { return sectionAddrs[section]; }
|
|
int GetSectionSize(SectionID section) const { return sections[section].sh_size; }
|
|
SectionID GetSectionByName(const char* name, int firstSection = 0) const; //-1 for not found
|
|
|
|
bool DidRelocate() const { return bRelocate; }
|
|
|
|
private:
|
|
void Initialize(u8* bytes);
|
|
|
|
char* base;
|
|
u32* base32;
|
|
|
|
Elf32_Ehdr* header;
|
|
Elf32_Phdr* segments;
|
|
Elf32_Shdr* sections;
|
|
|
|
u32* sectionAddrs;
|
|
bool bRelocate;
|
|
u32 entryPoint;
|
|
};
|