CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Core/ELF/ElfReader.h
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <vector>20#include "Common/CommonTypes.h"21#include "Core/ELF/ElfTypes.h"2223enum {24R_MIPS_NONE,25R_MIPS_16,26R_MIPS_32,27R_MIPS_REL32,28R_MIPS_26,29R_MIPS_HI16,30R_MIPS_LO16,31R_MIPS_GPREL16,32R_MIPS_LITERAL,33R_MIPS_GOT16,34R_MIPS_PC16,35R_MIPS_CALL16,36R_MIPS_GPREL3237};3839enum KnownElfTypes {40KNOWNELF_PSP = 0,41KNOWNELF_DS = 1,42KNOWNELF_GBA = 2,43};4445typedef int SectionID;4647class ElfReader {48public:49ElfReader(const void *ptr, size_t size) {50base = (const char*)ptr;51base32 = (const u32 *)ptr;52header = (const Elf32_Ehdr*)ptr;53segments = (const Elf32_Phdr *)(base + header->e_phoff);54sections = (const Elf32_Shdr *)(base + header->e_shoff);55size_ = size;56}5758~ElfReader() {59delete[] sectionOffsets;60delete[] sectionAddrs;61}6263u32 Read32(int off) const {64return base32[off >> 2];65}6667// Quick accessors68ElfType GetType() const { return (ElfType)(u16)(header->e_type); }69ElfMachine GetMachine() const { return (ElfMachine)(u16)(header->e_machine); }70u32 GetEntryPoint() const { return entryPoint; }71u32 GetFlags() const { return (u32)(header->e_flags); }7273int GetNumSegments() const { return (int)(header->e_phnum); }74int GetNumSections() const { return (int)(header->e_shnum); }75const char *GetSectionName(int section) const;76const u8 *GetPtr(u32 offset) const {77return (const u8*)base + offset;78}79// Note: zero is not a valid output, means unavailable.80u32 GetSectionDataOffset(int section) const {81if (section < 0 || section >= header->e_shnum)82return 0;83if (sections[section].sh_type == SHT_NOBITS)84return 0;85return sections[section].sh_offset;86}87const u8 *GetSectionDataPtr(int section) const {88u32 offset = GetSectionDataOffset(section);89if (offset == 0 || offset > size_)90return nullptr;91return GetPtr(offset);92}93const u8 *GetSegmentPtr(int segment) const {94if (segments[segment].p_offset > size_)95return nullptr;96return GetPtr(segments[segment].p_offset);97}98u32 GetSectionAddr(SectionID section) const {99return sectionAddrs[section];100}101int GetSectionSize(SectionID section) const {102return sections[section].sh_size;103}104105//-1 for not found106SectionID GetSectionByName(const char *name, int firstSection = 0) const;107108u32 GetSegmentPaddr(int segment) const {109return segments[segment].p_paddr;110}111u32 GetSegmentOffset(int segment) const {112return segments[segment].p_offset;113}114u32 GetSegmentVaddr(int segment) const {115return segmentVAddr[segment];116}117u32 GetSegmentDataSize(int segment) const {118return segments[segment].p_filesz;119}120u32 GetSegmentMemSize(int segment) const {121return segments[segment].p_memsz;122}123124u32 GetFirstSegmentAlign() const {125return firstSegAlign;126}127128bool DidRelocate() const {129return bRelocate;130}131132u32 GetVaddr() const {133return vaddr;134}135136u32 GetTotalSize() const {137return totalSize;138}139140u32 GetTotalTextSize() const;141u32 GetTotalTextSizeFromSeg() const;142u32 GetTotalDataSize() const;143u32 GetTotalSectionSizeByPrefix(const std::string &prefix) const;144145std::vector<SectionID> GetCodeSections() const;146147int LoadInto(u32 vaddr, bool fromTop);148bool LoadSymbols();149bool LoadRelocations(const Elf32_Rel *rels, int numRelocs);150void LoadRelocations2(int rel_seg);151152private:153const char *base = nullptr;154const u32 *base32 = nullptr;155const Elf32_Ehdr *header = nullptr;156const Elf32_Phdr *segments = nullptr;157const Elf32_Shdr *sections = nullptr;158u32 *sectionOffsets = nullptr;159u32 *sectionAddrs = nullptr;160bool bRelocate = false;161u32 entryPoint = 0;162u32 totalSize = 0;163u32 vaddr = 0;164u32 segmentVAddr[32]{};165size_t size_ = 0;166u32 firstSegAlign = 0;167};168169170