diff options
| author | 2014-06-16 23:18:10 -0400 | |
|---|---|---|
| committer | 2014-06-16 23:43:33 -0400 | |
| commit | 13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600 (patch) | |
| tree | 5d8cc87a93620948424be3bee61c206279f4dc7a /src/core/loader/elf.cpp | |
| parent | Elf: Renamed modules to be consistent with new loader naming, fixed tabs -> s... (diff) | |
| download | yuzu-13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600.tar.gz yuzu-13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600.tar.xz yuzu-13bdaa6c609a8718d4ce6ca3ce5f1e16f4d7c600.zip | |
Loader: Cleaned up and removed unused code, refactored ELF namespace.
Diffstat (limited to 'src/core/loader/elf.cpp')
| -rw-r--r-- | src/core/loader/elf.cpp | 107 |
1 files changed, 51 insertions, 56 deletions
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp index 153c30f51..f93354817 100644 --- a/src/core/loader/elf.cpp +++ b/src/core/loader/elf.cpp | |||
| @@ -5,65 +5,17 @@ | |||
| 5 | #include <string> | 5 | #include <string> |
| 6 | 6 | ||
| 7 | #include "common/common.h" | 7 | #include "common/common.h" |
| 8 | 8 | #include "common/file_util.h" | |
| 9 | #include "common/symbols.h" | 9 | #include "common/symbols.h" |
| 10 | |||
| 10 | #include "core/mem_map.h" | 11 | #include "core/mem_map.h" |
| 11 | #include "core/loader/elf.h" | 12 | #include "core/loader/elf.h" |
| 13 | #include "core/hle/kernel/kernel.h" | ||
| 12 | 14 | ||
| 13 | //void bswap(Elf32_Word &w) {w = Common::swap32(w);} | 15 | ElfReader::ElfReader(void *ptr) { |
| 14 | //void bswap(Elf32_Half &w) {w = Common::swap16(w);} | ||
| 15 | |||
| 16 | #define bswap(w) w // Dirty bswap disable for now... 3DS is little endian, anyway | ||
| 17 | |||
| 18 | static void byteswapHeader(Elf32_Ehdr &ELF_H) | ||
| 19 | { | ||
| 20 | bswap(ELF_H.e_type); | ||
| 21 | bswap(ELF_H.e_machine); | ||
| 22 | bswap(ELF_H.e_ehsize); | ||
| 23 | bswap(ELF_H.e_phentsize); | ||
| 24 | bswap(ELF_H.e_phnum); | ||
| 25 | bswap(ELF_H.e_shentsize); | ||
| 26 | bswap(ELF_H.e_shnum); | ||
| 27 | bswap(ELF_H.e_shstrndx); | ||
| 28 | bswap(ELF_H.e_version); | ||
| 29 | bswap(ELF_H.e_entry); | ||
| 30 | bswap(ELF_H.e_phoff); | ||
| 31 | bswap(ELF_H.e_shoff); | ||
| 32 | bswap(ELF_H.e_flags); | ||
| 33 | } | ||
| 34 | |||
| 35 | static void byteswapSegment(Elf32_Phdr &sec) | ||
| 36 | { | ||
| 37 | bswap(sec.p_align); | ||
| 38 | bswap(sec.p_filesz); | ||
| 39 | bswap(sec.p_flags); | ||
| 40 | bswap(sec.p_memsz); | ||
| 41 | bswap(sec.p_offset); | ||
| 42 | bswap(sec.p_paddr); | ||
| 43 | bswap(sec.p_vaddr); | ||
| 44 | bswap(sec.p_type); | ||
| 45 | } | ||
| 46 | |||
| 47 | static void byteswapSection(Elf32_Shdr &sec) | ||
| 48 | { | ||
| 49 | bswap(sec.sh_addr); | ||
| 50 | bswap(sec.sh_addralign); | ||
| 51 | bswap(sec.sh_entsize); | ||
| 52 | bswap(sec.sh_flags); | ||
| 53 | bswap(sec.sh_info); | ||
| 54 | bswap(sec.sh_link); | ||
| 55 | bswap(sec.sh_name); | ||
| 56 | bswap(sec.sh_offset); | ||
| 57 | bswap(sec.sh_size); | ||
| 58 | bswap(sec.sh_type); | ||
| 59 | } | ||
| 60 | |||
| 61 | ElfReader::ElfReader(void *ptr) | ||
| 62 | { | ||
| 63 | base = (char*)ptr; | 16 | base = (char*)ptr; |
| 64 | base32 = (u32 *)ptr; | 17 | base32 = (u32 *)ptr; |
| 65 | header = (Elf32_Ehdr*)ptr; | 18 | header = (Elf32_Ehdr*)ptr; |
| 66 | byteswapHeader(*header); | ||
| 67 | 19 | ||
| 68 | segments = (Elf32_Phdr *)(base + header->e_phoff); | 20 | segments = (Elf32_Phdr *)(base + header->e_phoff); |
| 69 | sections = (Elf32_Shdr *)(base + header->e_shoff); | 21 | sections = (Elf32_Shdr *)(base + header->e_shoff); |
| @@ -73,8 +25,7 @@ ElfReader::ElfReader(void *ptr) | |||
| 73 | LoadSymbols(); | 25 | LoadSymbols(); |
| 74 | } | 26 | } |
| 75 | 27 | ||
| 76 | const char *ElfReader::GetSectionName(int section) const | 28 | const char *ElfReader::GetSectionName(int section) const { |
| 77 | { | ||
| 78 | if (sections[section].sh_type == SHT_NULL) | 29 | if (sections[section].sh_type == SHT_NULL) |
| 79 | return nullptr; | 30 | return nullptr; |
| 80 | 31 | ||
| @@ -87,8 +38,7 @@ const char *ElfReader::GetSectionName(int section) const | |||
| 87 | return nullptr; | 38 | return nullptr; |
| 88 | } | 39 | } |
| 89 | 40 | ||
| 90 | bool ElfReader::LoadInto(u32 vaddr) | 41 | bool ElfReader::LoadInto(u32 vaddr) { |
| 91 | { | ||
| 92 | DEBUG_LOG(MASTER_LOG, "String section: %i", header->e_shstrndx); | 42 | DEBUG_LOG(MASTER_LOG, "String section: %i", header->e_shstrndx); |
| 93 | 43 | ||
| 94 | // Should we relocate? | 44 | // Should we relocate? |
| @@ -188,3 +138,48 @@ bool ElfReader::LoadSymbols() | |||
| 188 | 138 | ||
| 189 | return hasSymbols; | 139 | return hasSymbols; |
| 190 | } | 140 | } |
| 141 | |||
| 142 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 143 | // Loader namespace | ||
| 144 | |||
| 145 | namespace Loader { | ||
| 146 | |||
| 147 | /** | ||
| 148 | * Loads an ELF file | ||
| 149 | * @param filename String filename of ELF file | ||
| 150 | * @param error_string Pointer to string to put error message if an error has occurred | ||
| 151 | * @return True on success, otherwise false | ||
| 152 | */ | ||
| 153 | bool Load_ELF(std::string& filename, std::string* error_string) { | ||
| 154 | std::string full_path = filename; | ||
| 155 | std::string path, file, extension; | ||
| 156 | SplitPath(ReplaceAll(full_path, "\\", "/"), &path, &file, &extension); | ||
| 157 | #if EMU_PLATFORM == PLATFORM_WINDOWS | ||
| 158 | path = ReplaceAll(path, "/", "\\"); | ||
| 159 | #endif | ||
| 160 | File::IOFile f(filename, "rb"); | ||
| 161 | |||
| 162 | if (f.IsOpen()) { | ||
| 163 | u32 size = (u32)f.GetSize(); | ||
| 164 | u8* buffer = new u8[size]; | ||
| 165 | ElfReader* elf_reader = NULL; | ||
| 166 | |||
| 167 | f.ReadBytes(buffer, size); | ||
| 168 | |||
| 169 | elf_reader = new ElfReader(buffer); | ||
| 170 | elf_reader->LoadInto(0x00100000); | ||
| 171 | |||
| 172 | Kernel::LoadExec(elf_reader->GetEntryPoint()); | ||
| 173 | |||
| 174 | delete[] buffer; | ||
| 175 | delete elf_reader; | ||
| 176 | } else { | ||
| 177 | *error_string = "Unable to open ELF file!"; | ||
| 178 | return false; | ||
| 179 | } | ||
| 180 | f.Close(); | ||
| 181 | |||
| 182 | return true; | ||
| 183 | } | ||
| 184 | |||
| 185 | } // namespace Loader | ||