2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (c) 2012- PPSSPP Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official git repository and contact information can be found at
|
|
|
|
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2018-01-03 05:53:13 +00:00
|
|
|
#include <vector>
|
2013-12-30 09:17:11 +00:00
|
|
|
#include "Common/CommonTypes.h"
|
2018-01-03 05:53:13 +00:00
|
|
|
#include "Core/ELF/ElfTypes.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-06-01 16:29:38 +00:00
|
|
|
enum {
|
|
|
|
R_MIPS_NONE,
|
|
|
|
R_MIPS_16,
|
|
|
|
R_MIPS_32,
|
|
|
|
R_MIPS_REL32,
|
|
|
|
R_MIPS_26,
|
|
|
|
R_MIPS_HI16,
|
|
|
|
R_MIPS_LO16,
|
|
|
|
R_MIPS_GPREL16,
|
|
|
|
R_MIPS_LITERAL,
|
|
|
|
R_MIPS_GOT16,
|
|
|
|
R_MIPS_PC16,
|
|
|
|
R_MIPS_CALL16,
|
|
|
|
R_MIPS_GPREL32
|
|
|
|
};
|
|
|
|
|
2017-06-06 21:41:23 +00:00
|
|
|
enum KnownElfTypes {
|
2012-11-01 15:19:01 +00:00
|
|
|
KNOWNELF_PSP = 0,
|
|
|
|
KNOWNELF_DS = 1,
|
|
|
|
KNOWNELF_GBA = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int SectionID;
|
|
|
|
|
2017-06-06 21:41:23 +00:00
|
|
|
class ElfReader {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
2017-06-06 21:41:23 +00:00
|
|
|
ElfReader(const void *ptr, size_t size) {
|
2017-05-24 08:51:45 +00:00
|
|
|
base = (const char*)ptr;
|
|
|
|
base32 = (const u32 *)ptr;
|
|
|
|
header = (const Elf32_Ehdr*)ptr;
|
|
|
|
segments = (const Elf32_Phdr *)(base + header->e_phoff);
|
|
|
|
sections = (const Elf32_Shdr *)(base + header->e_shoff);
|
2017-06-06 21:41:23 +00:00
|
|
|
size_ = size;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-01-13 15:46:45 +00:00
|
|
|
~ElfReader() {
|
2017-06-06 21:41:23 +00:00
|
|
|
delete[] sectionOffsets;
|
|
|
|
delete[] sectionAddrs;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 21:41:23 +00:00
|
|
|
u32 Read32(int off) const {
|
|
|
|
return base32[off >> 2];
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Quick accessors
|
2014-12-03 20:16:11 +00:00
|
|
|
ElfType GetType() const { return (ElfType)(u16)(header->e_type); }
|
|
|
|
ElfMachine GetMachine() const { return (ElfMachine)(u16)(header->e_machine); }
|
|
|
|
u32 GetEntryPoint() const { return entryPoint; }
|
|
|
|
u32 GetFlags() const { return (u32)(header->e_flags); }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-07-14 02:53:00 +00:00
|
|
|
int GetNumSegments() const { return (int)(header->e_phnum); }
|
|
|
|
int GetNumSections() const { return (int)(header->e_shnum); }
|
2014-07-20 19:09:58 +00:00
|
|
|
const char *GetSectionName(int section) const;
|
2017-05-24 08:51:45 +00:00
|
|
|
const u8 *GetPtr(u32 offset) const {
|
|
|
|
return (const u8*)base + offset;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2022-10-10 11:29:09 +00:00
|
|
|
// Note: zero is not a valid output, means unavailable.
|
|
|
|
u32 GetSectionDataOffset(int section) const {
|
2022-10-10 09:42:24 +00:00
|
|
|
if (section < 0 || section >= header->e_shnum)
|
2022-10-10 11:29:09 +00:00
|
|
|
return 0;
|
|
|
|
if (sections[section].sh_type == SHT_NOBITS)
|
|
|
|
return 0;
|
|
|
|
return sections[section].sh_offset;
|
|
|
|
}
|
|
|
|
const u8 *GetSectionDataPtr(int section) const {
|
|
|
|
u32 offset = GetSectionDataOffset(section);
|
|
|
|
if (offset == 0 || offset > size_)
|
2022-10-10 09:42:24 +00:00
|
|
|
return nullptr;
|
2022-10-10 11:29:09 +00:00
|
|
|
return GetPtr(offset);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
2017-05-24 08:51:45 +00:00
|
|
|
const u8 *GetSegmentPtr(int segment) const {
|
2022-10-10 11:29:09 +00:00
|
|
|
if (segments[segment].p_offset > size_)
|
|
|
|
return nullptr;
|
2012-11-01 15:19:01 +00:00
|
|
|
return GetPtr(segments[segment].p_offset);
|
|
|
|
}
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetSectionAddr(SectionID section) const {
|
|
|
|
return sectionAddrs[section];
|
|
|
|
}
|
|
|
|
int GetSectionSize(SectionID section) const {
|
2012-11-01 15:19:01 +00:00
|
|
|
return sections[section].sh_size;
|
|
|
|
}
|
2017-06-06 21:41:23 +00:00
|
|
|
|
|
|
|
//-1 for not found
|
|
|
|
SectionID GetSectionByName(const char *name, int firstSection = 0) const;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetSegmentPaddr(int segment) const {
|
2017-06-06 21:41:23 +00:00
|
|
|
return segments[segment].p_paddr;
|
2012-11-04 18:01:20 +00:00
|
|
|
}
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetSegmentOffset(int segment) const {
|
2017-06-06 21:41:23 +00:00
|
|
|
return segments[segment].p_offset;
|
2012-11-04 18:01:20 +00:00
|
|
|
}
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetSegmentVaddr(int segment) const {
|
2012-12-28 00:05:03 +00:00
|
|
|
return segmentVAddr[segment];
|
|
|
|
}
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetSegmentDataSize(int segment) const {
|
2013-12-09 08:45:54 +00:00
|
|
|
return segments[segment].p_filesz;
|
|
|
|
}
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetSegmentMemSize(int segment) const {
|
|
|
|
return segments[segment].p_memsz;
|
|
|
|
}
|
2012-11-04 18:01:20 +00:00
|
|
|
|
2020-10-27 12:11:08 +00:00
|
|
|
u32 GetFirstSegmentAlign() const {
|
|
|
|
return firstSegAlign;
|
|
|
|
}
|
|
|
|
|
2014-07-14 01:23:30 +00:00
|
|
|
bool DidRelocate() const {
|
2012-11-01 15:19:01 +00:00
|
|
|
return bRelocate;
|
|
|
|
}
|
2012-11-05 15:54:35 +00:00
|
|
|
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetVaddr() const {
|
2012-11-05 15:54:35 +00:00
|
|
|
return vaddr;
|
|
|
|
}
|
|
|
|
|
2014-07-14 01:23:30 +00:00
|
|
|
u32 GetTotalSize() const {
|
2013-04-11 04:03:43 +00:00
|
|
|
return totalSize;
|
|
|
|
}
|
|
|
|
|
2014-07-14 02:53:00 +00:00
|
|
|
u32 GetTotalTextSize() const;
|
2020-10-27 12:11:08 +00:00
|
|
|
u32 GetTotalTextSizeFromSeg() const;
|
2014-07-14 02:53:00 +00:00
|
|
|
u32 GetTotalDataSize() const;
|
2014-07-20 19:09:58 +00:00
|
|
|
u32 GetTotalSectionSizeByPrefix(const std::string &prefix) const;
|
2014-07-14 02:53:00 +00:00
|
|
|
|
2018-01-03 05:53:13 +00:00
|
|
|
std::vector<SectionID> GetCodeSections() const;
|
|
|
|
|
2014-07-15 07:25:16 +00:00
|
|
|
int LoadInto(u32 vaddr, bool fromTop);
|
2012-11-01 15:19:01 +00:00
|
|
|
bool LoadSymbols();
|
2017-05-24 08:51:45 +00:00
|
|
|
bool LoadRelocations(const Elf32_Rel *rels, int numRelocs);
|
2013-05-31 10:34:47 +00:00
|
|
|
void LoadRelocations2(int rel_seg);
|
2013-01-13 15:46:45 +00:00
|
|
|
|
|
|
|
private:
|
2017-06-06 21:41:23 +00:00
|
|
|
const char *base = nullptr;
|
|
|
|
const u32 *base32 = nullptr;
|
|
|
|
const Elf32_Ehdr *header = nullptr;
|
|
|
|
const Elf32_Phdr *segments = nullptr;
|
|
|
|
const Elf32_Shdr *sections = nullptr;
|
|
|
|
u32 *sectionOffsets = nullptr;
|
|
|
|
u32 *sectionAddrs = nullptr;
|
|
|
|
bool bRelocate = false;
|
|
|
|
u32 entryPoint = 0;
|
|
|
|
u32 totalSize = 0;
|
|
|
|
u32 vaddr = 0;
|
2022-10-10 10:22:05 +00:00
|
|
|
u32 segmentVAddr[32]{};
|
2017-06-06 21:41:23 +00:00
|
|
|
size_t size_ = 0;
|
2020-10-27 12:11:08 +00:00
|
|
|
u32 firstSegAlign = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|