00001 // 00002 // Copyright (c) 2005-2008 Kenichi Watanabe. 00003 // Copyright (c) 2005-2008 Yasuhiro Watari. 00004 // Copyright (c) 2005-2008 Hironori Ichibayashi. 00005 // Copyright (c) 2008-2009 Kazuo Horio. 00006 // Copyright (c) 2009-2013 Naruki Kurata. 00007 // Copyright (c) 2005-2013 Ryota Shioya. 00008 // Copyright (c) 2005-2013 Masahiro Goshima. 00009 // 00010 // This software is provided 'as-is', without any express or implied 00011 // warranty. In no event will the authors be held liable for any damages 00012 // arising from the use of this software. 00013 // 00014 // Permission is granted to anyone to use this software for any purpose, 00015 // including commercial applications, and to alter it and redistribute it 00016 // freely, subject to the following restrictions: 00017 // 00018 // 1. The origin of this software must not be misrepresented; you must not 00019 // claim that you wrote the original software. If you use this software 00020 // in a product, an acknowledgment in the product documentation would be 00021 // appreciated but is not required. 00022 // 00023 // 2. Altered source versions must be plainly marked as such, and must not be 00024 // misrepresented as being the original software. 00025 // 00026 // 3. This notice may not be removed or altered from any source 00027 // distribution. 00028 // 00029 // 00030 00031 00032 #ifndef __EMULATORUTILITY_ELFREADER_H__ 00033 #define __EMULATORUTILITY_ELFREADER_H__ 00034 00035 #include <fstream> 00036 #include <vector> 00037 00038 #include "SysDeps/Endian.h" 00039 #include "Emu/Utility/System/Loader/ELF64.h" 00040 00041 namespace Onikiri { 00042 namespace EmulatorUtility { 00043 00044 // Elf64 reader 00045 class ElfReader 00046 { 00047 public: 00048 typedef ELF64_HEADER Elf_Ehdr; 00049 typedef ELF64_SECTION Elf_Shdr; 00050 typedef ELF64_PROGRAM Elf_Phdr; 00051 typedef u64 Elf_Addr; 00052 typedef u32 Elf_Word; 00053 typedef u64 Elf_Off; 00054 00055 typedef std::streamsize streamsize; 00056 00057 explicit ElfReader(); 00058 ~ElfReader(); 00059 00060 // ELFt@CJCwb_ 00061 // クs runtime_error 00062 void Open(const char *name); 00063 void Close(); 00064 00065 int GetClass() const; 00066 int GetDataEncoding() const; 00067 int GetVersion() const; 00068 u16 GetMachine() const; 00069 bool IsBigEndian() const; 00070 00071 int GetSectionHeaderCount() const; 00072 int GetProgramHeaderCount() const; 00073 00074 Elf_Addr GetEntryPoint() const; 00075 const Elf_Shdr &GetSectionHeader(int index) const; 00076 const Elf_Phdr &GetProgramHeader(int index) const; 00077 00078 // name ZNVindex 00079 // -1 00080 int FindSection(const char *name) const; 00081 // ZNVindexZNV 00082 const char *GetSectionName(int index) const; 00083 00084 // ZNVindexebuf (クs runtime_error ) 00085 void ReadSectionBody(int index, char *buf, size_t buf_size) const; 00086 // offset buf_size (クs runtime_error ) 00087 void ReadRange(size_t offset, char *buf, size_t buf_size) const; 00088 00089 // ELFt@CTCY 00090 streamsize GetImageSize() const; 00091 // ELFt@CS (クs runtime_error ) 00092 void ReadImage(char *buf, size_t buf_size) const; 00093 00094 Elf_Off GetSectionHeaderOffset() const; 00095 Elf_Off GetProgramHeaderOffset() const; 00096 00097 private: 00098 // ELFwb_Dmachine: machine (クs runtime_error ) 00099 void ReadELFHeader(); 00100 // ZNVwb_ (クs runtime_error ) 00101 void ReadSectionHeaders(); 00102 // vOwb_ (クs runtime_error ) 00103 void ReadProgramHeaders(); 00104 // ZNVl[e[u (Elf_Ehdr.e_shstrndxヲZNV) e (クs runtime_error ) 00105 void ReadSectionNameTable(); 00106 00107 bool m_bigEndian; 00108 00109 mutable std::ifstream m_file; 00110 00111 Elf_Ehdr m_elfHeader; 00112 std::vector<Elf_Shdr> m_elfSectionHeaders; 00113 std::vector<Elf_Phdr> m_elfProgramHeaders; 00114 char *m_sectionNameTable; 00115 }; 00116 00117 } // namespace EmulatorUtility 00118 } // namespace Onikiri 00119 00120 #endif // #ifndef ELFREAD_H_INCLUDED 00121